fsl-viu.c 39 KB

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