fsl-viu.c 39 KB

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