saa7146_core.c 14.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
/*
    saa7146.o - driver for generic saa7146-based hardware

    Copyright (C) 1998-2003 Michael Hunold <michael@mihu.de>

*/

9 10
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

11
#include <media/drv-intf/saa7146.h>
12
#include <linux/module.h>
L
Linus Torvalds 已提交
13

14
static int saa7146_num;
L
Linus Torvalds 已提交
15

16
unsigned int saa7146_debug;
L
Linus Torvalds 已提交
17

18
module_param(saa7146_debug, uint, 0644);
L
Linus Torvalds 已提交
19 20 21 22 23 24 25
MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");

#if 0
static void dump_registers(struct saa7146_dev* dev)
{
	int i = 0;

26 27 28
	pr_info(" @ %li jiffies:\n", jiffies);
	for (i = 0; i <= 0x148; i += 4)
		pr_info("0x%03x: 0x%08x\n", i, saa7146_read(dev, i));
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
}
#endif

/****************************************************************************
 * gpio and debi helper functions
 ****************************************************************************/

void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
{
	u32 value = 0;

	BUG_ON(port > 3);

	value = saa7146_read(dev, GPIO_CTRL);
	value &= ~(0xff << (8*port));
	value |= (data << (8*port));
	saa7146_write(dev, GPIO_CTRL, value);
}

/* This DEBI code is based on the saa7146 Stradis driver by Nathan Laredo */
49 50
static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev *dev,
				unsigned long us1, unsigned long us2)
L
Linus Torvalds 已提交
51
{
52
	unsigned long timeout;
53
	int err;
L
Linus Torvalds 已提交
54 55

	/* wait for registers to be programmed */
56
	timeout = jiffies + usecs_to_jiffies(us1);
L
Linus Torvalds 已提交
57
	while (1) {
58
		err = time_after(jiffies, timeout);
59 60 61
		if (saa7146_read(dev, MC2) & 2)
			break;
		if (err) {
62
			pr_debug("%s: %s timed out while waiting for registers getting programmed\n",
63
			       dev->name, __func__);
L
Linus Torvalds 已提交
64 65
			return -ETIMEDOUT;
		}
66
		msleep(1);
L
Linus Torvalds 已提交
67 68 69
	}

	/* wait for transfer to complete */
70
	timeout = jiffies + usecs_to_jiffies(us2);
L
Linus Torvalds 已提交
71
	while (1) {
72
		err = time_after(jiffies, timeout);
L
Linus Torvalds 已提交
73 74 75
		if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
			break;
		saa7146_read(dev, MC2);
76
		if (err) {
77 78
			DEB_S("%s: %s timed out while waiting for transfer completion\n",
			      dev->name, __func__);
L
Linus Torvalds 已提交
79 80
			return -ETIMEDOUT;
		}
81
		msleep(1);
L
Linus Torvalds 已提交
82 83 84 85 86
	}

	return 0;
}

87 88 89 90 91 92 93 94 95 96 97
static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev *dev,
				unsigned long us1, unsigned long us2)
{
	unsigned long loops;

	/* wait for registers to be programmed */
	loops = us1;
	while (1) {
		if (saa7146_read(dev, MC2) & 2)
			break;
		if (!loops--) {
98 99
			pr_err("%s: %s timed out while waiting for registers getting programmed\n",
			       dev->name, __func__);
100 101 102 103 104 105 106 107 108 109 110 111
			return -ETIMEDOUT;
		}
		udelay(1);
	}

	/* wait for transfer to complete */
	loops = us2 / 5;
	while (1) {
		if (!(saa7146_read(dev, PSR) & SPCI_DEBI_S))
			break;
		saa7146_read(dev, MC2);
		if (!loops--) {
112 113
			DEB_S("%s: %s timed out while waiting for transfer completion\n",
			      dev->name, __func__);
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
			return -ETIMEDOUT;
		}
		udelay(5);
	}

	return 0;
}

int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
{
	if (nobusyloop)
		return saa7146_wait_for_debi_done_sleep(dev, 50000, 250000);
	else
		return saa7146_wait_for_debi_done_busyloop(dev, 50000, 250000);
}

