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 struct dvb_frontend_ops ops;
36 const struct stv0297_config *config;
37 struct dvb_frontend frontend;
39 unsigned long base_freq;
43 #define dprintk(x...) printk(x)
48 #define STV0297_CLOCK_KHZ 28900
51 static int stv0297_writereg(struct stv0297_state *state, u8 reg, u8 data)
54 u8 buf[] = { reg, data };
55 struct i2c_msg msg = {.addr = state->config->demod_address,.flags = 0,.buf = buf,.len = 2 };
57 ret = i2c_transfer(state->i2c, &msg, 1);
60 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
61 "ret == %i)\n", __FUNCTION__, reg, data, ret);
63 return (ret != 1) ? -1 : 0;
66 static int stv0297_readreg(struct stv0297_state *state, u8 reg)
71 struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf = b0,.len = 1},
72 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b1,.len = 1}
75 // this device needs a STOP between the register and data
76 if (state->config->stop_during_read) {
77 if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
78 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret);
81 if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
82 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret);
86 if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
87 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg, ret);
95 static int stv0297_writereg_mask(struct stv0297_state *state, u8 reg, u8 mask, u8 data)
99 val = stv0297_readreg(state, reg);
101 val |= (data & mask);
102 stv0297_writereg(state, reg, val);
107 static int stv0297_readregs(struct stv0297_state *state, u8 reg1, u8 * b, u8 len)
110 struct i2c_msg msg[] = { {.addr = state->config->demod_address,.flags = 0,.buf =
112 {.addr = state->config->demod_address,.flags = I2C_M_RD,.buf = b,.len = len}
115 // this device needs a STOP between the register and data
116 if (state->config->stop_during_read) {
117 if ((ret = i2c_transfer(state->i2c, &msg[0], 1)) != 1) {
118 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);
121 if ((ret = i2c_transfer(state->i2c, &msg[1], 1)) != 1) {
122 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);
126 if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
127 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n", __FUNCTION__, reg1, ret);
135 static u32 stv0297_get_symbolrate(struct stv0297_state *state)
139 tmp = stv0297_readreg(state, 0x55);
140 tmp |= stv0297_readreg(state, 0x56) << 8;
141 tmp |= stv0297_readreg(state, 0x57) << 16;
142 tmp |= stv0297_readreg(state, 0x58) << 24;
144 tmp *= STV0297_CLOCK_KHZ;
150 static void stv0297_set_symbolrate(struct stv0297_state *state, u32 srate)
154 tmp = 131072L * srate; /* 131072 = 2^17 */
155 tmp = tmp / (STV0297_CLOCK_KHZ / 4); /* 1/4 = 2^-2 */
156 tmp = tmp * 8192L; /* 8192 = 2^13 */
158 stv0297_writereg(state, 0x55, (unsigned char) (tmp & 0xFF));
159 stv0297_writereg(state, 0x56, (unsigned char) (tmp >> 8));
160 stv0297_writereg(state, 0x57, (unsigned char) (tmp >> 16));
161 stv0297_writereg(state, 0x58, (unsigned char) (tmp >> 24));
164 static void stv0297_set_sweeprate(struct stv0297_state *state, short fshift, long symrate)
168 tmp = (long) fshift *262144L; /* 262144 = 2*18 */
170 tmp *= 1024; /* 1024 = 2*10 */
180 stv0297_writereg(state, 0x60, tmp & 0xFF);
181 stv0297_writereg_mask(state, 0x69, 0xF0, (tmp >> 4) & 0xf0);
184 static void stv0297_set_carrieroffset(struct stv0297_state *state, long offset)
188 /* symrate is hardcoded to 10000 */
189 tmp = offset * 26844L; /* (2**28)/10000 */
194 stv0297_writereg(state, 0x66, (unsigned char) (tmp & 0xFF));
195 stv0297_writereg(state, 0x67, (unsigned char) (tmp >> 8));
196 stv0297_writereg(state, 0x68, (unsigned char) (tmp >> 16));
197 stv0297_writereg_mask(state, 0x69, 0x0F, (tmp >> 24) & 0x0f);
201 static long stv0297_get_carrieroffset(struct stv0297_state *state)
205 stv0297_writereg(state, 0x6B, 0x00);
207 tmp = stv0297_readreg(state, 0x66);
208 tmp |= (stv0297_readreg(state, 0x67) << 8);
209 tmp |= (stv0297_readreg(state, 0x68) << 16);
210 tmp |= (stv0297_readreg(state, 0x69) & 0x0F) << 24;
212 tmp *= stv0297_get_symbolrate(state);
219 static void stv0297_set_initialdemodfreq(struct stv0297_state *state, long freq)
224 freq -= STV0297_CLOCK_KHZ;
226 tmp = (STV0297_CLOCK_KHZ * 1000) / (1 << 16);
227 tmp = (freq * 1000) / tmp;
231 stv0297_writereg_mask(state, 0x25, 0x80, 0x80);
232 stv0297_writereg(state, 0x21, tmp >> 8);
233 stv0297_writereg(state, 0x20, tmp);
236 static int stv0297_set_qam(struct stv0297_state *state, fe_modulation_t modulation)
240 switch (modulation) {
265 stv0297_writereg_mask(state, 0x00, 0x70, val << 4);
270 static int stv0297_set_inversion(struct stv0297_state *state, fe_spectral_inversion_t inversion)
287 stv0297_writereg_mask(state, 0x83, 0x08, val << 3);
292 static int stv0297_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
294 struct stv0297_state *state = fe->demodulator_priv;
297 stv0297_writereg(state, 0x87, 0x78);
298 stv0297_writereg(state, 0x86, 0xc8);
304 static int stv0297_init(struct dvb_frontend *fe)
306 struct stv0297_state *state = fe->demodulator_priv;
309 /* load init table */
310 for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
311 stv0297_writereg(state, state->config->inittab[i], state->config->inittab[i+1]);
317 static int stv0297_sleep(struct dvb_frontend *fe)
319 struct stv0297_state *state = fe->demodulator_priv;
321 stv0297_writereg_mask(state, 0x80, 1, 1);
326 static int stv0297_read_status(struct dvb_frontend *fe, fe_status_t * status)
328 struct stv0297_state *state = fe->demodulator_priv;
330 u8 sync = stv0297_readreg(state, 0xDF);
335 FE_HAS_SYNC | FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_LOCK;
339 static int stv0297_read_ber(struct dvb_frontend *fe, u32 * ber)
341 struct stv0297_state *state = fe->demodulator_priv;
344 stv0297_writereg(state, 0xA0, 0x80); // Start Counting bit errors for 4096 Bytes
345 mdelay(25); // Hopefully got 4096 Bytes
346 stv0297_readregs(state, 0xA0, BER, 3);
348 *ber = (BER[2] << 8 | BER[1]) / (8 * 4096);
354 static int stv0297_read_signal_strength(struct dvb_frontend *fe, u16 * strength)
356 struct stv0297_state *state = fe->demodulator_priv;
359 stv0297_readregs(state, 0x41, STRENGTH, 2);
360 *strength = (STRENGTH[1] & 0x03) << 8 | STRENGTH[0];
365 static int stv0297_read_snr(struct dvb_frontend *fe, u16 * snr)
367 struct stv0297_state *state = fe->demodulator_priv;
370 stv0297_readregs(state, 0x07, SNR, 2);
371 *snr = SNR[1] << 8 | SNR[0];
376 static int stv0297_read_ucblocks(struct dvb_frontend *fe, u32 * ucblocks)
378 struct stv0297_state *state = fe->demodulator_priv;
380 *ucblocks = (stv0297_readreg(state, 0xD5) << 8)
381 | stv0297_readreg(state, 0xD4);
386 static int stv0297_set_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
388 struct stv0297_state *state = fe->demodulator_priv;
395 unsigned long starttime;
396 unsigned long timeout;
397 fe_spectral_inversion_t inversion;
399 switch (p->u.qam.modulation) {
417 // determine inversion dependant parameters
418 inversion = p->inversion;
419 if (state->config->invert)
420 inversion = (inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
421 carrieroffset = -330;
427 sweeprate = -sweeprate;
428 carrieroffset = -carrieroffset;
436 if (fe->ops->tuner_ops.set_params) {
437 fe->ops->tuner_ops.set_params(fe, p);
438 if (fe->ops->i2c_gate_ctrl) fe->ops->i2c_gate_ctrl(fe, 0);
441 /* clear software interrupts */
442 stv0297_writereg(state, 0x82, 0x0);
444 /* set initial demodulation frequency */
445 stv0297_set_initialdemodfreq(state, 7250);
448 stv0297_writereg_mask(state, 0x43, 0x10, 0x00);
449 stv0297_writereg(state, 0x41, 0x00);
450 stv0297_writereg_mask(state, 0x42, 0x03, 0x01);
451 stv0297_writereg_mask(state, 0x36, 0x60, 0x00);
452 stv0297_writereg_mask(state, 0x36, 0x18, 0x00);
453 stv0297_writereg_mask(state, 0x71, 0x80, 0x80);
454 stv0297_writereg(state, 0x72, 0x00);
455 stv0297_writereg(state, 0x73, 0x00);
456 stv0297_writereg_mask(state, 0x74, 0x0F, 0x00);
457 stv0297_writereg_mask(state, 0x43, 0x08, 0x00);
458 stv0297_writereg_mask(state, 0x71, 0x80, 0x00);
461 stv0297_writereg_mask(state, 0x5a, 0x20, 0x20);
462 stv0297_writereg_mask(state, 0x5b, 0x02, 0x02);
463 stv0297_writereg_mask(state, 0x5b, 0x02, 0x00);
464 stv0297_writereg_mask(state, 0x5b, 0x01, 0x00);
465 stv0297_writereg_mask(state, 0x5a, 0x40, 0x40);
467 /* disable frequency sweep */
468 stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
470 /* reset deinterleaver */
471 stv0297_writereg_mask(state, 0x81, 0x01, 0x01);
472 stv0297_writereg_mask(state, 0x81, 0x01, 0x00);
475 stv0297_writereg_mask(state, 0x83, 0x20, 0x20);
476 stv0297_writereg_mask(state, 0x83, 0x20, 0x00);
478 /* reset equaliser */
479 u_threshold = stv0297_readreg(state, 0x00) & 0xf;
480 initial_u = stv0297_readreg(state, 0x01) >> 4;
481 blind_u = stv0297_readreg(state, 0x01) & 0xf;
482 stv0297_writereg_mask(state, 0x84, 0x01, 0x01);
483 stv0297_writereg_mask(state, 0x84, 0x01, 0x00);
484 stv0297_writereg_mask(state, 0x00, 0x0f, u_threshold);
485 stv0297_writereg_mask(state, 0x01, 0xf0, initial_u << 4);
486 stv0297_writereg_mask(state, 0x01, 0x0f, blind_u);
488 /* data comes from internal A/D */
489 stv0297_writereg_mask(state, 0x87, 0x80, 0x00);
491 /* clear phase registers */
492 stv0297_writereg(state, 0x63, 0x00);
493 stv0297_writereg(state, 0x64, 0x00);
494 stv0297_writereg(state, 0x65, 0x00);
495 stv0297_writereg(state, 0x66, 0x00);
496 stv0297_writereg(state, 0x67, 0x00);
497 stv0297_writereg(state, 0x68, 0x00);
498 stv0297_writereg_mask(state, 0x69, 0x0f, 0x00);
501 stv0297_set_qam(state, p->u.qam.modulation);
502 stv0297_set_symbolrate(state, p->u.qam.symbol_rate / 1000);
503 stv0297_set_sweeprate(state, sweeprate, p->u.qam.symbol_rate / 1000);
504 stv0297_set_carrieroffset(state, carrieroffset);
505 stv0297_set_inversion(state, inversion);
508 /* Disable corner detection for higher QAMs */
509 if (p->u.qam.modulation == QAM_128 ||
510 p->u.qam.modulation == QAM_256)
511 stv0297_writereg_mask(state, 0x88, 0x08, 0x00);
513 stv0297_writereg_mask(state, 0x88, 0x08, 0x08);
515 stv0297_writereg_mask(state, 0x5a, 0x20, 0x00);
516 stv0297_writereg_mask(state, 0x6a, 0x01, 0x01);
517 stv0297_writereg_mask(state, 0x43, 0x40, 0x40);
518 stv0297_writereg_mask(state, 0x5b, 0x30, 0x00);
519 stv0297_writereg_mask(state, 0x03, 0x0c, 0x0c);
520 stv0297_writereg_mask(state, 0x03, 0x03, 0x03);
521 stv0297_writereg_mask(state, 0x43, 0x10, 0x10);
523 /* wait for WGAGC lock */
525 timeout = jiffies + msecs_to_jiffies(2000);
526 while (time_before(jiffies, timeout)) {
528 if (stv0297_readreg(state, 0x43) & 0x08)
531 if (time_after(jiffies, timeout)) {
536 /* wait for equaliser partial convergence */
537 timeout = jiffies + msecs_to_jiffies(500);
538 while (time_before(jiffies, timeout)) {
541 if (stv0297_readreg(state, 0x82) & 0x04) {
545 if (time_after(jiffies, timeout)) {
549 /* wait for equaliser full convergence */
550 timeout = jiffies + msecs_to_jiffies(delay);
551 while (time_before(jiffies, timeout)) {
554 if (stv0297_readreg(state, 0x82) & 0x08) {
558 if (time_after(jiffies, timeout)) {
563 stv0297_writereg_mask(state, 0x6a, 1, 0);
564 stv0297_writereg_mask(state, 0x88, 8, 0);
566 /* wait for main lock */
567 timeout = jiffies + msecs_to_jiffies(20);
568 while (time_before(jiffies, timeout)) {
571 if (stv0297_readreg(state, 0xDF) & 0x80) {
575 if (time_after(jiffies, timeout)) {
580 /* is it still locked after that delay? */
581 if (!(stv0297_readreg(state, 0xDF) & 0x80)) {
586 stv0297_writereg_mask(state, 0x5a, 0x40, 0x00);
587 state->base_freq = p->frequency;
591 stv0297_writereg_mask(state, 0x6a, 0x01, 0x00);
595 static int stv0297_get_frontend(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
597 struct stv0297_state *state = fe->demodulator_priv;
600 reg_00 = stv0297_readreg(state, 0x00);
601 reg_83 = stv0297_readreg(state, 0x83);
603 p->frequency = state->base_freq;
604 p->inversion = (reg_83 & 0x08) ? INVERSION_ON : INVERSION_OFF;
605 if (state->config->invert)
606 p->inversion = (p->inversion == INVERSION_ON) ? INVERSION_OFF : INVERSION_ON;
607 p->u.qam.symbol_rate = stv0297_get_symbolrate(state) * 1000;
608 p->u.qam.fec_inner = FEC_NONE;
610 switch ((reg_00 >> 4) & 0x7) {
612 p->u.qam.modulation = QAM_16;
615 p->u.qam.modulation = QAM_32;
618 p->u.qam.modulation = QAM_128;
621 p->u.qam.modulation = QAM_256;
624 p->u.qam.modulation = QAM_64;
631 static void stv0297_release(struct dvb_frontend *fe)
633 struct stv0297_state *state = fe->demodulator_priv;
637 static struct dvb_frontend_ops stv0297_ops;
639 struct dvb_frontend *stv0297_attach(const struct stv0297_config *config,
640 struct i2c_adapter *i2c)
642 struct stv0297_state *state = NULL;
644 /* allocate memory for the internal state */
645 state = kmalloc(sizeof(struct stv0297_state), GFP_KERNEL);
649 /* setup the state */
650 state->config = config;
652 memcpy(&state->ops, &stv0297_ops, sizeof(struct dvb_frontend_ops));
653 state->base_freq = 0;
655 /* check if the demod is there */
656 if ((stv0297_readreg(state, 0x80) & 0x70) != 0x20)
659 /* create dvb_frontend */
660 state->frontend.ops = &state->ops;
661 state->frontend.demodulator_priv = state;
662 return &state->frontend;
669 static struct dvb_frontend_ops stv0297_ops = {
672 .name = "ST STV0297 DVB-C",
674 .frequency_min = 64000000,
675 .frequency_max = 1300000000,
676 .frequency_stepsize = 62500,
677 .symbol_rate_min = 870000,
678 .symbol_rate_max = 11700000,
679 .caps = FE_CAN_QAM_16 | FE_CAN_QAM_32 | FE_CAN_QAM_64 |
680 FE_CAN_QAM_128 | FE_CAN_QAM_256 | FE_CAN_FEC_AUTO},
682 .release = stv0297_release,
684 .init = stv0297_init,
685 .sleep = stv0297_sleep,
686 .i2c_gate_ctrl = stv0297_i2c_gate_ctrl,
688 .set_frontend = stv0297_set_frontend,
689 .get_frontend = stv0297_get_frontend,
691 .read_status = stv0297_read_status,
692 .read_ber = stv0297_read_ber,
693 .read_signal_strength = stv0297_read_signal_strength,
694 .read_snr = stv0297_read_snr,
695 .read_ucblocks = stv0297_read_ucblocks,
698 MODULE_DESCRIPTION("ST STV0297 DVB-C Demodulator driver");
699 MODULE_AUTHOR("Dennis Noermann and Andrew de Quincey");
700 MODULE_LICENSE("GPL");
702 EXPORT_SYMBOL(stv0297_attach);