tw68-video.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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 void *parg,
  346. unsigned int *num_buffers, unsigned int *num_planes,
  347. unsigned int sizes[], void *alloc_ctxs[])
  348. {
  349. const struct v4l2_format *fmt = parg;
  350. struct tw68_dev *dev = vb2_get_drv_priv(q);
  351. unsigned tot_bufs = q->num_buffers + *num_buffers;
  352. sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
  353. alloc_ctxs[0] = dev->alloc_ctx;
  354. /*
  355. * We allow create_bufs, but only if the sizeimage is the same as the
  356. * current sizeimage. The tw68_buffer_count calculation becomes quite
  357. * difficult otherwise.
  358. */
  359. if (fmt && fmt->fmt.pix.sizeimage < sizes[0])
  360. return -EINVAL;
  361. *num_planes = 1;
  362. if (tot_bufs < 2)
  363. tot_bufs = 2;
  364. tot_bufs = tw68_buffer_count(sizes[0], tot_bufs);
  365. *num_buffers = tot_bufs - q->num_buffers;
  366. return 0;
  367. }
  368. /*
  369. * The risc program for each buffers works as follows: it starts with a simple
  370. * 'JUMP to addr + 8', which is effectively a NOP. Then the program to DMA the
  371. * buffer follows and at the end we have a JUMP back to the start + 8 (skipping
  372. * the initial JUMP).
  373. *
  374. * This is the program of the first buffer to be queued if the active list is
  375. * empty and it just keeps DMAing this buffer without generating any interrupts.
  376. *
  377. * If a new buffer is added then the initial JUMP in the program generates an
  378. * interrupt as well which signals that the previous buffer has been DMAed
  379. * successfully and that it can be returned to userspace.
  380. *
  381. * It also sets the final jump of the previous buffer to the start of the new
  382. * buffer, thus chaining the new buffer into the DMA chain. This is a single
  383. * atomic u32 write, so there is no race condition.
  384. *
  385. * The end-result of all this that you only get an interrupt when a buffer
  386. * is ready, so the control flow is very easy.
  387. */
  388. static void tw68_buf_queue(struct vb2_buffer *vb)
  389. {
  390. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  391. struct vb2_queue *vq = vb->vb2_queue;
  392. struct tw68_dev *dev = vb2_get_drv_priv(vq);
  393. struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
  394. struct tw68_buf *prev;
  395. unsigned long flags;
  396. spin_lock_irqsave(&dev->slock, flags);
  397. /* append a 'JUMP to start of buffer' to the buffer risc program */
  398. buf->jmp[0] = cpu_to_le32(RISC_JUMP);
  399. buf->jmp[1] = cpu_to_le32(buf->dma + 8);
  400. if (!list_empty(&dev->active)) {
  401. prev = list_entry(dev->active.prev, struct tw68_buf, list);
  402. buf->cpu[0] |= cpu_to_le32(RISC_INT_BIT);
  403. prev->jmp[1] = cpu_to_le32(buf->dma);
  404. }
  405. list_add_tail(&buf->list, &dev->active);
  406. spin_unlock_irqrestore(&dev->slock, flags);
  407. }
  408. /*
  409. * buffer_prepare
  410. *
  411. * Set the ancilliary information into the buffer structure. This
  412. * includes generating the necessary risc program if it hasn't already
  413. * been done for the current buffer format.
  414. * The structure fh contains the details of the format requested by the
  415. * user - type, width, height and #fields. This is compared with the
  416. * last format set for the current buffer. If they differ, the risc
  417. * code (which controls the filling of the buffer) is (re-)generated.
  418. */
  419. static int tw68_buf_prepare(struct vb2_buffer *vb)
  420. {
  421. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  422. struct vb2_queue *vq = vb->vb2_queue;
  423. struct tw68_dev *dev = vb2_get_drv_priv(vq);
  424. struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
  425. struct sg_table *dma = vb2_dma_sg_plane_desc(vb, 0);
  426. unsigned size, bpl;
  427. size = (dev->width * dev->height * dev->fmt->depth) >> 3;
  428. if (vb2_plane_size(vb, 0) < size)
  429. return -EINVAL;
  430. vb2_set_plane_payload(vb, 0, size);
  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_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  462. struct vb2_queue *vq = vb->vb2_queue;
  463. struct tw68_dev *dev = vb2_get_drv_priv(vq);
  464. struct tw68_buf *buf = container_of(vbuf, struct tw68_buf, vb);
  465. pci_free_consistent(dev->pci, buf->size, buf->cpu, buf->dma);
  466. }
  467. static int tw68_start_streaming(struct vb2_queue *q, unsigned int count)
  468. {
  469. struct tw68_dev *dev = vb2_get_drv_priv(q);
  470. struct tw68_buf *buf =
  471. container_of(dev->active.next, struct tw68_buf, list);
  472. dev->seqnr = 0;
  473. tw68_video_start_dma(dev, buf);
  474. return 0;
  475. }
  476. static void tw68_stop_streaming(struct vb2_queue *q)
  477. {
  478. struct tw68_dev *dev = vb2_get_drv_priv(q);
  479. /* Stop risc & fifo */
  480. tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
  481. while (!list_empty(&dev->active)) {
  482. struct tw68_buf *buf =
  483. container_of(dev->active.next, struct tw68_buf, list);
  484. list_del(&buf->list);
  485. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  486. }
  487. }
  488. static struct vb2_ops tw68_video_qops = {
  489. .queue_setup = tw68_queue_setup,
  490. .buf_queue = tw68_buf_queue,
  491. .buf_prepare = tw68_buf_prepare,
  492. .buf_finish = tw68_buf_finish,
  493. .start_streaming = tw68_start_streaming,
  494. .stop_streaming = tw68_stop_streaming,
  495. .wait_prepare = vb2_ops_wait_prepare,
  496. .wait_finish = vb2_ops_wait_finish,
  497. };
  498. /* ------------------------------------------------------------------ */
  499. static int tw68_s_ctrl(struct v4l2_ctrl *ctrl)
  500. {
  501. struct tw68_dev *dev =
  502. container_of(ctrl->handler, struct tw68_dev, hdl);
  503. switch (ctrl->id) {
  504. case V4L2_CID_BRIGHTNESS:
  505. tw_writeb(TW68_BRIGHT, ctrl->val);
  506. break;
  507. case V4L2_CID_HUE:
  508. tw_writeb(TW68_HUE, ctrl->val);
  509. break;
  510. case V4L2_CID_CONTRAST:
  511. tw_writeb(TW68_CONTRAST, ctrl->val);
  512. break;
  513. case V4L2_CID_SATURATION:
  514. tw_writeb(TW68_SAT_U, ctrl->val);
  515. tw_writeb(TW68_SAT_V, ctrl->val);
  516. break;
  517. case V4L2_CID_COLOR_KILLER:
  518. if (ctrl->val)
  519. tw_andorb(TW68_MISC2, 0xe0, 0xe0);
  520. else
  521. tw_andorb(TW68_MISC2, 0xe0, 0x00);
  522. break;
  523. case V4L2_CID_CHROMA_AGC:
  524. if (ctrl->val)
  525. tw_andorb(TW68_LOOP, 0x30, 0x20);
  526. else
  527. tw_andorb(TW68_LOOP, 0x30, 0x00);
  528. break;
  529. }
  530. return 0;
  531. }
  532. /* ------------------------------------------------------------------ */
  533. /*
  534. * Note that this routine returns what is stored in the fh structure, and
  535. * does not interrogate any of the device registers.
  536. */
  537. static int tw68_g_fmt_vid_cap(struct file *file, void *priv,
  538. struct v4l2_format *f)
  539. {
  540. struct tw68_dev *dev = video_drvdata(file);
  541. f->fmt.pix.width = dev->width;
  542. f->fmt.pix.height = dev->height;
  543. f->fmt.pix.field = dev->field;
  544. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  545. f->fmt.pix.bytesperline =
  546. (f->fmt.pix.width * (dev->fmt->depth)) >> 3;
  547. f->fmt.pix.sizeimage =
  548. f->fmt.pix.height * f->fmt.pix.bytesperline;
  549. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  550. f->fmt.pix.priv = 0;
  551. return 0;
  552. }
  553. static int tw68_try_fmt_vid_cap(struct file *file, void *priv,
  554. struct v4l2_format *f)
  555. {
  556. struct tw68_dev *dev = video_drvdata(file);
  557. const struct tw68_format *fmt;
  558. enum v4l2_field field;
  559. unsigned int maxh;
  560. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  561. if (NULL == fmt)
  562. return -EINVAL;
  563. field = f->fmt.pix.field;
  564. maxh = (dev->tvnorm->id & V4L2_STD_525_60) ? 480 : 576;
  565. switch (field) {
  566. case V4L2_FIELD_TOP:
  567. case V4L2_FIELD_BOTTOM:
  568. break;
  569. case V4L2_FIELD_INTERLACED:
  570. case V4L2_FIELD_SEQ_BT:
  571. case V4L2_FIELD_SEQ_TB:
  572. maxh = maxh * 2;
  573. break;
  574. default:
  575. field = (f->fmt.pix.height > maxh / 2)
  576. ? V4L2_FIELD_INTERLACED
  577. : V4L2_FIELD_BOTTOM;
  578. break;
  579. }
  580. f->fmt.pix.field = field;
  581. if (f->fmt.pix.width < 48)
  582. f->fmt.pix.width = 48;
  583. if (f->fmt.pix.height < 32)
  584. f->fmt.pix.height = 32;
  585. if (f->fmt.pix.width > 720)
  586. f->fmt.pix.width = 720;
  587. if (f->fmt.pix.height > maxh)
  588. f->fmt.pix.height = maxh;
  589. f->fmt.pix.width &= ~0x03;
  590. f->fmt.pix.bytesperline =
  591. (f->fmt.pix.width * (fmt->depth)) >> 3;
  592. f->fmt.pix.sizeimage =
  593. f->fmt.pix.height * f->fmt.pix.bytesperline;
  594. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  595. return 0;
  596. }
  597. /*
  598. * Note that tw68_s_fmt_vid_cap sets the information into the fh structure,
  599. * and it will be used for all future new buffers. However, there could be
  600. * some number of buffers on the "active" chain which will be filled before
  601. * the change takes place.
  602. */
  603. static int tw68_s_fmt_vid_cap(struct file *file, void *priv,
  604. struct v4l2_format *f)
  605. {
  606. struct tw68_dev *dev = video_drvdata(file);
  607. int err;
  608. err = tw68_try_fmt_vid_cap(file, priv, f);
  609. if (0 != err)
  610. return err;
  611. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  612. dev->width = f->fmt.pix.width;
  613. dev->height = f->fmt.pix.height;
  614. dev->field = f->fmt.pix.field;
  615. return 0;
  616. }
  617. static int tw68_enum_input(struct file *file, void *priv,
  618. struct v4l2_input *i)
  619. {
  620. struct tw68_dev *dev = video_drvdata(file);
  621. unsigned int n;
  622. n = i->index;
  623. if (n >= TW68_INPUT_MAX)
  624. return -EINVAL;
  625. i->index = n;
  626. i->type = V4L2_INPUT_TYPE_CAMERA;
  627. snprintf(i->name, sizeof(i->name), "Composite %d", n);
  628. /* If the query is for the current input, get live data */
  629. if (n == dev->input) {
  630. int v1 = tw_readb(TW68_STATUS1);
  631. int v2 = tw_readb(TW68_MVSN);
  632. if (0 != (v1 & (1 << 7)))
  633. i->status |= V4L2_IN_ST_NO_SYNC;
  634. if (0 != (v1 & (1 << 6)))
  635. i->status |= V4L2_IN_ST_NO_H_LOCK;
  636. if (0 != (v1 & (1 << 2)))
  637. i->status |= V4L2_IN_ST_NO_SIGNAL;
  638. if (0 != (v1 & 1 << 1))
  639. i->status |= V4L2_IN_ST_NO_COLOR;
  640. if (0 != (v2 & (1 << 2)))
  641. i->status |= V4L2_IN_ST_MACROVISION;
  642. }
  643. i->std = video_devdata(file)->tvnorms;
  644. return 0;
  645. }
  646. static int tw68_g_input(struct file *file, void *priv, unsigned int *i)
  647. {
  648. struct tw68_dev *dev = video_drvdata(file);
  649. *i = dev->input;
  650. return 0;
  651. }
  652. static int tw68_s_input(struct file *file, void *priv, unsigned int i)
  653. {
  654. struct tw68_dev *dev = video_drvdata(file);
  655. if (i >= TW68_INPUT_MAX)
  656. return -EINVAL;
  657. dev->input = i;
  658. tw_andorb(TW68_INFORM, 0x03 << 2, dev->input << 2);
  659. return 0;
  660. }
  661. static int tw68_querycap(struct file *file, void *priv,
  662. struct v4l2_capability *cap)
  663. {
  664. struct tw68_dev *dev = video_drvdata(file);
  665. strcpy(cap->driver, "tw68");
  666. strlcpy(cap->card, "Techwell Capture Card",
  667. sizeof(cap->card));
  668. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  669. cap->device_caps =
  670. V4L2_CAP_VIDEO_CAPTURE |
  671. V4L2_CAP_READWRITE |
  672. V4L2_CAP_STREAMING;
  673. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  674. return 0;
  675. }
  676. static int tw68_s_std(struct file *file, void *priv, v4l2_std_id id)
  677. {
  678. struct tw68_dev *dev = video_drvdata(file);
  679. unsigned int i;
  680. if (vb2_is_busy(&dev->vidq))
  681. return -EBUSY;
  682. /* Look for match on complete norm id (may have mult bits) */
  683. for (i = 0; i < TVNORMS; i++) {
  684. if (id == tvnorms[i].id)
  685. break;
  686. }
  687. /* If no exact match, look for norm which contains this one */
  688. if (i == TVNORMS) {
  689. for (i = 0; i < TVNORMS; i++)
  690. if (id & tvnorms[i].id)
  691. break;
  692. }
  693. /* If still not matched, give up */
  694. if (i == TVNORMS)
  695. return -EINVAL;
  696. set_tvnorm(dev, &tvnorms[i]); /* do the actual setting */
  697. return 0;
  698. }
  699. static int tw68_g_std(struct file *file, void *priv, v4l2_std_id *id)
  700. {
  701. struct tw68_dev *dev = video_drvdata(file);
  702. *id = dev->tvnorm->id;
  703. return 0;
  704. }
  705. static int tw68_enum_fmt_vid_cap(struct file *file, void *priv,
  706. struct v4l2_fmtdesc *f)
  707. {
  708. if (f->index >= FORMATS)
  709. return -EINVAL;
  710. strlcpy(f->description, formats[f->index].name,
  711. sizeof(f->description));
  712. f->pixelformat = formats[f->index].fourcc;
  713. return 0;
  714. }
  715. /*
  716. * Used strictly for internal development and debugging, this routine
  717. * prints out the current register contents for the tw68xx device.
  718. */
  719. static void tw68_dump_regs(struct tw68_dev *dev)
  720. {
  721. unsigned char line[80];
  722. int i, j, k;
  723. unsigned char *cptr;
  724. pr_info("Full dump of TW68 registers:\n");
  725. /* First we do the PCI regs, 8 4-byte regs per line */
  726. for (i = 0; i < 0x100; i += 32) {
  727. cptr = line;
  728. cptr += sprintf(cptr, "%03x ", i);
  729. /* j steps through the next 4 words */
  730. for (j = i; j < i + 16; j += 4)
  731. cptr += sprintf(cptr, "%08x ", tw_readl(j));
  732. *cptr++ = ' ';
  733. for (; j < i + 32; j += 4)
  734. cptr += sprintf(cptr, "%08x ", tw_readl(j));
  735. *cptr++ = '\n';
  736. *cptr = 0;
  737. pr_info("%s", line);
  738. }
  739. /* Next the control regs, which are single-byte, address mod 4 */
  740. while (i < 0x400) {
  741. cptr = line;
  742. cptr += sprintf(cptr, "%03x ", i);
  743. /* Print out 4 groups of 4 bytes */
  744. for (j = 0; j < 4; j++) {
  745. for (k = 0; k < 4; k++) {
  746. cptr += sprintf(cptr, "%02x ",
  747. tw_readb(i));
  748. i += 4;
  749. }
  750. *cptr++ = ' ';
  751. }
  752. *cptr++ = '\n';
  753. *cptr = 0;
  754. pr_info("%s", line);
  755. }
  756. }
  757. static int vidioc_log_status(struct file *file, void *priv)
  758. {
  759. struct tw68_dev *dev = video_drvdata(file);
  760. tw68_dump_regs(dev);
  761. return v4l2_ctrl_log_status(file, priv);
  762. }
  763. #ifdef CONFIG_VIDEO_ADV_DEBUG
  764. static int vidioc_g_register(struct file *file, void *priv,
  765. struct v4l2_dbg_register *reg)
  766. {
  767. struct tw68_dev *dev = video_drvdata(file);
  768. if (reg->size == 1)
  769. reg->val = tw_readb(reg->reg);
  770. else
  771. reg->val = tw_readl(reg->reg);
  772. return 0;
  773. }
  774. static int vidioc_s_register(struct file *file, void *priv,
  775. const struct v4l2_dbg_register *reg)
  776. {
  777. struct tw68_dev *dev = video_drvdata(file);
  778. if (reg->size == 1)
  779. tw_writeb(reg->reg, reg->val);
  780. else
  781. tw_writel(reg->reg & 0xffff, reg->val);
  782. return 0;
  783. }
  784. #endif
  785. static const struct v4l2_ctrl_ops tw68_ctrl_ops = {
  786. .s_ctrl = tw68_s_ctrl,
  787. };
  788. static const struct v4l2_file_operations video_fops = {
  789. .owner = THIS_MODULE,
  790. .open = v4l2_fh_open,
  791. .release = vb2_fop_release,
  792. .read = vb2_fop_read,
  793. .poll = vb2_fop_poll,
  794. .mmap = vb2_fop_mmap,
  795. .unlocked_ioctl = video_ioctl2,
  796. };
  797. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  798. .vidioc_querycap = tw68_querycap,
  799. .vidioc_enum_fmt_vid_cap = tw68_enum_fmt_vid_cap,
  800. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  801. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  802. .vidioc_querybuf = vb2_ioctl_querybuf,
  803. .vidioc_qbuf = vb2_ioctl_qbuf,
  804. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  805. .vidioc_s_std = tw68_s_std,
  806. .vidioc_g_std = tw68_g_std,
  807. .vidioc_enum_input = tw68_enum_input,
  808. .vidioc_g_input = tw68_g_input,
  809. .vidioc_s_input = tw68_s_input,
  810. .vidioc_streamon = vb2_ioctl_streamon,
  811. .vidioc_streamoff = vb2_ioctl_streamoff,
  812. .vidioc_g_fmt_vid_cap = tw68_g_fmt_vid_cap,
  813. .vidioc_try_fmt_vid_cap = tw68_try_fmt_vid_cap,
  814. .vidioc_s_fmt_vid_cap = tw68_s_fmt_vid_cap,
  815. .vidioc_log_status = vidioc_log_status,
  816. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  817. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  818. #ifdef CONFIG_VIDEO_ADV_DEBUG
  819. .vidioc_g_register = vidioc_g_register,
  820. .vidioc_s_register = vidioc_s_register,
  821. #endif
  822. };
  823. static struct video_device tw68_video_template = {
  824. .name = "tw68_video",
  825. .fops = &video_fops,
  826. .ioctl_ops = &video_ioctl_ops,
  827. .release = video_device_release_empty,
  828. .tvnorms = TW68_NORMS,
  829. };
  830. /* ------------------------------------------------------------------ */
  831. /* exported stuff */
  832. void tw68_set_tvnorm_hw(struct tw68_dev *dev)
  833. {
  834. tw_andorb(TW68_SDT, 0x07, dev->tvnorm->format);
  835. }
  836. int tw68_video_init1(struct tw68_dev *dev)
  837. {
  838. struct v4l2_ctrl_handler *hdl = &dev->hdl;
  839. v4l2_ctrl_handler_init(hdl, 6);
  840. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  841. V4L2_CID_BRIGHTNESS, -128, 127, 1, 20);
  842. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  843. V4L2_CID_CONTRAST, 0, 255, 1, 100);
  844. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  845. V4L2_CID_SATURATION, 0, 255, 1, 128);
  846. /* NTSC only */
  847. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  848. V4L2_CID_HUE, -128, 127, 1, 0);
  849. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  850. V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
  851. v4l2_ctrl_new_std(hdl, &tw68_ctrl_ops,
  852. V4L2_CID_CHROMA_AGC, 0, 1, 1, 1);
  853. if (hdl->error) {
  854. v4l2_ctrl_handler_free(hdl);
  855. return hdl->error;
  856. }
  857. dev->v4l2_dev.ctrl_handler = hdl;
  858. v4l2_ctrl_handler_setup(hdl);
  859. return 0;
  860. }
  861. int tw68_video_init2(struct tw68_dev *dev, int video_nr)
  862. {
  863. int ret;
  864. set_tvnorm(dev, &tvnorms[0]);
  865. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  866. dev->width = 720;
  867. dev->height = 576;
  868. dev->field = V4L2_FIELD_INTERLACED;
  869. INIT_LIST_HEAD(&dev->active);
  870. dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  871. dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  872. dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
  873. dev->vidq.ops = &tw68_video_qops;
  874. dev->vidq.mem_ops = &vb2_dma_sg_memops;
  875. dev->vidq.drv_priv = dev;
  876. dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
  877. dev->vidq.buf_struct_size = sizeof(struct tw68_buf);
  878. dev->vidq.lock = &dev->lock;
  879. dev->vidq.min_buffers_needed = 2;
  880. ret = vb2_queue_init(&dev->vidq);
  881. if (ret)
  882. return ret;
  883. dev->vdev = tw68_video_template;
  884. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  885. dev->vdev.lock = &dev->lock;
  886. dev->vdev.queue = &dev->vidq;
  887. video_set_drvdata(&dev->vdev, dev);
  888. return video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr);
  889. }
  890. /*
  891. * tw68_irq_video_done
  892. */
  893. void tw68_irq_video_done(struct tw68_dev *dev, unsigned long status)
  894. {
  895. __u32 reg;
  896. /* reset interrupts handled by this routine */
  897. tw_writel(TW68_INTSTAT, status);
  898. /*
  899. * Check most likely first
  900. *
  901. * DMAPI shows we have reached the end of the risc code
  902. * for the current buffer.
  903. */
  904. if (status & TW68_DMAPI) {
  905. struct tw68_buf *buf;
  906. spin_lock(&dev->slock);
  907. buf = list_entry(dev->active.next, struct tw68_buf, list);
  908. list_del(&buf->list);
  909. spin_unlock(&dev->slock);
  910. v4l2_get_timestamp(&buf->vb.timestamp);
  911. buf->vb.field = dev->field;
  912. buf->vb.sequence = dev->seqnr++;
  913. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  914. status &= ~(TW68_DMAPI);
  915. if (0 == status)
  916. return;
  917. }
  918. if (status & (TW68_VLOCK | TW68_HLOCK))
  919. dev_dbg(&dev->pci->dev, "Lost sync\n");
  920. if (status & TW68_PABORT)
  921. dev_err(&dev->pci->dev, "PABORT interrupt\n");
  922. if (status & TW68_DMAPERR)
  923. dev_err(&dev->pci->dev, "DMAPERR interrupt\n");
  924. /*
  925. * On TW6800, FDMIS is apparently generated if video input is switched
  926. * during operation. Therefore, it is not enabled for that chip.
  927. */
  928. if (status & TW68_FDMIS)
  929. dev_dbg(&dev->pci->dev, "FDMIS interrupt\n");
  930. if (status & TW68_FFOF) {
  931. /* probably a logic error */
  932. reg = tw_readl(TW68_DMAC) & TW68_FIFO_EN;
  933. tw_clearl(TW68_DMAC, TW68_FIFO_EN);
  934. dev_dbg(&dev->pci->dev, "FFOF interrupt\n");
  935. tw_setl(TW68_DMAC, reg);
  936. }
  937. if (status & TW68_FFERR)
  938. dev_dbg(&dev->pci->dev, "FFERR interrupt\n");
  939. }