xref: /linux/drivers/media/pci/saa7134/saa7134-core.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  * device driver for philips saa7134 based TV cards
5  * driver core
6  *
7  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8  */
9 
10 #include "saa7134.h"
11 #include "saa7134-reg.h"
12 
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/kmod.h>
19 #include <linux/sound.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/mutex.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/pm.h>
25 
26 MODULE_DESCRIPTION("v4l2 driver module for saa7130/34 based TV cards");
27 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
28 MODULE_LICENSE("GPL");
29 MODULE_VERSION(SAA7134_VERSION);
30 
31 
32 /* ------------------------------------------------------------------ */
33 
34 static unsigned int irq_debug;
35 module_param(irq_debug, int, 0644);
36 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
37 
38 static unsigned int core_debug;
39 module_param(core_debug, int, 0644);
40 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
41 
42 static unsigned int gpio_tracking;
43 module_param(gpio_tracking, int, 0644);
44 MODULE_PARM_DESC(gpio_tracking,"enable debug messages [gpio]");
45 
46 static unsigned int alsa = 1;
47 module_param(alsa, int, 0644);
48 MODULE_PARM_DESC(alsa,"enable/disable ALSA DMA sound [dmasound]");
49 
50 static unsigned int latency = UNSET;
51 module_param(latency, int, 0444);
52 MODULE_PARM_DESC(latency,"pci latency timer");
53 
54 bool saa7134_userptr;
55 module_param(saa7134_userptr, bool, 0644);
56 MODULE_PARM_DESC(saa7134_userptr, "enable page-aligned userptr support");
57 
58 static unsigned int video_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
59 static unsigned int vbi_nr[]   = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
60 static unsigned int radio_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
61 static unsigned int tuner[]    = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
62 static unsigned int card[]     = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
63 
64 
65 module_param_array(video_nr, int, NULL, 0444);
66 module_param_array(vbi_nr,   int, NULL, 0444);
67 module_param_array(radio_nr, int, NULL, 0444);
68 module_param_array(tuner,    int, NULL, 0444);
69 module_param_array(card,     int, NULL, 0444);
70 
71 MODULE_PARM_DESC(video_nr, "video device number");
72 MODULE_PARM_DESC(vbi_nr,   "vbi device number");
73 MODULE_PARM_DESC(radio_nr, "radio device number");
74 MODULE_PARM_DESC(tuner,    "tuner type");
75 MODULE_PARM_DESC(card,     "card type");
76 
77 DEFINE_MUTEX(saa7134_devlist_lock);
78 EXPORT_SYMBOL(saa7134_devlist_lock);
79 LIST_HEAD(saa7134_devlist);
80 EXPORT_SYMBOL(saa7134_devlist);
81 static LIST_HEAD(mops_list);
82 static unsigned int saa7134_devcount;
83 
84 int (*saa7134_dmasound_init)(struct saa7134_dev *dev);
85 int (*saa7134_dmasound_exit)(struct saa7134_dev *dev);
86 
87 #define core_dbg(fmt, arg...) do { \
88 	if (core_debug) \
89 		printk(KERN_DEBUG pr_fmt("core: " fmt), ## arg); \
90 	} while (0)
91 
92 #define irq_dbg(level, fmt, arg...)  do {\
93 	if (irq_debug > level) \
94 		printk(KERN_DEBUG pr_fmt("irq: " fmt), ## arg); \
95 	} while (0)
96 
saa7134_track_gpio(struct saa7134_dev * dev,const char * msg)97 void saa7134_track_gpio(struct saa7134_dev *dev, const char *msg)
98 {
99 	unsigned long mode,status;
100 
101 	if (!gpio_tracking)
102 		return;
103 	/* rising SAA7134_GPIO_GPRESCAN reads the status */
104 	saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,0);
105 	saa_andorb(SAA7134_GPIO_GPMODE3,SAA7134_GPIO_GPRESCAN,SAA7134_GPIO_GPRESCAN);
106 	mode   = saa_readl(SAA7134_GPIO_GPMODE0   >> 2) & 0xfffffff;
107 	status = saa_readl(SAA7134_GPIO_GPSTATUS0 >> 2) & 0xfffffff;
108 	core_dbg("%s: gpio: mode=0x%07lx in=0x%07lx out=0x%07lx [%s]\n",
109 	       dev->name, mode, (~mode) & status, mode & status, msg);
110 }
111 
saa7134_set_gpio(struct saa7134_dev * dev,int bit_no,int value)112 void saa7134_set_gpio(struct saa7134_dev *dev, int bit_no, int value)
113 {
114 	u32 index, bitval;
115 
116 	index = 1 << bit_no;
117 	switch (value) {
118 	case 0: /* static value */
119 	case 1:
120 		core_dbg("setting GPIO%d to static %d\n", bit_no, value);
121 		/* turn sync mode off if necessary */
122 		if (index & 0x00c00000)
123 			saa_andorb(SAA7134_VIDEO_PORT_CTRL6, 0x0f, 0x00);
124 		if (value)
125 			bitval = index;
126 		else
127 			bitval = 0;
128 		saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, index);
129 		saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, index, bitval);
130 		break;
131 	case 3:	/* tristate */
132 		core_dbg("setting GPIO%d to tristate\n", bit_no);
133 		saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, index, 0);
134 		break;
135 	}
136 }
137 
138 /* ------------------------------------------------------------------ */
139 
140 
141 /* ----------------------------------------------------------- */
142 /* delayed request_module                                      */
143 
144 #if defined(CONFIG_MODULES) && defined(MODULE)
145 
request_module_async(struct work_struct * work)146 static void request_module_async(struct work_struct *work){
147 	struct saa7134_dev* dev = container_of(work, struct saa7134_dev, request_module_wk);
148 	if (card_is_empress(dev))
149 		request_module("saa7134-empress");
150 	if (card_is_dvb(dev))
151 		request_module("saa7134-dvb");
152 	if (card_is_go7007(dev))
153 		request_module("saa7134-go7007");
154 	if (alsa) {
155 		if (dev->pci->device != PCI_DEVICE_ID_PHILIPS_SAA7130)
156 			request_module("saa7134-alsa");
157 	}
158 }
159 
request_submodules(struct saa7134_dev * dev)160 static void request_submodules(struct saa7134_dev *dev)
161 {
162 	INIT_WORK(&dev->request_module_wk, request_module_async);
163 	schedule_work(&dev->request_module_wk);
164 }
165 
flush_request_submodules(struct saa7134_dev * dev)166 static void flush_request_submodules(struct saa7134_dev *dev)
167 {
168 	flush_work(&dev->request_module_wk);
169 }
170 
171 #else
172 #define request_submodules(dev)
173 #define flush_request_submodules(dev)
174 #endif /* CONFIG_MODULES */
175 
176 /* ------------------------------------------------------------------ */
177 
178 /* nr of (saa7134-)pages for the given buffer size */
saa7134_buffer_pages(int size)179 static int saa7134_buffer_pages(int size)
180 {
181 	size  = PAGE_ALIGN(size);
182 	size += PAGE_SIZE; /* for non-page-aligned buffers */
183 	size /= 4096;
184 	return size;
185 }
186 
187 /* calc max # of buffers from size (must not exceed the 4MB virtual
188  * address space per DMA channel) */
saa7134_buffer_count(unsigned int size,unsigned int count)189 int saa7134_buffer_count(unsigned int size, unsigned int count)
190 {
191 	unsigned int maxcount;
192 
193 	maxcount = 1024 / saa7134_buffer_pages(size);
194 	if (count > maxcount)
195 		count = maxcount;
196 	return count;
197 }
198 
saa7134_buffer_startpage(struct saa7134_buf * buf)199 int saa7134_buffer_startpage(struct saa7134_buf *buf)
200 {
201 	return saa7134_buffer_pages(vb2_plane_size(&buf->vb2.vb2_buf, 0))
202 			* buf->vb2.vb2_buf.index;
203 }
204 
saa7134_buffer_base(struct saa7134_buf * buf)205 unsigned long saa7134_buffer_base(struct saa7134_buf *buf)
206 {
207 	unsigned long base;
208 	struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2.vb2_buf, 0);
209 
210 	base  = saa7134_buffer_startpage(buf) * 4096;
211 	base += dma->sgl[0].offset;
212 	return base;
213 }
214 
215 /* ------------------------------------------------------------------ */
216 
saa7134_pgtable_alloc(struct pci_dev * pci,struct saa7134_pgtable * pt)217 int saa7134_pgtable_alloc(struct pci_dev *pci, struct saa7134_pgtable *pt)
218 {
219 	__le32       *cpu;
220 	dma_addr_t   dma_addr = 0;
221 
222 	cpu = dma_alloc_coherent(&pci->dev, SAA7134_PGTABLE_SIZE, &dma_addr,
223 				 GFP_KERNEL);
224 	if (NULL == cpu)
225 		return -ENOMEM;
226 	pt->size = SAA7134_PGTABLE_SIZE;
227 	pt->cpu  = cpu;
228 	pt->dma  = dma_addr;
229 	return 0;
230 }
231 
saa7134_pgtable_build(struct pci_dev * pci,struct saa7134_pgtable * pt,struct scatterlist * list,unsigned int length,unsigned int startpage)232 int saa7134_pgtable_build(struct pci_dev *pci, struct saa7134_pgtable *pt,
233 			  struct scatterlist *list, unsigned int length,
234 			  unsigned int startpage)
235 {
236 	__le32        *ptr;
237 	unsigned int  i, p;
238 
239 	BUG_ON(NULL == pt || NULL == pt->cpu);
240 
241 	ptr = pt->cpu + startpage;
242 	for (i = 0; i < length; i++, list = sg_next(list)) {
243 		for (p = 0; p * 4096 < sg_dma_len(list); p++, ptr++)
244 			*ptr = cpu_to_le32(sg_dma_address(list) +
245 						list->offset + p * 4096);
246 	}
247 	return 0;
248 }
249 
saa7134_pgtable_free(struct pci_dev * pci,struct saa7134_pgtable * pt)250 void saa7134_pgtable_free(struct pci_dev *pci, struct saa7134_pgtable *pt)
251 {
252 	if (NULL == pt->cpu)
253 		return;
254 	dma_free_coherent(&pci->dev, pt->size, pt->cpu, pt->dma);
255 	pt->cpu = NULL;
256 }
257 
258 /* ------------------------------------------------------------------ */
259 
saa7134_buffer_queue(struct saa7134_dev * dev,struct saa7134_dmaqueue * q,struct saa7134_buf * buf)260 int saa7134_buffer_queue(struct saa7134_dev *dev,
261 			 struct saa7134_dmaqueue *q,
262 			 struct saa7134_buf *buf)
263 {
264 	struct saa7134_buf *next = NULL;
265 	unsigned long flags;
266 
267 	spin_lock_irqsave(&dev->slock, flags);
268 	core_dbg("buffer_queue %p\n", buf);
269 	if (NULL == q->curr) {
270 		if (!q->need_two) {
271 			q->curr = buf;
272 			buf->activate(dev, buf, NULL);
273 		} else if (list_empty(&q->queue)) {
274 			list_add_tail(&buf->entry, &q->queue);
275 		} else {
276 			next = list_entry(q->queue.next, struct saa7134_buf,
277 					  entry);
278 			q->curr = buf;
279 			buf->activate(dev, buf, next);
280 		}
281 	} else {
282 		list_add_tail(&buf->entry, &q->queue);
283 	}
284 	spin_unlock_irqrestore(&dev->slock, flags);
285 	return 0;
286 }
287 
saa7134_buffer_finish(struct saa7134_dev * dev,struct saa7134_dmaqueue * q,unsigned int state)288 void saa7134_buffer_finish(struct saa7134_dev *dev,
289 			   struct saa7134_dmaqueue *q,
290 			   unsigned int state)
291 {
292 	core_dbg("buffer_finish %p\n", q->curr);
293 
294 	/* finish current buffer */
295 	q->curr->vb2.vb2_buf.timestamp = ktime_get_ns();
296 	q->curr->vb2.sequence = q->seq_nr++;
297 	vb2_buffer_done(&q->curr->vb2.vb2_buf, state);
298 	q->curr = NULL;
299 }
300 
saa7134_buffer_next(struct saa7134_dev * dev,struct saa7134_dmaqueue * q)301 void saa7134_buffer_next(struct saa7134_dev *dev,
302 			 struct saa7134_dmaqueue *q)
303 {
304 	struct saa7134_buf *buf,*next = NULL;
305 
306 	assert_spin_locked(&dev->slock);
307 	BUG_ON(NULL != q->curr);
308 
309 	if (!list_empty(&q->queue)) {
310 		/* activate next one from queue */
311 		buf = list_entry(q->queue.next, struct saa7134_buf, entry);
312 		core_dbg("buffer_next %p [prev=%p/next=%p]\n",
313 			buf, q->queue.prev, q->queue.next);
314 		list_del(&buf->entry);
315 		if (!list_empty(&q->queue))
316 			next = list_entry(q->queue.next, struct saa7134_buf, entry);
317 		q->curr = buf;
318 		buf->activate(dev, buf, next);
319 		core_dbg("buffer_next #2 prev=%p/next=%p\n",
320 			q->queue.prev, q->queue.next);
321 	} else {
322 		/* nothing to do -- just stop DMA */
323 		core_dbg("buffer_next %p\n", NULL);
324 		saa7134_set_dmabits(dev);
325 		del_timer(&q->timeout);
326 	}
327 }
328 
saa7134_buffer_timeout(struct timer_list * t)329 void saa7134_buffer_timeout(struct timer_list *t)
330 {
331 	struct saa7134_dmaqueue *q = from_timer(q, t, timeout);
332 	struct saa7134_dev *dev = q->dev;
333 	unsigned long flags;
334 
335 	spin_lock_irqsave(&dev->slock, flags);
336 
337 	/* try to reset the hardware (SWRST) */
338 	saa_writeb(SAA7134_REGION_ENABLE, 0x00);
339 	saa_writeb(SAA7134_REGION_ENABLE, 0x80);
340 	saa_writeb(SAA7134_REGION_ENABLE, 0x00);
341 
342 	/* flag current buffer as failed,
343 	   try to start over with the next one. */
344 	if (q->curr) {
345 		core_dbg("timeout on %p\n", q->curr);
346 		saa7134_buffer_finish(dev, q, VB2_BUF_STATE_ERROR);
347 	}
348 	saa7134_buffer_next(dev, q);
349 	spin_unlock_irqrestore(&dev->slock, flags);
350 }
351 
saa7134_stop_streaming(struct saa7134_dev * dev,struct saa7134_dmaqueue * q)352 void saa7134_stop_streaming(struct saa7134_dev *dev, struct saa7134_dmaqueue *q)
353 {
354 	unsigned long flags;
355 	struct list_head *pos, *n;
356 	struct saa7134_buf *tmp;
357 
358 	spin_lock_irqsave(&dev->slock, flags);
359 	list_for_each_safe(pos, n, &q->queue) {
360 		tmp = list_entry(pos, struct saa7134_buf, entry);
361 		vb2_buffer_done(&tmp->vb2.vb2_buf,
362 				VB2_BUF_STATE_ERROR);
363 		list_del(pos);
364 		tmp = NULL;
365 	}
366 	spin_unlock_irqrestore(&dev->slock, flags);
367 	saa7134_buffer_timeout(&q->timeout); /* also calls del_timer(&q->timeout) */
368 }
369 EXPORT_SYMBOL_GPL(saa7134_stop_streaming);
370 
371 /* ------------------------------------------------------------------ */
372 
saa7134_set_dmabits(struct saa7134_dev * dev)373 int saa7134_set_dmabits(struct saa7134_dev *dev)
374 {
375 	u32 split, task=0, ctrl=0, irq=0;
376 	enum v4l2_field cap = V4L2_FIELD_ANY;
377 	enum v4l2_field ov  = V4L2_FIELD_ANY;
378 
379 	assert_spin_locked(&dev->slock);
380 
381 	if (dev->insuspend)
382 		return 0;
383 
384 	/* video capture -- dma 0 + video task A */
385 	if (dev->video_q.curr) {
386 		task |= 0x01;
387 		ctrl |= SAA7134_MAIN_CTRL_TE0;
388 		irq  |= SAA7134_IRQ1_INTE_RA0_1 |
389 			SAA7134_IRQ1_INTE_RA0_0;
390 		cap = dev->field;
391 	}
392 
393 	/* video capture -- dma 1+2 (planar modes) */
394 	if (dev->video_q.curr && dev->fmt->planar) {
395 		ctrl |= SAA7134_MAIN_CTRL_TE4 |
396 			SAA7134_MAIN_CTRL_TE5;
397 	}
398 
399 	/* vbi capture -- dma 0 + vbi task A+B */
400 	if (dev->vbi_q.curr) {
401 		task |= 0x22;
402 		ctrl |= SAA7134_MAIN_CTRL_TE2 |
403 			SAA7134_MAIN_CTRL_TE3;
404 		irq  |= SAA7134_IRQ1_INTE_RA0_7 |
405 			SAA7134_IRQ1_INTE_RA0_6 |
406 			SAA7134_IRQ1_INTE_RA0_5 |
407 			SAA7134_IRQ1_INTE_RA0_4;
408 	}
409 
410 	/* audio capture -- dma 3 */
411 	if (dev->dmasound.dma_running) {
412 		ctrl |= SAA7134_MAIN_CTRL_TE6;
413 		irq  |= SAA7134_IRQ1_INTE_RA3_1 |
414 			SAA7134_IRQ1_INTE_RA3_0;
415 	}
416 
417 	/* TS capture -- dma 5 */
418 	if (dev->ts_q.curr) {
419 		ctrl |= SAA7134_MAIN_CTRL_TE5;
420 		irq  |= SAA7134_IRQ1_INTE_RA2_1 |
421 			SAA7134_IRQ1_INTE_RA2_0;
422 	}
423 
424 	/* set task conditions + field handling */
425 	if (V4L2_FIELD_HAS_BOTH(cap) || V4L2_FIELD_HAS_BOTH(ov) || cap == ov) {
426 		/* default config -- use full frames */
427 		saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
428 		saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
429 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x02);
430 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x02);
431 		split = 0;
432 	} else {
433 		/* split fields between tasks */
434 		if (V4L2_FIELD_TOP == cap) {
435 			/* odd A, even B, repeat */
436 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0d);
437 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0e);
438 		} else {
439 			/* odd B, even A, repeat */
440 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_A), 0x0e);
441 			saa_writeb(SAA7134_TASK_CONDITIONS(TASK_B), 0x0d);
442 		}
443 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_A),  0x01);
444 		saa_writeb(SAA7134_FIELD_HANDLING(TASK_B),  0x01);
445 		split = 1;
446 	}
447 
448 	/* irqs */
449 	saa_writeb(SAA7134_REGION_ENABLE, task);
450 	saa_writel(SAA7134_IRQ1,          irq);
451 	saa_andorl(SAA7134_MAIN_CTRL,
452 		   SAA7134_MAIN_CTRL_TE0 |
453 		   SAA7134_MAIN_CTRL_TE1 |
454 		   SAA7134_MAIN_CTRL_TE2 |
455 		   SAA7134_MAIN_CTRL_TE3 |
456 		   SAA7134_MAIN_CTRL_TE4 |
457 		   SAA7134_MAIN_CTRL_TE5 |
458 		   SAA7134_MAIN_CTRL_TE6,
459 		   ctrl);
460 	core_dbg("dmabits: task=0x%02x ctrl=0x%02x irq=0x%x split=%s\n",
461 		task, ctrl, irq, split ? "no" : "yes");
462 
463 	return 0;
464 }
465 
466 /* ------------------------------------------------------------------ */
467 /* IRQ handler + helpers                                              */
468 
469 static char *irqbits[] = {
470 	"DONE_RA0", "DONE_RA1", "DONE_RA2", "DONE_RA3",
471 	"AR", "PE", "PWR_ON", "RDCAP", "INTL", "FIDT", "MMC",
472 	"TRIG_ERR", "CONF_ERR", "LOAD_ERR",
473 	"GPIO16", "GPIO18", "GPIO22", "GPIO23"
474 };
475 #define IRQBITS ARRAY_SIZE(irqbits)
476 
print_irqstatus(struct saa7134_dev * dev,int loop,unsigned long report,unsigned long status)477 static void print_irqstatus(struct saa7134_dev *dev, int loop,
478 			    unsigned long report, unsigned long status)
479 {
480 	unsigned int i;
481 
482 	irq_dbg(1, "[%d,%ld]: r=0x%lx s=0x%02lx",
483 		loop, jiffies, report, status);
484 	for (i = 0; i < IRQBITS; i++) {
485 		if (!(report & (1 << i)))
486 			continue;
487 		pr_cont(" %s", irqbits[i]);
488 	}
489 	if (report & SAA7134_IRQ_REPORT_DONE_RA0) {
490 		pr_cont(" | RA0=%s,%s,%s,%ld",
491 			(status & 0x40) ? "vbi"  : "video",
492 			(status & 0x20) ? "b"    : "a",
493 			(status & 0x10) ? "odd"  : "even",
494 			(status & 0x0f));
495 	}
496 	pr_cont("\n");
497 }
498 
saa7134_irq(int irq,void * dev_id)499 static irqreturn_t saa7134_irq(int irq, void *dev_id)
500 {
501 	struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
502 	unsigned long report,status;
503 	int loop, handled = 0;
504 
505 	if (dev->insuspend)
506 		goto out;
507 
508 	for (loop = 0; loop < 10; loop++) {
509 		report = saa_readl(SAA7134_IRQ_REPORT);
510 		status = saa_readl(SAA7134_IRQ_STATUS);
511 
512 		/* If dmasound support is active and we get a sound report,
513 		 * mask out the report and let the saa7134-alsa module deal
514 		 * with it */
515 		if ((report & SAA7134_IRQ_REPORT_DONE_RA3) &&
516 			(dev->dmasound.priv_data != NULL) )
517 		{
518 			irq_dbg(2, "preserving DMA sound interrupt\n");
519 			report &= ~SAA7134_IRQ_REPORT_DONE_RA3;
520 		}
521 
522 		if (0 == report) {
523 			irq_dbg(2, "no (more) work\n");
524 			goto out;
525 		}
526 
527 		handled = 1;
528 		saa_writel(SAA7134_IRQ_REPORT,report);
529 		if (irq_debug)
530 			print_irqstatus(dev,loop,report,status);
531 
532 
533 		if ((report & SAA7134_IRQ_REPORT_RDCAP) ||
534 			(report & SAA7134_IRQ_REPORT_INTL))
535 				saa7134_irq_video_signalchange(dev);
536 
537 
538 		if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
539 		    (status & 0x60) == 0)
540 			saa7134_irq_video_done(dev,status);
541 
542 		if ((report & SAA7134_IRQ_REPORT_DONE_RA0) &&
543 		    (status & 0x40) == 0x40)
544 			saa7134_irq_vbi_done(dev,status);
545 
546 		if ((report & SAA7134_IRQ_REPORT_DONE_RA2) &&
547 		    card_has_mpeg(dev)) {
548 			if (dev->mops->irq_ts_done != NULL)
549 				dev->mops->irq_ts_done(dev, status);
550 			else
551 				saa7134_irq_ts_done(dev, status);
552 		}
553 
554 		if (report & SAA7134_IRQ_REPORT_GPIO16) {
555 			switch (dev->has_remote) {
556 				case SAA7134_REMOTE_GPIO:
557 					if (!dev->remote)
558 						break;
559 					if  (dev->remote->mask_keydown & 0x10000) {
560 						saa7134_input_irq(dev);
561 					}
562 					break;
563 
564 				case SAA7134_REMOTE_I2C:
565 					break;			/* FIXME: invoke I2C get_key() */
566 
567 				default:			/* GPIO16 not used by IR remote */
568 					break;
569 			}
570 		}
571 
572 		if (report & SAA7134_IRQ_REPORT_GPIO18) {
573 			switch (dev->has_remote) {
574 				case SAA7134_REMOTE_GPIO:
575 					if (!dev->remote)
576 						break;
577 					if ((dev->remote->mask_keydown & 0x40000) ||
578 					    (dev->remote->mask_keyup & 0x40000)) {
579 						saa7134_input_irq(dev);
580 					}
581 					break;
582 
583 				case SAA7134_REMOTE_I2C:
584 					break;			/* FIXME: invoke I2C get_key() */
585 
586 				default:			/* GPIO18 not used by IR remote */
587 					break;
588 			}
589 		}
590 	}
591 
592 	if (10 == loop) {
593 		print_irqstatus(dev,loop,report,status);
594 		if (report & SAA7134_IRQ_REPORT_PE) {
595 			/* disable all parity error */
596 			pr_warn("%s/irq: looping -- clearing PE (parity error!) enable bit\n",
597 				dev->name);
598 			saa_clearl(SAA7134_IRQ2,SAA7134_IRQ2_INTE_PE);
599 		} else if (report & SAA7134_IRQ_REPORT_GPIO16) {
600 			/* disable gpio16 IRQ */
601 			pr_warn("%s/irq: looping -- clearing GPIO16 enable bit\n",
602 				dev->name);
603 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_P);
604 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO16_N);
605 		} else if (report & SAA7134_IRQ_REPORT_GPIO18) {
606 			/* disable gpio18 IRQs */
607 			pr_warn("%s/irq: looping -- clearing GPIO18 enable bit\n",
608 				dev->name);
609 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_P);
610 			saa_clearl(SAA7134_IRQ2, SAA7134_IRQ2_INTE_GPIO18_N);
611 		} else {
612 			/* disable all irqs */
613 			pr_warn("%s/irq: looping -- clearing all enable bits\n",
614 				dev->name);
615 			saa_writel(SAA7134_IRQ1,0);
616 			saa_writel(SAA7134_IRQ2,0);
617 		}
618 	}
619 
620  out:
621 	return IRQ_RETVAL(handled);
622 }
623 
624 /* ------------------------------------------------------------------ */
625 
626 /* early init (no i2c, no irq) */
627 
saa7134_hw_enable1(struct saa7134_dev * dev)628 static int saa7134_hw_enable1(struct saa7134_dev *dev)
629 {
630 	/* RAM FIFO config */
631 	saa_writel(SAA7134_FIFO_SIZE, 0x08070503);
632 	saa_writel(SAA7134_THRESHOULD, 0x02020202);
633 
634 	/* enable audio + video processing */
635 	saa_writel(SAA7134_MAIN_CTRL,
636 			SAA7134_MAIN_CTRL_VPLLE |
637 			SAA7134_MAIN_CTRL_APLLE |
638 			SAA7134_MAIN_CTRL_EXOSC |
639 			SAA7134_MAIN_CTRL_EVFE1 |
640 			SAA7134_MAIN_CTRL_EVFE2 |
641 			SAA7134_MAIN_CTRL_ESFE  |
642 			SAA7134_MAIN_CTRL_EBDAC);
643 
644 	/*
645 	* Initialize OSS _after_ enabling audio clock PLL and audio processing.
646 	* OSS initialization writes to registers via the audio DSP; these
647 	* writes will fail unless the audio clock has been started.  At worst,
648 	* audio will not work.
649 	*/
650 
651 	/* enable peripheral devices */
652 	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
653 
654 	/* set vertical line numbering start (vbi needs this) */
655 	saa_writeb(SAA7134_SOURCE_TIMING2, 0x20);
656 
657 	return 0;
658 }
659 
saa7134_hwinit1(struct saa7134_dev * dev)660 static int saa7134_hwinit1(struct saa7134_dev *dev)
661 {
662 	core_dbg("hwinit1\n");
663 
664 	saa_writel(SAA7134_IRQ1, 0);
665 	saa_writel(SAA7134_IRQ2, 0);
666 
667 	/* Clear any stale IRQ reports */
668 	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
669 
670 	mutex_init(&dev->lock);
671 	spin_lock_init(&dev->slock);
672 
673 	saa7134_track_gpio(dev,"pre-init");
674 	saa7134_video_init1(dev);
675 	saa7134_vbi_init1(dev);
676 	if (card_has_mpeg(dev))
677 		saa7134_ts_init1(dev);
678 	saa7134_input_init1(dev);
679 
680 	saa7134_hw_enable1(dev);
681 
682 	return 0;
683 }
684 
685 /* late init (with i2c + irq) */
saa7134_hw_enable2(struct saa7134_dev * dev)686 static int saa7134_hw_enable2(struct saa7134_dev *dev)
687 {
688 
689 	unsigned int irq2_mask;
690 
691 	/* enable IRQ's */
692 	irq2_mask =
693 		SAA7134_IRQ2_INTE_DEC3    |
694 		SAA7134_IRQ2_INTE_DEC2    |
695 		SAA7134_IRQ2_INTE_DEC1    |
696 		SAA7134_IRQ2_INTE_DEC0    |
697 		SAA7134_IRQ2_INTE_PE      |
698 		SAA7134_IRQ2_INTE_AR;
699 
700 	if (dev->has_remote == SAA7134_REMOTE_GPIO && dev->remote) {
701 		if (dev->remote->mask_keydown & 0x10000)
702 			irq2_mask |= SAA7134_IRQ2_INTE_GPIO16_N;
703 		else {		/* Allow enabling both IRQ edge triggers */
704 			if (dev->remote->mask_keydown & 0x40000)
705 				irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_P;
706 			if (dev->remote->mask_keyup & 0x40000)
707 				irq2_mask |= SAA7134_IRQ2_INTE_GPIO18_N;
708 		}
709 	}
710 
711 	if (dev->has_remote == SAA7134_REMOTE_I2C) {
712 		request_module("ir-kbd-i2c");
713 	}
714 
715 	saa_writel(SAA7134_IRQ1, 0);
716 	saa_writel(SAA7134_IRQ2, irq2_mask);
717 
718 	return 0;
719 }
720 
saa7134_hwinit2(struct saa7134_dev * dev)721 static int saa7134_hwinit2(struct saa7134_dev *dev)
722 {
723 
724 	core_dbg("hwinit2\n");
725 
726 	saa7134_video_init2(dev);
727 	saa7134_tvaudio_init2(dev);
728 
729 	saa7134_hw_enable2(dev);
730 
731 	return 0;
732 }
733 
734 
735 /* shutdown */
saa7134_hwfini(struct saa7134_dev * dev)736 static int saa7134_hwfini(struct saa7134_dev *dev)
737 {
738 	core_dbg("hwfini\n");
739 
740 	if (card_has_mpeg(dev))
741 		saa7134_ts_fini(dev);
742 	saa7134_input_fini(dev);
743 	saa7134_vbi_fini(dev);
744 	saa7134_tvaudio_fini(dev);
745 	saa7134_video_fini(dev);
746 	return 0;
747 }
748 
must_configure_manually(int has_eeprom)749 static void must_configure_manually(int has_eeprom)
750 {
751 	unsigned int i,p;
752 
753 	if (!has_eeprom)
754 		pr_warn("saa7134: <rant>\n"
755 			"saa7134:  Congratulations!  Your TV card vendor saved a few\n"
756 			"saa7134:  cents for a eeprom, thus your pci board has no\n"
757 			"saa7134:  subsystem ID and I can't identify it automatically\n"
758 			"saa7134: </rant>\n"
759 			"saa7134: I feel better now.  Ok, here are the good news:\n"
760 			"saa7134: You can use the card=<nr> insmod option to specify\n"
761 			"saa7134: which board do you have.  The list:\n");
762 	else
763 		pr_warn("saa7134: Board is currently unknown. You might try to use the card=<nr>\n"
764 			"saa7134: insmod option to specify which board do you have, but this is\n"
765 			"saa7134: somewhat risky, as might damage your card. It is better to ask\n"
766 			"saa7134: for support at linux-media@vger.kernel.org.\n"
767 			"saa7134: The supported cards are:\n");
768 
769 	for (i = 0; i < saa7134_bcount; i++) {
770 		pr_warn("saa7134:   card=%d -> %-40.40s",
771 		       i,saa7134_boards[i].name);
772 		for (p = 0; saa7134_pci_tbl[p].driver_data; p++) {
773 			if (saa7134_pci_tbl[p].driver_data != i)
774 				continue;
775 			pr_cont(" %04x:%04x",
776 			       saa7134_pci_tbl[p].subvendor,
777 			       saa7134_pci_tbl[p].subdevice);
778 		}
779 		pr_cont("\n");
780 	}
781 }
782 
saa7134_unregister_media_device(struct saa7134_dev * dev)783 static void saa7134_unregister_media_device(struct saa7134_dev *dev)
784 {
785 
786 #ifdef CONFIG_MEDIA_CONTROLLER
787 	if (!dev->media_dev)
788 		return;
789 	media_device_unregister(dev->media_dev);
790 	media_device_cleanup(dev->media_dev);
791 	kfree(dev->media_dev);
792 	dev->media_dev = NULL;
793 #endif
794 }
795 
saa7134_media_release(struct saa7134_dev * dev)796 static void saa7134_media_release(struct saa7134_dev *dev)
797 {
798 #ifdef CONFIG_MEDIA_CONTROLLER
799 	int i;
800 
801 	for (i = 0; i < SAA7134_INPUT_MAX + 1; i++)
802 		media_device_unregister_entity(&dev->input_ent[i]);
803 #endif
804 }
805 
806 #if defined(CONFIG_MEDIA_CONTROLLER)
saa7134_create_entities(struct saa7134_dev * dev)807 static void saa7134_create_entities(struct saa7134_dev *dev)
808 {
809 	int ret, i;
810 	struct media_entity *entity;
811 	struct media_entity *decoder = NULL;
812 
813 	/* Check if it is using an external analog TV demod */
814 	media_device_for_each_entity(entity, dev->media_dev) {
815 		if (entity->function == MEDIA_ENT_F_ATV_DECODER) {
816 			decoder = entity;
817 			break;
818 		}
819 	}
820 
821 	/*
822 	 * saa713x is not using an external ATV demod.
823 	 * Register the internal one
824 	 */
825 	if (!decoder) {
826 		dev->demod.name = "saa713x";
827 		dev->demod_pad[SAA7134_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
828 		dev->demod_pad[SAA7134_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
829 		dev->demod_pad[SAA7134_PAD_VID_OUT].flags = MEDIA_PAD_FL_SOURCE;
830 		dev->demod_pad[SAA7134_PAD_VID_OUT].sig_type = PAD_SIGNAL_DV;
831 		dev->demod.function = MEDIA_ENT_F_ATV_DECODER;
832 
833 		ret = media_entity_pads_init(&dev->demod, SAA7134_NUM_PADS,
834 					     dev->demod_pad);
835 		if (ret < 0)
836 			pr_err("failed to initialize demod pad!\n");
837 
838 		ret = media_device_register_entity(dev->media_dev, &dev->demod);
839 		if (ret < 0)
840 			pr_err("failed to register demod entity!\n");
841 
842 		dev->decoder = &dev->demod;
843 	} else {
844 		dev->decoder = decoder;
845 	}
846 
847 	/* Initialize Video, VBI and Radio pads */
848 	dev->video_pad.flags = MEDIA_PAD_FL_SINK;
849 	ret = media_entity_pads_init(&dev->video_dev->entity, 1,
850 				     &dev->video_pad);
851 	if (ret < 0)
852 		pr_err("failed to initialize video media entity!\n");
853 
854 	dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
855 	ret = media_entity_pads_init(&dev->vbi_dev->entity, 1,
856 					&dev->vbi_pad);
857 	if (ret < 0)
858 		pr_err("failed to initialize vbi media entity!\n");
859 
860 	/* Create entities for each input connector */
861 	for (i = 0; i < SAA7134_INPUT_MAX; i++) {
862 		struct media_entity *ent = &dev->input_ent[i];
863 		struct saa7134_input *in = &card_in(dev, i);
864 
865 		if (in->type == SAA7134_NO_INPUT)
866 			break;
867 
868 		/* This input uses the S-Video connector */
869 		if (in->type == SAA7134_INPUT_COMPOSITE_OVER_SVIDEO)
870 			continue;
871 
872 		ent->name = saa7134_input_name[in->type];
873 		ent->flags = MEDIA_ENT_FL_CONNECTOR;
874 		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
875 
876 		switch (in->type) {
877 		case SAA7134_INPUT_COMPOSITE:
878 		case SAA7134_INPUT_COMPOSITE0:
879 		case SAA7134_INPUT_COMPOSITE1:
880 		case SAA7134_INPUT_COMPOSITE2:
881 		case SAA7134_INPUT_COMPOSITE3:
882 		case SAA7134_INPUT_COMPOSITE4:
883 			ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
884 			break;
885 		case SAA7134_INPUT_SVIDEO:
886 		case SAA7134_INPUT_SVIDEO0:
887 		case SAA7134_INPUT_SVIDEO1:
888 			ent->function = MEDIA_ENT_F_CONN_SVIDEO;
889 			break;
890 		default:
891 			/*
892 			 * SAA7134_INPUT_TV and SAA7134_INPUT_TV_MONO.
893 			 *
894 			 * Please notice that neither SAA7134_INPUT_MUTE or
895 			 * SAA7134_INPUT_RADIO are defined at
896 			 * saa7134_board.input.
897 			 */
898 			ent->function = MEDIA_ENT_F_CONN_RF;
899 			break;
900 		}
901 
902 		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
903 		if (ret < 0)
904 			pr_err("failed to initialize input pad[%d]!\n", i);
905 
906 		ret = media_device_register_entity(dev->media_dev, ent);
907 		if (ret < 0)
908 			pr_err("failed to register input entity %d!\n", i);
909 	}
910 
911 	/* Create input for Radio RF connector */
912 	if (card_has_radio(dev)) {
913 		struct saa7134_input *in = &saa7134_boards[dev->board].radio;
914 		struct media_entity *ent = &dev->input_ent[i];
915 
916 		ent->name = saa7134_input_name[in->type];
917 		ent->flags = MEDIA_ENT_FL_CONNECTOR;
918 		dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
919 		ent->function = MEDIA_ENT_F_CONN_RF;
920 
921 		ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
922 		if (ret < 0)
923 			pr_err("failed to initialize input pad[%d]!\n", i);
924 
925 		ret = media_device_register_entity(dev->media_dev, ent);
926 		if (ret < 0)
927 			pr_err("failed to register input entity %d!\n", i);
928 	}
929 }
930 #endif
931 
vdev_init(struct saa7134_dev * dev,struct video_device * template,char * type)932 static struct video_device *vdev_init(struct saa7134_dev *dev,
933 				      struct video_device *template,
934 				      char *type)
935 {
936 	struct video_device *vfd;
937 
938 	vfd = video_device_alloc();
939 	if (NULL == vfd)
940 		return NULL;
941 	*vfd = *template;
942 	vfd->v4l2_dev  = &dev->v4l2_dev;
943 	vfd->release = video_device_release;
944 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
945 		 dev->name, type, saa7134_boards[dev->board].name);
946 	video_set_drvdata(vfd, dev);
947 	return vfd;
948 }
949 
saa7134_unregister_video(struct saa7134_dev * dev)950 static void saa7134_unregister_video(struct saa7134_dev *dev)
951 {
952 	saa7134_media_release(dev);
953 
954 	if (dev->video_dev) {
955 		if (video_is_registered(dev->video_dev))
956 			vb2_video_unregister_device(dev->video_dev);
957 		else
958 			video_device_release(dev->video_dev);
959 		dev->video_dev = NULL;
960 	}
961 	if (dev->vbi_dev) {
962 		if (video_is_registered(dev->vbi_dev))
963 			vb2_video_unregister_device(dev->vbi_dev);
964 		else
965 			video_device_release(dev->vbi_dev);
966 		dev->vbi_dev = NULL;
967 	}
968 	if (dev->radio_dev) {
969 		if (video_is_registered(dev->radio_dev))
970 			video_unregister_device(dev->radio_dev);
971 		else
972 			video_device_release(dev->radio_dev);
973 		dev->radio_dev = NULL;
974 	}
975 }
976 
mpeg_ops_attach(struct saa7134_mpeg_ops * ops,struct saa7134_dev * dev)977 static void mpeg_ops_attach(struct saa7134_mpeg_ops *ops,
978 			    struct saa7134_dev *dev)
979 {
980 	int err;
981 
982 	if (NULL != dev->mops)
983 		return;
984 	if (saa7134_boards[dev->board].mpeg != ops->type)
985 		return;
986 	err = ops->init(dev);
987 	if (0 != err)
988 		return;
989 	dev->mops = ops;
990 }
991 
mpeg_ops_detach(struct saa7134_mpeg_ops * ops,struct saa7134_dev * dev)992 static void mpeg_ops_detach(struct saa7134_mpeg_ops *ops,
993 			    struct saa7134_dev *dev)
994 {
995 	if (NULL == dev->mops)
996 		return;
997 	if (dev->mops != ops)
998 		return;
999 	dev->mops->fini(dev);
1000 	dev->mops = NULL;
1001 }
1002 
saa7134_initdev(struct pci_dev * pci_dev,const struct pci_device_id * pci_id)1003 static int saa7134_initdev(struct pci_dev *pci_dev,
1004 			   const struct pci_device_id *pci_id)
1005 {
1006 	struct saa7134_dev *dev;
1007 	struct saa7134_mpeg_ops *mops;
1008 	int err;
1009 
1010 	if (saa7134_devcount == SAA7134_MAXBOARDS)
1011 		return -ENOMEM;
1012 
1013 	dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1014 	if (NULL == dev)
1015 		return -ENOMEM;
1016 
1017 	dev->nr = saa7134_devcount;
1018 	sprintf(dev->name, "saa%x[%d]", pci_dev->device, dev->nr);
1019 
1020 #ifdef CONFIG_MEDIA_CONTROLLER
1021 	dev->media_dev = kzalloc(sizeof(*dev->media_dev), GFP_KERNEL);
1022 	if (!dev->media_dev) {
1023 		err = -ENOMEM;
1024 		goto err_free_dev;
1025 	}
1026 	media_device_pci_init(dev->media_dev, pci_dev, dev->name);
1027 	dev->v4l2_dev.mdev = dev->media_dev;
1028 #endif
1029 
1030 	err = v4l2_device_register(&pci_dev->dev, &dev->v4l2_dev);
1031 	if (err)
1032 		goto err_free_dev;
1033 
1034 	/* pci init */
1035 	dev->pci = pci_dev;
1036 	if (pci_enable_device(pci_dev)) {
1037 		err = -EIO;
1038 		goto err_v4l2_unregister;
1039 	}
1040 
1041 	/* pci quirks */
1042 	if (pci_pci_problems) {
1043 		if (pci_pci_problems & PCIPCI_TRITON)
1044 			pr_info("%s: quirk: PCIPCI_TRITON\n", dev->name);
1045 		if (pci_pci_problems & PCIPCI_NATOMA)
1046 			pr_info("%s: quirk: PCIPCI_NATOMA\n", dev->name);
1047 		if (pci_pci_problems & PCIPCI_VIAETBF)
1048 			pr_info("%s: quirk: PCIPCI_VIAETBF\n", dev->name);
1049 		if (pci_pci_problems & PCIPCI_VSFX)
1050 			pr_info("%s: quirk: PCIPCI_VSFX\n", dev->name);
1051 #ifdef PCIPCI_ALIMAGIK
1052 		if (pci_pci_problems & PCIPCI_ALIMAGIK) {
1053 			pr_info("%s: quirk: PCIPCI_ALIMAGIK -- latency fixup\n",
1054 			       dev->name);
1055 			latency = 0x0A;
1056 		}
1057 #endif
1058 	}
1059 	if (UNSET != latency) {
1060 		pr_info("%s: setting pci latency timer to %d\n",
1061 		       dev->name,latency);
1062 		pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
1063 	}
1064 
1065 	/* print pci info */
1066 	dev->pci_rev = pci_dev->revision;
1067 	pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
1068 	pr_info("%s: found at %s, rev: %d, irq: %d, latency: %d, mmio: 0x%llx\n",
1069 		dev->name, pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1070 		dev->pci_lat,
1071 		(unsigned long long)pci_resource_start(pci_dev, 0));
1072 	pci_set_master(pci_dev);
1073 	err = dma_set_mask(&pci_dev->dev, DMA_BIT_MASK(32));
1074 	if (err) {
1075 		pr_warn("%s: Oops: no 32bit PCI DMA ???\n", dev->name);
1076 		goto err_v4l2_unregister;
1077 	}
1078 
1079 	/* board config */
1080 	dev->board = pci_id->driver_data;
1081 	if ((unsigned)card[dev->nr] < saa7134_bcount)
1082 		dev->board = card[dev->nr];
1083 	if (SAA7134_BOARD_UNKNOWN == dev->board)
1084 		must_configure_manually(0);
1085 	else if (SAA7134_BOARD_NOAUTO == dev->board) {
1086 		must_configure_manually(1);
1087 		dev->board = SAA7134_BOARD_UNKNOWN;
1088 	}
1089 	dev->autodetected = card[dev->nr] != dev->board;
1090 	dev->tuner_type = saa7134_boards[dev->board].tuner_type;
1091 	dev->tuner_addr = saa7134_boards[dev->board].tuner_addr;
1092 	dev->radio_type = saa7134_boards[dev->board].radio_type;
1093 	dev->radio_addr = saa7134_boards[dev->board].radio_addr;
1094 	dev->tda9887_conf = saa7134_boards[dev->board].tda9887_conf;
1095 	if (UNSET != tuner[dev->nr])
1096 		dev->tuner_type = tuner[dev->nr];
1097 	pr_info("%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
1098 		dev->name,pci_dev->subsystem_vendor,
1099 		pci_dev->subsystem_device,saa7134_boards[dev->board].name,
1100 		dev->board, dev->autodetected ?
1101 		"autodetected" : "insmod option");
1102 
1103 	/* get mmio */
1104 	if (!request_mem_region(pci_resource_start(pci_dev,0),
1105 				pci_resource_len(pci_dev,0),
1106 				dev->name)) {
1107 		err = -EBUSY;
1108 		pr_err("%s: can't get MMIO memory @ 0x%llx\n",
1109 		       dev->name,(unsigned long long)pci_resource_start(pci_dev,0));
1110 		goto err_v4l2_unregister;
1111 	}
1112 	dev->lmmio = ioremap(pci_resource_start(pci_dev, 0),
1113 			     pci_resource_len(pci_dev, 0));
1114 	dev->bmmio = (__u8 __iomem *)dev->lmmio;
1115 	if (NULL == dev->lmmio) {
1116 		err = -EIO;
1117 		pr_err("%s: can't ioremap() MMIO memory\n",
1118 		       dev->name);
1119 		goto err_release_mem_reg;
1120 	}
1121 
1122 	/* initialize hardware #1 */
1123 	saa7134_board_init1(dev);
1124 	saa7134_hwinit1(dev);
1125 
1126 	/* get irq */
1127 	err = request_irq(pci_dev->irq, saa7134_irq,
1128 			  IRQF_SHARED, dev->name, dev);
1129 	if (err < 0) {
1130 		pr_err("%s: can't get IRQ %d\n",
1131 		       dev->name,pci_dev->irq);
1132 		goto err_iounmap;
1133 	}
1134 
1135 	/* wait a bit, register i2c bus */
1136 	msleep(100);
1137 	saa7134_i2c_register(dev);
1138 	saa7134_board_init2(dev);
1139 
1140 	saa7134_hwinit2(dev);
1141 
1142 	/* load i2c helpers */
1143 	if (card_is_empress(dev)) {
1144 		dev->empress_sd =
1145 			v4l2_i2c_new_subdev(&dev->v4l2_dev, &dev->i2c_adap,
1146 				"saa6752hs",
1147 				saa7134_boards[dev->board].empress_addr, NULL);
1148 
1149 		if (dev->empress_sd)
1150 			dev->empress_sd->grp_id = GRP_EMPRESS;
1151 	}
1152 
1153 	if (saa7134_boards[dev->board].rds_addr) {
1154 		struct v4l2_subdev *sd;
1155 
1156 		sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1157 				&dev->i2c_adap, "saa6588",
1158 				0, I2C_ADDRS(saa7134_boards[dev->board].rds_addr));
1159 		if (sd) {
1160 			pr_info("%s: found RDS decoder\n", dev->name);
1161 			dev->has_rds = 1;
1162 		}
1163 	}
1164 
1165 	mutex_lock(&saa7134_devlist_lock);
1166 	list_for_each_entry(mops, &mops_list, next)
1167 		mpeg_ops_attach(mops, dev);
1168 	list_add_tail(&dev->devlist, &saa7134_devlist);
1169 	mutex_unlock(&saa7134_devlist_lock);
1170 
1171 	/* check for signal */
1172 	saa7134_irq_video_signalchange(dev);
1173 
1174 	if (TUNER_ABSENT != dev->tuner_type)
1175 		saa_call_all(dev, core, s_power, 0);
1176 
1177 	/* register v4l devices */
1178 	dev->video_dev = vdev_init(dev,&saa7134_video_template,"video");
1179 	dev->video_dev->ctrl_handler = &dev->ctrl_handler;
1180 	dev->video_dev->lock = &dev->lock;
1181 	dev->video_dev->queue = &dev->video_vbq;
1182 	dev->video_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1183 				      V4L2_CAP_VIDEO_CAPTURE;
1184 	if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET)
1185 		dev->video_dev->device_caps |= V4L2_CAP_TUNER;
1186 
1187 	err = video_register_device(dev->video_dev,VFL_TYPE_VIDEO,
1188 				    video_nr[dev->nr]);
1189 	if (err < 0) {
1190 		pr_info("%s: can't register video device\n",
1191 		       dev->name);
1192 		goto err_unregister_video;
1193 	}
1194 	pr_info("%s: registered device %s [v4l2]\n",
1195 	       dev->name, video_device_node_name(dev->video_dev));
1196 
1197 	dev->vbi_dev = vdev_init(dev, &saa7134_video_template, "vbi");
1198 	dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
1199 	dev->vbi_dev->lock = &dev->lock;
1200 	dev->vbi_dev->queue = &dev->vbi_vbq;
1201 	dev->vbi_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING |
1202 				    V4L2_CAP_VBI_CAPTURE;
1203 	if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET)
1204 		dev->vbi_dev->device_caps |= V4L2_CAP_TUNER;
1205 
1206 	err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1207 				    vbi_nr[dev->nr]);
1208 	if (err < 0)
1209 		goto err_unregister_video;
1210 	pr_info("%s: registered device %s\n",
1211 	       dev->name, video_device_node_name(dev->vbi_dev));
1212 
1213 	if (card_has_radio(dev)) {
1214 		dev->radio_dev = vdev_init(dev,&saa7134_radio_template,"radio");
1215 		dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
1216 		dev->radio_dev->lock = &dev->lock;
1217 		dev->radio_dev->device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
1218 		if (dev->has_rds)
1219 			dev->radio_dev->device_caps |= V4L2_CAP_RDS_CAPTURE;
1220 		err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1221 					    radio_nr[dev->nr]);
1222 		if (err < 0)
1223 			goto err_unregister_video;
1224 		pr_info("%s: registered device %s\n",
1225 		       dev->name, video_device_node_name(dev->radio_dev));
1226 	}
1227 
1228 #ifdef CONFIG_MEDIA_CONTROLLER
1229 	saa7134_create_entities(dev);
1230 
1231 	err = v4l2_mc_create_media_graph(dev->media_dev);
1232 	if (err) {
1233 		pr_err("failed to create media graph\n");
1234 		goto err_unregister_video;
1235 	}
1236 #endif
1237 	/* everything worked */
1238 	saa7134_devcount++;
1239 
1240 	if (saa7134_dmasound_init && !dev->dmasound.priv_data)
1241 		saa7134_dmasound_init(dev);
1242 
1243 	request_submodules(dev);
1244 
1245 	/*
1246 	 * Do it at the end, to reduce dynamic configuration changes during
1247 	 * the device init. Yet, as request_modules() can be async, the
1248 	 * topology will likely change after load the saa7134 subdrivers.
1249 	 */
1250 #ifdef CONFIG_MEDIA_CONTROLLER
1251 	err = media_device_register(dev->media_dev);
1252 	if (err) {
1253 		media_device_cleanup(dev->media_dev);
1254 		goto err_unregister_video;
1255 	}
1256 #endif
1257 
1258 	return 0;
1259 
1260 err_unregister_video:
1261 	saa7134_unregister_video(dev);
1262 	list_del(&dev->devlist);
1263 	saa7134_i2c_unregister(dev);
1264 	free_irq(pci_dev->irq, dev);
1265 err_iounmap:
1266 	saa7134_hwfini(dev);
1267 	iounmap(dev->lmmio);
1268 err_release_mem_reg:
1269 	release_mem_region(pci_resource_start(pci_dev,0),
1270 			   pci_resource_len(pci_dev,0));
1271 err_v4l2_unregister:
1272 	v4l2_device_unregister(&dev->v4l2_dev);
1273 err_free_dev:
1274 #ifdef CONFIG_MEDIA_CONTROLLER
1275 	kfree(dev->media_dev);
1276 #endif
1277 	kfree(dev);
1278 	return err;
1279 }
1280 
saa7134_finidev(struct pci_dev * pci_dev)1281 static void saa7134_finidev(struct pci_dev *pci_dev)
1282 {
1283 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1284 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1285 	struct saa7134_mpeg_ops *mops;
1286 
1287 	flush_request_submodules(dev);
1288 
1289 	/* Release DMA sound modules if present */
1290 	if (saa7134_dmasound_exit && dev->dmasound.priv_data) {
1291 		saa7134_dmasound_exit(dev);
1292 	}
1293 
1294 	/* debugging ... */
1295 	if (irq_debug) {
1296 		u32 report = saa_readl(SAA7134_IRQ_REPORT);
1297 		u32 status = saa_readl(SAA7134_IRQ_STATUS);
1298 		print_irqstatus(dev,42,report,status);
1299 	}
1300 
1301 	/* disable peripheral devices */
1302 	saa_writeb(SAA7134_SPECIAL_MODE,0);
1303 
1304 	/* shutdown hardware */
1305 	saa_writel(SAA7134_IRQ1,0);
1306 	saa_writel(SAA7134_IRQ2,0);
1307 	saa_writel(SAA7134_MAIN_CTRL,0);
1308 
1309 	/* shutdown subsystems */
1310 	saa7134_hwfini(dev);
1311 
1312 	/* unregister */
1313 	mutex_lock(&saa7134_devlist_lock);
1314 	list_del(&dev->devlist);
1315 	list_for_each_entry(mops, &mops_list, next)
1316 		mpeg_ops_detach(mops, dev);
1317 	mutex_unlock(&saa7134_devlist_lock);
1318 	saa7134_devcount--;
1319 
1320 	saa7134_i2c_unregister(dev);
1321 	saa7134_unregister_video(dev);
1322 
1323 
1324 	/* the DMA sound modules should be unloaded before reaching
1325 	   this, but just in case they are still present... */
1326 	if (dev->dmasound.priv_data != NULL) {
1327 		free_irq(pci_dev->irq, &dev->dmasound);
1328 		dev->dmasound.priv_data = NULL;
1329 	}
1330 
1331 
1332 	/* release resources */
1333 	free_irq(pci_dev->irq, dev);
1334 	iounmap(dev->lmmio);
1335 	release_mem_region(pci_resource_start(pci_dev,0),
1336 			   pci_resource_len(pci_dev,0));
1337 
1338 	v4l2_device_unregister(&dev->v4l2_dev);
1339 
1340 	saa7134_unregister_media_device(dev);
1341 
1342 	/* free memory */
1343 	kfree(dev);
1344 }
1345 
1346 /* resends a current buffer in queue after resume */
saa7134_buffer_requeue(struct saa7134_dev * dev,struct saa7134_dmaqueue * q)1347 static int __maybe_unused saa7134_buffer_requeue(struct saa7134_dev *dev,
1348 						 struct saa7134_dmaqueue *q)
1349 {
1350 	struct saa7134_buf *buf, *next;
1351 
1352 	assert_spin_locked(&dev->slock);
1353 
1354 	buf  = q->curr;
1355 	next = buf;
1356 	core_dbg("buffer_requeue\n");
1357 
1358 	if (!buf)
1359 		return 0;
1360 
1361 	core_dbg("buffer_requeue : resending active buffer\n");
1362 
1363 	if (!list_empty(&q->queue))
1364 		next = list_entry(q->queue.next, struct saa7134_buf,
1365 					  entry);
1366 	buf->activate(dev, buf, next);
1367 
1368 	return 0;
1369 }
1370 
saa7134_suspend(struct device * dev_d)1371 static int __maybe_unused saa7134_suspend(struct device *dev_d)
1372 {
1373 	struct pci_dev *pci_dev = to_pci_dev(dev_d);
1374 	struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
1375 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1376 
1377 	/* Disable interrupts, DMA, and rest of the chip*/
1378 	saa_writel(SAA7134_IRQ1, 0);
1379 	saa_writel(SAA7134_IRQ2, 0);
1380 	saa_writel(SAA7134_MAIN_CTRL, 0);
1381 
1382 	dev->insuspend = 1;
1383 	synchronize_irq(pci_dev->irq);
1384 
1385 	/* ACK interrupts once more, just in case,
1386 		since the IRQ handler won't ack them anymore*/
1387 
1388 	saa_writel(SAA7134_IRQ_REPORT, saa_readl(SAA7134_IRQ_REPORT));
1389 
1390 	/* Disable timeout timers - if we have active buffers, we will
1391 	   fill them on resume*/
1392 
1393 	del_timer(&dev->video_q.timeout);
1394 	del_timer(&dev->vbi_q.timeout);
1395 	del_timer(&dev->ts_q.timeout);
1396 
1397 	if (dev->remote && dev->remote->dev->users)
1398 		saa7134_ir_close(dev->remote->dev);
1399 
1400 	return 0;
1401 }
1402 
saa7134_resume(struct device * dev_d)1403 static int __maybe_unused saa7134_resume(struct device *dev_d)
1404 {
1405 	struct v4l2_device *v4l2_dev = dev_get_drvdata(dev_d);
1406 	struct saa7134_dev *dev = container_of(v4l2_dev, struct saa7134_dev, v4l2_dev);
1407 	unsigned long flags;
1408 
1409 	/* Do things that are done in saa7134_initdev ,
1410 		except of initializing memory structures.*/
1411 
1412 	saa7134_board_init1(dev);
1413 
1414 	/* saa7134_hwinit1 */
1415 	if (saa7134_boards[dev->board].video_out)
1416 		saa7134_videoport_init(dev);
1417 	if (card_has_mpeg(dev))
1418 		saa7134_ts_init_hw(dev);
1419 	if (dev->remote && dev->remote->dev->users)
1420 		saa7134_ir_open(dev->remote->dev);
1421 	saa7134_hw_enable1(dev);
1422 
1423 	msleep(100);
1424 
1425 	saa7134_board_init2(dev);
1426 
1427 	/*saa7134_hwinit2*/
1428 	saa7134_set_tvnorm_hw(dev);
1429 	saa7134_tvaudio_setmute(dev);
1430 	saa7134_tvaudio_setvolume(dev, dev->ctl_volume);
1431 	saa7134_tvaudio_init(dev);
1432 	saa7134_enable_i2s(dev);
1433 	saa7134_hw_enable2(dev);
1434 
1435 	saa7134_irq_video_signalchange(dev);
1436 
1437 	/*resume unfinished buffer(s)*/
1438 	spin_lock_irqsave(&dev->slock, flags);
1439 	saa7134_buffer_requeue(dev, &dev->video_q);
1440 	saa7134_buffer_requeue(dev, &dev->vbi_q);
1441 	saa7134_buffer_requeue(dev, &dev->ts_q);
1442 
1443 	/* FIXME: Disable DMA audio sound - temporary till proper support
1444 		  is implemented*/
1445 
1446 	dev->dmasound.dma_running = 0;
1447 
1448 	/* start DMA now*/
1449 	dev->insuspend = 0;
1450 	smp_wmb();
1451 	saa7134_set_dmabits(dev);
1452 	spin_unlock_irqrestore(&dev->slock, flags);
1453 
1454 	return 0;
1455 }
1456 
1457 /* ----------------------------------------------------------- */
1458 
saa7134_ts_register(struct saa7134_mpeg_ops * ops)1459 int saa7134_ts_register(struct saa7134_mpeg_ops *ops)
1460 {
1461 	struct saa7134_dev *dev;
1462 
1463 	mutex_lock(&saa7134_devlist_lock);
1464 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1465 		mpeg_ops_attach(ops, dev);
1466 	list_add_tail(&ops->next,&mops_list);
1467 	mutex_unlock(&saa7134_devlist_lock);
1468 	return 0;
1469 }
1470 
saa7134_ts_unregister(struct saa7134_mpeg_ops * ops)1471 void saa7134_ts_unregister(struct saa7134_mpeg_ops *ops)
1472 {
1473 	struct saa7134_dev *dev;
1474 
1475 	mutex_lock(&saa7134_devlist_lock);
1476 	list_del(&ops->next);
1477 	list_for_each_entry(dev, &saa7134_devlist, devlist)
1478 		mpeg_ops_detach(ops, dev);
1479 	mutex_unlock(&saa7134_devlist_lock);
1480 }
1481 
1482 EXPORT_SYMBOL(saa7134_ts_register);
1483 EXPORT_SYMBOL(saa7134_ts_unregister);
1484 
1485 /* ----------------------------------------------------------- */
1486 
1487 static SIMPLE_DEV_PM_OPS(saa7134_pm_ops, saa7134_suspend, saa7134_resume);
1488 
1489 static struct pci_driver saa7134_pci_driver = {
1490 	.name     = "saa7134",
1491 	.id_table = saa7134_pci_tbl,
1492 	.probe    = saa7134_initdev,
1493 	.remove   = saa7134_finidev,
1494 	.driver.pm = &saa7134_pm_ops,
1495 };
1496 
saa7134_init(void)1497 static int __init saa7134_init(void)
1498 {
1499 	pr_info("saa7130/34: v4l2 driver version %s loaded\n",
1500 	       SAA7134_VERSION);
1501 	return pci_register_driver(&saa7134_pci_driver);
1502 }
1503 
saa7134_fini(void)1504 static void __exit saa7134_fini(void)
1505 {
1506 	pci_unregister_driver(&saa7134_pci_driver);
1507 }
1508 
1509 module_init(saa7134_init);
1510 module_exit(saa7134_fini);
1511 
1512 /* ----------------------------------------------------------- */
1513 
1514 EXPORT_SYMBOL(saa7134_set_gpio);
1515 EXPORT_SYMBOL(saa7134_boards);
1516 
1517 /* ----------------- for the DMA sound modules --------------- */
1518 
1519 EXPORT_SYMBOL(saa7134_dmasound_init);
1520 EXPORT_SYMBOL(saa7134_dmasound_exit);
1521 EXPORT_SYMBOL(saa7134_pgtable_free);
1522 EXPORT_SYMBOL(saa7134_pgtable_build);
1523 EXPORT_SYMBOL(saa7134_pgtable_alloc);
1524 EXPORT_SYMBOL(saa7134_set_dmabits);
1525