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)
62 struct video_device
*vd
;
65 struct tasklet_struct ciintf_irq_tasklet
;
67 struct dvb_ca_en50221 ca
;
68 u8 reinitialise_demod
:1;
71 static int ciintf_slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
);
75 * 0 - Vcc/Reset (Reset is controlled by capacitor). Resets the frontend *AS WELL*!
76 * 1 - CI memory select 0=>IO memory, 1=>Attribute Memory
77 * 2 - CI Card Enable (Active Low)
81 /****************************************************************************
83 ****************************************************************************/
85 static u8
i2c_readreg(struct i2c_adapter
*i2c
, u8 id
, u8 reg
)
89 struct i2c_msg msgs
[2];
92 msgs
[1].flags
= I2C_M_RD
;
93 msgs
[0].addr
= msgs
[1].addr
= id
/ 2;
100 i2c_transfer(i2c
, msgs
, 2);
105 static int i2c_readregs(struct i2c_adapter
*i2c
, u8 id
, u8 reg
, u8
* buf
, u8 len
)
108 struct i2c_msg msgs
[2] = {
109 {.addr
= id
/ 2,.flags
= 0,.buf
= mm1
,.len
= 1},
110 {.addr
= id
/ 2,.flags
= I2C_M_RD
,.buf
= buf
,.len
= len
}
113 if (i2c_transfer(i2c
, msgs
, 2) != 2)
119 static int i2c_writereg(struct i2c_adapter
*i2c
, u8 id
, u8 reg
, u8 val
)
121 u8 msg
[2] = { reg
, val
};
128 return i2c_transfer(i2c
, &msgs
, 1);
131 static int ciintf_read_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
, int address
)
133 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
139 saa7146_setgpio(budget_av
->budget
.dev
, 1, SAA7146_GPIO_OUTHI
);
142 result
= ttpci_budget_debiread(&budget_av
->budget
, DEBICICAM
, address
& 0xfff, 1, 0, 1);
143 if (result
== -ETIMEDOUT
) {
144 ciintf_slot_shutdown(ca
, slot
);
145 printk(KERN_INFO
"budget-av: cam ejected 1\n");
150 static int ciintf_write_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
, int address
, u8 value
)
152 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
158 saa7146_setgpio(budget_av
->budget
.dev
, 1, SAA7146_GPIO_OUTHI
);
161 result
= ttpci_budget_debiwrite(&budget_av
->budget
, DEBICICAM
, address
& 0xfff, 1, value
, 0, 1);
162 if (result
== -ETIMEDOUT
) {
163 ciintf_slot_shutdown(ca
, slot
);
164 printk(KERN_INFO
"budget-av: cam ejected 2\n");
169 static int ciintf_read_cam_control(struct dvb_ca_en50221
*ca
, int slot
, u8 address
)
171 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
177 saa7146_setgpio(budget_av
->budget
.dev
, 1, SAA7146_GPIO_OUTLO
);
180 result
= ttpci_budget_debiread(&budget_av
->budget
, DEBICICAM
, address
& 3, 1, 0, 0);
181 if ((result
== -ETIMEDOUT
) || ((result
== 0xff) && ((address
& 3) < 2))) {
182 ciintf_slot_shutdown(ca
, slot
);
183 printk(KERN_INFO
"budget-av: cam ejected 3\n");
189 static int ciintf_write_cam_control(struct dvb_ca_en50221
*ca
, int slot
, u8 address
, u8 value
)
191 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
197 saa7146_setgpio(budget_av
->budget
.dev
, 1, SAA7146_GPIO_OUTLO
);
200 result
= ttpci_budget_debiwrite(&budget_av
->budget
, DEBICICAM
, address
& 3, 1, value
, 0, 0);
201 if (result
== -ETIMEDOUT
) {
202 ciintf_slot_shutdown(ca
, slot
);
203 printk(KERN_INFO
"budget-av: cam ejected 5\n");
208 static int ciintf_slot_reset(struct dvb_ca_en50221
*ca
, int slot
)
210 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
211 struct saa7146_dev
*saa
= budget_av
->budget
.dev
;
216 dprintk(1, "ciintf_slot_reset\n");
217 budget_av
->slot_status
= SLOTSTATUS_RESET
;
219 saa7146_setgpio(saa
, 2, SAA7146_GPIO_OUTHI
); /* disable card */
221 saa7146_setgpio(saa
, 0, SAA7146_GPIO_OUTHI
); /* Vcc off */
223 saa7146_setgpio(saa
, 0, SAA7146_GPIO_OUTLO
); /* Vcc on */
224 msleep(20); /* 20 ms Vcc settling time */
226 saa7146_setgpio(saa
, 2, SAA7146_GPIO_OUTLO
); /* enable card */
227 ttpci_budget_set_video_port(saa
, BUDGET_VIDEO_PORTB
);
230 /* reinitialise the frontend if necessary */
231 if (budget_av
->reinitialise_demod
)
232 dvb_frontend_reinitialise(budget_av
->budget
.dvb_frontend
);
237 static int ciintf_slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
)
239 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
240 struct saa7146_dev
*saa
= budget_av
->budget
.dev
;
245 dprintk(1, "ciintf_slot_shutdown\n");
247 ttpci_budget_set_video_port(saa
, BUDGET_VIDEO_PORTB
);
248 budget_av
->slot_status
= SLOTSTATUS_NONE
;
253 static int ciintf_slot_ts_enable(struct dvb_ca_en50221
*ca
, int slot
)
255 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
256 struct saa7146_dev
*saa
= budget_av
->budget
.dev
;
261 dprintk(1, "ciintf_slot_ts_enable: %d\n", budget_av
->slot_status
);
263 ttpci_budget_set_video_port(saa
, BUDGET_VIDEO_PORTA
);
268 static int ciintf_poll_slot_status(struct dvb_ca_en50221
*ca
, int slot
, int open
)
270 struct budget_av
*budget_av
= (struct budget_av
*) ca
->data
;
271 struct saa7146_dev
*saa
= budget_av
->budget
.dev
;
277 /* test the card detect line - needs to be done carefully
278 * since it never goes high for some CAMs on this interface (e.g. topuptv) */
279 if (budget_av
->slot_status
== SLOTSTATUS_NONE
) {
280 saa7146_setgpio(saa
, 3, SAA7146_GPIO_INPUT
);
282 if (saa7146_read(saa
, PSR
) & MASK_06
) {
283 if (budget_av
->slot_status
== SLOTSTATUS_NONE
) {
284 budget_av
->slot_status
= SLOTSTATUS_PRESENT
;
285 printk(KERN_INFO
"budget-av: cam inserted A\n");
288 saa7146_setgpio(saa
, 3, SAA7146_GPIO_OUTLO
);
291 /* We also try and read from IO memory to work round the above detection bug. If
292 * there is no CAM, we will get a timeout. Only done if there is no cam
293 * present, since this test actually breaks some cams :(
295 * if the CI interface is not open, we also do the above test since we
296 * don't care if the cam has problems - we'll be resetting it on open() anyway */
297 if ((budget_av
->slot_status
== SLOTSTATUS_NONE
) || (!open
)) {
298 saa7146_setgpio(budget_av
->budget
.dev
, 1, SAA7146_GPIO_OUTLO
);
299 result
= ttpci_budget_debiread(&budget_av
->budget
, DEBICICAM
, 0, 1, 0, 1);
300 if ((result
>= 0) && (budget_av
->slot_status
== SLOTSTATUS_NONE
)) {
301 budget_av
->slot_status
= SLOTSTATUS_PRESENT
;
302 printk(KERN_INFO
"budget-av: cam inserted B\n");
303 } else if (result
< 0) {
304 if (budget_av
->slot_status
!= SLOTSTATUS_NONE
) {
305 ciintf_slot_shutdown(ca
, slot
);
306 printk(KERN_INFO
"budget-av: cam ejected 5\n");
312 /* read from attribute memory in reset/ready state to know when the CAM is ready */
313 if (budget_av
->slot_status
== SLOTSTATUS_RESET
) {
314 result
= ciintf_read_attribute_mem(ca
, slot
, 0);
315 if (result
== 0x1d) {
316 budget_av
->slot_status
= SLOTSTATUS_READY
;
320 /* work out correct return code */
321 if (budget_av
->slot_status
!= SLOTSTATUS_NONE
) {
322 if (budget_av
->slot_status
& SLOTSTATUS_READY
) {
323 return DVB_CA_EN50221_POLL_CAM_PRESENT
| DVB_CA_EN50221_POLL_CAM_READY
;
325 return DVB_CA_EN50221_POLL_CAM_PRESENT
;
330 static int ciintf_init(struct budget_av
*budget_av
)
332 struct saa7146_dev
*saa
= budget_av
->budget
.dev
;
335 memset(&budget_av
->ca
, 0, sizeof(struct dvb_ca_en50221
));
337 saa7146_setgpio(saa
, 0, SAA7146_GPIO_OUTLO
);
338 saa7146_setgpio(saa
, 1, SAA7146_GPIO_OUTLO
);
339 saa7146_setgpio(saa
, 2, SAA7146_GPIO_OUTLO
);
340 saa7146_setgpio(saa
, 3, SAA7146_GPIO_OUTLO
);
342 /* Enable DEBI pins */
343 saa7146_write(saa
, MC1
, MASK_27
| MASK_11
);
345 /* register CI interface */
346 budget_av
->ca
.owner
= THIS_MODULE
;
347 budget_av
->ca
.read_attribute_mem
= ciintf_read_attribute_mem
;
348 budget_av
->ca
.write_attribute_mem
= ciintf_write_attribute_mem
;
349 budget_av
->ca
.read_cam_control
= ciintf_read_cam_control
;
350 budget_av
->ca
.write_cam_control
= ciintf_write_cam_control
;
351 budget_av
->ca
.slot_reset
= ciintf_slot_reset
;
352 budget_av
->ca
.slot_shutdown
= ciintf_slot_shutdown
;
353 budget_av
->ca
.slot_ts_enable
= ciintf_slot_ts_enable
;
354 budget_av
->ca
.poll_slot_status
= ciintf_poll_slot_status
;
355 budget_av
->ca
.data
= budget_av
;
356 budget_av
->budget
.ci_present
= 1;
357 budget_av
->slot_status
= SLOTSTATUS_NONE
;
359 if ((result
= dvb_ca_en50221_init(&budget_av
->budget
.dvb_adapter
,
360 &budget_av
->ca
, 0, 1)) != 0) {
361 printk(KERN_ERR
"budget-av: ci initialisation failed.\n");
365 printk(KERN_INFO
"budget-av: ci interface initialised.\n");
369 saa7146_write(saa
, MC1
, MASK_27
);
373 static void ciintf_deinit(struct budget_av
*budget_av
)
375 struct saa7146_dev
*saa
= budget_av
->budget
.dev
;
377 saa7146_setgpio(saa
, 0, SAA7146_GPIO_INPUT
);
378 saa7146_setgpio(saa
, 1, SAA7146_GPIO_INPUT
);
379 saa7146_setgpio(saa
, 2, SAA7146_GPIO_INPUT
);
380 saa7146_setgpio(saa
, 3, SAA7146_GPIO_INPUT
);
382 /* release the CA device */
383 dvb_ca_en50221_release(&budget_av
->ca
);
385 /* disable DEBI pins */
386 saa7146_write(saa
, MC1
, MASK_27
);
390 static const u8 saa7113_tab
[] = {
411 0x15, 0x00, 0x16, 0x00, 0x17, 0x00,
414 0x40, 0x82, 0x58, 0x00, 0x59, 0x54, 0x5a, 0x07,
415 0x5b, 0x83, 0x5e, 0x00,
419 static int saa7113_init(struct budget_av
*budget_av
)
421 struct budget
*budget
= &budget_av
->budget
;
422 struct saa7146_dev
*saa
= budget
->dev
;
423 const u8
*data
= saa7113_tab
;
425 saa7146_setgpio(saa
, 0, SAA7146_GPIO_OUTHI
);
428 if (i2c_writereg(&budget
->i2c_adap
, 0x4a, 0x01, 0x08) != 1) {
429 dprintk(1, "saa7113 not found on KNC card\n");
433 dprintk(1, "saa7113 detected and initializing\n");
435 while (*data
!= 0xff) {
436 i2c_writereg(&budget
->i2c_adap
, 0x4a, *data
, *(data
+ 1));
440 dprintk(1, "saa7113 status=%02x\n", i2c_readreg(&budget
->i2c_adap
, 0x4a, 0x1f));
445 static int saa7113_setinput(struct budget_av
*budget_av
, int input
)
447 struct budget
*budget
= &budget_av
->budget
;
449 if (1 != budget_av
->has_saa7113
)
453 i2c_writereg(&budget
->i2c_adap
, 0x4a, 0x02, 0xc7);
454 i2c_writereg(&budget
->i2c_adap
, 0x4a, 0x09, 0x80);
455 } else if (input
== 0) {
456 i2c_writereg(&budget
->i2c_adap
, 0x4a, 0x02, 0xc0);
457 i2c_writereg(&budget
->i2c_adap
, 0x4a, 0x09, 0x00);
461 budget_av
->cur_input
= input
;
466 static int philips_su1278_ty_ci_set_symbol_rate(struct dvb_frontend
*fe
, u32 srate
, u32 ratio
)
475 else if (srate
< 5000000)
477 else if (srate
< 15000000)
479 else if (srate
< 45000000)
486 stv0299_writereg(fe
, 0x13, aclk
);
487 stv0299_writereg(fe
, 0x14, bclk
);
488 stv0299_writereg(fe
, 0x1f, (ratio
>> 16) & 0xff);
489 stv0299_writereg(fe
, 0x20, (ratio
>> 8) & 0xff);
490 stv0299_writereg(fe
, 0x21, (ratio
) & 0xf0);
491 stv0299_writereg(fe
, 0x0f, 0x80 | m1
);
496 static int philips_su1278_ty_ci_tuner_set_params(struct dvb_frontend
*fe
,
497 struct dvb_frontend_parameters
*params
)
501 struct budget
*budget
= (struct budget
*) fe
->dvb
->priv
;
502 struct i2c_msg msg
= {.addr
= 0x61,.flags
= 0,.buf
= buf
,.len
= sizeof(buf
) };
504 if ((params
->frequency
< 950000) || (params
->frequency
> 2150000))
507 div
= (params
->frequency
+ (125 - 1)) / 125; // round correctly
508 buf
[0] = (div
>> 8) & 0x7f;
510 buf
[2] = 0x80 | ((div
& 0x18000) >> 10) | 4;
513 if (params
->u
.qpsk
.symbol_rate
< 4000000)
516 if (params
->frequency
< 1250000)
518 else if (params
->frequency
< 1550000)
520 else if (params
->frequency
< 2050000)
522 else if (params
->frequency
< 2150000)
525 if (fe
->ops
.i2c_gate_ctrl
)
526 fe
->ops
.i2c_gate_ctrl(fe
, 1);
527 if (i2c_transfer(&budget
->i2c_adap
, &msg
, 1) != 1)
532 static u8 typhoon_cinergy1200s_inittab
[] = {
536 0x04, 0x7d, /* F22FR = 0x7d, F22 = f_VCO / 128 / 0x7d = 22 kHz */
537 0x05, 0x35, /* I2CT = 0, SCLT = 1, SDAT = 1 */
538 0x06, 0x40, /* DAC not used, set to high impendance mode */
539 0x07, 0x00, /* DAC LSB */
540 0x08, 0x40, /* DiSEqC off */
541 0x09, 0x00, /* FIFO */
542 0x0c, 0x51, /* OP1 ctl = Normal, OP1 val = 1 (LNB Power ON) */
543 0x0d, 0x82, /* DC offset compensation = ON, beta_agc1 = 2 */
544 0x0e, 0x23, /* alpha_tmg = 2, beta_tmg = 3 */
545 0x10, 0x3f, // AGC2 0x3d
548 0x15, 0xc9, // lock detector threshold
559 0x28, 0x00, // out imp: normal out type: parallel FEC mode:0
560 0x29, 0x1e, // 1/2 threshold
561 0x2a, 0x14, // 2/3 threshold
562 0x2b, 0x0f, // 3/4 threshold
563 0x2c, 0x09, // 5/6 threshold
564 0x2d, 0x05, // 7/8 threshold
566 0x31, 0x1f, // test all FECs
567 0x32, 0x19, // viterbi and synchro search
568 0x33, 0xfc, // rs control
569 0x34, 0x93, // error control
574 static struct stv0299_config typhoon_config
= {
575 .demod_address
= 0x68,
576 .inittab
= typhoon_cinergy1200s_inittab
,
580 .lock_output
= STV0229_LOCKOUTPUT_1
,
581 .volt13_op0_op1
= STV0299_VOLT13_OP0
,
583 .set_symbol_rate
= philips_su1278_ty_ci_set_symbol_rate
,
587 static struct stv0299_config cinergy_1200s_config
= {
588 .demod_address
= 0x68,
589 .inittab
= typhoon_cinergy1200s_inittab
,
593 .lock_output
= STV0229_LOCKOUTPUT_0
,
594 .volt13_op0_op1
= STV0299_VOLT13_OP0
,
596 .set_symbol_rate
= philips_su1278_ty_ci_set_symbol_rate
,
599 static struct stv0299_config cinergy_1200s_1894_0010_config
= {
600 .demod_address
= 0x68,
601 .inittab
= typhoon_cinergy1200s_inittab
,
605 .lock_output
= STV0229_LOCKOUTPUT_1
,
606 .volt13_op0_op1
= STV0299_VOLT13_OP0
,
608 .set_symbol_rate
= philips_su1278_ty_ci_set_symbol_rate
,
611 static int philips_cu1216_tuner_set_params(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*params
)
613 struct budget
*budget
= (struct budget
*) fe
->dvb
->priv
;
615 struct i2c_msg msg
= {.addr
= 0x60,.flags
= 0,.buf
= buf
,.len
= sizeof(buf
) };
618 #define CU1216_IF 36125000
619 #define TUNER_MUL 62500
621 u32 div
= (params
->frequency
+ CU1216_IF
+ TUNER_MUL
/ 2) / TUNER_MUL
;
623 buf
[0] = (div
>> 8) & 0x7f;
626 buf
[3] = (params
->frequency
< 150000000 ? 0x01 :
627 params
->frequency
< 445000000 ? 0x02 : 0x04);
631 if (fe
->ops
.i2c_gate_ctrl
)
632 fe
->ops
.i2c_gate_ctrl(fe
, 1);
633 if (i2c_transfer(&budget
->i2c_adap
, &msg
, 1) != 1)
636 /* wait for the pll lock */
637 msg
.flags
= I2C_M_RD
;
639 for (i
= 0; i
< 20; i
++) {
640 if (fe
->ops
.i2c_gate_ctrl
)
641 fe
->ops
.i2c_gate_ctrl(fe
, 1);
642 if (i2c_transfer(&budget
->i2c_adap
, &msg
, 1) == 1 && (buf
[0] & 0x40))
647 /* switch the charge pump to the lower current */
652 if (fe
->ops
.i2c_gate_ctrl
)
653 fe
->ops
.i2c_gate_ctrl(fe
, 1);
654 if (i2c_transfer(&budget
->i2c_adap
, &msg
, 1) != 1)
660 static struct tda1002x_config philips_cu1216_config
= {
661 .demod_address
= 0x0c,
665 static struct tda1002x_config philips_cu1216_config_altaddress
= {
666 .demod_address
= 0x0d,
670 static int philips_tu1216_tuner_init(struct dvb_frontend
*fe
)
672 struct budget
*budget
= (struct budget
*) fe
->dvb
->priv
;
673 static u8 tu1216_init
[] = { 0x0b, 0xf5, 0x85, 0xab };
674 struct i2c_msg tuner_msg
= {.addr
= 0x60,.flags
= 0,.buf
= tu1216_init
,.len
= sizeof(tu1216_init
) };
676 // setup PLL configuration
677 if (fe
->ops
.i2c_gate_ctrl
)
678 fe
->ops
.i2c_gate_ctrl(fe
, 1);
679 if (i2c_transfer(&budget
->i2c_adap
, &tuner_msg
, 1) != 1)
686 static int philips_tu1216_tuner_set_params(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*params
)
688 struct budget
*budget
= (struct budget
*) fe
->dvb
->priv
;
690 struct i2c_msg tuner_msg
= {.addr
= 0x60,.flags
= 0,.buf
= tuner_buf
,.len
=
692 int tuner_frequency
= 0;
695 // determine charge pump
696 tuner_frequency
= params
->frequency
+ 36166000;
697 if (tuner_frequency
< 87000000)
699 else if (tuner_frequency
< 130000000)
701 else if (tuner_frequency
< 160000000)
703 else if (tuner_frequency
< 200000000)
705 else if (tuner_frequency
< 290000000)
707 else if (tuner_frequency
< 420000000)
709 else if (tuner_frequency
< 480000000)
711 else if (tuner_frequency
< 620000000)
713 else if (tuner_frequency
< 830000000)
715 else if (tuner_frequency
< 895000000)
721 if (params
->frequency
< 49000000)
723 else if (params
->frequency
< 161000000)
725 else if (params
->frequency
< 444000000)
727 else if (params
->frequency
< 861000000)
733 switch (params
->u
.ofdm
.bandwidth
) {
734 case BANDWIDTH_6_MHZ
:
738 case BANDWIDTH_7_MHZ
:
742 case BANDWIDTH_8_MHZ
:
751 // ((36166000+((1000000/6)/2)) + Finput)/(1000000/6)
752 tuner_frequency
= (((params
->frequency
/ 1000) * 6) + 217496) / 1000;
754 // setup tuner buffer
755 tuner_buf
[0] = (tuner_frequency
>> 8) & 0x7f;
756 tuner_buf
[1] = tuner_frequency
& 0xff;
758 tuner_buf
[3] = (cp
<< 5) | (filter
<< 3) | band
;
760 if (fe
->ops
.i2c_gate_ctrl
)
761 fe
->ops
.i2c_gate_ctrl(fe
, 1);
762 if (i2c_transfer(&budget
->i2c_adap
, &tuner_msg
, 1) != 1)
769 static int philips_tu1216_request_firmware(struct dvb_frontend
*fe
,
770 const struct firmware
**fw
, char *name
)
772 struct budget
*budget
= (struct budget
*) fe
->dvb
->priv
;
774 return request_firmware(fw
, name
, &budget
->dev
->pci
->dev
);
777 static struct tda1004x_config philips_tu1216_config
= {
779 .demod_address
= 0x8,
782 .xtal_freq
= TDA10046_XTAL_4M
,
783 .agc_config
= TDA10046_AGC_DEFAULT
,
784 .if_freq
= TDA10046_FREQ_3617
,
785 .request_firmware
= philips_tu1216_request_firmware
,
788 static u8 philips_sd1878_inittab
[] = {
831 static int philips_sd1878_tda8261_tuner_set_params(struct dvb_frontend
*fe
,
832 struct dvb_frontend_parameters
*params
)
836 struct i2c_msg tuner_msg
= {.addr
=0x60,.flags
=0,.buf
=buf
,.len
=sizeof(buf
)};
837 struct budget
*budget
= (struct budget
*) fe
->dvb
->priv
;
839 if((params
->frequency
< 950000) || (params
->frequency
> 2150000))
842 rc
=dvb_pll_configure(&dvb_pll_philips_sd1878_tda8261
, buf
,
843 params
->frequency
, 0);
844 if(rc
< 0) return rc
;
846 if (fe
->ops
.i2c_gate_ctrl
)
847 fe
->ops
.i2c_gate_ctrl(fe
, 1);
848 if(i2c_transfer(&budget
->i2c_adap
, &tuner_msg
, 1) != 1)
854 static int philips_sd1878_ci_set_symbol_rate(struct dvb_frontend
*fe
,
855 u32 srate
, u32 ratio
)
864 else if (srate
< 5000000)
866 else if (srate
< 15000000)
868 else if (srate
< 45000000)
875 stv0299_writereg(fe
, 0x0e, 0x23);
876 stv0299_writereg(fe
, 0x0f, 0x94);
877 stv0299_writereg(fe
, 0x10, 0x39);
878 stv0299_writereg(fe
, 0x13, aclk
);
879 stv0299_writereg(fe
, 0x14, bclk
);
880 stv0299_writereg(fe
, 0x15, 0xc9);
881 stv0299_writereg(fe
, 0x1f, (ratio
>> 16) & 0xff);
882 stv0299_writereg(fe
, 0x20, (ratio
>> 8) & 0xff);
883 stv0299_writereg(fe
, 0x21, (ratio
) & 0xf0);
884 stv0299_writereg(fe
, 0x0f, 0x80 | m1
);
889 static struct stv0299_config philips_sd1878_config
= {
890 .demod_address
= 0x68,
891 .inittab
= philips_sd1878_inittab
,
895 .lock_output
= STV0229_LOCKOUTPUT_1
,
896 .volt13_op0_op1
= STV0299_VOLT13_OP0
,
898 .set_symbol_rate
= philips_sd1878_ci_set_symbol_rate
,
901 static u8
read_pwm(struct budget_av
*budget_av
)
905 struct i2c_msg msg
[] = { {.addr
= 0x50,.flags
= 0,.buf
= &b
,.len
= 1},
906 {.addr
= 0x50,.flags
= I2C_M_RD
,.buf
= &pwm
,.len
= 1}
909 if ((i2c_transfer(&budget_av
->budget
.i2c_adap
, msg
, 2) != 2)
916 #define SUBID_DVBS_KNC1 0x0010
917 #define SUBID_DVBS_KNC1_PLUS 0x0011
918 #define SUBID_DVBS_TYPHOON 0x4f56
919 #define SUBID_DVBS_CINERGY1200 0x1154
920 #define SUBID_DVBS_CYNERGY1200N 0x1155
921 #define SUBID_DVBS_TV_STAR 0x0014
922 #define SUBID_DVBS_TV_STAR_CI 0x0016
923 #define SUBID_DVBS_EASYWATCH_1 0x001a
924 #define SUBID_DVBS_EASYWATCH 0x001e
926 #define SUBID_DVBC_EASYWATCH 0x002a
927 #define SUBID_DVBC_EASYWATCH_MK3 0x002c
928 #define SUBID_DVBC_KNC1 0x0020
929 #define SUBID_DVBC_KNC1_PLUS 0x0021
930 #define SUBID_DVBC_KNC1_MK3 0x0022
931 #define SUBID_DVBC_KNC1_PLUS_MK3 0x0023
932 #define SUBID_DVBC_CINERGY1200 0x1156
933 #define SUBID_DVBC_CINERGY1200_MK3 0x1176
935 #define SUBID_DVBT_KNC1_PLUS 0x0031
936 #define SUBID_DVBT_KNC1 0x0030
937 #define SUBID_DVBT_CINERGY1200 0x1157
939 static void frontend_init(struct budget_av
*budget_av
)
941 struct saa7146_dev
* saa
= budget_av
->budget
.dev
;
942 struct dvb_frontend
* fe
= NULL
;
944 /* Enable / PowerON Frontend */
945 saa7146_setgpio(saa
, 0, SAA7146_GPIO_OUTLO
);
947 /* Wait for PowerON */
950 /* additional setup necessary for the PLUS cards */
951 switch (saa
->pci
->subsystem_device
) {
952 case SUBID_DVBS_KNC1_PLUS
:
953 case SUBID_DVBC_KNC1_PLUS
:
954 case SUBID_DVBT_KNC1_PLUS
:
955 case SUBID_DVBC_EASYWATCH
:
956 case SUBID_DVBC_KNC1_PLUS_MK3
:
957 saa7146_setgpio(saa
, 3, SAA7146_GPIO_OUTHI
);
961 switch (saa
->pci
->subsystem_device
) {
963 case SUBID_DVBS_KNC1
:
964 case SUBID_DVBS_KNC1_PLUS
:
965 case SUBID_DVBS_EASYWATCH_1
:
966 if (saa
->pci
->subsystem_vendor
== 0x1894) {
967 fe
= dvb_attach(stv0299_attach
, &cinergy_1200s_1894_0010_config
,
968 &budget_av
->budget
.i2c_adap
);
970 dvb_attach(tua6100_attach
, fe
, 0x60, &budget_av
->budget
.i2c_adap
);
973 fe
= dvb_attach(stv0299_attach
, &typhoon_config
,
974 &budget_av
->budget
.i2c_adap
);
976 fe
->ops
.tuner_ops
.set_params
= philips_su1278_ty_ci_tuner_set_params
;
981 case SUBID_DVBS_TV_STAR
:
982 case SUBID_DVBS_TV_STAR_CI
:
983 case SUBID_DVBS_CYNERGY1200N
:
984 case SUBID_DVBS_EASYWATCH
:
985 fe
= dvb_attach(stv0299_attach
, &philips_sd1878_config
,
986 &budget_av
->budget
.i2c_adap
);
988 fe
->ops
.tuner_ops
.set_params
= philips_sd1878_tda8261_tuner_set_params
;
992 case SUBID_DVBS_TYPHOON
:
993 fe
= dvb_attach(stv0299_attach
, &typhoon_config
,
994 &budget_av
->budget
.i2c_adap
);
996 fe
->ops
.tuner_ops
.set_params
= philips_su1278_ty_ci_tuner_set_params
;
1000 case SUBID_DVBS_CINERGY1200
:
1001 fe
= dvb_attach(stv0299_attach
, &cinergy_1200s_config
,
1002 &budget_av
->budget
.i2c_adap
);
1004 fe
->ops
.tuner_ops
.set_params
= philips_su1278_ty_ci_tuner_set_params
;
1008 case SUBID_DVBC_KNC1
:
1009 case SUBID_DVBC_KNC1_PLUS
:
1010 case SUBID_DVBC_CINERGY1200
:
1011 case SUBID_DVBC_EASYWATCH
:
1012 budget_av
->reinitialise_demod
= 1;
1013 budget_av
->budget
.dev
->i2c_bitrate
= SAA7146_I2C_BUS_BIT_RATE_240
;
1014 fe
= dvb_attach(tda10021_attach
, &philips_cu1216_config
,
1015 &budget_av
->budget
.i2c_adap
,
1016 read_pwm(budget_av
));
1018 fe
= dvb_attach(tda10021_attach
, &philips_cu1216_config_altaddress
,
1019 &budget_av
->budget
.i2c_adap
,
1020 read_pwm(budget_av
));
1022 fe
->ops
.tuner_ops
.set_params
= philips_cu1216_tuner_set_params
;
1026 case SUBID_DVBC_EASYWATCH_MK3
:
1027 case SUBID_DVBC_CINERGY1200_MK3
:
1028 case SUBID_DVBC_KNC1_MK3
:
1029 case SUBID_DVBC_KNC1_PLUS_MK3
:
1030 budget_av
->reinitialise_demod
= 1;
1031 budget_av
->budget
.dev
->i2c_bitrate
= SAA7146_I2C_BUS_BIT_RATE_240
;
1032 fe
= dvb_attach(tda10023_attach
, &philips_cu1216_config
,
1033 &budget_av
->budget
.i2c_adap
,
1034 read_pwm(budget_av
));
1036 fe
->ops
.tuner_ops
.set_params
= philips_cu1216_tuner_set_params
;
1040 case SUBID_DVBT_KNC1
:
1041 case SUBID_DVBT_KNC1_PLUS
:
1042 case SUBID_DVBT_CINERGY1200
:
1043 budget_av
->reinitialise_demod
= 1;
1044 fe
= dvb_attach(tda10046_attach
, &philips_tu1216_config
,
1045 &budget_av
->budget
.i2c_adap
);
1047 fe
->ops
.tuner_ops
.init
= philips_tu1216_tuner_init
;
1048 fe
->ops
.tuner_ops
.set_params
= philips_tu1216_tuner_set_params
;
1054 printk(KERN_ERR
"budget-av: A frontend driver was not found "
1055 "for device %04x/%04x subsystem %04x/%04x\n",
1058 saa
->pci
->subsystem_vendor
,
1059 saa
->pci
->subsystem_device
);
1063 budget_av
->budget
.dvb_frontend
= fe
;
1065 if (dvb_register_frontend(&budget_av
->budget
.dvb_adapter
,
1066 budget_av
->budget
.dvb_frontend
)) {
1067 printk(KERN_ERR
"budget-av: Frontend registration failed!\n");
1068 dvb_frontend_detach(budget_av
->budget
.dvb_frontend
);
1069 budget_av
->budget
.dvb_frontend
= NULL
;
1074 static void budget_av_irq(struct saa7146_dev
*dev
, u32
* isr
)
1076 struct budget_av
*budget_av
= (struct budget_av
*) dev
->ext_priv
;
1078 dprintk(8, "dev: %p, budget_av: %p\n", dev
, budget_av
);
1081 ttpci_budget_irq10_handler(dev
, isr
);
1084 static int budget_av_detach(struct saa7146_dev
*dev
)
1086 struct budget_av
*budget_av
= (struct budget_av
*) dev
->ext_priv
;
1089 dprintk(2, "dev: %p\n", dev
);
1091 if (1 == budget_av
->has_saa7113
) {
1092 saa7146_setgpio(dev
, 0, SAA7146_GPIO_OUTLO
);
1096 saa7146_unregister_device(&budget_av
->vd
, dev
);
1098 saa7146_vv_release(dev
);
1101 if (budget_av
->budget
.ci_present
)
1102 ciintf_deinit(budget_av
);
1104 if (budget_av
->budget
.dvb_frontend
!= NULL
) {
1105 dvb_unregister_frontend(budget_av
->budget
.dvb_frontend
);
1106 dvb_frontend_detach(budget_av
->budget
.dvb_frontend
);
1108 err
= ttpci_budget_deinit(&budget_av
->budget
);
1115 static struct saa7146_ext_vv vv_data
;
1117 static int budget_av_attach(struct saa7146_dev
*dev
, struct saa7146_pci_extension_data
*info
)
1119 struct budget_av
*budget_av
;
1123 dprintk(2, "dev: %p\n", dev
);
1125 if (!(budget_av
= kzalloc(sizeof(struct budget_av
), GFP_KERNEL
)))
1128 budget_av
->has_saa7113
= 0;
1129 budget_av
->budget
.ci_present
= 0;
1131 dev
->ext_priv
= budget_av
;
1133 if ((err
= ttpci_budget_init(&budget_av
->budget
, dev
, info
, THIS_MODULE
))) {
1138 /* knc1 initialization */
1139 saa7146_write(dev
, DD1_STREAM_B
, 0x04000000);
1140 saa7146_write(dev
, DD1_INIT
, 0x07000600);
1141 saa7146_write(dev
, MC2
, MASK_09
| MASK_25
| MASK_10
| MASK_26
);
1143 if (saa7113_init(budget_av
) == 0) {
1144 budget_av
->has_saa7113
= 1;
1146 if (0 != saa7146_vv_init(dev
, &vv_data
)) {
1147 /* fixme: proper cleanup here */
1148 ERR(("cannot init vv subsystem.\n"));
1152 if ((err
= saa7146_register_device(&budget_av
->vd
, dev
, "knc1", VFL_TYPE_GRABBER
))) {
1153 /* fixme: proper cleanup here */
1154 ERR(("cannot register capture v4l2 device.\n"));
1155 saa7146_vv_release(dev
);
1159 /* beware: this modifies dev->vv ... */
1160 saa7146_set_hps_source_and_sync(dev
, SAA7146_HPS_SOURCE_PORT_A
,
1161 SAA7146_HPS_SYNC_PORT_A
);
1163 saa7113_setinput(budget_av
, 0);
1166 /* fixme: find some sane values here... */
1167 saa7146_write(dev
, PCI_BT_V1
, 0x1c00101f);
1169 mac
= budget_av
->budget
.dvb_adapter
.proposed_mac
;
1170 if (i2c_readregs(&budget_av
->budget
.i2c_adap
, 0xa0, 0x30, mac
, 6)) {
1171 printk(KERN_ERR
"KNC1-%d: Could not read MAC from KNC1 card\n",
1172 budget_av
->budget
.dvb_adapter
.num
);
1175 printk(KERN_INFO
"KNC1-%d: MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
1176 budget_av
->budget
.dvb_adapter
.num
,
1177 mac
[0], mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
1180 budget_av
->budget
.dvb_adapter
.priv
= budget_av
;
1181 frontend_init(budget_av
);
1182 ciintf_init(budget_av
);
1184 ttpci_budget_init_hooks(&budget_av
->budget
);
1189 #define KNC1_INPUTS 2
1190 static struct v4l2_input knc1_inputs
[KNC1_INPUTS
] = {
1191 {0, "Composite", V4L2_INPUT_TYPE_TUNER
, 1, 0, V4L2_STD_PAL_BG
| V4L2_STD_NTSC_M
, 0},
1192 {1, "S-Video", V4L2_INPUT_TYPE_CAMERA
, 2, 0, V4L2_STD_PAL_BG
| V4L2_STD_NTSC_M
, 0},
1195 static struct saa7146_extension_ioctls ioctls
[] = {
1196 {VIDIOC_ENUMINPUT
, SAA7146_EXCLUSIVE
},
1197 {VIDIOC_G_INPUT
, SAA7146_EXCLUSIVE
},
1198 {VIDIOC_S_INPUT
, SAA7146_EXCLUSIVE
},
1202 static int av_ioctl(struct saa7146_fh
*fh
, unsigned int cmd
, void *arg
)
1204 struct saa7146_dev
*dev
= fh
->dev
;
1205 struct budget_av
*budget_av
= (struct budget_av
*) dev
->ext_priv
;
1208 case VIDIOC_ENUMINPUT
:{
1209 struct v4l2_input
*i
= arg
;
1211 dprintk(1, "VIDIOC_ENUMINPUT %d.\n", i
->index
);
1212 if (i
->index
< 0 || i
->index
>= KNC1_INPUTS
) {
1215 memcpy(i
, &knc1_inputs
[i
->index
], sizeof(struct v4l2_input
));
1218 case VIDIOC_G_INPUT
:{
1219 int *input
= (int *) arg
;
1221 *input
= budget_av
->cur_input
;
1223 dprintk(1, "VIDIOC_G_INPUT %d.\n", *input
);
1226 case VIDIOC_S_INPUT
:{
1227 int input
= *(int *) arg
;
1228 dprintk(1, "VIDIOC_S_INPUT %d.\n", input
);
1229 return saa7113_setinput(budget_av
, input
);
1232 return -ENOIOCTLCMD
;
1237 static struct saa7146_standard standard
[] = {
1238 {.name
= "PAL",.id
= V4L2_STD_PAL
,
1239 .v_offset
= 0x17,.v_field
= 288,
1240 .h_offset
= 0x14,.h_pixels
= 680,
1241 .v_max_out
= 576,.h_max_out
= 768 },
1243 {.name
= "NTSC",.id
= V4L2_STD_NTSC
,
1244 .v_offset
= 0x16,.v_field
= 240,
1245 .h_offset
= 0x06,.h_pixels
= 708,
1246 .v_max_out
= 480,.h_max_out
= 640, },
1249 static struct saa7146_ext_vv vv_data
= {
1251 .capabilities
= 0, // perhaps later: V4L2_CAP_VBI_CAPTURE, but that need tweaking with the saa7113
1253 .stds
= &standard
[0],
1254 .num_stds
= sizeof(standard
) / sizeof(struct saa7146_standard
),
1255 .ioctls
= &ioctls
[0],
1259 static struct saa7146_extension budget_extension
;
1261 MAKE_BUDGET_INFO(knc1s
, "KNC1 DVB-S", BUDGET_KNC1S
);
1262 MAKE_BUDGET_INFO(knc1c
, "KNC1 DVB-C", BUDGET_KNC1C
);
1263 MAKE_BUDGET_INFO(knc1t
, "KNC1 DVB-T", BUDGET_KNC1T
);
1264 MAKE_BUDGET_INFO(kncxs
, "KNC TV STAR DVB-S", BUDGET_TVSTAR
);
1265 MAKE_BUDGET_INFO(satewpls
, "Satelco EasyWatch DVB-S light", BUDGET_TVSTAR
);
1266 MAKE_BUDGET_INFO(satewpls1
, "Satelco EasyWatch DVB-S light", BUDGET_KNC1S
);
1267 MAKE_BUDGET_INFO(satewplc
, "Satelco EasyWatch DVB-C", BUDGET_KNC1CP
);
1268 MAKE_BUDGET_INFO(satewcmk3
, "Satelco EasyWatch DVB-C MK3", BUDGET_KNC1C_MK3
);
1269 MAKE_BUDGET_INFO(knc1sp
, "KNC1 DVB-S Plus", BUDGET_KNC1SP
);
1270 MAKE_BUDGET_INFO(knc1cp
, "KNC1 DVB-C Plus", BUDGET_KNC1CP
);
1271 MAKE_BUDGET_INFO(knc1cmk3
, "KNC1 DVB-C MK3", BUDGET_KNC1C_MK3
);
1272 MAKE_BUDGET_INFO(knc1cpmk3
, "KNC1 DVB-C Plus MK3", BUDGET_KNC1CP_MK3
);
1273 MAKE_BUDGET_INFO(knc1tp
, "KNC1 DVB-T Plus", BUDGET_KNC1TP
);
1274 MAKE_BUDGET_INFO(cin1200s
, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S
);
1275 MAKE_BUDGET_INFO(cin1200sn
, "TerraTec Cinergy 1200 DVB-S", BUDGET_CIN1200S
);
1276 MAKE_BUDGET_INFO(cin1200c
, "Terratec Cinergy 1200 DVB-C", BUDGET_CIN1200C
);
1277 MAKE_BUDGET_INFO(cin1200cmk3
, "Terratec Cinergy 1200 DVB-C MK3", BUDGET_CIN1200C_MK3
);
1278 MAKE_BUDGET_INFO(cin1200t
, "Terratec Cinergy 1200 DVB-T", BUDGET_CIN1200T
);
1280 static struct pci_device_id pci_tbl
[] = {
1281 MAKE_EXTENSION_PCI(knc1s
, 0x1131, 0x4f56),
1282 MAKE_EXTENSION_PCI(knc1s
, 0x1131, 0x0010),
1283 MAKE_EXTENSION_PCI(knc1s
, 0x1894, 0x0010),
1284 MAKE_EXTENSION_PCI(knc1sp
, 0x1131, 0x0011),
1285 MAKE_EXTENSION_PCI(knc1sp
, 0x1894, 0x0011),
1286 MAKE_EXTENSION_PCI(kncxs
, 0x1894, 0x0014),
1287 MAKE_EXTENSION_PCI(kncxs
, 0x1894, 0x0016),
1288 MAKE_EXTENSION_PCI(satewpls
, 0x1894, 0x001e),
1289 MAKE_EXTENSION_PCI(satewpls1
, 0x1894, 0x001a),
1290 MAKE_EXTENSION_PCI(satewplc
, 0x1894, 0x002a),
1291 MAKE_EXTENSION_PCI(satewcmk3
, 0x1894, 0x002c),
1292 MAKE_EXTENSION_PCI(knc1c
, 0x1894, 0x0020),
1293 MAKE_EXTENSION_PCI(knc1cp
, 0x1894, 0x0021),
1294 MAKE_EXTENSION_PCI(knc1cmk3
, 0x1894, 0x0022),
1295 MAKE_EXTENSION_PCI(knc1cpmk3
, 0x1894, 0x0023),
1296 MAKE_EXTENSION_PCI(knc1t
, 0x1894, 0x0030),
1297 MAKE_EXTENSION_PCI(knc1tp
, 0x1894, 0x0031),
1298 MAKE_EXTENSION_PCI(cin1200s
, 0x153b, 0x1154),
1299 MAKE_EXTENSION_PCI(cin1200sn
, 0x153b, 0x1155),
1300 MAKE_EXTENSION_PCI(cin1200c
, 0x153b, 0x1156),
1301 MAKE_EXTENSION_PCI(cin1200cmk3
, 0x153b, 0x1176),
1302 MAKE_EXTENSION_PCI(cin1200t
, 0x153b, 0x1157),
1308 MODULE_DEVICE_TABLE(pci
, pci_tbl
);
1310 static struct saa7146_extension budget_extension
= {
1311 .name
= "budget_av",
1312 .flags
= SAA7146_USE_I2C_IRQ
,
1316 .module
= THIS_MODULE
,
1317 .attach
= budget_av_attach
,
1318 .detach
= budget_av_detach
,
1320 .irq_mask
= MASK_10
,
1321 .irq_func
= budget_av_irq
,
1324 static int __init
budget_av_init(void)
1326 return saa7146_register_extension(&budget_extension
);
1329 static void __exit
budget_av_exit(void)
1331 saa7146_unregister_extension(&budget_extension
);
1334 module_init(budget_av_init
);
1335 module_exit(budget_av_exit
);
1337 MODULE_LICENSE("GPL");
1338 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, Michael Hunold, others");
1339 MODULE_DESCRIPTION("driver for the SAA7146 based so-called "
1340 "budget PCI DVB w/ analog input and CI-module (e.g. the KNC cards)");