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 
189a0bf528SMauro Carvalho Chehab #include <linux/module.h>
199a0bf528SMauro Carvalho Chehab #include <linux/moduleparam.h>
209a0bf528SMauro Carvalho Chehab #include <linux/delay.h>
219a0bf528SMauro Carvalho Chehab #include <linux/dvb/frontend.h>
229a0bf528SMauro Carvalho Chehab #include <linux/i2c.h>
239a0bf528SMauro Carvalho Chehab #include <linux/slab.h>
249a0bf528SMauro Carvalho Chehab 
25fada1935SMauro Carvalho Chehab #include <media/dvb_frontend.h>
269a0bf528SMauro Carvalho Chehab 
279a0bf528SMauro Carvalho Chehab #include "itd1000.h"
289a0bf528SMauro Carvalho Chehab #include "itd1000_priv.h"
299a0bf528SMauro Carvalho Chehab 
308393796dSMauro Carvalho Chehab /* Max transfer size done by I2C transfer functions */
318393796dSMauro Carvalho Chehab #define MAX_XFER_SIZE  64
328393796dSMauro Carvalho Chehab 
339a0bf528SMauro Carvalho Chehab static int debug;
349a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644);
359a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
369a0bf528SMauro Carvalho Chehab 
379a0bf528SMauro Carvalho Chehab #define itd_dbg(args...)  do { \
389a0bf528SMauro Carvalho Chehab 	if (debug) { \
399a0bf528SMauro Carvalho Chehab 		printk(KERN_DEBUG   "ITD1000: " args);\
409a0bf528SMauro Carvalho Chehab 	} \
419a0bf528SMauro Carvalho Chehab } while (0)
429a0bf528SMauro Carvalho Chehab 
439a0bf528SMauro Carvalho Chehab #define itd_warn(args...) do { \
449a0bf528SMauro Carvalho Chehab 	printk(KERN_WARNING "ITD1000: " args); \
459a0bf528SMauro Carvalho Chehab } while (0)
469a0bf528SMauro Carvalho Chehab 
479a0bf528SMauro Carvalho Chehab #define itd_info(args...) do { \
489a0bf528SMauro Carvalho Chehab 	printk(KERN_INFO    "ITD1000: " args); \
499a0bf528SMauro Carvalho Chehab } while (0)
509a0bf528SMauro Carvalho Chehab 
519a0bf528SMauro Carvalho Chehab /* don't write more than one byte with flexcop behind */
529a0bf528SMauro Carvalho Chehab static int itd1000_write_regs(struct itd1000_state *state, u8 reg, u8 v[], u8 len)
539a0bf528SMauro Carvalho Chehab {
548393796dSMauro Carvalho Chehab 	u8 buf[MAX_XFER_SIZE];
559a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {
569a0bf528SMauro Carvalho Chehab 		.addr = state->cfg->i2c_address, .flags = 0, .buf = buf, .len = len+1
579a0bf528SMauro Carvalho Chehab 	};
588393796dSMauro Carvalho Chehab 
598393796dSMauro Carvalho Chehab 	if (1 + len > sizeof(buf)) {
608393796dSMauro Carvalho Chehab 		printk(KERN_WARNING
618393796dSMauro Carvalho Chehab 		       "itd1000: i2c wr reg=%04x: len=%d is too big!\n",
628393796dSMauro Carvalho Chehab 		       reg, len);
638393796dSMauro Carvalho Chehab 		return -EINVAL;
648393796dSMauro Carvalho Chehab 	}
658393796dSMauro Carvalho Chehab 
669a0bf528SMauro Carvalho Chehab 	buf[0] = reg;
679a0bf528SMauro Carvalho Chehab 	memcpy(&buf[1], v, len);
689a0bf528SMauro Carvalho Chehab 
699a0bf528SMauro Carvalho Chehab 	/* itd_dbg("wr %02x: %02x\n", reg, v[0]); */
709a0bf528SMauro Carvalho Chehab 
719a0bf528SMauro Carvalho Chehab 	if (i2c_transfer(state->i2c, &msg, 1) != 1) {
729a0bf528SMauro Carvalho Chehab 		printk(KERN_WARNING "itd1000 I2C write failed\n");
739a0bf528SMauro Carvalho Chehab 		return -EREMOTEIO;
749a0bf528SMauro Carvalho Chehab 	}
759a0bf528SMauro Carvalho Chehab 	return 0;
769a0bf528SMauro Carvalho Chehab }
779a0bf528SMauro Carvalho Chehab 
789a0bf528SMauro Carvalho Chehab static int itd1000_read_reg(struct itd1000_state *state, u8 reg)
799a0bf528SMauro Carvalho Chehab {
809a0bf528SMauro Carvalho Chehab 	u8 val;
819a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[2] = {
829a0bf528SMauro Carvalho Chehab 		{ .addr = state->cfg->i2c_address, .flags = 0,        .buf = &reg, .len = 1 },
839a0bf528SMauro Carvalho Chehab 		{ .addr = state->cfg->i2c_address, .flags = I2C_M_RD, .buf = &val, .len = 1 },
849a0bf528SMauro Carvalho Chehab 	};
859a0bf528SMauro Carvalho Chehab 
869a0bf528SMauro Carvalho Chehab 	/* ugly flexcop workaround */
879a0bf528SMauro Carvalho Chehab 	itd1000_write_regs(state, (reg - 1) & 0xff, &state->shadow[(reg - 1) & 0xff], 1);
889a0bf528SMauro Carvalho Chehab 
899a0bf528SMauro Carvalho Chehab 	if (i2c_transfer(state->i2c, msg, 2) != 2) {
909a0bf528SMauro Carvalho Chehab 		itd_warn("itd1000 I2C read failed\n");
919a0bf528SMauro Carvalho Chehab 		return -EREMOTEIO;
929a0bf528SMauro Carvalho Chehab 	}
939a0bf528SMauro Carvalho Chehab 	return val;
949a0bf528SMauro Carvalho Chehab }
959a0bf528SMauro Carvalho Chehab 
969a0bf528SMauro Carvalho Chehab static inline int itd1000_write_reg(struct itd1000_state *state, u8 r, u8 v)
979a0bf528SMauro Carvalho Chehab {
983cd890dbSArnd Bergmann 	u8 tmp = v; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
993cd890dbSArnd Bergmann 	int ret = itd1000_write_regs(state, r, &tmp, 1);
1003cd890dbSArnd Bergmann 	state->shadow[r] = tmp;
1019a0bf528SMauro Carvalho Chehab 	return ret;
1029a0bf528SMauro Carvalho Chehab }
1039a0bf528SMauro Carvalho Chehab 
1049a0bf528SMauro Carvalho Chehab 
1059a0bf528SMauro Carvalho Chehab static struct {
1069a0bf528SMauro Carvalho Chehab 	u32 symbol_rate;
1079a0bf528SMauro Carvalho Chehab 	u8  pgaext  : 4; /* PLLFH */
1089a0bf528SMauro Carvalho Chehab 	u8  bbgvmin : 4; /* BBGVMIN */
1099a0bf528SMauro Carvalho Chehab } itd1000_lpf_pga[] = {
1109a0bf528SMauro Carvalho Chehab 	{        0, 0x8, 0x3 },
1119a0bf528SMauro Carvalho Chehab 	{  5200000, 0x8, 0x3 },
1129a0bf528SMauro Carvalho Chehab 	{ 12200000, 0x4, 0x3 },
1139a0bf528SMauro Carvalho Chehab 	{ 15400000, 0x2, 0x3 },
1149a0bf528SMauro Carvalho Chehab 	{ 19800000, 0x2, 0x3 },
1159a0bf528SMauro Carvalho Chehab 	{ 21500000, 0x2, 0x3 },
1169a0bf528SMauro Carvalho Chehab 	{ 24500000, 0x2, 0x3 },
1179a0bf528SMauro Carvalho Chehab 	{ 28400000, 0x2, 0x3 },
1189a0bf528SMauro Carvalho Chehab 	{ 33400000, 0x2, 0x3 },
1199a0bf528SMauro Carvalho Chehab 	{ 34400000, 0x1, 0x4 },
1209a0bf528SMauro Carvalho Chehab 	{ 34400000, 0x1, 0x4 },
1219a0bf528SMauro Carvalho Chehab 	{ 38400000, 0x1, 0x4 },
1229a0bf528SMauro Carvalho Chehab 	{ 38400000, 0x1, 0x4 },
1239a0bf528SMauro Carvalho Chehab 	{ 40400000, 0x1, 0x4 },
1249a0bf528SMauro Carvalho Chehab 	{ 45400000, 0x1, 0x4 },
1259a0bf528SMauro Carvalho Chehab };
1269a0bf528SMauro Carvalho Chehab 
1279a0bf528SMauro Carvalho Chehab static void itd1000_set_lpf_bw(struct itd1000_state *state, u32 symbol_rate)
1289a0bf528SMauro Carvalho Chehab {
1299a0bf528SMauro Carvalho Chehab 	u8 i;
1309a0bf528SMauro Carvalho Chehab 	u8 con1    = itd1000_read_reg(state, CON1)    & 0xfd;
1319a0bf528SMauro Carvalho Chehab 	u8 pllfh   = itd1000_read_reg(state, PLLFH)   & 0x0f;
1329a0bf528SMauro Carvalho Chehab 	u8 bbgvmin = itd1000_read_reg(state, BBGVMIN) & 0xf0;
1339a0bf528SMauro Carvalho Chehab 	u8 bw      = itd1000_read_reg(state, BW)      & 0xf0;
1349a0bf528SMauro Carvalho Chehab 
1359a0bf528SMauro Carvalho Chehab 	itd_dbg("symbol_rate = %d\n", symbol_rate);
1369a0bf528SMauro Carvalho Chehab 
1379a0bf528SMauro Carvalho Chehab 	/* not sure what is that ? - starting to download the table */
1389a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, CON1, con1 | (1 << 1));
1399a0bf528SMauro Carvalho Chehab 
1409a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_lpf_pga); i++)
1419a0bf528SMauro Carvalho Chehab 		if (symbol_rate < itd1000_lpf_pga[i].symbol_rate) {
1429a0bf528SMauro Carvalho Chehab 			itd_dbg("symrate: index: %d pgaext: %x, bbgvmin: %x\n", i, itd1000_lpf_pga[i].pgaext, itd1000_lpf_pga[i].bbgvmin);
1439a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, PLLFH,   pllfh | (itd1000_lpf_pga[i].pgaext << 4));
1449a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, BBGVMIN, bbgvmin | (itd1000_lpf_pga[i].bbgvmin));
1459a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, BW,      bw | (i & 0x0f));
1469a0bf528SMauro Carvalho Chehab 			break;
1479a0bf528SMauro Carvalho Chehab 		}
1489a0bf528SMauro Carvalho Chehab 
1499a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, CON1, con1 | (0 << 1));
1509a0bf528SMauro Carvalho Chehab }
1519a0bf528SMauro Carvalho Chehab 
1529a0bf528SMauro Carvalho Chehab static struct {
1539a0bf528SMauro Carvalho Chehab 	u8 vcorg;
1549a0bf528SMauro Carvalho Chehab 	u32 fmax_rg;
1559a0bf528SMauro Carvalho Chehab } itd1000_vcorg[] = {
1569a0bf528SMauro Carvalho Chehab 	{  1,  920000 },
1579a0bf528SMauro Carvalho Chehab 	{  2,  971000 },
1589a0bf528SMauro Carvalho Chehab 	{  3, 1031000 },
1599a0bf528SMauro Carvalho Chehab 	{  4, 1091000 },
1609a0bf528SMauro Carvalho Chehab 	{  5, 1171000 },
1619a0bf528SMauro Carvalho Chehab 	{  6, 1281000 },
1629a0bf528SMauro Carvalho Chehab 	{  7, 1381000 },
1639a0bf528SMauro Carvalho Chehab 	{  8,  500000 },	/* this is intentional. */
1649a0bf528SMauro Carvalho Chehab 	{  9, 1451000 },
1659a0bf528SMauro Carvalho Chehab 	{ 10, 1531000 },
1669a0bf528SMauro Carvalho Chehab 	{ 11, 1631000 },
1679a0bf528SMauro Carvalho Chehab 	{ 12, 1741000 },
1689a0bf528SMauro Carvalho Chehab 	{ 13, 1891000 },
1699a0bf528SMauro Carvalho Chehab 	{ 14, 2071000 },
1709a0bf528SMauro Carvalho Chehab 	{ 15, 2250000 },
1719a0bf528SMauro Carvalho Chehab };
1729a0bf528SMauro Carvalho Chehab 
1739a0bf528SMauro Carvalho Chehab static void itd1000_set_vco(struct itd1000_state *state, u32 freq_khz)
1749a0bf528SMauro Carvalho Chehab {
1759a0bf528SMauro Carvalho Chehab 	u8 i;
1769a0bf528SMauro Carvalho Chehab 	u8 gvbb_i2c     = itd1000_read_reg(state, GVBB_I2C) & 0xbf;
1779a0bf528SMauro Carvalho Chehab 	u8 vco_chp1_i2c = itd1000_read_reg(state, VCO_CHP1_I2C) & 0x0f;
1789a0bf528SMauro Carvalho Chehab 	u8 adcout;
1799a0bf528SMauro Carvalho Chehab 
1809a0bf528SMauro Carvalho Chehab 	/* reserved bit again (reset ?) */
1819a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, GVBB_I2C, gvbb_i2c | (1 << 6));
1829a0bf528SMauro Carvalho Chehab 
1839a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_vcorg); i++) {
1849a0bf528SMauro Carvalho Chehab 		if (freq_khz < itd1000_vcorg[i].fmax_rg) {
1859a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | (itd1000_vcorg[i].vcorg << 4));
1869a0bf528SMauro Carvalho Chehab 			msleep(1);
1879a0bf528SMauro Carvalho Chehab 
1889a0bf528SMauro Carvalho Chehab 			adcout = itd1000_read_reg(state, PLLLOCK) & 0x0f;
1899a0bf528SMauro Carvalho Chehab 
1909a0bf528SMauro Carvalho Chehab 			itd_dbg("VCO: %dkHz: %d -> ADCOUT: %d %02x\n", freq_khz, itd1000_vcorg[i].vcorg, adcout, vco_chp1_i2c);
1919a0bf528SMauro Carvalho Chehab 
1929a0bf528SMauro Carvalho Chehab 			if (adcout > 13) {
1939a0bf528SMauro Carvalho Chehab 				if (!(itd1000_vcorg[i].vcorg == 7 || itd1000_vcorg[i].vcorg == 15))
1949a0bf528SMauro Carvalho Chehab 					itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg + 1) << 4));
1959a0bf528SMauro Carvalho Chehab 			} else if (adcout < 2) {
1969a0bf528SMauro Carvalho Chehab 				if (!(itd1000_vcorg[i].vcorg == 1 || itd1000_vcorg[i].vcorg == 9))
1979a0bf528SMauro Carvalho Chehab 					itd1000_write_reg(state, VCO_CHP1_I2C, vco_chp1_i2c | ((itd1000_vcorg[i].vcorg - 1) << 4));
1989a0bf528SMauro Carvalho Chehab 			}
1999a0bf528SMauro Carvalho Chehab 			break;
2009a0bf528SMauro Carvalho Chehab 		}
2019a0bf528SMauro Carvalho Chehab 	}
2029a0bf528SMauro Carvalho Chehab }
2039a0bf528SMauro Carvalho Chehab 
2049a0bf528SMauro Carvalho Chehab static const struct {
2059a0bf528SMauro Carvalho Chehab 	u32 freq;
2069a0bf528SMauro Carvalho Chehab 	u8 values[10]; /* RFTR, RFST1 - RFST9 */
2079a0bf528SMauro Carvalho Chehab } itd1000_fre_values[] = {
2089a0bf528SMauro Carvalho Chehab 	{ 1075000, { 0x59, 0x1d, 0x1c, 0x17, 0x16, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2099a0bf528SMauro Carvalho Chehab 	{ 1250000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2109a0bf528SMauro Carvalho Chehab 	{ 1450000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2119a0bf528SMauro Carvalho Chehab 	{ 1650000, { 0x69, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2129a0bf528SMauro Carvalho Chehab 	{ 1750000, { 0x69, 0x1e, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
2139a0bf528SMauro Carvalho Chehab 	{ 1850000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
2149a0bf528SMauro Carvalho Chehab 	{ 1900000, { 0x69, 0x1d, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
2159a0bf528SMauro Carvalho Chehab 	{ 1950000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0d, 0x0b, 0x0a } },
2169a0bf528SMauro Carvalho Chehab 	{ 2050000, { 0x69, 0x1e, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0b, 0x0a } },
2179a0bf528SMauro Carvalho Chehab 	{ 2150000, { 0x69, 0x1d, 0x1c, 0x17, 0x15, 0x14, 0x13, 0x0f, 0x0e, 0x0b } }
2189a0bf528SMauro Carvalho Chehab };
2199a0bf528SMauro Carvalho Chehab 
2209a0bf528SMauro Carvalho Chehab 
2219a0bf528SMauro Carvalho Chehab #define FREF 16
2229a0bf528SMauro Carvalho Chehab 
2239a0bf528SMauro Carvalho Chehab static void itd1000_set_lo(struct itd1000_state *state, u32 freq_khz)
2249a0bf528SMauro Carvalho Chehab {
2259a0bf528SMauro Carvalho Chehab 	int i, j;
2269a0bf528SMauro Carvalho Chehab 	u32 plln, pllf;
2279a0bf528SMauro Carvalho Chehab 	u64 tmp;
2289a0bf528SMauro Carvalho Chehab 
2299a0bf528SMauro Carvalho Chehab 	plln = (freq_khz * 1000) / 2 / FREF;
2309a0bf528SMauro Carvalho Chehab 
2319a0bf528SMauro Carvalho Chehab 	/* Compute the factional part times 1000 */
2329a0bf528SMauro Carvalho Chehab 	tmp  = plln % 1000000;
2339a0bf528SMauro Carvalho Chehab 	plln /= 1000000;
2349a0bf528SMauro Carvalho Chehab 
2359a0bf528SMauro Carvalho Chehab 	tmp *= 1048576;
2369a0bf528SMauro Carvalho Chehab 	do_div(tmp, 1000000);
2379a0bf528SMauro Carvalho Chehab 	pllf = (u32) tmp;
2389a0bf528SMauro Carvalho Chehab 
2399a0bf528SMauro Carvalho Chehab 	state->frequency = ((plln * 1000) + (pllf * 1000)/1048576) * 2*FREF;
2409a0bf528SMauro Carvalho Chehab 	itd_dbg("frequency: %dkHz (wanted) %dkHz (set), PLLF = %d, PLLN = %d\n", freq_khz, state->frequency, pllf, plln);
2419a0bf528SMauro Carvalho Chehab 
2421c4bbfd1SPeter Senna Tschudin 	itd1000_write_reg(state, PLLNH, 0x80); /* PLLNH */
2439a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLNL, plln & 0xff);
2449a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLFH, (itd1000_read_reg(state, PLLFH) & 0xf0) | ((pllf >> 16) & 0x0f));
2459a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLFM, (pllf >> 8) & 0xff);
2469a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLFL, (pllf >> 0) & 0xff);
2479a0bf528SMauro Carvalho Chehab 
2489a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_fre_values); i++) {
2499a0bf528SMauro Carvalho Chehab 		if (freq_khz <= itd1000_fre_values[i].freq) {
2509a0bf528SMauro Carvalho Chehab 			itd_dbg("fre_values: %d\n", i);
2519a0bf528SMauro Carvalho Chehab 			itd1000_write_reg(state, RFTR, itd1000_fre_values[i].values[0]);
2529a0bf528SMauro Carvalho Chehab 			for (j = 0; j < 9; j++)
2539a0bf528SMauro Carvalho Chehab 				itd1000_write_reg(state, RFST1+j, itd1000_fre_values[i].values[j+1]);
2549a0bf528SMauro Carvalho Chehab 			break;
2559a0bf528SMauro Carvalho Chehab 		}
2569a0bf528SMauro Carvalho Chehab 	}
2579a0bf528SMauro Carvalho Chehab 
2589a0bf528SMauro Carvalho Chehab 	itd1000_set_vco(state, freq_khz);
2599a0bf528SMauro Carvalho Chehab }
2609a0bf528SMauro Carvalho Chehab 
2619a0bf528SMauro Carvalho Chehab static int itd1000_set_parameters(struct dvb_frontend *fe)
2629a0bf528SMauro Carvalho Chehab {
2639a0bf528SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2649a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = fe->tuner_priv;
2659a0bf528SMauro Carvalho Chehab 	u8 pllcon1;
2669a0bf528SMauro Carvalho Chehab 
2679a0bf528SMauro Carvalho Chehab 	itd1000_set_lo(state, c->frequency);
2689a0bf528SMauro Carvalho Chehab 	itd1000_set_lpf_bw(state, c->symbol_rate);
2699a0bf528SMauro Carvalho Chehab 
2709a0bf528SMauro Carvalho Chehab 	pllcon1 = itd1000_read_reg(state, PLLCON1) & 0x7f;
2719a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLCON1, pllcon1 | (1 << 7));
2729a0bf528SMauro Carvalho Chehab 	itd1000_write_reg(state, PLLCON1, pllcon1);
2739a0bf528SMauro Carvalho Chehab 
2749a0bf528SMauro Carvalho Chehab 	return 0;
2759a0bf528SMauro Carvalho Chehab }
2769a0bf528SMauro Carvalho Chehab 
2779a0bf528SMauro Carvalho Chehab static int itd1000_get_frequency(struct dvb_frontend *fe, u32 *frequency)
2789a0bf528SMauro Carvalho Chehab {
2799a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = fe->tuner_priv;
2809a0bf528SMauro Carvalho Chehab 	*frequency = state->frequency;
2819a0bf528SMauro Carvalho Chehab 	return 0;
2829a0bf528SMauro Carvalho Chehab }
2839a0bf528SMauro Carvalho Chehab 
2849a0bf528SMauro Carvalho Chehab static int itd1000_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
2859a0bf528SMauro Carvalho Chehab {
2869a0bf528SMauro Carvalho Chehab 	return 0;
2879a0bf528SMauro Carvalho Chehab }
2889a0bf528SMauro Carvalho Chehab 
2899a0bf528SMauro Carvalho Chehab static u8 itd1000_init_tab[][2] = {
2909a0bf528SMauro Carvalho Chehab 	{ PLLCON1,       0x65 }, /* Register does not change */
2919a0bf528SMauro Carvalho Chehab 	{ PLLNH,         0x80 }, /* Bits [7:6] do not change */
2929a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X6D, 0x3b },
2939a0bf528SMauro Carvalho Chehab 	{ VCO_CHP2_I2C,  0x12 },
2949a0bf528SMauro Carvalho Chehab 	{ 0x72,          0xf9 }, /* No such regsister defined */
2959a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X73, 0xff },
2969a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X74, 0xb2 },
2979a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X75, 0xc7 },
2989a0bf528SMauro Carvalho Chehab 	{ EXTGVBBRF,     0xf0 },
2999a0bf528SMauro Carvalho Chehab 	{ DIVAGCCK,      0x80 },
3009a0bf528SMauro Carvalho Chehab 	{ BBTR,          0xa0 },
3019a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X7E, 0x4f },
3029a0bf528SMauro Carvalho Chehab 	{ 0x82,          0x88 }, /* No such regsister defined */
3039a0bf528SMauro Carvalho Chehab 	{ 0x83,          0x80 }, /* No such regsister defined */
3049a0bf528SMauro Carvalho Chehab 	{ 0x84,          0x80 }, /* No such regsister defined */
3059a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X85, 0x74 },
3069a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X86, 0xff },
3079a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X88, 0x02 },
3089a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X89, 0x16 },
3099a0bf528SMauro Carvalho Chehab 	{ RFST0,         0x1f },
3109a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X94, 0x66 },
3119a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X95, 0x66 },
3129a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X96, 0x77 },
3139a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X97, 0x99 },
3149a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X98, 0xff },
3159a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X99, 0xfc },
3169a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X9A, 0xba },
3179a0bf528SMauro Carvalho Chehab 	{ RESERVED_0X9B, 0xaa },
3189a0bf528SMauro Carvalho Chehab };
3199a0bf528SMauro Carvalho Chehab 
3209a0bf528SMauro Carvalho Chehab static u8 itd1000_reinit_tab[][2] = {
3219a0bf528SMauro Carvalho Chehab 	{ VCO_CHP1_I2C, 0x8a },
3229a0bf528SMauro Carvalho Chehab 	{ BW,           0x87 },
3239a0bf528SMauro Carvalho Chehab 	{ GVBB_I2C,     0x03 },
3249a0bf528SMauro Carvalho Chehab 	{ BBGVMIN,      0x03 },
3259a0bf528SMauro Carvalho Chehab 	{ CON1,         0x2e },
3269a0bf528SMauro Carvalho Chehab };
3279a0bf528SMauro Carvalho Chehab 
3289a0bf528SMauro Carvalho Chehab 
3299a0bf528SMauro Carvalho Chehab static int itd1000_init(struct dvb_frontend *fe)
3309a0bf528SMauro Carvalho Chehab {
3319a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = fe->tuner_priv;
3329a0bf528SMauro Carvalho Chehab 	int i;
3339a0bf528SMauro Carvalho Chehab 
3349a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_init_tab); i++)
3359a0bf528SMauro Carvalho Chehab 		itd1000_write_reg(state, itd1000_init_tab[i][0], itd1000_init_tab[i][1]);
3369a0bf528SMauro Carvalho Chehab 
3379a0bf528SMauro Carvalho Chehab 	for (i = 0; i < ARRAY_SIZE(itd1000_reinit_tab); i++)
3389a0bf528SMauro Carvalho Chehab 		itd1000_write_reg(state, itd1000_reinit_tab[i][0], itd1000_reinit_tab[i][1]);
3399a0bf528SMauro Carvalho Chehab 
3409a0bf528SMauro Carvalho Chehab 	return 0;
3419a0bf528SMauro Carvalho Chehab }
3429a0bf528SMauro Carvalho Chehab 
3439a0bf528SMauro Carvalho Chehab static int itd1000_sleep(struct dvb_frontend *fe)
3449a0bf528SMauro Carvalho Chehab {
3459a0bf528SMauro Carvalho Chehab 	return 0;
3469a0bf528SMauro Carvalho Chehab }
3479a0bf528SMauro Carvalho Chehab 
348f2709c20SMauro Carvalho Chehab static void itd1000_release(struct dvb_frontend *fe)
349f2709c20SMauro Carvalho Chehab {
350f2709c20SMauro Carvalho Chehab 	kfree(fe->tuner_priv);
351f2709c20SMauro Carvalho Chehab 	fe->tuner_priv = NULL;
352f2709c20SMauro Carvalho Chehab }
353f2709c20SMauro Carvalho Chehab 
3549a0bf528SMauro Carvalho Chehab static const struct dvb_tuner_ops itd1000_tuner_ops = {
3559a0bf528SMauro Carvalho Chehab 	.info = {
3569a0bf528SMauro Carvalho Chehab 		.name              = "Integrant ITD1000",
357a3f90c75SMauro Carvalho Chehab 		.frequency_min_hz  =  950 * MHz,
358a3f90c75SMauro Carvalho Chehab 		.frequency_max_hz  = 2150 * MHz,
359a3f90c75SMauro Carvalho Chehab 		.frequency_step_hz =  125 * kHz,
3609a0bf528SMauro Carvalho Chehab 	},
3619a0bf528SMauro Carvalho Chehab 
362f2709c20SMauro Carvalho Chehab 	.release       = itd1000_release,
3639a0bf528SMauro Carvalho Chehab 
3649a0bf528SMauro Carvalho Chehab 	.init          = itd1000_init,
3659a0bf528SMauro Carvalho Chehab 	.sleep         = itd1000_sleep,
3669a0bf528SMauro Carvalho Chehab 
3679a0bf528SMauro Carvalho Chehab 	.set_params    = itd1000_set_parameters,
3689a0bf528SMauro Carvalho Chehab 	.get_frequency = itd1000_get_frequency,
3699a0bf528SMauro Carvalho Chehab 	.get_bandwidth = itd1000_get_bandwidth
3709a0bf528SMauro Carvalho Chehab };
3719a0bf528SMauro Carvalho Chehab 
3729a0bf528SMauro Carvalho Chehab 
3739a0bf528SMauro Carvalho Chehab struct dvb_frontend *itd1000_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct itd1000_config *cfg)
3749a0bf528SMauro Carvalho Chehab {
3759a0bf528SMauro Carvalho Chehab 	struct itd1000_state *state = NULL;
3769a0bf528SMauro Carvalho Chehab 	u8 i = 0;
3779a0bf528SMauro Carvalho Chehab 
3789a0bf528SMauro Carvalho Chehab 	state = kzalloc(sizeof(struct itd1000_state), GFP_KERNEL);
3799a0bf528SMauro Carvalho Chehab 	if (state == NULL)
3809a0bf528SMauro Carvalho Chehab 		return NULL;
3819a0bf528SMauro Carvalho Chehab 
3829a0bf528SMauro Carvalho Chehab 	state->cfg = cfg;
3839a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
3849a0bf528SMauro Carvalho Chehab 
3859a0bf528SMauro Carvalho Chehab 	i = itd1000_read_reg(state, 0);
3869a0bf528SMauro Carvalho Chehab 	if (i != 0) {
3879a0bf528SMauro Carvalho Chehab 		kfree(state);
3889a0bf528SMauro Carvalho Chehab 		return NULL;
3899a0bf528SMauro Carvalho Chehab 	}
3909a0bf528SMauro Carvalho Chehab 	itd_info("successfully identified (ID: %d)\n", i);
3919a0bf528SMauro Carvalho Chehab 
3929a0bf528SMauro Carvalho Chehab 	memset(state->shadow, 0xff, sizeof(state->shadow));
3939a0bf528SMauro Carvalho Chehab 	for (i = 0x65; i < 0x9c; i++)
3949a0bf528SMauro Carvalho Chehab 		state->shadow[i] = itd1000_read_reg(state, i);
3959a0bf528SMauro Carvalho Chehab 
3969a0bf528SMauro Carvalho Chehab 	memcpy(&fe->ops.tuner_ops, &itd1000_tuner_ops, sizeof(struct dvb_tuner_ops));
3979a0bf528SMauro Carvalho Chehab 
3989a0bf528SMauro Carvalho Chehab 	fe->tuner_priv = state;
3999a0bf528SMauro Carvalho Chehab 
4009a0bf528SMauro Carvalho Chehab 	return fe;
4019a0bf528SMauro Carvalho Chehab }
4029a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(itd1000_attach);
4039a0bf528SMauro Carvalho Chehab 
4049a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
4059a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("Integrant ITD1000 driver");
4069a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
407