fsl-viu.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623
  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_irq;
  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_irq;
  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_irq;
  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_irq;
  1208. }
  1209. ad = i2c_get_adapter(0);
  1210. if (!ad) {
  1211. ret = -EFAULT;
  1212. dev_err(&op->dev, "couldn't get i2c adapter\n");
  1213. goto err_v4l2;
  1214. }
  1215. v4l2_ctrl_handler_init(&viu_dev->hdl, 5);
  1216. if (viu_dev->hdl.error) {
  1217. ret = viu_dev->hdl.error;
  1218. dev_err(&op->dev, "couldn't register control\n");
  1219. goto err_i2c;
  1220. }
  1221. /* This control handler will inherit the control(s) from the
  1222. sub-device(s). */
  1223. viu_dev->v4l2_dev.ctrl_handler = &viu_dev->hdl;
  1224. viu_dev->decoder = v4l2_i2c_new_subdev(&viu_dev->v4l2_dev, ad,
  1225. "saa7113", VIU_VIDEO_DECODER_ADDR, NULL);
  1226. viu_dev->vidq.timeout.function = viu_vid_timeout;
  1227. viu_dev->vidq.timeout.data = (unsigned long)viu_dev;
  1228. init_timer(&viu_dev->vidq.timeout);
  1229. viu_dev->std = V4L2_STD_NTSC_M;
  1230. viu_dev->first = 1;
  1231. /* Allocate memory for video device */
  1232. vdev = video_device_alloc();
  1233. if (vdev == NULL) {
  1234. ret = -ENOMEM;
  1235. goto err_hdl;
  1236. }
  1237. *vdev = viu_template;
  1238. vdev->v4l2_dev = &viu_dev->v4l2_dev;
  1239. viu_dev->vdev = vdev;
  1240. /* initialize locks */
  1241. mutex_init(&viu_dev->lock);
  1242. viu_dev->vdev->lock = &viu_dev->lock;
  1243. spin_lock_init(&viu_dev->slock);
  1244. video_set_drvdata(viu_dev->vdev, viu_dev);
  1245. mutex_lock(&viu_dev->lock);
  1246. ret = video_register_device(viu_dev->vdev, VFL_TYPE_GRABBER, -1);
  1247. if (ret < 0) {
  1248. video_device_release(viu_dev->vdev);
  1249. goto err_unlock;
  1250. }
  1251. /* enable VIU clock */
  1252. clk = devm_clk_get(&op->dev, "ipg");
  1253. if (IS_ERR(clk)) {
  1254. dev_err(&op->dev, "failed to lookup the clock!\n");
  1255. ret = PTR_ERR(clk);
  1256. goto err_vdev;
  1257. }
  1258. ret = clk_prepare_enable(clk);
  1259. if (ret) {
  1260. dev_err(&op->dev, "failed to enable the clock!\n");
  1261. goto err_vdev;
  1262. }
  1263. viu_dev->clk = clk;
  1264. /* reset VIU module */
  1265. viu_reset(viu_dev->vr);
  1266. /* install interrupt handler */
  1267. if (request_irq(viu_dev->irq, viu_intr, 0, "viu", (void *)viu_dev)) {
  1268. dev_err(&op->dev, "Request VIU IRQ failed.\n");
  1269. ret = -ENODEV;
  1270. goto err_clk;
  1271. }
  1272. mutex_unlock(&viu_dev->lock);
  1273. dev_info(&op->dev, "Freescale VIU Video Capture Board\n");
  1274. return ret;
  1275. err_clk:
  1276. clk_disable_unprepare(viu_dev->clk);
  1277. err_vdev:
  1278. video_unregister_device(viu_dev->vdev);
  1279. err_unlock:
  1280. mutex_unlock(&viu_dev->lock);
  1281. err_hdl:
  1282. v4l2_ctrl_handler_free(&viu_dev->hdl);
  1283. err_i2c:
  1284. i2c_put_adapter(ad);
  1285. err_v4l2:
  1286. v4l2_device_unregister(&viu_dev->v4l2_dev);
  1287. err_irq:
  1288. irq_dispose_mapping(viu_irq);
  1289. return ret;
  1290. }
  1291. static int viu_of_remove(struct platform_device *op)
  1292. {
  1293. struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
  1294. struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
  1295. struct v4l2_subdev *sdev = list_entry(v4l2_dev->subdevs.next,
  1296. struct v4l2_subdev, list);
  1297. struct i2c_client *client = v4l2_get_subdevdata(sdev);
  1298. free_irq(dev->irq, (void *)dev);
  1299. irq_dispose_mapping(dev->irq);
  1300. clk_disable_unprepare(dev->clk);
  1301. v4l2_ctrl_handler_free(&dev->hdl);
  1302. video_unregister_device(dev->vdev);
  1303. i2c_put_adapter(client->adapter);
  1304. v4l2_device_unregister(&dev->v4l2_dev);
  1305. return 0;
  1306. }
  1307. #ifdef CONFIG_PM
  1308. static int viu_suspend(struct platform_device *op, pm_message_t state)
  1309. {
  1310. struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
  1311. struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
  1312. clk_disable(dev->clk);
  1313. return 0;
  1314. }
  1315. static int viu_resume(struct platform_device *op)
  1316. {
  1317. struct v4l2_device *v4l2_dev = dev_get_drvdata(&op->dev);
  1318. struct viu_dev *dev = container_of(v4l2_dev, struct viu_dev, v4l2_dev);
  1319. clk_enable(dev->clk);
  1320. return 0;
  1321. }
  1322. #endif
  1323. /*
  1324. * Initialization and module stuff
  1325. */
  1326. static const struct of_device_id mpc512x_viu_of_match[] = {
  1327. {
  1328. .compatible = "fsl,mpc5121-viu",
  1329. },
  1330. {},
  1331. };
  1332. MODULE_DEVICE_TABLE(of, mpc512x_viu_of_match);
  1333. static struct platform_driver viu_of_platform_driver = {
  1334. .probe = viu_of_probe,
  1335. .remove = viu_of_remove,
  1336. #ifdef CONFIG_PM
  1337. .suspend = viu_suspend,
  1338. .resume = viu_resume,
  1339. #endif
  1340. .driver = {
  1341. .name = DRV_NAME,
  1342. .of_match_table = mpc512x_viu_of_match,
  1343. },
  1344. };
  1345. module_platform_driver(viu_of_platform_driver);
  1346. MODULE_DESCRIPTION("Freescale Video-In(VIU)");
  1347. MODULE_AUTHOR("Hongjun Chen");
  1348. MODULE_LICENSE("GPL");
  1349. MODULE_VERSION(VIU_VERSION);