L
Linus Torvalds 已提交
130 131 132 133
/****************************************************************************
 * general helper functions
 ****************************************************************************/

134
/* this is videobuf_vmalloc_to_sg() from videobuf-dma-sg.c
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142
   make sure virt has been allocated with vmalloc_32(), otherwise the BUG()
   may be triggered on highmem machines */
static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
{
	struct scatterlist *sglist;
	struct page *pg;
	int i;

143
	sglist = kmalloc_array(nr_pages, sizeof(struct scatterlist), GFP_KERNEL);
L
Linus Torvalds 已提交
144 145
	if (NULL == sglist)
		return NULL;
J
Jens Axboe 已提交
146
	sg_init_table(sglist, nr_pages);
L
Linus Torvalds 已提交
147 148 149 150
	for (i = 0; i < nr_pages; i++, virt += PAGE_SIZE) {
		pg = vmalloc_to_page(virt);
		if (NULL == pg)
			goto err;
151
		BUG_ON(PageHighMem(pg));
152
		sg_set_page(&sglist[i], pg, PAGE_SIZE, 0);
L
Linus Torvalds 已提交
153 154 155 156 157 158 159 160 161 162 163
	}
	return sglist;

 err:
	kfree(sglist);
	return NULL;
}

/********************************************************************************/
/* common page table functions */

164
void *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
L
Linus Torvalds 已提交
165 166
{
	int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
167
	void *mem = vmalloc_32(length);
L
Linus Torvalds 已提交
168 169
	int slen = 0;

170 171
	if (NULL == mem)
		goto err_null;
L
Linus Torvalds 已提交
172

173 174
	if (!(pt->slist = vmalloc_to_sg(mem, pages)))
		goto err_free_mem;
L
Linus Torvalds 已提交
175

176 177
	if (saa7146_pgtable_alloc(pci, pt))
		goto err_free_slist;
L
Linus Torvalds 已提交
178

179
	pt->nents = pages;
180
	slen = dma_map_sg(&pci->dev, pt->slist, pt->nents, DMA_FROM_DEVICE);
181 182 183 184 185
	if (0 == slen)
		goto err_free_pgtable;

	if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
		goto err_unmap_sg;
L
Linus Torvalds 已提交
186 187

	return mem;
188 189

err_unmap_sg:
190
	dma_unmap_sg(&pci->dev, pt->slist, pt->nents, DMA_FROM_DEVICE);
191 192 193 194 195 196 197 198 199 200 201
err_free_pgtable:
	saa7146_pgtable_free(pci, pt);
err_free_slist:
	kfree(pt->slist);
	pt->slist = NULL;
err_free_mem:
	vfree(mem);
err_null:
	return NULL;
}

202
void saa7146_vfree_destroy_pgtable(struct pci_dev *pci, void *mem, struct saa7146_pgtable *pt)
203
{
204
	dma_unmap_sg(&pci->dev, pt->slist, pt->nents, DMA_FROM_DEVICE);
205 206 207 208
	saa7146_pgtable_free(pci, pt);
	kfree(pt->slist);
	pt->slist = NULL;
	vfree(mem);
L
Linus Torvalds 已提交
209 210 211 212 213 214
}

void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
{
	if (NULL == pt->cpu)
		return;
215
	dma_free_coherent(&pci->dev, pt->size, pt->cpu, pt->dma);
L
Linus Torvalds 已提交
216 217 218 219 220
	pt->cpu = NULL;
}

int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
{
221
	__le32       *cpu;
222
	dma_addr_t   dma_addr = 0;
L
Linus Torvalds 已提交
223

224
	cpu = dma_alloc_coherent(&pci->dev, PAGE_SIZE, &dma_addr, GFP_KERNEL);
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237
	if (NULL == cpu) {
		return -ENOMEM;
	}
	pt->size = PAGE_SIZE;
	pt->cpu  = cpu;
	pt->dma  = dma_addr;

	return 0;
}

int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
	struct scatterlist *list, int sglen  )
{
238
	__le32 *ptr, fill;
L
Linus Torvalds 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251
	int nr_pages = 0;
	int i,p;

