Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / common / saa7146_core.c
blob31e53b6a881aa2f7894d72f43bb3fe50e84495a6
1 /*
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>
22 #include <linux/module.h>
24 LIST_HEAD(saa7146_devices);
25 DEFINE_MUTEX(saa7146_devices_lock);
27 static int saa7146_num;
29 unsigned int saa7146_debug;
31 module_param(saa7146_debug, uint, 0644);
32 MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
34 #if 0
35 static void dump_registers(struct saa7146_dev* dev)
37 int i = 0;
39 INFO((" @ %li jiffies:\n",jiffies));
40 for(i = 0; i <= 0x148; i+=4) {
41 printk("0x%03x: 0x%08x\n",i,saa7146_read(dev,i));
44 #endif
46 /****************************************************************************
47 * gpio and debi helper functions
48 ****************************************************************************/
50 void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
52 u32 value = 0;
54 BUG_ON(port > 3);
56 value = saa7146_read(dev, GPIO_CTRL);
57 value &= ~(0xff << (8*port));
58 value |= (data << (8*port));
59 saa7146_write(dev, GPIO_CTRL, value);
62 /* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
63 static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev *dev,
64 unsigned long us1, unsigned long us2)
66 unsigned long timeout;
67 int err;
69 /* wait for registers to be programmed */
70 timeout = jiffies + usecs_to_jiffies(us1);
71 while (1) {
72 err = time_after(jiffies, timeout);
73 if (saa7146_read(dev, MC2) & 2)
74 break;
75 if (err) {
76 printk(KERN_ERR "%s: %s timed out while waiting for "
77 "registers getting programmed\n",
78 dev->name, __func__);
79 return -ETIMEDOUT;
81 msleep(1);
84 /* wait for transfer to complete */
85 timeout = jiffies + usecs_to_jiffies(us2);
86 while (1) {
87 err = time_after(jiffies, timeout);
88 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
89 break;
90 saa7146_read(dev, MC2);
91 if (err) {
92 DEB_S(("%s: %s timed out while waiting for transfer "
93 "completion\n", dev->name, __func__));
94 return -ETIMEDOUT;
96 msleep(1);
99 return 0;
102 static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev *dev,
103 unsigned long us1, unsigned long us2)
105 unsigned long loops;
107 /* wait for registers to be programmed */
108 loops = us1;
109 while (1) {
110 if (saa7146_read(dev, MC2) & 2)
111 break;
112 if (!loops--) {
113 printk(KERN_ERR "%s: %s timed out while waiting for "
114 "registers getting programmed\n",
115 dev->name, __func__);
116 return -ETIMEDOUT;
118 udelay(1);
121 /* wait for transfer to complete */
122 loops = us2 / 5;
123 while (1) {
124 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
125 break;
126 saa7146_read(dev, MC2);
127 if (!loops--) {
128 DEB_S(("%s: %s timed out while waiting for transfer "
129 "completion\n", dev->name, __func__));
130 return -ETIMEDOUT;
132 udelay(5);
135 return 0;
138 int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
140 if (nobusyloop)
141 return saa7146_wait_for_debi_done_sleep(dev, 50000, 250000);
142 else
143 return saa7146_wait_for_debi_done_busyloop(dev, 50000, 250000);
146 /****************************************************************************
147 * general helper functions
148 ****************************************************************************/
150 /* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.c
151 make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
152 may be triggered on highmem machines */
153 static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
155 struct scatterlist *sglist;
156 struct page *pg;
157 int i;
159 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
160 if (NULL == sglist)
161 return NULL;
162 sg_init_table(sglist, nr_pages);
163 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
164 pg = vmalloc_to_page(virt);
165 if (NULL == pg)
166 goto err;
167 BUG_ON(PageHighMem(pg));
168 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
170 return sglist;
172 err:
173 kfree(sglist);
174 return NULL;
177 /********************************************************************************/
178 /* common page table functions */
180 void *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
182 int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
183 void *mem = vmalloc_32(length);
184 int slen = 0;
186 if (NULL == mem)
187 goto err_null;
189 if (!(pt->slist = vmalloc_to_sg(mem, pages)))
190 goto err_free_mem;
192 if (saa7146_pgtable_alloc(pci, pt))
193 goto err_free_slist;
195 pt->nents = pages;
196 slen = pci_map_sg(pci,pt->slist,pt->nents,PCI_DMA_FROMDEVICE);
197 if (0 == slen)
198 goto err_free_pgtable;
200 if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
201 goto err_unmap_sg;
203 return mem;
205 err_unmap_sg:
206 pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
207 err_free_pgtable:
208 saa7146_pgtable_free(pci, pt);
209 err_free_slist:
210 kfree(pt->slist);
211 pt->slist = NULL;
212 err_free_mem:
213 vfree(mem);
214 err_null:
215 return NULL;
218 void saa7146_vfree_destroy_pgtable(struct pci_dev *pci, void *mem, struct saa7146_pgtable *pt)
220 pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
221 saa7146_pgtable_free(pci, pt);
222 kfree(pt->slist);
223 pt->slist = NULL;
224 vfree(mem);
227 void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
229 if (NULL == pt->cpu)
230 return;
231 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
232 pt->cpu = NULL;
235 int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
237 __le32 *cpu;
238 dma_addr_t dma_addr = 0;
240 cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
241 if (NULL == cpu) {
242 return -ENOMEM;
244 pt->size = PAGE_SIZE;
245 pt->cpu = cpu;
246 pt->dma = dma_addr;
248 return 0;
251 int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
252 struct scatterlist *list, int sglen )
254 __le32 *ptr, fill;
255 int nr_pages = 0;
256 int i,p;
258 BUG_ON(0 == sglen);
259 BUG_ON(list->offset > PAGE_SIZE);
261 /* if we have a user buffer, the first page may not be
262 aligned to a page boundary. */
263 pt->offset = list->offset;
265 ptr = pt->cpu;
266 for (i = 0; i < sglen; i++, list++) {
268 printk("i:%d, adr:0x%08x, len:%d, offset:%d\n", i,sg_dma_address(list), sg_dma_len(list), list->offset);
270 for (p = 0; p * 4096 < list->length; p++, ptr++) {
271 *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
272 nr_pages++;
277 /* safety; fill the page table up with the last valid page */
278 fill = *(ptr-1);
279 for(i=nr_pages;i<1024;i++) {
280 *ptr++ = fill;
284 ptr = pt->cpu;
285 printk("offset: %d\n",pt->offset);
286 for(i=0;i<5;i++) {
287 printk("ptr1 %d: 0x%08x\n",i,ptr[i]);
290 return 0;
293 /********************************************************************************/
294 /* interrupt handler */
295 static irqreturn_t interrupt_hw(int irq, void *dev_id)
297 struct saa7146_dev *dev = dev_id;
298 u32 isr;
299 u32 ack_isr;
301 /* read out the interrupt status register */
302 ack_isr = isr = saa7146_read(dev, ISR);
304 /* is this our interrupt? */
305 if ( 0 == isr ) {
306 /* nope, some other device */
307 return IRQ_NONE;
310 if (dev->ext) {
311 if (dev->ext->irq_mask & isr) {
312 if (dev->ext->irq_func)
313 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 (dev->vv_data && dev->vv_callback)
320 dev->vv_callback(dev,isr);
321 isr &= ~MASK_27;
323 if (0 != (isr & (MASK_28))) {
324 if (dev->vv_data && dev->vv_callback)
325 dev->vv_callback(dev,isr);
326 isr &= ~MASK_28;
328 if (0 != (isr & (MASK_16|MASK_17))) {
329 SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
330 /* only wake up if we expect something */
331 if (0 != dev->i2c_op) {
332 dev->i2c_op = 0;
333 wake_up(&dev->i2c_wq);
334 } else {
335 u32 psr = saa7146_read(dev, PSR);
336 u32 ssr = saa7146_read(dev, SSR);
337 printk(KERN_WARNING "%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
338 dev->name, isr, psr, ssr);
340 isr &= ~(MASK_16|MASK_17);
342 if( 0 != isr ) {
343 ERR(("warning: interrupt enabled, but not handled properly.(0x%08x)\n",isr));
344 ERR(("disabling interrupt source(s)!\n"));
345 SAA7146_IER_DISABLE(dev,isr);
347 saa7146_write(dev, ISR, ack_isr);
348 return IRQ_HANDLED;
351 /*********************************************************************************/
352 /* configuration-functions */
354 static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
356 struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
357 struct saa7146_extension *ext = pci_ext->ext;
358 struct saa7146_dev *dev;
359 int err = -ENOMEM;
361 /* clear out mem for sure */
362 dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
363 if (!dev) {
364 ERR(("out of memory.\n"));
365 goto out;
368 DEB_EE(("pci:%p\n",pci));
370 err = pci_enable_device(pci);
371 if (err < 0) {
372 ERR(("pci_enable_device() failed.\n"));
373 goto err_free;
376 /* enable bus-mastering */
377 pci_set_master(pci);
379 dev->pci = pci;
381 /* get chip-revision; this is needed to enable bug-fixes */
382 dev->revision = pci->revision;
384 /* remap the memory from virtual to physical address */
386 err = pci_request_region(pci, 0, "saa7146");
387 if (err < 0)
388 goto err_disable;
390 dev->mem = ioremap(pci_resource_start(pci, 0),
391 pci_resource_len(pci, 0));
392 if (!dev->mem) {
393 ERR(("ioremap() failed.\n"));
394 err = -ENODEV;
395 goto err_release;
398 /* we don't do a master reset here anymore, it screws up
399 some boards that don't have an i2c-eeprom for configuration
400 values */
402 saa7146_write(dev, MC1, MASK_31);
405 /* disable all irqs */
406 saa7146_write(dev, IER, 0);
408 /* shut down all dma transfers and rps tasks */
409 saa7146_write(dev, MC1, 0x30ff0000);
411 /* clear out any rps-signals pending */
412 saa7146_write(dev, MC2, 0xf8000000);
414 /* request an interrupt for the saa7146 */
415 err = request_irq(pci->irq, interrupt_hw, IRQF_SHARED | IRQF_DISABLED,
416 dev->name, dev);
417 if (err < 0) {
418 ERR(("request_irq() failed.\n"));
419 goto err_unmap;
422 err = -ENOMEM;
424 /* get memory for various stuff */
425 dev->d_rps0.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
426 &dev->d_rps0.dma_handle);
427 if (!dev->d_rps0.cpu_addr)
428 goto err_free_irq;
429 memset(dev->d_rps0.cpu_addr, 0x0, SAA7146_RPS_MEM);
431 dev->d_rps1.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
432 &dev->d_rps1.dma_handle);
433 if (!dev->d_rps1.cpu_addr)
434 goto err_free_rps0;
435 memset(dev->d_rps1.cpu_addr, 0x0, SAA7146_RPS_MEM);
437 dev->d_i2c.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
438 &dev->d_i2c.dma_handle);
439 if (!dev->d_i2c.cpu_addr)
440 goto err_free_rps1;
441 memset(dev->d_i2c.cpu_addr, 0x0, SAA7146_RPS_MEM);
443 /* the rest + print status message */
445 /* create a nice device name */
446 sprintf(dev->name, "saa7146 (%d)", saa7146_num);
448 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));
449 dev->ext = ext;
451 mutex_init(&dev->v4l2_lock);
452 spin_lock_init(&dev->int_slock);
453 spin_lock_init(&dev->slock);
455 mutex_init(&dev->i2c_lock);
457 dev->module = THIS_MODULE;
458 init_waitqueue_head(&dev->i2c_wq);
460 /* set some sane pci arbitrition values */
461 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
463 /* TODO: use the status code of the callback */
465 err = -ENODEV;
467 if (ext->probe && ext->probe(dev)) {
468 DEB_D(("ext->probe() failed for %p. skipping device.\n",dev));
469 goto err_free_i2c;
472 if (ext->attach(dev, pci_ext)) {
473 DEB_D(("ext->attach() failed for %p. skipping device.\n",dev));
474 goto err_free_i2c;
476 /* V4L extensions will set the pci drvdata to the v4l2_device in the
477 attach() above. So for those cards that do not use V4L we have to
478 set it explicitly. */
479 pci_set_drvdata(pci, &dev->v4l2_dev);
481 INIT_LIST_HEAD(&dev->item);
482 list_add_tail(&dev->item,&saa7146_devices);
483 saa7146_num++;
485 err = 0;
486 out:
487 return err;
489 err_free_i2c:
490 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
491 dev->d_i2c.dma_handle);
492 err_free_rps1:
493 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
494 dev->d_rps1.dma_handle);
495 err_free_rps0:
496 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
497 dev->d_rps0.dma_handle);
498 err_free_irq:
499 free_irq(pci->irq, (void *)dev);
500 err_unmap:
501 iounmap(dev->mem);
502 err_release:
503 pci_release_region(pci, 0);
504 err_disable:
505 pci_disable_device(pci);
506 err_free:
507 kfree(dev);
508 goto out;
511 static void saa7146_remove_one(struct pci_dev *pdev)
513 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
514 struct saa7146_dev *dev = to_saa7146_dev(v4l2_dev);
515 struct {
516 void *addr;
517 dma_addr_t dma;
518 } dev_map[] = {
519 { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
520 { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
521 { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
522 { NULL, 0 }
523 }, *p;
525 DEB_EE(("dev:%p\n",dev));
527 dev->ext->detach(dev);
528 /* Zero the PCI drvdata after use. */
529 pci_set_drvdata(pdev, NULL);
531 /* shut down all video dma transfers */
532 saa7146_write(dev, MC1, 0x00ff0000);
534 /* disable all irqs, release irq-routine */
535 saa7146_write(dev, IER, 0);
537 free_irq(pdev->irq, dev);
539 for (p = dev_map; p->addr; p++)
540 pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
542 iounmap(dev->mem);
543 pci_release_region(pdev, 0);
544 list_del(&dev->item);
545 pci_disable_device(pdev);
546 kfree(dev);
548 saa7146_num--;
551 /*********************************************************************************/
552 /* extension handling functions */
554 int saa7146_register_extension(struct saa7146_extension* ext)
556 DEB_EE(("ext:%p\n",ext));
558 ext->driver.name = ext->name;
559 ext->driver.id_table = ext->pci_tbl;
560 ext->driver.probe = saa7146_init_one;
561 ext->driver.remove = saa7146_remove_one;
563 printk("saa7146: register extension '%s'.\n",ext->name);
564 return pci_register_driver(&ext->driver);
567 int saa7146_unregister_extension(struct saa7146_extension* ext)
569 DEB_EE(("ext:%p\n",ext));
570 printk("saa7146: unregister extension '%s'.\n",ext->name);
571 pci_unregister_driver(&ext->driver);
572 return 0;
575 EXPORT_SYMBOL_GPL(saa7146_register_extension);
576 EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
578 /* misc functions used by extension modules */
579 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
580 EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
581 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
582 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
583 EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable);
584 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
586 EXPORT_SYMBOL_GPL(saa7146_setgpio);
588 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
590 EXPORT_SYMBOL_GPL(saa7146_debug);
591 EXPORT_SYMBOL_GPL(saa7146_devices);
592 EXPORT_SYMBOL_GPL(saa7146_devices_lock);
594 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
595 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
596 MODULE_LICENSE("GPL");