tw68-video.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * tw68 functions to handle video data
  3. *
  4. * Much of this code is derived from the cx88 and sa7134 drivers, which
  5. * were in turn derived from the bt87x driver. The original work was by
  6. * Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
  7. * Hans Verkuil, Andy Walls and many others. Their work is gratefully
  8. * acknowledged. Full credit goes to them - any problems within this code
  9. * are mine.
  10. *
  11. * Copyright (C) 2009 William M. Brack
  12. *
  13. * Refactored and updated to the latest v4l core frameworks:
  14. *
  15. * Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. */
  27. #include <linux/module.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/v4l2-event.h>
  30. #include <media/videobuf2-dma-sg.h>
  31. #include "tw68.h"
  32. #include "tw68-reg.h"
  33. /* ------------------------------------------------------------------ */
  34. /* data structs for video */
  35. /*
  36. * FIXME -
  37. * Note that the saa7134 has formats, e.g. YUV420, which are classified
  38. * as "planar". These affect overlay mode, and are flagged with a field
  39. * ".planar" in the format. Do we need to implement this in this driver?
  40. */
  41. static const struct tw68_format formats[] = {
  42. {
  43. .name = "15 bpp RGB, le",
  44. .fourcc = V4L2_PIX_FMT_RGB555,
  45. .depth = 16,
  46. .twformat = ColorFormatRGB15,
  47. }, {
  48. .name = "15 bpp RGB, be",
  49. .fourcc = V4L2_PIX_FMT_RGB555X,
  50. .depth = 16,
  51. .twformat = ColorFormatRGB15 | ColorFormatBSWAP,
  52. }, {
  53. .name = "16 bpp RGB, le",
  54. .fourcc = V4L2_PIX_FMT_RGB565,
  55. .depth = 16,
  56. .twformat = ColorFormatRGB16,
  57. }, {
  58. .name = "16 bpp RGB, be",
  59. .fourcc = V4L2_PIX_FMT_RGB565X,
  60. .depth = 16,
  61. .twformat = ColorFormatRGB16 | ColorFormatBSWAP,
  62. }, {
  63. .name = "24 bpp RGB, le",
  64. .fourcc = V4L2_PIX_FMT_BGR24,
  65. .depth = 24,
  66. .twformat = ColorFormatRGB24,
  67. }, {
  68. .name = "24 bpp RGB, be",
  69. .fourcc = V4L2_PIX_FMT_RGB24,
  70. .depth = 24,
  71. .twformat = ColorFormatRGB24 | ColorFormatBSWAP,
  72. }, {
  73. .name = "32 bpp RGB, le",
  74. .fourcc = V4L2_PIX_FMT_BGR32,
  75. .depth = 32,
  76. .twformat = ColorFormatRGB32,
  77. }, {
  78. .name = "32 bpp RGB, be",
  79. .fourcc = V4L2_PIX_FMT_RGB32,
  80. .depth = 32,
  81. .twformat = ColorFormatRGB32 | ColorFormatBSWAP |
  82. ColorFormatWSWAP,
  83. }, {
  84. .name = "4:2:2 packed, YUYV",
  85. .fourcc = V4L2_PIX_FMT_YUYV,
  86. .depth = 16,
  87. .twformat = ColorFormatYUY2,
  88. }, {
  89. .name = "4:2:2 packed, UYVY",
  90. .fourcc = V4L2_PIX_FMT_UYVY,
  91. .depth = 16,
  92. .twformat = ColorFormatYUY2 | ColorFormatBSWAP,
  93. }
  94. };
  95. #define FORMATS ARRAY_SIZE(formats)
  96. #define NORM_625_50 \
  97. .h_delay = 3, \
  98. .h_delay0 = 133, \
  99. .h_start = 0, \
  100. .h_stop = 719, \
  101. .v_delay = 24, \
  102. .vbi_v_start_0 = 7, \
  103. .vbi_v_stop_0 = 22, \
  104. .video_v_start = 24, \
  105. .video_v_stop = 311, \
  106. .vbi_v_start_1 = 319
  107. #define NORM_525_60 \
  108. .h_delay = 8, \
  109. .h_delay0 = 138, \
  110. .h_start = 0, \
  111. .h_stop = 719, \
  112. .v_delay = 22, \
  113. .vbi_v_start_0 = 10, \
  114. .vbi_v_stop_0 = 21, \
  115. .video_v_start = 22, \
  116. .video_v_stop = 262, \
  117. .vbi_v_start_1 = 273
  118. /*
  119. * The following table is searched by tw68_s_std, first for a specific
  120. * match, then for an entry which contains the desired id. The table
  121. * entries should therefore be ordered in ascending order of specificity.
  122. */
  123. static const struct tw68_tvnorm tvnorms[] = {
  124. {
  125. .name = "PAL", /* autodetect */
  126. .id = V4L2_STD_PAL,
  127. NORM_625_50,
  128. .sync_control = 0x18,
  129. .luma_control = 0x40,
  130. .chroma_ctrl1 = 0x81,
  131. .chroma_gain = 0x2a,
  132. .chroma_ctrl2 = 0x06,
  133. .vgate_misc = 0x1c,
  134. .format = VideoFormatPALBDGHI,
  135. }, {
  136. .name = "NTSC",
  137. .id = V4L2_STD_NTSC,
  138. NORM_525_60,
  139. .sync_control = 0x59,
  140. .luma_control = 0x40,
  141. .chroma_ctrl1 = 0x89,
  142. .chroma_gain = 0x2a,
  143. .chroma_ctrl2 = 0x0e,
  144. .vgate_misc = 0x18,
  145. .format = VideoFormatNTSC,
  146. }, {
  147. .name = "SECAM",
  148. .id = V4L2_STD_SECAM,
  149. NORM_625_50,
  150. .sync_control = 0x18,
  151. .luma_control = 0x1b,
  152. .chroma_ctrl1 = 0xd1,
  153. .chroma_gain = 0x80,
  154. .chroma_ctrl2 = 0x00,
  155. .vgate_misc = 0x1c,
  156. .format = VideoFormatSECAM,
  157. }, {
  158. .name = "PAL-M",
  159. .id = V4L2_STD_PAL_M,
  160. NORM_525_60,
  161. .sync_control = 0x59,
  162. .luma_control = 0x40,
  163. .chroma_ctrl1 = 0xb9,
  164. .chroma_gain = 0x2a,
  165. .chroma_ctrl2 = 0x0e,
  166. .vgate_misc = 0x18,
  167. .format = VideoFormatPALM,
  168. }, {
  169. .name = "PAL-Nc",
  170. .id = V4L2_STD_PAL_Nc,
  171. NORM_625_50,
  172. .sync_control = 0x18,
  173. .luma_control = 0x40,
  174. .chroma_ctrl1 = 0xa1,
  175. .chroma_gain = 0x2a,
  176. .chroma_ctrl2 = 0x06,
  177. .vgate_misc = 0x1c,
  178. .format = VideoFormatPALNC,
  179. }, {
  180. .name = "PAL-60",
  181. .id = V4L2_STD_PAL_60,
  182. .h_delay = 186,
  183. .h_start = 0,
  184. .h_stop = 719,
  185. .v_delay = 26,
  186. .video_v_start = 23,
  187. .video_v_stop = 262,
  188. .vbi_v_start_0 = 10,
  189. .vbi_v_stop_0 = 21,
  190. .vbi_v_start_1 = 273,
  191. .sync_control = 0x18,
  192. .luma_control = 0x40,
  193. .chroma_ctrl1 = 0x81,
  194. .chroma_gain = 0x2a,
  195. .chroma_ctrl2 = 0x06,
  196. .vgate_misc = 0x1c,
  197. .format = VideoFormatPAL60,
  198. }
  199. };
  200. #define TVNORMS ARRAY_SIZE(tvnorms)
  201. static const struct tw68_format *format_by_fourcc(unsigned int fourcc)
  202. {
  203. unsigned int i;
  204. for (i = 0; i < FORMATS; i++)
  205. if (formats[i].fourcc == fourcc)
  206. return formats+i;
  207. return NULL;
  208. }
  209. /* ------------------------------------------------------------------ */
  210. /*
  211. * Note that the cropping rectangles are described in terms of a single
  212. * frame, i.e. line positions are only 1/2 the interlaced equivalent
  213. */
  214. static void set_tvnorm(struct tw68_dev *dev, const struct tw68_tvnorm *norm)
  215. {
  216. if (norm != dev->tvnorm) {
  217. dev->width = 720;
  218. dev->height = (norm->id & V4L2_STD_525_60) ? 480 : 576;
  219. dev->tvnorm = norm;
  220. tw68_set_tvnorm_hw(dev);
  221. }
  222. }
  223. /*
  224. * tw68_set_scale
  225. *
  226. * Scaling and Cropping for video decoding
  227. *
  228. * We are working with 3 values for horizontal and vertical - scale,
  229. * delay and active.
  230. *
  231. * HACTIVE represent the actual number of pixels in the "usable" image,
  232. * before scaling. HDELAY represents the number of pixels skipped
  233. * between the start of the horizontal sync and the start of the image.
  234. * HSCALE is calculated using the formula
  235. * HSCALE = (HACTIVE / (#pixels desired)) * 256
  236. *
  237. * The vertical registers are similar, except based upon the total number
  238. * of lines in the image, and the first line of the image (i.e. ignoring
  239. * vertical sync and VBI).
  240. *
  241. * Note that the number of bytes reaching the FIFO (and hence needing
  242. * to be processed by the DMAP program) is completely dependent upon
  243. * these values, especially HSCALE.
  244. *
  245. * Parameters:
  246. * @dev pointer to the device structure, needed for
  247. * getting current norm (as well as debug print)
  248. * @width actual image width (from user buffer)
  249. * @height actual image height
  250. * @field indicates Top, Bottom or Interlaced
  251. */
  252. static int tw68_set_scale(struct tw68_dev *dev, unsigned int width,
  253. unsigned int height, enum v4l2_field field)
  254. {
  255. const struct tw68_tvnorm *norm = dev->tvnorm;
  256. /* set individually for debugging clarity */
  257. int hactive, hdelay, hscale;
  258. int vactive, vdelay, vscale;
  259. int comb;
  260. if (V4L2_FIELD_HAS_BOTH(field)) /* if field is interlaced */
  261. height /= 2; /* we must set for 1-frame */
  262. pr_debug("%s: width=%d, height=%d, both=%d\n"
  263. " tvnorm h_delay=%d, h_start=%d, h_stop=%d, "
  264. "v_delay=%d, v_start=%d, v_stop=%d\n" , __func__,
  265. width, height, V4L2_FIELD_HAS_BOTH(field),
  266. norm->h_delay, norm->h_start, norm->h_stop,
  267. norm->v_delay, norm->video_v_start,
  268. norm->video_v_stop);
  269. switch (dev->vdecoder) {
  270. case TW6800:
  271. hdelay = norm->h_delay0;
  272. break;
  273. default:
  274. hdelay = norm->h_delay;
  275. break;
  276. }
  277. hdelay += norm->h_start;
  278. hactive = norm->h_stop - norm->h_start + 1;
  279. hscale = (hactive * 256) / (width);
  280. vdelay = norm->v_delay;
  281. vactive = ((norm->id & V4L2_STD_525_60) ? 524 : 624) / 2 - norm->video_v_start;
  282. vscale = (vactive * 256) / height;
  283. pr_debug("%s: %dx%d [%s%s,%s]\n", __func__,
  284. width, height,
  285. V4L2_FIELD_HAS_TOP(field) ? "T" : "",
  286. V4L2_FIELD_HAS_BOTTOM(field) ? "B" : "",
  287. v4l2_norm_to_name(dev->tvnorm->id));
  288. pr_debug("%s: hactive=%d, hdelay=%d, hscale=%d; "
  289. "vactive=%d, vdelay=%d, vscale=%d\n", __func__,
  290. hactive, hdelay, hscale, vactive, vdelay, vscale);
  291. comb = ((vdelay & 0x300) >> 2) |
  292. ((vactive & 0x300) >> 4) |
  293. ((hdelay & 0x300) >> 6) |
  294. ((hactive & 0x300) >> 8);
  295. pr_debug("%s: setting CROP_HI=%02x, VDELAY_LO=%02x, "
  296. "VACTIVE_LO=%02x, HDELAY_LO=%02x, HACTIVE_LO=%02x\n",
  297. __func__, comb, vdelay, vactive, hdelay, hactive);
  298. tw_writeb(TW68_CROP_HI, comb);
  299. tw_writeb(TW68_VDELAY_LO, vdelay & 0xff);
  300. tw_writeb(TW68_VACTIVE_LO, vactive & 0xff);
  301. tw_writeb(TW68_HDELAY_LO, hdelay & 0xff);
  302. tw_writeb(TW68_HACTIVE_LO, hactive & 0xff);
  303. comb = ((vscale & 0xf00) >> 4) | ((hscale & 0xf00) >> 8);
  304. pr_debug("%s: setting SCALE_HI=%02x, VSCALE_LO=%02x, "
  305. "HSCALE_LO=%02x\n", __func__, comb, vscale, hscale);
  306. tw_writeb(TW68_SCALE_HI, comb);
  307. tw_writeb(TW68_VSCALE_LO, vscale);
  308. tw_writeb(TW68_HSCALE_LO, hscale);
  309. return 0;
  310. }
  311. /* ------------------------------------------------------------------ */
  312. int tw68_video_start_dma(struct tw68_dev *dev, struct tw68_buf *buf)
  313. {
  314. /* Set cropping and scaling */
  315. tw68_set_scale(dev, dev->width, dev->height, dev->field);
  316. /*
  317. * Set start address for RISC program. Note that if the DMAP
  318. * processor is currently running, it must be stopped before
  319. * a new address can be set.
  320. */
  321. tw_clearl(TW68_DMAC, TW68_DMAP_EN);
  322. tw_writel(TW68_DMAP_SA, buf->dma);
  323. /* Clear any pending interrupts */
  324. tw_writel(TW68_INTSTAT, dev->board_virqmask);
  325. /* Enable the risc engine and the fifo */
  326. tw_andorl(TW68_DMAC, 0xff, dev->fmt->twformat |
  327. ColorFormatGamma | TW68_DMAP_EN | TW68_FIFO_EN);
  328. dev->pci_irqmask |= dev->board_virqmask;
  329. tw_setl(TW68_INTMASK, dev->pci_irqmask);
  330. return 0;
  331. }
  332. /* ------------------------------------------------------------------ */
  333. /* calc max # of buffers from size (must not exceed the 4MB virtual
  334. * address space per DMA channel) */
  335. static int tw68_buffer_count(unsigned int size, unsigned int count)
  336. {
  337. unsigned int maxcount;
  338. maxcount = (4 * 1024 * 1024) / roundup(size, PAGE_SIZE);
  339. if (count > maxcount)
  340. count = maxcount;
  341. return count;
  342. }
  343. /* ------------------------------------------------------------- */
  344. /* vb2 queue operations */
  345. static int tw68_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
  346. unsigned int *num_buffers, unsigned int *num_planes,
  347. unsigned int sizes[], void *alloc_ctxs[])
  348. {
  349. struct tw68_dev *dev = vb2_get_drv_priv(q);
  350. unsigned tot_bufs = q->num_buffers + *num_buffers;
  351. sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
  352. /*
  353. * We allow create_bufs, but only if the sizeimage is the same as the
  354. * current sizeimage. The tw68_buffer_count calculation becomes quite
  355. * difficult otherwise.
  356. */
  357. if (fmt && fmt->fmt.pix.sizeimage < sizes[0])
  358. return -EINVAL;
  359. *num_planes = 1;
  360. if (tot_bufs < 2)
  361. tot_bufs = 2;
  362. tot_bufs = tw68_buffer_count(sizes[0], tot_bufs);
  363. *num_buffers = tot_bufs - q->num_buffers;
  364. return 0;
  365. }
  366. /*
  367. * The risc program for each buffers works as follows: it starts with a simple
  368. * 'JUMP to addr + 8', which is effectively a NOP. Then the program to DMA the
  369. * buffer follows and at the end we have a JUMP back to the start + 8 (skipping
  370. * the initial JUMP).
  371. *
  372. * This is the program of the first buffer to be queued if the active list is
  373. * empty and it just keeps DMAing this buffer without generating any interrupts.
  374. *
  375. * If a new buffer is added then the initial JUMP in the program generates an
  376. * interrupt as well which signals that the previous buffer has been DMAed
  377. * successfully and that it can be returned to userspace.
  378. *
  379. * It also sets the final jump of the previous buffer to the start of the new
  380. * buffer, thus chaining the new buffer into the DMA chain. This is a single
  381. * atomic u32 write, so there is no race condition.
  382. *
  383. * The end-result of all this that you only get an interrupt when a buffer
  384. * is ready, so the control flow is very easy.
  385. */
  386. static void tw68_buf_queue(struct vb2_buffer *vb)
  387. {
  388. struct vb2_queue *vq = vb->vb2_queue;
  389. struct tw68_dev *dev = vb2_get_drv_priv(vq);
  390. struct tw68_buf *buf = container_of(vb, struct tw68_buf, vb);
  391. struct tw68_buf *prev;
  392. unsigned long flags;
  393. spin_lock_irqsave(&dev->slock, flags);
  394. /* append a 'JUMP to start of buffer' to the buffer risc program */
  395. buf->jmp[0] = cpu_to_le32(RISC_JUMP);
  396. buf->jmp[1] = cpu_to_le32(buf->dma + 8);
  397. if (!list_empty(&dev->active)) {
  398. prev = list_entry(dev->active.prev, struct tw68_buf, list);
  399. buf->cpu[0] |= cpu_to_le32(RISC_INT_BIT);
  400. prev->jmp[1] = cpu_to_le32(buf->dma);
  401. }
  402. list_add_tail(&buf->list, &dev->active);
  403. spin_unlock_irqrestore(&dev->slock, flags);
  404. }
  405. /*
  406. * buffer_prepare
  407. *
  408. * Set the ancilliary information into the buffer structure. This
  409. * includes generating the necessary risc program if it hasn't already
  410. * been done for the current buffer format.
  411. * The structure fh contains the details of the format requested by the
  412. * user - type, width, height and #fields. This is compared with the
  413. * last format set for the current buffer. If they differ, the risc
  414. * code (which controls the filling of the buffer) is (re-)generated.
  415. */
  416. static int tw68_buf_prepare(struct vb2_buffer *vb)
  417. {
  418. struct vb2_queue *vq = vb->vb2_queue;
  419. struct tw68_dev *dev = vb2_get_drv_priv(vq);
  420. struct tw68_buf *buf = container_of(vb, struct tw68_buf, vb);
  421. struct sg_table *dma = vb2_dma_sg_plane_desc(vb, 0);
  422. unsigned size, bpl;
  423. int rc;
  424. size = (dev->width * dev->height * dev->fmt->depth) >> 3;
  425. if (vb2_plane_size(vb, 0) < size)
  426. return -EINVAL;
  427. vb2_set_plane_payload(vb, 0, size);
  428. rc = dma_map_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
  429. if (!rc)
  430. return -EIO;
  431. bpl = (dev->width * dev->fmt->depth) >> 3;
  432. switch (dev->field) {
  433. case V4L2_FIELD_TOP:
  434. tw68_risc_buffer(dev->pci, buf, dma->sgl,
  435. 0, UNSET, bpl, 0, dev->height);
  436. break;
  437. case V4L2_FIELD_BOTTOM:
  438. tw68_risc_buffer(dev->pci, buf, dma->sgl,
  439. UNSET, 0, bpl, 0, dev->height);
  440. break;
  441. case V4L2_FIELD_SEQ_TB:
  442. tw68_risc_buffer(dev->pci, buf, dma->sgl,
  443. 0, bpl * (dev->height >> 1),
  444. bpl, 0, dev->height >> 1);
  445. break;
  446. case V4L2_FIELD_SEQ_BT:
  447. tw68_risc_buffer(dev->pci, buf, dma->sgl,
  448. bpl * (dev->height >> 1), 0,
  449. bpl, 0, dev->height >> 1);
  450. break;
  451. case V4L2_FIELD_INTERLACED:
  452. default:
  453. tw68_risc_buffer(dev->pci, buf, dma->sgl,
  454. 0, bpl, bpl, bpl, dev->height >> 1);
  455. break;
  456. }
  457. return 0;
  458. }
  459. static void tw68_buf_finish(struct vb2_buffer *vb)
  460. {
  461. struct vb2_queue *vq = vb->vb2_queue;
  462. struct tw68_dev *dev = vb2_get_drv_priv(vq);
  463. struct sg_table *dma = vb2_dma_sg_plane_desc(vb, 0);
  464. struct tw68_buf *buf = container_of(vb, struct tw68_buf, vb);
  465. dma_unmap_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
  466. pci_free_consistent(dev->pci, buf->size, buf->cpu, buf->dma);
  467. }
  468. static int tw68_start_streaming(struct vb2_queue *q, unsigned int count)
  469. {
  470. struct tw68_dev *dev = vb2_get_drv_priv(q);
  471. struct tw68_buf *buf =
  472. container_of(dev->active.next, struct tw68_buf, list);
  473. dev->seqnr = 0;
  474. tw68_video_start_dma(dev, buf);
  475. return 0;
  476. }
  477. static void tw68_stop_streaming(struct vb2_queue *q)
  478. {
  479. struct tw68_dev *dev = vb2_get_drv_priv(q);
  480. /* Stop risc & fifo */
  481. tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
  482. while (!list_empty(&dev->active)) {
  483. struct tw68_buf *buf =
  484. container_of(dev->active.next, struct tw68_buf, list);
  485. list_del(&buf->list);
  486. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  487. }
  488. }
  489. static struct vb2_ops tw68_video_qops = {
  490. .queue_setup = tw68_queue_setup,
  491. .buf_queue = tw68_buf_queue,
  492. .buf_prepare = tw68_buf_prepare,
  493. .buf_finish = tw68_buf_finish,
  494. .start_streaming = tw68_start_streaming,
  495. .stop_streaming = tw68_stop_streaming,
  496. .wait_prepare = vb2_ops_wait_prepare,
  497. .wait_finish = vb2_ops_wait_finish,
  498. };
  499. /* ------------------------------------------------------------------ */
  500. static int tw68_s_ctrl(struct v4l2_ctrl *ctrl)
  501. {
  502. struct tw68_dev *dev =
  503. container_of(ctrl->handler, struct tw68_dev, hdl);
  504. switch (ctrl->id) {
  505. case V4L2_CID_BRIGHTNESS:
  506. tw_writeb(TW68_BRIGHT, ctrl->val);
  507. break;
  508. case V4L2_CID_HUE:
  509. tw_writeb(TW68_HUE, ctrl->val);
  510. break;
  511. case V4L2_CID_CONTRAST:
  512. tw_writeb(TW68_CONTRAST, ctrl->val);
  513. break;
  514. case V4L2_CID_SATURATION:
  515. tw_writeb(TW68_SAT_U, ctrl->val);
  516. tw_writeb(TW68_SAT_V, ctrl->val);
  517. break;
  518. case V4L2_CID_COLOR_KILLER:
  519. if (ctrl->val)
  520. tw_andorb(TW68_MISC2, 0xe0, 0xe0);
  521. else
  522. tw_andorb(TW68_MISC2, 0xe0, 0x00);
  523. break;
  524. case V4L2_CID_CHROMA_AGC:
  525. if (ctrl->val)
  526. tw_andorb(TW68_LOOP, 0x30, 0x20);
  527. else
  528. tw_andorb(TW68_LOOP, 0x30, 0x00);
  529. break;
  530. }
  531. return 0;
  532. }
  533. /* ------------------------------------------------------------------ */
  534. /*
  535. * Note that this routine returns what is stored in the fh structure, and
  536. * does not interrogate any of the device registers.
  537. */
  538. static int tw68_g_fmt_vid_cap(struct file *file, void *priv,
  539. struct v4l2_format *f)
  540. {
  541. struct tw68_dev *dev = video_drvdata(file);
  542. f->fmt.pix.width = dev->width;
  543. f->fmt.pix.height = dev->height;
  544. f->fmt.pix.field = dev->field;
  545. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  546. f->fmt.pix.bytesperline =
  547. (f->fmt.pix.width * (dev->fmt->depth)) >> 3;
  548. f->fmt.pix.sizeimage =
  549. f->fmt.pix.height * f->fmt.pix.bytesperline;
  550. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  551. f->fmt.pix.priv = 0;
  552. return 0;
  553. }
  554. static int tw68_try_fmt_vid_cap(struct file *file, void *priv,
  555. struct v4l2_format *f)
  556. {
  557. struct tw68_dev *dev = video_drvdata(file);
  558. const struct tw68_format *fmt;
  559. enum v4l2_field field;
  560. unsigned int maxh;
  561. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  562. if (NULL == fmt)
  563. return -EINVAL;
  564. field = f->fmt.pix.field;
  565. maxh = (dev->tvnorm->id & V4L2_STD_525_60) ? 480 : 576;
  566. switch (field) {
  567. case V4L2_FIELD_TOP:
  568. case V4L2_FIELD_BOTTOM:
  569. break;
  570. case V4L2_FIELD_INTERLACED:
  571. case V4L2_FIELD_SEQ_BT:
  572. case V4L2_FIELD_SEQ_TB:
  573. maxh = maxh * 2;
  574. break;
  575. default:
  576. field = (f->fmt.pix.height > maxh / 2)
  577. ? V4L2_FIELD_INTERLACED
  578. : V4L2_FIELD_BOTTOM;
  579. break;
  580. }
  581. f->fmt.pix.field = field;
  582. if (f->fmt.pix.width < 48)
  583. f->fmt.pix.width = 48;
  584. if (f->fmt.pix.height < 32)
  585. f->fmt.pix.height = 32;
  586. if (f->fmt.pix.width > 720)
  587. f->fmt.pix.width = 720;
  588. if (f->fmt.pix.height > maxh)
  589. f->fmt.pix.height = maxh;
  590. f->fmt.pix.width &= ~0x03;
  591. f->fmt.pix.bytesperline =
  592. (f->fmt.pix.width * (fmt->depth)) >> 3;
  593. f->fmt.pix.sizeimage =
  594. f->fmt.pix.height * f->fmt.pix.bytesperline;
  595. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  596. return 0;
  597. }
  598. /*
  599. * Note that tw68_s_fmt_vid_cap sets the information into the fh structure,
  600. * and it will be used for all future new buffers. However, there could be
  601. * some number of buffers on the "active" chain which will be filled before
  602. * the change takes place.
  603. */
  604. static int tw68_s_fmt_vid_cap(struct file *file, void *priv,
  605. struct v4l2_format *f)
  606. {
  607. struct tw68_dev *dev = video_drvdata(file);
  608. int err;
  609. err = tw68_try_fmt_vid_cap(file, priv, f);
  610. if (0 != err)
  611. return err;
  612. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  613. dev->width = f->fmt.pix.width;
  614. dev->height = f->fmt.pix.height;
  615. dev->field = f->fmt.pix.field;
  616. return 0;
  617. }
  618. static int tw68_enum_input(struct file *file, void *priv,
  619. struct v4l2_input *i)
  620. {
  621. struct tw68_dev *dev = video_drvdata(file);
  622. unsigned int n;
  623. n = i->index;
  624. if (n >= TW68_INPUT_MAX)
  625. return -EINVAL;
  626. i->index = n;
  627. i->type = V4L2_INPUT_TYPE_CAMERA;
  628. snprintf(i->name, sizeof(i->name), "Composite %d", n);
  629. /* If the query is for the current input, get live data */
  630. if (n == dev->input) {
  631. int v1 = tw_readb(TW68_STATUS1);
  632. int v2 = tw_readb(TW68_MVSN);
  633. if (0 != (v1 & (1 << 7)))
  634. i->status |= V4L2_IN_ST_NO_SYNC;
  635. if (0 != (v1 & (1 << 6)))
  636. i->status |= V4L2_IN_ST_NO_H_LOCK;
  637. if (0 != (v1 & (1 << 2)))
  638. i->status |= V4L2_IN_ST_NO_SIGNAL;
  639. if (0 != (v1 & 1 << 1))
  640. i->status |= V4L2_IN_ST_NO_COLOR;
  641. if (0 != (v2 & (1 << 2)))
  642. i->status |= V4L2_IN_ST_MACROVISION;
  643. }
  644. i->std = video_devdata(file)->tvnorms;
  645. return 0;
  646. }
  647. static int tw68_g_input(struct file *file, void *priv, unsigned int *i)
  648. {
  649. struct tw68_dev *dev = video_drvdata(file);
  650. *i = dev->input;
  651. return 0;
  652. }
  653. static int tw68_s_input(struct file *file, void *priv, unsigned int i)
  654. {
  655. struct tw68_dev *dev = video_drvdata(file);
  656. if (i >= TW68_INPUT_MAX)
  657. return -EINVAL;
  658. dev->input = i;
  659. tw_andorb(TW68_INFORM, 0x03 << 2, dev->input << 2);
  660. return 0;
  661. }
  662. static int tw68_querycap(struct file *file, void *priv,
  663. struct v4l2_capability *cap)
  664. {
  665. struct tw68_dev *dev = video_drvdata(file);
  666. strcpy(cap->driver, "tw68");
  667. strlcpy(cap->card, "Techwell Capture Card",
  668. sizeof(cap->card));
  669. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  670. cap->device_caps =
  671. V4L2_CAP_VIDEO_CAPTURE |
  672. V4L2_CAP_READWRITE |
  673. V4L2_CAP_STREAMING;
  674. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  675. return 0;
  676. }
  677. static int tw68_s_std(struct file *file, void *priv, v4l2_std_id id)
  678. {
  679. struct tw68_dev *dev = video_drvdata(file);
  680. unsigned int i;
  681. if (vb2_is_busy(&dev->vidq))
  682. return -EBUSY;
  683. /* Look for match on complete norm id (may have mult bits) */
  684. for (i = 0; i < TVNORMS; i++) {
  685. if (id == tvnorms[i].id)
  686. break;
  687. }
  688. /* If no exact match, look for norm which contains this one */
  689. if (i == TVNORMS) {
  690. for (i = 0; i < TVNORMS; i++)
  691. if (id & tvnorms[i].id)
  692. break;
  693. }
  694. /* If still not matched, give up */
  695. if (i == TVNORMS)
  696. return -EINVAL;
  697. set_tvnorm(dev, &tvnorms[i]); /* do the actual setting */
  698. return 0;
  699. }
  700. static int tw68_g_std(struct file *file, void *priv, v4l2_std_id *id)
  701. {
  702. struct tw68_dev *dev = video_drvdata(file);
  703. *id = dev->tvnorm->id;
  704. return 0;
  705. }
  706. static int tw68_enum_fmt_vid_cap(struct file *file, void *priv,
  707. struct v4l2_fmtdesc *f)
  708. {
  709. if (f->index >= FORMATS)
  710. return -EINVAL;
  711. strlcpy(f->description, formats[f->index].name,
  712. sizeof(f->description));
  713. f->pixelformat = formats[f->index].fourcc;
  714. return 0;
  715. }
  716. /*
  717. * Used strictly for internal development and debugging, this routine
  718. * prints out the current register contents for the tw68xx device.
  719. */
  720. static void tw68_dump_regs(struct tw68_dev *dev)
  721. {
  722. unsigned char line[80];
  723. int i, j, k;
  724. unsigned char *cptr;
  725. pr_info("Full dump of TW68 registers:\n");
  726. /* First we do the PCI regs, 8 4-byte regs per line */
  727. for (i = 0; i < 0x100; i += 32) {
  728. cptr = line;
  729. cptr += sprintf(cptr, "%03x ", i);
  730. /* j steps through the next 4 words */
  731. for (j = i; j < i + 16; j += 4)
  732. cptr += sprintf(cptr, "%08x ", tw_readl(j));
  733. *cptr++ = ' ';
  734. for (; j < i + 32; j += 4)
  735. cptr += sprintf(cptr, "%08x ", tw_readl(j));
  736. *cptr++ = '\n';
  737. *cptr = 0;
  738. pr_info("%s", line);
  739. }
  740. /* Next the control regs, which are single-byte, address mod 4 */
  741. while (i < 0x400) {
  742. cptr = line;
  743. cptr += sprintf(cptr, "%03x ", i);
  744. /* Print out 4 groups of 4 bytes */
  745. for (j = 0; j < 4; j++) {
  746. for (k = 0; k < 4; k++) {
  747. cptr += sprintf(cptr, "%02x ",
  748. tw_readb(i));
  749. i += 4;
  750. }
  751. *cptr++ = ' ';
  752. }
  753. *cptr++ = '\n';
  754. *cptr = 0;
  755. pr_info("%s", line);
  756. }
  757. }
  758. static int vidioc_log_status(struct file *file, void *priv)
  759. {
  760. struct tw68_dev *dev = video_drvdata(file);
  761. tw68_dump_regs(dev);
  762. return v4l2_ctrl_log_status(file, priv);
  763. }
  764. #ifdef CONFIG_VIDEO_ADV_DEBUG
  765. static int vidioc_g_register(struct file *file, void *priv,
  766. struct v4l2_dbg_register *reg)
  767. {
  768. struct tw68_dev *dev = video_drvdata(file);
  769. if (reg->size == 1)
  770. reg->val = tw_readb(reg->reg);
  771. else
  772. reg->val = tw_readl(reg->reg);
  773. return 0;
  774. }
  775. static int vidioc_s_register(struct file *file, void *priv,
  776. const struct v4l2_dbg_register *reg)
  777. {
  778. struct tw68_dev *dev = video_drvdata(file);
  779. if (reg->size == 1)
  780. tw_writeb(reg->reg, reg->val);
  781. else
  782. tw_writel(reg->reg & 0xffff, reg->val);
  783. return 0;
  784. }
  785. #endif
  786. static const struct v4l2_ctrl_ops tw68_ctrl_ops = {
  787. .s_ctrl = tw68_s_ctrl,
  788. };
  789. static const struct v4l2_file_operations video_fops = {
  790. .owner = THIS_MODULE,
  791. .open = v4l2_fh_open,
  792. .release = vb2_fop_release,
  793. .read = vb2_fop_read,
  794. .poll = vb2_fop_poll,
  795. .mmap = vb2_fop_mmap,
  796. .unlocked_ioctl = video_ioctl2,
  797. };
  798. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  799. .vidioc_querycap = tw68_querycap,
  800. .vidioc_enum_fmt_vid_cap = tw68_enum_fmt_vid_cap,
  801. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  802. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  803. .vidioc_querybuf = vb2_ioctl_querybuf,
  804. .vidioc_qbuf = vb2_ioctl_qbuf,
  805. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  806. .vidioc_s_std = tw68_s_std,
  807. .vidioc_g_std = tw68_g_std,
  808. .vidioc_enum_input = tw68_enum_input,
  809. .vidioc_g_input = tw68_g_input,
  810. .vidioc_s_input = tw68_s_input,
  811. .vidioc_streamon = vb2_ioctl_streamon,
  812. .vidioc_streamoff = vb2_ioctl_streamoff,
  813. .vidioc_g_fmt_vid_cap = tw68_g_fmt_vid_cap,
  814. .vidioc_try_fmt_vid_cap = tw68_try_fmt_vid_cap,
  815. .vidioc_s_fmt_vid_cap = tw68_s_fmt_vid_cap,
  816. .vidioc_log_status = vidioc_log_status,
  817. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  818. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  819. #ifdef CONFIG_VIDEO_ADV_DEBUG
  820. .vidioc_g_register = vidioc_g_register,
  821. .vidioc_s_register = vidioc_s_register,
  822. #endif
  823. };
  824. static struct video_device tw68_video_template = {
  825. .name = "tw68_video",
  826. .fops = &video_fops,
  827. .ioctl_ops = &video_ioctl_ops,
  828. .release = video_device_release_empty,
  829. .tvnorms = TW68_NORMS,
  830. };
  831. /* ------------------------------------------------------------------ */
  832. /* exported stuff */
  833. void tw68_set_tvnorm_hw(struct tw68_dev *dev)
  834. {
  835. tw_andorb(TW68_SDT, 0x07, dev->tvnorm->format);
  836. }
  837. int tw68_video_init1(struct tw68_dev *dev)
  838. {
  839. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  840. v4l2_ctrl_handler_init(hdl, 6);
  841. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  842. V4L2_CID_BRIGHTNESS, -128, 127, 1, 20);
  843. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  844. V4L2_CID_CONTRAST, 0, 255, 1, 100);
  845. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  846. V4L2_CID_SATURATION, 0, 255, 1, 128);
  847. /* NTSC only */
  848. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  849. V4L2_CID_HUE, -128, 127, 1, 0);
  850. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  851. V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
  852. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  853. V4L2_CID_CHROMA_AGC, 0, 1, 1, 1);
  854. if (hdl->error) {
  855. v4l2_ctrl_handler_free(hdl);
  856. return hdl->error;
  857. }
  858. dev->v4l2_dev.ctrl_handler = hdl;
  859. v4l2_ctrl_handler_setup(hdl);
  860. return 0;
  861. }
  862. int tw68_video_init2(struct tw68_dev *dev, int video_nr)
  863. {
  864. int ret;
  865. set_tvnorm(dev, &tvnorms[0]);
  866. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  867. dev->width = 720;
  868. dev->height = 576;
  869. dev->field = V4L2_FIELD_INTERLACED;
  870. INIT_LIST_HEAD(&dev->active);
  871. dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  872. dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  873. dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
  874. dev->vidq.ops = &tw68_video_qops;
  875. dev->vidq.mem_ops = &vb2_dma_sg_memops;
  876. dev->vidq.drv_priv = dev;
  877. dev->vidq.gfp_flags = __GFP_DMA32;
  878. dev->vidq.buf_struct_size = sizeof(struct tw68_buf);
  879. dev->vidq.lock = &dev->lock;
  880. dev->vidq.min_buffers_needed = 2;
  881. ret = vb2_queue_init(&dev->vidq);
  882. if (ret)
  883. return ret;
  884. dev->vdev = tw68_video_template;
  885. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  886. dev->vdev.lock = &dev->lock;
  887. dev->vdev.queue = &dev->vidq;
  888. video_set_drvdata(&dev->vdev, dev);
  889. return video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr);
  890. }
  891. /*
  892. * tw68_irq_video_done
  893. */
  894. void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status)
  895. {
  896. __u32 reg;
  897. /* reset interrupts handled by this routine */
  898. tw_writel(TW68_INTSTAT, status);
  899. /*
  900. * Check most likely first
  901. *
  902. * DMAPI shows we have reached the end of the risc code
  903. * for the current buffer.
  904. */
  905. if (status & TW68_DMAPI) {
  906. struct tw68_buf *buf;
  907. spin_lock(&dev->slock);
  908. buf = list_entry(dev->active.next, struct tw68_buf, list);
  909. list_del(&buf->list);
  910. spin_unlock(&dev->slock);
  911. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  912. buf->vb.v4l2_buf.field = dev->field;
  913. buf->vb.v4l2_buf.sequence = dev->seqnr++;
  914. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  915. status &= ~(TW68_DMAPI);
  916. if (0 == status)
  917. return;
  918. }
  919. if (status & (TW68_VLOCK | TW68_HLOCK))
  920. dev_dbg(&dev->pci->dev, "Lost sync\n");
  921. if (status & TW68_PABORT)
  922. dev_err(&dev->pci->dev, "PABORT interrupt\n");
  923. if (status & TW68_DMAPERR)
  924. dev_err(&dev->pci->dev, "DMAPERR interrupt\n");
  925. /*
  926. * On TW6800, FDMIS is apparently generated if video input is switched
  927. * during operation. Therefore, it is not enabled for that chip.
  928. */
  929. if (status & TW68_FDMIS)
  930. dev_dbg(&dev->pci->dev, "FDMIS interrupt\n");
  931. if (status & TW68_FFOF) {
  932. /* probably a logic error */
  933. reg = tw_readl(TW68_DMAC) & TW68_FIFO_EN;
  934. tw_clearl(TW68_DMAC, TW68_FIFO_EN);
  935. dev_dbg(&dev->pci->dev, "FFOF interrupt\n");
  936. tw_setl(TW68_DMAC, reg);
  937. }
  938. if (status & TW68_FFERR)
  939. dev_dbg(&dev->pci->dev, "FFERR interrupt\n");
  940. }