1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * vsp1_drv.c  --  R-Car VSP1 Driver
4  *
5  * Copyright (C) 2013-2015 Renesas Electronics Corporation
6  *
7  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8  */
9 
10 #include <linux/clk.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/interrupt.h>
14 #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 #include <linux/reset.h>
20 #include <linux/videodev2.h>
21 
22 #include <media/rcar-fcp.h>
23 #include <media/v4l2-subdev.h>
24 
25 #include "vsp1.h"
26 #include "vsp1_brx.h"
27 #include "vsp1_clu.h"
28 #include "vsp1_dl.h"
29 #include "vsp1_drm.h"
30 #include "vsp1_hgo.h"
31 #include "vsp1_hgt.h"
32 #include "vsp1_hsit.h"
33 #include "vsp1_lif.h"
34 #include "vsp1_lut.h"
35 #include "vsp1_pipe.h"
36 #include "vsp1_rwpf.h"
37 #include "vsp1_sru.h"
38 #include "vsp1_uds.h"
39 #include "vsp1_uif.h"
40 #include "vsp1_video.h"
41 
42 /* -----------------------------------------------------------------------------
43  * Interrupt Handling
44  */
45 
46 static irqreturn_t vsp1_irq_handler(int irq, void *data)
47 {
48 	u32 mask = VI6_WPF_IRQ_STA_DFE | VI6_WPF_IRQ_STA_FRE;
49 	struct vsp1_device *vsp1 = data;
50 	irqreturn_t ret = IRQ_NONE;
51 	unsigned int i;
52 	u32 status;
53 
54 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
55 		struct vsp1_rwpf *wpf = vsp1->wpf[i];
56 
57 		if (wpf == NULL)
58 			continue;
59 
60 		status = vsp1_read(vsp1, VI6_WPF_IRQ_STA(i));
61 		vsp1_write(vsp1, VI6_WPF_IRQ_STA(i), ~status & mask);
62 
63 		if (status & VI6_WPF_IRQ_STA_DFE) {
64 			vsp1_pipeline_frame_end(wpf->entity.pipe);
65 			ret = IRQ_HANDLED;
66 		}
67 	}
68 
69 	return ret;
70 }
71 
72 /* -----------------------------------------------------------------------------
73  * Entities
74  */
75 
76 /*
77  * vsp1_create_sink_links - Create links from all sources to the given sink
78  *
79  * This function creates media links from all valid sources to the given sink
80  * pad. Links that would be invalid according to the VSP1 hardware capabilities
81  * are skipped. Those include all links
82  *
83  * - from a UDS to a UDS (UDS entities can't be chained)
84  * - from an entity to itself (no loops are allowed)
85  *
86  * Furthermore, the BRS can't be connected to histogram generators, but no
87  * special check is currently needed as all VSP instances that include a BRS
88  * have no histogram generator.
89  */
90 static int vsp1_create_sink_links(struct vsp1_device *vsp1,
91 				  struct vsp1_entity *sink)
92 {
93 	struct media_entity *entity = &sink->subdev.entity;
94 	struct vsp1_entity *source;
95 	unsigned int pad;
96 	int ret;
97 
98 	list_for_each_entry(source, &vsp1->entities, list_dev) {
99 		u32 flags;
100 
101 		if (source->type == sink->type)
102 			continue;
103 
104 		if (source->type == VSP1_ENTITY_HGO ||
105 		    source->type == VSP1_ENTITY_HGT ||
106 		    source->type == VSP1_ENTITY_LIF ||
107 		    source->type == VSP1_ENTITY_WPF)
108 			continue;
109 
110 		flags = source->type == VSP1_ENTITY_RPF &&
111 			sink->type == VSP1_ENTITY_WPF &&
112 			source->index == sink->index
113 		      ? MEDIA_LNK_FL_ENABLED : 0;
114 
115 		for (pad = 0; pad < entity->num_pads; ++pad) {
116 			if (!(entity->pads[pad].flags & MEDIA_PAD_FL_SINK))
117 				continue;
118 
119 			ret = media_create_pad_link(&source->subdev.entity,
120 						       source->source_pad,
121 						       entity, pad, flags);
122 			if (ret < 0)
123 				return ret;
124 
125 			if (flags & MEDIA_LNK_FL_ENABLED)
126 				source->sink = sink;
127 		}
128 	}
129 
130 	return 0;
131 }
132 
133 static int vsp1_uapi_create_links(struct vsp1_device *vsp1)
134 {
135 	struct vsp1_entity *entity;
136 	unsigned int i;
137 	int ret;
138 
139 	list_for_each_entry(entity, &vsp1->entities, list_dev) {
140 		if (entity->type == VSP1_ENTITY_LIF ||
141 		    entity->type == VSP1_ENTITY_RPF)
142 			continue;
143 
144 		ret = vsp1_create_sink_links(vsp1, entity);
145 		if (ret < 0)
146 			return ret;
147 	}
148 
149 	if (vsp1->hgo) {
150 		ret = media_create_pad_link(&vsp1->hgo->histo.entity.subdev.entity,
151 					    HISTO_PAD_SOURCE,
152 					    &vsp1->hgo->histo.video.entity, 0,
153 					    MEDIA_LNK_FL_ENABLED |
154 					    MEDIA_LNK_FL_IMMUTABLE);
155 		if (ret < 0)
156 			return ret;
157 	}
158 
159 	if (vsp1->hgt) {
160 		ret = media_create_pad_link(&vsp1->hgt->histo.entity.subdev.entity,
161 					    HISTO_PAD_SOURCE,
162 					    &vsp1->hgt->histo.video.entity, 0,
163 					    MEDIA_LNK_FL_ENABLED |
164 					    MEDIA_LNK_FL_IMMUTABLE);
165 		if (ret < 0)
166 			return ret;
167 	}
168 
169 	for (i = 0; i < vsp1->info->lif_count; ++i) {
170 		if (!vsp1->lif[i])
171 			continue;
172 
173 		ret = media_create_pad_link(&vsp1->wpf[i]->entity.subdev.entity,
174 					    RWPF_PAD_SOURCE,
175 					    &vsp1->lif[i]->entity.subdev.entity,
176 					    LIF_PAD_SINK, 0);
177 		if (ret < 0)
178 			return ret;
179 	}
180 
181 	for (i = 0; i < vsp1->info->rpf_count; ++i) {
182 		struct vsp1_rwpf *rpf = vsp1->rpf[i];
183 
184 		ret = media_create_pad_link(&rpf->video->video.entity, 0,
185 					    &rpf->entity.subdev.entity,
186 					    RWPF_PAD_SINK,
187 					    MEDIA_LNK_FL_ENABLED |
188 					    MEDIA_LNK_FL_IMMUTABLE);
189 		if (ret < 0)
190 			return ret;
191 	}
192 
193 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
194 		/*
195 		 * Connect the video device to the WPF. All connections are
196 		 * immutable.
197 		 */
198 		struct vsp1_rwpf *wpf = vsp1->wpf[i];
199 
200 		ret = media_create_pad_link(&wpf->entity.subdev.entity,
201 					    RWPF_PAD_SOURCE,
202 					    &wpf->video->video.entity, 0,
203 					    MEDIA_LNK_FL_IMMUTABLE |
204 					    MEDIA_LNK_FL_ENABLED);
205 		if (ret < 0)
206 			return ret;
207 	}
208 
209 	return 0;
210 }
211 
212 static void vsp1_destroy_entities(struct vsp1_device *vsp1)
213 {
214 	struct vsp1_entity *entity, *_entity;
215 	struct vsp1_video *video, *_video;
216 
217 	list_for_each_entry_safe(entity, _entity, &vsp1->entities, list_dev) {
218 		list_del(&entity->list_dev);
219 		vsp1_entity_destroy(entity);
220 	}
221 
222 	list_for_each_entry_safe(video, _video, &vsp1->videos, list) {
223 		list_del(&video->list);
224 		vsp1_video_cleanup(video);
225 	}
226 
227 	v4l2_device_unregister(&vsp1->v4l2_dev);
228 	if (vsp1->info->uapi)
229 		media_device_unregister(&vsp1->media_dev);
230 	media_device_cleanup(&vsp1->media_dev);
231 
232 	if (!vsp1->info->uapi)
233 		vsp1_drm_cleanup(vsp1);
234 }
235 
236 static int vsp1_create_entities(struct vsp1_device *vsp1)
237 {
238 	struct media_device *mdev = &vsp1->media_dev;
239 	struct v4l2_device *vdev = &vsp1->v4l2_dev;
240 	struct vsp1_entity *entity;
241 	unsigned int i;
242 	int ret;
243 
244 	mdev->dev = vsp1->dev;
245 	mdev->hw_revision = vsp1->version;
246 	strscpy(mdev->model, vsp1->info->model, sizeof(mdev->model));
247 	media_device_init(mdev);
248 
249 	vsp1->media_ops.link_setup = vsp1_entity_link_setup;
250 	/*
251 	 * Don't perform link validation when the userspace API is disabled as
252 	 * the pipeline is configured internally by the driver in that case, and
253 	 * its configuration can thus be trusted.
254 	 */
255 	if (vsp1->info->uapi)
256 		vsp1->media_ops.link_validate = v4l2_subdev_link_validate;
257 
258 	vdev->mdev = mdev;
259 	ret = v4l2_device_register(vsp1->dev, vdev);
260 	if (ret < 0) {
261 		dev_err(vsp1->dev, "V4L2 device registration failed (%d)\n",
262 			ret);
263 		goto done;
264 	}
265 
266 	/* Instantiate all the entities. */
267 	if (vsp1_feature(vsp1, VSP1_HAS_BRS)) {
268 		vsp1->brs = vsp1_brx_create(vsp1, VSP1_ENTITY_BRS);
269 		if (IS_ERR(vsp1->brs)) {
270 			ret = PTR_ERR(vsp1->brs);
271 			goto done;
272 		}
273 
274 		list_add_tail(&vsp1->brs->entity.list_dev, &vsp1->entities);
275 	}
276 
277 	if (vsp1_feature(vsp1, VSP1_HAS_BRU)) {
278 		vsp1->bru = vsp1_brx_create(vsp1, VSP1_ENTITY_BRU);
279 		if (IS_ERR(vsp1->bru)) {
280 			ret = PTR_ERR(vsp1->bru);
281 			goto done;
282 		}
283 
284 		list_add_tail(&vsp1->bru->entity.list_dev, &vsp1->entities);
285 	}
286 
287 	if (vsp1_feature(vsp1, VSP1_HAS_CLU)) {
288 		vsp1->clu = vsp1_clu_create(vsp1);
289 		if (IS_ERR(vsp1->clu)) {
290 			ret = PTR_ERR(vsp1->clu);
291 			goto done;
292 		}
293 
294 		list_add_tail(&vsp1->clu->entity.list_dev, &vsp1->entities);
295 	}
296 
297 	vsp1->hsi = vsp1_hsit_create(vsp1, true);
298 	if (IS_ERR(vsp1->hsi)) {
299 		ret = PTR_ERR(vsp1->hsi);
300 		goto done;
301 	}
302 
303 	list_add_tail(&vsp1->hsi->entity.list_dev, &vsp1->entities);
304 
305 	vsp1->hst = vsp1_hsit_create(vsp1, false);
306 	if (IS_ERR(vsp1->hst)) {
307 		ret = PTR_ERR(vsp1->hst);
308 		goto done;
309 	}
310 
311 	list_add_tail(&vsp1->hst->entity.list_dev, &vsp1->entities);
312 
313 	if (vsp1_feature(vsp1, VSP1_HAS_HGO) && vsp1->info->uapi) {
314 		vsp1->hgo = vsp1_hgo_create(vsp1);
315 		if (IS_ERR(vsp1->hgo)) {
316 			ret = PTR_ERR(vsp1->hgo);
317 			goto done;
318 		}
319 
320 		list_add_tail(&vsp1->hgo->histo.entity.list_dev,
321 			      &vsp1->entities);
322 	}
323 
324 	if (vsp1_feature(vsp1, VSP1_HAS_HGT) && vsp1->info->uapi) {
325 		vsp1->hgt = vsp1_hgt_create(vsp1);
326 		if (IS_ERR(vsp1->hgt)) {
327 			ret = PTR_ERR(vsp1->hgt);
328 			goto done;
329 		}
330 
331 		list_add_tail(&vsp1->hgt->histo.entity.list_dev,
332 			      &vsp1->entities);
333 	}
334 
335 	/*
336 	 * The LIFs are only supported when used in conjunction with the DU, in
337 	 * which case the userspace API is disabled. If the userspace API is
338 	 * enabled skip the LIFs, even when present.
339 	 */
340 	if (!vsp1->info->uapi) {
341 		for (i = 0; i < vsp1->info->lif_count; ++i) {
342 			struct vsp1_lif *lif;
343 
344 			lif = vsp1_lif_create(vsp1, i);
345 			if (IS_ERR(lif)) {
346 				ret = PTR_ERR(lif);
347 				goto done;
348 			}
349 
350 			vsp1->lif[i] = lif;
351 			list_add_tail(&lif->entity.list_dev, &vsp1->entities);
352 		}
353 	}
354 
355 	if (vsp1_feature(vsp1, VSP1_HAS_LUT)) {
356 		vsp1->lut = vsp1_lut_create(vsp1);
357 		if (IS_ERR(vsp1->lut)) {
358 			ret = PTR_ERR(vsp1->lut);
359 			goto done;
360 		}
361 
362 		list_add_tail(&vsp1->lut->entity.list_dev, &vsp1->entities);
363 	}
364 
365 	for (i = 0; i < vsp1->info->rpf_count; ++i) {
366 		struct vsp1_rwpf *rpf;
367 
368 		rpf = vsp1_rpf_create(vsp1, i);
369 		if (IS_ERR(rpf)) {
370 			ret = PTR_ERR(rpf);
371 			goto done;
372 		}
373 
374 		vsp1->rpf[i] = rpf;
375 		list_add_tail(&rpf->entity.list_dev, &vsp1->entities);
376 
377 		if (vsp1->info->uapi) {
378 			struct vsp1_video *video = vsp1_video_create(vsp1, rpf);
379 
380 			if (IS_ERR(video)) {
381 				ret = PTR_ERR(video);
382 				goto done;
383 			}
384 
385 			list_add_tail(&video->list, &vsp1->videos);
386 		}
387 	}
388 
389 	if (vsp1_feature(vsp1, VSP1_HAS_SRU)) {
390 		vsp1->sru = vsp1_sru_create(vsp1);
391 		if (IS_ERR(vsp1->sru)) {
392 			ret = PTR_ERR(vsp1->sru);
393 			goto done;
394 		}
395 
396 		list_add_tail(&vsp1->sru->entity.list_dev, &vsp1->entities);
397 	}
398 
399 	for (i = 0; i < vsp1->info->uds_count; ++i) {
400 		struct vsp1_uds *uds;
401 
402 		uds = vsp1_uds_create(vsp1, i);
403 		if (IS_ERR(uds)) {
404 			ret = PTR_ERR(uds);
405 			goto done;
406 		}
407 
408 		vsp1->uds[i] = uds;
409 		list_add_tail(&uds->entity.list_dev, &vsp1->entities);
410 	}
411 
412 	for (i = 0; i < vsp1->info->uif_count; ++i) {
413 		struct vsp1_uif *uif;
414 
415 		uif = vsp1_uif_create(vsp1, i);
416 		if (IS_ERR(uif)) {
417 			ret = PTR_ERR(uif);
418 			goto done;
419 		}
420 
421 		vsp1->uif[i] = uif;
422 		list_add_tail(&uif->entity.list_dev, &vsp1->entities);
423 	}
424 
425 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
426 		struct vsp1_rwpf *wpf;
427 
428 		wpf = vsp1_wpf_create(vsp1, i);
429 		if (IS_ERR(wpf)) {
430 			ret = PTR_ERR(wpf);
431 			goto done;
432 		}
433 
434 		vsp1->wpf[i] = wpf;
435 		list_add_tail(&wpf->entity.list_dev, &vsp1->entities);
436 
437 		if (vsp1->info->uapi) {
438 			struct vsp1_video *video = vsp1_video_create(vsp1, wpf);
439 
440 			if (IS_ERR(video)) {
441 				ret = PTR_ERR(video);
442 				goto done;
443 			}
444 
445 			list_add_tail(&video->list, &vsp1->videos);
446 		}
447 	}
448 
449 	/* Register all subdevs. */
450 	list_for_each_entry(entity, &vsp1->entities, list_dev) {
451 		ret = v4l2_device_register_subdev(&vsp1->v4l2_dev,
452 						  &entity->subdev);
453 		if (ret < 0)
454 			goto done;
455 	}
456 
457 	/*
458 	 * Create links and register subdev nodes if the userspace API is
459 	 * enabled or initialize the DRM pipeline otherwise.
460 	 */
461 	if (vsp1->info->uapi) {
462 		ret = vsp1_uapi_create_links(vsp1);
463 		if (ret < 0)
464 			goto done;
465 
466 		ret = v4l2_device_register_subdev_nodes(&vsp1->v4l2_dev);
467 		if (ret < 0)
468 			goto done;
469 
470 		ret = media_device_register(mdev);
471 	} else {
472 		ret = vsp1_drm_init(vsp1);
473 	}
474 
475 done:
476 	if (ret < 0)
477 		vsp1_destroy_entities(vsp1);
478 
479 	return ret;
480 }
481 
482 int vsp1_reset_wpf(struct vsp1_device *vsp1, unsigned int index)
483 {
484 	unsigned int timeout;
485 	u32 status;
486 
487 	status = vsp1_read(vsp1, VI6_STATUS);
488 	if (!(status & VI6_STATUS_SYS_ACT(index)))
489 		return 0;
490 
491 	vsp1_write(vsp1, VI6_SRESET, VI6_SRESET_SRTS(index));
492 	for (timeout = 10; timeout > 0; --timeout) {
493 		status = vsp1_read(vsp1, VI6_STATUS);
494 		if (!(status & VI6_STATUS_SYS_ACT(index)))
495 			break;
496 
497 		usleep_range(1000, 2000);
498 	}
499 
500 	if (!timeout) {
501 		dev_err(vsp1->dev, "failed to reset wpf.%u\n", index);
502 		return -ETIMEDOUT;
503 	}
504 
505 	return 0;
506 }
507 
508 static int vsp1_device_init(struct vsp1_device *vsp1)
509 {
510 	unsigned int i;
511 	int ret;
512 
513 	/* Reset any channel that might be running. */
514 	for (i = 0; i < vsp1->info->wpf_count; ++i) {
515 		ret = vsp1_reset_wpf(vsp1, i);
516 		if (ret < 0)
517 			return ret;
518 	}
519 
520 	vsp1_write(vsp1, VI6_CLK_DCSWT, (8 << VI6_CLK_DCSWT_CSTPW_SHIFT) |
521 		   (8 << VI6_CLK_DCSWT_CSTRW_SHIFT));
522 
523 	for (i = 0; i < vsp1->info->rpf_count; ++i)
524 		vsp1_write(vsp1, VI6_DPR_RPF_ROUTE(i), VI6_DPR_NODE_UNUSED);
525 
526 	for (i = 0; i < vsp1->info->uds_count; ++i)
527 		vsp1_write(vsp1, VI6_DPR_UDS_ROUTE(i), VI6_DPR_NODE_UNUSED);
528 
529 	for (i = 0; i < vsp1->info->uif_count; ++i)
530 		vsp1_write(vsp1, VI6_DPR_UIF_ROUTE(i), VI6_DPR_NODE_UNUSED);
531 
532 	vsp1_write(vsp1, VI6_DPR_SRU_ROUTE, VI6_DPR_NODE_UNUSED);
533 	vsp1_write(vsp1, VI6_DPR_LUT_ROUTE, VI6_DPR_NODE_UNUSED);
534 	vsp1_write(vsp1, VI6_DPR_CLU_ROUTE, VI6_DPR_NODE_UNUSED);
535 	vsp1_write(vsp1, VI6_DPR_HST_ROUTE, VI6_DPR_NODE_UNUSED);
536 	vsp1_write(vsp1, VI6_DPR_HSI_ROUTE, VI6_DPR_NODE_UNUSED);
537 	vsp1_write(vsp1, VI6_DPR_BRU_ROUTE, VI6_DPR_NODE_UNUSED);
538 
539 	if (vsp1_feature(vsp1, VSP1_HAS_BRS))
540 		vsp1_write(vsp1, VI6_DPR_ILV_BRS_ROUTE, VI6_DPR_NODE_UNUSED);
541 
542 	vsp1_write(vsp1, VI6_DPR_HGO_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
543 		   (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
544 	vsp1_write(vsp1, VI6_DPR_HGT_SMPPT, (7 << VI6_DPR_SMPPT_TGW_SHIFT) |
545 		   (VI6_DPR_NODE_UNUSED << VI6_DPR_SMPPT_PT_SHIFT));
546 
547 	vsp1_dlm_setup(vsp1);
548 
549 	return 0;
550 }
551 
552 static void vsp1_mask_all_interrupts(struct vsp1_device *vsp1)
553 {
554 	unsigned int i;
555 
556 	for (i = 0; i < vsp1->info->lif_count; ++i)
557 		vsp1_write(vsp1, VI6_DISP_IRQ_ENB(i), 0);
558 	for (i = 0; i < vsp1->info->wpf_count; ++i)
559 		vsp1_write(vsp1, VI6_WPF_IRQ_ENB(i), 0);
560 }
561 
562 /*
563  * vsp1_device_get - Acquire the VSP1 device
564  *
565  * Make sure the device is not suspended and initialize it if needed.
566  *
567  * Return 0 on success or a negative error code otherwise.
568  */
569 int vsp1_device_get(struct vsp1_device *vsp1)
570 {
571 	return pm_runtime_resume_and_get(vsp1->dev);
572 }
573 
574 /*
575  * vsp1_device_put - Release the VSP1 device
576  *
577  * Decrement the VSP1 reference count and cleanup the device if the last
578  * reference is released.
579  */
580 void vsp1_device_put(struct vsp1_device *vsp1)
581 {
582 	pm_runtime_put_sync(vsp1->dev);
583 }
584 
585 /* -----------------------------------------------------------------------------
586  * Power Management
587  */
588 
589 static int __maybe_unused vsp1_pm_suspend(struct device *dev)
590 {
591 	struct vsp1_device *vsp1 = dev_get_drvdata(dev);
592 
593 	/*
594 	 * When used as part of a display pipeline, the VSP is stopped and
595 	 * restarted explicitly by the DU.
596 	 */
597 	if (!vsp1->drm)
598 		vsp1_video_suspend(vsp1);
599 
600 	pm_runtime_force_suspend(vsp1->dev);
601 
602 	return 0;
603 }
604 
605 static int __maybe_unused vsp1_pm_resume(struct device *dev)
606 {
607 	struct vsp1_device *vsp1 = dev_get_drvdata(dev);
608 
609 	pm_runtime_force_resume(vsp1->dev);
610 
611 	/*
612 	 * When used as part of a display pipeline, the VSP is stopped and
613 	 * restarted explicitly by the DU.
614 	 */
615 	if (!vsp1->drm)
616 		vsp1_video_resume(vsp1);
617 
618 	return 0;
619 }
620 
621 static int __maybe_unused vsp1_pm_runtime_suspend(struct device *dev)
622 {
623 	struct vsp1_device *vsp1 = dev_get_drvdata(dev);
624 
625 	rcar_fcp_disable(vsp1->fcp);
626 	reset_control_assert(vsp1->rstc);
627 
628 	return 0;
629 }
630 
631 static int __maybe_unused vsp1_pm_runtime_resume(struct device *dev)
632 {
633 	struct vsp1_device *vsp1 = dev_get_drvdata(dev);
634 	int ret;
635 
636 	ret = reset_control_deassert(vsp1->rstc);
637 	if (ret < 0)
638 		return ret;
639 
640 	if (vsp1->info) {
641 		/*
642 		 * On R-Car Gen2 and RZ/G1, vsp1 register access after deassert
643 		 * can cause lock-up. It is a special case and needs some delay
644 		 * to avoid this lock-up.
645 		 */
646 		if (vsp1->info->gen == 2)
647 			udelay(1);
648 
649 		ret = vsp1_device_init(vsp1);
650 		if (ret < 0)
651 			goto done;
652 	}
653 
654 	ret = rcar_fcp_enable(vsp1->fcp);
655 
656 done:
657 	if (ret < 0)
658 		reset_control_assert(vsp1->rstc);
659 
660 	return ret;
661 }
662 
663 static const struct dev_pm_ops vsp1_pm_ops = {
664 	SET_SYSTEM_SLEEP_PM_OPS(vsp1_pm_suspend, vsp1_pm_resume)
665 	SET_RUNTIME_PM_OPS(vsp1_pm_runtime_suspend, vsp1_pm_runtime_resume, NULL)
666 };
667 
668 /* -----------------------------------------------------------------------------
669  * Platform Driver
670  */
671 
672 static const struct vsp1_device_info vsp1_device_infos[] = {
673 	{
674 		.version = VI6_IP_VERSION_MODEL_VSPS_H2,
675 		.model = "VSP1-S",
676 		.gen = 2,
677 		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
678 			  | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
679 			  | VSP1_HAS_WPF_VFLIP,
680 		.rpf_count = 5,
681 		.uds_count = 3,
682 		.wpf_count = 4,
683 		.num_bru_inputs = 4,
684 		.uapi = true,
685 	}, {
686 		.version = VI6_IP_VERSION_MODEL_VSPR_H2,
687 		.model = "VSP1-R",
688 		.gen = 2,
689 		.features = VSP1_HAS_BRU | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
690 		.rpf_count = 5,
691 		.uds_count = 3,
692 		.wpf_count = 4,
693 		.num_bru_inputs = 4,
694 		.uapi = true,
695 	}, {
696 		.version = VI6_IP_VERSION_MODEL_VSPD_GEN2,
697 		.model = "VSP1-D",
698 		.gen = 2,
699 		.features = VSP1_HAS_BRU | VSP1_HAS_HGO | VSP1_HAS_LUT,
700 		.lif_count = 1,
701 		.rpf_count = 4,
702 		.uds_count = 1,
703 		.wpf_count = 1,
704 		.num_bru_inputs = 4,
705 		.uapi = true,
706 	}, {
707 		.version = VI6_IP_VERSION_MODEL_VSPS_M2,
708 		.model = "VSP1-S",
709 		.gen = 2,
710 		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
711 			  | VSP1_HAS_HGT | VSP1_HAS_LUT | VSP1_HAS_SRU
712 			  | VSP1_HAS_WPF_VFLIP,
713 		.rpf_count = 5,
714 		.uds_count = 1,
715 		.wpf_count = 4,
716 		.num_bru_inputs = 4,
717 		.uapi = true,
718 	}, {
719 		.version = VI6_IP_VERSION_MODEL_VSPS_V2H,
720 		.model = "VSP1V-S",
721 		.gen = 2,
722 		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT
723 			  | VSP1_HAS_SRU | VSP1_HAS_WPF_VFLIP,
724 		.rpf_count = 4,
725 		.uds_count = 1,
726 		.wpf_count = 4,
727 		.num_bru_inputs = 4,
728 		.uapi = true,
729 	}, {
730 		.version = VI6_IP_VERSION_MODEL_VSPD_V2H,
731 		.model = "VSP1V-D",
732 		.gen = 2,
733 		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_LUT,
734 		.lif_count = 1,
735 		.rpf_count = 4,
736 		.uds_count = 1,
737 		.wpf_count = 1,
738 		.num_bru_inputs = 4,
739 		.uapi = true,
740 	}, {
741 		.version = VI6_IP_VERSION_MODEL_VSPI_GEN3,
742 		.model = "VSP2-I",
743 		.gen = 3,
744 		.features = VSP1_HAS_CLU | VSP1_HAS_HGO | VSP1_HAS_HGT
745 			  | VSP1_HAS_LUT | VSP1_HAS_SRU | VSP1_HAS_WPF_HFLIP
746 			  | VSP1_HAS_WPF_VFLIP,
747 		.rpf_count = 1,
748 		.uds_count = 1,
749 		.wpf_count = 1,
750 		.uapi = true,
751 	}, {
752 		.version = VI6_IP_VERSION_MODEL_VSPBD_GEN3,
753 		.model = "VSP2-BD",
754 		.gen = 3,
755 		.features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP,
756 		.rpf_count = 5,
757 		.wpf_count = 1,
758 		.num_bru_inputs = 5,
759 		.uapi = true,
760 	}, {
761 		.version = VI6_IP_VERSION_MODEL_VSPBC_GEN3,
762 		.model = "VSP2-BC",
763 		.gen = 3,
764 		.features = VSP1_HAS_BRU | VSP1_HAS_CLU | VSP1_HAS_HGO
765 			  | VSP1_HAS_LUT | VSP1_HAS_WPF_VFLIP,
766 		.rpf_count = 5,
767 		.wpf_count = 1,
768 		.num_bru_inputs = 5,
769 		.uapi = true,
770 	}, {
771 		.version = VI6_IP_VERSION_MODEL_VSPBS_GEN3,
772 		.model = "VSP2-BS",
773 		.gen = 3,
774 		.features = VSP1_HAS_BRS | VSP1_HAS_WPF_VFLIP,
775 		.rpf_count = 2,
776 		.wpf_count = 1,
777 		.uapi = true,
778 	}, {
779 		.version = VI6_IP_VERSION_MODEL_VSPD_GEN3,
780 		.model = "VSP2-D",
781 		.gen = 3,
782 		.features = VSP1_HAS_BRU | VSP1_HAS_WPF_VFLIP | VSP1_HAS_EXT_DL,
783 		.lif_count = 1,
784 		.rpf_count = 5,
785 		.uif_count = 1,
786 		.wpf_count = 2,
787 		.num_bru_inputs = 5,
788 	}, {
789 		.version = VI6_IP_VERSION_MODEL_VSPD_V3,
790 		.model = "VSP2-D",
791 		.soc = VI6_IP_VERSION_SOC_V3H,
792 		.gen = 3,
793 		.features = VSP1_HAS_BRS | VSP1_HAS_BRU,
794 		.lif_count = 1,
795 		.rpf_count = 5,
796 		.uif_count = 1,
797 		.wpf_count = 1,
798 		.num_bru_inputs = 5,
799 	}, {
800 		.version = VI6_IP_VERSION_MODEL_VSPD_V3,
801 		.model = "VSP2-D",
802 		.soc = VI6_IP_VERSION_SOC_V3M,
803 		.gen = 3,
804 		.features = VSP1_HAS_BRS | VSP1_HAS_BRU | VSP1_HAS_NON_ZERO_LBA,
805 		.lif_count = 1,
806 		.rpf_count = 5,
807 		.uif_count = 1,
808 		.wpf_count = 1,
809 		.num_bru_inputs = 5,
810 	}, {
811 		.version = VI6_IP_VERSION_MODEL_VSPDL_GEN3,
812 		.model = "VSP2-DL",
813 		.gen = 3,
814 		.features = VSP1_HAS_BRS | VSP1_HAS_BRU | VSP1_HAS_EXT_DL,
815 		.lif_count = 2,
816 		.rpf_count = 5,
817 		.uif_count = 2,
818 		.wpf_count = 2,
819 		.num_bru_inputs = 5,
820 	}, {
821 		.version = VI6_IP_VERSION_MODEL_VSPD_GEN4,
822 		.model = "VSP2-D",
823 		.gen = 4,
824 		.features = VSP1_HAS_BRU | VSP1_HAS_EXT_DL,
825 		.lif_count = 1,
826 		.rpf_count = 5,
827 		.uif_count = 2,
828 		.wpf_count = 1,
829 		.num_bru_inputs = 5,
830 	},
831 };
832 
833 static const struct vsp1_device_info rzg2l_vsp2_device_info = {
834 	.version = VI6_IP_VERSION_MODEL_VSPD_RZG2L,
835 	.model = "VSP2-D",
836 	.soc = VI6_IP_VERSION_SOC_RZG2L,
837 	.gen = 3,
838 	.features = VSP1_HAS_BRS | VSP1_HAS_WPF_VFLIP | VSP1_HAS_EXT_DL
839 		  | VSP1_HAS_NON_ZERO_LBA,
840 	.lif_count = 1,
841 	.rpf_count = 2,
842 	.wpf_count = 1,
843 };
844 
845 static const struct vsp1_device_info *vsp1_lookup_info(struct vsp1_device *vsp1)
846 {
847 	const struct vsp1_device_info *info;
848 	unsigned int i;
849 	u32 model;
850 	u32 soc;
851 
852 	/*
853 	 * Try the info stored in match data first for devices that don't have
854 	 * a version register.
855 	 */
856 	info = of_device_get_match_data(vsp1->dev);
857 	if (info) {
858 		vsp1->version = VI6_IP_VERSION_VSP_SW | info->version | info->soc;
859 		return info;
860 	}
861 
862 	vsp1->version = vsp1_read(vsp1, VI6_IP_VERSION);
863 	model = vsp1->version & VI6_IP_VERSION_MODEL_MASK;
864 	soc = vsp1->version & VI6_IP_VERSION_SOC_MASK;
865 
866 	for (i = 0; i < ARRAY_SIZE(vsp1_device_infos); ++i) {
867 		info = &vsp1_device_infos[i];
868 
869 		if (model == info->version && (!info->soc || soc == info->soc))
870 			return info;
871 	}
872 
873 	dev_err(vsp1->dev, "unsupported IP version 0x%08x\n", vsp1->version);
874 
875 	return NULL;
876 }
877 
878 static int vsp1_probe(struct platform_device *pdev)
879 {
880 	struct vsp1_device *vsp1;
881 	struct device_node *fcp_node;
882 	int ret;
883 	int irq;
884 
885 	vsp1 = devm_kzalloc(&pdev->dev, sizeof(*vsp1), GFP_KERNEL);
886 	if (vsp1 == NULL)
887 		return -ENOMEM;
888 
889 	vsp1->dev = &pdev->dev;
890 	INIT_LIST_HEAD(&vsp1->entities);
891 	INIT_LIST_HEAD(&vsp1->videos);
892 
893 	platform_set_drvdata(pdev, vsp1);
894 
895 	/* I/O and IRQ resources (clock managed by the clock PM domain). */
896 	vsp1->mmio = devm_platform_ioremap_resource(pdev, 0);
897 	if (IS_ERR(vsp1->mmio))
898 		return PTR_ERR(vsp1->mmio);
899 
900 	irq = platform_get_irq(pdev, 0);
901 	if (irq < 0)
902 		return irq;
903 
904 	vsp1->rstc = devm_reset_control_get_shared(&pdev->dev, NULL);
905 	if (IS_ERR(vsp1->rstc))
906 		return dev_err_probe(&pdev->dev, PTR_ERR(vsp1->rstc),
907 				     "failed to get reset control\n");
908 
909 	/* FCP (optional). */
910 	fcp_node = of_parse_phandle(pdev->dev.of_node, "renesas,fcp", 0);
911 	if (fcp_node) {
912 		vsp1->fcp = rcar_fcp_get(fcp_node);
913 		of_node_put(fcp_node);
914 		if (IS_ERR(vsp1->fcp)) {
915 			dev_dbg(&pdev->dev, "FCP not found (%ld)\n",
916 				PTR_ERR(vsp1->fcp));
917 			return PTR_ERR(vsp1->fcp);
918 		}
919 
920 		/*
921 		 * When the FCP is present, it handles all bus master accesses
922 		 * for the VSP and must thus be used in place of the VSP device
923 		 * to map DMA buffers.
924 		 */
925 		vsp1->bus_master = rcar_fcp_get_device(vsp1->fcp);
926 	} else {
927 		vsp1->bus_master = vsp1->dev;
928 	}
929 
930 	/* Configure device parameters based on the version register. */
931 	pm_runtime_enable(&pdev->dev);
932 
933 	ret = vsp1_device_get(vsp1);
934 	if (ret < 0)
935 		goto done;
936 
937 	vsp1->info = vsp1_lookup_info(vsp1);
938 	if (!vsp1->info) {
939 		vsp1_device_put(vsp1);
940 		ret = -ENXIO;
941 		goto done;
942 	}
943 
944 	dev_dbg(&pdev->dev, "IP version 0x%08x\n", vsp1->version);
945 
946 	/*
947 	 * Previous use of the hardware (e.g. by the bootloader) could leave
948 	 * some interrupts enabled and pending.
949 	 *
950 	 * TODO: Investigate if this shouldn't be better handled by using the
951 	 * device reset provided by the CPG.
952 	 */
953 	vsp1_mask_all_interrupts(vsp1);
954 
955 	vsp1_device_put(vsp1);
956 
957 	ret = devm_request_irq(&pdev->dev, irq, vsp1_irq_handler,
958 			       IRQF_SHARED, dev_name(&pdev->dev), vsp1);
959 	if (ret < 0) {
960 		dev_err(&pdev->dev, "failed to request IRQ\n");
961 		goto done;
962 	}
963 
964 	/* Instantiate entities. */
965 	ret = vsp1_create_entities(vsp1);
966 	if (ret < 0) {
967 		dev_err(&pdev->dev, "failed to create entities\n");
968 		goto done;
969 	}
970 
971 done:
972 	if (ret) {
973 		pm_runtime_disable(&pdev->dev);
974 		rcar_fcp_put(vsp1->fcp);
975 	}
976 
977 	return ret;
978 }
979 
980 static int vsp1_remove(struct platform_device *pdev)
981 {
982 	struct vsp1_device *vsp1 = platform_get_drvdata(pdev);
983 
984 	vsp1_destroy_entities(vsp1);
985 	rcar_fcp_put(vsp1->fcp);
986 
987 	pm_runtime_disable(&pdev->dev);
988 
989 	return 0;
990 }
991 
992 static const struct of_device_id vsp1_of_match[] = {
993 	{ .compatible = "renesas,vsp1" },
994 	{ .compatible = "renesas,vsp2" },
995 	{ .compatible = "renesas,r9a07g044-vsp2", .data = &rzg2l_vsp2_device_info },
996 	{ },
997 };
998 MODULE_DEVICE_TABLE(of, vsp1_of_match);
999 
1000 static struct platform_driver vsp1_platform_driver = {
1001 	.probe		= vsp1_probe,
1002 	.remove		= vsp1_remove,
1003 	.driver		= {
1004 		.name	= "vsp1",
1005 		.pm	= &vsp1_pm_ops,
1006 		.of_match_table = vsp1_of_match,
1007 	},
1008 };
1009 
1010 module_platform_driver(vsp1_platform_driver);
1011 
1012 MODULE_ALIAS("vsp1");
1013 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>");
1014 MODULE_DESCRIPTION("Renesas VSP1 Driver");
1015 MODULE_LICENSE("GPL");
1016