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 
2504fa725eSMauro Carvalho Chehab enum mb86a20s_bandwidth {
2604fa725eSMauro Carvalho Chehab 	MB86A20S_13SEG = 0,
2704fa725eSMauro Carvalho Chehab 	MB86A20S_13SEG_PARTIAL = 1,
2804fa725eSMauro Carvalho Chehab 	MB86A20S_1SEG = 2,
2904fa725eSMauro Carvalho Chehab 	MB86A20S_3SEG = 3,
3004fa725eSMauro Carvalho Chehab };
3104fa725eSMauro Carvalho Chehab 
32ce08131cSHans Verkuil static u8 mb86a20s_subchannel[] = {
3304fa725eSMauro Carvalho Chehab 	0xb0, 0xc0, 0xd0, 0xe0,
3404fa725eSMauro Carvalho Chehab 	0xf0, 0x00, 0x10, 0x20,
3504fa725eSMauro Carvalho Chehab };
3604fa725eSMauro Carvalho Chehab 
379a0bf528SMauro Carvalho Chehab struct mb86a20s_state {
389a0bf528SMauro Carvalho Chehab 	struct i2c_adapter *i2c;
399a0bf528SMauro Carvalho Chehab 	const struct mb86a20s_config *config;
4009b6d21eSMauro Carvalho Chehab 	u32 last_frequency;
419a0bf528SMauro Carvalho Chehab 
429a0bf528SMauro Carvalho Chehab 	struct dvb_frontend frontend;
439a0bf528SMauro Carvalho Chehab 
44768e6dadSMauro Carvalho Chehab 	u32 if_freq;
4504fa725eSMauro Carvalho Chehab 	enum mb86a20s_bandwidth bw;
4604fa725eSMauro Carvalho Chehab 	bool inversion;
4704fa725eSMauro Carvalho Chehab 	u32 subchannel;
48768e6dadSMauro Carvalho Chehab 
494f62a20dSMauro Carvalho Chehab 	u32 estimated_rate[NUM_LAYERS];
500921ecfdSMauro Carvalho Chehab 	unsigned long get_strength_time;
51d01a8ee3SMauro Carvalho Chehab 
529a0bf528SMauro Carvalho Chehab 	bool need_init;
539a0bf528SMauro Carvalho Chehab };
549a0bf528SMauro Carvalho Chehab 
559a0bf528SMauro Carvalho Chehab struct regdata {
569a0bf528SMauro Carvalho Chehab 	u8 reg;
579a0bf528SMauro Carvalho Chehab 	u8 data;
589a0bf528SMauro Carvalho Chehab };
599a0bf528SMauro Carvalho Chehab 
60d01a8ee3SMauro Carvalho Chehab #define BER_SAMPLING_RATE	1	/* Seconds */
61d01a8ee3SMauro Carvalho Chehab 
629a0bf528SMauro Carvalho Chehab /*
639a0bf528SMauro Carvalho Chehab  * Initialization sequence: Use whatevere default values that PV SBTVD
649a0bf528SMauro Carvalho Chehab  * does on its initialisation, obtained via USB snoop
659a0bf528SMauro Carvalho Chehab  */
66768e6dadSMauro Carvalho Chehab static struct regdata mb86a20s_init1[] = {
679a0bf528SMauro Carvalho Chehab 	{ 0x70, 0x0f },
689a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
699a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
7017e67d4cSMauro Carvalho Chehab 	{ 0x50, 0xd1 }, { 0x51, 0x20 },
71768e6dadSMauro Carvalho Chehab };
72768e6dadSMauro Carvalho Chehab 
73768e6dadSMauro Carvalho Chehab static struct regdata mb86a20s_init2[] = {
74505a0ea7SMauro Carvalho Chehab 	{ 0x50, 0xd1 }, { 0x51, 0x22 },
75505a0ea7SMauro Carvalho Chehab 	{ 0x39, 0x01 },
76505a0ea7SMauro Carvalho Chehab 	{ 0x71, 0x00 },
779a0bf528SMauro Carvalho Chehab 	{ 0x3b, 0x21 },
78505a0ea7SMauro Carvalho Chehab 	{ 0x3c, 0x3a },
799a0bf528SMauro Carvalho Chehab 	{ 0x01, 0x0d },
80505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x08 }, { 0x05, 0x05 },
819a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0e }, { 0x05, 0x00 },
82505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x0f }, { 0x05, 0x14 },
83505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x0b }, { 0x05, 0x8c },
849a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x00 }, { 0x05, 0x00 },
85505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x01 }, { 0x05, 0x07 },
86505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x02 }, { 0x05, 0x0f },
87505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x03 }, { 0x05, 0xa0 },
889a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x09 }, { 0x05, 0x00 },
899a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x0a }, { 0x05, 0xff },
90505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x27 }, { 0x05, 0x64 },
919a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x28 }, { 0x05, 0x00 },
92505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x1e }, { 0x05, 0xff },
93505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x29 }, { 0x05, 0x0a },
94505a0ea7SMauro Carvalho Chehab 	{ 0x04, 0x32 }, { 0x05, 0x0a },
959a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x14 }, { 0x05, 0x02 },
969a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x04 }, { 0x05, 0x00 },
979a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x05 }, { 0x05, 0x22 },
989a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x06 }, { 0x05, 0x0e },
999a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x07 }, { 0x05, 0xd8 },
1009a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x12 }, { 0x05, 0x00 },
1019a0bf528SMauro Carvalho Chehab 	{ 0x04, 0x13 }, { 0x05, 0xff },
10209b6d21eSMauro Carvalho Chehab 
10309b6d21eSMauro Carvalho Chehab 	/*
10409b6d21eSMauro Carvalho Chehab 	 * On this demod, when the bit count reaches the count below,
10509b6d21eSMauro Carvalho Chehab 	 * it collects the bit error count. The bit counters are initialized
10609b6d21eSMauro Carvalho Chehab 	 * to 65535 here. This warrants that all of them will be quickly
10709b6d21eSMauro Carvalho Chehab 	 * calculated when device gets locked. As TMCC is parsed, the values
108d01a8ee3SMauro Carvalho Chehab 	 * will be adjusted later in the driver's code.
10909b6d21eSMauro Carvalho Chehab 	 */
11009b6d21eSMauro Carvalho Chehab 	{ 0x52, 0x01 },				/* Turn on BER before Viterbi */
11109b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xa7 }, { 0x51, 0x00 },
1129a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa8 }, { 0x51, 0xff },
1139a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xa9 }, { 0x51, 0xff },
11409b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xaa }, { 0x51, 0x00 },
1159a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xab }, { 0x51, 0xff },
1169a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xac }, { 0x51, 0xff },
11709b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xad }, { 0x51, 0x00 },
1189a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xae }, { 0x51, 0xff },
1199a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xaf }, { 0x51, 0xff },
12009b6d21eSMauro Carvalho Chehab 
121d9b6f08aSMauro Carvalho Chehab 	/*
122d9b6f08aSMauro Carvalho Chehab 	 * On this demod, post BER counts blocks. When the count reaches the
123d9b6f08aSMauro Carvalho Chehab 	 * value below, it collects the block error count. The block counters
124d9b6f08aSMauro Carvalho Chehab 	 * are initialized to 127 here. This warrants that all of them will be
125d9b6f08aSMauro Carvalho Chehab 	 * quickly calculated when device gets locked. As TMCC is parsed, the
126d9b6f08aSMauro Carvalho Chehab 	 * values will be adjusted later in the driver's code.
127d9b6f08aSMauro Carvalho Chehab 	 */
128d9b6f08aSMauro Carvalho Chehab 	{ 0x5e, 0x07 },				/* Turn on BER after Viterbi */
129d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xdc }, { 0x51, 0x00 },
130d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xdd }, { 0x51, 0x7f },
131d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xde }, { 0x51, 0x00 },
132d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xdf }, { 0x51, 0x7f },
133d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xe0 }, { 0x51, 0x00 },
134d9b6f08aSMauro Carvalho Chehab 	{ 0x50, 0xe1 }, { 0x51, 0x7f },
135593ae89aSMauro Carvalho Chehab 
136593ae89aSMauro Carvalho Chehab 	/*
137593ae89aSMauro Carvalho Chehab 	 * On this demod, when the block count reaches the count below,
138593ae89aSMauro Carvalho Chehab 	 * it collects the block error count. The block counters are initialized
139593ae89aSMauro Carvalho Chehab 	 * to 127 here. This warrants that all of them will be quickly
140593ae89aSMauro Carvalho Chehab 	 * calculated when device gets locked. As TMCC is parsed, the values
141593ae89aSMauro Carvalho Chehab 	 * will be adjusted later in the driver's code.
142593ae89aSMauro Carvalho Chehab 	 */
143593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb0 }, { 0x51, 0x07 },		/* Enable PER */
144593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb2 }, { 0x51, 0x00 },
145593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb3 }, { 0x51, 0x7f },
146593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb4 }, { 0x51, 0x00 },
147593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb5 }, { 0x51, 0x7f },
148593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb6 }, { 0x51, 0x00 },
149593ae89aSMauro Carvalho Chehab 	{ 0x50, 0xb7 }, { 0x51, 0x7f },
15025188bd0SMauro Carvalho Chehab 
15125188bd0SMauro Carvalho Chehab 	{ 0x50, 0x50 }, { 0x51, 0x02 },		/* MER manual mode */
15209b6d21eSMauro Carvalho Chehab 	{ 0x50, 0x51 }, { 0x51, 0x04 },		/* MER symbol 4 */
15309b6d21eSMauro Carvalho Chehab 	{ 0x45, 0x04 },				/* CN symbol 4 */
15425188bd0SMauro Carvalho Chehab 	{ 0x48, 0x04 },				/* CN manual mode */
155505a0ea7SMauro Carvalho Chehab 	{ 0x50, 0xd5 }, { 0x51, 0x01 },
1569a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd6 }, { 0x51, 0x1f },
1579a0bf528SMauro Carvalho Chehab 	{ 0x50, 0xd2 }, { 0x51, 0x03 },
158505a0ea7SMauro Carvalho Chehab 	{ 0x50, 0xd7 }, { 0x51, 0x3f },
1599a0bf528SMauro Carvalho Chehab 	{ 0x1c, 0x01 },
160505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
161505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
162505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
163505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
164505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
165505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
166505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
167505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
168505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
169505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
170505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
171505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
172505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
173505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
174505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
175505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
176505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
177505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
178505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
179505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
180505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
181505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
182505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
183505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
184505a0ea7SMauro Carvalho Chehab 	{ 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
1859a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x1e }, { 0x51, 0x5d },
1869a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x22 }, { 0x51, 0x00 },
1879a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x23 }, { 0x51, 0xc8 },
1889a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x24 }, { 0x51, 0x00 },
1899a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x25 }, { 0x51, 0xf0 },
1909a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x26 }, { 0x51, 0x00 },
1919a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x27 }, { 0x51, 0xc3 },
1929a0bf528SMauro Carvalho Chehab 	{ 0x50, 0x39 }, { 0x51, 0x02 },
193505a0ea7SMauro Carvalho Chehab 	{ 0x50, 0xd5 }, { 0x51, 0x01 },
1949a0bf528SMauro Carvalho Chehab 	{ 0xd0, 0x00 },
1959a0bf528SMauro Carvalho Chehab };
1969a0bf528SMauro Carvalho Chehab 
1979a0bf528SMauro Carvalho Chehab static struct regdata mb86a20s_reset_reception[] = {
1989a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xf0 },
1999a0bf528SMauro Carvalho Chehab 	{ 0x70, 0xff },
2009a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x01 },
2019a0bf528SMauro Carvalho Chehab 	{ 0x08, 0x00 },
2029a0bf528SMauro Carvalho Chehab };
2039a0bf528SMauro Carvalho Chehab 
204d9b6f08aSMauro Carvalho Chehab static struct regdata mb86a20s_per_ber_reset[] = {
205d9b6f08aSMauro Carvalho Chehab 	{ 0x53, 0x00 },	/* pre BER Counter reset */
20609b6d21eSMauro Carvalho Chehab 	{ 0x53, 0x07 },
20709b6d21eSMauro Carvalho Chehab 
208d9b6f08aSMauro Carvalho Chehab 	{ 0x5f, 0x00 },	/* post BER Counter reset */
209d9b6f08aSMauro Carvalho Chehab 	{ 0x5f, 0x07 },
210d9b6f08aSMauro Carvalho Chehab 
21109b6d21eSMauro Carvalho Chehab 	{ 0x50, 0xb1 },	/* PER Counter reset */
21209b6d21eSMauro Carvalho Chehab 	{ 0x51, 0x07 },
21309b6d21eSMauro Carvalho Chehab 	{ 0x51, 0x00 },
21409b6d21eSMauro Carvalho Chehab };
21509b6d21eSMauro Carvalho Chehab 
216dd4493efSMauro Carvalho Chehab /*
217dd4493efSMauro Carvalho Chehab  * I2C read/write functions and macros
218dd4493efSMauro Carvalho Chehab  */
219dd4493efSMauro Carvalho Chehab 
2209a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
22109b6d21eSMauro Carvalho Chehab 			     u8 i2c_addr, u8 reg, u8 data)
2229a0bf528SMauro Carvalho Chehab {
2239a0bf528SMauro Carvalho Chehab 	u8 buf[] = { reg, data };
2249a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg = {
2259a0bf528SMauro Carvalho Chehab 		.addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
2269a0bf528SMauro Carvalho Chehab 	};
2279a0bf528SMauro Carvalho Chehab 	int rc;
2289a0bf528SMauro Carvalho Chehab 
2299a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, &msg, 1);
2309a0bf528SMauro Carvalho Chehab 	if (rc != 1) {
231f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
232f66d81b5SMauro Carvalho Chehab 			"%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
233f66d81b5SMauro Carvalho Chehab 			__func__, rc, reg, data);
2349a0bf528SMauro Carvalho Chehab 		return rc;
2359a0bf528SMauro Carvalho Chehab 	}
2369a0bf528SMauro Carvalho Chehab 
2379a0bf528SMauro Carvalho Chehab 	return 0;
2389a0bf528SMauro Carvalho Chehab }
2399a0bf528SMauro Carvalho Chehab 
2409a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
2419a0bf528SMauro Carvalho Chehab 				     u8 i2c_addr, struct regdata *rd, int size)
2429a0bf528SMauro Carvalho Chehab {
2439a0bf528SMauro Carvalho Chehab 	int i, rc;
2449a0bf528SMauro Carvalho Chehab 
2459a0bf528SMauro Carvalho Chehab 	for (i = 0; i < size; i++) {
2469a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
2479a0bf528SMauro Carvalho Chehab 					   rd[i].data);
2489a0bf528SMauro Carvalho Chehab 		if (rc < 0)
2499a0bf528SMauro Carvalho Chehab 			return rc;
2509a0bf528SMauro Carvalho Chehab 	}
2519a0bf528SMauro Carvalho Chehab 	return 0;
2529a0bf528SMauro Carvalho Chehab }
2539a0bf528SMauro Carvalho Chehab 
2549a0bf528SMauro Carvalho Chehab static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
2559a0bf528SMauro Carvalho Chehab 				u8 i2c_addr, u8 reg)
2569a0bf528SMauro Carvalho Chehab {
2579a0bf528SMauro Carvalho Chehab 	u8 val;
2589a0bf528SMauro Carvalho Chehab 	int rc;
2599a0bf528SMauro Carvalho Chehab 	struct i2c_msg msg[] = {
2609a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
2619a0bf528SMauro Carvalho Chehab 		{ .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
2629a0bf528SMauro Carvalho Chehab 	};
2639a0bf528SMauro Carvalho Chehab 
2649a0bf528SMauro Carvalho Chehab 	rc = i2c_transfer(state->i2c, msg, 2);
2659a0bf528SMauro Carvalho Chehab 
2669a0bf528SMauro Carvalho Chehab 	if (rc != 2) {
267f66d81b5SMauro Carvalho Chehab 		dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
268f66d81b5SMauro Carvalho Chehab 			__func__, reg, rc);
269f66d81b5SMauro Carvalho Chehab 		return (rc < 0) ? rc : -EIO;
2709a0bf528SMauro Carvalho Chehab 	}
2719a0bf528SMauro Carvalho Chehab 
2729a0bf528SMauro Carvalho Chehab 	return val;
2739a0bf528SMauro Carvalho Chehab }
2749a0bf528SMauro Carvalho Chehab 
2759a0bf528SMauro Carvalho Chehab #define mb86a20s_readreg(state, reg) \
2769a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
2779a0bf528SMauro Carvalho Chehab #define mb86a20s_writereg(state, reg, val) \
2789a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
2799a0bf528SMauro Carvalho Chehab #define mb86a20s_writeregdata(state, regdata) \
2809a0bf528SMauro Carvalho Chehab 	mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
2819a0bf528SMauro Carvalho Chehab 	regdata, ARRAY_SIZE(regdata))
2829a0bf528SMauro Carvalho Chehab 
28309b6d21eSMauro Carvalho Chehab /*
28409b6d21eSMauro Carvalho Chehab  * Ancillary internal routines (likely compiled inlined)
28509b6d21eSMauro Carvalho Chehab  *
28609b6d21eSMauro Carvalho Chehab  * The functions below assume that gateway lock has already obtained
28709b6d21eSMauro Carvalho Chehab  */
28809b6d21eSMauro Carvalho Chehab 
2890df289a2SMauro Carvalho Chehab static int mb86a20s_read_status(struct dvb_frontend *fe, enum fe_status *status)
2909a0bf528SMauro Carvalho Chehab {
2919a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
292dd4493efSMauro Carvalho Chehab 	int val;
2939a0bf528SMauro Carvalho Chehab 
294dd4493efSMauro Carvalho Chehab 	*status = 0;
2959a0bf528SMauro Carvalho Chehab 
296eca2d34bSColin Ian King 	val = mb86a20s_readreg(state, 0x0a);
297dd4493efSMauro Carvalho Chehab 	if (val < 0)
298dd4493efSMauro Carvalho Chehab 		return val;
2999a0bf528SMauro Carvalho Chehab 
300eca2d34bSColin Ian King 	val &= 0xf;
301dd4493efSMauro Carvalho Chehab 	if (val >= 2)
302dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_SIGNAL;
3039a0bf528SMauro Carvalho Chehab 
304dd4493efSMauro Carvalho Chehab 	if (val >= 4)
305dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_CARRIER;
3069a0bf528SMauro Carvalho Chehab 
307dd4493efSMauro Carvalho Chehab 	if (val >= 5)
308dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_VITERBI;
3099a0bf528SMauro Carvalho Chehab 
310dd4493efSMauro Carvalho Chehab 	if (val >= 7)
311dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_SYNC;
3129a0bf528SMauro Carvalho Chehab 
313dafb65fbSMauro Carvalho Chehab 	/*
314dafb65fbSMauro Carvalho Chehab 	 * Actually, on state S8, it starts receiving TS, but the TS
315dafb65fbSMauro Carvalho Chehab 	 * output is only on normal state after the transition to S9.
316dafb65fbSMauro Carvalho Chehab 	 */
317dafb65fbSMauro Carvalho Chehab 	if (val >= 9)
318dd4493efSMauro Carvalho Chehab 		*status |= FE_HAS_LOCK;
319dd4493efSMauro Carvalho Chehab 
320f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
321f66d81b5SMauro Carvalho Chehab 		 __func__, *status, val);
322dd4493efSMauro Carvalho Chehab 
32315b1c5a0SMauro Carvalho Chehab 	return val;
3249a0bf528SMauro Carvalho Chehab }
3259a0bf528SMauro Carvalho Chehab 
32609b6d21eSMauro Carvalho Chehab static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
3279a0bf528SMauro Carvalho Chehab {
3289a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
3290921ecfdSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
33009b6d21eSMauro Carvalho Chehab 	int rc;
3319a0bf528SMauro Carvalho Chehab 	unsigned rf_max, rf_min, rf;
3329a0bf528SMauro Carvalho Chehab 
3330921ecfdSMauro Carvalho Chehab 	if (state->get_strength_time &&
3340921ecfdSMauro Carvalho Chehab 	   (!time_after(jiffies, state->get_strength_time)))
3350921ecfdSMauro Carvalho Chehab 		return c->strength.stat[0].uvalue;
3360921ecfdSMauro Carvalho Chehab 
3370921ecfdSMauro Carvalho Chehab 	/* Reset its value if an error happen */
3380921ecfdSMauro Carvalho Chehab 	c->strength.stat[0].uvalue = 0;
3390921ecfdSMauro Carvalho Chehab 
3409a0bf528SMauro Carvalho Chehab 	/* Does a binary search to get RF strength */
3419a0bf528SMauro Carvalho Chehab 	rf_max = 0xfff;
3429a0bf528SMauro Carvalho Chehab 	rf_min = 0;
3439a0bf528SMauro Carvalho Chehab 	do {
3449a0bf528SMauro Carvalho Chehab 		rf = (rf_max + rf_min) / 2;
34509b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x04, 0x1f);
34609b6d21eSMauro Carvalho Chehab 		if (rc < 0)
34709b6d21eSMauro Carvalho Chehab 			return rc;
34809b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x05, rf >> 8);
34909b6d21eSMauro Carvalho Chehab 		if (rc < 0)
35009b6d21eSMauro Carvalho Chehab 			return rc;
35109b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x04, 0x20);
35209b6d21eSMauro Carvalho Chehab 		if (rc < 0)
35309b6d21eSMauro Carvalho Chehab 			return rc;
354dad78c56SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x05, rf);
35509b6d21eSMauro Carvalho Chehab 		if (rc < 0)
35609b6d21eSMauro Carvalho Chehab 			return rc;
3579a0bf528SMauro Carvalho Chehab 
35809b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x02);
35909b6d21eSMauro Carvalho Chehab 		if (rc < 0)
36009b6d21eSMauro Carvalho Chehab 			return rc;
36109b6d21eSMauro Carvalho Chehab 		if (rc & 0x08)
3629a0bf528SMauro Carvalho Chehab 			rf_min = (rf_max + rf_min) / 2;
3639a0bf528SMauro Carvalho Chehab 		else
3649a0bf528SMauro Carvalho Chehab 			rf_max = (rf_max + rf_min) / 2;
3659a0bf528SMauro Carvalho Chehab 		if (rf_max - rf_min < 4) {
36609b6d21eSMauro Carvalho Chehab 			rf = (rf_max + rf_min) / 2;
36709b6d21eSMauro Carvalho Chehab 
36809b6d21eSMauro Carvalho Chehab 			/* Rescale it from 2^12 (4096) to 2^16 */
3690921ecfdSMauro Carvalho Chehab 			rf = rf << (16 - 12);
3700921ecfdSMauro Carvalho Chehab 			if (rf)
3710921ecfdSMauro Carvalho Chehab 				rf |= (1 << 12) - 1;
3720921ecfdSMauro Carvalho Chehab 
373f66d81b5SMauro Carvalho Chehab 			dev_dbg(&state->i2c->dev,
374f66d81b5SMauro Carvalho Chehab 				"%s: signal strength = %d (%d < RF=%d < %d)\n",
375f66d81b5SMauro Carvalho Chehab 				__func__, rf, rf_min, rf >> 4, rf_max);
3760921ecfdSMauro Carvalho Chehab 			c->strength.stat[0].uvalue = rf;
3770921ecfdSMauro Carvalho Chehab 			state->get_strength_time = jiffies +
3780921ecfdSMauro Carvalho Chehab 						   msecs_to_jiffies(1000);
3790921ecfdSMauro Carvalho Chehab 			return 0;
3809a0bf528SMauro Carvalho Chehab 		}
3819a0bf528SMauro Carvalho Chehab 	} while (1);
3829a0bf528SMauro Carvalho Chehab }
3839a0bf528SMauro Carvalho Chehab 
3849a0bf528SMauro Carvalho Chehab static int mb86a20s_get_modulation(struct mb86a20s_state *state,
3859a0bf528SMauro Carvalho Chehab 				   unsigned layer)
3869a0bf528SMauro Carvalho Chehab {
3879a0bf528SMauro Carvalho Chehab 	int rc;
3889a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
3899a0bf528SMauro Carvalho Chehab 		[0] = 0x86,	/* Layer A */
3909a0bf528SMauro Carvalho Chehab 		[1] = 0x8a,	/* Layer B */
3919a0bf528SMauro Carvalho Chehab 		[2] = 0x8e,	/* Layer C */
3929a0bf528SMauro Carvalho Chehab 	};
3939a0bf528SMauro Carvalho Chehab 
3949a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
3959a0bf528SMauro Carvalho Chehab 		return -EINVAL;
3969a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
3979a0bf528SMauro Carvalho Chehab 	if (rc < 0)
3989a0bf528SMauro Carvalho Chehab 		return rc;
3999a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4009a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4019a0bf528SMauro Carvalho Chehab 		return rc;
40204585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
4039a0bf528SMauro Carvalho Chehab 	case 0:
4049a0bf528SMauro Carvalho Chehab 		return DQPSK;
4059a0bf528SMauro Carvalho Chehab 	case 1:
4069a0bf528SMauro Carvalho Chehab 		return QPSK;
4079a0bf528SMauro Carvalho Chehab 	case 2:
4089a0bf528SMauro Carvalho Chehab 		return QAM_16;
4099a0bf528SMauro Carvalho Chehab 	case 3:
4109a0bf528SMauro Carvalho Chehab 		return QAM_64;
4119a0bf528SMauro Carvalho Chehab 	default:
4129a0bf528SMauro Carvalho Chehab 		return QAM_AUTO;
4139a0bf528SMauro Carvalho Chehab 	}
4149a0bf528SMauro Carvalho Chehab }
4159a0bf528SMauro Carvalho Chehab 
4169a0bf528SMauro Carvalho Chehab static int mb86a20s_get_fec(struct mb86a20s_state *state,
4179a0bf528SMauro Carvalho Chehab 			    unsigned layer)
4189a0bf528SMauro Carvalho Chehab {
4199a0bf528SMauro Carvalho Chehab 	int rc;
4209a0bf528SMauro Carvalho Chehab 
4219a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4229a0bf528SMauro Carvalho Chehab 		[0] = 0x87,	/* Layer A */
4239a0bf528SMauro Carvalho Chehab 		[1] = 0x8b,	/* Layer B */
4249a0bf528SMauro Carvalho Chehab 		[2] = 0x8f,	/* Layer C */
4259a0bf528SMauro Carvalho Chehab 	};
4269a0bf528SMauro Carvalho Chehab 
4279a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4289a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4299a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4309a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4319a0bf528SMauro Carvalho Chehab 		return rc;
4329a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4339a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4349a0bf528SMauro Carvalho Chehab 		return rc;
43504585921SMauro Carvalho Chehab 	switch ((rc >> 4) & 0x07) {
4369a0bf528SMauro Carvalho Chehab 	case 0:
4379a0bf528SMauro Carvalho Chehab 		return FEC_1_2;
4389a0bf528SMauro Carvalho Chehab 	case 1:
4399a0bf528SMauro Carvalho Chehab 		return FEC_2_3;
4409a0bf528SMauro Carvalho Chehab 	case 2:
4419a0bf528SMauro Carvalho Chehab 		return FEC_3_4;
4429a0bf528SMauro Carvalho Chehab 	case 3:
4439a0bf528SMauro Carvalho Chehab 		return FEC_5_6;
4449a0bf528SMauro Carvalho Chehab 	case 4:
4459a0bf528SMauro Carvalho Chehab 		return FEC_7_8;
4469a0bf528SMauro Carvalho Chehab 	default:
4479a0bf528SMauro Carvalho Chehab 		return FEC_AUTO;
4489a0bf528SMauro Carvalho Chehab 	}
4499a0bf528SMauro Carvalho Chehab }
4509a0bf528SMauro Carvalho Chehab 
4519a0bf528SMauro Carvalho Chehab static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
4529a0bf528SMauro Carvalho Chehab 				     unsigned layer)
4539a0bf528SMauro Carvalho Chehab {
4549a0bf528SMauro Carvalho Chehab 	int rc;
4554247368bSMauro Carvalho Chehab 	int interleaving[] = {
4564247368bSMauro Carvalho Chehab 		0, 1, 2, 4, 8
4574247368bSMauro Carvalho Chehab 	};
4589a0bf528SMauro Carvalho Chehab 
4599a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4609a0bf528SMauro Carvalho Chehab 		[0] = 0x88,	/* Layer A */
4619a0bf528SMauro Carvalho Chehab 		[1] = 0x8c,	/* Layer B */
4629a0bf528SMauro Carvalho Chehab 		[2] = 0x90,	/* Layer C */
4639a0bf528SMauro Carvalho Chehab 	};
4649a0bf528SMauro Carvalho Chehab 
4659a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4669a0bf528SMauro Carvalho Chehab 		return -EINVAL;
4679a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4689a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4699a0bf528SMauro Carvalho Chehab 		return rc;
4709a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4719a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4729a0bf528SMauro Carvalho Chehab 		return rc;
47304585921SMauro Carvalho Chehab 
4744247368bSMauro Carvalho Chehab 	return interleaving[(rc >> 4) & 0x07];
4759a0bf528SMauro Carvalho Chehab }
4769a0bf528SMauro Carvalho Chehab 
4779a0bf528SMauro Carvalho Chehab static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
4789a0bf528SMauro Carvalho Chehab 				      unsigned layer)
4799a0bf528SMauro Carvalho Chehab {
4809a0bf528SMauro Carvalho Chehab 	int rc, count;
4819a0bf528SMauro Carvalho Chehab 	static unsigned char reg[] = {
4829a0bf528SMauro Carvalho Chehab 		[0] = 0x89,	/* Layer A */
4839a0bf528SMauro Carvalho Chehab 		[1] = 0x8d,	/* Layer B */
4849a0bf528SMauro Carvalho Chehab 		[2] = 0x91,	/* Layer C */
4859a0bf528SMauro Carvalho Chehab 	};
4869a0bf528SMauro Carvalho Chehab 
487f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
488f66d81b5SMauro Carvalho Chehab 
4899a0bf528SMauro Carvalho Chehab 	if (layer >= ARRAY_SIZE(reg))
4909a0bf528SMauro Carvalho Chehab 		return -EINVAL;
491f66d81b5SMauro Carvalho Chehab 
4929a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
4939a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4949a0bf528SMauro Carvalho Chehab 		return rc;
4959a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
4969a0bf528SMauro Carvalho Chehab 	if (rc < 0)
4979a0bf528SMauro Carvalho Chehab 		return rc;
4989a0bf528SMauro Carvalho Chehab 	count = (rc >> 4) & 0x0f;
4999a0bf528SMauro Carvalho Chehab 
500f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
501f66d81b5SMauro Carvalho Chehab 
5029a0bf528SMauro Carvalho Chehab 	return count;
5039a0bf528SMauro Carvalho Chehab }
5049a0bf528SMauro Carvalho Chehab 
505a77cfcacSMauro Carvalho Chehab static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
506a77cfcacSMauro Carvalho Chehab {
507f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
508a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
509a77cfcacSMauro Carvalho Chehab 
510f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
511f66d81b5SMauro Carvalho Chehab 
512a77cfcacSMauro Carvalho Chehab 	/* Fixed parameters */
513a77cfcacSMauro Carvalho Chehab 	c->delivery_system = SYS_ISDBT;
514a77cfcacSMauro Carvalho Chehab 	c->bandwidth_hz = 6000000;
515a77cfcacSMauro Carvalho Chehab 
516a77cfcacSMauro Carvalho Chehab 	/* Initialize values that will be later autodetected */
517a77cfcacSMauro Carvalho Chehab 	c->isdbt_layer_enabled = 0;
518a77cfcacSMauro Carvalho Chehab 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
519a77cfcacSMauro Carvalho Chehab 	c->guard_interval = GUARD_INTERVAL_AUTO;
520a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_mode = 0;
521a77cfcacSMauro Carvalho Chehab 	c->isdbt_sb_segment_count = 0;
522a77cfcacSMauro Carvalho Chehab }
523a77cfcacSMauro Carvalho Chehab 
524d01a8ee3SMauro Carvalho Chehab /*
525d01a8ee3SMauro Carvalho Chehab  * Estimates the bit rate using the per-segment bit rate given by
526d01a8ee3SMauro Carvalho Chehab  * ABNT/NBR 15601 spec (table 4).
527d01a8ee3SMauro Carvalho Chehab  */
528d01a8ee3SMauro Carvalho Chehab static u32 isdbt_rate[3][5][4] = {
529d01a8ee3SMauro Carvalho Chehab 	{	/* DQPSK/QPSK */
530d01a8ee3SMauro Carvalho Chehab 		{  280850,  312060,  330420,  340430 },	/* 1/2 */
531d01a8ee3SMauro Carvalho Chehab 		{  374470,  416080,  440560,  453910 },	/* 2/3 */
532d01a8ee3SMauro Carvalho Chehab 		{  421280,  468090,  495630,  510650 },	/* 3/4 */
533d01a8ee3SMauro Carvalho Chehab 		{  468090,  520100,  550700,  567390 },	/* 5/6 */
534d01a8ee3SMauro Carvalho Chehab 		{  491500,  546110,  578230,  595760 },	/* 7/8 */
535d01a8ee3SMauro Carvalho Chehab 	}, {	/* QAM16 */
536d01a8ee3SMauro Carvalho Chehab 		{  561710,  624130,  660840,  680870 },	/* 1/2 */
537d01a8ee3SMauro Carvalho Chehab 		{  748950,  832170,  881120,  907820 },	/* 2/3 */
538d01a8ee3SMauro Carvalho Chehab 		{  842570,  936190,  991260, 1021300 },	/* 3/4 */
539d01a8ee3SMauro Carvalho Chehab 		{  936190, 1040210, 1101400, 1134780 },	/* 5/6 */
540d01a8ee3SMauro Carvalho Chehab 		{  983000, 1092220, 1156470, 1191520 },	/* 7/8 */
541d01a8ee3SMauro Carvalho Chehab 	}, {	/* QAM64 */
542d01a8ee3SMauro Carvalho Chehab 		{  842570,  936190,  991260, 1021300 },	/* 1/2 */
543d01a8ee3SMauro Carvalho Chehab 		{ 1123430, 1248260, 1321680, 1361740 },	/* 2/3 */
544d01a8ee3SMauro Carvalho Chehab 		{ 1263860, 1404290, 1486900, 1531950 },	/* 3/4 */
545d01a8ee3SMauro Carvalho Chehab 		{ 1404290, 1560320, 1652110, 1702170 },	/* 5/6 */
546d01a8ee3SMauro Carvalho Chehab 		{ 1474500, 1638340, 1734710, 1787280 },	/* 7/8 */
547d01a8ee3SMauro Carvalho Chehab 	}
548d01a8ee3SMauro Carvalho Chehab };
549d01a8ee3SMauro Carvalho Chehab 
550d01a8ee3SMauro Carvalho Chehab static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
5510562aef2SMauro Carvalho Chehab 				   u32 modulation, u32 forward_error_correction,
552277bfd2fSMauro Carvalho Chehab 				   u32 guard_interval,
553d01a8ee3SMauro Carvalho Chehab 				   u32 segment)
554d01a8ee3SMauro Carvalho Chehab {
555d01a8ee3SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
556d01a8ee3SMauro Carvalho Chehab 	u32 rate;
5570562aef2SMauro Carvalho Chehab 	int mod, fec, guard;
558d01a8ee3SMauro Carvalho Chehab 
559d01a8ee3SMauro Carvalho Chehab 	/*
560277bfd2fSMauro Carvalho Chehab 	 * If modulation/fec/guard is not detected, the default is
561d01a8ee3SMauro Carvalho Chehab 	 * to consider the lowest bit rate, to avoid taking too long time
562d01a8ee3SMauro Carvalho Chehab 	 * to get BER.
563d01a8ee3SMauro Carvalho Chehab 	 */
564d01a8ee3SMauro Carvalho Chehab 	switch (modulation) {
565d01a8ee3SMauro Carvalho Chehab 	case DQPSK:
566d01a8ee3SMauro Carvalho Chehab 	case QPSK:
567d01a8ee3SMauro Carvalho Chehab 	default:
5680562aef2SMauro Carvalho Chehab 		mod = 0;
569d01a8ee3SMauro Carvalho Chehab 		break;
570d01a8ee3SMauro Carvalho Chehab 	case QAM_16:
5710562aef2SMauro Carvalho Chehab 		mod = 1;
572d01a8ee3SMauro Carvalho Chehab 		break;
573d01a8ee3SMauro Carvalho Chehab 	case QAM_64:
5740562aef2SMauro Carvalho Chehab 		mod = 2;
575d01a8ee3SMauro Carvalho Chehab 		break;
576d01a8ee3SMauro Carvalho Chehab 	}
577d01a8ee3SMauro Carvalho Chehab 
5780562aef2SMauro Carvalho Chehab 	switch (forward_error_correction) {
579d01a8ee3SMauro Carvalho Chehab 	default:
580d01a8ee3SMauro Carvalho Chehab 	case FEC_1_2:
581d01a8ee3SMauro Carvalho Chehab 	case FEC_AUTO:
5820562aef2SMauro Carvalho Chehab 		fec = 0;
583d01a8ee3SMauro Carvalho Chehab 		break;
584d01a8ee3SMauro Carvalho Chehab 	case FEC_2_3:
5850562aef2SMauro Carvalho Chehab 		fec = 1;
586d01a8ee3SMauro Carvalho Chehab 		break;
587d01a8ee3SMauro Carvalho Chehab 	case FEC_3_4:
5880562aef2SMauro Carvalho Chehab 		fec = 2;
589d01a8ee3SMauro Carvalho Chehab 		break;
590d01a8ee3SMauro Carvalho Chehab 	case FEC_5_6:
5910562aef2SMauro Carvalho Chehab 		fec = 3;
592d01a8ee3SMauro Carvalho Chehab 		break;
593d01a8ee3SMauro Carvalho Chehab 	case FEC_7_8:
5940562aef2SMauro Carvalho Chehab 		fec = 4;
595d01a8ee3SMauro Carvalho Chehab 		break;
596d01a8ee3SMauro Carvalho Chehab 	}
597d01a8ee3SMauro Carvalho Chehab 
598277bfd2fSMauro Carvalho Chehab 	switch (guard_interval) {
599d01a8ee3SMauro Carvalho Chehab 	default:
600d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_4:
6010562aef2SMauro Carvalho Chehab 		guard = 0;
602d01a8ee3SMauro Carvalho Chehab 		break;
603d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_8:
6040562aef2SMauro Carvalho Chehab 		guard = 1;
605d01a8ee3SMauro Carvalho Chehab 		break;
606d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_16:
6070562aef2SMauro Carvalho Chehab 		guard = 2;
608d01a8ee3SMauro Carvalho Chehab 		break;
609d01a8ee3SMauro Carvalho Chehab 	case GUARD_INTERVAL_1_32:
6100562aef2SMauro Carvalho Chehab 		guard = 3;
611d01a8ee3SMauro Carvalho Chehab 		break;
612d01a8ee3SMauro Carvalho Chehab 	}
613d01a8ee3SMauro Carvalho Chehab 
614d01a8ee3SMauro Carvalho Chehab 	/* Samples BER at BER_SAMPLING_RATE seconds */
6150562aef2SMauro Carvalho Chehab 	rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
616d01a8ee3SMauro Carvalho Chehab 
617d01a8ee3SMauro Carvalho Chehab 	/* Avoids sampling too quickly or to overflow the register */
618d01a8ee3SMauro Carvalho Chehab 	if (rate < 256)
619d01a8ee3SMauro Carvalho Chehab 		rate = 256;
620d01a8ee3SMauro Carvalho Chehab 	else if (rate > (1 << 24) - 1)
621d01a8ee3SMauro Carvalho Chehab 		rate = (1 << 24) - 1;
622d01a8ee3SMauro Carvalho Chehab 
623d01a8ee3SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
624d01a8ee3SMauro Carvalho Chehab 		"%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
6250562aef2SMauro Carvalho Chehab 		__func__, 'A' + layer,
6260562aef2SMauro Carvalho Chehab 		segment * isdbt_rate[mod][fec][guard]/1000,
627d01a8ee3SMauro Carvalho Chehab 		rate, rate);
628d01a8ee3SMauro Carvalho Chehab 
629b1f89331SMauro Carvalho Chehab 	state->estimated_rate[layer] = rate;
630d01a8ee3SMauro Carvalho Chehab }
631d01a8ee3SMauro Carvalho Chehab 
6329a0bf528SMauro Carvalho Chehab static int mb86a20s_get_frontend(struct dvb_frontend *fe)
6339a0bf528SMauro Carvalho Chehab {
6349a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
635a77cfcacSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6365cb88ca8SMauro Carvalho Chehab 	int layer, rc;
6379a0bf528SMauro Carvalho Chehab 
638f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
639f66d81b5SMauro Carvalho Chehab 
640a77cfcacSMauro Carvalho Chehab 	/* Reset frontend cache to default values */
641a77cfcacSMauro Carvalho Chehab 	mb86a20s_reset_frontend_cache(fe);
6429a0bf528SMauro Carvalho Chehab 
6439a0bf528SMauro Carvalho Chehab 	/* Check for partial reception */
6449a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x85);
645a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
646a77cfcacSMauro Carvalho Chehab 		return rc;
6479a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x6e);
648a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
649a77cfcacSMauro Carvalho Chehab 		return rc;
650a77cfcacSMauro Carvalho Chehab 	c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
6519a0bf528SMauro Carvalho Chehab 
6529a0bf528SMauro Carvalho Chehab 	/* Get per-layer data */
653a77cfcacSMauro Carvalho Chehab 
6545cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS; layer++) {
655f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
6565cb88ca8SMauro Carvalho Chehab 			__func__, 'A' + layer);
657f66d81b5SMauro Carvalho Chehab 
6585cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_segment_count(state, layer);
659a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
660f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
661d01a8ee3SMauro Carvalho Chehab 		if (rc >= 0 && rc < 14) {
6625cb88ca8SMauro Carvalho Chehab 			c->layer[layer].segment_count = rc;
663d01a8ee3SMauro Carvalho Chehab 		} else {
6645cb88ca8SMauro Carvalho Chehab 			c->layer[layer].segment_count = 0;
6655cb88ca8SMauro Carvalho Chehab 			state->estimated_rate[layer] = 0;
6669a0bf528SMauro Carvalho Chehab 			continue;
667a77cfcacSMauro Carvalho Chehab 		}
6685cb88ca8SMauro Carvalho Chehab 		c->isdbt_layer_enabled |= 1 << layer;
6695cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_modulation(state, layer);
670a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
671f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
672f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
673f66d81b5SMauro Carvalho Chehab 			__func__, rc);
6745cb88ca8SMauro Carvalho Chehab 		c->layer[layer].modulation = rc;
6755cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_fec(state, layer);
676a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
677f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
678f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
679f66d81b5SMauro Carvalho Chehab 			__func__, rc);
6805cb88ca8SMauro Carvalho Chehab 		c->layer[layer].fec = rc;
6815cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_get_interleaving(state, layer);
682a77cfcacSMauro Carvalho Chehab 		if (rc < 0)
683f66d81b5SMauro Carvalho Chehab 			goto noperlayer_error;
684f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
685f66d81b5SMauro Carvalho Chehab 			__func__, rc);
6865cb88ca8SMauro Carvalho Chehab 		c->layer[layer].interleaving = rc;
6875cb88ca8SMauro Carvalho Chehab 		mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
6885cb88ca8SMauro Carvalho Chehab 				       c->layer[layer].fec,
689277bfd2fSMauro Carvalho Chehab 				       c->guard_interval,
6905cb88ca8SMauro Carvalho Chehab 				       c->layer[layer].segment_count);
6919a0bf528SMauro Carvalho Chehab 	}
6929a0bf528SMauro Carvalho Chehab 
6939a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x6d, 0x84);
694a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
695a77cfcacSMauro Carvalho Chehab 		return rc;
696a77cfcacSMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
697a77cfcacSMauro Carvalho Chehab 		c->isdbt_sb_mode = 1;
6989a0bf528SMauro Carvalho Chehab 		/* At least, one segment should exist */
699a77cfcacSMauro Carvalho Chehab 		if (!c->isdbt_sb_segment_count)
700a77cfcacSMauro Carvalho Chehab 			c->isdbt_sb_segment_count = 1;
701a77cfcacSMauro Carvalho Chehab 	}
7029a0bf528SMauro Carvalho Chehab 
7039a0bf528SMauro Carvalho Chehab 	/* Get transmission mode and guard interval */
7049a0bf528SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x07);
705a77cfcacSMauro Carvalho Chehab 	if (rc < 0)
706a77cfcacSMauro Carvalho Chehab 		return rc;
707276dfc4bSMauro Carvalho Chehab 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
7089a0bf528SMauro Carvalho Chehab 	if ((rc & 0x60) == 0x20) {
709276dfc4bSMauro Carvalho Chehab 		/* Only modes 2 and 3 are supported */
710276dfc4bSMauro Carvalho Chehab 		switch ((rc >> 2) & 0x03) {
7119a0bf528SMauro Carvalho Chehab 		case 1:
712a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_4K;
7139a0bf528SMauro Carvalho Chehab 			break;
7149a0bf528SMauro Carvalho Chehab 		case 2:
715a77cfcacSMauro Carvalho Chehab 			c->transmission_mode = TRANSMISSION_MODE_8K;
7169a0bf528SMauro Carvalho Chehab 			break;
7179a0bf528SMauro Carvalho Chehab 		}
7189a0bf528SMauro Carvalho Chehab 	}
719276dfc4bSMauro Carvalho Chehab 	c->guard_interval = GUARD_INTERVAL_AUTO;
7209a0bf528SMauro Carvalho Chehab 	if (!(rc & 0x10)) {
721276dfc4bSMauro Carvalho Chehab 		/* Guard interval 1/32 is not supported */
7229a0bf528SMauro Carvalho Chehab 		switch (rc & 0x3) {
7239a0bf528SMauro Carvalho Chehab 		case 0:
724a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_4;
7259a0bf528SMauro Carvalho Chehab 			break;
7269a0bf528SMauro Carvalho Chehab 		case 1:
727a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_8;
7289a0bf528SMauro Carvalho Chehab 			break;
7299a0bf528SMauro Carvalho Chehab 		case 2:
730a77cfcacSMauro Carvalho Chehab 			c->guard_interval = GUARD_INTERVAL_1_16;
7319a0bf528SMauro Carvalho Chehab 			break;
7329a0bf528SMauro Carvalho Chehab 		}
7339a0bf528SMauro Carvalho Chehab 	}
73409b6d21eSMauro Carvalho Chehab 	return 0;
7359a0bf528SMauro Carvalho Chehab 
736f66d81b5SMauro Carvalho Chehab noperlayer_error:
73709b6d21eSMauro Carvalho Chehab 
73809b6d21eSMauro Carvalho Chehab 	/* per-layer info is incomplete; discard all per-layer */
73909b6d21eSMauro Carvalho Chehab 	c->isdbt_layer_enabled = 0;
7409a0bf528SMauro Carvalho Chehab 
741a77cfcacSMauro Carvalho Chehab 	return rc;
7429a0bf528SMauro Carvalho Chehab }
7439a0bf528SMauro Carvalho Chehab 
74409b6d21eSMauro Carvalho Chehab static int mb86a20s_reset_counters(struct dvb_frontend *fe)
74509b6d21eSMauro Carvalho Chehab {
74609b6d21eSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
74709b6d21eSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
74809b6d21eSMauro Carvalho Chehab 	int rc, val;
74909b6d21eSMauro Carvalho Chehab 
75009b6d21eSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
75109b6d21eSMauro Carvalho Chehab 
75209b6d21eSMauro Carvalho Chehab 	/* Reset the counters, if the channel changed */
75309b6d21eSMauro Carvalho Chehab 	if (state->last_frequency != c->frequency) {
75409b6d21eSMauro Carvalho Chehab 		memset(&c->cnr, 0, sizeof(c->cnr));
75509b6d21eSMauro Carvalho Chehab 		memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
75609b6d21eSMauro Carvalho Chehab 		memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
757d9b6f08aSMauro Carvalho Chehab 		memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
758d9b6f08aSMauro Carvalho Chehab 		memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
75909b6d21eSMauro Carvalho Chehab 		memset(&c->block_error, 0, sizeof(c->block_error));
76009b6d21eSMauro Carvalho Chehab 		memset(&c->block_count, 0, sizeof(c->block_count));
76109b6d21eSMauro Carvalho Chehab 
76209b6d21eSMauro Carvalho Chehab 		state->last_frequency = c->frequency;
76309b6d21eSMauro Carvalho Chehab 	}
76409b6d21eSMauro Carvalho Chehab 
76509b6d21eSMauro Carvalho Chehab 	/* Clear status for most stats */
76609b6d21eSMauro Carvalho Chehab 
767d9b6f08aSMauro Carvalho Chehab 	/* BER/PER counter reset */
768d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
76909b6d21eSMauro Carvalho Chehab 	if (rc < 0)
77009b6d21eSMauro Carvalho Chehab 		goto err;
77109b6d21eSMauro Carvalho Chehab 
77209b6d21eSMauro Carvalho Chehab 	/* CNR counter reset */
77309b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x45);
77409b6d21eSMauro Carvalho Chehab 	if (rc < 0)
77509b6d21eSMauro Carvalho Chehab 		goto err;
77609b6d21eSMauro Carvalho Chehab 	val = rc;
77709b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val | 0x10);
77809b6d21eSMauro Carvalho Chehab 	if (rc < 0)
77909b6d21eSMauro Carvalho Chehab 		goto err;
78009b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
78109b6d21eSMauro Carvalho Chehab 	if (rc < 0)
78209b6d21eSMauro Carvalho Chehab 		goto err;
78309b6d21eSMauro Carvalho Chehab 
78409b6d21eSMauro Carvalho Chehab 	/* MER counter reset */
78509b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0x50);
78609b6d21eSMauro Carvalho Chehab 	if (rc < 0)
78709b6d21eSMauro Carvalho Chehab 		goto err;
78809b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
78909b6d21eSMauro Carvalho Chehab 	if (rc < 0)
79009b6d21eSMauro Carvalho Chehab 		goto err;
79109b6d21eSMauro Carvalho Chehab 	val = rc;
79209b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val | 0x01);
79309b6d21eSMauro Carvalho Chehab 	if (rc < 0)
79409b6d21eSMauro Carvalho Chehab 		goto err;
79509b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val & 0x06);
79609b6d21eSMauro Carvalho Chehab 	if (rc < 0)
79709b6d21eSMauro Carvalho Chehab 		goto err;
79809b6d21eSMauro Carvalho Chehab 
799149d518aSMauro Carvalho Chehab 	goto ok;
80009b6d21eSMauro Carvalho Chehab err:
801149d518aSMauro Carvalho Chehab 	dev_err(&state->i2c->dev,
802149d518aSMauro Carvalho Chehab 		"%s: Can't reset FE statistics (error %d).\n",
803149d518aSMauro Carvalho Chehab 		__func__, rc);
804149d518aSMauro Carvalho Chehab ok:
80509b6d21eSMauro Carvalho Chehab 	return rc;
80609b6d21eSMauro Carvalho Chehab }
80709b6d21eSMauro Carvalho Chehab 
808ad0abbf1SMauro Carvalho Chehab static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
809149d518aSMauro Carvalho Chehab 				unsigned layer,
810149d518aSMauro Carvalho Chehab 				u32 *error, u32 *count)
811149d518aSMauro Carvalho Chehab {
812149d518aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
813ad0abbf1SMauro Carvalho Chehab 	int rc, val;
814149d518aSMauro Carvalho Chehab 
815149d518aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
816149d518aSMauro Carvalho Chehab 
8174f62a20dSMauro Carvalho Chehab 	if (layer >= NUM_LAYERS)
818149d518aSMauro Carvalho Chehab 		return -EINVAL;
819149d518aSMauro Carvalho Chehab 
820149d518aSMauro Carvalho Chehab 	/* Check if the BER measures are already available */
821149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x54);
822149d518aSMauro Carvalho Chehab 	if (rc < 0)
823149d518aSMauro Carvalho Chehab 		return rc;
824149d518aSMauro Carvalho Chehab 
825149d518aSMauro Carvalho Chehab 	/* Check if data is available for that layer */
826149d518aSMauro Carvalho Chehab 	if (!(rc & (1 << layer))) {
827149d518aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
828ad0abbf1SMauro Carvalho Chehab 			"%s: preBER for layer %c is not available yet.\n",
829149d518aSMauro Carvalho Chehab 			__func__, 'A' + layer);
830149d518aSMauro Carvalho Chehab 		return -EBUSY;
831149d518aSMauro Carvalho Chehab 	}
832149d518aSMauro Carvalho Chehab 
833149d518aSMauro Carvalho Chehab 	/* Read Bit Error Count */
834149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x55 + layer * 3);
835149d518aSMauro Carvalho Chehab 	if (rc < 0)
836149d518aSMauro Carvalho Chehab 		return rc;
837149d518aSMauro Carvalho Chehab 	*error = rc << 16;
838149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x56 + layer * 3);
839149d518aSMauro Carvalho Chehab 	if (rc < 0)
840149d518aSMauro Carvalho Chehab 		return rc;
841149d518aSMauro Carvalho Chehab 	*error |= rc << 8;
842149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x57 + layer * 3);
843149d518aSMauro Carvalho Chehab 	if (rc < 0)
844149d518aSMauro Carvalho Chehab 		return rc;
845149d518aSMauro Carvalho Chehab 	*error |= rc;
846149d518aSMauro Carvalho Chehab 
847149d518aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
848149d518aSMauro Carvalho Chehab 		"%s: bit error before Viterbi for layer %c: %d.\n",
849149d518aSMauro Carvalho Chehab 		__func__, 'A' + layer, *error);
850149d518aSMauro Carvalho Chehab 
851149d518aSMauro Carvalho Chehab 	/* Read Bit Count */
852149d518aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
853149d518aSMauro Carvalho Chehab 	if (rc < 0)
854149d518aSMauro Carvalho Chehab 		return rc;
855149d518aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
856149d518aSMauro Carvalho Chehab 	if (rc < 0)
857149d518aSMauro Carvalho Chehab 		return rc;
858149d518aSMauro Carvalho Chehab 	*count = rc << 16;
859149d518aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xa8 + 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 << 8;
866149d518aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xa9 + 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;
873149d518aSMauro Carvalho Chehab 
874149d518aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
875149d518aSMauro Carvalho Chehab 		"%s: bit count before Viterbi for layer %c: %d.\n",
876149d518aSMauro Carvalho Chehab 		__func__, 'A' + layer, *count);
877149d518aSMauro Carvalho Chehab 
878149d518aSMauro Carvalho Chehab 
879d01a8ee3SMauro Carvalho Chehab 	/*
880d01a8ee3SMauro Carvalho Chehab 	 * As we get TMCC data from the frontend, we can better estimate the
881d01a8ee3SMauro Carvalho Chehab 	 * BER bit counters, in order to do the BER measure during a longer
882d01a8ee3SMauro Carvalho Chehab 	 * time. Use those data, if available, to update the bit count
883d01a8ee3SMauro Carvalho Chehab 	 * measure.
884d01a8ee3SMauro Carvalho Chehab 	 */
885d01a8ee3SMauro Carvalho Chehab 
886d01a8ee3SMauro Carvalho Chehab 	if (state->estimated_rate[layer]
887d01a8ee3SMauro Carvalho Chehab 	    && state->estimated_rate[layer] != *count) {
888d01a8ee3SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
889ad0abbf1SMauro Carvalho Chehab 			"%s: updating layer %c preBER counter to %d.\n",
890d01a8ee3SMauro Carvalho Chehab 			__func__, 'A' + layer, state->estimated_rate[layer]);
891ad0abbf1SMauro Carvalho Chehab 
892ad0abbf1SMauro Carvalho Chehab 		/* Turn off BER before Viterbi */
893ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x52, 0x00);
894ad0abbf1SMauro Carvalho Chehab 
895ad0abbf1SMauro Carvalho Chehab 		/* Update counter for this layer */
896d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
897d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
898d01a8ee3SMauro Carvalho Chehab 			return rc;
899d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51,
900d01a8ee3SMauro Carvalho Chehab 				       state->estimated_rate[layer] >> 16);
901d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
902d01a8ee3SMauro Carvalho Chehab 			return rc;
903d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xa8 + 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] >> 8);
908d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
909d01a8ee3SMauro Carvalho Chehab 			return rc;
910d01a8ee3SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xa9 + 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]);
915d01a8ee3SMauro Carvalho Chehab 		if (rc < 0)
916d01a8ee3SMauro Carvalho Chehab 			return rc;
917ad0abbf1SMauro Carvalho Chehab 
918ad0abbf1SMauro Carvalho Chehab 		/* Turn on BER before Viterbi */
919ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x52, 0x01);
920ad0abbf1SMauro Carvalho Chehab 
921ad0abbf1SMauro Carvalho Chehab 		/* Reset all preBER counters */
922ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, 0x00);
923ad0abbf1SMauro Carvalho Chehab 		if (rc < 0)
924ad0abbf1SMauro Carvalho Chehab 			return rc;
925ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, 0x07);
926ad0abbf1SMauro Carvalho Chehab 	} else {
927ad0abbf1SMauro Carvalho Chehab 		/* Reset counter to collect new data */
928ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x53);
929ad0abbf1SMauro Carvalho Chehab 		if (rc < 0)
930ad0abbf1SMauro Carvalho Chehab 			return rc;
931ad0abbf1SMauro Carvalho Chehab 		val = rc;
932ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
933ad0abbf1SMauro Carvalho Chehab 		if (rc < 0)
934ad0abbf1SMauro Carvalho Chehab 			return rc;
935ad0abbf1SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
936d01a8ee3SMauro Carvalho Chehab 	}
937d01a8ee3SMauro Carvalho Chehab 
938d9b6f08aSMauro Carvalho Chehab 	return rc;
939d9b6f08aSMauro Carvalho Chehab }
940d01a8ee3SMauro Carvalho Chehab 
941d9b6f08aSMauro Carvalho Chehab static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
942d9b6f08aSMauro Carvalho Chehab 				 unsigned layer,
943d9b6f08aSMauro Carvalho Chehab 				  u32 *error, u32 *count)
944d9b6f08aSMauro Carvalho Chehab {
945d9b6f08aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
946d9b6f08aSMauro Carvalho Chehab 	u32 counter, collect_rate;
947d9b6f08aSMauro Carvalho Chehab 	int rc, val;
948d9b6f08aSMauro Carvalho Chehab 
949d9b6f08aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
950d9b6f08aSMauro Carvalho Chehab 
9514f62a20dSMauro Carvalho Chehab 	if (layer >= NUM_LAYERS)
952d9b6f08aSMauro Carvalho Chehab 		return -EINVAL;
953d9b6f08aSMauro Carvalho Chehab 
954d9b6f08aSMauro Carvalho Chehab 	/* Check if the BER measures are already available */
955d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x60);
956d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
957d9b6f08aSMauro Carvalho Chehab 		return rc;
958d9b6f08aSMauro Carvalho Chehab 
959d9b6f08aSMauro Carvalho Chehab 	/* Check if data is available for that layer */
960d9b6f08aSMauro Carvalho Chehab 	if (!(rc & (1 << layer))) {
961d9b6f08aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
962d9b6f08aSMauro Carvalho Chehab 			"%s: post BER for layer %c is not available yet.\n",
963d9b6f08aSMauro Carvalho Chehab 			__func__, 'A' + layer);
964d9b6f08aSMauro Carvalho Chehab 		return -EBUSY;
965d9b6f08aSMauro Carvalho Chehab 	}
966d9b6f08aSMauro Carvalho Chehab 
967d9b6f08aSMauro Carvalho Chehab 	/* Read Bit Error Count */
968d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x64 + layer * 3);
969d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
970d9b6f08aSMauro Carvalho Chehab 		return rc;
971d9b6f08aSMauro Carvalho Chehab 	*error = rc << 16;
972d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x65 + layer * 3);
973d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
974d9b6f08aSMauro Carvalho Chehab 		return rc;
975d9b6f08aSMauro Carvalho Chehab 	*error |= rc << 8;
976d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x66 + layer * 3);
977d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
978d9b6f08aSMauro Carvalho Chehab 		return rc;
979d9b6f08aSMauro Carvalho Chehab 	*error |= rc;
980d9b6f08aSMauro Carvalho Chehab 
981d9b6f08aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
982d9b6f08aSMauro Carvalho Chehab 		"%s: post bit error for layer %c: %d.\n",
983d9b6f08aSMauro Carvalho Chehab 		__func__, 'A' + layer, *error);
984d9b6f08aSMauro Carvalho Chehab 
985d9b6f08aSMauro Carvalho Chehab 	/* Read Bit Count */
986d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
987d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
988d9b6f08aSMauro Carvalho Chehab 		return rc;
989d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
990d9b6f08aSMauro Carvalho Chehab 	if (rc < 0)
991d9b6f08aSMauro Carvalho Chehab 		return rc;
992d9b6f08aSMauro Carvalho Chehab 	counter = rc << 8;
993d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xdd + 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;
1000d9b6f08aSMauro Carvalho Chehab 	*count = counter * 204 * 8;
1001d9b6f08aSMauro Carvalho Chehab 
1002d9b6f08aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
1003d9b6f08aSMauro Carvalho Chehab 		"%s: post bit count for layer %c: %d.\n",
1004d9b6f08aSMauro Carvalho Chehab 		__func__, 'A' + layer, *count);
1005d9b6f08aSMauro Carvalho Chehab 
1006d9b6f08aSMauro Carvalho Chehab 	/*
1007d9b6f08aSMauro Carvalho Chehab 	 * As we get TMCC data from the frontend, we can better estimate the
1008d9b6f08aSMauro Carvalho Chehab 	 * BER bit counters, in order to do the BER measure during a longer
1009d9b6f08aSMauro Carvalho Chehab 	 * time. Use those data, if available, to update the bit count
1010d9b6f08aSMauro Carvalho Chehab 	 * measure.
1011d9b6f08aSMauro Carvalho Chehab 	 */
1012d9b6f08aSMauro Carvalho Chehab 
1013d9b6f08aSMauro Carvalho Chehab 	if (!state->estimated_rate[layer])
1014d9b6f08aSMauro Carvalho Chehab 		goto reset_measurement;
1015d9b6f08aSMauro Carvalho Chehab 
1016d9b6f08aSMauro Carvalho Chehab 	collect_rate = state->estimated_rate[layer] / 204 / 8;
1017d9b6f08aSMauro Carvalho Chehab 	if (collect_rate < 32)
1018d9b6f08aSMauro Carvalho Chehab 		collect_rate = 32;
1019d9b6f08aSMauro Carvalho Chehab 	if (collect_rate > 65535)
1020d9b6f08aSMauro Carvalho Chehab 		collect_rate = 65535;
1021d9b6f08aSMauro Carvalho Chehab 	if (collect_rate != counter) {
1022d9b6f08aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
1023d9b6f08aSMauro Carvalho Chehab 			"%s: updating postBER counter on layer %c to %d.\n",
1024d9b6f08aSMauro Carvalho Chehab 			__func__, 'A' + layer, collect_rate);
1025d9b6f08aSMauro Carvalho Chehab 
1026d9b6f08aSMauro Carvalho Chehab 		/* Turn off BER after Viterbi */
1027d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5e, 0x00);
1028d9b6f08aSMauro Carvalho Chehab 
1029d9b6f08aSMauro Carvalho Chehab 		/* Update counter for this layer */
1030d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1031d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1032d9b6f08aSMauro Carvalho Chehab 			return rc;
1033d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1034d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1035d9b6f08aSMauro Carvalho Chehab 			return rc;
1036d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1037d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1038d9b6f08aSMauro Carvalho Chehab 			return rc;
1039d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1040d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1041d9b6f08aSMauro Carvalho Chehab 			return rc;
1042d9b6f08aSMauro Carvalho Chehab 
1043d9b6f08aSMauro Carvalho Chehab 		/* Turn on BER after Viterbi */
1044d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5e, 0x07);
1045d9b6f08aSMauro Carvalho Chehab 
1046d9b6f08aSMauro Carvalho Chehab 		/* Reset all preBER counters */
1047d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5f, 0x00);
1048d9b6f08aSMauro Carvalho Chehab 		if (rc < 0)
1049d9b6f08aSMauro Carvalho Chehab 			return rc;
1050d9b6f08aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x5f, 0x07);
1051d9b6f08aSMauro Carvalho Chehab 
1052d9b6f08aSMauro Carvalho Chehab 		return rc;
1053d9b6f08aSMauro Carvalho Chehab 	}
1054d9b6f08aSMauro Carvalho Chehab 
1055d9b6f08aSMauro Carvalho Chehab reset_measurement:
1056149d518aSMauro Carvalho Chehab 	/* Reset counter to collect new data */
1057ad0abbf1SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x5f);
1058149d518aSMauro Carvalho Chehab 	if (rc < 0)
1059149d518aSMauro Carvalho Chehab 		return rc;
1060ad0abbf1SMauro Carvalho Chehab 	val = rc;
1061ad0abbf1SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1062ad0abbf1SMauro Carvalho Chehab 	if (rc < 0)
1063ad0abbf1SMauro Carvalho Chehab 		return rc;
1064d9b6f08aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
1065149d518aSMauro Carvalho Chehab 
1066ad0abbf1SMauro Carvalho Chehab 	return rc;
1067149d518aSMauro Carvalho Chehab }
1068149d518aSMauro Carvalho Chehab 
1069593ae89aSMauro Carvalho Chehab static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1070593ae89aSMauro Carvalho Chehab 			    unsigned layer,
1071593ae89aSMauro Carvalho Chehab 			    u32 *error, u32 *count)
1072593ae89aSMauro Carvalho Chehab {
1073593ae89aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1074313cf4efSMauro Carvalho Chehab 	int rc, val;
1075593ae89aSMauro Carvalho Chehab 	u32 collect_rate;
1076593ae89aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1077593ae89aSMauro Carvalho Chehab 
10784f62a20dSMauro Carvalho Chehab 	if (layer >= NUM_LAYERS)
1079593ae89aSMauro Carvalho Chehab 		return -EINVAL;
1080593ae89aSMauro Carvalho Chehab 
1081593ae89aSMauro Carvalho Chehab 	/* Check if the PER measures are already available */
1082593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb8);
1083593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1084593ae89aSMauro Carvalho Chehab 		return rc;
1085593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1086593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1087593ae89aSMauro Carvalho Chehab 		return rc;
1088593ae89aSMauro Carvalho Chehab 
1089593ae89aSMauro Carvalho Chehab 	/* Check if data is available for that layer */
1090593ae89aSMauro Carvalho Chehab 
1091593ae89aSMauro Carvalho Chehab 	if (!(rc & (1 << layer))) {
1092593ae89aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
1093593ae89aSMauro Carvalho Chehab 			"%s: block counts for layer %c aren't available yet.\n",
1094593ae89aSMauro Carvalho Chehab 			__func__, 'A' + layer);
1095593ae89aSMauro Carvalho Chehab 		return -EBUSY;
1096593ae89aSMauro Carvalho Chehab 	}
1097593ae89aSMauro Carvalho Chehab 
1098593ae89aSMauro Carvalho Chehab 	/* Read Packet error Count */
1099593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1100593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1101593ae89aSMauro Carvalho Chehab 		return rc;
1102593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1103593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1104593ae89aSMauro Carvalho Chehab 		return rc;
1105593ae89aSMauro Carvalho Chehab 	*error = rc << 8;
1106593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xba + 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;
1113d56e326fSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
1114593ae89aSMauro Carvalho Chehab 		__func__, 'A' + layer, *error);
1115593ae89aSMauro Carvalho Chehab 
1116593ae89aSMauro Carvalho Chehab 	/* Read Bit Count */
1117593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1118593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1119593ae89aSMauro Carvalho Chehab 		return rc;
1120593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1121593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1122593ae89aSMauro Carvalho Chehab 		return rc;
1123593ae89aSMauro Carvalho Chehab 	*count = rc << 8;
1124593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb3 + 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;
1131593ae89aSMauro Carvalho Chehab 
1132593ae89aSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev,
1133593ae89aSMauro Carvalho Chehab 		"%s: block count for layer %c: %d.\n",
1134593ae89aSMauro Carvalho Chehab 		__func__, 'A' + layer, *count);
1135593ae89aSMauro Carvalho Chehab 
1136593ae89aSMauro Carvalho Chehab 	/*
1137593ae89aSMauro Carvalho Chehab 	 * As we get TMCC data from the frontend, we can better estimate the
1138593ae89aSMauro Carvalho Chehab 	 * BER bit counters, in order to do the BER measure during a longer
1139593ae89aSMauro Carvalho Chehab 	 * time. Use those data, if available, to update the bit count
1140593ae89aSMauro Carvalho Chehab 	 * measure.
1141593ae89aSMauro Carvalho Chehab 	 */
1142593ae89aSMauro Carvalho Chehab 
1143593ae89aSMauro Carvalho Chehab 	if (!state->estimated_rate[layer])
1144593ae89aSMauro Carvalho Chehab 		goto reset_measurement;
1145593ae89aSMauro Carvalho Chehab 
1146593ae89aSMauro Carvalho Chehab 	collect_rate = state->estimated_rate[layer] / 204 / 8;
1147593ae89aSMauro Carvalho Chehab 	if (collect_rate < 32)
1148593ae89aSMauro Carvalho Chehab 		collect_rate = 32;
1149593ae89aSMauro Carvalho Chehab 	if (collect_rate > 65535)
1150593ae89aSMauro Carvalho Chehab 		collect_rate = 65535;
1151593ae89aSMauro Carvalho Chehab 
1152593ae89aSMauro Carvalho Chehab 	if (collect_rate != *count) {
1153593ae89aSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
1154593ae89aSMauro Carvalho Chehab 			"%s: updating PER counter on layer %c to %d.\n",
1155593ae89aSMauro Carvalho Chehab 			__func__, 'A' + layer, collect_rate);
1156313cf4efSMauro Carvalho Chehab 
1157313cf4efSMauro Carvalho Chehab 		/* Stop PER measurement */
1158313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb0);
1159313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1160313cf4efSMauro Carvalho Chehab 			return rc;
1161313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x00);
1162313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1163313cf4efSMauro Carvalho Chehab 			return rc;
1164313cf4efSMauro Carvalho Chehab 
1165313cf4efSMauro Carvalho Chehab 		/* Update this layer's counter */
1166593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1167593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1168593ae89aSMauro Carvalho Chehab 			return rc;
1169593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1170593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1171593ae89aSMauro Carvalho Chehab 			return rc;
1172593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1173593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1174593ae89aSMauro Carvalho Chehab 			return rc;
1175593ae89aSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1176593ae89aSMauro Carvalho Chehab 		if (rc < 0)
1177593ae89aSMauro Carvalho Chehab 			return rc;
1178313cf4efSMauro Carvalho Chehab 
1179313cf4efSMauro Carvalho Chehab 		/* start PER measurement */
1180313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb0);
1181313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1182313cf4efSMauro Carvalho Chehab 			return rc;
1183313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x07);
1184313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1185313cf4efSMauro Carvalho Chehab 			return rc;
1186313cf4efSMauro Carvalho Chehab 
1187313cf4efSMauro Carvalho Chehab 		/* Reset all counters to collect new data */
1188313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0xb1);
1189313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1190313cf4efSMauro Carvalho Chehab 			return rc;
1191313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x07);
1192313cf4efSMauro Carvalho Chehab 		if (rc < 0)
1193313cf4efSMauro Carvalho Chehab 			return rc;
1194313cf4efSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x51, 0x00);
1195313cf4efSMauro Carvalho Chehab 
1196313cf4efSMauro Carvalho Chehab 		return rc;
1197593ae89aSMauro Carvalho Chehab 	}
1198593ae89aSMauro Carvalho Chehab 
1199593ae89aSMauro Carvalho Chehab reset_measurement:
1200593ae89aSMauro Carvalho Chehab 	/* Reset counter to collect new data */
1201593ae89aSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xb1);
1202593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1203593ae89aSMauro Carvalho Chehab 		return rc;
1204313cf4efSMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
1205593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1206593ae89aSMauro Carvalho Chehab 		return rc;
1207313cf4efSMauro Carvalho Chehab 	val = rc;
1208313cf4efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
1209593ae89aSMauro Carvalho Chehab 	if (rc < 0)
1210593ae89aSMauro Carvalho Chehab 		return rc;
1211313cf4efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
1212593ae89aSMauro Carvalho Chehab 
1213313cf4efSMauro Carvalho Chehab 	return rc;
1214593ae89aSMauro Carvalho Chehab }
1215593ae89aSMauro Carvalho Chehab 
121625188bd0SMauro Carvalho Chehab struct linear_segments {
121725188bd0SMauro Carvalho Chehab 	unsigned x, y;
121825188bd0SMauro Carvalho Chehab };
121925188bd0SMauro Carvalho Chehab 
122025188bd0SMauro Carvalho Chehab /*
122125188bd0SMauro Carvalho Chehab  * All tables below return a dB/1000 measurement
122225188bd0SMauro Carvalho Chehab  */
122325188bd0SMauro Carvalho Chehab 
1224ce08131cSHans Verkuil static const struct linear_segments cnr_to_db_table[] = {
122525188bd0SMauro Carvalho Chehab 	{ 19648,     0},
122625188bd0SMauro Carvalho Chehab 	{ 18187,  1000},
122725188bd0SMauro Carvalho Chehab 	{ 16534,  2000},
122825188bd0SMauro Carvalho Chehab 	{ 14823,  3000},
122925188bd0SMauro Carvalho Chehab 	{ 13161,  4000},
123025188bd0SMauro Carvalho Chehab 	{ 11622,  5000},
123125188bd0SMauro Carvalho Chehab 	{ 10279,  6000},
123225188bd0SMauro Carvalho Chehab 	{  9089,  7000},
123325188bd0SMauro Carvalho Chehab 	{  8042,  8000},
123425188bd0SMauro Carvalho Chehab 	{  7137,  9000},
123525188bd0SMauro Carvalho Chehab 	{  6342, 10000},
123625188bd0SMauro Carvalho Chehab 	{  5641, 11000},
123725188bd0SMauro Carvalho Chehab 	{  5030, 12000},
123825188bd0SMauro Carvalho Chehab 	{  4474, 13000},
123925188bd0SMauro Carvalho Chehab 	{  3988, 14000},
124025188bd0SMauro Carvalho Chehab 	{  3556, 15000},
124125188bd0SMauro Carvalho Chehab 	{  3180, 16000},
124225188bd0SMauro Carvalho Chehab 	{  2841, 17000},
124325188bd0SMauro Carvalho Chehab 	{  2541, 18000},
124425188bd0SMauro Carvalho Chehab 	{  2276, 19000},
124525188bd0SMauro Carvalho Chehab 	{  2038, 20000},
124625188bd0SMauro Carvalho Chehab 	{  1800, 21000},
124725188bd0SMauro Carvalho Chehab 	{  1625, 22000},
124825188bd0SMauro Carvalho Chehab 	{  1462, 23000},
124925188bd0SMauro Carvalho Chehab 	{  1324, 24000},
125025188bd0SMauro Carvalho Chehab 	{  1175, 25000},
125125188bd0SMauro Carvalho Chehab 	{  1063, 26000},
125225188bd0SMauro Carvalho Chehab 	{   980, 27000},
125325188bd0SMauro Carvalho Chehab 	{   907, 28000},
125425188bd0SMauro Carvalho Chehab 	{   840, 29000},
125525188bd0SMauro Carvalho Chehab 	{   788, 30000},
125625188bd0SMauro Carvalho Chehab };
125725188bd0SMauro Carvalho Chehab 
1258ce08131cSHans Verkuil static const struct linear_segments cnr_64qam_table[] = {
125925188bd0SMauro Carvalho Chehab 	{ 3922688,     0},
126025188bd0SMauro Carvalho Chehab 	{ 3920384,  1000},
126125188bd0SMauro Carvalho Chehab 	{ 3902720,  2000},
126225188bd0SMauro Carvalho Chehab 	{ 3894784,  3000},
126325188bd0SMauro Carvalho Chehab 	{ 3882496,  4000},
126425188bd0SMauro Carvalho Chehab 	{ 3872768,  5000},
126525188bd0SMauro Carvalho Chehab 	{ 3858944,  6000},
126625188bd0SMauro Carvalho Chehab 	{ 3851520,  7000},
126725188bd0SMauro Carvalho Chehab 	{ 3838976,  8000},
126825188bd0SMauro Carvalho Chehab 	{ 3829248,  9000},
126925188bd0SMauro Carvalho Chehab 	{ 3818240, 10000},
127025188bd0SMauro Carvalho Chehab 	{ 3806976, 11000},
127125188bd0SMauro Carvalho Chehab 	{ 3791872, 12000},
127225188bd0SMauro Carvalho Chehab 	{ 3767040, 13000},
127325188bd0SMauro Carvalho Chehab 	{ 3720960, 14000},
127425188bd0SMauro Carvalho Chehab 	{ 3637504, 15000},
127525188bd0SMauro Carvalho Chehab 	{ 3498496, 16000},
127625188bd0SMauro Carvalho Chehab 	{ 3296000, 17000},
127725188bd0SMauro Carvalho Chehab 	{ 3031040, 18000},
127825188bd0SMauro Carvalho Chehab 	{ 2715392, 19000},
127925188bd0SMauro Carvalho Chehab 	{ 2362624, 20000},
128025188bd0SMauro Carvalho Chehab 	{ 1963264, 21000},
128125188bd0SMauro Carvalho Chehab 	{ 1649664, 22000},
128225188bd0SMauro Carvalho Chehab 	{ 1366784, 23000},
128325188bd0SMauro Carvalho Chehab 	{ 1120768, 24000},
128425188bd0SMauro Carvalho Chehab 	{  890880, 25000},
128525188bd0SMauro Carvalho Chehab 	{  723456, 26000},
128625188bd0SMauro Carvalho Chehab 	{  612096, 27000},
128725188bd0SMauro Carvalho Chehab 	{  518912, 28000},
128825188bd0SMauro Carvalho Chehab 	{  448256, 29000},
128925188bd0SMauro Carvalho Chehab 	{  388864, 30000},
129025188bd0SMauro Carvalho Chehab };
129125188bd0SMauro Carvalho Chehab 
1292ce08131cSHans Verkuil static const struct linear_segments cnr_16qam_table[] = {
129325188bd0SMauro Carvalho Chehab 	{ 5314816,     0},
129425188bd0SMauro Carvalho Chehab 	{ 5219072,  1000},
129525188bd0SMauro Carvalho Chehab 	{ 5118720,  2000},
129625188bd0SMauro Carvalho Chehab 	{ 4998912,  3000},
129725188bd0SMauro Carvalho Chehab 	{ 4875520,  4000},
129825188bd0SMauro Carvalho Chehab 	{ 4736000,  5000},
129925188bd0SMauro Carvalho Chehab 	{ 4604160,  6000},
130025188bd0SMauro Carvalho Chehab 	{ 4458752,  7000},
130125188bd0SMauro Carvalho Chehab 	{ 4300288,  8000},
130225188bd0SMauro Carvalho Chehab 	{ 4092928,  9000},
130325188bd0SMauro Carvalho Chehab 	{ 3836160, 10000},
130425188bd0SMauro Carvalho Chehab 	{ 3521024, 11000},
130525188bd0SMauro Carvalho Chehab 	{ 3155968, 12000},
130625188bd0SMauro Carvalho Chehab 	{ 2756864, 13000},
130725188bd0SMauro Carvalho Chehab 	{ 2347008, 14000},
130825188bd0SMauro Carvalho Chehab 	{ 1955072, 15000},
130925188bd0SMauro Carvalho Chehab 	{ 1593600, 16000},
131025188bd0SMauro Carvalho Chehab 	{ 1297920, 17000},
131125188bd0SMauro Carvalho Chehab 	{ 1043968, 18000},
131225188bd0SMauro Carvalho Chehab 	{  839680, 19000},
131325188bd0SMauro Carvalho Chehab 	{  672256, 20000},
131425188bd0SMauro Carvalho Chehab 	{  523008, 21000},
131525188bd0SMauro Carvalho Chehab 	{  424704, 22000},
131625188bd0SMauro Carvalho Chehab 	{  345088, 23000},
131725188bd0SMauro Carvalho Chehab 	{  280064, 24000},
131825188bd0SMauro Carvalho Chehab 	{  221440, 25000},
131925188bd0SMauro Carvalho Chehab 	{  179712, 26000},
132025188bd0SMauro Carvalho Chehab 	{  151040, 27000},
132125188bd0SMauro Carvalho Chehab 	{  128512, 28000},
132225188bd0SMauro Carvalho Chehab 	{  110080, 29000},
132325188bd0SMauro Carvalho Chehab 	{   95744, 30000},
132425188bd0SMauro Carvalho Chehab };
132525188bd0SMauro Carvalho Chehab 
1326ce08131cSHans Verkuil static const struct linear_segments cnr_qpsk_table[] = {
132725188bd0SMauro Carvalho Chehab 	{ 2834176,     0},
132825188bd0SMauro Carvalho Chehab 	{ 2683648,  1000},
132925188bd0SMauro Carvalho Chehab 	{ 2536960,  2000},
133025188bd0SMauro Carvalho Chehab 	{ 2391808,  3000},
133125188bd0SMauro Carvalho Chehab 	{ 2133248,  4000},
133225188bd0SMauro Carvalho Chehab 	{ 1906176,  5000},
133325188bd0SMauro Carvalho Chehab 	{ 1666560,  6000},
133425188bd0SMauro Carvalho Chehab 	{ 1422080,  7000},
133525188bd0SMauro Carvalho Chehab 	{ 1189632,  8000},
133625188bd0SMauro Carvalho Chehab 	{  976384,  9000},
133725188bd0SMauro Carvalho Chehab 	{  790272, 10000},
133825188bd0SMauro Carvalho Chehab 	{  633344, 11000},
133925188bd0SMauro Carvalho Chehab 	{  505600, 12000},
134025188bd0SMauro Carvalho Chehab 	{  402944, 13000},
134125188bd0SMauro Carvalho Chehab 	{  320768, 14000},
134225188bd0SMauro Carvalho Chehab 	{  255488, 15000},
134325188bd0SMauro Carvalho Chehab 	{  204032, 16000},
134425188bd0SMauro Carvalho Chehab 	{  163072, 17000},
134525188bd0SMauro Carvalho Chehab 	{  130304, 18000},
134625188bd0SMauro Carvalho Chehab 	{  105216, 19000},
134725188bd0SMauro Carvalho Chehab 	{   83456, 20000},
134825188bd0SMauro Carvalho Chehab 	{   65024, 21000},
134925188bd0SMauro Carvalho Chehab 	{   52480, 22000},
135025188bd0SMauro Carvalho Chehab 	{   42752, 23000},
135125188bd0SMauro Carvalho Chehab 	{   34560, 24000},
135225188bd0SMauro Carvalho Chehab 	{   27136, 25000},
135325188bd0SMauro Carvalho Chehab 	{   22016, 26000},
135425188bd0SMauro Carvalho Chehab 	{   18432, 27000},
135525188bd0SMauro Carvalho Chehab 	{   15616, 28000},
135625188bd0SMauro Carvalho Chehab 	{   13312, 29000},
135725188bd0SMauro Carvalho Chehab 	{   11520, 30000},
135825188bd0SMauro Carvalho Chehab };
135925188bd0SMauro Carvalho Chehab 
1360ce08131cSHans Verkuil static u32 interpolate_value(u32 value, const struct linear_segments *segments,
136125188bd0SMauro Carvalho Chehab 			     unsigned len)
136225188bd0SMauro Carvalho Chehab {
136325188bd0SMauro Carvalho Chehab 	u64 tmp64;
136425188bd0SMauro Carvalho Chehab 	u32 dx, dy;
136525188bd0SMauro Carvalho Chehab 	int i, ret;
136625188bd0SMauro Carvalho Chehab 
136725188bd0SMauro Carvalho Chehab 	if (value >= segments[0].x)
136825188bd0SMauro Carvalho Chehab 		return segments[0].y;
136925188bd0SMauro Carvalho Chehab 	if (value < segments[len-1].x)
137025188bd0SMauro Carvalho Chehab 		return segments[len-1].y;
137125188bd0SMauro Carvalho Chehab 
137225188bd0SMauro Carvalho Chehab 	for (i = 1; i < len - 1; i++) {
137325188bd0SMauro Carvalho Chehab 		/* If value is identical, no need to interpolate */
137425188bd0SMauro Carvalho Chehab 		if (value == segments[i].x)
137525188bd0SMauro Carvalho Chehab 			return segments[i].y;
137625188bd0SMauro Carvalho Chehab 		if (value > segments[i].x)
137725188bd0SMauro Carvalho Chehab 			break;
137825188bd0SMauro Carvalho Chehab 	}
137925188bd0SMauro Carvalho Chehab 
138025188bd0SMauro Carvalho Chehab 	/* Linear interpolation between the two (x,y) points */
138125188bd0SMauro Carvalho Chehab 	dy = segments[i].y - segments[i - 1].y;
138225188bd0SMauro Carvalho Chehab 	dx = segments[i - 1].x - segments[i].x;
138325188bd0SMauro Carvalho Chehab 	tmp64 = value - segments[i].x;
138425188bd0SMauro Carvalho Chehab 	tmp64 *= dy;
138525188bd0SMauro Carvalho Chehab 	do_div(tmp64, dx);
138625188bd0SMauro Carvalho Chehab 	ret = segments[i].y - tmp64;
138725188bd0SMauro Carvalho Chehab 
138825188bd0SMauro Carvalho Chehab 	return ret;
138925188bd0SMauro Carvalho Chehab }
139025188bd0SMauro Carvalho Chehab 
139125188bd0SMauro Carvalho Chehab static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
139225188bd0SMauro Carvalho Chehab {
139325188bd0SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
139425188bd0SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
139525188bd0SMauro Carvalho Chehab 	u32 cnr_linear, cnr;
139625188bd0SMauro Carvalho Chehab 	int rc, val;
139725188bd0SMauro Carvalho Chehab 
139825188bd0SMauro Carvalho Chehab 	/* Check if CNR is available */
139925188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x45);
140025188bd0SMauro Carvalho Chehab 	if (rc < 0)
140125188bd0SMauro Carvalho Chehab 		return rc;
140225188bd0SMauro Carvalho Chehab 
140325188bd0SMauro Carvalho Chehab 	if (!(rc & 0x40)) {
1404d56e326fSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
140525188bd0SMauro Carvalho Chehab 			 __func__);
140625188bd0SMauro Carvalho Chehab 		return -EBUSY;
140725188bd0SMauro Carvalho Chehab 	}
140825188bd0SMauro Carvalho Chehab 	val = rc;
140925188bd0SMauro Carvalho Chehab 
141025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x46);
141125188bd0SMauro Carvalho Chehab 	if (rc < 0)
141225188bd0SMauro Carvalho Chehab 		return rc;
141325188bd0SMauro Carvalho Chehab 	cnr_linear = rc << 8;
141425188bd0SMauro Carvalho Chehab 
141525188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x46);
141625188bd0SMauro Carvalho Chehab 	if (rc < 0)
141725188bd0SMauro Carvalho Chehab 		return rc;
141825188bd0SMauro Carvalho Chehab 	cnr_linear |= rc;
141925188bd0SMauro Carvalho Chehab 
142025188bd0SMauro Carvalho Chehab 	cnr = interpolate_value(cnr_linear,
142125188bd0SMauro Carvalho Chehab 				cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
142225188bd0SMauro Carvalho Chehab 
142325188bd0SMauro Carvalho Chehab 	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
142425188bd0SMauro Carvalho Chehab 	c->cnr.stat[0].svalue = cnr;
142525188bd0SMauro Carvalho Chehab 
142625188bd0SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
142725188bd0SMauro Carvalho Chehab 		__func__, cnr / 1000, cnr % 1000, cnr_linear);
142825188bd0SMauro Carvalho Chehab 
142925188bd0SMauro Carvalho Chehab 	/* CNR counter reset */
143025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val | 0x10);
143125188bd0SMauro Carvalho Chehab 	if (rc < 0)
143225188bd0SMauro Carvalho Chehab 		return rc;
143325188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
143425188bd0SMauro Carvalho Chehab 
143525188bd0SMauro Carvalho Chehab 	return rc;
143625188bd0SMauro Carvalho Chehab }
143725188bd0SMauro Carvalho Chehab 
1438593ae89aSMauro Carvalho Chehab static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
143925188bd0SMauro Carvalho Chehab {
144025188bd0SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
144125188bd0SMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
144225188bd0SMauro Carvalho Chehab 	u32 mer, cnr;
14435cb88ca8SMauro Carvalho Chehab 	int rc, val, layer;
1444ce08131cSHans Verkuil 	const struct linear_segments *segs;
144525188bd0SMauro Carvalho Chehab 	unsigned segs_len;
144625188bd0SMauro Carvalho Chehab 
144725188bd0SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
144825188bd0SMauro Carvalho Chehab 
144925188bd0SMauro Carvalho Chehab 	/* Check if the measures are already available */
145025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0x5b);
145125188bd0SMauro Carvalho Chehab 	if (rc < 0)
145225188bd0SMauro Carvalho Chehab 		return rc;
145325188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
145425188bd0SMauro Carvalho Chehab 	if (rc < 0)
145525188bd0SMauro Carvalho Chehab 		return rc;
145625188bd0SMauro Carvalho Chehab 
145725188bd0SMauro Carvalho Chehab 	/* Check if data is available */
145825188bd0SMauro Carvalho Chehab 	if (!(rc & 0x01)) {
1459d56e326fSMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
146025188bd0SMauro Carvalho Chehab 			"%s: MER measures aren't available yet.\n", __func__);
146125188bd0SMauro Carvalho Chehab 		return -EBUSY;
146225188bd0SMauro Carvalho Chehab 	}
146325188bd0SMauro Carvalho Chehab 
146425188bd0SMauro Carvalho Chehab 	/* Read all layers */
14655cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS; layer++) {
14665cb88ca8SMauro Carvalho Chehab 		if (!(c->isdbt_layer_enabled & (1 << layer))) {
14675cb88ca8SMauro Carvalho Chehab 			c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
146825188bd0SMauro Carvalho Chehab 			continue;
146925188bd0SMauro Carvalho Chehab 		}
147025188bd0SMauro Carvalho Chehab 
14715cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
147225188bd0SMauro Carvalho Chehab 		if (rc < 0)
147325188bd0SMauro Carvalho Chehab 			return rc;
147425188bd0SMauro Carvalho Chehab 		rc = mb86a20s_readreg(state, 0x51);
147525188bd0SMauro Carvalho Chehab 		if (rc < 0)
147625188bd0SMauro Carvalho Chehab 			return rc;
147725188bd0SMauro Carvalho Chehab 		mer = rc << 16;
14785cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0x53 + 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 << 8;
14855cb88ca8SMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x50, 0x54 + 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;
149225188bd0SMauro Carvalho Chehab 
14935cb88ca8SMauro Carvalho Chehab 		switch (c->layer[layer].modulation) {
149425188bd0SMauro Carvalho Chehab 		case DQPSK:
149525188bd0SMauro Carvalho Chehab 		case QPSK:
149625188bd0SMauro Carvalho Chehab 			segs = cnr_qpsk_table;
149725188bd0SMauro Carvalho Chehab 			segs_len = ARRAY_SIZE(cnr_qpsk_table);
149825188bd0SMauro Carvalho Chehab 			break;
149925188bd0SMauro Carvalho Chehab 		case QAM_16:
150025188bd0SMauro Carvalho Chehab 			segs = cnr_16qam_table;
150125188bd0SMauro Carvalho Chehab 			segs_len = ARRAY_SIZE(cnr_16qam_table);
150225188bd0SMauro Carvalho Chehab 			break;
150325188bd0SMauro Carvalho Chehab 		default:
150425188bd0SMauro Carvalho Chehab 		case QAM_64:
150525188bd0SMauro Carvalho Chehab 			segs = cnr_64qam_table;
150625188bd0SMauro Carvalho Chehab 			segs_len = ARRAY_SIZE(cnr_64qam_table);
150725188bd0SMauro Carvalho Chehab 			break;
150825188bd0SMauro Carvalho Chehab 		}
150925188bd0SMauro Carvalho Chehab 		cnr = interpolate_value(mer, segs, segs_len);
151025188bd0SMauro Carvalho Chehab 
15115cb88ca8SMauro Carvalho Chehab 		c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
15125cb88ca8SMauro Carvalho Chehab 		c->cnr.stat[1 + layer].svalue = cnr;
151325188bd0SMauro Carvalho Chehab 
151425188bd0SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev,
151525188bd0SMauro Carvalho Chehab 			"%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
15165cb88ca8SMauro Carvalho Chehab 			__func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
151725188bd0SMauro Carvalho Chehab 
151825188bd0SMauro Carvalho Chehab 	}
151925188bd0SMauro Carvalho Chehab 
152025188bd0SMauro Carvalho Chehab 	/* Start a new MER measurement */
152125188bd0SMauro Carvalho Chehab 	/* MER counter reset */
152225188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0x50);
152325188bd0SMauro Carvalho Chehab 	if (rc < 0)
152425188bd0SMauro Carvalho Chehab 		return rc;
152525188bd0SMauro Carvalho Chehab 	rc = mb86a20s_readreg(state, 0x51);
152625188bd0SMauro Carvalho Chehab 	if (rc < 0)
152725188bd0SMauro Carvalho Chehab 		return rc;
152825188bd0SMauro Carvalho Chehab 	val = rc;
152925188bd0SMauro Carvalho Chehab 
153025188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val | 0x01);
153125188bd0SMauro Carvalho Chehab 	if (rc < 0)
153225188bd0SMauro Carvalho Chehab 		return rc;
153325188bd0SMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, val & 0x06);
153425188bd0SMauro Carvalho Chehab 	if (rc < 0)
153525188bd0SMauro Carvalho Chehab 		return rc;
153625188bd0SMauro Carvalho Chehab 
153725188bd0SMauro Carvalho Chehab 	return 0;
153825188bd0SMauro Carvalho Chehab }
153925188bd0SMauro Carvalho Chehab 
154009b6d21eSMauro Carvalho Chehab static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
154109b6d21eSMauro Carvalho Chehab {
154209b6d21eSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
154309b6d21eSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
15445cb88ca8SMauro Carvalho Chehab 	int layer;
154509b6d21eSMauro Carvalho Chehab 
154609b6d21eSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
154709b6d21eSMauro Carvalho Chehab 
154809b6d21eSMauro Carvalho Chehab 	/* Fill the length of each status counter */
154909b6d21eSMauro Carvalho Chehab 
155009b6d21eSMauro Carvalho Chehab 	/* Only global stats */
155109b6d21eSMauro Carvalho Chehab 	c->strength.len = 1;
155209b6d21eSMauro Carvalho Chehab 
155309b6d21eSMauro Carvalho Chehab 	/* Per-layer stats - 3 layers + global */
15544f62a20dSMauro Carvalho Chehab 	c->cnr.len = NUM_LAYERS + 1;
15554f62a20dSMauro Carvalho Chehab 	c->pre_bit_error.len = NUM_LAYERS + 1;
15564f62a20dSMauro Carvalho Chehab 	c->pre_bit_count.len = NUM_LAYERS + 1;
15574f62a20dSMauro Carvalho Chehab 	c->post_bit_error.len = NUM_LAYERS + 1;
15584f62a20dSMauro Carvalho Chehab 	c->post_bit_count.len = NUM_LAYERS + 1;
15594f62a20dSMauro Carvalho Chehab 	c->block_error.len = NUM_LAYERS + 1;
15604f62a20dSMauro Carvalho Chehab 	c->block_count.len = NUM_LAYERS + 1;
156109b6d21eSMauro Carvalho Chehab 
156209b6d21eSMauro Carvalho Chehab 	/* Signal is always available */
156309b6d21eSMauro Carvalho Chehab 	c->strength.stat[0].scale = FE_SCALE_RELATIVE;
156409b6d21eSMauro Carvalho Chehab 	c->strength.stat[0].uvalue = 0;
156509b6d21eSMauro Carvalho Chehab 
156609b6d21eSMauro Carvalho Chehab 	/* Put all of them at FE_SCALE_NOT_AVAILABLE */
15675cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
15685cb88ca8SMauro Carvalho Chehab 		c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15695cb88ca8SMauro Carvalho Chehab 		c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15705cb88ca8SMauro Carvalho Chehab 		c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15715cb88ca8SMauro Carvalho Chehab 		c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15725cb88ca8SMauro Carvalho Chehab 		c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15735cb88ca8SMauro Carvalho Chehab 		c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
15745cb88ca8SMauro Carvalho Chehab 		c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
157509b6d21eSMauro Carvalho Chehab 	}
157609b6d21eSMauro Carvalho Chehab }
157709b6d21eSMauro Carvalho Chehab 
157815b1c5a0SMauro Carvalho Chehab static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
1579149d518aSMauro Carvalho Chehab {
1580149d518aSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1581149d518aSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
15825cb88ca8SMauro Carvalho Chehab 	int rc = 0, layer;
1583149d518aSMauro Carvalho Chehab 	u32 bit_error = 0, bit_count = 0;
1584149d518aSMauro Carvalho Chehab 	u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
1585d9b6f08aSMauro Carvalho Chehab 	u32 t_post_bit_error = 0, t_post_bit_count = 0;
1586593ae89aSMauro Carvalho Chehab 	u32 block_error = 0, block_count = 0;
1587593ae89aSMauro Carvalho Chehab 	u32 t_block_error = 0, t_block_count = 0;
1588d9b6f08aSMauro Carvalho Chehab 	int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1589d9b6f08aSMauro Carvalho Chehab 	int per_layers = 0;
1590149d518aSMauro Carvalho Chehab 
159125188bd0SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
159225188bd0SMauro Carvalho Chehab 
159325188bd0SMauro Carvalho Chehab 	mb86a20s_get_main_CNR(fe);
159425188bd0SMauro Carvalho Chehab 
1595149d518aSMauro Carvalho Chehab 	/* Get per-layer stats */
1596593ae89aSMauro Carvalho Chehab 	mb86a20s_get_blk_error_layer_CNR(fe);
159725188bd0SMauro Carvalho Chehab 
159815b1c5a0SMauro Carvalho Chehab 	/*
159915b1c5a0SMauro Carvalho Chehab 	 * At state 7, only CNR is available
160015b1c5a0SMauro Carvalho Chehab 	 * For BER measures, state=9 is required
160115b1c5a0SMauro Carvalho Chehab 	 * FIXME: we may get MER measures with state=8
160215b1c5a0SMauro Carvalho Chehab 	 */
160315b1c5a0SMauro Carvalho Chehab 	if (status_nr < 9)
160415b1c5a0SMauro Carvalho Chehab 		return 0;
160515b1c5a0SMauro Carvalho Chehab 
16065cb88ca8SMauro Carvalho Chehab 	for (layer = 0; layer < NUM_LAYERS; layer++) {
16075cb88ca8SMauro Carvalho Chehab 		if (c->isdbt_layer_enabled & (1 << layer)) {
1608149d518aSMauro Carvalho Chehab 			/* Layer is active and has rc segments */
1609149d518aSMauro Carvalho Chehab 			active_layers++;
1610149d518aSMauro Carvalho Chehab 
1611149d518aSMauro Carvalho Chehab 			/* Handle BER before vterbi */
16125cb88ca8SMauro Carvalho Chehab 			rc = mb86a20s_get_pre_ber(fe, layer,
1613ad0abbf1SMauro Carvalho Chehab 						  &bit_error, &bit_count);
1614149d518aSMauro Carvalho Chehab 			if (rc >= 0) {
16155cb88ca8SMauro Carvalho Chehab 				c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
16165cb88ca8SMauro Carvalho Chehab 				c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
16175cb88ca8SMauro Carvalho Chehab 				c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
16185cb88ca8SMauro Carvalho Chehab 				c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
1619149d518aSMauro Carvalho Chehab 			} else if (rc != -EBUSY) {
1620149d518aSMauro Carvalho Chehab 				/*
1621149d518aSMauro Carvalho Chehab 					* If an I/O error happened,
1622149d518aSMauro Carvalho Chehab 					* measures are now unavailable
1623149d518aSMauro Carvalho Chehab 					*/
16245cb88ca8SMauro Carvalho Chehab 				c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
16255cb88ca8SMauro Carvalho Chehab 				c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1626149d518aSMauro Carvalho Chehab 				dev_err(&state->i2c->dev,
1627149d518aSMauro Carvalho Chehab 					"%s: Can't get BER for layer %c (error %d).\n",
16285cb88ca8SMauro Carvalho Chehab 					__func__, 'A' + layer, rc);
1629149d518aSMauro Carvalho Chehab 			}
16305cb88ca8SMauro Carvalho Chehab 			if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1631d9b6f08aSMauro Carvalho Chehab 				pre_ber_layers++;
1632d9b6f08aSMauro Carvalho Chehab 
1633d9b6f08aSMauro Carvalho Chehab 			/* Handle BER post vterbi */
16345cb88ca8SMauro Carvalho Chehab 			rc = mb86a20s_get_post_ber(fe, layer,
1635d9b6f08aSMauro Carvalho Chehab 						   &bit_error, &bit_count);
1636d9b6f08aSMauro Carvalho Chehab 			if (rc >= 0) {
16375cb88ca8SMauro Carvalho Chehab 				c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
16385cb88ca8SMauro Carvalho Chehab 				c->post_bit_error.stat[1 + layer].uvalue += bit_error;
16395cb88ca8SMauro Carvalho Chehab 				c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
16405cb88ca8SMauro Carvalho Chehab 				c->post_bit_count.stat[1 + layer].uvalue += bit_count;
1641d9b6f08aSMauro Carvalho Chehab 			} else if (rc != -EBUSY) {
1642d9b6f08aSMauro Carvalho Chehab 				/*
1643d9b6f08aSMauro Carvalho Chehab 					* If an I/O error happened,
1644d9b6f08aSMauro Carvalho Chehab 					* measures are now unavailable
1645d9b6f08aSMauro Carvalho Chehab 					*/
16465cb88ca8SMauro Carvalho Chehab 				c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
16475cb88ca8SMauro Carvalho Chehab 				c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1648d9b6f08aSMauro Carvalho Chehab 				dev_err(&state->i2c->dev,
1649d9b6f08aSMauro Carvalho Chehab 					"%s: Can't get BER for layer %c (error %d).\n",
16505cb88ca8SMauro Carvalho Chehab 					__func__, 'A' + layer, rc);
1651d9b6f08aSMauro Carvalho Chehab 			}
16525cb88ca8SMauro Carvalho Chehab 			if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1653d9b6f08aSMauro Carvalho Chehab 				post_ber_layers++;
1654149d518aSMauro Carvalho Chehab 
1655593ae89aSMauro Carvalho Chehab 			/* Handle Block errors for PER/UCB reports */
16565cb88ca8SMauro Carvalho Chehab 			rc = mb86a20s_get_blk_error(fe, layer,
1657593ae89aSMauro Carvalho Chehab 						&block_error,
1658593ae89aSMauro Carvalho Chehab 						&block_count);
1659593ae89aSMauro Carvalho Chehab 			if (rc >= 0) {
16605cb88ca8SMauro Carvalho Chehab 				c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
16615cb88ca8SMauro Carvalho Chehab 				c->block_error.stat[1 + layer].uvalue += block_error;
16625cb88ca8SMauro Carvalho Chehab 				c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
16635cb88ca8SMauro Carvalho Chehab 				c->block_count.stat[1 + layer].uvalue += block_count;
1664593ae89aSMauro Carvalho Chehab 			} else if (rc != -EBUSY) {
1665593ae89aSMauro Carvalho Chehab 				/*
1666593ae89aSMauro Carvalho Chehab 					* If an I/O error happened,
1667593ae89aSMauro Carvalho Chehab 					* measures are now unavailable
1668593ae89aSMauro Carvalho Chehab 					*/
16695cb88ca8SMauro Carvalho Chehab 				c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
16705cb88ca8SMauro Carvalho Chehab 				c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1671593ae89aSMauro Carvalho Chehab 				dev_err(&state->i2c->dev,
1672593ae89aSMauro Carvalho Chehab 					"%s: Can't get PER for layer %c (error %d).\n",
16735cb88ca8SMauro Carvalho Chehab 					__func__, 'A' + layer, rc);
1674593ae89aSMauro Carvalho Chehab 
1675593ae89aSMauro Carvalho Chehab 			}
16765cb88ca8SMauro Carvalho Chehab 			if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
1677593ae89aSMauro Carvalho Chehab 				per_layers++;
1678593ae89aSMauro Carvalho Chehab 
1679d9b6f08aSMauro Carvalho Chehab 			/* Update total preBER */
16805cb88ca8SMauro Carvalho Chehab 			t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
16815cb88ca8SMauro Carvalho Chehab 			t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
1682593ae89aSMauro Carvalho Chehab 
1683d9b6f08aSMauro Carvalho Chehab 			/* Update total postBER */
16845cb88ca8SMauro Carvalho Chehab 			t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
16855cb88ca8SMauro Carvalho Chehab 			t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
1686d9b6f08aSMauro Carvalho Chehab 
1687593ae89aSMauro Carvalho Chehab 			/* Update total PER */
16885cb88ca8SMauro Carvalho Chehab 			t_block_error += c->block_error.stat[1 + layer].uvalue;
16895cb88ca8SMauro Carvalho Chehab 			t_block_count += c->block_count.stat[1 + layer].uvalue;
1690149d518aSMauro Carvalho Chehab 		}
1691149d518aSMauro Carvalho Chehab 	}
1692149d518aSMauro Carvalho Chehab 
1693149d518aSMauro Carvalho Chehab 	/*
1694149d518aSMauro Carvalho Chehab 	 * Start showing global count if at least one error count is
1695149d518aSMauro Carvalho Chehab 	 * available.
1696149d518aSMauro Carvalho Chehab 	 */
1697d9b6f08aSMauro Carvalho Chehab 	if (pre_ber_layers) {
1698149d518aSMauro Carvalho Chehab 		/*
1699149d518aSMauro Carvalho Chehab 		 * At least one per-layer BER measure was read. We can now
1700149d518aSMauro Carvalho Chehab 		 * calculate the total BER
1701149d518aSMauro Carvalho Chehab 		 *
1702149d518aSMauro Carvalho Chehab 		 * Total Bit Error/Count is calculated as the sum of the
1703149d518aSMauro Carvalho Chehab 		 * bit errors on all active layers.
1704149d518aSMauro Carvalho Chehab 		 */
1705149d518aSMauro Carvalho Chehab 		c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1706149d518aSMauro Carvalho Chehab 		c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1707149d518aSMauro Carvalho Chehab 		c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1708149d518aSMauro Carvalho Chehab 		c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
1709f67102c4SMauro Carvalho Chehab 	} else {
1710f67102c4SMauro Carvalho Chehab 		c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1711f67102c4SMauro Carvalho Chehab 		c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1712149d518aSMauro Carvalho Chehab 	}
1713149d518aSMauro Carvalho Chehab 
1714d9b6f08aSMauro Carvalho Chehab 	/*
1715d9b6f08aSMauro Carvalho Chehab 	 * Start showing global count if at least one error count is
1716d9b6f08aSMauro Carvalho Chehab 	 * available.
1717d9b6f08aSMauro Carvalho Chehab 	 */
1718d9b6f08aSMauro Carvalho Chehab 	if (post_ber_layers) {
1719d9b6f08aSMauro Carvalho Chehab 		/*
1720d9b6f08aSMauro Carvalho Chehab 		 * At least one per-layer BER measure was read. We can now
1721d9b6f08aSMauro Carvalho Chehab 		 * calculate the total BER
1722d9b6f08aSMauro Carvalho Chehab 		 *
1723d9b6f08aSMauro Carvalho Chehab 		 * Total Bit Error/Count is calculated as the sum of the
1724d9b6f08aSMauro Carvalho Chehab 		 * bit errors on all active layers.
1725d9b6f08aSMauro Carvalho Chehab 		 */
1726d9b6f08aSMauro Carvalho Chehab 		c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1727d9b6f08aSMauro Carvalho Chehab 		c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1728d9b6f08aSMauro Carvalho Chehab 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1729d9b6f08aSMauro Carvalho Chehab 		c->post_bit_count.stat[0].uvalue = t_post_bit_count;
1730f67102c4SMauro Carvalho Chehab 	} else {
1731f67102c4SMauro Carvalho Chehab 		c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1732f67102c4SMauro Carvalho Chehab 		c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1733d9b6f08aSMauro Carvalho Chehab 	}
1734d9b6f08aSMauro Carvalho Chehab 
1735593ae89aSMauro Carvalho Chehab 	if (per_layers) {
1736593ae89aSMauro Carvalho Chehab 		/*
1737593ae89aSMauro Carvalho Chehab 		 * At least one per-layer UCB measure was read. We can now
1738593ae89aSMauro Carvalho Chehab 		 * calculate the total UCB
1739593ae89aSMauro Carvalho Chehab 		 *
1740593ae89aSMauro Carvalho Chehab 		 * Total block Error/Count is calculated as the sum of the
1741593ae89aSMauro Carvalho Chehab 		 * block errors on all active layers.
1742593ae89aSMauro Carvalho Chehab 		 */
1743593ae89aSMauro Carvalho Chehab 		c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1744593ae89aSMauro Carvalho Chehab 		c->block_error.stat[0].uvalue = t_block_error;
1745593ae89aSMauro Carvalho Chehab 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1746593ae89aSMauro Carvalho Chehab 		c->block_count.stat[0].uvalue = t_block_count;
1747f67102c4SMauro Carvalho Chehab 	} else {
1748f67102c4SMauro Carvalho Chehab 		c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1749f67102c4SMauro Carvalho Chehab 		c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1750593ae89aSMauro Carvalho Chehab 	}
1751593ae89aSMauro Carvalho Chehab 
1752149d518aSMauro Carvalho Chehab 	return rc;
1753149d518aSMauro Carvalho Chehab }
175409b6d21eSMauro Carvalho Chehab 
175509b6d21eSMauro Carvalho Chehab /*
175609b6d21eSMauro Carvalho Chehab  * The functions below are called via DVB callbacks, so they need to
175709b6d21eSMauro Carvalho Chehab  * properly use the I2C gate control
175809b6d21eSMauro Carvalho Chehab  */
175909b6d21eSMauro Carvalho Chehab 
1760dd4493efSMauro Carvalho Chehab static int mb86a20s_initfe(struct dvb_frontend *fe)
1761dd4493efSMauro Carvalho Chehab {
1762dd4493efSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1763768e6dadSMauro Carvalho Chehab 	u64 pll;
17640e4bbeddSMauro Carvalho Chehab 	u32 fclk;
1765dd4493efSMauro Carvalho Chehab 	int rc;
176604fa725eSMauro Carvalho Chehab 	u8  regD5 = 1, reg71, reg09 = 0x3a;
1767dd4493efSMauro Carvalho Chehab 
1768f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1769dd4493efSMauro Carvalho Chehab 
1770dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1771dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
1772dd4493efSMauro Carvalho Chehab 
1773dd4493efSMauro Carvalho Chehab 	/* Initialize the frontend */
1774768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_init1);
1775dd4493efSMauro Carvalho Chehab 	if (rc < 0)
1776dd4493efSMauro Carvalho Chehab 		goto err;
1777dd4493efSMauro Carvalho Chehab 
177804fa725eSMauro Carvalho Chehab 	if (!state->inversion)
177904fa725eSMauro Carvalho Chehab 		reg09 |= 0x04;
178004fa725eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x09, reg09);
178104fa725eSMauro Carvalho Chehab 	if (rc < 0)
178204fa725eSMauro Carvalho Chehab 		goto err;
178304fa725eSMauro Carvalho Chehab 	if (!state->bw)
178404fa725eSMauro Carvalho Chehab 		reg71 = 1;
178504fa725eSMauro Carvalho Chehab 	else
178604fa725eSMauro Carvalho Chehab 		reg71 = 0;
178704fa725eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x39, reg71);
178804fa725eSMauro Carvalho Chehab 	if (rc < 0)
178904fa725eSMauro Carvalho Chehab 		goto err;
179004fa725eSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x71, state->bw);
179104fa725eSMauro Carvalho Chehab 	if (rc < 0)
179204fa725eSMauro Carvalho Chehab 		goto err;
179304fa725eSMauro Carvalho Chehab 	if (state->subchannel) {
179404fa725eSMauro Carvalho Chehab 		rc = mb86a20s_writereg(state, 0x44, state->subchannel);
179504fa725eSMauro Carvalho Chehab 		if (rc < 0)
179604fa725eSMauro Carvalho Chehab 			goto err;
179704fa725eSMauro Carvalho Chehab 	}
179804fa725eSMauro Carvalho Chehab 
17990e4bbeddSMauro Carvalho Chehab 	fclk = state->config->fclk;
18000e4bbeddSMauro Carvalho Chehab 	if (!fclk)
18010e4bbeddSMauro Carvalho Chehab 		fclk = 32571428;
18020e4bbeddSMauro Carvalho Chehab 
1803768e6dadSMauro Carvalho Chehab 	/* Adjust IF frequency to match tuner */
1804768e6dadSMauro Carvalho Chehab 	if (fe->ops.tuner_ops.get_if_frequency)
1805768e6dadSMauro Carvalho Chehab 		fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1806768e6dadSMauro Carvalho Chehab 
1807768e6dadSMauro Carvalho Chehab 	if (!state->if_freq)
1808768e6dadSMauro Carvalho Chehab 		state->if_freq = 3300000;
1809768e6dadSMauro Carvalho Chehab 
18100e4bbeddSMauro Carvalho Chehab 	pll = (((u64)1) << 34) * state->if_freq;
18110e4bbeddSMauro Carvalho Chehab 	do_div(pll, 63 * fclk);
18120e4bbeddSMauro Carvalho Chehab 	pll = (1 << 25) - pll;
18130e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x28, 0x2a);
18140e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18150e4bbeddSMauro Carvalho Chehab 		goto err;
18160e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
18170e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18180e4bbeddSMauro Carvalho Chehab 		goto err;
18190e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
18200e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18210e4bbeddSMauro Carvalho Chehab 		goto err;
18220e4bbeddSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
18230e4bbeddSMauro Carvalho Chehab 	if (rc < 0)
18240e4bbeddSMauro Carvalho Chehab 		goto err;
18250e4bbeddSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
18260e4bbeddSMauro Carvalho Chehab 		__func__, fclk, state->if_freq, (long long)pll);
18270e4bbeddSMauro Carvalho Chehab 
1828768e6dadSMauro Carvalho Chehab 	/* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1829768e6dadSMauro Carvalho Chehab 	pll = state->if_freq * 1677721600L;
1830768e6dadSMauro Carvalho Chehab 	do_div(pll, 1628571429L);
1831768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x28, 0x20);
1832768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1833768e6dadSMauro Carvalho Chehab 		goto err;
1834768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1835768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1836768e6dadSMauro Carvalho Chehab 		goto err;
1837768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1838768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1839768e6dadSMauro Carvalho Chehab 		goto err;
1840768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1841768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1842768e6dadSMauro Carvalho Chehab 		goto err;
18430e4bbeddSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
1844768e6dadSMauro Carvalho Chehab 		__func__, state->if_freq, (long long)pll);
1845768e6dadSMauro Carvalho Chehab 
18469d32069fSMauro Carvalho Chehab 	if (!state->config->is_serial)
1847dd4493efSMauro Carvalho Chehab 		regD5 &= ~1;
1848dd4493efSMauro Carvalho Chehab 
1849dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x50, 0xd5);
1850dd4493efSMauro Carvalho Chehab 	if (rc < 0)
1851dd4493efSMauro Carvalho Chehab 		goto err;
1852dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writereg(state, 0x51, regD5);
1853dd4493efSMauro Carvalho Chehab 	if (rc < 0)
1854dd4493efSMauro Carvalho Chehab 		goto err;
1855dd4493efSMauro Carvalho Chehab 
1856768e6dadSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1857768e6dadSMauro Carvalho Chehab 	if (rc < 0)
1858768e6dadSMauro Carvalho Chehab 		goto err;
1859768e6dadSMauro Carvalho Chehab 
1860768e6dadSMauro Carvalho Chehab 
1861dd4493efSMauro Carvalho Chehab err:
1862dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1863dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
1864dd4493efSMauro Carvalho Chehab 
1865dd4493efSMauro Carvalho Chehab 	if (rc < 0) {
1866dd4493efSMauro Carvalho Chehab 		state->need_init = true;
1867f66d81b5SMauro Carvalho Chehab 		dev_info(&state->i2c->dev,
1868f66d81b5SMauro Carvalho Chehab 			 "mb86a20s: Init failed. Will try again later\n");
1869dd4493efSMauro Carvalho Chehab 	} else {
1870dd4493efSMauro Carvalho Chehab 		state->need_init = false;
1871f66d81b5SMauro Carvalho Chehab 		dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
1872dd4493efSMauro Carvalho Chehab 	}
1873dd4493efSMauro Carvalho Chehab 	return rc;
1874dd4493efSMauro Carvalho Chehab }
1875dd4493efSMauro Carvalho Chehab 
1876dd4493efSMauro Carvalho Chehab static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1877dd4493efSMauro Carvalho Chehab {
1878dd4493efSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
1879dd4493efSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
188004fa725eSMauro Carvalho Chehab 	int rc, if_freq;
1881f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1882dd4493efSMauro Carvalho Chehab 
188304fa725eSMauro Carvalho Chehab 	if (!c->isdbt_layer_enabled)
188404fa725eSMauro Carvalho Chehab 		c->isdbt_layer_enabled = 7;
188504fa725eSMauro Carvalho Chehab 
188604fa725eSMauro Carvalho Chehab 	if (c->isdbt_layer_enabled == 1)
188704fa725eSMauro Carvalho Chehab 		state->bw = MB86A20S_1SEG;
188804fa725eSMauro Carvalho Chehab 	else if (c->isdbt_partial_reception)
188904fa725eSMauro Carvalho Chehab 		state->bw = MB86A20S_13SEG_PARTIAL;
189004fa725eSMauro Carvalho Chehab 	else
189104fa725eSMauro Carvalho Chehab 		state->bw = MB86A20S_13SEG;
189204fa725eSMauro Carvalho Chehab 
189304fa725eSMauro Carvalho Chehab 	if (c->inversion == INVERSION_ON)
189404fa725eSMauro Carvalho Chehab 		state->inversion = true;
189504fa725eSMauro Carvalho Chehab 	else
189604fa725eSMauro Carvalho Chehab 		state->inversion = false;
189704fa725eSMauro Carvalho Chehab 
189804fa725eSMauro Carvalho Chehab 	if (!c->isdbt_sb_mode) {
189904fa725eSMauro Carvalho Chehab 		state->subchannel = 0;
190004fa725eSMauro Carvalho Chehab 	} else {
190141c6e9ddSMauro Carvalho Chehab 		if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
190204fa725eSMauro Carvalho Chehab 			c->isdbt_sb_subchannel = 0;
190304fa725eSMauro Carvalho Chehab 
190404fa725eSMauro Carvalho Chehab 		state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
190504fa725eSMauro Carvalho Chehab 	}
190604fa725eSMauro Carvalho Chehab 
1907dd4493efSMauro Carvalho Chehab 	/*
1908dd4493efSMauro Carvalho Chehab 	 * Gate should already be opened, but it doesn't hurt to
1909dd4493efSMauro Carvalho Chehab 	 * double-check
1910dd4493efSMauro Carvalho Chehab 	 */
1911dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1912dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
1913dd4493efSMauro Carvalho Chehab 	fe->ops.tuner_ops.set_params(fe);
1914dd4493efSMauro Carvalho Chehab 
1915a78b41d5SMauro Carvalho Chehab 	if (fe->ops.tuner_ops.get_if_frequency)
1916768e6dadSMauro Carvalho Chehab 		fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1917768e6dadSMauro Carvalho Chehab 
1918768e6dadSMauro Carvalho Chehab 	/*
1919dd4493efSMauro Carvalho Chehab 	 * Make it more reliable: if, for some reason, the initial
1920dd4493efSMauro Carvalho Chehab 	 * device initialization doesn't happen, initialize it when
1921dd4493efSMauro Carvalho Chehab 	 * a SBTVD parameters are adjusted.
1922dd4493efSMauro Carvalho Chehab 	 *
1923dd4493efSMauro Carvalho Chehab 	 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1924dd4493efSMauro Carvalho Chehab 	 * the agc callback logic is not called during DVB attach time,
1925dd4493efSMauro Carvalho Chehab 	 * causing mb86a20s to not be initialized with Kworld SBTVD.
1926dd4493efSMauro Carvalho Chehab 	 * So, this hack is needed, in order to make Kworld SBTVD to work.
1927768e6dadSMauro Carvalho Chehab 	 *
1928768e6dadSMauro Carvalho Chehab 	 * It is also needed to change the IF after the initial init.
1929a78b41d5SMauro Carvalho Chehab 	 *
1930a78b41d5SMauro Carvalho Chehab 	 * HACK: Always init the frontend when set_frontend is called:
1931a78b41d5SMauro Carvalho Chehab 	 * it was noticed that, on some devices, it fails to lock on a
1932a78b41d5SMauro Carvalho Chehab 	 * different channel. So, it is better to reset everything, even
1933a78b41d5SMauro Carvalho Chehab 	 * wasting some time, than to loose channel lock.
1934dd4493efSMauro Carvalho Chehab 	 */
1935dd4493efSMauro Carvalho Chehab 	mb86a20s_initfe(fe);
1936dd4493efSMauro Carvalho Chehab 
1937dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1938dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
1939d01a8ee3SMauro Carvalho Chehab 
1940dd4493efSMauro Carvalho Chehab 	rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
194109b6d21eSMauro Carvalho Chehab 	mb86a20s_reset_counters(fe);
19423a2e4751SMauro Carvalho Chehab 	mb86a20s_stats_not_ready(fe);
1943d01a8ee3SMauro Carvalho Chehab 
1944dd4493efSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1945dd4493efSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
1946dd4493efSMauro Carvalho Chehab 
1947dd4493efSMauro Carvalho Chehab 	return rc;
1948dd4493efSMauro Carvalho Chehab }
1949dd4493efSMauro Carvalho Chehab 
195009b6d21eSMauro Carvalho Chehab static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
19510df289a2SMauro Carvalho Chehab 					  enum fe_status *status)
1952d36e418aSMauro Carvalho Chehab {
195309b6d21eSMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
195415b1c5a0SMauro Carvalho Chehab 	int rc, status_nr;
1955d36e418aSMauro Carvalho Chehab 
195609b6d21eSMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1957d36e418aSMauro Carvalho Chehab 
1958d36e418aSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
1959d36e418aSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 0);
1960d36e418aSMauro Carvalho Chehab 
196109b6d21eSMauro Carvalho Chehab 	/* Get lock */
196215b1c5a0SMauro Carvalho Chehab 	status_nr = mb86a20s_read_status(fe, status);
196315b1c5a0SMauro Carvalho Chehab 	if (status_nr < 7) {
196409b6d21eSMauro Carvalho Chehab 		mb86a20s_stats_not_ready(fe);
196509b6d21eSMauro Carvalho Chehab 		mb86a20s_reset_frontend_cache(fe);
196609b6d21eSMauro Carvalho Chehab 	}
196715b1c5a0SMauro Carvalho Chehab 	if (status_nr < 0) {
1968149d518aSMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
1969149d518aSMauro Carvalho Chehab 			"%s: Can't read frontend lock status\n", __func__);
197019157003SNicolas Iooss 		rc = status_nr;
197109b6d21eSMauro Carvalho Chehab 		goto error;
1972149d518aSMauro Carvalho Chehab 	}
197309b6d21eSMauro Carvalho Chehab 
197409b6d21eSMauro Carvalho Chehab 	/* Get signal strength */
197509b6d21eSMauro Carvalho Chehab 	rc = mb86a20s_read_signal_strength(fe);
197609b6d21eSMauro Carvalho Chehab 	if (rc < 0) {
1977149d518aSMauro Carvalho Chehab 		dev_err(&state->i2c->dev,
1978149d518aSMauro Carvalho Chehab 			"%s: Can't reset VBER registers.\n", __func__);
197909b6d21eSMauro Carvalho Chehab 		mb86a20s_stats_not_ready(fe);
198009b6d21eSMauro Carvalho Chehab 		mb86a20s_reset_frontend_cache(fe);
1981149d518aSMauro Carvalho Chehab 
1982149d518aSMauro Carvalho Chehab 		rc = 0;		/* Status is OK */
198309b6d21eSMauro Carvalho Chehab 		goto error;
198409b6d21eSMauro Carvalho Chehab 	}
198509b6d21eSMauro Carvalho Chehab 
198615b1c5a0SMauro Carvalho Chehab 	if (status_nr >= 7) {
198709b6d21eSMauro Carvalho Chehab 		/* Get TMCC info*/
198809b6d21eSMauro Carvalho Chehab 		rc = mb86a20s_get_frontend(fe);
1989149d518aSMauro Carvalho Chehab 		if (rc < 0) {
1990149d518aSMauro Carvalho Chehab 			dev_err(&state->i2c->dev,
1991149d518aSMauro Carvalho Chehab 				"%s: Can't get FE TMCC data.\n", __func__);
1992149d518aSMauro Carvalho Chehab 			rc = 0;		/* Status is OK */
199309b6d21eSMauro Carvalho Chehab 			goto error;
199409b6d21eSMauro Carvalho Chehab 		}
199509b6d21eSMauro Carvalho Chehab 
1996149d518aSMauro Carvalho Chehab 		/* Get statistics */
199715b1c5a0SMauro Carvalho Chehab 		rc = mb86a20s_get_stats(fe, status_nr);
1998149d518aSMauro Carvalho Chehab 		if (rc < 0 && rc != -EBUSY) {
1999149d518aSMauro Carvalho Chehab 			dev_err(&state->i2c->dev,
2000149d518aSMauro Carvalho Chehab 				"%s: Can't get FE statistics.\n", __func__);
2001149d518aSMauro Carvalho Chehab 			rc = 0;
2002149d518aSMauro Carvalho Chehab 			goto error;
2003149d518aSMauro Carvalho Chehab 		}
2004149d518aSMauro Carvalho Chehab 		rc = 0;	/* Don't return EBUSY to userspace */
2005149d518aSMauro Carvalho Chehab 	}
2006149d518aSMauro Carvalho Chehab 	goto ok;
2007149d518aSMauro Carvalho Chehab 
2008149d518aSMauro Carvalho Chehab error:
200909b6d21eSMauro Carvalho Chehab 	mb86a20s_stats_not_ready(fe);
2010d36e418aSMauro Carvalho Chehab 
2011149d518aSMauro Carvalho Chehab ok:
2012d36e418aSMauro Carvalho Chehab 	if (fe->ops.i2c_gate_ctrl)
2013d36e418aSMauro Carvalho Chehab 		fe->ops.i2c_gate_ctrl(fe, 1);
2014149d518aSMauro Carvalho Chehab 
201509b6d21eSMauro Carvalho Chehab 	return rc;
2016d36e418aSMauro Carvalho Chehab }
2017d36e418aSMauro Carvalho Chehab 
201809b6d21eSMauro Carvalho Chehab static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
201909b6d21eSMauro Carvalho Chehab 						    u16 *strength)
202009b6d21eSMauro Carvalho Chehab {
202109b6d21eSMauro Carvalho Chehab 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
202209b6d21eSMauro Carvalho Chehab 
202309b6d21eSMauro Carvalho Chehab 
202409b6d21eSMauro Carvalho Chehab 	*strength = c->strength.stat[0].uvalue;
202509b6d21eSMauro Carvalho Chehab 
202609b6d21eSMauro Carvalho Chehab 	return 0;
202709b6d21eSMauro Carvalho Chehab }
202809b6d21eSMauro Carvalho Chehab 
20299a0bf528SMauro Carvalho Chehab static int mb86a20s_tune(struct dvb_frontend *fe,
20309a0bf528SMauro Carvalho Chehab 			bool re_tune,
20319a0bf528SMauro Carvalho Chehab 			unsigned int mode_flags,
20329a0bf528SMauro Carvalho Chehab 			unsigned int *delay,
20330df289a2SMauro Carvalho Chehab 			enum fe_status *status)
20349a0bf528SMauro Carvalho Chehab {
2035f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
20369a0bf528SMauro Carvalho Chehab 	int rc = 0;
20379a0bf528SMauro Carvalho Chehab 
2038f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
20399a0bf528SMauro Carvalho Chehab 
20409a0bf528SMauro Carvalho Chehab 	if (re_tune)
20419a0bf528SMauro Carvalho Chehab 		rc = mb86a20s_set_frontend(fe);
20429a0bf528SMauro Carvalho Chehab 
20439a0bf528SMauro Carvalho Chehab 	if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
204409b6d21eSMauro Carvalho Chehab 		mb86a20s_read_status_and_stats(fe, status);
20459a0bf528SMauro Carvalho Chehab 
20469a0bf528SMauro Carvalho Chehab 	return rc;
20479a0bf528SMauro Carvalho Chehab }
20489a0bf528SMauro Carvalho Chehab 
20499a0bf528SMauro Carvalho Chehab static void mb86a20s_release(struct dvb_frontend *fe)
20509a0bf528SMauro Carvalho Chehab {
20519a0bf528SMauro Carvalho Chehab 	struct mb86a20s_state *state = fe->demodulator_priv;
20529a0bf528SMauro Carvalho Chehab 
2053f66d81b5SMauro Carvalho Chehab 	dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
20549a0bf528SMauro Carvalho Chehab 
20559a0bf528SMauro Carvalho Chehab 	kfree(state);
20569a0bf528SMauro Carvalho Chehab }
20579a0bf528SMauro Carvalho Chehab 
2058dafb65fbSMauro Carvalho Chehab static int mb86a20s_get_frontend_algo(struct dvb_frontend *fe)
2059dafb65fbSMauro Carvalho Chehab {
2060dafb65fbSMauro Carvalho Chehab         return DVBFE_ALGO_HW;
2061dafb65fbSMauro Carvalho Chehab }
2062dafb65fbSMauro Carvalho Chehab 
20639a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops;
20649a0bf528SMauro Carvalho Chehab 
20659a0bf528SMauro Carvalho Chehab struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
20669a0bf528SMauro Carvalho Chehab 				    struct i2c_adapter *i2c)
20679a0bf528SMauro Carvalho Chehab {
2068f66d81b5SMauro Carvalho Chehab 	struct mb86a20s_state *state;
20699a0bf528SMauro Carvalho Chehab 	u8	rev;
20709a0bf528SMauro Carvalho Chehab 
2071f167e302SMauro Carvalho Chehab 	dev_dbg(&i2c->dev, "%s called.\n", __func__);
2072f167e302SMauro Carvalho Chehab 
20739a0bf528SMauro Carvalho Chehab 	/* allocate memory for the internal state */
2074f66d81b5SMauro Carvalho Chehab 	state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
20759a0bf528SMauro Carvalho Chehab 	if (state == NULL) {
2076f167e302SMauro Carvalho Chehab 		dev_err(&i2c->dev,
2077f66d81b5SMauro Carvalho Chehab 			"%s: unable to allocate memory for state\n", __func__);
20789a0bf528SMauro Carvalho Chehab 		goto error;
20799a0bf528SMauro Carvalho Chehab 	}
20809a0bf528SMauro Carvalho Chehab 
20819a0bf528SMauro Carvalho Chehab 	/* setup the state */
20829a0bf528SMauro Carvalho Chehab 	state->config = config;
20839a0bf528SMauro Carvalho Chehab 	state->i2c = i2c;
20849a0bf528SMauro Carvalho Chehab 
20859a0bf528SMauro Carvalho Chehab 	/* create dvb_frontend */
20869a0bf528SMauro Carvalho Chehab 	memcpy(&state->frontend.ops, &mb86a20s_ops,
20879a0bf528SMauro Carvalho Chehab 		sizeof(struct dvb_frontend_ops));
20889a0bf528SMauro Carvalho Chehab 	state->frontend.demodulator_priv = state;
20899a0bf528SMauro Carvalho Chehab 
20909a0bf528SMauro Carvalho Chehab 	/* Check if it is a mb86a20s frontend */
20919a0bf528SMauro Carvalho Chehab 	rev = mb86a20s_readreg(state, 0);
20929a0bf528SMauro Carvalho Chehab 
20939a0bf528SMauro Carvalho Chehab 	if (rev == 0x13) {
2094f167e302SMauro Carvalho Chehab 		dev_info(&i2c->dev,
2095f66d81b5SMauro Carvalho Chehab 			 "Detected a Fujitsu mb86a20s frontend\n");
20969a0bf528SMauro Carvalho Chehab 	} else {
2097f167e302SMauro Carvalho Chehab 		dev_dbg(&i2c->dev,
2098f66d81b5SMauro Carvalho Chehab 			"Frontend revision %d is unknown - aborting.\n",
20999a0bf528SMauro Carvalho Chehab 		       rev);
21009a0bf528SMauro Carvalho Chehab 		goto error;
21019a0bf528SMauro Carvalho Chehab 	}
21029a0bf528SMauro Carvalho Chehab 
21039a0bf528SMauro Carvalho Chehab 	return &state->frontend;
21049a0bf528SMauro Carvalho Chehab 
21059a0bf528SMauro Carvalho Chehab error:
21069a0bf528SMauro Carvalho Chehab 	kfree(state);
21079a0bf528SMauro Carvalho Chehab 	return NULL;
21089a0bf528SMauro Carvalho Chehab }
21099a0bf528SMauro Carvalho Chehab EXPORT_SYMBOL(mb86a20s_attach);
21109a0bf528SMauro Carvalho Chehab 
21119a0bf528SMauro Carvalho Chehab static struct dvb_frontend_ops mb86a20s_ops = {
21129a0bf528SMauro Carvalho Chehab 	.delsys = { SYS_ISDBT },
21139a0bf528SMauro Carvalho Chehab 	/* Use dib8000 values per default */
21149a0bf528SMauro Carvalho Chehab 	.info = {
21159a0bf528SMauro Carvalho Chehab 		.name = "Fujitsu mb86A20s",
211604fa725eSMauro Carvalho Chehab 		.caps = FE_CAN_RECOVER  |
21179a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_1_2  | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
21189a0bf528SMauro Carvalho Chehab 			FE_CAN_FEC_5_6  | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
21199a0bf528SMauro Carvalho Chehab 			FE_CAN_QPSK     | FE_CAN_QAM_16  | FE_CAN_QAM_64 |
21209a0bf528SMauro Carvalho Chehab 			FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
21219a0bf528SMauro Carvalho Chehab 			FE_CAN_GUARD_INTERVAL_AUTO    | FE_CAN_HIERARCHY_AUTO,
21229a0bf528SMauro Carvalho Chehab 		/* Actually, those values depend on the used tuner */
21239a0bf528SMauro Carvalho Chehab 		.frequency_min = 45000000,
21249a0bf528SMauro Carvalho Chehab 		.frequency_max = 864000000,
21259a0bf528SMauro Carvalho Chehab 		.frequency_stepsize = 62500,
21269a0bf528SMauro Carvalho Chehab 	},
21279a0bf528SMauro Carvalho Chehab 
21289a0bf528SMauro Carvalho Chehab 	.release = mb86a20s_release,
21299a0bf528SMauro Carvalho Chehab 
21309a0bf528SMauro Carvalho Chehab 	.init = mb86a20s_initfe,
21319a0bf528SMauro Carvalho Chehab 	.set_frontend = mb86a20s_set_frontend,
213209b6d21eSMauro Carvalho Chehab 	.read_status = mb86a20s_read_status_and_stats,
213309b6d21eSMauro Carvalho Chehab 	.read_signal_strength = mb86a20s_read_signal_strength_from_cache,
21349a0bf528SMauro Carvalho Chehab 	.tune = mb86a20s_tune,
2135dafb65fbSMauro Carvalho Chehab 	.get_frontend_algo = mb86a20s_get_frontend_algo,
21369a0bf528SMauro Carvalho Chehab };
21379a0bf528SMauro Carvalho Chehab 
21389a0bf528SMauro Carvalho Chehab MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
213937e59f87SMauro Carvalho Chehab MODULE_AUTHOR("Mauro Carvalho Chehab");
21409a0bf528SMauro Carvalho Chehab MODULE_LICENSE("GPL");
2141