tw68-core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. /* get irq */
  274. err = devm_request_irq(&pci_dev->dev, pci_dev->irq, tw68_irq,
  275. IRQF_SHARED | IRQF_DISABLED, dev->name, dev);
  276. if (err < 0) {
  277. pr_err("%s: can't get IRQ %d\n",
  278. dev->name, pci_dev->irq);
  279. goto fail3;
  280. }
  281. /*
  282. * Now do remainder of initialisation, first for
  283. * things unique for this card, then for general board
  284. */
  285. if (dev->instance < TW68_MAXBOARDS)
  286. vidnr = video_nr[dev->instance];
  287. /* initialise video function first */
  288. err = tw68_video_init2(dev, vidnr);
  289. if (err < 0) {
  290. pr_err("%s: can't register video device\n",
  291. dev->name);
  292. goto fail4;
  293. }
  294. tw_setl(TW68_INTMASK, dev->pci_irqmask);
  295. pr_info("%s: registered device %s\n",
  296. dev->name, video_device_node_name(&dev->vdev));
  297. return 0;
  298. fail4:
  299. video_unregister_device(&dev->vdev);
  300. fail3:
  301. iounmap(dev->lmmio);
  302. fail2:
  303. release_mem_region(pci_resource_start(pci_dev, 0),
  304. pci_resource_len(pci_dev, 0));
  305. fail1:
  306. v4l2_device_unregister(&dev->v4l2_dev);
  307. return err;
  308. }
  309. static void tw68_finidev(struct pci_dev *pci_dev)
  310. {
  311. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  312. struct tw68_dev *dev =
  313. container_of(v4l2_dev, struct tw68_dev, v4l2_dev);
  314. /* shutdown subsystems */
  315. tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
  316. tw_writel(TW68_INTMASK, 0);
  317. /* unregister */
  318. video_unregister_device(&dev->vdev);
  319. v4l2_ctrl_handler_free(&dev->hdl);
  320. /* release resources */
  321. iounmap(dev->lmmio);
  322. release_mem_region(pci_resource_start(pci_dev, 0),
  323. pci_resource_len(pci_dev, 0));
  324. v4l2_device_unregister(&dev->v4l2_dev);
  325. }
  326. #ifdef CONFIG_PM
  327. static int tw68_suspend(struct pci_dev *pci_dev , pm_message_t state)
  328. {
  329. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  330. struct tw68_dev *dev = container_of(v4l2_dev,
  331. struct tw68_dev, v4l2_dev);
  332. tw_clearl(TW68_DMAC, TW68_DMAP_EN | TW68_FIFO_EN);
  333. dev->pci_irqmask &= ~TW68_VID_INTS;
  334. tw_writel(TW68_INTMASK, 0);
  335. synchronize_irq(pci_dev->irq);
  336. pci_save_state(pci_dev);
  337. pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state));
  338. vb2_discard_done(&dev->vidq);
  339. return 0;
  340. }
  341. static int tw68_resume(struct pci_dev *pci_dev)
  342. {
  343. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  344. struct tw68_dev *dev = container_of(v4l2_dev,
  345. struct tw68_dev, v4l2_dev);
  346. struct tw68_buf *buf;
  347. unsigned long flags;
  348. pci_set_power_state(pci_dev, PCI_D0);
  349. pci_restore_state(pci_dev);
  350. /* Do things that are done in tw68_initdev ,
  351. except of initializing memory structures.*/
  352. msleep(100);
  353. tw68_set_tvnorm_hw(dev);
  354. /*resume unfinished buffer(s)*/
  355. spin_lock_irqsave(&dev->slock, flags);
  356. buf = container_of(dev->active.next, struct tw68_buf, list);
  357. tw68_video_start_dma(dev, buf);
  358. spin_unlock_irqrestore(&dev->slock, flags);
  359. return 0;
  360. }
  361. #endif
  362. /* ----------------------------------------------------------- */
  363. static struct pci_driver tw68_pci_driver = {
  364. .name = "tw68",
  365. .id_table = tw68_pci_tbl,
  366. .probe = tw68_initdev,
  367. .remove = tw68_finidev,
  368. #ifdef CONFIG_PM
  369. .suspend = tw68_suspend,
  370. .resume = tw68_resume
  371. #endif
  372. };
  373. module_pci_driver(tw68_pci_driver);