fsl-viu.c 40 KB

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