blob: d04b52e3f4cc7ee8a2b4a1344c2b313773475d00 [file] [log] [blame]
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001/*
2 * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
3 *
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -03004 * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03005 * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
6 *
Mauro Carvalho Chehabb9ede792010-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
23static int debug = 1;
24module_param(debug, int, 0644);
25MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
26
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -030027enum mb86a20s_bandwidth {
28 MB86A20S_13SEG = 0,
29 MB86A20S_13SEG_PARTIAL = 1,
30 MB86A20S_1SEG = 2,
31 MB86A20S_3SEG = 3,
32};
33
34u8 mb86a20s_subchannel[] = {
35 0xb0, 0xc0, 0xd0, 0xe0,
36 0xf0, 0x00, 0x10, 0x20,
37};
38
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030039struct mb86a20s_state {
40 struct i2c_adapter *i2c;
41 const struct mb86a20s_config *config;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030042 u32 last_frequency;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030043
44 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030045
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030046 u32 if_freq;
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -030047 enum mb86a20s_bandwidth bw;
48 bool inversion;
49 u32 subchannel;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030050
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030051 u32 estimated_rate[3];
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -030052 unsigned long get_strength_time;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030053
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030054 bool need_init;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030055};
56
57struct regdata {
58 u8 reg;
59 u8 data;
60};
61
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030062#define BER_SAMPLING_RATE 1 /* Seconds */
63
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030064/*
65 * Initialization sequence: Use whatevere default values that PV SBTVD
66 * does on its initialisation, obtained via USB snoop
67 */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030068static struct regdata mb86a20s_init1[] = {
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030069 { 0x70, 0x0f },
70 { 0x70, 0xff },
71 { 0x08, 0x01 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030072 { 0x50, 0xd1 }, { 0x51, 0x20 },
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030073};
74
75static struct regdata mb86a20s_init2[] = {
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020076 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030077 { 0x3b, 0x21 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030078 { 0x3c, 0x38 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030079 { 0x01, 0x0d },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030080 { 0x04, 0x08 }, { 0x05, 0x03 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020081 { 0x04, 0x0e }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030082 { 0x04, 0x0f }, { 0x05, 0x37 },
83 { 0x04, 0x0b }, { 0x05, 0x78 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020084 { 0x04, 0x00 }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030085 { 0x04, 0x01 }, { 0x05, 0x1e },
86 { 0x04, 0x02 }, { 0x05, 0x07 },
87 { 0x04, 0x03 }, { 0x05, 0xd0 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020088 { 0x04, 0x09 }, { 0x05, 0x00 },
89 { 0x04, 0x0a }, { 0x05, 0xff },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030090 { 0x04, 0x27 }, { 0x05, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020091 { 0x04, 0x28 }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030092 { 0x04, 0x1e }, { 0x05, 0x00 },
93 { 0x04, 0x29 }, { 0x05, 0x64 },
94 { 0x04, 0x32 }, { 0x05, 0x02 },
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 Chehabebe96742012-01-11 11:00:28 -0200102 { 0x04, 0x15 }, { 0x05, 0x4e },
103 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300104
105 /*
106 * On this demod, when the bit count reaches the count below,
107 * it collects the bit error count. The bit counters are initialized
108 * to 65535 here. This warrants that all of them will be quickly
109 * calculated when device gets locked. As TMCC is parsed, the values
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300110 * will be adjusted later in the driver's code.
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300111 */
112 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
113 { 0x50, 0xa7 }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200114 { 0x50, 0xa8 }, { 0x51, 0xff },
115 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300116 { 0x50, 0xaa }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200117 { 0x50, 0xab }, { 0x51, 0xff },
118 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300119 { 0x50, 0xad }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200120 { 0x50, 0xae }, { 0x51, 0xff },
121 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300122
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300123 /*
124 * On this demod, post BER counts blocks. When the count reaches the
125 * value below, it collects the block error count. The block counters
126 * are initialized to 127 here. This warrants that all of them will be
127 * quickly calculated when device gets locked. As TMCC is parsed, the
128 * values will be adjusted later in the driver's code.
129 */
130 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
131 { 0x50, 0xdc }, { 0x51, 0x00 },
132 { 0x50, 0xdd }, { 0x51, 0x7f },
133 { 0x50, 0xde }, { 0x51, 0x00 },
134 { 0x50, 0xdf }, { 0x51, 0x7f },
135 { 0x50, 0xe0 }, { 0x51, 0x00 },
136 { 0x50, 0xe1 }, { 0x51, 0x7f },
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300137
138 /*
139 * On this demod, when the block count reaches the count below,
140 * it collects the block error count. The block counters are initialized
141 * to 127 here. This warrants that all of them will be quickly
142 * calculated when device gets locked. As TMCC is parsed, the values
143 * will be adjusted later in the driver's code.
144 */
145 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
146 { 0x50, 0xb2 }, { 0x51, 0x00 },
147 { 0x50, 0xb3 }, { 0x51, 0x7f },
148 { 0x50, 0xb4 }, { 0x51, 0x00 },
149 { 0x50, 0xb5 }, { 0x51, 0x7f },
150 { 0x50, 0xb6 }, { 0x51, 0x00 },
151 { 0x50, 0xb7 }, { 0x51, 0x7f },
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300152
153 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300154 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
155 { 0x45, 0x04 }, /* CN symbol 4 */
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300156 { 0x48, 0x04 }, /* CN manual mode */
157
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200158 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
159 { 0x50, 0xd6 }, { 0x51, 0x1f },
160 { 0x50, 0xd2 }, { 0x51, 0x03 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300161 { 0x50, 0xd7 }, { 0x51, 0xbf },
162 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xff },
163 { 0x28, 0x46 }, { 0x29, 0x00 }, { 0x2a, 0x1a }, { 0x2b, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300164
165 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300166 { 0x28, 0x00 }, { 0x2b, 0x08 },
167 { 0x28, 0x05 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300168 { 0x1c, 0x01 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300169 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x1f },
170 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x18 },
171 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x12 },
172 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x30 },
173 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x37 },
174 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
175 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x09 },
176 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x06 },
177 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7b },
178 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x76 },
179 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7d },
180 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x08 },
181 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0b },
182 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
183 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf2 },
184 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf3 },
185 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x05 },
186 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
187 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
188 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xef },
189 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xd8 },
190 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xf1 },
191 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x3d },
192 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x94 },
193 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xba },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200194 { 0x50, 0x1e }, { 0x51, 0x5d },
195 { 0x50, 0x22 }, { 0x51, 0x00 },
196 { 0x50, 0x23 }, { 0x51, 0xc8 },
197 { 0x50, 0x24 }, { 0x51, 0x00 },
198 { 0x50, 0x25 }, { 0x51, 0xf0 },
199 { 0x50, 0x26 }, { 0x51, 0x00 },
200 { 0x50, 0x27 }, { 0x51, 0xc3 },
201 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300202 { 0xec, 0x0f },
203 { 0xeb, 0x1f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200204 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300205 { 0xd0, 0x00 },
206};
207
208static struct regdata mb86a20s_reset_reception[] = {
209 { 0x70, 0xf0 },
210 { 0x70, 0xff },
211 { 0x08, 0x01 },
212 { 0x08, 0x00 },
213};
214
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300215static struct regdata mb86a20s_per_ber_reset[] = {
216 { 0x53, 0x00 }, /* pre BER Counter reset */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300217 { 0x53, 0x07 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300218
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300219 { 0x5f, 0x00 }, /* post BER Counter reset */
220 { 0x5f, 0x07 },
221
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300222 { 0x50, 0xb1 }, /* PER Counter reset */
223 { 0x51, 0x07 },
224 { 0x51, 0x00 },
225};
226
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200227/*
228 * I2C read/write functions and macros
229 */
230
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300231static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300232 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300233{
234 u8 buf[] = { reg, data };
235 struct i2c_msg msg = {
236 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
237 };
238 int rc;
239
240 rc = i2c_transfer(state->i2c, &msg, 1);
241 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200242 dev_err(&state->i2c->dev,
243 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
244 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300245 return rc;
246 }
247
248 return 0;
249}
250
251static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
252 u8 i2c_addr, struct regdata *rd, int size)
253{
254 int i, rc;
255
256 for (i = 0; i < size; i++) {
257 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
258 rd[i].data);
259 if (rc < 0)
260 return rc;
261 }
262 return 0;
263}
264
265static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
266 u8 i2c_addr, u8 reg)
267{
268 u8 val;
269 int rc;
270 struct i2c_msg msg[] = {
271 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
272 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
273 };
274
275 rc = i2c_transfer(state->i2c, msg, 2);
276
277 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200278 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
279 __func__, reg, rc);
280 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300281 }
282
283 return val;
284}
285
286#define mb86a20s_readreg(state, reg) \
287 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
288#define mb86a20s_writereg(state, reg, val) \
289 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
290#define mb86a20s_writeregdata(state, regdata) \
291 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
292 regdata, ARRAY_SIZE(regdata))
293
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300294/*
295 * Ancillary internal routines (likely compiled inlined)
296 *
297 * The functions below assume that gateway lock has already obtained
298 */
299
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200300static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300301{
302 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200303 int val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300304
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200305 *status = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300306
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200307 val = mb86a20s_readreg(state, 0x0a) & 0xf;
308 if (val < 0)
309 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300310
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200311 if (val >= 2)
312 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300313
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200314 if (val >= 4)
315 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300316
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200317 if (val >= 5)
318 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300319
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200320 if (val >= 7)
321 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300322
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200323 if (val >= 8) /* Maybe 9? */
324 *status |= FE_HAS_LOCK;
325
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200326 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
327 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200328
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -0300329 return val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300330}
331
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300332static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300333{
334 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300335 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300336 int rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300337 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300338
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300339 if (state->get_strength_time &&
340 (!time_after(jiffies, state->get_strength_time)))
341 return c->strength.stat[0].uvalue;
342
343 /* Reset its value if an error happen */
344 c->strength.stat[0].uvalue = 0;
345
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300346 /* Does a binary search to get RF strength */
347 rf_max = 0xfff;
348 rf_min = 0;
349 do {
350 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300351 rc = mb86a20s_writereg(state, 0x04, 0x1f);
352 if (rc < 0)
353 return rc;
354 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
355 if (rc < 0)
356 return rc;
357 rc = mb86a20s_writereg(state, 0x04, 0x20);
358 if (rc < 0)
359 return rc;
Mauro Carvalho Chehabdad78c52013-03-01 16:15:16 -0300360 rc = mb86a20s_writereg(state, 0x05, rf);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300361 if (rc < 0)
362 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300363
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300364 rc = mb86a20s_readreg(state, 0x02);
365 if (rc < 0)
366 return rc;
367 if (rc & 0x08)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300368 rf_min = (rf_max + rf_min) / 2;
369 else
370 rf_max = (rf_max + rf_min) / 2;
371 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300372 rf = (rf_max + rf_min) / 2;
373
374 /* Rescale it from 2^12 (4096) to 2^16 */
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300375 rf = rf << (16 - 12);
376 if (rf)
377 rf |= (1 << 12) - 1;
378
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200379 dev_dbg(&state->i2c->dev,
380 "%s: signal strength = %d (%d < RF=%d < %d)\n",
381 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300382 c->strength.stat[0].uvalue = rf;
383 state->get_strength_time = jiffies +
384 msecs_to_jiffies(1000);
385 return 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300386 }
387 } while (1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300388}
389
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200390static int mb86a20s_get_modulation(struct mb86a20s_state *state,
391 unsigned layer)
392{
393 int rc;
394 static unsigned char reg[] = {
395 [0] = 0x86, /* Layer A */
396 [1] = 0x8a, /* Layer B */
397 [2] = 0x8e, /* Layer C */
398 };
399
Dan Carpenter82033bc2012-01-13 02:28:34 -0300400 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200401 return -EINVAL;
402 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
403 if (rc < 0)
404 return rc;
405 rc = mb86a20s_readreg(state, 0x6e);
406 if (rc < 0)
407 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300408 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200409 case 0:
410 return DQPSK;
411 case 1:
412 return QPSK;
413 case 2:
414 return QAM_16;
415 case 3:
416 return QAM_64;
417 default:
418 return QAM_AUTO;
419 }
420}
421
422static int mb86a20s_get_fec(struct mb86a20s_state *state,
423 unsigned layer)
424{
425 int rc;
426
427 static unsigned char reg[] = {
428 [0] = 0x87, /* Layer A */
429 [1] = 0x8b, /* Layer B */
430 [2] = 0x8f, /* Layer C */
431 };
432
Dan Carpenter82033bc2012-01-13 02:28:34 -0300433 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200434 return -EINVAL;
435 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
436 if (rc < 0)
437 return rc;
438 rc = mb86a20s_readreg(state, 0x6e);
439 if (rc < 0)
440 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300441 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200442 case 0:
443 return FEC_1_2;
444 case 1:
445 return FEC_2_3;
446 case 2:
447 return FEC_3_4;
448 case 3:
449 return FEC_5_6;
450 case 4:
451 return FEC_7_8;
452 default:
453 return FEC_AUTO;
454 }
455}
456
457static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
458 unsigned layer)
459{
460 int rc;
461
462 static unsigned char reg[] = {
463 [0] = 0x88, /* Layer A */
464 [1] = 0x8c, /* Layer B */
465 [2] = 0x90, /* Layer C */
466 };
467
Dan Carpenter82033bc2012-01-13 02:28:34 -0300468 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200469 return -EINVAL;
470 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
471 if (rc < 0)
472 return rc;
473 rc = mb86a20s_readreg(state, 0x6e);
474 if (rc < 0)
475 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300476
477 switch ((rc >> 4) & 0x07) {
478 case 1:
479 return GUARD_INTERVAL_1_4;
480 case 2:
481 return GUARD_INTERVAL_1_8;
482 case 3:
483 return GUARD_INTERVAL_1_16;
484 case 4:
485 return GUARD_INTERVAL_1_32;
486
487 default:
488 case 0:
489 return GUARD_INTERVAL_AUTO;
490 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200491}
492
493static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
494 unsigned layer)
495{
496 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200497 static unsigned char reg[] = {
498 [0] = 0x89, /* Layer A */
499 [1] = 0x8d, /* Layer B */
500 [2] = 0x91, /* Layer C */
501 };
502
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200503 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
504
Dan Carpenter82033bc2012-01-13 02:28:34 -0300505 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200506 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200507
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200508 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
509 if (rc < 0)
510 return rc;
511 rc = mb86a20s_readreg(state, 0x6e);
512 if (rc < 0)
513 return rc;
514 count = (rc >> 4) & 0x0f;
515
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200516 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
517
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200518 return count;
519}
520
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300521static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
522{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200523 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300524 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
525
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200526 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
527
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300528 /* Fixed parameters */
529 c->delivery_system = SYS_ISDBT;
530 c->bandwidth_hz = 6000000;
531
532 /* Initialize values that will be later autodetected */
533 c->isdbt_layer_enabled = 0;
534 c->transmission_mode = TRANSMISSION_MODE_AUTO;
535 c->guard_interval = GUARD_INTERVAL_AUTO;
536 c->isdbt_sb_mode = 0;
537 c->isdbt_sb_segment_count = 0;
538}
539
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300540/*
541 * Estimates the bit rate using the per-segment bit rate given by
542 * ABNT/NBR 15601 spec (table 4).
543 */
544static u32 isdbt_rate[3][5][4] = {
545 { /* DQPSK/QPSK */
546 { 280850, 312060, 330420, 340430 }, /* 1/2 */
547 { 374470, 416080, 440560, 453910 }, /* 2/3 */
548 { 421280, 468090, 495630, 510650 }, /* 3/4 */
549 { 468090, 520100, 550700, 567390 }, /* 5/6 */
550 { 491500, 546110, 578230, 595760 }, /* 7/8 */
551 }, { /* QAM16 */
552 { 561710, 624130, 660840, 680870 }, /* 1/2 */
553 { 748950, 832170, 881120, 907820 }, /* 2/3 */
554 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
555 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
556 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
557 }, { /* QAM64 */
558 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
559 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
560 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
561 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
562 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
563 }
564};
565
566static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
567 u32 modulation, u32 fec, u32 interleaving,
568 u32 segment)
569{
570 struct mb86a20s_state *state = fe->demodulator_priv;
571 u32 rate;
572 int m, f, i;
573
574 /*
575 * If modulation/fec/interleaving is not detected, the default is
576 * to consider the lowest bit rate, to avoid taking too long time
577 * to get BER.
578 */
579 switch (modulation) {
580 case DQPSK:
581 case QPSK:
582 default:
583 m = 0;
584 break;
585 case QAM_16:
586 m = 1;
587 break;
588 case QAM_64:
589 m = 2;
590 break;
591 }
592
593 switch (fec) {
594 default:
595 case FEC_1_2:
596 case FEC_AUTO:
597 f = 0;
598 break;
599 case FEC_2_3:
600 f = 1;
601 break;
602 case FEC_3_4:
603 f = 2;
604 break;
605 case FEC_5_6:
606 f = 3;
607 break;
608 case FEC_7_8:
609 f = 4;
610 break;
611 }
612
613 switch (interleaving) {
614 default:
615 case GUARD_INTERVAL_1_4:
616 i = 0;
617 break;
618 case GUARD_INTERVAL_1_8:
619 i = 1;
620 break;
621 case GUARD_INTERVAL_1_16:
622 i = 2;
623 break;
624 case GUARD_INTERVAL_1_32:
625 i = 3;
626 break;
627 }
628
629 /* Samples BER at BER_SAMPLING_RATE seconds */
630 rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
631
632 /* Avoids sampling too quickly or to overflow the register */
633 if (rate < 256)
634 rate = 256;
635 else if (rate > (1 << 24) - 1)
636 rate = (1 << 24) - 1;
637
638 dev_dbg(&state->i2c->dev,
639 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
640 __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
641 rate, rate);
642
643 state->estimated_rate[i] = rate;
644}
645
646
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300647static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300648{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200649 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300650 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200651 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300652
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200653 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
654
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300655 /* Reset frontend cache to default values */
656 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200657
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200658 /* Check for partial reception */
659 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300660 if (rc < 0)
661 return rc;
662 rc = mb86a20s_readreg(state, 0x6e);
663 if (rc < 0)
664 return rc;
665 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200666
667 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300668
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200669 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200670 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
671 __func__, 'A' + i);
672
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200673 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300674 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200675 goto noperlayer_error;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300676 if (rc >= 0 && rc < 14) {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300677 c->layer[i].segment_count = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300678 } else {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300679 c->layer[i].segment_count = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300680 state->estimated_rate[i] = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200681 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300682 }
683 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200684 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300685 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200686 goto noperlayer_error;
687 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
688 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300689 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200690 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300691 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200692 goto noperlayer_error;
693 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
694 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300695 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200696 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300697 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200698 goto noperlayer_error;
699 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
700 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300701 c->layer[i].interleaving = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300702 mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
703 c->layer[i].fec,
704 c->layer[i].interleaving,
705 c->layer[i].segment_count);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200706 }
707
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200708 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300709 if (rc < 0)
710 return rc;
711 if ((rc & 0x60) == 0x20) {
712 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200713 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300714 if (!c->isdbt_sb_segment_count)
715 c->isdbt_sb_segment_count = 1;
716 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200717
718 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200719 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300720 if (rc < 0)
721 return rc;
722 if ((rc & 0x60) == 0x20) {
723 switch (rc & 0x0c >> 2) {
724 case 0:
725 c->transmission_mode = TRANSMISSION_MODE_2K;
726 break;
727 case 1:
728 c->transmission_mode = TRANSMISSION_MODE_4K;
729 break;
730 case 2:
731 c->transmission_mode = TRANSMISSION_MODE_8K;
732 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200733 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300734 }
735 if (!(rc & 0x10)) {
736 switch (rc & 0x3) {
737 case 0:
738 c->guard_interval = GUARD_INTERVAL_1_4;
739 break;
740 case 1:
741 c->guard_interval = GUARD_INTERVAL_1_8;
742 break;
743 case 2:
744 c->guard_interval = GUARD_INTERVAL_1_16;
745 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200746 }
747 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300748 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200749
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200750noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300751
752 /* per-layer info is incomplete; discard all per-layer */
753 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300754
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300755 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300756}
757
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300758static int mb86a20s_reset_counters(struct dvb_frontend *fe)
759{
760 struct mb86a20s_state *state = fe->demodulator_priv;
761 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
762 int rc, val;
763
764 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
765
766 /* Reset the counters, if the channel changed */
767 if (state->last_frequency != c->frequency) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300768 memset(&c->cnr, 0, sizeof(c->cnr));
769 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
770 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300771 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
772 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300773 memset(&c->block_error, 0, sizeof(c->block_error));
774 memset(&c->block_count, 0, sizeof(c->block_count));
775
776 state->last_frequency = c->frequency;
777 }
778
779 /* Clear status for most stats */
780
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300781 /* BER/PER counter reset */
782 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300783 if (rc < 0)
784 goto err;
785
786 /* CNR counter reset */
787 rc = mb86a20s_readreg(state, 0x45);
788 if (rc < 0)
789 goto err;
790 val = rc;
791 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
792 if (rc < 0)
793 goto err;
794 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
795 if (rc < 0)
796 goto err;
797
798 /* MER counter reset */
799 rc = mb86a20s_writereg(state, 0x50, 0x50);
800 if (rc < 0)
801 goto err;
802 rc = mb86a20s_readreg(state, 0x51);
803 if (rc < 0)
804 goto err;
805 val = rc;
806 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
807 if (rc < 0)
808 goto err;
809 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
810 if (rc < 0)
811 goto err;
812
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300813 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300814err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300815 dev_err(&state->i2c->dev,
816 "%s: Can't reset FE statistics (error %d).\n",
817 __func__, rc);
818ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300819 return rc;
820}
821
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300822static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
823 unsigned layer,
824 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300825{
826 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300827 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300828
829 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
830
831 if (layer >= 3)
832 return -EINVAL;
833
834 /* Check if the BER measures are already available */
835 rc = mb86a20s_readreg(state, 0x54);
836 if (rc < 0)
837 return rc;
838
839 /* Check if data is available for that layer */
840 if (!(rc & (1 << layer))) {
841 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300842 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300843 __func__, 'A' + layer);
844 return -EBUSY;
845 }
846
847 /* Read Bit Error Count */
848 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
849 if (rc < 0)
850 return rc;
851 *error = rc << 16;
852 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
853 if (rc < 0)
854 return rc;
855 *error |= rc << 8;
856 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
857 if (rc < 0)
858 return rc;
859 *error |= rc;
860
861 dev_dbg(&state->i2c->dev,
862 "%s: bit error before Viterbi for layer %c: %d.\n",
863 __func__, 'A' + layer, *error);
864
865 /* Read Bit Count */
866 rc = mb86a20s_writereg(state, 0x50, 0xa7 + 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 << 16;
873 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
874 if (rc < 0)
875 return rc;
876 rc = mb86a20s_readreg(state, 0x51);
877 if (rc < 0)
878 return rc;
879 *count |= rc << 8;
880 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
881 if (rc < 0)
882 return rc;
883 rc = mb86a20s_readreg(state, 0x51);
884 if (rc < 0)
885 return rc;
886 *count |= rc;
887
888 dev_dbg(&state->i2c->dev,
889 "%s: bit count before Viterbi for layer %c: %d.\n",
890 __func__, 'A' + layer, *count);
891
892
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300893 /*
894 * As we get TMCC data from the frontend, we can better estimate the
895 * BER bit counters, in order to do the BER measure during a longer
896 * time. Use those data, if available, to update the bit count
897 * measure.
898 */
899
900 if (state->estimated_rate[layer]
901 && state->estimated_rate[layer] != *count) {
902 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300903 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300904 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300905
906 /* Turn off BER before Viterbi */
907 rc = mb86a20s_writereg(state, 0x52, 0x00);
908
909 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300910 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
911 if (rc < 0)
912 return rc;
913 rc = mb86a20s_writereg(state, 0x51,
914 state->estimated_rate[layer] >> 16);
915 if (rc < 0)
916 return rc;
917 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
918 if (rc < 0)
919 return rc;
920 rc = mb86a20s_writereg(state, 0x51,
921 state->estimated_rate[layer] >> 8);
922 if (rc < 0)
923 return rc;
924 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
925 if (rc < 0)
926 return rc;
927 rc = mb86a20s_writereg(state, 0x51,
928 state->estimated_rate[layer]);
929 if (rc < 0)
930 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300931
932 /* Turn on BER before Viterbi */
933 rc = mb86a20s_writereg(state, 0x52, 0x01);
934
935 /* Reset all preBER counters */
936 rc = mb86a20s_writereg(state, 0x53, 0x00);
937 if (rc < 0)
938 return rc;
939 rc = mb86a20s_writereg(state, 0x53, 0x07);
940 } else {
941 /* Reset counter to collect new data */
942 rc = mb86a20s_readreg(state, 0x53);
943 if (rc < 0)
944 return rc;
945 val = rc;
946 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
947 if (rc < 0)
948 return rc;
949 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300950 }
951
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300952 return rc;
953}
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300954
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300955static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
956 unsigned layer,
957 u32 *error, u32 *count)
958{
959 struct mb86a20s_state *state = fe->demodulator_priv;
960 u32 counter, collect_rate;
961 int rc, val;
962
963 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
964
965 if (layer >= 3)
966 return -EINVAL;
967
968 /* Check if the BER measures are already available */
969 rc = mb86a20s_readreg(state, 0x60);
970 if (rc < 0)
971 return rc;
972
973 /* Check if data is available for that layer */
974 if (!(rc & (1 << layer))) {
975 dev_dbg(&state->i2c->dev,
976 "%s: post BER for layer %c is not available yet.\n",
977 __func__, 'A' + layer);
978 return -EBUSY;
979 }
980
981 /* Read Bit Error Count */
982 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
983 if (rc < 0)
984 return rc;
985 *error = rc << 16;
986 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
987 if (rc < 0)
988 return rc;
989 *error |= rc << 8;
990 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
991 if (rc < 0)
992 return rc;
993 *error |= rc;
994
995 dev_dbg(&state->i2c->dev,
996 "%s: post bit error for layer %c: %d.\n",
997 __func__, 'A' + layer, *error);
998
999 /* Read Bit Count */
1000 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1001 if (rc < 0)
1002 return rc;
1003 rc = mb86a20s_readreg(state, 0x51);
1004 if (rc < 0)
1005 return rc;
1006 counter = rc << 8;
1007 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1008 if (rc < 0)
1009 return rc;
1010 rc = mb86a20s_readreg(state, 0x51);
1011 if (rc < 0)
1012 return rc;
1013 counter |= rc;
1014 *count = counter * 204 * 8;
1015
1016 dev_dbg(&state->i2c->dev,
1017 "%s: post bit count for layer %c: %d.\n",
1018 __func__, 'A' + layer, *count);
1019
1020 /*
1021 * As we get TMCC data from the frontend, we can better estimate the
1022 * BER bit counters, in order to do the BER measure during a longer
1023 * time. Use those data, if available, to update the bit count
1024 * measure.
1025 */
1026
1027 if (!state->estimated_rate[layer])
1028 goto reset_measurement;
1029
1030 collect_rate = state->estimated_rate[layer] / 204 / 8;
1031 if (collect_rate < 32)
1032 collect_rate = 32;
1033 if (collect_rate > 65535)
1034 collect_rate = 65535;
1035 if (collect_rate != counter) {
1036 dev_dbg(&state->i2c->dev,
1037 "%s: updating postBER counter on layer %c to %d.\n",
1038 __func__, 'A' + layer, collect_rate);
1039
1040 /* Turn off BER after Viterbi */
1041 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1042
1043 /* Update counter for this layer */
1044 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1045 if (rc < 0)
1046 return rc;
1047 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1048 if (rc < 0)
1049 return rc;
1050 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1051 if (rc < 0)
1052 return rc;
1053 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1054 if (rc < 0)
1055 return rc;
1056
1057 /* Turn on BER after Viterbi */
1058 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1059
1060 /* Reset all preBER counters */
1061 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1062 if (rc < 0)
1063 return rc;
1064 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1065
1066 return rc;
1067 }
1068
1069reset_measurement:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001070 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001071 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001072 if (rc < 0)
1073 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001074 val = rc;
1075 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1076 if (rc < 0)
1077 return rc;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001078 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001079
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001080 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001081}
1082
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001083static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1084 unsigned layer,
1085 u32 *error, u32 *count)
1086{
1087 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001088 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001089 u32 collect_rate;
1090 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1091
1092 if (layer >= 3)
1093 return -EINVAL;
1094
1095 /* Check if the PER measures are already available */
1096 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1097 if (rc < 0)
1098 return rc;
1099 rc = mb86a20s_readreg(state, 0x51);
1100 if (rc < 0)
1101 return rc;
1102
1103 /* Check if data is available for that layer */
1104
1105 if (!(rc & (1 << layer))) {
1106 dev_dbg(&state->i2c->dev,
1107 "%s: block counts for layer %c aren't available yet.\n",
1108 __func__, 'A' + layer);
1109 return -EBUSY;
1110 }
1111
1112 /* Read Packet error Count */
1113 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1114 if (rc < 0)
1115 return rc;
1116 rc = mb86a20s_readreg(state, 0x51);
1117 if (rc < 0)
1118 return rc;
1119 *error = rc << 8;
1120 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1121 if (rc < 0)
1122 return rc;
1123 rc = mb86a20s_readreg(state, 0x51);
1124 if (rc < 0)
1125 return rc;
1126 *error |= rc;
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001127 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001128 __func__, 'A' + layer, *error);
1129
1130 /* Read Bit Count */
1131 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1132 if (rc < 0)
1133 return rc;
1134 rc = mb86a20s_readreg(state, 0x51);
1135 if (rc < 0)
1136 return rc;
1137 *count = rc << 8;
1138 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1139 if (rc < 0)
1140 return rc;
1141 rc = mb86a20s_readreg(state, 0x51);
1142 if (rc < 0)
1143 return rc;
1144 *count |= rc;
1145
1146 dev_dbg(&state->i2c->dev,
1147 "%s: block count for layer %c: %d.\n",
1148 __func__, 'A' + layer, *count);
1149
1150 /*
1151 * As we get TMCC data from the frontend, we can better estimate the
1152 * BER bit counters, in order to do the BER measure during a longer
1153 * time. Use those data, if available, to update the bit count
1154 * measure.
1155 */
1156
1157 if (!state->estimated_rate[layer])
1158 goto reset_measurement;
1159
1160 collect_rate = state->estimated_rate[layer] / 204 / 8;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001161 if (collect_rate < 32)
1162 collect_rate = 32;
1163 if (collect_rate > 65535)
1164 collect_rate = 65535;
1165
1166 if (collect_rate != *count) {
1167 dev_dbg(&state->i2c->dev,
1168 "%s: updating PER counter on layer %c to %d.\n",
1169 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001170
1171 /* Stop PER measurement */
1172 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1173 if (rc < 0)
1174 return rc;
1175 rc = mb86a20s_writereg(state, 0x51, 0x00);
1176 if (rc < 0)
1177 return rc;
1178
1179 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001180 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1181 if (rc < 0)
1182 return rc;
1183 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1184 if (rc < 0)
1185 return rc;
1186 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1187 if (rc < 0)
1188 return rc;
1189 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1190 if (rc < 0)
1191 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001192
1193 /* start PER measurement */
1194 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1195 if (rc < 0)
1196 return rc;
1197 rc = mb86a20s_writereg(state, 0x51, 0x07);
1198 if (rc < 0)
1199 return rc;
1200
1201 /* Reset all counters to collect new data */
1202 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1203 if (rc < 0)
1204 return rc;
1205 rc = mb86a20s_writereg(state, 0x51, 0x07);
1206 if (rc < 0)
1207 return rc;
1208 rc = mb86a20s_writereg(state, 0x51, 0x00);
1209
1210 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001211 }
1212
1213reset_measurement:
1214 /* Reset counter to collect new data */
1215 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1216 if (rc < 0)
1217 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001218 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001219 if (rc < 0)
1220 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001221 val = rc;
1222 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001223 if (rc < 0)
1224 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001225 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001226
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001227 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001228}
1229
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001230struct linear_segments {
1231 unsigned x, y;
1232};
1233
1234/*
1235 * All tables below return a dB/1000 measurement
1236 */
1237
1238static struct linear_segments cnr_to_db_table[] = {
1239 { 19648, 0},
1240 { 18187, 1000},
1241 { 16534, 2000},
1242 { 14823, 3000},
1243 { 13161, 4000},
1244 { 11622, 5000},
1245 { 10279, 6000},
1246 { 9089, 7000},
1247 { 8042, 8000},
1248 { 7137, 9000},
1249 { 6342, 10000},
1250 { 5641, 11000},
1251 { 5030, 12000},
1252 { 4474, 13000},
1253 { 3988, 14000},
1254 { 3556, 15000},
1255 { 3180, 16000},
1256 { 2841, 17000},
1257 { 2541, 18000},
1258 { 2276, 19000},
1259 { 2038, 20000},
1260 { 1800, 21000},
1261 { 1625, 22000},
1262 { 1462, 23000},
1263 { 1324, 24000},
1264 { 1175, 25000},
1265 { 1063, 26000},
1266 { 980, 27000},
1267 { 907, 28000},
1268 { 840, 29000},
1269 { 788, 30000},
1270};
1271
1272static struct linear_segments cnr_64qam_table[] = {
1273 { 3922688, 0},
1274 { 3920384, 1000},
1275 { 3902720, 2000},
1276 { 3894784, 3000},
1277 { 3882496, 4000},
1278 { 3872768, 5000},
1279 { 3858944, 6000},
1280 { 3851520, 7000},
1281 { 3838976, 8000},
1282 { 3829248, 9000},
1283 { 3818240, 10000},
1284 { 3806976, 11000},
1285 { 3791872, 12000},
1286 { 3767040, 13000},
1287 { 3720960, 14000},
1288 { 3637504, 15000},
1289 { 3498496, 16000},
1290 { 3296000, 17000},
1291 { 3031040, 18000},
1292 { 2715392, 19000},
1293 { 2362624, 20000},
1294 { 1963264, 21000},
1295 { 1649664, 22000},
1296 { 1366784, 23000},
1297 { 1120768, 24000},
1298 { 890880, 25000},
1299 { 723456, 26000},
1300 { 612096, 27000},
1301 { 518912, 28000},
1302 { 448256, 29000},
1303 { 388864, 30000},
1304};
1305
1306static struct linear_segments cnr_16qam_table[] = {
1307 { 5314816, 0},
1308 { 5219072, 1000},
1309 { 5118720, 2000},
1310 { 4998912, 3000},
1311 { 4875520, 4000},
1312 { 4736000, 5000},
1313 { 4604160, 6000},
1314 { 4458752, 7000},
1315 { 4300288, 8000},
1316 { 4092928, 9000},
1317 { 3836160, 10000},
1318 { 3521024, 11000},
1319 { 3155968, 12000},
1320 { 2756864, 13000},
1321 { 2347008, 14000},
1322 { 1955072, 15000},
1323 { 1593600, 16000},
1324 { 1297920, 17000},
1325 { 1043968, 18000},
1326 { 839680, 19000},
1327 { 672256, 20000},
1328 { 523008, 21000},
1329 { 424704, 22000},
1330 { 345088, 23000},
1331 { 280064, 24000},
1332 { 221440, 25000},
1333 { 179712, 26000},
1334 { 151040, 27000},
1335 { 128512, 28000},
1336 { 110080, 29000},
1337 { 95744, 30000},
1338};
1339
1340struct linear_segments cnr_qpsk_table[] = {
1341 { 2834176, 0},
1342 { 2683648, 1000},
1343 { 2536960, 2000},
1344 { 2391808, 3000},
1345 { 2133248, 4000},
1346 { 1906176, 5000},
1347 { 1666560, 6000},
1348 { 1422080, 7000},
1349 { 1189632, 8000},
1350 { 976384, 9000},
1351 { 790272, 10000},
1352 { 633344, 11000},
1353 { 505600, 12000},
1354 { 402944, 13000},
1355 { 320768, 14000},
1356 { 255488, 15000},
1357 { 204032, 16000},
1358 { 163072, 17000},
1359 { 130304, 18000},
1360 { 105216, 19000},
1361 { 83456, 20000},
1362 { 65024, 21000},
1363 { 52480, 22000},
1364 { 42752, 23000},
1365 { 34560, 24000},
1366 { 27136, 25000},
1367 { 22016, 26000},
1368 { 18432, 27000},
1369 { 15616, 28000},
1370 { 13312, 29000},
1371 { 11520, 30000},
1372};
1373
1374static u32 interpolate_value(u32 value, struct linear_segments *segments,
1375 unsigned len)
1376{
1377 u64 tmp64;
1378 u32 dx, dy;
1379 int i, ret;
1380
1381 if (value >= segments[0].x)
1382 return segments[0].y;
1383 if (value < segments[len-1].x)
1384 return segments[len-1].y;
1385
1386 for (i = 1; i < len - 1; i++) {
1387 /* If value is identical, no need to interpolate */
1388 if (value == segments[i].x)
1389 return segments[i].y;
1390 if (value > segments[i].x)
1391 break;
1392 }
1393
1394 /* Linear interpolation between the two (x,y) points */
1395 dy = segments[i].y - segments[i - 1].y;
1396 dx = segments[i - 1].x - segments[i].x;
1397 tmp64 = value - segments[i].x;
1398 tmp64 *= dy;
1399 do_div(tmp64, dx);
1400 ret = segments[i].y - tmp64;
1401
1402 return ret;
1403}
1404
1405static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1406{
1407 struct mb86a20s_state *state = fe->demodulator_priv;
1408 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1409 u32 cnr_linear, cnr;
1410 int rc, val;
1411
1412 /* Check if CNR is available */
1413 rc = mb86a20s_readreg(state, 0x45);
1414 if (rc < 0)
1415 return rc;
1416
1417 if (!(rc & 0x40)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001418 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001419 __func__);
1420 return -EBUSY;
1421 }
1422 val = rc;
1423
1424 rc = mb86a20s_readreg(state, 0x46);
1425 if (rc < 0)
1426 return rc;
1427 cnr_linear = rc << 8;
1428
1429 rc = mb86a20s_readreg(state, 0x46);
1430 if (rc < 0)
1431 return rc;
1432 cnr_linear |= rc;
1433
1434 cnr = interpolate_value(cnr_linear,
1435 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1436
1437 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1438 c->cnr.stat[0].svalue = cnr;
1439
1440 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1441 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1442
1443 /* CNR counter reset */
1444 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1445 if (rc < 0)
1446 return rc;
1447 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1448
1449 return rc;
1450}
1451
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001452static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001453{
1454 struct mb86a20s_state *state = fe->demodulator_priv;
1455 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1456 u32 mer, cnr;
1457 int rc, val, i;
1458 struct linear_segments *segs;
1459 unsigned segs_len;
1460
1461 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1462
1463 /* Check if the measures are already available */
1464 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1465 if (rc < 0)
1466 return rc;
1467 rc = mb86a20s_readreg(state, 0x51);
1468 if (rc < 0)
1469 return rc;
1470
1471 /* Check if data is available */
1472 if (!(rc & 0x01)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001473 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001474 "%s: MER measures aren't available yet.\n", __func__);
1475 return -EBUSY;
1476 }
1477
1478 /* Read all layers */
1479 for (i = 0; i < 3; i++) {
1480 if (!(c->isdbt_layer_enabled & (1 << i))) {
1481 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1482 continue;
1483 }
1484
1485 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1486 if (rc < 0)
1487 return rc;
1488 rc = mb86a20s_readreg(state, 0x51);
1489 if (rc < 0)
1490 return rc;
1491 mer = rc << 16;
1492 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1493 if (rc < 0)
1494 return rc;
1495 rc = mb86a20s_readreg(state, 0x51);
1496 if (rc < 0)
1497 return rc;
1498 mer |= rc << 8;
1499 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1500 if (rc < 0)
1501 return rc;
1502 rc = mb86a20s_readreg(state, 0x51);
1503 if (rc < 0)
1504 return rc;
1505 mer |= rc;
1506
1507 switch (c->layer[i].modulation) {
1508 case DQPSK:
1509 case QPSK:
1510 segs = cnr_qpsk_table;
1511 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1512 break;
1513 case QAM_16:
1514 segs = cnr_16qam_table;
1515 segs_len = ARRAY_SIZE(cnr_16qam_table);
1516 break;
1517 default:
1518 case QAM_64:
1519 segs = cnr_64qam_table;
1520 segs_len = ARRAY_SIZE(cnr_64qam_table);
1521 break;
1522 }
1523 cnr = interpolate_value(mer, segs, segs_len);
1524
1525 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1526 c->cnr.stat[1 + i].svalue = cnr;
1527
1528 dev_dbg(&state->i2c->dev,
1529 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1530 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1531
1532 }
1533
1534 /* Start a new MER measurement */
1535 /* MER counter reset */
1536 rc = mb86a20s_writereg(state, 0x50, 0x50);
1537 if (rc < 0)
1538 return rc;
1539 rc = mb86a20s_readreg(state, 0x51);
1540 if (rc < 0)
1541 return rc;
1542 val = rc;
1543
1544 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1545 if (rc < 0)
1546 return rc;
1547 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1548 if (rc < 0)
1549 return rc;
1550
1551 return 0;
1552}
1553
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001554static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1555{
1556 struct mb86a20s_state *state = fe->demodulator_priv;
1557 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1558 int i;
1559
1560 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1561
1562 /* Fill the length of each status counter */
1563
1564 /* Only global stats */
1565 c->strength.len = 1;
1566
1567 /* Per-layer stats - 3 layers + global */
1568 c->cnr.len = 4;
1569 c->pre_bit_error.len = 4;
1570 c->pre_bit_count.len = 4;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001571 c->post_bit_error.len = 4;
1572 c->post_bit_count.len = 4;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001573 c->block_error.len = 4;
1574 c->block_count.len = 4;
1575
1576 /* Signal is always available */
1577 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1578 c->strength.stat[0].uvalue = 0;
1579
1580 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1581 for (i = 0; i < 4; i++) {
1582 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1583 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1584 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001585 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1586 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001587 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1588 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1589 }
1590}
1591
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001592static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001593{
1594 struct mb86a20s_state *state = fe->demodulator_priv;
1595 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1596 int rc = 0, i;
1597 u32 bit_error = 0, bit_count = 0;
1598 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001599 u32 t_post_bit_error = 0, t_post_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001600 u32 block_error = 0, block_count = 0;
1601 u32 t_block_error = 0, t_block_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001602 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1603 int per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001604
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001605 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1606
1607 mb86a20s_get_main_CNR(fe);
1608
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001609 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001610 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001611
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001612 /*
1613 * At state 7, only CNR is available
1614 * For BER measures, state=9 is required
1615 * FIXME: we may get MER measures with state=8
1616 */
1617 if (status_nr < 9)
1618 return 0;
1619
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001620 for (i = 0; i < 3; i++) {
1621 if (c->isdbt_layer_enabled & (1 << i)) {
1622 /* Layer is active and has rc segments */
1623 active_layers++;
1624
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001625 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001626 rc = mb86a20s_get_pre_ber(fe, i,
1627 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001628 if (rc >= 0) {
1629 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1630 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1631 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1632 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1633 } else if (rc != -EBUSY) {
1634 /*
1635 * If an I/O error happened,
1636 * measures are now unavailable
1637 */
1638 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1639 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1640 dev_err(&state->i2c->dev,
1641 "%s: Can't get BER for layer %c (error %d).\n",
1642 __func__, 'A' + i, rc);
1643 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001644 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001645 pre_ber_layers++;
1646
1647 /* Handle BER post vterbi */
1648 rc = mb86a20s_get_post_ber(fe, i,
1649 &bit_error, &bit_count);
1650 if (rc >= 0) {
1651 c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1652 c->post_bit_error.stat[1 + i].uvalue += bit_error;
1653 c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1654 c->post_bit_count.stat[1 + i].uvalue += bit_count;
1655 } else if (rc != -EBUSY) {
1656 /*
1657 * If an I/O error happened,
1658 * measures are now unavailable
1659 */
1660 c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1661 c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1662 dev_err(&state->i2c->dev,
1663 "%s: Can't get BER for layer %c (error %d).\n",
1664 __func__, 'A' + i, rc);
1665 }
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001666 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1667 post_ber_layers++;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001668
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001669 /* Handle Block errors for PER/UCB reports */
1670 rc = mb86a20s_get_blk_error(fe, i,
1671 &block_error,
1672 &block_count);
1673 if (rc >= 0) {
1674 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1675 c->block_error.stat[1 + i].uvalue += block_error;
1676 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1677 c->block_count.stat[1 + i].uvalue += block_count;
1678 } else if (rc != -EBUSY) {
1679 /*
1680 * If an I/O error happened,
1681 * measures are now unavailable
1682 */
1683 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1684 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1685 dev_err(&state->i2c->dev,
1686 "%s: Can't get PER for layer %c (error %d).\n",
1687 __func__, 'A' + i, rc);
1688
1689 }
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001690 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1691 per_layers++;
1692
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001693 /* Update total preBER */
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001694 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1695 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001696
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001697 /* Update total postBER */
1698 t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
1699 t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
1700
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001701 /* Update total PER */
1702 t_block_error += c->block_error.stat[1 + i].uvalue;
1703 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001704 }
1705 }
1706
1707 /*
1708 * Start showing global count if at least one error count is
1709 * available.
1710 */
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001711 if (pre_ber_layers) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001712 /*
1713 * At least one per-layer BER measure was read. We can now
1714 * calculate the total BER
1715 *
1716 * Total Bit Error/Count is calculated as the sum of the
1717 * bit errors on all active layers.
1718 */
1719 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1720 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1721 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1722 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001723 } else {
1724 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1725 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001726 }
1727
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001728 /*
1729 * Start showing global count if at least one error count is
1730 * available.
1731 */
1732 if (post_ber_layers) {
1733 /*
1734 * At least one per-layer BER measure was read. We can now
1735 * calculate the total BER
1736 *
1737 * Total Bit Error/Count is calculated as the sum of the
1738 * bit errors on all active layers.
1739 */
1740 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1741 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1742 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1743 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001744 } else {
1745 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1746 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001747 }
1748
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001749 if (per_layers) {
1750 /*
1751 * At least one per-layer UCB measure was read. We can now
1752 * calculate the total UCB
1753 *
1754 * Total block Error/Count is calculated as the sum of the
1755 * block errors on all active layers.
1756 */
1757 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1758 c->block_error.stat[0].uvalue = t_block_error;
1759 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1760 c->block_count.stat[0].uvalue = t_block_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001761 } else {
1762 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1763 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001764 }
1765
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001766 return rc;
1767}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001768
1769/*
1770 * The functions below are called via DVB callbacks, so they need to
1771 * properly use the I2C gate control
1772 */
1773
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001774static int mb86a20s_initfe(struct dvb_frontend *fe)
1775{
1776 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001777 u64 pll;
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001778 u32 fclk;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001779 int rc;
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001780 u8 regD5 = 1, reg71, reg09 = 0x3a;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001781
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001782 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001783
1784 if (fe->ops.i2c_gate_ctrl)
1785 fe->ops.i2c_gate_ctrl(fe, 0);
1786
1787 /* Initialize the frontend */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001788 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001789 if (rc < 0)
1790 goto err;
1791
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001792 if (!state->inversion)
1793 reg09 |= 0x04;
1794 rc = mb86a20s_writereg(state, 0x09, reg09);
1795 if (rc < 0)
1796 goto err;
1797 if (!state->bw)
1798 reg71 = 1;
1799 else
1800 reg71 = 0;
1801 rc = mb86a20s_writereg(state, 0x39, reg71);
1802 if (rc < 0)
1803 goto err;
1804 rc = mb86a20s_writereg(state, 0x71, state->bw);
1805 if (rc < 0)
1806 goto err;
1807 if (state->subchannel) {
1808 rc = mb86a20s_writereg(state, 0x44, state->subchannel);
1809 if (rc < 0)
1810 goto err;
1811 }
1812
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001813 fclk = state->config->fclk;
1814 if (!fclk)
1815 fclk = 32571428;
1816
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001817 /* Adjust IF frequency to match tuner */
1818 if (fe->ops.tuner_ops.get_if_frequency)
1819 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1820
1821 if (!state->if_freq)
1822 state->if_freq = 3300000;
1823
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001824 pll = (((u64)1) << 34) * state->if_freq;
1825 do_div(pll, 63 * fclk);
1826 pll = (1 << 25) - pll;
1827 rc = mb86a20s_writereg(state, 0x28, 0x2a);
1828 if (rc < 0)
1829 goto err;
1830 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1831 if (rc < 0)
1832 goto err;
1833 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1834 if (rc < 0)
1835 goto err;
1836 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1837 if (rc < 0)
1838 goto err;
1839 dev_dbg(&state->i2c->dev, "%s: fclk=%d, IF=%d, clock reg=0x%06llx\n",
1840 __func__, fclk, state->if_freq, (long long)pll);
1841
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001842 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1843 pll = state->if_freq * 1677721600L;
1844 do_div(pll, 1628571429L);
1845 rc = mb86a20s_writereg(state, 0x28, 0x20);
1846 if (rc < 0)
1847 goto err;
1848 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1849 if (rc < 0)
1850 goto err;
1851 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1852 if (rc < 0)
1853 goto err;
1854 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1855 if (rc < 0)
1856 goto err;
Mauro Carvalho Chehab0e4bbed2013-03-04 08:15:49 -03001857 dev_dbg(&state->i2c->dev, "%s: IF=%d, IF reg=0x%06llx\n",
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001858 __func__, state->if_freq, (long long)pll);
1859
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001860 if (!state->config->is_serial) {
1861 regD5 &= ~1;
1862
1863 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1864 if (rc < 0)
1865 goto err;
1866 rc = mb86a20s_writereg(state, 0x51, regD5);
1867 if (rc < 0)
1868 goto err;
1869 }
1870
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001871 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1872 if (rc < 0)
1873 goto err;
1874
1875
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001876err:
1877 if (fe->ops.i2c_gate_ctrl)
1878 fe->ops.i2c_gate_ctrl(fe, 1);
1879
1880 if (rc < 0) {
1881 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001882 dev_info(&state->i2c->dev,
1883 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001884 } else {
1885 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001886 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001887 }
1888 return rc;
1889}
1890
1891static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1892{
1893 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001894 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001895 int rc, if_freq;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001896 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001897
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03001898 if (!c->isdbt_layer_enabled)
1899 c->isdbt_layer_enabled = 7;
1900
1901 if (c->isdbt_layer_enabled == 1)
1902 state->bw = MB86A20S_1SEG;
1903 else if (c->isdbt_partial_reception)
1904 state->bw = MB86A20S_13SEG_PARTIAL;
1905 else
1906 state->bw = MB86A20S_13SEG;
1907
1908 if (c->inversion == INVERSION_ON)
1909 state->inversion = true;
1910 else
1911 state->inversion = false;
1912
1913 if (!c->isdbt_sb_mode) {
1914 state->subchannel = 0;
1915 } else {
1916 if (c->isdbt_sb_subchannel > ARRAY_SIZE(mb86a20s_subchannel))
1917 c->isdbt_sb_subchannel = 0;
1918
1919 state->subchannel = mb86a20s_subchannel[c->isdbt_sb_subchannel];
1920 }
1921
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001922 /*
1923 * Gate should already be opened, but it doesn't hurt to
1924 * double-check
1925 */
1926 if (fe->ops.i2c_gate_ctrl)
1927 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001928 fe->ops.tuner_ops.set_params(fe);
1929
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001930 if (fe->ops.tuner_ops.get_if_frequency)
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001931 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1932
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001933 /*
1934 * Make it more reliable: if, for some reason, the initial
1935 * device initialization doesn't happen, initialize it when
1936 * a SBTVD parameters are adjusted.
1937 *
1938 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1939 * the agc callback logic is not called during DVB attach time,
1940 * causing mb86a20s to not be initialized with Kworld SBTVD.
1941 * So, this hack is needed, in order to make Kworld SBTVD to work.
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001942 *
1943 * It is also needed to change the IF after the initial init.
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001944 *
1945 * HACK: Always init the frontend when set_frontend is called:
1946 * it was noticed that, on some devices, it fails to lock on a
1947 * different channel. So, it is better to reset everything, even
1948 * wasting some time, than to loose channel lock.
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001949 */
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001950 mb86a20s_initfe(fe);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001951
1952 if (fe->ops.i2c_gate_ctrl)
1953 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001954
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001955 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001956 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehab3a2e4752013-03-02 15:02:23 -03001957 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001958
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001959 if (fe->ops.i2c_gate_ctrl)
1960 fe->ops.i2c_gate_ctrl(fe, 1);
1961
1962 return rc;
1963}
1964
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001965static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1966 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001967{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001968 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001969 int rc, status_nr;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001970
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001971 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001972
1973 if (fe->ops.i2c_gate_ctrl)
1974 fe->ops.i2c_gate_ctrl(fe, 0);
1975
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001976 /* Get lock */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001977 status_nr = mb86a20s_read_status(fe, status);
1978 if (status_nr < 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001979 mb86a20s_stats_not_ready(fe);
1980 mb86a20s_reset_frontend_cache(fe);
1981 }
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001982 if (status_nr < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001983 dev_err(&state->i2c->dev,
1984 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001985 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001986 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001987
1988 /* Get signal strength */
1989 rc = mb86a20s_read_signal_strength(fe);
1990 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001991 dev_err(&state->i2c->dev,
1992 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001993 mb86a20s_stats_not_ready(fe);
1994 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001995
1996 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001997 goto error;
1998 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001999
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03002000 if (status_nr >= 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002001 /* Get TMCC info*/
2002 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002003 if (rc < 0) {
2004 dev_err(&state->i2c->dev,
2005 "%s: Can't get FE TMCC data.\n", __func__);
2006 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002007 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002008 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002009
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002010 /* Get statistics */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03002011 rc = mb86a20s_get_stats(fe, status_nr);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002012 if (rc < 0 && rc != -EBUSY) {
2013 dev_err(&state->i2c->dev,
2014 "%s: Can't get FE statistics.\n", __func__);
2015 rc = 0;
2016 goto error;
2017 }
2018 rc = 0; /* Don't return EBUSY to userspace */
2019 }
2020 goto ok;
2021
2022error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002023 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02002024
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002025ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02002026 if (fe->ops.i2c_gate_ctrl)
2027 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03002028
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002029 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02002030}
2031
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002032static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
2033 u16 *strength)
2034{
2035 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2036
2037
2038 *strength = c->strength.stat[0].uvalue;
2039
2040 return 0;
2041}
2042
2043static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
2044{
2045 /*
2046 * get_frontend is now handled together with other stats
2047 * retrival, when read_status() is called, as some statistics
2048 * will depend on the layers detection.
2049 */
2050 return 0;
2051};
2052
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002053static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03002054 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002055 unsigned int mode_flags,
2056 unsigned int *delay,
2057 fe_status_t *status)
2058{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002059 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002060 int rc = 0;
2061
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002062 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002063
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03002064 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002065 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002066
2067 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002068 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002069
2070 return rc;
2071}
2072
2073static void mb86a20s_release(struct dvb_frontend *fe)
2074{
2075 struct mb86a20s_state *state = fe->demodulator_priv;
2076
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002077 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002078
2079 kfree(state);
2080}
2081
2082static struct dvb_frontend_ops mb86a20s_ops;
2083
2084struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2085 struct i2c_adapter *i2c)
2086{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002087 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002088 u8 rev;
2089
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002090 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2091
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002092 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002093 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002094 if (state == NULL) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002095 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002096 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002097 goto error;
2098 }
2099
2100 /* setup the state */
2101 state->config = config;
2102 state->i2c = i2c;
2103
2104 /* create dvb_frontend */
2105 memcpy(&state->frontend.ops, &mb86a20s_ops,
2106 sizeof(struct dvb_frontend_ops));
2107 state->frontend.demodulator_priv = state;
2108
2109 /* Check if it is a mb86a20s frontend */
2110 rev = mb86a20s_readreg(state, 0);
2111
2112 if (rev == 0x13) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002113 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002114 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002115 } else {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002116 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002117 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002118 rev);
2119 goto error;
2120 }
2121
2122 return &state->frontend;
2123
2124error:
2125 kfree(state);
2126 return NULL;
2127}
2128EXPORT_SYMBOL(mb86a20s_attach);
2129
2130static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002131 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002132 /* Use dib8000 values per default */
2133 .info = {
2134 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehab04fa7252013-03-04 07:10:06 -03002135 .caps = FE_CAN_RECOVER |
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002136 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2137 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2138 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2139 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2140 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2141 /* Actually, those values depend on the used tuner */
2142 .frequency_min = 45000000,
2143 .frequency_max = 864000000,
2144 .frequency_stepsize = 62500,
2145 },
2146
2147 .release = mb86a20s_release,
2148
2149 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002150 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002151 .get_frontend = mb86a20s_get_frontend_dummy,
2152 .read_status = mb86a20s_read_status_and_stats,
2153 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002154 .tune = mb86a20s_tune,
2155};
2156
2157MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2158MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2159MODULE_LICENSE("GPL");