treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / pci / ttpci / budget-core.c
blobfadbdeeb449554211d3564d93f304c436d11b8cc
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * budget-core.c: 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
21 #include "budget.h"
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)
36 int budget_debug;
37 static int dma_buffer_size = TS_MIN_BUFSIZE_K;
38 module_param_named(debug, budget_debug, int, 0644);
39 module_param_named(bufsize, dma_buffer_size, int, 0444);
40 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
41 MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
43 /****************************************************************************
44 * TT budget / WinTV Nova
45 ****************************************************************************/
47 static int stop_ts_capture(struct budget *budget)
49 dprintk(2, "budget: %p\n", budget);
51 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
52 SAA7146_IER_DISABLE(budget->dev, MASK_10);
53 return 0;
56 static int start_ts_capture(struct budget *budget)
58 struct saa7146_dev *dev = budget->dev;
60 dprintk(2, "budget: %p\n", budget);
62 if (!budget->feeding || !budget->fe_synced)
63 return 0;
65 saa7146_write(dev, MC1, MASK_20); // DMA3 off
67 memset(budget->grabbing, 0x00, budget->buffer_size);
69 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
71 budget->ttbp = 0;
74 * Signal path on the Activy:
76 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
78 * Since the tuner feeds 204 bytes packets into the SAA7146,
79 * DMA3 is configured to strip the trailing 16 FEC bytes:
80 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
83 switch(budget->card->type) {
84 case BUDGET_FS_ACTIVY:
85 saa7146_write(dev, DD1_INIT, 0x04000000);
86 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
87 saa7146_write(dev, BRS_CTRL, 0x00000000);
88 break;
89 case BUDGET_PATCH:
90 saa7146_write(dev, DD1_INIT, 0x00000200);
91 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
92 saa7146_write(dev, BRS_CTRL, 0x60000000);
93 break;
94 case BUDGET_CIN1200C_MK3:
95 case BUDGET_KNC1C_MK3:
96 case BUDGET_KNC1C_TDA10024:
97 case BUDGET_KNC1CP_MK3:
98 if (budget->video_port == BUDGET_VIDEO_PORTA) {
99 saa7146_write(dev, DD1_INIT, 0x06000200);
100 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
101 saa7146_write(dev, BRS_CTRL, 0x00000000);
102 } else {
103 saa7146_write(dev, DD1_INIT, 0x00000600);
104 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
105 saa7146_write(dev, BRS_CTRL, 0x60000000);
107 break;
108 default:
109 if (budget->video_port == BUDGET_VIDEO_PORTA) {
110 saa7146_write(dev, DD1_INIT, 0x06000200);
111 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
112 saa7146_write(dev, BRS_CTRL, 0x00000000);
113 } else {
114 saa7146_write(dev, DD1_INIT, 0x02000600);
115 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
116 saa7146_write(dev, BRS_CTRL, 0x60000000);
120 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
121 mdelay(10);
123 saa7146_write(dev, BASE_ODD3, 0);
124 if (budget->buffer_size > budget->buffer_height * budget->buffer_width) {
125 // using odd/even buffers
126 saa7146_write(dev, BASE_EVEN3, budget->buffer_height * budget->buffer_width);
127 } else {
128 // using a single buffer
129 saa7146_write(dev, BASE_EVEN3, 0);
131 saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
132 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
134 saa7146_write(dev, PITCH3, budget->buffer_width);
135 saa7146_write(dev, NUM_LINE_BYTE3,
136 (budget->buffer_height << 16) | budget->buffer_width);
138 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
140 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
141 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
142 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
144 return 0;
147 static int budget_read_fe_status(struct dvb_frontend *fe,
148 enum fe_status *status)
150 struct budget *budget = (struct budget *) fe->dvb->priv;
151 int synced;
152 int ret;
154 if (budget->read_fe_status)
155 ret = budget->read_fe_status(fe, status);
156 else
157 ret = -EINVAL;
159 if (!ret) {
160 synced = (*status & FE_HAS_LOCK);
161 if (synced != budget->fe_synced) {
162 budget->fe_synced = synced;
163 spin_lock(&budget->feedlock);
164 if (synced)
165 start_ts_capture(budget);
166 else
167 stop_ts_capture(budget);
168 spin_unlock(&budget->feedlock);
171 return ret;
174 static void vpeirq(unsigned long data)
176 struct budget *budget = (struct budget *) data;
177 u8 *mem = (u8 *) (budget->grabbing);
178 u32 olddma = budget->ttbp;
179 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
180 u32 count;
182 /* Ensure streamed PCI data is synced to CPU */
183 pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
185 /* nearest lower position divisible by 188 */
186 newdma -= newdma % 188;
188 if (newdma >= budget->buffer_size)
189 return;
191 budget->ttbp = newdma;
193 if (budget->feeding == 0 || newdma == olddma)
194 return;
196 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
197 count = newdma - olddma;
198 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
199 } else { /* wraparound, dump olddma..buflen and 0..newdma */
200 count = budget->buffer_size - olddma;
201 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
202 count += newdma;
203 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
206 if (count > budget->buffer_warning_threshold)
207 budget->buffer_warnings++;
209 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
210 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
211 budget->dev->name, __func__, budget->buffer_warnings, count);
212 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
213 budget->buffer_warnings = 0;
218 static int ttpci_budget_debiread_nolock(struct budget *budget, u32 config,
219 int addr, int count, int nobusyloop)
221 struct saa7146_dev *saa = budget->dev;
222 int result;
224 result = saa7146_wait_for_debi_done(saa, nobusyloop);
225 if (result < 0)
226 return result;
228 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
229 saa7146_write(saa, DEBI_CONFIG, config);
230 saa7146_write(saa, DEBI_PAGE, 0);
231 saa7146_write(saa, MC2, (2 << 16) | 2);
233 result = saa7146_wait_for_debi_done(saa, nobusyloop);
234 if (result < 0)
235 return result;
237 result = saa7146_read(saa, DEBI_AD);
238 result &= (0xffffffffUL >> ((4 - count) * 8));
239 return result;
242 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
243 int uselocks, int nobusyloop)
245 if (count > 4 || count <= 0)
246 return 0;
248 if (uselocks) {
249 unsigned long flags;
250 int result;
252 spin_lock_irqsave(&budget->debilock, flags);
253 result = ttpci_budget_debiread_nolock(budget, config, addr,
254 count, nobusyloop);
255 spin_unlock_irqrestore(&budget->debilock, flags);
256 return result;
258 return ttpci_budget_debiread_nolock(budget, config, addr,
259 count, nobusyloop);
262 static int ttpci_budget_debiwrite_nolock(struct budget *budget, u32 config,
263 int addr, int count, u32 value, int nobusyloop)
265 struct saa7146_dev *saa = budget->dev;
266 int result;
268 result = saa7146_wait_for_debi_done(saa, nobusyloop);
269 if (result < 0)
270 return result;
272 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
273 saa7146_write(saa, DEBI_CONFIG, config);
274 saa7146_write(saa, DEBI_PAGE, 0);
275 saa7146_write(saa, DEBI_AD, value);
276 saa7146_write(saa, MC2, (2 << 16) | 2);
278 result = saa7146_wait_for_debi_done(saa, nobusyloop);
279 return result < 0 ? result : 0;
282 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
283 int count, u32 value, int uselocks, int nobusyloop)
285 if (count > 4 || count <= 0)
286 return 0;
288 if (uselocks) {
289 unsigned long flags;
290 int result;
292 spin_lock_irqsave(&budget->debilock, flags);
293 result = ttpci_budget_debiwrite_nolock(budget, config, addr,
294 count, value, nobusyloop);
295 spin_unlock_irqrestore(&budget->debilock, flags);
296 return result;
298 return ttpci_budget_debiwrite_nolock(budget, config, addr,
299 count, value, nobusyloop);
303 /****************************************************************************
304 * DVB API SECTION
305 ****************************************************************************/
307 static int budget_start_feed(struct dvb_demux_feed *feed)
309 struct dvb_demux *demux = feed->demux;
310 struct budget *budget = (struct budget *) demux->priv;
311 int status = 0;
313 dprintk(2, "budget: %p\n", budget);
315 if (!demux->dmx.frontend)
316 return -EINVAL;
318 spin_lock(&budget->feedlock);
319 feed->pusi_seen = false; /* have a clean section start */
320 if (budget->feeding++ == 0)
321 status = start_ts_capture(budget);
322 spin_unlock(&budget->feedlock);
323 return status;
326 static int budget_stop_feed(struct dvb_demux_feed *feed)
328 struct dvb_demux *demux = feed->demux;
329 struct budget *budget = (struct budget *) demux->priv;
330 int status = 0;
332 dprintk(2, "budget: %p\n", budget);
334 spin_lock(&budget->feedlock);
335 if (--budget->feeding == 0)
336 status = stop_ts_capture(budget);
337 spin_unlock(&budget->feedlock);
338 return status;
341 static int budget_register(struct budget *budget)
343 struct dvb_demux *dvbdemux = &budget->demux;
344 int ret;
346 dprintk(2, "budget: %p\n", budget);
348 dvbdemux->priv = (void *) budget;
350 dvbdemux->filternum = 256;
351 dvbdemux->feednum = 256;
352 dvbdemux->start_feed = budget_start_feed;
353 dvbdemux->stop_feed = budget_stop_feed;
354 dvbdemux->write_to_decoder = NULL;
356 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
357 DMX_MEMORY_BASED_FILTERING);
359 dvb_dmx_init(&budget->demux);
361 budget->dmxdev.filternum = 256;
362 budget->dmxdev.demux = &dvbdemux->dmx;
363 budget->dmxdev.capabilities = 0;
365 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
367 budget->hw_frontend.source = DMX_FRONTEND_0;
369 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
371 if (ret < 0)
372 return ret;
374 budget->mem_frontend.source = DMX_MEMORY_FE;
375 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
376 if (ret < 0)
377 return ret;
379 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
380 if (ret < 0)
381 return ret;
383 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
385 return 0;
388 static void budget_unregister(struct budget *budget)
390 struct dvb_demux *dvbdemux = &budget->demux;
392 dprintk(2, "budget: %p\n", budget);
394 dvb_net_release(&budget->dvb_net);
396 dvbdemux->dmx.close(&dvbdemux->dmx);
397 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
398 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
400 dvb_dmxdev_release(&budget->dmxdev);
401 dvb_dmx_release(&budget->demux);
404 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
405 struct saa7146_pci_extension_data *info,
406 struct module *owner, short *adapter_nums)
408 int ret = 0;
409 struct budget_info *bi = info->ext_priv;
410 int max_bufsize;
411 int height_mask;
413 memset(budget, 0, sizeof(struct budget));
415 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
417 budget->card = bi;
418 budget->dev = (struct saa7146_dev *) dev;
420 switch(budget->card->type) {
421 case BUDGET_FS_ACTIVY:
422 budget->buffer_width = TS_WIDTH_ACTIVY;
423 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
424 height_mask = TS_HEIGHT_MASK_ACTIVY;
425 break;
427 case BUDGET_KNC1C:
428 case BUDGET_KNC1CP:
429 case BUDGET_CIN1200C:
430 case BUDGET_KNC1C_MK3:
431 case BUDGET_KNC1C_TDA10024:
432 case BUDGET_KNC1CP_MK3:
433 case BUDGET_CIN1200C_MK3:
434 budget->buffer_width = TS_WIDTH_DVBC;
435 max_bufsize = TS_MAX_BUFSIZE_K_DVBC;
436 height_mask = TS_HEIGHT_MASK_DVBC;
437 break;
439 default:
440 budget->buffer_width = TS_WIDTH;
441 max_bufsize = TS_MAX_BUFSIZE_K;
442 height_mask = TS_HEIGHT_MASK;
445 if (dma_buffer_size < TS_MIN_BUFSIZE_K)
446 dma_buffer_size = TS_MIN_BUFSIZE_K;
447 else if (dma_buffer_size > max_bufsize)
448 dma_buffer_size = max_bufsize;
450 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
451 if (budget->buffer_height > 0xfff) {
452 budget->buffer_height /= 2;
453 budget->buffer_height &= height_mask;
454 budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width;
455 } else {
456 budget->buffer_height &= height_mask;
457 budget->buffer_size = budget->buffer_height * budget->buffer_width;
459 budget->buffer_warning_threshold = budget->buffer_size * 80/100;
460 budget->buffer_warnings = 0;
461 budget->buffer_warning_time = jiffies;
463 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n",
464 budget->dev->name,
465 budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single",
466 budget->buffer_width, budget->buffer_height);
467 printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
469 ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name,
470 owner, &budget->dev->pci->dev, adapter_nums);
471 if (ret < 0)
472 return ret;
474 /* set dd1 stream a & b */
475 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
476 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
477 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
478 saa7146_write(dev, DD1_INIT, 0x02000000);
479 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
481 if (bi->type != BUDGET_FS_ACTIVY)
482 budget->video_port = BUDGET_VIDEO_PORTB;
483 else
484 budget->video_port = BUDGET_VIDEO_PORTA;
485 spin_lock_init(&budget->feedlock);
486 spin_lock_init(&budget->debilock);
488 /* the Siemens DVB needs this if you want to have the i2c chips
489 get recognized before the main driver is loaded */
490 if (bi->type != BUDGET_FS_ACTIVY)
491 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
493 strscpy(budget->i2c_adap.name, budget->card->name,
494 sizeof(budget->i2c_adap.name));
496 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
497 strscpy(budget->i2c_adap.name, budget->card->name,
498 sizeof(budget->i2c_adap.name));
500 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
501 ret = -ENOMEM;
502 goto err_dvb_unregister;
505 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
507 budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt);
508 if (NULL == budget->grabbing) {
509 ret = -ENOMEM;
510 goto err_del_i2c;
513 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
514 /* upload all */
515 saa7146_write(dev, GPIO_CTRL, 0x000000);
517 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
519 /* frontend power on */
520 if (bi->type != BUDGET_FS_ACTIVY)
521 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
523 if ((ret = budget_register(budget)) == 0)
524 return 0; /* Everything OK */
526 /* An error occurred, cleanup resources */
527 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
529 err_del_i2c:
530 i2c_del_adapter(&budget->i2c_adap);
532 err_dvb_unregister:
533 dvb_unregister_adapter(&budget->dvb_adapter);
535 return ret;
538 void ttpci_budget_init_hooks(struct budget *budget)
540 if (budget->dvb_frontend && !budget->read_fe_status) {
541 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
542 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
546 int ttpci_budget_deinit(struct budget *budget)
548 struct saa7146_dev *dev = budget->dev;
550 dprintk(2, "budget: %p\n", budget);
552 budget_unregister(budget);
554 tasklet_kill(&budget->vpe_tasklet);
556 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
558 i2c_del_adapter(&budget->i2c_adap);
560 dvb_unregister_adapter(&budget->dvb_adapter);
562 return 0;
565 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
567 struct budget *budget = (struct budget *) dev->ext_priv;
569 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
571 if (*isr & MASK_10)
572 tasklet_schedule(&budget->vpe_tasklet);
575 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
577 struct budget *budget = (struct budget *) dev->ext_priv;
579 spin_lock(&budget->feedlock);
580 budget->video_port = video_port;
581 if (budget->feeding) {
582 stop_ts_capture(budget);
583 start_ts_capture(budget);
585 spin_unlock(&budget->feedlock);
588 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
589 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
590 EXPORT_SYMBOL_GPL(ttpci_budget_init);
591 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
592 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
593 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
594 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
595 EXPORT_SYMBOL_GPL(budget_debug);
597 MODULE_LICENSE("GPL");