tw68-core.c 14 KB

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