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