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