blob: ab0e09d391f19b719591ec3ce6614bf909d83643 [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
Anatolij Gustschin791ae692011-05-04 17:19:28 -0300769static int viu_setup_preview(struct viu_dev *dev, struct viu_fh *fh)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300770{
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
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300808 return 0;
809}
810
811static int vidioc_s_fmt_overlay(struct file *file, void *priv,
812 struct v4l2_format *f)
813{
814 struct viu_fh *fh = priv;
815 struct viu_dev *dev = (struct viu_dev *)fh->dev;
816 unsigned long flags;
817 int err;
818
819 err = verify_preview(dev, &f->fmt.win);
820 if (err)
821 return err;
822
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300823 fh->win = f->fmt.win;
824
825 spin_lock_irqsave(&dev->slock, flags);
Anatolij Gustschin791ae692011-05-04 17:19:28 -0300826 viu_setup_preview(dev, fh);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300827 spin_unlock_irqrestore(&dev->slock, flags);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300828 return 0;
829}
830
831static int vidioc_try_fmt_overlay(struct file *file, void *priv,
832 struct v4l2_format *f)
833{
834 return 0;
835}
836
Anatolij Gustschin791ae692011-05-04 17:19:28 -0300837static int vidioc_overlay(struct file *file, void *priv, unsigned int on)
838{
839 struct viu_fh *fh = priv;
840 struct viu_dev *dev = (struct viu_dev *)fh->dev;
841 unsigned long flags;
842
843 if (on) {
844 spin_lock_irqsave(&dev->slock, flags);
845 viu_activate_overlay(dev->vr);
846 dev->ovenable = 1;
847
848 /* start dma */
849 viu_start_dma(dev);
850 spin_unlock_irqrestore(&dev->slock, flags);
851 } else {
852 viu_stop_dma(dev);
853 dev->ovenable = 0;
854 }
855
856 return 0;
857}
858
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300859int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
860{
861 struct viu_fh *fh = priv;
862 struct viu_dev *dev = fh->dev;
863 struct v4l2_framebuffer *fb = arg;
864
865 *fb = dev->ovbuf;
866 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
867 return 0;
868}
869
870int vidioc_s_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
871{
872 struct viu_fh *fh = priv;
873 struct viu_dev *dev = fh->dev;
874 struct v4l2_framebuffer *fb = arg;
875 struct viu_fmt *fmt;
876
877 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
878 return -EPERM;
879
880 /* check args */
881 fmt = format_by_fourcc(fb->fmt.pixelformat);
882 if (fmt == NULL)
883 return -EINVAL;
884
885 /* ok, accept it */
886 dev->ovbuf = *fb;
887 dev->ovfmt = fmt;
888 if (dev->ovbuf.fmt.bytesperline == 0) {
889 dev->ovbuf.fmt.bytesperline =
890 dev->ovbuf.fmt.width * fmt->depth / 8;
891 }
892 return 0;
893}
894
895static int vidioc_reqbufs(struct file *file, void *priv,
896 struct v4l2_requestbuffers *p)
897{
898 struct viu_fh *fh = priv;
899
900 return videobuf_reqbufs(&fh->vb_vidq, p);
901}
902
903static int vidioc_querybuf(struct file *file, void *priv,
904 struct v4l2_buffer *p)
905{
906 struct viu_fh *fh = priv;
907
908 return videobuf_querybuf(&fh->vb_vidq, p);
909}
910
911static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
912{
913 struct viu_fh *fh = priv;
914
915 return videobuf_qbuf(&fh->vb_vidq, p);
916}
917
918static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
919{
920 struct viu_fh *fh = priv;
921
922 return videobuf_dqbuf(&fh->vb_vidq, p,
923 file->f_flags & O_NONBLOCK);
924}
925
926static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
927{
928 struct viu_fh *fh = priv;
929
930 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
931 return -EINVAL;
932 if (fh->type != i)
933 return -EINVAL;
934
Anatolij Gustschinc3353332010-12-17 06:40:50 -0300935 viu_start_dma(fh->dev);
936
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300937 return videobuf_streamon(&fh->vb_vidq);
938}
939
940static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
941{
942 struct viu_fh *fh = priv;
943
944 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
945 return -EINVAL;
946 if (fh->type != i)
947 return -EINVAL;
948
Anatolij Gustschinc3353332010-12-17 06:40:50 -0300949 viu_stop_dma(fh->dev);
950
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300951 return videobuf_streamoff(&fh->vb_vidq);
952}
953
954#define decoder_call(viu, o, f, args...) \
955 v4l2_subdev_call(viu->decoder, o, f, ##args)
956
Anatolij Gustschin50155c22010-12-22 17:31:59 -0300957static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
958{
959 struct viu_fh *fh = priv;
960
961 decoder_call(fh->dev, video, querystd, std_id);
962 return 0;
963}
964
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300965static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
966{
967 struct viu_fh *fh = priv;
968
Anatolij Gustschin50155c22010-12-22 17:31:59 -0300969 fh->dev->std = *id;
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300970 decoder_call(fh->dev, core, s_std, *id);
971 return 0;
972}
973
Anatolij Gustschin50155c22010-12-22 17:31:59 -0300974static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
975{
976 struct viu_fh *fh = priv;
977
978 *std_id = fh->dev->std;
979 return 0;
980}
981
Anatolij Gustschin95c5d602010-07-02 10:10:09 -0300982/* only one input in this driver */
983static int vidioc_enum_input(struct file *file, void *priv,
984 struct v4l2_input *inp)
985{
986 struct viu_fh *fh = priv;
987
988 if (inp->index != 0)
989 return -EINVAL;
990
991 inp->type = V4L2_INPUT_TYPE_CAMERA;
992 inp->std = fh->dev->vdev->tvnorms;
993 strcpy(inp->name, "Camera");
994 return 0;
995}
996
997static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
998{
999 *i = 0;
1000 return 0;
1001}
1002
1003static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1004{
1005 struct viu_fh *fh = priv;
1006
1007 if (i > 1)
1008 return -EINVAL;
1009
1010 decoder_call(fh->dev, video, s_routing, i, 0, 0);
1011 return 0;
1012}
1013
1014/* Controls */
1015static int vidioc_queryctrl(struct file *file, void *priv,
1016 struct v4l2_queryctrl *qc)
1017{
1018 int i;
1019
1020 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1021 if (qc->id && qc->id == viu_qctrl[i].id) {
1022 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
1023 return 0;
1024 }
1025 }
1026 return -EINVAL;
1027}
1028
1029static int vidioc_g_ctrl(struct file *file, void *priv,
1030 struct v4l2_control *ctrl)
1031{
1032 int i;
1033
1034 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1035 if (ctrl->id == viu_qctrl[i].id) {
1036 ctrl->value = qctl_regs[i];
1037 return 0;
1038 }
1039 }
1040 return -EINVAL;
1041}
1042static int vidioc_s_ctrl(struct file *file, void *priv,
1043 struct v4l2_control *ctrl)
1044{
1045 int i;
1046
1047 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1048 if (ctrl->id == viu_qctrl[i].id) {
1049 if (ctrl->value < viu_qctrl[i].minimum
1050 || ctrl->value > viu_qctrl[i].maximum)
1051 return -ERANGE;
1052 qctl_regs[i] = ctrl->value;
1053 return 0;
1054 }
1055 }
1056 return -EINVAL;
1057}
1058
1059inline void viu_activate_next_buf(struct viu_dev *dev,
1060 struct viu_dmaqueue *viuq)
1061{
1062 struct viu_dmaqueue *vidq = viuq;
1063 struct viu_buf *buf;
1064
1065 /* launch another DMA operation for an active/queued buffer */
1066 if (!list_empty(&vidq->active)) {
1067 buf = list_entry(vidq->active.next, struct viu_buf,
1068 vb.queue);
1069 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1070 buffer_activate(dev, buf);
1071 } else if (!list_empty(&vidq->queued)) {
1072 buf = list_entry(vidq->queued.next, struct viu_buf,
1073 vb.queue);
1074 list_del(&buf->vb.queue);
1075
1076 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1077 list_add_tail(&buf->vb.queue, &vidq->active);
1078 buf->vb.state = VIDEOBUF_ACTIVE;
1079 buffer_activate(dev, buf);
1080 }
1081}
1082
1083inline void viu_default_settings(struct viu_reg *viu_reg)
1084{
1085 struct viu_reg *vr = viu_reg;
1086
1087 out_be32(&vr->luminance, 0x9512A254);
1088 out_be32(&vr->chroma_r, 0x03310000);
1089 out_be32(&vr->chroma_g, 0x06600F38);
1090 out_be32(&vr->chroma_b, 0x00000409);
1091 out_be32(&vr->alpha, 0x000000ff);
1092 out_be32(&vr->req_alarm, 0x00000090);
1093 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1094 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1095}
1096
1097static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1098{
1099 struct viu_reg *vr = dev->vr;
1100
1101 if (status & INT_DMA_END_STATUS)
1102 dev->dma_done = 1;
1103
1104 if (status & INT_FIELD_STATUS) {
1105 if (dev->dma_done) {
1106 u32 addr = reg_val.field_base_addr;
1107
1108 dev->dma_done = 0;
1109 if (status & FIELD_NO)
1110 addr += reg_val.dma_inc;
1111
1112 out_be32(&vr->field_base_addr, addr);
1113 out_be32(&vr->dma_inc, reg_val.dma_inc);
1114 out_be32(&vr->status_cfg,
1115 (status & 0xffc0ffff) |
1116 (status & INT_ALL_STATUS) |
1117 reg_val.status_cfg);
1118 } else if (status & INT_VSYNC_STATUS) {
1119 out_be32(&vr->status_cfg,
1120 (status & 0xffc0ffff) |
1121 (status & INT_ALL_STATUS) |
1122 reg_val.status_cfg);
1123 }
1124 }
1125}
1126
1127static void viu_capture_intr(struct viu_dev *dev, u32 status)
1128{
1129 struct viu_dmaqueue *vidq = &dev->vidq;
1130 struct viu_reg *vr = dev->vr;
1131 struct viu_buf *buf;
1132 int field_num;
1133 int need_two;
1134 int dma_done = 0;
1135
1136 field_num = status & FIELD_NO;
1137 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1138
1139 if (status & INT_DMA_END_STATUS) {
1140 dma_done = 1;
1141 if (((field_num == 0) && (dev->field == 0)) ||
1142 (field_num && (dev->field == 1)))
1143 dev->field++;
1144 }
1145
1146 if (status & INT_FIELD_STATUS) {
1147 dprintk(1, "irq: field %d, done %d\n",
1148 !!field_num, dma_done);
1149 if (unlikely(dev->first)) {
1150 if (field_num == 0) {
1151 dev->first = 0;
1152 dprintk(1, "activate first buf\n");
1153 viu_activate_next_buf(dev, vidq);
1154 } else
1155 dprintk(1, "wait field 0\n");
1156 return;
1157 }
1158
1159 /* setup buffer address for next dma operation */
1160 if (!list_empty(&vidq->active)) {
1161 u32 addr = reg_val.field_base_addr;
1162
1163 if (field_num && need_two) {
1164 addr += reg_val.dma_inc;
1165 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1166 (unsigned long)addr, dev->field);
1167 }
1168 out_be32(&vr->field_base_addr, addr);
1169 out_be32(&vr->dma_inc, reg_val.dma_inc);
1170 out_be32(&vr->status_cfg,
1171 (status & 0xffc0ffff) |
1172 (status & INT_ALL_STATUS) |
1173 reg_val.status_cfg);
1174 return;
1175 }
1176 }
1177
1178 if (dma_done && field_num && (dev->field == 2)) {
1179 dev->field = 0;
1180 buf = list_entry(vidq->active.next,
1181 struct viu_buf, vb.queue);
1182 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1183 buf, buf->vb.i,
1184 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1185 (unsigned long)in_be32(&vr->field_base_addr));
1186
1187 if (waitqueue_active(&buf->vb.done)) {
1188 list_del(&buf->vb.queue);
1189 do_gettimeofday(&buf->vb.ts);
1190 buf->vb.state = VIDEOBUF_DONE;
1191 buf->vb.field_count++;
1192 wake_up(&buf->vb.done);
1193 }
1194 /* activate next dma buffer */
1195 viu_activate_next_buf(dev, vidq);
1196 }
1197}
1198
1199static irqreturn_t viu_intr(int irq, void *dev_id)
1200{
1201 struct viu_dev *dev = (struct viu_dev *)dev_id;
1202 struct viu_reg *vr = dev->vr;
1203 u32 status;
1204 u32 error;
1205
1206 status = in_be32(&vr->status_cfg);
1207
1208 if (status & INT_ERROR_STATUS) {
1209 dev->irqs.error_irq++;
1210 error = status & ERR_MASK;
1211 if (error)
1212 dprintk(1, "Err: error(%d), times:%d!\n",
1213 error >> 4, dev->irqs.error_irq);
1214 /* Clear interrupt error bit and error flags */
1215 out_be32(&vr->status_cfg,
1216 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1217 }
1218
1219 if (status & INT_DMA_END_STATUS) {
1220 dev->irqs.dma_end_irq++;
1221 dev->dma_done = 1;
1222 dprintk(2, "VIU DMA end interrupt times: %d\n",
1223 dev->irqs.dma_end_irq);
1224 }
1225
1226 if (status & INT_HSYNC_STATUS)
1227 dev->irqs.hsync_irq++;
1228
1229 if (status & INT_FIELD_STATUS) {
1230 dev->irqs.field_irq++;
1231 dprintk(2, "VIU field interrupt times: %d\n",
1232 dev->irqs.field_irq);
1233 }
1234
1235 if (status & INT_VSTART_STATUS)
1236 dev->irqs.vstart_irq++;
1237
1238 if (status & INT_VSYNC_STATUS) {
1239 dev->irqs.vsync_irq++;
1240 dprintk(2, "VIU vsync interrupt times: %d\n",
1241 dev->irqs.vsync_irq);
1242 }
1243
1244 /* clear all pending irqs */
1245 status = in_be32(&vr->status_cfg);
1246 out_be32(&vr->status_cfg,
1247 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1248
1249 if (dev->ovenable) {
1250 viu_overlay_intr(dev, status);
1251 return IRQ_HANDLED;
1252 }
1253
1254 /* Capture mode */
1255 viu_capture_intr(dev, status);
1256 return IRQ_HANDLED;
1257}
1258
1259/*
1260 * File operations for the device
1261 */
1262static int viu_open(struct file *file)
1263{
1264 struct video_device *vdev = video_devdata(file);
1265 struct viu_dev *dev = video_get_drvdata(vdev);
1266 struct viu_fh *fh;
1267 struct viu_reg *vr;
1268 int minor = vdev->minor;
1269 u32 status_cfg;
1270 int i;
1271
1272 dprintk(1, "viu: open (minor=%d)\n", minor);
1273
1274 dev->users++;
1275 if (dev->users > 1) {
1276 dev->users--;
1277 return -EBUSY;
1278 }
1279
1280 vr = dev->vr;
1281
1282 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1283 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1284
1285 /* allocate and initialize per filehandle data */
1286 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1287 if (!fh) {
1288 dev->users--;
1289 return -ENOMEM;
1290 }
1291
1292 file->private_data = fh;
1293 fh->dev = dev;
1294
1295 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1296 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1297 fh->width = norm_maxw();
1298 fh->height = norm_maxh();
1299 dev->crop_current.width = fh->width;
1300 dev->crop_current.height = fh->height;
1301
1302 /* Put all controls at a sane state */
1303 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1304 qctl_regs[i] = viu_qctrl[i].default_value;
1305
1306 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1307 (unsigned long)fh, (unsigned long)dev,
1308 (unsigned long)&dev->vidq);
1309 dprintk(1, "Open: list_empty queued=%d\n",
1310 list_empty(&dev->vidq.queued));
1311 dprintk(1, "Open: list_empty active=%d\n",
1312 list_empty(&dev->vidq.active));
1313
1314 viu_default_settings(vr);
1315
1316 status_cfg = in_be32(&vr->status_cfg);
1317 out_be32(&vr->status_cfg,
1318 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1319 INT_FIELD_EN | INT_VSTART_EN |
1320 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1321
1322 status_cfg = in_be32(&vr->status_cfg);
1323 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1324
1325 spin_lock_init(&fh->vbq_lock);
1326 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1327 dev->dev, &fh->vbq_lock,
1328 fh->type, V4L2_FIELD_INTERLACED,
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001329 sizeof(struct viu_buf), fh,
1330 &fh->dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001331 return 0;
1332}
1333
1334static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1335 loff_t *ppos)
1336{
1337 struct viu_fh *fh = file->private_data;
1338 struct viu_dev *dev = fh->dev;
1339 int ret = 0;
1340
1341 dprintk(2, "%s\n", __func__);
1342 if (dev->ovenable)
1343 dev->ovenable = 0;
1344
1345 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1346 viu_start_dma(dev);
1347 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1348 ppos, 0, file->f_flags & O_NONBLOCK);
1349 return ret;
1350 }
1351 return 0;
1352}
1353
1354static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1355{
1356 struct viu_fh *fh = file->private_data;
1357 struct videobuf_queue *q = &fh->vb_vidq;
1358
1359 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1360 return POLLERR;
1361
1362 return videobuf_poll_stream(file, q, wait);
1363}
1364
1365static int viu_release(struct file *file)
1366{
1367 struct viu_fh *fh = file->private_data;
1368 struct viu_dev *dev = fh->dev;
1369 int minor = video_devdata(file)->minor;
1370
1371 viu_stop_dma(dev);
1372 videobuf_stop(&fh->vb_vidq);
Anatolij Gustschinc3353332010-12-17 06:40:50 -03001373 videobuf_mmap_free(&fh->vb_vidq);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001374
1375 kfree(fh);
1376
1377 dev->users--;
1378 dprintk(1, "close (minor=%d, users=%d)\n",
1379 minor, dev->users);
1380 return 0;
1381}
1382
1383void viu_reset(struct viu_reg *reg)
1384{
1385 out_be32(&reg->status_cfg, 0);
1386 out_be32(&reg->luminance, 0x9512a254);
1387 out_be32(&reg->chroma_r, 0x03310000);
1388 out_be32(&reg->chroma_g, 0x06600f38);
1389 out_be32(&reg->chroma_b, 0x00000409);
1390 out_be32(&reg->field_base_addr, 0);
1391 out_be32(&reg->dma_inc, 0);
1392 out_be32(&reg->picture_count, 0x01e002d0);
1393 out_be32(&reg->req_alarm, 0x00000090);
1394 out_be32(&reg->alpha, 0x000000ff);
1395}
1396
1397static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1398{
1399 struct viu_fh *fh = file->private_data;
1400 int ret;
1401
1402 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1403
1404 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1405
1406 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1407 (unsigned long)vma->vm_start,
1408 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1409 ret);
1410
1411 return ret;
1412}
1413
1414static struct v4l2_file_operations viu_fops = {
1415 .owner = THIS_MODULE,
1416 .open = viu_open,
1417 .release = viu_release,
1418 .read = viu_read,
1419 .poll = viu_poll,
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001420 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001421 .mmap = viu_mmap,
1422};
1423
1424static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1425 .vidioc_querycap = vidioc_querycap,
1426 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1427 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1428 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1429 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1430 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1431 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1432 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1433 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
Anatolij Gustschin791ae692011-05-04 17:19:28 -03001434 .vidioc_overlay = vidioc_overlay,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001435 .vidioc_g_fbuf = vidioc_g_fbuf,
1436 .vidioc_s_fbuf = vidioc_s_fbuf,
1437 .vidioc_reqbufs = vidioc_reqbufs,
1438 .vidioc_querybuf = vidioc_querybuf,
1439 .vidioc_qbuf = vidioc_qbuf,
1440 .vidioc_dqbuf = vidioc_dqbuf,
Anatolij Gustschin50155c22010-12-22 17:31:59 -03001441 .vidioc_g_std = vidioc_g_std,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001442 .vidioc_s_std = vidioc_s_std,
Anatolij Gustschin50155c22010-12-22 17:31:59 -03001443 .vidioc_querystd = vidioc_querystd,
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001444 .vidioc_enum_input = vidioc_enum_input,
1445 .vidioc_g_input = vidioc_g_input,
1446 .vidioc_s_input = vidioc_s_input,
1447 .vidioc_queryctrl = vidioc_queryctrl,
1448 .vidioc_g_ctrl = vidioc_g_ctrl,
1449 .vidioc_s_ctrl = vidioc_s_ctrl,
1450 .vidioc_streamon = vidioc_streamon,
1451 .vidioc_streamoff = vidioc_streamoff,
1452};
1453
1454static struct video_device viu_template = {
1455 .name = "FSL viu",
1456 .fops = &viu_fops,
1457 .minor = -1,
1458 .ioctl_ops = &viu_ioctl_ops,
1459 .release = video_device_release,
1460
1461 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1462 .current_norm = V4L2_STD_NTSC_M,
1463};
1464
Grant Likely1c48a5c2011-02-17 02:43:24 -07001465static int __devinit viu_of_probe(struct platform_device *op)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001466{
1467 struct viu_dev *viu_dev;
1468 struct video_device *vdev;
1469 struct resource r;
1470 struct viu_reg __iomem *viu_regs;
1471 struct i2c_adapter *ad;
1472 int ret, viu_irq;
1473
1474 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1475 if (ret) {
1476 dev_err(&op->dev, "Can't parse device node resource\n");
1477 return -ENODEV;
1478 }
1479
1480 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1481 if (viu_irq == NO_IRQ) {
1482 dev_err(&op->dev, "Error while mapping the irq\n");
1483 return -EINVAL;
1484 }
1485
1486 /* request mem region */
1487 if (!devm_request_mem_region(&op->dev, r.start,
1488 sizeof(struct viu_reg), DRV_NAME)) {
1489 dev_err(&op->dev, "Error while requesting mem region\n");
1490 ret = -EBUSY;
1491 goto err;
1492 }
1493
1494 /* remap registers */
1495 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1496 if (!viu_regs) {
1497 dev_err(&op->dev, "Can't map register set\n");
1498 ret = -ENOMEM;
1499 goto err;
1500 }
1501
1502 /* Prepare our private structure */
1503 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1504 if (!viu_dev) {
1505 dev_err(&op->dev, "Can't allocate private structure\n");
1506 ret = -ENOMEM;
1507 goto err;
1508 }
1509
1510 viu_dev->vr = viu_regs;
1511 viu_dev->irq = viu_irq;
1512 viu_dev->dev = &op->dev;
1513
1514 /* init video dma queues */
1515 INIT_LIST_HEAD(&viu_dev->vidq.active);
1516 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1517
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001518 snprintf(viu_dev->v4l2_dev.name,
1519 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1520 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1521 if (ret < 0) {
1522 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1523 goto err;
1524 }
1525
1526 ad = i2c_get_adapter(0);
1527 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001528 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001529
1530 viu_dev->vidq.timeout.function = viu_vid_timeout;
1531 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1532 init_timer(&viu_dev->vidq.timeout);
1533 viu_dev->first = 1;
1534
1535 /* Allocate memory for video device */
1536 vdev = video_device_alloc();
1537 if (vdev == NULL) {
1538 ret = -ENOMEM;
1539 goto err_vdev;
1540 }
1541
1542 memcpy(vdev, &viu_template, sizeof(viu_template));
1543
1544 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1545
1546 viu_dev->vdev = vdev;
1547
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001548 /* initialize locks */
1549 mutex_init(&viu_dev->lock);
1550 viu_dev->vdev->lock = &viu_dev->lock;
1551 spin_lock_init(&viu_dev->slock);
1552
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001553 video_set_drvdata(viu_dev->vdev, viu_dev);
1554
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001555 mutex_lock(&viu_dev->lock);
1556
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001557 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1558 if (ret < 0) {
1559 video_device_release(viu_dev->vdev);
1560 goto err_vdev;
1561 }
1562
1563 /* enable VIU clock */
1564 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1565 if (IS_ERR(viu_dev->clk)) {
1566 dev_err(&op->dev, "failed to find the clock module!\n");
1567 ret = -ENODEV;
1568 goto err_clk;
1569 } else {
1570 clk_enable(viu_dev->clk);
1571 }
1572
1573 /* reset VIU module */
1574 viu_reset(viu_dev->vr);
1575
1576 /* install interrupt handler */
1577 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1578 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1579 ret = -ENODEV;
1580 goto err_irq;
1581 }
1582
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001583 mutex_unlock(&viu_dev->lock);
1584
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001585 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1586 return ret;
1587
1588err_irq:
1589 clk_disable(viu_dev->clk);
1590 clk_put(viu_dev->clk);
1591err_clk:
1592 video_unregister_device(viu_dev->vdev);
1593err_vdev:
Anatolij Gustschin2f970002011-02-19 17:33:18 -03001594 mutex_unlock(&viu_dev->lock);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001595 i2c_put_adapter(ad);
1596 v4l2_device_unregister(&viu_dev->v4l2_dev);
1597err:
1598 irq_dispose_mapping(viu_irq);
1599 return ret;
1600}
1601
Grant Likely2dc11582010-08-06 09:25:50 -06001602static int __devexit viu_of_remove(struct platform_device *op)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001603{
1604 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1605 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1606 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1607 struct v4l2_subdev, list);
1608 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1609
1610 free_irq(dev->irq, (void *)dev);
1611 irq_dispose_mapping(dev->irq);
1612
1613 clk_disable(dev->clk);
1614 clk_put(dev->clk);
1615
1616 video_unregister_device(dev->vdev);
1617 i2c_put_adapter(client->adapter);
1618 v4l2_device_unregister(&dev->v4l2_dev);
1619 return 0;
1620}
1621
1622#ifdef CONFIG_PM
Grant Likely2dc11582010-08-06 09:25:50 -06001623static int viu_suspend(struct platform_device *op, pm_message_t state)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001624{
1625 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1626 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1627
1628 clk_disable(dev->clk);
1629 return 0;
1630}
1631
Grant Likely2dc11582010-08-06 09:25:50 -06001632static int viu_resume(struct platform_device *op)
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001633{
1634 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1635 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1636
1637 clk_enable(dev->clk);
1638 return 0;
1639}
1640#endif
1641
1642/*
1643 * Initialization and module stuff
1644 */
1645static struct of_device_id mpc512x_viu_of_match[] = {
1646 {
1647 .compatible = "fsl,mpc5121-viu",
1648 },
1649 {},
1650};
1651MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1652
Grant Likely1c48a5c2011-02-17 02:43:24 -07001653static struct platform_driver viu_of_platform_driver = {
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001654 .probe = viu_of_probe,
1655 .remove = __devexit_p(viu_of_remove),
1656#ifdef CONFIG_PM
1657 .suspend = viu_suspend,
1658 .resume = viu_resume,
1659#endif
1660 .driver = {
1661 .name = DRV_NAME,
1662 .owner = THIS_MODULE,
1663 .of_match_table = mpc512x_viu_of_match,
1664 },
1665};
1666
1667static int __init viu_init(void)
1668{
Grant Likely1c48a5c2011-02-17 02:43:24 -07001669 return platform_driver_register(&viu_of_platform_driver);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001670}
1671
1672static void __exit viu_exit(void)
1673{
Grant Likely1c48a5c2011-02-17 02:43:24 -07001674 platform_driver_unregister(&viu_of_platform_driver);
Anatolij Gustschin95c5d602010-07-02 10:10:09 -03001675}
1676
1677module_init(viu_init);
1678module_exit(viu_exit);
1679
1680MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1681MODULE_AUTHOR("Hongjun Chen");
1682MODULE_LICENSE("GPL");