Merge branch 'sg' of git://git.kernel.dk/linux-2.6-block
[linux-2.6] / drivers / media / common / saa7146_core.c
1 /*
2     saa7146.o - driver for generic saa7146-based hardware
3
4     Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
5
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.
10
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.
15
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.
19 */
20
21 #include <media/saa7146.h>
22
23 LIST_HEAD(saa7146_devices);
24 DEFINE_MUTEX(saa7146_devices_lock);
25
26 static int saa7146_num;
27
28 unsigned int saa7146_debug;
29
30 module_param(saa7146_debug, uint, 0644);
31 MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
32
33 #if 0
34 static void dump_registers(struct saa7146_dev* dev)
35 {
36         int i = 0;
37
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));
41         }
42 }
43 #endif
44
45 /****************************************************************************
46  * gpio and debi helper functions
47  ****************************************************************************/
48
49 void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
50 {
51         u32 value = 0;
52
53         BUG_ON(port > 3);
54
55         value = saa7146_read(dev, GPIO_CTRL);
56         value &= ~(0xff << (8*port));
57         value |= (data << (8*port));
58         saa7146_write(dev, GPIO_CTRL, value);
59 }
60
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)
63 {
64         unsigned long start;
65         int err;
66
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;
76                 }
77                 if (nobusyloop)
78                         msleep(1);
79         }
80
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;
91                 }
92                 if (nobusyloop)
93                         msleep(1);
94         }
95
96         return 0;
97 }
98
99 /****************************************************************************
100  * general helper functions
101  ****************************************************************************/
102
103 /* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.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)
107 {
108         struct scatterlist *sglist;
109         struct page *pg;
110         int i;
111
112         sglist = kcalloc(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
113         if (NULL == sglist)
114                 return NULL;
115         sg_init_table(sglist, nr_pages);
116         for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
117                 pg = vmalloc_to_page(virt);
118                 if (NULL == pg)
119                         goto err;
120                 BUG_ON(PageHighMem(pg));
121                 sg_set_page(&sglist[i], pg);
122                 sglist[i].length = PAGE_SIZE;
123         }
124         return sglist;
125
126  err:
127         kfree(sglist);
128         return NULL;
129 }
130
131 /********************************************************************************/
132 /* common page table functions */
133
134 void *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
135 {
136         int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
137         void *mem = vmalloc_32(length);
138         int slen = 0;
139
140         if (NULL == mem)
141                 goto err_null;
142
143         if (!(pt->slist = vmalloc_to_sg(mem, pages)))
144                 goto err_free_mem;
145
146         if (saa7146_pgtable_alloc(pci, pt))
147                 goto err_free_slist;
148
149         pt->nents = pages;
150         slen = pci_map_sg(pci,pt->slist,pt->nents,PCI_DMA_FROMDEVICE);
151         if (0 == slen)
152                 goto err_free_pgtable;
153
154         if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
155                 goto err_unmap_sg;
156
157         return mem;
158
159 err_unmap_sg:
160         pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
161 err_free_pgtable:
162         saa7146_pgtable_free(pci, pt);
163 err_free_slist:
164         kfree(pt->slist);
165         pt->slist = NULL;
166 err_free_mem:
167         vfree(mem);
168 err_null:
169         return NULL;
170 }
171
172 void saa7146_vfree_destroy_pgtable(struct pci_dev *pci, void *mem, struct saa7146_pgtable *pt)
173 {
174         pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
175         saa7146_pgtable_free(pci, pt);
176         kfree(pt->slist);
177         pt->slist = NULL;
178         vfree(mem);
179 }
180
181 void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
182 {
183         if (NULL == pt->cpu)
184                 return;
185         pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
186         pt->cpu = NULL;
187 }
188
189 int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
190 {
191         u32          *cpu;
192         dma_addr_t   dma_addr;
193
194         cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
195         if (NULL == cpu) {
196                 return -ENOMEM;
197         }
198         pt->size = PAGE_SIZE;
199         pt->cpu  = cpu;
200         pt->dma  = dma_addr;
201
202         return 0;
203 }
204
205 int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
206         struct scatterlist *list, int sglen  )
207 {
208         u32 *ptr, fill;
209         int nr_pages = 0;
210         int i,p;
211
212         BUG_ON(0 == sglen);
213         BUG_ON(list->offset > PAGE_SIZE);
214
215         /* if we have a user buffer, the first page may not be
216            aligned to a page boundary. */
217         pt->offset = list->offset;
218
219         ptr = pt->cpu;
220         for (i = 0; i < sglen; i++, list++) {
221 /*
222                 printk("i:%d, adr:0x%08x, len:%d, offset:%d\n", i,sg_dma_address(list), sg_dma_len(list), list->offset);
223 */
224                 for (p = 0; p * 4096 < list->length; p++, ptr++) {
225                         *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
226                         nr_pages++;
227                 }
228         }
229
230
231         /* safety; fill the page table up with the last valid page */
232         fill = *(ptr-1);
233         for(i=nr_pages;i<1024;i++) {
234                 *ptr++ = fill;
235         }
236
237 /*
238         ptr = pt->cpu;
239         printk("offset: %d\n",pt->offset);
240         for(i=0;i<5;i++) {
241                 printk("ptr1 %d: 0x%08x\n",i,ptr[i]);
242         }
243 */
244         return 0;
245 }
246
247 /********************************************************************************/
248 /* interrupt handler */
249 static irqreturn_t interrupt_hw(int irq, void *dev_id)
250 {
251         struct saa7146_dev *dev = dev_id;
252         u32 isr;
253         u32 ack_isr;
254
255         /* read out the interrupt status register */
256         ack_isr = isr = saa7146_read(dev, ISR);
257
258         /* is this our interrupt? */
259         if ( 0 == isr ) {
260                 /* nope, some other device */
261                 return IRQ_NONE;
262         }
263
264         if( 0 != (dev->ext)) {
265                 if( 0 != (dev->ext->irq_mask & isr )) {
266                         if( 0 != dev->ext->irq_func ) {
267                                 dev->ext->irq_func(dev, &isr);
268                         }
269                         isr &= ~dev->ext->irq_mask;
270                 }
271         }
272         if (0 != (isr & (MASK_27))) {
273                 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
274                 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
275                         dev->vv_callback(dev,isr);
276                 }
277                 isr &= ~MASK_27;
278         }
279         if (0 != (isr & (MASK_28))) {
280                 if( 0 != dev->vv_data && 0 != dev->vv_callback) {
281                         dev->vv_callback(dev,isr);
282                 }
283                 isr &= ~MASK_28;
284         }
285         if (0 != (isr & (MASK_16|MASK_17))) {
286                 SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
287                 /* only wake up if we expect something */
288                 if (0 != dev->i2c_op) {
289                         dev->i2c_op = 0;
290                         wake_up(&dev->i2c_wq);
291                 } else {
292                         u32 psr = saa7146_read(dev, PSR);
293                         u32 ssr = saa7146_read(dev, SSR);
294                         printk(KERN_WARNING "%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
295                                dev->name, isr, psr, ssr);
296                 }
297                 isr &= ~(MASK_16|MASK_17);
298         }
299         if( 0 != isr ) {
300                 ERR(("warning: interrupt enabled, but not handled properly.(0x%08x)\n",isr));
301                 ERR(("disabling interrupt source(s)!\n"));
302                 SAA7146_IER_DISABLE(dev,isr);
303         }
304         saa7146_write(dev, ISR, ack_isr);
305         return IRQ_HANDLED;
306 }
307
308 /*********************************************************************************/
309 /* configuration-functions                                                       */
310
311 static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
312 {
313         struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
314         struct saa7146_extension *ext = pci_ext->ext;
315         struct saa7146_dev *dev;
316         int err = -ENOMEM;
317
318         /* clear out mem for sure */
319         dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
320         if (!dev) {
321                 ERR(("out of memory.\n"));
322                 goto out;
323         }
324
325         DEB_EE(("pci:%p\n",pci));
326
327         err = pci_enable_device(pci);
328         if (err < 0) {
329                 ERR(("pci_enable_device() failed.\n"));
330                 goto err_free;
331         }
332
333         /* enable bus-mastering */
334         pci_set_master(pci);
335
336         dev->pci = pci;
337
338         /* get chip-revision; this is needed to enable bug-fixes */
339         err = pci_read_config_dword(pci, PCI_CLASS_REVISION, &dev->revision);
340         if (err < 0) {
341                 ERR(("pci_read_config_dword() failed.\n"));
342                 goto err_disable;
343         }
344         dev->revision &= 0xf;
345
346         /* remap the memory from virtual to physical adress */
347
348         err = pci_request_region(pci, 0, "saa7146");
349         if (err < 0)
350                 goto err_disable;
351
352         dev->mem = ioremap(pci_resource_start(pci, 0),
353                            pci_resource_len(pci, 0));
354         if (!dev->mem) {
355                 ERR(("ioremap() failed.\n"));
356                 err = -ENODEV;
357                 goto err_release;
358         }
359
360         /* we don't do a master reset here anymore, it screws up
361            some boards that don't have an i2c-eeprom for configuration
362            values */
363 /*
364         saa7146_write(dev, MC1, MASK_31);
365 */
366
367         /* disable all irqs */
368         saa7146_write(dev, IER, 0);
369
370         /* shut down all dma transfers and rps tasks */
371         saa7146_write(dev, MC1, 0x30ff0000);
372
373         /* clear out any rps-signals pending */
374         saa7146_write(dev, MC2, 0xf8000000);
375
376         /* request an interrupt for the saa7146 */
377         err = request_irq(pci->irq, interrupt_hw, IRQF_SHARED | IRQF_DISABLED,
378                           dev->name, dev);
379         if (err < 0) {
380                 ERR(("request_irq() failed.\n"));
381                 goto err_unmap;
382         }
383
384         err = -ENOMEM;
385
386         /* get memory for various stuff */
387         dev->d_rps0.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
388                                                     &dev->d_rps0.dma_handle);
389         if (!dev->d_rps0.cpu_addr)
390                 goto err_free_irq;
391         memset(dev->d_rps0.cpu_addr, 0x0, SAA7146_RPS_MEM);
392
393         dev->d_rps1.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
394                                                     &dev->d_rps1.dma_handle);
395         if (!dev->d_rps1.cpu_addr)
396                 goto err_free_rps0;
397         memset(dev->d_rps1.cpu_addr, 0x0, SAA7146_RPS_MEM);
398
399         dev->d_i2c.cpu_addr = pci_alloc_consistent(pci, SAA7146_RPS_MEM,
400                                                    &dev->d_i2c.dma_handle);
401         if (!dev->d_i2c.cpu_addr)
402                 goto err_free_rps1;
403         memset(dev->d_i2c.cpu_addr, 0x0, SAA7146_RPS_MEM);
404
405         /* the rest + print status message */
406
407         /* create a nice device name */
408         sprintf(dev->name, "saa7146 (%d)", saa7146_num);
409
410         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));
411         dev->ext = ext;
412
413         pci_set_drvdata(pci, dev);
414
415         mutex_init(&dev->lock);
416         spin_lock_init(&dev->int_slock);
417         spin_lock_init(&dev->slock);
418
419         mutex_init(&dev->i2c_lock);
420
421         dev->module = THIS_MODULE;
422         init_waitqueue_head(&dev->i2c_wq);
423
424         /* set some sane pci arbitrition values */
425         saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
426
427         /* TODO: use the status code of the callback */
428
429         err = -ENODEV;
430
431         if (ext->probe && ext->probe(dev)) {
432                 DEB_D(("ext->probe() failed for %p. skipping device.\n",dev));
433                 goto err_free_i2c;
434         }
435
436         if (ext->attach(dev, pci_ext)) {
437                 DEB_D(("ext->attach() failed for %p. skipping device.\n",dev));
438                 goto err_unprobe;
439         }
440
441         INIT_LIST_HEAD(&dev->item);
442         list_add_tail(&dev->item,&saa7146_devices);
443         saa7146_num++;
444
445         err = 0;
446 out:
447         return err;
448
449 err_unprobe:
450         pci_set_drvdata(pci, NULL);
451 err_free_i2c:
452         pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
453                             dev->d_i2c.dma_handle);
454 err_free_rps1:
455         pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
456                             dev->d_rps1.dma_handle);
457 err_free_rps0:
458         pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
459                             dev->d_rps0.dma_handle);
460 err_free_irq:
461         free_irq(pci->irq, (void *)dev);
462 err_unmap:
463         iounmap(dev->mem);
464 err_release:
465         pci_release_region(pci, 0);
466 err_disable:
467         pci_disable_device(pci);
468 err_free:
469         kfree(dev);
470         goto out;
471 }
472
473 static void saa7146_remove_one(struct pci_dev *pdev)
474 {
475         struct saa7146_dev* dev = pci_get_drvdata(pdev);
476         struct {
477                 void *addr;
478                 dma_addr_t dma;
479         } dev_map[] = {
480                 { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
481                 { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
482                 { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
483                 { NULL, 0 }
484         }, *p;
485
486         DEB_EE(("dev:%p\n",dev));
487
488         dev->ext->detach(dev);
489
490         /* shut down all video dma transfers */
491         saa7146_write(dev, MC1, 0x00ff0000);
492
493         /* disable all irqs, release irq-routine */
494         saa7146_write(dev, IER, 0);
495
496         free_irq(pdev->irq, dev);
497
498         for (p = dev_map; p->addr; p++)
499                 pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
500
501         iounmap(dev->mem);
502         pci_release_region(pdev, 0);
503         list_del(&dev->item);
504         pci_disable_device(pdev);
505         kfree(dev);
506
507         saa7146_num--;
508 }
509
510 /*********************************************************************************/
511 /* extension handling functions                                                  */
512
513 int saa7146_register_extension(struct saa7146_extension* ext)
514 {
515         DEB_EE(("ext:%p\n",ext));
516
517         ext->driver.name = ext->name;
518         ext->driver.id_table = ext->pci_tbl;
519         ext->driver.probe = saa7146_init_one;
520         ext->driver.remove = saa7146_remove_one;
521
522         printk("saa7146: register extension '%s'.\n",ext->name);
523         return pci_register_driver(&ext->driver);
524 }
525
526 int saa7146_unregister_extension(struct saa7146_extension* ext)
527 {
528         DEB_EE(("ext:%p\n",ext));
529         printk("saa7146: unregister extension '%s'.\n",ext->name);
530         pci_unregister_driver(&ext->driver);
531         return 0;
532 }
533
534 EXPORT_SYMBOL_GPL(saa7146_register_extension);
535 EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
536
537 /* misc functions used by extension modules */
538 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
539 EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
540 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
541 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
542 EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable);
543 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
544
545 EXPORT_SYMBOL_GPL(saa7146_setgpio);
546
547 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
548
549 EXPORT_SYMBOL_GPL(saa7146_debug);
550 EXPORT_SYMBOL_GPL(saa7146_devices);
551 EXPORT_SYMBOL_GPL(saa7146_devices_lock);
552
553 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
554 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
555 MODULE_LICENSE("GPL");