1 /*
2 * Copyright 2008-2010 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * Freescale VIU video driver
5 *
6 * Authors: Hongjun Chen <hong-jun.chen@freescale.com>
7 * Porting to 2.6.35 by DENX Software Engineering,
8 * Anatolij Gustschin <agust@denx.de>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 */
16
17 #include <linux/module.h>
18 #include <linux/clk.h>
19 #include <linux/kernel.h>
20 #include <linux/i2c.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/of_platform.h>
25 #include <linux/slab.h>
26 #include <linux/version.h>
27 #include <media/v4l2-common.h>
28 #include <media/v4l2-device.h>
29 #include <media/v4l2-ioctl.h>
30 #include <media/videobuf-dma-contig.h>
31
32 #define DRV_NAME "fsl_viu"
33 #define VIU_MAJOR_VERSION 0
34 #define VIU_MINOR_VERSION 5
35 #define VIU_RELEASE 0
36 #define VIU_VERSION KERNEL_VERSION(VIU_MAJOR_VERSION, \
37 VIU_MINOR_VERSION, \
38 VIU_RELEASE)
39
40 #define BUFFER_TIMEOUT msecs_to_jiffies(500) /* 0.5 seconds */
41
42 #define VIU_VID_MEM_LIMIT 4 /* Video memory limit, in Mb */
43
44 /* I2C address of video decoder chip is 0x4A */
45 #define VIU_VIDEO_DECODER_ADDR 0x25
46
47 /* supported controls */
48 static struct v4l2_queryctrl viu_qctrl[] = {
49 {
50 .id = V4L2_CID_BRIGHTNESS,
51 .type = V4L2_CTRL_TYPE_INTEGER,
52 .name = "Brightness",
53 .minimum = 0,
54 .maximum = 255,
55 .step = 1,
56 .default_value = 127,
57 .flags = 0,
58 }, {
59 .id = V4L2_CID_CONTRAST,
60 .type = V4L2_CTRL_TYPE_INTEGER,
61 .name = "Contrast",
62 .minimum = 0,
63 .maximum = 255,
64 .step = 0x1,
65 .default_value = 0x10,
66 .flags = 0,
67 }, {
68 .id = V4L2_CID_SATURATION,
69 .type = V4L2_CTRL_TYPE_INTEGER,
70 .name = "Saturation",
71 .minimum = 0,
72 .maximum = 255,
73 .step = 0x1,
74 .default_value = 127,
75 .flags = 0,
76 }, {
77 .id = V4L2_CID_HUE,
78 .type = V4L2_CTRL_TYPE_INTEGER,
79 .name = "Hue",
80 .minimum = -128,
81 .maximum = 127,
82 .step = 0x1,
83 .default_value = 0,
84 .flags = 0,
85 }
86 };
87
88 static int qctl_regs[ARRAY_SIZE(viu_qctrl)];
89
90 static int info_level;
91
92 #define dprintk(level, fmt, arg...) \
93 do { \
94 if (level <= info_level) \
95 printk(KERN_DEBUG "viu: " fmt , ## arg); \
96 } while (0)
97
98 /*
99 * Basic structures
100 */
101 struct viu_fmt {
102 char name[32];
103 u32 fourcc; /* v4l2 format id */
104 u32 pixelformat;
105 int depth;
106 };
107
108 static struct viu_fmt formats[] = {
109 {
110 .name = "RGB-16 (5/B-6/G-5/R)",
111 .fourcc = V4L2_PIX_FMT_RGB565,
112 .pixelformat = V4L2_PIX_FMT_RGB565,
113 .depth = 16,
114 }, {
115 .name = "RGB-32 (A-R-G-B)",
116 .fourcc = V4L2_PIX_FMT_RGB32,
117 .pixelformat = V4L2_PIX_FMT_RGB32,
118 .depth = 32,
119 }
120 };
121
122 struct viu_dev;
123 struct viu_buf;
124
125 /* buffer for one video frame */
126 struct viu_buf {
127 /* common v4l buffer stuff -- must be first */
128 struct videobuf_buffer vb;
129 struct viu_fmt *fmt;
130 };
131
132 struct viu_dmaqueue {
133 struct viu_dev *dev;
134 struct list_head active;
135 struct list_head queued;
136 struct timer_list timeout;
137 };
138
139 struct viu_status {
140 u32 field_irq;
141 u32 vsync_irq;
142 u32 hsync_irq;
143 u32 vstart_irq;
144 u32 dma_end_irq;
145 u32 error_irq;
146 };
147
148 struct viu_reg {
149 u32 status_cfg;
150 u32 luminance;
151 u32 chroma_r;
152 u32 chroma_g;
153 u32 chroma_b;
154 u32 field_base_addr;
155 u32 dma_inc;
156 u32 picture_count;
157 u32 req_alarm;
158 u32 alpha;
159 } __attribute__ ((packed));
160
161 struct viu_dev {
162 struct v4l2_device v4l2_dev;
163 struct mutex lock;
164 spinlock_t slock;
165 int users;
166
167 struct device *dev;
168 /* various device info */
169 struct video_device *vdev;
170 struct viu_dmaqueue vidq;
171 enum v4l2_field capfield;
172 int field;
173 int first;
174 int dma_done;
175
176 /* Hardware register area */
177 struct viu_reg *vr;
178
179 /* Interrupt vector */
180 int irq;
181 struct viu_status irqs;
182
183 /* video overlay */
184 struct v4l2_framebuffer ovbuf;
185 struct viu_fmt *ovfmt;
186 unsigned int ovenable;
187 enum v4l2_field ovfield;
188
189 /* crop */
190 struct v4l2_rect crop_current;
191
192 /* clock pointer */
193 struct clk *clk;
194
195 /* decoder */
196 struct v4l2_subdev *decoder;
197
198 v4l2_std_id std;
199 };
200
201 struct 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
218 static struct viu_reg reg_val;
219
220 /*
221 * Macro definitions of VIU registers
222 */
223
224 /* STATUS_CONFIG register */
225 enum 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
274 static irqreturn_t viu_intr(int irq, void *dev_id);
275
format_by_fourcc(int fourcc)276 struct 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
viu_start_dma(struct viu_dev * dev)289 void 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
viu_stop_dma(struct viu_dev * dev)300 void 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
restart_video_queue(struct viu_dmaqueue * vidq)333 static 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
viu_vid_timeout(unsigned long data)388 static 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 */
buffer_setup(struct videobuf_queue * vq,unsigned int * count,unsigned int * size)408 static 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
free_buffer(struct videobuf_queue * vq,struct viu_buf * buf)424 static 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
431 videobuf_waiton(vq, &buf->vb, 0, 0);
432
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
buffer_activate(struct viu_dev * dev,struct viu_buf * buf)442 inline 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
buffer_prepare(struct videobuf_queue * vq,struct videobuf_buffer * vb,enum v4l2_field field)491 static 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
532 fail:
533 free_buffer(vq, buf);
534 return rc;
535 }
536
buffer_queue(struct videobuf_queue * vq,struct videobuf_buffer * vb)537 static 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
buffer_release(struct videobuf_queue * vq,struct videobuf_buffer * vb)587 static 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
598 static 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 */
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)608 static 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
vidioc_enum_fmt(struct file * file,void * priv,struct v4l2_fmtdesc * f)621 static 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
vidioc_g_fmt_cap(struct file * file,void * priv,struct v4l2_format * f)634 static 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
vidioc_try_fmt_cap(struct file * file,void * priv,struct v4l2_format * f)649 static 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
vidioc_s_fmt_cap(struct file * file,void * priv,struct v4l2_format * f)691 static 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
vidioc_g_fmt_overlay(struct file * file,void * priv,struct v4l2_format * f)711 static 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
verify_preview(struct viu_dev * dev,struct v4l2_window * win)720 static 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
viu_activate_overlay(struct viu_reg * viu_reg)760 inline 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
viu_start_preview(struct viu_dev * dev,struct viu_fh * fh)769 static int viu_start_preview(struct viu_dev *dev, struct viu_fh *fh)
770 {
771 int bpp;
772
773 dprintk(1, "%s %dx%d %s\n", __func__,
774 fh->win.w.width, fh->win.w.height, dev->ovfmt->name);
775
776 reg_val.status_cfg = 0;
777
778 /* setup window */
779 reg_val.picture_count = (fh->win.w.height / 2) << 16 |
780 fh->win.w.width;
781
782 /* setup color depth and dma increment */
783 bpp = dev->ovfmt->depth / 8;
784 switch (bpp) {
785 case 2:
786 reg_val.status_cfg &= ~MODE_32BIT;
787 reg_val.dma_inc = fh->win.w.width * 2;
788 break;
789 case 4:
790 reg_val.status_cfg |= MODE_32BIT;
791 reg_val.dma_inc = fh->win.w.width * 4;
792 break;
793 default:
794 dprintk(0, "device doesn't support color depth(%d)\n",
795 bpp * 8);
796 return -EINVAL;
797 }
798
799 dev->ovfield = fh->win.field;
800 if (!V4L2_FIELD_HAS_BOTH(dev->ovfield))
801 reg_val.dma_inc = 0;
802
803 reg_val.status_cfg |= DMA_ACT | INT_DMA_END_EN | INT_FIELD_EN;
804
805 /* setup the base address of the overlay buffer */
806 reg_val.field_base_addr = (u32)dev->ovbuf.base;
807
808 dev->ovenable = 1;
809 viu_activate_overlay(dev->vr);
810
811 /* start dma */
812 viu_start_dma(dev);
813 return 0;
814 }
815
vidioc_s_fmt_overlay(struct file * file,void * priv,struct v4l2_format * f)816 static int vidioc_s_fmt_overlay(struct file *file, void *priv,
817 struct v4l2_format *f)
818 {
819 struct viu_fh *fh = priv;
820 struct viu_dev *dev = (struct viu_dev *)fh->dev;
821 unsigned long flags;
822 int err;
823
824 err = verify_preview(dev, &f->fmt.win);
825 if (err)
826 return err;
827
828 mutex_lock(&dev->lock);
829 fh->win = f->fmt.win;
830
831 spin_lock_irqsave(&dev->slock, flags);
832 viu_start_preview(dev, fh);
833 spin_unlock_irqrestore(&dev->slock, flags);
834 mutex_unlock(&dev->lock);
835 return 0;
836 }
837
vidioc_try_fmt_overlay(struct file * file,void * priv,struct v4l2_format * f)838 static int vidioc_try_fmt_overlay(struct file *file, void *priv,
839 struct v4l2_format *f)
840 {
841 return 0;
842 }
843
vidioc_g_fbuf(struct file * file,void * priv,struct v4l2_framebuffer * arg)844 int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
845 {
846 struct viu_fh *fh = priv;
847 struct viu_dev *dev = fh->dev;
848 struct v4l2_framebuffer *fb = arg;
849
850 *fb = dev->ovbuf;
851 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
852 return 0;
853 }
854
vidioc_s_fbuf(struct file * file,void * priv,struct v4l2_framebuffer * arg)855 int vidioc_s_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *arg)
856 {
857 struct viu_fh *fh = priv;
858 struct viu_dev *dev = fh->dev;
859 struct v4l2_framebuffer *fb = arg;
860 struct viu_fmt *fmt;
861
862 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
863 return -EPERM;
864
865 /* check args */
866 fmt = format_by_fourcc(fb->fmt.pixelformat);
867 if (fmt == NULL)
868 return -EINVAL;
869
870 /* ok, accept it */
871 dev->ovbuf = *fb;
872 dev->ovfmt = fmt;
873 if (dev->ovbuf.fmt.bytesperline == 0) {
874 dev->ovbuf.fmt.bytesperline =
875 dev->ovbuf.fmt.width * fmt->depth / 8;
876 }
877 return 0;
878 }
879
vidioc_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * p)880 static int vidioc_reqbufs(struct file *file, void *priv,
881 struct v4l2_requestbuffers *p)
882 {
883 struct viu_fh *fh = priv;
884
885 return videobuf_reqbufs(&fh->vb_vidq, p);
886 }
887
vidioc_querybuf(struct file * file,void * priv,struct v4l2_buffer * p)888 static int vidioc_querybuf(struct file *file, void *priv,
889 struct v4l2_buffer *p)
890 {
891 struct viu_fh *fh = priv;
892
893 return videobuf_querybuf(&fh->vb_vidq, p);
894 }
895
vidioc_qbuf(struct file * file,void * priv,struct v4l2_buffer * p)896 static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
897 {
898 struct viu_fh *fh = priv;
899
900 return videobuf_qbuf(&fh->vb_vidq, p);
901 }
902
vidioc_dqbuf(struct file * file,void * priv,struct v4l2_buffer * p)903 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
904 {
905 struct viu_fh *fh = priv;
906
907 return videobuf_dqbuf(&fh->vb_vidq, p,
908 file->f_flags & O_NONBLOCK);
909 }
910
vidioc_streamon(struct file * file,void * priv,enum v4l2_buf_type i)911 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
912 {
913 struct viu_fh *fh = priv;
914
915 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
916 return -EINVAL;
917 if (fh->type != i)
918 return -EINVAL;
919
920 viu_start_dma(fh->dev);
921
922 return videobuf_streamon(&fh->vb_vidq);
923 }
924
vidioc_streamoff(struct file * file,void * priv,enum v4l2_buf_type i)925 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
926 {
927 struct viu_fh *fh = priv;
928
929 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
930 return -EINVAL;
931 if (fh->type != i)
932 return -EINVAL;
933
934 viu_stop_dma(fh->dev);
935
936 return videobuf_streamoff(&fh->vb_vidq);
937 }
938
939 #define decoder_call(viu, o, f, args...) \
940 v4l2_subdev_call(viu->decoder, o, f, ##args)
941
vidioc_querystd(struct file * file,void * priv,v4l2_std_id * std_id)942 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
943 {
944 struct viu_fh *fh = priv;
945
946 decoder_call(fh->dev, video, querystd, std_id);
947 return 0;
948 }
949
vidioc_s_std(struct file * file,void * priv,v4l2_std_id * id)950 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
951 {
952 struct viu_fh *fh = priv;
953
954 fh->dev->std = *id;
955 decoder_call(fh->dev, core, s_std, *id);
956 return 0;
957 }
958
vidioc_g_std(struct file * file,void * priv,v4l2_std_id * std_id)959 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std_id)
960 {
961 struct viu_fh *fh = priv;
962
963 *std_id = fh->dev->std;
964 return 0;
965 }
966
967 /* only one input in this driver */
vidioc_enum_input(struct file * file,void * priv,struct v4l2_input * inp)968 static int vidioc_enum_input(struct file *file, void *priv,
969 struct v4l2_input *inp)
970 {
971 struct viu_fh *fh = priv;
972
973 if (inp->index != 0)
974 return -EINVAL;
975
976 inp->type = V4L2_INPUT_TYPE_CAMERA;
977 inp->std = fh->dev->vdev->tvnorms;
978 strcpy(inp->name, "Camera");
979 return 0;
980 }
981
vidioc_g_input(struct file * file,void * priv,unsigned int * i)982 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
983 {
984 *i = 0;
985 return 0;
986 }
987
vidioc_s_input(struct file * file,void * priv,unsigned int i)988 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
989 {
990 struct viu_fh *fh = priv;
991
992 if (i > 1)
993 return -EINVAL;
994
995 decoder_call(fh->dev, video, s_routing, i, 0, 0);
996 return 0;
997 }
998
999 /* Controls */
vidioc_queryctrl(struct file * file,void * priv,struct v4l2_queryctrl * qc)1000 static int vidioc_queryctrl(struct file *file, void *priv,
1001 struct v4l2_queryctrl *qc)
1002 {
1003 int i;
1004
1005 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1006 if (qc->id && qc->id == viu_qctrl[i].id) {
1007 memcpy(qc, &(viu_qctrl[i]), sizeof(*qc));
1008 return 0;
1009 }
1010 }
1011 return -EINVAL;
1012 }
1013
vidioc_g_ctrl(struct file * file,void * priv,struct v4l2_control * ctrl)1014 static int vidioc_g_ctrl(struct file *file, void *priv,
1015 struct v4l2_control *ctrl)
1016 {
1017 int i;
1018
1019 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1020 if (ctrl->id == viu_qctrl[i].id) {
1021 ctrl->value = qctl_regs[i];
1022 return 0;
1023 }
1024 }
1025 return -EINVAL;
1026 }
vidioc_s_ctrl(struct file * file,void * priv,struct v4l2_control * ctrl)1027 static int vidioc_s_ctrl(struct file *file, void *priv,
1028 struct v4l2_control *ctrl)
1029 {
1030 int i;
1031
1032 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++) {
1033 if (ctrl->id == viu_qctrl[i].id) {
1034 if (ctrl->value < viu_qctrl[i].minimum
1035 || ctrl->value > viu_qctrl[i].maximum)
1036 return -ERANGE;
1037 qctl_regs[i] = ctrl->value;
1038 return 0;
1039 }
1040 }
1041 return -EINVAL;
1042 }
1043
viu_activate_next_buf(struct viu_dev * dev,struct viu_dmaqueue * viuq)1044 inline void viu_activate_next_buf(struct viu_dev *dev,
1045 struct viu_dmaqueue *viuq)
1046 {
1047 struct viu_dmaqueue *vidq = viuq;
1048 struct viu_buf *buf;
1049
1050 /* launch another DMA operation for an active/queued buffer */
1051 if (!list_empty(&vidq->active)) {
1052 buf = list_entry(vidq->active.next, struct viu_buf,
1053 vb.queue);
1054 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1055 buffer_activate(dev, buf);
1056 } else if (!list_empty(&vidq->queued)) {
1057 buf = list_entry(vidq->queued.next, struct viu_buf,
1058 vb.queue);
1059 list_del(&buf->vb.queue);
1060
1061 dprintk(1, "start another queued buffer: 0x%p\n", buf);
1062 list_add_tail(&buf->vb.queue, &vidq->active);
1063 buf->vb.state = VIDEOBUF_ACTIVE;
1064 buffer_activate(dev, buf);
1065 }
1066 }
1067
viu_default_settings(struct viu_reg * viu_reg)1068 inline void viu_default_settings(struct viu_reg *viu_reg)
1069 {
1070 struct viu_reg *vr = viu_reg;
1071
1072 out_be32(&vr->luminance, 0x9512A254);
1073 out_be32(&vr->chroma_r, 0x03310000);
1074 out_be32(&vr->chroma_g, 0x06600F38);
1075 out_be32(&vr->chroma_b, 0x00000409);
1076 out_be32(&vr->alpha, 0x000000ff);
1077 out_be32(&vr->req_alarm, 0x00000090);
1078 dprintk(1, "status reg: 0x%08x, field base: 0x%08x\n",
1079 in_be32(&vr->status_cfg), in_be32(&vr->field_base_addr));
1080 }
1081
viu_overlay_intr(struct viu_dev * dev,u32 status)1082 static void viu_overlay_intr(struct viu_dev *dev, u32 status)
1083 {
1084 struct viu_reg *vr = dev->vr;
1085
1086 if (status & INT_DMA_END_STATUS)
1087 dev->dma_done = 1;
1088
1089 if (status & INT_FIELD_STATUS) {
1090 if (dev->dma_done) {
1091 u32 addr = reg_val.field_base_addr;
1092
1093 dev->dma_done = 0;
1094 if (status & FIELD_NO)
1095 addr += reg_val.dma_inc;
1096
1097 out_be32(&vr->field_base_addr, addr);
1098 out_be32(&vr->dma_inc, reg_val.dma_inc);
1099 out_be32(&vr->status_cfg,
1100 (status & 0xffc0ffff) |
1101 (status & INT_ALL_STATUS) |
1102 reg_val.status_cfg);
1103 } else if (status & INT_VSYNC_STATUS) {
1104 out_be32(&vr->status_cfg,
1105 (status & 0xffc0ffff) |
1106 (status & INT_ALL_STATUS) |
1107 reg_val.status_cfg);
1108 }
1109 }
1110 }
1111
viu_capture_intr(struct viu_dev * dev,u32 status)1112 static void viu_capture_intr(struct viu_dev *dev, u32 status)
1113 {
1114 struct viu_dmaqueue *vidq = &dev->vidq;
1115 struct viu_reg *vr = dev->vr;
1116 struct viu_buf *buf;
1117 int field_num;
1118 int need_two;
1119 int dma_done = 0;
1120
1121 field_num = status & FIELD_NO;
1122 need_two = V4L2_FIELD_HAS_BOTH(dev->capfield);
1123
1124 if (status & INT_DMA_END_STATUS) {
1125 dma_done = 1;
1126 if (((field_num == 0) && (dev->field == 0)) ||
1127 (field_num && (dev->field == 1)))
1128 dev->field++;
1129 }
1130
1131 if (status & INT_FIELD_STATUS) {
1132 dprintk(1, "irq: field %d, done %d\n",
1133 !!field_num, dma_done);
1134 if (unlikely(dev->first)) {
1135 if (field_num == 0) {
1136 dev->first = 0;
1137 dprintk(1, "activate first buf\n");
1138 viu_activate_next_buf(dev, vidq);
1139 } else
1140 dprintk(1, "wait field 0\n");
1141 return;
1142 }
1143
1144 /* setup buffer address for next dma operation */
1145 if (!list_empty(&vidq->active)) {
1146 u32 addr = reg_val.field_base_addr;
1147
1148 if (field_num && need_two) {
1149 addr += reg_val.dma_inc;
1150 dprintk(1, "field 1, 0x%lx, dev field %d\n",
1151 (unsigned long)addr, dev->field);
1152 }
1153 out_be32(&vr->field_base_addr, addr);
1154 out_be32(&vr->dma_inc, reg_val.dma_inc);
1155 out_be32(&vr->status_cfg,
1156 (status & 0xffc0ffff) |
1157 (status & INT_ALL_STATUS) |
1158 reg_val.status_cfg);
1159 return;
1160 }
1161 }
1162
1163 if (dma_done && field_num && (dev->field == 2)) {
1164 dev->field = 0;
1165 buf = list_entry(vidq->active.next,
1166 struct viu_buf, vb.queue);
1167 dprintk(1, "viu/0: [%p/%d] 0x%lx/0x%lx: dma complete\n",
1168 buf, buf->vb.i,
1169 (unsigned long)videobuf_to_dma_contig(&buf->vb),
1170 (unsigned long)in_be32(&vr->field_base_addr));
1171
1172 if (waitqueue_active(&buf->vb.done)) {
1173 list_del(&buf->vb.queue);
1174 do_gettimeofday(&buf->vb.ts);
1175 buf->vb.state = VIDEOBUF_DONE;
1176 buf->vb.field_count++;
1177 wake_up(&buf->vb.done);
1178 }
1179 /* activate next dma buffer */
1180 viu_activate_next_buf(dev, vidq);
1181 }
1182 }
1183
viu_intr(int irq,void * dev_id)1184 static irqreturn_t viu_intr(int irq, void *dev_id)
1185 {
1186 struct viu_dev *dev = (struct viu_dev *)dev_id;
1187 struct viu_reg *vr = dev->vr;
1188 u32 status;
1189 u32 error;
1190
1191 status = in_be32(&vr->status_cfg);
1192
1193 if (status & INT_ERROR_STATUS) {
1194 dev->irqs.error_irq++;
1195 error = status & ERR_MASK;
1196 if (error)
1197 dprintk(1, "Err: error(%d), times:%d!\n",
1198 error >> 4, dev->irqs.error_irq);
1199 /* Clear interrupt error bit and error flags */
1200 out_be32(&vr->status_cfg,
1201 (status & 0xffc0ffff) | INT_ERROR_STATUS);
1202 }
1203
1204 if (status & INT_DMA_END_STATUS) {
1205 dev->irqs.dma_end_irq++;
1206 dev->dma_done = 1;
1207 dprintk(2, "VIU DMA end interrupt times: %d\n",
1208 dev->irqs.dma_end_irq);
1209 }
1210
1211 if (status & INT_HSYNC_STATUS)
1212 dev->irqs.hsync_irq++;
1213
1214 if (status & INT_FIELD_STATUS) {
1215 dev->irqs.field_irq++;
1216 dprintk(2, "VIU field interrupt times: %d\n",
1217 dev->irqs.field_irq);
1218 }
1219
1220 if (status & INT_VSTART_STATUS)
1221 dev->irqs.vstart_irq++;
1222
1223 if (status & INT_VSYNC_STATUS) {
1224 dev->irqs.vsync_irq++;
1225 dprintk(2, "VIU vsync interrupt times: %d\n",
1226 dev->irqs.vsync_irq);
1227 }
1228
1229 /* clear all pending irqs */
1230 status = in_be32(&vr->status_cfg);
1231 out_be32(&vr->status_cfg,
1232 (status & 0xffc0ffff) | (status & INT_ALL_STATUS));
1233
1234 if (dev->ovenable) {
1235 viu_overlay_intr(dev, status);
1236 return IRQ_HANDLED;
1237 }
1238
1239 /* Capture mode */
1240 viu_capture_intr(dev, status);
1241 return IRQ_HANDLED;
1242 }
1243
1244 /*
1245 * File operations for the device
1246 */
viu_open(struct file * file)1247 static int viu_open(struct file *file)
1248 {
1249 struct video_device *vdev = video_devdata(file);
1250 struct viu_dev *dev = video_get_drvdata(vdev);
1251 struct viu_fh *fh;
1252 struct viu_reg *vr;
1253 int minor = vdev->minor;
1254 u32 status_cfg;
1255 int i;
1256
1257 dprintk(1, "viu: open (minor=%d)\n", minor);
1258
1259 dev->users++;
1260 if (dev->users > 1) {
1261 dev->users--;
1262 return -EBUSY;
1263 }
1264
1265 vr = dev->vr;
1266
1267 dprintk(1, "open minor=%d type=%s users=%d\n", minor,
1268 v4l2_type_names[V4L2_BUF_TYPE_VIDEO_CAPTURE], dev->users);
1269
1270 /* allocate and initialize per filehandle data */
1271 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1272 if (!fh) {
1273 dev->users--;
1274 return -ENOMEM;
1275 }
1276
1277 file->private_data = fh;
1278 fh->dev = dev;
1279
1280 fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1281 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_RGB32);
1282 fh->width = norm_maxw();
1283 fh->height = norm_maxh();
1284 dev->crop_current.width = fh->width;
1285 dev->crop_current.height = fh->height;
1286
1287 /* Put all controls at a sane state */
1288 for (i = 0; i < ARRAY_SIZE(viu_qctrl); i++)
1289 qctl_regs[i] = viu_qctrl[i].default_value;
1290
1291 dprintk(1, "Open: fh=0x%08lx, dev=0x%08lx, dev->vidq=0x%08lx\n",
1292 (unsigned long)fh, (unsigned long)dev,
1293 (unsigned long)&dev->vidq);
1294 dprintk(1, "Open: list_empty queued=%d\n",
1295 list_empty(&dev->vidq.queued));
1296 dprintk(1, "Open: list_empty active=%d\n",
1297 list_empty(&dev->vidq.active));
1298
1299 viu_default_settings(vr);
1300
1301 status_cfg = in_be32(&vr->status_cfg);
1302 out_be32(&vr->status_cfg,
1303 status_cfg & ~(INT_VSYNC_EN | INT_HSYNC_EN |
1304 INT_FIELD_EN | INT_VSTART_EN |
1305 INT_DMA_END_EN | INT_ERROR_EN | INT_ECC_EN));
1306
1307 status_cfg = in_be32(&vr->status_cfg);
1308 out_be32(&vr->status_cfg, status_cfg | INT_ALL_STATUS);
1309
1310 spin_lock_init(&fh->vbq_lock);
1311 videobuf_queue_dma_contig_init(&fh->vb_vidq, &viu_video_qops,
1312 dev->dev, &fh->vbq_lock,
1313 fh->type, V4L2_FIELD_INTERLACED,
1314 sizeof(struct viu_buf), fh, NULL);
1315 return 0;
1316 }
1317
viu_read(struct file * file,char __user * data,size_t count,loff_t * ppos)1318 static ssize_t viu_read(struct file *file, char __user *data, size_t count,
1319 loff_t *ppos)
1320 {
1321 struct viu_fh *fh = file->private_data;
1322 struct viu_dev *dev = fh->dev;
1323 int ret = 0;
1324
1325 dprintk(2, "%s\n", __func__);
1326 if (dev->ovenable)
1327 dev->ovenable = 0;
1328
1329 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1330 viu_start_dma(dev);
1331 ret = videobuf_read_stream(&fh->vb_vidq, data, count,
1332 ppos, 0, file->f_flags & O_NONBLOCK);
1333 return ret;
1334 }
1335 return 0;
1336 }
1337
viu_poll(struct file * file,struct poll_table_struct * wait)1338 static unsigned int viu_poll(struct file *file, struct poll_table_struct *wait)
1339 {
1340 struct viu_fh *fh = file->private_data;
1341 struct videobuf_queue *q = &fh->vb_vidq;
1342
1343 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
1344 return POLLERR;
1345
1346 return videobuf_poll_stream(file, q, wait);
1347 }
1348
viu_release(struct file * file)1349 static int viu_release(struct file *file)
1350 {
1351 struct viu_fh *fh = file->private_data;
1352 struct viu_dev *dev = fh->dev;
1353 int minor = video_devdata(file)->minor;
1354
1355 viu_stop_dma(dev);
1356 videobuf_stop(&fh->vb_vidq);
1357 videobuf_mmap_free(&fh->vb_vidq);
1358
1359 kfree(fh);
1360
1361 dev->users--;
1362 dprintk(1, "close (minor=%d, users=%d)\n",
1363 minor, dev->users);
1364 return 0;
1365 }
1366
viu_reset(struct viu_reg * reg)1367 void viu_reset(struct viu_reg *reg)
1368 {
1369 out_be32(®->status_cfg, 0);
1370 out_be32(®->luminance, 0x9512a254);
1371 out_be32(®->chroma_r, 0x03310000);
1372 out_be32(®->chroma_g, 0x06600f38);
1373 out_be32(®->chroma_b, 0x00000409);
1374 out_be32(®->field_base_addr, 0);
1375 out_be32(®->dma_inc, 0);
1376 out_be32(®->picture_count, 0x01e002d0);
1377 out_be32(®->req_alarm, 0x00000090);
1378 out_be32(®->alpha, 0x000000ff);
1379 }
1380
viu_mmap(struct file * file,struct vm_area_struct * vma)1381 static int viu_mmap(struct file *file, struct vm_area_struct *vma)
1382 {
1383 struct viu_fh *fh = file->private_data;
1384 int ret;
1385
1386 dprintk(1, "mmap called, vma=0x%08lx\n", (unsigned long)vma);
1387
1388 ret = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1389
1390 dprintk(1, "vma start=0x%08lx, size=%ld, ret=%d\n",
1391 (unsigned long)vma->vm_start,
1392 (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
1393 ret);
1394
1395 return ret;
1396 }
1397
1398 static struct v4l2_file_operations viu_fops = {
1399 .owner = THIS_MODULE,
1400 .open = viu_open,
1401 .release = viu_release,
1402 .read = viu_read,
1403 .poll = viu_poll,
1404 .ioctl = video_ioctl2, /* V4L2 ioctl handler */
1405 .mmap = viu_mmap,
1406 };
1407
1408 static const struct v4l2_ioctl_ops viu_ioctl_ops = {
1409 .vidioc_querycap = vidioc_querycap,
1410 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1411 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_cap,
1412 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_cap,
1413 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_cap,
1414 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt,
1415 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_overlay,
1416 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_overlay,
1417 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_overlay,
1418 .vidioc_g_fbuf = vidioc_g_fbuf,
1419 .vidioc_s_fbuf = vidioc_s_fbuf,
1420 .vidioc_reqbufs = vidioc_reqbufs,
1421 .vidioc_querybuf = vidioc_querybuf,
1422 .vidioc_qbuf = vidioc_qbuf,
1423 .vidioc_dqbuf = vidioc_dqbuf,
1424 .vidioc_g_std = vidioc_g_std,
1425 .vidioc_s_std = vidioc_s_std,
1426 .vidioc_querystd = vidioc_querystd,
1427 .vidioc_enum_input = vidioc_enum_input,
1428 .vidioc_g_input = vidioc_g_input,
1429 .vidioc_s_input = vidioc_s_input,
1430 .vidioc_queryctrl = vidioc_queryctrl,
1431 .vidioc_g_ctrl = vidioc_g_ctrl,
1432 .vidioc_s_ctrl = vidioc_s_ctrl,
1433 .vidioc_streamon = vidioc_streamon,
1434 .vidioc_streamoff = vidioc_streamoff,
1435 };
1436
1437 static struct video_device viu_template = {
1438 .name = "FSL viu",
1439 .fops = &viu_fops,
1440 .minor = -1,
1441 .ioctl_ops = &viu_ioctl_ops,
1442 .release = video_device_release,
1443
1444 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1445 .current_norm = V4L2_STD_NTSC_M,
1446 };
1447
viu_of_probe(struct platform_device * op)1448 static int __devinit viu_of_probe(struct platform_device *op)
1449 {
1450 struct viu_dev *viu_dev;
1451 struct video_device *vdev;
1452 struct resource r;
1453 struct viu_reg __iomem *viu_regs;
1454 struct i2c_adapter *ad;
1455 int ret, viu_irq;
1456
1457 ret = of_address_to_resource(op->dev.of_node, 0, &r);
1458 if (ret) {
1459 dev_err(&op->dev, "Can't parse device node resource\n");
1460 return -ENODEV;
1461 }
1462
1463 viu_irq = irq_of_parse_and_map(op->dev.of_node, 0);
1464 if (viu_irq == NO_IRQ) {
1465 dev_err(&op->dev, "Error while mapping the irq\n");
1466 return -EINVAL;
1467 }
1468
1469 /* request mem region */
1470 if (!devm_request_mem_region(&op->dev, r.start,
1471 sizeof(struct viu_reg), DRV_NAME)) {
1472 dev_err(&op->dev, "Error while requesting mem region\n");
1473 ret = -EBUSY;
1474 goto err;
1475 }
1476
1477 /* remap registers */
1478 viu_regs = devm_ioremap(&op->dev, r.start, sizeof(struct viu_reg));
1479 if (!viu_regs) {
1480 dev_err(&op->dev, "Can't map register set\n");
1481 ret = -ENOMEM;
1482 goto err;
1483 }
1484
1485 /* Prepare our private structure */
1486 viu_dev = devm_kzalloc(&op->dev, sizeof(struct viu_dev), GFP_ATOMIC);
1487 if (!viu_dev) {
1488 dev_err(&op->dev, "Can't allocate private structure\n");
1489 ret = -ENOMEM;
1490 goto err;
1491 }
1492
1493 viu_dev->vr = viu_regs;
1494 viu_dev->irq = viu_irq;
1495 viu_dev->dev = &op->dev;
1496
1497 /* init video dma queues */
1498 INIT_LIST_HEAD(&viu_dev->vidq.active);
1499 INIT_LIST_HEAD(&viu_dev->vidq.queued);
1500
1501 /* initialize locks */
1502 mutex_init(&viu_dev->lock);
1503
1504 snprintf(viu_dev->v4l2_dev.name,
1505 sizeof(viu_dev->v4l2_dev.name), "%s", "VIU");
1506 ret = v4l2_device_register(viu_dev->dev, &viu_dev->v4l2_dev);
1507 if (ret < 0) {
1508 dev_err(&op->dev, "v4l2_device_register() failed: %d\n", ret);
1509 goto err;
1510 }
1511
1512 ad = i2c_get_adapter(0);
1513 viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
1514 "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
1515
1516 viu_dev->vidq.timeout.function = viu_vid_timeout;
1517 viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
1518 init_timer(&viu_dev->vidq.timeout);
1519 viu_dev->first = 1;
1520
1521 /* Allocate memory for video device */
1522 vdev = video_device_alloc();
1523 if (vdev == NULL) {
1524 ret = -ENOMEM;
1525 goto err_vdev;
1526 }
1527
1528 memcpy(vdev, &viu_template, sizeof(viu_template));
1529
1530 vdev->v4l2_dev = &viu_dev->v4l2_dev;
1531
1532 viu_dev->vdev = vdev;
1533
1534 video_set_drvdata(viu_dev->vdev, viu_dev);
1535
1536 ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
1537 if (ret < 0) {
1538 video_device_release(viu_dev->vdev);
1539 goto err_vdev;
1540 }
1541
1542 /* enable VIU clock */
1543 viu_dev->clk = clk_get(&op->dev, "viu_clk");
1544 if (IS_ERR(viu_dev->clk)) {
1545 dev_err(&op->dev, "failed to find the clock module!\n");
1546 ret = -ENODEV;
1547 goto err_clk;
1548 } else {
1549 clk_enable(viu_dev->clk);
1550 }
1551
1552 /* reset VIU module */
1553 viu_reset(viu_dev->vr);
1554
1555 /* install interrupt handler */
1556 if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
1557 dev_err(&op->dev, "Request VIU IRQ failed.\n");
1558 ret = -ENODEV;
1559 goto err_irq;
1560 }
1561
1562 dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
1563 return ret;
1564
1565 err_irq:
1566 clk_disable(viu_dev->clk);
1567 clk_put(viu_dev->clk);
1568 err_clk:
1569 video_unregister_device(viu_dev->vdev);
1570 err_vdev:
1571 i2c_put_adapter(ad);
1572 v4l2_device_unregister(&viu_dev->v4l2_dev);
1573 err:
1574 irq_dispose_mapping(viu_irq);
1575 return ret;
1576 }
1577
viu_of_remove(struct platform_device * op)1578 static int __devexit viu_of_remove(struct platform_device *op)
1579 {
1580 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1581 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1582 struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
1583 struct v4l2_subdev, list);
1584 struct i2c_client *client = v4l2_get_subdevdata(sdev);
1585
1586 free_irq(dev->irq, (void *)dev);
1587 irq_dispose_mapping(dev->irq);
1588
1589 clk_disable(dev->clk);
1590 clk_put(dev->clk);
1591
1592 video_unregister_device(dev->vdev);
1593 i2c_put_adapter(client->adapter);
1594 v4l2_device_unregister(&dev->v4l2_dev);
1595 return 0;
1596 }
1597
1598 #ifdef CONFIG_PM
viu_suspend(struct platform_device * op,pm_message_t state)1599 static int viu_suspend(struct platform_device *op, pm_message_t state)
1600 {
1601 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1602 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1603
1604 clk_disable(dev->clk);
1605 return 0;
1606 }
1607
viu_resume(struct platform_device * op)1608 static int viu_resume(struct platform_device *op)
1609 {
1610 struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
1611 struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
1612
1613 clk_enable(dev->clk);
1614 return 0;
1615 }
1616 #endif
1617
1618 /*
1619 * Initialization and module stuff
1620 */
1621 static struct of_device_id mpc512x_viu_of_match[] = {
1622 {
1623 .compatible = "fsl,mpc5121-viu",
1624 },
1625 {},
1626 };
1627 MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
1628
1629 static struct platform_driver viu_of_platform_driver = {
1630 .probe = viu_of_probe,
1631 .remove = __devexit_p(viu_of_remove),
1632 #ifdef CONFIG_PM
1633 .suspend = viu_suspend,
1634 .resume = viu_resume,
1635 #endif
1636 .driver = {
1637 .name = DRV_NAME,
1638 .owner = THIS_MODULE,
1639 .of_match_table = mpc512x_viu_of_match,
1640 },
1641 };
1642
viu_init(void)1643 static int __init viu_init(void)
1644 {
1645 return platform_driver_register(&viu_of_platform_driver);
1646 }
1647
viu_exit(void)1648 static void __exit viu_exit(void)
1649 {
1650 platform_driver_unregister(&viu_of_platform_driver);
1651 }
1652
1653 module_init(viu_init);
1654 module_exit(viu_exit);
1655
1656 MODULE_DESCRIPTION("Freescale Video-In(VIU)");
1657 MODULE_AUTHOR("Hongjun Chen");
1658 MODULE_LICENSE("GPL");
1659