[media] vsp1: declar vsp1_pipeline_stopped() as static
[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;
d079f99f 65 struct video_device vdev;
a81fb9b2
GL
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) {
bc1ebd70 210 default:
a81fb9b2
GL
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
230static 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 */
0e0809a5 235 videobuf_waiton(vq, vb, 0, 0);
a81fb9b2
GL
236 videobuf_dma_contig_free(vq, vb);
237 vb->state = VIDEOBUF_NEEDS_INIT;
238}
239
69756693 240/* Locking: caller holds fop_lock mutex */
a81fb9b2
GL
241static 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
d899eddd
LP
257 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): count=%d, size=%d\n", __func__,
258 *count, *size);
a81fb9b2
GL
259
260 return 0;
261}
262
69756693 263/* Locking: caller holds fop_lock mutex */
a81fb9b2
GL
264static 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
d899eddd 274 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
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 */
3f8965e0 289 dev_warn(vq->dev, "User buffer too small: [%zu] @ %lx\n",
a81fb9b2
GL
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
d899eddd 304 dev_dbg(vou_dev->v4l2_dev.dev,
3f8965e0 305 "%s(): fmt #%d, %u bytes per line, phys %pad, type %d, state %d\n",
a81fb9b2 306 __func__, vou_dev->pix_idx, bytes_per_line,
3f8965e0
LP
307 ({ dma_addr_t addr = videobuf_to_dma_contig(vb); &addr; }),
308 vb->memory, vb->state);
a81fb9b2
GL
309
310 return 0;
311}
312
69756693 313/* Locking: caller holds fop_lock mutex and vq->irqlock spinlock */
a81fb9b2
GL
314static 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
d899eddd 320 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
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);
d899eddd
LP
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));
a81fb9b2
GL
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);
d899eddd
LP
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));
a81fb9b2
GL
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
354static 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
d899eddd 361 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
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
383static 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 */
391static int sh_vou_querycap(struct file *file, void *priv,
392 struct v4l2_capability *cap)
393{
d899eddd 394 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 395
d899eddd 396 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
397
398 strlcpy(cap->card, "SuperH VOU", sizeof(cap->card));
a020c747
HV
399 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING;
400 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
a81fb9b2
GL
401 return 0;
402}
403
404/* Enumerate formats, that the device can accept from the user */
405static int sh_vou_enum_fmt_vid_out(struct file *file, void *priv,
406 struct v4l2_fmtdesc *fmt)
407{
d899eddd 408 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
409
410 if (fmt->index >= ARRAY_SIZE(vou_fmt))
411 return -EINVAL;
412
d899eddd 413 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
414
415 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
416 strlcpy(fmt->description, vou_fmt[fmt->index].desc,
417 sizeof(fmt->description));
418 fmt->pixelformat = vou_fmt[fmt->index].pfmt;
419
420 return 0;
421}
422
423static int sh_vou_g_fmt_vid_out(struct file *file, void *priv,
424 struct v4l2_format *fmt)
425{
fd51625d 426 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
427
428 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
429
430 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
431 fmt->fmt.pix = vou_dev->pix;
432
433 return 0;
434}
435
436static const unsigned char vou_scale_h_num[] = {1, 9, 2, 9, 4};
437static const unsigned char vou_scale_h_den[] = {1, 8, 1, 4, 1};
438static const unsigned char vou_scale_h_fld[] = {0, 2, 1, 3};
439static const unsigned char vou_scale_v_num[] = {1, 2, 4};
440static const unsigned char vou_scale_v_den[] = {1, 1, 1};
441static const unsigned char vou_scale_v_fld[] = {0, 1};
442
443static void sh_vou_configure_geometry(struct sh_vou_device *vou_dev,
444 int pix_idx, int w_idx, int h_idx)
445{
446 struct sh_vou_fmt *fmt = vou_fmt + pix_idx;
a622cc51 447 unsigned int black_left, black_top, width_max,
a81fb9b2
GL
448 frame_in_height, frame_out_height, frame_out_top;
449 struct v4l2_rect *rect = &vou_dev->rect;
450 struct v4l2_pix_format *pix = &vou_dev->pix;
451 u32 vouvcr = 0, dsr_h, dsr_v;
452
453 if (vou_dev->std & V4L2_STD_525_60) {
454 width_max = 858;
a622cc51 455 /* height_max = 262; */
a81fb9b2
GL
456 } else {
457 width_max = 864;
a622cc51 458 /* height_max = 312; */
a81fb9b2
GL
459 }
460
461 frame_in_height = pix->height / 2;
462 frame_out_height = rect->height / 2;
463 frame_out_top = rect->top / 2;
464
465 /*
466 * Cropping scheme: max useful image is 720x480, and the total video
467 * area is 858x525 (NTSC) or 864x625 (PAL). AK8813 / 8814 starts
468 * sampling data beginning with fixed 276th (NTSC) / 288th (PAL) clock,
469 * of which the first 33 / 25 clocks HSYNC must be held active. This
470 * has to be configured in CR[HW]. 1 pixel equals 2 clock periods.
471 * This gives CR[HW] = 16 / 12, VPR[HVP] = 138 / 144, which gives
472 * exactly 858 - 138 = 864 - 144 = 720! We call the out-of-display area,
473 * beyond DSR, specified on the left and top by the VPR register "black
474 * pixels" and out-of-image area (DPR) "background pixels." We fix VPR
475 * at 138 / 144 : 20, because that's the HSYNC timing, that our first
476 * client requires, and that's exactly what leaves us 720 pixels for the
477 * image; we leave VPR[VVP] at default 20 for now, because the client
478 * doesn't seem to have any special requirements for it. Otherwise we
479 * could also set it to max - 240 = 22 / 72. Thus VPR depends only on
480 * the selected standard, and DPR and DSR are selected according to
481 * cropping. Q: how does the client detect the first valid line? Does
482 * HSYNC stay inactive during invalid (black) lines?
483 */
484 black_left = width_max - VOU_MAX_IMAGE_WIDTH;
485 black_top = 20;
486
487 dsr_h = rect->width + rect->left;
488 dsr_v = frame_out_height + frame_out_top;
489
490 dev_dbg(vou_dev->v4l2_dev.dev,
491 "image %ux%u, black %u:%u, offset %u:%u, display %ux%u\n",
492 pix->width, frame_in_height, black_left, black_top,
493 rect->left, frame_out_top, dsr_h, dsr_v);
494
495 /* VOUISR height - half of a frame height in frame mode */
496 sh_vou_reg_ab_write(vou_dev, VOUISR, (pix->width << 16) | frame_in_height);
497 sh_vou_reg_ab_write(vou_dev, VOUVPR, (black_left << 16) | black_top);
498 sh_vou_reg_ab_write(vou_dev, VOUDPR, (rect->left << 16) | frame_out_top);
499 sh_vou_reg_ab_write(vou_dev, VOUDSR, (dsr_h << 16) | dsr_v);
500
501 /*
502 * if necessary, we could set VOUHIR to
503 * max(black_left + dsr_h, width_max) here
504 */
505
506 if (w_idx)
507 vouvcr |= (1 << 15) | (vou_scale_h_fld[w_idx - 1] << 4);
508 if (h_idx)
509 vouvcr |= (1 << 14) | vou_scale_v_fld[h_idx - 1];
510
511 dev_dbg(vou_dev->v4l2_dev.dev, "%s: scaling 0x%x\n", fmt->desc, vouvcr);
512
513 /* To produce a colour bar for testing set bit 23 of VOUVCR */
514 sh_vou_reg_ab_write(vou_dev, VOUVCR, vouvcr);
515 sh_vou_reg_ab_write(vou_dev, VOUDFR,
516 fmt->pkf | (fmt->yf << 8) | (fmt->rgb << 16));
517}
518
519struct sh_vou_geometry {
520 struct v4l2_rect output;
521 unsigned int in_width;
522 unsigned int in_height;
523 int scale_idx_h;
524 int scale_idx_v;
525};
526
527/*
528 * Find input geometry, that we can use to produce output, closest to the
529 * requested rectangle, using VOU scaling
530 */
531static void vou_adjust_input(struct sh_vou_geometry *geo, v4l2_std_id std)
532{
533 /* The compiler cannot know, that best and idx will indeed be set */
765fe17c 534 unsigned int best_err = UINT_MAX, best = 0, img_height_max;
a81fb9b2
GL
535 int i, idx = 0;
536
765fe17c
GL
537 if (std & V4L2_STD_525_60)
538 img_height_max = 480;
539 else
540 img_height_max = 576;
a81fb9b2
GL
541
542 /* Image width must be a multiple of 4 */
543 v4l_bound_align_image(&geo->in_width, 0, VOU_MAX_IMAGE_WIDTH, 2,
765fe17c 544 &geo->in_height, 0, img_height_max, 1, 0);
a81fb9b2
GL
545
546 /* Select scales to come as close as possible to the output image */
547 for (i = ARRAY_SIZE(vou_scale_h_num) - 1; i >= 0; i--) {
548 unsigned int err;
549 unsigned int found = geo->output.width * vou_scale_h_den[i] /
550 vou_scale_h_num[i];
551
552 if (found > VOU_MAX_IMAGE_WIDTH)
553 /* scales increase */
554 break;
555
556 err = abs(found - geo->in_width);
557 if (err < best_err) {
558 best_err = err;
559 idx = i;
560 best = found;
561 }
562 if (!err)
563 break;
564 }
565
566 geo->in_width = best;
567 geo->scale_idx_h = idx;
568
569 best_err = UINT_MAX;
570
571 /* This loop can be replaced with one division */
572 for (i = ARRAY_SIZE(vou_scale_v_num) - 1; i >= 0; i--) {
573 unsigned int err;
574 unsigned int found = geo->output.height * vou_scale_v_den[i] /
575 vou_scale_v_num[i];
576
765fe17c 577 if (found > img_height_max)
a81fb9b2
GL
578 /* scales increase */
579 break;
580
581 err = abs(found - geo->in_height);
582 if (err < best_err) {
583 best_err = err;
584 idx = i;
585 best = found;
586 }
587 if (!err)
588 break;
589 }
590
591 geo->in_height = best;
592 geo->scale_idx_v = idx;
593}
594
595/*
596 * Find output geometry, that we can produce, using VOU scaling, closest to
597 * the requested rectangle
598 */
599static void vou_adjust_output(struct sh_vou_geometry *geo, v4l2_std_id std)
600{
bc1ebd70
GL
601 unsigned int best_err = UINT_MAX, best = geo->in_width,
602 width_max, height_max, img_height_max;
5ac417ef 603 int i, idx_h = 0, idx_v = 0;
a81fb9b2
GL
604
605 if (std & V4L2_STD_525_60) {
606 width_max = 858;
607 height_max = 262 * 2;
765fe17c 608 img_height_max = 480;
a81fb9b2
GL
609 } else {
610 width_max = 864;
611 height_max = 312 * 2;
765fe17c 612 img_height_max = 576;
a81fb9b2
GL
613 }
614
615 /* Select scales to come as close as possible to the output image */
616 for (i = 0; i < ARRAY_SIZE(vou_scale_h_num); i++) {
617 unsigned int err;
618 unsigned int found = geo->in_width * vou_scale_h_num[i] /
619 vou_scale_h_den[i];
620
621 if (found > VOU_MAX_IMAGE_WIDTH)
622 /* scales increase */
623 break;
624
625 err = abs(found - geo->output.width);
626 if (err < best_err) {
627 best_err = err;
5ac417ef 628 idx_h = i;
a81fb9b2
GL
629 best = found;
630 }
631 if (!err)
632 break;
633 }
634
635 geo->output.width = best;
5ac417ef 636 geo->scale_idx_h = idx_h;
a81fb9b2
GL
637 if (geo->output.left + best > width_max)
638 geo->output.left = width_max - best;
639
640 pr_debug("%s(): W %u * %u/%u = %u\n", __func__, geo->in_width,
5ac417ef 641 vou_scale_h_num[idx_h], vou_scale_h_den[idx_h], best);
a81fb9b2
GL
642
643 best_err = UINT_MAX;
644
645 /* This loop can be replaced with one division */
646 for (i = 0; i < ARRAY_SIZE(vou_scale_v_num); i++) {
647 unsigned int err;
648 unsigned int found = geo->in_height * vou_scale_v_num[i] /
649 vou_scale_v_den[i];
650
765fe17c 651 if (found > img_height_max)
a81fb9b2
GL
652 /* scales increase */
653 break;
654
655 err = abs(found - geo->output.height);
656 if (err < best_err) {
657 best_err = err;
5ac417ef 658 idx_v = i;
a81fb9b2
GL
659 best = found;
660 }
661 if (!err)
662 break;
663 }
664
665 geo->output.height = best;
5ac417ef 666 geo->scale_idx_v = idx_v;
a81fb9b2
GL
667 if (geo->output.top + best > height_max)
668 geo->output.top = height_max - best;
669
670 pr_debug("%s(): H %u * %u/%u = %u\n", __func__, geo->in_height,
5ac417ef 671 vou_scale_v_num[idx_v], vou_scale_v_den[idx_v], best);
a81fb9b2
GL
672}
673
674static int sh_vou_s_fmt_vid_out(struct file *file, void *priv,
675 struct v4l2_format *fmt)
676{
fd51625d 677 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 678 struct v4l2_pix_format *pix = &fmt->fmt.pix;
765fe17c 679 unsigned int img_height_max;
a81fb9b2
GL
680 int pix_idx;
681 struct sh_vou_geometry geo;
ebf984bb
HV
682 struct v4l2_subdev_format format = {
683 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
a81fb9b2 684 /* Revisit: is this the correct code? */
ebf984bb
HV
685 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
686 .format.field = V4L2_FIELD_INTERLACED,
687 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
a81fb9b2 688 };
ebf984bb 689 struct v4l2_mbus_framefmt *mbfmt = &format.format;
a81fb9b2
GL
690 int ret;
691
692 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
693 vou_dev->rect.width, vou_dev->rect.height,
694 pix->width, pix->height);
695
696 if (pix->field == V4L2_FIELD_ANY)
697 pix->field = V4L2_FIELD_NONE;
698
699 if (fmt->type != V4L2_BUF_TYPE_VIDEO_OUTPUT ||
700 pix->field != V4L2_FIELD_NONE)
701 return -EINVAL;
702
703 for (pix_idx = 0; pix_idx < ARRAY_SIZE(vou_fmt); pix_idx++)
704 if (vou_fmt[pix_idx].pfmt == pix->pixelformat)
705 break;
706
707 if (pix_idx == ARRAY_SIZE(vou_fmt))
708 return -EINVAL;
709
765fe17c
GL
710 if (vou_dev->std & V4L2_STD_525_60)
711 img_height_max = 480;
712 else
713 img_height_max = 576;
714
a81fb9b2
GL
715 /* Image width must be a multiple of 4 */
716 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 2,
765fe17c 717 &pix->height, 0, img_height_max, 1, 0);
a81fb9b2
GL
718
719 geo.in_width = pix->width;
720 geo.in_height = pix->height;
721 geo.output = vou_dev->rect;
722
723 vou_adjust_output(&geo, vou_dev->std);
724
ebf984bb
HV
725 mbfmt->width = geo.output.width;
726 mbfmt->height = geo.output.height;
727 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
728 set_fmt, NULL, &format);
a81fb9b2
GL
729 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
730 if (ret < 0)
731 return ret;
732
733 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u -> %ux%u\n", __func__,
ebf984bb 734 geo.output.width, geo.output.height, mbfmt->width, mbfmt->height);
a81fb9b2
GL
735
736 /* Sanity checks */
ebf984bb
HV
737 if ((unsigned)mbfmt->width > VOU_MAX_IMAGE_WIDTH ||
738 (unsigned)mbfmt->height > img_height_max ||
739 mbfmt->code != MEDIA_BUS_FMT_YUYV8_2X8)
a81fb9b2
GL
740 return -EIO;
741
ebf984bb
HV
742 if (mbfmt->width != geo.output.width ||
743 mbfmt->height != geo.output.height) {
744 geo.output.width = mbfmt->width;
745 geo.output.height = mbfmt->height;
a81fb9b2
GL
746
747 vou_adjust_input(&geo, vou_dev->std);
748 }
749
750 /* We tried to preserve output rectangle, but it could have changed */
751 vou_dev->rect = geo.output;
752 pix->width = geo.in_width;
753 pix->height = geo.in_height;
754
755 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u\n", __func__,
756 pix->width, pix->height);
757
758 vou_dev->pix_idx = pix_idx;
759
760 vou_dev->pix = *pix;
761
762 sh_vou_configure_geometry(vou_dev, pix_idx,
763 geo.scale_idx_h, geo.scale_idx_v);
764
765 return 0;
766}
767
768static int sh_vou_try_fmt_vid_out(struct file *file, void *priv,
769 struct v4l2_format *fmt)
770{
d899eddd 771 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
772 struct v4l2_pix_format *pix = &fmt->fmt.pix;
773 int i;
774
d899eddd 775 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
776
777 fmt->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
778 pix->field = V4L2_FIELD_NONE;
779
780 v4l_bound_align_image(&pix->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
781 &pix->height, 0, VOU_MAX_IMAGE_HEIGHT, 1, 0);
782
47c32ec9 783 for (i = 0; i < ARRAY_SIZE(vou_fmt); i++)
a81fb9b2
GL
784 if (vou_fmt[i].pfmt == pix->pixelformat)
785 return 0;
786
787 pix->pixelformat = vou_fmt[0].pfmt;
788
789 return 0;
790}
791
792static int sh_vou_reqbufs(struct file *file, void *priv,
793 struct v4l2_requestbuffers *req)
794{
d899eddd 795 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
796 struct sh_vou_file *vou_file = priv;
797
d899eddd 798 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
799
800 if (req->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
801 return -EINVAL;
802
803 return videobuf_reqbufs(&vou_file->vbq, req);
804}
805
806static int sh_vou_querybuf(struct file *file, void *priv,
807 struct v4l2_buffer *b)
808{
d899eddd 809 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
810 struct sh_vou_file *vou_file = priv;
811
d899eddd 812 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
813
814 return videobuf_querybuf(&vou_file->vbq, b);
815}
816
817static int sh_vou_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
818{
d899eddd 819 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
820 struct sh_vou_file *vou_file = priv;
821
d899eddd 822 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
823
824 return videobuf_qbuf(&vou_file->vbq, b);
825}
826
827static int sh_vou_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
828{
d899eddd 829 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
830 struct sh_vou_file *vou_file = priv;
831
d899eddd 832 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
833
834 return videobuf_dqbuf(&vou_file->vbq, b, file->f_flags & O_NONBLOCK);
835}
836
837static int sh_vou_streamon(struct file *file, void *priv,
838 enum v4l2_buf_type buftype)
839{
fd51625d 840 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
841 struct sh_vou_file *vou_file = priv;
842 int ret;
843
d899eddd 844 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
845
846 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0,
847 video, s_stream, 1);
848 if (ret < 0 && ret != -ENOIOCTLCMD)
849 return ret;
850
851 /* This calls our .buf_queue() (== sh_vou_buf_queue) */
852 return videobuf_streamon(&vou_file->vbq);
853}
854
855static int sh_vou_streamoff(struct file *file, void *priv,
856 enum v4l2_buf_type buftype)
857{
fd51625d 858 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
859 struct sh_vou_file *vou_file = priv;
860
d899eddd 861 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
862
863 /*
864 * This calls buf_release from host driver's videobuf_queue_ops for all
865 * remaining buffers. When the last buffer is freed, stop streaming
866 */
867 videobuf_streamoff(&vou_file->vbq);
868 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video, s_stream, 0);
869
870 return 0;
871}
872
873static u32 sh_vou_ntsc_mode(enum sh_vou_bus_fmt bus_fmt)
874{
875 switch (bus_fmt) {
876 default:
877 pr_warning("%s(): Invalid bus-format code %d, using default 8-bit\n",
878 __func__, bus_fmt);
879 case SH_VOU_BUS_8BIT:
880 return 1;
881 case SH_VOU_BUS_16BIT:
882 return 0;
883 case SH_VOU_BUS_BT656:
884 return 3;
885 }
886}
887
314527ac 888static int sh_vou_s_std(struct file *file, void *priv, v4l2_std_id std_id)
a81fb9b2 889{
fd51625d 890 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
891 int ret;
892
314527ac 893 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): 0x%llx\n", __func__, std_id);
a81fb9b2 894
d079f99f 895 if (std_id & ~vou_dev->vdev.tvnorms)
a81fb9b2
GL
896 return -EINVAL;
897
898 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
314527ac 899 s_std_output, std_id);
a81fb9b2
GL
900 /* Shall we continue, if the subdev doesn't support .s_std_output()? */
901 if (ret < 0 && ret != -ENOIOCTLCMD)
902 return ret;
903
314527ac 904 if (std_id & V4L2_STD_525_60)
a81fb9b2
GL
905 sh_vou_reg_ab_set(vou_dev, VOUCR,
906 sh_vou_ntsc_mode(vou_dev->pdata->bus_fmt) << 29, 7 << 29);
907 else
908 sh_vou_reg_ab_set(vou_dev, VOUCR, 5 << 29, 7 << 29);
909
314527ac 910 vou_dev->std = std_id;
a81fb9b2
GL
911
912 return 0;
913}
914
915static int sh_vou_g_std(struct file *file, void *priv, v4l2_std_id *std)
916{
fd51625d 917 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
918
919 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
920
921 *std = vou_dev->std;
922
923 return 0;
924}
925
926static int sh_vou_g_crop(struct file *file, void *fh, struct v4l2_crop *a)
927{
fd51625d 928 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
929
930 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
931
932 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
933 a->c = vou_dev->rect;
934
935 return 0;
936}
937
938/* Assume a dull encoder, do all the work ourselves. */
4f996594 939static int sh_vou_s_crop(struct file *file, void *fh, const struct v4l2_crop *a)
a81fb9b2 940{
17803580 941 struct v4l2_crop a_writable = *a;
fd51625d 942 struct sh_vou_device *vou_dev = video_drvdata(file);
17803580 943 struct v4l2_rect *rect = &a_writable.c;
a81fb9b2
GL
944 struct v4l2_crop sd_crop = {.type = V4L2_BUF_TYPE_VIDEO_OUTPUT};
945 struct v4l2_pix_format *pix = &vou_dev->pix;
946 struct sh_vou_geometry geo;
ebf984bb
HV
947 struct v4l2_subdev_format format = {
948 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
a81fb9b2 949 /* Revisit: is this the correct code? */
ebf984bb
HV
950 .format.code = MEDIA_BUS_FMT_YUYV8_2X8,
951 .format.field = V4L2_FIELD_INTERLACED,
952 .format.colorspace = V4L2_COLORSPACE_SMPTE170M,
a81fb9b2 953 };
765fe17c 954 unsigned int img_height_max;
a81fb9b2
GL
955 int ret;
956
957 dev_dbg(vou_dev->v4l2_dev.dev, "%s(): %ux%u@%u:%u\n", __func__,
958 rect->width, rect->height, rect->left, rect->top);
959
960 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
961 return -EINVAL;
962
765fe17c
GL
963 if (vou_dev->std & V4L2_STD_525_60)
964 img_height_max = 480;
965 else
966 img_height_max = 576;
967
a81fb9b2 968 v4l_bound_align_image(&rect->width, 0, VOU_MAX_IMAGE_WIDTH, 1,
765fe17c 969 &rect->height, 0, img_height_max, 1, 0);
a81fb9b2
GL
970
971 if (rect->width + rect->left > VOU_MAX_IMAGE_WIDTH)
972 rect->left = VOU_MAX_IMAGE_WIDTH - rect->width;
973
765fe17c
GL
974 if (rect->height + rect->top > img_height_max)
975 rect->top = img_height_max - rect->height;
a81fb9b2
GL
976
977 geo.output = *rect;
978 geo.in_width = pix->width;
979 geo.in_height = pix->height;
980
981 /* Configure the encoder one-to-one, position at 0, ignore errors */
982 sd_crop.c.width = geo.output.width;
983 sd_crop.c.height = geo.output.height;
984 /*
985 * We first issue a S_CROP, so that the subsequent S_FMT delivers the
986 * final encoder configuration.
987 */
988 v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, video,
989 s_crop, &sd_crop);
ebf984bb
HV
990 format.format.width = geo.output.width;
991 format.format.height = geo.output.height;
992 ret = v4l2_device_call_until_err(&vou_dev->v4l2_dev, 0, pad,
993 set_fmt, NULL, &format);
a81fb9b2
GL
994 /* Must be implemented, so, don't check for -ENOIOCTLCMD */
995 if (ret < 0)
996 return ret;
997
998 /* Sanity checks */
ebf984bb
HV
999 if ((unsigned)format.format.width > VOU_MAX_IMAGE_WIDTH ||
1000 (unsigned)format.format.height > img_height_max ||
1001 format.format.code != MEDIA_BUS_FMT_YUYV8_2X8)
a81fb9b2
GL
1002 return -EIO;
1003
ebf984bb
HV
1004 geo.output.width = format.format.width;
1005 geo.output.height = format.format.height;
a81fb9b2
GL
1006
1007 /*
1008 * No down-scaling. According to the API, current call has precedence:
1009 * http://v4l2spec.bytesex.org/spec/x1904.htm#AEN1954 paragraph two.
1010 */
1011 vou_adjust_input(&geo, vou_dev->std);
1012
1013 /* We tried to preserve output rectangle, but it could have changed */
1014 vou_dev->rect = geo.output;
1015 pix->width = geo.in_width;
1016 pix->height = geo.in_height;
1017
1018 sh_vou_configure_geometry(vou_dev, vou_dev->pix_idx,
1019 geo.scale_idx_h, geo.scale_idx_v);
1020
1021 return 0;
1022}
1023
1024/*
1025 * Total field: NTSC 858 x 2 * 262/263, PAL 864 x 2 * 312/313, default rectangle
1026 * is the initial register values, height takes the interlaced format into
1027 * account. The actual image can only go up to 720 x 2 * 240, So, VOUVPR can
1028 * actually only meaningfully contain values <= 720 and <= 240 respectively, and
1029 * not <= 864 and <= 312.
1030 */
1031static int sh_vou_cropcap(struct file *file, void *priv,
1032 struct v4l2_cropcap *a)
1033{
d899eddd 1034 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 1035
d899eddd 1036 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
1037
1038 a->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1039 a->bounds.left = 0;
1040 a->bounds.top = 0;
1041 a->bounds.width = VOU_MAX_IMAGE_WIDTH;
1042 a->bounds.height = VOU_MAX_IMAGE_HEIGHT;
1043 /* Default = max, set VOUDPR = 0, which is not hardware default */
1044 a->defrect.left = 0;
1045 a->defrect.top = 0;
1046 a->defrect.width = VOU_MAX_IMAGE_WIDTH;
1047 a->defrect.height = VOU_MAX_IMAGE_HEIGHT;
1048 a->pixelaspect.numerator = 1;
1049 a->pixelaspect.denominator = 1;
1050
1051 return 0;
1052}
1053
1054static irqreturn_t sh_vou_isr(int irq, void *dev_id)
1055{
1056 struct sh_vou_device *vou_dev = dev_id;
1057 static unsigned long j;
1058 struct videobuf_buffer *vb;
1059 static int cnt;
a81fb9b2
GL
1060 u32 irq_status = sh_vou_reg_a_read(vou_dev, VOUIR), masked;
1061 u32 vou_status = sh_vou_reg_a_read(vou_dev, VOUSTR);
1062
1063 if (!(irq_status & 0x300)) {
1064 if (printk_timed_ratelimit(&j, 500))
1065 dev_warn(vou_dev->v4l2_dev.dev, "IRQ status 0x%x!\n",
1066 irq_status);
1067 return IRQ_NONE;
1068 }
1069
1070 spin_lock(&vou_dev->lock);
1071 if (!vou_dev->active || list_empty(&vou_dev->queue)) {
1072 if (printk_timed_ratelimit(&j, 500))
1073 dev_warn(vou_dev->v4l2_dev.dev,
1074 "IRQ without active buffer: %x!\n", irq_status);
1075 /* Just ack: buf_release will disable further interrupts */
1076 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x300);
1077 spin_unlock(&vou_dev->lock);
1078 return IRQ_HANDLED;
1079 }
1080
1081 masked = ~(0x300 & irq_status) & irq_status & 0x30304;
1082 dev_dbg(vou_dev->v4l2_dev.dev,
1083 "IRQ status 0x%x -> 0x%x, VOU status 0x%x, cnt %d\n",
1084 irq_status, masked, vou_status, cnt);
1085
1086 cnt++;
a622cc51 1087 /* side = vou_status & 0x10000; */
a81fb9b2
GL
1088
1089 /* Clear only set interrupts */
1090 sh_vou_reg_a_write(vou_dev, VOUIR, masked);
1091
1092 vb = vou_dev->active;
1093 list_del(&vb->queue);
1094
1095 vb->state = VIDEOBUF_DONE;
8e6057b5 1096 v4l2_get_timestamp(&vb->ts);
a81fb9b2
GL
1097 vb->field_count++;
1098 wake_up(&vb->done);
1099
1100 if (list_empty(&vou_dev->queue)) {
1101 /* Stop VOU */
1102 dev_dbg(vou_dev->v4l2_dev.dev, "%s: queue empty after %d\n",
1103 __func__, cnt);
1104 sh_vou_reg_a_set(vou_dev, VOUER, 0, 1);
1105 vou_dev->active = NULL;
1106 vou_dev->status = SH_VOU_INITIALISING;
1107 /* Disable End-of-Frame (VSYNC) interrupts */
1108 sh_vou_reg_a_set(vou_dev, VOUIR, 0, 0x30000);
1109 spin_unlock(&vou_dev->lock);
1110 return IRQ_HANDLED;
1111 }
1112
1113 vou_dev->active = list_entry(vou_dev->queue.next,
1114 struct videobuf_buffer, queue);
1115
1116 if (vou_dev->active->queue.next != &vou_dev->queue) {
1117 struct videobuf_buffer *new = list_entry(vou_dev->active->queue.next,
1118 struct videobuf_buffer, queue);
1119 sh_vou_schedule_next(vou_dev, new);
1120 }
1121
1122 spin_unlock(&vou_dev->lock);
1123
1124 return IRQ_HANDLED;
1125}
1126
1127static int sh_vou_hw_init(struct sh_vou_device *vou_dev)
1128{
1129 struct sh_vou_pdata *pdata = vou_dev->pdata;
1130 u32 voucr = sh_vou_ntsc_mode(pdata->bus_fmt) << 29;
1131 int i = 100;
1132
1133 /* Disable all IRQs */
1134 sh_vou_reg_a_write(vou_dev, VOUIR, 0);
1135
1136 /* Reset VOU interfaces - registers unaffected */
1137 sh_vou_reg_a_write(vou_dev, VOUSRR, 0x101);
1138 while (--i && (sh_vou_reg_a_read(vou_dev, VOUSRR) & 0x101))
1139 udelay(1);
1140
1141 if (!i)
1142 return -ETIMEDOUT;
1143
1144 dev_dbg(vou_dev->v4l2_dev.dev, "Reset took %dus\n", 100 - i);
1145
1146 if (pdata->flags & SH_VOU_PCLK_FALLING)
1147 voucr |= 1 << 28;
1148 if (pdata->flags & SH_VOU_HSYNC_LOW)
1149 voucr |= 1 << 27;
1150 if (pdata->flags & SH_VOU_VSYNC_LOW)
1151 voucr |= 1 << 26;
1152 sh_vou_reg_ab_set(vou_dev, VOUCR, voucr, 0xfc000000);
1153
1154 /* Manual register side switching at first */
1155 sh_vou_reg_a_write(vou_dev, VOURCR, 4);
1156 /* Default - fixed HSYNC length, can be made configurable is required */
1157 sh_vou_reg_ab_write(vou_dev, VOUMSR, 0x800000);
1158
1159 return 0;
1160}
1161
1162/* File operations */
1163static int sh_vou_open(struct file *file)
1164{
fd51625d 1165 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
1166 struct sh_vou_file *vou_file = kzalloc(sizeof(struct sh_vou_file),
1167 GFP_KERNEL);
1168
1169 if (!vou_file)
1170 return -ENOMEM;
1171
1172 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
1173
2160abb2
AK
1174 if (mutex_lock_interruptible(&vou_dev->fop_lock)) {
1175 kfree(vou_file);
f135a8a2 1176 return -ERESTARTSYS;
2160abb2 1177 }
a81fb9b2
GL
1178 if (atomic_inc_return(&vou_dev->use_count) == 1) {
1179 int ret;
1180 /* First open */
1181 vou_dev->status = SH_VOU_INITIALISING;
fd51625d 1182 pm_runtime_get_sync(vou_dev->v4l2_dev.dev);
a81fb9b2
GL
1183 ret = sh_vou_hw_init(vou_dev);
1184 if (ret < 0) {
1185 atomic_dec(&vou_dev->use_count);
fd51625d 1186 pm_runtime_put(vou_dev->v4l2_dev.dev);
a81fb9b2 1187 vou_dev->status = SH_VOU_IDLE;
f135a8a2 1188 mutex_unlock(&vou_dev->fop_lock);
2160abb2 1189 kfree(vou_file);
a81fb9b2
GL
1190 return ret;
1191 }
1192 }
1193
1194 videobuf_queue_dma_contig_init(&vou_file->vbq, &sh_vou_video_qops,
1195 vou_dev->v4l2_dev.dev, &vou_dev->lock,
1196 V4L2_BUF_TYPE_VIDEO_OUTPUT,
1197 V4L2_FIELD_NONE,
fd51625d 1198 sizeof(struct videobuf_buffer),
d079f99f 1199 &vou_dev->vdev, &vou_dev->fop_lock);
f135a8a2 1200 mutex_unlock(&vou_dev->fop_lock);
a81fb9b2 1201
2160abb2
AK
1202 file->private_data = vou_file;
1203
a81fb9b2
GL
1204 return 0;
1205}
1206
1207static int sh_vou_release(struct file *file)
1208{
fd51625d 1209 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2
GL
1210 struct sh_vou_file *vou_file = file->private_data;
1211
d899eddd 1212 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2
GL
1213
1214 if (!atomic_dec_return(&vou_dev->use_count)) {
f135a8a2 1215 mutex_lock(&vou_dev->fop_lock);
a81fb9b2
GL
1216 /* Last close */
1217 vou_dev->status = SH_VOU_IDLE;
1218 sh_vou_reg_a_set(vou_dev, VOUER, 0, 0x101);
fd51625d 1219 pm_runtime_put(vou_dev->v4l2_dev.dev);
f135a8a2 1220 mutex_unlock(&vou_dev->fop_lock);
a81fb9b2
GL
1221 }
1222
1223 file->private_data = NULL;
1224 kfree(vou_file);
1225
1226 return 0;
1227}
1228
1229static int sh_vou_mmap(struct file *file, struct vm_area_struct *vma)
1230{
fd51625d 1231 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 1232 struct sh_vou_file *vou_file = file->private_data;
f135a8a2 1233 int ret;
a81fb9b2 1234
d899eddd 1235 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2 1236
f135a8a2
HV
1237 if (mutex_lock_interruptible(&vou_dev->fop_lock))
1238 return -ERESTARTSYS;
1239 ret = videobuf_mmap_mapper(&vou_file->vbq, vma);
1240 mutex_unlock(&vou_dev->fop_lock);
1241 return ret;
a81fb9b2
GL
1242}
1243
1244static unsigned int sh_vou_poll(struct file *file, poll_table *wait)
1245{
fd51625d 1246 struct sh_vou_device *vou_dev = video_drvdata(file);
a81fb9b2 1247 struct sh_vou_file *vou_file = file->private_data;
f135a8a2 1248 unsigned int res;
a81fb9b2 1249
d899eddd 1250 dev_dbg(vou_dev->v4l2_dev.dev, "%s()\n", __func__);
a81fb9b2 1251
f135a8a2
HV
1252 mutex_lock(&vou_dev->fop_lock);
1253 res = videobuf_poll_stream(file, &vou_file->vbq, wait);
1254 mutex_unlock(&vou_dev->fop_lock);
1255 return res;
a81fb9b2
GL
1256}
1257
a81fb9b2
GL
1258/* sh_vou display ioctl operations */
1259static const struct v4l2_ioctl_ops sh_vou_ioctl_ops = {
1260 .vidioc_querycap = sh_vou_querycap,
1261 .vidioc_enum_fmt_vid_out = sh_vou_enum_fmt_vid_out,
1262 .vidioc_g_fmt_vid_out = sh_vou_g_fmt_vid_out,
1263 .vidioc_s_fmt_vid_out = sh_vou_s_fmt_vid_out,
1264 .vidioc_try_fmt_vid_out = sh_vou_try_fmt_vid_out,
1265 .vidioc_reqbufs = sh_vou_reqbufs,
1266 .vidioc_querybuf = sh_vou_querybuf,
1267 .vidioc_qbuf = sh_vou_qbuf,
1268 .vidioc_dqbuf = sh_vou_dqbuf,
1269 .vidioc_streamon = sh_vou_streamon,
1270 .vidioc_streamoff = sh_vou_streamoff,
1271 .vidioc_s_std = sh_vou_s_std,
1272 .vidioc_g_std = sh_vou_g_std,
1273 .vidioc_cropcap = sh_vou_cropcap,
1274 .vidioc_g_crop = sh_vou_g_crop,
1275 .vidioc_s_crop = sh_vou_s_crop,
a81fb9b2
GL
1276};
1277
1278static const struct v4l2_file_operations sh_vou_fops = {
1279 .owner = THIS_MODULE,
1280 .open = sh_vou_open,
1281 .release = sh_vou_release,
69756693 1282 .unlocked_ioctl = video_ioctl2,
a81fb9b2
GL
1283 .mmap = sh_vou_mmap,
1284 .poll = sh_vou_poll,
1285};
1286
1287static const struct video_device sh_vou_video_template = {
1288 .name = "sh_vou",
1289 .fops = &sh_vou_fops,
1290 .ioctl_ops = &sh_vou_ioctl_ops,
1291 .tvnorms = V4L2_STD_525_60, /* PAL only supported in 8-bit non-bt656 mode */
954f340f 1292 .vfl_dir = VFL_DIR_TX,
a81fb9b2
GL
1293};
1294
4c62e976 1295static int sh_vou_probe(struct platform_device *pdev)
a81fb9b2
GL
1296{
1297 struct sh_vou_pdata *vou_pdata = pdev->dev.platform_data;
1298 struct v4l2_rect *rect;
1299 struct v4l2_pix_format *pix;
1300 struct i2c_adapter *i2c_adap;
1301 struct video_device *vdev;
1302 struct sh_vou_device *vou_dev;
1303 struct resource *reg_res, *region;
1304 struct v4l2_subdev *subdev;
1305 int irq, ret;
1306
1307 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1308 irq = platform_get_irq(pdev, 0);
1309
1310 if (!vou_pdata || !reg_res || irq <= 0) {
1311 dev_err(&pdev->dev, "Insufficient VOU platform information.\n");
1312 return -ENODEV;
1313 }
1314
1315 vou_dev = kzalloc(sizeof(*vou_dev), GFP_KERNEL);
1316 if (!vou_dev)
1317 return -ENOMEM;
1318
1319 INIT_LIST_HEAD(&vou_dev->queue);
1320 spin_lock_init(&vou_dev->lock);
69756693 1321 mutex_init(&vou_dev->fop_lock);
a81fb9b2
GL
1322 atomic_set(&vou_dev->use_count, 0);
1323 vou_dev->pdata = vou_pdata;
1324 vou_dev->status = SH_VOU_IDLE;
1325
1326 rect = &vou_dev->rect;
1327 pix = &vou_dev->pix;
1328
1329 /* Fill in defaults */
5d408108 1330 vou_dev->std = V4L2_STD_NTSC_M;
a81fb9b2
GL
1331 rect->left = 0;
1332 rect->top = 0;
1333 rect->width = VOU_MAX_IMAGE_WIDTH;
765fe17c 1334 rect->height = 480;
a81fb9b2 1335 pix->width = VOU_MAX_IMAGE_WIDTH;
765fe17c 1336 pix->height = 480;
a81fb9b2
GL
1337 pix->pixelformat = V4L2_PIX_FMT_YVYU;
1338 pix->field = V4L2_FIELD_NONE;
1339 pix->bytesperline = VOU_MAX_IMAGE_WIDTH * 2;
765fe17c 1340 pix->sizeimage = VOU_MAX_IMAGE_WIDTH * 2 * 480;
a81fb9b2
GL
1341 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
1342
1343 region = request_mem_region(reg_res->start, resource_size(reg_res),
1344 pdev->name);
1345 if (!region) {
1346 dev_err(&pdev->dev, "VOU region already claimed\n");
1347 ret = -EBUSY;
1348 goto ereqmemreg;
1349 }
1350
1351 vou_dev->base = ioremap(reg_res->start, resource_size(reg_res));
1352 if (!vou_dev->base) {
1353 ret = -ENOMEM;
1354 goto emap;
1355 }
1356
1357 ret = request_irq(irq, sh_vou_isr, 0, "vou", vou_dev);
1358 if (ret < 0)
1359 goto ereqirq;
1360
1361 ret = v4l2_device_register(&pdev->dev, &vou_dev->v4l2_dev);
1362 if (ret < 0) {
1363 dev_err(&pdev->dev, "Error registering v4l2 device\n");
1364 goto ev4l2devreg;
1365 }
1366
d079f99f 1367 vdev = &vou_dev->vdev;
a81fb9b2
GL
1368 *vdev = sh_vou_video_template;
1369 if (vou_pdata->bus_fmt == SH_VOU_BUS_8BIT)
1370 vdev->tvnorms |= V4L2_STD_PAL;
1371 vdev->v4l2_dev = &vou_dev->v4l2_dev;
d079f99f 1372 vdev->release = video_device_release_empty;
69756693 1373 vdev->lock = &vou_dev->fop_lock;
a81fb9b2 1374
a81fb9b2
GL
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,
9a1f8b34 1391 vou_pdata->board_info, NULL);
a81fb9b2
GL
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
1403evregdev:
1404ei2cnd:
1405ereset:
1406 i2c_put_adapter(i2c_adap);
1407ei2cgadap:
a81fb9b2 1408 pm_runtime_disable(&pdev->dev);
a81fb9b2
GL
1409 v4l2_device_unregister(&vou_dev->v4l2_dev);
1410ev4l2devreg:
1411 free_irq(irq, vou_dev);
1412ereqirq:
1413 iounmap(vou_dev->base);
1414emap:
1415 release_mem_region(reg_res->start, resource_size(reg_res));
1416ereqmemreg:
1417 kfree(vou_dev);
1418 return ret;
1419}
1420
4c62e976 1421static int sh_vou_remove(struct platform_device *pdev)
a81fb9b2
GL
1422{
1423 int irq = platform_get_irq(pdev, 0);
1424 struct v4l2_device *v4l2_dev = platform_get_drvdata(pdev);
1425 struct sh_vou_device *vou_dev = container_of(v4l2_dev,
1426 struct sh_vou_device, v4l2_dev);
1427 struct v4l2_subdev *sd = list_entry(v4l2_dev->subdevs.next,
1428 struct v4l2_subdev, list);
1429 struct i2c_client *client = v4l2_get_subdevdata(sd);
1430 struct resource *reg_res;
1431
1432 if (irq > 0)
1433 free_irq(irq, vou_dev);
1434 pm_runtime_disable(&pdev->dev);
d079f99f 1435 video_unregister_device(&vou_dev->vdev);
a81fb9b2
GL
1436 i2c_put_adapter(client->adapter);
1437 v4l2_device_unregister(&vou_dev->v4l2_dev);
1438 iounmap(vou_dev->base);
1439 reg_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1440 if (reg_res)
1441 release_mem_region(reg_res->start, resource_size(reg_res));
1442 kfree(vou_dev);
1443 return 0;
1444}
1445
1446static struct platform_driver __refdata sh_vou = {
4c62e976 1447 .remove = sh_vou_remove,
a81fb9b2
GL
1448 .driver = {
1449 .name = "sh-vou",
a81fb9b2
GL
1450 },
1451};
1452
b3fd87bc 1453module_platform_driver_probe(sh_vou, sh_vou_probe);
a81fb9b2
GL
1454
1455MODULE_DESCRIPTION("SuperH VOU driver");
1456MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1457MODULE_LICENSE("GPL v2");
64dc3c1a 1458MODULE_VERSION("0.1.0");
a81fb9b2 1459MODULE_ALIAS("platform:sh-vou");
This page took 0.403077 seconds and 5 git commands to generate.