2 * Driver for Zarlink DVB-T MT352 demodulator
4 * Written by Holger Waechtler <holger@qanu.de>
5 * and Daniel Mack <daniel@qanu.de>
7 * AVerMedia AVerTV DVB-T 771 support by
8 * Wolfram Joost <dbox2@frokaschwei.de>
10 * Support for Samsung TDTC9251DH01C(M) tuner
11 * Copyright (C) 2004 Antonio Mancuso <antonio.mancuso@digitaltelevision.it>
12 * Amauri Celani <acelani@essegi.net>
14 * DVICO FusionHDTV DVB-T1 and DVICO FusionHDTV DVB-T Lite support by
15 * Christopher Pascoe <c.pascoe@itee.uq.edu.au>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/init.h>
37 #include <linux/delay.h>
38 #include <linux/string.h>
39 #include <linux/slab.h>
41 #include "dvb_frontend.h"
42 #include "mt352_priv.h"
46 struct i2c_adapter* i2c;
47 struct dvb_frontend frontend;
48 struct dvb_frontend_ops ops;
50 /* configuration settings */
51 struct mt352_config config;
55 #define dprintk(args...) \
57 if (debug) printk(KERN_DEBUG "mt352: " args); \
60 static int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
62 struct mt352_state* state = fe->demodulator_priv;
63 u8 buf[2] = { reg, val };
64 struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
65 .buf = buf, .len = 2 };
66 int err = i2c_transfer(state->i2c, &msg, 1);
68 printk("mt352_write() to reg %x failed (err = %d)!\n", reg, err);
74 int mt352_write(struct dvb_frontend* fe, u8* ibuf, int ilen)
77 for (i=0; i < ilen-1; i++)
78 if ((err = mt352_single_write(fe,ibuf[0]+i,ibuf[i+1])))
84 static int mt352_read_register(struct mt352_state* state, u8 reg)
89 struct i2c_msg msg [] = { { .addr = state->config.demod_address,
91 .buf = b0, .len = 1 },
92 { .addr = state->config.demod_address,
94 .buf = b1, .len = 1 } };
96 ret = i2c_transfer(state->i2c, msg, 2);
99 printk("%s: readreg error (reg=%d, ret==%i)\n",
100 __FUNCTION__, reg, ret);
107 static int mt352_sleep(struct dvb_frontend* fe)
109 static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
111 mt352_write(fe, mt352_softdown, sizeof(mt352_softdown));
115 static void mt352_calc_nominal_rate(struct mt352_state* state,
116 enum fe_bandwidth bandwidth,
119 u32 adc_clock = 20480; /* 20.340 MHz */
123 case BANDWIDTH_6_MHZ:
126 case BANDWIDTH_7_MHZ:
129 case BANDWIDTH_8_MHZ:
134 if (state->config.adc_clock)
135 adc_clock = state->config.adc_clock;
137 value = 64 * bw * (1<<16) / (7 * 8);
138 value = value * 1000 / adc_clock;
139 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
140 __FUNCTION__, bw, adc_clock, value);
145 static void mt352_calc_input_freq(struct mt352_state* state,
148 int adc_clock = 20480; /* 20.480000 MHz */
149 int if2 = 36167; /* 36.166667 MHz */
152 if (state->config.adc_clock)
153 adc_clock = state->config.adc_clock;
154 if (state->config.if2)
155 if2 = state->config.if2;
157 ife = (2*adc_clock - if2);
158 value = -16374 * ife / adc_clock;
159 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
160 __FUNCTION__, if2, ife, adc_clock, value, value & 0x3fff);
165 static int mt352_set_parameters(struct dvb_frontend* fe,
166 struct dvb_frontend_parameters *param)
168 struct mt352_state* state = fe->demodulator_priv;
169 unsigned char buf[13];
170 static unsigned char tuner_go[] = { 0x5d, 0x01 };
171 static unsigned char fsm_go[] = { 0x5e, 0x01 };
172 unsigned int tps = 0;
173 struct dvb_ofdm_parameters *op = ¶m->u.ofdm;
175 switch (op->code_rate_HP) {
195 switch (op->code_rate_LP) {
212 if (op->hierarchy_information == HIERARCHY_AUTO ||
213 op->hierarchy_information == HIERARCHY_NONE)
219 switch (op->constellation) {
233 switch (op->transmission_mode) {
234 case TRANSMISSION_MODE_2K:
235 case TRANSMISSION_MODE_AUTO:
237 case TRANSMISSION_MODE_8K:
244 switch (op->guard_interval) {
245 case GUARD_INTERVAL_1_32:
246 case GUARD_INTERVAL_AUTO:
248 case GUARD_INTERVAL_1_16:
251 case GUARD_INTERVAL_1_8:
254 case GUARD_INTERVAL_1_4:
261 switch (op->hierarchy_information) {
279 buf[0] = TPS_GIVEN_1; /* TPS_GIVEN_1 and following registers */
281 buf[1] = msb(tps); /* TPS_GIVEN_(1|0) */
284 buf[3] = 0x50; // old
285 // buf[3] = 0xf4; // pinnacle
287 mt352_calc_nominal_rate(state, op->bandwidth, buf+4);
288 mt352_calc_input_freq(state, buf+6);
289 state->config.pll_set(fe, param, buf+8);
291 mt352_write(fe, buf, sizeof(buf));
292 if (state->config.no_tuner) {
294 mt352_write(fe, fsm_go, 2);
297 mt352_write(fe, tuner_go, 2);
302 static int mt352_get_parameters(struct dvb_frontend* fe,
303 struct dvb_frontend_parameters *param)
305 struct mt352_state* state = fe->demodulator_priv;
309 struct dvb_ofdm_parameters *op = ¶m->u.ofdm;
310 static const u8 tps_fec_to_api[8] =
322 if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
325 /* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
326 * the mt352 sometimes works with the wrong parameters
328 tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
329 div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
330 trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
332 op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
333 op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
335 switch ( (tps >> 13) & 3)
338 op->constellation = QPSK;
341 op->constellation = QAM_16;
344 op->constellation = QAM_64;
347 op->constellation = QAM_AUTO;
351 op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
353 switch ( (tps >> 2) & 3)
356 op->guard_interval = GUARD_INTERVAL_1_32;
359 op->guard_interval = GUARD_INTERVAL_1_16;
362 op->guard_interval = GUARD_INTERVAL_1_8;
365 op->guard_interval = GUARD_INTERVAL_1_4;
368 op->guard_interval = GUARD_INTERVAL_AUTO;
372 switch ( (tps >> 10) & 7)
375 op->hierarchy_information = HIERARCHY_NONE;
378 op->hierarchy_information = HIERARCHY_1;
381 op->hierarchy_information = HIERARCHY_2;
384 op->hierarchy_information = HIERARCHY_4;
387 op->hierarchy_information = HIERARCHY_AUTO;
391 param->frequency = ( 500 * (div - IF_FREQUENCYx6) ) / 3 * 1000;
394 op->bandwidth = BANDWIDTH_8_MHZ;
395 else if (trl == 0x64)
396 op->bandwidth = BANDWIDTH_7_MHZ;
398 op->bandwidth = BANDWIDTH_6_MHZ;
401 if (mt352_read_register(state, STATUS_2) & 0x02)
402 param->inversion = INVERSION_OFF;
404 param->inversion = INVERSION_ON;
409 static int mt352_read_status(struct dvb_frontend* fe, fe_status_t* status)
411 struct mt352_state* state = fe->demodulator_priv;
416 * The MT352 design manual from Zarlink states (page 46-47):
418 * Notes about the TUNER_GO register:
420 * If the Read_Tuner_Byte (bit-1) is activated, then the tuner status
421 * byte is copied from the tuner to the STATUS_3 register and
422 * completion of the read operation is indicated by bit-5 of the
423 * INTERRUPT_3 register.
426 if ((s0 = mt352_read_register(state, STATUS_0)) < 0)
428 if ((s1 = mt352_read_register(state, STATUS_1)) < 0)
430 if ((s3 = mt352_read_register(state, STATUS_3)) < 0)
435 *status |= FE_HAS_CARRIER;
437 *status |= FE_HAS_VITERBI;
439 *status |= FE_HAS_LOCK;
441 *status |= FE_HAS_SYNC;
443 *status |= FE_HAS_SIGNAL;
445 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
446 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
447 *status &= ~FE_HAS_LOCK;
452 static int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
454 struct mt352_state* state = fe->demodulator_priv;
456 *ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
457 (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
458 (mt352_read_register (state, RS_ERR_CNT_0));
463 static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
465 struct mt352_state* state = fe->demodulator_priv;
467 /* align the 12 bit AGC gain with the most significant bits */
468 u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
469 (mt352_read_register(state, AGC_GAIN_0) << 4);
471 /* inverse of gain is signal strength */
476 static int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
478 struct mt352_state* state = fe->demodulator_priv;
480 u8 _snr = mt352_read_register (state, SNR);
481 *snr = (_snr << 8) | _snr;
486 static int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
488 struct mt352_state* state = fe->demodulator_priv;
490 *ucblocks = (mt352_read_register (state, RS_UBC_1) << 8) |
491 (mt352_read_register (state, RS_UBC_0));
496 static int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
498 fe_tune_settings->min_delay_ms = 800;
499 fe_tune_settings->step_size = 0;
500 fe_tune_settings->max_drift = 0;
505 static int mt352_init(struct dvb_frontend* fe)
507 struct mt352_state* state = fe->demodulator_priv;
509 static u8 mt352_reset_attach [] = { RESET, 0xC0 };
511 dprintk("%s: hello\n",__FUNCTION__);
513 if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
514 (mt352_read_register(state, CONFIG) & 0x20) == 0) {
516 /* Do a "hard" reset */
517 mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
518 return state->config.demod_init(fe);
524 static void mt352_release(struct dvb_frontend* fe)
526 struct mt352_state* state = fe->demodulator_priv;
530 static struct dvb_frontend_ops mt352_ops;
532 struct dvb_frontend* mt352_attach(const struct mt352_config* config,
533 struct i2c_adapter* i2c)
535 struct mt352_state* state = NULL;
537 /* allocate memory for the internal state */
538 state = kmalloc(sizeof(struct mt352_state), GFP_KERNEL);
539 if (state == NULL) goto error;
540 memset(state,0,sizeof(*state));
542 /* setup the state */
544 memcpy(&state->config,config,sizeof(struct mt352_config));
545 memcpy(&state->ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
547 /* check if the demod is there */
548 if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
550 /* create dvb_frontend */
551 state->frontend.ops = &state->ops;
552 state->frontend.demodulator_priv = state;
553 return &state->frontend;
560 static struct dvb_frontend_ops mt352_ops = {
563 .name = "Zarlink MT352 DVB-T",
565 .frequency_min = 174000000,
566 .frequency_max = 862000000,
567 .frequency_stepsize = 166667,
568 .frequency_tolerance = 0,
569 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
570 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
572 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
573 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
574 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
578 .release = mt352_release,
581 .sleep = mt352_sleep,
583 .set_frontend = mt352_set_parameters,
584 .get_frontend = mt352_get_parameters,
585 .get_tune_settings = mt352_get_tune_settings,
587 .read_status = mt352_read_status,
588 .read_ber = mt352_read_ber,
589 .read_signal_strength = mt352_read_signal_strength,
590 .read_snr = mt352_read_snr,
591 .read_ucblocks = mt352_read_ucblocks,
594 module_param(debug, int, 0644);
595 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
597 MODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
598 MODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
599 MODULE_LICENSE("GPL");
601 EXPORT_SYMBOL(mt352_attach);
602 EXPORT_SYMBOL(mt352_write);