Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / media / common / saa7146_core.c
blob8cdd4d265ffa235bb69a57f7a57679b6e0827e72
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>
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, int, 0644);
31 MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
33 #if 0
34 static void dump_registers(struct saa7146_dev* dev)
36 int i = 0;
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));
43 #endif
45 /****************************************************************************
46 * gpio and debi helper functions
47 ****************************************************************************/
49 void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
51 u32 value = 0;
53 BUG_ON(port > 3);
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 int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
64 unsigned long start;
65 int err;
67 /* wait for registers to be programmed */
68 start = jiffies;
69 while (1) {
70 err = time_after(jiffies, start + HZ/20);
71 if (saa7146_read(dev, MC2) & 2)
72 break;
73 if (err) {
74 DEB_S(("timed out while waiting for registers getting programmed\n"));
75 return -ETIMEDOUT;
77 if (nobusyloop)
78 msleep(1);
81 /* wait for transfer to complete */
82 start = jiffies;
83 while (1) {
84 err = time_after(jiffies, start + HZ/4);
85 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
86 break;
87 saa7146_read(dev, MC2);
88 if (err) {
89 DEB_S(("timed out while waiting for transfer completion\n"));
90 return -ETIMEDOUT;
92 if (nobusyloop)
93 msleep(1);
96 return 0;
99 /****************************************************************************
100 * general helper functions
101 ****************************************************************************/
103 /* this is videobuf_vmalloc_to_sg() from video-buf.c
104 make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
105 may be triggered on highmem machines */
106 static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
108 struct scatterlist *sglist;
109 struct page *pg;
110 int i;
112 sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
113 if (NULL == sglist)
114 return NULL;
115 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
116 pg = vmalloc_to_page(virt);
117 if (NULL == pg)
118 goto err;
119 BUG_ON(PageHighMem(pg));
120 sglist[i].page = pg;
121 sglist[i].length = PAGE_SIZE;
123 return sglist;
125 err:
126 kfree(sglist);
127 return NULL;
130 /********************************************************************************/
131 /* common page table functions */
133 char *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
135 int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
136 char *mem = vmalloc_32(length);
137 int slen = 0;
139 if (NULL == mem) {
140 return NULL;
143 if (!(pt->slist = vmalloc_to_sg(mem, pages))) {
144 vfree(mem);
145 return NULL;
148 if (saa7146_pgtable_alloc(pci, pt)) {
149 kfree(pt->slist);
150 pt->slist = NULL;
151 vfree(mem);
152 return NULL;
155 slen = pci_map_sg(pci,pt->slist,pages,PCI_DMA_FROMDEVICE);
156 if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen)) {
157 return NULL;
160 return mem;
163 void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
165 if (NULL == pt->cpu)
166 return;
167 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
168 pt->cpu = NULL;
169 kfree(pt->slist);
170 pt->slist = NULL;
173 int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
175 u32 *cpu;
176 dma_addr_t dma_addr;
178 cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
179 if (NULL == cpu) {
180 return -ENOMEM;
182 pt->size = PAGE_SIZE;
183 pt->cpu = cpu;
184 pt->dma = dma_addr;
186 return 0;
189 int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
190 struct scatterlist *list, int sglen )
192 u32 *ptr, fill;
193 int nr_pages = 0;
194 int i,p;
196 BUG_ON(0 == sglen);
197 BUG_ON(list->offset > PAGE_SIZE);
199 /* if we have a user buffer, the first page may not be
200 aligned to a page boundary. */
201 pt->offset = list->offset;
203 ptr = pt->cpu;
204 for (i = 0; i < sglen; i++, list++) {
206 printk("i:%d, adr:0x%08x, len:%d, offset:%d\n", i,sg_dma_address(list), sg_dma_len(list), list->offset);
208 for (p = 0; p * 4096 < list->length; p++, ptr++) {
209 *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
210 nr_pages++;
215 /* safety; fill the page table up with the last valid page */
216 fill = *(ptr-1);
217 for(i=nr_pages;i<1024;i++) {
218 *ptr++ = fill;
222 ptr = pt->cpu;
223 printk("offset: %d\n",pt->offset);
224 for(i=0;i<5;i++) {
225 printk("ptr1 %d: 0x%08x\n",i,ptr[i]);
228 return 0;
231 /********************************************************************************/
232 /* interrupt handler */
233 static irqreturn_t interrupt_hw(int irq, void *dev_id, struct pt_regs *regs)
235 struct saa7146_dev *dev = dev_id;
236 u32 isr = 0;
238 /* read out the interrupt status register */
239 isr = saa7146_read(dev, ISR);
241 /* is this our interrupt? */
242 if ( 0 == isr ) {
243 /* nope, some other device */
244 return IRQ_NONE;
247 saa7146_write(dev, ISR, isr);
249 if( 0 != (dev->ext)) {
250 if( 0 != (dev->ext->irq_mask & isr )) {
251 if( 0 != dev->ext->irq_func ) {
252 dev->ext->irq_func(dev, &isr);
254 isr &= ~dev->ext->irq_mask;
257 if (0 != (isr & (MASK_27))) {
258 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
259 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
260 dev->vv_callback(dev,isr);
262 isr &= ~MASK_27;
264 if (0 != (isr & (MASK_28))) {
265 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
266 dev->vv_callback(dev,isr);
268 isr &= ~MASK_28;
270 if (0 != (isr & (MASK_16|MASK_17))) {
271 u32 status = saa7146_read(dev, I2C_STATUS);
272 if( (0x3 == (status & 0x3)) || (0 == (status & 0x1)) ) {
273 SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
274 /* only wake up if we expect something */
275 if( 0 != dev->i2c_op ) {
276 u32 psr = (saa7146_read(dev, PSR) >> 16) & 0x2;
277 u32 ssr = (saa7146_read(dev, SSR) >> 17) & 0x1f;
278 DEB_I2C(("irq: i2c, status: 0x%08x, psr:0x%02x, ssr:0x%02x).\n",status,psr,ssr));
279 dev->i2c_op = 0;
280 wake_up(&dev->i2c_wq);
281 } else {
282 DEB_I2C(("unexpected irq: i2c, status: 0x%08x, isr %#x\n",status, isr));
284 } else {
285 DEB_I2C(("unhandled irq: i2c, status: 0x%08x, isr %#x\n",status, isr));
287 isr &= ~(MASK_16|MASK_17);
289 if( 0 != isr ) {
290 ERR(("warning: interrupt enabled, but not handled properly.(0x%08x)\n",isr));
291 ERR(("disabling interrupt source(s)!\n"));
292 SAA7146_IER_DISABLE(dev,isr);
294 return IRQ_HANDLED;
297 /*********************************************************************************/
298 /* configuration-functions */
300 static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
302 struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
303 struct saa7146_extension *ext = pci_ext->ext;
304 struct saa7146_dev *dev;
305 int err = -ENOMEM;
307 /* clear out mem for sure */
308 dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
309 if (!dev) {
310 ERR(("out of memory.\n"));
311 goto out;
314 DEB_EE(("pci:%p\n",pci));
316 err = pci_enable_device(pci);
317 if (err < 0) {
318 ERR(("pci_enable_device() failed.\n"));
319 goto err_free;
322 /* enable bus-mastering */
323 pci_set_master(pci);
325 dev->pci = pci;
327 /* get chip-revision; this is needed to enable bug-fixes */
328 err = pci_read_config_dword(pci, PCI_CLASS_REVISION, &dev->revision);
329 if (err < 0) {
330 ERR(("pci_read_config_dword() failed.\n"));
331 goto err_disable;
333 dev->revision &= 0xf;
335 /* remap the memory from virtual to physical adress */
337 err = pci_request_region(pci, 0, "saa7146");
338 if (err < 0)
339 goto err_disable;
341 dev->mem = ioremap(pci_resource_start(pci, 0),
342 pci_resource_len(pci, 0));
343 if (!dev->mem) {
344 ERR(("ioremap() failed.\n"));
345 err = -ENODEV;
346 goto err_release;
349 /* we don't do a master reset here anymore, it screws up
350 some boards that don't have an i2c-eeprom for configuration
351 values */
353 saa7146_write(dev, MC1, MASK_31);
356 /* disable all irqs */
357 saa7146_write(dev, IER, 0);
359 /* shut down all dma transfers and rps tasks */
360 saa7146_write(dev, MC1, 0x30ff0000);
362 /* clear out any rps-signals pending */
363 saa7146_write(dev, MC2, 0xf8000000);
365 /* request an interrupt for the saa7146 */
366 err = request_irq(pci->irq, interrupt_hw, SA_SHIRQ | SA_INTERRUPT,
367 dev->name, dev);
368 if (err < 0) {
369 ERR(("request_irq() failed.\n"));
370 goto err_unmap;
373 err = -ENOMEM;
375 /* get memory for various stuff */
376 dev->d_rps0.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
377 &dev->d_rps0.dma_handle);
378 if (!dev->d_rps0.cpu_addr)
379 goto err_free_irq;
380 memset(dev->d_rps0.cpu_addr, 0x0, SAA7146_RPS_MEM);
382 dev->d_rps1.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
383 &dev->d_rps1.dma_handle);
384 if (!dev->d_rps1.cpu_addr)
385 goto err_free_rps0;
386 memset(dev->d_rps1.cpu_addr, 0x0, SAA7146_RPS_MEM);
388 dev->d_i2c.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
389 &dev->d_i2c.dma_handle);
390 if (!dev->d_i2c.cpu_addr)
391 goto err_free_rps1;
392 memset(dev->d_i2c.cpu_addr, 0x0, SAA7146_RPS_MEM);
394 /* the rest + print status message */
396 /* create a nice device name */
397 sprintf(dev->name, "saa7146 (%d)", saa7146_num);
399 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));
400 dev->ext = ext;
402 pci_set_drvdata(pci, dev);
404 mutex_init(&dev->lock);
405 spin_lock_init(&dev->int_slock);
406 spin_lock_init(&dev->slock);
408 mutex_init(&dev->i2c_lock);
410 dev->module = THIS_MODULE;
411 init_waitqueue_head(&dev->i2c_wq);
413 /* set some sane pci arbitrition values */
414 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
416 /* TODO: use the status code of the callback */
418 err = -ENODEV;
420 if (ext->probe && ext->probe(dev)) {
421 DEB_D(("ext->probe() failed for %p. skipping device.\n",dev));
422 goto err_free_i2c;
425 if (ext->attach(dev, pci_ext)) {
426 DEB_D(("ext->attach() failed for %p. skipping device.\n",dev));
427 goto err_unprobe;
430 INIT_LIST_HEAD(&dev->item);
431 list_add_tail(&dev->item,&saa7146_devices);
432 saa7146_num++;
434 err = 0;
435 out:
436 return err;
438 err_unprobe:
439 pci_set_drvdata(pci, NULL);
440 err_free_i2c:
441 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
442 dev->d_i2c.dma_handle);
443 err_free_rps1:
444 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
445 dev->d_rps1.dma_handle);
446 err_free_rps0:
447 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
448 dev->d_rps0.dma_handle);
449 err_free_irq:
450 free_irq(pci->irq, (void *)dev);
451 err_unmap:
452 iounmap(dev->mem);
453 err_release:
454 pci_release_region(pci, 0);
455 err_disable:
456 pci_disable_device(pci);
457 err_free:
458 kfree(dev);
459 goto out;
462 static void saa7146_remove_one(struct pci_dev *pdev)
464 struct saa7146_dev* dev = pci_get_drvdata(pdev);
465 struct {
466 void *addr;
467 dma_addr_t dma;
468 } dev_map[] = {
469 { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
470 { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
471 { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
472 { NULL, 0 }
473 }, *p;
475 DEB_EE(("dev:%p\n",dev));
477 dev->ext->detach(dev);
479 /* shut down all video dma transfers */
480 saa7146_write(dev, MC1, 0x00ff0000);
482 /* disable all irqs, release irq-routine */
483 saa7146_write(dev, IER, 0);
485 free_irq(pdev->irq, dev);
487 for (p = dev_map; p->addr; p++)
488 pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
490 iounmap(dev->mem);
491 pci_release_region(pdev, 0);
492 list_del(&dev->item);
493 pci_disable_device(pdev);
494 kfree(dev);
496 saa7146_num--;
499 /*********************************************************************************/
500 /* extension handling functions */
502 int saa7146_register_extension(struct saa7146_extension* ext)
504 DEB_EE(("ext:%p\n",ext));
506 ext->driver.name = ext->name;
507 ext->driver.id_table = ext->pci_tbl;
508 ext->driver.probe = saa7146_init_one;
509 ext->driver.remove = saa7146_remove_one;
511 printk("saa7146: register extension '%s'.\n",ext->name);
512 return pci_register_driver(&ext->driver);
515 int saa7146_unregister_extension(struct saa7146_extension* ext)
517 DEB_EE(("ext:%p\n",ext));
518 printk("saa7146: unregister extension '%s'.\n",ext->name);
519 pci_unregister_driver(&ext->driver);
520 return 0;
523 EXPORT_SYMBOL_GPL(saa7146_register_extension);
524 EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
526 /* misc functions used by extension modules */
527 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
528 EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
529 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
530 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
531 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
533 EXPORT_SYMBOL_GPL(saa7146_setgpio);
535 EXPORT_SYMBOL_GPL(saa7146_i2c_transfer);
536 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
538 EXPORT_SYMBOL_GPL(saa7146_debug);
539 EXPORT_SYMBOL_GPL(saa7146_devices);
540 EXPORT_SYMBOL_GPL(saa7146_devices_lock);
542 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
543 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
544 MODULE_LICENSE("GPL");