fsl-viu.c 39 KB

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