tw686x-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * Notes
  13. * -----
  14. *
  15. * 1. Under stress-testing, it has been observed that the PCIe link
  16. * goes down, without reason. Therefore, the driver takes special care
  17. * to allow device hot-unplugging.
  18. *
  19. * 2. TW686X devices are capable of setting a few different DMA modes,
  20. * including: scatter-gather, field and frame modes. However,
  21. * under stress testings it has been found that the machine can
  22. * freeze completely if DMA registers are programmed while streaming
  23. * is active.
  24. *
  25. * Therefore, driver implements a dma_mode called 'memcpy' which
  26. * avoids cycling the DMA buffers, and insteads allocates extra DMA buffers
  27. * and then copies into vmalloc'ed user buffers.
  28. *
  29. * In addition to this, when streaming is on, the driver tries to access
  30. * hardware registers as infrequently as possible. This is done by using
  31. * a timer to limit the rate at which DMA is reset on DMA channels error.
  32. */
  33. #include <linux/init.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/delay.h>
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/pci_ids.h>
  39. #include <linux/slab.h>
  40. #include <linux/timer.h>
  41. #include "tw686x.h"
  42. #include "tw686x-regs.h"
  43. /*
  44. * This module parameter allows to control the DMA_TIMER_INTERVAL value.
  45. * The DMA_TIMER_INTERVAL register controls the minimum DMA interrupt
  46. * time span (iow, the maximum DMA interrupt rate) thus allowing for
  47. * IRQ coalescing.
  48. *
  49. * The chip datasheet does not mention a time unit for this value, so
  50. * users wanting fine-grain control over the interrupt rate should
  51. * determine the desired value through testing.
  52. */
  53. static u32 dma_interval = 0x00098968;
  54. module_param(dma_interval, int, 0444);
  55. MODULE_PARM_DESC(dma_interval, "Minimum time span for DMA interrupting host");
  56. static unsigned int dma_mode = TW686X_DMA_MODE_MEMCPY;
  57. static const char *dma_mode_name(unsigned int mode)
  58. {
  59. switch (mode) {
  60. case TW686X_DMA_MODE_MEMCPY:
  61. return "memcpy";
  62. case TW686X_DMA_MODE_CONTIG:
  63. return "contig";
  64. case TW686X_DMA_MODE_SG:
  65. return "sg";
  66. default:
  67. return "unknown";
  68. }
  69. }
  70. static int tw686x_dma_mode_get(char *buffer, const struct kernel_param *kp)
  71. {
  72. return sprintf(buffer, "%s", dma_mode_name(dma_mode));
  73. }
  74. static int tw686x_dma_mode_set(const char *val, const struct kernel_param *kp)
  75. {
  76. if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_MEMCPY)))
  77. dma_mode = TW686X_DMA_MODE_MEMCPY;
  78. else if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_CONTIG)))
  79. dma_mode = TW686X_DMA_MODE_CONTIG;
  80. else if (!strcasecmp(val, dma_mode_name(TW686X_DMA_MODE_SG)))
  81. dma_mode = TW686X_DMA_MODE_SG;
  82. else
  83. return -EINVAL;
  84. return 0;
  85. }
  86. module_param_call(dma_mode, tw686x_dma_mode_set, tw686x_dma_mode_get,
  87. &dma_mode, S_IRUGO|S_IWUSR);
  88. MODULE_PARM_DESC(dma_mode, "DMA operation mode (memcpy/contig/sg, default=memcpy)");
  89. void tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel)
  90. {
  91. u32 dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
  92. u32 dma_cmd = reg_read(dev, DMA_CMD);
  93. dma_en &= ~BIT(channel);
  94. dma_cmd &= ~BIT(channel);
  95. /* Must remove it from pending too */
  96. dev->pending_dma_en &= ~BIT(channel);
  97. dev->pending_dma_cmd &= ~BIT(channel);
  98. /* Stop DMA if no channels are enabled */
  99. if (!dma_en)
  100. dma_cmd = 0;
  101. reg_write(dev, DMA_CHANNEL_ENABLE, dma_en);
  102. reg_write(dev, DMA_CMD, dma_cmd);
  103. }
  104. void tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel)
  105. {
  106. u32 dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
  107. u32 dma_cmd = reg_read(dev, DMA_CMD);
  108. dev->pending_dma_en |= dma_en | BIT(channel);
  109. dev->pending_dma_cmd |= dma_cmd | DMA_CMD_ENABLE | BIT(channel);
  110. }
  111. /*
  112. * The purpose of this awful hack is to avoid enabling the DMA
  113. * channels "too fast" which makes some TW686x devices very
  114. * angry and freeze the CPU (see note 1).
  115. */
  116. static void tw686x_dma_delay(struct timer_list *t)
  117. {
  118. struct tw686x_dev *dev = from_timer(dev, t, dma_delay_timer);
  119. unsigned long flags;
  120. spin_lock_irqsave(&dev->lock, flags);
  121. reg_write(dev, DMA_CHANNEL_ENABLE, dev->pending_dma_en);
  122. reg_write(dev, DMA_CMD, dev->pending_dma_cmd);
  123. dev->pending_dma_en = 0;
  124. dev->pending_dma_cmd = 0;
  125. spin_unlock_irqrestore(&dev->lock, flags);
  126. }
  127. static void tw686x_reset_channels(struct tw686x_dev *dev, unsigned int ch_mask)
  128. {
  129. u32 dma_en, dma_cmd;
  130. dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
  131. dma_cmd = reg_read(dev, DMA_CMD);
  132. /*
  133. * Save pending register status, the timer will
  134. * restore them.
  135. */
  136. dev->pending_dma_en |= dma_en;
  137. dev->pending_dma_cmd |= dma_cmd;
  138. /* Disable the reset channels */
  139. reg_write(dev, DMA_CHANNEL_ENABLE, dma_en & ~ch_mask);
  140. if ((dma_en & ~ch_mask) == 0) {
  141. dev_dbg(&dev->pci_dev->dev, "reset: stopping DMA\n");
  142. dma_cmd &= ~DMA_CMD_ENABLE;
  143. }
  144. reg_write(dev, DMA_CMD, dma_cmd & ~ch_mask);
  145. }
  146. static irqreturn_t tw686x_irq(int irq, void *dev_id)
  147. {
  148. struct tw686x_dev *dev = (struct tw686x_dev *)dev_id;
  149. unsigned int video_requests, audio_requests, reset_ch;
  150. u32 fifo_status, fifo_signal, fifo_ov, fifo_bad, fifo_errors;
  151. u32 int_status, dma_en, video_en, pb_status;
  152. unsigned long flags;
  153. int_status = reg_read(dev, INT_STATUS); /* cleared on read */
  154. fifo_status = reg_read(dev, VIDEO_FIFO_STATUS);
  155. /* INT_STATUS does not include FIFO_STATUS errors! */
  156. if (!int_status && !TW686X_FIFO_ERROR(fifo_status))
  157. return IRQ_NONE;
  158. if (int_status & INT_STATUS_DMA_TOUT) {
  159. dev_dbg(&dev->pci_dev->dev,
  160. "DMA timeout. Resetting DMA for all channels\n");
  161. reset_ch = ~0;
  162. goto reset_channels;
  163. }
  164. spin_lock_irqsave(&dev->lock, flags);
  165. dma_en = reg_read(dev, DMA_CHANNEL_ENABLE);
  166. spin_unlock_irqrestore(&dev->lock, flags);
  167. video_en = dma_en & 0xff;
  168. fifo_signal = ~(fifo_status & 0xff) & video_en;
  169. fifo_ov = fifo_status >> 24;
  170. fifo_bad = fifo_status >> 16;
  171. /* Mask of channels with signal and FIFO errors */
  172. fifo_errors = fifo_signal & (fifo_ov | fifo_bad);
  173. reset_ch = 0;
  174. pb_status = reg_read(dev, PB_STATUS);
  175. /* Coalesce video frame/error events */
  176. video_requests = (int_status & video_en) | fifo_errors;
  177. audio_requests = (int_status & dma_en) >> 8;
  178. if (video_requests)
  179. tw686x_video_irq(dev, video_requests, pb_status,
  180. fifo_status, &reset_ch);
  181. if (audio_requests)
  182. tw686x_audio_irq(dev, audio_requests, pb_status);
  183. reset_channels:
  184. if (reset_ch) {
  185. spin_lock_irqsave(&dev->lock, flags);
  186. tw686x_reset_channels(dev, reset_ch);
  187. spin_unlock_irqrestore(&dev->lock, flags);
  188. mod_timer(&dev->dma_delay_timer,
  189. jiffies + msecs_to_jiffies(100));
  190. }
  191. return IRQ_HANDLED;
  192. }
  193. static void tw686x_dev_release(struct v4l2_device *v4l2_dev)
  194. {
  195. struct tw686x_dev *dev = container_of(v4l2_dev, struct tw686x_dev,
  196. v4l2_dev);
  197. unsigned int ch;
  198. for (ch = 0; ch < max_channels(dev); ch++)
  199. v4l2_ctrl_handler_free(&dev->video_channels[ch].ctrl_handler);
  200. v4l2_device_unregister(&dev->v4l2_dev);
  201. kfree(dev->audio_channels);
  202. kfree(dev->video_channels);
  203. kfree(dev);
  204. }
  205. static int tw686x_probe(struct pci_dev *pci_dev,
  206. const struct pci_device_id *pci_id)
  207. {
  208. struct tw686x_dev *dev;
  209. int err;
  210. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  211. if (!dev)
  212. return -ENOMEM;
  213. dev->type = pci_id->driver_data;
  214. dev->dma_mode = dma_mode;
  215. sprintf(dev->name, "tw%04X", pci_dev->device);
  216. dev->video_channels = kcalloc(max_channels(dev),
  217. sizeof(*dev->video_channels), GFP_KERNEL);
  218. if (!dev->video_channels) {
  219. err = -ENOMEM;
  220. goto free_dev;
  221. }
  222. dev->audio_channels = kcalloc(max_channels(dev),
  223. sizeof(*dev->audio_channels), GFP_KERNEL);
  224. if (!dev->audio_channels) {
  225. err = -ENOMEM;
  226. goto free_video;
  227. }
  228. pr_info("%s: PCI %s, IRQ %d, MMIO 0x%lx (%s mode)\n", dev->name,
  229. pci_name(pci_dev), pci_dev->irq,
  230. (unsigned long)pci_resource_start(pci_dev, 0),
  231. dma_mode_name(dma_mode));
  232. dev->pci_dev = pci_dev;
  233. if (pci_enable_device(pci_dev)) {
  234. err = -EIO;
  235. goto free_audio;
  236. }
  237. pci_set_master(pci_dev);
  238. err = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  239. if (err) {
  240. dev_err(&pci_dev->dev, "32-bit PCI DMA not supported\n");
  241. err = -EIO;
  242. goto disable_pci;
  243. }
  244. err = pci_request_regions(pci_dev, dev->name);
  245. if (err) {
  246. dev_err(&pci_dev->dev, "unable to request PCI region\n");
  247. goto disable_pci;
  248. }
  249. dev->mmio = pci_ioremap_bar(pci_dev, 0);
  250. if (!dev->mmio) {
  251. dev_err(&pci_dev->dev, "unable to remap PCI region\n");
  252. err = -ENOMEM;
  253. goto free_region;
  254. }
  255. /* Reset all subsystems */
  256. reg_write(dev, SYS_SOFT_RST, 0x0f);
  257. mdelay(1);
  258. reg_write(dev, SRST[0], 0x3f);
  259. if (max_channels(dev) > 4)
  260. reg_write(dev, SRST[1], 0x3f);
  261. /* Disable the DMA engine */
  262. reg_write(dev, DMA_CMD, 0);
  263. reg_write(dev, DMA_CHANNEL_ENABLE, 0);
  264. /* Enable DMA FIFO overflow and pointer check */
  265. reg_write(dev, DMA_CONFIG, 0xffffff04);
  266. reg_write(dev, DMA_CHANNEL_TIMEOUT, 0x140c8584);
  267. reg_write(dev, DMA_TIMER_INTERVAL, dma_interval);
  268. spin_lock_init(&dev->lock);
  269. err = request_irq(pci_dev->irq, tw686x_irq, IRQF_SHARED,
  270. dev->name, dev);
  271. if (err < 0) {
  272. dev_err(&pci_dev->dev, "unable to request interrupt\n");
  273. goto iounmap;
  274. }
  275. timer_setup(&dev->dma_delay_timer, tw686x_dma_delay, 0);
  276. /*
  277. * This must be set right before initializing v4l2_dev.
  278. * It's used to release resources after the last handle
  279. * held is released.
  280. */
  281. dev->v4l2_dev.release = tw686x_dev_release;
  282. err = tw686x_video_init(dev);
  283. if (err) {
  284. dev_err(&pci_dev->dev, "can't register video\n");
  285. goto free_irq;
  286. }
  287. err = tw686x_audio_init(dev);
  288. if (err)
  289. dev_warn(&pci_dev->dev, "can't register audio\n");
  290. pci_set_drvdata(pci_dev, dev);
  291. return 0;
  292. free_irq:
  293. free_irq(pci_dev->irq, dev);
  294. iounmap:
  295. pci_iounmap(pci_dev, dev->mmio);
  296. free_region:
  297. pci_release_regions(pci_dev);
  298. disable_pci:
  299. pci_disable_device(pci_dev);
  300. free_audio:
  301. kfree(dev->audio_channels);
  302. free_video:
  303. kfree(dev->video_channels);
  304. free_dev:
  305. kfree(dev);
  306. return err;
  307. }
  308. static void tw686x_remove(struct pci_dev *pci_dev)
  309. {
  310. struct tw686x_dev *dev = pci_get_drvdata(pci_dev);
  311. unsigned long flags;
  312. /* This guarantees the IRQ handler is no longer running,
  313. * which means we can kiss good-bye some resources.
  314. */
  315. free_irq(pci_dev->irq, dev);
  316. tw686x_video_free(dev);
  317. tw686x_audio_free(dev);
  318. del_timer_sync(&dev->dma_delay_timer);
  319. pci_iounmap(pci_dev, dev->mmio);
  320. pci_release_regions(pci_dev);
  321. pci_disable_device(pci_dev);
  322. /*
  323. * Setting pci_dev to NULL allows to detect hardware is no longer
  324. * available and will be used by vb2_ops. This is required because
  325. * the device sometimes hot-unplugs itself as the result of a PCIe
  326. * link down.
  327. * The lock is really important here.
  328. */
  329. spin_lock_irqsave(&dev->lock, flags);
  330. dev->pci_dev = NULL;
  331. spin_unlock_irqrestore(&dev->lock, flags);
  332. /*
  333. * This calls tw686x_dev_release if it's the last reference.
  334. * Otherwise, release is postponed until there are no users left.
  335. */
  336. v4l2_device_put(&dev->v4l2_dev);
  337. }
  338. /*
  339. * On TW6864 and TW6868, all channels share the pair of video DMA SG tables,
  340. * with 10-bit start_idx and end_idx determining start and end of frame buffer
  341. * for particular channel.
  342. * TW6868 with all its 8 channels would be problematic (only 127 SG entries per
  343. * channel) but we support only 4 channels on this chip anyway (the first
  344. * 4 channels are driven with internal video decoder, the other 4 would require
  345. * an external TW286x part).
  346. *
  347. * On TW6865 and TW6869, each channel has its own DMA SG table, with indexes
  348. * starting with 0. Both chips have complete sets of internal video decoders
  349. * (respectively 4 or 8-channel).
  350. *
  351. * All chips have separate SG tables for two video frames.
  352. */
  353. /* driver_data is number of A/V channels */
  354. static const struct pci_device_id tw686x_pci_tbl[] = {
  355. {
  356. PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6864),
  357. .driver_data = 4
  358. },
  359. {
  360. PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6865), /* not tested */
  361. .driver_data = 4 | TYPE_SECOND_GEN
  362. },
  363. /*
  364. * TW6868 supports 8 A/V channels with an external TW2865 chip;
  365. * not supported by the driver.
  366. */
  367. {
  368. PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6868), /* not tested */
  369. .driver_data = 4
  370. },
  371. {
  372. PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, 0x6869),
  373. .driver_data = 8 | TYPE_SECOND_GEN},
  374. {}
  375. };
  376. MODULE_DEVICE_TABLE(pci, tw686x_pci_tbl);
  377. static struct pci_driver tw686x_pci_driver = {
  378. .name = "tw686x",
  379. .id_table = tw686x_pci_tbl,
  380. .probe = tw686x_probe,
  381. .remove = tw686x_remove,
  382. };
  383. module_pci_driver(tw686x_pci_driver);
  384. MODULE_DESCRIPTION("Driver for video frame grabber cards based on Intersil/Techwell TW686[4589]");
  385. MODULE_AUTHOR("Ezequiel Garcia <ezequiel@vanguardiasur.com.ar>");
  386. MODULE_AUTHOR("Krzysztof Ha?asa <khalasa@piap.pl>");
  387. MODULE_LICENSE("GPL v2");