fsl-viu.c 39 KB

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