	BUG_ON(0 == sglen);
	BUG_ON(list->offset > PAGE_SIZE);

	/* if we have a user buffer, the first page may not be
	   aligned to a page boundary. */
	pt->offset = list->offset;

	ptr = pt->cpu;
	for (i = 0; i < sglen; i++, list++) {
/*
252 253 254
		pr_debug("i:%d, adr:0x%08x, len:%d, offset:%d\n",
			 i, sg_dma_address(list), sg_dma_len(list),
			 list->offset);
L
Linus Torvalds 已提交
255
*/
256
		for (p = 0; p * 4096 < sg_dma_len(list); p++, ptr++) {
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270
			*ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
			nr_pages++;
		}
	}


	/* safety; fill the page table up with the last valid page */
	fill = *(ptr-1);
	for(i=nr_pages;i<1024;i++) {
		*ptr++ = fill;
	}

/*
	ptr = pt->cpu;
271
	pr_debug("offset: %d\n", pt->offset);
L
Linus Torvalds 已提交
272
	for(i=0;i<5;i++) {
273
		pr_debug("ptr1 %d: 0x%08x\n", i, ptr[i]);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280
	}
*/
	return 0;
}

/********************************************************************************/
/* interrupt handler */
281
static irqreturn_t interrupt_hw(int irq, void *dev_id)
L
Linus Torvalds 已提交
282 283
{
	struct saa7146_dev *dev = dev_id;
284 285
	u32 isr;
	u32 ack_isr;
L
Linus Torvalds 已提交
286 287

	/* read out the interrupt status register */
288
	ack_isr = isr = saa7146_read(dev, ISR);
L
Linus Torvalds 已提交
289 290 291 292 293 294 295

	/* is this our interrupt? */
	if ( 0 == isr ) {
		/* nope, some other device */
		return IRQ_NONE;
	}

A
Al Viro 已提交
296 297 298
	if (dev->ext) {
		if (dev->ext->irq_mask & isr) {
			if (dev->ext->irq_func)
L
Linus Torvalds 已提交
299 300 301 302 303
				dev->ext->irq_func(dev, &isr);
			isr &= ~dev->ext->irq_mask;
		}
	}
	if (0 != (isr & (MASK_27))) {
304
		DEB_INT("irq: RPS0 (0x%08x)\n", isr);
A
Al Viro 已提交
305
		if (dev->vv_data && dev->vv_callback)
L
Linus Torvalds 已提交
306 307 308 309
			dev->vv_callback(dev,isr);
		isr &= ~MASK_27;
	}
	if (0 != (isr & (MASK_28))) {
A
Al Viro 已提交
310
		if (dev->vv_data && dev->vv_callback)
L
Linus Torvalds 已提交
311 312 313 314
			dev->vv_callback(dev,isr);
		isr &= ~MASK_28;
	}
	if (0 != (isr & (MASK_16|MASK_17))) {
315 316 317 318 319
		SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
		/* only wake up if we expect something */
		if (0 != dev->i2c_op) {
			dev->i2c_op = 0;
			wake_up(&dev->i2c_wq);
L
Linus Torvalds 已提交
320
		} else {
321 322
			u32 psr = saa7146_read(dev, PSR);
			u32 ssr = saa7146_read(dev, SSR);
323 324
			pr_warn("%s: unexpected i2c irq: isr %08x psr %08x ssr %08x\n",
				dev->name, isr, psr, ssr);
L
Linus Torvalds 已提交
325 326 327 328
		}
		isr &= ~(MASK_16|MASK_17);
	}
	if( 0 != isr ) {
329 330 331
		ERR("warning: interrupt enabled, but not handled properly.(0x%08x)\n",
		    isr);
		ERR("disabling interrupt source(s)!\n");
L
Linus Torvalds 已提交
332 333
		SAA7146_IER_DISABLE(dev,isr);
	}
334
	saa7146_write(dev, ISR, ack_isr);
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347
	return IRQ_HANDLED;
}

/*********************************************************************************/
/* configuration-functions                                                       */

