19a0bf528SMauro Carvalho Chehab /*
29a0bf528SMauro Carvalho Chehab  *   Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
39a0bf528SMauro Carvalho Chehab  *
4a77cfcacSMauro Carvalho Chehab  *   Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
59a0bf528SMauro Carvalho Chehab  *   Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
69a0bf528SMauro Carvalho Chehab  *
79a0bf528SMauro Carvalho Chehab  *   This program is free software; you can redistribute it and/or
89a0bf528SMauro Carvalho Chehab  *   modify it under the terms of the GNU General Public License as
99a0bf528SMauro Carvalho Chehab  *   published by the Free Software Foundation version 2.
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 GNU
149a0bf528SMauro Carvalho Chehab  *   General Public License for more details.
159a0bf528SMauro Carvalho Chehab  */
169a0bf528SMauro Carvalho Chehab 
179a0bf528SMauro Carvalho Chehab #include <linux/kernel.h>
189a0bf528SMauro Carvalho Chehab #include <asm/div64.h>
199a0bf528SMauro Carvalho Chehab 
209a0bf528SMauro Carvalho Chehab #include "dvb_frontend.h"
219a0bf528SMauro Carvalho Chehab #include "mb86a20s.h"
229a0bf528SMauro Carvalho Chehab 
239a0bf528SMauro Carvalho Chehab static int debug = 1;
249a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644);
259a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
269a0bf528SMauro Carvalho Chehab 
279a0bf528SMauro Carvalho Chehab #define rc(args...)  do {						\
289a0bf528SMauro Carvalho Chehab 	printk(KERN_ERR  "mb86a20s: " args);				\
299a0bf528SMauro Carvalho Chehab } while (0)
309a0bf528SMauro Carvalho Chehab 
319a0bf528SMauro Carvalho Chehab #define dprintk(args...)						\
329a0bf528SMauro Carvalho Chehab 	do {								\
339a0bf528SMauro Carvalho Chehab 		if (debug) {						\
349a0bf528SMauro Carvalho Chehab 			printk(KERN_DEBUG "mb86a20s: %s: ", __func__);	\
359a0bf528SMauro Carvalho Chehab 			printk(args);					\
369a0bf528SMauro Carvalho Chehab 		}							\
379a0bf528SMauro Carvalho Chehab 	} while (0)
389a0bf528SMauro Carvalho Chehab 
399a0bf528SMauro Carvalho Chehab struct mb86a20s_state {
409a0bf528SMauro Carvalho Chehab 	struct i2c_adapter *i2c;
419a0bf528SMauro Carvalho Chehab 	const struct mb86a20s_config *config;
429a0bf528SMauro Carvalho Chehab 
439a0bf528SMauro Carvalho Chehab 	struct dvb_frontend frontend;
449a0bf528SMauro Carvalho Chehab 
459a0bf528SMauro Carvalho Chehab 	bool need_init;
469a0bf528SMauro Carvalho Chehab };
479a0bf528SMauro Carvalho Chehab 
489a0bf528SMauro Carvalho Chehab struct regdata {
499a0bf528SMauro Carvalho Chehab 	u8 reg;
509a0bf528SMauro Carvalho Chehab 	u8 data;
519a0bf528SMauro Carvalho Chehab };
529a0bf528SMauro Carvalho Chehab 
539a0bf528SMauro Carvalho Chehab /*
549a0bf528SMauro Carvalho Chehab  * Initialization sequence: Use whatevere default values that PV SBTVD
559a0bf528SMauro Carvalho Chehab  * does on its initialisation, obtained via USB snoop
569a0bf528SMauro Carvalho Chehab  */
579a0bf528SMauro Carvalho Chehab static struct regdata mb86a20s_init[] = {
589a0bf528SMauro Carvalho Chehab 	{ 0x70, 0x0f },
599a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
609a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
619a0bf528SMauro Carvalho Chehab 	{ 0x09, 0x3e },
629a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd1 }, { 0x51, 0x22 },
639a0bf528SMauro Carvalho Chehab 	{ 0x39, 0x01 },
649a0bf528SMauro Carvalho Chehab 	{ 0x71, 0x00 },
659a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
669a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
679a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
689a0bf528SMauro Carvalho Chehab 	{ 0x3b, 0x21 },
699a0bf528SMauro Carvalho Chehab 	{ 0x3c, 0x3a },
709a0bf528SMauro Carvalho Chehab 	{ 0x01, 0x0d },
719a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x08 }, { 0x05, 0x05 },
729a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0e }, { 0x05, 0x00 },
739a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0f }, { 0x05, 0x14 },
749a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0b }, { 0x05, 0x8c },
759a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x00 }, { 0x05, 0x00 },
769a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x01 }, { 0x05, 0x07 },
779a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x02 }, { 0x05, 0x0f },
789a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x03 }, { 0x05, 0xa0 },
799a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x09 }, { 0x05, 0x00 },
809a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0a }, { 0x05, 0xff },
819a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x27 }, { 0x05, 0x64 },
829a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x28 }, { 0x05, 0x00 },
839a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x1e }, { 0x05, 0xff },
849a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x29 }, { 0x05, 0x0a },
859a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x32 }, { 0x05, 0x0a },
869a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x14 }, { 0x05, 0x02 },
879a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x04 }, { 0x05, 0x00 },
889a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x05 }, { 0x05, 0x22 },
899a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x06 }, { 0x05, 0x0e },
909a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x07 }, { 0x05, 0xd8 },
919a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x12 }, { 0x05, 0x00 },
929a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x13 }, { 0x05, 0xff },
939a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x15 }, { 0x05, 0x4e },
949a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x16 }, { 0x05, 0x20 },
959a0bf528SMauro Carvalho Chehab 	{ 0x52, 0x01 },
969a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa7 }, { 0x51, 0xff },
979a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa8 }, { 0x51, 0xff },
989a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa9 }, { 0x51, 0xff },
999a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xaa }, { 0x51, 0xff },
1009a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xab }, { 0x51, 0xff },
1019a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xac }, { 0x51, 0xff },
1029a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xad }, { 0x51, 0xff },
1039a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xae }, { 0x51, 0xff },
1049a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xaf }, { 0x51, 0xff },
1059a0bf528SMauro Carvalho Chehab 	{ 0x5e, 0x07 },
1069a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xdc }, { 0x51, 0x01 },
1079a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xdd }, { 0x51, 0xf4 },
1089a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xde }, { 0x51, 0x01 },
1099a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xdf }, { 0x51, 0xf4 },
1109a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xe0 }, { 0x51, 0x01 },
1119a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xe1 }, { 0x51, 0xf4 },
1129a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb0 }, { 0x51, 0x07 },
1139a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb2 }, { 0x51, 0xff },
1149a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb3 }, { 0x51, 0xff },
1159a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb4 }, { 0x51, 0xff },
1169a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb5 }, { 0x51, 0xff },
1179a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb6 }, { 0x51, 0xff },
1189a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb7 }, { 0x51, 0xff },
1199a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x50 }, { 0x51, 0x02 },
1209a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x51 }, { 0x51, 0x04 },
1219a0bf528SMauro Carvalho Chehab 	{ 0x45, 0x04 },
1229a0bf528SMauro Carvalho Chehab 	{ 0x48, 0x04 },
1239a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd5 }, { 0x51, 0x01 },		/* Serial */
1249a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd6 }, { 0x51, 0x1f },
1259a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd2 }, { 0x51, 0x03 },
1269a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd7 }, { 0x51, 0x3f },
1279a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
1289a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
129ce77d120SMauro Carvalho Chehab 
130ce77d120SMauro Carvalho Chehab 	{ 0x04, 0x40 }, { 0x05, 0x00 },
1319a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x00 }, { 0x29, 0x10 },
1329a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x05 }, { 0x29, 0x02 },
1339a0bf528SMauro Carvalho Chehab 	{ 0x1c, 0x01 },
1349a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
1359a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
1369a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
1379a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
1389a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
1399a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
1409a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
1419a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
1429a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
1439a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
1449a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
1459a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
1469a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
1479a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
1489a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
1499a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
1509a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
1519a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
1529a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
1539a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
1549a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
1559a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
1569a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
1579a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
1589a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
1599a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x1e }, { 0x51, 0x5d },
1609a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x22 }, { 0x51, 0x00 },
1619a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x23 }, { 0x51, 0xc8 },
1629a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x24 }, { 0x51, 0x00 },
1639a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x25 }, { 0x51, 0xf0 },
1649a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x26 }, { 0x51, 0x00 },
1659a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x27 }, { 0x51, 0xc3 },
1669a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x39 }, { 0x51, 0x02 },
1679a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
1689a0bf528SMauro Carvalho Chehab 	{ 0xd0, 0x00 },
1699a0bf528SMauro Carvalho Chehab };
1709a0bf528SMauro Carvalho Chehab 
1719a0bf528SMauro Carvalho Chehab static struct regdata mb86a20s_reset_reception[] = {
1729a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xf0 },
1739a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
1749a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
1759a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x00 },
1769a0bf528SMauro Carvalho Chehab };
1779a0bf528SMauro Carvalho Chehab 
1789a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
1799a0bf528SMauro Carvalho Chehab 			     u8 i2c_addr, int reg, int data)
1809a0bf528SMauro Carvalho Chehab {
1819a0bf528SMauro Carvalho Chehab 	u8 buf[] = { reg, data };
1829a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {
1839a0bf528SMauro Carvalho Chehab 		.addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
1849a0bf528SMauro Carvalho Chehab 	};
1859a0bf528SMauro Carvalho Chehab 	int rc;
1869a0bf528SMauro Carvalho Chehab 
1879a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, &msg, 1);
1889a0bf528SMauro Carvalho Chehab 	if (rc != 1) {
1899a0bf528SMauro Carvalho Chehab 		printk("%s: writereg error (rc == %i, reg == 0x%02x,"
1909a0bf528SMauro Carvalho Chehab 			 " data == 0x%02x)\n", __func__, rc, reg, data);
1919a0bf528SMauro Carvalho Chehab 		return rc;
1929a0bf528SMauro Carvalho Chehab 	}
1939a0bf528SMauro Carvalho Chehab 
1949a0bf528SMauro Carvalho Chehab 	return 0;
1959a0bf528SMauro Carvalho Chehab }
1969a0bf528SMauro Carvalho Chehab 
1979a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
1989a0bf528SMauro Carvalho Chehab 				     u8 i2c_addr, struct regdata *rd, int size)
1999a0bf528SMauro Carvalho Chehab {
2009a0bf528SMauro Carvalho Chehab 	int i, rc;
2019a0bf528SMauro Carvalho Chehab 
2029a0bf528SMauro Carvalho Chehab 	for (i = 0; i < size; i++) {
2039a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
2049a0bf528SMauro Carvalho Chehab 					   rd[i].data);
2059a0bf528SMauro Carvalho Chehab 		if (rc < 0)
2069a0bf528SMauro Carvalho Chehab 			return rc;
2079a0bf528SMauro Carvalho Chehab 	}
2089a0bf528SMauro Carvalho Chehab 	return 0;
2099a0bf528SMauro Carvalho Chehab }
2109a0bf528SMauro Carvalho Chehab 
2119a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
2129a0bf528SMauro Carvalho Chehab 				u8 i2c_addr, u8 reg)
2139a0bf528SMauro Carvalho Chehab {
2149a0bf528SMauro Carvalho Chehab 	u8 val;
2159a0bf528SMauro Carvalho Chehab 	int rc;
2169a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[] = {
2179a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
2189a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
2199a0bf528SMauro Carvalho Chehab 	};
2209a0bf528SMauro Carvalho Chehab 
2219a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, msg, 2);
2229a0bf528SMauro Carvalho Chehab 
2239a0bf528SMauro Carvalho Chehab 	if (rc != 2) {
2249a0bf528SMauro Carvalho Chehab 		rc("%s: reg=0x%x (error=%d)\n", __func__, reg, rc);
2259a0bf528SMauro Carvalho Chehab 		return rc;
2269a0bf528SMauro Carvalho Chehab 	}
2279a0bf528SMauro Carvalho Chehab 
2289a0bf528SMauro Carvalho Chehab 	return val;
2299a0bf528SMauro Carvalho Chehab }
2309a0bf528SMauro Carvalho Chehab 
2319a0bf528SMauro Carvalho Chehab #define mb86a20s_readreg(state, reg) \
2329a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
2339a0bf528SMauro Carvalho Chehab #define mb86a20s_writereg(state, reg, val) \
2349a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
2359a0bf528SMauro Carvalho Chehab #define mb86a20s_writeregdata(state, regdata) \
2369a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
2379a0bf528SMauro Carvalho Chehab 	regdata, ARRAY_SIZE(regdata))
2389a0bf528SMauro Carvalho Chehab 
2399a0bf528SMauro Carvalho Chehab static int mb86a20s_initfe(struct dvb_frontend *fe)
2409a0bf528SMauro Carvalho Chehab {
2419a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
2429a0bf528SMauro Carvalho Chehab 	int rc;
2439a0bf528SMauro Carvalho Chehab 	u8  regD5 = 1;
2449a0bf528SMauro Carvalho Chehab 
2459a0bf528SMauro Carvalho Chehab 	dprintk("\n");
2469a0bf528SMauro Carvalho Chehab 
2479a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2489a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
2499a0bf528SMauro Carvalho Chehab 
2509a0bf528SMauro Carvalho Chehab 	/* Initialize the frontend */
2519a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_init);
2529a0bf528SMauro Carvalho Chehab 	if (rc < 0)
2539a0bf528SMauro Carvalho Chehab 		goto err;
2549a0bf528SMauro Carvalho Chehab 
2559a0bf528SMauro Carvalho Chehab 	if (!state->config->is_serial) {
2569a0bf528SMauro Carvalho Chehab 		regD5 &= ~1;
2579a0bf528SMauro Carvalho Chehab 
2589a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xd5);
2599a0bf528SMauro Carvalho Chehab 		if (rc < 0)
2609a0bf528SMauro Carvalho Chehab 			goto err;
2619a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, regD5);
2629a0bf528SMauro Carvalho Chehab 		if (rc < 0)
2639a0bf528SMauro Carvalho Chehab 			goto err;
2649a0bf528SMauro Carvalho Chehab 	}
2659a0bf528SMauro Carvalho Chehab 
266fd53744eSMauro Carvalho Chehab err:
2679a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2689a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
2699a0bf528SMauro Carvalho Chehab 
2709a0bf528SMauro Carvalho Chehab 	if (rc < 0) {
2719a0bf528SMauro Carvalho Chehab 		state->need_init = true;
2729a0bf528SMauro Carvalho Chehab 		printk(KERN_INFO "mb86a20s: Init failed. Will try again later\n");
2739a0bf528SMauro Carvalho Chehab 	} else {
2749a0bf528SMauro Carvalho Chehab 		state->need_init = false;
2759a0bf528SMauro Carvalho Chehab 		dprintk("Initialization succeeded.\n");
2769a0bf528SMauro Carvalho Chehab 	}
2779a0bf528SMauro Carvalho Chehab 	return rc;
2789a0bf528SMauro Carvalho Chehab }
2799a0bf528SMauro Carvalho Chehab 
2809a0bf528SMauro Carvalho Chehab static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
2819a0bf528SMauro Carvalho Chehab {
2829a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
2839a0bf528SMauro Carvalho Chehab 	unsigned rf_max, rf_min, rf;
2849a0bf528SMauro Carvalho Chehab 	u8	 val;
2859a0bf528SMauro Carvalho Chehab 
2869a0bf528SMauro Carvalho Chehab 	dprintk("\n");
2879a0bf528SMauro Carvalho Chehab 
2889a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2899a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
2909a0bf528SMauro Carvalho Chehab 
2919a0bf528SMauro Carvalho Chehab 	/* Does a binary search to get RF strength */
2929a0bf528SMauro Carvalho Chehab 	rf_max = 0xfff;
2939a0bf528SMauro Carvalho Chehab 	rf_min = 0;
2949a0bf528SMauro Carvalho Chehab 	do {
2959a0bf528SMauro Carvalho Chehab 		rf = (rf_max + rf_min) / 2;
2969a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x04, 0x1f);
2979a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x05, rf >> 8);
2989a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x04, 0x20);
2999a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x04, rf);
3009a0bf528SMauro Carvalho Chehab 
3019a0bf528SMauro Carvalho Chehab 		val = mb86a20s_readreg(state, 0x02);
3029a0bf528SMauro Carvalho Chehab 		if (val & 0x08)
3039a0bf528SMauro Carvalho Chehab 			rf_min = (rf_max + rf_min) / 2;
3049a0bf528SMauro Carvalho Chehab 		else
3059a0bf528SMauro Carvalho Chehab 			rf_max = (rf_max + rf_min) / 2;
3069a0bf528SMauro Carvalho Chehab 		if (rf_max - rf_min < 4) {
3079a0bf528SMauro Carvalho Chehab 			*strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
3089a0bf528SMauro Carvalho Chehab 			break;
3099a0bf528SMauro Carvalho Chehab 		}
3109a0bf528SMauro Carvalho Chehab 	} while (1);
3119a0bf528SMauro Carvalho Chehab 
3129a0bf528SMauro Carvalho Chehab 	dprintk("signal strength = %d\n", *strength);
3139a0bf528SMauro Carvalho Chehab 
3149a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
3159a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
3169a0bf528SMauro Carvalho Chehab 
3179a0bf528SMauro Carvalho Chehab 	return 0;
3189a0bf528SMauro Carvalho Chehab }
3199a0bf528SMauro Carvalho Chehab 
3209a0bf528SMauro Carvalho Chehab static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
3219a0bf528SMauro Carvalho Chehab {
3229a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
3239a0bf528SMauro Carvalho Chehab 	u8 val;
3249a0bf528SMauro Carvalho Chehab 
3259a0bf528SMauro Carvalho Chehab 	dprintk("\n");
3269a0bf528SMauro Carvalho Chehab 	*status = 0;
3279a0bf528SMauro Carvalho Chehab 
3289a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
3299a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
3309a0bf528SMauro Carvalho Chehab 	val = mb86a20s_readreg(state, 0x0a) & 0xf;
3319a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
3329a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
3339a0bf528SMauro Carvalho Chehab 
3349a0bf528SMauro Carvalho Chehab 	if (val >= 2)
3359a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_SIGNAL;
3369a0bf528SMauro Carvalho Chehab 
3379a0bf528SMauro Carvalho Chehab 	if (val >= 4)
3389a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_CARRIER;
3399a0bf528SMauro Carvalho Chehab 
3409a0bf528SMauro Carvalho Chehab 	if (val >= 5)
3419a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_VITERBI;
3429a0bf528SMauro Carvalho Chehab 
3439a0bf528SMauro Carvalho Chehab 	if (val >= 7)
3449a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_SYNC;
3459a0bf528SMauro Carvalho Chehab 
3469a0bf528SMauro Carvalho Chehab 	if (val >= 8)				/* Maybe 9? */
3479a0bf528SMauro Carvalho Chehab 		*status |= FE_HAS_LOCK;
3489a0bf528SMauro Carvalho Chehab 
3499a0bf528SMauro Carvalho Chehab 	dprintk("val = %d, status = 0x%02x\n", val, *status);
3509a0bf528SMauro Carvalho Chehab 
3519a0bf528SMauro Carvalho Chehab 	return 0;
3529a0bf528SMauro Carvalho Chehab }
3539a0bf528SMauro Carvalho Chehab 
3549a0bf528SMauro Carvalho Chehab static int mb86a20s_set_frontend(struct dvb_frontend *fe)
3559a0bf528SMauro Carvalho Chehab {
3569a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
3579a0bf528SMauro Carvalho Chehab 	int rc;
3589a0bf528SMauro Carvalho Chehab #if 0
3599a0bf528SMauro Carvalho Chehab 	/*
3609a0bf528SMauro Carvalho Chehab 	 * FIXME: Properly implement the set frontend properties
3619a0bf528SMauro Carvalho Chehab 	 */
362a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
3639a0bf528SMauro Carvalho Chehab #endif
3649a0bf528SMauro Carvalho Chehab 
3659a0bf528SMauro Carvalho Chehab 	dprintk("\n");
3669a0bf528SMauro Carvalho Chehab 
367fd53744eSMauro Carvalho Chehab 	/*
368fd53744eSMauro Carvalho Chehab 	 * Gate should already be opened, but it doesn't hurt to
369fd53744eSMauro Carvalho Chehab 	 * double-check
370fd53744eSMauro Carvalho Chehab 	 */
3719a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
3729a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
3739a0bf528SMauro Carvalho Chehab 	dprintk("Calling tuner set parameters\n");
3749a0bf528SMauro Carvalho Chehab 	fe->ops.tuner_ops.set_params(fe);
3759a0bf528SMauro Carvalho Chehab 
3769a0bf528SMauro Carvalho Chehab 	/*
3779a0bf528SMauro Carvalho Chehab 	 * Make it more reliable: if, for some reason, the initial
3789a0bf528SMauro Carvalho Chehab 	 * device initialization doesn't happen, initialize it when
3799a0bf528SMauro Carvalho Chehab 	 * a SBTVD parameters are adjusted.
3809a0bf528SMauro Carvalho Chehab 	 *
3819a0bf528SMauro Carvalho Chehab 	 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
3829a0bf528SMauro Carvalho Chehab 	 * the agc callback logic is not called during DVB attach time,
3839a0bf528SMauro Carvalho Chehab 	 * causing mb86a20s to not be initialized with Kworld SBTVD.
3849a0bf528SMauro Carvalho Chehab 	 * So, this hack is needed, in order to make Kworld SBTVD to work.
3859a0bf528SMauro Carvalho Chehab 	 */
3869a0bf528SMauro Carvalho Chehab 	if (state->need_init)
3879a0bf528SMauro Carvalho Chehab 		mb86a20s_initfe(fe);
3889a0bf528SMauro Carvalho Chehab 
3899a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
3909a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
3919a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
3929a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
3939a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
3949a0bf528SMauro Carvalho Chehab 
3959a0bf528SMauro Carvalho Chehab 	return rc;
3969a0bf528SMauro Carvalho Chehab }
3979a0bf528SMauro Carvalho Chehab 
3989a0bf528SMauro Carvalho Chehab static int mb86a20s_get_modulation(struct mb86a20s_state *state,
3999a0bf528SMauro Carvalho Chehab 				   unsigned layer)
4009a0bf528SMauro Carvalho Chehab {
4019a0bf528SMauro Carvalho Chehab 	int rc;
4029a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4039a0bf528SMauro Carvalho Chehab 		[0] = 0x86,	/* Layer A */
4049a0bf528SMauro Carvalho Chehab 		[1] = 0x8a,	/* Layer B */
4059a0bf528SMauro Carvalho Chehab 		[2] = 0x8e,	/* Layer C */
4069a0bf528SMauro Carvalho Chehab 	};
4079a0bf528SMauro Carvalho Chehab 
4089a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4099a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4109a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4119a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4129a0bf528SMauro Carvalho Chehab 		return rc;
4139a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4149a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4159a0bf528SMauro Carvalho Chehab 		return rc;
41604585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
4179a0bf528SMauro Carvalho Chehab 	case 0:
4189a0bf528SMauro Carvalho Chehab 		return DQPSK;
4199a0bf528SMauro Carvalho Chehab 	case 1:
4209a0bf528SMauro Carvalho Chehab 		return QPSK;
4219a0bf528SMauro Carvalho Chehab 	case 2:
4229a0bf528SMauro Carvalho Chehab 		return QAM_16;
4239a0bf528SMauro Carvalho Chehab 	case 3:
4249a0bf528SMauro Carvalho Chehab 		return QAM_64;
4259a0bf528SMauro Carvalho Chehab 	default:
4269a0bf528SMauro Carvalho Chehab 		return QAM_AUTO;
4279a0bf528SMauro Carvalho Chehab 	}
4289a0bf528SMauro Carvalho Chehab }
4299a0bf528SMauro Carvalho Chehab 
4309a0bf528SMauro Carvalho Chehab static int mb86a20s_get_fec(struct mb86a20s_state *state,
4319a0bf528SMauro Carvalho Chehab 			    unsigned layer)
4329a0bf528SMauro Carvalho Chehab {
4339a0bf528SMauro Carvalho Chehab 	int rc;
4349a0bf528SMauro Carvalho Chehab 
4359a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4369a0bf528SMauro Carvalho Chehab 		[0] = 0x87,	/* Layer A */
4379a0bf528SMauro Carvalho Chehab 		[1] = 0x8b,	/* Layer B */
4389a0bf528SMauro Carvalho Chehab 		[2] = 0x8f,	/* Layer C */
4399a0bf528SMauro Carvalho Chehab 	};
4409a0bf528SMauro Carvalho Chehab 
4419a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4429a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4439a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4449a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4459a0bf528SMauro Carvalho Chehab 		return rc;
4469a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4479a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4489a0bf528SMauro Carvalho Chehab 		return rc;
44904585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
4509a0bf528SMauro Carvalho Chehab 	case 0:
4519a0bf528SMauro Carvalho Chehab 		return FEC_1_2;
4529a0bf528SMauro Carvalho Chehab 	case 1:
4539a0bf528SMauro Carvalho Chehab 		return FEC_2_3;
4549a0bf528SMauro Carvalho Chehab 	case 2:
4559a0bf528SMauro Carvalho Chehab 		return FEC_3_4;
4569a0bf528SMauro Carvalho Chehab 	case 3:
4579a0bf528SMauro Carvalho Chehab 		return FEC_5_6;
4589a0bf528SMauro Carvalho Chehab 	case 4:
4599a0bf528SMauro Carvalho Chehab 		return FEC_7_8;
4609a0bf528SMauro Carvalho Chehab 	default:
4619a0bf528SMauro Carvalho Chehab 		return FEC_AUTO;
4629a0bf528SMauro Carvalho Chehab 	}
4639a0bf528SMauro Carvalho Chehab }
4649a0bf528SMauro Carvalho Chehab 
4659a0bf528SMauro Carvalho Chehab static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
4669a0bf528SMauro Carvalho Chehab 				     unsigned layer)
4679a0bf528SMauro Carvalho Chehab {
4689a0bf528SMauro Carvalho Chehab 	int rc;
4699a0bf528SMauro Carvalho Chehab 
4709a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4719a0bf528SMauro Carvalho Chehab 		[0] = 0x88,	/* Layer A */
4729a0bf528SMauro Carvalho Chehab 		[1] = 0x8c,	/* Layer B */
4739a0bf528SMauro Carvalho Chehab 		[2] = 0x90,	/* Layer C */
4749a0bf528SMauro Carvalho Chehab 	};
4759a0bf528SMauro Carvalho Chehab 
4769a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4779a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4789a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4799a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4809a0bf528SMauro Carvalho Chehab 		return rc;
4819a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4829a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4839a0bf528SMauro Carvalho Chehab 		return rc;
48404585921SMauro Carvalho Chehab 
48504585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
48604585921SMauro Carvalho Chehab 	case 1:
48704585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_4;
48804585921SMauro Carvalho Chehab 	case 2:
48904585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_8;
49004585921SMauro Carvalho Chehab 	case 3:
49104585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_16;
49204585921SMauro Carvalho Chehab 	case 4:
49304585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_32;
49404585921SMauro Carvalho Chehab 
49504585921SMauro Carvalho Chehab 	default:
49604585921SMauro Carvalho Chehab 	case 0:
49704585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_AUTO;
49804585921SMauro Carvalho Chehab 	}
4999a0bf528SMauro Carvalho Chehab }
5009a0bf528SMauro Carvalho Chehab 
5019a0bf528SMauro Carvalho Chehab static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
5029a0bf528SMauro Carvalho Chehab 				      unsigned layer)
5039a0bf528SMauro Carvalho Chehab {
5049a0bf528SMauro Carvalho Chehab 	int rc, count;
5059a0bf528SMauro Carvalho Chehab 
5069a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
5079a0bf528SMauro Carvalho Chehab 		[0] = 0x89,	/* Layer A */
5089a0bf528SMauro Carvalho Chehab 		[1] = 0x8d,	/* Layer B */
5099a0bf528SMauro Carvalho Chehab 		[2] = 0x91,	/* Layer C */
5109a0bf528SMauro Carvalho Chehab 	};
5119a0bf528SMauro Carvalho Chehab 
5129a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
5139a0bf528SMauro Carvalho Chehab 		return -EINVAL;
5149a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
5159a0bf528SMauro Carvalho Chehab 	if (rc < 0)
5169a0bf528SMauro Carvalho Chehab 		return rc;
5179a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
5189a0bf528SMauro Carvalho Chehab 	if (rc < 0)
5199a0bf528SMauro Carvalho Chehab 		return rc;
5209a0bf528SMauro Carvalho Chehab 	count = (rc >> 4) & 0x0f;
5219a0bf528SMauro Carvalho Chehab 
5229a0bf528SMauro Carvalho Chehab 	return count;
5239a0bf528SMauro Carvalho Chehab }
5249a0bf528SMauro Carvalho Chehab 
525a77cfcacSMauro Carvalho Chehab static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
526a77cfcacSMauro Carvalho Chehab {
527a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
528a77cfcacSMauro Carvalho Chehab 
529a77cfcacSMauro Carvalho Chehab 	/* Fixed parameters */
530a77cfcacSMauro Carvalho Chehab 	c->delivery_system = SYS_ISDBT;
531a77cfcacSMauro Carvalho Chehab 	c->bandwidth_hz = 6000000;
532a77cfcacSMauro Carvalho Chehab 
533a77cfcacSMauro Carvalho Chehab 	/* Initialize values that will be later autodetected */
534a77cfcacSMauro Carvalho Chehab 	c->isdbt_layer_enabled = 0;
535a77cfcacSMauro Carvalho Chehab 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
536a77cfcacSMauro Carvalho Chehab 	c->guard_interval = GUARD_INTERVAL_AUTO;
537a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_mode = 0;
538a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_segment_count = 0;
539a77cfcacSMauro Carvalho Chehab }
540a77cfcacSMauro Carvalho Chehab 
5419a0bf528SMauro Carvalho Chehab static int mb86a20s_get_frontend(struct dvb_frontend *fe)
5429a0bf528SMauro Carvalho Chehab {
5439a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
544a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
5459a0bf528SMauro Carvalho Chehab 	int i, rc;
5469a0bf528SMauro Carvalho Chehab 
547a77cfcacSMauro Carvalho Chehab 	/* Reset frontend cache to default values */
548a77cfcacSMauro Carvalho Chehab 	mb86a20s_reset_frontend_cache(fe);
5499a0bf528SMauro Carvalho Chehab 
5509a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
5519a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
5529a0bf528SMauro Carvalho Chehab 
5539a0bf528SMauro Carvalho Chehab 	/* Check for partial reception */
5549a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x85);
555a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
556a77cfcacSMauro Carvalho Chehab 		return rc;
5579a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
558a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
559a77cfcacSMauro Carvalho Chehab 		return rc;
560a77cfcacSMauro Carvalho Chehab 	c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
5619a0bf528SMauro Carvalho Chehab 
5629a0bf528SMauro Carvalho Chehab 	/* Get per-layer data */
563a77cfcacSMauro Carvalho Chehab 
5649a0bf528SMauro Carvalho Chehab 	for (i = 0; i < 3; i++) {
5659a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_segment_count(state, i);
566a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
567a77cfcacSMauro Carvalho Chehab 			goto error;
5689a0bf528SMauro Carvalho Chehab 		if (rc >= 0 && rc < 14)
569a77cfcacSMauro Carvalho Chehab 			c->layer[i].segment_count = rc;
570a77cfcacSMauro Carvalho Chehab 		else {
571a77cfcacSMauro Carvalho Chehab 			c->layer[i].segment_count = 0;
5729a0bf528SMauro Carvalho Chehab 			continue;
573a77cfcacSMauro Carvalho Chehab 		}
574a77cfcacSMauro Carvalho Chehab 		c->isdbt_layer_enabled |= 1 << i;
5759a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_modulation(state, i);
576a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
577a77cfcacSMauro Carvalho Chehab 			goto error;
578a77cfcacSMauro Carvalho Chehab 		c->layer[i].modulation = rc;
5799a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_fec(state, i);
580a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
581a77cfcacSMauro Carvalho Chehab 			goto error;
582a77cfcacSMauro Carvalho Chehab 		c->layer[i].fec = rc;
5839a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_interleaving(state, i);
584a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
585a77cfcacSMauro Carvalho Chehab 			goto error;
586a77cfcacSMauro Carvalho Chehab 		c->layer[i].interleaving = rc;
5879a0bf528SMauro Carvalho Chehab 	}
5889a0bf528SMauro Carvalho Chehab 
5899a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x84);
590a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
591a77cfcacSMauro Carvalho Chehab 		return rc;
592a77cfcacSMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
593a77cfcacSMauro Carvalho Chehab 		c->isdbt_sb_mode = 1;
5949a0bf528SMauro Carvalho Chehab 		/* At least, one segment should exist */
595a77cfcacSMauro Carvalho Chehab 		if (!c->isdbt_sb_segment_count)
596a77cfcacSMauro Carvalho Chehab 			c->isdbt_sb_segment_count = 1;
597a77cfcacSMauro Carvalho Chehab 	}
5989a0bf528SMauro Carvalho Chehab 
5999a0bf528SMauro Carvalho Chehab 	/* Get transmission mode and guard interval */
6009a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x07);
601a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
602a77cfcacSMauro Carvalho Chehab 		return rc;
6039a0bf528SMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
6049a0bf528SMauro Carvalho Chehab 		switch (rc & 0x0c >> 2) {
6059a0bf528SMauro Carvalho Chehab 		case 0:
606a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_2K;
6079a0bf528SMauro Carvalho Chehab 			break;
6089a0bf528SMauro Carvalho Chehab 		case 1:
609a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_4K;
6109a0bf528SMauro Carvalho Chehab 			break;
6119a0bf528SMauro Carvalho Chehab 		case 2:
612a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_8K;
6139a0bf528SMauro Carvalho Chehab 			break;
6149a0bf528SMauro Carvalho Chehab 		}
6159a0bf528SMauro Carvalho Chehab 	}
6169a0bf528SMauro Carvalho Chehab 	if (!(rc & 0x10)) {
6179a0bf528SMauro Carvalho Chehab 		switch (rc & 0x3) {
6189a0bf528SMauro Carvalho Chehab 		case 0:
619a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_4;
6209a0bf528SMauro Carvalho Chehab 			break;
6219a0bf528SMauro Carvalho Chehab 		case 1:
622a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_8;
6239a0bf528SMauro Carvalho Chehab 			break;
6249a0bf528SMauro Carvalho Chehab 		case 2:
625a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_16;
6269a0bf528SMauro Carvalho Chehab 			break;
6279a0bf528SMauro Carvalho Chehab 		}
6289a0bf528SMauro Carvalho Chehab 	}
6299a0bf528SMauro Carvalho Chehab 
630a77cfcacSMauro Carvalho Chehab error:
6319a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
6329a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
6339a0bf528SMauro Carvalho Chehab 
634a77cfcacSMauro Carvalho Chehab 	return rc;
635a77cfcacSMauro Carvalho Chehab 
6369a0bf528SMauro Carvalho Chehab }
6379a0bf528SMauro Carvalho Chehab 
6389a0bf528SMauro Carvalho Chehab static int mb86a20s_tune(struct dvb_frontend *fe,
6399a0bf528SMauro Carvalho Chehab 			bool re_tune,
6409a0bf528SMauro Carvalho Chehab 			unsigned int mode_flags,
6419a0bf528SMauro Carvalho Chehab 			unsigned int *delay,
6429a0bf528SMauro Carvalho Chehab 			fe_status_t *status)
6439a0bf528SMauro Carvalho Chehab {
6449a0bf528SMauro Carvalho Chehab 	int rc = 0;
6459a0bf528SMauro Carvalho Chehab 
6469a0bf528SMauro Carvalho Chehab 	dprintk("\n");
6479a0bf528SMauro Carvalho Chehab 
6489a0bf528SMauro Carvalho Chehab 	if (re_tune)
6499a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_set_frontend(fe);
6509a0bf528SMauro Carvalho Chehab 
6519a0bf528SMauro Carvalho Chehab 	if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
6529a0bf528SMauro Carvalho Chehab 		mb86a20s_read_status(fe, status);
6539a0bf528SMauro Carvalho Chehab 
6549a0bf528SMauro Carvalho Chehab 	return rc;
6559a0bf528SMauro Carvalho Chehab }
6569a0bf528SMauro Carvalho Chehab 
6579a0bf528SMauro Carvalho Chehab static void mb86a20s_release(struct dvb_frontend *fe)
6589a0bf528SMauro Carvalho Chehab {
6599a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
6609a0bf528SMauro Carvalho Chehab 
6619a0bf528SMauro Carvalho Chehab 	dprintk("\n");
6629a0bf528SMauro Carvalho Chehab 
6639a0bf528SMauro Carvalho Chehab 	kfree(state);
6649a0bf528SMauro Carvalho Chehab }
6659a0bf528SMauro Carvalho Chehab 
6669a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops;
6679a0bf528SMauro Carvalho Chehab 
6689a0bf528SMauro Carvalho Chehab struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
6699a0bf528SMauro Carvalho Chehab 				    struct i2c_adapter *i2c)
6709a0bf528SMauro Carvalho Chehab {
6719a0bf528SMauro Carvalho Chehab 	u8	rev;
6729a0bf528SMauro Carvalho Chehab 
6739a0bf528SMauro Carvalho Chehab 	/* allocate memory for the internal state */
6749a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state =
6759a0bf528SMauro Carvalho Chehab 		kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
6769a0bf528SMauro Carvalho Chehab 
6779a0bf528SMauro Carvalho Chehab 	dprintk("\n");
6789a0bf528SMauro Carvalho Chehab 	if (state == NULL) {
6799a0bf528SMauro Carvalho Chehab 		rc("Unable to kzalloc\n");
6809a0bf528SMauro Carvalho Chehab 		goto error;
6819a0bf528SMauro Carvalho Chehab 	}
6829a0bf528SMauro Carvalho Chehab 
6839a0bf528SMauro Carvalho Chehab 	/* setup the state */
6849a0bf528SMauro Carvalho Chehab 	state->config = config;
6859a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
6869a0bf528SMauro Carvalho Chehab 
6879a0bf528SMauro Carvalho Chehab 	/* create dvb_frontend */
6889a0bf528SMauro Carvalho Chehab 	memcpy(&state->frontend.ops, &mb86a20s_ops,
6899a0bf528SMauro Carvalho Chehab 		sizeof(struct dvb_frontend_ops));
6909a0bf528SMauro Carvalho Chehab 	state->frontend.demodulator_priv = state;
6919a0bf528SMauro Carvalho Chehab 
6929a0bf528SMauro Carvalho Chehab 	/* Check if it is a mb86a20s frontend */
6939a0bf528SMauro Carvalho Chehab 	rev = mb86a20s_readreg(state, 0);
6949a0bf528SMauro Carvalho Chehab 
6959a0bf528SMauro Carvalho Chehab 	if (rev == 0x13) {
6969a0bf528SMauro Carvalho Chehab 		printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
6979a0bf528SMauro Carvalho Chehab 	} else {
6989a0bf528SMauro Carvalho Chehab 		printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
6999a0bf528SMauro Carvalho Chehab 		       rev);
7009a0bf528SMauro Carvalho Chehab 		goto error;
7019a0bf528SMauro Carvalho Chehab 	}
7029a0bf528SMauro Carvalho Chehab 
7039a0bf528SMauro Carvalho Chehab 	return &state->frontend;
7049a0bf528SMauro Carvalho Chehab 
7059a0bf528SMauro Carvalho Chehab error:
7069a0bf528SMauro Carvalho Chehab 	kfree(state);
7079a0bf528SMauro Carvalho Chehab 	return NULL;
7089a0bf528SMauro Carvalho Chehab }
7099a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(mb86a20s_attach);
7109a0bf528SMauro Carvalho Chehab 
7119a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops = {
7129a0bf528SMauro Carvalho Chehab 	.delsys = { SYS_ISDBT },
7139a0bf528SMauro Carvalho Chehab 	/* Use dib8000 values per default */
7149a0bf528SMauro Carvalho Chehab 	.info = {
7159a0bf528SMauro Carvalho Chehab 		.name = "Fujitsu mb86A20s",
7169a0bf528SMauro Carvalho Chehab 		.caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
7179a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_1_2  | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
7189a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_5_6  | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
7199a0bf528SMauro Carvalho Chehab 			FE_CAN_QPSK     | FE_CAN_QAM_16  | FE_CAN_QAM_64 |
7209a0bf528SMauro Carvalho Chehab 			FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
7219a0bf528SMauro Carvalho Chehab 			FE_CAN_GUARD_INTERVAL_AUTO    | FE_CAN_HIERARCHY_AUTO,
7229a0bf528SMauro Carvalho Chehab 		/* Actually, those values depend on the used tuner */
7239a0bf528SMauro Carvalho Chehab 		.frequency_min = 45000000,
7249a0bf528SMauro Carvalho Chehab 		.frequency_max = 864000000,
7259a0bf528SMauro Carvalho Chehab 		.frequency_stepsize = 62500,
7269a0bf528SMauro Carvalho Chehab 	},
7279a0bf528SMauro Carvalho Chehab 
7289a0bf528SMauro Carvalho Chehab 	.release = mb86a20s_release,
7299a0bf528SMauro Carvalho Chehab 
7309a0bf528SMauro Carvalho Chehab 	.init = mb86a20s_initfe,
7319a0bf528SMauro Carvalho Chehab 	.set_frontend = mb86a20s_set_frontend,
7329a0bf528SMauro Carvalho Chehab 	.get_frontend = mb86a20s_get_frontend,
7339a0bf528SMauro Carvalho Chehab 	.read_status = mb86a20s_read_status,
7349a0bf528SMauro Carvalho Chehab 	.read_signal_strength = mb86a20s_read_signal_strength,
7359a0bf528SMauro Carvalho Chehab 	.tune = mb86a20s_tune,
7369a0bf528SMauro Carvalho Chehab };
7379a0bf528SMauro Carvalho Chehab 
7389a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
7399a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
7409a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
741