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 struct mb86a20s_state {
289a0bf528SMauro Carvalho Chehab 	struct i2c_adapter *i2c;
299a0bf528SMauro Carvalho Chehab 	const struct mb86a20s_config *config;
309a0bf528SMauro Carvalho Chehab 
319a0bf528SMauro Carvalho Chehab 	struct dvb_frontend frontend;
329a0bf528SMauro Carvalho Chehab 
339a0bf528SMauro Carvalho Chehab 	bool need_init;
349a0bf528SMauro Carvalho Chehab };
359a0bf528SMauro Carvalho Chehab 
369a0bf528SMauro Carvalho Chehab struct regdata {
379a0bf528SMauro Carvalho Chehab 	u8 reg;
389a0bf528SMauro Carvalho Chehab 	u8 data;
399a0bf528SMauro Carvalho Chehab };
409a0bf528SMauro Carvalho Chehab 
419a0bf528SMauro Carvalho Chehab /*
429a0bf528SMauro Carvalho Chehab  * Initialization sequence: Use whatevere default values that PV SBTVD
439a0bf528SMauro Carvalho Chehab  * does on its initialisation, obtained via USB snoop
449a0bf528SMauro Carvalho Chehab  */
459a0bf528SMauro Carvalho Chehab static struct regdata mb86a20s_init[] = {
469a0bf528SMauro Carvalho Chehab 	{ 0x70, 0x0f },
479a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
489a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
499a0bf528SMauro Carvalho Chehab 	{ 0x09, 0x3e },
509a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd1 }, { 0x51, 0x22 },
519a0bf528SMauro Carvalho Chehab 	{ 0x39, 0x01 },
529a0bf528SMauro Carvalho Chehab 	{ 0x71, 0x00 },
539a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
549a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
559a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
569a0bf528SMauro Carvalho Chehab 	{ 0x3b, 0x21 },
579a0bf528SMauro Carvalho Chehab 	{ 0x3c, 0x3a },
589a0bf528SMauro Carvalho Chehab 	{ 0x01, 0x0d },
599a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x08 }, { 0x05, 0x05 },
609a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0e }, { 0x05, 0x00 },
619a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0f }, { 0x05, 0x14 },
629a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0b }, { 0x05, 0x8c },
639a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x00 }, { 0x05, 0x00 },
649a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x01 }, { 0x05, 0x07 },
659a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x02 }, { 0x05, 0x0f },
669a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x03 }, { 0x05, 0xa0 },
679a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x09 }, { 0x05, 0x00 },
689a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0a }, { 0x05, 0xff },
699a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x27 }, { 0x05, 0x64 },
709a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x28 }, { 0x05, 0x00 },
719a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x1e }, { 0x05, 0xff },
729a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x29 }, { 0x05, 0x0a },
739a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x32 }, { 0x05, 0x0a },
749a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x14 }, { 0x05, 0x02 },
759a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x04 }, { 0x05, 0x00 },
769a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x05 }, { 0x05, 0x22 },
779a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x06 }, { 0x05, 0x0e },
789a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x07 }, { 0x05, 0xd8 },
799a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x12 }, { 0x05, 0x00 },
809a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x13 }, { 0x05, 0xff },
819a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x15 }, { 0x05, 0x4e },
829a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x16 }, { 0x05, 0x20 },
839a0bf528SMauro Carvalho Chehab 	{ 0x52, 0x01 },
849a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa7 }, { 0x51, 0xff },
859a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa8 }, { 0x51, 0xff },
869a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa9 }, { 0x51, 0xff },
879a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xaa }, { 0x51, 0xff },
889a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xab }, { 0x51, 0xff },
899a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xac }, { 0x51, 0xff },
909a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xad }, { 0x51, 0xff },
919a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xae }, { 0x51, 0xff },
929a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xaf }, { 0x51, 0xff },
939a0bf528SMauro Carvalho Chehab 	{ 0x5e, 0x07 },
949a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xdc }, { 0x51, 0x01 },
959a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xdd }, { 0x51, 0xf4 },
969a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xde }, { 0x51, 0x01 },
979a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xdf }, { 0x51, 0xf4 },
989a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xe0 }, { 0x51, 0x01 },
999a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xe1 }, { 0x51, 0xf4 },
1009a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb0 }, { 0x51, 0x07 },
1019a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb2 }, { 0x51, 0xff },
1029a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb3 }, { 0x51, 0xff },
1039a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb4 }, { 0x51, 0xff },
1049a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb5 }, { 0x51, 0xff },
1059a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb6 }, { 0x51, 0xff },
1069a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xb7 }, { 0x51, 0xff },
1079a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x50 }, { 0x51, 0x02 },
1089a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x51 }, { 0x51, 0x04 },
1099a0bf528SMauro Carvalho Chehab 	{ 0x45, 0x04 },
1109a0bf528SMauro Carvalho Chehab 	{ 0x48, 0x04 },
1119a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd5 }, { 0x51, 0x01 },		/* Serial */
1129a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd6 }, { 0x51, 0x1f },
1139a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd2 }, { 0x51, 0x03 },
1149a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd7 }, { 0x51, 0x3f },
1159a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
1169a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
117ce77d120SMauro Carvalho Chehab 
118ce77d120SMauro Carvalho Chehab 	{ 0x04, 0x40 }, { 0x05, 0x00 },
1199a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x00 }, { 0x29, 0x10 },
1209a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x05 }, { 0x29, 0x02 },
1219a0bf528SMauro Carvalho Chehab 	{ 0x1c, 0x01 },
1229a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
1239a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
1249a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
1259a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
1269a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
1279a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
1289a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
1299a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
1309a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
1319a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
1329a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
1339a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
1349a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
1359a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
1369a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
1379a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
1389a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
1399a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
1409a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
1419a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
1429a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
1439a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
1449a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
1459a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
1469a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
1479a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x1e }, { 0x51, 0x5d },
1489a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x22 }, { 0x51, 0x00 },
1499a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x23 }, { 0x51, 0xc8 },
1509a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x24 }, { 0x51, 0x00 },
1519a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x25 }, { 0x51, 0xf0 },
1529a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x26 }, { 0x51, 0x00 },
1539a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x27 }, { 0x51, 0xc3 },
1549a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x39 }, { 0x51, 0x02 },
1559a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
1569a0bf528SMauro Carvalho Chehab 	{ 0xd0, 0x00 },
1579a0bf528SMauro Carvalho Chehab };
1589a0bf528SMauro Carvalho Chehab 
1599a0bf528SMauro Carvalho Chehab static struct regdata mb86a20s_reset_reception[] = {
1609a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xf0 },
1619a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
1629a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
1639a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x00 },
1649a0bf528SMauro Carvalho Chehab };
1659a0bf528SMauro Carvalho Chehab 
166dd4493efSMauro Carvalho Chehab /*
167dd4493efSMauro Carvalho Chehab  * I2C read/write functions and macros
168dd4493efSMauro Carvalho Chehab  */
169dd4493efSMauro Carvalho Chehab 
1709a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
1719a0bf528SMauro Carvalho Chehab 			     u8 i2c_addr, int reg, int data)
1729a0bf528SMauro Carvalho Chehab {
1739a0bf528SMauro Carvalho Chehab 	u8 buf[] = { reg, data };
1749a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {
1759a0bf528SMauro Carvalho Chehab 		.addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
1769a0bf528SMauro Carvalho Chehab 	};
1779a0bf528SMauro Carvalho Chehab 	int rc;
1789a0bf528SMauro Carvalho Chehab 
1799a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, &msg, 1);
1809a0bf528SMauro Carvalho Chehab 	if (rc != 1) {
181f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
182f66d81b5SMauro Carvalho Chehab 			"%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
183f66d81b5SMauro Carvalho Chehab 			__func__, rc, reg, data);
1849a0bf528SMauro Carvalho Chehab 		return rc;
1859a0bf528SMauro Carvalho Chehab 	}
1869a0bf528SMauro Carvalho Chehab 
1879a0bf528SMauro Carvalho Chehab 	return 0;
1889a0bf528SMauro Carvalho Chehab }
1899a0bf528SMauro Carvalho Chehab 
1909a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
1919a0bf528SMauro Carvalho Chehab 				     u8 i2c_addr, struct regdata *rd, int size)
1929a0bf528SMauro Carvalho Chehab {
1939a0bf528SMauro Carvalho Chehab 	int i, rc;
1949a0bf528SMauro Carvalho Chehab 
1959a0bf528SMauro Carvalho Chehab 	for (i = 0; i < size; i++) {
1969a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
1979a0bf528SMauro Carvalho Chehab 					   rd[i].data);
1989a0bf528SMauro Carvalho Chehab 		if (rc < 0)
1999a0bf528SMauro Carvalho Chehab 			return rc;
2009a0bf528SMauro Carvalho Chehab 	}
2019a0bf528SMauro Carvalho Chehab 	return 0;
2029a0bf528SMauro Carvalho Chehab }
2039a0bf528SMauro Carvalho Chehab 
2049a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
2059a0bf528SMauro Carvalho Chehab 				u8 i2c_addr, u8 reg)
2069a0bf528SMauro Carvalho Chehab {
2079a0bf528SMauro Carvalho Chehab 	u8 val;
2089a0bf528SMauro Carvalho Chehab 	int rc;
2099a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[] = {
2109a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
2119a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
2129a0bf528SMauro Carvalho Chehab 	};
2139a0bf528SMauro Carvalho Chehab 
2149a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, msg, 2);
2159a0bf528SMauro Carvalho Chehab 
2169a0bf528SMauro Carvalho Chehab 	if (rc != 2) {
217f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
218f66d81b5SMauro Carvalho Chehab 			__func__, reg, rc);
219f66d81b5SMauro Carvalho Chehab 		return (rc < 0) ? rc : -EIO;
2209a0bf528SMauro Carvalho Chehab 	}
2219a0bf528SMauro Carvalho Chehab 
2229a0bf528SMauro Carvalho Chehab 	return val;
2239a0bf528SMauro Carvalho Chehab }
2249a0bf528SMauro Carvalho Chehab 
2259a0bf528SMauro Carvalho Chehab #define mb86a20s_readreg(state, reg) \
2269a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
2279a0bf528SMauro Carvalho Chehab #define mb86a20s_writereg(state, reg, val) \
2289a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
2299a0bf528SMauro Carvalho Chehab #define mb86a20s_writeregdata(state, regdata) \
2309a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
2319a0bf528SMauro Carvalho Chehab 	regdata, ARRAY_SIZE(regdata))
2329a0bf528SMauro Carvalho Chehab 
233dd4493efSMauro Carvalho Chehab static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
2349a0bf528SMauro Carvalho Chehab {
2359a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
236dd4493efSMauro Carvalho Chehab 	int val;
2379a0bf528SMauro Carvalho Chehab 
238dd4493efSMauro Carvalho Chehab 	*status = 0;
2399a0bf528SMauro Carvalho Chehab 
240dd4493efSMauro Carvalho Chehab 	val = mb86a20s_readreg(state, 0x0a) & 0xf;
241dd4493efSMauro Carvalho Chehab 	if (val < 0)
242dd4493efSMauro Carvalho Chehab 		return val;
2439a0bf528SMauro Carvalho Chehab 
244dd4493efSMauro Carvalho Chehab 	if (val >= 2)
245dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_SIGNAL;
2469a0bf528SMauro Carvalho Chehab 
247dd4493efSMauro Carvalho Chehab 	if (val >= 4)
248dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_CARRIER;
2499a0bf528SMauro Carvalho Chehab 
250dd4493efSMauro Carvalho Chehab 	if (val >= 5)
251dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_VITERBI;
2529a0bf528SMauro Carvalho Chehab 
253dd4493efSMauro Carvalho Chehab 	if (val >= 7)
254dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_SYNC;
2559a0bf528SMauro Carvalho Chehab 
256dd4493efSMauro Carvalho Chehab 	if (val >= 8)				/* Maybe 9? */
257dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_LOCK;
258dd4493efSMauro Carvalho Chehab 
259f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
260f66d81b5SMauro Carvalho Chehab 		 __func__, *status, val);
261dd4493efSMauro Carvalho Chehab 
262dd4493efSMauro Carvalho Chehab 	return 0;
2639a0bf528SMauro Carvalho Chehab }
2649a0bf528SMauro Carvalho Chehab 
2659a0bf528SMauro Carvalho Chehab static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
2669a0bf528SMauro Carvalho Chehab {
2679a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
2689a0bf528SMauro Carvalho Chehab 	unsigned rf_max, rf_min, rf;
2699a0bf528SMauro Carvalho Chehab 	u8	 val;
2709a0bf528SMauro Carvalho Chehab 
2719a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2729a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
2739a0bf528SMauro Carvalho Chehab 
2749a0bf528SMauro Carvalho Chehab 	/* Does a binary search to get RF strength */
2759a0bf528SMauro Carvalho Chehab 	rf_max = 0xfff;
2769a0bf528SMauro Carvalho Chehab 	rf_min = 0;
2779a0bf528SMauro Carvalho Chehab 	do {
2789a0bf528SMauro Carvalho Chehab 		rf = (rf_max + rf_min) / 2;
2799a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x04, 0x1f);
2809a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x05, rf >> 8);
2819a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x04, 0x20);
2829a0bf528SMauro Carvalho Chehab 		mb86a20s_writereg(state, 0x04, rf);
2839a0bf528SMauro Carvalho Chehab 
2849a0bf528SMauro Carvalho Chehab 		val = mb86a20s_readreg(state, 0x02);
2859a0bf528SMauro Carvalho Chehab 		if (val & 0x08)
2869a0bf528SMauro Carvalho Chehab 			rf_min = (rf_max + rf_min) / 2;
2879a0bf528SMauro Carvalho Chehab 		else
2889a0bf528SMauro Carvalho Chehab 			rf_max = (rf_max + rf_min) / 2;
2899a0bf528SMauro Carvalho Chehab 		if (rf_max - rf_min < 4) {
2909a0bf528SMauro Carvalho Chehab 			*strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
291f66d81b5SMauro Carvalho Chehab 			dev_dbg(&state->i2c->dev,
292f66d81b5SMauro Carvalho Chehab 				"%s: signal strength = %d (%d < RF=%d < %d)\n",
293f66d81b5SMauro Carvalho Chehab 				__func__, rf, rf_min, rf >> 4, rf_max);
2949a0bf528SMauro Carvalho Chehab 			break;
2959a0bf528SMauro Carvalho Chehab 		}
2969a0bf528SMauro Carvalho Chehab 	} while (1);
2979a0bf528SMauro Carvalho Chehab 
2989a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2999a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
3009a0bf528SMauro Carvalho Chehab 
3019a0bf528SMauro Carvalho Chehab 	return 0;
3029a0bf528SMauro Carvalho Chehab }
3039a0bf528SMauro Carvalho Chehab 
3049a0bf528SMauro Carvalho Chehab static int mb86a20s_get_modulation(struct mb86a20s_state *state,
3059a0bf528SMauro Carvalho Chehab 				   unsigned layer)
3069a0bf528SMauro Carvalho Chehab {
3079a0bf528SMauro Carvalho Chehab 	int rc;
3089a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
3099a0bf528SMauro Carvalho Chehab 		[0] = 0x86,	/* Layer A */
3109a0bf528SMauro Carvalho Chehab 		[1] = 0x8a,	/* Layer B */
3119a0bf528SMauro Carvalho Chehab 		[2] = 0x8e,	/* Layer C */
3129a0bf528SMauro Carvalho Chehab 	};
3139a0bf528SMauro Carvalho Chehab 
3149a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
3159a0bf528SMauro Carvalho Chehab 		return -EINVAL;
3169a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
3179a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3189a0bf528SMauro Carvalho Chehab 		return rc;
3199a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
3209a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3219a0bf528SMauro Carvalho Chehab 		return rc;
32204585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
3239a0bf528SMauro Carvalho Chehab 	case 0:
3249a0bf528SMauro Carvalho Chehab 		return DQPSK;
3259a0bf528SMauro Carvalho Chehab 	case 1:
3269a0bf528SMauro Carvalho Chehab 		return QPSK;
3279a0bf528SMauro Carvalho Chehab 	case 2:
3289a0bf528SMauro Carvalho Chehab 		return QAM_16;
3299a0bf528SMauro Carvalho Chehab 	case 3:
3309a0bf528SMauro Carvalho Chehab 		return QAM_64;
3319a0bf528SMauro Carvalho Chehab 	default:
3329a0bf528SMauro Carvalho Chehab 		return QAM_AUTO;
3339a0bf528SMauro Carvalho Chehab 	}
3349a0bf528SMauro Carvalho Chehab }
3359a0bf528SMauro Carvalho Chehab 
3369a0bf528SMauro Carvalho Chehab static int mb86a20s_get_fec(struct mb86a20s_state *state,
3379a0bf528SMauro Carvalho Chehab 			    unsigned layer)
3389a0bf528SMauro Carvalho Chehab {
3399a0bf528SMauro Carvalho Chehab 	int rc;
3409a0bf528SMauro Carvalho Chehab 
3419a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
3429a0bf528SMauro Carvalho Chehab 		[0] = 0x87,	/* Layer A */
3439a0bf528SMauro Carvalho Chehab 		[1] = 0x8b,	/* Layer B */
3449a0bf528SMauro Carvalho Chehab 		[2] = 0x8f,	/* Layer C */
3459a0bf528SMauro Carvalho Chehab 	};
3469a0bf528SMauro Carvalho Chehab 
3479a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
3489a0bf528SMauro Carvalho Chehab 		return -EINVAL;
3499a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
3509a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3519a0bf528SMauro Carvalho Chehab 		return rc;
3529a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
3539a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3549a0bf528SMauro Carvalho Chehab 		return rc;
35504585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
3569a0bf528SMauro Carvalho Chehab 	case 0:
3579a0bf528SMauro Carvalho Chehab 		return FEC_1_2;
3589a0bf528SMauro Carvalho Chehab 	case 1:
3599a0bf528SMauro Carvalho Chehab 		return FEC_2_3;
3609a0bf528SMauro Carvalho Chehab 	case 2:
3619a0bf528SMauro Carvalho Chehab 		return FEC_3_4;
3629a0bf528SMauro Carvalho Chehab 	case 3:
3639a0bf528SMauro Carvalho Chehab 		return FEC_5_6;
3649a0bf528SMauro Carvalho Chehab 	case 4:
3659a0bf528SMauro Carvalho Chehab 		return FEC_7_8;
3669a0bf528SMauro Carvalho Chehab 	default:
3679a0bf528SMauro Carvalho Chehab 		return FEC_AUTO;
3689a0bf528SMauro Carvalho Chehab 	}
3699a0bf528SMauro Carvalho Chehab }
3709a0bf528SMauro Carvalho Chehab 
3719a0bf528SMauro Carvalho Chehab static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
3729a0bf528SMauro Carvalho Chehab 				     unsigned layer)
3739a0bf528SMauro Carvalho Chehab {
3749a0bf528SMauro Carvalho Chehab 	int rc;
3759a0bf528SMauro Carvalho Chehab 
3769a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
3779a0bf528SMauro Carvalho Chehab 		[0] = 0x88,	/* Layer A */
3789a0bf528SMauro Carvalho Chehab 		[1] = 0x8c,	/* Layer B */
3799a0bf528SMauro Carvalho Chehab 		[2] = 0x90,	/* Layer C */
3809a0bf528SMauro Carvalho Chehab 	};
3819a0bf528SMauro Carvalho Chehab 
3829a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
3839a0bf528SMauro Carvalho Chehab 		return -EINVAL;
3849a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
3859a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3869a0bf528SMauro Carvalho Chehab 		return rc;
3879a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
3889a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3899a0bf528SMauro Carvalho Chehab 		return rc;
39004585921SMauro Carvalho Chehab 
39104585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
39204585921SMauro Carvalho Chehab 	case 1:
39304585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_4;
39404585921SMauro Carvalho Chehab 	case 2:
39504585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_8;
39604585921SMauro Carvalho Chehab 	case 3:
39704585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_16;
39804585921SMauro Carvalho Chehab 	case 4:
39904585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_1_32;
40004585921SMauro Carvalho Chehab 
40104585921SMauro Carvalho Chehab 	default:
40204585921SMauro Carvalho Chehab 	case 0:
40304585921SMauro Carvalho Chehab 		return GUARD_INTERVAL_AUTO;
40404585921SMauro Carvalho Chehab 	}
4059a0bf528SMauro Carvalho Chehab }
4069a0bf528SMauro Carvalho Chehab 
4079a0bf528SMauro Carvalho Chehab static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
4089a0bf528SMauro Carvalho Chehab 				      unsigned layer)
4099a0bf528SMauro Carvalho Chehab {
4109a0bf528SMauro Carvalho Chehab 	int rc, count;
4119a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4129a0bf528SMauro Carvalho Chehab 		[0] = 0x89,	/* Layer A */
4139a0bf528SMauro Carvalho Chehab 		[1] = 0x8d,	/* Layer B */
4149a0bf528SMauro Carvalho Chehab 		[2] = 0x91,	/* Layer C */
4159a0bf528SMauro Carvalho Chehab 	};
4169a0bf528SMauro Carvalho Chehab 
417f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
418f66d81b5SMauro Carvalho Chehab 
4199a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4209a0bf528SMauro Carvalho Chehab 		return -EINVAL;
421f66d81b5SMauro Carvalho Chehab 
4229a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4239a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4249a0bf528SMauro Carvalho Chehab 		return rc;
4259a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4269a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4279a0bf528SMauro Carvalho Chehab 		return rc;
4289a0bf528SMauro Carvalho Chehab 	count = (rc >> 4) & 0x0f;
4299a0bf528SMauro Carvalho Chehab 
430f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
431f66d81b5SMauro Carvalho Chehab 
4329a0bf528SMauro Carvalho Chehab 	return count;
4339a0bf528SMauro Carvalho Chehab }
4349a0bf528SMauro Carvalho Chehab 
435a77cfcacSMauro Carvalho Chehab static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
436a77cfcacSMauro Carvalho Chehab {
437f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
438a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
439a77cfcacSMauro Carvalho Chehab 
440f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
441f66d81b5SMauro Carvalho Chehab 
442a77cfcacSMauro Carvalho Chehab 	/* Fixed parameters */
443a77cfcacSMauro Carvalho Chehab 	c->delivery_system = SYS_ISDBT;
444a77cfcacSMauro Carvalho Chehab 	c->bandwidth_hz = 6000000;
445a77cfcacSMauro Carvalho Chehab 
446a77cfcacSMauro Carvalho Chehab 	/* Initialize values that will be later autodetected */
447a77cfcacSMauro Carvalho Chehab 	c->isdbt_layer_enabled = 0;
448a77cfcacSMauro Carvalho Chehab 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
449a77cfcacSMauro Carvalho Chehab 	c->guard_interval = GUARD_INTERVAL_AUTO;
450a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_mode = 0;
451a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_segment_count = 0;
452a77cfcacSMauro Carvalho Chehab }
453a77cfcacSMauro Carvalho Chehab 
4549a0bf528SMauro Carvalho Chehab static int mb86a20s_get_frontend(struct dvb_frontend *fe)
4559a0bf528SMauro Carvalho Chehab {
4569a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
457a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
4589a0bf528SMauro Carvalho Chehab 	int i, rc;
4599a0bf528SMauro Carvalho Chehab 
460f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
461f66d81b5SMauro Carvalho Chehab 
462a77cfcacSMauro Carvalho Chehab 	/* Reset frontend cache to default values */
463a77cfcacSMauro Carvalho Chehab 	mb86a20s_reset_frontend_cache(fe);
4649a0bf528SMauro Carvalho Chehab 
4659a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
4669a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
4679a0bf528SMauro Carvalho Chehab 
4689a0bf528SMauro Carvalho Chehab 	/* Check for partial reception */
4699a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x85);
470a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
471a77cfcacSMauro Carvalho Chehab 		return rc;
4729a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
473a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
474a77cfcacSMauro Carvalho Chehab 		return rc;
475a77cfcacSMauro Carvalho Chehab 	c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
4769a0bf528SMauro Carvalho Chehab 
4779a0bf528SMauro Carvalho Chehab 	/* Get per-layer data */
478a77cfcacSMauro Carvalho Chehab 
4799a0bf528SMauro Carvalho Chehab 	for (i = 0; i < 3; i++) {
480f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
481f66d81b5SMauro Carvalho Chehab 			__func__, 'A' + i);
482f66d81b5SMauro Carvalho Chehab 
4839a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_segment_count(state, i);
484a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
485f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
4869a0bf528SMauro Carvalho Chehab 		if (rc >= 0 && rc < 14)
487a77cfcacSMauro Carvalho Chehab 			c->layer[i].segment_count = rc;
488a77cfcacSMauro Carvalho Chehab 		else {
489a77cfcacSMauro Carvalho Chehab 			c->layer[i].segment_count = 0;
4909a0bf528SMauro Carvalho Chehab 			continue;
491a77cfcacSMauro Carvalho Chehab 		}
492a77cfcacSMauro Carvalho Chehab 		c->isdbt_layer_enabled |= 1 << i;
4939a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_modulation(state, i);
494a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
495f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
496f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
497f66d81b5SMauro Carvalho Chehab 			__func__, rc);
498a77cfcacSMauro Carvalho Chehab 		c->layer[i].modulation = rc;
4999a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_fec(state, i);
500a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
501f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
502f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
503f66d81b5SMauro Carvalho Chehab 			__func__, rc);
504a77cfcacSMauro Carvalho Chehab 		c->layer[i].fec = rc;
5059a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_get_interleaving(state, i);
506a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
507f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
508f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
509f66d81b5SMauro Carvalho Chehab 			__func__, rc);
510a77cfcacSMauro Carvalho Chehab 		c->layer[i].interleaving = rc;
5119a0bf528SMauro Carvalho Chehab 	}
5129a0bf528SMauro Carvalho Chehab 
5139a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x84);
514a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
515a77cfcacSMauro Carvalho Chehab 		return rc;
516a77cfcacSMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
517a77cfcacSMauro Carvalho Chehab 		c->isdbt_sb_mode = 1;
5189a0bf528SMauro Carvalho Chehab 		/* At least, one segment should exist */
519a77cfcacSMauro Carvalho Chehab 		if (!c->isdbt_sb_segment_count)
520a77cfcacSMauro Carvalho Chehab 			c->isdbt_sb_segment_count = 1;
521a77cfcacSMauro Carvalho Chehab 	}
5229a0bf528SMauro Carvalho Chehab 
5239a0bf528SMauro Carvalho Chehab 	/* Get transmission mode and guard interval */
5249a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x07);
525a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
526a77cfcacSMauro Carvalho Chehab 		return rc;
5279a0bf528SMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
5289a0bf528SMauro Carvalho Chehab 		switch (rc & 0x0c >> 2) {
5299a0bf528SMauro Carvalho Chehab 		case 0:
530a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_2K;
5319a0bf528SMauro Carvalho Chehab 			break;
5329a0bf528SMauro Carvalho Chehab 		case 1:
533a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_4K;
5349a0bf528SMauro Carvalho Chehab 			break;
5359a0bf528SMauro Carvalho Chehab 		case 2:
536a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_8K;
5379a0bf528SMauro Carvalho Chehab 			break;
5389a0bf528SMauro Carvalho Chehab 		}
5399a0bf528SMauro Carvalho Chehab 	}
5409a0bf528SMauro Carvalho Chehab 	if (!(rc & 0x10)) {
5419a0bf528SMauro Carvalho Chehab 		switch (rc & 0x3) {
5429a0bf528SMauro Carvalho Chehab 		case 0:
543a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_4;
5449a0bf528SMauro Carvalho Chehab 			break;
5459a0bf528SMauro Carvalho Chehab 		case 1:
546a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_8;
5479a0bf528SMauro Carvalho Chehab 			break;
5489a0bf528SMauro Carvalho Chehab 		case 2:
549a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_16;
5509a0bf528SMauro Carvalho Chehab 			break;
5519a0bf528SMauro Carvalho Chehab 		}
5529a0bf528SMauro Carvalho Chehab 	}
5539a0bf528SMauro Carvalho Chehab 
554f66d81b5SMauro Carvalho Chehab noperlayer_error:
5559a0bf528SMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
5569a0bf528SMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
5579a0bf528SMauro Carvalho Chehab 
558a77cfcacSMauro Carvalho Chehab 	return rc;
559a77cfcacSMauro Carvalho Chehab 
5609a0bf528SMauro Carvalho Chehab }
5619a0bf528SMauro Carvalho Chehab 
562dd4493efSMauro Carvalho Chehab static int mb86a20s_initfe(struct dvb_frontend *fe)
563dd4493efSMauro Carvalho Chehab {
564dd4493efSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
565dd4493efSMauro Carvalho Chehab 	int rc;
566dd4493efSMauro Carvalho Chehab 	u8  regD5 = 1;
567dd4493efSMauro Carvalho Chehab 
568f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
569dd4493efSMauro Carvalho Chehab 
570dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
571dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
572dd4493efSMauro Carvalho Chehab 
573dd4493efSMauro Carvalho Chehab 	/* Initialize the frontend */
574dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_init);
575dd4493efSMauro Carvalho Chehab 	if (rc < 0)
576dd4493efSMauro Carvalho Chehab 		goto err;
577dd4493efSMauro Carvalho Chehab 
578dd4493efSMauro Carvalho Chehab 	if (!state->config->is_serial) {
579dd4493efSMauro Carvalho Chehab 		regD5 &= ~1;
580dd4493efSMauro Carvalho Chehab 
581dd4493efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xd5);
582dd4493efSMauro Carvalho Chehab 		if (rc < 0)
583dd4493efSMauro Carvalho Chehab 			goto err;
584dd4493efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, regD5);
585dd4493efSMauro Carvalho Chehab 		if (rc < 0)
586dd4493efSMauro Carvalho Chehab 			goto err;
587dd4493efSMauro Carvalho Chehab 	}
588dd4493efSMauro Carvalho Chehab 
589dd4493efSMauro Carvalho Chehab err:
590dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
591dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
592dd4493efSMauro Carvalho Chehab 
593dd4493efSMauro Carvalho Chehab 	if (rc < 0) {
594dd4493efSMauro Carvalho Chehab 		state->need_init = true;
595f66d81b5SMauro Carvalho Chehab 		dev_info(&state->i2c->dev,
596f66d81b5SMauro Carvalho Chehab 			 "mb86a20s: Init failed. Will try again later\n");
597dd4493efSMauro Carvalho Chehab 	} else {
598dd4493efSMauro Carvalho Chehab 		state->need_init = false;
599f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
600dd4493efSMauro Carvalho Chehab 	}
601dd4493efSMauro Carvalho Chehab 	return rc;
602dd4493efSMauro Carvalho Chehab }
603dd4493efSMauro Carvalho Chehab 
604dd4493efSMauro Carvalho Chehab static int mb86a20s_set_frontend(struct dvb_frontend *fe)
605dd4493efSMauro Carvalho Chehab {
606dd4493efSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
607dd4493efSMauro Carvalho Chehab 	int rc;
608dd4493efSMauro Carvalho Chehab #if 0
609dd4493efSMauro Carvalho Chehab 	/*
610dd4493efSMauro Carvalho Chehab 	 * FIXME: Properly implement the set frontend properties
611dd4493efSMauro Carvalho Chehab 	 */
612dd4493efSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
613dd4493efSMauro Carvalho Chehab #endif
614f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
615dd4493efSMauro Carvalho Chehab 
616dd4493efSMauro Carvalho Chehab 	/*
617dd4493efSMauro Carvalho Chehab 	 * Gate should already be opened, but it doesn't hurt to
618dd4493efSMauro Carvalho Chehab 	 * double-check
619dd4493efSMauro Carvalho Chehab 	 */
620dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
621dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
622dd4493efSMauro Carvalho Chehab 	fe->ops.tuner_ops.set_params(fe);
623dd4493efSMauro Carvalho Chehab 
624dd4493efSMauro Carvalho Chehab 	/*
625dd4493efSMauro Carvalho Chehab 	 * Make it more reliable: if, for some reason, the initial
626dd4493efSMauro Carvalho Chehab 	 * device initialization doesn't happen, initialize it when
627dd4493efSMauro Carvalho Chehab 	 * a SBTVD parameters are adjusted.
628dd4493efSMauro Carvalho Chehab 	 *
629dd4493efSMauro Carvalho Chehab 	 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
630dd4493efSMauro Carvalho Chehab 	 * the agc callback logic is not called during DVB attach time,
631dd4493efSMauro Carvalho Chehab 	 * causing mb86a20s to not be initialized with Kworld SBTVD.
632dd4493efSMauro Carvalho Chehab 	 * So, this hack is needed, in order to make Kworld SBTVD to work.
633dd4493efSMauro Carvalho Chehab 	 */
634dd4493efSMauro Carvalho Chehab 	if (state->need_init)
635dd4493efSMauro Carvalho Chehab 		mb86a20s_initfe(fe);
636dd4493efSMauro Carvalho Chehab 
637dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
638dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
639dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
640dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
641dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
642dd4493efSMauro Carvalho Chehab 
643dd4493efSMauro Carvalho Chehab 	return rc;
644dd4493efSMauro Carvalho Chehab }
645dd4493efSMauro Carvalho Chehab 
646d36e418aSMauro Carvalho Chehab static int mb86a20s_read_status_gate(struct dvb_frontend *fe,
647d36e418aSMauro Carvalho Chehab 				     fe_status_t *status)
648d36e418aSMauro Carvalho Chehab {
649d36e418aSMauro Carvalho Chehab 	int ret;
650d36e418aSMauro Carvalho Chehab 
651d36e418aSMauro Carvalho Chehab 	*status = 0;
652d36e418aSMauro Carvalho Chehab 
653d36e418aSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
654d36e418aSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
655d36e418aSMauro Carvalho Chehab 
656d36e418aSMauro Carvalho Chehab 	ret = mb86a20s_read_status(fe, status);
657d36e418aSMauro Carvalho Chehab 
658d36e418aSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
659d36e418aSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
660d36e418aSMauro Carvalho Chehab 
661d36e418aSMauro Carvalho Chehab 	return ret;
662d36e418aSMauro Carvalho Chehab }
663d36e418aSMauro Carvalho Chehab 
6649a0bf528SMauro Carvalho Chehab static int mb86a20s_tune(struct dvb_frontend *fe,
6659a0bf528SMauro Carvalho Chehab 			bool re_tune,
6669a0bf528SMauro Carvalho Chehab 			unsigned int mode_flags,
6679a0bf528SMauro Carvalho Chehab 			unsigned int *delay,
6689a0bf528SMauro Carvalho Chehab 			fe_status_t *status)
6699a0bf528SMauro Carvalho Chehab {
670f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
6719a0bf528SMauro Carvalho Chehab 	int rc = 0;
6729a0bf528SMauro Carvalho Chehab 
673f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
6749a0bf528SMauro Carvalho Chehab 
6759a0bf528SMauro Carvalho Chehab 	if (re_tune)
6769a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_set_frontend(fe);
6779a0bf528SMauro Carvalho Chehab 
6789a0bf528SMauro Carvalho Chehab 	if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
679d36e418aSMauro Carvalho Chehab 		mb86a20s_read_status_gate(fe, status);
6809a0bf528SMauro Carvalho Chehab 
6819a0bf528SMauro Carvalho Chehab 	return rc;
6829a0bf528SMauro Carvalho Chehab }
6839a0bf528SMauro Carvalho Chehab 
6849a0bf528SMauro Carvalho Chehab static void mb86a20s_release(struct dvb_frontend *fe)
6859a0bf528SMauro Carvalho Chehab {
6869a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
6879a0bf528SMauro Carvalho Chehab 
688f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
6899a0bf528SMauro Carvalho Chehab 
6909a0bf528SMauro Carvalho Chehab 	kfree(state);
6919a0bf528SMauro Carvalho Chehab }
6929a0bf528SMauro Carvalho Chehab 
6939a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops;
6949a0bf528SMauro Carvalho Chehab 
6959a0bf528SMauro Carvalho Chehab struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
6969a0bf528SMauro Carvalho Chehab 				    struct i2c_adapter *i2c)
6979a0bf528SMauro Carvalho Chehab {
698f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state;
6999a0bf528SMauro Carvalho Chehab 	u8	rev;
7009a0bf528SMauro Carvalho Chehab 
7019a0bf528SMauro Carvalho Chehab 	/* allocate memory for the internal state */
702f66d81b5SMauro Carvalho Chehab 	state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
7039a0bf528SMauro Carvalho Chehab 
704f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
7059a0bf528SMauro Carvalho Chehab 	if (state == NULL) {
706f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
707f66d81b5SMauro Carvalho Chehab 			"%s: unable to allocate memory for state\n", __func__);
7089a0bf528SMauro Carvalho Chehab 		goto error;
7099a0bf528SMauro Carvalho Chehab 	}
7109a0bf528SMauro Carvalho Chehab 
7119a0bf528SMauro Carvalho Chehab 	/* setup the state */
7129a0bf528SMauro Carvalho Chehab 	state->config = config;
7139a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
7149a0bf528SMauro Carvalho Chehab 
7159a0bf528SMauro Carvalho Chehab 	/* create dvb_frontend */
7169a0bf528SMauro Carvalho Chehab 	memcpy(&state->frontend.ops, &mb86a20s_ops,
7179a0bf528SMauro Carvalho Chehab 		sizeof(struct dvb_frontend_ops));
7189a0bf528SMauro Carvalho Chehab 	state->frontend.demodulator_priv = state;
7199a0bf528SMauro Carvalho Chehab 
7209a0bf528SMauro Carvalho Chehab 	/* Check if it is a mb86a20s frontend */
7219a0bf528SMauro Carvalho Chehab 	rev = mb86a20s_readreg(state, 0);
7229a0bf528SMauro Carvalho Chehab 
7239a0bf528SMauro Carvalho Chehab 	if (rev == 0x13) {
724f66d81b5SMauro Carvalho Chehab 		dev_info(&state->i2c->dev,
725f66d81b5SMauro Carvalho Chehab 			 "Detected a Fujitsu mb86a20s frontend\n");
7269a0bf528SMauro Carvalho Chehab 	} else {
727f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
728f66d81b5SMauro Carvalho Chehab 			"Frontend revision %d is unknown - aborting.\n",
7299a0bf528SMauro Carvalho Chehab 		       rev);
7309a0bf528SMauro Carvalho Chehab 		goto error;
7319a0bf528SMauro Carvalho Chehab 	}
7329a0bf528SMauro Carvalho Chehab 
7339a0bf528SMauro Carvalho Chehab 	return &state->frontend;
7349a0bf528SMauro Carvalho Chehab 
7359a0bf528SMauro Carvalho Chehab error:
7369a0bf528SMauro Carvalho Chehab 	kfree(state);
7379a0bf528SMauro Carvalho Chehab 	return NULL;
7389a0bf528SMauro Carvalho Chehab }
7399a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(mb86a20s_attach);
7409a0bf528SMauro Carvalho Chehab 
7419a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops = {
7429a0bf528SMauro Carvalho Chehab 	.delsys = { SYS_ISDBT },
7439a0bf528SMauro Carvalho Chehab 	/* Use dib8000 values per default */
7449a0bf528SMauro Carvalho Chehab 	.info = {
7459a0bf528SMauro Carvalho Chehab 		.name = "Fujitsu mb86A20s",
7469a0bf528SMauro Carvalho Chehab 		.caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
7479a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_1_2  | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
7489a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_5_6  | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
7499a0bf528SMauro Carvalho Chehab 			FE_CAN_QPSK     | FE_CAN_QAM_16  | FE_CAN_QAM_64 |
7509a0bf528SMauro Carvalho Chehab 			FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
7519a0bf528SMauro Carvalho Chehab 			FE_CAN_GUARD_INTERVAL_AUTO    | FE_CAN_HIERARCHY_AUTO,
7529a0bf528SMauro Carvalho Chehab 		/* Actually, those values depend on the used tuner */
7539a0bf528SMauro Carvalho Chehab 		.frequency_min = 45000000,
7549a0bf528SMauro Carvalho Chehab 		.frequency_max = 864000000,
7559a0bf528SMauro Carvalho Chehab 		.frequency_stepsize = 62500,
7569a0bf528SMauro Carvalho Chehab 	},
7579a0bf528SMauro Carvalho Chehab 
7589a0bf528SMauro Carvalho Chehab 	.release = mb86a20s_release,
7599a0bf528SMauro Carvalho Chehab 
7609a0bf528SMauro Carvalho Chehab 	.init = mb86a20s_initfe,
7619a0bf528SMauro Carvalho Chehab 	.set_frontend = mb86a20s_set_frontend,
7629a0bf528SMauro Carvalho Chehab 	.get_frontend = mb86a20s_get_frontend,
763d36e418aSMauro Carvalho Chehab 	.read_status = mb86a20s_read_status_gate,
7649a0bf528SMauro Carvalho Chehab 	.read_signal_strength = mb86a20s_read_signal_strength,
7659a0bf528SMauro Carvalho Chehab 	.tune = mb86a20s_tune,
7669a0bf528SMauro Carvalho Chehab };
7679a0bf528SMauro Carvalho Chehab 
7689a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
7699a0bf528SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
7709a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
771