Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / pci / ttpci / budget-core.c
blobb3dc45b91101ddb0861c41de1c083b70aabe1eb7
1 /*
2 * budget-core.c: driver for the SAA7146 based Budget DVB cards
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
8 * Copyright (C) 1999-2002 Ralph Metzler
9 * & Marcus Metzler for convergence integrated media GmbH
11 * 26feb2004 Support for FS Activy Card (Grundig tuner) by
12 * Michael Dreher <michael@5dot1.de>,
13 * Oliver Endriss <o.endriss@gmx.de>,
14 * Andreas 'randy' Weinberger
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * To obtain the license, point your browser to
28 * http://www.gnu.org/copyleft/gpl.html
31 * the project's page is at https://linuxtv.org
35 #include "budget.h"
36 #include "ttpci-eeprom.h"
38 #define TS_WIDTH (2 * TS_SIZE)
39 #define TS_WIDTH_ACTIVY TS_SIZE
40 #define TS_WIDTH_DVBC TS_SIZE
41 #define TS_HEIGHT_MASK 0xf00
42 #define TS_HEIGHT_MASK_ACTIVY 0xc00
43 #define TS_HEIGHT_MASK_DVBC 0xe00
44 #define TS_MIN_BUFSIZE_K 188
45 #define TS_MAX_BUFSIZE_K 1410
46 #define TS_MAX_BUFSIZE_K_ACTIVY 564
47 #define TS_MAX_BUFSIZE_K_DVBC 1316
48 #define BUFFER_WARNING_WAIT (30*HZ)
50 int budget_debug;
51 static int dma_buffer_size = TS_MIN_BUFSIZE_K;
52 module_param_named(debug, budget_debug, int, 0644);
53 module_param_named(bufsize, dma_buffer_size, int, 0444);
54 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
55 MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
57 /****************************************************************************
58 * TT budget / WinTV Nova
59 ****************************************************************************/
61 static int stop_ts_capture(struct budget *budget)
63 dprintk(2, "budget: %p\n", budget);
65 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
66 SAA7146_IER_DISABLE(budget->dev, MASK_10);
67 return 0;
70 static int start_ts_capture(struct budget *budget)
72 struct saa7146_dev *dev = budget->dev;
74 dprintk(2, "budget: %p\n", budget);
76 if (!budget->feeding || !budget->fe_synced)
77 return 0;
79 saa7146_write(dev, MC1, MASK_20); // DMA3 off
81 memset(budget->grabbing, 0x00, budget->buffer_size);
83 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
85 budget->ttbp = 0;
88 * Signal path on the Activy:
90 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
92 * Since the tuner feeds 204 bytes packets into the SAA7146,
93 * DMA3 is configured to strip the trailing 16 FEC bytes:
94 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
97 switch(budget->card->type) {
98 case BUDGET_FS_ACTIVY:
99 saa7146_write(dev, DD1_INIT, 0x04000000);
100 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
101 saa7146_write(dev, BRS_CTRL, 0x00000000);
102 break;
103 case BUDGET_PATCH:
104 saa7146_write(dev, DD1_INIT, 0x00000200);
105 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
106 saa7146_write(dev, BRS_CTRL, 0x60000000);
107 break;
108 case BUDGET_CIN1200C_MK3:
109 case BUDGET_KNC1C_MK3:
110 case BUDGET_KNC1C_TDA10024:
111 case BUDGET_KNC1CP_MK3:
112 if (budget->video_port == BUDGET_VIDEO_PORTA) {
113 saa7146_write(dev, DD1_INIT, 0x06000200);
114 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
115 saa7146_write(dev, BRS_CTRL, 0x00000000);
116 } else {
117 saa7146_write(dev, DD1_INIT, 0x00000600);
118 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
119 saa7146_write(dev, BRS_CTRL, 0x60000000);
121 break;
122 default:
123 if (budget->video_port == BUDGET_VIDEO_PORTA) {
124 saa7146_write(dev, DD1_INIT, 0x06000200);
125 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
126 saa7146_write(dev, BRS_CTRL, 0x00000000);
127 } else {
128 saa7146_write(dev, DD1_INIT, 0x02000600);
129 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
130 saa7146_write(dev, BRS_CTRL, 0x60000000);
134 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
135 mdelay(10);
137 saa7146_write(dev, BASE_ODD3, 0);
138 if (budget->buffer_size > budget->buffer_height * budget->buffer_width) {
139 // using odd/even buffers
140 saa7146_write(dev, BASE_EVEN3, budget->buffer_height * budget->buffer_width);
141 } else {
142 // using a single buffer
143 saa7146_write(dev, BASE_EVEN3, 0);
145 saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
146 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
148 saa7146_write(dev, PITCH3, budget->buffer_width);
149 saa7146_write(dev, NUM_LINE_BYTE3,
150 (budget->buffer_height << 16) | budget->buffer_width);
152 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
154 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
155 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
156 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
158 return 0;
161 static int budget_read_fe_status(struct dvb_frontend *fe,
162 enum fe_status *status)
164 struct budget *budget = (struct budget *) fe->dvb->priv;
165 int synced;
166 int ret;
168 if (budget->read_fe_status)
169 ret = budget->read_fe_status(fe, status);
170 else
171 ret = -EINVAL;
173 if (!ret) {
174 synced = (*status & FE_HAS_LOCK);
175 if (synced != budget->fe_synced) {
176 budget->fe_synced = synced;
177 spin_lock(&budget->feedlock);
178 if (synced)
179 start_ts_capture(budget);
180 else
181 stop_ts_capture(budget);
182 spin_unlock(&budget->feedlock);
185 return ret;
188 static void vpeirq(unsigned long data)
190 struct budget *budget = (struct budget *) data;
191 u8 *mem = (u8 *) (budget->grabbing);
192 u32 olddma = budget->ttbp;
193 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
194 u32 count;
196 /* Ensure streamed PCI data is synced to CPU */
197 pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
199 /* nearest lower position divisible by 188 */
200 newdma -= newdma % 188;
202 if (newdma >= budget->buffer_size)
203 return;
205 budget->ttbp = newdma;
207 if (budget->feeding == 0 || newdma == olddma)
208 return;
210 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
211 count = newdma - olddma;
212 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
213 } else { /* wraparound, dump olddma..buflen and 0..newdma */
214 count = budget->buffer_size - olddma;
215 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
216 count += newdma;
217 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
220 if (count > budget->buffer_warning_threshold)
221 budget->buffer_warnings++;
223 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
224 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
225 budget->dev->name, __func__, budget->buffer_warnings, count);
226 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
227 budget->buffer_warnings = 0;
232 static int ttpci_budget_debiread_nolock(struct budget *budget, u32 config,
233 int addr, int count, int nobusyloop)
235 struct saa7146_dev *saa = budget->dev;
236 int result;
238 result = saa7146_wait_for_debi_done(saa, nobusyloop);
239 if (result < 0)
240 return result;
242 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
243 saa7146_write(saa, DEBI_CONFIG, config);
244 saa7146_write(saa, DEBI_PAGE, 0);
245 saa7146_write(saa, MC2, (2 << 16) | 2);
247 result = saa7146_wait_for_debi_done(saa, nobusyloop);
248 if (result < 0)
249 return result;
251 result = saa7146_read(saa, DEBI_AD);
252 result &= (0xffffffffUL >> ((4 - count) * 8));
253 return result;
256 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
257 int uselocks, int nobusyloop)
259 if (count > 4 || count <= 0)
260 return 0;
262 if (uselocks) {
263 unsigned long flags;
264 int result;
266 spin_lock_irqsave(&budget->debilock, flags);
267 result = ttpci_budget_debiread_nolock(budget, config, addr,
268 count, nobusyloop);
269 spin_unlock_irqrestore(&budget->debilock, flags);
270 return result;
272 return ttpci_budget_debiread_nolock(budget, config, addr,
273 count, nobusyloop);
276 static int ttpci_budget_debiwrite_nolock(struct budget *budget, u32 config,
277 int addr, int count, u32 value, int nobusyloop)
279 struct saa7146_dev *saa = budget->dev;
280 int result;
282 result = saa7146_wait_for_debi_done(saa, nobusyloop);
283 if (result < 0)
284 return result;
286 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
287 saa7146_write(saa, DEBI_CONFIG, config);
288 saa7146_write(saa, DEBI_PAGE, 0);
289 saa7146_write(saa, DEBI_AD, value);
290 saa7146_write(saa, MC2, (2 << 16) | 2);
292 result = saa7146_wait_for_debi_done(saa, nobusyloop);
293 return result < 0 ? result : 0;
296 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
297 int count, u32 value, int uselocks, int nobusyloop)
299 if (count > 4 || count <= 0)
300 return 0;
302 if (uselocks) {
303 unsigned long flags;
304 int result;
306 spin_lock_irqsave(&budget->debilock, flags);
307 result = ttpci_budget_debiwrite_nolock(budget, config, addr,
308 count, value, nobusyloop);
309 spin_unlock_irqrestore(&budget->debilock, flags);
310 return result;
312 return ttpci_budget_debiwrite_nolock(budget, config, addr,
313 count, value, nobusyloop);
317 /****************************************************************************
318 * DVB API SECTION
319 ****************************************************************************/
321 static int budget_start_feed(struct dvb_demux_feed *feed)
323 struct dvb_demux *demux = feed->demux;
324 struct budget *budget = (struct budget *) demux->priv;
325 int status = 0;
327 dprintk(2, "budget: %p\n", budget);
329 if (!demux->dmx.frontend)
330 return -EINVAL;
332 spin_lock(&budget->feedlock);
333 feed->pusi_seen = false; /* have a clean section start */
334 if (budget->feeding++ == 0)
335 status = start_ts_capture(budget);
336 spin_unlock(&budget->feedlock);
337 return status;
340 static int budget_stop_feed(struct dvb_demux_feed *feed)
342 struct dvb_demux *demux = feed->demux;
343 struct budget *budget = (struct budget *) demux->priv;
344 int status = 0;
346 dprintk(2, "budget: %p\n", budget);
348 spin_lock(&budget->feedlock);
349 if (--budget->feeding == 0)
350 status = stop_ts_capture(budget);
351 spin_unlock(&budget->feedlock);
352 return status;
355 static int budget_register(struct budget *budget)
357 struct dvb_demux *dvbdemux = &budget->demux;
358 int ret;
360 dprintk(2, "budget: %p\n", budget);
362 dvbdemux->priv = (void *) budget;
364 dvbdemux->filternum = 256;
365 dvbdemux->feednum = 256;
366 dvbdemux->start_feed = budget_start_feed;
367 dvbdemux->stop_feed = budget_stop_feed;
368 dvbdemux->write_to_decoder = NULL;
370 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
371 DMX_MEMORY_BASED_FILTERING);
373 dvb_dmx_init(&budget->demux);
375 budget->dmxdev.filternum = 256;
376 budget->dmxdev.demux = &dvbdemux->dmx;
377 budget->dmxdev.capabilities = 0;
379 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
381 budget->hw_frontend.source = DMX_FRONTEND_0;
383 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
385 if (ret < 0)
386 return ret;
388 budget->mem_frontend.source = DMX_MEMORY_FE;
389 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
390 if (ret < 0)
391 return ret;
393 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
394 if (ret < 0)
395 return ret;
397 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
399 return 0;
402 static void budget_unregister(struct budget *budget)
404 struct dvb_demux *dvbdemux = &budget->demux;
406 dprintk(2, "budget: %p\n", budget);
408 dvb_net_release(&budget->dvb_net);
410 dvbdemux->dmx.close(&dvbdemux->dmx);
411 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
412 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
414 dvb_dmxdev_release(&budget->dmxdev);
415 dvb_dmx_release(&budget->demux);
418 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
419 struct saa7146_pci_extension_data *info,
420 struct module *owner, short *adapter_nums)
422 int ret = 0;
423 struct budget_info *bi = info->ext_priv;
424 int max_bufsize;
425 int height_mask;
427 memset(budget, 0, sizeof(struct budget));
429 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
431 budget->card = bi;
432 budget->dev = (struct saa7146_dev *) dev;
434 switch(budget->card->type) {
435 case BUDGET_FS_ACTIVY:
436 budget->buffer_width = TS_WIDTH_ACTIVY;
437 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
438 height_mask = TS_HEIGHT_MASK_ACTIVY;
439 break;
441 case BUDGET_KNC1C:
442 case BUDGET_KNC1CP:
443 case BUDGET_CIN1200C:
444 case BUDGET_KNC1C_MK3:
445 case BUDGET_KNC1C_TDA10024:
446 case BUDGET_KNC1CP_MK3:
447 case BUDGET_CIN1200C_MK3:
448 budget->buffer_width = TS_WIDTH_DVBC;
449 max_bufsize = TS_MAX_BUFSIZE_K_DVBC;
450 height_mask = TS_HEIGHT_MASK_DVBC;
451 break;
453 default:
454 budget->buffer_width = TS_WIDTH;
455 max_bufsize = TS_MAX_BUFSIZE_K;
456 height_mask = TS_HEIGHT_MASK;
459 if (dma_buffer_size < TS_MIN_BUFSIZE_K)
460 dma_buffer_size = TS_MIN_BUFSIZE_K;
461 else if (dma_buffer_size > max_bufsize)
462 dma_buffer_size = max_bufsize;
464 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
465 if (budget->buffer_height > 0xfff) {
466 budget->buffer_height /= 2;
467 budget->buffer_height &= height_mask;
468 budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width;
469 } else {
470 budget->buffer_height &= height_mask;
471 budget->buffer_size = budget->buffer_height * budget->buffer_width;
473 budget->buffer_warning_threshold = budget->buffer_size * 80/100;
474 budget->buffer_warnings = 0;
475 budget->buffer_warning_time = jiffies;
477 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n",
478 budget->dev->name,
479 budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single",
480 budget->buffer_width, budget->buffer_height);
481 printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
483 ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name,
484 owner, &budget->dev->pci->dev, adapter_nums);
485 if (ret < 0)
486 return ret;
488 /* set dd1 stream a & b */
489 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
490 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
491 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
492 saa7146_write(dev, DD1_INIT, 0x02000000);
493 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
495 if (bi->type != BUDGET_FS_ACTIVY)
496 budget->video_port = BUDGET_VIDEO_PORTB;
497 else
498 budget->video_port = BUDGET_VIDEO_PORTA;
499 spin_lock_init(&budget->feedlock);
500 spin_lock_init(&budget->debilock);
502 /* the Siemens DVB needs this if you want to have the i2c chips
503 get recognized before the main driver is loaded */
504 if (bi->type != BUDGET_FS_ACTIVY)
505 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
507 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
509 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
510 strcpy(budget->i2c_adap.name, budget->card->name);
512 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
513 ret = -ENOMEM;
514 goto err_dvb_unregister;
517 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
519 budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt);
520 if (NULL == budget->grabbing) {
521 ret = -ENOMEM;
522 goto err_del_i2c;
525 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
526 /* upload all */
527 saa7146_write(dev, GPIO_CTRL, 0x000000);
529 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
531 /* frontend power on */
532 if (bi->type != BUDGET_FS_ACTIVY)
533 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
535 if ((ret = budget_register(budget)) == 0)
536 return 0; /* Everything OK */
538 /* An error occurred, cleanup resources */
539 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
541 err_del_i2c:
542 i2c_del_adapter(&budget->i2c_adap);
544 err_dvb_unregister:
545 dvb_unregister_adapter(&budget->dvb_adapter);
547 return ret;
550 void ttpci_budget_init_hooks(struct budget *budget)
552 if (budget->dvb_frontend && !budget->read_fe_status) {
553 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
554 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
558 int ttpci_budget_deinit(struct budget *budget)
560 struct saa7146_dev *dev = budget->dev;
562 dprintk(2, "budget: %p\n", budget);
564 budget_unregister(budget);
566 tasklet_kill(&budget->vpe_tasklet);
568 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
570 i2c_del_adapter(&budget->i2c_adap);
572 dvb_unregister_adapter(&budget->dvb_adapter);
574 return 0;
577 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
579 struct budget *budget = (struct budget *) dev->ext_priv;
581 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
583 if (*isr & MASK_10)
584 tasklet_schedule(&budget->vpe_tasklet);
587 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
589 struct budget *budget = (struct budget *) dev->ext_priv;
591 spin_lock(&budget->feedlock);
592 budget->video_port = video_port;
593 if (budget->feeding) {
594 stop_ts_capture(budget);
595 start_ts_capture(budget);
597 spin_unlock(&budget->feedlock);
600 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
601 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
602 EXPORT_SYMBOL_GPL(ttpci_budget_init);
603 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
604 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
605 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
606 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
607 EXPORT_SYMBOL_GPL(budget_debug);
609 MODULE_LICENSE("GPL");