WIP FPC-III support
[linux/fpc-iii.git] / drivers / media / common / saa7146 / saa7146_core.c
blobf2d13b71416cbd0a3e7c0488b6bd6c8c997eae25
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 saa7146.o - driver for generic saa7146-based hardware
5 Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>
7 */
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)");
21 #if 0
22 static void dump_registers(struct saa7146_dev* dev)
24 int i = 0;
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));
30 #endif
32 /****************************************************************************
33 * gpio and debi helper functions
34 ****************************************************************************/
36 void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
38 u32 value = 0;
40 BUG_ON(port > 3);
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;
53 int err;
55 /* wait for registers to be programmed */
56 timeout = jiffies + usecs_to_jiffies(us1);
57 while (1) {
58 err = time_after(jiffies, timeout);
59 if (saa7146_read(dev, MC2) & 2)
60 break;
61 if (err) {
62 pr_debug("%s: %s timed out while waiting for registers getting programmed\n",
63 dev->name, __func__);
64 return -ETIMEDOUT;
66 msleep(1);
69 /* wait for transfer to complete */
70 timeout = jiffies + usecs_to_jiffies(us2);
71 while (1) {
72 err = time_after(jiffies, timeout);
73 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
74 break;
75 saa7146_read(dev, MC2);
76 if (err) {
77 DEB_S("%s: %s timed out while waiting for transfer completion\n",
78 dev->name, __func__);
79 return -ETIMEDOUT;
81 msleep(1);
84 return 0;
87 static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev *dev,
88 unsigned long us1, unsigned long us2)
90 unsigned long loops;
92 /* wait for registers to be programmed */
93 loops = us1;
94 while (1) {
95 if (saa7146_read(dev, MC2) & 2)
96 break;
97 if (!loops--) {
98 pr_err("%s: %s timed out while waiting for registers getting programmed\n",
99 dev->name, __func__);
100 return -ETIMEDOUT;
102 udelay(1);
105 /* wait for transfer to complete */
106 loops = us2 / 5;
107 while (1) {
108 if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
109 break;
110 saa7146_read(dev, MC2);
111 if (!loops--) {
112 DEB_S("%s: %s timed out while waiting for transfer completion\n",
113 dev->name, __func__);
114 return -ETIMEDOUT;
116 udelay(5);
119 return 0;
122 int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
124 if (nobusyloop)
125 return saa7146_wait_for_debi_done_sleep(dev, 50000, 250000);
126 else
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;
140 struct page *pg;
141 int i;
143 sglist = kmalloc_array(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
144 if (NULL == sglist)
145 return NULL;
146 sg_init_table(sglist, nr_pages);
147 for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
148 pg = vmalloc_to_page(virt);
149 if (NULL == pg)
150 goto err;
151 BUG_ON(PageHighMem(pg));
152 sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
154 return sglist;
156 err:
157 kfree(sglist);
158 return NULL;
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);
168 int slen = 0;
170 if (NULL == mem)
171 goto err_null;
173 if (!(pt->slist = vmalloc_to_sg(mem, pages)))
174 goto err_free_mem;
176 if (saa7146_pgtable_alloc(pci, pt))
177 goto err_free_slist;
179 pt->nents = pages;
180 slen = dma_map_sg(&pci->dev, pt->slist, pt->nents, DMA_FROM_DEVICE);
181 if (0 == slen)
182 goto err_free_pgtable;
184 if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
185 goto err_unmap_sg;
187 return mem;
189 err_unmap_sg:
190 dma_unmap_sg(&pci->dev, pt->slist, pt->nents, DMA_FROM_DEVICE);
191 err_free_pgtable:
192 saa7146_pgtable_free(pci, pt);
193 err_free_slist:
194 kfree(pt->slist);
195 pt->slist = NULL;
196 err_free_mem:
197 vfree(mem);
198 err_null:
199 return NULL;
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);
206 kfree(pt->slist);
207 pt->slist = NULL;
208 vfree(mem);
211 void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
213 if (NULL == pt->cpu)
214 return;
215 dma_free_coherent(&pci->dev, pt->size, pt->cpu, pt->dma);
216 pt->cpu = NULL;
219 int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
221 __le32 *cpu;
222 dma_addr_t dma_addr = 0;
224 cpu = dma_alloc_coherent(&pci->dev, PAGE_SIZE, &dma_addr, GFP_KERNEL);
225 if (NULL == cpu) {
226 return -ENOMEM;
228 pt->size = PAGE_SIZE;
229 pt->cpu = cpu;
230 pt->dma = dma_addr;
232 return 0;
235 int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
236 struct scatterlist *list, int sglen )
238 __le32 *ptr, fill;
239 int nr_pages = 0;
240 int i,p;
242 BUG_ON(0 == 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;
249 ptr = pt->cpu;
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),
254 list->offset);
256 for (p = 0; p * 4096 < list->length; p++, ptr++) {
257 *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
258 nr_pages++;
263 /* safety; fill the page table up with the last valid page */
264 fill = *(ptr-1);
265 for(i=nr_pages;i<1024;i++) {
266 *ptr++ = fill;
270 ptr = pt->cpu;
271 pr_debug("offset: %d\n", pt->offset);
272 for(i=0;i<5;i++) {
273 pr_debug("ptr1 %d: 0x%08x\n", i, ptr[i]);
276 return 0;
279 /********************************************************************************/
280 /* interrupt handler */
281 static irqreturn_t interrupt_hw(int irq, void *dev_id)
283 struct saa7146_dev *dev = dev_id;
284 u32 isr;
285 u32 ack_isr;
287 /* read out the interrupt status register */
288 ack_isr = isr = saa7146_read(dev, ISR);
290 /* is this our interrupt? */
291 if ( 0 == isr ) {
292 /* nope, some other device */
293 return IRQ_NONE;
296 if (dev->ext) {
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);
307 isr &= ~MASK_27;
309 if (0 != (isr & (MASK_28))) {
310 if (dev->vv_data && dev->vv_callback)
311 dev->vv_callback(dev,isr);
312 isr &= ~MASK_28;
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) {
318 dev->i2c_op = 0;
319 wake_up(&dev->i2c_wq);
320 } else {
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);
328 if( 0 != isr ) {
329 ERR("warning: interrupt enabled, but not handled properly.(0x%08x)\n",
330 isr);
331 ERR("disabling interrupt source(s)!\n");
332 SAA7146_IER_DISABLE(dev,isr);
334 saa7146_write(dev, ISR, ack_isr);
335 return IRQ_HANDLED;
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;
346 int err = -ENOMEM;
348 /* clear out mem for sure */
349 dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
350 if (!dev) {
351 ERR("out of memory\n");
352 goto out;
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);
361 if (err < 0) {
362 ERR("pci_enable_device() failed\n");
363 goto err_free;
366 /* enable bus-mastering */
367 pci_set_master(pci);
369 dev->pci = pci;
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");
377 if (err < 0)
378 goto err_disable;
380 dev->mem = ioremap(pci_resource_start(pci, 0),
381 pci_resource_len(pci, 0));
382 if (!dev->mem) {
383 ERR("ioremap() failed\n");
384 err = -ENODEV;
385 goto err_release;
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
390 values */
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,
406 dev->name, dev);
407 if (err < 0) {
408 ERR("request_irq() failed\n");
409 goto err_unmap;
412 err = -ENOMEM;
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,
417 GFP_KERNEL);
418 if (!dev->d_rps0.cpu_addr)
419 goto err_free_irq;
421 dev->d_rps1.cpu_addr = dma_alloc_coherent(&pci->dev, SAA7146_RPS_MEM,
422 &dev->d_rps1.dma_handle,
423 GFP_KERNEL);
424 if (!dev->d_rps1.cpu_addr)
425 goto err_free_rps0;
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)
430 goto err_free_rps1;
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);
437 dev->ext = ext;
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 */
453 err = -ENODEV;
455 if (ext->probe && ext->probe(dev)) {
456 DEB_D("ext->probe() failed for %p. skipping device.\n", dev);
457 goto err_free_i2c;
460 if (ext->attach(dev, pci_ext)) {
461 DEB_D("ext->attach() failed for %p. skipping device.\n", dev);
462 goto err_free_i2c;
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);
469 saa7146_num++;
471 err = 0;
472 out:
473 return err;
475 err_free_i2c:
476 dma_free_coherent(&pci->dev, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
477 dev->d_i2c.dma_handle);
478 err_free_rps1:
479 dma_free_coherent(&pci->dev, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
480 dev->d_rps1.dma_handle);
481 err_free_rps0:
482 dma_free_coherent(&pci->dev, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
483 dev->d_rps0.dma_handle);
484 err_free_irq:
485 free_irq(pci->irq, (void *)dev);
486 err_unmap:
487 iounmap(dev->mem);
488 err_release:
489 pci_release_region(pci, 0);
490 err_disable:
491 pci_disable_device(pci);
492 err_free:
493 kfree(dev);
494 goto out;
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);
501 struct {
502 void *addr;
503 dma_addr_t dma;
504 } dev_map[] = {
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 },
508 { NULL, 0 }
509 }, *p;
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,
525 p->dma);
527 iounmap(dev->mem);
528 pci_release_region(pdev, 0);
529 pci_disable_device(pdev);
530 kfree(dev);
532 saa7146_num--;
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);
556 return 0;
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");