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/
42 #include <media/saa7146_vv.h>
43 #include <linux/module.h>
44 #include <linux/errno.h>
45 #include <linux/slab.h>
46 #include <linux/interrupt.h>
47 #include <linux/input.h>
48 #include <linux/spinlock.h>
50 #include "dvb_ca_en50221.h"
52 #define DEBICICAM 0x02420000
54 #define SLOTSTATUS_NONE 1
55 #define SLOTSTATUS_PRESENT 2
56 #define SLOTSTATUS_RESET 4
57 #define SLOTSTATUS_READY 8
58 #define SLOTSTATUS_OCCUPIED (SLOTSTATUS_PRESENT|SLOTSTATUS_RESET|SLOTSTATUS_READY)
60 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
64 struct video_device *vd;
67 struct tasklet_struct ciintf_irq_tasklet;
69 struct dvb_ca_en50221 ca;
70 u8 reinitialise_demod:1;
73 static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot);
77 * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
78 * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
79 * 2 - CI Card Enable (Active Low)
83 /****************************************************************************
85 ****************************************************************************/
87 static u8 i2c_readreg(struct i2c_adapter *i2c, u8 id, u8 reg)
91 struct i2c_msg msgs[2];
94 msgs[1].flags = I2C_M_RD;
95 msgs[0].addr = msgs[1].addr = id / 2;
102 i2c_transfer(i2c, msgs, 2);
107 static int i2c_readregs(struct i2c_adapter *i2c, u8 id, u8 reg, u8 * buf, u8 len)
110 struct i2c_msg msgs[2] = {
111 {.addr = id / 2,.flags = 0,.buf = mm1,.len = 1},
112 {.addr = id / 2,.flags = I2C_M_RD,.buf = buf,.len = len}
115 if (i2c_transfer(i2c, msgs, 2) != 2)
121 static int i2c_writereg(struct i2c_adapter *i2c, u8 id, u8 reg, u8 val)
123 u8 msg[2] = { reg, val };
130 return i2c_transfer(i2c, &msgs, 1);
133 static int ciintf_read_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address)
135 struct budget_av *budget_av = (struct budget_av *) ca->data;
141 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
144 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 0xfff, 1, 0, 1);
145 if (result == -ETIMEDOUT) {
146 ciintf_slot_shutdown(ca, slot);
147 printk(KERN_INFO "budget-av: cam ejected 1\n");
152 static int ciintf_write_attribute_mem(struct dvb_ca_en50221 *ca, int slot, int address, u8 value)
154 struct budget_av *budget_av = (struct budget_av *) ca->data;
160 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTHI);
163 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 0xfff, 1, value, 0, 1);
164 if (result == -ETIMEDOUT) {
165 ciintf_slot_shutdown(ca, slot);
166 printk(KERN_INFO "budget-av: cam ejected 2\n");
171 static int ciintf_read_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address)
173 struct budget_av *budget_av = (struct budget_av *) ca->data;
179 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
182 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, address & 3, 1, 0, 0);
183 if (result == -ETIMEDOUT) {
184 ciintf_slot_shutdown(ca, slot);
185 printk(KERN_INFO "budget-av: cam ejected 3\n");
191 static int ciintf_write_cam_control(struct dvb_ca_en50221 *ca, int slot, u8 address, u8 value)
193 struct budget_av *budget_av = (struct budget_av *) ca->data;
199 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
202 result = ttpci_budget_debiwrite(&budget_av->budget, DEBICICAM, address & 3, 1, value, 0, 0);
203 if (result == -ETIMEDOUT) {
204 ciintf_slot_shutdown(ca, slot);
205 printk(KERN_INFO "budget-av: cam ejected 5\n");
210 static int ciintf_slot_reset(struct dvb_ca_en50221 *ca, int slot)
212 struct budget_av *budget_av = (struct budget_av *) ca->data;
213 struct saa7146_dev *saa = budget_av->budget.dev;
218 dprintk(1, "ciintf_slot_reset\n");
219 budget_av->slot_status = SLOTSTATUS_RESET;
221 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTHI); /* disable card */
223 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI); /* Vcc off */
225 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO); /* Vcc on */
226 msleep(20); /* 20 ms Vcc settling time */
228 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO); /* enable card */
229 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
232 /* reinitialise the frontend if necessary */
233 if (budget_av->reinitialise_demod)
234 dvb_frontend_reinitialise(budget_av->budget.dvb_frontend);
239 static int ciintf_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
241 struct budget_av *budget_av = (struct budget_av *) ca->data;
242 struct saa7146_dev *saa = budget_av->budget.dev;
247 dprintk(1, "ciintf_slot_shutdown\n");
249 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTB);
250 budget_av->slot_status = SLOTSTATUS_NONE;
255 static int ciintf_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
257 struct budget_av *budget_av = (struct budget_av *) ca->data;
258 struct saa7146_dev *saa = budget_av->budget.dev;
263 dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av->slot_status);
265 ttpci_budget_set_video_port(saa, BUDGET_VIDEO_PORTA);
270 static int ciintf_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
272 struct budget_av *budget_av = (struct budget_av *) ca->data;
273 struct saa7146_dev *saa = budget_av->budget.dev;
279 /* test the card detect line - needs to be done carefully
280 * since it never goes high for some CAMs on this interface (e.g. topuptv) */
281 if (budget_av->slot_status == SLOTSTATUS_NONE) {
282 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
284 if (saa7146_read(saa, PSR) & MASK_06) {
285 if (budget_av->slot_status == SLOTSTATUS_NONE) {
286 budget_av->slot_status = SLOTSTATUS_PRESENT;
287 printk(KERN_INFO "budget-av: cam inserted A\n");
290 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
293 /* We also try and read from IO memory to work round the above detection bug. If
294 * there is no CAM, we will get a timeout. Only done if there is no cam
295 * present, since this test actually breaks some cams :(
297 * if the CI interface is not open, we also do the above test since we
298 * don't care if the cam has problems - we'll be resetting it on open() anyway */
299 if ((budget_av->slot_status == SLOTSTATUS_NONE) || (!open)) {
300 saa7146_setgpio(budget_av->budget.dev, 1, SAA7146_GPIO_OUTLO);
301 result = ttpci_budget_debiread(&budget_av->budget, DEBICICAM, 0, 1, 0, 1);
302 if ((result >= 0) && (budget_av->slot_status == SLOTSTATUS_NONE)) {
303 budget_av->slot_status = SLOTSTATUS_PRESENT;
304 printk(KERN_INFO "budget-av: cam inserted B\n");
305 } else if (result < 0) {
306 if (budget_av->slot_status != SLOTSTATUS_NONE) {
307 ciintf_slot_shutdown(ca, slot);
308 printk(KERN_INFO "budget-av: cam ejected 5\n");
314 /* read from attribute memory in reset/ready state to know when the CAM is ready */
315 if (budget_av->slot_status == SLOTSTATUS_RESET) {
316 result = ciintf_read_attribute_mem(ca, slot, 0);
317 if (result == 0x1d) {
318 budget_av->slot_status = SLOTSTATUS_READY;
322 /* work out correct return code */
323 if (budget_av->slot_status != SLOTSTATUS_NONE) {
324 if (budget_av->slot_status & SLOTSTATUS_READY) {
325 return DVB_CA_EN50221_POLL_CAM_PRESENT | DVB_CA_EN50221_POLL_CAM_READY;
327 return DVB_CA_EN50221_POLL_CAM_PRESENT;
332 static int ciintf_init(struct budget_av *budget_av)
334 struct saa7146_dev *saa = budget_av->budget.dev;
337 memset(&budget_av->ca, 0, sizeof(struct dvb_ca_en50221));
339 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
340 saa7146_setgpio(saa, 1, SAA7146_GPIO_OUTLO);
341 saa7146_setgpio(saa, 2, SAA7146_GPIO_OUTLO);
342 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTLO);
344 /* Enable DEBI pins */
345 saa7146_write(saa, MC1, MASK_27 | MASK_11);
347 /* register CI interface */
348 budget_av->ca.owner = THIS_MODULE;
349 budget_av->ca.read_attribute_mem = ciintf_read_attribute_mem;
350 budget_av->ca.write_attribute_mem = ciintf_write_attribute_mem;
351 budget_av->ca.read_cam_control = ciintf_read_cam_control;
352 budget_av->ca.write_cam_control = ciintf_write_cam_control;
353 budget_av->ca.slot_reset = ciintf_slot_reset;
354 budget_av->ca.slot_shutdown = ciintf_slot_shutdown;
355 budget_av->ca.slot_ts_enable = ciintf_slot_ts_enable;
356 budget_av->ca.poll_slot_status = ciintf_poll_slot_status;
357 budget_av->ca.data = budget_av;
358 budget_av->budget.ci_present = 1;
359 budget_av->slot_status = SLOTSTATUS_NONE;
361 if ((result = dvb_ca_en50221_init(&budget_av->budget.dvb_adapter,
362 &budget_av->ca, 0, 1)) != 0) {
363 printk(KERN_ERR "budget-av: ci initialisation failed.\n");
367 printk(KERN_INFO "budget-av: ci interface initialised.\n");
371 saa7146_write(saa, MC1, MASK_27);
375 static void ciintf_deinit(struct budget_av *budget_av)
377 struct saa7146_dev *saa = budget_av->budget.dev;
379 saa7146_setgpio(saa, 0, SAA7146_GPIO_INPUT);
380 saa7146_setgpio(saa, 1, SAA7146_GPIO_INPUT);
381 saa7146_setgpio(saa, 2, SAA7146_GPIO_INPUT);
382 saa7146_setgpio(saa, 3, SAA7146_GPIO_INPUT);
384 /* release the CA device */
385 dvb_ca_en50221_release(&budget_av->ca);
387 /* disable DEBI pins */
388 saa7146_write(saa, MC1, MASK_27);
392 static const u8 saa7113_tab[] = {
413 0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
416 0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
417 0x5b, 0x83, 0x5e, 0x00,
421 static int saa7113_init(struct budget_av *budget_av)
423 struct budget *budget = &budget_av->budget;
424 struct saa7146_dev *saa = budget->dev;
425 const u8 *data = saa7113_tab;
427 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTHI);
430 if (i2c_writereg(&budget->i2c_adap, 0x4a, 0x01, 0x08) != 1) {
431 dprintk(1, "saa7113 not found on KNC card\n");
435 dprintk(1, "saa7113 detected and initializing\n");
437 while (*data != 0xff) {
438 i2c_writereg(&budget->i2c_adap, 0x4a, *data, *(data + 1));
442 dprintk(1, "saa7113 status=%02x\n", i2c_readreg(&budget->i2c_adap, 0x4a, 0x1f));
447 static int saa7113_setinput(struct budget_av *budget_av, int input)
449 struct budget *budget = &budget_av->budget;
451 if (1 != budget_av->has_saa7113)
455 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc7);
456 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x80);
457 } else if (input == 0) {
458 i2c_writereg(&budget->i2c_adap, 0x4a, 0x02, 0xc0);
459 i2c_writereg(&budget->i2c_adap, 0x4a, 0x09, 0x00);
463 budget_av->cur_input = input;
468 static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend *fe, u32 srate, u32 ratio)
477 else if (srate < 5000000)
479 else if (srate < 15000000)
481 else if (srate < 45000000)
488 stv0299_writereg(fe, 0x13, aclk);
489 stv0299_writereg(fe, 0x14, bclk);
490 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
491 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
492 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
493 stv0299_writereg(fe, 0x0f, 0x80 | m1);
498 static int philips_su1278_ty_ci_tuner_set_params(struct dvb_frontend *fe,
499 struct dvb_frontend_parameters *params)
503 struct budget *budget = (struct budget *) fe->dvb->priv;
504 struct i2c_msg msg = {.addr = 0x61,.flags = 0,.buf = buf,.len = sizeof(buf) };
506 if ((params->frequency < 950000) || (params->frequency > 2150000))
509 div = (params->frequency + (125 - 1)) / 125; // round correctly
510 buf[0] = (div >> 8) & 0x7f;
512 buf[2] = 0x80 | ((div & 0x18000) >> 10) | 4;
515 if (params->u.qpsk.symbol_rate < 4000000)
518 if (params->frequency < 1250000)
520 else if (params->frequency < 1550000)
522 else if (params->frequency < 2050000)
524 else if (params->frequency < 2150000)
527 if (fe->ops.i2c_gate_ctrl)
528 fe->ops.i2c_gate_ctrl(fe, 1);
529 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
534 static u8 typhoon_cinergy1200s_inittab[] = {
538 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
539 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
540 0x06, 0x40, /* DAC not used, set to high impendance mode */
541 0x07, 0x00, /* DAC LSB */
542 0x08, 0x40, /* DiSEqC off */
543 0x09, 0x00, /* FIFO */
544 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
545 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
546 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
547 0x10, 0x3f, // AGC2 0x3d
550 0x15, 0xc9, // lock detector threshold
561 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
562 0x29, 0x1e, // 1/2 threshold
563 0x2a, 0x14, // 2/3 threshold
564 0x2b, 0x0f, // 3/4 threshold
565 0x2c, 0x09, // 5/6 threshold
566 0x2d, 0x05, // 7/8 threshold
568 0x31, 0x1f, // test all FECs
569 0x32, 0x19, // viterbi and synchro search
570 0x33, 0xfc, // rs control
571 0x34, 0x93, // error control
576 static struct stv0299_config typhoon_config = {
577 .demod_address = 0x68,
578 .inittab = typhoon_cinergy1200s_inittab,
582 .lock_output = STV0299_LOCKOUTPUT_1,
583 .volt13_op0_op1 = STV0299_VOLT13_OP0,
585 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
589 static struct stv0299_config cinergy_1200s_config = {
590 .demod_address = 0x68,
591 .inittab = typhoon_cinergy1200s_inittab,
595 .lock_output = STV0299_LOCKOUTPUT_0,
596 .volt13_op0_op1 = STV0299_VOLT13_OP0,
598 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
601 static struct stv0299_config cinergy_1200s_1894_0010_config = {
602 .demod_address = 0x68,
603 .inittab = typhoon_cinergy1200s_inittab,
607 .lock_output = STV0299_LOCKOUTPUT_1,
608 .volt13_op0_op1 = STV0299_VOLT13_OP0,
610 .set_symbol_rate = philips_su1278_ty_ci_set_symbol_rate,
613 static int philips_cu1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
615 struct budget *budget = (struct budget *) fe->dvb->priv;
617 struct i2c_msg msg = {.addr = 0x60,.flags = 0,.buf = buf,.len = sizeof(buf) };
620 #define CU1216_IF 36125000
621 #define TUNER_MUL 62500
623 u32 div = (params->frequency + CU1216_IF + TUNER_MUL / 2) / TUNER_MUL;
625 buf[0] = (div >> 8) & 0x7f;
628 buf[3] = (params->frequency < 150000000 ? 0x01 :
629 params->frequency < 445000000 ? 0x02 : 0x04);
633 if (fe->ops.i2c_gate_ctrl)
634 fe->ops.i2c_gate_ctrl(fe, 1);
635 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
638 /* wait for the pll lock */
639 msg.flags = I2C_M_RD;
641 for (i = 0; i < 20; i++) {
642 if (fe->ops.i2c_gate_ctrl)
643 fe->ops.i2c_gate_ctrl(fe, 1);
644 if (i2c_transfer(&budget->i2c_adap, &msg, 1) == 1 && (buf[0] & 0x40))
649 /* switch the charge pump to the lower current */
654 if (fe->ops.i2c_gate_ctrl)
655 fe->ops.i2c_gate_ctrl(fe, 1);
656 if (i2c_transfer(&budget->i2c_adap, &msg, 1) != 1)
662 static struct tda1002x_config philips_cu1216_config = {
663 .demod_address = 0x0c,
667 static struct tda1002x_config philips_cu1216_config_altaddress = {
668 .demod_address = 0x0d,
672 static struct tda10023_config philips_cu1216_tda10023_config = {
673 .demod_address = 0x0c,
677 static int philips_tu1216_tuner_init(struct dvb_frontend *fe)
679 struct budget *budget = (struct budget *) fe->dvb->priv;
680 static u8 tu1216_init[] = { 0x0b, 0xf5, 0x85, 0xab };
681 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tu1216_init,.len = sizeof(tu1216_init) };
683 // setup PLL configuration
684 if (fe->ops.i2c_gate_ctrl)
685 fe->ops.i2c_gate_ctrl(fe, 1);
686 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
693 static int philips_tu1216_tuner_set_params(struct dvb_frontend *fe, struct dvb_frontend_parameters *params)
695 struct budget *budget = (struct budget *) fe->dvb->priv;
697 struct i2c_msg tuner_msg = {.addr = 0x60,.flags = 0,.buf = tuner_buf,.len =
699 int tuner_frequency = 0;
702 // determine charge pump
703 tuner_frequency = params->frequency + 36166000;
704 if (tuner_frequency < 87000000)
706 else if (tuner_frequency < 130000000)
708 else if (tuner_frequency < 160000000)
710 else if (tuner_frequency < 200000000)
712 else if (tuner_frequency < 290000000)
714 else if (tuner_frequency < 420000000)
716 else if (tuner_frequency < 480000000)
718 else if (tuner_frequency < 620000000)
720 else if (tuner_frequency < 830000000)
722 else if (tuner_frequency < 895000000)
728 if (params->frequency < 49000000)
730 else if (params->frequency < 161000000)
732 else if (params->frequency < 444000000)
734 else if (params->frequency < 861000000)
740 switch (params->u.ofdm.bandwidth) {
741 case BANDWIDTH_6_MHZ:
745 case BANDWIDTH_7_MHZ:
749 case BANDWIDTH_8_MHZ:
758 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
759 tuner_frequency = (((params->frequency / 1000) * 6) + 217496) / 1000;
761 // setup tuner buffer
762 tuner_buf[0] = (tuner_frequency >> 8) & 0x7f;
763 tuner_buf[1] = tuner_frequency & 0xff;
765 tuner_buf[3] = (cp << 5) | (filter << 3) | band;
767 if (fe->ops.i2c_gate_ctrl)
768 fe->ops.i2c_gate_ctrl(fe, 1);
769 if (i2c_transfer(&budget->i2c_adap, &tuner_msg, 1) != 1)
776 static int philips_tu1216_request_firmware(struct dvb_frontend *fe,
777 const struct firmware **fw, char *name)
779 struct budget *budget = (struct budget *) fe->dvb->priv;
781 return request_firmware(fw, name, &budget->dev->pci->dev);
784 static struct tda1004x_config philips_tu1216_config = {
786 .demod_address = 0x8,
789 .xtal_freq = TDA10046_XTAL_4M,
790 .agc_config = TDA10046_AGC_DEFAULT,
791 .if_freq = TDA10046_FREQ_3617,
792 .request_firmware = philips_tu1216_request_firmware,
795 static u8 philips_sd1878_inittab[] = {
838 static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend *fe,
839 u32 srate, u32 ratio)
848 else if (srate < 5000000)
850 else if (srate < 15000000)
852 else if (srate < 45000000)
859 stv0299_writereg(fe, 0x0e, 0x23);
860 stv0299_writereg(fe, 0x0f, 0x94);
861 stv0299_writereg(fe, 0x10, 0x39);
862 stv0299_writereg(fe, 0x13, aclk);
863 stv0299_writereg(fe, 0x14, bclk);
864 stv0299_writereg(fe, 0x15, 0xc9);
865 stv0299_writereg(fe, 0x1f, (ratio >> 16) & 0xff);
866 stv0299_writereg(fe, 0x20, (ratio >> 8) & 0xff);
867 stv0299_writereg(fe, 0x21, (ratio) & 0xf0);
868 stv0299_writereg(fe, 0x0f, 0x80 | m1);
873 static struct stv0299_config philips_sd1878_config = {
874 .demod_address = 0x68,
875 .inittab = philips_sd1878_inittab,
879 .lock_output = STV0299_LOCKOUTPUT_1,
880 .volt13_op0_op1 = STV0299_VOLT13_OP0,
882 .set_symbol_rate = philips_sd1878_ci_set_symbol_rate,
885 static u8 read_pwm(struct budget_av *budget_av)
889 struct i2c_msg msg[] = { {.addr = 0x50,.flags = 0,.buf = &b,.len = 1},
890 {.addr = 0x50,.flags = I2C_M_RD,.buf = &pwm,.len = 1}
893 if ((i2c_transfer(&budget_av->budget.i2c_adap, msg, 2) != 2)
900 #define SUBID_DVBS_KNC1 0x0010
901 #define SUBID_DVBS_KNC1_PLUS 0x0011
902 #define SUBID_DVBS_TYPHOON 0x4f56
903 #define SUBID_DVBS_CINERGY1200 0x1154
904 #define SUBID_DVBS_CYNERGY1200N 0x1155
905 #define SUBID_DVBS_TV_STAR 0x0014
906 #define SUBID_DVBS_TV_STAR_PLUS_X4 0x0015
907 #define SUBID_DVBS_TV_STAR_CI 0x0016
908 #define SUBID_DVBS_EASYWATCH_1 0x001a
909 #define SUBID_DVBS_EASYWATCH_2 0x001b
910 #define SUBID_DVBS_EASYWATCH 0x001e
912 #define SUBID_DVBC_EASYWATCH 0x002a
913 #define SUBID_DVBC_EASYWATCH_MK3 0x002c
914 #define SUBID_DVBC_KNC1 0x0020
915 #define SUBID_DVBC_KNC1_PLUS 0x0021
916 #define SUBID_DVBC_KNC1_MK3 0x0022
917 #define SUBID_DVBC_KNC1_PLUS_MK3 0x0023
918 #define SUBID_DVBC_CINERGY1200 0x1156
919 #define SUBID_DVBC_CINERGY1200_MK3 0x1176
921 #define SUBID_DVBT_EASYWATCH 0x003a
922 #define SUBID_DVBT_KNC1_PLUS 0x0031
923 #define SUBID_DVBT_KNC1 0x0030
924 #define SUBID_DVBT_CINERGY1200 0x1157
926 static void frontend_init(struct budget_av *budget_av)
928 struct saa7146_dev * saa = budget_av->budget.dev;
929 struct dvb_frontend * fe = NULL;
931 /* Enable / PowerON Frontend */
932 saa7146_setgpio(saa, 0, SAA7146_GPIO_OUTLO);
934 /* Wait for PowerON */
937 /* additional setup necessary for the PLUS cards */
938 switch (saa->pci->subsystem_device) {
939 case SUBID_DVBS_KNC1_PLUS:
940 case SUBID_DVBC_KNC1_PLUS:
941 case SUBID_DVBT_KNC1_PLUS:
942 case SUBID_DVBC_EASYWATCH:
943 case SUBID_DVBC_KNC1_PLUS_MK3:
944 saa7146_setgpio(saa, 3, SAA7146_GPIO_OUTHI);
948 switch (saa->pci->subsystem_device) {
950 case SUBID_DVBS_KNC1:
952 * maybe that setting is needed for other dvb-s cards as well,
953 * but so far it has been only confirmed for this type
955 budget_av->reinitialise_demod = 1;
957 case SUBID_DVBS_KNC1_PLUS:
958 case SUBID_DVBS_EASYWATCH_1:
959 if (saa->pci->subsystem_vendor == 0x1894) {
960 fe = dvb_attach(stv0299_attach, &cinergy_1200s_1894_0010_config,
961 &budget_av->budget.i2c_adap);
963 dvb_attach(tua6100_attach, fe, 0x60, &budget_av->budget.i2c_adap);
966 fe = dvb_attach(stv0299_attach, &typhoon_config,
967 &budget_av->budget.i2c_adap);
969 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
974 case SUBID_DVBS_TV_STAR:
975 case SUBID_DVBS_TV_STAR_PLUS_X4:
976 case SUBID_DVBS_TV_STAR_CI:
977 case SUBID_DVBS_CYNERGY1200N:
978 case SUBID_DVBS_EASYWATCH:
979 case SUBID_DVBS_EASYWATCH_2:
980 fe = dvb_attach(stv0299_attach, &philips_sd1878_config,
981 &budget_av->budget.i2c_adap);
983 dvb_attach(dvb_pll_attach, fe, 0x60,
984 &budget_av->budget.i2c_adap,
985 DVB_PLL_PHILIPS_SD1878_TDA8261);
989 case SUBID_DVBS_TYPHOON:
990 fe = dvb_attach(stv0299_attach, &typhoon_config,
991 &budget_av->budget.i2c_adap);
993 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
997 case SUBID_DVBS_CINERGY1200:
998 fe = dvb_attach(stv0299_attach, &cinergy_1200s_config,
999 &budget_av->budget.i2c_adap);
1001 fe->ops.tuner_ops.set_params = philips_su1278_ty_ci_tuner_set_params;
1005 case SUBID_DVBC_KNC1:
1006 case SUBID_DVBC_KNC1_PLUS:
1007 case SUBID_DVBC_CINERGY1200:
1008 case SUBID_DVBC_EASYWATCH:
1009 budget_av->reinitialise_demod = 1;
1010 budget_av->budget.dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
1011 fe = dvb_attach(tda10021_attach, &philips_cu1216_config,
1012 &budget_av->budget.i2c_adap,
1013 read_pwm(budget_av));
1015 fe = dvb_attach(tda10021_attach, &philips_cu1216_config_altaddress,
1016 &budget_av->budget.i2c_adap,
1017 read_pwm(budget_av));
1019 fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
1023 case SUBID_DVBC_EASYWATCH_MK3:
1024 case SUBID_DVBC_CINERGY1200_MK3:
1025 case SUBID_DVBC_KNC1_MK3:
1026 case SUBID_DVBC_KNC1_PLUS_MK3:
1027 budget_av->reinitialise_demod = 1;
1028 budget_av->budget.dev->i2c_bitrate = SAA7146_I2C_BUS_BIT_RATE_240;
1029 fe = dvb_attach(tda10023_attach,
1030 &philips_cu1216_tda10023_config,
1031 &budget_av->budget.i2c_adap,
1032 read_pwm(budget_av));
1034 fe->ops.tuner_ops.set_params = philips_cu1216_tuner_set_params;
1038 case SUBID_DVBT_EASYWATCH:
1039 case SUBID_DVBT_KNC1:
1040 case SUBID_DVBT_KNC1_PLUS:
1041 case SUBID_DVBT_CINERGY1200:
1042 budget_av->reinitialise_demod = 1;
1043 fe = dvb_attach(tda10046_attach, &philips_tu1216_config,
1044 &budget_av->budget.i2c_adap);
1046 fe->ops.tuner_ops.init = philips_tu1216_tuner_init;
1047 fe->ops.tuner_ops.set_params = philips_tu1216_tuner_set_params;
1053 printk(KERN_ERR "budget-av: A frontend driver was not found "
1054 "for device [%04x:%04x] subsystem [%04x:%04x]\n",
1057 saa->pci->subsystem_vendor,
1058 saa->pci->subsystem_device);
1062 budget_av->budget.dvb_frontend = fe;
1064 if (dvb_register_frontend(&budget_av->budget.dvb_adapter,
1065 budget_av->budget.dvb_frontend)) {
1066 printk(KERN_ERR "budget-av: Frontend registration failed!\n");
1067 dvb_frontend_detach(budget_av->budget.dvb_frontend);
1068 budget_av->budget.dvb_frontend = NULL;
1073 static void budget_av_irq(struct saa7146_dev *dev, u32 * isr)
1075 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1077 dprintk(8, "dev: %p, budget_av: %p\n", dev, budget_av);
1080 ttpci_budget_irq10_handler(dev, isr);
1083 static int budget_av_detach(struct saa7146_dev *dev)
1085 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1088 dprintk(2, "dev: %p\n", dev);
1090 if (1 == budget_av->has_saa7113) {
1091 saa7146_setgpio(dev, 0, SAA7146_GPIO_OUTLO);
1095 saa7146_unregister_device(&budget_av->vd, dev);
1097 saa7146_vv_release(dev);
1100 if (budget_av->budget.ci_present)
1101 ciintf_deinit(budget_av);
1103 if (budget_av->budget.dvb_frontend != NULL) {
1104 dvb_unregister_frontend(budget_av->budget.dvb_frontend);
1105 dvb_frontend_detach(budget_av->budget.dvb_frontend);
1107 err = ttpci_budget_deinit(&budget_av->budget);
1114 static struct saa7146_ext_vv vv_data;
1116 static int budget_av_attach(struct saa7146_dev *dev, struct saa7146_pci_extension_data *info)
1118 struct budget_av *budget_av;
1122 dprintk(2, "dev: %p\n", dev);
1124 if (!(budget_av = kzalloc(sizeof(struct budget_av), GFP_KERNEL)))
1127 budget_av->has_saa7113 = 0;
1128 budget_av->budget.ci_present = 0;
1130 dev->ext_priv = budget_av;
1132 err = ttpci_budget_init(&budget_av->budget, dev, info, THIS_MODULE,
1139 /* knc1 initialization */
1140 saa7146_write(dev, DD1_STREAM_B, 0x04000000);
1141 saa7146_write(dev, DD1_INIT, 0x07000600);
1142 saa7146_write(dev, MC2, MASK_09 | MASK_25 | MASK_10 | MASK_26);
1144 if (saa7113_init(budget_av) == 0) {
1145 budget_av->has_saa7113 = 1;
1147 if (0 != saa7146_vv_init(dev, &vv_data)) {
1148 /* fixme: proper cleanup here */
1149 ERR(("cannot init vv subsystem.\n"));
1153 if ((err = saa7146_register_device(&budget_av->vd, dev, "knc1", VFL_TYPE_GRABBER))) {
1154 /* fixme: proper cleanup here */
1155 ERR(("cannot register capture v4l2 device.\n"));
1156 saa7146_vv_release(dev);
1160 /* beware: this modifies dev->vv ... */
1161 saa7146_set_hps_source_and_sync(dev, SAA7146_HPS_SOURCE_PORT_A,
1162 SAA7146_HPS_SYNC_PORT_A);
1164 saa7113_setinput(budget_av, 0);
1167 /* fixme: find some sane values here... */
1168 saa7146_write(dev, PCI_BT_V1, 0x1c00101f);
1170 mac = budget_av->budget.dvb_adapter.proposed_mac;
1171 if (i2c_readregs(&budget_av->budget.i2c_adap, 0xa0, 0x30, mac, 6)) {
1172 printk(KERN_ERR "KNC1-%d: Could not read MAC from KNC1 card\n",
1173 budget_av->budget.dvb_adapter.num);
1176 printk(KERN_INFO "KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
1177 budget_av->budget.dvb_adapter.num,
1178 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1181 budget_av->budget.dvb_adapter.priv = budget_av;
1182 frontend_init(budget_av);
1183 ciintf_init(budget_av);
1185 ttpci_budget_init_hooks(&budget_av->budget);
1190 #define KNC1_INPUTS 2
1191 static struct v4l2_input knc1_inputs[KNC1_INPUTS] = {
1192 {0, "Composite", V4L2_INPUT_TYPE_TUNER, 1, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1193 {1, "S-Video", V4L2_INPUT_TYPE_CAMERA, 2, 0, V4L2_STD_PAL_BG | V4L2_STD_NTSC_M, 0},
1196 static struct saa7146_extension_ioctls ioctls[] = {
1197 {VIDIOC_ENUMINPUT, SAA7146_EXCLUSIVE},
1198 {VIDIOC_G_INPUT, SAA7146_EXCLUSIVE},
1199 {VIDIOC_S_INPUT, SAA7146_EXCLUSIVE},
1203 static int av_ioctl(struct saa7146_fh *fh, unsigned int cmd, void *arg)
1205 struct saa7146_dev *dev = fh->dev;
1206 struct budget_av *budget_av = (struct budget_av *) dev->ext_priv;
1209 case VIDIOC_ENUMINPUT:{
1210 struct v4l2_input *i = arg;
1212 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i->index);
1213 if (i->index < 0 || i->index >= KNC1_INPUTS) {
1216 memcpy(i, &knc1_inputs[i->index], sizeof(struct v4l2_input));
1219 case VIDIOC_G_INPUT:{
1220 int *input = (int *) arg;
1222 *input = budget_av->cur_input;
1224 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input);
1227 case VIDIOC_S_INPUT:{
1228 int input = *(int *) arg;
1229 dprintk(1, "VIDIOC_S_INPUT %d.\n", input);
1230 return saa7113_setinput(budget_av, input);
1233 return -ENOIOCTLCMD;
1238 static struct saa7146_standard standard[] = {
1239 {.name = "PAL",.id = V4L2_STD_PAL,
1240 .v_offset = 0x17,.v_field = 288,
1241 .h_offset = 0x14,.h_pixels = 680,
1242 .v_max_out = 576,.h_max_out = 768 },
1244 {.name = "NTSC",.id = V4L2_STD_NTSC,
1245 .v_offset = 0x16,.v_field = 240,
1246 .h_offset = 0x06,.h_pixels = 708,
1247 .v_max_out = 480,.h_max_out = 640, },
1250 static struct saa7146_ext_vv vv_data = {
1252 .capabilities = 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1254 .stds = &standard[0],
1255 .num_stds = ARRAY_SIZE(standard),
1256 .ioctls = &ioctls[0],
1260 static struct saa7146_extension budget_extension;
1262 MAKE_BUDGET_INFO(knc1s, "KNC1 DVB-S", BUDGET_KNC1S);
1263 MAKE_BUDGET_INFO(knc1c, "KNC1 DVB-C", BUDGET_KNC1C);
1264 MAKE_BUDGET_INFO(knc1t, "KNC1 DVB-T", BUDGET_KNC1T);
1265 MAKE_BUDGET_INFO(kncxs, "KNC TV STAR DVB-S", BUDGET_TVSTAR);
1266 MAKE_BUDGET_INFO(satewpls, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR);
1267 MAKE_BUDGET_INFO(satewpls1, "Satelco EasyWatch DVB-S light", BUDGET_KNC1S);
1268 MAKE_BUDGET_INFO(satewps, "Satelco EasyWatch DVB-S", BUDGET_KNC1S);
1269 MAKE_BUDGET_INFO(satewplc, "Satelco EasyWatch DVB-C", BUDGET_KNC1CP);
1270 MAKE_BUDGET_INFO(satewcmk3, "Satelco EasyWatch DVB-C MK3", BUDGET_KNC1C_MK3);
1271 MAKE_BUDGET_INFO(satewt, "Satelco EasyWatch DVB-T", BUDGET_KNC1T);
1272 MAKE_BUDGET_INFO(knc1sp, "KNC1 DVB-S Plus", BUDGET_KNC1SP);
1273 MAKE_BUDGET_INFO(knc1spx4, "KNC1 DVB-S Plus X4", BUDGET_KNC1SP);
1274 MAKE_BUDGET_INFO(knc1cp, "KNC1 DVB-C Plus", BUDGET_KNC1CP);
1275 MAKE_BUDGET_INFO(knc1cmk3, "KNC1 DVB-C MK3", BUDGET_KNC1C_MK3);
1276 MAKE_BUDGET_INFO(knc1cpmk3, "KNC1 DVB-C Plus MK3", BUDGET_KNC1CP_MK3);
1277 MAKE_BUDGET_INFO(knc1tp, "KNC1 DVB-T Plus", BUDGET_KNC1TP);
1278 MAKE_BUDGET_INFO(cin1200s, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1279 MAKE_BUDGET_INFO(cin1200sn, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S);
1280 MAKE_BUDGET_INFO(cin1200c, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C);
1281 MAKE_BUDGET_INFO(cin1200cmk3, "Terratec Cinergy 1200 DVB-C MK3", BUDGET_CIN1200C_MK3);
1282 MAKE_BUDGET_INFO(cin1200t, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T);
1284 static struct pci_device_id pci_tbl[] = {
1285 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x4f56),
1286 MAKE_EXTENSION_PCI(knc1s, 0x1131, 0x0010),
1287 MAKE_EXTENSION_PCI(knc1s, 0x1894, 0x0010),
1288 MAKE_EXTENSION_PCI(knc1sp, 0x1131, 0x0011),
1289 MAKE_EXTENSION_PCI(knc1sp, 0x1894, 0x0011),
1290 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0014),
1291 MAKE_EXTENSION_PCI(knc1spx4, 0x1894, 0x0015),
1292 MAKE_EXTENSION_PCI(kncxs, 0x1894, 0x0016),
1293 MAKE_EXTENSION_PCI(satewpls, 0x1894, 0x001e),
1294 MAKE_EXTENSION_PCI(satewpls1, 0x1894, 0x001a),
1295 MAKE_EXTENSION_PCI(satewps, 0x1894, 0x001b),
1296 MAKE_EXTENSION_PCI(satewplc, 0x1894, 0x002a),
1297 MAKE_EXTENSION_PCI(satewcmk3, 0x1894, 0x002c),
1298 MAKE_EXTENSION_PCI(satewt, 0x1894, 0x003a),
1299 MAKE_EXTENSION_PCI(knc1c, 0x1894, 0x0020),
1300 MAKE_EXTENSION_PCI(knc1cp, 0x1894, 0x0021),
1301 MAKE_EXTENSION_PCI(knc1cmk3, 0x1894, 0x0022),
1302 MAKE_EXTENSION_PCI(knc1cpmk3, 0x1894, 0x0023),
1303 MAKE_EXTENSION_PCI(knc1t, 0x1894, 0x0030),
1304 MAKE_EXTENSION_PCI(knc1tp, 0x1894, 0x0031),
1305 MAKE_EXTENSION_PCI(cin1200s, 0x153b, 0x1154),
1306 MAKE_EXTENSION_PCI(cin1200sn, 0x153b, 0x1155),
1307 MAKE_EXTENSION_PCI(cin1200c, 0x153b, 0x1156),
1308 MAKE_EXTENSION_PCI(cin1200cmk3, 0x153b, 0x1176),
1309 MAKE_EXTENSION_PCI(cin1200t, 0x153b, 0x1157),
1315 MODULE_DEVICE_TABLE(pci, pci_tbl);
1317 static struct saa7146_extension budget_extension = {
1318 .name = "budget_av",
1319 .flags = SAA7146_USE_I2C_IRQ,
1323 .module = THIS_MODULE,
1324 .attach = budget_av_attach,
1325 .detach = budget_av_detach,
1327 .irq_mask = MASK_10,
1328 .irq_func = budget_av_irq,
1331 static int __init budget_av_init(void)
1333 return saa7146_register_extension(&budget_extension);
1336 static void __exit budget_av_exit(void)
1338 saa7146_unregister_extension(&budget_extension);
1341 module_init(budget_av_init);
1342 module_exit(budget_av_exit);
1344 MODULE_LICENSE("GPL");
1345 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1346 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1347 "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");