Linux-libre 3.17.4-gnu
[librecmc/linux-libre.git] / drivers / media / platform / sh_vou.c
1 /*
2  * SuperH Video Output Unit (VOU) driver
3  *
4  * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5  *
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  */
10
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>
24
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>
31
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
53
54 enum sh_vou_status {
55         SH_VOU_IDLE,
56         SH_VOU_INITIALISING,
57         SH_VOU_RUNNING,
58 };
59
60 #define VOU_MAX_IMAGE_WIDTH     720
61 #define VOU_MAX_IMAGE_HEIGHT    576
62
63 struct sh_vou_device {
64         struct v4l2_device v4l2_dev;
65         struct video_device *vdev;
66         atomic_t use_count;
67         struct sh_vou_pdata *pdata;
68         spinlock_t lock;
69         void __iomem *base;
70         /* State information */
71         struct v4l2_pix_format pix;
72         struct v4l2_rect rect;
73         struct list_head queue;
74         v4l2_std_id std;
75         int pix_idx;
76         struct videobuf_buffer *active;
77         enum sh_vou_status status;
78         struct mutex fop_lock;
79 };
80
81 struct sh_vou_file {
82         struct videobuf_queue vbq;
83 };
84
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)
88 {
89         __raw_writel(value, vou_dev->base + reg);
90 }
91
92 static void sh_vou_reg_ab_write(struct sh_vou_device *vou_dev, unsigned int reg,
93                                 u32 value)
94 {
95         __raw_writel(value, vou_dev->base + reg);
96         __raw_writel(value, vou_dev->base + reg + 0x1000);
97 }
98
99 static void sh_vou_reg_m_write(struct sh_vou_device *vou_dev, unsigned int reg,
100                                u32 value)
101 {
102         __raw_writel(value, vou_dev->base + reg + 0x2000);
103 }
104
105 static u32 sh_vou_reg_a_read(struct sh_vou_device *vou_dev, unsigned int reg)
106 {
107         return __raw_readl(vou_dev->base + reg);
108 }
109
110 static void sh_vou_reg_a_set(struct sh_vou_device *vou_dev, unsigned int reg,
111                              u32 value, u32 mask)
112 {
113         u32 old = __raw_readl(vou_dev->base + reg);
114
115         value = (value & mask) | (old & ~mask);
116         __raw_writel(value, vou_dev->base + reg);
117 }
118
119 static void sh_vou_reg_b_set(struct sh_vou_device *vou_dev, unsigned int reg,
120                              u32 value, u32 mask)
121 {
122         sh_vou_reg_a_set(vou_dev, reg + 0x1000, value, mask);
123 }
124
125 static void sh_vou_reg_ab_set(struct sh_vou_device *vou_dev, unsigned int reg,
126                               u32 value, u32 mask)
127 {
128         sh_vou_reg_a_set(vou_dev, reg, value, mask);
129         sh_vou_reg_b_set(vou_dev, reg, value, mask);
130 }
131
132 struct sh_vou_fmt {
133         u32             pfmt;
134         char            *desc;
135         unsigned char   bpp;
136         unsigned char   rgb;
137         unsigned char   yf;
138         unsigned char   pkf;
139 };
140
141 /* Further pixel formats can be added */
142 static struct sh_vou_fmt vou_fmt[] = {
143         {
144                 .pfmt   = V4L2_PIX_FMT_NV12,
145                 .bpp    = 12,
146                 .desc   = "YVU420 planar",
147                 .yf     = 0,
148                 .rgb    = 0,
149         },
150         {
151                 .pfmt   = V4L2_PIX_FMT_NV16,
152                 .bpp    = 16,
153                 .desc   = "YVYU planar",
154                 .yf     = 1,
155                 .rgb    = 0,
156         },
157         {
158                 .pfmt   = V4L2_PIX_FMT_RGB24,
159                 .bpp    = 24,
160                 .desc   = "RGB24",
161                 .pkf    = 2,
162                 .rgb    = 1,
163         },
164         {
165                 .pfmt   = V4L2_PIX_FMT_RGB565,
166                 .bpp    = 16,
167                 .desc   = "RGB565",
168                 .pkf    = 3,
169                 .rgb    = 1,
170         },
171         {
172                 .pfmt   = V4L2_PIX_FMT_RGB565X,
173                 .bpp    = 16,
174                 .desc   = "RGB565 byteswapped",
175                 .pkf    = 3,
176                 .rgb    = 1,
177         },
178 };
179
180 static void sh_vou_schedule_next(struct sh_vou_device *vou_dev,
181                                  struct videobuf_buffer *vb)
182 {
183         dma_addr_t addr1, addr2;
184
185         addr1 = videobuf_to_dma_contig(vb);
186         switch (vou_dev->pix.pixelformat) {
187         case V4L2_PIX_FMT_NV12:
188         case V4L2_PIX_FMT_NV16:
189                 addr2 = addr1 + vou_dev->pix.width * vou_dev->pix.height;
190                 break;
191         default:
192                 addr2 = 0;
193         }
194
195         sh_vou_reg_m_write(vou_dev, VOUAD1R, addr1);
196         sh_vou_reg_m_write(vou_dev, VOUAD2R, addr2);
197 }
198
199 static void sh_vou_stream_start(struct sh_vou_device *vou_dev,
200                                 struct videobuf_buffer *vb)
201 {
202         unsigned int row_coeff;
203 #ifdef __LITTLE_ENDIAN
204         u32 dataswap = 7;
205 #else
206         u32 dataswap = 0;
207 #endif
208
209         switch (vou_dev->pix.pixelformat) {
210         default:
211         case V4L2_PIX_FMT_NV12:
212         case V4L2_PIX_FMT_NV16:
213                 row_coeff = 1;
214                 break;
215         case V4L2_PIX_FMT_RGB565:
216                 dataswap ^= 1;
217         case V4L2_PIX_FMT_RGB565X:
218                 row_coeff = 2;
219                 break;
220         case V4L2_PIX_FMT_RGB24:
221                 row_coeff = 3;
222                 break;
223         }
224
225         sh_vou_reg_a_write(vou_dev, VOUSWR, dataswap);
226         sh_vou_reg_ab_write(vou_dev, VOUAIR, vou_dev->pix.width * row_coeff);
227         sh_vou_schedule_next(vou_dev, vb);
228 }
229
230 static void free_buffer(struct videobuf_queue *vq, struct videobuf_buffer *vb)
231 {
232         BUG_ON(in_interrupt());
233
234         /* Wait until this buffer is no longer in STATE_QUEUED or STATE_ACTIVE */
235         videobuf_waiton(vq, vb, 0, 0);
236         videobuf_dma_contig_free(vq, vb);
237         vb->state = VIDEOBUF_NEEDS_INIT;
238 }
239
240 /* Locking: caller holds fop_lock mutex */
241 static int sh_vou_buf_setup(struct videobuf_queue *vq, unsigned int *count,
242                             unsigned int *size)
243 {
244         struct video_device *vdev = vq->priv_data;
245         struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
246
247         *size = vou_fmt[vou_dev->pix_idx].bpp * vou_dev->pix.width *
248                 vou_dev->pix.height / 8;
249
250         if (*count < 2)
251                 *count = 2;
252
253         /* Taking into account maximum frame size, *count will stay >= 2 */
254         if (PAGE_ALIGN(*size) * *count > 4 * 1024 * 1024)
255                 *count = 4 * 1024 * 1024 / PAGE_ALIGN(*size);
256
257         dev_dbg(vou_dev->v4l2_dev.dev, "%s(): count=%d, size=%d\n", __func__,
258                 *count, *size);
259
260         return 0;
261 }
262
263 /* Locking: caller holds fop_lock mutex */
264 static int sh_vou_buf_prepare(struct videobuf_queue *vq,
265                               struct videobuf_buffer *vb,
266                               enum v4l2_field field)
267 {
268         struct video_device *vdev = vq->priv_data;
269         struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
270         struct v4l2_pix_format *pix = &vou_dev->pix;
271         int bytes_per_line = vou_fmt[vou_dev->pix_idx].bpp * pix->width / 8;
272         int ret;
273
274         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
275
276         if (vb->width   != pix->width ||
277             vb->height  != pix->height ||
278             vb->field   != pix->field) {
279                 vb->width       = pix->width;
280                 vb->height      = pix->height;
281                 vb->field       = field;
282                 if (vb->state != VIDEOBUF_NEEDS_INIT)
283                         free_buffer(vq, vb);
284         }
285
286         vb->size = vb->height * bytes_per_line;
287         if (vb->baddr && vb->bsize < vb->size) {
288                 /* User buffer too small */
289                 dev_warn(vq->dev, "User buffer too small: [%zu] @ %lx\n",
290                          vb->bsize, vb->baddr);
291                 return -EINVAL;
292         }
293
294         if (vb->state == VIDEOBUF_NEEDS_INIT) {
295                 ret = videobuf_iolock(vq, vb, NULL);
296                 if (ret < 0) {
297                         dev_warn(vq->dev, "IOLOCK buf-type %d: %d\n",
298                                  vb->memory, ret);
299                         return ret;
300                 }
301                 vb->state = VIDEOBUF_PREPARED;
302         }
303
304         dev_dbg(vou_dev->v4l2_dev.dev,
305                 "%s(): fmt #%d, %u bytes per line, phys %pad, type %d, state %d\n",
306                 __func__, vou_dev->pix_idx, bytes_per_line,
307                 ({ dma_addr_t addr = videobuf_to_dma_contig(vb); &addr; }),
308                 vb->memory, vb->state);
309
310         return 0;
311 }
312
313 /* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
314 static void sh_vou_buf_queue(struct videobuf_queue *vq,
315                              struct videobuf_buffer *vb)
316 {
317         struct video_device *vdev = vq->priv_data;
318         struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
319
320         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
321
322         vb->state = VIDEOBUF_QUEUED;
323         list_add_tail(&vb->queue, &vou_dev->queue);
324
325         if (vou_dev->status == SH_VOU_RUNNING) {
326                 return;
327         } else if (!vou_dev->active) {
328                 vou_dev->active = vb;
329                 /* Start from side A: we use mirror addresses, so, set B */
330                 sh_vou_reg_a_write(vou_dev, VOURPR, 1);
331                 dev_dbg(vou_dev->v4l2_dev.dev, "%s: first buffer status 0x%x\n",
332                         __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
333                 sh_vou_schedule_next(vou_dev, vb);
334                 /* Only activate VOU after the second buffer */
335         } else if (vou_dev->active->queue.next == &vb->queue) {
336                 /* Second buffer - initialise register side B */
337                 sh_vou_reg_a_write(vou_dev, VOURPR, 0);
338                 sh_vou_stream_start(vou_dev, vb);
339
340                 /* Register side switching with frame VSYNC */
341                 sh_vou_reg_a_write(vou_dev, VOURCR, 5);
342                 dev_dbg(vou_dev->v4l2_dev.dev, "%s: second buffer status 0x%x\n",
343                         __func__, sh_vou_reg_a_read(vou_dev, VOUSTR));
344
345                 /* Enable End-of-Frame (VSYNC) interrupts */
346                 sh_vou_reg_a_write(vou_dev, VOUIR, 0x10004);
347                 /* Two buffers on the queue - activate the hardware */
348
349                 vou_dev->status = SH_VOU_RUNNING;
350                 sh_vou_reg_a_write(vou_dev, VOUER, 0x107);
351         }
352 }
353
354 static void sh_vou_buf_release(struct videobuf_queue *vq,
355                                struct videobuf_buffer *vb)
356 {
357         struct video_device *vdev = vq->priv_data;
358         struct sh_vou_device *vou_dev = video_get_drvdata(vdev);
359         unsigned long flags;
360
361         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
362
363         spin_lock_irqsave(&vou_dev->lock, flags);
364
365         if (vou_dev->active == vb) {
366                 /* disable output */
367                 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
368                 /* ...but the current frame will complete */
369                 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
370                 vou_dev->active = NULL;
371         }
372
373         if ((vb->state == VIDEOBUF_ACTIVE || vb->state == VIDEOBUF_QUEUED)) {
374                 vb->state = VIDEOBUF_ERROR;
375                 list_del(&vb->queue);
376         }
377
378         spin_unlock_irqrestore(&vou_dev->lock, flags);
379
380         free_buffer(vq, vb);
381 }
382
383 static struct videobuf_queue_ops sh_vou_video_qops = {
384         .buf_setup      = sh_vou_buf_setup,
385         .buf_prepare    = sh_vou_buf_prepare,
386         .buf_queue      = sh_vou_buf_queue,
387         .buf_release    = sh_vou_buf_release,
388 };
389
390 /* Video IOCTLs */
391 static int sh_vou_querycap(struct file *file, void  *priv,
392                            struct v4l2_capability *cap)
393 {
394         struct sh_vou_device *vou_dev = video_drvdata(file);
395
396         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
397
398         strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
399         cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
400         return 0;
401 }
402
403 /* Enumerate formats, that the device can accept from the user */
404 static int sh_vou_enum_fmt_vid_out(struct file *file, void  *priv,
405                                    struct v4l2_fmtdesc *fmt)
406 {
407         struct sh_vou_device *vou_dev = video_drvdata(file);
408
409         if (fmt->index >= ARRAY_SIZE(vou_fmt))
410                 return -EINVAL;
411
412         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
413
414         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
415         strlcpy(fmt->description, vou_fmt[fmt->index].desc,
416                 sizeof(fmt->description));
417         fmt->pixelformat = vou_fmt[fmt->index].pfmt;
418
419         return 0;
420 }
421
422 static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
423                                 struct v4l2_format *fmt)
424 {
425         struct sh_vou_device *vou_dev = video_drvdata(file);
426
427         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
428
429         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
430         fmt->fmt.pix = vou_dev->pix;
431
432         return 0;
433 }
434
435 static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
436 static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
437 static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
438 static const unsigned char vou_scale_v_num[] = {1, 2, 4};
439 static const unsigned char vou_scale_v_den[] = {1, 1, 1};
440 static const unsigned char vou_scale_v_fld[] = {0, 1};
441
442 static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
443                                       int pix_idx, int w_idx, int h_idx)
444 {
445         struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
446         unsigned int black_left, black_top, width_max,
447                 frame_in_height, frame_out_height, frame_out_top;
448         struct v4l2_rect *rect = &vou_dev->rect;
449         struct v4l2_pix_format *pix = &vou_dev->pix;
450         u32 vouvcr = 0, dsr_h, dsr_v;
451
452         if (vou_dev->std & V4L2_STD_525_60) {
453                 width_max = 858;
454                 /* height_max = 262; */
455         } else {
456                 width_max = 864;
457                 /* height_max = 312; */
458         }
459
460         frame_in_height = pix->height / 2;
461         frame_out_height = rect->height / 2;
462         frame_out_top = rect->top / 2;
463
464         /*
465          * Cropping scheme: max useful image is 720x480, and the total video
466          * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
467          * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
468          * of which the first 33 / 25 clocks HSYNC must be held active. This
469          * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
470          * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
471          * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
472          * beyond DSR, specified on the left and top by the VPR register "black
473          * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
474          * at 138 / 144 : 20, because that's the HSYNC timing, that our first
475          * client requires, and that's exactly what leaves us 720 pixels for the
476          * image; we leave VPR[VVP] at default 20 for now, because the client
477          * doesn't seem to have any special requirements for it. Otherwise we
478          * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
479          * the selected standard, and DPR and DSR are selected according to
480          * cropping. Q: how does the client detect the first valid line? Does
481          * HSYNC stay inactive during invalid (black) lines?
482          */
483         black_left = width_max - VOU_MAX_IMAGE_WIDTH;
484         black_top = 20;
485
486         dsr_h = rect->width + rect->left;
487         dsr_v = frame_out_height + frame_out_top;
488
489         dev_dbg(vou_dev->v4l2_dev.dev,
490                 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
491                 pix->width, frame_in_height, black_left, black_top,
492                 rect->left, frame_out_top, dsr_h, dsr_v);
493
494         /* VOUISR height - half of a frame height in frame mode */
495         sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
496         sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
497         sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
498         sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
499
500         /*
501          * if necessary, we could set VOUHIR to
502          * max(black_left + dsr_h, width_max) here
503          */
504
505         if (w_idx)
506                 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
507         if (h_idx)
508                 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
509
510         dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
511
512         /* To produce a colour bar for testing set bit 23 of VOUVCR */
513         sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
514         sh_vou_reg_ab_write(vou_dev, VOUDFR,
515                             fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
516 }
517
518 struct sh_vou_geometry {
519         struct v4l2_rect output;
520         unsigned int in_width;
521         unsigned int in_height;
522         int scale_idx_h;
523         int scale_idx_v;
524 };
525
526 /*
527  * Find input geometry, that we can use to produce output, closest to the
528  * requested rectangle, using VOU scaling
529  */
530 static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
531 {
532         /* The compiler cannot know, that best and idx will indeed be set */
533         unsigned int best_err = UINT_MAX, best = 0, img_height_max;
534         int i, idx = 0;
535
536         if (std & V4L2_STD_525_60)
537                 img_height_max = 480;
538         else
539                 img_height_max = 576;
540
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, img_height_max, 1, 0);
544
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--) {
547                 unsigned int err;
548                 unsigned int found = geo->output.width * vou_scale_h_den[i] /
549                         vou_scale_h_num[i];
550
551                 if (found > VOU_MAX_IMAGE_WIDTH)
552                         /* scales increase */
553                         break;
554
555                 err = abs(found - geo->in_width);
556                 if (err < best_err) {
557                         best_err = err;
558                         idx = i;
559                         best = found;
560                 }
561                 if (!err)
562                         break;
563         }
564
565         geo->in_width = best;
566         geo->scale_idx_h = idx;
567
568         best_err = UINT_MAX;
569
570         /* This loop can be replaced with one division */
571         for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
572                 unsigned int err;
573                 unsigned int found = geo->output.height * vou_scale_v_den[i] /
574                         vou_scale_v_num[i];
575
576                 if (found > img_height_max)
577                         /* scales increase */
578                         break;
579
580                 err = abs(found - geo->in_height);
581                 if (err < best_err) {
582                         best_err = err;
583                         idx = i;
584                         best = found;
585                 }
586                 if (!err)
587                         break;
588         }
589
590         geo->in_height = best;
591         geo->scale_idx_v = idx;
592 }
593
594 /*
595  * Find output geometry, that we can produce, using VOU scaling, closest to
596  * the requested rectangle
597  */
598 static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
599 {
600         unsigned int best_err = UINT_MAX, best = geo->in_width,
601                 width_max, height_max, img_height_max;
602         int i, idx = 0;
603
604         if (std & V4L2_STD_525_60) {
605                 width_max = 858;
606                 height_max = 262 * 2;
607                 img_height_max = 480;
608         } else {
609                 width_max = 864;
610                 height_max = 312 * 2;
611                 img_height_max = 576;
612         }
613
614         /* Select scales to come as close as possible to the output image */
615         for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
616                 unsigned int err;
617                 unsigned int found = geo->in_width * vou_scale_h_num[i] /
618                         vou_scale_h_den[i];
619
620                 if (found > VOU_MAX_IMAGE_WIDTH)
621                         /* scales increase */
622                         break;
623
624                 err = abs(found - geo->output.width);
625                 if (err < best_err) {
626                         best_err = err;
627                         idx = i;
628                         best = found;
629                 }
630                 if (!err)
631                         break;
632         }
633
634         geo->output.width = best;
635         geo->scale_idx_h = idx;
636         if (geo->output.left + best > width_max)
637                 geo->output.left = width_max - best;
638
639         pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
640                  vou_scale_h_num[idx], vou_scale_h_den[idx], best);
641
642         best_err = UINT_MAX;
643
644         /* This loop can be replaced with one division */
645         for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
646                 unsigned int err;
647                 unsigned int found = geo->in_height * vou_scale_v_num[i] /
648                         vou_scale_v_den[i];
649
650                 if (found > img_height_max)
651                         /* scales increase */
652                         break;
653
654                 err = abs(found - geo->output.height);
655                 if (err < best_err) {
656                         best_err = err;
657                         idx = i;
658                         best = found;
659                 }
660                 if (!err)
661                         break;
662         }
663
664         geo->output.height = best;
665         geo->scale_idx_v = idx;
666         if (geo->output.top + best > height_max)
667                 geo->output.top = height_max - best;
668
669         pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
670                  vou_scale_v_num[idx], vou_scale_v_den[idx], best);
671 }
672
673 static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
674                                 struct v4l2_format *fmt)
675 {
676         struct sh_vou_device *vou_dev = video_drvdata(file);
677         struct v4l2_pix_format *pix = &fmt->fmt.pix;
678         unsigned int img_height_max;
679         int pix_idx;
680         struct sh_vou_geometry geo;
681         struct v4l2_mbus_framefmt mbfmt = {
682                 /* Revisit: is this the correct code? */
683                 .code = V4L2_MBUS_FMT_YUYV8_2X8,
684                 .field = V4L2_FIELD_INTERLACED,
685                 .colorspace = V4L2_COLORSPACE_SMPTE170M,
686         };
687         int ret;
688
689         dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
690                 vou_dev->rect.width, vou_dev->rect.height,
691                 pix->width, pix->height);
692
693         if (pix->field == V4L2_FIELD_ANY)
694                 pix->field = V4L2_FIELD_NONE;
695
696         if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
697             pix->field != V4L2_FIELD_NONE)
698                 return -EINVAL;
699
700         for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
701                 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
702                         break;
703
704         if (pix_idx == ARRAY_SIZE(vou_fmt))
705                 return -EINVAL;
706
707         if (vou_dev->std & V4L2_STD_525_60)
708                 img_height_max = 480;
709         else
710                 img_height_max = 576;
711
712         /* Image width must be a multiple of 4 */
713         v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
714                               &pix->height, 0, img_height_max, 1, 0);
715
716         geo.in_width = pix->width;
717         geo.in_height = pix->height;
718         geo.output = vou_dev->rect;
719
720         vou_adjust_output(&geo, vou_dev->std);
721
722         mbfmt.width = geo.output.width;
723         mbfmt.height = geo.output.height;
724         ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
725                                          s_mbus_fmt, &mbfmt);
726         /* Must be implemented, so, don't check for -ENOIOCTLCMD */
727         if (ret < 0)
728                 return ret;
729
730         dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
731                 geo.output.width, geo.output.height, mbfmt.width, mbfmt.height);
732
733         /* Sanity checks */
734         if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
735             (unsigned)mbfmt.height > img_height_max ||
736             mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
737                 return -EIO;
738
739         if (mbfmt.width != geo.output.width ||
740             mbfmt.height != geo.output.height) {
741                 geo.output.width = mbfmt.width;
742                 geo.output.height = mbfmt.height;
743
744                 vou_adjust_input(&geo, vou_dev->std);
745         }
746
747         /* We tried to preserve output rectangle, but it could have changed */
748         vou_dev->rect = geo.output;
749         pix->width = geo.in_width;
750         pix->height = geo.in_height;
751
752         dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
753                 pix->width, pix->height);
754
755         vou_dev->pix_idx = pix_idx;
756
757         vou_dev->pix = *pix;
758
759         sh_vou_configure_geometry(vou_dev, pix_idx,
760                                   geo.scale_idx_h, geo.scale_idx_v);
761
762         return 0;
763 }
764
765 static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
766                                   struct v4l2_format *fmt)
767 {
768         struct sh_vou_device *vou_dev = video_drvdata(file);
769         struct v4l2_pix_format *pix = &fmt->fmt.pix;
770         int i;
771
772         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
773
774         fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
775         pix->field = V4L2_FIELD_NONE;
776
777         v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
778                               &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
779
780         for (i = 0; i < ARRAY_SIZE(vou_fmt); i++)
781                 if (vou_fmt[i].pfmt == pix->pixelformat)
782                         return 0;
783
784         pix->pixelformat = vou_fmt[0].pfmt;
785
786         return 0;
787 }
788
789 static int sh_vou_reqbufs(struct file *file, void *priv,
790                           struct v4l2_requestbuffers *req)
791 {
792         struct sh_vou_device *vou_dev = video_drvdata(file);
793         struct sh_vou_file *vou_file = priv;
794
795         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
796
797         if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
798                 return -EINVAL;
799
800         return videobuf_reqbufs(&vou_file->vbq, req);
801 }
802
803 static int sh_vou_querybuf(struct file *file, void *priv,
804                            struct v4l2_buffer *b)
805 {
806         struct sh_vou_device *vou_dev = video_drvdata(file);
807         struct sh_vou_file *vou_file = priv;
808
809         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
810
811         return videobuf_querybuf(&vou_file->vbq, b);
812 }
813
814 static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
815 {
816         struct sh_vou_device *vou_dev = video_drvdata(file);
817         struct sh_vou_file *vou_file = priv;
818
819         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
820
821         return videobuf_qbuf(&vou_file->vbq, b);
822 }
823
824 static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
825 {
826         struct sh_vou_device *vou_dev = video_drvdata(file);
827         struct sh_vou_file *vou_file = priv;
828
829         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
830
831         return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
832 }
833
834 static int sh_vou_streamon(struct file *file, void *priv,
835                            enum v4l2_buf_type buftype)
836 {
837         struct sh_vou_device *vou_dev = video_drvdata(file);
838         struct sh_vou_file *vou_file = priv;
839         int ret;
840
841         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
842
843         ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
844                                          video, s_stream, 1);
845         if (ret < 0 && ret != -ENOIOCTLCMD)
846                 return ret;
847
848         /* This calls our .buf_queue() (== sh_vou_buf_queue) */
849         return videobuf_streamon(&vou_file->vbq);
850 }
851
852 static int sh_vou_streamoff(struct file *file, void *priv,
853                             enum v4l2_buf_type buftype)
854 {
855         struct sh_vou_device *vou_dev = video_drvdata(file);
856         struct sh_vou_file *vou_file = priv;
857
858         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
859
860         /*
861          * This calls buf_release from host driver's videobuf_queue_ops for all
862          * remaining buffers. When the last buffer is freed, stop streaming
863          */
864         videobuf_streamoff(&vou_file->vbq);
865         v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
866
867         return 0;
868 }
869
870 static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
871 {
872         switch (bus_fmt) {
873         default:
874                 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
875                            __func__, bus_fmt);
876         case SH_VOU_BUS_8BIT:
877                 return 1;
878         case SH_VOU_BUS_16BIT:
879                 return 0;
880         case SH_VOU_BUS_BT656:
881                 return 3;
882         }
883 }
884
885 static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
886 {
887         struct sh_vou_device *vou_dev = video_drvdata(file);
888         int ret;
889
890         dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
891
892         if (std_id & ~vou_dev->vdev->tvnorms)
893                 return -EINVAL;
894
895         ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
896                                          s_std_output, std_id);
897         /* Shall we continue, if the subdev doesn't support .s_std_output()? */
898         if (ret < 0 && ret != -ENOIOCTLCMD)
899                 return ret;
900
901         if (std_id & V4L2_STD_525_60)
902                 sh_vou_reg_ab_set(vou_dev, VOUCR,
903                         sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
904         else
905                 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
906
907         vou_dev->std = std_id;
908
909         return 0;
910 }
911
912 static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
913 {
914         struct sh_vou_device *vou_dev = video_drvdata(file);
915
916         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
917
918         *std = vou_dev->std;
919
920         return 0;
921 }
922
923 static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
924 {
925         struct sh_vou_device *vou_dev = video_drvdata(file);
926
927         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
928
929         a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
930         a->c = vou_dev->rect;
931
932         return 0;
933 }
934
935 /* Assume a dull encoder, do all the work ourselves. */
936 static int sh_vou_s_crop(struct file *file, void *fh, const struct v4l2_crop *a)
937 {
938         struct v4l2_crop a_writable = *a;
939         struct sh_vou_device *vou_dev = video_drvdata(file);
940         struct v4l2_rect *rect = &a_writable.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,
949         };
950         unsigned int img_height_max;
951         int ret;
952
953         dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
954                 rect->width, rect->height, rect->left, rect->top);
955
956         if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
957                 return -EINVAL;
958
959         if (vou_dev->std & V4L2_STD_525_60)
960                 img_height_max = 480;
961         else
962                 img_height_max = 576;
963
964         v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
965                               &rect->height, 0, img_height_max, 1, 0);
966
967         if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
968                 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
969
970         if (rect->height + rect->top > img_height_max)
971                 rect->top = img_height_max - rect->height;
972
973         geo.output = *rect;
974         geo.in_width = pix->width;
975         geo.in_height = pix->height;
976
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;
980         /*
981          * We first issue a S_CROP, so that the subsequent S_FMT delivers the
982          * final encoder configuration.
983          */
984         v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
985                                    s_crop, &sd_crop);
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,
989                                          s_mbus_fmt, &mbfmt);
990         /* Must be implemented, so, don't check for -ENOIOCTLCMD */
991         if (ret < 0)
992                 return ret;
993
994         /* Sanity checks */
995         if ((unsigned)mbfmt.width > VOU_MAX_IMAGE_WIDTH ||
996             (unsigned)mbfmt.height > img_height_max ||
997             mbfmt.code != V4L2_MBUS_FMT_YUYV8_2X8)
998                 return -EIO;
999
1000         geo.output.width = mbfmt.width;
1001         geo.output.height = mbfmt.height;
1002
1003         /*
1004          * No down-scaling. According to the API, current call has precedence:
1005          * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1006          */
1007         vou_adjust_input(&geo, vou_dev->std);
1008
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;
1013
1014         sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1015                                   geo.scale_idx_h, geo.scale_idx_v);
1016
1017         return 0;
1018 }
1019
1020 /*
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.
1026  */
1027 static int sh_vou_cropcap(struct file *file, void *priv,
1028                           struct v4l2_cropcap *a)
1029 {
1030         struct sh_vou_device *vou_dev = video_drvdata(file);
1031
1032         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1033
1034         a->type                         = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1035         a->bounds.left                  = 0;
1036         a->bounds.top                   = 0;
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;
1041         a->defrect.top                  = 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;
1046
1047         return 0;
1048 }
1049
1050 static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1051 {
1052         struct sh_vou_device *vou_dev = dev_id;
1053         static unsigned long j;
1054         struct videobuf_buffer *vb;
1055         static int cnt;
1056         u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1057         u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1058
1059         if (!(irq_status & 0x300)) {
1060                 if (printk_timed_ratelimit(&j, 500))
1061                         dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1062                                  irq_status);
1063                 return IRQ_NONE;
1064         }
1065
1066         spin_lock(&vou_dev->lock);
1067         if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1068                 if (printk_timed_ratelimit(&j, 500))
1069                         dev_warn(vou_dev->v4l2_dev.dev,
1070                                  "IRQ without active buffer: %x!\n", irq_status);
1071                 /* Just ack: buf_release will disable further interrupts */
1072                 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1073                 spin_unlock(&vou_dev->lock);
1074                 return IRQ_HANDLED;
1075         }
1076
1077         masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1078         dev_dbg(vou_dev->v4l2_dev.dev,
1079                 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1080                 irq_status, masked, vou_status, cnt);
1081
1082         cnt++;
1083         /* side = vou_status & 0x10000; */
1084
1085         /* Clear only set interrupts */
1086         sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1087
1088         vb = vou_dev->active;
1089         list_del(&vb->queue);
1090
1091         vb->state = VIDEOBUF_DONE;
1092         v4l2_get_timestamp(&vb->ts);
1093         vb->field_count++;
1094         wake_up(&vb->done);
1095
1096         if (list_empty(&vou_dev->queue)) {
1097                 /* Stop VOU */
1098                 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1099                         __func__, cnt);
1100                 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1101                 vou_dev->active = NULL;
1102                 vou_dev->status = SH_VOU_INITIALISING;
1103                 /* Disable End-of-Frame (VSYNC) interrupts */
1104                 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1105                 spin_unlock(&vou_dev->lock);
1106                 return IRQ_HANDLED;
1107         }
1108
1109         vou_dev->active = list_entry(vou_dev->queue.next,
1110                                      struct videobuf_buffer, queue);
1111
1112         if (vou_dev->active->queue.next != &vou_dev->queue) {
1113                 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1114                                                 struct videobuf_buffer, queue);
1115                 sh_vou_schedule_next(vou_dev, new);
1116         }
1117
1118         spin_unlock(&vou_dev->lock);
1119
1120         return IRQ_HANDLED;
1121 }
1122
1123 static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1124 {
1125         struct sh_vou_pdata *pdata = vou_dev->pdata;
1126         u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1127         int i = 100;
1128
1129         /* Disable all IRQs */
1130         sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1131
1132         /* Reset VOU interfaces - registers unaffected */
1133         sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1134         while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1135                 udelay(1);
1136
1137         if (!i)
1138                 return -ETIMEDOUT;
1139
1140         dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1141
1142         if (pdata->flags & SH_VOU_PCLK_FALLING)
1143                 voucr |= 1 << 28;
1144         if (pdata->flags & SH_VOU_HSYNC_LOW)
1145                 voucr |= 1 << 27;
1146         if (pdata->flags & SH_VOU_VSYNC_LOW)
1147                 voucr |= 1 << 26;
1148         sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1149
1150         /* Manual register side switching at first */
1151         sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1152         /* Default - fixed HSYNC length, can be made configurable is required */
1153         sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1154
1155         return 0;
1156 }
1157
1158 /* File operations */
1159 static int sh_vou_open(struct file *file)
1160 {
1161         struct sh_vou_device *vou_dev = video_drvdata(file);
1162         struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1163                                                GFP_KERNEL);
1164
1165         if (!vou_file)
1166                 return -ENOMEM;
1167
1168         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1169
1170         file->private_data = vou_file;
1171
1172         if (mutex_lock_interruptible(&vou_dev->fop_lock))
1173                 return -ERESTARTSYS;
1174         if (atomic_inc_return(&vou_dev->use_count) == 1) {
1175                 int ret;
1176                 /* First open */
1177                 vou_dev->status = SH_VOU_INITIALISING;
1178                 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
1179                 ret = sh_vou_hw_init(vou_dev);
1180                 if (ret < 0) {
1181                         atomic_dec(&vou_dev->use_count);
1182                         pm_runtime_put(vou_dev->v4l2_dev.dev);
1183                         vou_dev->status = SH_VOU_IDLE;
1184                         mutex_unlock(&vou_dev->fop_lock);
1185                         return ret;
1186                 }
1187         }
1188
1189         videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1190                                        vou_dev->v4l2_dev.dev, &vou_dev->lock,
1191                                        V4L2_BUF_TYPE_VIDEO_OUTPUT,
1192                                        V4L2_FIELD_NONE,
1193                                        sizeof(struct videobuf_buffer),
1194                                        vou_dev->vdev, &vou_dev->fop_lock);
1195         mutex_unlock(&vou_dev->fop_lock);
1196
1197         return 0;
1198 }
1199
1200 static int sh_vou_release(struct file *file)
1201 {
1202         struct sh_vou_device *vou_dev = video_drvdata(file);
1203         struct sh_vou_file *vou_file = file->private_data;
1204
1205         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1206
1207         if (!atomic_dec_return(&vou_dev->use_count)) {
1208                 mutex_lock(&vou_dev->fop_lock);
1209                 /* Last close */
1210                 vou_dev->status = SH_VOU_IDLE;
1211                 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
1212                 pm_runtime_put(vou_dev->v4l2_dev.dev);
1213                 mutex_unlock(&vou_dev->fop_lock);
1214         }
1215
1216         file->private_data = NULL;
1217         kfree(vou_file);
1218
1219         return 0;
1220 }
1221
1222 static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1223 {
1224         struct sh_vou_device *vou_dev = video_drvdata(file);
1225         struct sh_vou_file *vou_file = file->private_data;
1226         int ret;
1227
1228         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1229
1230         if (mutex_lock_interruptible(&vou_dev->fop_lock))
1231                 return -ERESTARTSYS;
1232         ret = videobuf_mmap_mapper(&vou_file->vbq, vma);
1233         mutex_unlock(&vou_dev->fop_lock);
1234         return ret;
1235 }
1236
1237 static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1238 {
1239         struct sh_vou_device *vou_dev = video_drvdata(file);
1240         struct sh_vou_file *vou_file = file->private_data;
1241         unsigned int res;
1242
1243         dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1244
1245         mutex_lock(&vou_dev->fop_lock);
1246         res = videobuf_poll_stream(file, &vou_file->vbq, wait);
1247         mutex_unlock(&vou_dev->fop_lock);
1248         return res;
1249 }
1250
1251 /* sh_vou display ioctl operations */
1252 static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1253         .vidioc_querycap                = sh_vou_querycap,
1254         .vidioc_enum_fmt_vid_out        = sh_vou_enum_fmt_vid_out,
1255         .vidioc_g_fmt_vid_out           = sh_vou_g_fmt_vid_out,
1256         .vidioc_s_fmt_vid_out           = sh_vou_s_fmt_vid_out,
1257         .vidioc_try_fmt_vid_out         = sh_vou_try_fmt_vid_out,
1258         .vidioc_reqbufs                 = sh_vou_reqbufs,
1259         .vidioc_querybuf                = sh_vou_querybuf,
1260         .vidioc_qbuf                    = sh_vou_qbuf,
1261         .vidioc_dqbuf                   = sh_vou_dqbuf,
1262         .vidioc_streamon                = sh_vou_streamon,
1263         .vidioc_streamoff               = sh_vou_streamoff,
1264         .vidioc_s_std                   = sh_vou_s_std,
1265         .vidioc_g_std                   = sh_vou_g_std,
1266         .vidioc_cropcap                 = sh_vou_cropcap,
1267         .vidioc_g_crop                  = sh_vou_g_crop,
1268         .vidioc_s_crop                  = sh_vou_s_crop,
1269 };
1270
1271 static const struct v4l2_file_operations sh_vou_fops = {
1272         .owner          = THIS_MODULE,
1273         .open           = sh_vou_open,
1274         .release        = sh_vou_release,
1275         .unlocked_ioctl = video_ioctl2,
1276         .mmap           = sh_vou_mmap,
1277         .poll           = sh_vou_poll,
1278 };
1279
1280 static const struct video_device sh_vou_video_template = {
1281         .name           = "sh_vou",
1282         .fops           = &sh_vou_fops,
1283         .ioctl_ops      = &sh_vou_ioctl_ops,
1284         .tvnorms        = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
1285         .vfl_dir        = VFL_DIR_TX,
1286 };
1287
1288 static int sh_vou_probe(struct platform_device *pdev)
1289 {
1290         struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1291         struct v4l2_rect *rect;
1292         struct v4l2_pix_format *pix;
1293         struct i2c_adapter *i2c_adap;
1294         struct video_device *vdev;
1295         struct sh_vou_device *vou_dev;
1296         struct resource *reg_res, *region;
1297         struct v4l2_subdev *subdev;
1298         int irq, ret;
1299
1300         reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1301         irq = platform_get_irq(pdev, 0);
1302
1303         if (!vou_pdata || !reg_res || irq <= 0) {
1304                 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1305                 return -ENODEV;
1306         }
1307
1308         vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
1309         if (!vou_dev)
1310                 return -ENOMEM;
1311
1312         INIT_LIST_HEAD(&vou_dev->queue);
1313         spin_lock_init(&vou_dev->lock);
1314         mutex_init(&vou_dev->fop_lock);
1315         atomic_set(&vou_dev->use_count, 0);
1316         vou_dev->pdata = vou_pdata;
1317         vou_dev->status = SH_VOU_IDLE;
1318
1319         rect = &vou_dev->rect;
1320         pix = &vou_dev->pix;
1321
1322         /* Fill in defaults */
1323         vou_dev->std            = V4L2_STD_NTSC_M;
1324         rect->left              = 0;
1325         rect->top               = 0;
1326         rect->width             = VOU_MAX_IMAGE_WIDTH;
1327         rect->height            = 480;
1328         pix->width              = VOU_MAX_IMAGE_WIDTH;
1329         pix->height             = 480;
1330         pix->pixelformat        = V4L2_PIX_FMT_YVYU;
1331         pix->field              = V4L2_FIELD_NONE;
1332         pix->bytesperline       = VOU_MAX_IMAGE_WIDTH * 2;
1333         pix->sizeimage          = VOU_MAX_IMAGE_WIDTH * 2 * 480;
1334         pix->colorspace         = V4L2_COLORSPACE_SMPTE170M;
1335
1336         region = request_mem_region(reg_res->start, resource_size(reg_res),
1337                                     pdev->name);
1338         if (!region) {
1339                 dev_err(&pdev->dev, "VOU region already claimed\n");
1340                 ret = -EBUSY;
1341                 goto ereqmemreg;
1342         }
1343
1344         vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
1345         if (!vou_dev->base) {
1346                 ret = -ENOMEM;
1347                 goto emap;
1348         }
1349
1350         ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
1351         if (ret < 0)
1352                 goto ereqirq;
1353
1354         ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1355         if (ret < 0) {
1356                 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1357                 goto ev4l2devreg;
1358         }
1359
1360         /* Allocate memory for video device */
1361         vdev = video_device_alloc();
1362         if (vdev == NULL) {
1363                 ret = -ENOMEM;
1364                 goto evdevalloc;
1365         }
1366
1367         *vdev = sh_vou_video_template;
1368         if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1369                 vdev->tvnorms |= V4L2_STD_PAL;
1370         vdev->v4l2_dev = &vou_dev->v4l2_dev;
1371         vdev->release = video_device_release;
1372         vdev->lock = &vou_dev->fop_lock;
1373
1374         vou_dev->vdev = vdev;
1375         video_set_drvdata(vdev, vou_dev);
1376
1377         pm_runtime_enable(&pdev->dev);
1378         pm_runtime_resume(&pdev->dev);
1379
1380         i2c_adap = i2c_get_adapter(vou_pdata->i2c_adap);
1381         if (!i2c_adap) {
1382                 ret = -ENODEV;
1383                 goto ei2cgadap;
1384         }
1385
1386         ret = sh_vou_hw_init(vou_dev);
1387         if (ret < 0)
1388                 goto ereset;
1389
1390         subdev = v4l2_i2c_new_subdev_board(&vou_dev->v4l2_dev, i2c_adap,
1391                         vou_pdata->board_info, NULL);
1392         if (!subdev) {
1393                 ret = -ENOMEM;
1394                 goto ei2cnd;
1395         }
1396
1397         ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1398         if (ret < 0)
1399                 goto evregdev;
1400
1401         return 0;
1402
1403 evregdev:
1404 ei2cnd:
1405 ereset:
1406         i2c_put_adapter(i2c_adap);
1407 ei2cgadap:
1408         video_device_release(vdev);
1409         pm_runtime_disable(&pdev->dev);
1410 evdevalloc:
1411         v4l2_device_unregister(&vou_dev->v4l2_dev);
1412 ev4l2devreg:
1413         free_irq(irq, vou_dev);
1414 ereqirq:
1415         iounmap(vou_dev->base);
1416 emap:
1417         release_mem_region(reg_res->start, resource_size(reg_res));
1418 ereqmemreg:
1419         kfree(vou_dev);
1420         return ret;
1421 }
1422
1423 static int sh_vou_remove(struct platform_device *pdev)
1424 {
1425         int irq = platform_get_irq(pdev, 0);
1426         struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1427         struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1428                                                 struct sh_vou_device, v4l2_dev);
1429         struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1430                                             struct v4l2_subdev, list);
1431         struct i2c_client *client = v4l2_get_subdevdata(sd);
1432         struct resource *reg_res;
1433
1434         if (irq > 0)
1435                 free_irq(irq, vou_dev);
1436         pm_runtime_disable(&pdev->dev);
1437         video_unregister_device(vou_dev->vdev);
1438         i2c_put_adapter(client->adapter);
1439         v4l2_device_unregister(&vou_dev->v4l2_dev);
1440         iounmap(vou_dev->base);
1441         reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1442         if (reg_res)
1443                 release_mem_region(reg_res->start, resource_size(reg_res));
1444         kfree(vou_dev);
1445         return 0;
1446 }
1447
1448 static struct platform_driver __refdata sh_vou = {
1449         .remove  = sh_vou_remove,
1450         .driver  = {
1451                 .name   = "sh-vou",
1452                 .owner  = THIS_MODULE,
1453         },
1454 };
1455
1456 module_platform_driver_probe(sh_vou, sh_vou_probe);
1457
1458 MODULE_DESCRIPTION("SuperH VOU driver");
1459 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1460 MODULE_LICENSE("GPL v2");
1461 MODULE_VERSION("0.1.0");
1462 MODULE_ALIAS("platform:sh-vou");