2 * Driver for Zarlink DVB-T MT352 demodulator
7 * AVerMedia AVerTV DVB-T 771 support by
10 * Support for Samsung TDTC9251DH01C(M) tuner
14 * DVICO FusionHDTV DVB-T1 and DVICO FusionHDTV DVB-T Lite support by
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.
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/string.h>
34 #include <linux/slab.h>
36 #include "dvb_frontend.h"
37 #include "mt352_priv.h"
41 struct i2c_adapter* i2c;
42 struct dvb_frontend frontend;
44 /* configuration settings */
45 struct mt352_config config;
49 #define dprintk(args...) \
51 if (debug) printk(KERN_DEBUG "mt352: " args); \
54 static int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
56 struct mt352_state* state = fe->demodulator_priv;
57 u8 buf[2] = { reg, val };
58 struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
59 .buf = buf, .len = 2 };
60 int err = i2c_transfer(state->i2c, &msg, 1);
62 printk("mt352_write() to reg %x failed (err = %d)!\n", reg, err);
68 static int _mt352_write(struct dvb_frontend* fe, const u8 ibuf[], int ilen)
71 for (i=0; i < ilen-1; i++)
72 if ((err = mt352_single_write(fe,ibuf[0]+i,ibuf[i+1])))
78 static int mt352_read_register(struct mt352_state* state, u8 reg)
83 struct i2c_msg msg [] = { { .addr = state->config.demod_address,
85 .buf = b0, .len = 1 },
86 { .addr = state->config.demod_address,
88 .buf = b1, .len = 1 } };
90 ret = i2c_transfer(state->i2c, msg, 2);
93 printk("%s: readreg error (reg=%d, ret==%i)\n",
101 static int mt352_sleep(struct dvb_frontend* fe)
103 static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
105 _mt352_write(fe, mt352_softdown, sizeof(mt352_softdown));
109 static void mt352_calc_nominal_rate(struct mt352_state* state,
113 u32 adc_clock = 20480; /* 20.340 MHz */
128 if (state->config.adc_clock)
129 adc_clock = state->config.adc_clock;
131 value = 64 * bw * (1<<16) / (7 * 8);
132 value = value * 1000 / adc_clock;
133 dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
134 __func__, bw, adc_clock, value);
139 static void mt352_calc_input_freq(struct mt352_state* state,
142 int adc_clock = 20480; /* 20.480000 MHz */
143 int if2 = 36167; /* 36.166667 MHz */
146 if (state->config.adc_clock)
147 adc_clock = state->config.adc_clock;
148 if (state->config.if2)
149 if2 = state->config.if2;
151 if (adc_clock >= if2 * 2)
154 ife = adc_clock - (if2 % adc_clock);
155 if (ife > adc_clock / 2)
156 ife = adc_clock - ife;
158 value = -16374 * ife / adc_clock;
159 dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
160 __func__, if2, ife, adc_clock, value, value & 0x3fff);
165 static int mt352_set_parameters(struct dvb_frontend *fe)
167 struct dtv_frontend_properties *op = &fe->dtv_property_cache;
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;
174 switch (op->code_rate_HP) {
194 switch (op->code_rate_LP) {
211 if (op->hierarchy == HIERARCHY_AUTO ||
212 op->hierarchy == HIERARCHY_NONE)
219 switch (op->modulation) {
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) {
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_hz, buf+4);
288 mt352_calc_input_freq(state, buf+6);
290 if (state->config.no_tuner) {
291 if (fe->ops.tuner_ops.set_params) {
292 fe->ops.tuner_ops.set_params(fe);
293 if (fe->ops.i2c_gate_ctrl)
294 fe->ops.i2c_gate_ctrl(fe, 0);
297 _mt352_write(fe, buf, 8);
298 _mt352_write(fe, fsm_go, 2);
300 if (fe->ops.tuner_ops.calc_regs) {
301 fe->ops.tuner_ops.calc_regs(fe, buf+8, 5);
303 _mt352_write(fe, buf, sizeof(buf));
304 _mt352_write(fe, tuner_go, 2);
311 static int mt352_get_parameters(struct dvb_frontend* fe,
312 struct dtv_frontend_properties *op)
314 struct mt352_state* state = fe->demodulator_priv;
318 static const u8 tps_fec_to_api[8] =
330 if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
333 /* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
334 * the mt352 sometimes works with the wrong parameters
336 tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
337 div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
338 trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
340 op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
341 op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
343 switch ( (tps >> 13) & 3)
346 op->modulation = QPSK;
349 op->modulation = QAM_16;
352 op->modulation = QAM_64;
355 op->modulation = QAM_AUTO;
359 op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
361 switch ( (tps >> 2) & 3)
364 op->guard_interval = GUARD_INTERVAL_1_32;
367 op->guard_interval = GUARD_INTERVAL_1_16;
370 op->guard_interval = GUARD_INTERVAL_1_8;
373 op->guard_interval = GUARD_INTERVAL_1_4;
376 op->guard_interval = GUARD_INTERVAL_AUTO;
380 switch ( (tps >> 10) & 7)
383 op->hierarchy = HIERARCHY_NONE;
386 op->hierarchy = HIERARCHY_1;
389 op->hierarchy = HIERARCHY_2;
392 op->hierarchy = HIERARCHY_4;
395 op->hierarchy = HIERARCHY_AUTO;
399 op->frequency = (500 * (div - IF_FREQUENCYx6)) / 3 * 1000;
402 op->bandwidth_hz = 8000000;
403 else if (trl == 0x64)
404 op->bandwidth_hz = 7000000;
406 op->bandwidth_hz = 6000000;
409 if (mt352_read_register(state, STATUS_2) & 0x02)
410 op->inversion = INVERSION_OFF;
412 op->inversion = INVERSION_ON;
417 static int mt352_read_status(struct dvb_frontend *fe, enum fe_status *status)
419 struct mt352_state* state = fe->demodulator_priv;
424 * The MT352 design manual from Zarlink states (page 46-47):
426 * Notes about the TUNER_GO register:
428 * If the Read_Tuner_Byte (bit-1) is activated, then the tuner status
429 * byte is copied from the tuner to the STATUS_3 register and
430 * completion of the read operation is indicated by bit-5 of the
431 * INTERRUPT_3 register.
434 if ((s0 = mt352_read_register(state, STATUS_0)) < 0)
436 if ((s1 = mt352_read_register(state, STATUS_1)) < 0)
438 if ((s3 = mt352_read_register(state, STATUS_3)) < 0)
443 *status |= FE_HAS_CARRIER;
445 *status |= FE_HAS_VITERBI;
447 *status |= FE_HAS_LOCK;
449 *status |= FE_HAS_SYNC;
451 *status |= FE_HAS_SIGNAL;
453 if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
454 (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
455 *status &= ~FE_HAS_LOCK;
460 static int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
462 struct mt352_state* state = fe->demodulator_priv;
464 *ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
465 (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
466 (mt352_read_register (state, RS_ERR_CNT_0));
471 static int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
473 struct mt352_state* state = fe->demodulator_priv;
475 /* align the 12 bit AGC gain with the most significant bits */
476 u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
477 (mt352_read_register(state, AGC_GAIN_0) << 4);
479 /* inverse of gain is signal strength */
484 static int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
486 struct mt352_state* state = fe->demodulator_priv;
488 u8 _snr = mt352_read_register (state, SNR);
489 *snr = (_snr << 8) | _snr;
494 static int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
496 struct mt352_state* state = fe->demodulator_priv;
498 *ucblocks = (mt352_read_register (state, RS_UBC_1) << 8) |
499 (mt352_read_register (state, RS_UBC_0));
504 static int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
506 fe_tune_settings->min_delay_ms = 800;
507 fe_tune_settings->step_size = 0;
508 fe_tune_settings->max_drift = 0;
513 static int mt352_init(struct dvb_frontend* fe)
515 struct mt352_state* state = fe->demodulator_priv;
517 static u8 mt352_reset_attach [] = { RESET, 0xC0 };
519 dprintk("%s: hello\n",__func__);
521 if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
522 (mt352_read_register(state, CONFIG) & 0x20) == 0) {
524 /* Do a "hard" reset */
525 _mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
526 return state->config.demod_init(fe);
532 static void mt352_release(struct dvb_frontend* fe)
534 struct mt352_state* state = fe->demodulator_priv;
538 static const struct dvb_frontend_ops mt352_ops;
540 struct dvb_frontend* mt352_attach(const struct mt352_config* config,
541 struct i2c_adapter* i2c)
543 struct mt352_state* state = NULL;
545 /* allocate memory for the internal state */
546 state = kzalloc(sizeof(struct mt352_state), GFP_KERNEL);
547 if (state == NULL) goto error;
549 /* setup the state */
551 memcpy(&state->config,config,sizeof(struct mt352_config));
553 /* check if the demod is there */
554 if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
556 /* create dvb_frontend */
557 memcpy(&state->frontend.ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
558 state->frontend.demodulator_priv = state;
559 return &state->frontend;
566 static const struct dvb_frontend_ops mt352_ops = {
567 .delsys = { SYS_DVBT },
569 .name = "Zarlink MT352 DVB-T",
570 .frequency_min = 174000000,
571 .frequency_max = 862000000,
572 .frequency_stepsize = 166667,
573 .frequency_tolerance = 0,
574 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
575 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
577 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
578 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
579 FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
583 .release = mt352_release,
586 .sleep = mt352_sleep,
587 .write = _mt352_write,
589 .set_frontend = mt352_set_parameters,
590 .get_frontend = mt352_get_parameters,
591 .get_tune_settings = mt352_get_tune_settings,
593 .read_status = mt352_read_status,
594 .read_ber = mt352_read_ber,
595 .read_signal_strength = mt352_read_signal_strength,
596 .read_snr = mt352_read_snr,
597 .read_ucblocks = mt352_read_ucblocks,
600 module_param(debug, int, 0644);
601 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
603 MODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
604 MODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
605 MODULE_LICENSE("GPL");
607 EXPORT_SYMBOL(mt352_attach);