saa7146_core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. saa7146.o - driver for generic saa7146-based hardware
  3. Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <media/drv-intf/saa7146.h>
  18. #include <linux/module.h>
  19. static int saa7146_num;
  20. unsigned int saa7146_debug;
  21. module_param(saa7146_debug, uint, 0644);
  22. MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
  23. #if 0
  24. static void dump_registers(struct saa7146_dev* dev)
  25. {
  26. int i = 0;
  27. pr_info(" @ %li jiffies:\n", jiffies);
  28. for (i = 0; i <= 0x148; i += 4)
  29. pr_info("0x%03x: 0x%08x\n", i, saa7146_read(dev, i));
  30. }
  31. #endif
  32. /****************************************************************************
  33. * gpio and debi helper functions
  34. ****************************************************************************/
  35. void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
  36. {
  37. u32 value = 0;
  38. BUG_ON(port > 3);
  39. value = saa7146_read(dev, GPIO_CTRL);
  40. value &= ~(0xff << (8*port));
  41. value |= (data << (8*port));
  42. saa7146_write(dev, GPIO_CTRL, value);
  43. }
  44. /* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
  45. static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev *dev,
  46. unsigned long us1, unsigned long us2)
  47. {
  48. unsigned long timeout;
  49. int err;
  50. /* wait for registers to be programmed */
  51. timeout = jiffies + usecs_to_jiffies(us1);
  52. while (1) {
  53. err = time_after(jiffies, timeout);
  54. if (saa7146_read(dev, MC2) & 2)
  55. break;
  56. if (err) {
  57. pr_debug("%s: %s timed out while waiting for registers getting programmed\n",
  58. dev->name, __func__);
  59. return -ETIMEDOUT;
  60. }
  61. msleep(1);
  62. }
  63. /* wait for transfer to complete */
  64. timeout = jiffies + usecs_to_jiffies(us2);
  65. while (1) {
  66. err = time_after(jiffies, timeout);
  67. if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
  68. break;
  69. saa7146_read(dev, MC2);
  70. if (err) {
  71. DEB_S("%s: %s timed out while waiting for transfer completion\n",
  72. dev->name, __func__);
  73. return -ETIMEDOUT;
  74. }
  75. msleep(1);
  76. }
  77. return 0;
  78. }
  79. static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev *dev,
  80. unsigned long us1, unsigned long us2)
  81. {
  82. unsigned long loops;
  83. /* wait for registers to be programmed */
  84. loops = us1;
  85. while (1) {
  86. if (saa7146_read(dev, MC2) & 2)
  87. break;
  88. if (!loops--) {
  89. pr_err("%s: %s timed out while waiting for registers getting programmed\n",
  90. dev->name, __func__);
  91. return -ETIMEDOUT;
  92. }
  93. udelay(1);
  94. }
  95. /* wait for transfer to complete */
  96. loops = us2 / 5;
  97. while (1) {
  98. if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
  99. break;
  100. saa7146_read(dev, MC2);
  101. if (!loops--) {
  102. DEB_S("%s: %s timed out while waiting for transfer completion\n",
  103. dev->name, __func__);
  104. return -ETIMEDOUT;
  105. }
  106. udelay(5);
  107. }
  108. return 0;
  109. }
  110. int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
  111. {
  112. if (nobusyloop)
  113. return saa7146_wait_for_debi_done_sleep(dev, 50000, 250000);
  114. else
  115. return saa7146_wait_for_debi_done_busyloop(dev, 50000, 250000);
  116. }
  117. /****************************************************************************
  118. * general helper functions
  119. ****************************************************************************/
  120. /* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.c
  121. make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
  122. may be triggered on highmem machines */
  123. static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
  124. {
  125. struct scatterlist *sglist;
  126. struct page *pg;
  127. int i;
  128. sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
  129. if (NULL == sglist)
  130. return NULL;
  131. sg_init_table(sglist, nr_pages);
  132. for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
  133. pg = vmalloc_to_page(virt);
  134. if (NULL == pg)
  135. goto err;
  136. BUG_ON(PageHighMem(pg));
  137. sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
  138. }
  139. return sglist;
  140. err:
  141. kfree(sglist);
  142. return NULL;
  143. }
  144. /********************************************************************************/
  145. /* common page table functions */
  146. void *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
  147. {
  148. int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
  149. void *mem = vmalloc_32(length);
  150. int slen = 0;
  151. if (NULL == mem)
  152. goto err_null;
  153. if (!(pt->slist = vmalloc_to_sg(mem, pages)))
  154. goto err_free_mem;
  155. if (saa7146_pgtable_alloc(pci, pt))
  156. goto err_free_slist;
  157. pt->nents = pages;
  158. slen = pci_map_sg(pci,pt->slist,pt->nents,PCI_DMA_FROMDEVICE);
  159. if (0 == slen)
  160. goto err_free_pgtable;
  161. if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
  162. goto err_unmap_sg;
  163. return mem;
  164. err_unmap_sg:
  165. pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
  166. err_free_pgtable:
  167. saa7146_pgtable_free(pci, pt);
  168. err_free_slist:
  169. kfree(pt->slist);
  170. pt->slist = NULL;
  171. err_free_mem:
  172. vfree(mem);
  173. err_null:
  174. return NULL;
  175. }
  176. void saa7146_vfree_destroy_pgtable(struct pci_dev *pci, void *mem, struct saa7146_pgtable *pt)
  177. {
  178. pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
  179. saa7146_pgtable_free(pci, pt);
  180. kfree(pt->slist);
  181. pt->slist = NULL;
  182. vfree(mem);
  183. }
  184. void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
  185. {
  186. if (NULL == pt->cpu)
  187. return;
  188. pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
  189. pt->cpu = NULL;
  190. }
  191. int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
  192. {
  193. __le32 *cpu;
  194. dma_addr_t dma_addr = 0;
  195. cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
  196. if (NULL == cpu) {
  197. return -ENOMEM;
  198. }
  199. pt->size = PAGE_SIZE;
  200. pt->cpu = cpu;
  201. pt->dma = dma_addr;
  202. return 0;
  203. }
  204. int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
  205. struct scatterlist *list, int sglen )
  206. {
  207. __le32 *ptr, fill;
  208. int nr_pages = 0;
  209. int i,p;
  210. BUG_ON(0 == sglen);
  211. BUG_ON(list->offset > PAGE_SIZE);
  212. /* if we have a user buffer, the first page may not be
  213. aligned to a page boundary. */
  214. pt->offset = list->offset;
  215. ptr = pt->cpu;
  216. for (i = 0; i < sglen; i++, list++) {
  217. /*
  218. pr_debug("i:%d, adr:0x%08x, len:%d, offset:%d\n",
  219. i, sg_dma_address(list), sg_dma_len(list),
  220. list->offset);
  221. */
  222. for (p = 0; p * 4096 < list->length; p++, ptr++) {
  223. *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
  224. nr_pages++;
  225. }
  226. }
  227. /* safety; fill the page table up with the last valid page */
  228. fill = *(ptr-1);
  229. for(i=nr_pages;i<1024;i++) {
  230. *ptr++ = fill;
  231. }
  232. /*
  233. ptr = pt->cpu;
  234. pr_debug("offset: %d\n", pt->offset);
  235. for(i=0;i<5;i++) {
  236. pr_debug("ptr1 %d: 0x%08x\n", i, ptr[i]);
  237. }
  238. */
  239. return 0;
  240. }
  241. /********************************************************************************/
  242. /* interrupt handler */
  243. static irqreturn_t interrupt_hw(int irq, void *dev_id)
  244. {
  245. struct saa7146_dev *dev = dev_id;
  246. u32 isr;
  247. u32 ack_isr;
  248. /* read out the interrupt status register */
  249. ack_isr = isr = saa7146_read(dev, ISR);
  250. /* is this our interrupt? */
  251. if ( 0 == isr ) {
  252. /* nope, some other device */
  253. return IRQ_NONE;
  254. }
  255. if (dev->ext) {
  256. if (dev->ext->irq_mask & isr) {
  257. if (dev->ext->irq_func)
  258. dev->ext->irq_func(dev, &isr);
  259. isr &= ~dev->ext->irq_mask;
  260. }
  261. }
  262. if (0 != (isr & (MASK_27))) {
  263. DEB_INT("irq: RPS0 (0x%08x)\n", isr);
  264. if (dev->vv_data && dev->vv_callback)
  265. dev->vv_callback(dev,isr);
  266. isr &= ~MASK_27;
  267. }
  268. if (0 != (isr & (MASK_28))) {
  269. if (dev->vv_data && dev->vv_callback)
  270. dev->vv_callback(dev,isr);
  271. isr &= ~MASK_28;
  272. }
  273. if (0 != (isr & (MASK_16|MASK_17))) {
  274. SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
  275. /* only wake up if we expect something */
  276. if (0 != dev->i2c_op) {
  277. dev->i2c_op = 0;
  278. wake_up(&dev->i2c_wq);
  279. } else {
  280. u32 psr = saa7146_read(dev, PSR);
  281. u32 ssr = saa7146_read(dev, SSR);
  282. pr_warn("%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
  283. dev->name, isr, psr, ssr);
  284. }
  285. isr &= ~(MASK_16|MASK_17);
  286. }
  287. if( 0 != isr ) {
  288. ERR("warning: interrupt enabled, but not handled properly.(0x%08x)\n",
  289. isr);
  290. ERR("disabling interrupt source(s)!\n");
  291. SAA7146_IER_DISABLE(dev,isr);
  292. }
  293. saa7146_write(dev, ISR, ack_isr);
  294. return IRQ_HANDLED;
  295. }
  296. /*********************************************************************************/
  297. /* configuration-functions */
  298. static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
  299. {
  300. struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
  301. struct saa7146_extension *ext = pci_ext->ext;
  302. struct saa7146_dev *dev;
  303. int err = -ENOMEM;
  304. /* clear out mem for sure */
  305. dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
  306. if (!dev) {
  307. ERR("out of memory\n");
  308. goto out;
  309. }
  310. /* create a nice device name */
  311. sprintf(dev->name, "saa7146 (%d)", saa7146_num);
  312. DEB_EE("pci:%p\n", pci);
  313. err = pci_enable_device(pci);
  314. if (err < 0) {
  315. ERR("pci_enable_device() failed\n");
  316. goto err_free;
  317. }
  318. /* enable bus-mastering */
  319. pci_set_master(pci);
  320. dev->pci = pci;
  321. /* get chip-revision; this is needed to enable bug-fixes */
  322. dev->revision = pci->revision;
  323. /* remap the memory from virtual to physical address */
  324. err = pci_request_region(pci, 0, "saa7146");
  325. if (err < 0)
  326. goto err_disable;
  327. dev->mem = ioremap(pci_resource_start(pci, 0),
  328. pci_resource_len(pci, 0));
  329. if (!dev->mem) {
  330. ERR("ioremap() failed\n");
  331. err = -ENODEV;
  332. goto err_release;
  333. }
  334. /* we don't do a master reset here anymore, it screws up
  335. some boards that don't have an i2c-eeprom for configuration
  336. values */
  337. /*
  338. saa7146_write(dev, MC1, MASK_31);
  339. */
  340. /* disable all irqs */
  341. saa7146_write(dev, IER, 0);
  342. /* shut down all dma transfers and rps tasks */
  343. saa7146_write(dev, MC1, 0x30ff0000);
  344. /* clear out any rps-signals pending */
  345. saa7146_write(dev, MC2, 0xf8000000);
  346. /* request an interrupt for the saa7146 */
  347. err = request_irq(pci->irq, interrupt_hw, IRQF_SHARED,
  348. dev->name, dev);
  349. if (err < 0) {
  350. ERR("request_irq() failed\n");
  351. goto err_unmap;
  352. }
  353. err = -ENOMEM;
  354. /* get memory for various stuff */
  355. dev->d_rps0.cpu_addr = pci_zalloc_consistent(pci, SAA7146_RPS_MEM,
  356. &dev->d_rps0.dma_handle);
  357. if (!dev->d_rps0.cpu_addr)
  358. goto err_free_irq;
  359. dev->d_rps1.cpu_addr = pci_zalloc_consistent(pci, SAA7146_RPS_MEM,
  360. &dev->d_rps1.dma_handle);
  361. if (!dev->d_rps1.cpu_addr)
  362. goto err_free_rps0;
  363. dev->d_i2c.cpu_addr = pci_zalloc_consistent(pci, SAA7146_RPS_MEM,
  364. &dev->d_i2c.dma_handle);
  365. if (!dev->d_i2c.cpu_addr)
  366. goto err_free_rps1;
  367. /* the rest + print status message */
  368. pr_info("found saa7146 @ mem %p (revision %d, irq %d) (0x%04x,0x%04x)\n",
  369. dev->mem, dev->revision, pci->irq,
  370. pci->subsystem_vendor, pci->subsystem_device);
  371. dev->ext = ext;
  372. mutex_init(&dev->v4l2_lock);
  373. spin_lock_init(&dev->int_slock);
  374. spin_lock_init(&dev->slock);
  375. mutex_init(&dev->i2c_lock);
  376. dev->module = THIS_MODULE;
  377. init_waitqueue_head(&dev->i2c_wq);
  378. /* set some sane pci arbitrition values */
  379. saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
  380. /* TODO: use the status code of the callback */
  381. err = -ENODEV;
  382. if (ext->probe && ext->probe(dev)) {
  383. DEB_D("ext->probe() failed for %p. skipping device.\n", dev);
  384. goto err_free_i2c;
  385. }
  386. if (ext->attach(dev, pci_ext)) {
  387. DEB_D("ext->attach() failed for %p. skipping device.\n", dev);
  388. goto err_free_i2c;
  389. }
  390. /* V4L extensions will set the pci drvdata to the v4l2_device in the
  391. attach() above. So for those cards that do not use V4L we have to
  392. set it explicitly. */
  393. pci_set_drvdata(pci, &dev->v4l2_dev);
  394. saa7146_num++;
  395. err = 0;
  396. out:
  397. return err;
  398. err_free_i2c:
  399. pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
  400. dev->d_i2c.dma_handle);
  401. err_free_rps1:
  402. pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
  403. dev->d_rps1.dma_handle);
  404. err_free_rps0:
  405. pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
  406. dev->d_rps0.dma_handle);
  407. err_free_irq:
  408. free_irq(pci->irq, (void *)dev);
  409. err_unmap:
  410. iounmap(dev->mem);
  411. err_release:
  412. pci_release_region(pci, 0);
  413. err_disable:
  414. pci_disable_device(pci);
  415. err_free:
  416. kfree(dev);
  417. goto out;
  418. }
  419. static void saa7146_remove_one(struct pci_dev *pdev)
  420. {
  421. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  422. struct saa7146_dev *dev = to_saa7146_dev(v4l2_dev);
  423. struct {
  424. void *addr;
  425. dma_addr_t dma;
  426. } dev_map[] = {
  427. { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
  428. { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
  429. { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
  430. { NULL, 0 }
  431. }, *p;
  432. DEB_EE("dev:%p\n", dev);
  433. dev->ext->detach(dev);
  434. /* shut down all video dma transfers */
  435. saa7146_write(dev, MC1, 0x00ff0000);
  436. /* disable all irqs, release irq-routine */
  437. saa7146_write(dev, IER, 0);
  438. free_irq(pdev->irq, dev);
  439. for (p = dev_map; p->addr; p++)
  440. pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
  441. iounmap(dev->mem);
  442. pci_release_region(pdev, 0);
  443. pci_disable_device(pdev);
  444. kfree(dev);
  445. saa7146_num--;
  446. }
  447. /*********************************************************************************/
  448. /* extension handling functions */
  449. int saa7146_register_extension(struct saa7146_extension* ext)
  450. {
  451. DEB_EE("ext:%p\n", ext);
  452. ext->driver.name = ext->name;
  453. ext->driver.id_table = ext->pci_tbl;
  454. ext->driver.probe = saa7146_init_one;
  455. ext->driver.remove = saa7146_remove_one;
  456. pr_info("register extension '%s'\n", ext->name);
  457. return pci_register_driver(&ext->driver);
  458. }
  459. int saa7146_unregister_extension(struct saa7146_extension* ext)
  460. {
  461. DEB_EE("ext:%p\n", ext);
  462. pr_info("unregister extension '%s'\n", ext->name);
  463. pci_unregister_driver(&ext->driver);
  464. return 0;
  465. }
  466. EXPORT_SYMBOL_GPL(saa7146_register_extension);
  467. EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
  468. /* misc functions used by extension modules */
  469. EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
  470. EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
  471. EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
  472. EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
  473. EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable);
  474. EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
  475. EXPORT_SYMBOL_GPL(saa7146_setgpio);
  476. EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
  477. EXPORT_SYMBOL_GPL(saa7146_debug);
  478. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  479. MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
  480. MODULE_LICENSE("GPL");