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