fsl-viu.c 40 KB

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