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