fsl-viu.c 40 KB

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