tw5864-core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * TW5864 driver - core functions
  3. *
  4. * Copyright (C) 2016 Bluecherry, LLC <maintainers@bluecherrydvr.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/kmod.h>
  22. #include <linux/sound.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/pm.h>
  27. #include <linux/pci_ids.h>
  28. #include <linux/jiffies.h>
  29. #include <asm/dma.h>
  30. #include <media/v4l2-dev.h>
  31. #include "tw5864.h"
  32. #include "tw5864-reg.h"
  33. MODULE_DESCRIPTION("V4L2 driver module for tw5864-based multimedia capture & encoding devices");
  34. MODULE_AUTHOR("Bluecherry Maintainers <maintainers@bluecherrydvr.com>");
  35. MODULE_AUTHOR("Andrey Utkin <andrey.utkin@corp.bluecherry.net>");
  36. MODULE_LICENSE("GPL");
  37. /*
  38. * BEWARE OF KNOWN ISSUES WITH VIDEO QUALITY
  39. *
  40. * This driver was developed by Bluecherry LLC by deducing behaviour of
  41. * original manufacturer's driver, from both source code and execution traces.
  42. * It is known that there are some artifacts on output video with this driver:
  43. * - on all known hardware samples: random pixels of wrong color (mostly
  44. * white, red or blue) appearing and disappearing on sequences of P-frames;
  45. * - on some hardware samples (known with H.264 core version e006:2800):
  46. * total madness on P-frames: blocks of wrong luminance; blocks of wrong
  47. * colors "creeping" across the picture.
  48. * There is a workaround for both issues: avoid P-frames by setting GOP size
  49. * to 1. To do that, run this command on device files created by this driver:
  50. *
  51. * v4l2-ctl --device /dev/videoX --set-ctrl=video_gop_size=1
  52. *
  53. * These issues are not decoding errors; all produced H.264 streams are decoded
  54. * properly. Streams without P-frames don't have these artifacts so it's not
  55. * analog-to-digital conversion issues nor internal memory errors; we conclude
  56. * it's internal H.264 encoder issues.
  57. * We cannot even check the original driver's behaviour because it has never
  58. * worked properly at all in our development environment. So these issues may
  59. * be actually related to firmware or hardware. However it may be that there's
  60. * just some more register settings missing in the driver which would please
  61. * the hardware.
  62. * Manufacturer didn't help much on our inquiries, but feel free to disturb
  63. * again the support of Intersil (owner of former Techwell).
  64. */
  65. /* take first free /dev/videoX indexes by default */
  66. static unsigned int video_nr[] = {[0 ... (TW5864_INPUTS - 1)] = -1 };
  67. module_param_array(video_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(video_nr, "video devices numbers array");
  69. /*
  70. * Please add any new PCI IDs to: http://pci-ids.ucw.cz. This keeps
  71. * the PCI ID database up to date. Note that the entries must be
  72. * added under vendor 0x1797 (Techwell Inc.) as subsystem IDs.
  73. */
  74. static const struct pci_device_id tw5864_pci_tbl[] = {
  75. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_TECHWELL_5864)},
  76. {0,}
  77. };
  78. void tw5864_irqmask_apply(struct tw5864_dev *dev)
  79. {
  80. tw_writel(TW5864_INTR_ENABLE_L, dev->irqmask & 0xffff);
  81. tw_writel(TW5864_INTR_ENABLE_H, (dev->irqmask >> 16));
  82. }
  83. static void tw5864_interrupts_disable(struct tw5864_dev *dev)
  84. {
  85. unsigned long flags;
  86. spin_lock_irqsave(&dev->slock, flags);
  87. dev->irqmask = 0;
  88. tw5864_irqmask_apply(dev);
  89. spin_unlock_irqrestore(&dev->slock, flags);
  90. }
  91. static void tw5864_timer_isr(struct tw5864_dev *dev);
  92. static void tw5864_h264_isr(struct tw5864_dev *dev);
  93. static irqreturn_t tw5864_isr(int irq, void *dev_id)
  94. {
  95. struct tw5864_dev *dev = dev_id;
  96. u32 status;
  97. status = tw_readl(TW5864_INTR_STATUS_L) |
  98. tw_readl(TW5864_INTR_STATUS_H) << 16;
  99. if (!status)
  100. return IRQ_NONE;
  101. tw_writel(TW5864_INTR_CLR_L, 0xffff);
  102. tw_writel(TW5864_INTR_CLR_H, 0xffff);
  103. if (status & TW5864_INTR_VLC_DONE)
  104. tw5864_h264_isr(dev);
  105. if (status & TW5864_INTR_TIMER)
  106. tw5864_timer_isr(dev);
  107. if (!(status & (TW5864_INTR_TIMER | TW5864_INTR_VLC_DONE))) {
  108. dev_dbg(&dev->pci->dev, "Unknown interrupt, status 0x%08X\n",
  109. status);
  110. }
  111. return IRQ_HANDLED;
  112. }
  113. static void tw5864_h264_isr(struct tw5864_dev *dev)
  114. {
  115. int channel = tw_readl(TW5864_DSP) & TW5864_DSP_ENC_CHN;
  116. struct tw5864_input *input = &dev->inputs[channel];
  117. int cur_frame_index, next_frame_index;
  118. struct tw5864_h264_frame *cur_frame, *next_frame;
  119. unsigned long flags;
  120. spin_lock_irqsave(&dev->slock, flags);
  121. cur_frame_index = dev->h264_buf_w_index;
  122. next_frame_index = (cur_frame_index + 1) % H264_BUF_CNT;
  123. cur_frame = &dev->h264_buf[cur_frame_index];
  124. next_frame = &dev->h264_buf[next_frame_index];
  125. if (next_frame_index != dev->h264_buf_r_index) {
  126. cur_frame->vlc_len = tw_readl(TW5864_VLC_LENGTH) << 2;
  127. cur_frame->checksum = tw_readl(TW5864_VLC_CRC_REG);
  128. cur_frame->input = input;
  129. cur_frame->timestamp = ktime_get_ns();
  130. cur_frame->seqno = input->frame_seqno;
  131. cur_frame->gop_seqno = input->frame_gop_seqno;
  132. dev->h264_buf_w_index = next_frame_index;
  133. tasklet_schedule(&dev->tasklet);
  134. cur_frame = next_frame;
  135. spin_lock(&input->slock);
  136. input->frame_seqno++;
  137. input->frame_gop_seqno++;
  138. if (input->frame_gop_seqno >= input->gop)
  139. input->frame_gop_seqno = 0;
  140. spin_unlock(&input->slock);
  141. } else {
  142. dev_err(&dev->pci->dev,
  143. "Skipped frame on input %d because all buffers busy\n",
  144. channel);
  145. }
  146. dev->encoder_busy = 0;
  147. spin_unlock_irqrestore(&dev->slock, flags);
  148. tw_writel(TW5864_VLC_STREAM_BASE_ADDR, cur_frame->vlc.dma_addr);
  149. tw_writel(TW5864_MV_STREAM_BASE_ADDR, cur_frame->mv.dma_addr);
  150. /* Additional ack for this interrupt */
  151. tw_writel(TW5864_VLC_DSP_INTR, 0x00000001);
  152. tw_writel(TW5864_PCI_INTR_STATUS, TW5864_VLC_DONE_INTR);
  153. }
  154. static void tw5864_input_deadline_update(struct tw5864_input *input)
  155. {
  156. input->new_frame_deadline = jiffies + msecs_to_jiffies(1000);
  157. }
  158. static void tw5864_timer_isr(struct tw5864_dev *dev)
  159. {
  160. unsigned long flags;
  161. int i;
  162. int encoder_busy;
  163. /* Additional ack for this interrupt */
  164. tw_writel(TW5864_PCI_INTR_STATUS, TW5864_TIMER_INTR);
  165. spin_lock_irqsave(&dev->slock, flags);
  166. encoder_busy = dev->encoder_busy;
  167. spin_unlock_irqrestore(&dev->slock, flags);
  168. if (encoder_busy)
  169. return;
  170. /*
  171. * Traversing inputs in round-robin fashion, starting from next to the
  172. * last processed one
  173. */
  174. for (i = 0; i < TW5864_INPUTS; i++) {
  175. int next_input = (i + dev->next_input) % TW5864_INPUTS;
  176. struct tw5864_input *input = &dev->inputs[next_input];
  177. int raw_buf_id; /* id of internal buf with last raw frame */
  178. spin_lock_irqsave(&input->slock, flags);
  179. if (!input->enabled)
  180. goto next;
  181. /* Check if new raw frame is available */
  182. raw_buf_id = tw_mask_shift_readl(TW5864_SENIF_ORG_FRM_PTR1, 0x3,
  183. 2 * input->nr);
  184. if (input->buf_id != raw_buf_id) {
  185. input->buf_id = raw_buf_id;
  186. tw5864_input_deadline_update(input);
  187. spin_unlock_irqrestore(&input->slock, flags);
  188. spin_lock_irqsave(&dev->slock, flags);
  189. dev->encoder_busy = 1;
  190. dev->next_input = (next_input + 1) % TW5864_INPUTS;
  191. spin_unlock_irqrestore(&dev->slock, flags);
  192. tw5864_request_encoded_frame(input);
  193. break;
  194. }
  195. /* No new raw frame; check if channel is stuck */
  196. if (time_is_after_jiffies(input->new_frame_deadline)) {
  197. /* If stuck, request new raw frames again */
  198. tw_mask_shift_writel(TW5864_ENC_BUF_PTR_REC1, 0x3,
  199. 2 * input->nr, input->buf_id + 3);
  200. tw5864_input_deadline_update(input);
  201. }
  202. next:
  203. spin_unlock_irqrestore(&input->slock, flags);
  204. }
  205. }
  206. static int tw5864_initdev(struct pci_dev *pci_dev,
  207. const struct pci_device_id *pci_id)
  208. {
  209. struct tw5864_dev *dev;
  210. int err;
  211. dev = devm_kzalloc(&pci_dev->dev, sizeof(*dev), GFP_KERNEL);
  212. if (!dev)
  213. return -ENOMEM;
  214. snprintf(dev->name, sizeof(dev->name), "tw5864:%s", pci_name(pci_dev));
  215. err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
  216. if (err)
  217. return err;
  218. /* pci init */
  219. dev->pci = pci_dev;
  220. err = pci_enable_device(pci_dev);
  221. if (err) {
  222. dev_err(&dev->pci->dev, "pci_enable_device() failed\n");
  223. goto unreg_v4l2;
  224. }
  225. pci_set_master(pci_dev);
  226. err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  227. if (err) {
  228. dev_err(&dev->pci->dev, "32 bit PCI DMA is not supported\n");
  229. goto disable_pci;
  230. }
  231. /* get mmio */
  232. err = pci_request_regions(pci_dev, dev->name);
  233. if (err) {
  234. dev_err(&dev->pci->dev, "Cannot request regions for MMIO\n");
  235. goto disable_pci;
  236. }
  237. dev->mmio = pci_ioremap_bar(pci_dev, 0);
  238. if (!dev->mmio) {
  239. err = -EIO;
  240. dev_err(&dev->pci->dev, "can't ioremap() MMIO memory\n");
  241. goto release_mmio;
  242. }
  243. spin_lock_init(&dev->slock);
  244. dev_info(&pci_dev->dev, "TW5864 hardware version: %04x\n",
  245. tw_readl(TW5864_HW_VERSION));
  246. dev_info(&pci_dev->dev, "TW5864 H.264 core version: %04x:%04x\n",
  247. tw_readl(TW5864_H264REV),
  248. tw_readl(TW5864_UNDECLARED_H264REV_PART2));
  249. err = tw5864_video_init(dev, video_nr);
  250. if (err)
  251. goto unmap_mmio;
  252. /* get irq */
  253. err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw5864_isr,
  254. IRQF_SHARED, "tw5864", dev);
  255. if (err < 0) {
  256. dev_err(&dev->pci->dev, "can't get IRQ %d\n", pci_dev->irq);
  257. goto fini_video;
  258. }
  259. dev_info(&pci_dev->dev, "Note: there are known video quality issues. For details\n");
  260. dev_info(&pci_dev->dev, "see the comment in drivers/media/pci/tw5864/tw5864-core.c.\n");
  261. return 0;
  262. fini_video:
  263. tw5864_video_fini(dev);
  264. unmap_mmio:
  265. iounmap(dev->mmio);
  266. release_mmio:
  267. pci_release_regions(pci_dev);
  268. disable_pci:
  269. pci_disable_device(pci_dev);
  270. unreg_v4l2:
  271. v4l2_device_unregister(&dev->v4l2_dev);
  272. return err;
  273. }
  274. static void tw5864_finidev(struct pci_dev *pci_dev)
  275. {
  276. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  277. struct tw5864_dev *dev =
  278. container_of(v4l2_dev, struct tw5864_dev, v4l2_dev);
  279. /* shutdown subsystems */
  280. tw5864_interrupts_disable(dev);
  281. /* unregister */
  282. tw5864_video_fini(dev);
  283. /* release resources */
  284. iounmap(dev->mmio);
  285. release_mem_region(pci_resource_start(pci_dev, 0),
  286. pci_resource_len(pci_dev, 0));
  287. v4l2_device_unregister(&dev->v4l2_dev);
  288. devm_kfree(&pci_dev->dev, dev);
  289. }
  290. static struct pci_driver tw5864_pci_driver = {
  291. .name = "tw5864",
  292. .id_table = tw5864_pci_tbl,
  293. .probe = tw5864_initdev,
  294. .remove = tw5864_finidev,
  295. };
  296. module_pci_driver(tw5864_pci_driver);