[go: nahoru, domu]

11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Driver for Zarlink DVB-T MT352 demodulator
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Written by Holger Waechtler <holger@qanu.de>
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *	 and Daniel Mack <daniel@qanu.de>
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  AVerMedia AVerTV DVB-T 771 support by
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *       Wolfram Joost <dbox2@frokaschwei.de>
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Support for Samsung TDTC9251DH01C(M) tuner
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Copyright (C) 2004 Antonio Mancuso <antonio.mancuso@digitaltelevision.it>
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *                     Amauri  Celani  <acelani@essegi.net>
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  DVICO FusionHDTV DVB-T1 and DVICO FusionHDTV DVB-T Lite support by
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *       Christopher Pascoe <c.pascoe@itee.uq.edu.au>
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is free software; you can redistribute it and/or modify
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it under the terms of the GNU General Public License as published by
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  the Free Software Foundation; either version 2 of the License, or
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  (at your option) any later version.
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is distributed in the hope that it will be useful,
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  but WITHOUT ANY WARRANTY; without even the implied warranty of
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  GNU General Public License for more details.
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  You should have received a copy of the GNU General Public License
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  along with this program; if not, write to the Free Software
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/delay.h>
374e57b6817880946a3a78d5d8cad1ace363f7e449Tim Schmielau#include <linux/string.h>
384e57b6817880946a3a78d5d8cad1ace363f7e449Tim Schmielau#include <linux/slab.h>
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "dvb_frontend.h"
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "mt352_priv.h"
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include "mt352.h"
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct mt352_state {
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_adapter* i2c;
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct dvb_frontend frontend;
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* configuration settings */
4977b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	struct mt352_config config;
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int debug;
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define dprintk(args...) \
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do { \
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (debug) printk(KERN_DEBUG "mt352: " args); \
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} while (0)
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_single_write(struct dvb_frontend *fe, u8 reg, u8 val)
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 buf[2] = { reg, val };
6277b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	struct i2c_msg msg = { .addr = state->config.demod_address, .flags = 0,
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       .buf = buf, .len = 2 };
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err = i2c_transfer(state->i2c, &msg, 1);
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (err != 1) {
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk("mt352_write() to reg %x failed (err = %d)!\n", reg, err);
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return err;
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
722e4e98e788d8fbe30892bee3375067a4937155dalawrence ruststatic int _mt352_write(struct dvb_frontend* fe, const u8 ibuf[], int ilen)
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err,i;
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i=0; i < ilen-1; i++)
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((err = mt352_single_write(fe,ibuf[0]+i,ibuf[i+1])))
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return err;
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_read_register(struct mt352_state* state, u8 reg)
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ret;
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 b0 [] = { reg };
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 b1 [] = { 0 };
8777b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	struct i2c_msg msg [] = { { .addr = state->config.demod_address,
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    .flags = 0,
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    .buf = b0, .len = 1 },
9077b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach				  { .addr = state->config.demod_address,
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    .flags = I2C_M_RD,
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    .buf = b1, .len = 1 } };
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	ret = i2c_transfer(state->i2c, msg, 2);
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (ret != 2) {
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk("%s: readreg error (reg=%d, ret==%i)\n",
98271ddbf702c3a4e6b18f6464180eda0f62efd9a5Harvey Harrison		       __func__, reg, ret);
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return ret;
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return b1[0];
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_sleep(struct dvb_frontend* fe)
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static u8 mt352_softdown[] = { CLOCK_CTL, 0x20, 0x08 };
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
109c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey	_mt352_write(fe, mt352_softdown, sizeof(mt352_softdown));
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void mt352_calc_nominal_rate(struct mt352_state* state,
114cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab				    u32 bandwidth,
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				    unsigned char *buf)
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 adc_clock = 20480; /* 20.340 MHz */
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u32 bw,value;
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (bandwidth) {
121cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	case 6000000:
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bw = 6;
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
124cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	case 7000000:
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bw = 7;
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
127cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	case 8000000:
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		bw = 8;
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
13277b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	if (state->config.adc_clock)
13377b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach		adc_clock = state->config.adc_clock;
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	value = 64 * bw * (1<<16) / (7 * 8);
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	value = value * 1000 / adc_clock;
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dprintk("%s: bw %d, adc_clock %d => 0x%x\n",
138271ddbf702c3a4e6b18f6464180eda0f62efd9a5Harvey Harrison		__func__, bw, adc_clock, value);
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[0] = msb(value);
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[1] = lsb(value);
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void mt352_calc_input_freq(struct mt352_state* state,
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  unsigned char *buf)
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int adc_clock = 20480; /* 20.480000 MHz */
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int if2       = 36167; /* 36.166667 MHz */
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int ife,value;
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
15077b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	if (state->config.adc_clock)
15177b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach		adc_clock = state->config.adc_clock;
15277b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	if (state->config.if2)
15377b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach		if2 = state->config.if2;
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
155c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe	if (adc_clock >= if2 * 2)
156c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe		ife = if2;
157c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe	else {
158c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe		ife = adc_clock - (if2 % adc_clock);
159c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe		if (ife > adc_clock / 2)
160c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe			ife = adc_clock - ife;
161c6e62a3a398d62e8ae366ac1465911db0ac7fc0bChris Pascoe	}
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	value = -16374 * ife / adc_clock;
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	dprintk("%s: if2 %d, ife %d, adc_clock %d => %d / 0x%x\n",
164271ddbf702c3a4e6b18f6464180eda0f62efd9a5Harvey Harrison		__func__, if2, ife, adc_clock, value, value & 0x3fff);
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[0] = msb(value);
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[1] = lsb(value);
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
169cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehabstatic int mt352_set_parameters(struct dvb_frontend *fe)
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
171cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	struct dtv_frontend_properties *op = &fe->dtv_property_cache;
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned char buf[13];
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static unsigned char tuner_go[] = { 0x5d, 0x01 };
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static unsigned char fsm_go[]   = { 0x5e, 0x01 };
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned int tps = 0;
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (op->code_rate_HP) {
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_2_3:
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (1 << 7);
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_3_4:
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (2 << 7);
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_5_6:
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (3 << 7);
1871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_7_8:
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (4 << 7);
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_1_2:
1921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_AUTO:
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (op->code_rate_LP) {
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_2_3:
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (1 << 4);
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_3_4:
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (2 << 4);
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_5_6:
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (3 << 4);
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_7_8:
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (4 << 4);
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_1_2:
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_AUTO:
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case FEC_NONE:
215cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			if (op->hierarchy == HIERARCHY_AUTO ||
216cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			    op->hierarchy == HIERARCHY_NONE)
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				break;
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
222cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	switch (op->modulation) {
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case QPSK:
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case QAM_AUTO:
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case QAM_16:
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (1 << 13);
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case QAM_64:
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (2 << 13);
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (op->transmission_mode) {
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TRANSMISSION_MODE_2K:
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TRANSMISSION_MODE_AUTO:
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case TRANSMISSION_MODE_8K:
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (1 << 0);
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (op->guard_interval) {
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case GUARD_INTERVAL_1_32:
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case GUARD_INTERVAL_AUTO:
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case GUARD_INTERVAL_1_16:
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (1 << 2);
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case GUARD_INTERVAL_1_8:
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (2 << 2);
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case GUARD_INTERVAL_1_4:
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (3 << 2);
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
264cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	switch (op->hierarchy) {
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case HIERARCHY_AUTO:
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case HIERARCHY_NONE:
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case HIERARCHY_1:
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (1 << 10);
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case HIERARCHY_2:
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (2 << 10);
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case HIERARCHY_4:
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			tps |= (3 << 10);
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return -EINVAL;
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[0] = TPS_GIVEN_1; /* TPS_GIVEN_1 and following registers */
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[1] = msb(tps);      /* TPS_GIVEN_(1|0) */
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[2] = lsb(tps);
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf[3] = 0x50;  // old
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds//	buf[3] = 0xf4;  // pinnacle
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
290cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	mt352_calc_nominal_rate(state, op->bandwidth_hz, buf+4);
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	mt352_calc_input_freq(state, buf+6);
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2934e2eccae27d13d756826533e7cd1214b5825d97cAndrew de Quincey	if (state->config.no_tuner) {
294dea74869f3c62b0b7addd67017b22b394e942aacPatrick Boettcher		if (fe->ops.tuner_ops.set_params) {
29514d24d148c7521b2b88b396652e36f55d061e195Mauro Carvalho Chehab			fe->ops.tuner_ops.set_params(fe);
296dea74869f3c62b0b7addd67017b22b394e942aacPatrick Boettcher			if (fe->ops.i2c_gate_ctrl)
297dea74869f3c62b0b7addd67017b22b394e942aacPatrick Boettcher				fe->ops.i2c_gate_ctrl(fe, 0);
2984e2eccae27d13d756826533e7cd1214b5825d97cAndrew de Quincey		}
2994e2eccae27d13d756826533e7cd1214b5825d97cAndrew de Quincey
300c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey		_mt352_write(fe, buf, 8);
301c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey		_mt352_write(fe, fsm_go, 2);
3020463f12c5cdc3d5577002826c302471b95b2532cAndrew de Quincey	} else {
303dea74869f3c62b0b7addd67017b22b394e942aacPatrick Boettcher		if (fe->ops.tuner_ops.calc_regs) {
304249fa0b01c82f497efa15cb98081183e8938985fMauro Carvalho Chehab			fe->ops.tuner_ops.calc_regs(fe, buf+8, 5);
3050463f12c5cdc3d5577002826c302471b95b2532cAndrew de Quincey			buf[8] <<= 1;
306c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey			_mt352_write(fe, buf, sizeof(buf));
307c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey			_mt352_write(fe, tuner_go, 2);
3080463f12c5cdc3d5577002826c302471b95b2532cAndrew de Quincey		}
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3104e2eccae27d13d756826533e7cd1214b5825d97cAndrew de Quincey
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3147c61d80a9bcfc3fdec8ffd75756cad6a64678229Mauro Carvalho Chehabstatic int mt352_get_parameters(struct dvb_frontend* fe)
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3167c61d80a9bcfc3fdec8ffd75756cad6a64678229Mauro Carvalho Chehab	struct dtv_frontend_properties *op = &fe->dtv_property_cache;
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u16 tps;
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u16 div;
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 trl;
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static const u8 tps_fec_to_api[8] =
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_1_2,
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_2_3,
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_3_4,
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_5_6,
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_7_8,
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_AUTO,
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_AUTO,
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		FEC_AUTO
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	};
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ( (mt352_read_register(state,0x00) & 0xC0) != 0xC0 )
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EINVAL;
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Use TPS_RECEIVED-registers, not the TPS_CURRENT-registers because
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the mt352 sometimes works with the wrong parameters
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	tps = (mt352_read_register(state, TPS_RECEIVED_1) << 8) | mt352_read_register(state, TPS_RECEIVED_0);
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	div = (mt352_read_register(state, CHAN_START_1) << 8) | mt352_read_register(state, CHAN_START_0);
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	trl = mt352_read_register(state, TRL_NOMINAL_RATE_1);
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	op->code_rate_HP = tps_fec_to_api[(tps >> 7) & 7];
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	op->code_rate_LP = tps_fec_to_api[(tps >> 4) & 7];
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch ( (tps >> 13) & 3)
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 0:
349cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->modulation = QPSK;
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 1:
352cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->modulation = QAM_16;
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 2:
355cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->modulation = QAM_64;
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
358cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->modulation = QAM_AUTO;
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	op->transmission_mode = (tps & 0x01) ? TRANSMISSION_MODE_8K : TRANSMISSION_MODE_2K;
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch ( (tps >> 2) & 3)
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 0:
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			op->guard_interval = GUARD_INTERVAL_1_32;
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 1:
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			op->guard_interval = GUARD_INTERVAL_1_16;
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 2:
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			op->guard_interval = GUARD_INTERVAL_1_8;
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 3:
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			op->guard_interval = GUARD_INTERVAL_1_4;
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			op->guard_interval = GUARD_INTERVAL_AUTO;
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch ( (tps >> 10) & 7)
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 0:
386cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->hierarchy = HIERARCHY_NONE;
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 1:
389cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->hierarchy = HIERARCHY_1;
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 2:
392cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->hierarchy = HIERARCHY_2;
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case 3:
395cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->hierarchy = HIERARCHY_4;
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		default:
398cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab			op->hierarchy = HIERARCHY_AUTO;
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
402cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	op->frequency = (500 * (div - IF_FREQUENCYx6)) / 3 * 1000;
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (trl == 0x72)
405cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab		op->bandwidth_hz = 8000000;
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else if (trl == 0x64)
407cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab		op->bandwidth_hz = 7000000;
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
409cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab		op->bandwidth_hz = 6000000;
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (mt352_read_register(state, STATUS_2) & 0x02)
413cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab		op->inversion = INVERSION_OFF;
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	else
415cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab		op->inversion = INVERSION_ON;
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_read_status(struct dvb_frontend* fe, fe_status_t* status)
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int s0, s1, s3;
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* FIXME:
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * The MT352 design manual from Zarlink states (page 46-47):
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Notes about the TUNER_GO register:
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 *
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * If the Read_Tuner_Byte (bit-1) is activated, then the tuner status
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * byte is copied from the tuner to the STATUS_3 register and
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * completion of the read operation is indicated by bit-5 of the
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * INTERRUPT_3 register.
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((s0 = mt352_read_register(state, STATUS_0)) < 0)
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EREMOTEIO;
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((s1 = mt352_read_register(state, STATUS_1)) < 0)
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EREMOTEIO;
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((s3 = mt352_read_register(state, STATUS_3)) < 0)
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -EREMOTEIO;
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*status = 0;
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (s0 & (1 << 4))
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*status |= FE_HAS_CARRIER;
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (s0 & (1 << 1))
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*status |= FE_HAS_VITERBI;
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (s0 & (1 << 5))
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*status |= FE_HAS_LOCK;
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (s1 & (1 << 1))
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*status |= FE_HAS_SYNC;
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (s3 & (1 << 6))
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*status |= FE_HAS_SIGNAL;
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((*status & (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC)) !=
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		      (FE_HAS_CARRIER | FE_HAS_VITERBI | FE_HAS_SYNC))
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		*status &= ~FE_HAS_LOCK;
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_read_ber(struct dvb_frontend* fe, u32* ber)
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*ber = (mt352_read_register (state, RS_ERR_CNT_2) << 16) |
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	       (mt352_read_register (state, RS_ERR_CNT_1) << 8) |
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	       (mt352_read_register (state, RS_ERR_CNT_0));
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_read_signal_strength(struct dvb_frontend* fe, u16* strength)
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4784ff4ac1beae58a2fea7ec2ad43d6c3b60d90ec61Barry Scott	/* align the 12 bit AGC gain with the most significant bits */
4794ff4ac1beae58a2fea7ec2ad43d6c3b60d90ec61Barry Scott	u16 signal = ((mt352_read_register(state, AGC_GAIN_1) & 0x0f) << 12) |
4804ff4ac1beae58a2fea7ec2ad43d6c3b60d90ec61Barry Scott		(mt352_read_register(state, AGC_GAIN_0) << 4);
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4824ff4ac1beae58a2fea7ec2ad43d6c3b60d90ec61Barry Scott	/* inverse of gain is signal strength */
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*strength = ~signal;
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_read_snr(struct dvb_frontend* fe, u16* snr)
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 _snr = mt352_read_register (state, SNR);
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*snr = (_snr << 8) | _snr;
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	*ucblocks = (mt352_read_register (state,  RS_UBC_1) << 8) |
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    (mt352_read_register (state,  RS_UBC_0));
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fe_tune_settings)
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fe_tune_settings->min_delay_ms = 800;
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fe_tune_settings->step_size = 0;
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	fe_tune_settings->max_drift = 0;
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int mt352_init(struct dvb_frontend* fe)
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	static u8 mt352_reset_attach [] = { RESET, 0xC0 };
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
522271ddbf702c3a4e6b18f6464180eda0f62efd9a5Harvey Harrison	dprintk("%s: hello\n",__func__);
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((mt352_read_register(state, CLOCK_CTL) & 0x10) == 0 ||
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	    (mt352_read_register(state, CONFIG) & 0x20) == 0) {
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Do a "hard" reset */
528c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey		_mt352_write(fe, mt352_reset_attach, sizeof(mt352_reset_attach));
52977b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach		return state->config.demod_init(fe);
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void mt352_release(struct dvb_frontend* fe)
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = fe->demodulator_priv;
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(state);
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct dvb_frontend_ops mt352_ops;
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct dvb_frontend* mt352_attach(const struct mt352_config* config,
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  struct i2c_adapter* i2c)
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mt352_state* state = NULL;
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* allocate memory for the internal state */
5497408187d223f63d46a13b6a35b8f96b032c2f623Panagiotis Issaris	state = kzalloc(sizeof(struct mt352_state), GFP_KERNEL);
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (state == NULL) goto error;
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* setup the state */
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	state->i2c = i2c;
55477b3bd0ccb2fae27e710b921f053ae16f34d70deJohannes Stezenbach	memcpy(&state->config,config,sizeof(struct mt352_config));
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* check if the demod is there */
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (mt352_read_register(state, CHIP_ID) != ID_MT352) goto error;
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* create dvb_frontend */
560dea74869f3c62b0b7addd67017b22b394e942aacPatrick Boettcher	memcpy(&state->frontend.ops, &mt352_ops, sizeof(struct dvb_frontend_ops));
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	state->frontend.demodulator_priv = state;
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return &state->frontend;
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldserror:
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(state);
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return NULL;
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct dvb_frontend_ops mt352_ops = {
570cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	.delsys = { SYS_DVBT },
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.info = {
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.name			= "Zarlink MT352 DVB-T",
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.frequency_min		= 174000000,
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.frequency_max		= 862000000,
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.frequency_stepsize	= 166667,
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.frequency_tolerance	= 0,
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
5781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			FE_CAN_FEC_AUTO |
5801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER |
5831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			FE_CAN_MUTE_TS
5841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	},
5851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.release = mt352_release,
5871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.init = mt352_init,
5891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.sleep = mt352_sleep,
590c10d14d62d7b7596fd5c7bb8aad3f2b56f8640e6Andrew de Quincey	.write = _mt352_write,
5911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
592cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	.set_frontend = mt352_set_parameters,
593cf45787ced807791fc02ba9477b1bb320dd14801Mauro Carvalho Chehab	.get_frontend = mt352_get_parameters,
5941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.get_tune_settings = mt352_get_tune_settings,
5951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_status = mt352_read_status,
5971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_ber = mt352_read_ber,
5981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_signal_strength = mt352_read_signal_strength,
5991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_snr = mt352_read_snr,
6001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.read_ucblocks = mt352_read_ucblocks,
6011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
6021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_param(debug, int, 0644);
6041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
6051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("Zarlink MT352 DVB-T Demodulator driver");
6071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_AUTHOR("Holger Waechtler, Daniel Mack, Antonio Mancuso");
6081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
6091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsEXPORT_SYMBOL(mt352_attach);
611