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
)
40 if (WARN_ON(port
> 3))
43 value
= saa7146_read(dev
, GPIO_CTRL
);
44 value
&= ~(0xff << (8*port
));
45 value
|= (data
<< (8*port
));
46 saa7146_write(dev
, GPIO_CTRL
, value
);
49 /* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
50 static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev
*dev
,
51 unsigned long us1
, unsigned long us2
)
53 unsigned long timeout
;
56 /* wait for registers to be programmed */
57 timeout
= jiffies
+ usecs_to_jiffies(us1
);
59 err
= time_after(jiffies
, timeout
);
60 if (saa7146_read(dev
, MC2
) & 2)
63 pr_debug("%s: %s timed out while waiting for registers getting programmed\n",
70 /* wait for transfer to complete */
71 timeout
= jiffies
+ usecs_to_jiffies(us2
);
73 err
= time_after(jiffies
, timeout
);
74 if (!(saa7146_read(dev
, PSR
) & SPCI_DEBI_S
))
76 saa7146_read(dev
, MC2
);
78 DEB_S("%s: %s timed out while waiting for transfer completion\n",
88 static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev
*dev
,
89 unsigned long us1
, unsigned long us2
)
93 /* wait for registers to be programmed */
96 if (saa7146_read(dev
, MC2
) & 2)
99 pr_err("%s: %s timed out while waiting for registers getting programmed\n",
100 dev
->name
, __func__
);
106 /* wait for transfer to complete */
109 if (!(saa7146_read(dev
, PSR
) & SPCI_DEBI_S
))
111 saa7146_read(dev
, MC2
);
113 DEB_S("%s: %s timed out while waiting for transfer completion\n",
114 dev
->name
, __func__
);
123 int saa7146_wait_for_debi_done(struct saa7146_dev
*dev
, int nobusyloop
)
126 return saa7146_wait_for_debi_done_sleep(dev
, 50000, 250000);
128 return saa7146_wait_for_debi_done_busyloop(dev
, 50000, 250000);
131 /****************************************************************************
132 * general helper functions
133 ****************************************************************************/
135 /* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.c
136 make sure virt has been allocated with vmalloc_32(), otherwise return NULL
137 on highmem machines */
138 static struct scatterlist
* vmalloc_to_sg(unsigned char *virt
, int nr_pages
)
140 struct scatterlist
*sglist
;
144 sglist
= kmalloc_array(nr_pages
, sizeof(struct scatterlist
), GFP_KERNEL
);
147 sg_init_table(sglist
, nr_pages
);
148 for (i
= 0; i
< nr_pages
; i
++, virt
+= PAGE_SIZE
) {
149 pg
= vmalloc_to_page(virt
);
152 if (WARN_ON(PageHighMem(pg
)))
154 sg_set_page(&sglist
[i
], pg
, PAGE_SIZE
, 0);
163 /********************************************************************************/
164 /* common page table functions */
166 void *saa7146_vmalloc_build_pgtable(struct pci_dev
*pci
, long length
, struct saa7146_pgtable
*pt
)
168 int pages
= (length
+PAGE_SIZE
-1)/PAGE_SIZE
;
169 void *mem
= vmalloc_32(length
);
175 if (!(pt
->slist
= vmalloc_to_sg(mem
, pages
)))
178 if (saa7146_pgtable_alloc(pci
, pt
))
182 slen
= dma_map_sg(&pci
->dev
, pt
->slist
, pt
->nents
, DMA_FROM_DEVICE
);
184 goto err_free_pgtable
;
186 if (0 != saa7146_pgtable_build_single(pci
, pt
, pt
->slist
, slen
))
192 dma_unmap_sg(&pci
->dev
, pt
->slist
, pt
->nents
, DMA_FROM_DEVICE
);
194 saa7146_pgtable_free(pci
, pt
);
204 void saa7146_vfree_destroy_pgtable(struct pci_dev
*pci
, void *mem
, struct saa7146_pgtable
*pt
)
206 dma_unmap_sg(&pci
->dev
, pt
->slist
, pt
->nents
, DMA_FROM_DEVICE
);
207 saa7146_pgtable_free(pci
, pt
);
213 void saa7146_pgtable_free(struct pci_dev
*pci
, struct saa7146_pgtable
*pt
)
217 dma_free_coherent(&pci
->dev
, pt
->size
, pt
->cpu
, pt
->dma
);
221 int saa7146_pgtable_alloc(struct pci_dev
*pci
, struct saa7146_pgtable
*pt
)
224 dma_addr_t dma_addr
= 0;
226 cpu
= dma_alloc_coherent(&pci
->dev
, PAGE_SIZE
, &dma_addr
, GFP_KERNEL
);
230 pt
->size
= PAGE_SIZE
;
237 int saa7146_pgtable_build_single(struct pci_dev
*pci
, struct saa7146_pgtable
*pt
,
238 struct scatterlist
*list
, int sglen
)
240 struct sg_dma_page_iter dma_iter
;
245 if (WARN_ON(!sglen
) ||
246 WARN_ON(list
->offset
> PAGE_SIZE
))
249 /* if we have a user buffer, the first page may not be
250 aligned to a page boundary. */
251 pt
->offset
= list
->offset
;
254 for_each_sg_dma_page(list
, &dma_iter
, sglen
, 0) {
255 *ptr
++ = cpu_to_le32(sg_page_iter_dma_address(&dma_iter
));
260 /* safety; fill the page table up with the last valid page */
262 for (i
= nr_pages
; i
< 1024; i
++)
267 /********************************************************************************/
268 /* interrupt handler */
269 static irqreturn_t
interrupt_hw(int irq
, void *dev_id
)
271 struct saa7146_dev
*dev
= dev_id
;
275 /* read out the interrupt status register */
276 ack_isr
= isr
= saa7146_read(dev
, ISR
);
278 /* is this our interrupt? */
280 /* nope, some other device */
285 if (dev
->ext
->irq_mask
& isr
) {
286 if (dev
->ext
->irq_func
)
287 dev
->ext
->irq_func(dev
, &isr
);
288 isr
&= ~dev
->ext
->irq_mask
;
291 if (0 != (isr
& (MASK_27
))) {
292 DEB_INT("irq: RPS0 (0x%08x)\n", isr
);
293 if (dev
->vv_data
&& dev
->vv_callback
)
294 dev
->vv_callback(dev
,isr
);
297 if (0 != (isr
& (MASK_28
))) {
298 if (dev
->vv_data
&& dev
->vv_callback
)
299 dev
->vv_callback(dev
,isr
);
302 if (0 != (isr
& (MASK_16
|MASK_17
))) {
303 SAA7146_IER_DISABLE(dev
, MASK_16
|MASK_17
);
304 /* only wake up if we expect something */
305 if (0 != dev
->i2c_op
) {
307 wake_up(&dev
->i2c_wq
);
309 u32 psr
= saa7146_read(dev
, PSR
);
310 u32 ssr
= saa7146_read(dev
, SSR
);
311 pr_warn("%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
312 dev
->name
, isr
, psr
, ssr
);
314 isr
&= ~(MASK_16
|MASK_17
);
317 ERR("warning: interrupt enabled, but not handled properly.(0x%08x)\n",
319 ERR("disabling interrupt source(s)!\n");
320 SAA7146_IER_DISABLE(dev
,isr
);
322 saa7146_write(dev
, ISR
, ack_isr
);
326 /*********************************************************************************/
327 /* configuration-functions */
329 static int saa7146_init_one(struct pci_dev
*pci
, const struct pci_device_id
*ent
)
331 struct saa7146_pci_extension_data
*pci_ext
= (struct saa7146_pci_extension_data
*)ent
->driver_data
;
332 struct saa7146_extension
*ext
= pci_ext
->ext
;
333 struct saa7146_dev
*dev
;
336 /* clear out mem for sure */
337 dev
= kzalloc(sizeof(struct saa7146_dev
), GFP_KERNEL
);
339 ERR("out of memory\n");
343 /* create a nice device name */
344 sprintf(dev
->name
, "saa7146 (%d)", saa7146_num
);
346 DEB_EE("pci:%p\n", pci
);
348 err
= pci_enable_device(pci
);
350 ERR("pci_enable_device() failed\n");
354 /* enable bus-mastering */
359 /* get chip-revision; this is needed to enable bug-fixes */
360 dev
->revision
= pci
->revision
;
362 /* remap the memory from virtual to physical address */
364 err
= pci_request_region(pci
, 0, "saa7146");
368 dev
->mem
= ioremap(pci_resource_start(pci
, 0),
369 pci_resource_len(pci
, 0));
371 ERR("ioremap() failed\n");
376 /* we don't do a master reset here anymore, it screws up
377 some boards that don't have an i2c-eeprom for configuration
380 saa7146_write(dev, MC1, MASK_31);
383 /* disable all irqs */
384 saa7146_write(dev
, IER
, 0);
386 /* shut down all dma transfers and rps tasks */
387 saa7146_write(dev
, MC1
, 0x30ff0000);
389 /* clear out any rps-signals pending */
390 saa7146_write(dev
, MC2
, 0xf8000000);
392 /* request an interrupt for the saa7146 */
393 err
= request_irq(pci
->irq
, interrupt_hw
, IRQF_SHARED
,
396 ERR("request_irq() failed\n");
402 /* get memory for various stuff */
403 dev
->d_rps0
.cpu_addr
= dma_alloc_coherent(&pci
->dev
, SAA7146_RPS_MEM
,
404 &dev
->d_rps0
.dma_handle
,
406 if (!dev
->d_rps0
.cpu_addr
)
409 dev
->d_rps1
.cpu_addr
= dma_alloc_coherent(&pci
->dev
, SAA7146_RPS_MEM
,
410 &dev
->d_rps1
.dma_handle
,
412 if (!dev
->d_rps1
.cpu_addr
)
415 dev
->d_i2c
.cpu_addr
= dma_alloc_coherent(&pci
->dev
, SAA7146_RPS_MEM
,
416 &dev
->d_i2c
.dma_handle
, GFP_KERNEL
);
417 if (!dev
->d_i2c
.cpu_addr
)
420 /* the rest + print status message */
422 pr_info("found saa7146 @ mem %p (revision %d, irq %d) (0x%04x,0x%04x)\n",
423 dev
->mem
, dev
->revision
, pci
->irq
,
424 pci
->subsystem_vendor
, pci
->subsystem_device
);
427 mutex_init(&dev
->v4l2_lock
);
428 spin_lock_init(&dev
->int_slock
);
429 spin_lock_init(&dev
->slock
);
431 mutex_init(&dev
->i2c_lock
);
433 dev
->module
= THIS_MODULE
;
434 init_waitqueue_head(&dev
->i2c_wq
);
436 /* set some sane pci arbitrition values */
437 saa7146_write(dev
, PCI_BT_V1
, 0x1c00101f);
439 /* TODO: use the status code of the callback */
443 if (ext
->probe
&& ext
->probe(dev
)) {
444 DEB_D("ext->probe() failed for %p. skipping device.\n", dev
);
448 if (ext
->attach(dev
, pci_ext
)) {
449 DEB_D("ext->attach() failed for %p. skipping device.\n", dev
);
452 /* V4L extensions will set the pci drvdata to the v4l2_device in the
453 attach() above. So for those cards that do not use V4L we have to
454 set it explicitly. */
455 pci_set_drvdata(pci
, &dev
->v4l2_dev
);
464 dma_free_coherent(&pci
->dev
, SAA7146_RPS_MEM
, dev
->d_i2c
.cpu_addr
,
465 dev
->d_i2c
.dma_handle
);
467 dma_free_coherent(&pci
->dev
, SAA7146_RPS_MEM
, dev
->d_rps1
.cpu_addr
,
468 dev
->d_rps1
.dma_handle
);
470 dma_free_coherent(&pci
->dev
, SAA7146_RPS_MEM
, dev
->d_rps0
.cpu_addr
,
471 dev
->d_rps0
.dma_handle
);
473 free_irq(pci
->irq
, (void *)dev
);
477 pci_release_region(pci
, 0);
479 pci_disable_device(pci
);
485 static void saa7146_remove_one(struct pci_dev
*pdev
)
487 struct v4l2_device
*v4l2_dev
= pci_get_drvdata(pdev
);
488 struct saa7146_dev
*dev
= to_saa7146_dev(v4l2_dev
);
493 { dev
->d_i2c
.cpu_addr
, dev
->d_i2c
.dma_handle
},
494 { dev
->d_rps1
.cpu_addr
, dev
->d_rps1
.dma_handle
},
495 { dev
->d_rps0
.cpu_addr
, dev
->d_rps0
.dma_handle
},
499 DEB_EE("dev:%p\n", dev
);
501 dev
->ext
->detach(dev
);
503 /* shut down all video dma transfers */
504 saa7146_write(dev
, MC1
, 0x00ff0000);
506 /* disable all irqs, release irq-routine */
507 saa7146_write(dev
, IER
, 0);
509 free_irq(pdev
->irq
, dev
);
511 for (p
= dev_map
; p
->addr
; p
++)
512 dma_free_coherent(&pdev
->dev
, SAA7146_RPS_MEM
, p
->addr
,
516 pci_release_region(pdev
, 0);
517 pci_disable_device(pdev
);
523 /*********************************************************************************/
524 /* extension handling functions */
526 int saa7146_register_extension(struct saa7146_extension
* ext
)
528 DEB_EE("ext:%p\n", ext
);
530 ext
->driver
.name
= ext
->name
;
531 ext
->driver
.id_table
= ext
->pci_tbl
;
532 ext
->driver
.probe
= saa7146_init_one
;
533 ext
->driver
.remove
= saa7146_remove_one
;
535 pr_info("register extension '%s'\n", ext
->name
);
536 return pci_register_driver(&ext
->driver
);
539 int saa7146_unregister_extension(struct saa7146_extension
* ext
)
541 DEB_EE("ext:%p\n", ext
);
542 pr_info("unregister extension '%s'\n", ext
->name
);
543 pci_unregister_driver(&ext
->driver
);
547 EXPORT_SYMBOL_GPL(saa7146_register_extension
);
548 EXPORT_SYMBOL_GPL(saa7146_unregister_extension
);
550 /* misc functions used by extension modules */
551 EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc
);
552 EXPORT_SYMBOL_GPL(saa7146_pgtable_free
);
553 EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single
);
554 EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable
);
555 EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable
);
556 EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done
);
558 EXPORT_SYMBOL_GPL(saa7146_setgpio
);
560 EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare
);
562 EXPORT_SYMBOL_GPL(saa7146_debug
);
564 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
565 MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
566 MODULE_LICENSE("GPL");