[media] sh-vou: fix bytesperline
[linux-2.6/btrfs-unstable.git] / drivers / media / platform / sh_vou.c
blob6cf80835e8baba848bf455cb32e6420a27c50a0b
1 /*
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.
9 */
11 #include <linux/dma-mapping.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/fs.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/videodev2.h>
23 #include <linux/module.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 */
33 #define VOUER 0
34 #define VOUCR 4
35 #define VOUSTR 8
36 #define VOUVCR 0xc
37 #define VOUISR 0x10
38 #define VOUBCR 0x14
39 #define VOUDPR 0x18
40 #define VOUDSR 0x1c
41 #define VOUVPR 0x20
42 #define VOUIR 0x24
43 #define VOUSRR 0x28
44 #define VOUMSR 0x2c
45 #define VOUHIR 0x30
46 #define VOUDFR 0x34
47 #define VOUAD1R 0x38
48 #define VOUAD2R 0x3c
49 #define VOUAIR 0x40
50 #define VOUSWR 0x44
51 #define VOURCR 0x48
52 #define VOURPR 0x50
54 enum sh_vou_status {
55 SH_VOU_IDLE,
56 SH_VOU_INITIALISING,
57 SH_VOU_RUNNING,
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;
66 struct sh_vou_pdata *pdata;
67 spinlock_t lock;
68 void __iomem *base;
69 /* State information */
70 struct v4l2_pix_format pix;
71 struct v4l2_rect rect;
72 struct list_head queue;
73 v4l2_std_id std;
74 int pix_idx;
75 struct videobuf_buffer *active;
76 enum sh_vou_status status;
77 struct mutex fop_lock;
80 struct sh_vou_file {
81 struct v4l2_fh fh;
82 struct videobuf_queue vbq;
85 /* Register access routines for sides A, B and mirror addresses */
86 static void sh_vou_reg_a_write(struct sh_vou_device *vou_dev, unsigned int reg,
87 u32 value)
89 __raw_writel(value, vou_dev->base + reg);
92 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
93 u32 value)
95 __raw_writel(value, vou_dev->base + reg);
96 __raw_writel(value, vou_dev->base + reg + 0x1000);
99 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
100 u32 value)
102 __raw_writel(value, vou_dev->base + reg + 0x2000);
105 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
107 return __raw_readl(vou_dev->base + reg);
110 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
111 u32 value, u32 mask)
113 u32 old = __raw_readl(vou_dev->base + reg);
115 value = (value & mask) | (old & ~mask);
116 __raw_writel(value, vou_dev->base + reg);
119 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
120 u32 value, u32 mask)
122 sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
125 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
126 u32 value, u32 mask)
128 sh_vou_reg_a_set(vou_dev, reg, value, mask);
129 sh_vou_reg_b_set(vou_dev, reg, value, mask);
132 struct sh_vou_fmt {
133 u32 pfmt;
134 char *desc;
135 unsigned char bpp;
136 unsigned char bpl;
137 unsigned char rgb;
138 unsigned char yf;
139 unsigned char pkf;
142 /* Further pixel formats can be added */
143 static struct sh_vou_fmt vou_fmt[] = {
145 .pfmt = V4L2_PIX_FMT_NV12,
146 .bpp = 12,
147 .bpl = 1,
148 .desc = "YVU420 planar",
149 .yf = 0,
150 .rgb = 0,
153 .pfmt = V4L2_PIX_FMT_NV16,
154 .bpp = 16,
155 .bpl = 1,
156 .desc = "YVYU planar",
157 .yf = 1,
158 .rgb = 0,
161 .pfmt = V4L2_PIX_FMT_RGB24,
162 .bpp = 24,
163 .bpl = 3,
164 .desc = "RGB24",
165 .pkf = 2,
166 .rgb = 1,
169 .pfmt = V4L2_PIX_FMT_RGB565,
170 .bpp = 16,
171 .bpl = 2,
172 .desc = "RGB565",
173 .pkf = 3,
174 .rgb = 1,
177 .pfmt = V4L2_PIX_FMT_RGB565X,
178 .bpp = 16,
179 .bpl = 2,
180 .desc = "RGB565 byteswapped",
181 .pkf = 3,
182 .rgb = 1,
186 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
187 struct videobuf_buffer *vb)
189 dma_addr_t addr1, addr2;
191 addr1 = videobuf_to_dma_contig(vb);
192 switch (vou_dev->pix.pixelformat) {
193 case V4L2_PIX_FMT_NV12:
194 case V4L2_PIX_FMT_NV16:
195 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
196 break;
197 default:
198 addr2 = 0;
201 sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
202 sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
205 static void sh_vou_stream_start(struct sh_vou_device *vou_dev,
206 struct videobuf_buffer *vb)
208 unsigned int row_coeff;
209 #ifdef __LITTLE_ENDIAN
210 u32 dataswap = 7;
211 #else
212 u32 dataswap = 0;
213 #endif
215 switch (vou_dev->pix.pixelformat) {
216 default:
217 case V4L2_PIX_FMT_NV12:
218 case V4L2_PIX_FMT_NV16:
219 row_coeff = 1;
220 break;
221 case V4L2_PIX_FMT_RGB565:
222 dataswap ^= 1;
223 case V4L2_PIX_FMT_RGB565X:
224 row_coeff = 2;
225 break;
226 case V4L2_PIX_FMT_RGB24:
227 row_coeff = 3;
228 break;
231 sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
232 sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
233 sh_vou_schedule_next(vou_dev, vb);
236 static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
238 BUG_ON(in_interrupt());
240 /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
241 videobuf_waiton(vq, vb, 0, 0);
242 videobuf_dma_contig_free(vq, vb);
243 vb->state = VIDEOBUF_NEEDS_INIT;
246 /* Locking: caller holds fop_lock mutex */
247 static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
248 unsigned int *size)
250 struct video_device *vdev = vq->priv_data;
251 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
253 *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
254 vou_dev->pix.height / 8;
256 if (*count < 2)
257 *count = 2;
259 /* Taking into account maximum frame size, *count will stay >= 2 */
260 if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
261 *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
263 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): count=%d, size=%d\n", __func__,
264 *count, *size);
266 return 0;
269 /* Locking: caller holds fop_lock mutex */
270 static int sh_vou_buf_prepare(struct videobuf_queue *vq,
271 struct videobuf_buffer *vb,
272 enum v4l2_field field)
274 struct video_device *vdev = vq->priv_data;
275 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
276 struct v4l2_pix_format *pix = &vou_dev->pix;
277 int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
278 int ret;
280 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
282 if (vb->width != pix->width ||
283 vb->height != pix->height ||
284 vb->field != pix->field) {
285 vb->width = pix->width;
286 vb->height = pix->height;
287 vb->field = field;
288 if (vb->state != VIDEOBUF_NEEDS_INIT)
289 free_buffer(vq, vb);
292 vb->size = vb->height * bytes_per_line;
293 if (vb->baddr && vb->bsize < vb->size) {
294 /* User buffer too small */
295 dev_warn(vq->dev, "User buffer too small: [%zu] @ %lx\n",
296 vb->bsize, vb->baddr);
297 return -EINVAL;
300 if (vb->state == VIDEOBUF_NEEDS_INIT) {
301 ret = videobuf_iolock(vq, vb, NULL);
302 if (ret < 0) {
303 dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
304 vb->memory, ret);
305 return ret;
307 vb->state = VIDEOBUF_PREPARED;
310 dev_dbg(vou_dev->v4l2_dev.dev,
311 "%s(): fmt #%d, %u bytes per line, phys %pad, type %d, state %d\n",
312 __func__, vou_dev->pix_idx, bytes_per_line,
313 ({ dma_addr_t addr = videobuf_to_dma_contig(vb); &addr; }),
314 vb->memory, vb->state);
316 return 0;
319 /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
320 static void sh_vou_buf_queue(struct videobuf_queue *vq,
321 struct videobuf_buffer *vb)
323 struct video_device *vdev = vq->priv_data;
324 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
326 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
328 vb->state = VIDEOBUF_QUEUED;
329 list_add_tail(&vb->queue, &vou_dev->queue);
331 if (vou_dev->status == SH_VOU_RUNNING) {
332 return;
333 } else if (!vou_dev->active) {
334 vou_dev->active = vb;
335 /* Start from side A: we use mirror addresses, so, set B */
336 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
337 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
338 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
339 sh_vou_schedule_next(vou_dev, vb);
340 /* Only activate VOU after the second buffer */
341 } else if (vou_dev->active->queue.next == &vb->queue) {
342 /* Second buffer - initialise register side B */
343 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
344 sh_vou_stream_start(vou_dev, vb);
346 /* Register side switching with frame VSYNC */
347 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
348 dev_dbg(vou_dev->v4l2_dev.dev, "%s: second buffer status 0x%x\n",
349 __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
351 /* Enable End-of-Frame (VSYNC) interrupts */
352 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
353 /* Two buffers on the queue - activate the hardware */
355 vou_dev->status = SH_VOU_RUNNING;
356 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
360 static void sh_vou_buf_release(struct videobuf_queue *vq,
361 struct videobuf_buffer *vb)
363 struct video_device *vdev = vq->priv_data;
364 struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
365 unsigned long flags;
367 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
369 spin_lock_irqsave(&vou_dev->lock, flags);
371 if (vou_dev->active == vb) {
372 /* disable output */
373 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
374 /* ...but the current frame will complete */
375 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
376 vou_dev->active = NULL;
379 if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
380 vb->state = VIDEOBUF_ERROR;
381 list_del(&vb->queue);
384 spin_unlock_irqrestore(&vou_dev->lock, flags);
386 free_buffer(vq, vb);
389 static struct videobuf_queue_ops sh_vou_video_qops = {
390 .buf_setup = sh_vou_buf_setup,
391 .buf_prepare = sh_vou_buf_prepare,
392 .buf_queue = sh_vou_buf_queue,
393 .buf_release = sh_vou_buf_release,
396 /* Video IOCTLs */
397 static int sh_vou_querycap(struct file *file, void *priv,
398 struct v4l2_capability *cap)
400 struct sh_vou_device *vou_dev = video_drvdata(file);
402 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
404 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
405 strlcpy(cap->driver, "sh-vou", sizeof(cap->driver));
406 strlcpy(cap->bus_info, "platform:sh-vou", sizeof(cap->bus_info));
407 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
408 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
409 return 0;
412 /* Enumerate formats, that the device can accept from the user */
413 static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
414 struct v4l2_fmtdesc *fmt)
416 struct sh_vou_device *vou_dev = video_drvdata(file);
418 if (fmt->index >= ARRAY_SIZE(vou_fmt))
419 return -EINVAL;
421 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
423 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
424 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
425 sizeof(fmt->description));
426 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
428 return 0;
431 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
432 struct v4l2_format *fmt)
434 struct sh_vou_device *vou_dev = video_drvdata(file);
436 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
438 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
439 fmt->fmt.pix = vou_dev->pix;
441 return 0;
444 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
445 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
446 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
447 static const unsigned char vou_scale_v_num[] = {1, 2, 4};
448 static const unsigned char vou_scale_v_den[] = {1, 1, 1};
449 static const unsigned char vou_scale_v_fld[] = {0, 1};
451 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
452 int pix_idx, int w_idx, int h_idx)
454 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
455 unsigned int black_left, black_top, width_max,
456 frame_in_height, frame_out_height, frame_out_top;
457 struct v4l2_rect *rect = &vou_dev->rect;
458 struct v4l2_pix_format *pix = &vou_dev->pix;
459 u32 vouvcr = 0, dsr_h, dsr_v;
461 if (vou_dev->std & V4L2_STD_525_60) {
462 width_max = 858;
463 /* height_max = 262; */
464 } else {
465 width_max = 864;
466 /* height_max = 312; */
469 frame_in_height = pix->height / 2;
470 frame_out_height = rect->height / 2;
471 frame_out_top = rect->top / 2;
474 * Cropping scheme: max useful image is 720x480, and the total video
475 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
476 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
477 * of which the first 33 / 25 clocks HSYNC must be held active. This
478 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
479 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
480 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
481 * beyond DSR, specified on the left and top by the VPR register "black
482 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
483 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
484 * client requires, and that's exactly what leaves us 720 pixels for the
485 * image; we leave VPR[VVP] at default 20 for now, because the client
486 * doesn't seem to have any special requirements for it. Otherwise we
487 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
488 * the selected standard, and DPR and DSR are selected according to
489 * cropping. Q: how does the client detect the first valid line? Does
490 * HSYNC stay inactive during invalid (black) lines?
492 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
493 black_top = 20;
495 dsr_h = rect->width + rect->left;
496 dsr_v = frame_out_height + frame_out_top;
498 dev_dbg(vou_dev->v4l2_dev.dev,
499 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
500 pix->width, frame_in_height, black_left, black_top,
501 rect->left, frame_out_top, dsr_h, dsr_v);
503 /* VOUISR height - half of a frame height in frame mode */
504 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
505 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
506 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
507 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
510 * if necessary, we could set VOUHIR to
511 * max(black_left + dsr_h, width_max) here
514 if (w_idx)
515 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
516 if (h_idx)
517 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
519 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
521 /* To produce a colour bar for testing set bit 23 of VOUVCR */
522 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
523 sh_vou_reg_ab_write(vou_dev, VOUDFR,
524 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
527 struct sh_vou_geometry {
528 struct v4l2_rect output;
529 unsigned int in_width;
530 unsigned int in_height;
531 int scale_idx_h;
532 int scale_idx_v;
536 * Find input geometry, that we can use to produce output, closest to the
537 * requested rectangle, using VOU scaling
539 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
541 /* The compiler cannot know, that best and idx will indeed be set */
542 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
543 int i, idx = 0;
545 if (std & V4L2_STD_525_60)
546 img_height_max = 480;
547 else
548 img_height_max = 576;
550 /* Image width must be a multiple of 4 */
551 v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
552 &geo->in_height, 0, img_height_max, 1, 0);
554 /* Select scales to come as close as possible to the output image */
555 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
556 unsigned int err;
557 unsigned int found = geo->output.width * vou_scale_h_den[i] /
558 vou_scale_h_num[i];
560 if (found > VOU_MAX_IMAGE_WIDTH)
561 /* scales increase */
562 break;
564 err = abs(found - geo->in_width);
565 if (err < best_err) {
566 best_err = err;
567 idx = i;
568 best = found;
570 if (!err)
571 break;
574 geo->in_width = best;
575 geo->scale_idx_h = idx;
577 best_err = UINT_MAX;
579 /* This loop can be replaced with one division */
580 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
581 unsigned int err;
582 unsigned int found = geo->output.height * vou_scale_v_den[i] /
583 vou_scale_v_num[i];
585 if (found > img_height_max)
586 /* scales increase */
587 break;
589 err = abs(found - geo->in_height);
590 if (err < best_err) {
591 best_err = err;
592 idx = i;
593 best = found;
595 if (!err)
596 break;
599 geo->in_height = best;
600 geo->scale_idx_v = idx;
604 * Find output geometry, that we can produce, using VOU scaling, closest to
605 * the requested rectangle
607 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
609 unsigned int best_err = UINT_MAX, best = geo->in_width,
610 width_max, height_max, img_height_max;
611 int i, idx_h = 0, idx_v = 0;
613 if (std & V4L2_STD_525_60) {
614 width_max = 858;
615 height_max = 262 * 2;
616 img_height_max = 480;
617 } else {
618 width_max = 864;
619 height_max = 312 * 2;
620 img_height_max = 576;
623 /* Select scales to come as close as possible to the output image */
624 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
625 unsigned int err;
626 unsigned int found = geo->in_width * vou_scale_h_num[i] /
627 vou_scale_h_den[i];
629 if (found > VOU_MAX_IMAGE_WIDTH)
630 /* scales increase */
631 break;
633 err = abs(found - geo->output.width);
634 if (err < best_err) {
635 best_err = err;
636 idx_h = i;
637 best = found;
639 if (!err)
640 break;
643 geo->output.width = best;
644 geo->scale_idx_h = idx_h;
645 if (geo->output.left + best > width_max)
646 geo->output.left = width_max - best;
648 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
649 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best);
651 best_err = UINT_MAX;
653 /* This loop can be replaced with one division */
654 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
655 unsigned int err;
656 unsigned int found = geo->in_height * vou_scale_v_num[i] /
657 vou_scale_v_den[i];
659 if (found > img_height_max)
660 /* scales increase */
661 break;
663 err = abs(found - geo->output.height);
664 if (err < best_err) {
665 best_err = err;
666 idx_v = i;
667 best = found;
669 if (!err)
670 break;
673 geo->output.height = best;
674 geo->scale_idx_v = idx_v;
675 if (geo->output.top + best > height_max)
676 geo->output.top = height_max - best;
678 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
679 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best);
682 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
683 struct v4l2_format *fmt)
685 struct sh_vou_device *vou_dev = video_drvdata(file);
686 struct v4l2_pix_format *pix = &fmt->fmt.pix;
687 unsigned int img_height_max;
688 int pix_idx;
690 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
692 pix->field = V4L2_FIELD_INTERLACED;
693 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
694 pix->ycbcr_enc = pix->quantization = 0;
696 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
697 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
698 break;
700 if (pix_idx == ARRAY_SIZE(vou_fmt))
701 return -EINVAL;
703 if (vou_dev->std & V4L2_STD_525_60)
704 img_height_max = 480;
705 else
706 img_height_max = 576;
708 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
709 &pix->height, 0, img_height_max, 1, 0);
710 pix->bytesperline = pix->width * vou_fmt[pix_idx].bpl;
711 pix->sizeimage = pix->height * ((pix->width * vou_fmt[pix_idx].bpp) >> 3);
713 return 0;
716 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
717 struct v4l2_format *fmt)
719 struct sh_vou_device *vou_dev = video_drvdata(file);
720 struct v4l2_pix_format *pix = &fmt->fmt.pix;
721 unsigned int img_height_max;
722 struct sh_vou_geometry geo;
723 struct v4l2_subdev_format format = {
724 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
725 /* Revisit: is this the correct code? */
726 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
727 .format.field = V4L2_FIELD_INTERLACED,
728 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
730 struct v4l2_mbus_framefmt *mbfmt = &format.format;
731 int ret = sh_vou_try_fmt_vid_out(file, priv, fmt);
732 int pix_idx;
734 if (ret)
735 return ret;
737 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
738 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
739 break;
741 geo.in_width = pix->width;
742 geo.in_height = pix->height;
743 geo.output = vou_dev->rect;
745 vou_adjust_output(&geo, vou_dev->std);
747 mbfmt->width = geo.output.width;
748 mbfmt->height = geo.output.height;
749 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
750 set_fmt, NULL, &format);
751 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
752 if (ret < 0)
753 return ret;
755 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
756 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height);
758 if (vou_dev->std & V4L2_STD_525_60)
759 img_height_max = 480;
760 else
761 img_height_max = 576;
763 /* Sanity checks */
764 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH ||
765 (unsigned)mbfmt->height > img_height_max ||
766 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8)
767 return -EIO;
769 if (mbfmt->width != geo.output.width ||
770 mbfmt->height != geo.output.height) {
771 geo.output.width = mbfmt->width;
772 geo.output.height = mbfmt->height;
774 vou_adjust_input(&geo, vou_dev->std);
777 /* We tried to preserve output rectangle, but it could have changed */
778 vou_dev->rect = geo.output;
779 pix->width = geo.in_width;
780 pix->height = geo.in_height;
782 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
783 pix->width, pix->height);
785 vou_dev->pix_idx = pix_idx;
787 vou_dev->pix = *pix;
789 sh_vou_configure_geometry(vou_dev, pix_idx,
790 geo.scale_idx_h, geo.scale_idx_v);
792 return 0;
795 static int sh_vou_reqbufs(struct file *file, void *priv,
796 struct v4l2_requestbuffers *req)
798 struct sh_vou_device *vou_dev = video_drvdata(file);
799 struct sh_vou_file *vou_file = priv;
801 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
803 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
804 return -EINVAL;
806 return videobuf_reqbufs(&vou_file->vbq, req);
809 static int sh_vou_querybuf(struct file *file, void *priv,
810 struct v4l2_buffer *b)
812 struct sh_vou_device *vou_dev = video_drvdata(file);
813 struct sh_vou_file *vou_file = priv;
815 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
817 return videobuf_querybuf(&vou_file->vbq, b);
820 static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
822 struct sh_vou_device *vou_dev = video_drvdata(file);
823 struct sh_vou_file *vou_file = priv;
825 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
827 return videobuf_qbuf(&vou_file->vbq, b);
830 static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
832 struct sh_vou_device *vou_dev = video_drvdata(file);
833 struct sh_vou_file *vou_file = priv;
835 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
837 return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
840 static int sh_vou_streamon(struct file *file, void *priv,
841 enum v4l2_buf_type buftype)
843 struct sh_vou_device *vou_dev = video_drvdata(file);
844 struct sh_vou_file *vou_file = priv;
845 int ret;
847 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
849 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
850 video, s_stream, 1);
851 if (ret < 0 && ret != -ENOIOCTLCMD)
852 return ret;
854 /* This calls our .buf_queue() (== sh_vou_buf_queue) */
855 return videobuf_streamon(&vou_file->vbq);
858 static int sh_vou_streamoff(struct file *file, void *priv,
859 enum v4l2_buf_type buftype)
861 struct sh_vou_device *vou_dev = video_drvdata(file);
862 struct sh_vou_file *vou_file = priv;
864 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
867 * This calls buf_release from host driver's videobuf_queue_ops for all
868 * remaining buffers. When the last buffer is freed, stop streaming
870 videobuf_streamoff(&vou_file->vbq);
871 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
873 return 0;
876 static int sh_vou_enum_output(struct file *file, void *fh,
877 struct v4l2_output *a)
879 struct sh_vou_device *vou_dev = video_drvdata(file);
881 if (a->index)
882 return -EINVAL;
883 strlcpy(a->name, "Video Out", sizeof(a->name));
884 a->type = V4L2_OUTPUT_TYPE_ANALOG;
885 a->std = vou_dev->vdev.tvnorms;
886 return 0;
889 int sh_vou_g_output(struct file *file, void *fh, unsigned int *i)
891 *i = 0;
892 return 0;
895 int sh_vou_s_output(struct file *file, void *fh, unsigned int i)
897 return i ? -EINVAL : 0;
900 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
902 switch (bus_fmt) {
903 default:
904 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
905 __func__, bus_fmt);
906 case SH_VOU_BUS_8BIT:
907 return 1;
908 case SH_VOU_BUS_16BIT:
909 return 0;
910 case SH_VOU_BUS_BT656:
911 return 3;
915 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
917 struct sh_vou_device *vou_dev = video_drvdata(file);
918 int ret;
920 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
922 if (std_id & ~vou_dev->vdev.tvnorms)
923 return -EINVAL;
925 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
926 s_std_output, std_id);
927 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
928 if (ret < 0 && ret != -ENOIOCTLCMD)
929 return ret;
931 if (std_id & V4L2_STD_525_60)
932 sh_vou_reg_ab_set(vou_dev, VOUCR,
933 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
934 else
935 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
937 vou_dev->std = std_id;
939 return 0;
942 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
944 struct sh_vou_device *vou_dev = video_drvdata(file);
946 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
948 *std = vou_dev->std;
950 return 0;
953 static int sh_vou_g_selection(struct file *file, void *fh,
954 struct v4l2_selection *sel)
956 struct sh_vou_device *vou_dev = video_drvdata(file);
958 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
959 return -EINVAL;
960 switch (sel->target) {
961 case V4L2_SEL_TGT_COMPOSE:
962 sel->r = vou_dev->rect;
963 break;
964 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
965 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
966 sel->r.left = 0;
967 sel->r.top = 0;
968 sel->r.width = VOU_MAX_IMAGE_WIDTH;
969 sel->r.height = VOU_MAX_IMAGE_HEIGHT;
970 break;
971 default:
972 return -EINVAL;
974 return 0;
977 /* Assume a dull encoder, do all the work ourselves. */
978 static int sh_vou_s_selection(struct file *file, void *fh,
979 struct v4l2_selection *sel)
981 struct v4l2_rect *rect = &sel->r;
982 struct sh_vou_device *vou_dev = video_drvdata(file);
983 struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
984 struct v4l2_pix_format *pix = &vou_dev->pix;
985 struct sh_vou_geometry geo;
986 struct v4l2_subdev_format format = {
987 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
988 /* Revisit: is this the correct code? */
989 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
990 .format.field = V4L2_FIELD_INTERLACED,
991 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
993 unsigned int img_height_max;
994 int ret;
996 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
997 sel->target != V4L2_SEL_TGT_COMPOSE)
998 return -EINVAL;
1000 if (vou_dev->std & V4L2_STD_525_60)
1001 img_height_max = 480;
1002 else
1003 img_height_max = 576;
1005 v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
1006 &rect->height, 0, img_height_max, 1, 0);
1008 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
1009 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
1011 if (rect->height + rect->top > img_height_max)
1012 rect->top = img_height_max - rect->height;
1014 geo.output = *rect;
1015 geo.in_width = pix->width;
1016 geo.in_height = pix->height;
1018 /* Configure the encoder one-to-one, position at 0, ignore errors */
1019 sd_crop.c.width = geo.output.width;
1020 sd_crop.c.height = geo.output.height;
1022 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
1023 * final encoder configuration.
1025 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
1026 s_crop, &sd_crop);
1027 format.format.width = geo.output.width;
1028 format.format.height = geo.output.height;
1029 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
1030 set_fmt, NULL, &format);
1031 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
1032 if (ret < 0)
1033 return ret;
1035 /* Sanity checks */
1036 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH ||
1037 (unsigned)format.format.height > img_height_max ||
1038 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8)
1039 return -EIO;
1041 geo.output.width = format.format.width;
1042 geo.output.height = format.format.height;
1045 * No down-scaling. According to the API, current call has precedence:
1046 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1048 vou_adjust_input(&geo, vou_dev->std);
1050 /* We tried to preserve output rectangle, but it could have changed */
1051 vou_dev->rect = geo.output;
1052 pix->width = geo.in_width;
1053 pix->height = geo.in_height;
1055 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1056 geo.scale_idx_h, geo.scale_idx_v);
1058 return 0;
1061 static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1063 struct sh_vou_device *vou_dev = dev_id;
1064 static unsigned long j;
1065 struct videobuf_buffer *vb;
1066 static int cnt;
1067 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1068 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1070 if (!(irq_status & 0x300)) {
1071 if (printk_timed_ratelimit(&j, 500))
1072 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1073 irq_status);
1074 return IRQ_NONE;
1077 spin_lock(&vou_dev->lock);
1078 if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1079 if (printk_timed_ratelimit(&j, 500))
1080 dev_warn(vou_dev->v4l2_dev.dev,
1081 "IRQ without active buffer: %x!\n", irq_status);
1082 /* Just ack: buf_release will disable further interrupts */
1083 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1084 spin_unlock(&vou_dev->lock);
1085 return IRQ_HANDLED;
1088 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1089 dev_dbg(vou_dev->v4l2_dev.dev,
1090 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1091 irq_status, masked, vou_status, cnt);
1093 cnt++;
1094 /* side = vou_status & 0x10000; */
1096 /* Clear only set interrupts */
1097 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1099 vb = vou_dev->active;
1100 list_del(&vb->queue);
1102 vb->state = VIDEOBUF_DONE;
1103 v4l2_get_timestamp(&vb->ts);
1104 vb->field_count++;
1105 wake_up(&vb->done);
1107 if (list_empty(&vou_dev->queue)) {
1108 /* Stop VOU */
1109 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1110 __func__, cnt);
1111 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1112 vou_dev->active = NULL;
1113 vou_dev->status = SH_VOU_INITIALISING;
1114 /* Disable End-of-Frame (VSYNC) interrupts */
1115 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1116 spin_unlock(&vou_dev->lock);
1117 return IRQ_HANDLED;
1120 vou_dev->active = list_entry(vou_dev->queue.next,
1121 struct videobuf_buffer, queue);
1123 if (vou_dev->active->queue.next != &vou_dev->queue) {
1124 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1125 struct videobuf_buffer, queue);
1126 sh_vou_schedule_next(vou_dev, new);
1129 spin_unlock(&vou_dev->lock);
1131 return IRQ_HANDLED;
1134 static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1136 struct sh_vou_pdata *pdata = vou_dev->pdata;
1137 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1138 int i = 100;
1140 /* Disable all IRQs */
1141 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1143 /* Reset VOU interfaces - registers unaffected */
1144 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1145 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1146 udelay(1);
1148 if (!i)
1149 return -ETIMEDOUT;
1151 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1153 if (pdata->flags & SH_VOU_PCLK_FALLING)
1154 voucr |= 1 << 28;
1155 if (pdata->flags & SH_VOU_HSYNC_LOW)
1156 voucr |= 1 << 27;
1157 if (pdata->flags & SH_VOU_VSYNC_LOW)
1158 voucr |= 1 << 26;
1159 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1161 /* Manual register side switching at first */
1162 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1163 /* Default - fixed HSYNC length, can be made configurable is required */
1164 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1166 return 0;
1169 /* File operations */
1170 static int sh_vou_open(struct file *file)
1172 struct sh_vou_device *vou_dev = video_drvdata(file);
1173 struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1174 GFP_KERNEL);
1176 if (!vou_file)
1177 return -ENOMEM;
1179 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1181 v4l2_fh_init(&vou_file->fh, &vou_dev->vdev);
1182 if (mutex_lock_interruptible(&vou_dev->fop_lock)) {
1183 kfree(vou_file);
1184 return -ERESTARTSYS;
1186 v4l2_fh_add(&vou_file->fh);
1187 if (v4l2_fh_is_singular(&vou_file->fh)) {
1188 int ret;
1190 /* First open */
1191 vou_dev->status = SH_VOU_INITIALISING;
1192 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
1193 ret = sh_vou_hw_init(vou_dev);
1194 if (ret < 0) {
1195 pm_runtime_put(vou_dev->v4l2_dev.dev);
1196 vou_dev->status = SH_VOU_IDLE;
1197 v4l2_fh_del(&vou_file->fh);
1198 v4l2_fh_exit(&vou_file->fh);
1199 mutex_unlock(&vou_dev->fop_lock);
1200 kfree(vou_file);
1201 return ret;
1205 videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1206 vou_dev->v4l2_dev.dev, &vou_dev->lock,
1207 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1208 V4L2_FIELD_NONE,
1209 sizeof(struct videobuf_buffer),
1210 &vou_dev->vdev, &vou_dev->fop_lock);
1211 mutex_unlock(&vou_dev->fop_lock);
1213 file->private_data = vou_file;
1215 return 0;
1218 static int sh_vou_release(struct file *file)
1220 struct sh_vou_device *vou_dev = video_drvdata(file);
1221 struct sh_vou_file *vou_file = file->private_data;
1223 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1225 mutex_lock(&vou_dev->fop_lock);
1226 if (v4l2_fh_is_singular(&vou_file->fh)) {
1227 /* Last close */
1228 vou_dev->status = SH_VOU_IDLE;
1229 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1230 pm_runtime_put(vou_dev->v4l2_dev.dev);
1232 v4l2_fh_del(&vou_file->fh);
1233 v4l2_fh_exit(&vou_file->fh);
1234 mutex_unlock(&vou_dev->fop_lock);
1236 file->private_data = NULL;
1237 kfree(vou_file);
1239 return 0;
1242 static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1244 struct sh_vou_device *vou_dev = video_drvdata(file);
1245 struct sh_vou_file *vou_file = file->private_data;
1246 int ret;
1248 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1250 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1251 return -ERESTARTSYS;
1252 ret = videobuf_mmap_mapper(&vou_file->vbq, vma);
1253 mutex_unlock(&vou_dev->fop_lock);
1254 return ret;
1257 static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1259 struct sh_vou_device *vou_dev = video_drvdata(file);
1260 struct sh_vou_file *vou_file = file->private_data;
1261 unsigned int res;
1263 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1265 mutex_lock(&vou_dev->fop_lock);
1266 res = videobuf_poll_stream(file, &vou_file->vbq, wait);
1267 mutex_unlock(&vou_dev->fop_lock);
1268 return res;
1271 /* sh_vou display ioctl operations */
1272 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1273 .vidioc_querycap = sh_vou_querycap,
1274 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1275 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1276 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1277 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1278 .vidioc_reqbufs = sh_vou_reqbufs,
1279 .vidioc_querybuf = sh_vou_querybuf,
1280 .vidioc_qbuf = sh_vou_qbuf,
1281 .vidioc_dqbuf = sh_vou_dqbuf,
1282 .vidioc_streamon = sh_vou_streamon,
1283 .vidioc_streamoff = sh_vou_streamoff,
1284 .vidioc_g_output = sh_vou_g_output,
1285 .vidioc_s_output = sh_vou_s_output,
1286 .vidioc_enum_output = sh_vou_enum_output,
1287 .vidioc_s_std = sh_vou_s_std,
1288 .vidioc_g_std = sh_vou_g_std,
1289 .vidioc_g_selection = sh_vou_g_selection,
1290 .vidioc_s_selection = sh_vou_s_selection,
1293 static const struct v4l2_file_operations sh_vou_fops = {
1294 .owner = THIS_MODULE,
1295 .open = sh_vou_open,
1296 .release = sh_vou_release,
1297 .unlocked_ioctl = video_ioctl2,
1298 .mmap = sh_vou_mmap,
1299 .poll = sh_vou_poll,
1302 static const struct video_device sh_vou_video_template = {
1303 .name = "sh_vou",
1304 .fops = &sh_vou_fops,
1305 .ioctl_ops = &sh_vou_ioctl_ops,
1306 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
1307 .vfl_dir = VFL_DIR_TX,
1310 static int sh_vou_probe(struct platform_device *pdev)
1312 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1313 struct v4l2_rect *rect;
1314 struct v4l2_pix_format *pix;
1315 struct i2c_adapter *i2c_adap;
1316 struct video_device *vdev;
1317 struct sh_vou_device *vou_dev;
1318 struct resource *reg_res;
1319 struct v4l2_subdev *subdev;
1320 int irq, ret;
1322 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1323 irq = platform_get_irq(pdev, 0);
1325 if (!vou_pdata || !reg_res || irq <= 0) {
1326 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1327 return -ENODEV;
1330 vou_dev = devm_kzalloc(&pdev->dev, sizeof(*vou_dev), GFP_KERNEL);
1331 if (!vou_dev)
1332 return -ENOMEM;
1334 INIT_LIST_HEAD(&vou_dev->queue);
1335 spin_lock_init(&vou_dev->lock);
1336 mutex_init(&vou_dev->fop_lock);
1337 vou_dev->pdata = vou_pdata;
1338 vou_dev->status = SH_VOU_IDLE;
1340 rect = &vou_dev->rect;
1341 pix = &vou_dev->pix;
1343 /* Fill in defaults */
1344 vou_dev->std = V4L2_STD_NTSC_M;
1345 rect->left = 0;
1346 rect->top = 0;
1347 rect->width = VOU_MAX_IMAGE_WIDTH;
1348 rect->height = 480;
1349 pix->width = VOU_MAX_IMAGE_WIDTH;
1350 pix->height = 480;
1351 pix->pixelformat = V4L2_PIX_FMT_NV16;
1352 pix->field = V4L2_FIELD_NONE;
1353 pix->bytesperline = VOU_MAX_IMAGE_WIDTH;
1354 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
1355 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1357 vou_dev->base = devm_ioremap_resource(&pdev->dev, reg_res);
1358 if (IS_ERR(vou_dev->base))
1359 return PTR_ERR(vou_dev->base);
1361 ret = devm_request_irq(&pdev->dev, irq, sh_vou_isr, 0, "vou", vou_dev);
1362 if (ret < 0)
1363 return ret;
1365 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1366 if (ret < 0) {
1367 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1368 return ret;
1371 vdev = &vou_dev->vdev;
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_empty;
1377 vdev->lock = &vou_dev->fop_lock;
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);
1385 if (!i2c_adap) {
1386 ret = -ENODEV;
1387 goto ei2cgadap;
1390 ret = sh_vou_hw_init(vou_dev);
1391 if (ret < 0)
1392 goto ereset;
1394 subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1395 vou_pdata->board_info, NULL);
1396 if (!subdev) {
1397 ret = -ENOMEM;
1398 goto ei2cnd;
1401 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1402 if (ret < 0)
1403 goto evregdev;
1405 return 0;
1407 evregdev:
1408 ei2cnd:
1409 ereset:
1410 i2c_put_adapter(i2c_adap);
1411 ei2cgadap:
1412 pm_runtime_disable(&pdev->dev);
1413 v4l2_device_unregister(&vou_dev->v4l2_dev);
1414 return ret;
1417 static int sh_vou_remove(struct platform_device *pdev)
1419 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1420 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1421 struct sh_vou_device, v4l2_dev);
1422 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1423 struct v4l2_subdev, list);
1424 struct i2c_client *client = v4l2_get_subdevdata(sd);
1426 pm_runtime_disable(&pdev->dev);
1427 video_unregister_device(&vou_dev->vdev);
1428 i2c_put_adapter(client->adapter);
1429 v4l2_device_unregister(&vou_dev->v4l2_dev);
1430 return 0;
1433 static struct platform_driver __refdata sh_vou = {
1434 .remove = sh_vou_remove,
1435 .driver = {
1436 .name = "sh-vou",
1440 module_platform_driver_probe(sh_vou, sh_vou_probe);
1442 MODULE_DESCRIPTION("SuperH VOU driver");
1443 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1444 MODULE_LICENSE("GPL v2");
1445 MODULE_VERSION("0.1.0");
1446 MODULE_ALIAS("platform:sh-vou");