2 saa7146.o - driver for generic saa7146-based hardware
4 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <media/saa7146.h>
23 LIST_HEAD(saa7146_devices);
24 DEFINE_MUTEX(saa7146_devices_lock);
26 static int saa7146_num;
28 unsigned int saa7146_debug;
30 module_param(saa7146_debug, uint, 0644);
31 MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
34 static void dump_registers(struct saa7146_dev* dev)
38 INFO((" @ %li jiffies:\n",jiffies));
39 for(i = 0; i <= 0x148; i+=4) {
40 printk("0x%03x: 0x%08x\n",i,saa7146_read(dev,i));
45 /****************************************************************************
46 * gpio and debi helper functions
47 ****************************************************************************/
49 void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
55 value = saa7146_read(dev, GPIO_CTRL);
56 value &= ~(0xff << (8*port));
57 value |= (data << (8*port));
58 saa7146_write(dev, GPIO_CTRL, value);
61 /* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
62 static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev *dev,
63 unsigned long us1, unsigned long us2)
65 unsigned long timeout;
68 /* wait for registers to be programmed */
69 timeout = jiffies + usecs_to_jiffies(us1);
71 err = time_after(jiffies, timeout);
72 if (saa7146_read(dev, MC2) & 2)
75 printk(KERN_ERR "%s: %s timed out while waiting for "
76 "registers getting programmed\n",
77 dev->name, __FUNCTION__);
83 /* wait for transfer to complete */
84 timeout = jiffies + usecs_to_jiffies(us2);
86 err = time_after(jiffies, timeout);
87 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
89 saa7146_read(dev, MC2);
91 DEB_S(("%s: %s timed out while waiting for transfer "
92 "completion\n", dev->name, __FUNCTION__));
101 static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev *dev,
102 unsigned long us1, unsigned long us2)
106 /* wait for registers to be programmed */
109 if (saa7146_read(dev, MC2) & 2)
112 printk(KERN_ERR "%s: %s timed out while waiting for "
113 "registers getting programmed\n",
114 dev->name, __FUNCTION__);
120 /* wait for transfer to complete */
123 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
125 saa7146_read(dev, MC2);
127 DEB_S(("%s: %s timed out while waiting for transfer "
128 "completion\n", dev->name, __FUNCTION__));
137 int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
140 return saa7146_wait_for_debi_done_sleep(dev, 50000, 250000);
142 return saa7146_wait_for_debi_done_busyloop(dev, 50000, 250000);
145 /****************************************************************************
146 * general helper functions
147 ****************************************************************************/
149 /* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.c
150 make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
151 may be triggered on highmem machines */
152 static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
154 struct scatterlist *sglist;
158 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
161 sg_init_table(sglist, nr_pages);
162 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
163 pg = vmalloc_to_page(virt);
166 BUG_ON(PageHighMem(pg));
167 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
176 /********************************************************************************/
177 /* common page table functions */
179 void *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
181 int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
182 void *mem = vmalloc_32(length);
188 if (!(pt->slist = vmalloc_to_sg(mem, pages)))
191 if (saa7146_pgtable_alloc(pci, pt))
195 slen = pci_map_sg(pci,pt->slist,pt->nents,PCI_DMA_FROMDEVICE);
197 goto err_free_pgtable;
199 if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
205 pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
207 saa7146_pgtable_free(pci, pt);
217 void saa7146_vfree_destroy_pgtable(struct pci_dev *pci, void *mem, struct saa7146_pgtable *pt)
219 pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
220 saa7146_pgtable_free(pci, pt);
226 void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
230 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
234 int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
239 cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
243 pt->size = PAGE_SIZE;
250 int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
251 struct scatterlist *list, int sglen )
258 BUG_ON(list->offset > PAGE_SIZE);
260 /* if we have a user buffer, the first page may not be
261 aligned to a page boundary. */
262 pt->offset = list->offset;
265 for (i = 0; i < sglen; i++, list++) {
267 printk("i:%d, adr:0x%08x, len:%d, offset:%d\n", i,sg_dma_address(list), sg_dma_len(list), list->offset);
269 for (p = 0; p * 4096 < list->length; p++, ptr++) {
270 *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
276 /* safety; fill the page table up with the last valid page */
278 for(i=nr_pages;i<1024;i++) {
284 printk("offset: %d\n",pt->offset);
286 printk("ptr1 %d: 0x%08x\n",i,ptr[i]);
292 /********************************************************************************/
293 /* interrupt handler */
294 static irqreturn_t interrupt_hw(int irq, void *dev_id)
296 struct saa7146_dev *dev = dev_id;
300 /* read out the interrupt status register */
301 ack_isr = isr = saa7146_read(dev, ISR);
303 /* is this our interrupt? */
305 /* nope, some other device */
309 if( 0 != (dev->ext)) {
310 if( 0 != (dev->ext->irq_mask & isr )) {
311 if( 0 != dev->ext->irq_func ) {
312 dev->ext->irq_func(dev, &isr);
314 isr &= ~dev->ext->irq_mask;
317 if (0 != (isr & (MASK_27))) {
318 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
319 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
320 dev->vv_callback(dev,isr);
324 if (0 != (isr & (MASK_28))) {
325 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
326 dev->vv_callback(dev,isr);
330 if (0 != (isr & (MASK_16|MASK_17))) {
331 SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
332 /* only wake up if we expect something */
333 if (0 != dev->i2c_op) {
335 wake_up(&dev->i2c_wq);
337 u32 psr = saa7146_read(dev, PSR);
338 u32 ssr = saa7146_read(dev, SSR);
339 printk(KERN_WARNING "%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
340 dev->name, isr, psr, ssr);
342 isr &= ~(MASK_16|MASK_17);
345 ERR(("warning: interrupt enabled, but not handled properly.(0x%08x)\n",isr));
346 ERR(("disabling interrupt source(s)!\n"));
347 SAA7146_IER_DISABLE(dev,isr);
349 saa7146_write(dev, ISR, ack_isr);
353 /*********************************************************************************/
354 /* configuration-functions */
356 static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
358 struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
359 struct saa7146_extension *ext = pci_ext->ext;
360 struct saa7146_dev *dev;
363 /* clear out mem for sure */
364 dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
366 ERR(("out of memory.\n"));
370 DEB_EE(("pci:%p\n",pci));
372 err = pci_enable_device(pci);
374 ERR(("pci_enable_device() failed.\n"));
378 /* enable bus-mastering */
383 /* get chip-revision; this is needed to enable bug-fixes */
384 err = pci_read_config_dword(pci, PCI_CLASS_REVISION, &dev->revision);
386 ERR(("pci_read_config_dword() failed.\n"));
389 dev->revision &= 0xf;
391 /* remap the memory from virtual to physical adress */
393 err = pci_request_region(pci, 0, "saa7146");
397 dev->mem = ioremap(pci_resource_start(pci, 0),
398 pci_resource_len(pci, 0));
400 ERR(("ioremap() failed.\n"));
405 /* we don't do a master reset here anymore, it screws up
406 some boards that don't have an i2c-eeprom for configuration
409 saa7146_write(dev, MC1, MASK_31);
412 /* disable all irqs */
413 saa7146_write(dev, IER, 0);
415 /* shut down all dma transfers and rps tasks */
416 saa7146_write(dev, MC1, 0x30ff0000);
418 /* clear out any rps-signals pending */
419 saa7146_write(dev, MC2, 0xf8000000);
421 /* request an interrupt for the saa7146 */
422 err = request_irq(pci->irq, interrupt_hw, IRQF_SHARED | IRQF_DISABLED,
425 ERR(("request_irq() failed.\n"));
431 /* get memory for various stuff */
432 dev->d_rps0.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
433 &dev->d_rps0.dma_handle);
434 if (!dev->d_rps0.cpu_addr)
436 memset(dev->d_rps0.cpu_addr, 0x0, SAA7146_RPS_MEM);
438 dev->d_rps1.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
439 &dev->d_rps1.dma_handle);
440 if (!dev->d_rps1.cpu_addr)
442 memset(dev->d_rps1.cpu_addr, 0x0, SAA7146_RPS_MEM);
444 dev->d_i2c.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
445 &dev->d_i2c.dma_handle);
446 if (!dev->d_i2c.cpu_addr)
448 memset(dev->d_i2c.cpu_addr, 0x0, SAA7146_RPS_MEM);
450 /* the rest + print status message */
452 /* create a nice device name */
453 sprintf(dev->name, "saa7146 (%d)", saa7146_num);
455 INFO(("found saa7146 @ mem %p (revision %d, irq %d) (0x%04x,0x%04x).\n", dev->mem, dev->revision, pci->irq, pci->subsystem_vendor, pci->subsystem_device));
458 pci_set_drvdata(pci, dev);
460 mutex_init(&dev->lock);
461 spin_lock_init(&dev->int_slock);
462 spin_lock_init(&dev->slock);
464 mutex_init(&dev->i2c_lock);
466 dev->module = THIS_MODULE;
467 init_waitqueue_head(&dev->i2c_wq);
469 /* set some sane pci arbitrition values */
470 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
472 /* TODO: use the status code of the callback */
476 if (ext->probe && ext->probe(dev)) {
477 DEB_D(("ext->probe() failed for %p. skipping device.\n",dev));
481 if (ext->attach(dev, pci_ext)) {
482 DEB_D(("ext->attach() failed for %p. skipping device.\n",dev));
486 INIT_LIST_HEAD(&dev->item);
487 list_add_tail(&dev->item,&saa7146_devices);
495 pci_set_drvdata(pci, NULL);
497 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
498 dev->d_i2c.dma_handle);
500 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
501 dev->d_rps1.dma_handle);
503 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
504 dev->d_rps0.dma_handle);
506 free_irq(pci->irq, (void *)dev);
510 pci_release_region(pci, 0);
512 pci_disable_device(pci);
518 static void saa7146_remove_one(struct pci_dev *pdev)
520 struct saa7146_dev* dev = pci_get_drvdata(pdev);
525 { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
526 { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
527 { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
531 DEB_EE(("dev:%p\n",dev));
533 dev->ext->detach(dev);
535 /* shut down all video dma transfers */
536 saa7146_write(dev, MC1, 0x00ff0000);
538 /* disable all irqs, release irq-routine */
539 saa7146_write(dev, IER, 0);
541 free_irq(pdev->irq, dev);
543 for (p = dev_map; p->addr; p++)
544 pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
547 pci_release_region(pdev, 0);
548 list_del(&dev->item);
549 pci_disable_device(pdev);
555 /*********************************************************************************/
556 /* extension handling functions */
558 int saa7146_register_extension(struct saa7146_extension* ext)
560 DEB_EE(("ext:%p\n",ext));
562 ext->driver.name = ext->name;
563 ext->driver.id_table = ext->pci_tbl;
564 ext->driver.probe = saa7146_init_one;
565 ext->driver.remove = saa7146_remove_one;
567 printk("saa7146: register extension '%s'.\n",ext->name);
568 return pci_register_driver(&ext->driver);
571 int saa7146_unregister_extension(struct saa7146_extension* ext)
573 DEB_EE(("ext:%p\n",ext));
574 printk("saa7146: unregister extension '%s'.\n",ext->name);
575 pci_unregister_driver(&ext->driver);
579 EXPORT_SYMBOL_GPL(saa7146_register_extension);
580 EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
582 /* misc functions used by extension modules */
583 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
584 EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
585 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
586 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
587 EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable);
588 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
590 EXPORT_SYMBOL_GPL(saa7146_setgpio);
592 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
594 EXPORT_SYMBOL_GPL(saa7146_debug);
595 EXPORT_SYMBOL_GPL(saa7146_devices);
596 EXPORT_SYMBOL_GPL(saa7146_devices_lock);
598 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
599 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
600 MODULE_LICENSE("GPL");