1 // SPDX-License-Identifier: GPL-2.0
3 * SuperH Video Output Unit (VOU) driver
8 #include <linux/dma-mapping.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <linux/videodev2.h>
20 #include <linux/module.h>
22 #include <media/drv-intf/sh_vou.h>
23 #include <media/v4l2-common.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-ioctl.h>
26 #include <media/v4l2-mediabus.h>
27 #include <media/videobuf2-v4l2.h>
28 #include <media/videobuf2-dma-contig.h>
30 /* Mirror addresses are not available for all registers */
58 #define VOU_MIN_IMAGE_WIDTH 16
59 #define VOU_MAX_IMAGE_WIDTH 720
60 #define VOU_MIN_IMAGE_HEIGHT 16
62 struct sh_vou_buffer {
63 struct vb2_v4l2_buffer vb;
64 struct list_head list;
68 sh_vou_buffer *to_sh_vou_buffer(struct vb2_v4l2_buffer *vb2)
70 return container_of(vb2, struct sh_vou_buffer, vb);
73 struct sh_vou_device {
74 struct v4l2_device v4l2_dev;
75 struct video_device vdev;
76 struct sh_vou_pdata *pdata;
79 /* State information */
80 struct v4l2_pix_format pix;
81 struct v4l2_rect rect;
82 struct list_head buf_list;
85 struct vb2_queue queue;
86 struct sh_vou_buffer *active;
87 enum sh_vou_status status;
89 struct mutex fop_lock;
92 /* Register access routines for sides A, B and mirror addresses */
93 static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
96 __raw_writel(value, vou_dev->base + reg);
99 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
102 __raw_writel(value, vou_dev->base + reg);
103 __raw_writel(value, vou_dev->base + reg + 0x1000);
106 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
109 __raw_writel(value, vou_dev->base + reg + 0x2000);
112 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
114 return __raw_readl(vou_dev->base + reg);
117 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
120 u32 old = __raw_readl(vou_dev->base + reg);
122 value = (value & mask) | (old & ~mask);
123 __raw_writel(value, vou_dev->base + reg);
126 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
129 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
132 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
135 sh_vou_reg_a_set(vou_dev, reg, value, mask);
136 sh_vou_reg_b_set(vou_dev, reg, value, mask);
148 /* Further pixel formats can be added */
149 static struct sh_vou_fmt vou_fmt[] = {
151 .pfmt = V4L2_PIX_FMT_NV12,
158 .pfmt = V4L2_PIX_FMT_NV16,
165 .pfmt = V4L2_PIX_FMT_RGB24,
172 .pfmt = V4L2_PIX_FMT_RGB565,
179 .pfmt = V4L2_PIX_FMT_RGB565X,
187 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
188 struct vb2_v4l2_buffer *vbuf)
190 dma_addr_t addr1, addr2;
192 addr1 = vb2_dma_contig_plane_dma_addr(&vbuf->vb2_buf, 0);
193 switch (vou_dev->pix.pixelformat) {
194 case V4L2_PIX_FMT_NV12:
195 case V4L2_PIX_FMT_NV16:
196 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
202 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
203 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
206 static void sh_vou_stream_config(struct sh_vou_device *vou_dev)
208 unsigned int row_coeff;
209 #ifdef __LITTLE_ENDIAN
215 switch (vou_dev->pix.pixelformat) {
217 case V4L2_PIX_FMT_NV12:
218 case V4L2_PIX_FMT_NV16:
221 case V4L2_PIX_FMT_RGB565:
224 case V4L2_PIX_FMT_RGB565X:
227 case V4L2_PIX_FMT_RGB24:
232 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
233 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
236 /* Locking: caller holds fop_lock mutex */
237 static int sh_vou_queue_setup(struct vb2_queue *vq,
238 unsigned int *nbuffers, unsigned int *nplanes,
239 unsigned int sizes[], struct device *alloc_devs[])
241 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
242 struct v4l2_pix_format *pix = &vou_dev->pix;
243 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
245 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
248 return sizes[0] < pix->height * bytes_per_line ? -EINVAL : 0;
250 sizes[0] = pix->height * bytes_per_line;
254 static int sh_vou_buf_prepare(struct vb2_buffer *vb)
256 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue);
257 struct v4l2_pix_format *pix = &vou_dev->pix;
258 unsigned bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
259 unsigned size = pix->height * bytes_per_line;
261 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
263 if (vb2_plane_size(vb, 0) < size) {
264 /* User buffer too small */
265 dev_warn(vou_dev->v4l2_dev.dev, "buffer too small (%lu < %u)\n",
266 vb2_plane_size(vb, 0), size);
270 vb2_set_plane_payload(vb, 0, size);
274 /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
275 static void sh_vou_buf_queue(struct vb2_buffer *vb)
277 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
278 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vb->vb2_queue);
279 struct sh_vou_buffer *shbuf = to_sh_vou_buffer(vbuf);
282 spin_lock_irqsave(&vou_dev->lock, flags);
283 list_add_tail(&shbuf->list, &vou_dev->buf_list);
284 spin_unlock_irqrestore(&vou_dev->lock, flags);
287 static int sh_vou_start_streaming(struct vb2_queue *vq, unsigned int count)
289 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
290 struct sh_vou_buffer *buf, *node;
293 vou_dev->sequence = 0;
294 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
296 if (ret < 0 && ret != -ENOIOCTLCMD) {
297 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) {
298 vb2_buffer_done(&buf->vb.vb2_buf,
299 VB2_BUF_STATE_QUEUED);
300 list_del(&buf->list);
302 vou_dev->active = NULL;
306 buf = list_entry(vou_dev->buf_list.next, struct sh_vou_buffer, list);
308 vou_dev->active = buf;
310 /* Start from side A: we use mirror addresses, so, set B */
311 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
312 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
313 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
314 sh_vou_schedule_next(vou_dev, &buf->vb);
316 buf = list_entry(buf->list.next, struct sh_vou_buffer, list);
318 /* Second buffer - initialise register side B */
319 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
320 sh_vou_schedule_next(vou_dev, &buf->vb);
322 /* Register side switching with frame VSYNC */
323 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
325 sh_vou_stream_config(vou_dev);
326 /* Enable End-of-Frame (VSYNC) interrupts */
327 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
329 /* Two buffers on the queue - activate the hardware */
330 vou_dev->status = SH_VOU_RUNNING;
331 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
335 static void sh_vou_stop_streaming(struct vb2_queue *vq)
337 struct sh_vou_device *vou_dev = vb2_get_drv_priv(vq);
338 struct sh_vou_buffer *buf, *node;
341 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
344 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
345 /* ...but the current frame will complete */
346 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
348 spin_lock_irqsave(&vou_dev->lock, flags);
349 list_for_each_entry_safe(buf, node, &vou_dev->buf_list, list) {
350 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
351 list_del(&buf->list);
353 vou_dev->active = NULL;
354 spin_unlock_irqrestore(&vou_dev->lock, flags);
357 static const struct vb2_ops sh_vou_qops = {
358 .queue_setup = sh_vou_queue_setup,
359 .buf_prepare = sh_vou_buf_prepare,
360 .buf_queue = sh_vou_buf_queue,
361 .start_streaming = sh_vou_start_streaming,
362 .stop_streaming = sh_vou_stop_streaming,
366 static int sh_vou_querycap(struct file *file, void *priv,
367 struct v4l2_capability *cap)
369 struct sh_vou_device *vou_dev = video_drvdata(file);
371 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
373 strscpy(cap->card, "SuperH VOU", sizeof(cap->card));
374 strscpy(cap->driver, "sh-vou", sizeof(cap->driver));
375 strscpy(cap->bus_info, "platform:sh-vou", sizeof(cap->bus_info));
379 /* Enumerate formats, that the device can accept from the user */
380 static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
381 struct v4l2_fmtdesc *fmt)
383 struct sh_vou_device *vou_dev = video_drvdata(file);
385 if (fmt->index >= ARRAY_SIZE(vou_fmt))
388 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
390 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
395 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
396 struct v4l2_format *fmt)
398 struct sh_vou_device *vou_dev = video_drvdata(file);
400 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
402 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
403 fmt->fmt.pix = vou_dev->pix;
408 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
409 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
410 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
411 static const unsigned char vou_scale_v_num[] = {1, 2, 4};
412 static const unsigned char vou_scale_v_den[] = {1, 1, 1};
413 static const unsigned char vou_scale_v_fld[] = {0, 1};
415 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
416 int pix_idx, int w_idx, int h_idx)
418 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
419 unsigned int black_left, black_top, width_max,
420 frame_in_height, frame_out_height, frame_out_top;
421 struct v4l2_rect *rect = &vou_dev->rect;
422 struct v4l2_pix_format *pix = &vou_dev->pix;
423 u32 vouvcr = 0, dsr_h, dsr_v;
425 if (vou_dev->std & V4L2_STD_525_60) {
427 /* height_max = 262; */
430 /* height_max = 312; */
433 frame_in_height = pix->height / 2;
434 frame_out_height = rect->height / 2;
435 frame_out_top = rect->top / 2;
438 * Cropping scheme: max useful image is 720x480, and the total video
439 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
440 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
441 * of which the first 33 / 25 clocks HSYNC must be held active. This
442 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
443 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
444 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
445 * beyond DSR, specified on the left and top by the VPR register "black
446 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
447 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
448 * client requires, and that's exactly what leaves us 720 pixels for the
449 * image; we leave VPR[VVP] at default 20 for now, because the client
450 * doesn't seem to have any special requirements for it. Otherwise we
451 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
452 * the selected standard, and DPR and DSR are selected according to
453 * cropping. Q: how does the client detect the first valid line? Does
454 * HSYNC stay inactive during invalid (black) lines?
456 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
459 dsr_h = rect->width + rect->left;
460 dsr_v = frame_out_height + frame_out_top;
462 dev_dbg(vou_dev->v4l2_dev.dev,
463 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
464 pix->width, frame_in_height, black_left, black_top,
465 rect->left, frame_out_top, dsr_h, dsr_v);
467 /* VOUISR height - half of a frame height in frame mode */
468 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
469 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
470 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
471 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
474 * if necessary, we could set VOUHIR to
475 * max(black_left + dsr_h, width_max) here
479 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
481 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
483 dev_dbg(vou_dev->v4l2_dev.dev, "0x%08x: scaling 0x%x\n",
486 /* To produce a colour bar for testing set bit 23 of VOUVCR */
487 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
488 sh_vou_reg_ab_write(vou_dev, VOUDFR,
489 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
492 struct sh_vou_geometry {
493 struct v4l2_rect output;
494 unsigned int in_width;
495 unsigned int in_height;
501 * Find input geometry, that we can use to produce output, closest to the
502 * requested rectangle, using VOU scaling
504 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
506 /* The compiler cannot know, that best and idx will indeed be set */
507 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
510 if (std & V4L2_STD_525_60)
511 img_height_max = 480;
513 img_height_max = 576;
515 /* Image width must be a multiple of 4 */
516 v4l_bound_align_image(&geo->in_width,
517 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2,
519 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
521 /* Select scales to come as close as possible to the output image */
522 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
524 unsigned int found = geo->output.width * vou_scale_h_den[i] /
527 if (found > VOU_MAX_IMAGE_WIDTH)
528 /* scales increase */
531 err = abs(found - geo->in_width);
532 if (err < best_err) {
541 geo->in_width = best;
542 geo->scale_idx_h = idx;
546 /* This loop can be replaced with one division */
547 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
549 unsigned int found = geo->output.height * vou_scale_v_den[i] /
552 if (found > img_height_max)
553 /* scales increase */
556 err = abs(found - geo->in_height);
557 if (err < best_err) {
566 geo->in_height = best;
567 geo->scale_idx_v = idx;
571 * Find output geometry, that we can produce, using VOU scaling, closest to
572 * the requested rectangle
574 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
576 unsigned int best_err = UINT_MAX, best = geo->in_width,
577 width_max, height_max, img_height_max;
578 int i, idx_h = 0, idx_v = 0;
580 if (std & V4L2_STD_525_60) {
582 height_max = 262 * 2;
583 img_height_max = 480;
586 height_max = 312 * 2;
587 img_height_max = 576;
590 /* Select scales to come as close as possible to the output image */
591 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
593 unsigned int found = geo->in_width * vou_scale_h_num[i] /
596 if (found > VOU_MAX_IMAGE_WIDTH)
597 /* scales increase */
600 err = abs(found - geo->output.width);
601 if (err < best_err) {
610 geo->output.width = best;
611 geo->scale_idx_h = idx_h;
612 if (geo->output.left + best > width_max)
613 geo->output.left = width_max - best;
615 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
616 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best);
620 /* This loop can be replaced with one division */
621 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
623 unsigned int found = geo->in_height * vou_scale_v_num[i] /
626 if (found > img_height_max)
627 /* scales increase */
630 err = abs(found - geo->output.height);
631 if (err < best_err) {
640 geo->output.height = best;
641 geo->scale_idx_v = idx_v;
642 if (geo->output.top + best > height_max)
643 geo->output.top = height_max - best;
645 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
646 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best);
649 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
650 struct v4l2_format *fmt)
652 struct sh_vou_device *vou_dev = video_drvdata(file);
653 struct v4l2_pix_format *pix = &fmt->fmt.pix;
654 unsigned int img_height_max;
657 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
659 pix->field = V4L2_FIELD_INTERLACED;
660 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
661 pix->ycbcr_enc = pix->quantization = 0;
663 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
664 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
667 if (pix_idx == ARRAY_SIZE(vou_fmt))
670 if (vou_dev->std & V4L2_STD_525_60)
671 img_height_max = 480;
673 img_height_max = 576;
675 v4l_bound_align_image(&pix->width,
676 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 2,
678 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
679 pix->bytesperline = pix->width * vou_fmt[pix_idx].bpl;
680 pix->sizeimage = pix->height * ((pix->width * vou_fmt[pix_idx].bpp) >> 3);
685 static int sh_vou_set_fmt_vid_out(struct sh_vou_device *vou_dev,
686 struct v4l2_pix_format *pix)
688 unsigned int img_height_max;
689 struct sh_vou_geometry geo;
690 struct v4l2_subdev_format format = {
691 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
692 /* Revisit: is this the correct code? */
693 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
694 .format.field = V4L2_FIELD_INTERLACED,
695 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
697 struct v4l2_mbus_framefmt *mbfmt = &format.format;
701 if (vb2_is_busy(&vou_dev->queue))
704 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
705 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
708 geo.in_width = pix->width;
709 geo.in_height = pix->height;
710 geo.output = vou_dev->rect;
712 vou_adjust_output(&geo, vou_dev->std);
714 mbfmt->width = geo.output.width;
715 mbfmt->height = geo.output.height;
716 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
717 set_fmt, NULL, &format);
718 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
722 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
723 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height);
725 if (vou_dev->std & V4L2_STD_525_60)
726 img_height_max = 480;
728 img_height_max = 576;
731 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH ||
732 (unsigned)mbfmt->height > img_height_max ||
733 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8)
736 if (mbfmt->width != geo.output.width ||
737 mbfmt->height != geo.output.height) {
738 geo.output.width = mbfmt->width;
739 geo.output.height = mbfmt->height;
741 vou_adjust_input(&geo, vou_dev->std);
744 /* We tried to preserve output rectangle, but it could have changed */
745 vou_dev->rect = geo.output;
746 pix->width = geo.in_width;
747 pix->height = geo.in_height;
749 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
750 pix->width, pix->height);
752 vou_dev->pix_idx = pix_idx;
756 sh_vou_configure_geometry(vou_dev, pix_idx,
757 geo.scale_idx_h, geo.scale_idx_v);
762 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
763 struct v4l2_format *fmt)
765 struct sh_vou_device *vou_dev = video_drvdata(file);
766 int ret = sh_vou_try_fmt_vid_out(file, priv, fmt);
770 return sh_vou_set_fmt_vid_out(vou_dev, &fmt->fmt.pix);
773 static int sh_vou_enum_output(struct file *file, void *fh,
774 struct v4l2_output *a)
776 struct sh_vou_device *vou_dev = video_drvdata(file);
780 strscpy(a->name, "Video Out", sizeof(a->name));
781 a->type = V4L2_OUTPUT_TYPE_ANALOG;
782 a->std = vou_dev->vdev.tvnorms;
786 static int sh_vou_g_output(struct file *file, void *fh, unsigned int *i)
792 static int sh_vou_s_output(struct file *file, void *fh, unsigned int i)
794 return i ? -EINVAL : 0;
797 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
801 pr_warn("%s(): Invalid bus-format code %d, using default 8-bit\n",
804 case SH_VOU_BUS_8BIT:
806 case SH_VOU_BUS_16BIT:
808 case SH_VOU_BUS_BT656:
813 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
815 struct sh_vou_device *vou_dev = video_drvdata(file);
818 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
820 if (std_id == vou_dev->std)
823 if (vb2_is_busy(&vou_dev->queue))
826 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
827 s_std_output, std_id);
828 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
829 if (ret < 0 && ret != -ENOIOCTLCMD)
832 vou_dev->rect.top = vou_dev->rect.left = 0;
833 vou_dev->rect.width = VOU_MAX_IMAGE_WIDTH;
834 if (std_id & V4L2_STD_525_60) {
835 sh_vou_reg_ab_set(vou_dev, VOUCR,
836 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
837 vou_dev->rect.height = 480;
839 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
840 vou_dev->rect.height = 576;
843 vou_dev->pix.width = vou_dev->rect.width;
844 vou_dev->pix.height = vou_dev->rect.height;
845 vou_dev->pix.bytesperline =
846 vou_dev->pix.width * vou_fmt[vou_dev->pix_idx].bpl;
847 vou_dev->pix.sizeimage = vou_dev->pix.height *
848 ((vou_dev->pix.width * vou_fmt[vou_dev->pix_idx].bpp) >> 3);
849 vou_dev->std = std_id;
850 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix);
855 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
857 struct sh_vou_device *vou_dev = video_drvdata(file);
859 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
866 static int sh_vou_log_status(struct file *file, void *priv)
868 struct sh_vou_device *vou_dev = video_drvdata(file);
870 pr_info("VOUER: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUER));
871 pr_info("VOUCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUCR));
872 pr_info("VOUSTR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSTR));
873 pr_info("VOUVCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUVCR));
874 pr_info("VOUISR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUISR));
875 pr_info("VOUBCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUBCR));
876 pr_info("VOUDPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDPR));
877 pr_info("VOUDSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDSR));
878 pr_info("VOUVPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUVPR));
879 pr_info("VOUIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUIR));
880 pr_info("VOUSRR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSRR));
881 pr_info("VOUMSR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUMSR));
882 pr_info("VOUHIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUHIR));
883 pr_info("VOUDFR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUDFR));
884 pr_info("VOUAD1R: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAD1R));
885 pr_info("VOUAD2R: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAD2R));
886 pr_info("VOUAIR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUAIR));
887 pr_info("VOUSWR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOUSWR));
888 pr_info("VOURCR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOURCR));
889 pr_info("VOURPR: 0x%08x\n", sh_vou_reg_a_read(vou_dev, VOURPR));
893 static int sh_vou_g_selection(struct file *file, void *fh,
894 struct v4l2_selection *sel)
896 struct sh_vou_device *vou_dev = video_drvdata(file);
898 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
900 switch (sel->target) {
901 case V4L2_SEL_TGT_COMPOSE:
902 sel->r = vou_dev->rect;
904 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
905 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
908 sel->r.width = VOU_MAX_IMAGE_WIDTH;
909 if (vou_dev->std & V4L2_STD_525_60)
920 /* Assume a dull encoder, do all the work ourselves. */
921 static int sh_vou_s_selection(struct file *file, void *fh,
922 struct v4l2_selection *sel)
924 struct v4l2_rect *rect = &sel->r;
925 struct sh_vou_device *vou_dev = video_drvdata(file);
926 struct v4l2_subdev_selection sd_sel = {
927 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
928 .target = V4L2_SEL_TGT_COMPOSE,
930 struct v4l2_pix_format *pix = &vou_dev->pix;
931 struct sh_vou_geometry geo;
932 struct v4l2_subdev_format format = {
933 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
934 /* Revisit: is this the correct code? */
935 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
936 .format.field = V4L2_FIELD_INTERLACED,
937 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
939 unsigned int img_height_max;
942 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
943 sel->target != V4L2_SEL_TGT_COMPOSE)
946 if (vb2_is_busy(&vou_dev->queue))
949 if (vou_dev->std & V4L2_STD_525_60)
950 img_height_max = 480;
952 img_height_max = 576;
954 v4l_bound_align_image(&rect->width,
955 VOU_MIN_IMAGE_WIDTH, VOU_MAX_IMAGE_WIDTH, 1,
957 VOU_MIN_IMAGE_HEIGHT, img_height_max, 1, 0);
959 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
960 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
962 if (rect->height + rect->top > img_height_max)
963 rect->top = img_height_max - rect->height;
966 geo.in_width = pix->width;
967 geo.in_height = pix->height;
969 /* Configure the encoder one-to-one, position at 0, ignore errors */
970 sd_sel.r.width = geo.output.width;
971 sd_sel.r.height = geo.output.height;
973 * We first issue a S_SELECTION, so that the subsequent S_FMT delivers the
974 * final encoder configuration.
976 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
977 set_selection, NULL, &sd_sel);
978 format.format.width = geo.output.width;
979 format.format.height = geo.output.height;
980 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
981 set_fmt, NULL, &format);
982 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
987 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH ||
988 (unsigned)format.format.height > img_height_max ||
989 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8)
992 geo.output.width = format.format.width;
993 geo.output.height = format.format.height;
996 * No down-scaling. According to the API, current call has precedence:
997 * https://linuxtv.org/downloads/v4l-dvb-apis/uapi/v4l/crop.html#cropping-structures
999 vou_adjust_input(&geo, vou_dev->std);
1001 /* We tried to preserve output rectangle, but it could have changed */
1002 vou_dev->rect = geo.output;
1003 pix->width = geo.in_width;
1004 pix->height = geo.in_height;
1006 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1007 geo.scale_idx_h, geo.scale_idx_v);
1012 static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1014 struct sh_vou_device *vou_dev = dev_id;
1015 static unsigned long j;
1016 struct sh_vou_buffer *vb;
1018 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1019 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1021 if (!(irq_status & 0x300)) {
1022 if (printk_timed_ratelimit(&j, 500))
1023 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1028 spin_lock(&vou_dev->lock);
1029 if (!vou_dev->active || list_empty(&vou_dev->buf_list)) {
1030 if (printk_timed_ratelimit(&j, 500))
1031 dev_warn(vou_dev->v4l2_dev.dev,
1032 "IRQ without active buffer: %x!\n", irq_status);
1033 /* Just ack: buf_release will disable further interrupts */
1034 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1035 spin_unlock(&vou_dev->lock);
1039 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1040 dev_dbg(vou_dev->v4l2_dev.dev,
1041 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1042 irq_status, masked, vou_status, cnt);
1045 /* side = vou_status & 0x10000; */
1047 /* Clear only set interrupts */
1048 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1050 vb = vou_dev->active;
1051 if (list_is_singular(&vb->list)) {
1052 /* Keep cycling while no next buffer is available */
1053 sh_vou_schedule_next(vou_dev, &vb->vb);
1054 spin_unlock(&vou_dev->lock);
1058 list_del(&vb->list);
1060 vb->vb.vb2_buf.timestamp = ktime_get_ns();
1061 vb->vb.sequence = vou_dev->sequence++;
1062 vb->vb.field = V4L2_FIELD_INTERLACED;
1063 vb2_buffer_done(&vb->vb.vb2_buf, VB2_BUF_STATE_DONE);
1065 vou_dev->active = list_entry(vou_dev->buf_list.next,
1066 struct sh_vou_buffer, list);
1068 if (list_is_singular(&vou_dev->buf_list)) {
1069 /* Keep cycling while no next buffer is available */
1070 sh_vou_schedule_next(vou_dev, &vou_dev->active->vb);
1072 struct sh_vou_buffer *new = list_entry(vou_dev->active->list.next,
1073 struct sh_vou_buffer, list);
1074 sh_vou_schedule_next(vou_dev, &new->vb);
1077 spin_unlock(&vou_dev->lock);
1082 static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1084 struct sh_vou_pdata *pdata = vou_dev->pdata;
1085 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1088 /* Disable all IRQs */
1089 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1091 /* Reset VOU interfaces - registers unaffected */
1092 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1093 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1099 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1101 if (pdata->flags & SH_VOU_PCLK_FALLING)
1103 if (pdata->flags & SH_VOU_HSYNC_LOW)
1105 if (pdata->flags & SH_VOU_VSYNC_LOW)
1107 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1109 /* Manual register side switching at first */
1110 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1111 /* Default - fixed HSYNC length, can be made configurable is required */
1112 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1114 sh_vou_set_fmt_vid_out(vou_dev, &vou_dev->pix);
1119 /* File operations */
1120 static int sh_vou_open(struct file *file)
1122 struct sh_vou_device *vou_dev = video_drvdata(file);
1125 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1126 return -ERESTARTSYS;
1128 err = v4l2_fh_open(file);
1131 if (v4l2_fh_is_singular_file(file) &&
1132 vou_dev->status == SH_VOU_INITIALISING) {
1134 err = pm_runtime_resume_and_get(vou_dev->v4l2_dev.dev);
1136 v4l2_fh_release(file);
1139 err = sh_vou_hw_init(vou_dev);
1141 pm_runtime_put(vou_dev->v4l2_dev.dev);
1142 v4l2_fh_release(file);
1144 vou_dev->status = SH_VOU_IDLE;
1148 mutex_unlock(&vou_dev->fop_lock);
1152 static int sh_vou_release(struct file *file)
1154 struct sh_vou_device *vou_dev = video_drvdata(file);
1157 mutex_lock(&vou_dev->fop_lock);
1158 is_last = v4l2_fh_is_singular_file(file);
1159 _vb2_fop_release(file, NULL);
1162 vou_dev->status = SH_VOU_INITIALISING;
1163 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1164 pm_runtime_put(vou_dev->v4l2_dev.dev);
1166 mutex_unlock(&vou_dev->fop_lock);
1170 /* sh_vou display ioctl operations */
1171 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1172 .vidioc_querycap = sh_vou_querycap,
1173 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1174 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1175 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1176 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1177 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1178 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1179 .vidioc_querybuf = vb2_ioctl_querybuf,
1180 .vidioc_qbuf = vb2_ioctl_qbuf,
1181 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1182 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1183 .vidioc_streamon = vb2_ioctl_streamon,
1184 .vidioc_streamoff = vb2_ioctl_streamoff,
1185 .vidioc_expbuf = vb2_ioctl_expbuf,
1186 .vidioc_g_output = sh_vou_g_output,
1187 .vidioc_s_output = sh_vou_s_output,
1188 .vidioc_enum_output = sh_vou_enum_output,
1189 .vidioc_s_std = sh_vou_s_std,
1190 .vidioc_g_std = sh_vou_g_std,
1191 .vidioc_g_selection = sh_vou_g_selection,
1192 .vidioc_s_selection = sh_vou_s_selection,
1193 .vidioc_log_status = sh_vou_log_status,
1196 static const struct v4l2_file_operations sh_vou_fops = {
1197 .owner = THIS_MODULE,
1198 .open = sh_vou_open,
1199 .release = sh_vou_release,
1200 .unlocked_ioctl = video_ioctl2,
1201 .mmap = vb2_fop_mmap,
1202 .poll = vb2_fop_poll,
1203 .write = vb2_fop_write,
1206 static const struct video_device sh_vou_video_template = {
1208 .fops = &sh_vou_fops,
1209 .ioctl_ops = &sh_vou_ioctl_ops,
1210 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
1211 .vfl_dir = VFL_DIR_TX,
1212 .device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_READWRITE |
1216 static int sh_vou_probe(struct platform_device *pdev)
1218 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1219 struct v4l2_rect *rect;
1220 struct v4l2_pix_format *pix;
1221 struct i2c_adapter *i2c_adap;
1222 struct video_device *vdev;
1223 struct sh_vou_device *vou_dev;
1224 struct v4l2_subdev *subdev;
1225 struct vb2_queue *q;
1229 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1233 irq = platform_get_irq(pdev, 0);
1237 vou_dev = devm_kzalloc(&pdev->dev, sizeof(*vou_dev), GFP_KERNEL);
1241 INIT_LIST_HEAD(&vou_dev->buf_list);
1242 spin_lock_init(&vou_dev->lock);
1243 mutex_init(&vou_dev->fop_lock);
1244 vou_dev->pdata = vou_pdata;
1245 vou_dev->status = SH_VOU_INITIALISING;
1246 vou_dev->pix_idx = 1;
1248 rect = &vou_dev->rect;
1249 pix = &vou_dev->pix;
1251 /* Fill in defaults */
1252 vou_dev->std = V4L2_STD_NTSC_M;
1255 rect->width = VOU_MAX_IMAGE_WIDTH;
1257 pix->width = VOU_MAX_IMAGE_WIDTH;
1259 pix->pixelformat = V4L2_PIX_FMT_NV16;
1260 pix->field = V4L2_FIELD_INTERLACED;
1261 pix->bytesperline = VOU_MAX_IMAGE_WIDTH;
1262 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
1263 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1265 vou_dev->base = devm_platform_ioremap_resource(pdev, 0);
1266 if (IS_ERR(vou_dev->base))
1267 return PTR_ERR(vou_dev->base);
1269 ret = devm_request_irq(&pdev->dev, irq, sh_vou_isr, 0, "vou", vou_dev);
1273 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1275 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1279 vdev = &vou_dev->vdev;
1280 *vdev = sh_vou_video_template;
1281 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1282 vdev->tvnorms |= V4L2_STD_PAL;
1283 vdev->v4l2_dev = &vou_dev->v4l2_dev;
1284 vdev->release = video_device_release_empty;
1285 vdev->lock = &vou_dev->fop_lock;
1287 video_set_drvdata(vdev, vou_dev);
1289 /* Initialize the vb2 queue */
1290 q = &vou_dev->queue;
1291 q->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1292 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_WRITE;
1293 q->drv_priv = vou_dev;
1294 q->buf_struct_size = sizeof(struct sh_vou_buffer);
1295 q->ops = &sh_vou_qops;
1296 q->mem_ops = &vb2_dma_contig_memops;
1297 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1298 q->min_queued_buffers = 2;
1299 q->lock = &vou_dev->fop_lock;
1300 q->dev = &pdev->dev;
1301 ret = vb2_queue_init(q);
1306 INIT_LIST_HEAD(&vou_dev->buf_list);
1308 pm_runtime_enable(&pdev->dev);
1309 pm_runtime_resume(&pdev->dev);
1311 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1317 ret = sh_vou_hw_init(vou_dev);
1321 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1322 vou_pdata->board_info, NULL);
1328 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1337 i2c_put_adapter(i2c_adap);
1339 pm_runtime_disable(&pdev->dev);
1340 v4l2_device_unregister(&vou_dev->v4l2_dev);
1344 static void sh_vou_remove(struct platform_device *pdev)
1346 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1347 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1348 struct sh_vou_device, v4l2_dev);
1349 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1350 struct v4l2_subdev, list);
1351 struct i2c_client *client = v4l2_get_subdevdata(sd);
1353 pm_runtime_disable(&pdev->dev);
1354 video_unregister_device(&vou_dev->vdev);
1355 i2c_put_adapter(client->adapter);
1356 v4l2_device_unregister(&vou_dev->v4l2_dev);
1359 static struct platform_driver sh_vou = {
1360 .remove = sh_vou_remove,
1366 module_platform_driver_probe(sh_vou, sh_vou_probe);
1368 MODULE_DESCRIPTION("SuperH VOU driver");
1370 MODULE_LICENSE("GPL v2");
1371 MODULE_VERSION("0.1.0");
1372 MODULE_ALIAS("platform:sh-vou");