tw686x-video.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /*
  2. * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
  3. *
  4. * Based on original driver by Krzysztof Ha?asa:
  5. * Copyright (C) 2015 Industrial Research Institute for Automation
  6. * and Measurements PIAP
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License
  10. * as published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <media/v4l2-common.h>
  20. #include <media/v4l2-event.h>
  21. #include <media/videobuf2-dma-contig.h>
  22. #include <media/videobuf2-dma-sg.h>
  23. #include <media/videobuf2-vmalloc.h>
  24. #include "tw686x.h"
  25. #include "tw686x-regs.h"
  26. #define TW686X_INPUTS_PER_CH 4
  27. #define TW686X_VIDEO_WIDTH 720
  28. #define TW686X_VIDEO_HEIGHT(id) ((id & V4L2_STD_525_60) ? 480 : 576)
  29. #define TW686X_MAX_FPS(id) ((id & V4L2_STD_525_60) ? 30 : 25)
  30. #define TW686X_MAX_SG_ENTRY_SIZE 4096
  31. #define TW686X_MAX_SG_DESC_COUNT 256 /* PAL 720x576 needs 203 4-KB pages */
  32. #define TW686X_SG_TABLE_SIZE (TW686X_MAX_SG_DESC_COUNT * sizeof(struct tw686x_sg_desc))
  33. static const struct tw686x_format formats[] = {
  34. {
  35. .fourcc = V4L2_PIX_FMT_UYVY,
  36. .mode = 0,
  37. .depth = 16,
  38. }, {
  39. .fourcc = V4L2_PIX_FMT_RGB565,
  40. .mode = 5,
  41. .depth = 16,
  42. }, {
  43. .fourcc = V4L2_PIX_FMT_YUYV,
  44. .mode = 6,
  45. .depth = 16,
  46. }
  47. };
  48. static void tw686x_buf_done(struct tw686x_video_channel *vc,
  49. unsigned int pb)
  50. {
  51. struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
  52. struct tw686x_dev *dev = vc->dev;
  53. struct vb2_v4l2_buffer *vb;
  54. struct vb2_buffer *vb2_buf;
  55. if (vc->curr_bufs[pb]) {
  56. vb = &vc->curr_bufs[pb]->vb;
  57. vb->field = dev->dma_ops->field;
  58. vb->sequence = vc->sequence++;
  59. vb2_buf = &vb->vb2_buf;
  60. if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY)
  61. memcpy(vb2_plane_vaddr(vb2_buf, 0), desc->virt,
  62. desc->size);
  63. vb2_buf->timestamp = ktime_get_ns();
  64. vb2_buffer_done(vb2_buf, VB2_BUF_STATE_DONE);
  65. }
  66. vc->pb = !pb;
  67. }
  68. /*
  69. * We can call this even when alloc_dma failed for the given channel
  70. */
  71. static void tw686x_memcpy_dma_free(struct tw686x_video_channel *vc,
  72. unsigned int pb)
  73. {
  74. struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
  75. struct tw686x_dev *dev = vc->dev;
  76. struct pci_dev *pci_dev;
  77. unsigned long flags;
  78. /* Check device presence. Shouldn't really happen! */
  79. spin_lock_irqsave(&dev->lock, flags);
  80. pci_dev = dev->pci_dev;
  81. spin_unlock_irqrestore(&dev->lock, flags);
  82. if (!pci_dev) {
  83. WARN(1, "trying to deallocate on missing device\n");
  84. return;
  85. }
  86. if (desc->virt) {
  87. pci_free_consistent(dev->pci_dev, desc->size,
  88. desc->virt, desc->phys);
  89. desc->virt = NULL;
  90. }
  91. }
  92. static int tw686x_memcpy_dma_alloc(struct tw686x_video_channel *vc,
  93. unsigned int pb)
  94. {
  95. struct tw686x_dev *dev = vc->dev;
  96. u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
  97. unsigned int len;
  98. void *virt;
  99. WARN(vc->dma_descs[pb].virt,
  100. "Allocating buffer but previous still here\n");
  101. len = (vc->width * vc->height * vc->format->depth) >> 3;
  102. virt = pci_alloc_consistent(dev->pci_dev, len,
  103. &vc->dma_descs[pb].phys);
  104. if (!virt) {
  105. v4l2_err(&dev->v4l2_dev,
  106. "dma%d: unable to allocate %s-buffer\n",
  107. vc->ch, pb ? "B" : "P");
  108. return -ENOMEM;
  109. }
  110. vc->dma_descs[pb].size = len;
  111. vc->dma_descs[pb].virt = virt;
  112. reg_write(dev, reg, vc->dma_descs[pb].phys);
  113. return 0;
  114. }
  115. static void tw686x_memcpy_buf_refill(struct tw686x_video_channel *vc,
  116. unsigned int pb)
  117. {
  118. struct tw686x_v4l2_buf *buf;
  119. while (!list_empty(&vc->vidq_queued)) {
  120. buf = list_first_entry(&vc->vidq_queued,
  121. struct tw686x_v4l2_buf, list);
  122. list_del(&buf->list);
  123. vc->curr_bufs[pb] = buf;
  124. return;
  125. }
  126. vc->curr_bufs[pb] = NULL;
  127. }
  128. static const struct tw686x_dma_ops memcpy_dma_ops = {
  129. .alloc = tw686x_memcpy_dma_alloc,
  130. .free = tw686x_memcpy_dma_free,
  131. .buf_refill = tw686x_memcpy_buf_refill,
  132. .mem_ops = &vb2_vmalloc_memops,
  133. .hw_dma_mode = TW686X_FRAME_MODE,
  134. .field = V4L2_FIELD_INTERLACED,
  135. };
  136. static void tw686x_contig_buf_refill(struct tw686x_video_channel *vc,
  137. unsigned int pb)
  138. {
  139. struct tw686x_v4l2_buf *buf;
  140. while (!list_empty(&vc->vidq_queued)) {
  141. u32 reg = pb ? VDMA_B_ADDR[vc->ch] : VDMA_P_ADDR[vc->ch];
  142. dma_addr_t phys;
  143. buf = list_first_entry(&vc->vidq_queued,
  144. struct tw686x_v4l2_buf, list);
  145. list_del(&buf->list);
  146. phys = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
  147. reg_write(vc->dev, reg, phys);
  148. buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
  149. vc->curr_bufs[pb] = buf;
  150. return;
  151. }
  152. vc->curr_bufs[pb] = NULL;
  153. }
  154. static const struct tw686x_dma_ops contig_dma_ops = {
  155. .buf_refill = tw686x_contig_buf_refill,
  156. .mem_ops = &vb2_dma_contig_memops,
  157. .hw_dma_mode = TW686X_FRAME_MODE,
  158. .field = V4L2_FIELD_INTERLACED,
  159. };
  160. static int tw686x_sg_desc_fill(struct tw686x_sg_desc *descs,
  161. struct tw686x_v4l2_buf *buf,
  162. unsigned int buf_len)
  163. {
  164. struct sg_table *vbuf = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
  165. unsigned int len, entry_len;
  166. struct scatterlist *sg;
  167. int i, count;
  168. /* Clear the scatter-gather table */
  169. memset(descs, 0, TW686X_SG_TABLE_SIZE);
  170. count = 0;
  171. for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
  172. dma_addr_t phys = sg_dma_address(sg);
  173. len = sg_dma_len(sg);
  174. while (len && buf_len) {
  175. if (count == TW686X_MAX_SG_DESC_COUNT)
  176. return -ENOMEM;
  177. entry_len = min_t(unsigned int, len,
  178. TW686X_MAX_SG_ENTRY_SIZE);
  179. entry_len = min_t(unsigned int, entry_len, buf_len);
  180. descs[count].phys = cpu_to_le32(phys);
  181. descs[count++].flags_length =
  182. cpu_to_le32(BIT(30) | entry_len);
  183. phys += entry_len;
  184. len -= entry_len;
  185. buf_len -= entry_len;
  186. }
  187. if (!buf_len)
  188. return 0;
  189. }
  190. return -ENOMEM;
  191. }
  192. static void tw686x_sg_buf_refill(struct tw686x_video_channel *vc,
  193. unsigned int pb)
  194. {
  195. struct tw686x_dev *dev = vc->dev;
  196. struct tw686x_v4l2_buf *buf;
  197. while (!list_empty(&vc->vidq_queued)) {
  198. unsigned int buf_len;
  199. buf = list_first_entry(&vc->vidq_queued,
  200. struct tw686x_v4l2_buf, list);
  201. list_del(&buf->list);
  202. buf_len = (vc->width * vc->height * vc->format->depth) >> 3;
  203. if (tw686x_sg_desc_fill(vc->sg_descs[pb], buf, buf_len)) {
  204. v4l2_err(&dev->v4l2_dev,
  205. "dma%d: unable to fill %s-buffer\n",
  206. vc->ch, pb ? "B" : "P");
  207. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  208. continue;
  209. }
  210. buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
  211. vc->curr_bufs[pb] = buf;
  212. return;
  213. }
  214. vc->curr_bufs[pb] = NULL;
  215. }
  216. static void tw686x_sg_dma_free(struct tw686x_video_channel *vc,
  217. unsigned int pb)
  218. {
  219. struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
  220. struct tw686x_dev *dev = vc->dev;
  221. if (desc->size) {
  222. pci_free_consistent(dev->pci_dev, desc->size,
  223. desc->virt, desc->phys);
  224. desc->virt = NULL;
  225. }
  226. vc->sg_descs[pb] = NULL;
  227. }
  228. static int tw686x_sg_dma_alloc(struct tw686x_video_channel *vc,
  229. unsigned int pb)
  230. {
  231. struct tw686x_dma_desc *desc = &vc->dma_descs[pb];
  232. struct tw686x_dev *dev = vc->dev;
  233. u32 reg = pb ? DMA_PAGE_TABLE1_ADDR[vc->ch] :
  234. DMA_PAGE_TABLE0_ADDR[vc->ch];
  235. void *virt;
  236. if (desc->size) {
  237. virt = pci_alloc_consistent(dev->pci_dev, desc->size,
  238. &desc->phys);
  239. if (!virt) {
  240. v4l2_err(&dev->v4l2_dev,
  241. "dma%d: unable to allocate %s-buffer\n",
  242. vc->ch, pb ? "B" : "P");
  243. return -ENOMEM;
  244. }
  245. desc->virt = virt;
  246. reg_write(dev, reg, desc->phys);
  247. } else {
  248. virt = dev->video_channels[0].dma_descs[pb].virt +
  249. vc->ch * TW686X_SG_TABLE_SIZE;
  250. }
  251. vc->sg_descs[pb] = virt;
  252. return 0;
  253. }
  254. static int tw686x_sg_setup(struct tw686x_dev *dev)
  255. {
  256. unsigned int sg_table_size, pb, ch, channels;
  257. if (is_second_gen(dev)) {
  258. /*
  259. * TW6865/TW6869: each channel needs a pair of
  260. * P-B descriptor tables.
  261. */
  262. channels = max_channels(dev);
  263. sg_table_size = TW686X_SG_TABLE_SIZE;
  264. } else {
  265. /*
  266. * TW6864/TW6868: we need to allocate a pair of
  267. * P-B descriptor tables, common for all channels.
  268. * Each table will be bigger than 4 KB.
  269. */
  270. channels = 1;
  271. sg_table_size = max_channels(dev) * TW686X_SG_TABLE_SIZE;
  272. }
  273. for (ch = 0; ch < channels; ch++) {
  274. struct tw686x_video_channel *vc = &dev->video_channels[ch];
  275. for (pb = 0; pb < 2; pb++)
  276. vc->dma_descs[pb].size = sg_table_size;
  277. }
  278. return 0;
  279. }
  280. static const struct tw686x_dma_ops sg_dma_ops = {
  281. .setup = tw686x_sg_setup,
  282. .alloc = tw686x_sg_dma_alloc,
  283. .free = tw686x_sg_dma_free,
  284. .buf_refill = tw686x_sg_buf_refill,
  285. .mem_ops = &vb2_dma_sg_memops,
  286. .hw_dma_mode = TW686X_SG_MODE,
  287. .field = V4L2_FIELD_SEQ_TB,
  288. };
  289. static const unsigned int fps_map[15] = {
  290. /*
  291. * bit 31 enables selecting the field control register
  292. * bits 0-29 are a bitmask with fields that will be output.
  293. * For NTSC (and PAL-M, PAL-60), all 30 bits are used.
  294. * For other PAL standards, only the first 25 bits are used.
  295. */
  296. 0x00000000, /* output all fields */
  297. 0x80000006, /* 2 fps (60Hz), 2 fps (50Hz) */
  298. 0x80018006, /* 4 fps (60Hz), 4 fps (50Hz) */
  299. 0x80618006, /* 6 fps (60Hz), 6 fps (50Hz) */
  300. 0x81818186, /* 8 fps (60Hz), 8 fps (50Hz) */
  301. 0x86186186, /* 10 fps (60Hz), 8 fps (50Hz) */
  302. 0x86619866, /* 12 fps (60Hz), 10 fps (50Hz) */
  303. 0x86666666, /* 14 fps (60Hz), 12 fps (50Hz) */
  304. 0x9999999e, /* 16 fps (60Hz), 14 fps (50Hz) */
  305. 0x99e6799e, /* 18 fps (60Hz), 16 fps (50Hz) */
  306. 0x9e79e79e, /* 20 fps (60Hz), 16 fps (50Hz) */
  307. 0x9e7e7e7e, /* 22 fps (60Hz), 18 fps (50Hz) */
  308. 0x9fe7f9fe, /* 24 fps (60Hz), 20 fps (50Hz) */
  309. 0x9ffe7ffe, /* 26 fps (60Hz), 22 fps (50Hz) */
  310. 0x9ffffffe, /* 28 fps (60Hz), 24 fps (50Hz) */
  311. };
  312. static unsigned int tw686x_real_fps(unsigned int index, unsigned int max_fps)
  313. {
  314. unsigned long mask;
  315. if (!index || index >= ARRAY_SIZE(fps_map))
  316. return max_fps;
  317. mask = GENMASK(max_fps - 1, 0);
  318. return hweight_long(fps_map[index] & mask);
  319. }
  320. static unsigned int tw686x_fps_idx(unsigned int fps, unsigned int max_fps)
  321. {
  322. unsigned int idx, real_fps;
  323. int delta;
  324. /* First guess */
  325. idx = (12 + 15 * fps) / max_fps;
  326. /* Minimal possible framerate is 2 frames per second */
  327. if (!idx)
  328. return 1;
  329. /* Check if the difference is bigger than abs(1) and adjust */
  330. real_fps = tw686x_real_fps(idx, max_fps);
  331. delta = real_fps - fps;
  332. if (delta < -1)
  333. idx++;
  334. else if (delta > 1)
  335. idx--;
  336. /* Max framerate */
  337. if (idx >= 15)
  338. return 0;
  339. return idx;
  340. }
  341. static void tw686x_set_framerate(struct tw686x_video_channel *vc,
  342. unsigned int fps)
  343. {
  344. unsigned int i;
  345. i = tw686x_fps_idx(fps, TW686X_MAX_FPS(vc->video_standard));
  346. reg_write(vc->dev, VIDEO_FIELD_CTRL[vc->ch], fps_map[i]);
  347. vc->fps = tw686x_real_fps(i, TW686X_MAX_FPS(vc->video_standard));
  348. }
  349. static const struct tw686x_format *format_by_fourcc(unsigned int fourcc)
  350. {
  351. unsigned int cnt;
  352. for (cnt = 0; cnt < ARRAY_SIZE(formats); cnt++)
  353. if (formats[cnt].fourcc == fourcc)
  354. return &formats[cnt];
  355. return NULL;
  356. }
  357. static int tw686x_queue_setup(struct vb2_queue *vq,
  358. unsigned int *nbuffers, unsigned int *nplanes,
  359. unsigned int sizes[], struct device *alloc_devs[])
  360. {
  361. struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
  362. unsigned int szimage =
  363. (vc->width * vc->height * vc->format->depth) >> 3;
  364. /*
  365. * Let's request at least three buffers: two for the
  366. * DMA engine and one for userspace.
  367. */
  368. if (vq->num_buffers + *nbuffers < 3)
  369. *nbuffers = 3 - vq->num_buffers;
  370. if (*nplanes) {
  371. if (*nplanes != 1 || sizes[0] < szimage)
  372. return -EINVAL;
  373. return 0;
  374. }
  375. sizes[0] = szimage;
  376. *nplanes = 1;
  377. return 0;
  378. }
  379. static void tw686x_buf_queue(struct vb2_buffer *vb)
  380. {
  381. struct tw686x_video_channel *vc = vb2_get_drv_priv(vb->vb2_queue);
  382. struct tw686x_dev *dev = vc->dev;
  383. struct pci_dev *pci_dev;
  384. unsigned long flags;
  385. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  386. struct tw686x_v4l2_buf *buf =
  387. container_of(vbuf, struct tw686x_v4l2_buf, vb);
  388. /* Check device presence */
  389. spin_lock_irqsave(&dev->lock, flags);
  390. pci_dev = dev->pci_dev;
  391. spin_unlock_irqrestore(&dev->lock, flags);
  392. if (!pci_dev) {
  393. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  394. return;
  395. }
  396. spin_lock_irqsave(&vc->qlock, flags);
  397. list_add_tail(&buf->list, &vc->vidq_queued);
  398. spin_unlock_irqrestore(&vc->qlock, flags);
  399. }
  400. static void tw686x_clear_queue(struct tw686x_video_channel *vc,
  401. enum vb2_buffer_state state)
  402. {
  403. unsigned int pb;
  404. while (!list_empty(&vc->vidq_queued)) {
  405. struct tw686x_v4l2_buf *buf;
  406. buf = list_first_entry(&vc->vidq_queued,
  407. struct tw686x_v4l2_buf, list);
  408. list_del(&buf->list);
  409. vb2_buffer_done(&buf->vb.vb2_buf, state);
  410. }
  411. for (pb = 0; pb < 2; pb++) {
  412. if (vc->curr_bufs[pb])
  413. vb2_buffer_done(&vc->curr_bufs[pb]->vb.vb2_buf, state);
  414. vc->curr_bufs[pb] = NULL;
  415. }
  416. }
  417. static int tw686x_start_streaming(struct vb2_queue *vq, unsigned int count)
  418. {
  419. struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
  420. struct tw686x_dev *dev = vc->dev;
  421. struct pci_dev *pci_dev;
  422. unsigned long flags;
  423. int pb, err;
  424. /* Check device presence */
  425. spin_lock_irqsave(&dev->lock, flags);
  426. pci_dev = dev->pci_dev;
  427. spin_unlock_irqrestore(&dev->lock, flags);
  428. if (!pci_dev) {
  429. err = -ENODEV;
  430. goto err_clear_queue;
  431. }
  432. spin_lock_irqsave(&vc->qlock, flags);
  433. /* Sanity check */
  434. if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY &&
  435. (!vc->dma_descs[0].virt || !vc->dma_descs[1].virt)) {
  436. spin_unlock_irqrestore(&vc->qlock, flags);
  437. v4l2_err(&dev->v4l2_dev,
  438. "video%d: refusing to start without DMA buffers\n",
  439. vc->num);
  440. err = -ENOMEM;
  441. goto err_clear_queue;
  442. }
  443. for (pb = 0; pb < 2; pb++)
  444. dev->dma_ops->buf_refill(vc, pb);
  445. spin_unlock_irqrestore(&vc->qlock, flags);
  446. vc->sequence = 0;
  447. vc->pb = 0;
  448. spin_lock_irqsave(&dev->lock, flags);
  449. tw686x_enable_channel(dev, vc->ch);
  450. spin_unlock_irqrestore(&dev->lock, flags);
  451. mod_timer(&dev->dma_delay_timer, jiffies + msecs_to_jiffies(100));
  452. return 0;
  453. err_clear_queue:
  454. spin_lock_irqsave(&vc->qlock, flags);
  455. tw686x_clear_queue(vc, VB2_BUF_STATE_QUEUED);
  456. spin_unlock_irqrestore(&vc->qlock, flags);
  457. return err;
  458. }
  459. static void tw686x_stop_streaming(struct vb2_queue *vq)
  460. {
  461. struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
  462. struct tw686x_dev *dev = vc->dev;
  463. struct pci_dev *pci_dev;
  464. unsigned long flags;
  465. /* Check device presence */
  466. spin_lock_irqsave(&dev->lock, flags);
  467. pci_dev = dev->pci_dev;
  468. spin_unlock_irqrestore(&dev->lock, flags);
  469. if (pci_dev)
  470. tw686x_disable_channel(dev, vc->ch);
  471. spin_lock_irqsave(&vc->qlock, flags);
  472. tw686x_clear_queue(vc, VB2_BUF_STATE_ERROR);
  473. spin_unlock_irqrestore(&vc->qlock, flags);
  474. }
  475. static int tw686x_buf_prepare(struct vb2_buffer *vb)
  476. {
  477. struct tw686x_video_channel *vc = vb2_get_drv_priv(vb->vb2_queue);
  478. unsigned int size =
  479. (vc->width * vc->height * vc->format->depth) >> 3;
  480. if (vb2_plane_size(vb, 0) < size)
  481. return -EINVAL;
  482. vb2_set_plane_payload(vb, 0, size);
  483. return 0;
  484. }
  485. static const struct vb2_ops tw686x_video_qops = {
  486. .queue_setup = tw686x_queue_setup,
  487. .buf_queue = tw686x_buf_queue,
  488. .buf_prepare = tw686x_buf_prepare,
  489. .start_streaming = tw686x_start_streaming,
  490. .stop_streaming = tw686x_stop_streaming,
  491. .wait_prepare = vb2_ops_wait_prepare,
  492. .wait_finish = vb2_ops_wait_finish,
  493. };
  494. static int tw686x_s_ctrl(struct v4l2_ctrl *ctrl)
  495. {
  496. struct tw686x_video_channel *vc;
  497. struct tw686x_dev *dev;
  498. unsigned int ch;
  499. vc = container_of(ctrl->handler, struct tw686x_video_channel,
  500. ctrl_handler);
  501. dev = vc->dev;
  502. ch = vc->ch;
  503. switch (ctrl->id) {
  504. case V4L2_CID_BRIGHTNESS:
  505. reg_write(dev, BRIGHT[ch], ctrl->val & 0xff);
  506. return 0;
  507. case V4L2_CID_CONTRAST:
  508. reg_write(dev, CONTRAST[ch], ctrl->val);
  509. return 0;
  510. case V4L2_CID_SATURATION:
  511. reg_write(dev, SAT_U[ch], ctrl->val);
  512. reg_write(dev, SAT_V[ch], ctrl->val);
  513. return 0;
  514. case V4L2_CID_HUE:
  515. reg_write(dev, HUE[ch], ctrl->val & 0xff);
  516. return 0;
  517. }
  518. return -EINVAL;
  519. }
  520. static const struct v4l2_ctrl_ops ctrl_ops = {
  521. .s_ctrl = tw686x_s_ctrl,
  522. };
  523. static int tw686x_g_fmt_vid_cap(struct file *file, void *priv,
  524. struct v4l2_format *f)
  525. {
  526. struct tw686x_video_channel *vc = video_drvdata(file);
  527. struct tw686x_dev *dev = vc->dev;
  528. f->fmt.pix.width = vc->width;
  529. f->fmt.pix.height = vc->height;
  530. f->fmt.pix.field = dev->dma_ops->field;
  531. f->fmt.pix.pixelformat = vc->format->fourcc;
  532. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  533. f->fmt.pix.bytesperline = (f->fmt.pix.width * vc->format->depth) / 8;
  534. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  535. return 0;
  536. }
  537. static int tw686x_try_fmt_vid_cap(struct file *file, void *priv,
  538. struct v4l2_format *f)
  539. {
  540. struct tw686x_video_channel *vc = video_drvdata(file);
  541. struct tw686x_dev *dev = vc->dev;
  542. unsigned int video_height = TW686X_VIDEO_HEIGHT(vc->video_standard);
  543. const struct tw686x_format *format;
  544. format = format_by_fourcc(f->fmt.pix.pixelformat);
  545. if (!format) {
  546. format = &formats[0];
  547. f->fmt.pix.pixelformat = format->fourcc;
  548. }
  549. if (f->fmt.pix.width <= TW686X_VIDEO_WIDTH / 2)
  550. f->fmt.pix.width = TW686X_VIDEO_WIDTH / 2;
  551. else
  552. f->fmt.pix.width = TW686X_VIDEO_WIDTH;
  553. if (f->fmt.pix.height <= video_height / 2)
  554. f->fmt.pix.height = video_height / 2;
  555. else
  556. f->fmt.pix.height = video_height;
  557. f->fmt.pix.bytesperline = (f->fmt.pix.width * format->depth) / 8;
  558. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  559. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  560. f->fmt.pix.field = dev->dma_ops->field;
  561. return 0;
  562. }
  563. static int tw686x_set_format(struct tw686x_video_channel *vc,
  564. unsigned int pixelformat, unsigned int width,
  565. unsigned int height, bool realloc)
  566. {
  567. struct tw686x_dev *dev = vc->dev;
  568. u32 val, dma_width, dma_height, dma_line_width;
  569. int err, pb;
  570. vc->format = format_by_fourcc(pixelformat);
  571. vc->width = width;
  572. vc->height = height;
  573. /* We need new DMA buffers if the framesize has changed */
  574. if (dev->dma_ops->alloc && realloc) {
  575. for (pb = 0; pb < 2; pb++)
  576. dev->dma_ops->free(vc, pb);
  577. for (pb = 0; pb < 2; pb++) {
  578. err = dev->dma_ops->alloc(vc, pb);
  579. if (err) {
  580. if (pb > 0)
  581. dev->dma_ops->free(vc, 0);
  582. return err;
  583. }
  584. }
  585. }
  586. val = reg_read(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch]);
  587. if (vc->width <= TW686X_VIDEO_WIDTH / 2)
  588. val |= BIT(23);
  589. else
  590. val &= ~BIT(23);
  591. if (vc->height <= TW686X_VIDEO_HEIGHT(vc->video_standard) / 2)
  592. val |= BIT(24);
  593. else
  594. val &= ~BIT(24);
  595. val &= ~0x7ffff;
  596. /* Program the DMA scatter-gather */
  597. if (dev->dma_mode == TW686X_DMA_MODE_SG) {
  598. u32 start_idx, end_idx;
  599. start_idx = is_second_gen(dev) ?
  600. 0 : vc->ch * TW686X_MAX_SG_DESC_COUNT;
  601. end_idx = start_idx + TW686X_MAX_SG_DESC_COUNT - 1;
  602. val |= (end_idx << 10) | start_idx;
  603. }
  604. val &= ~(0x7 << 20);
  605. val |= vc->format->mode << 20;
  606. reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], val);
  607. /* Program the DMA frame size */
  608. dma_width = (vc->width * 2) & 0x7ff;
  609. dma_height = vc->height / 2;
  610. dma_line_width = (vc->width * 2) & 0x7ff;
  611. val = (dma_height << 22) | (dma_line_width << 11) | dma_width;
  612. reg_write(vc->dev, VDMA_WHP[vc->ch], val);
  613. return 0;
  614. }
  615. static int tw686x_s_fmt_vid_cap(struct file *file, void *priv,
  616. struct v4l2_format *f)
  617. {
  618. struct tw686x_video_channel *vc = video_drvdata(file);
  619. unsigned long area;
  620. bool realloc;
  621. int err;
  622. if (vb2_is_busy(&vc->vidq))
  623. return -EBUSY;
  624. area = vc->width * vc->height;
  625. err = tw686x_try_fmt_vid_cap(file, priv, f);
  626. if (err)
  627. return err;
  628. realloc = area != (f->fmt.pix.width * f->fmt.pix.height);
  629. return tw686x_set_format(vc, f->fmt.pix.pixelformat,
  630. f->fmt.pix.width, f->fmt.pix.height,
  631. realloc);
  632. }
  633. static int tw686x_querycap(struct file *file, void *priv,
  634. struct v4l2_capability *cap)
  635. {
  636. struct tw686x_video_channel *vc = video_drvdata(file);
  637. struct tw686x_dev *dev = vc->dev;
  638. strlcpy(cap->driver, "tw686x", sizeof(cap->driver));
  639. strlcpy(cap->card, dev->name, sizeof(cap->card));
  640. snprintf(cap->bus_info, sizeof(cap->bus_info),
  641. "PCI:%s", pci_name(dev->pci_dev));
  642. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  643. V4L2_CAP_READWRITE;
  644. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  645. return 0;
  646. }
  647. static int tw686x_set_standard(struct tw686x_video_channel *vc, v4l2_std_id id)
  648. {
  649. u32 val;
  650. if (id & V4L2_STD_NTSC)
  651. val = 0;
  652. else if (id & V4L2_STD_PAL)
  653. val = 1;
  654. else if (id & V4L2_STD_SECAM)
  655. val = 2;
  656. else if (id & V4L2_STD_NTSC_443)
  657. val = 3;
  658. else if (id & V4L2_STD_PAL_M)
  659. val = 4;
  660. else if (id & V4L2_STD_PAL_Nc)
  661. val = 5;
  662. else if (id & V4L2_STD_PAL_60)
  663. val = 6;
  664. else
  665. return -EINVAL;
  666. vc->video_standard = id;
  667. reg_write(vc->dev, SDT[vc->ch], val);
  668. val = reg_read(vc->dev, VIDEO_CONTROL1);
  669. if (id & V4L2_STD_525_60)
  670. val &= ~(1 << (SYS_MODE_DMA_SHIFT + vc->ch));
  671. else
  672. val |= (1 << (SYS_MODE_DMA_SHIFT + vc->ch));
  673. reg_write(vc->dev, VIDEO_CONTROL1, val);
  674. return 0;
  675. }
  676. static int tw686x_s_std(struct file *file, void *priv, v4l2_std_id id)
  677. {
  678. struct tw686x_video_channel *vc = video_drvdata(file);
  679. struct v4l2_format f;
  680. int ret;
  681. if (vc->video_standard == id)
  682. return 0;
  683. if (vb2_is_busy(&vc->vidq))
  684. return -EBUSY;
  685. ret = tw686x_set_standard(vc, id);
  686. if (ret)
  687. return ret;
  688. /*
  689. * Adjust format after V4L2_STD_525_60/V4L2_STD_625_50 change,
  690. * calling g_fmt and s_fmt will sanitize the height
  691. * according to the standard.
  692. */
  693. tw686x_g_fmt_vid_cap(file, priv, &f);
  694. tw686x_s_fmt_vid_cap(file, priv, &f);
  695. /*
  696. * Frame decimation depends on the chosen standard,
  697. * so reset it to the current value.
  698. */
  699. tw686x_set_framerate(vc, vc->fps);
  700. return 0;
  701. }
  702. static int tw686x_querystd(struct file *file, void *priv, v4l2_std_id *std)
  703. {
  704. struct tw686x_video_channel *vc = video_drvdata(file);
  705. struct tw686x_dev *dev = vc->dev;
  706. unsigned int old_std, detected_std = 0;
  707. unsigned long end;
  708. if (vb2_is_streaming(&vc->vidq))
  709. return -EBUSY;
  710. /* Enable and start standard detection */
  711. old_std = reg_read(dev, SDT[vc->ch]);
  712. reg_write(dev, SDT[vc->ch], 0x7);
  713. reg_write(dev, SDT_EN[vc->ch], 0xff);
  714. end = jiffies + msecs_to_jiffies(500);
  715. while (time_is_after_jiffies(end)) {
  716. detected_std = reg_read(dev, SDT[vc->ch]);
  717. if (!(detected_std & BIT(7)))
  718. break;
  719. msleep(100);
  720. }
  721. reg_write(dev, SDT[vc->ch], old_std);
  722. /* Exit if still busy */
  723. if (detected_std & BIT(7))
  724. return 0;
  725. detected_std = (detected_std >> 4) & 0x7;
  726. switch (detected_std) {
  727. case TW686X_STD_NTSC_M:
  728. *std &= V4L2_STD_NTSC;
  729. break;
  730. case TW686X_STD_NTSC_443:
  731. *std &= V4L2_STD_NTSC_443;
  732. break;
  733. case TW686X_STD_PAL_M:
  734. *std &= V4L2_STD_PAL_M;
  735. break;
  736. case TW686X_STD_PAL_60:
  737. *std &= V4L2_STD_PAL_60;
  738. break;
  739. case TW686X_STD_PAL:
  740. *std &= V4L2_STD_PAL;
  741. break;
  742. case TW686X_STD_PAL_CN:
  743. *std &= V4L2_STD_PAL_Nc;
  744. break;
  745. case TW686X_STD_SECAM:
  746. *std &= V4L2_STD_SECAM;
  747. break;
  748. default:
  749. *std = 0;
  750. }
  751. return 0;
  752. }
  753. static int tw686x_g_std(struct file *file, void *priv, v4l2_std_id *id)
  754. {
  755. struct tw686x_video_channel *vc = video_drvdata(file);
  756. *id = vc->video_standard;
  757. return 0;
  758. }
  759. static int tw686x_enum_framesizes(struct file *file, void *priv,
  760. struct v4l2_frmsizeenum *fsize)
  761. {
  762. struct tw686x_video_channel *vc = video_drvdata(file);
  763. if (fsize->index)
  764. return -EINVAL;
  765. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  766. fsize->stepwise.max_width = TW686X_VIDEO_WIDTH;
  767. fsize->stepwise.min_width = fsize->stepwise.max_width / 2;
  768. fsize->stepwise.step_width = fsize->stepwise.min_width;
  769. fsize->stepwise.max_height = TW686X_VIDEO_HEIGHT(vc->video_standard);
  770. fsize->stepwise.min_height = fsize->stepwise.max_height / 2;
  771. fsize->stepwise.step_height = fsize->stepwise.min_height;
  772. return 0;
  773. }
  774. static int tw686x_enum_frameintervals(struct file *file, void *priv,
  775. struct v4l2_frmivalenum *ival)
  776. {
  777. struct tw686x_video_channel *vc = video_drvdata(file);
  778. int max_fps = TW686X_MAX_FPS(vc->video_standard);
  779. int max_rates = DIV_ROUND_UP(max_fps, 2);
  780. if (ival->index >= max_rates)
  781. return -EINVAL;
  782. ival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  783. ival->discrete.numerator = 1;
  784. if (ival->index < (max_rates - 1))
  785. ival->discrete.denominator = (ival->index + 1) * 2;
  786. else
  787. ival->discrete.denominator = max_fps;
  788. return 0;
  789. }
  790. static int tw686x_g_parm(struct file *file, void *priv,
  791. struct v4l2_streamparm *sp)
  792. {
  793. struct tw686x_video_channel *vc = video_drvdata(file);
  794. struct v4l2_captureparm *cp = &sp->parm.capture;
  795. if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  796. return -EINVAL;
  797. sp->parm.capture.readbuffers = 3;
  798. cp->capability = V4L2_CAP_TIMEPERFRAME;
  799. cp->timeperframe.numerator = 1;
  800. cp->timeperframe.denominator = vc->fps;
  801. return 0;
  802. }
  803. static int tw686x_s_parm(struct file *file, void *priv,
  804. struct v4l2_streamparm *sp)
  805. {
  806. struct tw686x_video_channel *vc = video_drvdata(file);
  807. struct v4l2_captureparm *cp = &sp->parm.capture;
  808. unsigned int denominator = cp->timeperframe.denominator;
  809. unsigned int numerator = cp->timeperframe.numerator;
  810. unsigned int fps;
  811. if (vb2_is_busy(&vc->vidq))
  812. return -EBUSY;
  813. fps = (!numerator || !denominator) ? 0 : denominator / numerator;
  814. if (vc->fps != fps)
  815. tw686x_set_framerate(vc, fps);
  816. return tw686x_g_parm(file, priv, sp);
  817. }
  818. static int tw686x_enum_fmt_vid_cap(struct file *file, void *priv,
  819. struct v4l2_fmtdesc *f)
  820. {
  821. if (f->index >= ARRAY_SIZE(formats))
  822. return -EINVAL;
  823. f->pixelformat = formats[f->index].fourcc;
  824. return 0;
  825. }
  826. static void tw686x_set_input(struct tw686x_video_channel *vc, unsigned int i)
  827. {
  828. u32 val;
  829. vc->input = i;
  830. val = reg_read(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch]);
  831. val &= ~(0x3 << 30);
  832. val |= i << 30;
  833. reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], val);
  834. }
  835. static int tw686x_s_input(struct file *file, void *priv, unsigned int i)
  836. {
  837. struct tw686x_video_channel *vc = video_drvdata(file);
  838. if (i >= TW686X_INPUTS_PER_CH)
  839. return -EINVAL;
  840. if (i == vc->input)
  841. return 0;
  842. /*
  843. * Not sure we are able to support on the fly input change
  844. */
  845. if (vb2_is_busy(&vc->vidq))
  846. return -EBUSY;
  847. tw686x_set_input(vc, i);
  848. return 0;
  849. }
  850. static int tw686x_g_input(struct file *file, void *priv, unsigned int *i)
  851. {
  852. struct tw686x_video_channel *vc = video_drvdata(file);
  853. *i = vc->input;
  854. return 0;
  855. }
  856. static int tw686x_enum_input(struct file *file, void *priv,
  857. struct v4l2_input *i)
  858. {
  859. struct tw686x_video_channel *vc = video_drvdata(file);
  860. unsigned int vidstat;
  861. if (i->index >= TW686X_INPUTS_PER_CH)
  862. return -EINVAL;
  863. snprintf(i->name, sizeof(i->name), "Composite%d", i->index);
  864. i->type = V4L2_INPUT_TYPE_CAMERA;
  865. i->std = vc->device->tvnorms;
  866. i->capabilities = V4L2_IN_CAP_STD;
  867. vidstat = reg_read(vc->dev, VIDSTAT[vc->ch]);
  868. i->status = 0;
  869. if (vidstat & TW686X_VIDSTAT_VDLOSS)
  870. i->status |= V4L2_IN_ST_NO_SIGNAL;
  871. if (!(vidstat & TW686X_VIDSTAT_HLOCK))
  872. i->status |= V4L2_IN_ST_NO_H_LOCK;
  873. return 0;
  874. }
  875. static const struct v4l2_file_operations tw686x_video_fops = {
  876. .owner = THIS_MODULE,
  877. .open = v4l2_fh_open,
  878. .unlocked_ioctl = video_ioctl2,
  879. .release = vb2_fop_release,
  880. .poll = vb2_fop_poll,
  881. .read = vb2_fop_read,
  882. .mmap = vb2_fop_mmap,
  883. };
  884. static const struct v4l2_ioctl_ops tw686x_video_ioctl_ops = {
  885. .vidioc_querycap = tw686x_querycap,
  886. .vidioc_g_fmt_vid_cap = tw686x_g_fmt_vid_cap,
  887. .vidioc_s_fmt_vid_cap = tw686x_s_fmt_vid_cap,
  888. .vidioc_enum_fmt_vid_cap = tw686x_enum_fmt_vid_cap,
  889. .vidioc_try_fmt_vid_cap = tw686x_try_fmt_vid_cap,
  890. .vidioc_querystd = tw686x_querystd,
  891. .vidioc_g_std = tw686x_g_std,
  892. .vidioc_s_std = tw686x_s_std,
  893. .vidioc_g_parm = tw686x_g_parm,
  894. .vidioc_s_parm = tw686x_s_parm,
  895. .vidioc_enum_framesizes = tw686x_enum_framesizes,
  896. .vidioc_enum_frameintervals = tw686x_enum_frameintervals,
  897. .vidioc_enum_input = tw686x_enum_input,
  898. .vidioc_g_input = tw686x_g_input,
  899. .vidioc_s_input = tw686x_s_input,
  900. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  901. .vidioc_querybuf = vb2_ioctl_querybuf,
  902. .vidioc_qbuf = vb2_ioctl_qbuf,
  903. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  904. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  905. .vidioc_streamon = vb2_ioctl_streamon,
  906. .vidioc_streamoff = vb2_ioctl_streamoff,
  907. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  908. .vidioc_log_status = v4l2_ctrl_log_status,
  909. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  910. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  911. };
  912. void tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests,
  913. unsigned int pb_status, unsigned int fifo_status,
  914. unsigned int *reset_ch)
  915. {
  916. struct tw686x_video_channel *vc;
  917. unsigned long flags;
  918. unsigned int ch, pb;
  919. for_each_set_bit(ch, &requests, max_channels(dev)) {
  920. vc = &dev->video_channels[ch];
  921. /*
  922. * This can either be a blue frame (with signal-lost bit set)
  923. * or a good frame (with signal-lost bit clear). If we have just
  924. * got signal, then this channel needs resetting.
  925. */
  926. if (vc->no_signal && !(fifo_status & BIT(ch))) {
  927. v4l2_printk(KERN_DEBUG, &dev->v4l2_dev,
  928. "video%d: signal recovered\n", vc->num);
  929. vc->no_signal = false;
  930. *reset_ch |= BIT(ch);
  931. vc->pb = 0;
  932. continue;
  933. }
  934. vc->no_signal = !!(fifo_status & BIT(ch));
  935. /* Check FIFO errors only if there's signal */
  936. if (!vc->no_signal) {
  937. u32 fifo_ov, fifo_bad;
  938. fifo_ov = (fifo_status >> 24) & BIT(ch);
  939. fifo_bad = (fifo_status >> 16) & BIT(ch);
  940. if (fifo_ov || fifo_bad) {
  941. /* Mark this channel for reset */
  942. v4l2_printk(KERN_DEBUG, &dev->v4l2_dev,
  943. "video%d: FIFO error\n", vc->num);
  944. *reset_ch |= BIT(ch);
  945. vc->pb = 0;
  946. continue;
  947. }
  948. }
  949. pb = !!(pb_status & BIT(ch));
  950. if (vc->pb != pb) {
  951. /* Mark this channel for reset */
  952. v4l2_printk(KERN_DEBUG, &dev->v4l2_dev,
  953. "video%d: unexpected p-b buffer!\n",
  954. vc->num);
  955. *reset_ch |= BIT(ch);
  956. vc->pb = 0;
  957. continue;
  958. }
  959. spin_lock_irqsave(&vc->qlock, flags);
  960. tw686x_buf_done(vc, pb);
  961. dev->dma_ops->buf_refill(vc, pb);
  962. spin_unlock_irqrestore(&vc->qlock, flags);
  963. }
  964. }
  965. void tw686x_video_free(struct tw686x_dev *dev)
  966. {
  967. unsigned int ch, pb;
  968. for (ch = 0; ch < max_channels(dev); ch++) {
  969. struct tw686x_video_channel *vc = &dev->video_channels[ch];
  970. video_unregister_device(vc->device);
  971. if (dev->dma_ops->free)
  972. for (pb = 0; pb < 2; pb++)
  973. dev->dma_ops->free(vc, pb);
  974. }
  975. }
  976. int tw686x_video_init(struct tw686x_dev *dev)
  977. {
  978. unsigned int ch, val;
  979. int err;
  980. if (dev->dma_mode == TW686X_DMA_MODE_MEMCPY)
  981. dev->dma_ops = &memcpy_dma_ops;
  982. else if (dev->dma_mode == TW686X_DMA_MODE_CONTIG)
  983. dev->dma_ops = &contig_dma_ops;
  984. else if (dev->dma_mode == TW686X_DMA_MODE_SG)
  985. dev->dma_ops = &sg_dma_ops;
  986. else
  987. return -EINVAL;
  988. err = v4l2_device_register(&dev->pci_dev->dev, &dev->v4l2_dev);
  989. if (err)
  990. return err;
  991. if (dev->dma_ops->setup) {
  992. err = dev->dma_ops->setup(dev);
  993. if (err)
  994. return err;
  995. }
  996. /* Initialize vc->dev and vc->ch for the error path */
  997. for (ch = 0; ch < max_channels(dev); ch++) {
  998. struct tw686x_video_channel *vc = &dev->video_channels[ch];
  999. vc->dev = dev;
  1000. vc->ch = ch;
  1001. }
  1002. for (ch = 0; ch < max_channels(dev); ch++) {
  1003. struct tw686x_video_channel *vc = &dev->video_channels[ch];
  1004. struct video_device *vdev;
  1005. mutex_init(&vc->vb_mutex);
  1006. spin_lock_init(&vc->qlock);
  1007. INIT_LIST_HEAD(&vc->vidq_queued);
  1008. /* default settings */
  1009. err = tw686x_set_standard(vc, V4L2_STD_NTSC);
  1010. if (err)
  1011. goto error;
  1012. err = tw686x_set_format(vc, formats[0].fourcc,
  1013. TW686X_VIDEO_WIDTH,
  1014. TW686X_VIDEO_HEIGHT(vc->video_standard),
  1015. true);
  1016. if (err)
  1017. goto error;
  1018. tw686x_set_input(vc, 0);
  1019. tw686x_set_framerate(vc, 30);
  1020. reg_write(dev, VDELAY_LO[ch], 0x14);
  1021. reg_write(dev, HACTIVE_LO[ch], 0xd0);
  1022. reg_write(dev, VIDEO_SIZE[ch], 0);
  1023. vc->vidq.io_modes = VB2_READ | VB2_MMAP | VB2_DMABUF;
  1024. vc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1025. vc->vidq.drv_priv = vc;
  1026. vc->vidq.buf_struct_size = sizeof(struct tw686x_v4l2_buf);
  1027. vc->vidq.ops = &tw686x_video_qops;
  1028. vc->vidq.mem_ops = dev->dma_ops->mem_ops;
  1029. vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1030. vc->vidq.min_buffers_needed = 2;
  1031. vc->vidq.lock = &vc->vb_mutex;
  1032. vc->vidq.gfp_flags = dev->dma_mode != TW686X_DMA_MODE_MEMCPY ?
  1033. GFP_DMA32 : 0;
  1034. vc->vidq.dev = &dev->pci_dev->dev;
  1035. err = vb2_queue_init(&vc->vidq);
  1036. if (err) {
  1037. v4l2_err(&dev->v4l2_dev,
  1038. "dma%d: cannot init vb2 queue\n", ch);
  1039. goto error;
  1040. }
  1041. err = v4l2_ctrl_handler_init(&vc->ctrl_handler, 4);
  1042. if (err) {
  1043. v4l2_err(&dev->v4l2_dev,
  1044. "dma%d: cannot init ctrl handler\n", ch);
  1045. goto error;
  1046. }
  1047. v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
  1048. V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
  1049. v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
  1050. V4L2_CID_CONTRAST, 0, 255, 1, 100);
  1051. v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
  1052. V4L2_CID_SATURATION, 0, 255, 1, 128);
  1053. v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
  1054. V4L2_CID_HUE, -128, 127, 1, 0);
  1055. err = vc->ctrl_handler.error;
  1056. if (err)
  1057. goto error;
  1058. err = v4l2_ctrl_handler_setup(&vc->ctrl_handler);
  1059. if (err)
  1060. goto error;
  1061. vdev = video_device_alloc();
  1062. if (!vdev) {
  1063. v4l2_err(&dev->v4l2_dev,
  1064. "dma%d: unable to allocate device\n", ch);
  1065. err = -ENOMEM;
  1066. goto error;
  1067. }
  1068. snprintf(vdev->name, sizeof(vdev->name), "%s video", dev->name);
  1069. vdev->fops = &tw686x_video_fops;
  1070. vdev->ioctl_ops = &tw686x_video_ioctl_ops;
  1071. vdev->release = video_device_release;
  1072. vdev->v4l2_dev = &dev->v4l2_dev;
  1073. vdev->queue = &vc->vidq;
  1074. vdev->tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50;
  1075. vdev->minor = -1;
  1076. vdev->lock = &vc->vb_mutex;
  1077. vdev->ctrl_handler = &vc->ctrl_handler;
  1078. vc->device = vdev;
  1079. video_set_drvdata(vdev, vc);
  1080. err = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  1081. if (err < 0)
  1082. goto error;
  1083. vc->num = vdev->num;
  1084. }
  1085. val = TW686X_DEF_PHASE_REF;
  1086. for (ch = 0; ch < max_channels(dev); ch++)
  1087. val |= dev->dma_ops->hw_dma_mode << (16 + ch * 2);
  1088. reg_write(dev, PHASE_REF, val);
  1089. reg_write(dev, MISC2[0], 0xe7);
  1090. reg_write(dev, VCTRL1[0], 0xcc);
  1091. reg_write(dev, LOOP[0], 0xa5);
  1092. if (max_channels(dev) > 4) {
  1093. reg_write(dev, VCTRL1[1], 0xcc);
  1094. reg_write(dev, LOOP[1], 0xa5);
  1095. reg_write(dev, MISC2[1], 0xe7);
  1096. }
  1097. return 0;
  1098. error:
  1099. tw686x_video_free(dev);
  1100. return err;
  1101. }