1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Driver for STV0297 demodulator
5 Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
6 Copyright (C) 2003-2004 Dennis Noermann <dennis.noermann@noernet.de>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <linux/jiffies.h>
16 #include <linux/slab.h>
18 #include <media/dvb_frontend.h>
21 struct stv0297_state
{
22 struct i2c_adapter
*i2c
;
23 const struct stv0297_config
*config
;
24 struct dvb_frontend frontend
;
26 unsigned long last_ber
;
27 unsigned long base_freq
;
31 #define dprintk(x...) printk(x)
36 #define STV0297_CLOCK_KHZ 28900
39 static int stv0297_writereg(struct stv0297_state
*state
, u8 reg
, u8 data
)
42 u8 buf
[] = { reg
, data
};
43 struct i2c_msg msg
= {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= buf
,.len
= 2 };
45 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
48 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
49 __func__
, reg
, data
, ret
);
51 return (ret
!= 1) ? -1 : 0;
54 static int stv0297_readreg(struct stv0297_state
*state
, u8 reg
)
59 struct i2c_msg msg
[] = { {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= b0
,.len
= 1},
60 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b1
,.len
= 1}
63 // this device needs a STOP between the register and data
64 if (state
->config
->stop_during_read
) {
65 if ((ret
= i2c_transfer(state
->i2c
, &msg
[0], 1)) != 1) {
66 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg
, ret
);
69 if ((ret
= i2c_transfer(state
->i2c
, &msg
[1], 1)) != 1) {
70 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg
, ret
);
74 if ((ret
= i2c_transfer(state
->i2c
, msg
, 2)) != 2) {
75 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg
, ret
);
83 static int stv0297_writereg_mask(struct stv0297_state
*state
, u8 reg
, u8 mask
, u8 data
)
87 val
= stv0297_readreg(state
, reg
);
90 stv0297_writereg(state
, reg
, val
);
95 static int stv0297_readregs(struct stv0297_state
*state
, u8 reg1
, u8
* b
, u8 len
)
98 struct i2c_msg msg
[] = { {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
=
100 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b
,.len
= len
}
103 // this device needs a STOP between the register and data
104 if (state
->config
->stop_during_read
) {
105 if ((ret
= i2c_transfer(state
->i2c
, &msg
[0], 1)) != 1) {
106 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg1
, ret
);
109 if ((ret
= i2c_transfer(state
->i2c
, &msg
[1], 1)) != 1) {
110 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg1
, ret
);
114 if ((ret
= i2c_transfer(state
->i2c
, msg
, 2)) != 2) {
115 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __func__
, reg1
, ret
);
123 static u32
stv0297_get_symbolrate(struct stv0297_state
*state
)
127 tmp
= (u64
)(stv0297_readreg(state
, 0x55)
128 | (stv0297_readreg(state
, 0x56) << 8)
129 | (stv0297_readreg(state
, 0x57) << 16)
130 | (stv0297_readreg(state
, 0x58) << 24));
132 tmp
*= STV0297_CLOCK_KHZ
;
138 static void stv0297_set_symbolrate(struct stv0297_state
*state
, u32 srate
)
142 tmp
= 131072L * srate
; /* 131072 = 2^17 */
143 tmp
= tmp
/ (STV0297_CLOCK_KHZ
/ 4); /* 1/4 = 2^-2 */
144 tmp
= tmp
* 8192L; /* 8192 = 2^13 */
146 stv0297_writereg(state
, 0x55, (unsigned char) (tmp
& 0xFF));
147 stv0297_writereg(state
, 0x56, (unsigned char) (tmp
>> 8));
148 stv0297_writereg(state
, 0x57, (unsigned char) (tmp
>> 16));
149 stv0297_writereg(state
, 0x58, (unsigned char) (tmp
>> 24));
152 static void stv0297_set_sweeprate(struct stv0297_state
*state
, short fshift
, long symrate
)
156 tmp
= (long) fshift
*262144L; /* 262144 = 2*18 */
158 tmp
*= 1024; /* 1024 = 2*10 */
168 stv0297_writereg(state
, 0x60, tmp
& 0xFF);
169 stv0297_writereg_mask(state
, 0x69, 0xF0, (tmp
>> 4) & 0xf0);
172 static void stv0297_set_carrieroffset(struct stv0297_state
*state
, long offset
)
176 /* symrate is hardcoded to 10000 */
177 tmp
= offset
* 26844L; /* (2**28)/10000 */
182 stv0297_writereg(state
, 0x66, (unsigned char) (tmp
& 0xFF));
183 stv0297_writereg(state
, 0x67, (unsigned char) (tmp
>> 8));
184 stv0297_writereg(state
, 0x68, (unsigned char) (tmp
>> 16));
185 stv0297_writereg_mask(state
, 0x69, 0x0F, (tmp
>> 24) & 0x0f);
189 static long stv0297_get_carrieroffset(struct stv0297_state *state)
193 stv0297_writereg(state, 0x6B, 0x00);
195 tmp = stv0297_readreg(state, 0x66);
196 tmp |= (stv0297_readreg(state, 0x67) << 8);
197 tmp |= (stv0297_readreg(state, 0x68) << 16);
198 tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
200 tmp *= stv0297_get_symbolrate(state);
207 static void stv0297_set_initialdemodfreq(struct stv0297_state
*state
, long freq
)
212 freq
-= STV0297_CLOCK_KHZ
;
214 tmp
= (STV0297_CLOCK_KHZ
* 1000) / (1 << 16);
215 tmp
= (freq
* 1000) / tmp
;
219 stv0297_writereg_mask(state
, 0x25, 0x80, 0x80);
220 stv0297_writereg(state
, 0x21, tmp
>> 8);
221 stv0297_writereg(state
, 0x20, tmp
);
224 static int stv0297_set_qam(struct stv0297_state
*state
,
225 enum fe_modulation modulation
)
229 switch (modulation
) {
254 stv0297_writereg_mask(state
, 0x00, 0x70, val
<< 4);
259 static int stv0297_set_inversion(struct stv0297_state
*state
,
260 enum fe_spectral_inversion inversion
)
277 stv0297_writereg_mask(state
, 0x83, 0x08, val
<< 3);
282 static int stv0297_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
284 struct stv0297_state
*state
= fe
->demodulator_priv
;
287 stv0297_writereg(state
, 0x87, 0x78);
288 stv0297_writereg(state
, 0x86, 0xc8);
294 static int stv0297_init(struct dvb_frontend
*fe
)
296 struct stv0297_state
*state
= fe
->demodulator_priv
;
299 /* load init table */
300 for (i
=0; !(state
->config
->inittab
[i
] == 0xff && state
->config
->inittab
[i
+1] == 0xff); i
+=2)
301 stv0297_writereg(state
, state
->config
->inittab
[i
], state
->config
->inittab
[i
+1]);
309 static int stv0297_sleep(struct dvb_frontend
*fe
)
311 struct stv0297_state
*state
= fe
->demodulator_priv
;
313 stv0297_writereg_mask(state
, 0x80, 1, 1);
318 static int stv0297_read_status(struct dvb_frontend
*fe
,
319 enum fe_status
*status
)
321 struct stv0297_state
*state
= fe
->demodulator_priv
;
323 u8 sync
= stv0297_readreg(state
, 0xDF);
328 FE_HAS_SYNC
| FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_LOCK
;
332 static int stv0297_read_ber(struct dvb_frontend
*fe
, u32
* ber
)
334 struct stv0297_state
*state
= fe
->demodulator_priv
;
337 stv0297_readregs(state
, 0xA0, BER
, 3);
338 if (!(BER
[0] & 0x80)) {
339 state
->last_ber
= BER
[2] << 8 | BER
[1];
340 stv0297_writereg_mask(state
, 0xA0, 0x80, 0x80);
343 *ber
= state
->last_ber
;
349 static int stv0297_read_signal_strength(struct dvb_frontend
*fe
, u16
* strength
)
351 struct stv0297_state
*state
= fe
->demodulator_priv
;
355 stv0297_readregs(state
, 0x41, STRENGTH
, 3);
356 tmp
= (STRENGTH
[1] & 0x03) << 8 | STRENGTH
[0];
357 if (STRENGTH
[2] & 0x20) {
368 *strength
= (tmp
<< 7) | (tmp
>> 2);
372 static int stv0297_read_snr(struct dvb_frontend
*fe
, u16
* snr
)
374 struct stv0297_state
*state
= fe
->demodulator_priv
;
377 stv0297_readregs(state
, 0x07, SNR
, 2);
378 *snr
= SNR
[1] << 8 | SNR
[0];
383 static int stv0297_read_ucblocks(struct dvb_frontend
*fe
, u32
* ucblocks
)
385 struct stv0297_state
*state
= fe
->demodulator_priv
;
387 stv0297_writereg_mask(state
, 0xDF, 0x03, 0x03); /* freeze the counters */
389 *ucblocks
= (stv0297_readreg(state
, 0xD5) << 8)
390 | stv0297_readreg(state
, 0xD4);
392 stv0297_writereg_mask(state
, 0xDF, 0x03, 0x02); /* clear the counters */
393 stv0297_writereg_mask(state
, 0xDF, 0x03, 0x01); /* re-enable the counters */
398 static int stv0297_set_frontend(struct dvb_frontend
*fe
)
400 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
401 struct stv0297_state
*state
= fe
->demodulator_priv
;
408 unsigned long timeout
;
409 enum fe_spectral_inversion inversion
;
411 switch (p
->modulation
) {
429 // determine inversion dependent parameters
430 inversion
= p
->inversion
;
431 if (state
->config
->invert
)
432 inversion
= (inversion
== INVERSION_ON
) ? INVERSION_OFF
: INVERSION_ON
;
433 carrieroffset
= -330;
439 sweeprate
= -sweeprate
;
440 carrieroffset
= -carrieroffset
;
448 if (fe
->ops
.tuner_ops
.set_params
) {
449 fe
->ops
.tuner_ops
.set_params(fe
);
450 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
453 /* clear software interrupts */
454 stv0297_writereg(state
, 0x82, 0x0);
456 /* set initial demodulation frequency */
457 stv0297_set_initialdemodfreq(state
, 7250);
460 stv0297_writereg_mask(state
, 0x43, 0x10, 0x00);
461 stv0297_writereg(state
, 0x41, 0x00);
462 stv0297_writereg_mask(state
, 0x42, 0x03, 0x01);
463 stv0297_writereg_mask(state
, 0x36, 0x60, 0x00);
464 stv0297_writereg_mask(state
, 0x36, 0x18, 0x00);
465 stv0297_writereg_mask(state
, 0x71, 0x80, 0x80);
466 stv0297_writereg(state
, 0x72, 0x00);
467 stv0297_writereg(state
, 0x73, 0x00);
468 stv0297_writereg_mask(state
, 0x74, 0x0F, 0x00);
469 stv0297_writereg_mask(state
, 0x43, 0x08, 0x00);
470 stv0297_writereg_mask(state
, 0x71, 0x80, 0x00);
473 stv0297_writereg_mask(state
, 0x5a, 0x20, 0x20);
474 stv0297_writereg_mask(state
, 0x5b, 0x02, 0x02);
475 stv0297_writereg_mask(state
, 0x5b, 0x02, 0x00);
476 stv0297_writereg_mask(state
, 0x5b, 0x01, 0x00);
477 stv0297_writereg_mask(state
, 0x5a, 0x40, 0x40);
479 /* disable frequency sweep */
480 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x00);
482 /* reset deinterleaver */
483 stv0297_writereg_mask(state
, 0x81, 0x01, 0x01);
484 stv0297_writereg_mask(state
, 0x81, 0x01, 0x00);
487 stv0297_writereg_mask(state
, 0x83, 0x20, 0x20);
488 stv0297_writereg_mask(state
, 0x83, 0x20, 0x00);
490 /* reset equaliser */
491 u_threshold
= stv0297_readreg(state
, 0x00) & 0xf;
492 initial_u
= stv0297_readreg(state
, 0x01) >> 4;
493 blind_u
= stv0297_readreg(state
, 0x01) & 0xf;
494 stv0297_writereg_mask(state
, 0x84, 0x01, 0x01);
495 stv0297_writereg_mask(state
, 0x84, 0x01, 0x00);
496 stv0297_writereg_mask(state
, 0x00, 0x0f, u_threshold
);
497 stv0297_writereg_mask(state
, 0x01, 0xf0, initial_u
<< 4);
498 stv0297_writereg_mask(state
, 0x01, 0x0f, blind_u
);
500 /* data comes from internal A/D */
501 stv0297_writereg_mask(state
, 0x87, 0x80, 0x00);
503 /* clear phase registers */
504 stv0297_writereg(state
, 0x63, 0x00);
505 stv0297_writereg(state
, 0x64, 0x00);
506 stv0297_writereg(state
, 0x65, 0x00);
507 stv0297_writereg(state
, 0x66, 0x00);
508 stv0297_writereg(state
, 0x67, 0x00);
509 stv0297_writereg(state
, 0x68, 0x00);
510 stv0297_writereg_mask(state
, 0x69, 0x0f, 0x00);
513 stv0297_set_qam(state
, p
->modulation
);
514 stv0297_set_symbolrate(state
, p
->symbol_rate
/ 1000);
515 stv0297_set_sweeprate(state
, sweeprate
, p
->symbol_rate
/ 1000);
516 stv0297_set_carrieroffset(state
, carrieroffset
);
517 stv0297_set_inversion(state
, inversion
);
520 /* Disable corner detection for higher QAMs */
521 if (p
->modulation
== QAM_128
||
522 p
->modulation
== QAM_256
)
523 stv0297_writereg_mask(state
, 0x88, 0x08, 0x00);
525 stv0297_writereg_mask(state
, 0x88, 0x08, 0x08);
527 stv0297_writereg_mask(state
, 0x5a, 0x20, 0x00);
528 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x01);
529 stv0297_writereg_mask(state
, 0x43, 0x40, 0x40);
530 stv0297_writereg_mask(state
, 0x5b, 0x30, 0x00);
531 stv0297_writereg_mask(state
, 0x03, 0x0c, 0x0c);
532 stv0297_writereg_mask(state
, 0x03, 0x03, 0x03);
533 stv0297_writereg_mask(state
, 0x43, 0x10, 0x10);
535 /* wait for WGAGC lock */
536 timeout
= jiffies
+ msecs_to_jiffies(2000);
537 while (time_before(jiffies
, timeout
)) {
539 if (stv0297_readreg(state
, 0x43) & 0x08)
542 if (time_after(jiffies
, timeout
)) {
547 /* wait for equaliser partial convergence */
548 timeout
= jiffies
+ msecs_to_jiffies(500);
549 while (time_before(jiffies
, timeout
)) {
552 if (stv0297_readreg(state
, 0x82) & 0x04) {
556 if (time_after(jiffies
, timeout
)) {
560 /* wait for equaliser full convergence */
561 timeout
= jiffies
+ msecs_to_jiffies(delay
);
562 while (time_before(jiffies
, timeout
)) {
565 if (stv0297_readreg(state
, 0x82) & 0x08) {
569 if (time_after(jiffies
, timeout
)) {
574 stv0297_writereg_mask(state
, 0x6a, 1, 0);
575 stv0297_writereg_mask(state
, 0x88, 8, 0);
577 /* wait for main lock */
578 timeout
= jiffies
+ msecs_to_jiffies(20);
579 while (time_before(jiffies
, timeout
)) {
582 if (stv0297_readreg(state
, 0xDF) & 0x80) {
586 if (time_after(jiffies
, timeout
)) {
591 /* is it still locked after that delay? */
592 if (!(stv0297_readreg(state
, 0xDF) & 0x80)) {
597 stv0297_writereg_mask(state
, 0x5a, 0x40, 0x00);
598 state
->base_freq
= p
->frequency
;
602 stv0297_writereg_mask(state
, 0x6a, 0x01, 0x00);
606 static int stv0297_get_frontend(struct dvb_frontend
*fe
,
607 struct dtv_frontend_properties
*p
)
609 struct stv0297_state
*state
= fe
->demodulator_priv
;
612 reg_00
= stv0297_readreg(state
, 0x00);
613 reg_83
= stv0297_readreg(state
, 0x83);
615 p
->frequency
= state
->base_freq
;
616 p
->inversion
= (reg_83
& 0x08) ? INVERSION_ON
: INVERSION_OFF
;
617 if (state
->config
->invert
)
618 p
->inversion
= (p
->inversion
== INVERSION_ON
) ? INVERSION_OFF
: INVERSION_ON
;
619 p
->symbol_rate
= stv0297_get_symbolrate(state
) * 1000;
620 p
->fec_inner
= FEC_NONE
;
622 switch ((reg_00
>> 4) & 0x7) {
624 p
->modulation
= QAM_16
;
627 p
->modulation
= QAM_32
;
630 p
->modulation
= QAM_128
;
633 p
->modulation
= QAM_256
;
636 p
->modulation
= QAM_64
;
643 static void stv0297_release(struct dvb_frontend
*fe
)
645 struct stv0297_state
*state
= fe
->demodulator_priv
;
649 static const struct dvb_frontend_ops stv0297_ops
;
651 struct dvb_frontend
*stv0297_attach(const struct stv0297_config
*config
,
652 struct i2c_adapter
*i2c
)
654 struct stv0297_state
*state
= NULL
;
656 /* allocate memory for the internal state */
657 state
= kzalloc(sizeof(struct stv0297_state
), GFP_KERNEL
);
661 /* setup the state */
662 state
->config
= config
;
665 state
->base_freq
= 0;
667 /* check if the demod is there */
668 if ((stv0297_readreg(state
, 0x80) & 0x70) != 0x20)
671 /* create dvb_frontend */
672 memcpy(&state
->frontend
.ops
, &stv0297_ops
, sizeof(struct dvb_frontend_ops
));
673 state
->frontend
.demodulator_priv
= state
;
674 return &state
->frontend
;
681 static const struct dvb_frontend_ops stv0297_ops
= {
682 .delsys
= { SYS_DVBC_ANNEX_A
},
684 .name
= "ST STV0297 DVB-C",
685 .frequency_min_hz
= 47 * MHz
,
686 .frequency_max_hz
= 862 * MHz
,
687 .frequency_stepsize_hz
= 62500,
688 .symbol_rate_min
= 870000,
689 .symbol_rate_max
= 11700000,
690 .caps
= FE_CAN_QAM_16
| FE_CAN_QAM_32
| FE_CAN_QAM_64
|
691 FE_CAN_QAM_128
| FE_CAN_QAM_256
| FE_CAN_FEC_AUTO
},
693 .release
= stv0297_release
,
695 .init
= stv0297_init
,
696 .sleep
= stv0297_sleep
,
697 .i2c_gate_ctrl
= stv0297_i2c_gate_ctrl
,
699 .set_frontend
= stv0297_set_frontend
,
700 .get_frontend
= stv0297_get_frontend
,
702 .read_status
= stv0297_read_status
,
703 .read_ber
= stv0297_read_ber
,
704 .read_signal_strength
= stv0297_read_signal_strength
,
705 .read_snr
= stv0297_read_snr
,
706 .read_ucblocks
= stv0297_read_ucblocks
,
709 MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
710 MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
711 MODULE_LICENSE("GPL");
713 EXPORT_SYMBOL(stv0297_attach
);