19a0bf528SMauro Carvalho Chehab /*
29a0bf528SMauro Carvalho Chehab  *   Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
39a0bf528SMauro Carvalho Chehab  *
437e59f87SMauro Carvalho Chehab  *   Copyright (C) 2010-2013 Mauro Carvalho Chehab
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 
234f62a20dSMauro Carvalho Chehab #define NUM_LAYERS 3
244f62a20dSMauro Carvalho Chehab 
259a0bf528SMauro Carvalho Chehab static int debug = 1;
269a0bf528SMauro Carvalho Chehab module_param(debug, int, 0644);
279a0bf528SMauro Carvalho Chehab MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
289a0bf528SMauro Carvalho Chehab 
2904fa725eSMauro Carvalho Chehab enum mb86a20s_bandwidth {
3004fa725eSMauro Carvalho Chehab 	MB86A20S_13SEG = 0,
3104fa725eSMauro Carvalho Chehab 	MB86A20S_13SEG_PARTIAL = 1,
3204fa725eSMauro Carvalho Chehab 	MB86A20S_1SEG = 2,
3304fa725eSMauro Carvalho Chehab 	MB86A20S_3SEG = 3,
3404fa725eSMauro Carvalho Chehab };
3504fa725eSMauro Carvalho Chehab 
36ce08131cSHans Verkuil static u8 mb86a20s_subchannel[] = {
3704fa725eSMauro Carvalho Chehab 	0xb0, 0xc0, 0xd0, 0xe0,
3804fa725eSMauro Carvalho Chehab 	0xf0, 0x00, 0x10, 0x20,
3904fa725eSMauro Carvalho Chehab };
4004fa725eSMauro Carvalho Chehab 
419a0bf528SMauro Carvalho Chehab struct mb86a20s_state {
429a0bf528SMauro Carvalho Chehab 	struct i2c_adapter *i2c;
439a0bf528SMauro Carvalho Chehab 	const struct mb86a20s_config *config;
4409b6d21eSMauro Carvalho Chehab 	u32 last_frequency;
459a0bf528SMauro Carvalho Chehab 
469a0bf528SMauro Carvalho Chehab 	struct dvb_frontend frontend;
479a0bf528SMauro Carvalho Chehab 
48768e6dadSMauro Carvalho Chehab 	u32 if_freq;
4904fa725eSMauro Carvalho Chehab 	enum mb86a20s_bandwidth bw;
5004fa725eSMauro Carvalho Chehab 	bool inversion;
5104fa725eSMauro Carvalho Chehab 	u32 subchannel;
52768e6dadSMauro Carvalho Chehab 
534f62a20dSMauro Carvalho Chehab 	u32 estimated_rate[NUM_LAYERS];
540921ecfdSMauro Carvalho Chehab 	unsigned long get_strength_time;
55d01a8ee3SMauro Carvalho Chehab 
569a0bf528SMauro Carvalho Chehab 	bool need_init;
579a0bf528SMauro Carvalho Chehab };
589a0bf528SMauro Carvalho Chehab 
599a0bf528SMauro Carvalho Chehab struct regdata {
609a0bf528SMauro Carvalho Chehab 	u8 reg;
619a0bf528SMauro Carvalho Chehab 	u8 data;
629a0bf528SMauro Carvalho Chehab };
639a0bf528SMauro Carvalho Chehab 
64d01a8ee3SMauro Carvalho Chehab #define BER_SAMPLING_RATE	1	/* Seconds */
65d01a8ee3SMauro Carvalho Chehab 
669a0bf528SMauro Carvalho Chehab /*
679a0bf528SMauro Carvalho Chehab  * Initialization sequence: Use whatevere default values that PV SBTVD
689a0bf528SMauro Carvalho Chehab  * does on its initialisation, obtained via USB snoop
699a0bf528SMauro Carvalho Chehab  */
70768e6dadSMauro Carvalho Chehab static struct regdata mb86a20s_init1[] = {
719a0bf528SMauro Carvalho Chehab 	{ 0x70, 0x0f },
729a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
739a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
7417e67d4cSMauro Carvalho Chehab 	{ 0x50, 0xd1 }, { 0x51, 0x20 },
75768e6dadSMauro Carvalho Chehab };
76768e6dadSMauro Carvalho Chehab 
77768e6dadSMauro Carvalho Chehab static struct regdata mb86a20s_init2[] = {
789a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
799a0bf528SMauro Carvalho Chehab 	{ 0x3b, 0x21 },
8017e67d4cSMauro Carvalho Chehab 	{ 0x3c, 0x38 },
819a0bf528SMauro Carvalho Chehab 	{ 0x01, 0x0d },
8217e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x08 }, { 0x05, 0x03 },
839a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0e }, { 0x05, 0x00 },
8417e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x0f }, { 0x05, 0x37 },
8517e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x0b }, { 0x05, 0x78 },
869a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x00 }, { 0x05, 0x00 },
8717e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x01 }, { 0x05, 0x1e },
8817e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x02 }, { 0x05, 0x07 },
8917e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x03 }, { 0x05, 0xd0 },
909a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x09 }, { 0x05, 0x00 },
919a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0a }, { 0x05, 0xff },
9217e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x27 }, { 0x05, 0x00 },
939a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x28 }, { 0x05, 0x00 },
9417e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x1e }, { 0x05, 0x00 },
9517e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x29 }, { 0x05, 0x64 },
9617e67d4cSMauro Carvalho Chehab 	{ 0x04, 0x32 }, { 0x05, 0x02 },
979a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x14 }, { 0x05, 0x02 },
989a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x04 }, { 0x05, 0x00 },
999a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x05 }, { 0x05, 0x22 },
1009a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x06 }, { 0x05, 0x0e },
1019a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x07 }, { 0x05, 0xd8 },
1029a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x12 }, { 0x05, 0x00 },
1039a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x13 }, { 0x05, 0xff },
1049a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x15 }, { 0x05, 0x4e },
1059a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x16 }, { 0x05, 0x20 },
10609b6d21eSMauro Carvalho Chehab 
10709b6d21eSMauro Carvalho Chehab 	/*
10809b6d21eSMauro Carvalho Chehab 	 * On this demod, when the bit count reaches the count below,
10909b6d21eSMauro Carvalho Chehab 	 * it collects the bit error count. The bit counters are initialized
11009b6d21eSMauro Carvalho Chehab 	 * to 65535 here. This warrants that all of them will be quickly
11109b6d21eSMauro Carvalho Chehab 	 * calculated when device gets locked. As TMCC is parsed, the values
112d01a8ee3SMauro Carvalho Chehab 	 * will be adjusted later in the driver's code.
11309b6d21eSMauro Carvalho Chehab 	 */
11409b6d21eSMauro Carvalho Chehab 	{ 0x52, 0x01 },				/* Turn on BER before Viterbi */
11509b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xa7 }, { 0x51, 0x00 },
1169a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa8 }, { 0x51, 0xff },
1179a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa9 }, { 0x51, 0xff },
11809b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xaa }, { 0x51, 0x00 },
1199a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xab }, { 0x51, 0xff },
1209a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xac }, { 0x51, 0xff },
12109b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xad }, { 0x51, 0x00 },
1229a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xae }, { 0x51, 0xff },
1239a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xaf }, { 0x51, 0xff },
12409b6d21eSMauro Carvalho Chehab 
125d9b6f08aSMauro Carvalho Chehab 	/*
126d9b6f08aSMauro Carvalho Chehab 	 * On this demod, post BER counts blocks. When the count reaches the
127d9b6f08aSMauro Carvalho Chehab 	 * value below, it collects the block error count. The block counters
128d9b6f08aSMauro Carvalho Chehab 	 * are initialized to 127 here. This warrants that all of them will be
129d9b6f08aSMauro Carvalho Chehab 	 * quickly calculated when device gets locked. As TMCC is parsed, the
130d9b6f08aSMauro Carvalho Chehab 	 * values will be adjusted later in the driver's code.
131d9b6f08aSMauro Carvalho Chehab 	 */
132d9b6f08aSMauro Carvalho Chehab 	{ 0x5e, 0x07 },				/* Turn on BER after Viterbi */
133d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xdc }, { 0x51, 0x00 },
134d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xdd }, { 0x51, 0x7f },
135d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xde }, { 0x51, 0x00 },
136d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xdf }, { 0x51, 0x7f },
137d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xe0 }, { 0x51, 0x00 },
138d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xe1 }, { 0x51, 0x7f },
139593ae89aSMauro Carvalho Chehab 
140593ae89aSMauro Carvalho Chehab 	/*
141593ae89aSMauro Carvalho Chehab 	 * On this demod, when the block count reaches the count below,
142593ae89aSMauro Carvalho Chehab 	 * it collects the block error count. The block counters are initialized
143593ae89aSMauro Carvalho Chehab 	 * to 127 here. This warrants that all of them will be quickly
144593ae89aSMauro Carvalho Chehab 	 * calculated when device gets locked. As TMCC is parsed, the values
145593ae89aSMauro Carvalho Chehab 	 * will be adjusted later in the driver's code.
146593ae89aSMauro Carvalho Chehab 	 */
147593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb0 }, { 0x51, 0x07 },		/* Enable PER */
148593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb2 }, { 0x51, 0x00 },
149593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb3 }, { 0x51, 0x7f },
150593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb4 }, { 0x51, 0x00 },
151593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb5 }, { 0x51, 0x7f },
152593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb6 }, { 0x51, 0x00 },
153593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb7 }, { 0x51, 0x7f },
15425188bd0SMauro Carvalho Chehab 
15525188bd0SMauro Carvalho Chehab 	{ 0x50, 0x50 }, { 0x51, 0x02 },		/* MER manual mode */
15609b6d21eSMauro Carvalho Chehab 	{ 0x50, 0x51 }, { 0x51, 0x04 },		/* MER symbol 4 */
15709b6d21eSMauro Carvalho Chehab 	{ 0x45, 0x04 },				/* CN symbol 4 */
15825188bd0SMauro Carvalho Chehab 	{ 0x48, 0x04 },				/* CN manual mode */
15925188bd0SMauro Carvalho Chehab 
1609a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd6 }, { 0x51, 0x1f },
1619a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd2 }, { 0x51, 0x03 },
16217e67d4cSMauro Carvalho Chehab 	{ 0x50, 0xd7 }, { 0x51, 0xbf },
16317e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x74 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xff },
16417e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x46 }, { 0x29, 0x00 }, { 0x2a, 0x1a }, { 0x2b, 0x0c },
165ce77d120SMauro Carvalho Chehab 
166ce77d120SMauro Carvalho Chehab 	{ 0x04, 0x40 }, { 0x05, 0x00 },
16717e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x00 }, { 0x2b, 0x08 },
16817e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x05 }, { 0x2b, 0x00 },
1699a0bf528SMauro Carvalho Chehab 	{ 0x1c, 0x01 },
17017e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x1f },
17117e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x18 },
17217e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x12 },
17317e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x30 },
17417e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x37 },
17517e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
17617e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x09 },
17717e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x06 },
17817e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7b },
17917e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x76 },
18017e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7d },
18117e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x08 },
18217e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0b },
18317e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
18417e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf2 },
18517e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf3 },
18617e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x05 },
18717e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
18817e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
18917e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xef },
19017e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xd8 },
19117e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xf1 },
19217e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x3d },
19317e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x94 },
19417e67d4cSMauro Carvalho Chehab 	{ 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xba },
1959a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x1e }, { 0x51, 0x5d },
1969a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x22 }, { 0x51, 0x00 },
1979a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x23 }, { 0x51, 0xc8 },
1989a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x24 }, { 0x51, 0x00 },
1999a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x25 }, { 0x51, 0xf0 },
2009a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x26 }, { 0x51, 0x00 },
2019a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x27 }, { 0x51, 0xc3 },
2029a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x39 }, { 0x51, 0x02 },
20317e67d4cSMauro Carvalho Chehab 	{ 0xec, 0x0f },
20417e67d4cSMauro Carvalho Chehab 	{ 0xeb, 0x1f },
2059a0bf528SMauro Carvalho Chehab 	{ 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
2069a0bf528SMauro Carvalho Chehab 	{ 0xd0, 0x00 },
2079a0bf528SMauro Carvalho Chehab };
2089a0bf528SMauro Carvalho Chehab 
2099a0bf528SMauro Carvalho Chehab static struct regdata mb86a20s_reset_reception[] = {
2109a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xf0 },
2119a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
2129a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
2139a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x00 },
2149a0bf528SMauro Carvalho Chehab };
2159a0bf528SMauro Carvalho Chehab 
216d9b6f08aSMauro Carvalho Chehab static struct regdata mb86a20s_per_ber_reset[] = {
217d9b6f08aSMauro Carvalho Chehab 	{ 0x53, 0x00 },	/* pre BER Counter reset */
21809b6d21eSMauro Carvalho Chehab 	{ 0x53, 0x07 },
21909b6d21eSMauro Carvalho Chehab 
220d9b6f08aSMauro Carvalho Chehab 	{ 0x5f, 0x00 },	/* post BER Counter reset */
221d9b6f08aSMauro Carvalho Chehab 	{ 0x5f, 0x07 },
222d9b6f08aSMauro Carvalho Chehab 
22309b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xb1 },	/* PER Counter reset */
22409b6d21eSMauro Carvalho Chehab 	{ 0x51, 0x07 },
22509b6d21eSMauro Carvalho Chehab 	{ 0x51, 0x00 },
22609b6d21eSMauro Carvalho Chehab };
22709b6d21eSMauro Carvalho Chehab 
228dd4493efSMauro Carvalho Chehab /*
229dd4493efSMauro Carvalho Chehab  * I2C read/write functions and macros
230dd4493efSMauro Carvalho Chehab  */
231dd4493efSMauro Carvalho Chehab 
2329a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
23309b6d21eSMauro Carvalho Chehab 			     u8 i2c_addr, u8 reg, u8 data)
2349a0bf528SMauro Carvalho Chehab {
2359a0bf528SMauro Carvalho Chehab 	u8 buf[] = { reg, data };
2369a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {
2379a0bf528SMauro Carvalho Chehab 		.addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
2389a0bf528SMauro Carvalho Chehab 	};
2399a0bf528SMauro Carvalho Chehab 	int rc;
2409a0bf528SMauro Carvalho Chehab 
2419a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, &msg, 1);
2429a0bf528SMauro Carvalho Chehab 	if (rc != 1) {
243f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
244f66d81b5SMauro Carvalho Chehab 			"%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
245f66d81b5SMauro Carvalho Chehab 			__func__, rc, reg, data);
2469a0bf528SMauro Carvalho Chehab 		return rc;
2479a0bf528SMauro Carvalho Chehab 	}
2489a0bf528SMauro Carvalho Chehab 
2499a0bf528SMauro Carvalho Chehab 	return 0;
2509a0bf528SMauro Carvalho Chehab }
2519a0bf528SMauro Carvalho Chehab 
2529a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
2539a0bf528SMauro Carvalho Chehab 				     u8 i2c_addr, struct regdata *rd, int size)
2549a0bf528SMauro Carvalho Chehab {
2559a0bf528SMauro Carvalho Chehab 	int i, rc;
2569a0bf528SMauro Carvalho Chehab 
2579a0bf528SMauro Carvalho Chehab 	for (i = 0; i < size; i++) {
2589a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
2599a0bf528SMauro Carvalho Chehab 					   rd[i].data);
2609a0bf528SMauro Carvalho Chehab 		if (rc < 0)
2619a0bf528SMauro Carvalho Chehab 			return rc;
2629a0bf528SMauro Carvalho Chehab 	}
2639a0bf528SMauro Carvalho Chehab 	return 0;
2649a0bf528SMauro Carvalho Chehab }
2659a0bf528SMauro Carvalho Chehab 
2669a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
2679a0bf528SMauro Carvalho Chehab 				u8 i2c_addr, u8 reg)
2689a0bf528SMauro Carvalho Chehab {
2699a0bf528SMauro Carvalho Chehab 	u8 val;
2709a0bf528SMauro Carvalho Chehab 	int rc;
2719a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[] = {
2729a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
2739a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
2749a0bf528SMauro Carvalho Chehab 	};
2759a0bf528SMauro Carvalho Chehab 
2769a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, msg, 2);
2779a0bf528SMauro Carvalho Chehab 
2789a0bf528SMauro Carvalho Chehab 	if (rc != 2) {
279f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
280f66d81b5SMauro Carvalho Chehab 			__func__, reg, rc);
281f66d81b5SMauro Carvalho Chehab 		return (rc < 0) ? rc : -EIO;
2829a0bf528SMauro Carvalho Chehab 	}
2839a0bf528SMauro Carvalho Chehab 
2849a0bf528SMauro Carvalho Chehab 	return val;
2859a0bf528SMauro Carvalho Chehab }
2869a0bf528SMauro Carvalho Chehab 
2879a0bf528SMauro Carvalho Chehab #define mb86a20s_readreg(state, reg) \
2889a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
2899a0bf528SMauro Carvalho Chehab #define mb86a20s_writereg(state, reg, val) \
2909a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
2919a0bf528SMauro Carvalho Chehab #define mb86a20s_writeregdata(state, regdata) \
2929a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
2939a0bf528SMauro Carvalho Chehab 	regdata, ARRAY_SIZE(regdata))
2949a0bf528SMauro Carvalho Chehab 
29509b6d21eSMauro Carvalho Chehab /*
29609b6d21eSMauro Carvalho Chehab  * Ancillary internal routines (likely compiled inlined)
29709b6d21eSMauro Carvalho Chehab  *
29809b6d21eSMauro Carvalho Chehab  * The functions below assume that gateway lock has already obtained
29909b6d21eSMauro Carvalho Chehab  */
30009b6d21eSMauro Carvalho Chehab 
301dd4493efSMauro Carvalho Chehab static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
3029a0bf528SMauro Carvalho Chehab {
3039a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
304dd4493efSMauro Carvalho Chehab 	int val;
3059a0bf528SMauro Carvalho Chehab 
306dd4493efSMauro Carvalho Chehab 	*status = 0;
3079a0bf528SMauro Carvalho Chehab 
308dd4493efSMauro Carvalho Chehab 	val = mb86a20s_readreg(state, 0x0a) & 0xf;
309dd4493efSMauro Carvalho Chehab 	if (val < 0)
310dd4493efSMauro Carvalho Chehab 		return val;
3119a0bf528SMauro Carvalho Chehab 
312dd4493efSMauro Carvalho Chehab 	if (val >= 2)
313dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_SIGNAL;
3149a0bf528SMauro Carvalho Chehab 
315dd4493efSMauro Carvalho Chehab 	if (val >= 4)
316dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_CARRIER;
3179a0bf528SMauro Carvalho Chehab 
318dd4493efSMauro Carvalho Chehab 	if (val >= 5)
319dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_VITERBI;
3209a0bf528SMauro Carvalho Chehab 
321dd4493efSMauro Carvalho Chehab 	if (val >= 7)
322dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_SYNC;
3239a0bf528SMauro Carvalho Chehab 
324dd4493efSMauro Carvalho Chehab 	if (val >= 8)				/* Maybe 9? */
325dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_LOCK;
326dd4493efSMauro Carvalho Chehab 
327f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
328f66d81b5SMauro Carvalho Chehab 		 __func__, *status, val);
329dd4493efSMauro Carvalho Chehab 
33015b1c5a0SMauro Carvalho Chehab 	return val;
3319a0bf528SMauro Carvalho Chehab }
3329a0bf528SMauro Carvalho Chehab 
33309b6d21eSMauro Carvalho Chehab static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
3349a0bf528SMauro Carvalho Chehab {
3359a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
3360921ecfdSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
33709b6d21eSMauro Carvalho Chehab 	int rc;
3389a0bf528SMauro Carvalho Chehab 	unsigned rf_max, rf_min, rf;
3399a0bf528SMauro Carvalho Chehab 
3400921ecfdSMauro Carvalho Chehab 	if (state->get_strength_time &&
3410921ecfdSMauro Carvalho Chehab 	   (!time_after(jiffies, state->get_strength_time)))
3420921ecfdSMauro Carvalho Chehab 		return c->strength.stat[0].uvalue;
3430921ecfdSMauro Carvalho Chehab 
3440921ecfdSMauro Carvalho Chehab 	/* Reset its value if an error happen */
3450921ecfdSMauro Carvalho Chehab 	c->strength.stat[0].uvalue = 0;
3460921ecfdSMauro Carvalho Chehab 
3479a0bf528SMauro Carvalho Chehab 	/* Does a binary search to get RF strength */
3489a0bf528SMauro Carvalho Chehab 	rf_max = 0xfff;
3499a0bf528SMauro Carvalho Chehab 	rf_min = 0;
3509a0bf528SMauro Carvalho Chehab 	do {
3519a0bf528SMauro Carvalho Chehab 		rf = (rf_max + rf_min) / 2;
35209b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x04, 0x1f);
35309b6d21eSMauro Carvalho Chehab 		if (rc < 0)
35409b6d21eSMauro Carvalho Chehab 			return rc;
35509b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x05, rf >> 8);
35609b6d21eSMauro Carvalho Chehab 		if (rc < 0)
35709b6d21eSMauro Carvalho Chehab 			return rc;
35809b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x04, 0x20);
35909b6d21eSMauro Carvalho Chehab 		if (rc < 0)
36009b6d21eSMauro Carvalho Chehab 			return rc;
361dad78c56SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x05, rf);
36209b6d21eSMauro Carvalho Chehab 		if (rc < 0)
36309b6d21eSMauro Carvalho Chehab 			return rc;
3649a0bf528SMauro Carvalho Chehab 
36509b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x02);
36609b6d21eSMauro Carvalho Chehab 		if (rc < 0)
36709b6d21eSMauro Carvalho Chehab 			return rc;
36809b6d21eSMauro Carvalho Chehab 		if (rc & 0x08)
3699a0bf528SMauro Carvalho Chehab 			rf_min = (rf_max + rf_min) / 2;
3709a0bf528SMauro Carvalho Chehab 		else
3719a0bf528SMauro Carvalho Chehab 			rf_max = (rf_max + rf_min) / 2;
3729a0bf528SMauro Carvalho Chehab 		if (rf_max - rf_min < 4) {
37309b6d21eSMauro Carvalho Chehab 			rf = (rf_max + rf_min) / 2;
37409b6d21eSMauro Carvalho Chehab 
37509b6d21eSMauro Carvalho Chehab 			/* Rescale it from 2^12 (4096) to 2^16 */
3760921ecfdSMauro Carvalho Chehab 			rf = rf << (16 - 12);
3770921ecfdSMauro Carvalho Chehab 			if (rf)
3780921ecfdSMauro Carvalho Chehab 				rf |= (1 << 12) - 1;
3790921ecfdSMauro Carvalho Chehab 
380f66d81b5SMauro Carvalho Chehab 			dev_dbg(&state->i2c->dev,
381f66d81b5SMauro Carvalho Chehab 				"%s: signal strength = %d (%d < RF=%d < %d)\n",
382f66d81b5SMauro Carvalho Chehab 				__func__, rf, rf_min, rf >> 4, rf_max);
3830921ecfdSMauro Carvalho Chehab 			c->strength.stat[0].uvalue = rf;
3840921ecfdSMauro Carvalho Chehab 			state->get_strength_time = jiffies +
3850921ecfdSMauro Carvalho Chehab 						   msecs_to_jiffies(1000);
3860921ecfdSMauro Carvalho Chehab 			return 0;
3879a0bf528SMauro Carvalho Chehab 		}
3889a0bf528SMauro Carvalho Chehab 	} while (1);
3899a0bf528SMauro Carvalho Chehab }
3909a0bf528SMauro Carvalho Chehab 
3919a0bf528SMauro Carvalho Chehab static int mb86a20s_get_modulation(struct mb86a20s_state *state,
3929a0bf528SMauro Carvalho Chehab 				   unsigned layer)
3939a0bf528SMauro Carvalho Chehab {
3949a0bf528SMauro Carvalho Chehab 	int rc;
3959a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
3969a0bf528SMauro Carvalho Chehab 		[0] = 0x86,	/* Layer A */
3979a0bf528SMauro Carvalho Chehab 		[1] = 0x8a,	/* Layer B */
3989a0bf528SMauro Carvalho Chehab 		[2] = 0x8e,	/* Layer C */
3999a0bf528SMauro Carvalho Chehab 	};
4009a0bf528SMauro Carvalho Chehab 
4019a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4029a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4039a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4049a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4059a0bf528SMauro Carvalho Chehab 		return rc;
4069a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4079a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4089a0bf528SMauro Carvalho Chehab 		return rc;
40904585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
4109a0bf528SMauro Carvalho Chehab 	case 0:
4119a0bf528SMauro Carvalho Chehab 		return DQPSK;
4129a0bf528SMauro Carvalho Chehab 	case 1:
4139a0bf528SMauro Carvalho Chehab 		return QPSK;
4149a0bf528SMauro Carvalho Chehab 	case 2:
4159a0bf528SMauro Carvalho Chehab 		return QAM_16;
4169a0bf528SMauro Carvalho Chehab 	case 3:
4179a0bf528SMauro Carvalho Chehab 		return QAM_64;
4189a0bf528SMauro Carvalho Chehab 	default:
4199a0bf528SMauro Carvalho Chehab 		return QAM_AUTO;
4209a0bf528SMauro Carvalho Chehab 	}
4219a0bf528SMauro Carvalho Chehab }
4229a0bf528SMauro Carvalho Chehab 
4239a0bf528SMauro Carvalho Chehab static int mb86a20s_get_fec(struct mb86a20s_state *state,
4249a0bf528SMauro Carvalho Chehab 			    unsigned layer)
4259a0bf528SMauro Carvalho Chehab {
4269a0bf528SMauro Carvalho Chehab 	int rc;
4279a0bf528SMauro Carvalho Chehab 
4289a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4299a0bf528SMauro Carvalho Chehab 		[0] = 0x87,	/* Layer A */
4309a0bf528SMauro Carvalho Chehab 		[1] = 0x8b,	/* Layer B */
4319a0bf528SMauro Carvalho Chehab 		[2] = 0x8f,	/* Layer C */
4329a0bf528SMauro Carvalho Chehab 	};
4339a0bf528SMauro Carvalho Chehab 
4349a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4359a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4369a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4379a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4389a0bf528SMauro Carvalho Chehab 		return rc;
4399a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4409a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4419a0bf528SMauro Carvalho Chehab 		return rc;
44204585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
4439a0bf528SMauro Carvalho Chehab 	case 0:
4449a0bf528SMauro Carvalho Chehab 		return FEC_1_2;
4459a0bf528SMauro Carvalho Chehab 	case 1:
4469a0bf528SMauro Carvalho Chehab 		return FEC_2_3;
4479a0bf528SMauro Carvalho Chehab 	case 2:
4489a0bf528SMauro Carvalho Chehab 		return FEC_3_4;
4499a0bf528SMauro Carvalho Chehab 	case 3:
4509a0bf528SMauro Carvalho Chehab 		return FEC_5_6;
4519a0bf528SMauro Carvalho Chehab 	case 4:
4529a0bf528SMauro Carvalho Chehab 		return FEC_7_8;
4539a0bf528SMauro Carvalho Chehab 	default:
4549a0bf528SMauro Carvalho Chehab 		return FEC_AUTO;
4559a0bf528SMauro Carvalho Chehab 	}
4569a0bf528SMauro Carvalho Chehab }
4579a0bf528SMauro Carvalho Chehab 
4589a0bf528SMauro Carvalho Chehab static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
4599a0bf528SMauro Carvalho Chehab 				     unsigned layer)
4609a0bf528SMauro Carvalho Chehab {
4619a0bf528SMauro Carvalho Chehab 	int rc;
4624247368bSMauro Carvalho Chehab 	int interleaving[] = {
4634247368bSMauro Carvalho Chehab 		0, 1, 2, 4, 8
4644247368bSMauro Carvalho Chehab 	};
4659a0bf528SMauro Carvalho Chehab 
4669a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4679a0bf528SMauro Carvalho Chehab 		[0] = 0x88,	/* Layer A */
4689a0bf528SMauro Carvalho Chehab 		[1] = 0x8c,	/* Layer B */
4699a0bf528SMauro Carvalho Chehab 		[2] = 0x90,	/* Layer C */
4709a0bf528SMauro Carvalho Chehab 	};
4719a0bf528SMauro Carvalho Chehab 
4729a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4739a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4749a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4759a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4769a0bf528SMauro Carvalho Chehab 		return rc;
4779a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4789a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4799a0bf528SMauro Carvalho Chehab 		return rc;
48004585921SMauro Carvalho Chehab 
4814247368bSMauro Carvalho Chehab 	return interleaving[(rc >> 4) & 0x07];
4829a0bf528SMauro Carvalho Chehab }
4839a0bf528SMauro Carvalho Chehab 
4849a0bf528SMauro Carvalho Chehab static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
4859a0bf528SMauro Carvalho Chehab 				      unsigned layer)
4869a0bf528SMauro Carvalho Chehab {
4879a0bf528SMauro Carvalho Chehab 	int rc, count;
4889a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4899a0bf528SMauro Carvalho Chehab 		[0] = 0x89,	/* Layer A */
4909a0bf528SMauro Carvalho Chehab 		[1] = 0x8d,	/* Layer B */
4919a0bf528SMauro Carvalho Chehab 		[2] = 0x91,	/* Layer C */
4929a0bf528SMauro Carvalho Chehab 	};
4939a0bf528SMauro Carvalho Chehab 
494f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
495f66d81b5SMauro Carvalho Chehab 
4969a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4979a0bf528SMauro Carvalho Chehab 		return -EINVAL;
498f66d81b5SMauro Carvalho Chehab 
4999a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
5009a0bf528SMauro Carvalho Chehab 	if (rc < 0)
5019a0bf528SMauro Carvalho Chehab 		return rc;
5029a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
5039a0bf528SMauro Carvalho Chehab 	if (rc < 0)
5049a0bf528SMauro Carvalho Chehab 		return rc;
5059a0bf528SMauro Carvalho Chehab 	count = (rc >> 4) & 0x0f;
5069a0bf528SMauro Carvalho Chehab 
507f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
508f66d81b5SMauro Carvalho Chehab 
5099a0bf528SMauro Carvalho Chehab 	return count;
5109a0bf528SMauro Carvalho Chehab }
5119a0bf528SMauro Carvalho Chehab 
512a77cfcacSMauro Carvalho Chehab static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
513a77cfcacSMauro Carvalho Chehab {
514f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
515a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
516a77cfcacSMauro Carvalho Chehab 
517f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
518f66d81b5SMauro Carvalho Chehab 
519a77cfcacSMauro Carvalho Chehab 	/* Fixed parameters */
520a77cfcacSMauro Carvalho Chehab 	c->delivery_system = SYS_ISDBT;
521a77cfcacSMauro Carvalho Chehab 	c->bandwidth_hz = 6000000;
522a77cfcacSMauro Carvalho Chehab 
523a77cfcacSMauro Carvalho Chehab 	/* Initialize values that will be later autodetected */
524a77cfcacSMauro Carvalho Chehab 	c->isdbt_layer_enabled = 0;
525a77cfcacSMauro Carvalho Chehab 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
526a77cfcacSMauro Carvalho Chehab 	c->guard_interval = GUARD_INTERVAL_AUTO;
527a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_mode = 0;
528a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_segment_count = 0;
529a77cfcacSMauro Carvalho Chehab }
530a77cfcacSMauro Carvalho Chehab 
531d01a8ee3SMauro Carvalho Chehab /*
532d01a8ee3SMauro Carvalho Chehab  * Estimates the bit rate using the per-segment bit rate given by
533d01a8ee3SMauro Carvalho Chehab  * ABNT/NBR 15601 spec (table 4).
534d01a8ee3SMauro Carvalho Chehab  */
535d01a8ee3SMauro Carvalho Chehab static u32 isdbt_rate[3][5][4] = {
536d01a8ee3SMauro Carvalho Chehab 	{	/* DQPSK/QPSK */
537d01a8ee3SMauro Carvalho Chehab 		{  280850,  312060,  330420,  340430 },	/* 1/2 */
538d01a8ee3SMauro Carvalho Chehab 		{  374470,  416080,  440560,  453910 },	/* 2/3 */
539d01a8ee3SMauro Carvalho Chehab 		{  421280,  468090,  495630,  510650 },	/* 3/4 */
540d01a8ee3SMauro Carvalho Chehab 		{  468090,  520100,  550700,  567390 },	/* 5/6 */
541d01a8ee3SMauro Carvalho Chehab 		{  491500,  546110,  578230,  595760 },	/* 7/8 */
542d01a8ee3SMauro Carvalho Chehab 	}, {	/* QAM16 */
543d01a8ee3SMauro Carvalho Chehab 		{  561710,  624130,  660840,  680870 },	/* 1/2 */
544d01a8ee3SMauro Carvalho Chehab 		{  748950,  832170,  881120,  907820 },	/* 2/3 */
545d01a8ee3SMauro Carvalho Chehab 		{  842570,  936190,  991260, 1021300 },	/* 3/4 */
546d01a8ee3SMauro Carvalho Chehab 		{  936190, 1040210, 1101400, 1134780 },	/* 5/6 */
547d01a8ee3SMauro Carvalho Chehab 		{  983000, 1092220, 1156470, 1191520 },	/* 7/8 */
548d01a8ee3SMauro Carvalho Chehab 	}, {	/* QAM64 */
549d01a8ee3SMauro Carvalho Chehab 		{  842570,  936190,  991260, 1021300 },	/* 1/2 */
550d01a8ee3SMauro Carvalho Chehab 		{ 1123430, 1248260, 1321680, 1361740 },	/* 2/3 */
551d01a8ee3SMauro Carvalho Chehab 		{ 1263860, 1404290, 1486900, 1531950 },	/* 3/4 */
552d01a8ee3SMauro Carvalho Chehab 		{ 1404290, 1560320, 1652110, 1702170 },	/* 5/6 */
553d01a8ee3SMauro Carvalho Chehab 		{ 1474500, 1638340, 1734710, 1787280 },	/* 7/8 */
554d01a8ee3SMauro Carvalho Chehab 	}
555d01a8ee3SMauro Carvalho Chehab };
556d01a8ee3SMauro Carvalho Chehab 
557d01a8ee3SMauro Carvalho Chehab static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
5580562aef2SMauro Carvalho Chehab 				   u32 modulation, u32 forward_error_correction,
559277bfd2fSMauro Carvalho Chehab 				   u32 guard_interval,
560d01a8ee3SMauro Carvalho Chehab 				   u32 segment)
561d01a8ee3SMauro Carvalho Chehab {
562d01a8ee3SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
563d01a8ee3SMauro Carvalho Chehab 	u32 rate;
5640562aef2SMauro Carvalho Chehab 	int mod, fec, guard;
565d01a8ee3SMauro Carvalho Chehab 
566d01a8ee3SMauro Carvalho Chehab 	/*
567277bfd2fSMauro Carvalho Chehab 	 * If modulation/fec/guard is not detected, the default is
568d01a8ee3SMauro Carvalho Chehab 	 * to consider the lowest bit rate, to avoid taking too long time
569d01a8ee3SMauro Carvalho Chehab 	 * to get BER.
570d01a8ee3SMauro Carvalho Chehab 	 */
571d01a8ee3SMauro Carvalho Chehab 	switch (modulation) {
572d01a8ee3SMauro Carvalho Chehab 	case DQPSK:
573d01a8ee3SMauro Carvalho Chehab 	case QPSK:
574d01a8ee3SMauro Carvalho Chehab 	default:
5750562aef2SMauro Carvalho Chehab 		mod = 0;
576d01a8ee3SMauro Carvalho Chehab 		break;
577d01a8ee3SMauro Carvalho Chehab 	case QAM_16:
5780562aef2SMauro Carvalho Chehab 		mod = 1;
579d01a8ee3SMauro Carvalho Chehab 		break;
580d01a8ee3SMauro Carvalho Chehab 	case QAM_64:
5810562aef2SMauro Carvalho Chehab 		mod = 2;
582d01a8ee3SMauro Carvalho Chehab 		break;
583d01a8ee3SMauro Carvalho Chehab 	}
584d01a8ee3SMauro Carvalho Chehab 
5850562aef2SMauro Carvalho Chehab 	switch (forward_error_correction) {
586d01a8ee3SMauro Carvalho Chehab 	default:
587d01a8ee3SMauro Carvalho Chehab 	case FEC_1_2:
588d01a8ee3SMauro Carvalho Chehab 	case FEC_AUTO:
5890562aef2SMauro Carvalho Chehab 		fec = 0;
590d01a8ee3SMauro Carvalho Chehab 		break;
591d01a8ee3SMauro Carvalho Chehab 	case FEC_2_3:
5920562aef2SMauro Carvalho Chehab 		fec = 1;
593d01a8ee3SMauro Carvalho Chehab 		break;
594d01a8ee3SMauro Carvalho Chehab 	case FEC_3_4:
5950562aef2SMauro Carvalho Chehab 		fec = 2;
596d01a8ee3SMauro Carvalho Chehab 		break;
597d01a8ee3SMauro Carvalho Chehab 	case FEC_5_6:
5980562aef2SMauro Carvalho Chehab 		fec = 3;
599d01a8ee3SMauro Carvalho Chehab 		break;
600d01a8ee3SMauro Carvalho Chehab 	case FEC_7_8:
6010562aef2SMauro Carvalho Chehab 		fec = 4;
602d01a8ee3SMauro Carvalho Chehab 		break;
603d01a8ee3SMauro Carvalho Chehab 	}
604d01a8ee3SMauro Carvalho Chehab 
605277bfd2fSMauro Carvalho Chehab 	switch (guard_interval) {
606d01a8ee3SMauro Carvalho Chehab 	default:
607d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_4:
6080562aef2SMauro Carvalho Chehab 		guard = 0;
609d01a8ee3SMauro Carvalho Chehab 		break;
610d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_8:
6110562aef2SMauro Carvalho Chehab 		guard = 1;
612d01a8ee3SMauro Carvalho Chehab 		break;
613d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_16:
6140562aef2SMauro Carvalho Chehab 		guard = 2;
615d01a8ee3SMauro Carvalho Chehab 		break;
616d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_32:
6170562aef2SMauro Carvalho Chehab 		guard = 3;
618d01a8ee3SMauro Carvalho Chehab 		break;
619d01a8ee3SMauro Carvalho Chehab 	}
620d01a8ee3SMauro Carvalho Chehab 
621d01a8ee3SMauro Carvalho Chehab 	/* Samples BER at BER_SAMPLING_RATE seconds */
6220562aef2SMauro Carvalho Chehab 	rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
623d01a8ee3SMauro Carvalho Chehab 
624d01a8ee3SMauro Carvalho Chehab 	/* Avoids sampling too quickly or to overflow the register */
625d01a8ee3SMauro Carvalho Chehab 	if (rate < 256)
626d01a8ee3SMauro Carvalho Chehab 		rate = 256;
627d01a8ee3SMauro Carvalho Chehab 	else if (rate > (1 << 24) - 1)
628d01a8ee3SMauro Carvalho Chehab 		rate = (1 << 24) - 1;
629d01a8ee3SMauro Carvalho Chehab 
630d01a8ee3SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
631d01a8ee3SMauro Carvalho Chehab 		"%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
6320562aef2SMauro Carvalho Chehab 		__func__, 'A' + layer,
6330562aef2SMauro Carvalho Chehab 		segment * isdbt_rate[mod][fec][guard]/1000,
634d01a8ee3SMauro Carvalho Chehab 		rate, rate);
635d01a8ee3SMauro Carvalho Chehab 
636b1f89331SMauro Carvalho Chehab 	state->estimated_rate[layer] = rate;
637d01a8ee3SMauro Carvalho Chehab }
638d01a8ee3SMauro Carvalho Chehab 
6399a0bf528SMauro Carvalho Chehab static int mb86a20s_get_frontend(struct dvb_frontend *fe)
6409a0bf528SMauro Carvalho Chehab {
6419a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
642a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6435cb88ca8SMauro Carvalho Chehab 	int layer, rc;
6449a0bf528SMauro Carvalho Chehab 
645f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
646f66d81b5SMauro Carvalho Chehab 
647a77cfcacSMauro Carvalho Chehab 	/* Reset frontend cache to default values */
648a77cfcacSMauro Carvalho Chehab 	mb86a20s_reset_frontend_cache(fe);
6499a0bf528SMauro Carvalho Chehab 
6509a0bf528SMauro Carvalho Chehab 	/* Check for partial reception */
6519a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x85);
652a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
653a77cfcacSMauro Carvalho Chehab 		return rc;
6549a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
655a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
656a77cfcacSMauro Carvalho Chehab 		return rc;
657a77cfcacSMauro Carvalho Chehab 	c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
6589a0bf528SMauro Carvalho Chehab 
6599a0bf528SMauro Carvalho Chehab 	/* Get per-layer data */
660a77cfcacSMauro Carvalho Chehab 
6615cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS; layer++) {
662f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
6635cb88ca8SMauro Carvalho Chehab 			__func__, 'A' + layer);
664f66d81b5SMauro Carvalho Chehab 
6655cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_segment_count(state, layer);
666a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
667f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
668d01a8ee3SMauro Carvalho Chehab 		if (rc >= 0 && rc < 14) {
6695cb88ca8SMauro Carvalho Chehab 			c->layer[layer].segment_count = rc;
670d01a8ee3SMauro Carvalho Chehab 		} else {
6715cb88ca8SMauro Carvalho Chehab 			c->layer[layer].segment_count = 0;
6725cb88ca8SMauro Carvalho Chehab 			state->estimated_rate[layer] = 0;
6739a0bf528SMauro Carvalho Chehab 			continue;
674a77cfcacSMauro Carvalho Chehab 		}
6755cb88ca8SMauro Carvalho Chehab 		c->isdbt_layer_enabled |= 1 << layer;
6765cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_modulation(state, layer);
677a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
678f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
679f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
680f66d81b5SMauro Carvalho Chehab 			__func__, rc);
6815cb88ca8SMauro Carvalho Chehab 		c->layer[layer].modulation = rc;
6825cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_fec(state, layer);
683a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
684f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
685f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
686f66d81b5SMauro Carvalho Chehab 			__func__, rc);
6875cb88ca8SMauro Carvalho Chehab 		c->layer[layer].fec = rc;
6885cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_interleaving(state, layer);
689a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
690f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
691f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
692f66d81b5SMauro Carvalho Chehab 			__func__, rc);
6935cb88ca8SMauro Carvalho Chehab 		c->layer[layer].interleaving = rc;
6945cb88ca8SMauro Carvalho Chehab 		mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
6955cb88ca8SMauro Carvalho Chehab 				       c->layer[layer].fec,
696277bfd2fSMauro Carvalho Chehab 				       c->guard_interval,
6975cb88ca8SMauro Carvalho Chehab 				       c->layer[layer].segment_count);
6989a0bf528SMauro Carvalho Chehab 	}
6999a0bf528SMauro Carvalho Chehab 
7009a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x84);
701a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
702a77cfcacSMauro Carvalho Chehab 		return rc;
703a77cfcacSMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
704a77cfcacSMauro Carvalho Chehab 		c->isdbt_sb_mode = 1;
7059a0bf528SMauro Carvalho Chehab 		/* At least, one segment should exist */
706a77cfcacSMauro Carvalho Chehab 		if (!c->isdbt_sb_segment_count)
707a77cfcacSMauro Carvalho Chehab 			c->isdbt_sb_segment_count = 1;
708a77cfcacSMauro Carvalho Chehab 	}
7099a0bf528SMauro Carvalho Chehab 
7109a0bf528SMauro Carvalho Chehab 	/* Get transmission mode and guard interval */
7119a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x07);
712a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
713a77cfcacSMauro Carvalho Chehab 		return rc;
714276dfc4bSMauro Carvalho Chehab 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
7159a0bf528SMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
716276dfc4bSMauro Carvalho Chehab 		/* Only modes 2 and 3 are supported */
717276dfc4bSMauro Carvalho Chehab 		switch ((rc >> 2) & 0x03) {
7189a0bf528SMauro Carvalho Chehab 		case 1:
719a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_4K;
7209a0bf528SMauro Carvalho Chehab 			break;
7219a0bf528SMauro Carvalho Chehab 		case 2:
722a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_8K;
7239a0bf528SMauro Carvalho Chehab 			break;
7249a0bf528SMauro Carvalho Chehab 		}
7259a0bf528SMauro Carvalho Chehab 	}
726276dfc4bSMauro Carvalho Chehab 	c->guard_interval = GUARD_INTERVAL_AUTO;
7279a0bf528SMauro Carvalho Chehab 	if (!(rc & 0x10)) {
728276dfc4bSMauro Carvalho Chehab 		/* Guard interval 1/32 is not supported */
7299a0bf528SMauro Carvalho Chehab 		switch (rc & 0x3) {
7309a0bf528SMauro Carvalho Chehab 		case 0:
731a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_4;
7329a0bf528SMauro Carvalho Chehab 			break;
7339a0bf528SMauro Carvalho Chehab 		case 1:
734a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_8;
7359a0bf528SMauro Carvalho Chehab 			break;
7369a0bf528SMauro Carvalho Chehab 		case 2:
737a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_16;
7389a0bf528SMauro Carvalho Chehab 			break;
7399a0bf528SMauro Carvalho Chehab 		}
7409a0bf528SMauro Carvalho Chehab 	}
74109b6d21eSMauro Carvalho Chehab 	return 0;
7429a0bf528SMauro Carvalho Chehab 
743f66d81b5SMauro Carvalho Chehab noperlayer_error:
74409b6d21eSMauro Carvalho Chehab 
74509b6d21eSMauro Carvalho Chehab 	/* per-layer info is incomplete; discard all per-layer */
74609b6d21eSMauro Carvalho Chehab 	c->isdbt_layer_enabled = 0;
7479a0bf528SMauro Carvalho Chehab 
748a77cfcacSMauro Carvalho Chehab 	return rc;
7499a0bf528SMauro Carvalho Chehab }
7509a0bf528SMauro Carvalho Chehab 
75109b6d21eSMauro Carvalho Chehab static int mb86a20s_reset_counters(struct dvb_frontend *fe)
75209b6d21eSMauro Carvalho Chehab {
75309b6d21eSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
75409b6d21eSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
75509b6d21eSMauro Carvalho Chehab 	int rc, val;
75609b6d21eSMauro Carvalho Chehab 
75709b6d21eSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
75809b6d21eSMauro Carvalho Chehab 
75909b6d21eSMauro Carvalho Chehab 	/* Reset the counters, if the channel changed */
76009b6d21eSMauro Carvalho Chehab 	if (state->last_frequency != c->frequency) {
76109b6d21eSMauro Carvalho Chehab 		memset(&c->cnr, 0, sizeof(c->cnr));
76209b6d21eSMauro Carvalho Chehab 		memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
76309b6d21eSMauro Carvalho Chehab 		memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
764d9b6f08aSMauro Carvalho Chehab 		memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
765d9b6f08aSMauro Carvalho Chehab 		memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
76609b6d21eSMauro Carvalho Chehab 		memset(&c->block_error, 0, sizeof(c->block_error));
76709b6d21eSMauro Carvalho Chehab 		memset(&c->block_count, 0, sizeof(c->block_count));
76809b6d21eSMauro Carvalho Chehab 
76909b6d21eSMauro Carvalho Chehab 		state->last_frequency = c->frequency;
77009b6d21eSMauro Carvalho Chehab 	}
77109b6d21eSMauro Carvalho Chehab 
77209b6d21eSMauro Carvalho Chehab 	/* Clear status for most stats */
77309b6d21eSMauro Carvalho Chehab 
774d9b6f08aSMauro Carvalho Chehab 	/* BER/PER counter reset */
775d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
77609b6d21eSMauro Carvalho Chehab 	if (rc < 0)
77709b6d21eSMauro Carvalho Chehab 		goto err;
77809b6d21eSMauro Carvalho Chehab 
77909b6d21eSMauro Carvalho Chehab 	/* CNR counter reset */
78009b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x45);
78109b6d21eSMauro Carvalho Chehab 	if (rc < 0)
78209b6d21eSMauro Carvalho Chehab 		goto err;
78309b6d21eSMauro Carvalho Chehab 	val = rc;
78409b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val | 0x10);
78509b6d21eSMauro Carvalho Chehab 	if (rc < 0)
78609b6d21eSMauro Carvalho Chehab 		goto err;
78709b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
78809b6d21eSMauro Carvalho Chehab 	if (rc < 0)
78909b6d21eSMauro Carvalho Chehab 		goto err;
79009b6d21eSMauro Carvalho Chehab 
79109b6d21eSMauro Carvalho Chehab 	/* MER counter reset */
79209b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0x50);
79309b6d21eSMauro Carvalho Chehab 	if (rc < 0)
79409b6d21eSMauro Carvalho Chehab 		goto err;
79509b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
79609b6d21eSMauro Carvalho Chehab 	if (rc < 0)
79709b6d21eSMauro Carvalho Chehab 		goto err;
79809b6d21eSMauro Carvalho Chehab 	val = rc;
79909b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val | 0x01);
80009b6d21eSMauro Carvalho Chehab 	if (rc < 0)
80109b6d21eSMauro Carvalho Chehab 		goto err;
80209b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val & 0x06);
80309b6d21eSMauro Carvalho Chehab 	if (rc < 0)
80409b6d21eSMauro Carvalho Chehab 		goto err;
80509b6d21eSMauro Carvalho Chehab 
806149d518aSMauro Carvalho Chehab 	goto ok;
80709b6d21eSMauro Carvalho Chehab err:
808149d518aSMauro Carvalho Chehab 	dev_err(&state->i2c->dev,
809149d518aSMauro Carvalho Chehab 		"%s: Can't reset FE statistics (error %d).\n",
810149d518aSMauro Carvalho Chehab 		__func__, rc);
811149d518aSMauro Carvalho Chehab ok:
81209b6d21eSMauro Carvalho Chehab 	return rc;
81309b6d21eSMauro Carvalho Chehab }
81409b6d21eSMauro Carvalho Chehab 
815ad0abbf1SMauro Carvalho Chehab static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
816149d518aSMauro Carvalho Chehab 				unsigned layer,
817149d518aSMauro Carvalho Chehab 				u32 *error, u32 *count)
818149d518aSMauro Carvalho Chehab {
819149d518aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
820ad0abbf1SMauro Carvalho Chehab 	int rc, val;
821149d518aSMauro Carvalho Chehab 
822149d518aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
823149d518aSMauro Carvalho Chehab 
8244f62a20dSMauro Carvalho Chehab 	if (layer >= NUM_LAYERS)
825149d518aSMauro Carvalho Chehab 		return -EINVAL;
826149d518aSMauro Carvalho Chehab 
827149d518aSMauro Carvalho Chehab 	/* Check if the BER measures are already available */
828149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x54);
829149d518aSMauro Carvalho Chehab 	if (rc < 0)
830149d518aSMauro Carvalho Chehab 		return rc;
831149d518aSMauro Carvalho Chehab 
832149d518aSMauro Carvalho Chehab 	/* Check if data is available for that layer */
833149d518aSMauro Carvalho Chehab 	if (!(rc & (1 << layer))) {
834149d518aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
835ad0abbf1SMauro Carvalho Chehab 			"%s: preBER for layer %c is not available yet.\n",
836149d518aSMauro Carvalho Chehab 			__func__, 'A' + layer);
837149d518aSMauro Carvalho Chehab 		return -EBUSY;
838149d518aSMauro Carvalho Chehab 	}
839149d518aSMauro Carvalho Chehab 
840149d518aSMauro Carvalho Chehab 	/* Read Bit Error Count */
841149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x55 + layer * 3);
842149d518aSMauro Carvalho Chehab 	if (rc < 0)
843149d518aSMauro Carvalho Chehab 		return rc;
844149d518aSMauro Carvalho Chehab 	*error = rc << 16;
845149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x56 + layer * 3);
846149d518aSMauro Carvalho Chehab 	if (rc < 0)
847149d518aSMauro Carvalho Chehab 		return rc;
848149d518aSMauro Carvalho Chehab 	*error |= rc << 8;
849149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x57 + layer * 3);
850149d518aSMauro Carvalho Chehab 	if (rc < 0)
851149d518aSMauro Carvalho Chehab 		return rc;
852149d518aSMauro Carvalho Chehab 	*error |= rc;
853149d518aSMauro Carvalho Chehab 
854149d518aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
855149d518aSMauro Carvalho Chehab 		"%s: bit error before Viterbi for layer %c: %d.\n",
856149d518aSMauro Carvalho Chehab 		__func__, 'A' + layer, *error);
857149d518aSMauro Carvalho Chehab 
858149d518aSMauro Carvalho Chehab 	/* Read Bit Count */
859149d518aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
860149d518aSMauro Carvalho Chehab 	if (rc < 0)
861149d518aSMauro Carvalho Chehab 		return rc;
862149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
863149d518aSMauro Carvalho Chehab 	if (rc < 0)
864149d518aSMauro Carvalho Chehab 		return rc;
865149d518aSMauro Carvalho Chehab 	*count = rc << 16;
866149d518aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
867149d518aSMauro Carvalho Chehab 	if (rc < 0)
868149d518aSMauro Carvalho Chehab 		return rc;
869149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
870149d518aSMauro Carvalho Chehab 	if (rc < 0)
871149d518aSMauro Carvalho Chehab 		return rc;
872149d518aSMauro Carvalho Chehab 	*count |= rc << 8;
873149d518aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
874149d518aSMauro Carvalho Chehab 	if (rc < 0)
875149d518aSMauro Carvalho Chehab 		return rc;
876149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
877149d518aSMauro Carvalho Chehab 	if (rc < 0)
878149d518aSMauro Carvalho Chehab 		return rc;
879149d518aSMauro Carvalho Chehab 	*count |= rc;
880149d518aSMauro Carvalho Chehab 
881149d518aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
882149d518aSMauro Carvalho Chehab 		"%s: bit count before Viterbi for layer %c: %d.\n",
883149d518aSMauro Carvalho Chehab 		__func__, 'A' + layer, *count);
884149d518aSMauro Carvalho Chehab 
885149d518aSMauro Carvalho Chehab 
886d01a8ee3SMauro Carvalho Chehab 	/*
887d01a8ee3SMauro Carvalho Chehab 	 * As we get TMCC data from the frontend, we can better estimate the
888d01a8ee3SMauro Carvalho Chehab 	 * BER bit counters, in order to do the BER measure during a longer
889d01a8ee3SMauro Carvalho Chehab 	 * time. Use those data, if available, to update the bit count
890d01a8ee3SMauro Carvalho Chehab 	 * measure.
891d01a8ee3SMauro Carvalho Chehab 	 */
892d01a8ee3SMauro Carvalho Chehab 
893d01a8ee3SMauro Carvalho Chehab 	if (state->estimated_rate[layer]
894d01a8ee3SMauro Carvalho Chehab 	    && state->estimated_rate[layer] != *count) {
895d01a8ee3SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
896ad0abbf1SMauro Carvalho Chehab 			"%s: updating layer %c preBER counter to %d.\n",
897d01a8ee3SMauro Carvalho Chehab 			__func__, 'A' + layer, state->estimated_rate[layer]);
898ad0abbf1SMauro Carvalho Chehab 
899ad0abbf1SMauro Carvalho Chehab 		/* Turn off BER before Viterbi */
900ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x52, 0x00);
901ad0abbf1SMauro Carvalho Chehab 
902ad0abbf1SMauro Carvalho Chehab 		/* Update counter for this layer */
903d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
904d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
905d01a8ee3SMauro Carvalho Chehab 			return rc;
906d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51,
907d01a8ee3SMauro Carvalho Chehab 				       state->estimated_rate[layer] >> 16);
908d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
909d01a8ee3SMauro Carvalho Chehab 			return rc;
910d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
911d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
912d01a8ee3SMauro Carvalho Chehab 			return rc;
913d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51,
914d01a8ee3SMauro Carvalho Chehab 				       state->estimated_rate[layer] >> 8);
915d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
916d01a8ee3SMauro Carvalho Chehab 			return rc;
917d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
918d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
919d01a8ee3SMauro Carvalho Chehab 			return rc;
920d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51,
921d01a8ee3SMauro Carvalho Chehab 				       state->estimated_rate[layer]);
922d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
923d01a8ee3SMauro Carvalho Chehab 			return rc;
924ad0abbf1SMauro Carvalho Chehab 
925ad0abbf1SMauro Carvalho Chehab 		/* Turn on BER before Viterbi */
926ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x52, 0x01);
927ad0abbf1SMauro Carvalho Chehab 
928ad0abbf1SMauro Carvalho Chehab 		/* Reset all preBER counters */
929ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, 0x00);
930ad0abbf1SMauro Carvalho Chehab 		if (rc < 0)
931ad0abbf1SMauro Carvalho Chehab 			return rc;
932ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, 0x07);
933ad0abbf1SMauro Carvalho Chehab 	} else {
934ad0abbf1SMauro Carvalho Chehab 		/* Reset counter to collect new data */
935ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x53);
936ad0abbf1SMauro Carvalho Chehab 		if (rc < 0)
937ad0abbf1SMauro Carvalho Chehab 			return rc;
938ad0abbf1SMauro Carvalho Chehab 		val = rc;
939ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
940ad0abbf1SMauro Carvalho Chehab 		if (rc < 0)
941ad0abbf1SMauro Carvalho Chehab 			return rc;
942ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
943d01a8ee3SMauro Carvalho Chehab 	}
944d01a8ee3SMauro Carvalho Chehab 
945d9b6f08aSMauro Carvalho Chehab 	return rc;
946d9b6f08aSMauro Carvalho Chehab }
947d01a8ee3SMauro Carvalho Chehab 
948d9b6f08aSMauro Carvalho Chehab static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
949d9b6f08aSMauro Carvalho Chehab 				 unsigned layer,
950d9b6f08aSMauro Carvalho Chehab 				  u32 *error, u32 *count)
951d9b6f08aSMauro Carvalho Chehab {
952d9b6f08aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
953d9b6f08aSMauro Carvalho Chehab 	u32 counter, collect_rate;
954d9b6f08aSMauro Carvalho Chehab 	int rc, val;
955d9b6f08aSMauro Carvalho Chehab 
956d9b6f08aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
957d9b6f08aSMauro Carvalho Chehab 
9584f62a20dSMauro Carvalho Chehab 	if (layer >= NUM_LAYERS)
959d9b6f08aSMauro Carvalho Chehab 		return -EINVAL;
960d9b6f08aSMauro Carvalho Chehab 
961d9b6f08aSMauro Carvalho Chehab 	/* Check if the BER measures are already available */
962d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x60);
963d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
964d9b6f08aSMauro Carvalho Chehab 		return rc;
965d9b6f08aSMauro Carvalho Chehab 
966d9b6f08aSMauro Carvalho Chehab 	/* Check if data is available for that layer */
967d9b6f08aSMauro Carvalho Chehab 	if (!(rc & (1 << layer))) {
968d9b6f08aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
969d9b6f08aSMauro Carvalho Chehab 			"%s: post BER for layer %c is not available yet.\n",
970d9b6f08aSMauro Carvalho Chehab 			__func__, 'A' + layer);
971d9b6f08aSMauro Carvalho Chehab 		return -EBUSY;
972d9b6f08aSMauro Carvalho Chehab 	}
973d9b6f08aSMauro Carvalho Chehab 
974d9b6f08aSMauro Carvalho Chehab 	/* Read Bit Error Count */
975d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x64 + layer * 3);
976d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
977d9b6f08aSMauro Carvalho Chehab 		return rc;
978d9b6f08aSMauro Carvalho Chehab 	*error = rc << 16;
979d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x65 + layer * 3);
980d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
981d9b6f08aSMauro Carvalho Chehab 		return rc;
982d9b6f08aSMauro Carvalho Chehab 	*error |= rc << 8;
983d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x66 + layer * 3);
984d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
985d9b6f08aSMauro Carvalho Chehab 		return rc;
986d9b6f08aSMauro Carvalho Chehab 	*error |= rc;
987d9b6f08aSMauro Carvalho Chehab 
988d9b6f08aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
989d9b6f08aSMauro Carvalho Chehab 		"%s: post bit error for layer %c: %d.\n",
990d9b6f08aSMauro Carvalho Chehab 		__func__, 'A' + layer, *error);
991d9b6f08aSMauro Carvalho Chehab 
992d9b6f08aSMauro Carvalho Chehab 	/* Read Bit Count */
993d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
994d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
995d9b6f08aSMauro Carvalho Chehab 		return rc;
996d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
997d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
998d9b6f08aSMauro Carvalho Chehab 		return rc;
999d9b6f08aSMauro Carvalho Chehab 	counter = rc << 8;
1000d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1001d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
1002d9b6f08aSMauro Carvalho Chehab 		return rc;
1003d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1004d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
1005d9b6f08aSMauro Carvalho Chehab 		return rc;
1006d9b6f08aSMauro Carvalho Chehab 	counter |= rc;
1007d9b6f08aSMauro Carvalho Chehab 	*count = counter * 204 * 8;
1008d9b6f08aSMauro Carvalho Chehab 
1009d9b6f08aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
1010d9b6f08aSMauro Carvalho Chehab 		"%s: post bit count for layer %c: %d.\n",
1011d9b6f08aSMauro Carvalho Chehab 		__func__, 'A' + layer, *count);
1012d9b6f08aSMauro Carvalho Chehab 
1013d9b6f08aSMauro Carvalho Chehab 	/*
1014d9b6f08aSMauro Carvalho Chehab 	 * As we get TMCC data from the frontend, we can better estimate the
1015d9b6f08aSMauro Carvalho Chehab 	 * BER bit counters, in order to do the BER measure during a longer
1016d9b6f08aSMauro Carvalho Chehab 	 * time. Use those data, if available, to update the bit count
1017d9b6f08aSMauro Carvalho Chehab 	 * measure.
1018d9b6f08aSMauro Carvalho Chehab 	 */
1019d9b6f08aSMauro Carvalho Chehab 
1020d9b6f08aSMauro Carvalho Chehab 	if (!state->estimated_rate[layer])
1021d9b6f08aSMauro Carvalho Chehab 		goto reset_measurement;
1022d9b6f08aSMauro Carvalho Chehab 
1023d9b6f08aSMauro Carvalho Chehab 	collect_rate = state->estimated_rate[layer] / 204 / 8;
1024d9b6f08aSMauro Carvalho Chehab 	if (collect_rate < 32)
1025d9b6f08aSMauro Carvalho Chehab 		collect_rate = 32;
1026d9b6f08aSMauro Carvalho Chehab 	if (collect_rate > 65535)
1027d9b6f08aSMauro Carvalho Chehab 		collect_rate = 65535;
1028d9b6f08aSMauro Carvalho Chehab 	if (collect_rate != counter) {
1029d9b6f08aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
1030d9b6f08aSMauro Carvalho Chehab 			"%s: updating postBER counter on layer %c to %d.\n",
1031d9b6f08aSMauro Carvalho Chehab 			__func__, 'A' + layer, collect_rate);
1032d9b6f08aSMauro Carvalho Chehab 
1033d9b6f08aSMauro Carvalho Chehab 		/* Turn off BER after Viterbi */
1034d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5e, 0x00);
1035d9b6f08aSMauro Carvalho Chehab 
1036d9b6f08aSMauro Carvalho Chehab 		/* Update counter for this layer */
1037d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1038d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1039d9b6f08aSMauro Carvalho Chehab 			return rc;
1040d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1041d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1042d9b6f08aSMauro Carvalho Chehab 			return rc;
1043d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1044d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1045d9b6f08aSMauro Carvalho Chehab 			return rc;
1046d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1047d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1048d9b6f08aSMauro Carvalho Chehab 			return rc;
1049d9b6f08aSMauro Carvalho Chehab 
1050d9b6f08aSMauro Carvalho Chehab 		/* Turn on BER after Viterbi */
1051d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5e, 0x07);
1052d9b6f08aSMauro Carvalho Chehab 
1053d9b6f08aSMauro Carvalho Chehab 		/* Reset all preBER counters */
1054d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5f, 0x00);
1055d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1056d9b6f08aSMauro Carvalho Chehab 			return rc;
1057d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5f, 0x07);
1058d9b6f08aSMauro Carvalho Chehab 
1059d9b6f08aSMauro Carvalho Chehab 		return rc;
1060d9b6f08aSMauro Carvalho Chehab 	}
1061d9b6f08aSMauro Carvalho Chehab 
1062d9b6f08aSMauro Carvalho Chehab reset_measurement:
1063149d518aSMauro Carvalho Chehab 	/* Reset counter to collect new data */
1064ad0abbf1SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x5f);
1065149d518aSMauro Carvalho Chehab 	if (rc < 0)
1066149d518aSMauro Carvalho Chehab 		return rc;
1067ad0abbf1SMauro Carvalho Chehab 	val = rc;
1068ad0abbf1SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1069ad0abbf1SMauro Carvalho Chehab 	if (rc < 0)
1070ad0abbf1SMauro Carvalho Chehab 		return rc;
1071d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
1072149d518aSMauro Carvalho Chehab 
1073ad0abbf1SMauro Carvalho Chehab 	return rc;
1074149d518aSMauro Carvalho Chehab }
1075149d518aSMauro Carvalho Chehab 
1076593ae89aSMauro Carvalho Chehab static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1077593ae89aSMauro Carvalho Chehab 			    unsigned layer,
1078593ae89aSMauro Carvalho Chehab 			    u32 *error, u32 *count)
1079593ae89aSMauro Carvalho Chehab {
1080593ae89aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1081313cf4efSMauro Carvalho Chehab 	int rc, val;
1082593ae89aSMauro Carvalho Chehab 	u32 collect_rate;
1083593ae89aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1084593ae89aSMauro Carvalho Chehab 
10854f62a20dSMauro Carvalho Chehab 	if (layer >= NUM_LAYERS)
1086593ae89aSMauro Carvalho Chehab 		return -EINVAL;
1087593ae89aSMauro Carvalho Chehab 
1088593ae89aSMauro Carvalho Chehab 	/* Check if the PER measures are already available */
1089593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb8);
1090593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1091593ae89aSMauro Carvalho Chehab 		return rc;
1092593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1093593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1094593ae89aSMauro Carvalho Chehab 		return rc;
1095593ae89aSMauro Carvalho Chehab 
1096593ae89aSMauro Carvalho Chehab 	/* Check if data is available for that layer */
1097593ae89aSMauro Carvalho Chehab 
1098593ae89aSMauro Carvalho Chehab 	if (!(rc & (1 << layer))) {
1099593ae89aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
1100593ae89aSMauro Carvalho Chehab 			"%s: block counts for layer %c aren't available yet.\n",
1101593ae89aSMauro Carvalho Chehab 			__func__, 'A' + layer);
1102593ae89aSMauro Carvalho Chehab 		return -EBUSY;
1103593ae89aSMauro Carvalho Chehab 	}
1104593ae89aSMauro Carvalho Chehab 
1105593ae89aSMauro Carvalho Chehab 	/* Read Packet error Count */
1106593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1107593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1108593ae89aSMauro Carvalho Chehab 		return rc;
1109593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1110593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1111593ae89aSMauro Carvalho Chehab 		return rc;
1112593ae89aSMauro Carvalho Chehab 	*error = rc << 8;
1113593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1114593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1115593ae89aSMauro Carvalho Chehab 		return rc;
1116593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1117593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1118593ae89aSMauro Carvalho Chehab 		return rc;
1119593ae89aSMauro Carvalho Chehab 	*error |= rc;
1120d56e326fSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
1121593ae89aSMauro Carvalho Chehab 		__func__, 'A' + layer, *error);
1122593ae89aSMauro Carvalho Chehab 
1123593ae89aSMauro Carvalho Chehab 	/* Read Bit Count */
1124593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1125593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1126593ae89aSMauro Carvalho Chehab 		return rc;
1127593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1128593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1129593ae89aSMauro Carvalho Chehab 		return rc;
1130593ae89aSMauro Carvalho Chehab 	*count = rc << 8;
1131593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1132593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1133593ae89aSMauro Carvalho Chehab 		return rc;
1134593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1135593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1136593ae89aSMauro Carvalho Chehab 		return rc;
1137593ae89aSMauro Carvalho Chehab 	*count |= rc;
1138593ae89aSMauro Carvalho Chehab 
1139593ae89aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
1140593ae89aSMauro Carvalho Chehab 		"%s: block count for layer %c: %d.\n",
1141593ae89aSMauro Carvalho Chehab 		__func__, 'A' + layer, *count);
1142593ae89aSMauro Carvalho Chehab 
1143593ae89aSMauro Carvalho Chehab 	/*
1144593ae89aSMauro Carvalho Chehab 	 * As we get TMCC data from the frontend, we can better estimate the
1145593ae89aSMauro Carvalho Chehab 	 * BER bit counters, in order to do the BER measure during a longer
1146593ae89aSMauro Carvalho Chehab 	 * time. Use those data, if available, to update the bit count
1147593ae89aSMauro Carvalho Chehab 	 * measure.
1148593ae89aSMauro Carvalho Chehab 	 */
1149593ae89aSMauro Carvalho Chehab 
1150593ae89aSMauro Carvalho Chehab 	if (!state->estimated_rate[layer])
1151593ae89aSMauro Carvalho Chehab 		goto reset_measurement;
1152593ae89aSMauro Carvalho Chehab 
1153593ae89aSMauro Carvalho Chehab 	collect_rate = state->estimated_rate[layer] / 204 / 8;
1154593ae89aSMauro Carvalho Chehab 	if (collect_rate < 32)
1155593ae89aSMauro Carvalho Chehab 		collect_rate = 32;
1156593ae89aSMauro Carvalho Chehab 	if (collect_rate > 65535)
1157593ae89aSMauro Carvalho Chehab 		collect_rate = 65535;
1158593ae89aSMauro Carvalho Chehab 
1159593ae89aSMauro Carvalho Chehab 	if (collect_rate != *count) {
1160593ae89aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
1161593ae89aSMauro Carvalho Chehab 			"%s: updating PER counter on layer %c to %d.\n",
1162593ae89aSMauro Carvalho Chehab 			__func__, 'A' + layer, collect_rate);
1163313cf4efSMauro Carvalho Chehab 
1164313cf4efSMauro Carvalho Chehab 		/* Stop PER measurement */
1165313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb0);
1166313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1167313cf4efSMauro Carvalho Chehab 			return rc;
1168313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x00);
1169313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1170313cf4efSMauro Carvalho Chehab 			return rc;
1171313cf4efSMauro Carvalho Chehab 
1172313cf4efSMauro Carvalho Chehab 		/* Update this layer's counter */
1173593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1174593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1175593ae89aSMauro Carvalho Chehab 			return rc;
1176593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1177593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1178593ae89aSMauro Carvalho Chehab 			return rc;
1179593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1180593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1181593ae89aSMauro Carvalho Chehab 			return rc;
1182593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1183593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1184593ae89aSMauro Carvalho Chehab 			return rc;
1185313cf4efSMauro Carvalho Chehab 
1186313cf4efSMauro Carvalho Chehab 		/* start PER measurement */
1187313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb0);
1188313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1189313cf4efSMauro Carvalho Chehab 			return rc;
1190313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x07);
1191313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1192313cf4efSMauro Carvalho Chehab 			return rc;
1193313cf4efSMauro Carvalho Chehab 
1194313cf4efSMauro Carvalho Chehab 		/* Reset all counters to collect new data */
1195313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb1);
1196313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1197313cf4efSMauro Carvalho Chehab 			return rc;
1198313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x07);
1199313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1200313cf4efSMauro Carvalho Chehab 			return rc;
1201313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x00);
1202313cf4efSMauro Carvalho Chehab 
1203313cf4efSMauro Carvalho Chehab 		return rc;
1204593ae89aSMauro Carvalho Chehab 	}
1205593ae89aSMauro Carvalho Chehab 
1206593ae89aSMauro Carvalho Chehab reset_measurement:
1207593ae89aSMauro Carvalho Chehab 	/* Reset counter to collect new data */
1208593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb1);
1209593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1210593ae89aSMauro Carvalho Chehab 		return rc;
1211313cf4efSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1212593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1213593ae89aSMauro Carvalho Chehab 		return rc;
1214313cf4efSMauro Carvalho Chehab 	val = rc;
1215313cf4efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
1216593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1217593ae89aSMauro Carvalho Chehab 		return rc;
1218313cf4efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
1219593ae89aSMauro Carvalho Chehab 
1220313cf4efSMauro Carvalho Chehab 	return rc;
1221593ae89aSMauro Carvalho Chehab }
1222593ae89aSMauro Carvalho Chehab 
122325188bd0SMauro Carvalho Chehab struct linear_segments {
122425188bd0SMauro Carvalho Chehab 	unsigned x, y;
122525188bd0SMauro Carvalho Chehab };
122625188bd0SMauro Carvalho Chehab 
122725188bd0SMauro Carvalho Chehab /*
122825188bd0SMauro Carvalho Chehab  * All tables below return a dB/1000 measurement
122925188bd0SMauro Carvalho Chehab  */
123025188bd0SMauro Carvalho Chehab 
1231ce08131cSHans Verkuil static const struct linear_segments cnr_to_db_table[] = {
123225188bd0SMauro Carvalho Chehab 	{ 19648,     0},
123325188bd0SMauro Carvalho Chehab 	{ 18187,  1000},
123425188bd0SMauro Carvalho Chehab 	{ 16534,  2000},
123525188bd0SMauro Carvalho Chehab 	{ 14823,  3000},
123625188bd0SMauro Carvalho Chehab 	{ 13161,  4000},
123725188bd0SMauro Carvalho Chehab 	{ 11622,  5000},
123825188bd0SMauro Carvalho Chehab 	{ 10279,  6000},
123925188bd0SMauro Carvalho Chehab 	{  9089,  7000},
124025188bd0SMauro Carvalho Chehab 	{  8042,  8000},
124125188bd0SMauro Carvalho Chehab 	{  7137,  9000},
124225188bd0SMauro Carvalho Chehab 	{  6342, 10000},
124325188bd0SMauro Carvalho Chehab 	{  5641, 11000},
124425188bd0SMauro Carvalho Chehab 	{  5030, 12000},
124525188bd0SMauro Carvalho Chehab 	{  4474, 13000},
124625188bd0SMauro Carvalho Chehab 	{  3988, 14000},
124725188bd0SMauro Carvalho Chehab 	{  3556, 15000},
124825188bd0SMauro Carvalho Chehab 	{  3180, 16000},
124925188bd0SMauro Carvalho Chehab 	{  2841, 17000},
125025188bd0SMauro Carvalho Chehab 	{  2541, 18000},
125125188bd0SMauro Carvalho Chehab 	{  2276, 19000},
125225188bd0SMauro Carvalho Chehab 	{  2038, 20000},
125325188bd0SMauro Carvalho Chehab 	{  1800, 21000},
125425188bd0SMauro Carvalho Chehab 	{  1625, 22000},
125525188bd0SMauro Carvalho Chehab 	{  1462, 23000},
125625188bd0SMauro Carvalho Chehab 	{  1324, 24000},
125725188bd0SMauro Carvalho Chehab 	{  1175, 25000},
125825188bd0SMauro Carvalho Chehab 	{  1063, 26000},
125925188bd0SMauro Carvalho Chehab 	{   980, 27000},
126025188bd0SMauro Carvalho Chehab 	{   907, 28000},
126125188bd0SMauro Carvalho Chehab 	{   840, 29000},
126225188bd0SMauro Carvalho Chehab 	{   788, 30000},
126325188bd0SMauro Carvalho Chehab };
126425188bd0SMauro Carvalho Chehab 
1265ce08131cSHans Verkuil static const struct linear_segments cnr_64qam_table[] = {
126625188bd0SMauro Carvalho Chehab 	{ 3922688,     0},
126725188bd0SMauro Carvalho Chehab 	{ 3920384,  1000},
126825188bd0SMauro Carvalho Chehab 	{ 3902720,  2000},
126925188bd0SMauro Carvalho Chehab 	{ 3894784,  3000},
127025188bd0SMauro Carvalho Chehab 	{ 3882496,  4000},
127125188bd0SMauro Carvalho Chehab 	{ 3872768,  5000},
127225188bd0SMauro Carvalho Chehab 	{ 3858944,  6000},
127325188bd0SMauro Carvalho Chehab 	{ 3851520,  7000},
127425188bd0SMauro Carvalho Chehab 	{ 3838976,  8000},
127525188bd0SMauro Carvalho Chehab 	{ 3829248,  9000},
127625188bd0SMauro Carvalho Chehab 	{ 3818240, 10000},
127725188bd0SMauro Carvalho Chehab 	{ 3806976, 11000},
127825188bd0SMauro Carvalho Chehab 	{ 3791872, 12000},
127925188bd0SMauro Carvalho Chehab 	{ 3767040, 13000},
128025188bd0SMauro Carvalho Chehab 	{ 3720960, 14000},
128125188bd0SMauro Carvalho Chehab 	{ 3637504, 15000},
128225188bd0SMauro Carvalho Chehab 	{ 3498496, 16000},
128325188bd0SMauro Carvalho Chehab 	{ 3296000, 17000},
128425188bd0SMauro Carvalho Chehab 	{ 3031040, 18000},
128525188bd0SMauro Carvalho Chehab 	{ 2715392, 19000},
128625188bd0SMauro Carvalho Chehab 	{ 2362624, 20000},
128725188bd0SMauro Carvalho Chehab 	{ 1963264, 21000},
128825188bd0SMauro Carvalho Chehab 	{ 1649664, 22000},
128925188bd0SMauro Carvalho Chehab 	{ 1366784, 23000},
129025188bd0SMauro Carvalho Chehab 	{ 1120768, 24000},
129125188bd0SMauro Carvalho Chehab 	{  890880, 25000},
129225188bd0SMauro Carvalho Chehab 	{  723456, 26000},
129325188bd0SMauro Carvalho Chehab 	{  612096, 27000},
129425188bd0SMauro Carvalho Chehab 	{  518912, 28000},
129525188bd0SMauro Carvalho Chehab 	{  448256, 29000},
129625188bd0SMauro Carvalho Chehab 	{  388864, 30000},
129725188bd0SMauro Carvalho Chehab };
129825188bd0SMauro Carvalho Chehab 
1299ce08131cSHans Verkuil static const struct linear_segments cnr_16qam_table[] = {
130025188bd0SMauro Carvalho Chehab 	{ 5314816,     0},
130125188bd0SMauro Carvalho Chehab 	{ 5219072,  1000},
130225188bd0SMauro Carvalho Chehab 	{ 5118720,  2000},
130325188bd0SMauro Carvalho Chehab 	{ 4998912,  3000},
130425188bd0SMauro Carvalho Chehab 	{ 4875520,  4000},
130525188bd0SMauro Carvalho Chehab 	{ 4736000,  5000},
130625188bd0SMauro Carvalho Chehab 	{ 4604160,  6000},
130725188bd0SMauro Carvalho Chehab 	{ 4458752,  7000},
130825188bd0SMauro Carvalho Chehab 	{ 4300288,  8000},
130925188bd0SMauro Carvalho Chehab 	{ 4092928,  9000},
131025188bd0SMauro Carvalho Chehab 	{ 3836160, 10000},
131125188bd0SMauro Carvalho Chehab 	{ 3521024, 11000},
131225188bd0SMauro Carvalho Chehab 	{ 3155968, 12000},
131325188bd0SMauro Carvalho Chehab 	{ 2756864, 13000},
131425188bd0SMauro Carvalho Chehab 	{ 2347008, 14000},
131525188bd0SMauro Carvalho Chehab 	{ 1955072, 15000},
131625188bd0SMauro Carvalho Chehab 	{ 1593600, 16000},
131725188bd0SMauro Carvalho Chehab 	{ 1297920, 17000},
131825188bd0SMauro Carvalho Chehab 	{ 1043968, 18000},
131925188bd0SMauro Carvalho Chehab 	{  839680, 19000},
132025188bd0SMauro Carvalho Chehab 	{  672256, 20000},
132125188bd0SMauro Carvalho Chehab 	{  523008, 21000},
132225188bd0SMauro Carvalho Chehab 	{  424704, 22000},
132325188bd0SMauro Carvalho Chehab 	{  345088, 23000},
132425188bd0SMauro Carvalho Chehab 	{  280064, 24000},
132525188bd0SMauro Carvalho Chehab 	{  221440, 25000},
132625188bd0SMauro Carvalho Chehab 	{  179712, 26000},
132725188bd0SMauro Carvalho Chehab 	{  151040, 27000},
132825188bd0SMauro Carvalho Chehab 	{  128512, 28000},
132925188bd0SMauro Carvalho Chehab 	{  110080, 29000},
133025188bd0SMauro Carvalho Chehab 	{   95744, 30000},
133125188bd0SMauro Carvalho Chehab };
133225188bd0SMauro Carvalho Chehab 
1333ce08131cSHans Verkuil static const struct linear_segments cnr_qpsk_table[] = {
133425188bd0SMauro Carvalho Chehab 	{ 2834176,     0},
133525188bd0SMauro Carvalho Chehab 	{ 2683648,  1000},
133625188bd0SMauro Carvalho Chehab 	{ 2536960,  2000},
133725188bd0SMauro Carvalho Chehab 	{ 2391808,  3000},
133825188bd0SMauro Carvalho Chehab 	{ 2133248,  4000},
133925188bd0SMauro Carvalho Chehab 	{ 1906176,  5000},
134025188bd0SMauro Carvalho Chehab 	{ 1666560,  6000},
134125188bd0SMauro Carvalho Chehab 	{ 1422080,  7000},
134225188bd0SMauro Carvalho Chehab 	{ 1189632,  8000},
134325188bd0SMauro Carvalho Chehab 	{  976384,  9000},
134425188bd0SMauro Carvalho Chehab 	{  790272, 10000},
134525188bd0SMauro Carvalho Chehab 	{  633344, 11000},
134625188bd0SMauro Carvalho Chehab 	{  505600, 12000},
134725188bd0SMauro Carvalho Chehab 	{  402944, 13000},
134825188bd0SMauro Carvalho Chehab 	{  320768, 14000},
134925188bd0SMauro Carvalho Chehab 	{  255488, 15000},
135025188bd0SMauro Carvalho Chehab 	{  204032, 16000},
135125188bd0SMauro Carvalho Chehab 	{  163072, 17000},
135225188bd0SMauro Carvalho Chehab 	{  130304, 18000},
135325188bd0SMauro Carvalho Chehab 	{  105216, 19000},
135425188bd0SMauro Carvalho Chehab 	{   83456, 20000},
135525188bd0SMauro Carvalho Chehab 	{   65024, 21000},
135625188bd0SMauro Carvalho Chehab 	{   52480, 22000},
135725188bd0SMauro Carvalho Chehab 	{   42752, 23000},
135825188bd0SMauro Carvalho Chehab 	{   34560, 24000},
135925188bd0SMauro Carvalho Chehab 	{   27136, 25000},
136025188bd0SMauro Carvalho Chehab 	{   22016, 26000},
136125188bd0SMauro Carvalho Chehab 	{   18432, 27000},
136225188bd0SMauro Carvalho Chehab 	{   15616, 28000},
136325188bd0SMauro Carvalho Chehab 	{   13312, 29000},
136425188bd0SMauro Carvalho Chehab 	{   11520, 30000},
136525188bd0SMauro Carvalho Chehab };
136625188bd0SMauro Carvalho Chehab 
1367ce08131cSHans Verkuil static u32 interpolate_value(u32 value, const struct linear_segments *segments,
136825188bd0SMauro Carvalho Chehab 			     unsigned len)
136925188bd0SMauro Carvalho Chehab {
137025188bd0SMauro Carvalho Chehab 	u64 tmp64;
137125188bd0SMauro Carvalho Chehab 	u32 dx, dy;
137225188bd0SMauro Carvalho Chehab 	int i, ret;
137325188bd0SMauro Carvalho Chehab 
137425188bd0SMauro Carvalho Chehab 	if (value >= segments[0].x)
137525188bd0SMauro Carvalho Chehab 		return segments[0].y;
137625188bd0SMauro Carvalho Chehab 	if (value < segments[len-1].x)
137725188bd0SMauro Carvalho Chehab 		return segments[len-1].y;
137825188bd0SMauro Carvalho Chehab 
137925188bd0SMauro Carvalho Chehab 	for (i = 1; i < len - 1; i++) {
138025188bd0SMauro Carvalho Chehab 		/* If value is identical, no need to interpolate */
138125188bd0SMauro Carvalho Chehab 		if (value == segments[i].x)
138225188bd0SMauro Carvalho Chehab 			return segments[i].y;
138325188bd0SMauro Carvalho Chehab 		if (value > segments[i].x)
138425188bd0SMauro Carvalho Chehab 			break;
138525188bd0SMauro Carvalho Chehab 	}
138625188bd0SMauro Carvalho Chehab 
138725188bd0SMauro Carvalho Chehab 	/* Linear interpolation between the two (x,y) points */
138825188bd0SMauro Carvalho Chehab 	dy = segments[i].y - segments[i - 1].y;
138925188bd0SMauro Carvalho Chehab 	dx = segments[i - 1].x - segments[i].x;
139025188bd0SMauro Carvalho Chehab 	tmp64 = value - segments[i].x;
139125188bd0SMauro Carvalho Chehab 	tmp64 *= dy;
139225188bd0SMauro Carvalho Chehab 	do_div(tmp64, dx);
139325188bd0SMauro Carvalho Chehab 	ret = segments[i].y - tmp64;
139425188bd0SMauro Carvalho Chehab 
139525188bd0SMauro Carvalho Chehab 	return ret;
139625188bd0SMauro Carvalho Chehab }
139725188bd0SMauro Carvalho Chehab 
139825188bd0SMauro Carvalho Chehab static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
139925188bd0SMauro Carvalho Chehab {
140025188bd0SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
140125188bd0SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
140225188bd0SMauro Carvalho Chehab 	u32 cnr_linear, cnr;
140325188bd0SMauro Carvalho Chehab 	int rc, val;
140425188bd0SMauro Carvalho Chehab 
140525188bd0SMauro Carvalho Chehab 	/* Check if CNR is available */
140625188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x45);
140725188bd0SMauro Carvalho Chehab 	if (rc < 0)
140825188bd0SMauro Carvalho Chehab 		return rc;
140925188bd0SMauro Carvalho Chehab 
141025188bd0SMauro Carvalho Chehab 	if (!(rc & 0x40)) {
1411d56e326fSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
141225188bd0SMauro Carvalho Chehab 			 __func__);
141325188bd0SMauro Carvalho Chehab 		return -EBUSY;
141425188bd0SMauro Carvalho Chehab 	}
141525188bd0SMauro Carvalho Chehab 	val = rc;
141625188bd0SMauro Carvalho Chehab 
141725188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x46);
141825188bd0SMauro Carvalho Chehab 	if (rc < 0)
141925188bd0SMauro Carvalho Chehab 		return rc;
142025188bd0SMauro Carvalho Chehab 	cnr_linear = rc << 8;
142125188bd0SMauro Carvalho Chehab 
142225188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x46);
142325188bd0SMauro Carvalho Chehab 	if (rc < 0)
142425188bd0SMauro Carvalho Chehab 		return rc;
142525188bd0SMauro Carvalho Chehab 	cnr_linear |= rc;
142625188bd0SMauro Carvalho Chehab 
142725188bd0SMauro Carvalho Chehab 	cnr = interpolate_value(cnr_linear,
142825188bd0SMauro Carvalho Chehab 				cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
142925188bd0SMauro Carvalho Chehab 
143025188bd0SMauro Carvalho Chehab 	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
143125188bd0SMauro Carvalho Chehab 	c->cnr.stat[0].svalue = cnr;
143225188bd0SMauro Carvalho Chehab 
143325188bd0SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
143425188bd0SMauro Carvalho Chehab 		__func__, cnr / 1000, cnr % 1000, cnr_linear);
143525188bd0SMauro Carvalho Chehab 
143625188bd0SMauro Carvalho Chehab 	/* CNR counter reset */
143725188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val | 0x10);
143825188bd0SMauro Carvalho Chehab 	if (rc < 0)
143925188bd0SMauro Carvalho Chehab 		return rc;
144025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
144125188bd0SMauro Carvalho Chehab 
144225188bd0SMauro Carvalho Chehab 	return rc;
144325188bd0SMauro Carvalho Chehab }
144425188bd0SMauro Carvalho Chehab 
1445593ae89aSMauro Carvalho Chehab static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
144625188bd0SMauro Carvalho Chehab {
144725188bd0SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
144825188bd0SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
144925188bd0SMauro Carvalho Chehab 	u32 mer, cnr;
14505cb88ca8SMauro Carvalho Chehab 	int rc, val, layer;
1451ce08131cSHans Verkuil 	const struct linear_segments *segs;
145225188bd0SMauro Carvalho Chehab 	unsigned segs_len;
145325188bd0SMauro Carvalho Chehab 
145425188bd0SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
145525188bd0SMauro Carvalho Chehab 
145625188bd0SMauro Carvalho Chehab 	/* Check if the measures are already available */
145725188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0x5b);
145825188bd0SMauro Carvalho Chehab 	if (rc < 0)
145925188bd0SMauro Carvalho Chehab 		return rc;
146025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
146125188bd0SMauro Carvalho Chehab 	if (rc < 0)
146225188bd0SMauro Carvalho Chehab 		return rc;
146325188bd0SMauro Carvalho Chehab 
146425188bd0SMauro Carvalho Chehab 	/* Check if data is available */
146525188bd0SMauro Carvalho Chehab 	if (!(rc & 0x01)) {
1466d56e326fSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
146725188bd0SMauro Carvalho Chehab 			"%s: MER measures aren't available yet.\n", __func__);
146825188bd0SMauro Carvalho Chehab 		return -EBUSY;
146925188bd0SMauro Carvalho Chehab 	}
147025188bd0SMauro Carvalho Chehab 
147125188bd0SMauro Carvalho Chehab 	/* Read all layers */
14725cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS; layer++) {
14735cb88ca8SMauro Carvalho Chehab 		if (!(c->isdbt_layer_enabled & (1 << layer))) {
14745cb88ca8SMauro Carvalho Chehab 			c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
147525188bd0SMauro Carvalho Chehab 			continue;
147625188bd0SMauro Carvalho Chehab 		}
147725188bd0SMauro Carvalho Chehab 
14785cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
147925188bd0SMauro Carvalho Chehab 		if (rc < 0)
148025188bd0SMauro Carvalho Chehab 			return rc;
148125188bd0SMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x51);
148225188bd0SMauro Carvalho Chehab 		if (rc < 0)
148325188bd0SMauro Carvalho Chehab 			return rc;
148425188bd0SMauro Carvalho Chehab 		mer = rc << 16;
14855cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0x53 + layer * 3);
148625188bd0SMauro Carvalho Chehab 		if (rc < 0)
148725188bd0SMauro Carvalho Chehab 			return rc;
148825188bd0SMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x51);
148925188bd0SMauro Carvalho Chehab 		if (rc < 0)
149025188bd0SMauro Carvalho Chehab 			return rc;
149125188bd0SMauro Carvalho Chehab 		mer |= rc << 8;
14925cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0x54 + layer * 3);
149325188bd0SMauro Carvalho Chehab 		if (rc < 0)
149425188bd0SMauro Carvalho Chehab 			return rc;
149525188bd0SMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x51);
149625188bd0SMauro Carvalho Chehab 		if (rc < 0)
149725188bd0SMauro Carvalho Chehab 			return rc;
149825188bd0SMauro Carvalho Chehab 		mer |= rc;
149925188bd0SMauro Carvalho Chehab 
15005cb88ca8SMauro Carvalho Chehab 		switch (c->layer[layer].modulation) {
150125188bd0SMauro Carvalho Chehab 		case DQPSK:
150225188bd0SMauro Carvalho Chehab 		case QPSK:
150325188bd0SMauro Carvalho Chehab 			segs = cnr_qpsk_table;
150425188bd0SMauro Carvalho Chehab 			segs_len = ARRAY_SIZE(cnr_qpsk_table);
150525188bd0SMauro Carvalho Chehab 			break;
150625188bd0SMauro Carvalho Chehab 		case QAM_16:
150725188bd0SMauro Carvalho Chehab 			segs = cnr_16qam_table;
150825188bd0SMauro Carvalho Chehab 			segs_len = ARRAY_SIZE(cnr_16qam_table);
150925188bd0SMauro Carvalho Chehab 			break;
151025188bd0SMauro Carvalho Chehab 		default:
151125188bd0SMauro Carvalho Chehab 		case QAM_64:
151225188bd0SMauro Carvalho Chehab 			segs = cnr_64qam_table;
151325188bd0SMauro Carvalho Chehab 			segs_len = ARRAY_SIZE(cnr_64qam_table);
151425188bd0SMauro Carvalho Chehab 			break;
151525188bd0SMauro Carvalho Chehab 		}
151625188bd0SMauro Carvalho Chehab 		cnr = interpolate_value(mer, segs, segs_len);
151725188bd0SMauro Carvalho Chehab 
15185cb88ca8SMauro Carvalho Chehab 		c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
15195cb88ca8SMauro Carvalho Chehab 		c->cnr.stat[1 + layer].svalue = cnr;
152025188bd0SMauro Carvalho Chehab 
152125188bd0SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
152225188bd0SMauro Carvalho Chehab 			"%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
15235cb88ca8SMauro Carvalho Chehab 			__func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
152425188bd0SMauro Carvalho Chehab 
152525188bd0SMauro Carvalho Chehab 	}
152625188bd0SMauro Carvalho Chehab 
152725188bd0SMauro Carvalho Chehab 	/* Start a new MER measurement */
152825188bd0SMauro Carvalho Chehab 	/* MER counter reset */
152925188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0x50);
153025188bd0SMauro Carvalho Chehab 	if (rc < 0)
153125188bd0SMauro Carvalho Chehab 		return rc;
153225188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
153325188bd0SMauro Carvalho Chehab 	if (rc < 0)
153425188bd0SMauro Carvalho Chehab 		return rc;
153525188bd0SMauro Carvalho Chehab 	val = rc;
153625188bd0SMauro Carvalho Chehab 
153725188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val | 0x01);
153825188bd0SMauro Carvalho Chehab 	if (rc < 0)
153925188bd0SMauro Carvalho Chehab 		return rc;
154025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val & 0x06);
154125188bd0SMauro Carvalho Chehab 	if (rc < 0)
154225188bd0SMauro Carvalho Chehab 		return rc;
154325188bd0SMauro Carvalho Chehab 
154425188bd0SMauro Carvalho Chehab 	return 0;
154525188bd0SMauro Carvalho Chehab }
154625188bd0SMauro Carvalho Chehab 
154709b6d21eSMauro Carvalho Chehab static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
154809b6d21eSMauro Carvalho Chehab {
154909b6d21eSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
155009b6d21eSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
15515cb88ca8SMauro Carvalho Chehab 	int layer;
155209b6d21eSMauro Carvalho Chehab 
155309b6d21eSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
155409b6d21eSMauro Carvalho Chehab 
155509b6d21eSMauro Carvalho Chehab 	/* Fill the length of each status counter */
155609b6d21eSMauro Carvalho Chehab 
155709b6d21eSMauro Carvalho Chehab 	/* Only global stats */
155809b6d21eSMauro Carvalho Chehab 	c->strength.len = 1;
155909b6d21eSMauro Carvalho Chehab 
156009b6d21eSMauro Carvalho Chehab 	/* Per-layer stats - 3 layers + global */
15614f62a20dSMauro Carvalho Chehab 	c->cnr.len = NUM_LAYERS + 1;
15624f62a20dSMauro Carvalho Chehab 	c->pre_bit_error.len = NUM_LAYERS + 1;
15634f62a20dSMauro Carvalho Chehab 	c->pre_bit_count.len = NUM_LAYERS + 1;
15644f62a20dSMauro Carvalho Chehab 	c->post_bit_error.len = NUM_LAYERS + 1;
15654f62a20dSMauro Carvalho Chehab 	c->post_bit_count.len = NUM_LAYERS + 1;
15664f62a20dSMauro Carvalho Chehab 	c->block_error.len = NUM_LAYERS + 1;
15674f62a20dSMauro Carvalho Chehab 	c->block_count.len = NUM_LAYERS + 1;
156809b6d21eSMauro Carvalho Chehab 
156909b6d21eSMauro Carvalho Chehab 	/* Signal is always available */
157009b6d21eSMauro Carvalho Chehab 	c->strength.stat[0].scale = FE_SCALE_RELATIVE;
157109b6d21eSMauro Carvalho Chehab 	c->strength.stat[0].uvalue = 0;
157209b6d21eSMauro Carvalho Chehab 
157309b6d21eSMauro Carvalho Chehab 	/* Put all of them at FE_SCALE_NOT_AVAILABLE */
15745cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
15755cb88ca8SMauro Carvalho Chehab 		c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15765cb88ca8SMauro Carvalho Chehab 		c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15775cb88ca8SMauro Carvalho Chehab 		c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15785cb88ca8SMauro Carvalho Chehab 		c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15795cb88ca8SMauro Carvalho Chehab 		c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15805cb88ca8SMauro Carvalho Chehab 		c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15815cb88ca8SMauro Carvalho Chehab 		c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
158209b6d21eSMauro Carvalho Chehab 	}
158309b6d21eSMauro Carvalho Chehab }
158409b6d21eSMauro Carvalho Chehab 
158515b1c5a0SMauro Carvalho Chehab static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
1586149d518aSMauro Carvalho Chehab {
1587149d518aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1588149d518aSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
15895cb88ca8SMauro Carvalho Chehab 	int rc = 0, layer;
1590149d518aSMauro Carvalho Chehab 	u32 bit_error = 0, bit_count = 0;
1591149d518aSMauro Carvalho Chehab 	u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
1592d9b6f08aSMauro Carvalho Chehab 	u32 t_post_bit_error = 0, t_post_bit_count = 0;
1593593ae89aSMauro Carvalho Chehab 	u32 block_error = 0, block_count = 0;
1594593ae89aSMauro Carvalho Chehab 	u32 t_block_error = 0, t_block_count = 0;
1595d9b6f08aSMauro Carvalho Chehab 	int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1596d9b6f08aSMauro Carvalho Chehab 	int per_layers = 0;
1597149d518aSMauro Carvalho Chehab 
159825188bd0SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
159925188bd0SMauro Carvalho Chehab 
160025188bd0SMauro Carvalho Chehab 	mb86a20s_get_main_CNR(fe);
160125188bd0SMauro Carvalho Chehab 
1602149d518aSMauro Carvalho Chehab 	/* Get per-layer stats */
1603593ae89aSMauro Carvalho Chehab 	mb86a20s_get_blk_error_layer_CNR(fe);
160425188bd0SMauro Carvalho Chehab 
160515b1c5a0SMauro Carvalho Chehab 	/*
160615b1c5a0SMauro Carvalho Chehab 	 * At state 7, only CNR is available
160715b1c5a0SMauro Carvalho Chehab 	 * For BER measures, state=9 is required
160815b1c5a0SMauro Carvalho Chehab 	 * FIXME: we may get MER measures with state=8
160915b1c5a0SMauro Carvalho Chehab 	 */
161015b1c5a0SMauro Carvalho Chehab 	if (status_nr < 9)
161115b1c5a0SMauro Carvalho Chehab 		return 0;
161215b1c5a0SMauro Carvalho Chehab 
16135cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS; layer++) {
16145cb88ca8SMauro Carvalho Chehab 		if (c->isdbt_layer_enabled & (1 << layer)) {
1615149d518aSMauro Carvalho Chehab 			/* Layer is active and has rc segments */
1616149d518aSMauro Carvalho Chehab 			active_layers++;
1617149d518aSMauro Carvalho Chehab 
1618149d518aSMauro Carvalho Chehab 			/* Handle BER before vterbi */
16195cb88ca8SMauro Carvalho Chehab 			rc = mb86a20s_get_pre_ber(fe, layer,
1620ad0abbf1SMauro Carvalho Chehab 						  &bit_error, &bit_count);
1621149d518aSMauro Carvalho Chehab 			if (rc >= 0) {
16225cb88ca8SMauro Carvalho Chehab 				c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
16235cb88ca8SMauro Carvalho Chehab 				c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
16245cb88ca8SMauro Carvalho Chehab 				c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
16255cb88ca8SMauro Carvalho Chehab 				c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
1626149d518aSMauro Carvalho Chehab 			} else if (rc != -EBUSY) {
1627149d518aSMauro Carvalho Chehab 				/*
1628149d518aSMauro Carvalho Chehab 					* If an I/O error happened,
1629149d518aSMauro Carvalho Chehab 					* measures are now unavailable
1630149d518aSMauro Carvalho Chehab 					*/
16315cb88ca8SMauro Carvalho Chehab 				c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
16325cb88ca8SMauro Carvalho Chehab 				c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1633149d518aSMauro Carvalho Chehab 				dev_err(&state->i2c->dev,
1634149d518aSMauro Carvalho Chehab 					"%s: Can't get BER for layer %c (error %d).\n",
16355cb88ca8SMauro Carvalho Chehab 					__func__, 'A' + layer, rc);
1636149d518aSMauro Carvalho Chehab 			}
16375cb88ca8SMauro Carvalho Chehab 			if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1638d9b6f08aSMauro Carvalho Chehab 				pre_ber_layers++;
1639d9b6f08aSMauro Carvalho Chehab 
1640d9b6f08aSMauro Carvalho Chehab 			/* Handle BER post vterbi */
16415cb88ca8SMauro Carvalho Chehab 			rc = mb86a20s_get_post_ber(fe, layer,
1642d9b6f08aSMauro Carvalho Chehab 						   &bit_error, &bit_count);
1643d9b6f08aSMauro Carvalho Chehab 			if (rc >= 0) {
16445cb88ca8SMauro Carvalho Chehab 				c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
16455cb88ca8SMauro Carvalho Chehab 				c->post_bit_error.stat[1 + layer].uvalue += bit_error;
16465cb88ca8SMauro Carvalho Chehab 				c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
16475cb88ca8SMauro Carvalho Chehab 				c->post_bit_count.stat[1 + layer].uvalue += bit_count;
1648d9b6f08aSMauro Carvalho Chehab 			} else if (rc != -EBUSY) {
1649d9b6f08aSMauro Carvalho Chehab 				/*
1650d9b6f08aSMauro Carvalho Chehab 					* If an I/O error happened,
1651d9b6f08aSMauro Carvalho Chehab 					* measures are now unavailable
1652d9b6f08aSMauro Carvalho Chehab 					*/
16535cb88ca8SMauro Carvalho Chehab 				c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
16545cb88ca8SMauro Carvalho Chehab 				c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1655d9b6f08aSMauro Carvalho Chehab 				dev_err(&state->i2c->dev,
1656d9b6f08aSMauro Carvalho Chehab 					"%s: Can't get BER for layer %c (error %d).\n",
16575cb88ca8SMauro Carvalho Chehab 					__func__, 'A' + layer, rc);
1658d9b6f08aSMauro Carvalho Chehab 			}
16595cb88ca8SMauro Carvalho Chehab 			if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1660d9b6f08aSMauro Carvalho Chehab 				post_ber_layers++;
1661149d518aSMauro Carvalho Chehab 
1662593ae89aSMauro Carvalho Chehab 			/* Handle Block errors for PER/UCB reports */
16635cb88ca8SMauro Carvalho Chehab 			rc = mb86a20s_get_blk_error(fe, layer,
1664593ae89aSMauro Carvalho Chehab 						&block_error,
1665593ae89aSMauro Carvalho Chehab 						&block_count);
1666593ae89aSMauro Carvalho Chehab 			if (rc >= 0) {
16675cb88ca8SMauro Carvalho Chehab 				c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
16685cb88ca8SMauro Carvalho Chehab 				c->block_error.stat[1 + layer].uvalue += block_error;
16695cb88ca8SMauro Carvalho Chehab 				c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
16705cb88ca8SMauro Carvalho Chehab 				c->block_count.stat[1 + layer].uvalue += block_count;
1671593ae89aSMauro Carvalho Chehab 			} else if (rc != -EBUSY) {
1672593ae89aSMauro Carvalho Chehab 				/*
1673593ae89aSMauro Carvalho Chehab 					* If an I/O error happened,
1674593ae89aSMauro Carvalho Chehab 					* measures are now unavailable
1675593ae89aSMauro Carvalho Chehab 					*/
16765cb88ca8SMauro Carvalho Chehab 				c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
16775cb88ca8SMauro Carvalho Chehab 				c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1678593ae89aSMauro Carvalho Chehab 				dev_err(&state->i2c->dev,
1679593ae89aSMauro Carvalho Chehab 					"%s: Can't get PER for layer %c (error %d).\n",
16805cb88ca8SMauro Carvalho Chehab 					__func__, 'A' + layer, rc);
1681593ae89aSMauro Carvalho Chehab 
1682593ae89aSMauro Carvalho Chehab 			}
16835cb88ca8SMauro Carvalho Chehab 			if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1684593ae89aSMauro Carvalho Chehab 				per_layers++;
1685593ae89aSMauro Carvalho Chehab 
1686d9b6f08aSMauro Carvalho Chehab 			/* Update total preBER */
16875cb88ca8SMauro Carvalho Chehab 			t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
16885cb88ca8SMauro Carvalho Chehab 			t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
1689593ae89aSMauro Carvalho Chehab 
1690d9b6f08aSMauro Carvalho Chehab 			/* Update total postBER */
16915cb88ca8SMauro Carvalho Chehab 			t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
16925cb88ca8SMauro Carvalho Chehab 			t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
1693d9b6f08aSMauro Carvalho Chehab 
1694593ae89aSMauro Carvalho Chehab 			/* Update total PER */
16955cb88ca8SMauro Carvalho Chehab 			t_block_error += c->block_error.stat[1 + layer].uvalue;
16965cb88ca8SMauro Carvalho Chehab 			t_block_count += c->block_count.stat[1 + layer].uvalue;
1697149d518aSMauro Carvalho Chehab 		}
1698149d518aSMauro Carvalho Chehab 	}
1699149d518aSMauro Carvalho Chehab 
1700149d518aSMauro Carvalho Chehab 	/*
1701149d518aSMauro Carvalho Chehab 	 * Start showing global count if at least one error count is
1702149d518aSMauro Carvalho Chehab 	 * available.
1703149d518aSMauro Carvalho Chehab 	 */
1704d9b6f08aSMauro Carvalho Chehab 	if (pre_ber_layers) {
1705149d518aSMauro Carvalho Chehab 		/*
1706149d518aSMauro Carvalho Chehab 		 * At least one per-layer BER measure was read. We can now
1707149d518aSMauro Carvalho Chehab 		 * calculate the total BER
1708149d518aSMauro Carvalho Chehab 		 *
1709149d518aSMauro Carvalho Chehab 		 * Total Bit Error/Count is calculated as the sum of the
1710149d518aSMauro Carvalho Chehab 		 * bit errors on all active layers.
1711149d518aSMauro Carvalho Chehab 		 */
1712149d518aSMauro Carvalho Chehab 		c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1713149d518aSMauro Carvalho Chehab 		c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1714149d518aSMauro Carvalho Chehab 		c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1715149d518aSMauro Carvalho Chehab 		c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
1716f67102c4SMauro Carvalho Chehab 	} else {
1717f67102c4SMauro Carvalho Chehab 		c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1718f67102c4SMauro Carvalho Chehab 		c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1719149d518aSMauro Carvalho Chehab 	}
1720149d518aSMauro Carvalho Chehab 
1721d9b6f08aSMauro Carvalho Chehab 	/*
1722d9b6f08aSMauro Carvalho Chehab 	 * Start showing global count if at least one error count is
1723d9b6f08aSMauro Carvalho Chehab 	 * available.
1724d9b6f08aSMauro Carvalho Chehab 	 */
1725d9b6f08aSMauro Carvalho Chehab 	if (post_ber_layers) {
1726d9b6f08aSMauro Carvalho Chehab 		/*
1727d9b6f08aSMauro Carvalho Chehab 		 * At least one per-layer BER measure was read. We can now
1728d9b6f08aSMauro Carvalho Chehab 		 * calculate the total BER
1729d9b6f08aSMauro Carvalho Chehab 		 *
1730d9b6f08aSMauro Carvalho Chehab 		 * Total Bit Error/Count is calculated as the sum of the
1731d9b6f08aSMauro Carvalho Chehab 		 * bit errors on all active layers.
1732d9b6f08aSMauro Carvalho Chehab 		 */
1733d9b6f08aSMauro Carvalho Chehab 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1734d9b6f08aSMauro Carvalho Chehab 		c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1735d9b6f08aSMauro Carvalho Chehab 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1736d9b6f08aSMauro Carvalho Chehab 		c->post_bit_count.stat[0].uvalue = t_post_bit_count;
1737f67102c4SMauro Carvalho Chehab 	} else {
1738f67102c4SMauro Carvalho Chehab 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1739f67102c4SMauro Carvalho Chehab 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1740d9b6f08aSMauro Carvalho Chehab 	}
1741d9b6f08aSMauro Carvalho Chehab 
1742593ae89aSMauro Carvalho Chehab 	if (per_layers) {
1743593ae89aSMauro Carvalho Chehab 		/*
1744593ae89aSMauro Carvalho Chehab 		 * At least one per-layer UCB measure was read. We can now
1745593ae89aSMauro Carvalho Chehab 		 * calculate the total UCB
1746593ae89aSMauro Carvalho Chehab 		 *
1747593ae89aSMauro Carvalho Chehab 		 * Total block Error/Count is calculated as the sum of the
1748593ae89aSMauro Carvalho Chehab 		 * block errors on all active layers.
1749593ae89aSMauro Carvalho Chehab 		 */
1750593ae89aSMauro Carvalho Chehab 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1751593ae89aSMauro Carvalho Chehab 		c->block_error.stat[0].uvalue = t_block_error;
1752593ae89aSMauro Carvalho Chehab 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1753593ae89aSMauro Carvalho Chehab 		c->block_count.stat[0].uvalue = t_block_count;
1754f67102c4SMauro Carvalho Chehab 	} else {
1755f67102c4SMauro Carvalho Chehab 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1756f67102c4SMauro Carvalho Chehab 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1757593ae89aSMauro Carvalho Chehab 	}
1758593ae89aSMauro Carvalho Chehab 
1759149d518aSMauro Carvalho Chehab 	return rc;
1760149d518aSMauro Carvalho Chehab }
176109b6d21eSMauro Carvalho Chehab 
176209b6d21eSMauro Carvalho Chehab /*
176309b6d21eSMauro Carvalho Chehab  * The functions below are called via DVB callbacks, so they need to
176409b6d21eSMauro Carvalho Chehab  * properly use the I2C gate control
176509b6d21eSMauro Carvalho Chehab  */
176609b6d21eSMauro Carvalho Chehab 
1767dd4493efSMauro Carvalho Chehab static int mb86a20s_initfe(struct dvb_frontend *fe)
1768dd4493efSMauro Carvalho Chehab {
1769dd4493efSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1770768e6dadSMauro Carvalho Chehab 	u64 pll;
17710e4bbeddSMauro Carvalho Chehab 	u32 fclk;
1772dd4493efSMauro Carvalho Chehab 	int rc;
177304fa725eSMauro Carvalho Chehab 	u8  regD5 = 1, reg71, reg09 = 0x3a;
1774dd4493efSMauro Carvalho Chehab 
1775f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1776dd4493efSMauro Carvalho Chehab 
1777dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1778dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
1779dd4493efSMauro Carvalho Chehab 
1780dd4493efSMauro Carvalho Chehab 	/* Initialize the frontend */
1781768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_init1);
1782dd4493efSMauro Carvalho Chehab 	if (rc < 0)
1783dd4493efSMauro Carvalho Chehab 		goto err;
1784dd4493efSMauro Carvalho Chehab 
178504fa725eSMauro Carvalho Chehab 	if (!state->inversion)
178604fa725eSMauro Carvalho Chehab 		reg09 |= 0x04;
178704fa725eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x09, reg09);
178804fa725eSMauro Carvalho Chehab 	if (rc < 0)
178904fa725eSMauro Carvalho Chehab 		goto err;
179004fa725eSMauro Carvalho Chehab 	if (!state->bw)
179104fa725eSMauro Carvalho Chehab 		reg71 = 1;
179204fa725eSMauro Carvalho Chehab 	else
179304fa725eSMauro Carvalho Chehab 		reg71 = 0;
179404fa725eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x39, reg71);
179504fa725eSMauro Carvalho Chehab 	if (rc < 0)
179604fa725eSMauro Carvalho Chehab 		goto err;
179704fa725eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x71, state->bw);
179804fa725eSMauro Carvalho Chehab 	if (rc < 0)
179904fa725eSMauro Carvalho Chehab 		goto err;
180004fa725eSMauro Carvalho Chehab 	if (state->subchannel) {
180104fa725eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x44, state->subchannel);
180204fa725eSMauro Carvalho Chehab 		if (rc < 0)
180304fa725eSMauro Carvalho Chehab 			goto err;
180404fa725eSMauro Carvalho Chehab 	}
180504fa725eSMauro Carvalho Chehab 
18060e4bbeddSMauro Carvalho Chehab 	fclk = state->config->fclk;
18070e4bbeddSMauro Carvalho Chehab 	if (!fclk)
18080e4bbeddSMauro Carvalho Chehab 		fclk = 32571428;
18090e4bbeddSMauro Carvalho Chehab 
1810768e6dadSMauro Carvalho Chehab 	/* Adjust IF frequency to match tuner */
1811768e6dadSMauro Carvalho Chehab 	if (fe->ops.tuner_ops.get_if_frequency)
1812768e6dadSMauro Carvalho Chehab 		fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1813768e6dadSMauro Carvalho Chehab 
1814768e6dadSMauro Carvalho Chehab 	if (!state->if_freq)
1815768e6dadSMauro Carvalho Chehab 		state->if_freq = 3300000;
1816768e6dadSMauro Carvalho Chehab 
18170e4bbeddSMauro Carvalho Chehab 	pll = (((u64)1) << 34) * state->if_freq;
18180e4bbeddSMauro Carvalho Chehab 	do_div(pll, 63 * fclk);
18190e4bbeddSMauro Carvalho Chehab 	pll = (1 << 25) - pll;
18200e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x28, 0x2a);
18210e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18220e4bbeddSMauro Carvalho Chehab 		goto err;
18230e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
18240e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18250e4bbeddSMauro Carvalho Chehab 		goto err;
18260e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
18270e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18280e4bbeddSMauro Carvalho Chehab 		goto err;
18290e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
18300e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18310e4bbeddSMauro Carvalho Chehab 		goto err;
18320e4bbeddSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
18330e4bbeddSMauro Carvalho Chehab 		__func__, fclk, state->if_freq, (long long)pll);
18340e4bbeddSMauro Carvalho Chehab 
1835768e6dadSMauro Carvalho Chehab 	/* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1836768e6dadSMauro Carvalho Chehab 	pll = state->if_freq * 1677721600L;
1837768e6dadSMauro Carvalho Chehab 	do_div(pll, 1628571429L);
1838768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x28, 0x20);
1839768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1840768e6dadSMauro Carvalho Chehab 		goto err;
1841768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1842768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1843768e6dadSMauro Carvalho Chehab 		goto err;
1844768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1845768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1846768e6dadSMauro Carvalho Chehab 		goto err;
1847768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1848768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1849768e6dadSMauro Carvalho Chehab 		goto err;
18500e4bbeddSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
1851768e6dadSMauro Carvalho Chehab 		__func__, state->if_freq, (long long)pll);
1852768e6dadSMauro Carvalho Chehab 
18539d32069fSMauro Carvalho Chehab 	if (!state->config->is_serial)
1854dd4493efSMauro Carvalho Chehab 		regD5 &= ~1;
1855dd4493efSMauro Carvalho Chehab 
1856dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xd5);
1857dd4493efSMauro Carvalho Chehab 	if (rc < 0)
1858dd4493efSMauro Carvalho Chehab 		goto err;
1859dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, regD5);
1860dd4493efSMauro Carvalho Chehab 	if (rc < 0)
1861dd4493efSMauro Carvalho Chehab 		goto err;
1862dd4493efSMauro Carvalho Chehab 
1863768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1864768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1865768e6dadSMauro Carvalho Chehab 		goto err;
1866768e6dadSMauro Carvalho Chehab 
1867768e6dadSMauro Carvalho Chehab 
1868dd4493efSMauro Carvalho Chehab err:
1869dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1870dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
1871dd4493efSMauro Carvalho Chehab 
1872dd4493efSMauro Carvalho Chehab 	if (rc < 0) {
1873dd4493efSMauro Carvalho Chehab 		state->need_init = true;
1874f66d81b5SMauro Carvalho Chehab 		dev_info(&state->i2c->dev,
1875f66d81b5SMauro Carvalho Chehab 			 "mb86a20s: Init failed. Will try again later\n");
1876dd4493efSMauro Carvalho Chehab 	} else {
1877dd4493efSMauro Carvalho Chehab 		state->need_init = false;
1878f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
1879dd4493efSMauro Carvalho Chehab 	}
1880dd4493efSMauro Carvalho Chehab 	return rc;
1881dd4493efSMauro Carvalho Chehab }
1882dd4493efSMauro Carvalho Chehab 
1883dd4493efSMauro Carvalho Chehab static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1884dd4493efSMauro Carvalho Chehab {
1885dd4493efSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1886dd4493efSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
188704fa725eSMauro Carvalho Chehab 	int rc, if_freq;
1888f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1889dd4493efSMauro Carvalho Chehab 
189004fa725eSMauro Carvalho Chehab 	if (!c->isdbt_layer_enabled)
189104fa725eSMauro Carvalho Chehab 		c->isdbt_layer_enabled = 7;
189204fa725eSMauro Carvalho Chehab 
189304fa725eSMauro Carvalho Chehab 	if (c->isdbt_layer_enabled == 1)
189404fa725eSMauro Carvalho Chehab 		state->bw = MB86A20S_1SEG;
189504fa725eSMauro Carvalho Chehab 	else if (c->isdbt_partial_reception)
189604fa725eSMauro Carvalho Chehab 		state->bw = MB86A20S_13SEG_PARTIAL;
189704fa725eSMauro Carvalho Chehab 	else
189804fa725eSMauro Carvalho Chehab 		state->bw = MB86A20S_13SEG;
189904fa725eSMauro Carvalho Chehab 
190004fa725eSMauro Carvalho Chehab 	if (c->inversion == INVERSION_ON)
190104fa725eSMauro Carvalho Chehab 		state->inversion = true;
190204fa725eSMauro Carvalho Chehab 	else
190304fa725eSMauro Carvalho Chehab 		state->inversion = false;
190404fa725eSMauro Carvalho Chehab 
190504fa725eSMauro Carvalho Chehab 	if (!c->isdbt_sb_mode) {
190604fa725eSMauro Carvalho Chehab 		state->subchannel = 0;
190704fa725eSMauro Carvalho Chehab 	} else {
190841c6e9ddSMauro Carvalho Chehab 		if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
190904fa725eSMauro Carvalho Chehab 			c->isdbt_sb_subchannel = 0;
191004fa725eSMauro Carvalho Chehab 
191104fa725eSMauro Carvalho Chehab 		state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
191204fa725eSMauro Carvalho Chehab 	}
191304fa725eSMauro Carvalho Chehab 
1914dd4493efSMauro Carvalho Chehab 	/*
1915dd4493efSMauro Carvalho Chehab 	 * Gate should already be opened, but it doesn't hurt to
1916dd4493efSMauro Carvalho Chehab 	 * double-check
1917dd4493efSMauro Carvalho Chehab 	 */
1918dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1919dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
1920dd4493efSMauro Carvalho Chehab 	fe->ops.tuner_ops.set_params(fe);
1921dd4493efSMauro Carvalho Chehab 
1922a78b41d5SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.get_if_frequency)
1923768e6dadSMauro Carvalho Chehab 		fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1924768e6dadSMauro Carvalho Chehab 
1925768e6dadSMauro Carvalho Chehab 	/*
1926dd4493efSMauro Carvalho Chehab 	 * Make it more reliable: if, for some reason, the initial
1927dd4493efSMauro Carvalho Chehab 	 * device initialization doesn't happen, initialize it when
1928dd4493efSMauro Carvalho Chehab 	 * a SBTVD parameters are adjusted.
1929dd4493efSMauro Carvalho Chehab 	 *
1930dd4493efSMauro Carvalho Chehab 	 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1931dd4493efSMauro Carvalho Chehab 	 * the agc callback logic is not called during DVB attach time,
1932dd4493efSMauro Carvalho Chehab 	 * causing mb86a20s to not be initialized with Kworld SBTVD.
1933dd4493efSMauro Carvalho Chehab 	 * So, this hack is needed, in order to make Kworld SBTVD to work.
1934768e6dadSMauro Carvalho Chehab 	 *
1935768e6dadSMauro Carvalho Chehab 	 * It is also needed to change the IF after the initial init.
1936a78b41d5SMauro Carvalho Chehab 	 *
1937a78b41d5SMauro Carvalho Chehab 	 * HACK: Always init the frontend when set_frontend is called:
1938a78b41d5SMauro Carvalho Chehab 	 * it was noticed that, on some devices, it fails to lock on a
1939a78b41d5SMauro Carvalho Chehab 	 * different channel. So, it is better to reset everything, even
1940a78b41d5SMauro Carvalho Chehab 	 * wasting some time, than to loose channel lock.
1941dd4493efSMauro Carvalho Chehab 	 */
1942dd4493efSMauro Carvalho Chehab 	mb86a20s_initfe(fe);
1943dd4493efSMauro Carvalho Chehab 
1944dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1945dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
1946d01a8ee3SMauro Carvalho Chehab 
1947dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
194809b6d21eSMauro Carvalho Chehab 	mb86a20s_reset_counters(fe);
19493a2e4751SMauro Carvalho Chehab 	mb86a20s_stats_not_ready(fe);
1950d01a8ee3SMauro Carvalho Chehab 
1951dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1952dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
1953dd4493efSMauro Carvalho Chehab 
1954dd4493efSMauro Carvalho Chehab 	return rc;
1955dd4493efSMauro Carvalho Chehab }
1956dd4493efSMauro Carvalho Chehab 
195709b6d21eSMauro Carvalho Chehab static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1958d36e418aSMauro Carvalho Chehab 					  fe_status_t *status)
1959d36e418aSMauro Carvalho Chehab {
196009b6d21eSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
196115b1c5a0SMauro Carvalho Chehab 	int rc, status_nr;
1962d36e418aSMauro Carvalho Chehab 
196309b6d21eSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1964d36e418aSMauro Carvalho Chehab 
1965d36e418aSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1966d36e418aSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
1967d36e418aSMauro Carvalho Chehab 
196809b6d21eSMauro Carvalho Chehab 	/* Get lock */
196915b1c5a0SMauro Carvalho Chehab 	status_nr = mb86a20s_read_status(fe, status);
197015b1c5a0SMauro Carvalho Chehab 	if (status_nr < 7) {
197109b6d21eSMauro Carvalho Chehab 		mb86a20s_stats_not_ready(fe);
197209b6d21eSMauro Carvalho Chehab 		mb86a20s_reset_frontend_cache(fe);
197309b6d21eSMauro Carvalho Chehab 	}
197415b1c5a0SMauro Carvalho Chehab 	if (status_nr < 0) {
1975149d518aSMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
1976149d518aSMauro Carvalho Chehab 			"%s: Can't read frontend lock status\n", __func__);
197709b6d21eSMauro Carvalho Chehab 		goto error;
1978149d518aSMauro Carvalho Chehab 	}
197909b6d21eSMauro Carvalho Chehab 
198009b6d21eSMauro Carvalho Chehab 	/* Get signal strength */
198109b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_read_signal_strength(fe);
198209b6d21eSMauro Carvalho Chehab 	if (rc < 0) {
1983149d518aSMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
1984149d518aSMauro Carvalho Chehab 			"%s: Can't reset VBER registers.\n", __func__);
198509b6d21eSMauro Carvalho Chehab 		mb86a20s_stats_not_ready(fe);
198609b6d21eSMauro Carvalho Chehab 		mb86a20s_reset_frontend_cache(fe);
1987149d518aSMauro Carvalho Chehab 
1988149d518aSMauro Carvalho Chehab 		rc = 0;		/* Status is OK */
198909b6d21eSMauro Carvalho Chehab 		goto error;
199009b6d21eSMauro Carvalho Chehab 	}
199109b6d21eSMauro Carvalho Chehab 
199215b1c5a0SMauro Carvalho Chehab 	if (status_nr >= 7) {
199309b6d21eSMauro Carvalho Chehab 		/* Get TMCC info*/
199409b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_get_frontend(fe);
1995149d518aSMauro Carvalho Chehab 		if (rc < 0) {
1996149d518aSMauro Carvalho Chehab 			dev_err(&state->i2c->dev,
1997149d518aSMauro Carvalho Chehab 				"%s: Can't get FE TMCC data.\n", __func__);
1998149d518aSMauro Carvalho Chehab 			rc = 0;		/* Status is OK */
199909b6d21eSMauro Carvalho Chehab 			goto error;
200009b6d21eSMauro Carvalho Chehab 		}
200109b6d21eSMauro Carvalho Chehab 
2002149d518aSMauro Carvalho Chehab 		/* Get statistics */
200315b1c5a0SMauro Carvalho Chehab 		rc = mb86a20s_get_stats(fe, status_nr);
2004149d518aSMauro Carvalho Chehab 		if (rc < 0 && rc != -EBUSY) {
2005149d518aSMauro Carvalho Chehab 			dev_err(&state->i2c->dev,
2006149d518aSMauro Carvalho Chehab 				"%s: Can't get FE statistics.\n", __func__);
2007149d518aSMauro Carvalho Chehab 			rc = 0;
2008149d518aSMauro Carvalho Chehab 			goto error;
2009149d518aSMauro Carvalho Chehab 		}
2010149d518aSMauro Carvalho Chehab 		rc = 0;	/* Don't return EBUSY to userspace */
2011149d518aSMauro Carvalho Chehab 	}
2012149d518aSMauro Carvalho Chehab 	goto ok;
2013149d518aSMauro Carvalho Chehab 
2014149d518aSMauro Carvalho Chehab error:
201509b6d21eSMauro Carvalho Chehab 	mb86a20s_stats_not_ready(fe);
2016d36e418aSMauro Carvalho Chehab 
2017149d518aSMauro Carvalho Chehab ok:
2018d36e418aSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2019d36e418aSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
2020149d518aSMauro Carvalho Chehab 
202109b6d21eSMauro Carvalho Chehab 	return rc;
2022d36e418aSMauro Carvalho Chehab }
2023d36e418aSMauro Carvalho Chehab 
202409b6d21eSMauro Carvalho Chehab static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
202509b6d21eSMauro Carvalho Chehab 						    u16 *strength)
202609b6d21eSMauro Carvalho Chehab {
202709b6d21eSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
202809b6d21eSMauro Carvalho Chehab 
202909b6d21eSMauro Carvalho Chehab 
203009b6d21eSMauro Carvalho Chehab 	*strength = c->strength.stat[0].uvalue;
203109b6d21eSMauro Carvalho Chehab 
203209b6d21eSMauro Carvalho Chehab 	return 0;
203309b6d21eSMauro Carvalho Chehab }
203409b6d21eSMauro Carvalho Chehab 
203509b6d21eSMauro Carvalho Chehab static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
203609b6d21eSMauro Carvalho Chehab {
203709b6d21eSMauro Carvalho Chehab 	/*
203809b6d21eSMauro Carvalho Chehab 	 * get_frontend is now handled together with other stats
203909b6d21eSMauro Carvalho Chehab 	 * retrival, when read_status() is called, as some statistics
204009b6d21eSMauro Carvalho Chehab 	 * will depend on the layers detection.
204109b6d21eSMauro Carvalho Chehab 	 */
204209b6d21eSMauro Carvalho Chehab 	return 0;
204309b6d21eSMauro Carvalho Chehab };
204409b6d21eSMauro Carvalho Chehab 
20459a0bf528SMauro Carvalho Chehab static int mb86a20s_tune(struct dvb_frontend *fe,
20469a0bf528SMauro Carvalho Chehab 			bool re_tune,
20479a0bf528SMauro Carvalho Chehab 			unsigned int mode_flags,
20489a0bf528SMauro Carvalho Chehab 			unsigned int *delay,
20499a0bf528SMauro Carvalho Chehab 			fe_status_t *status)
20509a0bf528SMauro Carvalho Chehab {
2051f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
20529a0bf528SMauro Carvalho Chehab 	int rc = 0;
20539a0bf528SMauro Carvalho Chehab 
2054f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
20559a0bf528SMauro Carvalho Chehab 
20569a0bf528SMauro Carvalho Chehab 	if (re_tune)
20579a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_set_frontend(fe);
20589a0bf528SMauro Carvalho Chehab 
20599a0bf528SMauro Carvalho Chehab 	if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
206009b6d21eSMauro Carvalho Chehab 		mb86a20s_read_status_and_stats(fe, status);
20619a0bf528SMauro Carvalho Chehab 
20629a0bf528SMauro Carvalho Chehab 	return rc;
20639a0bf528SMauro Carvalho Chehab }
20649a0bf528SMauro Carvalho Chehab 
20659a0bf528SMauro Carvalho Chehab static void mb86a20s_release(struct dvb_frontend *fe)
20669a0bf528SMauro Carvalho Chehab {
20679a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
20689a0bf528SMauro Carvalho Chehab 
2069f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
20709a0bf528SMauro Carvalho Chehab 
20719a0bf528SMauro Carvalho Chehab 	kfree(state);
20729a0bf528SMauro Carvalho Chehab }
20739a0bf528SMauro Carvalho Chehab 
20749a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops;
20759a0bf528SMauro Carvalho Chehab 
20769a0bf528SMauro Carvalho Chehab struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
20779a0bf528SMauro Carvalho Chehab 				    struct i2c_adapter *i2c)
20789a0bf528SMauro Carvalho Chehab {
2079f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state;
20809a0bf528SMauro Carvalho Chehab 	u8	rev;
20819a0bf528SMauro Carvalho Chehab 
2082f167e302SMauro Carvalho Chehab 	dev_dbg(&i2c->dev, "%s called.\n", __func__);
2083f167e302SMauro Carvalho Chehab 
20849a0bf528SMauro Carvalho Chehab 	/* allocate memory for the internal state */
2085f66d81b5SMauro Carvalho Chehab 	state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
20869a0bf528SMauro Carvalho Chehab 	if (state == NULL) {
2087f167e302SMauro Carvalho Chehab 		dev_err(&i2c->dev,
2088f66d81b5SMauro Carvalho Chehab 			"%s: unable to allocate memory for state\n", __func__);
20899a0bf528SMauro Carvalho Chehab 		goto error;
20909a0bf528SMauro Carvalho Chehab 	}
20919a0bf528SMauro Carvalho Chehab 
20929a0bf528SMauro Carvalho Chehab 	/* setup the state */
20939a0bf528SMauro Carvalho Chehab 	state->config = config;
20949a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
20959a0bf528SMauro Carvalho Chehab 
20969a0bf528SMauro Carvalho Chehab 	/* create dvb_frontend */
20979a0bf528SMauro Carvalho Chehab 	memcpy(&state->frontend.ops, &mb86a20s_ops,
20989a0bf528SMauro Carvalho Chehab 		sizeof(struct dvb_frontend_ops));
20999a0bf528SMauro Carvalho Chehab 	state->frontend.demodulator_priv = state;
21009a0bf528SMauro Carvalho Chehab 
21019a0bf528SMauro Carvalho Chehab 	/* Check if it is a mb86a20s frontend */
21029a0bf528SMauro Carvalho Chehab 	rev = mb86a20s_readreg(state, 0);
21039a0bf528SMauro Carvalho Chehab 
21049a0bf528SMauro Carvalho Chehab 	if (rev == 0x13) {
2105f167e302SMauro Carvalho Chehab 		dev_info(&i2c->dev,
2106f66d81b5SMauro Carvalho Chehab 			 "Detected a Fujitsu mb86a20s frontend\n");
21079a0bf528SMauro Carvalho Chehab 	} else {
2108f167e302SMauro Carvalho Chehab 		dev_dbg(&i2c->dev,
2109f66d81b5SMauro Carvalho Chehab 			"Frontend revision %d is unknown - aborting.\n",
21109a0bf528SMauro Carvalho Chehab 		       rev);
21119a0bf528SMauro Carvalho Chehab 		goto error;
21129a0bf528SMauro Carvalho Chehab 	}
21139a0bf528SMauro Carvalho Chehab 
21149a0bf528SMauro Carvalho Chehab 	return &state->frontend;
21159a0bf528SMauro Carvalho Chehab 
21169a0bf528SMauro Carvalho Chehab error:
21179a0bf528SMauro Carvalho Chehab 	kfree(state);
21189a0bf528SMauro Carvalho Chehab 	return NULL;
21199a0bf528SMauro Carvalho Chehab }
21209a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(mb86a20s_attach);
21219a0bf528SMauro Carvalho Chehab 
21229a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops = {
21239a0bf528SMauro Carvalho Chehab 	.delsys = { SYS_ISDBT },
21249a0bf528SMauro Carvalho Chehab 	/* Use dib8000 values per default */
21259a0bf528SMauro Carvalho Chehab 	.info = {
21269a0bf528SMauro Carvalho Chehab 		.name = "Fujitsu mb86A20s",
212704fa725eSMauro Carvalho Chehab 		.caps = FE_CAN_RECOVER  |
21289a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_1_2  | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
21299a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_5_6  | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
21309a0bf528SMauro Carvalho Chehab 			FE_CAN_QPSK     | FE_CAN_QAM_16  | FE_CAN_QAM_64 |
21319a0bf528SMauro Carvalho Chehab 			FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
21329a0bf528SMauro Carvalho Chehab 			FE_CAN_GUARD_INTERVAL_AUTO    | FE_CAN_HIERARCHY_AUTO,
21339a0bf528SMauro Carvalho Chehab 		/* Actually, those values depend on the used tuner */
21349a0bf528SMauro Carvalho Chehab 		.frequency_min = 45000000,
21359a0bf528SMauro Carvalho Chehab 		.frequency_max = 864000000,
21369a0bf528SMauro Carvalho Chehab 		.frequency_stepsize = 62500,
21379a0bf528SMauro Carvalho Chehab 	},
21389a0bf528SMauro Carvalho Chehab 
21399a0bf528SMauro Carvalho Chehab 	.release = mb86a20s_release,
21409a0bf528SMauro Carvalho Chehab 
21419a0bf528SMauro Carvalho Chehab 	.init = mb86a20s_initfe,
21429a0bf528SMauro Carvalho Chehab 	.set_frontend = mb86a20s_set_frontend,
214309b6d21eSMauro Carvalho Chehab 	.get_frontend = mb86a20s_get_frontend_dummy,
214409b6d21eSMauro Carvalho Chehab 	.read_status = mb86a20s_read_status_and_stats,
214509b6d21eSMauro Carvalho Chehab 	.read_signal_strength = mb86a20s_read_signal_strength_from_cache,
21469a0bf528SMauro Carvalho Chehab 	.tune = mb86a20s_tune,
21479a0bf528SMauro Carvalho Chehab };
21489a0bf528SMauro Carvalho Chehab 
21499a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
215037e59f87SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab");
21519a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
2152