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/version.h>
22 #include <linux/videodev2.h>
24 #include <media/sh_vou.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-device.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-mediabus.h>
29 #include <media/videobuf-dma-contig.h>
31 /* Mirror addresses are not available for all registers */
59 #define VOU_MAX_IMAGE_WIDTH 720
60 #define VOU_MAX_IMAGE_HEIGHT 480
62 struct sh_vou_device {
63 struct v4l2_device v4l2_dev;
64 struct video_device *vdev;
66 struct sh_vou_pdata *pdata;
69 /* State information */
70 struct v4l2_pix_format pix;
71 struct v4l2_rect rect;
72 struct list_head queue;
75 struct videobuf_buffer *active;
76 enum sh_vou_status status;
80 struct videobuf_queue vbq;
83 /* Register access routines for sides A, B and mirror addresses */
84 static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
87 __raw_writel(value, vou_dev->base + reg);
90 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
93 __raw_writel(value, vou_dev->base + reg);
94 __raw_writel(value, vou_dev->base + reg + 0x1000);
97 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
100 __raw_writel(value, vou_dev->base + reg + 0x2000);
103 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
105 return __raw_readl(vou_dev->base + reg);
108 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
111 u32 old = __raw_readl(vou_dev->base + reg);
113 value = (value & mask) | (old & ~mask);
114 __raw_writel(value, vou_dev->base + reg);
117 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
120 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
123 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
126 sh_vou_reg_a_set(vou_dev, reg, value, mask);
127 sh_vou_reg_b_set(vou_dev, reg, value, mask);
139 /* Further pixel formats can be added */
140 static struct sh_vou_fmt vou_fmt[] = {
142 .pfmt = V4L2_PIX_FMT_NV12,
144 .desc = "YVU420 planar",
149 .pfmt = V4L2_PIX_FMT_NV16,
151 .desc = "YVYU planar",
156 .pfmt = V4L2_PIX_FMT_RGB24,
163 .pfmt = V4L2_PIX_FMT_RGB565,
170 .pfmt = V4L2_PIX_FMT_RGB565X,
172 .desc = "RGB565 byteswapped",
178 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
179 struct videobuf_buffer *vb)
181 dma_addr_t addr1, addr2;
183 addr1 = videobuf_to_dma_contig(vb);
184 switch (vou_dev->pix.pixelformat) {
185 case V4L2_PIX_FMT_NV12:
186 case V4L2_PIX_FMT_NV16:
187 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
193 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
194 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
197 static void sh_vou_stream_start(struct sh_vou_device *vou_dev,
198 struct videobuf_buffer *vb)
200 unsigned int row_coeff;
201 #ifdef __LITTLE_ENDIAN
207 switch (vou_dev->pix.pixelformat) {
208 case V4L2_PIX_FMT_NV12:
209 case V4L2_PIX_FMT_NV16:
212 case V4L2_PIX_FMT_RGB565:
214 case V4L2_PIX_FMT_RGB565X:
217 case V4L2_PIX_FMT_RGB24:
222 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
223 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
224 sh_vou_schedule_next(vou_dev, vb);
227 static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
229 BUG_ON(in_interrupt());
231 /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
232 videobuf_waiton(vb, 0, 0);
233 videobuf_dma_contig_free(vq, vb);
234 vb->state = VIDEOBUF_NEEDS_INIT;
237 /* Locking: caller holds vq->vb_lock mutex */
238 static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
241 struct video_device *vdev = vq->priv_data;
242 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
244 *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
245 vou_dev->pix.height / 8;
250 /* Taking into account maximum frame size, *count will stay >= 2 */
251 if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
252 *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
254 dev_dbg(vq->dev, "%s(): count=%d, size=%d\n", __func__, *count, *size);
259 /* Locking: caller holds vq->vb_lock mutex */
260 static int sh_vou_buf_prepare(struct videobuf_queue *vq,
261 struct videobuf_buffer *vb,
262 enum v4l2_field field)
264 struct video_device *vdev = vq->priv_data;
265 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
266 struct v4l2_pix_format *pix = &vou_dev->pix;
267 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
270 dev_dbg(vq->dev, "%s()\n", __func__);
272 if (vb->width != pix->width ||
273 vb->height != pix->height ||
274 vb->field != pix->field) {
275 vb->width = pix->width;
276 vb->height = pix->height;
278 if (vb->state != VIDEOBUF_NEEDS_INIT)
282 vb->size = vb->height * bytes_per_line;
283 if (vb->baddr && vb->bsize < vb->size) {
284 /* User buffer too small */
285 dev_warn(vq->dev, "User buffer too small: [%u] @ %lx\n",
286 vb->bsize, vb->baddr);
290 if (vb->state == VIDEOBUF_NEEDS_INIT) {
291 ret = videobuf_iolock(vq, vb, NULL);
293 dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
297 vb->state = VIDEOBUF_PREPARED;
301 "%s(): fmt #%d, %u bytes per line, phys 0x%x, type %d, state %d\n",
302 __func__, vou_dev->pix_idx, bytes_per_line,
303 videobuf_to_dma_contig(vb), vb->memory, vb->state);
308 /* Locking: caller holds vq->vb_lock mutex and vq->irqlock spinlock */
309 static void sh_vou_buf_queue(struct videobuf_queue *vq,
310 struct videobuf_buffer *vb)
312 struct video_device *vdev = vq->priv_data;
313 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
315 dev_dbg(vq->dev, "%s()\n", __func__);
317 vb->state = VIDEOBUF_QUEUED;
318 list_add_tail(&vb->queue, &vou_dev->queue);
320 if (vou_dev->status == SH_VOU_RUNNING) {
322 } else if (!vou_dev->active) {
323 vou_dev->active = vb;
324 /* Start from side A: we use mirror addresses, so, set B */
325 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
326 dev_dbg(vq->dev, "%s: first buffer status 0x%x\n", __func__,
327 sh_vou_reg_a_read(vou_dev, VOUSTR));
328 sh_vou_schedule_next(vou_dev, vb);
329 /* Only activate VOU after the second buffer */
330 } else if (vou_dev->active->queue.next == &vb->queue) {
331 /* Second buffer - initialise register side B */
332 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
333 sh_vou_stream_start(vou_dev, vb);
335 /* Register side switching with frame VSYNC */
336 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
337 dev_dbg(vq->dev, "%s: second buffer status 0x%x\n", __func__,
338 sh_vou_reg_a_read(vou_dev, VOUSTR));
340 /* Enable End-of-Frame (VSYNC) interrupts */
341 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
342 /* Two buffers on the queue - activate the hardware */
344 vou_dev->status = SH_VOU_RUNNING;
345 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
349 static void sh_vou_buf_release(struct videobuf_queue *vq,
350 struct videobuf_buffer *vb)
352 struct video_device *vdev = vq->priv_data;
353 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
356 dev_dbg(vq->dev, "%s()\n", __func__);
358 spin_lock_irqsave(&vou_dev->lock, flags);
360 if (vou_dev->active == vb) {
362 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
363 /* ...but the current frame will complete */
364 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
365 vou_dev->active = NULL;
368 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
369 vb->state = VIDEOBUF_ERROR;
370 list_del(&vb->queue);
373 spin_unlock_irqrestore(&vou_dev->lock, flags);
378 static struct videobuf_queue_ops sh_vou_video_qops = {
379 .buf_setup = sh_vou_buf_setup,
380 .buf_prepare = sh_vou_buf_prepare,
381 .buf_queue = sh_vou_buf_queue,
382 .buf_release = sh_vou_buf_release,
386 static int sh_vou_querycap(struct file *file, void *priv,
387 struct v4l2_capability *cap)
389 struct sh_vou_file *vou_file = priv;
391 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
393 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
394 cap->version = KERNEL_VERSION(0, 1, 0);
395 cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
399 /* Enumerate formats, that the device can accept from the user */
400 static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
401 struct v4l2_fmtdesc *fmt)
403 struct sh_vou_file *vou_file = priv;
405 if (fmt->index >= ARRAY_SIZE(vou_fmt))
408 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
410 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
411 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
412 sizeof(fmt->description));
413 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
418 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
419 struct v4l2_format *fmt)
421 struct video_device *vdev = video_devdata(file);
422 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
424 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
426 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
427 fmt->fmt.pix = vou_dev->pix;
432 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
433 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
434 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
435 static const unsigned char vou_scale_v_num[] = {1, 2, 4};
436 static const unsigned char vou_scale_v_den[] = {1, 1, 1};
437 static const unsigned char vou_scale_v_fld[] = {0, 1};
439 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
440 int pix_idx, int w_idx, int h_idx)
442 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
443 unsigned int black_left, black_top, width_max, height_max,
444 frame_in_height, frame_out_height, frame_out_top;
445 struct v4l2_rect *rect = &vou_dev->rect;
446 struct v4l2_pix_format *pix = &vou_dev->pix;
447 u32 vouvcr = 0, dsr_h, dsr_v;
449 if (vou_dev->std & V4L2_STD_525_60) {
457 frame_in_height = pix->height / 2;
458 frame_out_height = rect->height / 2;
459 frame_out_top = rect->top / 2;
462 * Cropping scheme: max useful image is 720x480, and the total video
463 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
464 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
465 * of which the first 33 / 25 clocks HSYNC must be held active. This
466 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
467 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
468 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
469 * beyond DSR, specified on the left and top by the VPR register "black
470 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
471 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
472 * client requires, and that's exactly what leaves us 720 pixels for the
473 * image; we leave VPR[VVP] at default 20 for now, because the client
474 * doesn't seem to have any special requirements for it. Otherwise we
475 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
476 * the selected standard, and DPR and DSR are selected according to
477 * cropping. Q: how does the client detect the first valid line? Does
478 * HSYNC stay inactive during invalid (black) lines?
480 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
483 dsr_h = rect->width + rect->left;
484 dsr_v = frame_out_height + frame_out_top;
486 dev_dbg(vou_dev->v4l2_dev.dev,
487 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
488 pix->width, frame_in_height, black_left, black_top,
489 rect->left, frame_out_top, dsr_h, dsr_v);
491 /* VOUISR height - half of a frame height in frame mode */
492 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
493 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
494 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
495 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
498 * if necessary, we could set VOUHIR to
499 * max(black_left + dsr_h, width_max) here
503 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
505 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
507 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
509 /* To produce a colour bar for testing set bit 23 of VOUVCR */
510 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
511 sh_vou_reg_ab_write(vou_dev, VOUDFR,
512 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
515 struct sh_vou_geometry {
516 struct v4l2_rect output;
517 unsigned int in_width;
518 unsigned int in_height;
524 * Find input geometry, that we can use to produce output, closest to the
525 * requested rectangle, using VOU scaling
527 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
529 /* The compiler cannot know, that best and idx will indeed be set */
530 unsigned int best_err = UINT_MAX, best = 0, width_max, height_max;
533 if (std & V4L2_STD_525_60) {
541 /* Image width must be a multiple of 4 */
542 v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
543 &geo->in_height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
545 /* Select scales to come as close as possible to the output image */
546 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
548 unsigned int found = geo->output.width * vou_scale_h_den[i] /
551 if (found > VOU_MAX_IMAGE_WIDTH)
552 /* scales increase */
555 err = abs(found - geo->in_width);
556 if (err < best_err) {
565 geo->in_width = best;
566 geo->scale_idx_h = idx;
570 /* This loop can be replaced with one division */
571 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
573 unsigned int found = geo->output.height * vou_scale_v_den[i] /
576 if (found > VOU_MAX_IMAGE_HEIGHT)
577 /* scales increase */
580 err = abs(found - geo->in_height);
581 if (err < best_err) {
590 geo->in_height = best;
591 geo->scale_idx_v = idx;
595 * Find output geometry, that we can produce, using VOU scaling, closest to
596 * the requested rectangle
598 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
600 unsigned int best_err = UINT_MAX, best, width_max, height_max;
603 if (std & V4L2_STD_525_60) {
605 height_max = 262 * 2;
608 height_max = 312 * 2;
611 /* Select scales to come as close as possible to the output image */
612 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
614 unsigned int found = geo->in_width * vou_scale_h_num[i] /
617 if (found > VOU_MAX_IMAGE_WIDTH)
618 /* scales increase */
621 err = abs(found - geo->output.width);
622 if (err < best_err) {
631 geo->output.width = best;
632 geo->scale_idx_h = idx;
633 if (geo->output.left + best > width_max)
634 geo->output.left = width_max - best;
636 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
637 vou_scale_h_num[idx], vou_scale_h_den[idx], best);
641 /* This loop can be replaced with one division */
642 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
644 unsigned int found = geo->in_height * vou_scale_v_num[i] /
647 if (found > VOU_MAX_IMAGE_HEIGHT)
648 /* scales increase */
651 err = abs(found - geo->output.height);
652 if (err < best_err) {
661 geo->output.height = best;
662 geo->scale_idx_v = idx;
663 if (geo->output.top + best > height_max)
664 geo->output.top = height_max - best;
666 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
667 vou_scale_v_num[idx], vou_scale_v_den[idx], best);
670 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
671 struct v4l2_format *fmt)
673 struct video_device *vdev = video_devdata(file);
674 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
675 struct v4l2_pix_format *pix = &fmt->fmt.pix;
677 struct sh_vou_geometry geo;
678 struct v4l2_mbus_framefmt mbfmt = {
679 /* Revisit: is this the correct code? */
680 .code = V4L2_MBUS_FMT_YUYV8_2X8_LE,
681 .field = V4L2_FIELD_INTERLACED,
682 .colorspace = V4L2_COLORSPACE_SMPTE170M,
686 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
687 vou_dev->rect.width, vou_dev->rect.height,
688 pix->width, pix->height);
690 if (pix->field == V4L2_FIELD_ANY)
691 pix->field = V4L2_FIELD_NONE;
693 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
694 pix->field != V4L2_FIELD_NONE)
697 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
698 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
701 if (pix_idx == ARRAY_SIZE(vou_fmt))
704 /* Image width must be a multiple of 4 */
705 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
706 &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
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, video,
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);
726 if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
727 (unsigned)mbfmt.height > VOU_MAX_IMAGE_HEIGHT ||
728 mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8_LE)
731 if (mbfmt.width != geo.output.width ||
732 mbfmt.height != geo.output.height) {
733 geo.output.width = mbfmt.width;
734 geo.output.height = mbfmt.height;
736 vou_adjust_input(&geo, vou_dev->std);
739 /* We tried to preserve output rectangle, but it could have changed */
740 vou_dev->rect = geo.output;
741 pix->width = geo.in_width;
742 pix->height = geo.in_height;
744 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
745 pix->width, pix->height);
747 vou_dev->pix_idx = pix_idx;
751 sh_vou_configure_geometry(vou_dev, pix_idx,
752 geo.scale_idx_h, geo.scale_idx_v);
757 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
758 struct v4l2_format *fmt)
760 struct sh_vou_file *vou_file = priv;
761 struct v4l2_pix_format *pix = &fmt->fmt.pix;
764 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
766 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
767 pix->field = V4L2_FIELD_NONE;
769 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
770 &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
772 for (i = 0; ARRAY_SIZE(vou_fmt); i++)
773 if (vou_fmt[i].pfmt == pix->pixelformat)
776 pix->pixelformat = vou_fmt[0].pfmt;
781 static int sh_vou_reqbufs(struct file *file, void *priv,
782 struct v4l2_requestbuffers *req)
784 struct sh_vou_file *vou_file = priv;
786 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
788 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
791 return videobuf_reqbufs(&vou_file->vbq, req);
794 static int sh_vou_querybuf(struct file *file, void *priv,
795 struct v4l2_buffer *b)
797 struct sh_vou_file *vou_file = priv;
799 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
801 return videobuf_querybuf(&vou_file->vbq, b);
804 static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
806 struct sh_vou_file *vou_file = priv;
808 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
810 return videobuf_qbuf(&vou_file->vbq, b);
813 static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
815 struct sh_vou_file *vou_file = priv;
817 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
819 return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
822 static int sh_vou_streamon(struct file *file, void *priv,
823 enum v4l2_buf_type buftype)
825 struct video_device *vdev = video_devdata(file);
826 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
827 struct sh_vou_file *vou_file = priv;
830 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
832 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
834 if (ret < 0 && ret != -ENOIOCTLCMD)
837 /* This calls our .buf_queue() (== sh_vou_buf_queue) */
838 return videobuf_streamon(&vou_file->vbq);
841 static int sh_vou_streamoff(struct file *file, void *priv,
842 enum v4l2_buf_type buftype)
844 struct video_device *vdev = video_devdata(file);
845 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
846 struct sh_vou_file *vou_file = priv;
848 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
851 * This calls buf_release from host driver's videobuf_queue_ops for all
852 * remaining buffers. When the last buffer is freed, stop streaming
854 videobuf_streamoff(&vou_file->vbq);
855 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
860 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
864 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
866 case SH_VOU_BUS_8BIT:
868 case SH_VOU_BUS_16BIT:
870 case SH_VOU_BUS_BT656:
875 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
877 struct video_device *vdev = video_devdata(file);
878 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
881 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, *std_id);
883 if (*std_id & ~vdev->tvnorms)
886 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
887 s_std_output, *std_id);
888 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
889 if (ret < 0 && ret != -ENOIOCTLCMD)
892 if (*std_id & V4L2_STD_525_60)
893 sh_vou_reg_ab_set(vou_dev, VOUCR,
894 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
896 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
898 vou_dev->std = *std_id;
903 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
905 struct video_device *vdev = video_devdata(file);
906 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
908 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
915 static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
917 struct video_device *vdev = video_devdata(file);
918 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
920 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
922 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
923 a->c = vou_dev->rect;
928 /* Assume a dull encoder, do all the work ourselves. */
929 static int sh_vou_s_crop(struct file *file, void *fh, struct v4l2_crop *a)
931 struct video_device *vdev = video_devdata(file);
932 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
933 struct v4l2_rect *rect = &a->c;
934 struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
935 struct v4l2_pix_format *pix = &vou_dev->pix;
936 struct sh_vou_geometry geo;
937 struct v4l2_mbus_framefmt mbfmt = {
938 /* Revisit: is this the correct code? */
939 .code = V4L2_MBUS_FMT_YUYV8_2X8_LE,
940 .field = V4L2_FIELD_INTERLACED,
941 .colorspace = V4L2_COLORSPACE_SMPTE170M,
945 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
946 rect->width, rect->height, rect->left, rect->top);
948 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
951 v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
952 &rect->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
954 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
955 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
957 if (rect->height + rect->top > VOU_MAX_IMAGE_HEIGHT)
958 rect->top = VOU_MAX_IMAGE_HEIGHT - rect->height;
961 geo.in_width = pix->width;
962 geo.in_height = pix->height;
964 /* Configure the encoder one-to-one, position at 0, ignore errors */
965 sd_crop.c.width = geo.output.width;
966 sd_crop.c.height = geo.output.height;
968 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
969 * final encoder configuration.
971 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
973 mbfmt.width = geo.output.width;
974 mbfmt.height = geo.output.height;
975 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
977 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
982 if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
983 (unsigned)mbfmt.height > VOU_MAX_IMAGE_HEIGHT ||
984 mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8_LE)
987 geo.output.width = mbfmt.width;
988 geo.output.height = mbfmt.height;
991 * No down-scaling. According to the API, current call has precedence:
992 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
994 vou_adjust_input(&geo, vou_dev->std);
996 /* We tried to preserve output rectangle, but it could have changed */
997 vou_dev->rect = geo.output;
998 pix->width = geo.in_width;
999 pix->height = geo.in_height;
1001 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1002 geo.scale_idx_h, geo.scale_idx_v);
1008 * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
1009 * is the initial register values, height takes the interlaced format into
1010 * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
1011 * actually only meaningfully contain values <= 720 and <= 240 respectively, and
1012 * not <= 864 and <= 312.
1014 static int sh_vou_cropcap(struct file *file, void *priv,
1015 struct v4l2_cropcap *a)
1017 struct sh_vou_file *vou_file = priv;
1019 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1021 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1024 a->bounds.width = VOU_MAX_IMAGE_WIDTH;
1025 a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
1026 /* Default = max, set VOUDPR = 0, which is not hardware default */
1027 a->defrect.left = 0;
1029 a->defrect.width = VOU_MAX_IMAGE_WIDTH;
1030 a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
1031 a->pixelaspect.numerator = 1;
1032 a->pixelaspect.denominator = 1;
1037 static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1039 struct sh_vou_device *vou_dev = dev_id;
1040 static unsigned long j;
1041 struct videobuf_buffer *vb;
1044 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1045 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1047 if (!(irq_status & 0x300)) {
1048 if (printk_timed_ratelimit(&j, 500))
1049 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1054 spin_lock(&vou_dev->lock);
1055 if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1056 if (printk_timed_ratelimit(&j, 500))
1057 dev_warn(vou_dev->v4l2_dev.dev,
1058 "IRQ without active buffer: %x!\n", irq_status);
1059 /* Just ack: buf_release will disable further interrupts */
1060 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1061 spin_unlock(&vou_dev->lock);
1065 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1066 dev_dbg(vou_dev->v4l2_dev.dev,
1067 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1068 irq_status, masked, vou_status, cnt);
1071 side = vou_status & 0x10000;
1073 /* Clear only set interrupts */
1074 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1076 vb = vou_dev->active;
1077 list_del(&vb->queue);
1079 vb->state = VIDEOBUF_DONE;
1080 do_gettimeofday(&vb->ts);
1084 if (list_empty(&vou_dev->queue)) {
1086 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1088 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1089 vou_dev->active = NULL;
1090 vou_dev->status = SH_VOU_INITIALISING;
1091 /* Disable End-of-Frame (VSYNC) interrupts */
1092 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1093 spin_unlock(&vou_dev->lock);
1097 vou_dev->active = list_entry(vou_dev->queue.next,
1098 struct videobuf_buffer, queue);
1100 if (vou_dev->active->queue.next != &vou_dev->queue) {
1101 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1102 struct videobuf_buffer, queue);
1103 sh_vou_schedule_next(vou_dev, new);
1106 spin_unlock(&vou_dev->lock);
1111 static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1113 struct sh_vou_pdata *pdata = vou_dev->pdata;
1114 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1117 /* Disable all IRQs */
1118 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1120 /* Reset VOU interfaces - registers unaffected */
1121 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1122 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1128 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1130 if (pdata->flags & SH_VOU_PCLK_FALLING)
1132 if (pdata->flags & SH_VOU_HSYNC_LOW)
1134 if (pdata->flags & SH_VOU_VSYNC_LOW)
1136 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1138 /* Manual register side switching at first */
1139 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1140 /* Default - fixed HSYNC length, can be made configurable is required */
1141 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1146 /* File operations */
1147 static int sh_vou_open(struct file *file)
1149 struct video_device *vdev = video_devdata(file);
1150 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1151 struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1157 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1159 file->private_data = vou_file;
1161 if (atomic_inc_return(&vou_dev->use_count) == 1) {
1164 vou_dev->status = SH_VOU_INITIALISING;
1165 pm_runtime_get_sync(vdev->v4l2_dev->dev);
1166 ret = sh_vou_hw_init(vou_dev);
1168 atomic_dec(&vou_dev->use_count);
1169 pm_runtime_put(vdev->v4l2_dev->dev);
1170 vou_dev->status = SH_VOU_IDLE;
1175 videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1176 vou_dev->v4l2_dev.dev, &vou_dev->lock,
1177 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1179 sizeof(struct videobuf_buffer), vdev);
1184 static int sh_vou_release(struct file *file)
1186 struct video_device *vdev = video_devdata(file);
1187 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1188 struct sh_vou_file *vou_file = file->private_data;
1190 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1192 if (!atomic_dec_return(&vou_dev->use_count)) {
1194 vou_dev->status = SH_VOU_IDLE;
1195 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1196 pm_runtime_put(vdev->v4l2_dev->dev);
1199 file->private_data = NULL;
1205 static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1207 struct sh_vou_file *vou_file = file->private_data;
1209 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1211 return videobuf_mmap_mapper(&vou_file->vbq, vma);
1214 static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1216 struct sh_vou_file *vou_file = file->private_data;
1218 dev_dbg(vou_file->vbq.dev, "%s()\n", __func__);
1220 return videobuf_poll_stream(file, &vou_file->vbq, wait);
1223 static int sh_vou_g_chip_ident(struct file *file, void *fh,
1224 struct v4l2_dbg_chip_ident *id)
1226 struct video_device *vdev = video_devdata(file);
1227 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1229 return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_chip_ident, id);
1232 #ifdef CONFIG_VIDEO_ADV_DEBUG
1233 static int sh_vou_g_register(struct file *file, void *fh,
1234 struct v4l2_dbg_register *reg)
1236 struct video_device *vdev = video_devdata(file);
1237 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1239 return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, g_register, reg);
1242 static int sh_vou_s_register(struct file *file, void *fh,
1243 struct v4l2_dbg_register *reg)
1245 struct video_device *vdev = video_devdata(file);
1246 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
1248 return v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, core, s_register, reg);
1252 /* sh_vou display ioctl operations */
1253 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1254 .vidioc_querycap = sh_vou_querycap,
1255 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1256 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1257 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1258 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1259 .vidioc_reqbufs = sh_vou_reqbufs,
1260 .vidioc_querybuf = sh_vou_querybuf,
1261 .vidioc_qbuf = sh_vou_qbuf,
1262 .vidioc_dqbuf = sh_vou_dqbuf,
1263 .vidioc_streamon = sh_vou_streamon,
1264 .vidioc_streamoff = sh_vou_streamoff,
1265 .vidioc_s_std = sh_vou_s_std,
1266 .vidioc_g_std = sh_vou_g_std,
1267 .vidioc_cropcap = sh_vou_cropcap,
1268 .vidioc_g_crop = sh_vou_g_crop,
1269 .vidioc_s_crop = sh_vou_s_crop,
1270 .vidioc_g_chip_ident = sh_vou_g_chip_ident,
1271 #ifdef CONFIG_VIDEO_ADV_DEBUG
1272 .vidioc_g_register = sh_vou_g_register,
1273 .vidioc_s_register = sh_vou_s_register,
1277 static const struct v4l2_file_operations sh_vou_fops = {
1278 .owner = THIS_MODULE,
1279 .open = sh_vou_open,
1280 .release = sh_vou_release,
1281 .ioctl = video_ioctl2,
1282 .mmap = sh_vou_mmap,
1283 .poll = sh_vou_poll,
1286 static const struct video_device sh_vou_video_template = {
1288 .fops = &sh_vou_fops,
1289 .ioctl_ops = &sh_vou_ioctl_ops,
1290 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
1291 .current_norm = V4L2_STD_NTSC_M,
1294 static int __devinit sh_vou_probe(struct platform_device *pdev)
1296 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1297 struct v4l2_rect *rect;
1298 struct v4l2_pix_format *pix;
1299 struct i2c_adapter *i2c_adap;
1300 struct video_device *vdev;
1301 struct sh_vou_device *vou_dev;
1302 struct resource *reg_res, *region;
1303 struct v4l2_subdev *subdev;
1306 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1307 irq = platform_get_irq(pdev, 0);
1309 if (!vou_pdata || !reg_res || irq <= 0) {
1310 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1314 vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
1318 INIT_LIST_HEAD(&vou_dev->queue);
1319 spin_lock_init(&vou_dev->lock);
1320 atomic_set(&vou_dev->use_count, 0);
1321 vou_dev->pdata = vou_pdata;
1322 vou_dev->status = SH_VOU_IDLE;
1324 rect = &vou_dev->rect;
1325 pix = &vou_dev->pix;
1327 /* Fill in defaults */
1328 vou_dev->std = sh_vou_video_template.current_norm;
1331 rect->width = VOU_MAX_IMAGE_WIDTH;
1332 rect->height = VOU_MAX_IMAGE_HEIGHT;
1333 pix->width = VOU_MAX_IMAGE_WIDTH;
1334 pix->height = VOU_MAX_IMAGE_HEIGHT;
1335 pix->pixelformat = V4L2_PIX_FMT_YVYU;
1336 pix->field = V4L2_FIELD_NONE;
1337 pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
1338 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * VOU_MAX_IMAGE_HEIGHT;
1339 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1341 region = request_mem_region(reg_res->start, resource_size(reg_res),
1344 dev_err(&pdev->dev, "VOU region already claimed\n");
1349 vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
1350 if (!vou_dev->base) {
1355 ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
1359 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1361 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1365 /* Allocate memory for video device */
1366 vdev = video_device_alloc();
1372 *vdev = sh_vou_video_template;
1373 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1374 vdev->tvnorms |= V4L2_STD_PAL;
1375 vdev->v4l2_dev = &vou_dev->v4l2_dev;
1376 vdev->release = video_device_release;
1378 vou_dev->vdev = vdev;
1379 video_set_drvdata(vdev, vou_dev);
1381 pm_runtime_enable(&pdev->dev);
1382 pm_runtime_resume(&pdev->dev);
1384 i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1390 ret = sh_vou_hw_init(vou_dev);
1394 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1395 vou_pdata->module_name, vou_pdata->board_info, NULL);
1401 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1410 i2c_put_adapter(i2c_adap);
1412 video_device_release(vdev);
1413 pm_runtime_disable(&pdev->dev);
1415 v4l2_device_unregister(&vou_dev->v4l2_dev);
1417 free_irq(irq, vou_dev);
1419 iounmap(vou_dev->base);
1421 release_mem_region(reg_res->start, resource_size(reg_res));
1427 static int __devexit sh_vou_remove(struct platform_device *pdev)
1429 int irq = platform_get_irq(pdev, 0);
1430 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1431 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1432 struct sh_vou_device, v4l2_dev);
1433 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1434 struct v4l2_subdev, list);
1435 struct i2c_client *client = v4l2_get_subdevdata(sd);
1436 struct resource *reg_res;
1439 free_irq(irq, vou_dev);
1440 pm_runtime_disable(&pdev->dev);
1441 video_unregister_device(vou_dev->vdev);
1442 i2c_put_adapter(client->adapter);
1443 v4l2_device_unregister(&vou_dev->v4l2_dev);
1444 iounmap(vou_dev->base);
1445 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1447 release_mem_region(reg_res->start, resource_size(reg_res));
1452 static struct platform_driver __refdata sh_vou = {
1453 .remove = __devexit_p(sh_vou_remove),
1456 .owner = THIS_MODULE,
1460 static int __init sh_vou_init(void)
1462 return platform_driver_probe(&sh_vou, sh_vou_probe);
1465 static void __exit sh_vou_exit(void)
1467 platform_driver_unregister(&sh_vou);
1470 module_init(sh_vou_init);
1471 module_exit(sh_vou_exit);
1473 MODULE_DESCRIPTION("SuperH VOU driver");
1474 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1475 MODULE_LICENSE("GPL v2");
1476 MODULE_ALIAS("platform:sh-vou");