static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
{
	struct saa7146_pci_extension_data *pci_ext = (struct saa7146_pci_extension_data *)ent->driver_data;
	struct saa7146_extension *ext = pci_ext->ext;
	struct saa7146_dev *dev;
	int err = -ENOMEM;

P
 
Panagiotis Issaris 已提交
348 349
	/* clear out mem for sure */
	dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
L
Linus Torvalds 已提交
350
	if (!dev) {
351
		ERR("out of memory\n");
L
Linus Torvalds 已提交
352 353 354
		goto out;
	}

355 356 357
	/* create a nice device name */
	sprintf(dev->name, "saa7146 (%d)", saa7146_num);

358
	DEB_EE("pci:%p\n", pci);
L
Linus Torvalds 已提交
359 360 361

	err = pci_enable_device(pci);
	if (err < 0) {
362
		ERR("pci_enable_device() failed\n");
363
		goto err_free;
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371
	}

	/* enable bus-mastering */
	pci_set_master(pci);

	dev->pci = pci;

	/* get chip-revision; this is needed to enable bug-fixes */
B
Bjørn Mork 已提交
372
	dev->revision = pci->revision;
L
Linus Torvalds 已提交
373

J
Joe Perches 已提交
374
	/* remap the memory from virtual to physical address */
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382

	err = pci_request_region(pci, 0, "saa7146");
	if (err < 0)
		goto err_disable;

	dev->mem = ioremap(pci_resource_start(pci, 0),
			   pci_resource_len(pci, 0));
	if (!dev->mem) {
383
		ERR("ioremap() failed\n");
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		err = -ENODEV;
		goto err_release;
	}

	/* we don't do a master reset here anymore, it screws up
	   some boards that don't have an i2c-eeprom for configuration
	   values */
/*
	saa7146_write(dev, MC1, MASK_31);
*/

	/* disable all irqs */
	saa7146_write(dev, IER, 0);

	/* shut down all dma transfers and rps tasks */
	saa7146_write(dev, MC1, 0x30ff0000);

	/* clear out any rps-signals pending */
	saa7146_write(dev, MC2, 0xf8000000);

	/* request an interrupt for the saa7146 */
405
	err = request_irq(pci->irq, interrupt_hw, IRQF_SHARED,
L
Linus Torvalds 已提交
406 407
			  dev->name, dev);
	if (err < 0) {
408
		ERR("request_irq() failed\n");
L
Linus Torvalds 已提交
409 410 411 412 413 414
		goto err_unmap;
	}

	err = -ENOMEM;

	/* get memory for various stuff */
415 416 417
	dev->d_rps0.cpu_addr = dma_alloc_coherent(&pci->dev, SAA7146_RPS_MEM,
						  &dev->d_rps0.dma_handle,
						  GFP_KERNEL);
L
Linus Torvalds 已提交
418 419 420
	if (!dev->d_rps0.cpu_addr)
		goto err_free_irq;

421 422 423
	dev->d_rps1.cpu_addr = dma_alloc_coherent(&pci->dev, SAA7146_RPS_MEM,
						  &dev->d_rps1.dma_handle,
						  GFP_KERNEL);
L
Linus Torvalds 已提交
424 425 426
	if (!dev->d_rps1.cpu_addr)
		goto err_free_rps0;

427 428
	dev->d_i2c.cpu_addr = dma_alloc_coherent(&pci->dev, SAA7146_RPS_MEM,
						 &dev->d_i2c.dma_handle, GFP_KERNEL);
L
Linus Torvalds 已提交
429 430 431 432 433
	if (!dev->d_i2c.cpu_addr)
		goto err_free_rps1;

	/* the rest + print status message */

434 435 436
	pr_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);
L
Linus Torvalds 已提交
437 438
	dev->ext = ext;

439
	mutex_init(&dev->v4l2_lock);
L
Linus Torvalds 已提交
440 441 442
	spin_lock_init(&dev->int_slock);
	spin_lock_init(&dev->slock);

443
	mutex_init(&dev->i2c_lock);
