fsl-viu.c 40 KB

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