]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/fsl-viu.c
x86: Resume trampoline must be executable
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / fsl-viu.c
1 /*
2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Freescale VIU video driver
5 *
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Porting to 2.6.35 by DENX Software Engineering,
8 * Anatolij Gustschin <agust@denx.de>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/clk.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/of_platform.h>
25 #include <linux/slab.h>
26 #include <linux/version.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf-dma-contig.h>
31
32 #define DRV_NAME "fsl_viu"
33 #define VIU_MAJOR_VERSION 0
34 #define VIU_MINOR_VERSION 5
35 #define VIU_RELEASE 0
36 #define VIU_VERSION KERNEL_VERSION(VIU_MAJOR_VERSION, \
37 VIU_MINOR_VERSION, \
38 VIU_RELEASE)
39
40 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
41
42 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
43
44 /* I2C address of video decoder chip is 0x4A */
45 #define VIU_VIDEO_DECODER_ADDR 0x25
46
47 /* supported controls */
48 static struct v4l2_queryctrl viu_qctrl[] = {
49 {
50 .id = V4L2_CID_BRIGHTNESS,
51 .type = V4L2_CTRL_TYPE_INTEGER,
52 .name = "Brightness",
53 .minimum = 0,
54 .maximum = 255,
55 .step = 1,
56 .default_value = 127,
57 .flags = 0,
58 }, {
59 .id = V4L2_CID_CONTRAST,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Contrast",
62 .minimum = 0,
63 .maximum = 255,
64 .step = 0x1,
65 .default_value = 0x10,
66 .flags = 0,
67 }, {
68 .id = V4L2_CID_SATURATION,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Saturation",
71 .minimum = 0,
72 .maximum = 255,
73 .step = 0x1,
74 .default_value = 127,
75 .flags = 0,
76 }, {
77 .id = V4L2_CID_HUE,
78 .type = V4L2_CTRL_TYPE_INTEGER,
79 .name = "Hue",
80 .minimum = -128,
81 .maximum = 127,
82 .step = 0x1,
83 .default_value = 0,
84 .flags = 0,
85 }
86 };
87
88 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
89
90 static int info_level;
91
92 #define dprintk(level, fmt, arg...) \
93 do { \
94 if (level <= info_level) \
95 printk(KERN_DEBUG "viu: " fmt , ## arg); \
96 } while (0)
97
98 /*
99 * Basic structures
100 */
101 struct viu_fmt {
102 char name[32];
103 u32 fourcc; /* v4l2 format id */
104 u32 pixelformat;
105 int depth;
106 };
107
108 static struct viu_fmt formats[] = {
109 {
110 .name = "RGB-16 (5/B-6/G-5/R)",
111 .fourcc = V4L2_PIX_FMT_RGB565,
112 .pixelformat = V4L2_PIX_FMT_RGB565,
113 .depth = 16,
114 }, {
115 .name = "RGB-32 (A-R-G-B)",
116 .fourcc = V4L2_PIX_FMT_RGB32,
117 .pixelformat = V4L2_PIX_FMT_RGB32,
118 .depth = 32,
119 }
120 };
121
122 struct viu_dev;
123 struct viu_buf;
124
125 /* buffer for one video frame */
126 struct viu_buf {
127 /* common v4l buffer stuff -- must be first */
128 struct videobuf_buffer vb;
129 struct viu_fmt *fmt;
130 };
131
132 struct viu_dmaqueue {
133 struct viu_dev *dev;
134 struct list_head active;
135 struct list_head queued;
136 struct timer_list timeout;
137 };
138
139 struct viu_status {
140 u32 field_irq;
141 u32 vsync_irq;
142 u32 hsync_irq;
143 u32 vstart_irq;
144 u32 dma_end_irq;
145 u32 error_irq;
146 };
147
148 struct viu_reg {
149 u32 status_cfg;
150 u32 luminance;
151 u32 chroma_r;
152 u32 chroma_g;
153 u32 chroma_b;
154 u32 field_base_addr;
155 u32 dma_inc;
156 u32 picture_count;
157 u32 req_alarm;
158 u32 alpha;
159 } __attribute__ ((packed));
160
161 struct viu_dev {
162 struct v4l2_device v4l2_dev;
163 struct mutex lock;
164 spinlock_t slock;
165 int users;
166
167 struct device *dev;
168 /* various device info */
169 struct video_device *vdev;
170 struct viu_dmaqueue vidq;
171 enum v4l2_field capfield;
172 int field;
173 int first;
174 int dma_done;
175
176 /* Hardware register area */
177 struct viu_reg *vr;
178
179 /* Interrupt vector */
180 int irq;
181 struct viu_status irqs;
182
183 /* video overlay */
184 struct v4l2_framebuffer ovbuf;
185 struct viu_fmt *ovfmt;
186 unsigned int ovenable;
187 enum v4l2_field ovfield;
188
189 /* crop */
190 struct v4l2_rect crop_current;
191
192 /* clock pointer */
193 struct clk *clk;
194
195 /* decoder */
196 struct v4l2_subdev *decoder;
197 };
198
199 struct viu_fh {
200 struct viu_dev *dev;
201
202 /* video capture */
203 struct videobuf_queue vb_vidq;
204 spinlock_t vbq_lock; /* spinlock for the videobuf queue */
205
206 /* video overlay */
207 struct v4l2_window win;
208 struct v4l2_clip clips[1];
209
210 /* video capture */
211 struct viu_fmt *fmt;
212 int width, height, sizeimage;
213 enum v4l2_buf_type type;
214 };
215
216 static struct viu_reg reg_val;
217
218 /*
219 * Macro definitions of VIU registers
220 */
221
222 /* STATUS_CONFIG register */
223 enum status_config {
224 SOFT_RST = 1 << 0,
225
226 ERR_MASK = 0x0f << 4, /* Error code mask */
227 ERR_NO = 0x00, /* No error */
228 ERR_DMA_V = 0x01 << 4, /* DMA in vertical active */
229 ERR_DMA_VB = 0x02 << 4, /* DMA in vertical blanking */
230 ERR_LINE_TOO_LONG = 0x04 << 4, /* Line too long */
231 ERR_TOO_MANG_LINES = 0x05 << 4, /* Too many lines in field */
232 ERR_LINE_TOO_SHORT = 0x06 << 4, /* Line too short */
233 ERR_NOT_ENOUGH_LINE = 0x07 << 4, /* Not enough lines in field */
234 ERR_FIFO_OVERFLOW = 0x08 << 4, /* FIFO overflow */
235 ERR_FIFO_UNDERFLOW = 0x09 << 4, /* FIFO underflow */
236 ERR_1bit_ECC = 0x0a << 4, /* One bit ECC error */
237 ERR_MORE_ECC = 0x0b << 4, /* Two/more bits ECC error */
238
239 INT_FIELD_EN = 0x01 << 8, /* Enable field interrupt */
240 INT_VSYNC_EN = 0x01 << 9, /* Enable vsync interrupt */
241 INT_HSYNC_EN = 0x01 << 10, /* Enable hsync interrupt */
242 INT_VSTART_EN = 0x01 << 11, /* Enable vstart interrupt */
243 INT_DMA_END_EN = 0x01 << 12, /* Enable DMA end interrupt */
244 INT_ERROR_EN = 0x01 << 13, /* Enable error interrupt */
245 INT_ECC_EN = 0x01 << 14, /* Enable ECC interrupt */
246
247 INT_FIELD_STATUS = 0x01 << 16, /* field interrupt status */
248 INT_VSYNC_STATUS = 0x01 << 17, /* vsync interrupt status */
249 INT_HSYNC_STATUS = 0x01 << 18, /* hsync interrupt status */
250 INT_VSTART_STATUS = 0x01 << 19, /* vstart interrupt status */
251 INT_DMA_END_STATUS = 0x01 << 20, /* DMA end interrupt status */
252 INT_ERROR_STATUS = 0x01 << 21, /* error interrupt status */
253
254 DMA_ACT = 0x01 << 27, /* Enable DMA transfer */
255 FIELD_NO = 0x01 << 28, /* Field number */
256 DITHER_ON = 0x01 << 29, /* Dithering is on */
257 ROUND_ON = 0x01 << 30, /* Round is on */
258 MODE_32BIT = 0x01 << 31, /* Data in RGBa888,
259 * 0 in RGB565
260 */
261 };
262
263 #define norm_maxw() 720
264 #define norm_maxh() 576
265
266 #define INT_ALL_STATUS (INT_FIELD_STATUS | INT_VSYNC_STATUS | \
267 INT_HSYNC_STATUS | INT_VSTART_STATUS | \
268 INT_DMA_END_STATUS | INT_ERROR_STATUS)
269
270 #define NUM_FORMATS ARRAY_SIZE(formats)
271
272 static irqreturn_t viu_intr(int irq, void *dev_id);
273
274 struct viu_fmt *format_by_fourcc(int fourcc)
275 {
276 int i;
277
278 for (i = 0; i < NUM_FORMATS; i++) {
279 if (formats[i].pixelformat == fourcc)
280 return formats + i;
281 }
282
283 dprintk(0, "unknown pixelformat:'%4.4s'\n", (char *)&fourcc);
284 return NULL;
285 }
286
287 void viu_start_dma(struct viu_dev *dev)
288 {
289 struct viu_reg *vr = dev->vr;
290
291 dev->field = 0;
292
293 /* Enable DMA operation */
294 out_be32(&vr->status_cfg, SOFT_RST);
295 out_be32(&vr->status_cfg, INT_FIELD_EN);
296 }
297
298 void viu_stop_dma(struct viu_dev *dev)
299 {
300 struct viu_reg *vr = dev->vr;
301 int cnt = 100;
302 u32 status_cfg;
303
304 out_be32(&vr->status_cfg, 0);
305
306 /* Clear pending interrupts */
307 status_cfg = in_be32(&vr->status_cfg);
308 if (status_cfg & 0x3f0000)
309 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
310
311 if (status_cfg & DMA_ACT) {
312 do {
313 status_cfg = in_be32(&vr->status_cfg);
314 if (status_cfg & INT_DMA_END_STATUS)
315 break;
316 } while (cnt--);
317
318 if (cnt < 0) {
319 /* timed out, issue soft reset */
320 out_be32(&vr->status_cfg, SOFT_RST);
321 out_be32(&vr->status_cfg, 0);
322 } else {
323 /* clear DMA_END and other pending irqs */
324 out_be32(&vr->status_cfg, status_cfg & 0x3f0000);
325 }
326 }
327
328 dev->field = 0;
329 }
330
331 static int restart_video_queue(struct viu_dmaqueue *vidq)
332 {
333 struct viu_buf *buf, *prev;
334
335 dprintk(1, "%s vidq=0x%08lx\n", __func__, (unsigned long)vidq);
336 if (!list_empty(&vidq->active)) {
337 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
338 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
339 buf, buf->vb.i);
340
341 viu_stop_dma(vidq->dev);
342
343 /* cancel all outstanding capture requests */
344 list_for_each_entry_safe(buf, prev, &vidq->active, vb.queue) {
345 list_del(&buf->vb.queue);
346 buf->vb.state = VIDEOBUF_ERROR;
347 wake_up(&buf->vb.done);
348 }
349 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
350 return 0;
351 }
352
353 prev = NULL;
354 for (;;) {
355 if (list_empty(&vidq->queued))
356 return 0;
357 buf = list_entry(vidq->queued.next, struct viu_buf, vb.queue);
358 if (prev == NULL) {
359 list_del(&buf->vb.queue);
360 list_add_tail(&buf->vb.queue, &vidq->active);
361
362 dprintk(1, "Restarting video dma\n");
363 viu_stop_dma(vidq->dev);
364 viu_start_dma(vidq->dev);
365
366 buf->vb.state = VIDEOBUF_ACTIVE;
367 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
368 dprintk(2, "[%p/%d] restart_queue - first active\n",
369 buf, buf->vb.i);
370
371 } else if (prev->vb.width == buf->vb.width &&
372 prev->vb.height == buf->vb.height &&
373 prev->fmt == buf->fmt) {
374 list_del(&buf->vb.queue);
375 list_add_tail(&buf->vb.queue, &vidq->active);
376 buf->vb.state = VIDEOBUF_ACTIVE;
377 dprintk(2, "[%p/%d] restart_queue - move to active\n",
378 buf, buf->vb.i);
379 } else {
380 return 0;
381 }
382 prev = buf;
383 }
384 }
385
386 static void viu_vid_timeout(unsigned long data)
387 {
388 struct viu_dev *dev = (struct viu_dev *)data;
389 struct viu_buf *buf;
390 struct viu_dmaqueue *vidq = &dev->vidq;
391
392 while (!list_empty(&vidq->active)) {
393 buf = list_entry(vidq->active.next, struct viu_buf, vb.queue);
394 list_del(&buf->vb.queue);
395 buf->vb.state = VIDEOBUF_ERROR;
396 wake_up(&buf->vb.done);
397 dprintk(1, "viu/0: [%p/%d] timeout\n", buf, buf->vb.i);
398 }
399
400 restart_video_queue(vidq);
401 }
402
403 /*
404 * Videobuf operations
405 */
406 static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
407 unsigned int *size)
408 {
409 struct viu_fh *fh = vq->priv_data;
410
411 *size = fh->width * fh->height * fh->fmt->depth >> 3;
412 if (*count == 0)
413 *count = 32;
414
415 while (*size * *count > VIU_VID_MEM_LIMIT * 1024 * 1024)
416 (*count)--;
417
418 dprintk(1, "%s, count=%d, size=%d\n", __func__, *count, *size);
419 return 0;
420 }
421
422 static void free_buffer(struct videobuf_queue *vq, struct viu_buf *buf)
423 {
424 struct videobuf_buffer *vb = &buf->vb;
425 void *vaddr = NULL;
426
427 BUG_ON(in_interrupt());
428
429 videobuf_waiton(vq, &buf->vb, 0, 0);
430
431 if (vq->int_ops && vq->int_ops->vaddr)
432 vaddr = vq->int_ops->vaddr(vb);
433
434 if (vaddr)
435 videobuf_dma_contig_free(vq, &buf->vb);
436
437 buf->vb.state = VIDEOBUF_NEEDS_INIT;
438 }
439
440 inline int buffer_activate(struct viu_dev *dev, struct viu_buf *buf)
441 {
442 struct viu_reg *vr = dev->vr;
443 int bpp;
444
445 /* setup the DMA base address */
446 reg_val.field_base_addr = videobuf_to_dma_contig(&buf->vb);
447
448 dprintk(1, "buffer_activate [%p/%d]: dma addr 0x%lx\n",
449 buf, buf->vb.i, (unsigned long)reg_val.field_base_addr);
450
451 /* interlace is on by default, set horizontal DMA increment */
452 reg_val.status_cfg = 0;
453 bpp = buf->fmt->depth >> 3;
454 switch (bpp) {
455 case 2:
456 reg_val.status_cfg &= ~MODE_32BIT;
457 reg_val.dma_inc = buf->vb.width * 2;
458 break;
459 case 4:
460 reg_val.status_cfg |= MODE_32BIT;
461 reg_val.dma_inc = buf->vb.width * 4;
462 break;
463 default:
464 dprintk(0, "doesn't support color depth(%d)\n",
465 bpp * 8);
466 return -EINVAL;
467 }
468
469 /* setup picture_count register */
470 reg_val.picture_count = (buf->vb.height / 2) << 16 |
471 buf->vb.width;
472
473 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
474
475 buf->vb.state = VIDEOBUF_ACTIVE;
476 dev->capfield = buf->vb.field;
477
478 /* reset dma increment if needed */
479 if (!V4L2_FIELD_HAS_BOTH(buf->vb.field))
480 reg_val.dma_inc = 0;
481
482 out_be32(&vr->dma_inc, reg_val.dma_inc);
483 out_be32(&vr->picture_count, reg_val.picture_count);
484 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
485 mod_timer(&dev->vidq.timeout, jiffies + BUFFER_TIMEOUT);
486 return 0;
487 }
488
489 static int buffer_prepare(struct videobuf_queue *vq,
490 struct videobuf_buffer *vb,
491 enum v4l2_field field)
492 {
493 struct viu_fh *fh = vq->priv_data;
494 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
495 int rc;
496
497 BUG_ON(fh->fmt == NULL);
498
499 if (fh->width < 48 || fh->width > norm_maxw() ||
500 fh->height < 32 || fh->height > norm_maxh())
501 return -EINVAL;
502 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
503 if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size)
504 return -EINVAL;
505
506 if (buf->fmt != fh->fmt ||
507 buf->vb.width != fh->width ||
508 buf->vb.height != fh->height ||
509 buf->vb.field != field) {
510 buf->fmt = fh->fmt;
511 buf->vb.width = fh->width;
512 buf->vb.height = fh->height;
513 buf->vb.field = field;
514 }
515
516 if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
517 rc = videobuf_iolock(vq, &buf->vb, NULL);
518 if (rc != 0)
519 goto fail;
520
521 buf->vb.width = fh->width;
522 buf->vb.height = fh->height;
523 buf->vb.field = field;
524 buf->fmt = fh->fmt;
525 }
526
527 buf->vb.state = VIDEOBUF_PREPARED;
528 return 0;
529
530 fail:
531 free_buffer(vq, buf);
532 return rc;
533 }
534
535 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
536 {
537 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
538 struct viu_fh *fh = vq->priv_data;
539 struct viu_dev *dev = fh->dev;
540 struct viu_dmaqueue *vidq = &dev->vidq;
541 struct viu_buf *prev;
542
543 if (!list_empty(&vidq->queued)) {
544 dprintk(1, "adding vb queue=0x%08lx\n",
545 (unsigned long)&buf->vb.queue);
546 dprintk(1, "vidq pointer 0x%p, queued 0x%p\n",
547 vidq, &vidq->queued);
548 dprintk(1, "dev %p, queued: self %p, next %p, head %p\n",
549 dev, &vidq->queued, vidq->queued.next,
550 vidq->queued.prev);
551 list_add_tail(&buf->vb.queue, &vidq->queued);
552 buf->vb.state = VIDEOBUF_QUEUED;
553 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
554 buf, buf->vb.i);
555 } else if (list_empty(&vidq->active)) {
556 dprintk(1, "adding vb active=0x%08lx\n",
557 (unsigned long)&buf->vb.queue);
558 list_add_tail(&buf->vb.queue, &vidq->active);
559 buf->vb.state = VIDEOBUF_ACTIVE;
560 mod_timer(&vidq->timeout, jiffies+BUFFER_TIMEOUT);
561 dprintk(2, "[%p/%d] buffer_queue - first active\n",
562 buf, buf->vb.i);
563
564 buffer_activate(dev, buf);
565 } else {
566 dprintk(1, "adding vb queue2=0x%08lx\n",
567 (unsigned long)&buf->vb.queue);
568 prev = list_entry(vidq->active.prev, struct viu_buf, vb.queue);
569 if (prev->vb.width == buf->vb.width &&
570 prev->vb.height == buf->vb.height &&
571 prev->fmt == buf->fmt) {
572 list_add_tail(&buf->vb.queue, &vidq->active);
573 buf->vb.state = VIDEOBUF_ACTIVE;
574 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
575 buf, buf->vb.i);
576 } else {
577 list_add_tail(&buf->vb.queue, &vidq->queued);
578 buf->vb.state = VIDEOBUF_QUEUED;
579 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
580 buf, buf->vb.i);
581 }
582 }
583 }
584
585 static void buffer_release(struct videobuf_queue *vq,
586 struct videobuf_buffer *vb)
587 {
588 struct viu_buf *buf = container_of(vb, struct viu_buf, vb);
589 struct viu_fh *fh = vq->priv_data;
590 struct viu_dev *dev = (struct viu_dev *)fh->dev;
591
592 viu_stop_dma(dev);
593 free_buffer(vq, buf);
594 }
595
596 static struct videobuf_queue_ops viu_video_qops = {
597 .buf_setup = buffer_setup,
598 .buf_prepare = buffer_prepare,
599 .buf_queue = buffer_queue,
600 .buf_release = buffer_release,
601 };
602
603 /*
604 * IOCTL vidioc handling
605 */
606 static int vidioc_querycap(struct file *file, void *priv,
607 struct v4l2_capability *cap)
608 {
609 strcpy(cap->driver, "viu");
610 strcpy(cap->card, "viu");
611 cap->version = VIU_VERSION;
612 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
613 V4L2_CAP_STREAMING |
614 V4L2_CAP_VIDEO_OVERLAY |
615 V4L2_CAP_READWRITE;
616 return 0;
617 }
618
619 static int vidioc_enum_fmt(struct file *file, void *priv,
620 struct v4l2_fmtdesc *f)
621 {
622 int index = f->index;
623
624 if (f->index > NUM_FORMATS)
625 return -EINVAL;
626
627 strlcpy(f->description, formats[index].name, sizeof(f->description));
628 f->pixelformat = formats[index].fourcc;
629 return 0;
630 }
631
632 static int vidioc_g_fmt_cap(struct file *file, void *priv,
633 struct v4l2_format *f)
634 {
635 struct viu_fh *fh = priv;
636
637 f->fmt.pix.width = fh->width;
638 f->fmt.pix.height = fh->height;
639 f->fmt.pix.field = fh->vb_vidq.field;
640 f->fmt.pix.pixelformat = fh->fmt->pixelformat;
641 f->fmt.pix.bytesperline =
642 (f->fmt.pix.width * fh->fmt->depth) >> 3;
643 f->fmt.pix.sizeimage = fh->sizeimage;
644 return 0;
645 }
646
647 static int vidioc_try_fmt_cap(struct file *file, void *priv,
648 struct v4l2_format *f)
649 {
650 struct viu_fmt *fmt;
651 enum v4l2_field field;
652 unsigned int maxw, maxh;
653
654 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
655 if (!fmt) {
656 dprintk(1, "Fourcc format (0x%08x) invalid.",
657 f->fmt.pix.pixelformat);
658 return -EINVAL;
659 }
660
661 field = f->fmt.pix.field;
662
663 if (field == V4L2_FIELD_ANY) {
664 field = V4L2_FIELD_INTERLACED;
665 } else if (field != V4L2_FIELD_INTERLACED) {
666 dprintk(1, "Field type invalid.\n");
667 return -EINVAL;
668 }
669
670 maxw = norm_maxw();
671 maxh = norm_maxh();
672
673 f->fmt.pix.field = field;
674 if (f->fmt.pix.height < 32)
675 f->fmt.pix.height = 32;
676 if (f->fmt.pix.height > maxh)
677 f->fmt.pix.height = maxh;
678 if (f->fmt.pix.width < 48)
679 f->fmt.pix.width = 48;
680 if (f->fmt.pix.width > maxw)
681 f->fmt.pix.width = maxw;
682 f->fmt.pix.width &= ~0x03;
683 f->fmt.pix.bytesperline =
684 (f->fmt.pix.width * fmt->depth) >> 3;
685
686 return 0;
687 }
688
689 static int vidioc_s_fmt_cap(struct file *file, void *priv,
690 struct v4l2_format *f)
691 {
692 struct viu_fh *fh = priv;
693 int ret;
694
695 ret = vidioc_try_fmt_cap(file, fh, f);
696 if (ret < 0)
697 return ret;
698
699 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
700 fh->width = f->fmt.pix.width;
701 fh->height = f->fmt.pix.height;
702 fh->sizeimage = f->fmt.pix.sizeimage;
703 fh->vb_vidq.field = f->fmt.pix.field;
704 fh->type = f->type;
705 dprintk(1, "set to pixelformat '%4.6s'\n", (char *)&fh->fmt->name);
706 return 0;
707 }
708
709 static int vidioc_g_fmt_overlay(struct file *file, void *priv,
710 struct v4l2_format *f)
711 {
712 struct viu_fh *fh = priv;
713
714 f->fmt.win = fh->win;
715 return 0;
716 }
717
718 static int verify_preview(struct viu_dev *dev, struct v4l2_window *win)
719 {
720 enum v4l2_field field;
721 int maxw, maxh;
722
723 if (dev->ovbuf.base == NULL)
724 return -EINVAL;
725 if (dev->ovfmt == NULL)
726 return -EINVAL;
727 if (win->w.width < 48 || win->w.height < 32)
728 return -EINVAL;
729
730 field = win->field;
731 maxw = dev->crop_current.width;
732 maxh = dev->crop_current.height;
733
734 if (field == V4L2_FIELD_ANY) {
735 field = (win->w.height > maxh/2)
736 ? V4L2_FIELD_INTERLACED
737 : V4L2_FIELD_TOP;
738 }
739 switch (field) {
740 case V4L2_FIELD_TOP:
741 case V4L2_FIELD_BOTTOM:
742 maxh = maxh / 2;
743 break;
744 case V4L2_FIELD_INTERLACED:
745 break;
746 default:
747 return -EINVAL;
748 }
749
750 win->field = field;
751 if (win->w.width > maxw)
752 win->w.width = maxw;
753 if (win->w.height > maxh)
754 win->w.height = maxh;
755 return 0;
756 }
757
758 inline void viu_activate_overlay(struct viu_reg *viu_reg)
759 {
760 struct viu_reg *vr = viu_reg;
761
762 out_be32(&vr->field_base_addr, reg_val.field_base_addr);
763 out_be32(&vr->dma_inc, reg_val.dma_inc);
764 out_be32(&vr->picture_count, reg_val.picture_count);
765 }
766
767 static int viu_start_preview(struct viu_dev *dev, struct viu_fh *fh)
768 {
769 int bpp;
770
771 dprintk(1, "%s %dx%d %s\n", __func__,
772 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
773
774 reg_val.status_cfg = 0;
775
776 /* setup window */
777 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
778 fh->win.w.width;
779
780 /* setup color depth and dma increment */
781 bpp = dev->ovfmt->depth / 8;
782 switch (bpp) {
783 case 2:
784 reg_val.status_cfg &= ~MODE_32BIT;
785 reg_val.dma_inc = fh->win.w.width * 2;
786 break;
787 case 4:
788 reg_val.status_cfg |= MODE_32BIT;
789 reg_val.dma_inc = fh->win.w.width * 4;
790 break;
791 default:
792 dprintk(0, "device doesn't support color depth(%d)\n",
793 bpp * 8);
794 return -EINVAL;
795 }
796
797 dev->ovfield = fh->win.field;
798 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
799 reg_val.dma_inc = 0;
800
801 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
802
803 /* setup the base address of the overlay buffer */
804 reg_val.field_base_addr = (u32)dev->ovbuf.base;
805
806 dev->ovenable = 1;
807 viu_activate_overlay(dev->vr);
808
809 /* start dma */
810 viu_start_dma(dev);
811 return 0;
812 }
813
814 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
815 struct v4l2_format *f)
816 {
817 struct viu_fh *fh = priv;
818 struct viu_dev *dev = (struct viu_dev *)fh->dev;
819 unsigned long flags;
820 int err;
821
822 err = verify_preview(dev, &f->fmt.win);
823 if (err)
824 return err;
825
826 mutex_lock(&dev->lock);
827 fh->win = f->fmt.win;
828
829 spin_lock_irqsave(&dev->slock, flags);
830 viu_start_preview(dev, fh);
831 spin_unlock_irqrestore(&dev->slock, flags);
832 mutex_unlock(&dev->lock);
833 return 0;
834 }
835
836 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
837 struct v4l2_format *f)
838 {
839 return 0;
840 }
841
842 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
843 {
844 struct viu_fh *fh = priv;
845 struct viu_dev *dev = fh->dev;
846 struct v4l2_framebuffer *fb = arg;
847
848 *fb = dev->ovbuf;
849 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
850 return 0;
851 }
852
853 int vidioc_s_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
854 {
855 struct viu_fh *fh = priv;
856 struct viu_dev *dev = fh->dev;
857 struct v4l2_framebuffer *fb = arg;
858 struct viu_fmt *fmt;
859
860 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
861 return -EPERM;
862
863 /* check args */
864 fmt = format_by_fourcc(fb->fmt.pixelformat);
865 if (fmt == NULL)
866 return -EINVAL;
867
868 /* ok, accept it */
869 dev->ovbuf = *fb;
870 dev->ovfmt = fmt;
871 if (dev->ovbuf.fmt.bytesperline == 0) {
872 dev->ovbuf.fmt.bytesperline =
873 dev->ovbuf.fmt.width * fmt->depth / 8;
874 }
875 return 0;
876 }
877
878 static int vidioc_reqbufs(struct file *file, void *priv,
879 struct v4l2_requestbuffers *p)
880 {
881 struct viu_fh *fh = priv;
882
883 return videobuf_reqbufs(&fh->vb_vidq, p);
884 }
885
886 static int vidioc_querybuf(struct file *file, void *priv,
887 struct v4l2_buffer *p)
888 {
889 struct viu_fh *fh = priv;
890
891 return videobuf_querybuf(&fh->vb_vidq, p);
892 }
893
894 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
895 {
896 struct viu_fh *fh = priv;
897
898 return videobuf_qbuf(&fh->vb_vidq, p);
899 }
900
901 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
902 {
903 struct viu_fh *fh = priv;
904
905 return videobuf_dqbuf(&fh->vb_vidq, p,
906 file->f_flags & O_NONBLOCK);
907 }
908
909 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
910 {
911 struct viu_fh *fh = priv;
912
913 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
914 return -EINVAL;
915 if (fh->type != i)
916 return -EINVAL;
917
918 return videobuf_streamon(&fh->vb_vidq);
919 }
920
921 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
922 {
923 struct viu_fh *fh = priv;
924
925 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 return -EINVAL;
927 if (fh->type != i)
928 return -EINVAL;
929
930 return videobuf_streamoff(&fh->vb_vidq);
931 }
932
933 #define decoder_call(viu, o, f, args...) \
934 v4l2_subdev_call(viu->decoder, o, f, ##args)
935
936 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
937 {
938 struct viu_fh *fh = priv;
939
940 decoder_call(fh->dev, core, s_std, *id);
941 return 0;
942 }
943
944 /* only one input in this driver */
945 static int vidioc_enum_input(struct file *file, void *priv,
946 struct v4l2_input *inp)
947 {
948 struct viu_fh *fh = priv;
949
950 if (inp->index != 0)
951 return -EINVAL;
952
953 inp->type = V4L2_INPUT_TYPE_CAMERA;
954 inp->std = fh->dev->vdev->tvnorms;
955 strcpy(inp->name, "Camera");
956 return 0;
957 }
958
959 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
960 {
961 *i = 0;
962 return 0;
963 }
964
965 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
966 {
967 struct viu_fh *fh = priv;
968
969 if (i > 1)
970 return -EINVAL;
971
972 decoder_call(fh->dev, video, s_routing, i, 0, 0);
973 return 0;
974 }
975
976 /* Controls */
977 static int vidioc_queryctrl(struct file *file, void *priv,
978 struct v4l2_queryctrl *qc)
979 {
980 int i;
981
982 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
983 if (qc->id && qc->id == viu_qctrl[i].id) {
984 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
985 return 0;
986 }
987 }
988 return -EINVAL;
989 }
990
991 static int vidioc_g_ctrl(struct file *file, void *priv,
992 struct v4l2_control *ctrl)
993 {
994 int i;
995
996 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
997 if (ctrl->id == viu_qctrl[i].id) {
998 ctrl->value = qctl_regs[i];
999 return 0;
1000 }
1001 }
1002 return -EINVAL;
1003 }
1004 static int vidioc_s_ctrl(struct file *file, void *priv,
1005 struct v4l2_control *ctrl)
1006 {
1007 int i;
1008
1009 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1010 if (ctrl->id == viu_qctrl[i].id) {
1011 if (ctrl->value < viu_qctrl[i].minimum
1012 || ctrl->value > viu_qctrl[i].maximum)
1013 return -ERANGE;
1014 qctl_regs[i] = ctrl->value;
1015 return 0;
1016 }
1017 }
1018 return -EINVAL;
1019 }
1020
1021 inline void viu_activate_next_buf(struct viu_dev *dev,
1022 struct viu_dmaqueue *viuq)
1023 {
1024 struct viu_dmaqueue *vidq = viuq;
1025 struct viu_buf *buf;
1026
1027 /* launch another DMA operation for an active/queued buffer */
1028 if (!list_empty(&vidq->active)) {
1029 buf = list_entry(vidq->active.next, struct viu_buf,
1030 vb.queue);
1031 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1032 buffer_activate(dev, buf);
1033 } else if (!list_empty(&vidq->queued)) {
1034 buf = list_entry(vidq->queued.next, struct viu_buf,
1035 vb.queue);
1036 list_del(&buf->vb.queue);
1037
1038 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1039 list_add_tail(&buf->vb.queue, &vidq->active);
1040 buf->vb.state = VIDEOBUF_ACTIVE;
1041 buffer_activate(dev, buf);
1042 }
1043 }
1044
1045 inline void viu_default_settings(struct viu_reg *viu_reg)
1046 {
1047 struct viu_reg *vr = viu_reg;
1048
1049 out_be32(&vr->luminance, 0x9512A254);
1050 out_be32(&vr->chroma_r, 0x03310000);
1051 out_be32(&vr->chroma_g, 0x06600F38);
1052 out_be32(&vr->chroma_b, 0x00000409);
1053 out_be32(&vr->alpha, 0x000000ff);
1054 out_be32(&vr->req_alarm, 0x00000090);
1055 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1056 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1057 }
1058
1059 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1060 {
1061 struct viu_reg *vr = dev->vr;
1062
1063 if (status & INT_DMA_END_STATUS)
1064 dev->dma_done = 1;
1065
1066 if (status & INT_FIELD_STATUS) {
1067 if (dev->dma_done) {
1068 u32 addr = reg_val.field_base_addr;
1069
1070 dev->dma_done = 0;
1071 if (status & FIELD_NO)
1072 addr += reg_val.dma_inc;
1073
1074 out_be32(&vr->field_base_addr, addr);
1075 out_be32(&vr->dma_inc, reg_val.dma_inc);
1076 out_be32(&vr->status_cfg,
1077 (status & 0xffc0ffff) |
1078 (status & INT_ALL_STATUS) |
1079 reg_val.status_cfg);
1080 } else if (status & INT_VSYNC_STATUS) {
1081 out_be32(&vr->status_cfg,
1082 (status & 0xffc0ffff) |
1083 (status & INT_ALL_STATUS) |
1084 reg_val.status_cfg);
1085 }
1086 }
1087 }
1088
1089 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1090 {
1091 struct viu_dmaqueue *vidq = &dev->vidq;
1092 struct viu_reg *vr = dev->vr;
1093 struct viu_buf *buf;
1094 int field_num;
1095 int need_two;
1096 int dma_done = 0;
1097
1098 field_num = status & FIELD_NO;
1099 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1100
1101 if (status & INT_DMA_END_STATUS) {
1102 dma_done = 1;
1103 if (((field_num == 0) && (dev->field == 0)) ||
1104 (field_num && (dev->field == 1)))
1105 dev->field++;
1106 }
1107
1108 if (status & INT_FIELD_STATUS) {
1109 dprintk(1, "irq: field %d, done %d\n",
1110 !!field_num, dma_done);
1111 if (unlikely(dev->first)) {
1112 if (field_num == 0) {
1113 dev->first = 0;
1114 dprintk(1, "activate first buf\n");
1115 viu_activate_next_buf(dev, vidq);
1116 } else
1117 dprintk(1, "wait field 0\n");
1118 return;
1119 }
1120
1121 /* setup buffer address for next dma operation */
1122 if (!list_empty(&vidq->active)) {
1123 u32 addr = reg_val.field_base_addr;
1124
1125 if (field_num && need_two) {
1126 addr += reg_val.dma_inc;
1127 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1128 (unsigned long)addr, dev->field);
1129 }
1130 out_be32(&vr->field_base_addr, addr);
1131 out_be32(&vr->dma_inc, reg_val.dma_inc);
1132 out_be32(&vr->status_cfg,
1133 (status & 0xffc0ffff) |
1134 (status & INT_ALL_STATUS) |
1135 reg_val.status_cfg);
1136 return;
1137 }
1138 }
1139
1140 if (dma_done && field_num && (dev->field == 2)) {
1141 dev->field = 0;
1142 buf = list_entry(vidq->active.next,
1143 struct viu_buf, vb.queue);
1144 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1145 buf, buf->vb.i,
1146 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1147 (unsigned long)in_be32(&vr->field_base_addr));
1148
1149 if (waitqueue_active(&buf->vb.done)) {
1150 list_del(&buf->vb.queue);
1151 do_gettimeofday(&buf->vb.ts);
1152 buf->vb.state = VIDEOBUF_DONE;
1153 buf->vb.field_count++;
1154 wake_up(&buf->vb.done);
1155 }
1156 /* activate next dma buffer */
1157 viu_activate_next_buf(dev, vidq);
1158 }
1159 }
1160
1161 static irqreturn_t viu_intr(int irq, void *dev_id)
1162 {
1163 struct viu_dev *dev = (struct viu_dev *)dev_id;
1164 struct viu_reg *vr = dev->vr;
1165 u32 status;
1166 u32 error;
1167
1168 status = in_be32(&vr->status_cfg);
1169
1170 if (status & INT_ERROR_STATUS) {
1171 dev->irqs.error_irq++;
1172 error = status & ERR_MASK;
1173 if (error)
1174 dprintk(1, "Err: error(%d), times:%d!\n",
1175 error >> 4, dev->irqs.error_irq);
1176 /* Clear interrupt error bit and error flags */
1177 out_be32(&vr->status_cfg,
1178 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1179 }
1180
1181 if (status & INT_DMA_END_STATUS) {
1182 dev->irqs.dma_end_irq++;
1183 dev->dma_done = 1;
1184 dprintk(2, "VIU DMA end interrupt times: %d\n",
1185 dev->irqs.dma_end_irq);
1186 }
1187
1188 if (status & INT_HSYNC_STATUS)
1189 dev->irqs.hsync_irq++;
1190
1191 if (status & INT_FIELD_STATUS) {
1192 dev->irqs.field_irq++;
1193 dprintk(2, "VIU field interrupt times: %d\n",
1194 dev->irqs.field_irq);
1195 }
1196
1197 if (status & INT_VSTART_STATUS)
1198 dev->irqs.vstart_irq++;
1199
1200 if (status & INT_VSYNC_STATUS) {
1201 dev->irqs.vsync_irq++;
1202 dprintk(2, "VIU vsync interrupt times: %d\n",
1203 dev->irqs.vsync_irq);
1204 }
1205
1206 /* clear all pending irqs */
1207 status = in_be32(&vr->status_cfg);
1208 out_be32(&vr->status_cfg,
1209 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1210
1211 if (dev->ovenable) {
1212 viu_overlay_intr(dev, status);
1213 return IRQ_HANDLED;
1214 }
1215
1216 /* Capture mode */
1217 viu_capture_intr(dev, status);
1218 return IRQ_HANDLED;
1219 }
1220
1221 /*
1222 * File operations for the device
1223 */
1224 static int viu_open(struct file *file)
1225 {
1226 struct video_device *vdev = video_devdata(file);
1227 struct viu_dev *dev = video_get_drvdata(vdev);
1228 struct viu_fh *fh;
1229 struct viu_reg *vr;
1230 int minor = vdev->minor;
1231 u32 status_cfg;
1232 int i;
1233
1234 dprintk(1, "viu: open (minor=%d)\n", minor);
1235
1236 dev->users++;
1237 if (dev->users > 1) {
1238 dev->users--;
1239 return -EBUSY;
1240 }
1241
1242 vr = dev->vr;
1243
1244 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1245 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1246
1247 /* allocate and initialize per filehandle data */
1248 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1249 if (!fh) {
1250 dev->users--;
1251 return -ENOMEM;
1252 }
1253
1254 file->private_data = fh;
1255 fh->dev = dev;
1256
1257 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1258 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1259 fh->width = norm_maxw();
1260 fh->height = norm_maxh();
1261 dev->crop_current.width = fh->width;
1262 dev->crop_current.height = fh->height;
1263
1264 /* Put all controls at a sane state */
1265 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1266 qctl_regs[i] = viu_qctrl[i].default_value;
1267
1268 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1269 (unsigned long)fh, (unsigned long)dev,
1270 (unsigned long)&dev->vidq);
1271 dprintk(1, "Open: list_empty queued=%d\n",
1272 list_empty(&dev->vidq.queued));
1273 dprintk(1, "Open: list_empty active=%d\n",
1274 list_empty(&dev->vidq.active));
1275
1276 viu_default_settings(vr);
1277
1278 status_cfg = in_be32(&vr->status_cfg);
1279 out_be32(&vr->status_cfg,
1280 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1281 INT_FIELD_EN | INT_VSTART_EN |
1282 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1283
1284 status_cfg = in_be32(&vr->status_cfg);
1285 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1286
1287 spin_lock_init(&fh->vbq_lock);
1288 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1289 dev->dev, &fh->vbq_lock,
1290 fh->type, V4L2_FIELD_INTERLACED,
1291 sizeof(struct viu_buf), fh, NULL);
1292 return 0;
1293 }
1294
1295 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1296 loff_t *ppos)
1297 {
1298 struct viu_fh *fh = file->private_data;
1299 struct viu_dev *dev = fh->dev;
1300 int ret = 0;
1301
1302 dprintk(2, "%s\n", __func__);
1303 if (dev->ovenable)
1304 dev->ovenable = 0;
1305
1306 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1307 viu_start_dma(dev);
1308 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1309 ppos, 0, file->f_flags & O_NONBLOCK);
1310 return ret;
1311 }
1312 return 0;
1313 }
1314
1315 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1316 {
1317 struct viu_fh *fh = file->private_data;
1318 struct videobuf_queue *q = &fh->vb_vidq;
1319
1320 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1321 return POLLERR;
1322
1323 return videobuf_poll_stream(file, q, wait);
1324 }
1325
1326 static int viu_release(struct file *file)
1327 {
1328 struct viu_fh *fh = file->private_data;
1329 struct viu_dev *dev = fh->dev;
1330 int minor = video_devdata(file)->minor;
1331
1332 viu_stop_dma(dev);
1333 videobuf_stop(&fh->vb_vidq);
1334
1335 kfree(fh);
1336
1337 dev->users--;
1338 dprintk(1, "close (minor=%d, users=%d)\n",
1339 minor, dev->users);
1340 return 0;
1341 }
1342
1343 void viu_reset(struct viu_reg *reg)
1344 {
1345 out_be32(&reg->status_cfg, 0);
1346 out_be32(&reg->luminance, 0x9512a254);
1347 out_be32(&reg->chroma_r, 0x03310000);
1348 out_be32(&reg->chroma_g, 0x06600f38);
1349 out_be32(&reg->chroma_b, 0x00000409);
1350 out_be32(&reg->field_base_addr, 0);
1351 out_be32(&reg->dma_inc, 0);
1352 out_be32(&reg->picture_count, 0x01e002d0);
1353 out_be32(&reg->req_alarm, 0x00000090);
1354 out_be32(&reg->alpha, 0x000000ff);
1355 }
1356
1357 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1358 {
1359 struct viu_fh *fh = file->private_data;
1360 int ret;
1361
1362 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1363
1364 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1365
1366 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1367 (unsigned long)vma->vm_start,
1368 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1369 ret);
1370
1371 return ret;
1372 }
1373
1374 static struct v4l2_file_operations viu_fops = {
1375 .owner = THIS_MODULE,
1376 .open = viu_open,
1377 .release = viu_release,
1378 .read = viu_read,
1379 .poll = viu_poll,
1380 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1381 .mmap = viu_mmap,
1382 };
1383
1384 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1385 .vidioc_querycap = vidioc_querycap,
1386 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1387 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1388 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1389 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1390 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1391 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1392 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1393 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1394 .vidioc_g_fbuf = vidioc_g_fbuf,
1395 .vidioc_s_fbuf = vidioc_s_fbuf,
1396 .vidioc_reqbufs = vidioc_reqbufs,
1397 .vidioc_querybuf = vidioc_querybuf,
1398 .vidioc_qbuf = vidioc_qbuf,
1399 .vidioc_dqbuf = vidioc_dqbuf,
1400 .vidioc_s_std = vidioc_s_std,
1401 .vidioc_enum_input = vidioc_enum_input,
1402 .vidioc_g_input = vidioc_g_input,
1403 .vidioc_s_input = vidioc_s_input,
1404 .vidioc_queryctrl = vidioc_queryctrl,
1405 .vidioc_g_ctrl = vidioc_g_ctrl,
1406 .vidioc_s_ctrl = vidioc_s_ctrl,
1407 .vidioc_streamon = vidioc_streamon,
1408 .vidioc_streamoff = vidioc_streamoff,
1409 };
1410
1411 static struct video_device viu_template = {
1412 .name = "FSL viu",
1413 .fops = &viu_fops,
1414 .minor = -1,
1415 .ioctl_ops = &viu_ioctl_ops,
1416 .release = video_device_release,
1417
1418 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1419 .current_norm = V4L2_STD_NTSC_M,
1420 };
1421
1422 static int __devinit viu_of_probe(struct platform_device *op,
1423 const struct of_device_id *match)
1424 {
1425 struct viu_dev *viu_dev;
1426 struct video_device *vdev;
1427 struct resource r;
1428 struct viu_reg __iomem *viu_regs;
1429 struct i2c_adapter *ad;
1430 int ret, viu_irq;
1431
1432 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1433 if (ret) {
1434 dev_err(&op->dev, "Can't parse device node resource\n");
1435 return -ENODEV;
1436 }
1437
1438 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1439 if (viu_irq == NO_IRQ) {
1440 dev_err(&op->dev, "Error while mapping the irq\n");
1441 return -EINVAL;
1442 }
1443
1444 /* request mem region */
1445 if (!devm_request_mem_region(&op->dev, r.start,
1446 sizeof(struct viu_reg), DRV_NAME)) {
1447 dev_err(&op->dev, "Error while requesting mem region\n");
1448 ret = -EBUSY;
1449 goto err;
1450 }
1451
1452 /* remap registers */
1453 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1454 if (!viu_regs) {
1455 dev_err(&op->dev, "Can't map register set\n");
1456 ret = -ENOMEM;
1457 goto err;
1458 }
1459
1460 /* Prepare our private structure */
1461 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1462 if (!viu_dev) {
1463 dev_err(&op->dev, "Can't allocate private structure\n");
1464 ret = -ENOMEM;
1465 goto err;
1466 }
1467
1468 viu_dev->vr = viu_regs;
1469 viu_dev->irq = viu_irq;
1470 viu_dev->dev = &op->dev;
1471
1472 /* init video dma queues */
1473 INIT_LIST_HEAD(&viu_dev->vidq.active);
1474 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1475
1476 /* initialize locks */
1477 mutex_init(&viu_dev->lock);
1478
1479 snprintf(viu_dev->v4l2_dev.name,
1480 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1481 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1482 if (ret < 0) {
1483 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1484 goto err;
1485 }
1486
1487 ad = i2c_get_adapter(0);
1488 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1489 NULL, "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1490
1491 viu_dev->vidq.timeout.function = viu_vid_timeout;
1492 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1493 init_timer(&viu_dev->vidq.timeout);
1494 viu_dev->first = 1;
1495
1496 /* Allocate memory for video device */
1497 vdev = video_device_alloc();
1498 if (vdev == NULL) {
1499 ret = -ENOMEM;
1500 goto err_vdev;
1501 }
1502
1503 memcpy(vdev, &viu_template, sizeof(viu_template));
1504
1505 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1506
1507 viu_dev->vdev = vdev;
1508
1509 video_set_drvdata(viu_dev->vdev, viu_dev);
1510
1511 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1512 if (ret < 0) {
1513 video_device_release(viu_dev->vdev);
1514 goto err_vdev;
1515 }
1516
1517 /* enable VIU clock */
1518 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1519 if (IS_ERR(viu_dev->clk)) {
1520 dev_err(&op->dev, "failed to find the clock module!\n");
1521 ret = -ENODEV;
1522 goto err_clk;
1523 } else {
1524 clk_enable(viu_dev->clk);
1525 }
1526
1527 /* reset VIU module */
1528 viu_reset(viu_dev->vr);
1529
1530 /* install interrupt handler */
1531 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1532 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1533 ret = -ENODEV;
1534 goto err_irq;
1535 }
1536
1537 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1538 return ret;
1539
1540 err_irq:
1541 clk_disable(viu_dev->clk);
1542 clk_put(viu_dev->clk);
1543 err_clk:
1544 video_unregister_device(viu_dev->vdev);
1545 err_vdev:
1546 i2c_put_adapter(ad);
1547 v4l2_device_unregister(&viu_dev->v4l2_dev);
1548 err:
1549 irq_dispose_mapping(viu_irq);
1550 return ret;
1551 }
1552
1553 static int __devexit viu_of_remove(struct platform_device *op)
1554 {
1555 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1556 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1557 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1558 struct v4l2_subdev, list);
1559 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1560
1561 free_irq(dev->irq, (void *)dev);
1562 irq_dispose_mapping(dev->irq);
1563
1564 clk_disable(dev->clk);
1565 clk_put(dev->clk);
1566
1567 video_unregister_device(dev->vdev);
1568 i2c_put_adapter(client->adapter);
1569 v4l2_device_unregister(&dev->v4l2_dev);
1570 return 0;
1571 }
1572
1573 #ifdef CONFIG_PM
1574 static int viu_suspend(struct platform_device *op, pm_message_t state)
1575 {
1576 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1577 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1578
1579 clk_disable(dev->clk);
1580 return 0;
1581 }
1582
1583 static int viu_resume(struct platform_device *op)
1584 {
1585 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1586 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1587
1588 clk_enable(dev->clk);
1589 return 0;
1590 }
1591 #endif
1592
1593 /*
1594 * Initialization and module stuff
1595 */
1596 static struct of_device_id mpc512x_viu_of_match[] = {
1597 {
1598 .compatible = "fsl,mpc5121-viu",
1599 },
1600 {},
1601 };
1602 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1603
1604 static struct of_platform_driver viu_of_platform_driver = {
1605 .probe = viu_of_probe,
1606 .remove = __devexit_p(viu_of_remove),
1607 #ifdef CONFIG_PM
1608 .suspend = viu_suspend,
1609 .resume = viu_resume,
1610 #endif
1611 .driver = {
1612 .name = DRV_NAME,
1613 .owner = THIS_MODULE,
1614 .of_match_table = mpc512x_viu_of_match,
1615 },
1616 };
1617
1618 static int __init viu_init(void)
1619 {
1620 return of_register_platform_driver(&viu_of_platform_driver);
1621 }
1622
1623 static void __exit viu_exit(void)
1624 {
1625 of_unregister_platform_driver(&viu_of_platform_driver);
1626 }
1627
1628 module_init(viu_init);
1629 module_exit(viu_exit);
1630
1631 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1632 MODULE_AUTHOR("Hongjun Chen");
1633 MODULE_LICENSE("GPL");