tw686x-video.c 33 KB

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