2 Driver for STV0297 demodulator
4 Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
5 Copyright (C) 2003-2004 Dennis Noermann <dennis.noermann@noernet.de>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/delay.h>
28 #include "dvb_frontend.h"
31 struct stv0297_state
{
32 struct i2c_adapter
*i2c
;
33 struct dvb_frontend_ops ops
;
34 const struct stv0297_config
*config
;
35 struct dvb_frontend frontend
;
37 unsigned long base_freq
;
41 #define dprintk(x...) printk(x)
46 #define STV0297_CLOCK_KHZ 28900
49 static int stv0297_writereg(struct stv0297_state
*state
, u8 reg
, u8 data
)
52 u8 buf
[] = { reg
, data
};
53 struct i2c_msg msg
= {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= buf
,.len
= 2 };
55 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
58 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
59 "ret == %i)\n", __FUNCTION__
, reg
, data
, ret
);
61 return (ret
!= 1) ? -1 : 0;
64 static int stv0297_readreg(struct stv0297_state
*state
, u8 reg
)
69 struct i2c_msg msg
[] = { {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= b0
,.len
=
71 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b1
,.len
= 1}
74 // this device needs a STOP between the register and data
75 if ((ret
= i2c_transfer(state
->i2c
, &msg
[0], 1)) != 1) {
76 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__
, reg
, ret
);
79 if ((ret
= i2c_transfer(state
->i2c
, &msg
[1], 1)) != 1) {
80 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__
, reg
, ret
);
87 static int stv0297_writereg_mask(struct stv0297_state
*state
, u8 reg
, u8 mask
, u8 data
)
91 val
= stv0297_readreg(state
, reg
);
94 stv0297_writereg(state
, reg
, val
);
99 static int stv0297_readregs(struct stv0297_state
*state
, u8 reg1
, u8
* b
, u8 len
)
102 struct i2c_msg msg
[] = { {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
=
104 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b
,.len
= len
}
107 // this device needs a STOP between the register and data
108 if ((ret
= i2c_transfer(state
->i2c
, &msg
[0], 1)) != 1) {
109 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__
, reg1
, ret
);
112 if ((ret
= i2c_transfer(state
->i2c
, &msg
[1], 1)) != 1) {
113 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__
, reg1
, ret
);
120 static u32
stv0297_get_symbolrate(struct stv0297_state
*state
)
124 tmp
= stv0297_readreg(state
, 0x55);
125 tmp
|= stv0297_readreg(state
, 0x56) << 8;
126 tmp
|= stv0297_readreg(state
, 0x57) << 16;
127 tmp
|= stv0297_readreg(state
, 0x58) << 24;
129 tmp
*= STV0297_CLOCK_KHZ
;
135 static void stv0297_set_symbolrate(struct stv0297_state
*state
, u32 srate
)
139 tmp
= 131072L * srate
; /* 131072 = 2^17 */
140 tmp
= tmp
/ (STV0297_CLOCK_KHZ
/ 4); /* 1/4 = 2^-2 */
141 tmp
= tmp
* 8192L; /* 8192 = 2^13 */
143 stv0297_writereg(state
, 0x55, (unsigned char) (tmp
& 0xFF));
144 stv0297_writereg(state
, 0x56, (unsigned char) (tmp
>> 8));
145 stv0297_writereg(state
, 0x57, (unsigned char) (tmp
>> 16));
146 stv0297_writereg(state
, 0x58, (unsigned char) (tmp
>> 24));
149 static void stv0297_set_sweeprate(struct stv0297_state
*state
, short fshift
, long symrate
)
153 tmp
= (long) fshift
*262144L; /* 262144 = 2*18 */
155 tmp
*= 1024; /* 1024 = 2*10 */
165 stv0297_writereg(state
, 0x60, tmp
& 0xFF);
166 stv0297_writereg_mask(state
, 0x69, 0xF0, (tmp
>> 4) & 0xf0);
169 static void stv0297_set_carrieroffset(struct stv0297_state
*state
, long offset
)
173 /* symrate is hardcoded to 10000 */
174 tmp
= offset
* 26844L; /* (2**28)/10000 */
179 stv0297_writereg(state
, 0x66, (unsigned char) (tmp
& 0xFF));
180 stv0297_writereg(state
, 0x67, (unsigned char) (tmp
>> 8));
181 stv0297_writereg(state
, 0x68, (unsigned char) (tmp
>> 16));
182 stv0297_writereg_mask(state
, 0x69, 0x0F, (tmp
>> 24) & 0x0f);
186 static long stv0297_get_carrieroffset(struct stv0297_state *state)
190 stv0297_writereg(state, 0x6B, 0x00);
192 tmp = stv0297_readreg(state, 0x66);
193 tmp |= (stv0297_readreg(state, 0x67) << 8);
194 tmp |= (stv0297_readreg(state, 0x68) << 16);
195 tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
197 tmp *= stv0297_get_symbolrate(state);
204 static void stv0297_set_initialdemodfreq(struct stv0297_state
*state
, long freq
)
209 freq
-= STV0297_CLOCK_KHZ
;
211 tmp
= (STV0297_CLOCK_KHZ
* 1000) / (1 << 16);
212 tmp
= (freq
* 1000) / tmp
;
216 stv0297_writereg_mask(state
, 0x25, 0x80, 0x80);
217 stv0297_writereg(state
, 0x21, tmp
>> 8);
218 stv0297_writereg(state
, 0x20, tmp
);
221 static int stv0297_set_qam(struct stv0297_state
*state
, fe_modulation_t modulation
)
225 switch (modulation
) {
250 stv0297_writereg_mask(state
, 0x00, 0x70, val
<< 4);
255 static int stv0297_set_inversion(struct stv0297_state
*state
, fe_spectral_inversion_t inversion
)
272 stv0297_writereg_mask(state
, 0x83, 0x08, val
<< 3);
277 int stv0297_enable_plli2c(struct dvb_frontend
*fe
)
279 struct stv0297_state
*state
= fe
->demodulator_priv
;
281 stv0297_writereg(state
, 0x87, 0x78);
282 stv0297_writereg(state
, 0x86, 0xc8);
287 static int stv0297_init(struct dvb_frontend
*fe
)
289 struct stv0297_state
*state
= fe
->demodulator_priv
;
292 /* load init table */
293 for (i
=0; !(state
->config
->inittab
[i
] == 0xff && state
->config
->inittab
[i
+1] == 0xff); i
+=2)
294 stv0297_writereg(state
, state
->config
->inittab
[i
], state
->config
->inittab
[i
+1]);
297 if (state
->config
->pll_init
)
298 state
->config
->pll_init(fe
);
303 static int stv0297_sleep(struct dvb_frontend
*fe
)
305 struct stv0297_state
*state
= fe
->demodulator_priv
;
307 stv0297_writereg_mask(state
, 0x80, 1, 1);
312 static int stv0297_read_status(struct dvb_frontend
*fe
, fe_status_t
* status
)
314 struct stv0297_state
*state
= fe
->demodulator_priv
;
316 u8 sync
= stv0297_readreg(state
, 0xDF);
321 FE_HAS_SYNC
| FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_LOCK
;
325 static int stv0297_read_ber(struct dvb_frontend
*fe
, u32
* ber
)
327 struct stv0297_state
*state
= fe
->demodulator_priv
;
330 stv0297_writereg(state
, 0xA0, 0x80); // Start Counting bit errors for 4096 Bytes
331 mdelay(25); // Hopefully got 4096 Bytes
332 stv0297_readregs(state
, 0xA0, BER
, 3);
334 *ber
= (BER
[2] << 8 | BER
[1]) / (8 * 4096);
340 static int stv0297_read_signal_strength(struct dvb_frontend
*fe
, u16
* strength
)
342 struct stv0297_state
*state
= fe
->demodulator_priv
;
345 stv0297_readregs(state
, 0x41, STRENGTH
, 2);
346 *strength
= (STRENGTH
[1] & 0x03) << 8 | STRENGTH
[0];
351 static int stv0297_read_snr(struct dvb_frontend
*fe
, u16
* snr
)
353 struct stv0297_state
*state
= fe
->demodulator_priv
;
356 stv0297_readregs(state
, 0x07, SNR
, 2);
357 *snr
= SNR
[1] << 8 | SNR
[0];
362 static int stv0297_read_ucblocks(struct dvb_frontend
*fe
, u32
* ucblocks
)
364 struct stv0297_state
*state
= fe
->demodulator_priv
;
366 *ucblocks
= (stv0297_readreg(state
, 0xD5) << 8)
367 | stv0297_readreg(state
, 0xD4);
372 static int stv0297_set_frontend(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*p
)
374 struct stv0297_state
*state
= fe
->demodulator_priv
;
381 unsigned long starttime
;
382 unsigned long timeout
;
383 fe_spectral_inversion_t inversion
;
385 switch (p
->u
.qam
.modulation
) {
407 // determine inversion dependant parameters
408 inversion
= p
->inversion
;
409 if (state
->config
->invert
)
410 inversion
= (inversion
== INVERSION_ON
) ? INVERSION_OFF
: INVERSION_ON
;
411 carrieroffset
= -330;
417 sweeprate
= -sweeprate
;
418 carrieroffset
= -carrieroffset
;
426 state
->config
->pll_set(fe
, p
);
428 /* clear software interrupts */
429 stv0297_writereg(state
, 0x82, 0x0);
431 /* set initial demodulation frequency */
432 stv0297_set_initialdemodfreq(state
, 7250);
435 stv0297_writereg_mask(state
, 0x43, 0x10, 0x00);
436 stv0297_writereg(state
, 0x41, 0x00);
437 stv0297_writereg_mask(state
, 0x42, 0x03, 0x01);
438 stv0297_writereg_mask(state
, 0x36, 0x60, 0x00);
439 stv0297_writereg_mask(state
, 0x36, 0x18, 0x00);
440 stv0297_writereg_mask(state
, 0x71, 0x80, 0x80);
441 stv0297_writereg(state
, 0x72, 0x00);
442 stv0297_writereg(state
, 0x73, 0x00);
443 stv0297_writereg_mask(state
, 0x74, 0x0F, 0x00);
444 stv0297_writereg_mask(state
, 0x43, 0x08, 0x00);
445 stv0297_writereg_mask(state
, 0x71, 0x80, 0x00);
448 stv0297_writereg_mask(state
, 0x5a, 0x20, 0x20);
449 stv0297_writereg_mask(state
, 0x5b, 0x02, 0x02);
450 stv0297_writereg_mask(state
, 0x5b, 0x02, 0x00);
451 stv0297_writereg_mask(state
, 0x5b, 0x01, 0x00);
452 stv0297_writereg_mask(state
, 0x5a, 0x40, 0x40);
454 /* disable frequency sweep */
455 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x00);
457 /* reset deinterleaver */
458 stv0297_writereg_mask(state
, 0x81, 0x01, 0x01);
459 stv0297_writereg_mask(state
, 0x81, 0x01, 0x00);
462 stv0297_writereg_mask(state
, 0x83, 0x20, 0x20);
463 stv0297_writereg_mask(state
, 0x83, 0x20, 0x00);
465 /* reset equaliser */
466 u_threshold
= stv0297_readreg(state
, 0x00) & 0xf;
467 initial_u
= stv0297_readreg(state
, 0x01) >> 4;
468 blind_u
= stv0297_readreg(state
, 0x01) & 0xf;
469 stv0297_writereg_mask(state
, 0x84, 0x01, 0x01);
470 stv0297_writereg_mask(state
, 0x84, 0x01, 0x00);
471 stv0297_writereg_mask(state
, 0x00, 0x0f, u_threshold
);
472 stv0297_writereg_mask(state
, 0x01, 0xf0, initial_u
<< 4);
473 stv0297_writereg_mask(state
, 0x01, 0x0f, blind_u
);
475 /* data comes from internal A/D */
476 stv0297_writereg_mask(state
, 0x87, 0x80, 0x00);
478 /* clear phase registers */
479 stv0297_writereg(state
, 0x63, 0x00);
480 stv0297_writereg(state
, 0x64, 0x00);
481 stv0297_writereg(state
, 0x65, 0x00);
482 stv0297_writereg(state
, 0x66, 0x00);
483 stv0297_writereg(state
, 0x67, 0x00);
484 stv0297_writereg(state
, 0x68, 0x00);
485 stv0297_writereg_mask(state
, 0x69, 0x0f, 0x00);
488 stv0297_set_qam(state
, p
->u
.qam
.modulation
);
489 stv0297_set_symbolrate(state
, p
->u
.qam
.symbol_rate
/ 1000);
490 stv0297_set_sweeprate(state
, sweeprate
, p
->u
.qam
.symbol_rate
/ 1000);
491 stv0297_set_carrieroffset(state
, carrieroffset
);
492 stv0297_set_inversion(state
, inversion
);
495 /* Disable corner detection for higher QAMs */
496 if (p
->u
.qam
.modulation
== QAM_128
||
497 p
->u
.qam
.modulation
== QAM_256
)
498 stv0297_writereg_mask(state
, 0x88, 0x08, 0x00);
500 stv0297_writereg_mask(state
, 0x88, 0x08, 0x08);
502 stv0297_writereg_mask(state
, 0x5a, 0x20, 0x00);
503 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x01);
504 stv0297_writereg_mask(state
, 0x43, 0x40, 0x40);
505 stv0297_writereg_mask(state
, 0x5b, 0x30, 0x00);
506 stv0297_writereg_mask(state
, 0x03, 0x0c, 0x0c);
507 stv0297_writereg_mask(state
, 0x03, 0x03, 0x03);
508 stv0297_writereg_mask(state
, 0x43, 0x10, 0x10);
510 /* wait for WGAGC lock */
512 timeout
= jiffies
+ msecs_to_jiffies(2000);
513 while (time_before(jiffies
, timeout
)) {
515 if (stv0297_readreg(state
, 0x43) & 0x08)
518 if (time_after(jiffies
, timeout
)) {
523 /* wait for equaliser partial convergence */
524 timeout
= jiffies
+ msecs_to_jiffies(500);
525 while (time_before(jiffies
, timeout
)) {
528 if (stv0297_readreg(state
, 0x82) & 0x04) {
532 if (time_after(jiffies
, timeout
)) {
536 /* wait for equaliser full convergence */
537 timeout
= jiffies
+ msecs_to_jiffies(delay
);
538 while (time_before(jiffies
, timeout
)) {
541 if (stv0297_readreg(state
, 0x82) & 0x08) {
545 if (time_after(jiffies
, timeout
)) {
550 stv0297_writereg_mask(state
, 0x6a, 1, 0);
551 stv0297_writereg_mask(state
, 0x88, 8, 0);
553 /* wait for main lock */
554 timeout
= jiffies
+ msecs_to_jiffies(20);
555 while (time_before(jiffies
, timeout
)) {
558 if (stv0297_readreg(state
, 0xDF) & 0x80) {
562 if (time_after(jiffies
, timeout
)) {
567 /* is it still locked after that delay? */
568 if (!(stv0297_readreg(state
, 0xDF) & 0x80)) {
573 stv0297_writereg_mask(state
, 0x5a, 0x40, 0x00);
574 state
->base_freq
= p
->frequency
;
578 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x00);
582 static int stv0297_get_frontend(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*p
)
584 struct stv0297_state
*state
= fe
->demodulator_priv
;
587 reg_00
= stv0297_readreg(state
, 0x00);
588 reg_83
= stv0297_readreg(state
, 0x83);
590 p
->frequency
= state
->base_freq
;
591 p
->inversion
= (reg_83
& 0x08) ? INVERSION_ON
: INVERSION_OFF
;
592 if (state
->config
->invert
)
593 p
->inversion
= (p
->inversion
== INVERSION_ON
) ? INVERSION_OFF
: INVERSION_ON
;
594 p
->u
.qam
.symbol_rate
= stv0297_get_symbolrate(state
) * 1000;
595 p
->u
.qam
.fec_inner
= FEC_NONE
;
597 switch ((reg_00
>> 4) & 0x7) {
599 p
->u
.qam
.modulation
= QAM_16
;
602 p
->u
.qam
.modulation
= QAM_32
;
605 p
->u
.qam
.modulation
= QAM_128
;
608 p
->u
.qam
.modulation
= QAM_256
;
611 p
->u
.qam
.modulation
= QAM_64
;
618 static void stv0297_release(struct dvb_frontend
*fe
)
620 struct stv0297_state
*state
= fe
->demodulator_priv
;
624 static struct dvb_frontend_ops stv0297_ops
;
626 struct dvb_frontend
*stv0297_attach(const struct stv0297_config
*config
,
627 struct i2c_adapter
*i2c
)
629 struct stv0297_state
*state
= NULL
;
631 /* allocate memory for the internal state */
632 state
= kmalloc(sizeof(struct stv0297_state
), GFP_KERNEL
);
636 /* setup the state */
637 state
->config
= config
;
639 memcpy(&state
->ops
, &stv0297_ops
, sizeof(struct dvb_frontend_ops
));
640 state
->base_freq
= 0;
642 /* check if the demod is there */
643 if ((stv0297_readreg(state
, 0x80) & 0x70) != 0x20)
646 /* create dvb_frontend */
647 state
->frontend
.ops
= &state
->ops
;
648 state
->frontend
.demodulator_priv
= state
;
649 return &state
->frontend
;
656 static struct dvb_frontend_ops stv0297_ops
= {
659 .name
= "ST STV0297 DVB-C",
661 .frequency_min
= 64000000,
662 .frequency_max
= 1300000000,
663 .frequency_stepsize
= 62500,
664 .symbol_rate_min
= 870000,
665 .symbol_rate_max
= 11700000,
666 .caps
= FE_CAN_QAM_16
| FE_CAN_QAM_32
| FE_CAN_QAM_64
|
667 FE_CAN_QAM_128
| FE_CAN_QAM_256
| FE_CAN_FEC_AUTO
},
669 .release
= stv0297_release
,
671 .init
= stv0297_init
,
672 .sleep
= stv0297_sleep
,
674 .set_frontend
= stv0297_set_frontend
,
675 .get_frontend
= stv0297_get_frontend
,
677 .read_status
= stv0297_read_status
,
678 .read_ber
= stv0297_read_ber
,
679 .read_signal_strength
= stv0297_read_signal_strength
,
680 .read_snr
= stv0297_read_snr
,
681 .read_ucblocks
= stv0297_read_ucblocks
,
684 MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
685 MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
686 MODULE_LICENSE("GPL");
688 EXPORT_SYMBOL(stv0297_attach
);
689 EXPORT_SYMBOL(stv0297_enable_plli2c
);