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