tw68-video.c 29 KB

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