1 // SPDX-License-Identifier: GPL-2.0-or-later
3 saa7146.o - driver for generic saa7146-based hardware
5 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <media/drv-intf/saa7146.h>
12 #include <linux/module.h>
14 static int saa7146_num
;
16 unsigned int saa7146_debug
;
18 module_param(saa7146_debug
, uint
, 0644);
19 MODULE_PARM_DESC(saa7146_debug
, "debug level (default: 0)");
22 static void dump_registers(struct saa7146_dev
* dev
)
26 pr_info(" @ %li jiffies:\n", jiffies
);
27 for (i
= 0; i
<= 0x148; i
+= 4)
28 pr_info("0x%03x: 0x%08x\n", i
, saa7146_read(dev
, i
));
32 /****************************************************************************
33 * gpio and debi helper functions
34 ****************************************************************************/
36 void saa7146_setgpio(struct saa7146_dev
*dev
, int port
, u32 data
)
42 value
= saa7146_read(dev
, GPIO_CTRL
);
43 value
&= ~(0xff << (8*port
));
44 value
|= (data
<< (8*port
));
45 saa7146_write(dev
, GPIO_CTRL
, value
);
48 /* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
49 static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev
*dev
,
50 unsigned long us1
, unsigned long us2
)
52 unsigned long timeout
;
55 /* wait for registers to be programmed */
56 timeout
= jiffies
+ usecs_to_jiffies(us1
);
58 err
= time_after(jiffies
, timeout
);
59 if (saa7146_read(dev
, MC2
) & 2)
62 pr_debug("%s: %s timed out while waiting for registers getting programmed\n",
69 /* wait for transfer to complete */
70 timeout
= jiffies
+ usecs_to_jiffies(us2
);
72 err
= time_after(jiffies
, timeout
);
73 if (!(saa7146_read(dev
, PSR
) & SPCI_DEBI_S
))
75 saa7146_read(dev
, MC2
);
77 DEB_S("%s: %s timed out while waiting for transfer completion\n",
87 static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev
*dev
,
88 unsigned long us1
, unsigned long us2
)
92 /* wait for registers to be programmed */
95 if (saa7146_read(dev
, MC2
) & 2)
98 pr_err("%s: %s timed out while waiting for registers getting programmed\n",
105 /* wait for transfer to complete */
108 if (!(saa7146_read(dev
, PSR
) & SPCI_DEBI_S
))
110 saa7146_read(dev
, MC2
);
112 DEB_S("%s: %s timed out while waiting for transfer completion\n",
113 dev
->name
, __func__
);
122 int saa7146_wait_for_debi_done(struct saa7146_dev
*dev
, int nobusyloop
)
125 return saa7146_wait_for_debi_done_sleep(dev
, 50000, 250000);
127 return saa7146_wait_for_debi_done_busyloop(dev
, 50000, 250000);
130 /****************************************************************************
131 * general helper functions
132 ****************************************************************************/
134 /* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.c
135 make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
136 may be triggered on highmem machines */
137 static struct scatterlist
* vmalloc_to_sg(unsigned char *virt
, int nr_pages
)
139 struct scatterlist
*sglist
;
143 sglist
= kmalloc_array(nr_pages
, sizeof(struct scatterlist
), GFP_KERNEL
);
146 sg_init_table(sglist
, nr_pages
);
147 for (i
= 0; i
< nr_pages
; i
++, virt
+= PAGE_SIZE
) {
148 pg
= vmalloc_to_page(virt
);
151 BUG_ON(PageHighMem(pg
));
152 sg_set_page(&sglist
[i
], pg
, PAGE_SIZE
, 0);
161 /********************************************************************************/
162 /* common page table functions */
164 void *saa7146_vmalloc_build_pgtable(struct pci_dev
*pci
, long length
, struct saa7146_pgtable
*pt
)
166 int pages
= (length
+PAGE_SIZE
-1)/PAGE_SIZE
;
167 void *mem
= vmalloc_32(length
);
173 if (!(pt
->slist
= vmalloc_to_sg(mem
, pages
)))
176 if (saa7146_pgtable_alloc(pci
, pt
))
180 slen
= dma_map_sg(&pci
->dev
, pt
->slist
, pt
->nents
, DMA_FROM_DEVICE
);
182 goto err_free_pgtable
;
184 if (0 != saa7146_pgtable_build_single(pci
, pt
, pt
->slist
, slen
))
190 dma_unmap_sg(&pci
->dev
, pt
->slist
, pt
->nents
, DMA_FROM_DEVICE
);
192 saa7146_pgtable_free(pci
, pt
);
202 void saa7146_vfree_destroy_pgtable(struct pci_dev
*pci
, void *mem
, struct saa7146_pgtable
*pt
)
204 dma_unmap_sg(&pci
->dev
, pt
->slist
, pt
->nents
, DMA_FROM_DEVICE
);
205 saa7146_pgtable_free(pci
, pt
);
211 void saa7146_pgtable_free(struct pci_dev
*pci
, struct saa7146_pgtable
*pt
)
215 dma_free_coherent(&pci
->dev
, pt
->size
, pt
->cpu
, pt
->dma
);
219 int saa7146_pgtable_alloc(struct pci_dev
*pci
, struct saa7146_pgtable
*pt
)
222 dma_addr_t dma_addr
= 0;
224 cpu
= dma_alloc_coherent(&pci
->dev
, PAGE_SIZE
, &dma_addr
, GFP_KERNEL
);
228 pt
->size
= PAGE_SIZE
;
235 int saa7146_pgtable_build_single(struct pci_dev
*pci
, struct saa7146_pgtable
*pt
,
236 struct scatterlist
*list
, int sglen
)
243 BUG_ON(list
->offset
> PAGE_SIZE
);
245 /* if we have a user buffer, the first page may not be
246 aligned to a page boundary. */
247 pt
->offset
= list
->offset
;
250 for (i
= 0; i
< sglen
; i
++, list
++) {
252 pr_debug("i:%d, adr:0x%08x, len:%d, offset:%d\n",
253 i, sg_dma_address(list), sg_dma_len(list),
256 for (p
= 0; p
* 4096 < list
->length
; p
++, ptr
++) {
257 *ptr
= cpu_to_le32(sg_dma_address(list
) + p
* 4096);
263 /* safety; fill the page table up with the last valid page */
265 for(i
=nr_pages
;i
<1024;i
++) {
271 pr_debug("offset: %d\n", pt->offset);
273 pr_debug("ptr1 %d: 0x%08x\n", i, ptr[i]);
279 /********************************************************************************/
280 /* interrupt handler */
281 static irqreturn_t
interrupt_hw(int irq
, void *dev_id
)
283 struct saa7146_dev
*dev
= dev_id
;
287 /* read out the interrupt status register */
288 ack_isr
= isr
= saa7146_read(dev
, ISR
);
290 /* is this our interrupt? */
292 /* nope, some other device */
297 if (dev
->ext
->irq_mask
& isr
) {
298 if (dev
->ext
->irq_func
)
299 dev
->ext
->irq_func(dev
, &isr
);
300 isr
&= ~dev
->ext
->irq_mask
;
303 if (0 != (isr
& (MASK_27
))) {
304 DEB_INT("irq: RPS0 (0x%08x)\n", isr
);
305 if (dev
->vv_data
&& dev
->vv_callback
)
306 dev
->vv_callback(dev
,isr
);
309 if (0 != (isr
& (MASK_28
))) {
310 if (dev
->vv_data
&& dev
->vv_callback
)
311 dev
->vv_callback(dev
,isr
);
314 if (0 != (isr
& (MASK_16
|MASK_17
))) {
315 SAA7146_IER_DISABLE(dev
, MASK_16
|MASK_17
);
316 /* only wake up if we expect something */
317 if (0 != dev
->i2c_op
) {
319 wake_up(&dev
->i2c_wq
);
321 u32 psr
= saa7146_read(dev
, PSR
);
322 u32 ssr
= saa7146_read(dev
, SSR
);
323 pr_warn("%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
324 dev
->name
, isr
, psr
, ssr
);
326 isr
&= ~(MASK_16
|MASK_17
);
329 ERR("warning: interrupt enabled, but not handled properly.(0x%08x)\n",
331 ERR("disabling interrupt source(s)!\n");
332 SAA7146_IER_DISABLE(dev
,isr
);
334 saa7146_write(dev
, ISR
, ack_isr
);
338 /*********************************************************************************/
339 /* configuration-functions */
341 static int saa7146_init_one(struct pci_dev
*pci
, const struct pci_device_id
*ent
)
343 struct saa7146_pci_extension_data
*pci_ext
= (struct saa7146_pci_extension_data
*)ent
->driver_data
;
344 struct saa7146_extension
*ext
= pci_ext
->ext
;
345 struct saa7146_dev
*dev
;
348 /* clear out mem for sure */
349 dev
= kzalloc(sizeof(struct saa7146_dev
), GFP_KERNEL
);
351 ERR("out of memory\n");
355 /* create a nice device name */
356 sprintf(dev
->name
, "saa7146 (%d)", saa7146_num
);
358 DEB_EE("pci:%p\n", pci
);
360 err
= pci_enable_device(pci
);
362 ERR("pci_enable_device() failed\n");
366 /* enable bus-mastering */
371 /* get chip-revision; this is needed to enable bug-fixes */
372 dev
->revision
= pci
->revision
;
374 /* remap the memory from virtual to physical address */
376 err
= pci_request_region(pci
, 0, "saa7146");
380 dev
->mem
= ioremap(pci_resource_start(pci
, 0),
381 pci_resource_len(pci
, 0));
383 ERR("ioremap() failed\n");
388 /* we don't do a master reset here anymore, it screws up
389 some boards that don't have an i2c-eeprom for configuration
392 saa7146_write(dev, MC1, MASK_31);
395 /* disable all irqs */
396 saa7146_write(dev
, IER
, 0);
398 /* shut down all dma transfers and rps tasks */
399 saa7146_write(dev
, MC1
, 0x30ff0000);
401 /* clear out any rps-signals pending */
402 saa7146_write(dev
, MC2
, 0xf8000000);
404 /* request an interrupt for the saa7146 */
405 err
= request_irq(pci
->irq
, interrupt_hw
, IRQF_SHARED
,
408 ERR("request_irq() failed\n");
414 /* get memory for various stuff */
415 dev
->d_rps0
.cpu_addr
= dma_alloc_coherent(&pci
->dev
, SAA7146_RPS_MEM
,
416 &dev
->d_rps0
.dma_handle
,
418 if (!dev
->d_rps0
.cpu_addr
)
421 dev
->d_rps1
.cpu_addr
= dma_alloc_coherent(&pci
->dev
, SAA7146_RPS_MEM
,
422 &dev
->d_rps1
.dma_handle
,
424 if (!dev
->d_rps1
.cpu_addr
)
427 dev
->d_i2c
.cpu_addr
= dma_alloc_coherent(&pci
->dev
, SAA7146_RPS_MEM
,
428 &dev
->d_i2c
.dma_handle
, GFP_KERNEL
);
429 if (!dev
->d_i2c
.cpu_addr
)
432 /* the rest + print status message */
434 pr_info("found saa7146 @ mem %p (revision %d, irq %d) (0x%04x,0x%04x)\n",
435 dev
->mem
, dev
->revision
, pci
->irq
,
436 pci
->subsystem_vendor
, pci
->subsystem_device
);
439 mutex_init(&dev
->v4l2_lock
);
440 spin_lock_init(&dev
->int_slock
);
441 spin_lock_init(&dev
->slock
);
443 mutex_init(&dev
->i2c_lock
);
445 dev
->module
= THIS_MODULE
;
446 init_waitqueue_head(&dev
->i2c_wq
);
448 /* set some sane pci arbitrition values */
449 saa7146_write(dev
, PCI_BT_V1
, 0x1c00101f);
451 /* TODO: use the status code of the callback */
455 if (ext
->probe
&& ext
->probe(dev
)) {
456 DEB_D("ext->probe() failed for %p. skipping device.\n", dev
);
460 if (ext
->attach(dev
, pci_ext
)) {
461 DEB_D("ext->attach() failed for %p. skipping device.\n", dev
);
464 /* V4L extensions will set the pci drvdata to the v4l2_device in the
465 attach() above. So for those cards that do not use V4L we have to
466 set it explicitly. */
467 pci_set_drvdata(pci
, &dev
->v4l2_dev
);
476 dma_free_coherent(&pci
->dev
, SAA7146_RPS_MEM
, dev
->d_i2c
.cpu_addr
,
477 dev
->d_i2c
.dma_handle
);
479 dma_free_coherent(&pci
->dev
, SAA7146_RPS_MEM
, dev
->d_rps1
.cpu_addr
,
480 dev
->d_rps1
.dma_handle
);
482 dma_free_coherent(&pci
->dev
, SAA7146_RPS_MEM
, dev
->d_rps0
.cpu_addr
,
483 dev
->d_rps0
.dma_handle
);
485 free_irq(pci
->irq
, (void *)dev
);
489 pci_release_region(pci
, 0);
491 pci_disable_device(pci
);
497 static void saa7146_remove_one(struct pci_dev
*pdev
)
499 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
500 struct saa7146_dev
*dev
= to_saa7146_dev(v4l2_dev
);
505 { dev
->d_i2c
.cpu_addr
, dev
->d_i2c
.dma_handle
},
506 { dev
->d_rps1
.cpu_addr
, dev
->d_rps1
.dma_handle
},
507 { dev
->d_rps0
.cpu_addr
, dev
->d_rps0
.dma_handle
},
511 DEB_EE("dev:%p\n", dev
);
513 dev
->ext
->detach(dev
);
515 /* shut down all video dma transfers */
516 saa7146_write(dev
, MC1
, 0x00ff0000);
518 /* disable all irqs, release irq-routine */
519 saa7146_write(dev
, IER
, 0);
521 free_irq(pdev
->irq
, dev
);
523 for (p
= dev_map
; p
->addr
; p
++)
524 dma_free_coherent(&pdev
->dev
, SAA7146_RPS_MEM
, p
->addr
,
528 pci_release_region(pdev
, 0);
529 pci_disable_device(pdev
);
535 /*********************************************************************************/
536 /* extension handling functions */
538 int saa7146_register_extension(struct saa7146_extension
* ext
)
540 DEB_EE("ext:%p\n", ext
);
542 ext
->driver
.name
= ext
->name
;
543 ext
->driver
.id_table
= ext
->pci_tbl
;
544 ext
->driver
.probe
= saa7146_init_one
;
545 ext
->driver
.remove
= saa7146_remove_one
;
547 pr_info("register extension '%s'\n", ext
->name
);
548 return pci_register_driver(&ext
->driver
);
551 int saa7146_unregister_extension(struct saa7146_extension
* ext
)
553 DEB_EE("ext:%p\n", ext
);
554 pr_info("unregister extension '%s'\n", ext
->name
);
555 pci_unregister_driver(&ext
->driver
);
559 EXPORT_SYMBOL_GPL(saa7146_register_extension
);
560 EXPORT_SYMBOL_GPL(saa7146_unregister_extension
);
562 /* misc functions used by extension modules */
563 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc
);
564 EXPORT_SYMBOL_GPL(saa7146_pgtable_free
);
565 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single
);
566 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable
);
567 EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable
);
568 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done
);
570 EXPORT_SYMBOL_GPL(saa7146_setgpio
);
572 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare
);
574 EXPORT_SYMBOL_GPL(saa7146_debug
);
576 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
577 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
578 MODULE_LICENSE("GPL");