Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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