19a0bf528SMauro Carvalho Chehab /*
29a0bf528SMauro Carvalho Chehab  *  Driver for the Integrant ITD1000 "Zero-IF Tuner IC for Direct Broadcast Satellite"
39a0bf528SMauro Carvalho Chehab  *
49a0bf528SMauro Carvalho Chehab  *  Copyright (c) 2007-8 Patrick Boettcher <pb@linuxtv.org>
59a0bf528SMauro Carvalho Chehab  *
69a0bf528SMauro Carvalho Chehab  *  This program is free software; you can redistribute it and/or modify
79a0bf528SMauro Carvalho Chehab  *  it under the terms of the GNU General Public License as published by
89a0bf528SMauro Carvalho Chehab  *  the Free Software Foundation; either version 2 of the License, or
99a0bf528SMauro Carvalho Chehab  *  (at your option) any later version.
109a0bf528SMauro Carvalho Chehab  *
119a0bf528SMauro Carvalho Chehab  *  This program is distributed in the hope that it will be useful,
129a0bf528SMauro Carvalho Chehab  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
139a0bf528SMauro Carvalho Chehab  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
149a0bf528SMauro Carvalho Chehab  *
159a0bf528SMauro Carvalho Chehab  *  GNU General Public License for more details.
169a0bf528SMauro Carvalho Chehab  *
179a0bf528SMauro Carvalho Chehab  *  You should have received a copy of the GNU General Public License
189a0bf528SMauro Carvalho Chehab  *  along with this program; if not, write to the Free Software
199a0bf528SMauro Carvalho Chehab  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
209a0bf528SMauro Carvalho Chehab  */
219a0bf528SMauro Carvalho Chehab 
229a0bf528SMauro Carvalho Chehab #include <linux/module.h>
239a0bf528SMauro Carvalho Chehab #include <linux/moduleparam.h>
249a0bf528SMauro Carvalho Chehab #include <linux/delay.h>
259a0bf528SMauro Carvalho Chehab #include <linux/dvb/frontend.h>
269a0bf528SMauro Carvalho Chehab #include <linux/i2c.h>
279a0bf528SMauro Carvalho Chehab #include <linux/slab.h>
289a0bf528SMauro Carvalho Chehab 
299a0bf528SMauro Carvalho Chehab #include "dvb_frontend.h"
309a0bf528SMauro Carvalho Chehab 
319a0bf528SMauro Carvalho Chehab #include "itd1000.h"
329a0bf528SMauro Carvalho Chehab #include "itd1000_priv.h"
339a0bf528SMauro Carvalho Chehab 
349a0bf528SMauro Carvalho Chehab static int debug;
359a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644);
369a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
379a0bf528SMauro Carvalho Chehab 
389a0bf528SMauro Carvalho Chehab #define itd_dbg(args...)  do { \
399a0bf528SMauro Carvalho Chehab 	if (debug) { \
409a0bf528SMauro Carvalho Chehab 		printk(KERN_DEBUG   "ITD1000: " args);\
419a0bf528SMauro Carvalho Chehab 	} \
429a0bf528SMauro Carvalho Chehab } while (0)
439a0bf528SMauro Carvalho Chehab 
449a0bf528SMauro Carvalho Chehab #define itd_warn(args...) do { \
459a0bf528SMauro Carvalho Chehab 	printk(KERN_WARNING "ITD1000: " args); \
469a0bf528SMauro Carvalho Chehab } while (0)
479a0bf528SMauro Carvalho Chehab 
489a0bf528SMauro Carvalho Chehab #define itd_info(args...) do { \
499a0bf528SMauro Carvalho Chehab 	printk(KERN_INFO    "ITD1000: " args); \
509a0bf528SMauro Carvalho Chehab } while (0)
519a0bf528SMauro Carvalho Chehab 
529a0bf528SMauro Carvalho Chehab /* don't write more than one byte with flexcop behind */
539a0bf528SMauro Carvalho Chehab static int itd1000_write_regs(struct itd1000_state *state, u8 reg, u8 v[], u8 len)
549a0bf528SMauro Carvalho Chehab {
559a0bf528SMauro Carvalho Chehab 	u8 buf[1+len];
569a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {
579a0bf528SMauro Carvalho Chehab 		.addr = state->cfg->i2c_address, .flags = 0, .buf = buf, .len = len+1
589a0bf528SMauro Carvalho Chehab 	};
599a0bf528SMauro Carvalho Chehab 	buf[0] = reg;
609a0bf528SMauro Carvalho Chehab 	memcpy(&buf[1], v, len);
619a0bf528SMauro Carvalho Chehab 
629a0bf528SMauro Carvalho Chehab 	/* itd_dbg("wr %02x: %02x\n", reg, v[0]); */
639a0bf528SMauro Carvalho Chehab 
649a0bf528SMauro Carvalho Chehab 	if (i2c_transfer(state->i2c, &msg, 1) != 1) {
659a0bf528SMauro Carvalho Chehab 		printk(KERN_WARNING "itd1000 I2C write failed\n");
669a0bf528SMauro Carvalho Chehab 		return -EREMOTEIO;
679a0bf528SMauro Carvalho Chehab 	}
689a0bf528SMauro Carvalho Chehab 	return 0;
699a0bf528SMauro Carvalho Chehab }
709a0bf528SMauro Carvalho Chehab 
719a0bf528SMauro Carvalho Chehab static int itd1000_read_reg(struct itd1000_state *state, u8 reg)
729a0bf528SMauro Carvalho Chehab {
739a0bf528SMauro Carvalho Chehab 	u8 val;
749a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[2] = {
759a0bf528SMauro Carvalho Chehab 		{ .addr = state->cfg->i2c_address, .flags = 0,        .buf = &reg, .len = 1 },
769a0bf528SMauro Carvalho Chehab 		{ .addr = state->cfg->i2c_address, .flags = I2C_M_RD, .buf = &val, .len = 1 },
779a0bf528SMauro Carvalho Chehab 	};
789a0bf528SMauro Carvalho Chehab 
799a0bf528SMauro Carvalho Chehab 	/* ugly flexcop workaround */
809a0bf528SMauro Carvalho Chehab 	itd1000_write_regs(state, (reg - 1) & 0xff, &state->shadow[(reg - 1) & 0xff], 1);
819a0bf528SMauro Carvalho Chehab 
829a0bf528SMauro Carvalho Chehab 	if (i2c_transfer(state->i2c, msg, 2) != 2) {
839a0bf528SMauro Carvalho Chehab 		itd_warn("itd1000 I2C read failed\n");
849a0bf528SMauro Carvalho Chehab 		return -EREMOTEIO;
859a0bf528SMauro Carvalho Chehab 	}
869a0bf528SMauro Carvalho Chehab 	return val;
879a0bf528SMauro Carvalho Chehab }
889a0bf528SMauro Carvalho Chehab 
899a0bf528SMauro Carvalho Chehab static inline int itd1000_write_reg(struct itd1000_state *state, u8 r, u8 v)
909a0bf528SMauro Carvalho Chehab {
919a0bf528SMauro Carvalho Chehab 	int ret = itd1000_write_regs(state, r, &v, 1);
929a0bf528SMauro Carvalho Chehab 	state->shadow[r] = v;
939a0bf528SMauro Carvalho Chehab 	return ret;
949a0bf528SMauro Carvalho Chehab }
959a0bf528SMauro Carvalho Chehab 
969a0bf528SMauro Carvalho Chehab 
979a0bf528SMauro Carvalho Chehab static struct {
989a0bf528SMauro Carvalho Chehab 	u32 symbol_rate;
999a0bf528SMauro Carvalho Chehab 	u8  pgaext  : 4; /* PLLFH */
1009a0bf528SMauro Carvalho Chehab 	u8  bbgvmin : 4; /* BBGVMIN */
1019a0bf528SMauro Carvalho Chehab } itd1000_lpf_pga[] = {
1029a0bf528SMauro Carvalho Chehab 	{        0, 0x8, 0x3 },
1039a0bf528SMauro Carvalho Chehab 	{  5200000, 0x8, 0x3 },
1049a0bf528SMauro Carvalho Chehab 	{ 12200000, 0x4, 0x3 },
1059a0bf528SMauro Carvalho Chehab 	{ 15400000, 0x2, 0x3 },
1069a0bf528SMauro Carvalho Chehab 	{ 19800000, 0x2, 0x3 },
1079a0bf528SMauro Carvalho Chehab 	{ 21500000, 0x2, 0x3 },
1089a0bf528SMauro Carvalho Chehab 	{ 24500000, 0x2, 0x3 },
1099a0bf528SMauro Carvalho Chehab 	{ 28400000, 0x2, 0x3 },
1109a0bf528SMauro Carvalho Chehab 	{ 33400000, 0x2, 0x3 },
1119a0bf528SMauro Carvalho Chehab 	{ 34400000, 0x1, 0x4 },
1129a0bf528SMauro Carvalho Chehab 	{ 34400000, 0x1, 0x4 },
1139a0bf528SMauro Carvalho Chehab 	{ 38400000, 0x1, 0x4 },
1149a0bf528SMauro Carvalho Chehab 	{ 38400000, 0x1, 0x4 },
1159a0bf528SMauro Carvalho Chehab 	{ 40400000, 0x1, 0x4 },
1169a0bf528SMauro Carvalho Chehab 	{ 45400000, 0x1, 0x4 },
1179a0bf528SMauro Carvalho Chehab };
1189a0bf528SMauro Carvalho Chehab 
1199a0bf528SMauro Carvalho Chehab static void itd1000_set_lpf_bw(struct itd1000_state *state, u32 symbol_rate)
1209a0bf528SMauro Carvalho Chehab {
1219a0bf528SMauro Carvalho Chehab 	u8 i;
1229a0bf528SMauro Carvalho Chehab 	u8 con1    = itd1000_read_reg(state, CON1)    & 0xfd;
1239a0bf528SMauro Carvalho Chehab 	u8 pllfh   = itd1000_read_reg(state, PLLFH)   & 0x0f;
1249a0bf528SMauro Carvalho Chehab 	u8 bbgvmin = itd1000_read_reg(state, BBGVMIN) & 0xf0;
1259a0bf528SMauro Carvalho Chehab 	u8 bw      = itd1000_read_reg(state, BW)      & 0xf0;
1269a0bf528SMauro Carvalho Chehab 
1279a0bf528SMauro Carvalho Chehab 	itd_dbg("symbol_rate = %d\n", symbol_rate);
1289a0bf528SMauro Carvalho Chehab 
1299a0bf528SMauro Carvalho Chehab 	/* not sure what is that ? - starting to download the table */
1309a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, CON1, con1 | (1 << 1));
1319a0bf528SMauro Carvalho Chehab 
1329a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_lpf_pga); i++)
1339a0bf528SMauro Carvalho Chehab 		if (symbol_rate < itd1000_lpf_pga[i].symbol_rate) {
1349a0bf528SMauro Carvalho Chehab 			itd_dbg("symrate: index: %d pgaext: %x, bbgvmin: %x\n", i, itd1000_lpf_pga[i].pgaext, itd1000_lpf_pga[i].bbgvmin);
1359a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, PLLFH,   pllfh | (itd1000_lpf_pga[i].pgaext << 4));
1369a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, BBGVMIN, bbgvmin | (itd1000_lpf_pga[i].bbgvmin));
1379a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, BW,      bw | (i & 0x0f));
1389a0bf528SMauro Carvalho Chehab 			break;
1399a0bf528SMauro Carvalho Chehab 		}
1409a0bf528SMauro Carvalho Chehab 
1419a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, CON1, con1 | (0 << 1));
1429a0bf528SMauro Carvalho Chehab }
1439a0bf528SMauro Carvalho Chehab 
1449a0bf528SMauro Carvalho Chehab static struct {
1459a0bf528SMauro Carvalho Chehab 	u8 vcorg;
1469a0bf528SMauro Carvalho Chehab 	u32 fmax_rg;
1479a0bf528SMauro Carvalho Chehab } itd1000_vcorg[] = {
1489a0bf528SMauro Carvalho Chehab 	{  1,  920000 },
1499a0bf528SMauro Carvalho Chehab 	{  2,  971000 },
1509a0bf528SMauro Carvalho Chehab 	{  3, 1031000 },
1519a0bf528SMauro Carvalho Chehab 	{  4, 1091000 },
1529a0bf528SMauro Carvalho Chehab 	{  5, 1171000 },
1539a0bf528SMauro Carvalho Chehab 	{  6, 1281000 },
1549a0bf528SMauro Carvalho Chehab 	{  7, 1381000 },
1559a0bf528SMauro Carvalho Chehab 	{  8,  500000 },	/* this is intentional. */
1569a0bf528SMauro Carvalho Chehab 	{  9, 1451000 },
1579a0bf528SMauro Carvalho Chehab 	{ 10, 1531000 },
1589a0bf528SMauro Carvalho Chehab 	{ 11, 1631000 },
1599a0bf528SMauro Carvalho Chehab 	{ 12, 1741000 },
1609a0bf528SMauro Carvalho Chehab 	{ 13, 1891000 },
1619a0bf528SMauro Carvalho Chehab 	{ 14, 2071000 },
1629a0bf528SMauro Carvalho Chehab 	{ 15, 2250000 },
1639a0bf528SMauro Carvalho Chehab };
1649a0bf528SMauro Carvalho Chehab 
1659a0bf528SMauro Carvalho Chehab static void itd1000_set_vco(struct itd1000_state *state, u32 freq_khz)
1669a0bf528SMauro Carvalho Chehab {
1679a0bf528SMauro Carvalho Chehab 	u8 i;
1689a0bf528SMauro Carvalho Chehab 	u8 gvbb_i2c     = itd1000_read_reg(state, GVBB_I2C) & 0xbf;
1699a0bf528SMauro Carvalho Chehab 	u8 vco_chp1_i2c = itd1000_read_reg(state, VCO_CHP1_I2C) & 0x0f;
1709a0bf528SMauro Carvalho Chehab 	u8 adcout;
1719a0bf528SMauro Carvalho Chehab 
1729a0bf528SMauro Carvalho Chehab 	/* reserved bit again (reset ?) */
1739a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, GVBB_I2C, gvbb_i2c | (1 << 6));
1749a0bf528SMauro Carvalho Chehab 
1759a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_vcorg); i++) {
1769a0bf528SMauro Carvalho Chehab 		if (freq_khz < itd1000_vcorg[i].fmax_rg) {
1779a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | (itd1000_vcorg[i].vcorg << 4));
1789a0bf528SMauro Carvalho Chehab 			msleep(1);
1799a0bf528SMauro Carvalho Chehab 
1809a0bf528SMauro Carvalho Chehab 			adcout = itd1000_read_reg(state, PLLLOCK) & 0x0f;
1819a0bf528SMauro Carvalho Chehab 
1829a0bf528SMauro Carvalho Chehab 			itd_dbg("VCO: %dkHz: %d -> ADCOUT: %d %02x\n", freq_khz, itd1000_vcorg[i].vcorg, adcout, vco_chp1_i2c);
1839a0bf528SMauro Carvalho Chehab 
1849a0bf528SMauro Carvalho Chehab 			if (adcout > 13) {
1859a0bf528SMauro Carvalho Chehab 				if (!(itd1000_vcorg[i].vcorg == 7 || itd1000_vcorg[i].vcorg == 15))
1869a0bf528SMauro Carvalho Chehab 					itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg + 1) << 4));
1879a0bf528SMauro Carvalho Chehab 			} else if (adcout < 2) {
1889a0bf528SMauro Carvalho Chehab 				if (!(itd1000_vcorg[i].vcorg == 1 || itd1000_vcorg[i].vcorg == 9))
1899a0bf528SMauro Carvalho Chehab 					itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg - 1) << 4));
1909a0bf528SMauro Carvalho Chehab 			}
1919a0bf528SMauro Carvalho Chehab 			break;
1929a0bf528SMauro Carvalho Chehab 		}
1939a0bf528SMauro Carvalho Chehab 	}
1949a0bf528SMauro Carvalho Chehab }
1959a0bf528SMauro Carvalho Chehab 
1969a0bf528SMauro Carvalho Chehab static const struct {
1979a0bf528SMauro Carvalho Chehab 	u32 freq;
1989a0bf528SMauro Carvalho Chehab 	u8 values[10]; /* RFTR, RFST1 - RFST9 */
1999a0bf528SMauro Carvalho Chehab } itd1000_fre_values[] = {
2009a0bf528SMauro Carvalho Chehab 	{ 1075000, { 0x59, 0x1d, 0x1c, 0x17, 0x16, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2019a0bf528SMauro Carvalho Chehab 	{ 1250000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2029a0bf528SMauro Carvalho Chehab 	{ 1450000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2039a0bf528SMauro Carvalho Chehab 	{ 1650000, { 0x69, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2049a0bf528SMauro Carvalho Chehab 	{ 1750000, { 0x69, 0x1e, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2059a0bf528SMauro Carvalho Chehab 	{ 1850000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
2069a0bf528SMauro Carvalho Chehab 	{ 1900000, { 0x69, 0x1d, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
2079a0bf528SMauro Carvalho Chehab 	{ 1950000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0d, 0x0b, 0x0a } },
2089a0bf528SMauro Carvalho Chehab 	{ 2050000, { 0x69, 0x1e, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0b, 0x0a } },
2099a0bf528SMauro Carvalho Chehab 	{ 2150000, { 0x69, 0x1d, 0x1c, 0x17, 0x15, 0x14, 0x13, 0x0f, 0x0e, 0x0b } }
2109a0bf528SMauro Carvalho Chehab };
2119a0bf528SMauro Carvalho Chehab 
2129a0bf528SMauro Carvalho Chehab 
2139a0bf528SMauro Carvalho Chehab #define FREF 16
2149a0bf528SMauro Carvalho Chehab 
2159a0bf528SMauro Carvalho Chehab static void itd1000_set_lo(struct itd1000_state *state, u32 freq_khz)
2169a0bf528SMauro Carvalho Chehab {
2179a0bf528SMauro Carvalho Chehab 	int i, j;
2189a0bf528SMauro Carvalho Chehab 	u32 plln, pllf;
2199a0bf528SMauro Carvalho Chehab 	u64 tmp;
2209a0bf528SMauro Carvalho Chehab 
2219a0bf528SMauro Carvalho Chehab 	plln = (freq_khz * 1000) / 2 / FREF;
2229a0bf528SMauro Carvalho Chehab 
2239a0bf528SMauro Carvalho Chehab 	/* Compute the factional part times 1000 */
2249a0bf528SMauro Carvalho Chehab 	tmp  = plln % 1000000;
2259a0bf528SMauro Carvalho Chehab 	plln /= 1000000;
2269a0bf528SMauro Carvalho Chehab 
2279a0bf528SMauro Carvalho Chehab 	tmp *= 1048576;
2289a0bf528SMauro Carvalho Chehab 	do_div(tmp, 1000000);
2299a0bf528SMauro Carvalho Chehab 	pllf = (u32) tmp;
2309a0bf528SMauro Carvalho Chehab 
2319a0bf528SMauro Carvalho Chehab 	state->frequency = ((plln * 1000) + (pllf * 1000)/1048576) * 2*FREF;
2329a0bf528SMauro Carvalho Chehab 	itd_dbg("frequency: %dkHz (wanted) %dkHz (set), PLLF = %d, PLLN = %d\n", freq_khz, state->frequency, pllf, plln);
2339a0bf528SMauro Carvalho Chehab 
2341c4bbfd1SPeter Senna Tschudin 	itd1000_write_reg(state, PLLNH, 0x80); /* PLLNH */
2359a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLNL, plln & 0xff);
2369a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLFH, (itd1000_read_reg(state, PLLFH) & 0xf0) | ((pllf >> 16) & 0x0f));
2379a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLFM, (pllf >> 8) & 0xff);
2389a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLFL, (pllf >> 0) & 0xff);
2399a0bf528SMauro Carvalho Chehab 
2409a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_fre_values); i++) {
2419a0bf528SMauro Carvalho Chehab 		if (freq_khz <= itd1000_fre_values[i].freq) {
2429a0bf528SMauro Carvalho Chehab 			itd_dbg("fre_values: %d\n", i);
2439a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, RFTR, itd1000_fre_values[i].values[0]);
2449a0bf528SMauro Carvalho Chehab 			for (j = 0; j < 9; j++)
2459a0bf528SMauro Carvalho Chehab 				itd1000_write_reg(state, RFST1+j, itd1000_fre_values[i].values[j+1]);
2469a0bf528SMauro Carvalho Chehab 			break;
2479a0bf528SMauro Carvalho Chehab 		}
2489a0bf528SMauro Carvalho Chehab 	}
2499a0bf528SMauro Carvalho Chehab 
2509a0bf528SMauro Carvalho Chehab 	itd1000_set_vco(state, freq_khz);
2519a0bf528SMauro Carvalho Chehab }
2529a0bf528SMauro Carvalho Chehab 
2539a0bf528SMauro Carvalho Chehab static int itd1000_set_parameters(struct dvb_frontend *fe)
2549a0bf528SMauro Carvalho Chehab {
2559a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2569a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = fe->tuner_priv;
2579a0bf528SMauro Carvalho Chehab 	u8 pllcon1;
2589a0bf528SMauro Carvalho Chehab 
2599a0bf528SMauro Carvalho Chehab 	itd1000_set_lo(state, c->frequency);
2609a0bf528SMauro Carvalho Chehab 	itd1000_set_lpf_bw(state, c->symbol_rate);
2619a0bf528SMauro Carvalho Chehab 
2629a0bf528SMauro Carvalho Chehab 	pllcon1 = itd1000_read_reg(state, PLLCON1) & 0x7f;
2639a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLCON1, pllcon1 | (1 << 7));
2649a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLCON1, pllcon1);
2659a0bf528SMauro Carvalho Chehab 
2669a0bf528SMauro Carvalho Chehab 	return 0;
2679a0bf528SMauro Carvalho Chehab }
2689a0bf528SMauro Carvalho Chehab 
2699a0bf528SMauro Carvalho Chehab static int itd1000_get_frequency(struct dvb_frontend *fe, u32 *frequency)
2709a0bf528SMauro Carvalho Chehab {
2719a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = fe->tuner_priv;
2729a0bf528SMauro Carvalho Chehab 	*frequency = state->frequency;
2739a0bf528SMauro Carvalho Chehab 	return 0;
2749a0bf528SMauro Carvalho Chehab }
2759a0bf528SMauro Carvalho Chehab 
2769a0bf528SMauro Carvalho Chehab static int itd1000_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
2779a0bf528SMauro Carvalho Chehab {
2789a0bf528SMauro Carvalho Chehab 	return 0;
2799a0bf528SMauro Carvalho Chehab }
2809a0bf528SMauro Carvalho Chehab 
2819a0bf528SMauro Carvalho Chehab static u8 itd1000_init_tab[][2] = {
2829a0bf528SMauro Carvalho Chehab 	{ PLLCON1,       0x65 }, /* Register does not change */
2839a0bf528SMauro Carvalho Chehab 	{ PLLNH,         0x80 }, /* Bits [7:6] do not change */
2849a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X6D, 0x3b },
2859a0bf528SMauro Carvalho Chehab 	{ VCO_CHP2_I2C,  0x12 },
2869a0bf528SMauro Carvalho Chehab 	{ 0x72,          0xf9 }, /* No such regsister defined */
2879a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X73, 0xff },
2889a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X74, 0xb2 },
2899a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X75, 0xc7 },
2909a0bf528SMauro Carvalho Chehab 	{ EXTGVBBRF,     0xf0 },
2919a0bf528SMauro Carvalho Chehab 	{ DIVAGCCK,      0x80 },
2929a0bf528SMauro Carvalho Chehab 	{ BBTR,          0xa0 },
2939a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X7E, 0x4f },
2949a0bf528SMauro Carvalho Chehab 	{ 0x82,          0x88 }, /* No such regsister defined */
2959a0bf528SMauro Carvalho Chehab 	{ 0x83,          0x80 }, /* No such regsister defined */
2969a0bf528SMauro Carvalho Chehab 	{ 0x84,          0x80 }, /* No such regsister defined */
2979a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X85, 0x74 },
2989a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X86, 0xff },
2999a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X88, 0x02 },
3009a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X89, 0x16 },
3019a0bf528SMauro Carvalho Chehab 	{ RFST0,         0x1f },
3029a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X94, 0x66 },
3039a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X95, 0x66 },
3049a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X96, 0x77 },
3059a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X97, 0x99 },
3069a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X98, 0xff },
3079a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X99, 0xfc },
3089a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X9A, 0xba },
3099a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X9B, 0xaa },
3109a0bf528SMauro Carvalho Chehab };
3119a0bf528SMauro Carvalho Chehab 
3129a0bf528SMauro Carvalho Chehab static u8 itd1000_reinit_tab[][2] = {
3139a0bf528SMauro Carvalho Chehab 	{ VCO_CHP1_I2C, 0x8a },
3149a0bf528SMauro Carvalho Chehab 	{ BW,           0x87 },
3159a0bf528SMauro Carvalho Chehab 	{ GVBB_I2C,     0x03 },
3169a0bf528SMauro Carvalho Chehab 	{ BBGVMIN,      0x03 },
3179a0bf528SMauro Carvalho Chehab 	{ CON1,         0x2e },
3189a0bf528SMauro Carvalho Chehab };
3199a0bf528SMauro Carvalho Chehab 
3209a0bf528SMauro Carvalho Chehab 
3219a0bf528SMauro Carvalho Chehab static int itd1000_init(struct dvb_frontend *fe)
3229a0bf528SMauro Carvalho Chehab {
3239a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = fe->tuner_priv;
3249a0bf528SMauro Carvalho Chehab 	int i;
3259a0bf528SMauro Carvalho Chehab 
3269a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_init_tab); i++)
3279a0bf528SMauro Carvalho Chehab 		itd1000_write_reg(state, itd1000_init_tab[i][0], itd1000_init_tab[i][1]);
3289a0bf528SMauro Carvalho Chehab 
3299a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_reinit_tab); i++)
3309a0bf528SMauro Carvalho Chehab 		itd1000_write_reg(state, itd1000_reinit_tab[i][0], itd1000_reinit_tab[i][1]);
3319a0bf528SMauro Carvalho Chehab 
3329a0bf528SMauro Carvalho Chehab 	return 0;
3339a0bf528SMauro Carvalho Chehab }
3349a0bf528SMauro Carvalho Chehab 
3359a0bf528SMauro Carvalho Chehab static int itd1000_sleep(struct dvb_frontend *fe)
3369a0bf528SMauro Carvalho Chehab {
3379a0bf528SMauro Carvalho Chehab 	return 0;
3389a0bf528SMauro Carvalho Chehab }
3399a0bf528SMauro Carvalho Chehab 
3409a0bf528SMauro Carvalho Chehab static int itd1000_release(struct dvb_frontend *fe)
3419a0bf528SMauro Carvalho Chehab {
3429a0bf528SMauro Carvalho Chehab 	kfree(fe->tuner_priv);
3439a0bf528SMauro Carvalho Chehab 	fe->tuner_priv = NULL;
3449a0bf528SMauro Carvalho Chehab 	return 0;
3459a0bf528SMauro Carvalho Chehab }
3469a0bf528SMauro Carvalho Chehab 
3479a0bf528SMauro Carvalho Chehab static const struct dvb_tuner_ops itd1000_tuner_ops = {
3489a0bf528SMauro Carvalho Chehab 	.info = {
3499a0bf528SMauro Carvalho Chehab 		.name           = "Integrant ITD1000",
3509a0bf528SMauro Carvalho Chehab 		.frequency_min  = 950000,
3519a0bf528SMauro Carvalho Chehab 		.frequency_max  = 2150000,
3529a0bf528SMauro Carvalho Chehab 		.frequency_step = 125,     /* kHz for QPSK frontends */
3539a0bf528SMauro Carvalho Chehab 	},
3549a0bf528SMauro Carvalho Chehab 
3559a0bf528SMauro Carvalho Chehab 	.release       = itd1000_release,
3569a0bf528SMauro Carvalho Chehab 
3579a0bf528SMauro Carvalho Chehab 	.init          = itd1000_init,
3589a0bf528SMauro Carvalho Chehab 	.sleep         = itd1000_sleep,
3599a0bf528SMauro Carvalho Chehab 
3609a0bf528SMauro Carvalho Chehab 	.set_params    = itd1000_set_parameters,
3619a0bf528SMauro Carvalho Chehab 	.get_frequency = itd1000_get_frequency,
3629a0bf528SMauro Carvalho Chehab 	.get_bandwidth = itd1000_get_bandwidth
3639a0bf528SMauro Carvalho Chehab };
3649a0bf528SMauro Carvalho Chehab 
3659a0bf528SMauro Carvalho Chehab 
3669a0bf528SMauro Carvalho Chehab struct dvb_frontend *itd1000_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct itd1000_config *cfg)
3679a0bf528SMauro Carvalho Chehab {
3689a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = NULL;
3699a0bf528SMauro Carvalho Chehab 	u8 i = 0;
3709a0bf528SMauro Carvalho Chehab 
3719a0bf528SMauro Carvalho Chehab 	state = kzalloc(sizeof(struct itd1000_state), GFP_KERNEL);
3729a0bf528SMauro Carvalho Chehab 	if (state == NULL)
3739a0bf528SMauro Carvalho Chehab 		return NULL;
3749a0bf528SMauro Carvalho Chehab 
3759a0bf528SMauro Carvalho Chehab 	state->cfg = cfg;
3769a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
3779a0bf528SMauro Carvalho Chehab 
3789a0bf528SMauro Carvalho Chehab 	i = itd1000_read_reg(state, 0);
3799a0bf528SMauro Carvalho Chehab 	if (i != 0) {
3809a0bf528SMauro Carvalho Chehab 		kfree(state);
3819a0bf528SMauro Carvalho Chehab 		return NULL;
3829a0bf528SMauro Carvalho Chehab 	}
3839a0bf528SMauro Carvalho Chehab 	itd_info("successfully identified (ID: %d)\n", i);
3849a0bf528SMauro Carvalho Chehab 
3859a0bf528SMauro Carvalho Chehab 	memset(state->shadow, 0xff, sizeof(state->shadow));
3869a0bf528SMauro Carvalho Chehab 	for (i = 0x65; i < 0x9c; i++)
3879a0bf528SMauro Carvalho Chehab 		state->shadow[i] = itd1000_read_reg(state, i);
3889a0bf528SMauro Carvalho Chehab 
3899a0bf528SMauro Carvalho Chehab 	memcpy(&fe->ops.tuner_ops, &itd1000_tuner_ops, sizeof(struct dvb_tuner_ops));
3909a0bf528SMauro Carvalho Chehab 
3919a0bf528SMauro Carvalho Chehab 	fe->tuner_priv = state;
3929a0bf528SMauro Carvalho Chehab 
3939a0bf528SMauro Carvalho Chehab 	return fe;
3949a0bf528SMauro Carvalho Chehab }
3959a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(itd1000_attach);
3969a0bf528SMauro Carvalho Chehab 
3979a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
3989a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Integrant ITD1000 driver");
3999a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
400