1
2
3
4
5
6
7
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11#include <media/drv-intf/saa7146.h>
12#include <linux/module.h>
13
14static int saa7146_num;
15
16unsigned int saa7146_debug;
17
18module_param(saa7146_debug, uint, 0644);
19MODULE_PARM_DESC(saa7146_debug, "debug level (default: 0)");
20
21#if 0
22static void dump_registers(struct saa7146_dev* dev)
23{
24 int i = 0;
25
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));
29}
30#endif
31
32
33
34
35
36void saa7146_setgpio(struct saa7146_dev *dev, int port, u32 data)
37{
38 u32 value = 0;
39
40 BUG_ON(port > 3);
41
42 value = saa7146_read(dev, GPIO_CTRL);
43 value &= ~(0xff << (8*port));
44 value |= (data << (8*port));
45 saa7146_write(dev, GPIO_CTRL, value);
46}
47
48
49static inline int saa7146_wait_for_debi_done_sleep(struct saa7146_dev *dev,
50 unsigned long us1, unsigned long us2)
51{
52 unsigned long timeout;
53 int err;
54
55
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;
65 }
66 msleep(1);
67 }
68
69
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;
80 }
81 msleep(1);
82 }
83
84 return 0;
85}
86
87static inline int saa7146_wait_for_debi_done_busyloop(struct saa7146_dev *dev,
88 unsigned long us1, unsigned long us2)
89{
90 unsigned long loops;
91
92
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;
101 }
102 udelay(1);
103 }
104
105
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;
115 }
116 udelay(5);
117 }
118
119 return 0;
120}
121
122int saa7146_wait_for_debi_done(struct saa7146_dev *dev, int nobusyloop)
123{
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);
128}
129
130
131
132
133
134
135
136
137static struct scatterlist* vmalloc_to_sg(unsigned char *virt, int nr_pages)
138{
139 struct scatterlist *sglist;
140 struct page *pg;
141 int i;
142
143 sglist = kcalloc(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);
153 }
154 return sglist;
155
156 err:
157 kfree(sglist);
158 return NULL;
159}
160
161
162
163
164void *saa7146_vmalloc_build_pgtable(struct pci_dev *pci, long length, struct saa7146_pgtable *pt)
165{
166 int pages = (length+PAGE_SIZE-1)/PAGE_SIZE;
167 void *mem = vmalloc_32(length);
168 int slen = 0;
169
170 if (NULL == mem)
171 goto err_null;
172
173 if (!(pt->slist = vmalloc_to_sg(mem, pages)))
174 goto err_free_mem;
175
176 if (saa7146_pgtable_alloc(pci, pt))
177 goto err_free_slist;
178
179 pt->nents = pages;
180 slen = pci_map_sg(pci,pt->slist,pt->nents,PCI_DMA_FROMDEVICE);
181 if (0 == slen)
182 goto err_free_pgtable;
183
184 if (0 != saa7146_pgtable_build_single(pci, pt, pt->slist, slen))
185 goto err_unmap_sg;
186
187 return mem;
188
189err_unmap_sg:
190 pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
191err_free_pgtable:
192 saa7146_pgtable_free(pci, pt);
193err_free_slist:
194 kfree(pt->slist);
195 pt->slist = NULL;
196err_free_mem:
197 vfree(mem);
198err_null:
199 return NULL;
200}
201
202void saa7146_vfree_destroy_pgtable(struct pci_dev *pci, void *mem, struct saa7146_pgtable *pt)
203{
204 pci_unmap_sg(pci, pt->slist, pt->nents, PCI_DMA_FROMDEVICE);
205 saa7146_pgtable_free(pci, pt);
206 kfree(pt->slist);
207 pt->slist = NULL;
208 vfree(mem);
209}
210
211void saa7146_pgtable_free(struct pci_dev *pci, struct saa7146_pgtable *pt)
212{
213 if (NULL == pt->cpu)
214 return;
215 pci_free_consistent(pci, pt->size, pt->cpu, pt->dma);
216 pt->cpu = NULL;
217}
218
219int saa7146_pgtable_alloc(struct pci_dev *pci, struct saa7146_pgtable *pt)
220{
221 __le32 *cpu;
222 dma_addr_t dma_addr = 0;
223
224 cpu = pci_alloc_consistent(pci, PAGE_SIZE, &dma_addr);
225 if (NULL == cpu) {
226 return -ENOMEM;
227 }
228 pt->size = PAGE_SIZE;
229 pt->cpu = cpu;
230 pt->dma = dma_addr;
231
232 return 0;
233}
234
235int saa7146_pgtable_build_single(struct pci_dev *pci, struct saa7146_pgtable *pt,
236 struct scatterlist *list, int sglen )
237{
238 __le32 *ptr, fill;
239 int nr_pages = 0;
240 int i,p;
241
242 BUG_ON(0 == sglen);
243 BUG_ON(list->offset > PAGE_SIZE);
244
245
246
247 pt->offset = list->offset;
248
249 ptr = pt->cpu;
250 for (i = 0; i < sglen; i++, list++) {
251
252
253
254
255
256 for (p = 0; p * 4096 < list->length; p++, ptr++) {
257 *ptr = cpu_to_le32(sg_dma_address(list) + p * 4096);
258 nr_pages++;
259 }
260 }
261
262
263
264 fill = *(ptr-1);
265 for(i=nr_pages;i<1024;i++) {
266 *ptr++ = fill;
267 }
268
269
270
271
272
273
274
275
276 return 0;
277}
278
279
280
281static irqreturn_t interrupt_hw(int irq, void *dev_id)
282{
283 struct saa7146_dev *dev = dev_id;
284 u32 isr;
285 u32 ack_isr;
286
287
288 ack_isr = isr = saa7146_read(dev, ISR);
289
290
291 if ( 0 == isr ) {
292
293 return IRQ_NONE;
294 }
295
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;
301 }
302 }
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;
308 }
309 if (0 != (isr & (MASK_28))) {
310 if (dev->vv_data && dev->vv_callback)
311 dev->vv_callback(dev,isr);
312 isr &= ~MASK_28;
313 }
314 if (0 != (isr & (MASK_16|MASK_17))) {
315 SAA7146_IER_DISABLE(dev, MASK_16|MASK_17);
316
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);
325 }
326 isr &= ~(MASK_16|MASK_17);
327 }
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);
333 }
334 saa7146_write(dev, ISR, ack_isr);
335 return IRQ_HANDLED;
336}
337
338
339
340
341static int saa7146_init_one(struct pci_dev *pci, const struct pci_device_id *ent)
342{
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;
347
348
349 dev = kzalloc(sizeof(struct saa7146_dev), GFP_KERNEL);
350 if (!dev) {
351 ERR("out of memory\n");
352 goto out;
353 }
354
355
356 sprintf(dev->name, "saa7146 (%d)", saa7146_num);
357
358 DEB_EE("pci:%p\n", pci);
359
360 err = pci_enable_device(pci);
361 if (err < 0) {
362 ERR("pci_enable_device() failed\n");
363 goto err_free;
364 }
365
366
367 pci_set_master(pci);
368
369 dev->pci = pci;
370
371
372 dev->revision = pci->revision;
373
374
375
376 err = pci_request_region(pci, 0, "saa7146");
377 if (err < 0)
378 goto err_disable;
379
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;
386 }
387
388
389
390
391
392
393
394
395
396 saa7146_write(dev, IER, 0);
397
398
399 saa7146_write(dev, MC1, 0x30ff0000);
400
401
402 saa7146_write(dev, MC2, 0xf8000000);
403
404
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;
410 }
411
412 err = -ENOMEM;
413
414
415 dev->d_rps0.cpu_addr = pci_zalloc_consistent(pci, SAA7146_RPS_MEM,
416 &dev->d_rps0.dma_handle);
417 if (!dev->d_rps0.cpu_addr)
418 goto err_free_irq;
419
420 dev->d_rps1.cpu_addr = pci_zalloc_consistent(pci, SAA7146_RPS_MEM,
421 &dev->d_rps1.dma_handle);
422 if (!dev->d_rps1.cpu_addr)
423 goto err_free_rps0;
424
425 dev->d_i2c.cpu_addr = pci_zalloc_consistent(pci, SAA7146_RPS_MEM,
426 &dev->d_i2c.dma_handle);
427 if (!dev->d_i2c.cpu_addr)
428 goto err_free_rps1;
429
430
431
432 pr_info("found saa7146 @ mem %p (revision %d, irq %d) (0x%04x,0x%04x)\n",
433 dev->mem, dev->revision, pci->irq,
434 pci->subsystem_vendor, pci->subsystem_device);
435 dev->ext = ext;
436
437 mutex_init(&dev->v4l2_lock);
438 spin_lock_init(&dev->int_slock);
439 spin_lock_init(&dev->slock);
440
441 mutex_init(&dev->i2c_lock);
442
443 dev->module = THIS_MODULE;
444 init_waitqueue_head(&dev->i2c_wq);
445
446
447 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
448
449
450
451 err = -ENODEV;
452
453 if (ext->probe && ext->probe(dev)) {
454 DEB_D("ext->probe() failed for %p. skipping device.\n", dev);
455 goto err_free_i2c;
456 }
457
458 if (ext->attach(dev, pci_ext)) {
459 DEB_D("ext->attach() failed for %p. skipping device.\n", dev);
460 goto err_free_i2c;
461 }
462
463
464
465 pci_set_drvdata(pci, &dev->v4l2_dev);
466
467 saa7146_num++;
468
469 err = 0;
470out:
471 return err;
472
473err_free_i2c:
474 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_i2c.cpu_addr,
475 dev->d_i2c.dma_handle);
476err_free_rps1:
477 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps1.cpu_addr,
478 dev->d_rps1.dma_handle);
479err_free_rps0:
480 pci_free_consistent(pci, SAA7146_RPS_MEM, dev->d_rps0.cpu_addr,
481 dev->d_rps0.dma_handle);
482err_free_irq:
483 free_irq(pci->irq, (void *)dev);
484err_unmap:
485 iounmap(dev->mem);
486err_release:
487 pci_release_region(pci, 0);
488err_disable:
489 pci_disable_device(pci);
490err_free:
491 kfree(dev);
492 goto out;
493}
494
495static void saa7146_remove_one(struct pci_dev *pdev)
496{
497 struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
498 struct saa7146_dev *dev = to_saa7146_dev(v4l2_dev);
499 struct {
500 void *addr;
501 dma_addr_t dma;
502 } dev_map[] = {
503 { dev->d_i2c.cpu_addr, dev->d_i2c.dma_handle },
504 { dev->d_rps1.cpu_addr, dev->d_rps1.dma_handle },
505 { dev->d_rps0.cpu_addr, dev->d_rps0.dma_handle },
506 { NULL, 0 }
507 }, *p;
508
509 DEB_EE("dev:%p\n", dev);
510
511 dev->ext->detach(dev);
512
513
514 saa7146_write(dev, MC1, 0x00ff0000);
515
516
517 saa7146_write(dev, IER, 0);
518
519 free_irq(pdev->irq, dev);
520
521 for (p = dev_map; p->addr; p++)
522 pci_free_consistent(pdev, SAA7146_RPS_MEM, p->addr, p->dma);
523
524 iounmap(dev->mem);
525 pci_release_region(pdev, 0);
526 pci_disable_device(pdev);
527 kfree(dev);
528
529 saa7146_num--;
530}
531
532
533
534
535int saa7146_register_extension(struct saa7146_extension* ext)
536{
537 DEB_EE("ext:%p\n", ext);
538
539 ext->driver.name = ext->name;
540 ext->driver.id_table = ext->pci_tbl;
541 ext->driver.probe = saa7146_init_one;
542 ext->driver.remove = saa7146_remove_one;
543
544 pr_info("register extension '%s'\n", ext->name);
545 return pci_register_driver(&ext->driver);
546}
547
548int saa7146_unregister_extension(struct saa7146_extension* ext)
549{
550 DEB_EE("ext:%p\n", ext);
551 pr_info("unregister extension '%s'\n", ext->name);
552 pci_unregister_driver(&ext->driver);
553 return 0;
554}
555
556EXPORT_SYMBOL_GPL(saa7146_register_extension);
557EXPORT_SYMBOL_GPL(saa7146_unregister_extension);
558
559
560EXPORT_SYMBOL_GPL(saa7146_pgtable_alloc);
561EXPORT_SYMBOL_GPL(saa7146_pgtable_free);
562EXPORT_SYMBOL_GPL(saa7146_pgtable_build_single);
563EXPORT_SYMBOL_GPL(saa7146_vmalloc_build_pgtable);
564EXPORT_SYMBOL_GPL(saa7146_vfree_destroy_pgtable);
565EXPORT_SYMBOL_GPL(saa7146_wait_for_debi_done);
566
567EXPORT_SYMBOL_GPL(saa7146_setgpio);
568
569EXPORT_SYMBOL_GPL(saa7146_i2c_adapter_prepare);
570
571EXPORT_SYMBOL_GPL(saa7146_debug);
572
573MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
574MODULE_DESCRIPTION("driver for generic saa7146-based hardware");
575MODULE_LICENSE("GPL");
576