1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * budget-core.ko: base-driver for the SAA7146 based Budget DVB cards
5 * Compiled from various sources by Michael Hunold <michael@mihu.de>
7 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
9 * Copyright (C) 1999-2002 Ralph Metzler
10 * & Marcus Metzler for convergence integrated media GmbH
12 * 26feb2004 Support for FS Activy Card (Grundig tuner) by
13 * Michael Dreher <michael@5dot1.de>,
14 * Oliver Endriss <o.endriss@gmx.de>,
15 * Andreas 'randy' Weinberger
17 * the project's page is at https://linuxtv.org
22 #include "ttpci-eeprom.h"
24 #define TS_WIDTH (2 * TS_SIZE)
25 #define TS_WIDTH_ACTIVY TS_SIZE
26 #define TS_WIDTH_DVBC TS_SIZE
27 #define TS_HEIGHT_MASK 0xf00
28 #define TS_HEIGHT_MASK_ACTIVY 0xc00
29 #define TS_HEIGHT_MASK_DVBC 0xe00
30 #define TS_MIN_BUFSIZE_K 188
31 #define TS_MAX_BUFSIZE_K 1410
32 #define TS_MAX_BUFSIZE_K_ACTIVY 564
33 #define TS_MAX_BUFSIZE_K_DVBC 1316
34 #define BUFFER_WARNING_WAIT (30*HZ)
37 EXPORT_SYMBOL_GPL(budget_debug
);
38 static int dma_buffer_size
= TS_MIN_BUFSIZE_K
;
39 module_param_named(debug
, budget_debug
, int, 0644);
40 module_param_named(bufsize
, dma_buffer_size
, int, 0444);
41 MODULE_PARM_DESC(debug
, "Turn on/off budget debugging (default:off).");
42 MODULE_PARM_DESC(bufsize
, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
44 /****************************************************************************
45 * TT budget / WinTV Nova
46 ****************************************************************************/
48 static int stop_ts_capture(struct budget
*budget
)
50 dprintk(2, "budget: %p\n", budget
);
52 saa7146_write(budget
->dev
, MC1
, MASK_20
); // DMA3 off
53 SAA7146_IER_DISABLE(budget
->dev
, MASK_10
);
57 static int start_ts_capture(struct budget
*budget
)
59 struct saa7146_dev
*dev
= budget
->dev
;
61 dprintk(2, "budget: %p\n", budget
);
63 if (!budget
->feeding
|| !budget
->fe_synced
)
66 saa7146_write(dev
, MC1
, MASK_20
); // DMA3 off
68 memset(budget
->grabbing
, 0x00, budget
->buffer_size
);
70 saa7146_write(dev
, PCI_BT_V1
, 0x001c0000 | (saa7146_read(dev
, PCI_BT_V1
) & ~0x001f0000));
75 * Signal path on the Activy:
77 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
79 * Since the tuner feeds 204 bytes packets into the SAA7146,
80 * DMA3 is configured to strip the trailing 16 FEC bytes:
81 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
84 switch (budget
->card
->type
) {
85 case BUDGET_FS_ACTIVY
:
86 saa7146_write(dev
, DD1_INIT
, 0x04000000);
87 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
));
88 saa7146_write(dev
, BRS_CTRL
, 0x00000000);
91 saa7146_write(dev
, DD1_INIT
, 0x00000200);
92 saa7146_write(dev
, MC2
, (MASK_10
| MASK_26
));
93 saa7146_write(dev
, BRS_CTRL
, 0x60000000);
95 case BUDGET_CIN1200C_MK3
:
96 case BUDGET_KNC1C_MK3
:
97 case BUDGET_KNC1C_TDA10024
:
98 case BUDGET_KNC1CP_MK3
:
99 if (budget
->video_port
== BUDGET_VIDEO_PORTA
) {
100 saa7146_write(dev
, DD1_INIT
, 0x06000200);
101 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
| MASK_10
| MASK_26
));
102 saa7146_write(dev
, BRS_CTRL
, 0x00000000);
104 saa7146_write(dev
, DD1_INIT
, 0x00000600);
105 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
| MASK_10
| MASK_26
));
106 saa7146_write(dev
, BRS_CTRL
, 0x60000000);
110 if (budget
->video_port
== BUDGET_VIDEO_PORTA
) {
111 saa7146_write(dev
, DD1_INIT
, 0x06000200);
112 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
| MASK_10
| MASK_26
));
113 saa7146_write(dev
, BRS_CTRL
, 0x00000000);
115 saa7146_write(dev
, DD1_INIT
, 0x02000600);
116 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
| MASK_10
| MASK_26
));
117 saa7146_write(dev
, BRS_CTRL
, 0x60000000);
121 saa7146_write(dev
, MC2
, (MASK_08
| MASK_24
));
124 saa7146_write(dev
, BASE_ODD3
, 0);
125 if (budget
->buffer_size
> budget
->buffer_height
* budget
->buffer_width
) {
126 // using odd/even buffers
127 saa7146_write(dev
, BASE_EVEN3
, budget
->buffer_height
* budget
->buffer_width
);
129 // using a single buffer
130 saa7146_write(dev
, BASE_EVEN3
, 0);
132 saa7146_write(dev
, PROT_ADDR3
, budget
->buffer_size
);
133 saa7146_write(dev
, BASE_PAGE3
, budget
->pt
.dma
| ME1
| 0x90);
135 saa7146_write(dev
, PITCH3
, budget
->buffer_width
);
136 saa7146_write(dev
, NUM_LINE_BYTE3
,
137 (budget
->buffer_height
<< 16) | budget
->buffer_width
);
139 saa7146_write(dev
, MC2
, (MASK_04
| MASK_20
));
141 SAA7146_ISR_CLEAR(budget
->dev
, MASK_10
); /* VPE */
142 SAA7146_IER_ENABLE(budget
->dev
, MASK_10
); /* VPE */
143 saa7146_write(dev
, MC1
, (MASK_04
| MASK_20
)); /* DMA3 on */
148 static int budget_read_fe_status(struct dvb_frontend
*fe
,
149 enum fe_status
*status
)
151 struct budget
*budget
= fe
->dvb
->priv
;
155 if (budget
->read_fe_status
)
156 ret
= budget
->read_fe_status(fe
, status
);
161 synced
= (*status
& FE_HAS_LOCK
);
162 if (synced
!= budget
->fe_synced
) {
163 budget
->fe_synced
= synced
;
164 spin_lock(&budget
->feedlock
);
166 start_ts_capture(budget
);
168 stop_ts_capture(budget
);
169 spin_unlock(&budget
->feedlock
);
175 static void vpeirq(struct work_struct
*t
)
177 struct budget
*budget
= from_work(budget
, t
, vpe_bh_work
);
178 u8
*mem
= (u8
*) (budget
->grabbing
);
179 u32 olddma
= budget
->ttbp
;
180 u32 newdma
= saa7146_read(budget
->dev
, PCI_VDP3
);
183 /* Ensure streamed PCI data is synced to CPU */
184 dma_sync_sg_for_cpu(&budget
->dev
->pci
->dev
, budget
->pt
.slist
,
185 budget
->pt
.nents
, DMA_FROM_DEVICE
);
187 /* nearest lower position divisible by 188 */
188 newdma
-= newdma
% 188;
190 if (newdma
>= budget
->buffer_size
)
193 budget
->ttbp
= newdma
;
195 if (budget
->feeding
== 0 || newdma
== olddma
)
198 if (newdma
> olddma
) { /* no wraparound, dump olddma..newdma */
199 count
= newdma
- olddma
;
200 dvb_dmx_swfilter_packets(&budget
->demux
, mem
+ olddma
, count
/ 188);
201 } else { /* wraparound, dump olddma..buflen and 0..newdma */
202 count
= budget
->buffer_size
- olddma
;
203 dvb_dmx_swfilter_packets(&budget
->demux
, mem
+ olddma
, count
/ 188);
205 dvb_dmx_swfilter_packets(&budget
->demux
, mem
, newdma
/ 188);
208 if (count
> budget
->buffer_warning_threshold
)
209 budget
->buffer_warnings
++;
211 if (budget
->buffer_warnings
&& time_after(jiffies
, budget
->buffer_warning_time
)) {
212 pr_warn("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
213 budget
->dev
->name
, __func__
, budget
->buffer_warnings
, count
);
214 budget
->buffer_warning_time
= jiffies
+ BUFFER_WARNING_WAIT
;
215 budget
->buffer_warnings
= 0;
220 static int ttpci_budget_debiread_nolock(struct budget
*budget
, u32 config
,
221 int addr
, int count
, int nobusyloop
)
223 struct saa7146_dev
*saa
= budget
->dev
;
226 result
= saa7146_wait_for_debi_done(saa
, nobusyloop
);
230 saa7146_write(saa
, DEBI_COMMAND
, (count
<< 17) | 0x10000 | (addr
& 0xffff));
231 saa7146_write(saa
, DEBI_CONFIG
, config
);
232 saa7146_write(saa
, DEBI_PAGE
, 0);
233 saa7146_write(saa
, MC2
, (2 << 16) | 2);
235 result
= saa7146_wait_for_debi_done(saa
, nobusyloop
);
239 result
= saa7146_read(saa
, DEBI_AD
);
240 result
&= (0xffffffffUL
>> ((4 - count
) * 8));
244 int ttpci_budget_debiread(struct budget
*budget
, u32 config
, int addr
, int count
,
245 int uselocks
, int nobusyloop
)
247 if (count
> 4 || count
<= 0)
254 spin_lock_irqsave(&budget
->debilock
, flags
);
255 result
= ttpci_budget_debiread_nolock(budget
, config
, addr
,
257 spin_unlock_irqrestore(&budget
->debilock
, flags
);
260 return ttpci_budget_debiread_nolock(budget
, config
, addr
,
263 EXPORT_SYMBOL_GPL(ttpci_budget_debiread
);
265 static int ttpci_budget_debiwrite_nolock(struct budget
*budget
, u32 config
,
266 int addr
, int count
, u32 value
, int nobusyloop
)
268 struct saa7146_dev
*saa
= budget
->dev
;
271 result
= saa7146_wait_for_debi_done(saa
, nobusyloop
);
275 saa7146_write(saa
, DEBI_COMMAND
, (count
<< 17) | 0x00000 | (addr
& 0xffff));
276 saa7146_write(saa
, DEBI_CONFIG
, config
);
277 saa7146_write(saa
, DEBI_PAGE
, 0);
278 saa7146_write(saa
, DEBI_AD
, value
);
279 saa7146_write(saa
, MC2
, (2 << 16) | 2);
281 result
= saa7146_wait_for_debi_done(saa
, nobusyloop
);
282 return result
< 0 ? result
: 0;
285 int ttpci_budget_debiwrite(struct budget
*budget
, u32 config
, int addr
,
286 int count
, u32 value
, int uselocks
, int nobusyloop
)
288 if (count
> 4 || count
<= 0)
295 spin_lock_irqsave(&budget
->debilock
, flags
);
296 result
= ttpci_budget_debiwrite_nolock(budget
, config
, addr
,
297 count
, value
, nobusyloop
);
298 spin_unlock_irqrestore(&budget
->debilock
, flags
);
301 return ttpci_budget_debiwrite_nolock(budget
, config
, addr
,
302 count
, value
, nobusyloop
);
304 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite
);
307 /****************************************************************************
309 ****************************************************************************/
311 static int budget_start_feed(struct dvb_demux_feed
*feed
)
313 struct dvb_demux
*demux
= feed
->demux
;
314 struct budget
*budget
= demux
->priv
;
317 dprintk(2, "budget: %p\n", budget
);
319 if (!demux
->dmx
.frontend
)
322 spin_lock(&budget
->feedlock
);
323 feed
->pusi_seen
= false; /* have a clean section start */
324 if (budget
->feeding
++ == 0)
325 status
= start_ts_capture(budget
);
326 spin_unlock(&budget
->feedlock
);
330 static int budget_stop_feed(struct dvb_demux_feed
*feed
)
332 struct dvb_demux
*demux
= feed
->demux
;
333 struct budget
*budget
= demux
->priv
;
336 dprintk(2, "budget: %p\n", budget
);
338 spin_lock(&budget
->feedlock
);
339 if (--budget
->feeding
== 0)
340 status
= stop_ts_capture(budget
);
341 spin_unlock(&budget
->feedlock
);
345 static int budget_register(struct budget
*budget
)
347 struct dvb_demux
*dvbdemux
= &budget
->demux
;
350 dprintk(2, "budget: %p\n", budget
);
352 dvbdemux
->priv
= (void *) budget
;
354 dvbdemux
->filternum
= 256;
355 dvbdemux
->feednum
= 256;
356 dvbdemux
->start_feed
= budget_start_feed
;
357 dvbdemux
->stop_feed
= budget_stop_feed
;
358 dvbdemux
->write_to_decoder
= NULL
;
360 dvbdemux
->dmx
.capabilities
= (DMX_TS_FILTERING
| DMX_SECTION_FILTERING
|
361 DMX_MEMORY_BASED_FILTERING
);
363 dvb_dmx_init(&budget
->demux
);
365 budget
->dmxdev
.filternum
= 256;
366 budget
->dmxdev
.demux
= &dvbdemux
->dmx
;
367 budget
->dmxdev
.capabilities
= 0;
369 dvb_dmxdev_init(&budget
->dmxdev
, &budget
->dvb_adapter
);
371 budget
->hw_frontend
.source
= DMX_FRONTEND_0
;
373 ret
= dvbdemux
->dmx
.add_frontend(&dvbdemux
->dmx
, &budget
->hw_frontend
);
376 goto err_release_dmx
;
378 budget
->mem_frontend
.source
= DMX_MEMORY_FE
;
379 ret
= dvbdemux
->dmx
.add_frontend(&dvbdemux
->dmx
, &budget
->mem_frontend
);
381 goto err_release_dmx
;
383 ret
= dvbdemux
->dmx
.connect_frontend(&dvbdemux
->dmx
, &budget
->hw_frontend
);
385 goto err_release_dmx
;
387 dvb_net_init(&budget
->dvb_adapter
, &budget
->dvb_net
, &dvbdemux
->dmx
);
392 dvb_dmxdev_release(&budget
->dmxdev
);
393 dvb_dmx_release(&budget
->demux
);
397 static void budget_unregister(struct budget
*budget
)
399 struct dvb_demux
*dvbdemux
= &budget
->demux
;
401 dprintk(2, "budget: %p\n", budget
);
403 dvb_net_release(&budget
->dvb_net
);
405 dvbdemux
->dmx
.close(&dvbdemux
->dmx
);
406 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
, &budget
->hw_frontend
);
407 dvbdemux
->dmx
.remove_frontend(&dvbdemux
->dmx
, &budget
->mem_frontend
);
409 dvb_dmxdev_release(&budget
->dmxdev
);
410 dvb_dmx_release(&budget
->demux
);
413 int ttpci_budget_init(struct budget
*budget
, struct saa7146_dev
*dev
,
414 struct saa7146_pci_extension_data
*info
,
415 struct module
*owner
, short *adapter_nums
)
418 struct budget_info
*bi
= info
->ext_priv
;
422 memset(budget
, 0, sizeof(struct budget
));
424 dprintk(2, "dev: %p, budget: %p\n", dev
, budget
);
427 budget
->dev
= (struct saa7146_dev
*) dev
;
429 switch (budget
->card
->type
) {
430 case BUDGET_FS_ACTIVY
:
431 budget
->buffer_width
= TS_WIDTH_ACTIVY
;
432 max_bufsize
= TS_MAX_BUFSIZE_K_ACTIVY
;
433 height_mask
= TS_HEIGHT_MASK_ACTIVY
;
438 case BUDGET_CIN1200C
:
439 case BUDGET_KNC1C_MK3
:
440 case BUDGET_KNC1C_TDA10024
:
441 case BUDGET_KNC1CP_MK3
:
442 case BUDGET_CIN1200C_MK3
:
443 budget
->buffer_width
= TS_WIDTH_DVBC
;
444 max_bufsize
= TS_MAX_BUFSIZE_K_DVBC
;
445 height_mask
= TS_HEIGHT_MASK_DVBC
;
449 budget
->buffer_width
= TS_WIDTH
;
450 max_bufsize
= TS_MAX_BUFSIZE_K
;
451 height_mask
= TS_HEIGHT_MASK
;
454 if (dma_buffer_size
< TS_MIN_BUFSIZE_K
)
455 dma_buffer_size
= TS_MIN_BUFSIZE_K
;
456 else if (dma_buffer_size
> max_bufsize
)
457 dma_buffer_size
= max_bufsize
;
459 budget
->buffer_height
= dma_buffer_size
* 1024 / budget
->buffer_width
;
460 if (budget
->buffer_height
> 0xfff) {
461 budget
->buffer_height
/= 2;
462 budget
->buffer_height
&= height_mask
;
463 budget
->buffer_size
= 2 * budget
->buffer_height
* budget
->buffer_width
;
465 budget
->buffer_height
&= height_mask
;
466 budget
->buffer_size
= budget
->buffer_height
* budget
->buffer_width
;
468 budget
->buffer_warning_threshold
= budget
->buffer_size
* 80/100;
469 budget
->buffer_warnings
= 0;
470 budget
->buffer_warning_time
= jiffies
;
472 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n",
474 budget
->buffer_size
> budget
->buffer_width
* budget
->buffer_height
? "odd/even" : "single",
475 budget
->buffer_width
, budget
->buffer_height
);
476 pr_info("%s: dma buffer size %u\n", budget
->dev
->name
, budget
->buffer_size
);
478 ret
= dvb_register_adapter(&budget
->dvb_adapter
, budget
->card
->name
,
479 owner
, &budget
->dev
->pci
->dev
, adapter_nums
);
483 /* set dd1 stream a & b */
484 saa7146_write(dev
, DD1_STREAM_B
, 0x00000000);
485 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
));
486 saa7146_write(dev
, MC2
, (MASK_10
| MASK_26
));
487 saa7146_write(dev
, DD1_INIT
, 0x02000000);
488 saa7146_write(dev
, MC2
, (MASK_09
| MASK_25
| MASK_10
| MASK_26
));
490 if (bi
->type
!= BUDGET_FS_ACTIVY
)
491 budget
->video_port
= BUDGET_VIDEO_PORTB
;
493 budget
->video_port
= BUDGET_VIDEO_PORTA
;
494 spin_lock_init(&budget
->feedlock
);
495 spin_lock_init(&budget
->debilock
);
498 * the Siemens DVB needs this if you want to have the i2c chips
499 * get recognized before the main driver is loaded
501 if (bi
->type
!= BUDGET_FS_ACTIVY
)
502 saa7146_write(dev
, GPIO_CTRL
, 0x500000); /* GPIO 3 = 1 */
504 strscpy(budget
->i2c_adap
.name
, budget
->card
->name
,
505 sizeof(budget
->i2c_adap
.name
));
507 saa7146_i2c_adapter_prepare(dev
, &budget
->i2c_adap
, SAA7146_I2C_BUS_BIT_RATE_120
);
508 strscpy(budget
->i2c_adap
.name
, budget
->card
->name
,
509 sizeof(budget
->i2c_adap
.name
));
511 if (i2c_add_adapter(&budget
->i2c_adap
) < 0) {
513 goto err_dvb_unregister
;
516 ttpci_eeprom_parse_mac(&budget
->i2c_adap
, budget
->dvb_adapter
.proposed_mac
);
518 budget
->grabbing
= saa7146_vmalloc_build_pgtable(dev
->pci
, budget
->buffer_size
, &budget
->pt
);
519 if (budget
->grabbing
== NULL
) {
524 saa7146_write(dev
, PCI_BT_V1
, 0x001c0000);
526 saa7146_write(dev
, GPIO_CTRL
, 0x000000);
528 INIT_WORK(&budget
->vpe_bh_work
, vpeirq
);
530 /* frontend power on */
531 if (bi
->type
!= BUDGET_FS_ACTIVY
)
532 saa7146_setgpio(dev
, 2, SAA7146_GPIO_OUTHI
);
534 ret
= budget_register(budget
);
536 return 0; /* Everything OK */
538 /* An error occurred, cleanup resources */
539 saa7146_vfree_destroy_pgtable(dev
->pci
, budget
->grabbing
, &budget
->pt
);
542 i2c_del_adapter(&budget
->i2c_adap
);
545 dvb_unregister_adapter(&budget
->dvb_adapter
);
549 EXPORT_SYMBOL_GPL(ttpci_budget_init
);
551 void ttpci_budget_init_hooks(struct budget
*budget
)
553 if (budget
->dvb_frontend
&& !budget
->read_fe_status
) {
554 budget
->read_fe_status
= budget
->dvb_frontend
->ops
.read_status
;
555 budget
->dvb_frontend
->ops
.read_status
= budget_read_fe_status
;
558 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks
);
560 int ttpci_budget_deinit(struct budget
*budget
)
562 struct saa7146_dev
*dev
= budget
->dev
;
564 dprintk(2, "budget: %p\n", budget
);
566 budget_unregister(budget
);
568 cancel_work_sync(&budget
->vpe_bh_work
);
570 saa7146_vfree_destroy_pgtable(dev
->pci
, budget
->grabbing
, &budget
->pt
);
572 i2c_del_adapter(&budget
->i2c_adap
);
574 dvb_unregister_adapter(&budget
->dvb_adapter
);
578 EXPORT_SYMBOL_GPL(ttpci_budget_deinit
);
580 void ttpci_budget_irq10_handler(struct saa7146_dev
*dev
, u32
*isr
)
582 struct budget
*budget
= dev
->ext_priv
;
584 dprintk(8, "dev: %p, budget: %p\n", dev
, budget
);
587 queue_work(system_bh_wq
, &budget
->vpe_bh_work
);
589 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler
);
591 void ttpci_budget_set_video_port(struct saa7146_dev
*dev
, int video_port
)
593 struct budget
*budget
= dev
->ext_priv
;
595 spin_lock(&budget
->feedlock
);
596 budget
->video_port
= video_port
;
597 if (budget
->feeding
) {
598 stop_ts_capture(budget
);
599 start_ts_capture(budget
);
601 spin_unlock(&budget
->feedlock
);
603 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port
);
605 MODULE_DESCRIPTION("base driver for the SAA7146 based Budget DVB cards");
606 MODULE_LICENSE("GPL");