2 * SuperH Video Output Unit (VOU) driver
4 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/dma-mapping.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/slab.h>
22 #include <linux/version.h>
23 #include <linux/videodev2.h>
25 #include <media/sh_vou.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-device.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-mediabus.h>
30 #include <media/videobuf-dma-contig.h>
32 /* Mirror addresses are not available for all registers */
60 #define VOU_MAX_IMAGE_WIDTH 720
61 #define VOU_MAX_IMAGE_HEIGHT 576
63 struct sh_vou_device {
64 struct v4l2_device v4l2_dev;
65 struct video_device *vdev;
67 struct sh_vou_pdata *pdata;
70 /* State information */
71 struct v4l2_pix_format pix;
72 struct v4l2_rect rect;
73 struct list_head queue;
76 struct videobuf_buffer *active;
77 enum sh_vou_status status;
81 struct videobuf_queue vbq;
84 /* Register access routines for sides A, B and mirror addresses */
85 static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
88 __raw_writel(value, vou_dev->base + reg);
91 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
94 __raw_writel(value, vou_dev->base + reg);
95 __raw_writel(value, vou_dev->base + reg + 0x1000);
98 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
101 __raw_writel(value, vou_dev->base + reg + 0x2000);
104 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
106 return __raw_readl(vou_dev->base + reg);
109 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
112 u32 old = __raw_readl(vou_dev->base + reg);
114 value = (value & mask) | (old & ~mask);
115 __raw_writel(value, vou_dev->base + reg);
118 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
121 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
124 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
127 sh_vou_reg_a_set(vou_dev, reg, value, mask);
128 sh_vou_reg_b_set(vou_dev, reg, value, mask);
140 /* Further pixel formats can be added */
141 static struct sh_vou_fmt vou_fmt[] = {
143 .pfmt = V4L2_PIX_FMT_NV12,
145 .desc = "YVU420 planar",
150 .pfmt = V4L2_PIX_FMT_NV16,
152 .desc = "YVYU planar",
157 .pfmt = V4L2_PIX_FMT_RGB24,
164 .pfmt = V4L2_PIX_FMT_RGB565,
171 .pfmt = V4L2_PIX_FMT_RGB565X,
173 .desc = "RGB565 byteswapped",
179 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
180 struct videobuf_buffer *vb)
182 dma_addr_t addr1, addr2;
184 addr1 = videobuf_to_dma_contig(vb);
185 switch (vou_dev->pix.pixelformat) {
186 case V4L2_PIX_FMT_NV12:
187 case V4L2_PIX_FMT_NV16:
188 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
194 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
195 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
198 static void sh_vou_stream_start(struct sh_vou_device *vou_dev,
199 struct videobuf_buffer *vb)
201 unsigned int row_coeff;
202 #ifdef __LITTLE_ENDIAN
208 switch (vou_dev->pix.pixelformat) {
209 case V4L2_PIX_FMT_NV12:
210 case V4L2_PIX_FMT_NV16:
213 case V4L2_PIX_FMT_RGB565:
215 case V4L2_PIX_FMT_RGB565X:
218 case V4L2_PIX_FMT_RGB24:
223 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
224 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
225 sh_vou_schedule_next(vou_dev, vb);
228 static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
230 BUG_ON(in_interrupt());
232 /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
233 videobuf_waiton(vq, vb, 0, 0);
234 videobuf_dma_contig_free(vq, vb);
235 vb->state = VIDEOBUF_NEEDS_INIT;
238 /* Locking: caller holds vq->vb_lock mutex */
239 static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
242 struct video_device *vdev = vq->priv_data;
243 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
245 *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
246 vou_dev->pix.height / 8;
251 /* Taking into account maximum frame size, *count will stay >= 2 */
252 if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
253 *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
255 dev_dbg(vq->dev, "%s(): count=%d, size=%d\n", __func__, *count, *size);
260 /* Locking: caller holds vq->vb_lock mutex */
261 static int sh_vou_buf_prepare(struct videobuf_queue *vq,
262 struct videobuf_buffer *vb,
263 enum v4l2_field field)
265 struct video_device *vdev = vq->priv_data;
266 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
267 struct v4l2_pix_format *pix = &vou_dev->pix;
268 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
271 dev_dbg(vq->dev, "%s()\n", __func__);
273 if (vb->width != pix->width ||
274 vb->height != pix->height ||
275 vb->field != pix->field) {
276 vb->width = pix->width;
277 vb->height = pix->height;
279 if (vb->state != VIDEOBUF_NEEDS_INIT)
283 vb->size = vb->height * bytes_per_line;
284 if (vb->baddr && vb->bsize < vb->size) {
285 /* User buffer too small */
286 dev_warn(vq->dev, "User buffer too small: [%u] @ %lx\n",
287 vb->bsize, vb->baddr);
291 if (vb->state == VIDEOBUF_NEEDS_INIT) {
292 ret = videobuf_iolock(vq, vb, NULL);
294 dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
298 vb->state = VIDEOBUF_PREPARED;
302 "%s(): fmt #%d, %u bytes per line, phys 0x%x, type %d, state %d\n",
303 __func__, vou_dev->pix_idx, bytes_per_line,
304 videobuf_to_dma_contig(vb), vb->memory, vb->state);
309 /* Locking: caller holds vq->vb_lock mutex and vq->irqlock spinlock */
310 static void sh_vou_buf_queue(struct videobuf_queue *vq,
311 struct videobuf_buffer *vb)
313 struct video_device *vdev = vq->priv_data;
314 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
316 dev_dbg(vq->dev, "%s()\n", __func__);
318 vb->state = VIDEOBUF_QUEUED;
319 list_add_tail(&vb->queue, &vou_dev->queue);
321 if (vou_dev->status == SH_VOU_RUNNING) {
323 } else if (!vou_dev->active) {
324 vou_dev->active = vb;
325 /* Start from side A: we use mirror addresses, so, set B */
326 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
327 dev_dbg(vq->dev, "%s: first buffer status 0x%x\n", __func__,
328 sh_vou_reg_a_read(vou_dev, VOUSTR));
329 sh_vou_schedule_next(vou_dev, vb);
330 /* Only activate VOU after the second buffer */
331 } else if (vou_dev->active->queue.next == &vb->queue) {
332 /* Second buffer - initialise register side B */
333 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
334 sh_vou_stream_start(vou_dev, vb);
336 /* Register side switching with frame VSYNC */
337 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
338 dev_dbg(vq->dev, "%s: second buffer status 0x%x\n", __func__,
339 sh_vou_reg_a_read(vou_dev, VOUSTR));
341 /* Enable End-of-Frame (VSYNC) interrupts */
342 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
343 /* Two buffers on the queue - activate the hardware */
345 vou_dev->status = SH_VOU_RUNNING;
346 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
350 static void sh_vou_buf_release(struct videobuf_queue *vq,
351 struct videobuf_buffer *vb)
353 struct video_device *vdev = vq->priv_data;
354 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
357 dev_dbg(vq->dev, "%s()\n", __func__);
359 spin_lock_irqsave(&vou_dev->lock, flags);
361 if (vou_dev->active == vb) {
363 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
364 /* ...but the current frame will complete */
365 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
366 vou_dev->active = NULL;
369 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
370 vb->state = VIDEOBUF_ERROR;
371 list_del(&vb->queue);
374 spin_unlock_irqrestore(&vou_dev->lock, flags);
379 static struct videobuf_queue_ops sh_vou_video_qops = {
380 .buf_setup = sh_vou_buf_setup,
381 .buf_prepare = sh_vou_buf_prepare,
382 .buf_queue = sh_vou_buf_queue,
383 .buf_release = sh_vou_buf_release,
387 static int sh_vou_querycap(struct file *file, void *priv,
388 struct v4l2_capability *cap)
390 struct sh_vou_file *vou_file = priv;
392 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
394 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
395 cap->version = KERNEL_VERSION(0, 1, 0);
396 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
400 /* Enumerate formats, that the device can accept from the user */
401 static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
402 struct v4l2_fmtdesc *fmt)
404 struct sh_vou_file *vou_file = priv;
406 if (fmt->index >= ARRAY_SIZE(vou_fmt))
409 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
411 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
412 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
413 sizeof(fmt->description));
414 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
419 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
420 struct v4l2_format *fmt)
422 struct video_device *vdev = video_devdata(file);
423 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
425 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
427 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
428 fmt->fmt.pix = vou_dev->pix;
433 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
434 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
435 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
436 static const unsigned char vou_scale_v_num[] = {1, 2, 4};
437 static const unsigned char vou_scale_v_den[] = {1, 1, 1};
438 static const unsigned char vou_scale_v_fld[] = {0, 1};
440 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
441 int pix_idx, int w_idx, int h_idx)
443 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
444 unsigned int black_left, black_top, width_max, height_max,
445 frame_in_height, frame_out_height, frame_out_top;
446 struct v4l2_rect *rect = &vou_dev->rect;
447 struct v4l2_pix_format *pix = &vou_dev->pix;
448 u32 vouvcr = 0, dsr_h, dsr_v;
450 if (vou_dev->std & V4L2_STD_525_60) {
458 frame_in_height = pix->height / 2;
459 frame_out_height = rect->height / 2;
460 frame_out_top = rect->top / 2;
463 * Cropping scheme: max useful image is 720x480, and the total video
464 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
465 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
466 * of which the first 33 / 25 clocks HSYNC must be held active. This
467 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
468 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
469 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
470 * beyond DSR, specified on the left and top by the VPR register "black
471 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
472 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
473 * client requires, and that's exactly what leaves us 720 pixels for the
474 * image; we leave VPR[VVP] at default 20 for now, because the client
475 * doesn't seem to have any special requirements for it. Otherwise we
476 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
477 * the selected standard, and DPR and DSR are selected according to
478 * cropping. Q: how does the client detect the first valid line? Does
479 * HSYNC stay inactive during invalid (black) lines?
481 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
484 dsr_h = rect->width + rect->left;
485 dsr_v = frame_out_height + frame_out_top;
487 dev_dbg(vou_dev->v4l2_dev.dev,
488 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
489 pix->width, frame_in_height, black_left, black_top,
490 rect->left, frame_out_top, dsr_h, dsr_v);
492 /* VOUISR height - half of a frame height in frame mode */
493 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
494 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
495 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
496 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
499 * if necessary, we could set VOUHIR to
500 * max(black_left + dsr_h, width_max) here
504 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
506 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
508 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
510 /* To produce a colour bar for testing set bit 23 of VOUVCR */
511 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
512 sh_vou_reg_ab_write(vou_dev, VOUDFR,
513 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
516 struct sh_vou_geometry {
517 struct v4l2_rect output;
518 unsigned int in_width;
519 unsigned int in_height;
525 * Find input geometry, that we can use to produce output, closest to the
526 * requested rectangle, using VOU scaling
528 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
530 /* The compiler cannot know, that best and idx will indeed be set */
531 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
534 if (std & V4L2_STD_525_60)
535 img_height_max = 480;
537 img_height_max = 576;
539 /* Image width must be a multiple of 4 */
540 v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
541 &geo->in_height, 0, img_height_max, 1, 0);
543 /* Select scales to come as close as possible to the output image */
544 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
546 unsigned int found = geo->output.width * vou_scale_h_den[i] /
549 if (found > VOU_MAX_IMAGE_WIDTH)
550 /* scales increase */
553 err = abs(found - geo->in_width);
554 if (err < best_err) {
563 geo->in_width = best;
564 geo->scale_idx_h = idx;
568 /* This loop can be replaced with one division */
569 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
571 unsigned int found = geo->output.height * vou_scale_v_den[i] /
574 if (found > img_height_max)
575 /* scales increase */
578 err = abs(found - geo->in_height);
579 if (err < best_err) {
588 geo->in_height = best;
589 geo->scale_idx_v = idx;
593 * Find output geometry, that we can produce, using VOU scaling, closest to
594 * the requested rectangle
596 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
598 unsigned int best_err = UINT_MAX, best, width_max, height_max,
602 if (std & V4L2_STD_525_60) {
604 height_max = 262 * 2;
605 img_height_max = 480;
608 height_max = 312 * 2;
609 img_height_max = 576;
612 /* Select scales to come as close as possible to the output image */
613 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
615 unsigned int found = geo->in_width * vou_scale_h_num[i] /
618 if (found > VOU_MAX_IMAGE_WIDTH)
619 /* scales increase */
622 err = abs(found - geo->output.width);
623 if (err < best_err) {
632 geo->output.width = best;
633 geo->scale_idx_h = idx;
634 if (geo->output.left + best > width_max)
635 geo->output.left = width_max - best;
637 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
638 vou_scale_h_num[idx], vou_scale_h_den[idx], best);
642 /* This loop can be replaced with one division */
643 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
645 unsigned int found = geo->in_height * vou_scale_v_num[i] /
648 if (found > img_height_max)
649 /* scales increase */
652 err = abs(found - geo->output.height);
653 if (err < best_err) {
662 geo->output.height = best;
663 geo->scale_idx_v = idx;
664 if (geo->output.top + best > height_max)
665 geo->output.top = height_max - best;
667 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
668 vou_scale_v_num[idx], vou_scale_v_den[idx], best);
671 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
672 struct v4l2_format *fmt)
674 struct video_device *vdev = video_devdata(file);
675 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
676 struct v4l2_pix_format *pix = &fmt->fmt.pix;
677 unsigned int img_height_max;
679 struct sh_vou_geometry geo;
680 struct v4l2_mbus_framefmt mbfmt = {
681 /* Revisit: is this the correct code? */
682 .code = V4L2_MBUS_FMT_YUYV8_2X8,
683 .field = V4L2_FIELD_INTERLACED,
684 .colorspace = V4L2_COLORSPACE_SMPTE170M,
688 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
689 vou_dev->rect.width, vou_dev->rect.height,
690 pix->width, pix->height);
692 if (pix->field == V4L2_FIELD_ANY)
693 pix->field = V4L2_FIELD_NONE;
695 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
696 pix->field != V4L2_FIELD_NONE)
699 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
700 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
703 if (pix_idx == ARRAY_SIZE(vou_fmt))
706 if (vou_dev->std & V4L2_STD_525_60)
707 img_height_max = 480;
709 img_height_max = 576;
711 /* Image width must be a multiple of 4 */
712 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
713 &pix->height, 0, img_height_max, 1, 0);
715 geo.in_width = pix->width;
716 geo.in_height = pix->height;
717 geo.output = vou_dev->rect;
719 vou_adjust_output(&geo, vou_dev->std);
721 mbfmt.width = geo.output.width;
722 mbfmt.height = geo.output.height;
723 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
725 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
729 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
730 geo.output.width, geo.output.height, mbfmt.width, mbfmt.height);
733 if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
734 (unsigned)mbfmt.height > img_height_max ||
735 mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
738 if (mbfmt.width != geo.output.width ||
739 mbfmt.height != geo.output.height) {
740 geo.output.width = mbfmt.width;
741 geo.output.height = mbfmt.height;
743 vou_adjust_input(&geo, vou_dev->std);
746 /* We tried to preserve output rectangle, but it could have changed */
747 vou_dev->rect = geo.output;
748 pix->width = geo.in_width;
749 pix->height = geo.in_height;
751 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
752 pix->width, pix->height);
754 vou_dev->pix_idx = pix_idx;
758 sh_vou_configure_geometry(vou_dev, pix_idx,
759 geo.scale_idx_h, geo.scale_idx_v);
764 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
765 struct v4l2_format *fmt)
767 struct sh_vou_file *vou_file = priv;
768 struct v4l2_pix_format *pix = &fmt->fmt.pix;
771 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
773 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
774 pix->field = V4L2_FIELD_NONE;
776 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
777 &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
779 for (i = 0; ARRAY_SIZE(vou_fmt); i++)
780 if (vou_fmt[i].pfmt == pix->pixelformat)
783 pix->pixelformat = vou_fmt[0].pfmt;
788 static int sh_vou_reqbufs(struct file *file, void *priv,
789 struct v4l2_requestbuffers *req)
791 struct sh_vou_file *vou_file = priv;
793 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
795 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
798 return videobuf_reqbufs(&vou_file->vbq, req);
801 static int sh_vou_querybuf(struct file *file, void *priv,
802 struct v4l2_buffer *b)
804 struct sh_vou_file *vou_file = priv;
806 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
808 return videobuf_querybuf(&vou_file->vbq, b);
811 static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
813 struct sh_vou_file *vou_file = priv;
815 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
817 return videobuf_qbuf(&vou_file->vbq, b);
820 static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
822 struct sh_vou_file *vou_file = priv;
824 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
826 return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
829 static int sh_vou_streamon(struct file *file, void *priv,
830 enum v4l2_buf_type buftype)
832 struct video_device *vdev = video_devdata(file);
833 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
834 struct sh_vou_file *vou_file = priv;
837 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
839 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
841 if (ret < 0 && ret != -ENOIOCTLCMD)
844 /* This calls our .buf_queue() (== sh_vou_buf_queue) */
845 return videobuf_streamon(&vou_file->vbq);
848 static int sh_vou_streamoff(struct file *file, void *priv,
849 enum v4l2_buf_type buftype)
851 struct video_device *vdev = video_devdata(file);
852 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
853 struct sh_vou_file *vou_file = priv;
855 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
858 * This calls buf_release from host driver's videobuf_queue_ops for all
859 * remaining buffers. When the last buffer is freed, stop streaming
861 videobuf_streamoff(&vou_file->vbq);
862 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
867 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
871 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
873 case SH_VOU_BUS_8BIT:
875 case SH_VOU_BUS_16BIT:
877 case SH_VOU_BUS_BT656:
882 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
884 struct video_device *vdev = video_devdata(file);
885 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
888 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, *std_id);
890 if (*std_id & ~vdev->tvnorms)
893 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
894 s_std_output, *std_id);
895 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
896 if (ret < 0 && ret != -ENOIOCTLCMD)
899 if (*std_id & V4L2_STD_525_60)
900 sh_vou_reg_ab_set(vou_dev, VOUCR,
901 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
903 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
905 vou_dev->std = *std_id;
910 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
912 struct video_device *vdev = video_devdata(file);
913 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
915 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
922 static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
924 struct video_device *vdev = video_devdata(file);
925 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
927 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
929 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
930 a->c = vou_dev->rect;
935 /* Assume a dull encoder, do all the work ourselves. */
936 static int sh_vou_s_crop(struct file *file, void *fh, struct v4l2_crop *a)
938 struct video_device *vdev = video_devdata(file);
939 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
940 struct v4l2_rect *rect = &a->c;
941 struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
942 struct v4l2_pix_format *pix = &vou_dev->pix;
943 struct sh_vou_geometry geo;
944 struct v4l2_mbus_framefmt mbfmt = {
945 /* Revisit: is this the correct code? */
946 .code = V4L2_MBUS_FMT_YUYV8_2X8,
947 .field = V4L2_FIELD_INTERLACED,
948 .colorspace = V4L2_COLORSPACE_SMPTE170M,
950 unsigned int img_height_max;
953 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
954 rect->width, rect->height, rect->left, rect->top);
956 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
959 if (vou_dev->std & V4L2_STD_525_60)
960 img_height_max = 480;
962 img_height_max = 576;
964 v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
965 &rect->height, 0, img_height_max, 1, 0);
967 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
968 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
970 if (rect->height + rect->top > img_height_max)
971 rect->top = img_height_max - rect->height;
974 geo.in_width = pix->width;
975 geo.in_height = pix->height;
977 /* Configure the encoder one-to-one, position at 0, ignore errors */
978 sd_crop.c.width = geo.output.width;
979 sd_crop.c.height = geo.output.height;
981 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
982 * final encoder configuration.
984 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
986 mbfmt.width = geo.output.width;
987 mbfmt.height = geo.output.height;
988 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
990 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
995 if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
996 (unsigned)mbfmt.height > img_height_max ||
997 mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
1000 geo.output.width = mbfmt.width;
1001 geo.output.height = mbfmt.height;
1004 * No down-scaling. According to the API, current call has precedence:
1005 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1007 vou_adjust_input(&geo, vou_dev->std);
1009 /* We tried to preserve output rectangle, but it could have changed */
1010 vou_dev->rect = geo.output;
1011 pix->width = geo.in_width;
1012 pix->height = geo.in_height;
1014 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1015 geo.scale_idx_h, geo.scale_idx_v);
1021 * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
1022 * is the initial register values, height takes the interlaced format into
1023 * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
1024 * actually only meaningfully contain values <= 720 and <= 240 respectively, and
1025 * not <= 864 and <= 312.
1027 static int sh_vou_cropcap(struct file *file, void *priv,
1028 struct v4l2_cropcap *a)
1030 struct sh_vou_file *vou_file = priv;
1032 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1034 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1037 a->bounds.width = VOU_MAX_IMAGE_WIDTH;
1038 a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
1039 /* Default = max, set VOUDPR = 0, which is not hardware default */
1040 a->defrect.left = 0;
1042 a->defrect.width = VOU_MAX_IMAGE_WIDTH;
1043 a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
1044 a->pixelaspect.numerator = 1;
1045 a->pixelaspect.denominator = 1;
1050 static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1052 struct sh_vou_device *vou_dev = dev_id;
1053 static unsigned long j;
1054 struct videobuf_buffer *vb;
1057 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1058 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1060 if (!(irq_status & 0x300)) {
1061 if (printk_timed_ratelimit(&j, 500))
1062 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1067 spin_lock(&vou_dev->lock);
1068 if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1069 if (printk_timed_ratelimit(&j, 500))
1070 dev_warn(vou_dev->v4l2_dev.dev,
1071 "IRQ without active buffer: %x!\n", irq_status);
1072 /* Just ack: buf_release will disable further interrupts */
1073 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1074 spin_unlock(&vou_dev->lock);
1078 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1079 dev_dbg(vou_dev->v4l2_dev.dev,
1080 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1081 irq_status, masked, vou_status, cnt);
1084 side = vou_status & 0x10000;
1086 /* Clear only set interrupts */
1087 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1089 vb = vou_dev->active;
1090 list_del(&vb->queue);
1092 vb->state = VIDEOBUF_DONE;
1093 do_gettimeofday(&vb->ts);
1097 if (list_empty(&vou_dev->queue)) {
1099 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1101 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1102 vou_dev->active = NULL;
1103 vou_dev->status = SH_VOU_INITIALISING;
1104 /* Disable End-of-Frame (VSYNC) interrupts */
1105 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1106 spin_unlock(&vou_dev->lock);
1110 vou_dev->active = list_entry(vou_dev->queue.next,
1111 struct videobuf_buffer, queue);
1113 if (vou_dev->active->queue.next != &vou_dev->queue) {
1114 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1115 struct videobuf_buffer, queue);
1116 sh_vou_schedule_next(vou_dev, new);
1119 spin_unlock(&vou_dev->lock);
1124 static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1126 struct sh_vou_pdata *pdata = vou_dev->pdata;
1127 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1130 /* Disable all IRQs */
1131 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1133 /* Reset VOU interfaces - registers unaffected */
1134 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1135 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1141 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1143 if (pdata->flags & SH_VOU_PCLK_FALLING)
1145 if (pdata->flags & SH_VOU_HSYNC_LOW)
1147 if (pdata->flags & SH_VOU_VSYNC_LOW)
1149 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1151 /* Manual register side switching at first */
1152 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1153 /* Default - fixed HSYNC length, can be made configurable is required */
1154 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1159 /* File operations */
1160 static int sh_vou_open(struct file *file)
1162 struct video_device *vdev = video_devdata(file);
1163 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1164 struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1170 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1172 file->private_data = vou_file;
1174 if (atomic_inc_return(&vou_dev->use_count) == 1) {
1177 vou_dev->status = SH_VOU_INITIALISING;
1178 pm_runtime_get_sync(vdev->v4l2_dev->dev);
1179 ret = sh_vou_hw_init(vou_dev);
1181 atomic_dec(&vou_dev->use_count);
1182 pm_runtime_put(vdev->v4l2_dev->dev);
1183 vou_dev->status = SH_VOU_IDLE;
1188 videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1189 vou_dev->v4l2_dev.dev, &vou_dev->lock,
1190 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1192 sizeof(struct videobuf_buffer), vdev,
1198 static int sh_vou_release(struct file *file)
1200 struct video_device *vdev = video_devdata(file);
1201 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1202 struct sh_vou_file *vou_file = file->private_data;
1204 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1206 if (!atomic_dec_return(&vou_dev->use_count)) {
1208 vou_dev->status = SH_VOU_IDLE;
1209 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1210 pm_runtime_put(vdev->v4l2_dev->dev);
1213 file->private_data = NULL;
1219 static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1221 struct sh_vou_file *vou_file = file->private_data;
1223 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1225 return videobuf_mmap_mapper(&vou_file->vbq, vma);
1228 static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1230 struct sh_vou_file *vou_file = file->private_data;
1232 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1234 return videobuf_poll_stream(file, &vou_file->vbq, wait);
1237 static int sh_vou_g_chip_ident(struct file *file, void *fh,
1238 struct v4l2_dbg_chip_ident *id)
1240 struct video_device *vdev = video_devdata(file);
1241 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1243 return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_chip_ident, id);
1246 #ifdef CONFIG_VIDEO_ADV_DEBUG
1247 static int sh_vou_g_register(struct file *file, void *fh,
1248 struct v4l2_dbg_register *reg)
1250 struct video_device *vdev = video_devdata(file);
1251 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1253 return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_register, reg);
1256 static int sh_vou_s_register(struct file *file, void *fh,
1257 struct v4l2_dbg_register *reg)
1259 struct video_device *vdev = video_devdata(file);
1260 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1262 return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, s_register, reg);
1266 /* sh_vou display ioctl operations */
1267 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1268 .vidioc_querycap = sh_vou_querycap,
1269 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1270 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1271 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1272 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1273 .vidioc_reqbufs = sh_vou_reqbufs,
1274 .vidioc_querybuf = sh_vou_querybuf,
1275 .vidioc_qbuf = sh_vou_qbuf,
1276 .vidioc_dqbuf = sh_vou_dqbuf,
1277 .vidioc_streamon = sh_vou_streamon,
1278 .vidioc_streamoff = sh_vou_streamoff,
1279 .vidioc_s_std = sh_vou_s_std,
1280 .vidioc_g_std = sh_vou_g_std,
1281 .vidioc_cropcap = sh_vou_cropcap,
1282 .vidioc_g_crop = sh_vou_g_crop,
1283 .vidioc_s_crop = sh_vou_s_crop,
1284 .vidioc_g_chip_ident = sh_vou_g_chip_ident,
1285 #ifdef CONFIG_VIDEO_ADV_DEBUG
1286 .vidioc_g_register = sh_vou_g_register,
1287 .vidioc_s_register = sh_vou_s_register,
1291 static const struct v4l2_file_operations sh_vou_fops = {
1292 .owner = THIS_MODULE,
1293 .open = sh_vou_open,
1294 .release = sh_vou_release,
1295 .ioctl = video_ioctl2,
1296 .mmap = sh_vou_mmap,
1297 .poll = sh_vou_poll,
1300 static const struct video_device sh_vou_video_template = {
1302 .fops = &sh_vou_fops,
1303 .ioctl_ops = &sh_vou_ioctl_ops,
1304 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
1305 .current_norm = V4L2_STD_NTSC_M,
1308 static int __devinit sh_vou_probe(struct platform_device *pdev)
1310 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1311 struct v4l2_rect *rect;
1312 struct v4l2_pix_format *pix;
1313 struct i2c_adapter *i2c_adap;
1314 struct video_device *vdev;
1315 struct sh_vou_device *vou_dev;
1316 struct resource *reg_res, *region;
1317 struct v4l2_subdev *subdev;
1320 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1321 irq = platform_get_irq(pdev, 0);
1323 if (!vou_pdata || !reg_res || irq <= 0) {
1324 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1328 vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
1332 INIT_LIST_HEAD(&vou_dev->queue);
1333 spin_lock_init(&vou_dev->lock);
1334 atomic_set(&vou_dev->use_count, 0);
1335 vou_dev->pdata = vou_pdata;
1336 vou_dev->status = SH_VOU_IDLE;
1338 rect = &vou_dev->rect;
1339 pix = &vou_dev->pix;
1341 /* Fill in defaults */
1342 vou_dev->std = sh_vou_video_template.current_norm;
1345 rect->width = VOU_MAX_IMAGE_WIDTH;
1347 pix->width = VOU_MAX_IMAGE_WIDTH;
1349 pix->pixelformat = V4L2_PIX_FMT_YVYU;
1350 pix->field = V4L2_FIELD_NONE;
1351 pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
1352 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
1353 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1355 region = request_mem_region(reg_res->start, resource_size(reg_res),
1358 dev_err(&pdev->dev, "VOU region already claimed\n");
1363 vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
1364 if (!vou_dev->base) {
1369 ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
1373 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1375 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1379 /* Allocate memory for video device */
1380 vdev = video_device_alloc();
1386 *vdev = sh_vou_video_template;
1387 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1388 vdev->tvnorms |= V4L2_STD_PAL;
1389 vdev->v4l2_dev = &vou_dev->v4l2_dev;
1390 vdev->release = video_device_release;
1392 vou_dev->vdev = vdev;
1393 video_set_drvdata(vdev, vou_dev);
1395 pm_runtime_enable(&pdev->dev);
1396 pm_runtime_resume(&pdev->dev);
1398 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1404 ret = sh_vou_hw_init(vou_dev);
1408 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1409 NULL, vou_pdata->board_info, NULL);
1415 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1424 i2c_put_adapter(i2c_adap);
1426 video_device_release(vdev);
1427 pm_runtime_disable(&pdev->dev);
1429 v4l2_device_unregister(&vou_dev->v4l2_dev);
1431 free_irq(irq, vou_dev);
1433 iounmap(vou_dev->base);
1435 release_mem_region(reg_res->start, resource_size(reg_res));
1441 static int __devexit sh_vou_remove(struct platform_device *pdev)
1443 int irq = platform_get_irq(pdev, 0);
1444 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1445 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1446 struct sh_vou_device, v4l2_dev);
1447 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1448 struct v4l2_subdev, list);
1449 struct i2c_client *client = v4l2_get_subdevdata(sd);
1450 struct resource *reg_res;
1453 free_irq(irq, vou_dev);
1454 pm_runtime_disable(&pdev->dev);
1455 video_unregister_device(vou_dev->vdev);
1456 i2c_put_adapter(client->adapter);
1457 v4l2_device_unregister(&vou_dev->v4l2_dev);
1458 iounmap(vou_dev->base);
1459 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1461 release_mem_region(reg_res->start, resource_size(reg_res));
1466 static struct platform_driver __refdata sh_vou = {
1467 .remove = __devexit_p(sh_vou_remove),
1470 .owner = THIS_MODULE,
1474 static int __init sh_vou_init(void)
1476 return platform_driver_probe(&sh_vou, sh_vou_probe);
1479 static void __exit sh_vou_exit(void)
1481 platform_driver_unregister(&sh_vou);
1484 module_init(sh_vou_init);
1485 module_exit(sh_vou_exit);
1487 MODULE_DESCRIPTION("SuperH VOU driver");
1488 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1489 MODULE_LICENSE("GPL v2");
1490 MODULE_ALIAS("platform:sh-vou");