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