2 * budget-av.c: driver for the SAA7146 based Budget DVB cards
5 * Compiled from various sources by Michael Hunold <michael@mihu.de>
7 * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
8 * Andrew de Quincey <adq_dvb@lidskialf.net>
10 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
33 * the project's page is at http://www.linuxtv.org/dvb/
41 #include <media/saa7146_vv.h>
42 #include <linux/module.h>
43 #include <linux/errno.h>
44 #include <linux/slab.h>
45 #include <linux/interrupt.h>
46 #include <linux/input.h>
47 #include <linux/spinlock.h>
49 #include "dvb_ca_en50221.h"
51 #define DEBICICAM 0x02420000
55 struct video_device *vd;
58 struct tasklet_struct ciintf_irq_tasklet;
60 struct dvb_ca_en50221 ca;
64 * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
65 * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
66 * 2 - CI Card Enable (Active Low)
70 /****************************************************************************
72 ****************************************************************************/
74 static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
78 struct i2c_msg msgs[2];
81 msgs[1].flags = I2C_M_RD;
82 msgs[0].addr = msgs[1].addr = id / 2;
89 i2c_transfer(i2c, msgs, 2);
94 static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
97 struct i2c_msg msgs[2] = {
98 {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
99 {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
102 if (i2c_transfer(i2c, msgs, 2) != 2)
108 static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
110 u8 msg[2] = { reg, val };
117 return i2c_transfer(i2c, &msgs, 1);
120 static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
122 struct budget_av *budget_av = (struct budget_av *) ca->data;
128 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
131 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
133 if (result == -ETIMEDOUT)
134 budget_av->slot_status = 0;
138 static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
140 struct budget_av *budget_av = (struct budget_av *) ca->data;
146 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
149 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
151 if (result == -ETIMEDOUT)
152 budget_av->slot_status = 0;
156 static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
158 struct budget_av *budget_av = (struct budget_av *) ca->data;
164 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
167 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
169 if (result == -ETIMEDOUT)
170 budget_av->slot_status = 0;
174 static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
176 struct budget_av *budget_av = (struct budget_av *) ca->data;
182 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
185 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
187 if (result == -ETIMEDOUT)
188 budget_av->slot_status = 0;
192 static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
194 struct budget_av *budget_av = (struct budget_av *) ca->data;
195 struct saa7146_dev *saa = budget_av->budget.dev;
196 int timeout = 50; // 5 seconds (4.4.6 Ready)
201 dprintk(1, "ciintf_slot_reset\n");
203 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
205 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
207 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
208 msleep(20); /* 20 ms Vcc settling time */
210 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
212 /* This should have been based on pin 16 READY of the pcmcia port,
213 * but AFAICS it is not routed to the saa7146 */
214 while (--timeout > 0 && ciintf_read_attribute_mem(ca, slot, 0) != 0x1d)
217 /* reinitialise the frontend */
218 dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
222 printk(KERN_ERR "budget-av: cam reset failed (timeout).\n");
223 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
230 static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
232 struct budget_av *budget_av = (struct budget_av *) ca->data;
233 struct saa7146_dev *saa = budget_av->budget.dev;
238 dprintk(1, "ciintf_slot_shutdown\n");
240 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
241 budget_av->slot_status = 0;
245 static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
247 struct budget_av *budget_av = (struct budget_av *) ca->data;
248 struct saa7146_dev *saa = budget_av->budget.dev;
253 dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
255 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
259 static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
261 struct budget_av *budget_av = (struct budget_av *) ca->data;
262 struct saa7146_dev *saa = budget_av->budget.dev;
268 if (!budget_av->slot_status)
270 // first of all test the card detect line
271 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
273 if (saa7146_read(saa, PSR) & MASK_06)
277 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
279 // that is unreliable however, so try and read from IO memory
282 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
283 if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) != -ETIMEDOUT)
289 // did we find something?
291 printk(KERN_INFO "budget-av: cam inserted\n");
292 budget_av->slot_status = 1;
295 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
296 if (ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1) == -ETIMEDOUT)
298 printk(KERN_INFO "budget-av: cam ejected\n");
299 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
300 budget_av->slot_status = 0;
304 if (budget_av->slot_status == 1)
305 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
310 static int ciintf_init(struct budget_av *budget_av)
312 struct saa7146_dev *saa = budget_av->budget.dev;
315 memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
317 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
318 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
319 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
320 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
322 /* Enable DEBI pins */
323 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16) | 0x800);
325 /* register CI interface */
326 budget_av->ca.owner = THIS_MODULE;
327 budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
328 budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
329 budget_av->ca.read_cam_control = ciintf_read_cam_control;
330 budget_av->ca.write_cam_control = ciintf_write_cam_control;
331 budget_av->ca.slot_reset = ciintf_slot_reset;
332 budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
333 budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
334 budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
335 budget_av->ca.data = budget_av;
337 if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
338 &budget_av->ca, 0, 1)) != 0) {
339 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
343 printk(KERN_INFO "budget-av: ci interface initialised.\n");
344 budget_av->budget.ci_present = 1;
348 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
352 static void ciintf_deinit(struct budget_av *budget_av)
354 struct saa7146_dev *saa = budget_av->budget.dev;
356 saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
357 saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
358 saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
359 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
361 /* release the CA device */
362 dvb_ca_en50221_release(&budget_av->ca);
364 /* disable DEBI pins */
365 saa7146_write(saa, MC1, saa7146_read(saa, MC1) | (0x800 << 16));
369 static const u8 saa7113_tab[] = {
390 0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
393 0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
394 0x5b, 0x83, 0x5e, 0x00,
398 static int saa7113_init(struct budget_av *budget_av)
400 struct budget *budget = &budget_av->budget;
401 struct saa7146_dev *saa = budget->dev;
402 const u8 *data = saa7113_tab;
404 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
407 if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
408 dprintk(1, "saa7113 not found on KNC card\n");
412 dprintk(1, "saa7113 detected and initializing\n");
414 while (*data != 0xff) {
415 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
419 dprintk(1, "saa7113 status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
424 static int saa7113_setinput(struct budget_av *budget_av, int input)
426 struct budget *budget = &budget_av->budget;
428 if (1 != budget_av->has_saa7113)
432 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
433 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
434 } else if (input == 0) {
435 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
436 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
440 budget_av->cur_input = input;
445 static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
454 else if (srate < 5000000)
456 else if (srate < 15000000)
458 else if (srate < 45000000)
465 stv0299_writereg(fe, 0x13, aclk);
466 stv0299_writereg(fe, 0x14, bclk);
467 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
468 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
469 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
470 stv0299_writereg(fe, 0x0f, 0x80 | m1);
475 static int philips_su1278_ty_ci_pll_set(struct dvb_frontend *fe,
476 struct i2c_adapter *i2c,
477 struct dvb_frontend_parameters *params)
481 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
483 if ((params->frequency < 950000) || (params->frequency > 2150000))
486 div = (params->frequency + (125 - 1)) / 125; // round correctly
487 buf[0] = (div >> 8) & 0x7f;
489 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
492 if (params->u.qpsk.symbol_rate < 4000000)
495 if (params->frequency < 1250000)
497 else if (params->frequency < 1550000)
499 else if (params->frequency < 2050000)
501 else if (params->frequency < 2150000)
504 if (i2c_transfer(i2c, &msg, 1) != 1)
509 #define MIN2(a,b) ((a) < (b) ? (a) : (b))
510 #define MIN3(a,b,c) MIN2(MIN2(a,b),c)
512 static int philips_su1278sh2_tua6100_pll_set(struct dvb_frontend *fe,
513 struct i2c_adapter *i2c,
514 struct dvb_frontend_parameters *params)
516 u8 reg0 [2] = { 0x00, 0x00 };
517 u8 reg1 [4] = { 0x01, 0x00, 0x00, 0x00 };
518 u8 reg2 [3] = { 0x02, 0x00, 0x00 };
522 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = NULL,.len = 0 };
523 int freq = params->frequency;
525 first_ZF = (freq) / 1000;
527 if (abs(MIN2(abs(first_ZF-1190),abs(first_ZF-1790))) <
528 abs(MIN3(abs(first_ZF-1202),abs(first_ZF-1542),abs(first_ZF-1890))))
534 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
535 ((first_ZF >= 1430) && (first_ZF < 1950)))
537 else if (((first_ZF >= 1350) && (first_ZF < 1430)) ||
538 ((first_ZF >= 1950) && (first_ZF < 2150)))
543 if (((first_ZF >= 950) && (first_ZF < 1350)) ||
544 ((first_ZF >= 1455) && (first_ZF < 1950)))
546 else if (((first_ZF >= 1350) && (first_ZF < 1420)) ||
547 ((first_ZF >= 1950) && (first_ZF < 2150)))
549 else if ((first_ZF >= 1420) && (first_ZF < 1455))
559 if (first_ZF > 1430) { /* 1430MHZ */
560 reg1[1] &= 0xCF; /* N2 */
561 reg2[1] &= 0xCF; /* R2 */
564 reg1[1] &= 0xCF; /* N2 */
566 reg2[1] &= 0xCF; /* R2 */
572 if ((first_ZF >= 1455) &&
574 reg1[1] &= 0xCF; /* N2 */
576 reg2[1] &= 0xCF; /* R2 */
578 if (first_ZF < 1455) {
579 reg1[1] &= 0xCF; /* N2 */
581 reg2[1] &= 0xCF; /* R2 */
584 if (first_ZF >= 1630) {
585 reg1[1] &= 0xCF; /* N2 */
586 reg2[1] &= 0xCF; /* R2 */
593 /* set ports, enable P0 for symbol rates > 4Ms/s */
594 if (params->u.qpsk.symbol_rate >= 4000000)
605 M = (freq * R) / 4; /* in Mhz */
606 N = (M - A * 1000) / (P * 1000);
608 reg1[1] |= (N >> 9) & 0x03;
609 reg1[2] = (N >> 1) & 0xff;
610 reg1[3] = (N << 7) & 0x80;
612 reg2[1] |= (R >> 8) & 0x03;
613 reg2[2] = R & 0xFF; /* R */
615 reg1[3] |= A & 0x7f; /* A */
618 reg1[1] |= 0x40; /* Prescaler 64/65 */
622 /* already enabled - do not reenable i2c repeater or TX fails */
624 msg.len = sizeof(reg0);
625 if (i2c_transfer(i2c, &msg, 1) != 1)
628 stv0299_enable_plli2c(fe);
630 msg.len = sizeof(reg1);
631 if (i2c_transfer(i2c, &msg, 1) != 1)
634 stv0299_enable_plli2c(fe);
636 msg.len = sizeof(reg2);
637 if (i2c_transfer(i2c, &msg, 1) != 1)
643 static u8 typhoon_cinergy1200s_inittab[] = {
647 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
648 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
649 0x06, 0x40, /* DAC not used, set to high impendance mode */
650 0x07, 0x00, /* DAC LSB */
651 0x08, 0x40, /* DiSEqC off */
652 0x09, 0x00, /* FIFO */
653 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
654 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
655 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
656 0x10, 0x3f, // AGC2 0x3d
659 0x15, 0xc9, // lock detector threshold
670 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
671 0x29, 0x1e, // 1/2 threshold
672 0x2a, 0x14, // 2/3 threshold
673 0x2b, 0x0f, // 3/4 threshold
674 0x2c, 0x09, // 5/6 threshold
675 0x2d, 0x05, // 7/8 threshold
677 0x31, 0x1f, // test all FECs
678 0x32, 0x19, // viterbi and synchro search
679 0x33, 0xfc, // rs control
680 0x34, 0x93, // error control
685 static struct stv0299_config typhoon_config = {
686 .demod_address = 0x68,
687 .inittab = typhoon_cinergy1200s_inittab,
691 .lock_output = STV0229_LOCKOUTPUT_1,
692 .volt13_op0_op1 = STV0299_VOLT13_OP0,
694 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
695 .pll_set = philips_su1278_ty_ci_pll_set,
699 static struct stv0299_config cinergy_1200s_config = {
700 .demod_address = 0x68,
701 .inittab = typhoon_cinergy1200s_inittab,
705 .lock_output = STV0229_LOCKOUTPUT_0,
706 .volt13_op0_op1 = STV0299_VOLT13_OP0,
708 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
709 .pll_set = philips_su1278_ty_ci_pll_set,
712 static struct stv0299_config cinergy_1200s_1894_0010_config = {
713 .demod_address = 0x68,
714 .inittab = typhoon_cinergy1200s_inittab,
718 .lock_output = STV0229_LOCKOUTPUT_1,
719 .volt13_op0_op1 = STV0299_VOLT13_OP0,
721 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
722 .pll_set = philips_su1278sh2_tua6100_pll_set,
725 static int philips_cu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
727 struct budget *budget = (struct budget *) fe->dvb->priv;
729 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
731 #define TUNER_MUL 62500
733 u32 div = (params->frequency + 36125000 + TUNER_MUL / 2) / TUNER_MUL;
735 buf[0] = (div >> 8) & 0x7f;
738 buf[3] = (params->frequency < 150000000 ? 0x01 :
739 params->frequency < 445000000 ? 0x02 : 0x04);
741 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
746 static struct tda10021_config philips_cu1216_config = {
747 .demod_address = 0x0c,
748 .pll_set = philips_cu1216_pll_set,
754 static int philips_tu1216_pll_init(struct dvb_frontend *fe)
756 struct budget *budget = (struct budget *) fe->dvb->priv;
757 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
758 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
760 // setup PLL configuration
761 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
768 static int philips_tu1216_pll_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
770 struct budget *budget = (struct budget *) fe->dvb->priv;
772 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
774 int tuner_frequency = 0;
777 // determine charge pump
778 tuner_frequency = params->frequency + 36166000;
779 if (tuner_frequency < 87000000)
781 else if (tuner_frequency < 130000000)
783 else if (tuner_frequency < 160000000)
785 else if (tuner_frequency < 200000000)
787 else if (tuner_frequency < 290000000)
789 else if (tuner_frequency < 420000000)
791 else if (tuner_frequency < 480000000)
793 else if (tuner_frequency < 620000000)
795 else if (tuner_frequency < 830000000)
797 else if (tuner_frequency < 895000000)
803 if (params->frequency < 49000000)
805 else if (params->frequency < 161000000)
807 else if (params->frequency < 444000000)
809 else if (params->frequency < 861000000)
815 switch (params->u.ofdm.bandwidth) {
816 case BANDWIDTH_6_MHZ:
820 case BANDWIDTH_7_MHZ:
824 case BANDWIDTH_8_MHZ:
833 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
834 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
836 // setup tuner buffer
837 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
838 tuner_buf[1] = tuner_frequency & 0xff;
840 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
842 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
849 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
850 const struct firmware **fw, char *name)
852 struct budget *budget = (struct budget *) fe->dvb->priv;
854 return request_firmware(fw, name, &budget->dev->pci->dev);
857 static struct tda1004x_config philips_tu1216_config = {
859 .demod_address = 0x8,
862 .xtal_freq = TDA10046_XTAL_4M,
863 .agc_config = TDA10046_AGC_DEFAULT,
864 .if_freq = TDA10046_FREQ_3617,
865 .pll_init = philips_tu1216_pll_init,
866 .pll_set = philips_tu1216_pll_set,
868 .request_firmware = philips_tu1216_request_firmware,
871 static u8 philips_sd1878_inittab[] = {
914 static int philips_sd1878_tda8261_pll_set(struct dvb_frontend *fe,
915 struct i2c_adapter *i2c,
916 struct dvb_frontend_parameters *params)
920 struct i2c_msg tuner_msg = {.addr=0x60,.flags=0,.buf=buf,.len=sizeof(buf)};
922 if((params->frequency < 950000) || (params->frequency > 2150000))
925 rc=dvb_pll_configure(&dvb_pll_philips_sd1878_tda8261, buf,
926 params->frequency, 0);
927 if(rc < 0) return rc;
929 if(i2c_transfer(i2c, &tuner_msg, 1) != 1)
935 static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
936 u32 srate, u32 ratio)
945 else if (srate < 5000000)
947 else if (srate < 15000000)
949 else if (srate < 45000000)
956 stv0299_writereg(fe, 0x0e, 0x23);
957 stv0299_writereg(fe, 0x0f, 0x94);
958 stv0299_writereg(fe, 0x10, 0x39);
959 stv0299_writereg(fe, 0x13, aclk);
960 stv0299_writereg(fe, 0x14, bclk);
961 stv0299_writereg(fe, 0x15, 0xc9);
962 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
963 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
964 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
965 stv0299_writereg(fe, 0x0f, 0x80 | m1);
970 static struct stv0299_config philips_sd1878_config = {
971 .demod_address = 0x68,
972 .inittab = philips_sd1878_inittab,
976 .lock_output = STV0229_LOCKOUTPUT_1,
977 .volt13_op0_op1 = STV0299_VOLT13_OP0,
979 .set_symbol_rate = philips_sd1878_ci_set_symbol_rate,
980 .pll_set = philips_sd1878_tda8261_pll_set,
983 static u8 read_pwm(struct budget_av *budget_av)
987 struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
988 {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
991 if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
998 #define SUBID_DVBS_KNC1 0x0010
999 #define SUBID_DVBS_KNC1_PLUS 0x0011
1000 #define SUBID_DVBS_TYPHOON 0x4f56
1001 #define SUBID_DVBS_CINERGY1200 0x1154
1002 #define SUBID_DVBS_CYNERGY1200N 0x1155
1004 #define SUBID_DVBS_TV_STAR 0x0014
1005 #define SUBID_DVBS_TV_STAR_CI 0x0016
1006 #define SUBID_DVBS_EASYWATCH 0x001e
1007 #define SUBID_DVBC_KNC1 0x0020
1008 #define SUBID_DVBC_KNC1_PLUS 0x0021
1009 #define SUBID_DVBC_CINERGY1200 0x1156
1011 #define SUBID_DVBT_KNC1_PLUS 0x0031
1012 #define SUBID_DVBT_KNC1 0x0030
1013 #define SUBID_DVBT_CINERGY1200 0x1157
1015 static void frontend_init(struct budget_av *budget_av)
1017 struct saa7146_dev * saa = budget_av->budget.dev;
1018 struct dvb_frontend * fe = NULL;
1020 switch (saa->pci->subsystem_device) {
1021 case SUBID_DVBS_KNC1_PLUS:
1022 case SUBID_DVBC_KNC1_PLUS:
1023 case SUBID_DVBT_KNC1_PLUS:
1024 // Enable / PowerON Frontend
1025 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
1026 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
1030 switch (saa->pci->subsystem_device) {
1032 case SUBID_DVBS_KNC1:
1033 if (saa->pci->subsystem_vendor == 0x1894) {
1034 fe = stv0299_attach(&cinergy_1200s_1894_0010_config,
1035 &budget_av->budget.i2c_adap);
1037 fe = stv0299_attach(&typhoon_config,
1038 &budget_av->budget.i2c_adap);
1042 case SUBID_DVBS_TV_STAR:
1043 case SUBID_DVBS_TV_STAR_CI:
1044 case SUBID_DVBS_CYNERGY1200N:
1045 case SUBID_DVBS_EASYWATCH:
1046 fe = stv0299_attach(&philips_sd1878_config,
1047 &budget_av->budget.i2c_adap);
1050 case SUBID_DVBS_KNC1_PLUS:
1051 case SUBID_DVBS_TYPHOON:
1052 fe = stv0299_attach(&typhoon_config,
1053 &budget_av->budget.i2c_adap);
1056 case SUBID_DVBS_CINERGY1200:
1057 fe = stv0299_attach(&cinergy_1200s_config,
1058 &budget_av->budget.i2c_adap);
1061 case SUBID_DVBC_KNC1:
1062 case SUBID_DVBC_KNC1_PLUS:
1063 fe = tda10021_attach(&philips_cu1216_config,
1064 &budget_av->budget.i2c_adap,
1065 read_pwm(budget_av));
1068 case SUBID_DVBT_KNC1:
1069 case SUBID_DVBT_KNC1_PLUS:
1070 fe = tda10046_attach(&philips_tu1216_config,
1071 &budget_av->budget.i2c_adap);
1074 case SUBID_DVBC_CINERGY1200:
1075 fe = tda10021_attach(&philips_cu1216_config,
1076 &budget_av->budget.i2c_adap,
1077 read_pwm(budget_av));
1080 case SUBID_DVBT_CINERGY1200:
1081 fe = tda10046_attach(&philips_tu1216_config,
1082 &budget_av->budget.i2c_adap);
1087 printk(KERN_ERR "budget-av: A frontend driver was not found "
1088 "for device %04x/%04x subsystem %04x/%04x\n",
1091 saa->pci->subsystem_vendor,
1092 saa->pci->subsystem_device);
1096 budget_av->budget.dvb_frontend = fe;
1098 if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
1099 budget_av->budget.dvb_frontend)) {
1100 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
1101 if (budget_av->budget.dvb_frontend->ops->release)
1102 budget_av->budget.dvb_frontend->ops->release(budget_av->budget.dvb_frontend);
1103 budget_av->budget.dvb_frontend = NULL;
1108 static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
1110 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1112 dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
1115 ttpci_budget_irq10_handler(dev, isr);
1118 static int budget_av_detach(struct saa7146_dev *dev)
1120 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1123 dprintk(2, "dev: %p\n", dev);
1125 if (1 == budget_av->has_saa7113) {
1126 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
1130 saa7146_unregister_device(&budget_av->vd, dev);
1133 if (budget_av->budget.ci_present)
1134 ciintf_deinit(budget_av);
1136 if (budget_av->budget.dvb_frontend != NULL)
1137 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
1138 err = ttpci_budget_deinit(&budget_av->budget);
1145 static struct saa7146_ext_vv vv_data;
1147 static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1149 struct budget_av *budget_av;
1153 dprintk(2, "dev: %p\n", dev);
1155 if (!(budget_av = kzalloc(sizeof(struct budget_av), GFP_KERNEL)))
1158 budget_av->has_saa7113 = 0;
1159 budget_av->budget.ci_present = 0;
1161 dev->ext_priv = budget_av;
1163 if ((err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE))) {
1168 /* knc1 initialization */
1169 saa7146_write(dev, DD1_STREAM_B, 0x04000000);
1170 saa7146_write(dev, DD1_INIT, 0x07000600);
1171 saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
1173 if (saa7113_init(budget_av) == 0) {
1174 budget_av->has_saa7113 = 1;
1176 if (0 != saa7146_vv_init(dev, &vv_data)) {
1177 /* fixme: proper cleanup here */
1178 ERR(("cannot init vv subsystem.\n"));
1182 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
1183 /* fixme: proper cleanup here */
1184 ERR(("cannot register capture v4l2 device.\n"));
1188 /* beware: this modifies dev->vv ... */
1189 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
1190 SAA7146_HPS_SYNC_PORT_A);
1192 saa7113_setinput(budget_av, 0);
1195 /* fixme: find some sane values here... */
1196 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
1198 mac = budget_av->budget.dvb_adapter.proposed_mac;
1199 if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
1200 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
1201 budget_av->budget.dvb_adapter.num);
1204 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
1205 budget_av->budget.dvb_adapter.num,
1206 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1209 budget_av->budget.dvb_adapter.priv = budget_av;
1210 frontend_init(budget_av);
1212 if (!budget_av->has_saa7113) {
1213 ciintf_init(budget_av);
1219 #define KNC1_INPUTS 2
1220 static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
1221 {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1222 {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1225 static struct saa7146_extension_ioctls ioctls[] = {
1226 {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
1227 {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
1228 {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
1232 static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
1234 struct saa7146_dev *dev = fh->dev;
1235 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1238 case VIDIOC_ENUMINPUT:{
1239 struct v4l2_input *i = arg;
1241 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
1242 if (i->index < 0 || i->index >= KNC1_INPUTS) {
1245 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
1248 case VIDIOC_G_INPUT:{
1249 int *input = (int *) arg;
1251 *input = budget_av->cur_input;
1253 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
1256 case VIDIOC_S_INPUT:{
1257 int input = *(int *) arg;
1258 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
1259 return saa7113_setinput(budget_av, input);
1262 return -ENOIOCTLCMD;
1267 static struct saa7146_standard standard[] = {
1268 {.name = "PAL",.id = V4L2_STD_PAL,
1269 .v_offset = 0x17,.v_field = 288,
1270 .h_offset = 0x14,.h_pixels = 680,
1271 .v_max_out = 576,.h_max_out = 768 },
1273 {.name = "NTSC",.id = V4L2_STD_NTSC,
1274 .v_offset = 0x16,.v_field = 240,
1275 .h_offset = 0x06,.h_pixels = 708,
1276 .v_max_out = 480,.h_max_out = 640, },
1279 static struct saa7146_ext_vv vv_data = {
1281 .capabilities = 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1283 .stds = &standard[0],
1284 .num_stds = sizeof(standard) / sizeof(struct saa7146_standard),
1285 .ioctls = &ioctls[0],
1289 static struct saa7146_extension budget_extension;
1291 MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1292 MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1293 MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
1294 MAKE_BUDGET_INFO(kncxs, "KNC TV STAR DVB-S", BUDGET_TVSTAR);
1295 MAKE_BUDGET_INFO(satewpls, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR);
1296 MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1297 MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1298 MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
1299 MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1300 MAKE_BUDGET_INFO(cin1200sn, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1301 MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1302 MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1304 static struct pci_device_id pci_tbl[] = {
1305 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
1306 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1307 MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
1308 MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
1309 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0014),
1310 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0016),
1311 MAKE_EXTENSION_PCI(satewpls, 0x1894, 0x001e),
1312 MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
1313 MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
1314 MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
1315 MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
1316 MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1317 MAKE_EXTENSION_PCI(cin1200sn, 0x153b, 0x1155),
1318 MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1319 MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1325 MODULE_DEVICE_TABLE(pci, pci_tbl);
1327 static struct saa7146_extension budget_extension = {
1328 .name = "budget_av",
1329 .flags = SAA7146_I2C_SHORT_DELAY,
1333 .module = THIS_MODULE,
1334 .attach = budget_av_attach,
1335 .detach = budget_av_detach,
1337 .irq_mask = MASK_10,
1338 .irq_func = budget_av_irq,
1341 static int __init budget_av_init(void)
1343 return saa7146_register_extension(&budget_extension);
1346 static void __exit budget_av_exit(void)
1348 saa7146_unregister_extension(&budget_extension);
1351 module_init(budget_av_init);
1352 module_exit(budget_av_exit);
1354 MODULE_LICENSE("GPL");
1355 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1356 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1357 "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");