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;
42 #define dprintk(x...) printk(x)
47 #define STV0297_CLOCK_KHZ 28900
49 static u8 init_tab[] = {
138 static int stv0297_writereg(struct stv0297_state *state, u8 reg, u8 data)
141 u8 buf[] = { reg, data };
142 struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 2 };
144 ret = i2c_transfer(state->i2c, &msg, 1);
147 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
148 "ret == %i)\n", __FUNCTION__, reg, data, ret);
150 return (ret != 1) ? -1 : 0;
153 static int stv0297_readreg(struct stv0297_state *state, u8 reg)
158 struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len =
160 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1}
163 // this device needs a STOP between the register and data
164 if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
165 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret);
168 if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
169 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret);
176 static int stv0297_writereg_mask(struct stv0297_state *state, u8 reg, u8 mask, u8 data)
180 val = stv0297_readreg(state, reg);
182 val |= (data & mask);
183 stv0297_writereg(state, reg, val);
188 static int stv0297_readregs(struct stv0297_state *state, u8 reg1, u8 * b, u8 len)
191 struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf =
193 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b,.len = len}
196 // this device needs a STOP between the register and data
197 if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
198 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);
201 if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
202 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);
209 static u32 stv0297_get_symbolrate(struct stv0297_state *state)
213 tmp = stv0297_readreg(state, 0x55);
214 tmp |= stv0297_readreg(state, 0x56) << 8;
215 tmp |= stv0297_readreg(state, 0x57) << 16;
216 tmp |= stv0297_readreg(state, 0x58) << 24;
218 tmp *= STV0297_CLOCK_KHZ;
224 static void stv0297_set_symbolrate(struct stv0297_state *state, u32 srate)
228 tmp = 131072L * srate; /* 131072 = 2^17 */
229 tmp = tmp / (STV0297_CLOCK_KHZ / 4); /* 1/4 = 2^-2 */
230 tmp = tmp * 8192L; /* 8192 = 2^13 */
232 stv0297_writereg(state, 0x55, (unsigned char) (tmp & 0xFF));
233 stv0297_writereg(state, 0x56, (unsigned char) (tmp >> 8));
234 stv0297_writereg(state, 0x57, (unsigned char) (tmp >> 16));
235 stv0297_writereg(state, 0x58, (unsigned char) (tmp >> 24));
238 static void stv0297_set_sweeprate(struct stv0297_state *state, short fshift, long symrate)
242 tmp = (long) fshift *262144L; /* 262144 = 2*18 */
244 tmp *= 1024; /* 1024 = 2*10 */
254 stv0297_writereg(state, 0x60, tmp & 0xFF);
255 stv0297_writereg_mask(state, 0x69, 0xF0, (tmp >> 4) & 0xf0);
258 static void stv0297_set_carrieroffset(struct stv0297_state *state, long offset)
262 /* symrate is hardcoded to 10000 */
263 tmp = offset * 26844L; /* (2**28)/10000 */
268 stv0297_writereg(state, 0x66, (unsigned char) (tmp & 0xFF));
269 stv0297_writereg(state, 0x67, (unsigned char) (tmp >> 8));
270 stv0297_writereg(state, 0x68, (unsigned char) (tmp >> 16));
271 stv0297_writereg_mask(state, 0x69, 0x0F, (tmp >> 24) & 0x0f);
275 static long stv0297_get_carrieroffset(struct stv0297_state *state)
279 stv0297_writereg(state, 0x6B, 0x00);
281 tmp = stv0297_readreg(state, 0x66);
282 tmp |= (stv0297_readreg(state, 0x67) << 8);
283 tmp |= (stv0297_readreg(state, 0x68) << 16);
284 tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
286 tmp *= stv0297_get_symbolrate(state);
293 static void stv0297_set_initialdemodfreq(struct stv0297_state *state, long freq)
298 freq -= STV0297_CLOCK_KHZ;
300 tmp = (STV0297_CLOCK_KHZ * 1000) / (1 << 16);
301 tmp = (freq * 1000) / tmp;
305 stv0297_writereg_mask(state, 0x25, 0x80, 0x80);
306 stv0297_writereg(state, 0x21, tmp >> 8);
307 stv0297_writereg(state, 0x20, tmp);
310 static int stv0297_set_qam(struct stv0297_state *state, fe_modulation_t modulation)
314 switch (modulation) {
339 stv0297_writereg_mask(state, 0x00, 0x70, val << 4);
344 static int stv0297_set_inversion(struct stv0297_state *state, fe_spectral_inversion_t inversion)
361 stv0297_writereg_mask(state, 0x83, 0x08, val << 3);
366 int stv0297_enable_plli2c(struct dvb_frontend *fe)
368 struct stv0297_state *state = fe->demodulator_priv;
370 stv0297_writereg(state, 0x87, 0x78);
371 stv0297_writereg(state, 0x86, 0xc8);
376 static int stv0297_init(struct dvb_frontend *fe)
378 struct stv0297_state *state = fe->demodulator_priv;
382 stv0297_writereg_mask(state, 0x80, 1, 1);
383 stv0297_writereg_mask(state, 0x80, 1, 0);
385 /* reset deinterleaver */
386 stv0297_writereg_mask(state, 0x81, 1, 1);
387 stv0297_writereg_mask(state, 0x81, 1, 0);
389 /* load init table */
390 for (i = 0; i < sizeof(init_tab); i += 2) {
391 stv0297_writereg(state, init_tab[i], init_tab[i + 1]);
394 /* set a dummy symbol rate */
395 stv0297_set_symbolrate(state, 6900);
397 /* invert AGC1 polarity */
398 stv0297_writereg_mask(state, 0x88, 0x10, 0x10);
400 /* setup bit error counting */
401 stv0297_writereg_mask(state, 0xA0, 0x80, 0x00);
402 stv0297_writereg_mask(state, 0xA0, 0x10, 0x00);
403 stv0297_writereg_mask(state, 0xA0, 0x08, 0x00);
404 stv0297_writereg_mask(state, 0xA0, 0x07, 0x04);
407 stv0297_writereg(state, 0x4a, 0x00);
408 stv0297_writereg(state, 0x4b, state->pwm);
411 if (state->config->pll_init)
412 state->config->pll_init(fe);
417 static int stv0297_sleep(struct dvb_frontend *fe)
419 struct stv0297_state *state = fe->demodulator_priv;
421 stv0297_writereg_mask(state, 0x80, 1, 1);
426 static int stv0297_read_status(struct dvb_frontend *fe, fe_status_t * status)
428 struct stv0297_state *state = fe->demodulator_priv;
430 u8 sync = stv0297_readreg(state, 0xDF);
435 FE_HAS_SYNC | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK;
439 static int stv0297_read_ber(struct dvb_frontend *fe, u32 * ber)
441 struct stv0297_state *state = fe->demodulator_priv;
444 stv0297_writereg(state, 0xA0, 0x80); // Start Counting bit errors for 4096 Bytes
445 mdelay(25); // Hopefully got 4096 Bytes
446 stv0297_readregs(state, 0xA0, BER, 3);
448 *ber = (BER[2] << 8 | BER[1]) / (8 * 4096);
454 static int stv0297_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
456 struct stv0297_state *state = fe->demodulator_priv;
459 stv0297_readregs(state, 0x41, STRENGTH, 2);
460 *strength = (STRENGTH[1] & 0x03) << 8 | STRENGTH[0];
465 static int stv0297_read_snr(struct dvb_frontend *fe, u16 * snr)
467 struct stv0297_state *state = fe->demodulator_priv;
470 stv0297_readregs(state, 0x07, SNR, 2);
471 *snr = SNR[1] << 8 | SNR[0];
476 static int stv0297_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
478 struct stv0297_state *state = fe->demodulator_priv;
480 *ucblocks = (stv0297_readreg(state, 0xD5) << 8)
481 | stv0297_readreg(state, 0xD4);
486 static int stv0297_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
488 struct stv0297_state *state = fe->demodulator_priv;
495 unsigned long starttime;
496 unsigned long timeout;
497 fe_spectral_inversion_t inversion;
499 switch (p->u.qam.modulation) {
521 // determine inversion dependant parameters
522 inversion = p->inversion;
523 if (state->config->invert)
524 inversion = (inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
525 carrieroffset = -330;
531 sweeprate = -sweeprate;
532 carrieroffset = -carrieroffset;
540 state->config->pll_set(fe, p);
542 /* clear software interrupts */
543 stv0297_writereg(state, 0x82, 0x0);
545 /* set initial demodulation frequency */
546 stv0297_set_initialdemodfreq(state, 7250);
549 stv0297_writereg_mask(state, 0x43, 0x10, 0x00);
550 stv0297_writereg(state, 0x41, 0x00);
551 stv0297_writereg_mask(state, 0x42, 0x03, 0x01);
552 stv0297_writereg_mask(state, 0x36, 0x60, 0x00);
553 stv0297_writereg_mask(state, 0x36, 0x18, 0x00);
554 stv0297_writereg_mask(state, 0x71, 0x80, 0x80);
555 stv0297_writereg(state, 0x72, 0x00);
556 stv0297_writereg(state, 0x73, 0x00);
557 stv0297_writereg_mask(state, 0x74, 0x0F, 0x00);
558 stv0297_writereg_mask(state, 0x43, 0x08, 0x00);
559 stv0297_writereg_mask(state, 0x71, 0x80, 0x00);
562 stv0297_writereg_mask(state, 0x5a, 0x20, 0x20);
563 stv0297_writereg_mask(state, 0x5b, 0x02, 0x02);
564 stv0297_writereg_mask(state, 0x5b, 0x02, 0x00);
565 stv0297_writereg_mask(state, 0x5b, 0x01, 0x00);
566 stv0297_writereg_mask(state, 0x5a, 0x40, 0x40);
568 /* disable frequency sweep */
569 stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
571 /* reset deinterleaver */
572 stv0297_writereg_mask(state, 0x81, 0x01, 0x01);
573 stv0297_writereg_mask(state, 0x81, 0x01, 0x00);
576 stv0297_writereg_mask(state, 0x83, 0x20, 0x20);
577 stv0297_writereg_mask(state, 0x83, 0x20, 0x00);
579 /* reset equaliser */
580 u_threshold = stv0297_readreg(state, 0x00) & 0xf;
581 initial_u = stv0297_readreg(state, 0x01) >> 4;
582 blind_u = stv0297_readreg(state, 0x01) & 0xf;
583 stv0297_writereg_mask(state, 0x84, 0x01, 0x01);
584 stv0297_writereg_mask(state, 0x84, 0x01, 0x00);
585 stv0297_writereg_mask(state, 0x00, 0x0f, u_threshold);
586 stv0297_writereg_mask(state, 0x01, 0xf0, initial_u << 4);
587 stv0297_writereg_mask(state, 0x01, 0x0f, blind_u);
589 /* data comes from internal A/D */
590 stv0297_writereg_mask(state, 0x87, 0x80, 0x00);
592 /* clear phase registers */
593 stv0297_writereg(state, 0x63, 0x00);
594 stv0297_writereg(state, 0x64, 0x00);
595 stv0297_writereg(state, 0x65, 0x00);
596 stv0297_writereg(state, 0x66, 0x00);
597 stv0297_writereg(state, 0x67, 0x00);
598 stv0297_writereg(state, 0x68, 0x00);
599 stv0297_writereg_mask(state, 0x69, 0x0f, 0x00);
602 stv0297_set_qam(state, p->u.qam.modulation);
603 stv0297_set_symbolrate(state, p->u.qam.symbol_rate / 1000);
604 stv0297_set_sweeprate(state, sweeprate, p->u.qam.symbol_rate / 1000);
605 stv0297_set_carrieroffset(state, carrieroffset);
606 stv0297_set_inversion(state, inversion);
609 stv0297_writereg_mask(state, 0x88, 0x08, 0x08);
610 stv0297_writereg_mask(state, 0x5a, 0x20, 0x00);
611 stv0297_writereg_mask(state, 0x6a, 0x01, 0x01);
612 stv0297_writereg_mask(state, 0x43, 0x40, 0x40);
613 stv0297_writereg_mask(state, 0x5b, 0x30, 0x00);
614 stv0297_writereg_mask(state, 0x03, 0x0c, 0x0c);
615 stv0297_writereg_mask(state, 0x03, 0x03, 0x03);
616 stv0297_writereg_mask(state, 0x43, 0x10, 0x10);
618 /* wait for WGAGC lock */
620 timeout = jiffies + (200 * HZ) / 1000;
621 while (time_before(jiffies, timeout)) {
623 if (stv0297_readreg(state, 0x43) & 0x08)
626 if (time_after(jiffies, timeout)) {
631 /* wait for equaliser partial convergence */
632 timeout = jiffies + (50 * HZ) / 1000;
633 while (time_before(jiffies, timeout)) {
636 if (stv0297_readreg(state, 0x82) & 0x04) {
640 if (time_after(jiffies, timeout)) {
644 /* wait for equaliser full convergence */
645 timeout = jiffies + (delay * HZ) / 1000;
646 while (time_before(jiffies, timeout)) {
649 if (stv0297_readreg(state, 0x82) & 0x08) {
653 if (time_after(jiffies, timeout)) {
658 stv0297_writereg_mask(state, 0x6a, 1, 0);
659 stv0297_writereg_mask(state, 0x88, 8, 0);
661 /* wait for main lock */
662 timeout = jiffies + (20 * HZ) / 1000;
663 while (time_before(jiffies, timeout)) {
666 if (stv0297_readreg(state, 0xDF) & 0x80) {
670 if (time_after(jiffies, timeout)) {
675 /* is it still locked after that delay? */
676 if (!(stv0297_readreg(state, 0xDF) & 0x80)) {
681 stv0297_writereg_mask(state, 0x5a, 0x40, 0x00);
682 state->base_freq = p->frequency;
686 stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
690 static int stv0297_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
692 struct stv0297_state *state = fe->demodulator_priv;
695 reg_00 = stv0297_readreg(state, 0x00);
696 reg_83 = stv0297_readreg(state, 0x83);
698 p->frequency = state->base_freq;
699 p->inversion = (reg_83 & 0x08) ? INVERSION_ON : INVERSION_OFF;
700 if (state->config->invert)
701 p->inversion = (p->inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
702 p->u.qam.symbol_rate = stv0297_get_symbolrate(state) * 1000;
703 p->u.qam.fec_inner = FEC_NONE;
705 switch ((reg_00 >> 4) & 0x7) {
707 p->u.qam.modulation = QAM_16;
710 p->u.qam.modulation = QAM_32;
713 p->u.qam.modulation = QAM_128;
716 p->u.qam.modulation = QAM_256;
719 p->u.qam.modulation = QAM_64;
726 static void stv0297_release(struct dvb_frontend *fe)
728 struct stv0297_state *state = fe->demodulator_priv;
732 static struct dvb_frontend_ops stv0297_ops;
734 struct dvb_frontend *stv0297_attach(const struct stv0297_config *config,
735 struct i2c_adapter *i2c, int pwm)
737 struct stv0297_state *state = NULL;
739 /* allocate memory for the internal state */
740 state = kmalloc(sizeof(struct stv0297_state), GFP_KERNEL);
744 /* setup the state */
745 state->config = config;
747 memcpy(&state->ops, &stv0297_ops, sizeof(struct dvb_frontend_ops));
748 state->base_freq = 0;
751 /* check if the demod is there */
752 if ((stv0297_readreg(state, 0x80) & 0x70) != 0x20)
755 /* create dvb_frontend */
756 state->frontend.ops = &state->ops;
757 state->frontend.demodulator_priv = state;
758 return &state->frontend;
765 static struct dvb_frontend_ops stv0297_ops = {
768 .name = "ST STV0297 DVB-C",
770 .frequency_min = 64000000,
771 .frequency_max = 1300000000,
772 .frequency_stepsize = 62500,
773 .symbol_rate_min = 870000,
774 .symbol_rate_max = 11700000,
775 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
776 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
778 .release = stv0297_release,
780 .init = stv0297_init,
781 .sleep = stv0297_sleep,
783 .set_frontend = stv0297_set_frontend,
784 .get_frontend = stv0297_get_frontend,
786 .read_status = stv0297_read_status,
787 .read_ber = stv0297_read_ber,
788 .read_signal_strength = stv0297_read_signal_strength,
789 .read_snr = stv0297_read_snr,
790 .read_ucblocks = stv0297_read_ucblocks,
793 MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
794 MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
795 MODULE_LICENSE("GPL");
797 EXPORT_SYMBOL(stv0297_attach);
798 EXPORT_SYMBOL(stv0297_enable_plli2c);