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>
27 #include <linux/jiffies.h>
28 #include <linux/slab.h>
30 #include "dvb_frontend.h"
33 struct stv0297_state
{
34 struct i2c_adapter
*i2c
;
35 const struct stv0297_config
*config
;
36 struct dvb_frontend frontend
;
38 unsigned long last_ber
;
39 unsigned long base_freq
;
42 #define dprintk(x...) printk(x)
44 #define STV0297_CLOCK_KHZ 28900
47 static int stv0297_writereg(struct stv0297_state
*state
, u8 reg
, u8 data
)
50 u8 buf
[] = { reg
, data
};
51 struct i2c_msg msg
= {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= buf
,.len
= 2 };
53 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
56 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
57 "ret == %i)\n", __func__
, reg
, data
, ret
);
59 return (ret
!= 1) ? -1 : 0;
62 static int stv0297_readreg(struct stv0297_state
*state
, u8 reg
)
67 struct i2c_msg msg
[] = { {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= b0
,.len
= 1},
68 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b1
,.len
= 1}
71 // this device needs a STOP between the register and data
72 if (state
->config
->stop_during_read
) {
73 if ((ret
= i2c_transfer(state
->i2c
, &msg
[0], 1)) != 1) {
74 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg
, ret
);
77 if ((ret
= i2c_transfer(state
->i2c
, &msg
[1], 1)) != 1) {
78 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg
, ret
);
82 if ((ret
= i2c_transfer(state
->i2c
, msg
, 2)) != 2) {
83 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg
, ret
);
91 static int stv0297_writereg_mask(struct stv0297_state
*state
, u8 reg
, u8 mask
, u8 data
)
95 val
= stv0297_readreg(state
, reg
);
98 stv0297_writereg(state
, reg
, val
);
103 static int stv0297_readregs(struct stv0297_state
*state
, u8 reg1
, u8
* b
, u8 len
)
106 struct i2c_msg msg
[] = { {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
=
108 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b
,.len
= len
}
111 // this device needs a STOP between the register and data
112 if (state
->config
->stop_during_read
) {
113 if ((ret
= i2c_transfer(state
->i2c
, &msg
[0], 1)) != 1) {
114 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg1
, ret
);
117 if ((ret
= i2c_transfer(state
->i2c
, &msg
[1], 1)) != 1) {
118 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg1
, ret
);
122 if ((ret
= i2c_transfer(state
->i2c
, msg
, 2)) != 2) {
123 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg1
, ret
);
131 static u32
stv0297_get_symbolrate(struct stv0297_state
*state
)
135 tmp
= stv0297_readreg(state
, 0x55);
136 tmp
|= stv0297_readreg(state
, 0x56) << 8;
137 tmp
|= stv0297_readreg(state
, 0x57) << 16;
138 tmp
|= stv0297_readreg(state
, 0x58) << 24;
140 tmp
*= STV0297_CLOCK_KHZ
;
146 static void stv0297_set_symbolrate(struct stv0297_state
*state
, u32 srate
)
150 tmp
= 131072L * srate
; /* 131072 = 2^17 */
151 tmp
= tmp
/ (STV0297_CLOCK_KHZ
/ 4); /* 1/4 = 2^-2 */
152 tmp
= tmp
* 8192L; /* 8192 = 2^13 */
154 stv0297_writereg(state
, 0x55, (unsigned char) (tmp
& 0xFF));
155 stv0297_writereg(state
, 0x56, (unsigned char) (tmp
>> 8));
156 stv0297_writereg(state
, 0x57, (unsigned char) (tmp
>> 16));
157 stv0297_writereg(state
, 0x58, (unsigned char) (tmp
>> 24));
160 static void stv0297_set_sweeprate(struct stv0297_state
*state
, short fshift
, long symrate
)
164 tmp
= (long) fshift
*262144L; /* 262144 = 2*18 */
166 tmp
*= 1024; /* 1024 = 2*10 */
176 stv0297_writereg(state
, 0x60, tmp
& 0xFF);
177 stv0297_writereg_mask(state
, 0x69, 0xF0, (tmp
>> 4) & 0xf0);
180 static void stv0297_set_carrieroffset(struct stv0297_state
*state
, long offset
)
184 /* symrate is hardcoded to 10000 */
185 tmp
= offset
* 26844L; /* (2**28)/10000 */
190 stv0297_writereg(state
, 0x66, (unsigned char) (tmp
& 0xFF));
191 stv0297_writereg(state
, 0x67, (unsigned char) (tmp
>> 8));
192 stv0297_writereg(state
, 0x68, (unsigned char) (tmp
>> 16));
193 stv0297_writereg_mask(state
, 0x69, 0x0F, (tmp
>> 24) & 0x0f);
197 static long stv0297_get_carrieroffset(struct stv0297_state *state)
201 stv0297_writereg(state, 0x6B, 0x00);
203 tmp = stv0297_readreg(state, 0x66);
204 tmp |= (stv0297_readreg(state, 0x67) << 8);
205 tmp |= (stv0297_readreg(state, 0x68) << 16);
206 tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
208 tmp *= stv0297_get_symbolrate(state);
215 static void stv0297_set_initialdemodfreq(struct stv0297_state
*state
, long freq
)
220 freq
-= STV0297_CLOCK_KHZ
;
222 tmp
= (STV0297_CLOCK_KHZ
* 1000) / (1 << 16);
223 tmp
= (freq
* 1000) / tmp
;
227 stv0297_writereg_mask(state
, 0x25, 0x80, 0x80);
228 stv0297_writereg(state
, 0x21, tmp
>> 8);
229 stv0297_writereg(state
, 0x20, tmp
);
232 static int stv0297_set_qam(struct stv0297_state
*state
, fe_modulation_t modulation
)
236 switch (modulation
) {
261 stv0297_writereg_mask(state
, 0x00, 0x70, val
<< 4);
266 static int stv0297_set_inversion(struct stv0297_state
*state
, fe_spectral_inversion_t inversion
)
283 stv0297_writereg_mask(state
, 0x83, 0x08, val
<< 3);
288 static int stv0297_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
290 struct stv0297_state
*state
= fe
->demodulator_priv
;
293 stv0297_writereg(state
, 0x87, 0x78);
294 stv0297_writereg(state
, 0x86, 0xc8);
300 static int stv0297_init(struct dvb_frontend
*fe
)
302 struct stv0297_state
*state
= fe
->demodulator_priv
;
305 /* load init table */
306 for (i
=0; !(state
->config
->inittab
[i
] == 0xff && state
->config
->inittab
[i
+1] == 0xff); i
+=2)
307 stv0297_writereg(state
, state
->config
->inittab
[i
], state
->config
->inittab
[i
+1]);
315 static int stv0297_sleep(struct dvb_frontend
*fe
)
317 struct stv0297_state
*state
= fe
->demodulator_priv
;
319 stv0297_writereg_mask(state
, 0x80, 1, 1);
324 static int stv0297_read_status(struct dvb_frontend
*fe
, fe_status_t
* status
)
326 struct stv0297_state
*state
= fe
->demodulator_priv
;
328 u8 sync
= stv0297_readreg(state
, 0xDF);
333 FE_HAS_SYNC
| FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_LOCK
;
337 static int stv0297_read_ber(struct dvb_frontend
*fe
, u32
* ber
)
339 struct stv0297_state
*state
= fe
->demodulator_priv
;
342 stv0297_readregs(state
, 0xA0, BER
, 3);
343 if (!(BER
[0] & 0x80)) {
344 state
->last_ber
= BER
[2] << 8 | BER
[1];
345 stv0297_writereg_mask(state
, 0xA0, 0x80, 0x80);
348 *ber
= state
->last_ber
;
354 static int stv0297_read_signal_strength(struct dvb_frontend
*fe
, u16
* strength
)
356 struct stv0297_state
*state
= fe
->demodulator_priv
;
360 stv0297_readregs(state
, 0x41, STRENGTH
, 3);
361 tmp
= (STRENGTH
[1] & 0x03) << 8 | STRENGTH
[0];
362 if (STRENGTH
[2] & 0x20) {
373 *strength
= (tmp
<< 7) | (tmp
>> 2);
377 static int stv0297_read_snr(struct dvb_frontend
*fe
, u16
* snr
)
379 struct stv0297_state
*state
= fe
->demodulator_priv
;
382 stv0297_readregs(state
, 0x07, SNR
, 2);
383 *snr
= SNR
[1] << 8 | SNR
[0];
388 static int stv0297_read_ucblocks(struct dvb_frontend
*fe
, u32
* ucblocks
)
390 struct stv0297_state
*state
= fe
->demodulator_priv
;
392 stv0297_writereg_mask(state
, 0xDF, 0x03, 0x03); /* freeze the counters */
394 *ucblocks
= (stv0297_readreg(state
, 0xD5) << 8)
395 | stv0297_readreg(state
, 0xD4);
397 stv0297_writereg_mask(state
, 0xDF, 0x03, 0x02); /* clear the counters */
398 stv0297_writereg_mask(state
, 0xDF, 0x03, 0x01); /* re-enable the counters */
403 static int stv0297_set_frontend(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*p
)
405 struct stv0297_state
*state
= fe
->demodulator_priv
;
412 unsigned long starttime
;
413 unsigned long timeout
;
414 fe_spectral_inversion_t inversion
;
416 switch (p
->u
.qam
.modulation
) {
434 // determine inversion dependant parameters
435 inversion
= p
->inversion
;
436 if (state
->config
->invert
)
437 inversion
= (inversion
== INVERSION_ON
) ? INVERSION_OFF
: INVERSION_ON
;
438 carrieroffset
= -330;
444 sweeprate
= -sweeprate
;
445 carrieroffset
= -carrieroffset
;
453 if (fe
->ops
.tuner_ops
.set_params
) {
454 fe
->ops
.tuner_ops
.set_params(fe
, p
);
455 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
458 /* clear software interrupts */
459 stv0297_writereg(state
, 0x82, 0x0);
461 /* set initial demodulation frequency */
462 stv0297_set_initialdemodfreq(state
, 7250);
465 stv0297_writereg_mask(state
, 0x43, 0x10, 0x00);
466 stv0297_writereg(state
, 0x41, 0x00);
467 stv0297_writereg_mask(state
, 0x42, 0x03, 0x01);
468 stv0297_writereg_mask(state
, 0x36, 0x60, 0x00);
469 stv0297_writereg_mask(state
, 0x36, 0x18, 0x00);
470 stv0297_writereg_mask(state
, 0x71, 0x80, 0x80);
471 stv0297_writereg(state
, 0x72, 0x00);
472 stv0297_writereg(state
, 0x73, 0x00);
473 stv0297_writereg_mask(state
, 0x74, 0x0F, 0x00);
474 stv0297_writereg_mask(state
, 0x43, 0x08, 0x00);
475 stv0297_writereg_mask(state
, 0x71, 0x80, 0x00);
478 stv0297_writereg_mask(state
, 0x5a, 0x20, 0x20);
479 stv0297_writereg_mask(state
, 0x5b, 0x02, 0x02);
480 stv0297_writereg_mask(state
, 0x5b, 0x02, 0x00);
481 stv0297_writereg_mask(state
, 0x5b, 0x01, 0x00);
482 stv0297_writereg_mask(state
, 0x5a, 0x40, 0x40);
484 /* disable frequency sweep */
485 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x00);
487 /* reset deinterleaver */
488 stv0297_writereg_mask(state
, 0x81, 0x01, 0x01);
489 stv0297_writereg_mask(state
, 0x81, 0x01, 0x00);
492 stv0297_writereg_mask(state
, 0x83, 0x20, 0x20);
493 stv0297_writereg_mask(state
, 0x83, 0x20, 0x00);
495 /* reset equaliser */
496 u_threshold
= stv0297_readreg(state
, 0x00) & 0xf;
497 initial_u
= stv0297_readreg(state
, 0x01) >> 4;
498 blind_u
= stv0297_readreg(state
, 0x01) & 0xf;
499 stv0297_writereg_mask(state
, 0x84, 0x01, 0x01);
500 stv0297_writereg_mask(state
, 0x84, 0x01, 0x00);
501 stv0297_writereg_mask(state
, 0x00, 0x0f, u_threshold
);
502 stv0297_writereg_mask(state
, 0x01, 0xf0, initial_u
<< 4);
503 stv0297_writereg_mask(state
, 0x01, 0x0f, blind_u
);
505 /* data comes from internal A/D */
506 stv0297_writereg_mask(state
, 0x87, 0x80, 0x00);
508 /* clear phase registers */
509 stv0297_writereg(state
, 0x63, 0x00);
510 stv0297_writereg(state
, 0x64, 0x00);
511 stv0297_writereg(state
, 0x65, 0x00);
512 stv0297_writereg(state
, 0x66, 0x00);
513 stv0297_writereg(state
, 0x67, 0x00);
514 stv0297_writereg(state
, 0x68, 0x00);
515 stv0297_writereg_mask(state
, 0x69, 0x0f, 0x00);
518 stv0297_set_qam(state
, p
->u
.qam
.modulation
);
519 stv0297_set_symbolrate(state
, p
->u
.qam
.symbol_rate
/ 1000);
520 stv0297_set_sweeprate(state
, sweeprate
, p
->u
.qam
.symbol_rate
/ 1000);
521 stv0297_set_carrieroffset(state
, carrieroffset
);
522 stv0297_set_inversion(state
, inversion
);
525 /* Disable corner detection for higher QAMs */
526 if (p
->u
.qam
.modulation
== QAM_128
||
527 p
->u
.qam
.modulation
== QAM_256
)
528 stv0297_writereg_mask(state
, 0x88, 0x08, 0x00);
530 stv0297_writereg_mask(state
, 0x88, 0x08, 0x08);
532 stv0297_writereg_mask(state
, 0x5a, 0x20, 0x00);
533 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x01);
534 stv0297_writereg_mask(state
, 0x43, 0x40, 0x40);
535 stv0297_writereg_mask(state
, 0x5b, 0x30, 0x00);
536 stv0297_writereg_mask(state
, 0x03, 0x0c, 0x0c);
537 stv0297_writereg_mask(state
, 0x03, 0x03, 0x03);
538 stv0297_writereg_mask(state
, 0x43, 0x10, 0x10);
540 /* wait for WGAGC lock */
542 timeout
= jiffies
+ msecs_to_jiffies(2000);
543 while (time_before(jiffies
, timeout
)) {
545 if (stv0297_readreg(state
, 0x43) & 0x08)
548 if (time_after(jiffies
, timeout
)) {
553 /* wait for equaliser partial convergence */
554 timeout
= jiffies
+ msecs_to_jiffies(500);
555 while (time_before(jiffies
, timeout
)) {
558 if (stv0297_readreg(state
, 0x82) & 0x04) {
562 if (time_after(jiffies
, timeout
)) {
566 /* wait for equaliser full convergence */
567 timeout
= jiffies
+ msecs_to_jiffies(delay
);
568 while (time_before(jiffies
, timeout
)) {
571 if (stv0297_readreg(state
, 0x82) & 0x08) {
575 if (time_after(jiffies
, timeout
)) {
580 stv0297_writereg_mask(state
, 0x6a, 1, 0);
581 stv0297_writereg_mask(state
, 0x88, 8, 0);
583 /* wait for main lock */
584 timeout
= jiffies
+ msecs_to_jiffies(20);
585 while (time_before(jiffies
, timeout
)) {
588 if (stv0297_readreg(state
, 0xDF) & 0x80) {
592 if (time_after(jiffies
, timeout
)) {
597 /* is it still locked after that delay? */
598 if (!(stv0297_readreg(state
, 0xDF) & 0x80)) {
603 stv0297_writereg_mask(state
, 0x5a, 0x40, 0x00);
604 state
->base_freq
= p
->frequency
;
608 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x00);
612 static int stv0297_get_frontend(struct dvb_frontend
*fe
, struct dvb_frontend_parameters
*p
)
614 struct stv0297_state
*state
= fe
->demodulator_priv
;
617 reg_00
= stv0297_readreg(state
, 0x00);
618 reg_83
= stv0297_readreg(state
, 0x83);
620 p
->frequency
= state
->base_freq
;
621 p
->inversion
= (reg_83
& 0x08) ? INVERSION_ON
: INVERSION_OFF
;
622 if (state
->config
->invert
)
623 p
->inversion
= (p
->inversion
== INVERSION_ON
) ? INVERSION_OFF
: INVERSION_ON
;
624 p
->u
.qam
.symbol_rate
= stv0297_get_symbolrate(state
) * 1000;
625 p
->u
.qam
.fec_inner
= FEC_NONE
;
627 switch ((reg_00
>> 4) & 0x7) {
629 p
->u
.qam
.modulation
= QAM_16
;
632 p
->u
.qam
.modulation
= QAM_32
;
635 p
->u
.qam
.modulation
= QAM_128
;
638 p
->u
.qam
.modulation
= QAM_256
;
641 p
->u
.qam
.modulation
= QAM_64
;
648 static void stv0297_release(struct dvb_frontend
*fe
)
650 struct stv0297_state
*state
= fe
->demodulator_priv
;
654 static struct dvb_frontend_ops stv0297_ops
;
656 struct dvb_frontend
*stv0297_attach(const struct stv0297_config
*config
,
657 struct i2c_adapter
*i2c
)
659 struct stv0297_state
*state
= NULL
;
661 /* allocate memory for the internal state */
662 state
= kzalloc(sizeof(struct stv0297_state
), GFP_KERNEL
);
666 /* setup the state */
667 state
->config
= config
;
670 state
->base_freq
= 0;
672 /* check if the demod is there */
673 if ((stv0297_readreg(state
, 0x80) & 0x70) != 0x20)
676 /* create dvb_frontend */
677 memcpy(&state
->frontend
.ops
, &stv0297_ops
, sizeof(struct dvb_frontend_ops
));
678 state
->frontend
.demodulator_priv
= state
;
679 return &state
->frontend
;
686 static struct dvb_frontend_ops stv0297_ops
= {
689 .name
= "ST STV0297 DVB-C",
691 .frequency_min
= 47000000,
692 .frequency_max
= 862000000,
693 .frequency_stepsize
= 62500,
694 .symbol_rate_min
= 870000,
695 .symbol_rate_max
= 11700000,
696 .caps
= FE_CAN_QAM_16
| FE_CAN_QAM_32
| FE_CAN_QAM_64
|
697 FE_CAN_QAM_128
| FE_CAN_QAM_256
| FE_CAN_FEC_AUTO
},
699 .release
= stv0297_release
,
701 .init
= stv0297_init
,
702 .sleep
= stv0297_sleep
,
703 .i2c_gate_ctrl
= stv0297_i2c_gate_ctrl
,
705 .set_frontend
= stv0297_set_frontend
,
706 .get_frontend
= stv0297_get_frontend
,
708 .read_status
= stv0297_read_status
,
709 .read_ber
= stv0297_read_ber
,
710 .read_signal_strength
= stv0297_read_signal_strength
,
711 .read_snr
= stv0297_read_snr
,
712 .read_ucblocks
= stv0297_read_ucblocks
,
715 MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
716 MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
717 MODULE_LICENSE("GPL");
719 EXPORT_SYMBOL(stv0297_attach
);