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