/drivers/media/video/fsl-viu.c

https://bitbucket.org/ndreys/linux-sunxi · C · 1686 lines · 1319 code · 283 blank · 84 comment · 171 complexity · e13f22dfcbc86539af5a839ed7668f9a MD5 · raw file

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