L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454 455

	dev->module = THIS_MODULE;
	init_waitqueue_head(&dev->i2c_wq);

	/* set some sane pci arbitrition values */
	saa7146_write(dev, PCI_BT_V1, 0x1c00101f);

	/* TODO: use the status code of the callback */

	err = -ENODEV;

	if (ext->probe && ext->probe(dev)) {
456
		DEB_D("ext->probe() failed for %p. skipping device.\n", dev);
L
Linus Torvalds 已提交
457 458 459 460
		goto err_free_i2c;
	}

	if (ext->attach(dev, pci_ext)) {
461
		DEB_D("ext->attach() failed for %p. skipping device.\n", dev);
462
		goto err_free_i2c;
L
Linus Torvalds 已提交
463
	}
464 465 466 467
	/* V4L extensions will set the pci drvdata to the v4l2_device in the
	   attach() above. So for those cards that do not use V4L we have to
	   set it explicitly. */
	pci_set_drvdata(pci, &dev->v4l2_dev);
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475

	saa7146_num++;

	err = 0;
out:
	return err;

err_free_i2c:
476 477
	dma_free_coherent(&pci->dev, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
			  dev->d_i2c.dma_handle);
L
Linus Torvalds 已提交
478
err_free_rps1:
479 480
	dma_free_coherent(&pci->dev, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
			  dev->d_rps1.dma_handle);
L
Linus Torvalds 已提交
481
err_free_rps0:
482 483
	dma_free_coherent(&pci->dev, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
			  dev->d_rps0.dma_handle);
L
Linus Torvalds 已提交
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
err_free_irq:
	free_irq(pci->irq, (void *)dev);
err_unmap:
	iounmap(dev->mem);
err_release:
	pci_release_region(pci, 0);
err_disable:
	pci_disable_device(pci);
err_free:
	kfree(dev);
	goto out;
}

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);
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510
	struct {
		void *addr;
		dma_addr_t dma;
	} dev_map[] = {
		{ dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
		{ dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
		{ dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
		{ NULL, 0 }
	}, *p;

511
	DEB_EE("dev:%p\n", dev);
L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521 522 523

	dev->ext->detach(dev);

	/* shut down all video dma transfers */
	saa7146_write(dev, MC1, 0x00ff0000);

	/* disable all irqs, release irq-routine */
	saa7146_write(dev, IER, 0);

	free_irq(pdev->irq, dev);

	for (p = dev_map; p->addr; p++)
524 525
		dma_free_coherent(&pdev->dev, SAA7146_RPS_MEM, p->addr,
				  p->dma);
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536 537 538 539

	iounmap(dev->mem);
	pci_release_region(pdev, 0);
	pci_disable_device(pdev);
	kfree(dev);

	saa7146_num--;
}

/*********************************************************************************/
/* extension handling functions                                                  */

int saa7146_register_extension(struct saa7146_extension* ext)
{
540
	DEB_EE("ext:%p\n", ext);
L
Linus Torvalds 已提交
541 542 543 544 545 546

	ext->driver.name = ext->name;
	ext->driver.id_table = ext->pci_tbl;
	ext->driver.probe = saa7146_init_one;
	ext->driver.remove = saa7146_remove_one;

547
	pr_info("register extension '%s'\n", ext->name);
548
	return pci_register_driver(&ext->driver);
L
Linus Torvalds 已提交
549 550 551 552
}

int saa7146_unregister_extension(struct saa7146_extension* ext)
{
553 554
	DEB_EE("ext:%p\n", ext);
	pr_info("unregister extension '%s'\n", ext->name);
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566
	pci_unregister_driver(&ext->driver);
	return 0;
}

EXPORT_SYMBOL_GPL(saa7146_register_extension);
EXPORT_SYMBOL_GPL(saa7146_unregister_extension);

/* misc functions used by extension modules */
EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
567
EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable);
L
Linus Torvalds 已提交
568 569 570 571 572 573 574 575 576 577 578
EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);

EXPORT_SYMBOL_GPL(saa7146_setgpio);

EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);

EXPORT_SYMBOL_GPL(saa7146_debug);

MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
MODULE_LICENSE("GPL");