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