blob: fe79358b035e5e7d608c1fd2255db4060772a654 [file] [log] [blame]
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001/*
2 * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
3 *
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02004 * Copyright (C) 2010-2013 Mauro Carvalho Chehab
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03005 * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
6 *
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <asm/div64.h>
19
20#include "dvb_frontend.h"
21#include "mb86a20s.h"
22
Mauro Carvalho Chehab4f62a202013-04-07 20:50:34 -030023#define NUM_LAYERS 3
24
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -030025enum mb86a20s_bandwidth {
26 MB86A20S_13SEG = 0,
27 MB86A20S_13SEG_PARTIAL = 1,
28 MB86A20S_1SEG = 2,
29 MB86A20S_3SEG = 3,
30};
31
Hans Verkuilce081312014-08-20 18:08:17 -030032static u8 mb86a20s_subchannel[] = {
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -030033 0xb0, 0xc0, 0xd0, 0xe0,
34 0xf0, 0x00, 0x10, 0x20,
35};
36
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030037struct mb86a20s_state {
38 struct i2c_adapter *i2c;
39 const struct mb86a20s_config *config;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030040 u32 last_frequency;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030041
42 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030043
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030044 u32 if_freq;
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -030045 enum mb86a20s_bandwidth bw;
46 bool inversion;
47 u32 subchannel;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030048
Mauro Carvalho Chehab4f62a202013-04-07 20:50:34 -030049 u32 estimated_rate[NUM_LAYERS];
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -030050 unsigned long get_strength_time;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030051
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030052 bool need_init;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030053};
54
55struct regdata {
56 u8 reg;
57 u8 data;
58};
59
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030060#define BER_SAMPLING_RATE 1 /* Seconds */
61
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030062/*
63 * Initialization sequence: Use whatevere default values that PV SBTVD
64 * does on its initialisation, obtained via USB snoop
65 */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030066static struct regdata mb86a20s_init1[] = {
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030067 { 0x70, 0x0f },
68 { 0x70, 0xff },
69 { 0x08, 0x01 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030070 { 0x50, 0xd1 }, { 0x51, 0x20 },
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030071};
72
73static struct regdata mb86a20s_init2[] = {
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030074 { 0x50, 0xd1 }, { 0x51, 0x22 },
75 { 0x39, 0x01 },
76 { 0x71, 0x00 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030077 { 0x3b, 0x21 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030078 { 0x3c, 0x3a },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030079 { 0x01, 0x0d },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030080 { 0x04, 0x08 }, { 0x05, 0x05 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020081 { 0x04, 0x0e }, { 0x05, 0x00 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030082 { 0x04, 0x0f }, { 0x05, 0x14 },
83 { 0x04, 0x0b }, { 0x05, 0x8c },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020084 { 0x04, 0x00 }, { 0x05, 0x00 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030085 { 0x04, 0x01 }, { 0x05, 0x07 },
86 { 0x04, 0x02 }, { 0x05, 0x0f },
87 { 0x04, 0x03 }, { 0x05, 0xa0 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020088 { 0x04, 0x09 }, { 0x05, 0x00 },
89 { 0x04, 0x0a }, { 0x05, 0xff },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030090 { 0x04, 0x27 }, { 0x05, 0x64 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020091 { 0x04, 0x28 }, { 0x05, 0x00 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -030092 { 0x04, 0x1e }, { 0x05, 0xff },
93 { 0x04, 0x29 }, { 0x05, 0x0a },
94 { 0x04, 0x32 }, { 0x05, 0x0a },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020095 { 0x04, 0x14 }, { 0x05, 0x02 },
96 { 0x04, 0x04 }, { 0x05, 0x00 },
97 { 0x04, 0x05 }, { 0x05, 0x22 },
98 { 0x04, 0x06 }, { 0x05, 0x0e },
99 { 0x04, 0x07 }, { 0x05, 0xd8 },
100 { 0x04, 0x12 }, { 0x05, 0x00 },
101 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300102
103 /*
104 * On this demod, when the bit count reaches the count below,
105 * it collects the bit error count. The bit counters are initialized
106 * to 65535 here. This warrants that all of them will be quickly
107 * calculated when device gets locked. As TMCC is parsed, the values
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300108 * will be adjusted later in the driver's code.
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300109 */
110 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
111 { 0x50, 0xa7 }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200112 { 0x50, 0xa8 }, { 0x51, 0xff },
113 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300114 { 0x50, 0xaa }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200115 { 0x50, 0xab }, { 0x51, 0xff },
116 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300117 { 0x50, 0xad }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200118 { 0x50, 0xae }, { 0x51, 0xff },
119 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300120
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300121 /*
122 * On this demod, post BER counts blocks. When the count reaches the
123 * value below, it collects the block error count. The block counters
124 * are initialized to 127 here. This warrants that all of them will be
125 * quickly calculated when device gets locked. As TMCC is parsed, the
126 * values will be adjusted later in the driver's code.
127 */
128 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
129 { 0x50, 0xdc }, { 0x51, 0x00 },
130 { 0x50, 0xdd }, { 0x51, 0x7f },
131 { 0x50, 0xde }, { 0x51, 0x00 },
132 { 0x50, 0xdf }, { 0x51, 0x7f },
133 { 0x50, 0xe0 }, { 0x51, 0x00 },
134 { 0x50, 0xe1 }, { 0x51, 0x7f },
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300135
136 /*
137 * On this demod, when the block count reaches the count below,
138 * it collects the block error count. The block counters are initialized
139 * to 127 here. This warrants that all of them will be quickly
140 * calculated when device gets locked. As TMCC is parsed, the values
141 * will be adjusted later in the driver's code.
142 */
143 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
144 { 0x50, 0xb2 }, { 0x51, 0x00 },
145 { 0x50, 0xb3 }, { 0x51, 0x7f },
146 { 0x50, 0xb4 }, { 0x51, 0x00 },
147 { 0x50, 0xb5 }, { 0x51, 0x7f },
148 { 0x50, 0xb6 }, { 0x51, 0x00 },
149 { 0x50, 0xb7 }, { 0x51, 0x7f },
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300150
151 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300152 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
153 { 0x45, 0x04 }, /* CN symbol 4 */
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300154 { 0x48, 0x04 }, /* CN manual mode */
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -0300155 { 0x50, 0xd5 }, { 0x51, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200156 { 0x50, 0xd6 }, { 0x51, 0x1f },
157 { 0x50, 0xd2 }, { 0x51, 0x03 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -0300158 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300159 { 0x1c, 0x01 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -0300160 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
161 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
162 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
163 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
164 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
165 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
166 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
167 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
168 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
169 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
170 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
171 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
172 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
173 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
174 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
175 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
176 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
177 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
178 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
179 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
180 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
181 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
182 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
183 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
184 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200185 { 0x50, 0x1e }, { 0x51, 0x5d },
186 { 0x50, 0x22 }, { 0x51, 0x00 },
187 { 0x50, 0x23 }, { 0x51, 0xc8 },
188 { 0x50, 0x24 }, { 0x51, 0x00 },
189 { 0x50, 0x25 }, { 0x51, 0xf0 },
190 { 0x50, 0x26 }, { 0x51, 0x00 },
191 { 0x50, 0x27 }, { 0x51, 0xc3 },
192 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehab505a0ea2016-09-04 10:43:53 -0300193 { 0x50, 0xd5 }, { 0x51, 0x01 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300194 { 0xd0, 0x00 },
195};
196
197static struct regdata mb86a20s_reset_reception[] = {
198 { 0x70, 0xf0 },
199 { 0x70, 0xff },
200 { 0x08, 0x01 },
201 { 0x08, 0x00 },
202};
203
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300204static struct regdata mb86a20s_per_ber_reset[] = {
205 { 0x53, 0x00 }, /* pre BER Counter reset */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300206 { 0x53, 0x07 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300207
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300208 { 0x5f, 0x00 }, /* post BER Counter reset */
209 { 0x5f, 0x07 },
210
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300211 { 0x50, 0xb1 }, /* PER Counter reset */
212 { 0x51, 0x07 },
213 { 0x51, 0x00 },
214};
215
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200216/*
217 * I2C read/write functions and macros
218 */
219
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300220static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300221 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300222{
223 u8 buf[] = { reg, data };
224 struct i2c_msg msg = {
225 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
226 };
227 int rc;
228
229 rc = i2c_transfer(state->i2c, &msg, 1);
230 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200231 dev_err(&state->i2c->dev,
232 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
233 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300234 return rc;
235 }
236
237 return 0;
238}
239
240static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
241 u8 i2c_addr, struct regdata *rd, int size)
242{
243 int i, rc;
244
245 for (i = 0; i < size; i++) {
246 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
247 rd[i].data);
248 if (rc < 0)
249 return rc;
250 }
251 return 0;
252}
253
254static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
255 u8 i2c_addr, u8 reg)
256{
257 u8 val;
258 int rc;
259 struct i2c_msg msg[] = {
260 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
261 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
262 };
263
264 rc = i2c_transfer(state->i2c, msg, 2);
265
266 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200267 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
268 __func__, reg, rc);
269 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300270 }
271
272 return val;
273}
274
275#define mb86a20s_readreg(state, reg) \
276 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
277#define mb86a20s_writereg(state, reg, val) \
278 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
279#define mb86a20s_writeregdata(state, regdata) \
280 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
281 regdata, ARRAY_SIZE(regdata))
282
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300283/*
284 * Ancillary internal routines (likely compiled inlined)
285 *
286 * The functions below assume that gateway lock has already obtained
287 */
288
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300289static int mb86a20s_read_status(struct dvb_frontend *fe, enum fe_status *status)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300290{
291 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200292 int val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300293
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200294 *status = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300295
Colin Ian Kingeca2d342016-07-15 12:59:15 -0300296 val = mb86a20s_readreg(state, 0x0a);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200297 if (val < 0)
298 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300299
Colin Ian Kingeca2d342016-07-15 12:59:15 -0300300 val &= 0xf;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200301 if (val >= 2)
302 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300303
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200304 if (val >= 4)
305 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300306
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200307 if (val >= 5)
308 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300309
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200310 if (val >= 7)
311 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300312
Mauro Carvalho Chehabdafb65f2016-09-04 10:16:18 -0300313 /*
314 * Actually, on state S8, it starts receiving TS, but the TS
315 * output is only on normal state after the transition to S9.
316 */
317 if (val >= 9)
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200318 *status |= FE_HAS_LOCK;
319
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200320 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
321 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200322
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -0300323 return val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300324}
325
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300326static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300327{
328 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300329 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300330 int rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300331 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300332
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300333 if (state->get_strength_time &&
334 (!time_after(jiffies, state->get_strength_time)))
335 return c->strength.stat[0].uvalue;
336
337 /* Reset its value if an error happen */
338 c->strength.stat[0].uvalue = 0;
339
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300340 /* Does a binary search to get RF strength */
341 rf_max = 0xfff;
342 rf_min = 0;
343 do {
344 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300345 rc = mb86a20s_writereg(state, 0x04, 0x1f);
346 if (rc < 0)
347 return rc;
348 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
349 if (rc < 0)
350 return rc;
351 rc = mb86a20s_writereg(state, 0x04, 0x20);
352 if (rc < 0)
353 return rc;
Mauro Carvalho Chehabdad78c52013-03-01 16:15:16 -0300354 rc = mb86a20s_writereg(state, 0x05, rf);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300355 if (rc < 0)
356 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300357
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300358 rc = mb86a20s_readreg(state, 0x02);
359 if (rc < 0)
360 return rc;
361 if (rc & 0x08)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300362 rf_min = (rf_max + rf_min) / 2;
363 else
364 rf_max = (rf_max + rf_min) / 2;
365 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300366 rf = (rf_max + rf_min) / 2;
367
368 /* Rescale it from 2^12 (4096) to 2^16 */
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300369 rf = rf << (16 - 12);
370 if (rf)
371 rf |= (1 << 12) - 1;
372
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200373 dev_dbg(&state->i2c->dev,
374 "%s: signal strength = %d (%d < RF=%d < %d)\n",
375 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300376 c->strength.stat[0].uvalue = rf;
377 state->get_strength_time = jiffies +
378 msecs_to_jiffies(1000);
379 return 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300380 }
381 } while (1);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300382}
383
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200384static int mb86a20s_get_modulation(struct mb86a20s_state *state,
385 unsigned layer)
386{
387 int rc;
388 static unsigned char reg[] = {
389 [0] = 0x86, /* Layer A */
390 [1] = 0x8a, /* Layer B */
391 [2] = 0x8e, /* Layer C */
392 };
393
Dan Carpenter82033bc2012-01-13 02:28:34 -0300394 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200395 return -EINVAL;
396 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
397 if (rc < 0)
398 return rc;
399 rc = mb86a20s_readreg(state, 0x6e);
400 if (rc < 0)
401 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300402 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200403 case 0:
404 return DQPSK;
405 case 1:
406 return QPSK;
407 case 2:
408 return QAM_16;
409 case 3:
410 return QAM_64;
411 default:
412 return QAM_AUTO;
413 }
414}
415
416static int mb86a20s_get_fec(struct mb86a20s_state *state,
417 unsigned layer)
418{
419 int rc;
420
421 static unsigned char reg[] = {
422 [0] = 0x87, /* Layer A */
423 [1] = 0x8b, /* Layer B */
424 [2] = 0x8f, /* Layer C */
425 };
426
Dan Carpenter82033bc2012-01-13 02:28:34 -0300427 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200428 return -EINVAL;
429 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
430 if (rc < 0)
431 return rc;
432 rc = mb86a20s_readreg(state, 0x6e);
433 if (rc < 0)
434 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300435 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200436 case 0:
437 return FEC_1_2;
438 case 1:
439 return FEC_2_3;
440 case 2:
441 return FEC_3_4;
442 case 3:
443 return FEC_5_6;
444 case 4:
445 return FEC_7_8;
446 default:
447 return FEC_AUTO;
448 }
449}
450
451static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
452 unsigned layer)
453{
454 int rc;
Mauro Carvalho Chehab42473682014-07-11 21:37:47 -0300455 int interleaving[] = {
456 0, 1, 2, 4, 8
457 };
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200458
459 static unsigned char reg[] = {
460 [0] = 0x88, /* Layer A */
461 [1] = 0x8c, /* Layer B */
462 [2] = 0x90, /* Layer C */
463 };
464
Dan Carpenter82033bc2012-01-13 02:28:34 -0300465 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200466 return -EINVAL;
467 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
468 if (rc < 0)
469 return rc;
470 rc = mb86a20s_readreg(state, 0x6e);
471 if (rc < 0)
472 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300473
Mauro Carvalho Chehab42473682014-07-11 21:37:47 -0300474 return interleaving[(rc >> 4) & 0x07];
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200475}
476
477static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
478 unsigned layer)
479{
480 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200481 static unsigned char reg[] = {
482 [0] = 0x89, /* Layer A */
483 [1] = 0x8d, /* Layer B */
484 [2] = 0x91, /* Layer C */
485 };
486
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200487 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
488
Dan Carpenter82033bc2012-01-13 02:28:34 -0300489 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200490 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200491
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200492 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
493 if (rc < 0)
494 return rc;
495 rc = mb86a20s_readreg(state, 0x6e);
496 if (rc < 0)
497 return rc;
498 count = (rc >> 4) & 0x0f;
499
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200500 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
501
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200502 return count;
503}
504
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300505static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
506{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200507 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300508 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
509
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200510 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
511
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300512 /* Fixed parameters */
513 c->delivery_system = SYS_ISDBT;
514 c->bandwidth_hz = 6000000;
515
516 /* Initialize values that will be later autodetected */
517 c->isdbt_layer_enabled = 0;
518 c->transmission_mode = TRANSMISSION_MODE_AUTO;
519 c->guard_interval = GUARD_INTERVAL_AUTO;
520 c->isdbt_sb_mode = 0;
521 c->isdbt_sb_segment_count = 0;
522}
523
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300524/*
525 * Estimates the bit rate using the per-segment bit rate given by
526 * ABNT/NBR 15601 spec (table 4).
527 */
528static u32 isdbt_rate[3][5][4] = {
529 { /* DQPSK/QPSK */
530 { 280850, 312060, 330420, 340430 }, /* 1/2 */
531 { 374470, 416080, 440560, 453910 }, /* 2/3 */
532 { 421280, 468090, 495630, 510650 }, /* 3/4 */
533 { 468090, 520100, 550700, 567390 }, /* 5/6 */
534 { 491500, 546110, 578230, 595760 }, /* 7/8 */
535 }, { /* QAM16 */
536 { 561710, 624130, 660840, 680870 }, /* 1/2 */
537 { 748950, 832170, 881120, 907820 }, /* 2/3 */
538 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
539 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
540 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
541 }, { /* QAM64 */
542 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
543 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
544 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
545 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
546 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
547 }
548};
549
550static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300551 u32 modulation, u32 forward_error_correction,
Mauro Carvalho Chehab277bfd22014-07-11 21:37:48 -0300552 u32 guard_interval,
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300553 u32 segment)
554{
555 struct mb86a20s_state *state = fe->demodulator_priv;
556 u32 rate;
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300557 int mod, fec, guard;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300558
559 /*
Mauro Carvalho Chehab277bfd22014-07-11 21:37:48 -0300560 * If modulation/fec/guard is not detected, the default is
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300561 * to consider the lowest bit rate, to avoid taking too long time
562 * to get BER.
563 */
564 switch (modulation) {
565 case DQPSK:
566 case QPSK:
567 default:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300568 mod = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300569 break;
570 case QAM_16:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300571 mod = 1;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300572 break;
573 case QAM_64:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300574 mod = 2;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300575 break;
576 }
577
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300578 switch (forward_error_correction) {
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300579 default:
580 case FEC_1_2:
581 case FEC_AUTO:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300582 fec = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300583 break;
584 case FEC_2_3:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300585 fec = 1;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300586 break;
587 case FEC_3_4:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300588 fec = 2;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300589 break;
590 case FEC_5_6:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300591 fec = 3;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300592 break;
593 case FEC_7_8:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300594 fec = 4;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300595 break;
596 }
597
Mauro Carvalho Chehab277bfd22014-07-11 21:37:48 -0300598 switch (guard_interval) {
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300599 default:
600 case GUARD_INTERVAL_1_4:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300601 guard = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300602 break;
603 case GUARD_INTERVAL_1_8:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300604 guard = 1;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300605 break;
606 case GUARD_INTERVAL_1_16:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300607 guard = 2;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300608 break;
609 case GUARD_INTERVAL_1_32:
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300610 guard = 3;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300611 break;
612 }
613
614 /* Samples BER at BER_SAMPLING_RATE seconds */
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300615 rate = isdbt_rate[mod][fec][guard] * segment * BER_SAMPLING_RATE;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300616
617 /* Avoids sampling too quickly or to overflow the register */
618 if (rate < 256)
619 rate = 256;
620 else if (rate > (1 << 24) - 1)
621 rate = (1 << 24) - 1;
622
623 dev_dbg(&state->i2c->dev,
624 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
Mauro Carvalho Chehab0562aef2013-04-07 21:07:47 -0300625 __func__, 'A' + layer,
626 segment * isdbt_rate[mod][fec][guard]/1000,
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300627 rate, rate);
628
Mauro Carvalho Chehabb1f89332013-04-07 21:06:12 -0300629 state->estimated_rate[layer] = rate;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300630}
631
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300632static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300633{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200634 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300635 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300636 int layer, rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300637
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200638 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
639
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300640 /* Reset frontend cache to default values */
641 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200642
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200643 /* Check for partial reception */
644 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300645 if (rc < 0)
646 return rc;
647 rc = mb86a20s_readreg(state, 0x6e);
648 if (rc < 0)
649 return rc;
650 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200651
652 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300653
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300654 for (layer = 0; layer < NUM_LAYERS; layer++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200655 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300656 __func__, 'A' + layer);
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200657
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300658 rc = mb86a20s_get_segment_count(state, layer);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300659 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200660 goto noperlayer_error;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300661 if (rc >= 0 && rc < 14) {
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300662 c->layer[layer].segment_count = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300663 } else {
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300664 c->layer[layer].segment_count = 0;
665 state->estimated_rate[layer] = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200666 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300667 }
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300668 c->isdbt_layer_enabled |= 1 << layer;
669 rc = mb86a20s_get_modulation(state, layer);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300670 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200671 goto noperlayer_error;
672 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
673 __func__, rc);
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300674 c->layer[layer].modulation = rc;
675 rc = mb86a20s_get_fec(state, layer);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300676 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200677 goto noperlayer_error;
678 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
679 __func__, rc);
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300680 c->layer[layer].fec = rc;
681 rc = mb86a20s_get_interleaving(state, layer);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300682 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200683 goto noperlayer_error;
684 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
685 __func__, rc);
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300686 c->layer[layer].interleaving = rc;
687 mb86a20s_layer_bitrate(fe, layer, c->layer[layer].modulation,
688 c->layer[layer].fec,
Mauro Carvalho Chehab277bfd22014-07-11 21:37:48 -0300689 c->guard_interval,
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -0300690 c->layer[layer].segment_count);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200691 }
692
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200693 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300694 if (rc < 0)
695 return rc;
696 if ((rc & 0x60) == 0x20) {
697 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200698 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300699 if (!c->isdbt_sb_segment_count)
700 c->isdbt_sb_segment_count = 1;
701 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200702
703 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200704 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300705 if (rc < 0)
706 return rc;
Mauro Carvalho Chehab276dfc42014-07-21 14:55:05 -0300707 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300708 if ((rc & 0x60) == 0x20) {
Mauro Carvalho Chehab276dfc42014-07-21 14:55:05 -0300709 /* Only modes 2 and 3 are supported */
710 switch ((rc >> 2) & 0x03) {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300711 case 1:
712 c->transmission_mode = TRANSMISSION_MODE_4K;
713 break;
714 case 2:
715 c->transmission_mode = TRANSMISSION_MODE_8K;
716 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200717 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300718 }
Mauro Carvalho Chehab276dfc42014-07-21 14:55:05 -0300719 c->guard_interval = GUARD_INTERVAL_AUTO;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300720 if (!(rc & 0x10)) {
Mauro Carvalho Chehab276dfc42014-07-21 14:55:05 -0300721 /* Guard interval 1/32 is not supported */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300722 switch (rc & 0x3) {
723 case 0:
724 c->guard_interval = GUARD_INTERVAL_1_4;
725 break;
726 case 1:
727 c->guard_interval = GUARD_INTERVAL_1_8;
728 break;
729 case 2:
730 c->guard_interval = GUARD_INTERVAL_1_16;
731 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200732 }
733 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300734 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200735
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200736noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300737
738 /* per-layer info is incomplete; discard all per-layer */
739 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300740
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300741 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300742}
743
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300744static int mb86a20s_reset_counters(struct dvb_frontend *fe)
745{
746 struct mb86a20s_state *state = fe->demodulator_priv;
747 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
748 int rc, val;
749
750 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
751
752 /* Reset the counters, if the channel changed */
753 if (state->last_frequency != c->frequency) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300754 memset(&c->cnr, 0, sizeof(c->cnr));
755 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
756 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300757 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
758 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300759 memset(&c->block_error, 0, sizeof(c->block_error));
760 memset(&c->block_count, 0, sizeof(c->block_count));
761
762 state->last_frequency = c->frequency;
763 }
764
765 /* Clear status for most stats */
766
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300767 /* BER/PER counter reset */
768 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300769 if (rc < 0)
770 goto err;
771
772 /* CNR counter reset */
773 rc = mb86a20s_readreg(state, 0x45);
774 if (rc < 0)
775 goto err;
776 val = rc;
777 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
778 if (rc < 0)
779 goto err;
780 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
781 if (rc < 0)
782 goto err;
783
784 /* MER counter reset */
785 rc = mb86a20s_writereg(state, 0x50, 0x50);
786 if (rc < 0)
787 goto err;
788 rc = mb86a20s_readreg(state, 0x51);
789 if (rc < 0)
790 goto err;
791 val = rc;
792 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
793 if (rc < 0)
794 goto err;
795 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
796 if (rc < 0)
797 goto err;
798
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300799 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300800err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300801 dev_err(&state->i2c->dev,
802 "%s: Can't reset FE statistics (error %d).\n",
803 __func__, rc);
804ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300805 return rc;
806}
807
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300808static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
809 unsigned layer,
810 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300811{
812 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300813 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300814
815 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
816
Mauro Carvalho Chehab4f62a202013-04-07 20:50:34 -0300817 if (layer >= NUM_LAYERS)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300818 return -EINVAL;
819
820 /* Check if the BER measures are already available */
821 rc = mb86a20s_readreg(state, 0x54);
822 if (rc < 0)
823 return rc;
824
825 /* Check if data is available for that layer */
826 if (!(rc & (1 << layer))) {
827 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300828 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300829 __func__, 'A' + layer);
830 return -EBUSY;
831 }
832
833 /* Read Bit Error Count */
834 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
835 if (rc < 0)
836 return rc;
837 *error = rc << 16;
838 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
839 if (rc < 0)
840 return rc;
841 *error |= rc << 8;
842 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
843 if (rc < 0)
844 return rc;
845 *error |= rc;
846
847 dev_dbg(&state->i2c->dev,
848 "%s: bit error before Viterbi for layer %c: %d.\n",
849 __func__, 'A' + layer, *error);
850
851 /* Read Bit Count */
852 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
853 if (rc < 0)
854 return rc;
855 rc = mb86a20s_readreg(state, 0x51);
856 if (rc < 0)
857 return rc;
858 *count = rc << 16;
859 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
860 if (rc < 0)
861 return rc;
862 rc = mb86a20s_readreg(state, 0x51);
863 if (rc < 0)
864 return rc;
865 *count |= rc << 8;
866 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
867 if (rc < 0)
868 return rc;
869 rc = mb86a20s_readreg(state, 0x51);
870 if (rc < 0)
871 return rc;
872 *count |= rc;
873
874 dev_dbg(&state->i2c->dev,
875 "%s: bit count before Viterbi for layer %c: %d.\n",
876 __func__, 'A' + layer, *count);
877
878
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300879 /*
880 * As we get TMCC data from the frontend, we can better estimate the
881 * BER bit counters, in order to do the BER measure during a longer
882 * time. Use those data, if available, to update the bit count
883 * measure.
884 */
885
886 if (state->estimated_rate[layer]
887 && state->estimated_rate[layer] != *count) {
888 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300889 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300890 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300891
892 /* Turn off BER before Viterbi */
893 rc = mb86a20s_writereg(state, 0x52, 0x00);
894
895 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300896 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
897 if (rc < 0)
898 return rc;
899 rc = mb86a20s_writereg(state, 0x51,
900 state->estimated_rate[layer] >> 16);
901 if (rc < 0)
902 return rc;
903 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
904 if (rc < 0)
905 return rc;
906 rc = mb86a20s_writereg(state, 0x51,
907 state->estimated_rate[layer] >> 8);
908 if (rc < 0)
909 return rc;
910 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
911 if (rc < 0)
912 return rc;
913 rc = mb86a20s_writereg(state, 0x51,
914 state->estimated_rate[layer]);
915 if (rc < 0)
916 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300917
918 /* Turn on BER before Viterbi */
919 rc = mb86a20s_writereg(state, 0x52, 0x01);
920
921 /* Reset all preBER counters */
922 rc = mb86a20s_writereg(state, 0x53, 0x00);
923 if (rc < 0)
924 return rc;
925 rc = mb86a20s_writereg(state, 0x53, 0x07);
926 } else {
927 /* Reset counter to collect new data */
928 rc = mb86a20s_readreg(state, 0x53);
929 if (rc < 0)
930 return rc;
931 val = rc;
932 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
933 if (rc < 0)
934 return rc;
935 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300936 }
937
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300938 return rc;
939}
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300940
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300941static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
942 unsigned layer,
943 u32 *error, u32 *count)
944{
945 struct mb86a20s_state *state = fe->demodulator_priv;
946 u32 counter, collect_rate;
947 int rc, val;
948
949 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
950
Mauro Carvalho Chehab4f62a202013-04-07 20:50:34 -0300951 if (layer >= NUM_LAYERS)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300952 return -EINVAL;
953
954 /* Check if the BER measures are already available */
955 rc = mb86a20s_readreg(state, 0x60);
956 if (rc < 0)
957 return rc;
958
959 /* Check if data is available for that layer */
960 if (!(rc & (1 << layer))) {
961 dev_dbg(&state->i2c->dev,
962 "%s: post BER for layer %c is not available yet.\n",
963 __func__, 'A' + layer);
964 return -EBUSY;
965 }
966
967 /* Read Bit Error Count */
968 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
969 if (rc < 0)
970 return rc;
971 *error = rc << 16;
972 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
973 if (rc < 0)
974 return rc;
975 *error |= rc << 8;
976 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
977 if (rc < 0)
978 return rc;
979 *error |= rc;
980
981 dev_dbg(&state->i2c->dev,
982 "%s: post bit error for layer %c: %d.\n",
983 __func__, 'A' + layer, *error);
984
985 /* Read Bit Count */
986 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
987 if (rc < 0)
988 return rc;
989 rc = mb86a20s_readreg(state, 0x51);
990 if (rc < 0)
991 return rc;
992 counter = rc << 8;
993 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
994 if (rc < 0)
995 return rc;
996 rc = mb86a20s_readreg(state, 0x51);
997 if (rc < 0)
998 return rc;
999 counter |= rc;
1000 *count = counter * 204 * 8;
1001
1002 dev_dbg(&state->i2c->dev,
1003 "%s: post bit count for layer %c: %d.\n",
1004 __func__, 'A' + layer, *count);
1005
1006 /*
1007 * As we get TMCC data from the frontend, we can better estimate the
1008 * BER bit counters, in order to do the BER measure during a longer
1009 * time. Use those data, if available, to update the bit count
1010 * measure.
1011 */
1012
1013 if (!state->estimated_rate[layer])
1014 goto reset_measurement;
1015
1016 collect_rate = state->estimated_rate[layer] / 204 / 8;
1017 if (collect_rate < 32)
1018 collect_rate = 32;
1019 if (collect_rate > 65535)
1020 collect_rate = 65535;
1021 if (collect_rate != counter) {
1022 dev_dbg(&state->i2c->dev,
1023 "%s: updating postBER counter on layer %c to %d.\n",
1024 __func__, 'A' + layer, collect_rate);
1025
1026 /* Turn off BER after Viterbi */
1027 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1028
1029 /* Update counter for this layer */
1030 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1031 if (rc < 0)
1032 return rc;
1033 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1034 if (rc < 0)
1035 return rc;
1036 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1037 if (rc < 0)
1038 return rc;
1039 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1040 if (rc < 0)
1041 return rc;
1042
1043 /* Turn on BER after Viterbi */
1044 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1045
1046 /* Reset all preBER counters */
1047 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1048 if (rc < 0)
1049 return rc;
1050 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1051
1052 return rc;
1053 }
1054
1055reset_measurement:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001056 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001057 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001058 if (rc < 0)
1059 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001060 val = rc;
1061 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1062 if (rc < 0)
1063 return rc;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001064 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001065
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001066 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001067}
1068
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001069static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1070 unsigned layer,
1071 u32 *error, u32 *count)
1072{
1073 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001074 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001075 u32 collect_rate;
1076 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1077
Mauro Carvalho Chehab4f62a202013-04-07 20:50:34 -03001078 if (layer >= NUM_LAYERS)
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001079 return -EINVAL;
1080
1081 /* Check if the PER measures are already available */
1082 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1083 if (rc < 0)
1084 return rc;
1085 rc = mb86a20s_readreg(state, 0x51);
1086 if (rc < 0)
1087 return rc;
1088
1089 /* Check if data is available for that layer */
1090
1091 if (!(rc & (1 << layer))) {
1092 dev_dbg(&state->i2c->dev,
1093 "%s: block counts for layer %c aren't available yet.\n",
1094 __func__, 'A' + layer);
1095 return -EBUSY;
1096 }
1097
1098 /* Read Packet error Count */
1099 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1100 if (rc < 0)
1101 return rc;
1102 rc = mb86a20s_readreg(state, 0x51);
1103 if (rc < 0)
1104 return rc;
1105 *error = rc << 8;
1106 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1107 if (rc < 0)
1108 return rc;
1109 rc = mb86a20s_readreg(state, 0x51);
1110 if (rc < 0)
1111 return rc;
1112 *error |= rc;
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001113 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001114 __func__, 'A' + layer, *error);
1115
1116 /* Read Bit Count */
1117 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1118 if (rc < 0)
1119 return rc;
1120 rc = mb86a20s_readreg(state, 0x51);
1121 if (rc < 0)
1122 return rc;
1123 *count = rc << 8;
1124 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1125 if (rc < 0)
1126 return rc;
1127 rc = mb86a20s_readreg(state, 0x51);
1128 if (rc < 0)
1129 return rc;
1130 *count |= rc;
1131
1132 dev_dbg(&state->i2c->dev,
1133 "%s: block count for layer %c: %d.\n",
1134 __func__, 'A' + layer, *count);
1135
1136 /*
1137 * As we get TMCC data from the frontend, we can better estimate the
1138 * BER bit counters, in order to do the BER measure during a longer
1139 * time. Use those data, if available, to update the bit count
1140 * measure.
1141 */
1142
1143 if (!state->estimated_rate[layer])
1144 goto reset_measurement;
1145
1146 collect_rate = state->estimated_rate[layer] / 204 / 8;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001147 if (collect_rate < 32)
1148 collect_rate = 32;
1149 if (collect_rate > 65535)
1150 collect_rate = 65535;
1151
1152 if (collect_rate != *count) {
1153 dev_dbg(&state->i2c->dev,
1154 "%s: updating PER counter on layer %c to %d.\n",
1155 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001156
1157 /* Stop PER measurement */
1158 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1159 if (rc < 0)
1160 return rc;
1161 rc = mb86a20s_writereg(state, 0x51, 0x00);
1162 if (rc < 0)
1163 return rc;
1164
1165 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001166 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1167 if (rc < 0)
1168 return rc;
1169 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1170 if (rc < 0)
1171 return rc;
1172 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1173 if (rc < 0)
1174 return rc;
1175 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1176 if (rc < 0)
1177 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001178
1179 /* start PER measurement */
1180 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1181 if (rc < 0)
1182 return rc;
1183 rc = mb86a20s_writereg(state, 0x51, 0x07);
1184 if (rc < 0)
1185 return rc;
1186
1187 /* Reset all counters to collect new data */
1188 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1189 if (rc < 0)
1190 return rc;
1191 rc = mb86a20s_writereg(state, 0x51, 0x07);
1192 if (rc < 0)
1193 return rc;
1194 rc = mb86a20s_writereg(state, 0x51, 0x00);
1195
1196 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001197 }
1198
1199reset_measurement:
1200 /* Reset counter to collect new data */
1201 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1202 if (rc < 0)
1203 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001204 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001205 if (rc < 0)
1206 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001207 val = rc;
1208 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001209 if (rc < 0)
1210 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001211 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001212
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001213 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001214}
1215
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001216struct linear_segments {
1217 unsigned x, y;
1218};
1219
1220/*
1221 * All tables below return a dB/1000 measurement
1222 */
1223
Hans Verkuilce081312014-08-20 18:08:17 -03001224static const struct linear_segments cnr_to_db_table[] = {
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001225 { 19648, 0},
1226 { 18187, 1000},
1227 { 16534, 2000},
1228 { 14823, 3000},
1229 { 13161, 4000},
1230 { 11622, 5000},
1231 { 10279, 6000},
1232 { 9089, 7000},
1233 { 8042, 8000},
1234 { 7137, 9000},
1235 { 6342, 10000},
1236 { 5641, 11000},
1237 { 5030, 12000},
1238 { 4474, 13000},
1239 { 3988, 14000},
1240 { 3556, 15000},
1241 { 3180, 16000},
1242 { 2841, 17000},
1243 { 2541, 18000},
1244 { 2276, 19000},
1245 { 2038, 20000},
1246 { 1800, 21000},
1247 { 1625, 22000},
1248 { 1462, 23000},
1249 { 1324, 24000},
1250 { 1175, 25000},
1251 { 1063, 26000},
1252 { 980, 27000},
1253 { 907, 28000},
1254 { 840, 29000},
1255 { 788, 30000},
1256};
1257
Hans Verkuilce081312014-08-20 18:08:17 -03001258static const struct linear_segments cnr_64qam_table[] = {
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001259 { 3922688, 0},
1260 { 3920384, 1000},
1261 { 3902720, 2000},
1262 { 3894784, 3000},
1263 { 3882496, 4000},
1264 { 3872768, 5000},
1265 { 3858944, 6000},
1266 { 3851520, 7000},
1267 { 3838976, 8000},
1268 { 3829248, 9000},
1269 { 3818240, 10000},
1270 { 3806976, 11000},
1271 { 3791872, 12000},
1272 { 3767040, 13000},
1273 { 3720960, 14000},
1274 { 3637504, 15000},
1275 { 3498496, 16000},
1276 { 3296000, 17000},
1277 { 3031040, 18000},
1278 { 2715392, 19000},
1279 { 2362624, 20000},
1280 { 1963264, 21000},
1281 { 1649664, 22000},
1282 { 1366784, 23000},
1283 { 1120768, 24000},
1284 { 890880, 25000},
1285 { 723456, 26000},
1286 { 612096, 27000},
1287 { 518912, 28000},
1288 { 448256, 29000},
1289 { 388864, 30000},
1290};
1291
Hans Verkuilce081312014-08-20 18:08:17 -03001292static const struct linear_segments cnr_16qam_table[] = {
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001293 { 5314816, 0},
1294 { 5219072, 1000},
1295 { 5118720, 2000},
1296 { 4998912, 3000},
1297 { 4875520, 4000},
1298 { 4736000, 5000},
1299 { 4604160, 6000},
1300 { 4458752, 7000},
1301 { 4300288, 8000},
1302 { 4092928, 9000},
1303 { 3836160, 10000},
1304 { 3521024, 11000},
1305 { 3155968, 12000},
1306 { 2756864, 13000},
1307 { 2347008, 14000},
1308 { 1955072, 15000},
1309 { 1593600, 16000},
1310 { 1297920, 17000},
1311 { 1043968, 18000},
1312 { 839680, 19000},
1313 { 672256, 20000},
1314 { 523008, 21000},
1315 { 424704, 22000},
1316 { 345088, 23000},
1317 { 280064, 24000},
1318 { 221440, 25000},
1319 { 179712, 26000},
1320 { 151040, 27000},
1321 { 128512, 28000},
1322 { 110080, 29000},
1323 { 95744, 30000},
1324};
1325
Hans Verkuilce081312014-08-20 18:08:17 -03001326static const struct linear_segments cnr_qpsk_table[] = {
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001327 { 2834176, 0},
1328 { 2683648, 1000},
1329 { 2536960, 2000},
1330 { 2391808, 3000},
1331 { 2133248, 4000},
1332 { 1906176, 5000},
1333 { 1666560, 6000},
1334 { 1422080, 7000},
1335 { 1189632, 8000},
1336 { 976384, 9000},
1337 { 790272, 10000},
1338 { 633344, 11000},
1339 { 505600, 12000},
1340 { 402944, 13000},
1341 { 320768, 14000},
1342 { 255488, 15000},
1343 { 204032, 16000},
1344 { 163072, 17000},
1345 { 130304, 18000},
1346 { 105216, 19000},
1347 { 83456, 20000},
1348 { 65024, 21000},
1349 { 52480, 22000},
1350 { 42752, 23000},
1351 { 34560, 24000},
1352 { 27136, 25000},
1353 { 22016, 26000},
1354 { 18432, 27000},
1355 { 15616, 28000},
1356 { 13312, 29000},
1357 { 11520, 30000},
1358};
1359
Hans Verkuilce081312014-08-20 18:08:17 -03001360static u32 interpolate_value(u32 value, const struct linear_segments *segments,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001361 unsigned len)
1362{
1363 u64 tmp64;
1364 u32 dx, dy;
1365 int i, ret;
1366
1367 if (value >= segments[0].x)
1368 return segments[0].y;
1369 if (value < segments[len-1].x)
1370 return segments[len-1].y;
1371
1372 for (i = 1; i < len - 1; i++) {
1373 /* If value is identical, no need to interpolate */
1374 if (value == segments[i].x)
1375 return segments[i].y;
1376 if (value > segments[i].x)
1377 break;
1378 }
1379
1380 /* Linear interpolation between the two (x,y) points */
1381 dy = segments[i].y - segments[i - 1].y;
1382 dx = segments[i - 1].x - segments[i].x;
1383 tmp64 = value - segments[i].x;
1384 tmp64 *= dy;
1385 do_div(tmp64, dx);
1386 ret = segments[i].y - tmp64;
1387
1388 return ret;
1389}
1390
1391static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1392{
1393 struct mb86a20s_state *state = fe->demodulator_priv;
1394 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1395 u32 cnr_linear, cnr;
1396 int rc, val;
1397
1398 /* Check if CNR is available */
1399 rc = mb86a20s_readreg(state, 0x45);
1400 if (rc < 0)
1401 return rc;
1402
1403 if (!(rc & 0x40)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001404 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001405 __func__);
1406 return -EBUSY;
1407 }
1408 val = rc;
1409
1410 rc = mb86a20s_readreg(state, 0x46);
1411 if (rc < 0)
1412 return rc;
1413 cnr_linear = rc << 8;
1414
1415 rc = mb86a20s_readreg(state, 0x46);
1416 if (rc < 0)
1417 return rc;
1418 cnr_linear |= rc;
1419
1420 cnr = interpolate_value(cnr_linear,
1421 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1422
1423 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1424 c->cnr.stat[0].svalue = cnr;
1425
1426 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1427 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1428
1429 /* CNR counter reset */
1430 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1431 if (rc < 0)
1432 return rc;
1433 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1434
1435 return rc;
1436}
1437
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001438static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001439{
1440 struct mb86a20s_state *state = fe->demodulator_priv;
1441 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1442 u32 mer, cnr;
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001443 int rc, val, layer;
Hans Verkuilce081312014-08-20 18:08:17 -03001444 const struct linear_segments *segs;
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001445 unsigned segs_len;
1446
1447 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1448
1449 /* Check if the measures are already available */
1450 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1451 if (rc < 0)
1452 return rc;
1453 rc = mb86a20s_readreg(state, 0x51);
1454 if (rc < 0)
1455 return rc;
1456
1457 /* Check if data is available */
1458 if (!(rc & 0x01)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001459 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001460 "%s: MER measures aren't available yet.\n", __func__);
1461 return -EBUSY;
1462 }
1463
1464 /* Read all layers */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001465 for (layer = 0; layer < NUM_LAYERS; layer++) {
1466 if (!(c->isdbt_layer_enabled & (1 << layer))) {
1467 c->cnr.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001468 continue;
1469 }
1470
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001471 rc = mb86a20s_writereg(state, 0x50, 0x52 + layer * 3);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001472 if (rc < 0)
1473 return rc;
1474 rc = mb86a20s_readreg(state, 0x51);
1475 if (rc < 0)
1476 return rc;
1477 mer = rc << 16;
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001478 rc = mb86a20s_writereg(state, 0x50, 0x53 + layer * 3);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001479 if (rc < 0)
1480 return rc;
1481 rc = mb86a20s_readreg(state, 0x51);
1482 if (rc < 0)
1483 return rc;
1484 mer |= rc << 8;
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001485 rc = mb86a20s_writereg(state, 0x50, 0x54 + layer * 3);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001486 if (rc < 0)
1487 return rc;
1488 rc = mb86a20s_readreg(state, 0x51);
1489 if (rc < 0)
1490 return rc;
1491 mer |= rc;
1492
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001493 switch (c->layer[layer].modulation) {
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001494 case DQPSK:
1495 case QPSK:
1496 segs = cnr_qpsk_table;
1497 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1498 break;
1499 case QAM_16:
1500 segs = cnr_16qam_table;
1501 segs_len = ARRAY_SIZE(cnr_16qam_table);
1502 break;
1503 default:
1504 case QAM_64:
1505 segs = cnr_64qam_table;
1506 segs_len = ARRAY_SIZE(cnr_64qam_table);
1507 break;
1508 }
1509 cnr = interpolate_value(mer, segs, segs_len);
1510
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001511 c->cnr.stat[1 + layer].scale = FE_SCALE_DECIBEL;
1512 c->cnr.stat[1 + layer].svalue = cnr;
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001513
1514 dev_dbg(&state->i2c->dev,
1515 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001516 __func__, 'A' + layer, cnr / 1000, cnr % 1000, mer);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001517
1518 }
1519
1520 /* Start a new MER measurement */
1521 /* MER counter reset */
1522 rc = mb86a20s_writereg(state, 0x50, 0x50);
1523 if (rc < 0)
1524 return rc;
1525 rc = mb86a20s_readreg(state, 0x51);
1526 if (rc < 0)
1527 return rc;
1528 val = rc;
1529
1530 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1531 if (rc < 0)
1532 return rc;
1533 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1534 if (rc < 0)
1535 return rc;
1536
1537 return 0;
1538}
1539
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001540static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1541{
1542 struct mb86a20s_state *state = fe->demodulator_priv;
1543 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001544 int layer;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001545
1546 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1547
1548 /* Fill the length of each status counter */
1549
1550 /* Only global stats */
1551 c->strength.len = 1;
1552
1553 /* Per-layer stats - 3 layers + global */
Mauro Carvalho Chehab4f62a202013-04-07 20:50:34 -03001554 c->cnr.len = NUM_LAYERS + 1;
1555 c->pre_bit_error.len = NUM_LAYERS + 1;
1556 c->pre_bit_count.len = NUM_LAYERS + 1;
1557 c->post_bit_error.len = NUM_LAYERS + 1;
1558 c->post_bit_count.len = NUM_LAYERS + 1;
1559 c->block_error.len = NUM_LAYERS + 1;
1560 c->block_count.len = NUM_LAYERS + 1;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001561
1562 /* Signal is always available */
1563 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1564 c->strength.stat[0].uvalue = 0;
1565
1566 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001567 for (layer = 0; layer < NUM_LAYERS + 1; layer++) {
1568 c->cnr.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1569 c->pre_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1570 c->pre_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1571 c->post_bit_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1572 c->post_bit_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1573 c->block_error.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
1574 c->block_count.stat[layer].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001575 }
1576}
1577
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001578static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001579{
1580 struct mb86a20s_state *state = fe->demodulator_priv;
1581 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001582 int rc = 0, layer;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001583 u32 bit_error = 0, bit_count = 0;
1584 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001585 u32 t_post_bit_error = 0, t_post_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001586 u32 block_error = 0, block_count = 0;
1587 u32 t_block_error = 0, t_block_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001588 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1589 int per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001590
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001591 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1592
1593 mb86a20s_get_main_CNR(fe);
1594
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001595 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001596 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001597
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001598 /*
1599 * At state 7, only CNR is available
1600 * For BER measures, state=9 is required
1601 * FIXME: we may get MER measures with state=8
1602 */
1603 if (status_nr < 9)
1604 return 0;
1605
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001606 for (layer = 0; layer < NUM_LAYERS; layer++) {
1607 if (c->isdbt_layer_enabled & (1 << layer)) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001608 /* Layer is active and has rc segments */
1609 active_layers++;
1610
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001611 /* Handle BER before vterbi */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001612 rc = mb86a20s_get_pre_ber(fe, layer,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001613 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001614 if (rc >= 0) {
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001615 c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1616 c->pre_bit_error.stat[1 + layer].uvalue += bit_error;
1617 c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1618 c->pre_bit_count.stat[1 + layer].uvalue += bit_count;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001619 } else if (rc != -EBUSY) {
1620 /*
1621 * If an I/O error happened,
1622 * measures are now unavailable
1623 */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001624 c->pre_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1625 c->pre_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001626 dev_err(&state->i2c->dev,
1627 "%s: Can't get BER for layer %c (error %d).\n",
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001628 __func__, 'A' + layer, rc);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001629 }
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001630 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001631 pre_ber_layers++;
1632
1633 /* Handle BER post vterbi */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001634 rc = mb86a20s_get_post_ber(fe, layer,
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001635 &bit_error, &bit_count);
1636 if (rc >= 0) {
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001637 c->post_bit_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1638 c->post_bit_error.stat[1 + layer].uvalue += bit_error;
1639 c->post_bit_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1640 c->post_bit_count.stat[1 + layer].uvalue += bit_count;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001641 } else if (rc != -EBUSY) {
1642 /*
1643 * If an I/O error happened,
1644 * measures are now unavailable
1645 */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001646 c->post_bit_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1647 c->post_bit_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001648 dev_err(&state->i2c->dev,
1649 "%s: Can't get BER for layer %c (error %d).\n",
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001650 __func__, 'A' + layer, rc);
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001651 }
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001652 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001653 post_ber_layers++;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001654
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001655 /* Handle Block errors for PER/UCB reports */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001656 rc = mb86a20s_get_blk_error(fe, layer,
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001657 &block_error,
1658 &block_count);
1659 if (rc >= 0) {
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001660 c->block_error.stat[1 + layer].scale = FE_SCALE_COUNTER;
1661 c->block_error.stat[1 + layer].uvalue += block_error;
1662 c->block_count.stat[1 + layer].scale = FE_SCALE_COUNTER;
1663 c->block_count.stat[1 + layer].uvalue += block_count;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001664 } else if (rc != -EBUSY) {
1665 /*
1666 * If an I/O error happened,
1667 * measures are now unavailable
1668 */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001669 c->block_error.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
1670 c->block_count.stat[1 + layer].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001671 dev_err(&state->i2c->dev,
1672 "%s: Can't get PER for layer %c (error %d).\n",
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001673 __func__, 'A' + layer, rc);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001674
1675 }
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001676 if (c->block_error.stat[1 + layer].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001677 per_layers++;
1678
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001679 /* Update total preBER */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001680 t_pre_bit_error += c->pre_bit_error.stat[1 + layer].uvalue;
1681 t_pre_bit_count += c->pre_bit_count.stat[1 + layer].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001682
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001683 /* Update total postBER */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001684 t_post_bit_error += c->post_bit_error.stat[1 + layer].uvalue;
1685 t_post_bit_count += c->post_bit_count.stat[1 + layer].uvalue;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001686
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001687 /* Update total PER */
Mauro Carvalho Chehab5cb88ca2013-04-07 20:52:16 -03001688 t_block_error += c->block_error.stat[1 + layer].uvalue;
1689 t_block_count += c->block_count.stat[1 + layer].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001690 }
1691 }
1692
1693 /*
1694 * Start showing global count if at least one error count is
1695 * available.
1696 */
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001697 if (pre_ber_layers) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001698 /*
1699 * At least one per-layer BER measure was read. We can now
1700 * calculate the total BER
1701 *
1702 * Total Bit Error/Count is calculated as the sum of the
1703 * bit errors on all active layers.
1704 */
1705 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1706 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1707 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1708 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001709 } else {
1710 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1711 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001712 }
1713
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001714 /*
1715 * Start showing global count if at least one error count is
1716 * available.
1717 */
1718 if (post_ber_layers) {
1719 /*
1720 * At least one per-layer BER measure was read. We can now
1721 * calculate the total BER
1722 *
1723 * Total Bit Error/Count is calculated as the sum of the
1724 * bit errors on all active layers.
1725 */
1726 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1727 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1728 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1729 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001730 } else {
1731 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1732 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001733 }
1734
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001735 if (per_layers) {
1736 /*
1737 * At least one per-layer UCB measure was read. We can now
1738 * calculate the total UCB
1739 *
1740 * Total block Error/Count is calculated as the sum of the
1741 * block errors on all active layers.
1742 */
1743 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1744 c->block_error.stat[0].uvalue = t_block_error;
1745 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1746 c->block_count.stat[0].uvalue = t_block_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001747 } else {
1748 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1749 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001750 }
1751
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001752 return rc;
1753}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001754
1755/*
1756 * The functions below are called via DVB callbacks, so they need to
1757 * properly use the I2C gate control
1758 */
1759
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001760static int mb86a20s_initfe(struct dvb_frontend *fe)
1761{
1762 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001763 u64 pll;
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001764 u32 fclk;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001765 int rc;
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001766 u8 regD5 = 1, reg71, reg09 = 0x3a;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001767
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001768 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001769
1770 if (fe->ops.i2c_gate_ctrl)
1771 fe->ops.i2c_gate_ctrl(fe, 0);
1772
1773 /* Initialize the frontend */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001774 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001775 if (rc < 0)
1776 goto err;
1777
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001778 if (!state->inversion)
1779 reg09 |= 0x04;
1780 rc = mb86a20s_writereg(state, 0x09, reg09);
1781 if (rc < 0)
1782 goto err;
1783 if (!state->bw)
1784 reg71 = 1;
1785 else
1786 reg71 = 0;
1787 rc = mb86a20s_writereg(state, 0x39, reg71);
1788 if (rc < 0)
1789 goto err;
1790 rc = mb86a20s_writereg(state, 0x71, state->bw);
1791 if (rc < 0)
1792 goto err;
1793 if (state->subchannel) {
1794 rc = mb86a20s_writereg(state, 0x44, state->subchannel);
1795 if (rc < 0)
1796 goto err;
1797 }
1798
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001799 fclk = state->config->fclk;
1800 if (!fclk)
1801 fclk = 32571428;
1802
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001803 /* Adjust IF frequency to match tuner */
1804 if (fe->ops.tuner_ops.get_if_frequency)
1805 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1806
1807 if (!state->if_freq)
1808 state->if_freq = 3300000;
1809
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001810 pll = (((u64)1) << 34) * state->if_freq;
1811 do_div(pll, 63 * fclk);
1812 pll = (1 << 25) - pll;
1813 rc = mb86a20s_writereg(state, 0x28, 0x2a);
1814 if (rc < 0)
1815 goto err;
1816 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1817 if (rc < 0)
1818 goto err;
1819 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1820 if (rc < 0)
1821 goto err;
1822 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1823 if (rc < 0)
1824 goto err;
1825 dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
1826 __func__, fclk, state->if_freq, (long long)pll);
1827
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001828 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1829 pll = state->if_freq * 1677721600L;
1830 do_div(pll, 1628571429L);
1831 rc = mb86a20s_writereg(state, 0x28, 0x20);
1832 if (rc < 0)
1833 goto err;
1834 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1835 if (rc < 0)
1836 goto err;
1837 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1838 if (rc < 0)
1839 goto err;
1840 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1841 if (rc < 0)
1842 goto err;
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001843 dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001844 __func__, state->if_freq, (long long)pll);
1845
Mauro Carvalho Chehab9d32069f2013-08-09 08:53:26 -03001846 if (!state->config->is_serial)
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001847 regD5 &= ~1;
1848
Mauro Carvalho Chehab9d32069f2013-08-09 08:53:26 -03001849 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1850 if (rc < 0)
1851 goto err;
1852 rc = mb86a20s_writereg(state, 0x51, regD5);
1853 if (rc < 0)
1854 goto err;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001855
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001856 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1857 if (rc < 0)
1858 goto err;
1859
1860
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001861err:
1862 if (fe->ops.i2c_gate_ctrl)
1863 fe->ops.i2c_gate_ctrl(fe, 1);
1864
1865 if (rc < 0) {
1866 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001867 dev_info(&state->i2c->dev,
1868 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001869 } else {
1870 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001871 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001872 }
1873 return rc;
1874}
1875
1876static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1877{
1878 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001879 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001880 int rc, if_freq;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001881 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001882
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001883 if (!c->isdbt_layer_enabled)
1884 c->isdbt_layer_enabled = 7;
1885
1886 if (c->isdbt_layer_enabled == 1)
1887 state->bw = MB86A20S_1SEG;
1888 else if (c->isdbt_partial_reception)
1889 state->bw = MB86A20S_13SEG_PARTIAL;
1890 else
1891 state->bw = MB86A20S_13SEG;
1892
1893 if (c->inversion == INVERSION_ON)
1894 state->inversion = true;
1895 else
1896 state->inversion = false;
1897
1898 if (!c->isdbt_sb_mode) {
1899 state->subchannel = 0;
1900 } else {
Mauro Carvalho Chehab41c6e9d2013-04-07 20:51:32 -03001901 if (c->isdbt_sb_subchannel >= ARRAY_SIZE(mb86a20s_subchannel))
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001902 c->isdbt_sb_subchannel = 0;
1903
1904 state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
1905 }
1906
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001907 /*
1908 * Gate should already be opened, but it doesn't hurt to
1909 * double-check
1910 */
1911 if (fe->ops.i2c_gate_ctrl)
1912 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001913 fe->ops.tuner_ops.set_params(fe);
1914
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001915 if (fe->ops.tuner_ops.get_if_frequency)
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001916 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1917
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001918 /*
1919 * Make it more reliable: if, for some reason, the initial
1920 * device initialization doesn't happen, initialize it when
1921 * a SBTVD parameters are adjusted.
1922 *
1923 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1924 * the agc callback logic is not called during DVB attach time,
1925 * causing mb86a20s to not be initialized with Kworld SBTVD.
1926 * So, this hack is needed, in order to make Kworld SBTVD to work.
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001927 *
1928 * It is also needed to change the IF after the initial init.
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001929 *
1930 * HACK: Always init the frontend when set_frontend is called:
1931 * it was noticed that, on some devices, it fails to lock on a
1932 * different channel. So, it is better to reset everything, even
1933 * wasting some time, than to loose channel lock.
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001934 */
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001935 mb86a20s_initfe(fe);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001936
1937 if (fe->ops.i2c_gate_ctrl)
1938 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001939
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001940 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001941 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehab3a2e4752013-03-02 15:02:23 -03001942 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001943
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001944 if (fe->ops.i2c_gate_ctrl)
1945 fe->ops.i2c_gate_ctrl(fe, 1);
1946
1947 return rc;
1948}
1949
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001950static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03001951 enum fe_status *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001952{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001953 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001954 int rc, status_nr;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001955
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001956 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001957
1958 if (fe->ops.i2c_gate_ctrl)
1959 fe->ops.i2c_gate_ctrl(fe, 0);
1960
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001961 /* Get lock */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001962 status_nr = mb86a20s_read_status(fe, status);
1963 if (status_nr < 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001964 mb86a20s_stats_not_ready(fe);
1965 mb86a20s_reset_frontend_cache(fe);
1966 }
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001967 if (status_nr < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001968 dev_err(&state->i2c->dev,
1969 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001970 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001971 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001972
1973 /* Get signal strength */
1974 rc = mb86a20s_read_signal_strength(fe);
1975 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001976 dev_err(&state->i2c->dev,
1977 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001978 mb86a20s_stats_not_ready(fe);
1979 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001980
1981 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001982 goto error;
1983 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001984
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001985 if (status_nr >= 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001986 /* Get TMCC info*/
1987 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001988 if (rc < 0) {
1989 dev_err(&state->i2c->dev,
1990 "%s: Can't get FE TMCC data.\n", __func__);
1991 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001992 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001993 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001994
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001995 /* Get statistics */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001996 rc = mb86a20s_get_stats(fe, status_nr);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001997 if (rc < 0 && rc != -EBUSY) {
1998 dev_err(&state->i2c->dev,
1999 "%s: Can't get FE statistics.\n", __func__);
2000 rc = 0;
2001 goto error;
2002 }
2003 rc = 0; /* Don't return EBUSY to userspace */
2004 }
2005 goto ok;
2006
2007error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002008 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02002009
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002010ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02002011 if (fe->ops.i2c_gate_ctrl)
2012 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002013
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002014 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02002015}
2016
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002017static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
2018 u16 *strength)
2019{
2020 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2021
2022
2023 *strength = c->strength.stat[0].uvalue;
2024
2025 return 0;
2026}
2027
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002028static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03002029 bool re_tune,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002030 unsigned int mode_flags,
2031 unsigned int *delay,
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002032 enum fe_status *status)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002033{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002034 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002035 int rc = 0;
2036
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002037 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002038
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03002039 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002040 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002041
2042 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002043 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002044
2045 return rc;
2046}
2047
2048static void mb86a20s_release(struct dvb_frontend *fe)
2049{
2050 struct mb86a20s_state *state = fe->demodulator_priv;
2051
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002052 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002053
2054 kfree(state);
2055}
2056
Mauro Carvalho Chehabdafb65f2016-09-04 10:16:18 -03002057static int mb86a20s_get_frontend_algo(struct dvb_frontend *fe)
2058{
2059 return DVBFE_ALGO_HW;
2060}
2061
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002062static struct dvb_frontend_ops mb86a20s_ops;
2063
2064struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2065 struct i2c_adapter *i2c)
2066{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002067 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002068 u8 rev;
2069
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002070 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2071
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002072 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002073 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002074 if (state == NULL) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002075 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002076 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002077 goto error;
2078 }
2079
2080 /* setup the state */
2081 state->config = config;
2082 state->i2c = i2c;
2083
2084 /* create dvb_frontend */
2085 memcpy(&state->frontend.ops, &mb86a20s_ops,
2086 sizeof(struct dvb_frontend_ops));
2087 state->frontend.demodulator_priv = state;
2088
2089 /* Check if it is a mb86a20s frontend */
2090 rev = mb86a20s_readreg(state, 0);
2091
2092 if (rev == 0x13) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002093 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002094 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002095 } else {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002096 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002097 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002098 rev);
2099 goto error;
2100 }
2101
2102 return &state->frontend;
2103
2104error:
2105 kfree(state);
2106 return NULL;
2107}
2108EXPORT_SYMBOL(mb86a20s_attach);
2109
2110static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002111 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002112 /* Use dib8000 values per default */
2113 .info = {
2114 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03002115 .caps = FE_CAN_RECOVER |
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002116 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2117 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2118 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2119 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2120 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2121 /* Actually, those values depend on the used tuner */
2122 .frequency_min = 45000000,
2123 .frequency_max = 864000000,
2124 .frequency_stepsize = 62500,
2125 },
2126
2127 .release = mb86a20s_release,
2128
2129 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002130 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002131 .read_status = mb86a20s_read_status_and_stats,
2132 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002133 .tune = mb86a20s_tune,
Mauro Carvalho Chehabdafb65f2016-09-04 10:16:18 -03002134 .get_frontend_algo = mb86a20s_get_frontend_algo,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002135};
2136
2137MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
Mauro Carvalho Chehab37e59f82014-02-07 08:03:07 -02002138MODULE_AUTHOR("Mauro Carvalho Chehab");
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002139MODULE_LICENSE("GPL");