tw68-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * tw68-core.c
  3. * Core functions for the Techwell 68xx driver
  4. *
  5. * Much of this code is derived from the cx88 and sa7134 drivers, which
  6. * were in turn derived from the bt87x driver. The original work was by
  7. * Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
  8. * Hans Verkuil, Andy Walls and many others. Their work is gratefully
  9. * acknowledged. Full credit goes to them - any problems within this code
  10. * are mine.
  11. *
  12. * Copyright (C) 2009 William M. Brack
  13. *
  14. * Refactored and updated to the latest v4l core frameworks:
  15. *
  16. * Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. */
  28. #include <linux/init.h>
  29. #include <linux/list.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/kmod.h>
  34. #include <linux/sound.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/delay.h>
  37. #include <linux/mutex.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/pm.h>
  40. #include <media/v4l2-dev.h>
  41. #include "tw68.h"
  42. #include "tw68-reg.h"
  43. MODULE_DESCRIPTION("v4l2 driver module for tw6800 based video capture cards");
  44. MODULE_AUTHOR("William M. Brack");
  45. MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
  46. MODULE_LICENSE("GPL");
  47. static unsigned int latency = UNSET;
  48. module_param(latency, int, 0444);
  49. MODULE_PARM_DESC(latency, "pci latency timer");
  50. static unsigned int video_nr[] = {[0 ... (TW68_MAXBOARDS - 1)] = UNSET };
  51. module_param_array(video_nr, int, NULL, 0444);
  52. MODULE_PARM_DESC(video_nr, "video device number");
  53. static unsigned int card[] = {[0 ... (TW68_MAXBOARDS - 1)] = UNSET };
  54. module_param_array(card, int, NULL, 0444);
  55. MODULE_PARM_DESC(card, "card type");
  56. static atomic_t tw68_instance = ATOMIC_INIT(0);
  57. /* ------------------------------------------------------------------ */
  58. /*
  59. * Please add any new PCI IDs to: http://pci-ids.ucw.cz. This keeps
  60. * the PCI ID database up to date. Note that the entries must be
  61. * added under vendor 0x1797 (Techwell Inc.) as subsystem IDs.
  62. */
  63. static const struct pci_device_id tw68_pci_tbl[] = {
  64. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6800)},
  65. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6801)},
  66. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6804)},
  67. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6816_1)},
  68. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6816_2)},
  69. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6816_3)},
  70. {PCI_DEVICE(PCI_VENDOR_ID_TECHWELL, PCI_DEVICE_ID_6816_4)},
  71. {0,}
  72. };
  73. /* ------------------------------------------------------------------ */
  74. /*
  75. * The device is given a "soft reset". According to the specifications,
  76. * after this "all register content remain unchanged", so we also write
  77. * to all specified registers manually as well (mostly to manufacturer's
  78. * specified reset values)
  79. */
  80. static int tw68_hw_init1(struct tw68_dev *dev)
  81. {
  82. /* Assure all interrupts are disabled */
  83. tw_writel(TW68_INTMASK, 0); /* 020 */
  84. /* Clear any pending interrupts */
  85. tw_writel(TW68_INTSTAT, 0xffffffff); /* 01C */
  86. /* Stop risc processor, set default buffer level */
  87. tw_writel(TW68_DMAC, 0x1600);
  88. tw_writeb(TW68_ACNTL, 0x80); /* 218 soft reset */
  89. msleep(100);
  90. tw_writeb(TW68_INFORM, 0x40); /* 208 mux0, 27mhz xtal */
  91. tw_writeb(TW68_OPFORM, 0x04); /* 20C analog line-lock */
  92. tw_writeb(TW68_HSYNC, 0); /* 210 color-killer high sens */
  93. tw_writeb(TW68_ACNTL, 0x42); /* 218 int vref #2, chroma adc off */
  94. tw_writeb(TW68_CROP_HI, 0x02); /* 21C Hactive m.s. bits */
  95. tw_writeb(TW68_VDELAY_LO, 0x12);/* 220 Mfg specified reset value */
  96. tw_writeb(TW68_VACTIVE_LO, 0xf0);
  97. tw_writeb(TW68_HDELAY_LO, 0x0f);
  98. tw_writeb(TW68_HACTIVE_LO, 0xd0);
  99. tw_writeb(TW68_CNTRL1, 0xcd); /* 230 Wide Chroma BPF B/W
  100. * Secam reduction, Adap comb for
  101. * NTSC, Op Mode 1 */
  102. tw_writeb(TW68_VSCALE_LO, 0); /* 234 */
  103. tw_writeb(TW68_SCALE_HI, 0x11); /* 238 */
  104. tw_writeb(TW68_HSCALE_LO, 0); /* 23c */
  105. tw_writeb(TW68_BRIGHT, 0); /* 240 */
  106. tw_writeb(TW68_CONTRAST, 0x5c); /* 244 */
  107. tw_writeb(TW68_SHARPNESS, 0x51);/* 248 */
  108. tw_writeb(TW68_SAT_U, 0x80); /* 24C */
  109. tw_writeb(TW68_SAT_V, 0x80); /* 250 */
  110. tw_writeb(TW68_HUE, 0x00); /* 254 */
  111. /* TODO - Check that none of these are set by control defaults */
  112. tw_writeb(TW68_SHARP2, 0x53); /* 258 Mfg specified reset val */
  113. tw_writeb(TW68_VSHARP, 0x80); /* 25C Sharpness Coring val 8 */
  114. tw_writeb(TW68_CORING, 0x44); /* 260 CTI and Vert Peak coring */
  115. tw_writeb(TW68_CNTRL2, 0x00); /* 268 No power saving enabled */
  116. tw_writeb(TW68_SDT, 0x07); /* 270 Enable shadow reg, auto-det */
  117. tw_writeb(TW68_SDTR, 0x7f); /* 274 All stds recog, don't start */
  118. tw_writeb(TW68_CLMPG, 0x50); /* 280 Clamp end at 40 sys clocks */
  119. tw_writeb(TW68_IAGC, 0x22); /* 284 Mfg specified reset val */
  120. tw_writeb(TW68_AGCGAIN, 0xf0); /* 288 AGC gain when loop disabled */
  121. tw_writeb(TW68_PEAKWT, 0xd8); /* 28C White peak threshold */
  122. tw_writeb(TW68_CLMPL, 0x3c); /* 290 Y channel clamp level */
  123. /* tw_writeb(TW68_SYNCT, 0x38);*/ /* 294 Sync amplitude */
  124. tw_writeb(TW68_SYNCT, 0x30); /* 294 Sync amplitude */
  125. tw_writeb(TW68_MISSCNT, 0x44); /* 298 Horiz sync, VCR detect sens */
  126. tw_writeb(TW68_PCLAMP, 0x28); /* 29C Clamp pos from PLL sync */
  127. /* Bit DETV of VCNTL1 helps sync multi cams/chip board */
  128. tw_writeb(TW68_VCNTL1, 0x04); /* 2A0 */
  129. tw_writeb(TW68_VCNTL2, 0); /* 2A4 */
  130. tw_writeb(TW68_CKILL, 0x68); /* 2A8 Mfg specified reset val */
  131. tw_writeb(TW68_COMB, 0x44); /* 2AC Mfg specified reset val */
  132. tw_writeb(TW68_LDLY, 0x30); /* 2B0 Max positive luma delay */
  133. tw_writeb(TW68_MISC1, 0x14); /* 2B4 Mfg specified reset val */
  134. tw_writeb(TW68_LOOP, 0xa5); /* 2B8 Mfg specified reset val */
  135. tw_writeb(TW68_MISC2, 0xe0); /* 2BC Enable colour killer */
  136. tw_writeb(TW68_MVSN, 0); /* 2C0 */
  137. tw_writeb(TW68_CLMD, 0x05); /* 2CC slice level auto, clamp med. */
  138. tw_writeb(TW68_IDCNTL, 0); /* 2D0 Writing zero to this register
  139. * selects NTSC ID detection,
  140. * but doesn't change the
  141. * sensitivity (which has a reset
  142. * value of 1E). Since we are
  143. * not doing auto-detection, it
  144. * has no real effect */
  145. tw_writeb(TW68_CLCNTL1, 0); /* 2D4 */
  146. tw_writel(TW68_VBIC, 0x03); /* 010 */
  147. tw_writel(TW68_CAP_CTL, 0x03); /* 040 Enable both even & odd flds */
  148. tw_writel(TW68_DMAC, 0x2000); /* patch set had 0x2080 */
  149. tw_writel(TW68_TESTREG, 0); /* 02C */
  150. /*
  151. * Some common boards, especially inexpensive single-chip models,
  152. * use the GPIO bits 0-3 to control an on-board video-output mux.
  153. * For these boards, we need to set up the GPIO register into
  154. * "normal" mode, set bits 0-3 as output, and then set those bits
  155. * zero.
  156. *
  157. * Eventually, it would be nice if we could identify these boards
  158. * uniquely, and only do this initialisation if the board has been
  159. * identify. For the moment, however, it shouldn't hurt anything
  160. * to do these steps.
  161. */
  162. tw_writel(TW68_GPIOC, 0); /* Set the GPIO to "normal", no ints */
  163. tw_writel(TW68_GPOE, 0x0f); /* Set bits 0-3 to "output" */
  164. tw_writel(TW68_GPDATA, 0); /* Set all bits to low state */
  165. /* Initialize the device control structures */
  166. mutex_init(&dev->lock);
  167. spin_lock_init(&dev->slock);
  168. /* Initialize any subsystems */
  169. tw68_video_init1(dev);
  170. return 0;
  171. }
  172. static irqreturn_t tw68_irq(int irq, void *dev_id)
  173. {
  174. struct tw68_dev *dev = dev_id;
  175. u32 status, orig;
  176. int loop;
  177. status = orig = tw_readl(TW68_INTSTAT) & dev->pci_irqmask;
  178. /* Check if anything to do */
  179. if (0 == status)
  180. return IRQ_NONE; /* Nope - return */
  181. for (loop = 0; loop < 10; loop++) {
  182. if (status & dev->board_virqmask) /* video interrupt */
  183. tw68_irq_video_done(dev, status);
  184. status = tw_readl(TW68_INTSTAT) & dev->pci_irqmask;
  185. if (0 == status)
  186. return IRQ_HANDLED;
  187. }
  188. dev_dbg(&dev->pci->dev, "%s: **** INTERRUPT NOT HANDLED - clearing mask (orig 0x%08x, cur 0x%08x)",
  189. dev->name, orig, tw_readl(TW68_INTSTAT));
  190. dev_dbg(&dev->pci->dev, "%s: pci_irqmask 0x%08x; board_virqmask 0x%08x ****\n",
  191. dev->name, dev->pci_irqmask, dev->board_virqmask);
  192. tw_clearl(TW68_INTMASK, dev->pci_irqmask);
  193. return IRQ_HANDLED;
  194. }
  195. static int tw68_initdev(struct pci_dev *pci_dev,
  196. const struct pci_device_id *pci_id)
  197. {
  198. struct tw68_dev *dev;
  199. int vidnr = -1;
  200. int err;
  201. dev = devm_kzalloc(&pci_dev->dev, sizeof(*dev), GFP_KERNEL);
  202. if (NULL == dev)
  203. return -ENOMEM;
  204. dev->instance = v4l2_device_set_name(&dev->v4l2_dev, "tw68",
  205. &tw68_instance);
  206. err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
  207. if (err)
  208. return err;
  209. /* pci init */
  210. dev->pci = pci_dev;
  211. if (pci_enable_device(pci_dev)) {
  212. err = -EIO;
  213. goto fail1;
  214. }
  215. dev->name = dev->v4l2_dev.name;
  216. if (UNSET != latency) {
  217. pr_info("%s: setting pci latency timer to %d\n",
  218. dev->name, latency);
  219. pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
  220. }
  221. /* print pci info */
  222. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  223. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  224. pr_info("%s: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
  225. dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  226. dev->pci_lat, (u64)pci_resource_start(pci_dev, 0));
  227. pci_set_master(pci_dev);
  228. if (!pci_dma_supported(pci_dev, DMA_BIT_MASK(32))) {
  229. pr_info("%s: Oops: no 32bit PCI DMA ???\n", dev->name);
  230. err = -EIO;
  231. goto fail1;
  232. }
  233. switch (pci_id->device) {
  234. case PCI_DEVICE_ID_6800: /* TW6800 */
  235. dev->vdecoder = TW6800;
  236. dev->board_virqmask = TW68_VID_INTS;
  237. break;
  238. case PCI_DEVICE_ID_6801: /* Video decoder for TW6802 */
  239. dev->vdecoder = TW6801;
  240. dev->board_virqmask = TW68_VID_INTS | TW68_VID_INTSX;
  241. break;
  242. case PCI_DEVICE_ID_6804: /* Video decoder for TW6804 */
  243. dev->vdecoder = TW6804;
  244. dev->board_virqmask = TW68_VID_INTS | TW68_VID_INTSX;
  245. break;
  246. default:
  247. dev->vdecoder = TWXXXX; /* To be announced */
  248. dev->board_virqmask = TW68_VID_INTS | TW68_VID_INTSX;
  249. break;
  250. }
  251. /* get mmio */
  252. if (!request_mem_region(pci_resource_start(pci_dev, 0),
  253. pci_resource_len(pci_dev, 0),
  254. dev->name)) {
  255. err = -EBUSY;
  256. pr_err("%s: can't get MMIO memory @ 0x%llx\n",
  257. dev->name,
  258. (unsigned long long)pci_resource_start(pci_dev, 0));
  259. goto fail1;
  260. }
  261. dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
  262. pci_resource_len(pci_dev, 0));
  263. dev->bmmio = (__u8 __iomem *)dev->lmmio;
  264. if (NULL == dev->lmmio) {
  265. err = -EIO;
  266. pr_err("%s: can't ioremap() MMIO memory\n",
  267. dev->name);
  268. goto fail2;
  269. }
  270. /* initialize hardware #1 */
  271. /* Then do any initialisation wanted before interrupts are on */
  272. tw68_hw_init1(dev);
  273. dev->alloc_ctx = vb2_dma_sg_init_ctx(&pci_dev->dev);
  274. if (IS_ERR(dev->alloc_ctx)) {
  275. err = PTR_ERR(dev->alloc_ctx);
  276. goto fail3;
  277. }
  278. /* get irq */
  279. err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw68_irq,
  280. IRQF_SHARED, dev->name, dev);
  281. if (err < 0) {
  282. pr_err("%s: can't get IRQ %d\n",
  283. dev->name, pci_dev->irq);
  284. goto fail4;
  285. }
  286. /*
  287. * Now do remainder of initialisation, first for
  288. * things unique for this card, then for general board
  289. */
  290. if (dev->instance < TW68_MAXBOARDS)
  291. vidnr = video_nr[dev->instance];
  292. /* initialise video function first */
  293. err = tw68_video_init2(dev, vidnr);
  294. if (err < 0) {
  295. pr_err("%s: can't register video device\n",
  296. dev->name);
  297. goto fail5;
  298. }
  299. tw_setl(TW68_INTMASK, dev->pci_irqmask);
  300. pr_info("%s: registered device %s\n",
  301. dev->name, video_device_node_name(&dev->vdev));
  302. return 0;
  303. fail5:
  304. video_unregister_device(&dev->vdev);
  305. fail4:
  306. vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
  307. fail3:
  308. iounmap(dev->lmmio);
  309. fail2:
  310. release_mem_region(pci_resource_start(pci_dev, 0),
  311. pci_resource_len(pci_dev, 0));
  312. fail1:
  313. v4l2_device_unregister(&dev->v4l2_dev);
  314. return err;
  315. }
  316. static void tw68_finidev(struct pci_dev *pci_dev)
  317. {
  318. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  319. struct tw68_dev *dev =
  320. container_of(v4l2_dev, struct tw68_dev, v4l2_dev);
  321. /* shutdown subsystems */
  322. tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
  323. tw_writel(TW68_INTMASK, 0);
  324. /* unregister */
  325. video_unregister_device(&dev->vdev);
  326. v4l2_ctrl_handler_free(&dev->hdl);
  327. vb2_dma_sg_cleanup_ctx(dev->alloc_ctx);
  328. /* release resources */
  329. iounmap(dev->lmmio);
  330. release_mem_region(pci_resource_start(pci_dev, 0),
  331. pci_resource_len(pci_dev, 0));
  332. v4l2_device_unregister(&dev->v4l2_dev);
  333. }
  334. #ifdef CONFIG_PM
  335. static int tw68_suspend(struct pci_dev *pci_dev , pm_message_t state)
  336. {
  337. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  338. struct tw68_dev *dev = container_of(v4l2_dev,
  339. struct tw68_dev, v4l2_dev);
  340. tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
  341. dev->pci_irqmask &= ~TW68_VID_INTS;
  342. tw_writel(TW68_INTMASK, 0);
  343. synchronize_irq(pci_dev->irq);
  344. pci_save_state(pci_dev);
  345. pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  346. vb2_discard_done(&dev->vidq);
  347. return 0;
  348. }
  349. static int tw68_resume(struct pci_dev *pci_dev)
  350. {
  351. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  352. struct tw68_dev *dev = container_of(v4l2_dev,
  353. struct tw68_dev, v4l2_dev);
  354. struct tw68_buf *buf;
  355. unsigned long flags;
  356. pci_set_power_state(pci_dev, PCI_D0);
  357. pci_restore_state(pci_dev);
  358. /* Do things that are done in tw68_initdev ,
  359. except of initializing memory structures.*/
  360. msleep(100);
  361. tw68_set_tvnorm_hw(dev);
  362. /*resume unfinished buffer(s)*/
  363. spin_lock_irqsave(&dev->slock, flags);
  364. buf = container_of(dev->active.next, struct tw68_buf, list);
  365. tw68_video_start_dma(dev, buf);
  366. spin_unlock_irqrestore(&dev->slock, flags);
  367. return 0;
  368. }
  369. #endif
  370. /* ----------------------------------------------------------- */
  371. static struct pci_driver tw68_pci_driver = {
  372. .name = "tw68",
  373. .id_table = tw68_pci_tbl,
  374. .probe = tw68_initdev,
  375. .remove = tw68_finidev,
  376. #ifdef CONFIG_PM
  377. .suspend = tw68_suspend,
  378. .resume = tw68_resume
  379. #endif
  380. };
  381. module_pci_driver(tw68_pci_driver);