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