blob: 305ebc08cf4319a3b76899148195c2d758d22f6c [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 Chehabb9ede792010-09-27 20:52:43 -030027struct mb86a20s_state {
28 struct i2c_adapter *i2c;
29 const struct mb86a20s_config *config;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030030 u32 last_frequency;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030031
32 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030033
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030034 u32 estimated_rate[3];
35
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030036 bool need_init;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030037};
38
39struct regdata {
40 u8 reg;
41 u8 data;
42};
43
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030044#define BER_SAMPLING_RATE 1 /* Seconds */
45
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030046/*
47 * Initialization sequence: Use whatevere default values that PV SBTVD
48 * does on its initialisation, obtained via USB snoop
49 */
50static struct regdata mb86a20s_init[] = {
51 { 0x70, 0x0f },
52 { 0x70, 0xff },
53 { 0x08, 0x01 },
54 { 0x09, 0x3e },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020055 { 0x50, 0xd1 }, { 0x51, 0x22 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030056 { 0x39, 0x01 },
57 { 0x71, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020058 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
59 { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020060 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030061 { 0x3b, 0x21 },
62 { 0x3c, 0x3a },
63 { 0x01, 0x0d },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020064 { 0x04, 0x08 }, { 0x05, 0x05 },
65 { 0x04, 0x0e }, { 0x05, 0x00 },
66 { 0x04, 0x0f }, { 0x05, 0x14 },
67 { 0x04, 0x0b }, { 0x05, 0x8c },
68 { 0x04, 0x00 }, { 0x05, 0x00 },
69 { 0x04, 0x01 }, { 0x05, 0x07 },
70 { 0x04, 0x02 }, { 0x05, 0x0f },
71 { 0x04, 0x03 }, { 0x05, 0xa0 },
72 { 0x04, 0x09 }, { 0x05, 0x00 },
73 { 0x04, 0x0a }, { 0x05, 0xff },
74 { 0x04, 0x27 }, { 0x05, 0x64 },
75 { 0x04, 0x28 }, { 0x05, 0x00 },
76 { 0x04, 0x1e }, { 0x05, 0xff },
77 { 0x04, 0x29 }, { 0x05, 0x0a },
78 { 0x04, 0x32 }, { 0x05, 0x0a },
79 { 0x04, 0x14 }, { 0x05, 0x02 },
80 { 0x04, 0x04 }, { 0x05, 0x00 },
81 { 0x04, 0x05 }, { 0x05, 0x22 },
82 { 0x04, 0x06 }, { 0x05, 0x0e },
83 { 0x04, 0x07 }, { 0x05, 0xd8 },
84 { 0x04, 0x12 }, { 0x05, 0x00 },
85 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020086 { 0x04, 0x15 }, { 0x05, 0x4e },
87 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030088
89 /*
90 * On this demod, when the bit count reaches the count below,
91 * it collects the bit error count. The bit counters are initialized
92 * to 65535 here. This warrants that all of them will be quickly
93 * calculated when device gets locked. As TMCC is parsed, the values
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030094 * will be adjusted later in the driver's code.
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030095 */
96 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
97 { 0x50, 0xa7 }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020098 { 0x50, 0xa8 }, { 0x51, 0xff },
99 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300100 { 0x50, 0xaa }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200101 { 0x50, 0xab }, { 0x51, 0xff },
102 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300103 { 0x50, 0xad }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200104 { 0x50, 0xae }, { 0x51, 0xff },
105 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300106
107 { 0x5e, 0x00 }, /* Turn off BER after Viterbi */
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200108 { 0x50, 0xdc }, { 0x51, 0x01 },
109 { 0x50, 0xdd }, { 0x51, 0xf4 },
110 { 0x50, 0xde }, { 0x51, 0x01 },
111 { 0x50, 0xdf }, { 0x51, 0xf4 },
112 { 0x50, 0xe0 }, { 0x51, 0x01 },
113 { 0x50, 0xe1 }, { 0x51, 0xf4 },
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300114
115 /*
116 * On this demod, when the block count reaches the count below,
117 * it collects the block error count. The block counters are initialized
118 * to 127 here. This warrants that all of them will be quickly
119 * calculated when device gets locked. As TMCC is parsed, the values
120 * will be adjusted later in the driver's code.
121 */
122 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
123 { 0x50, 0xb2 }, { 0x51, 0x00 },
124 { 0x50, 0xb3 }, { 0x51, 0x7f },
125 { 0x50, 0xb4 }, { 0x51, 0x00 },
126 { 0x50, 0xb5 }, { 0x51, 0x7f },
127 { 0x50, 0xb6 }, { 0x51, 0x00 },
128 { 0x50, 0xb7 }, { 0x51, 0x7f },
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300129
130 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300131 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
132 { 0x45, 0x04 }, /* CN symbol 4 */
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300133 { 0x48, 0x04 }, /* CN manual mode */
134
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200135 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
136 { 0x50, 0xd6 }, { 0x51, 0x1f },
137 { 0x50, 0xd2 }, { 0x51, 0x03 },
138 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200139 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
140 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300141
142 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200143 { 0x28, 0x00 }, { 0x29, 0x10 },
144 { 0x28, 0x05 }, { 0x29, 0x02 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300145 { 0x1c, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200146 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
147 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
148 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
149 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
150 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
151 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
152 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
153 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
154 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
155 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
156 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
157 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
158 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
159 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
160 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
161 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
162 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
163 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
164 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
165 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
166 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
167 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
168 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
169 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
170 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
171 { 0x50, 0x1e }, { 0x51, 0x5d },
172 { 0x50, 0x22 }, { 0x51, 0x00 },
173 { 0x50, 0x23 }, { 0x51, 0xc8 },
174 { 0x50, 0x24 }, { 0x51, 0x00 },
175 { 0x50, 0x25 }, { 0x51, 0xf0 },
176 { 0x50, 0x26 }, { 0x51, 0x00 },
177 { 0x50, 0x27 }, { 0x51, 0xc3 },
178 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200179 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300180 { 0xd0, 0x00 },
181};
182
183static struct regdata mb86a20s_reset_reception[] = {
184 { 0x70, 0xf0 },
185 { 0x70, 0xff },
186 { 0x08, 0x01 },
187 { 0x08, 0x00 },
188};
189
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300190static struct regdata mb86a20s_vber_reset[] = {
191 { 0x53, 0x00 }, /* VBER Counter reset */
192 { 0x53, 0x07 },
193};
194
195static struct regdata mb86a20s_per_reset[] = {
196 { 0x50, 0xb1 }, /* PER Counter reset */
197 { 0x51, 0x07 },
198 { 0x51, 0x00 },
199};
200
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200201/*
202 * I2C read/write functions and macros
203 */
204
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300205static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300206 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300207{
208 u8 buf[] = { reg, data };
209 struct i2c_msg msg = {
210 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
211 };
212 int rc;
213
214 rc = i2c_transfer(state->i2c, &msg, 1);
215 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200216 dev_err(&state->i2c->dev,
217 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
218 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300219 return rc;
220 }
221
222 return 0;
223}
224
225static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
226 u8 i2c_addr, struct regdata *rd, int size)
227{
228 int i, rc;
229
230 for (i = 0; i < size; i++) {
231 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
232 rd[i].data);
233 if (rc < 0)
234 return rc;
235 }
236 return 0;
237}
238
239static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
240 u8 i2c_addr, u8 reg)
241{
242 u8 val;
243 int rc;
244 struct i2c_msg msg[] = {
245 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
246 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
247 };
248
249 rc = i2c_transfer(state->i2c, msg, 2);
250
251 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200252 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
253 __func__, reg, rc);
254 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300255 }
256
257 return val;
258}
259
260#define mb86a20s_readreg(state, reg) \
261 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
262#define mb86a20s_writereg(state, reg, val) \
263 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
264#define mb86a20s_writeregdata(state, regdata) \
265 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
266 regdata, ARRAY_SIZE(regdata))
267
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300268/*
269 * Ancillary internal routines (likely compiled inlined)
270 *
271 * The functions below assume that gateway lock has already obtained
272 */
273
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200274static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300275{
276 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200277 int val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300278
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200279 *status = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300280
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200281 val = mb86a20s_readreg(state, 0x0a) & 0xf;
282 if (val < 0)
283 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300284
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200285 if (val >= 2)
286 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300287
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200288 if (val >= 4)
289 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300290
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200291 if (val >= 5)
292 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300293
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200294 if (val >= 7)
295 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300296
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200297 if (val >= 8) /* Maybe 9? */
298 *status |= FE_HAS_LOCK;
299
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200300 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
301 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200302
303 return 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300304}
305
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300306static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300307{
308 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300309 int rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300310 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300311
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300312 /* Does a binary search to get RF strength */
313 rf_max = 0xfff;
314 rf_min = 0;
315 do {
316 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300317 rc = mb86a20s_writereg(state, 0x04, 0x1f);
318 if (rc < 0)
319 return rc;
320 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
321 if (rc < 0)
322 return rc;
323 rc = mb86a20s_writereg(state, 0x04, 0x20);
324 if (rc < 0)
325 return rc;
326 rc = mb86a20s_writereg(state, 0x04, rf);
327 if (rc < 0)
328 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300329
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300330 rc = mb86a20s_readreg(state, 0x02);
331 if (rc < 0)
332 return rc;
333 if (rc & 0x08)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300334 rf_min = (rf_max + rf_min) / 2;
335 else
336 rf_max = (rf_max + rf_min) / 2;
337 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300338 rf = (rf_max + rf_min) / 2;
339
340 /* Rescale it from 2^12 (4096) to 2^16 */
341 rf <<= (16 - 12);
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200342 dev_dbg(&state->i2c->dev,
343 "%s: signal strength = %d (%d < RF=%d < %d)\n",
344 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300345 return rf;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300346 }
347 } while (1);
348
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300349 return 0;
350}
351
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200352static int mb86a20s_get_modulation(struct mb86a20s_state *state,
353 unsigned layer)
354{
355 int rc;
356 static unsigned char reg[] = {
357 [0] = 0x86, /* Layer A */
358 [1] = 0x8a, /* Layer B */
359 [2] = 0x8e, /* Layer C */
360 };
361
Dan Carpenter82033bc2012-01-13 02:28:34 -0300362 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200363 return -EINVAL;
364 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
365 if (rc < 0)
366 return rc;
367 rc = mb86a20s_readreg(state, 0x6e);
368 if (rc < 0)
369 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300370 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200371 case 0:
372 return DQPSK;
373 case 1:
374 return QPSK;
375 case 2:
376 return QAM_16;
377 case 3:
378 return QAM_64;
379 default:
380 return QAM_AUTO;
381 }
382}
383
384static int mb86a20s_get_fec(struct mb86a20s_state *state,
385 unsigned layer)
386{
387 int rc;
388
389 static unsigned char reg[] = {
390 [0] = 0x87, /* Layer A */
391 [1] = 0x8b, /* Layer B */
392 [2] = 0x8f, /* Layer C */
393 };
394
Dan Carpenter82033bc2012-01-13 02:28:34 -0300395 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200396 return -EINVAL;
397 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
398 if (rc < 0)
399 return rc;
400 rc = mb86a20s_readreg(state, 0x6e);
401 if (rc < 0)
402 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300403 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200404 case 0:
405 return FEC_1_2;
406 case 1:
407 return FEC_2_3;
408 case 2:
409 return FEC_3_4;
410 case 3:
411 return FEC_5_6;
412 case 4:
413 return FEC_7_8;
414 default:
415 return FEC_AUTO;
416 }
417}
418
419static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
420 unsigned layer)
421{
422 int rc;
423
424 static unsigned char reg[] = {
425 [0] = 0x88, /* Layer A */
426 [1] = 0x8c, /* Layer B */
427 [2] = 0x90, /* Layer C */
428 };
429
Dan Carpenter82033bc2012-01-13 02:28:34 -0300430 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200431 return -EINVAL;
432 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
433 if (rc < 0)
434 return rc;
435 rc = mb86a20s_readreg(state, 0x6e);
436 if (rc < 0)
437 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300438
439 switch ((rc >> 4) & 0x07) {
440 case 1:
441 return GUARD_INTERVAL_1_4;
442 case 2:
443 return GUARD_INTERVAL_1_8;
444 case 3:
445 return GUARD_INTERVAL_1_16;
446 case 4:
447 return GUARD_INTERVAL_1_32;
448
449 default:
450 case 0:
451 return GUARD_INTERVAL_AUTO;
452 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200453}
454
455static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
456 unsigned layer)
457{
458 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200459 static unsigned char reg[] = {
460 [0] = 0x89, /* Layer A */
461 [1] = 0x8d, /* Layer B */
462 [2] = 0x91, /* Layer C */
463 };
464
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200465 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
466
Dan Carpenter82033bc2012-01-13 02:28:34 -0300467 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200468 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200469
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200470 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;
476 count = (rc >> 4) & 0x0f;
477
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200478 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
479
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200480 return count;
481}
482
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300483static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
484{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200485 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300486 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
487
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200488 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
489
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300490 /* Fixed parameters */
491 c->delivery_system = SYS_ISDBT;
492 c->bandwidth_hz = 6000000;
493
494 /* Initialize values that will be later autodetected */
495 c->isdbt_layer_enabled = 0;
496 c->transmission_mode = TRANSMISSION_MODE_AUTO;
497 c->guard_interval = GUARD_INTERVAL_AUTO;
498 c->isdbt_sb_mode = 0;
499 c->isdbt_sb_segment_count = 0;
500}
501
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300502/*
503 * Estimates the bit rate using the per-segment bit rate given by
504 * ABNT/NBR 15601 spec (table 4).
505 */
506static u32 isdbt_rate[3][5][4] = {
507 { /* DQPSK/QPSK */
508 { 280850, 312060, 330420, 340430 }, /* 1/2 */
509 { 374470, 416080, 440560, 453910 }, /* 2/3 */
510 { 421280, 468090, 495630, 510650 }, /* 3/4 */
511 { 468090, 520100, 550700, 567390 }, /* 5/6 */
512 { 491500, 546110, 578230, 595760 }, /* 7/8 */
513 }, { /* QAM16 */
514 { 561710, 624130, 660840, 680870 }, /* 1/2 */
515 { 748950, 832170, 881120, 907820 }, /* 2/3 */
516 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
517 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
518 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
519 }, { /* QAM64 */
520 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
521 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
522 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
523 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
524 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
525 }
526};
527
528static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
529 u32 modulation, u32 fec, u32 interleaving,
530 u32 segment)
531{
532 struct mb86a20s_state *state = fe->demodulator_priv;
533 u32 rate;
534 int m, f, i;
535
536 /*
537 * If modulation/fec/interleaving is not detected, the default is
538 * to consider the lowest bit rate, to avoid taking too long time
539 * to get BER.
540 */
541 switch (modulation) {
542 case DQPSK:
543 case QPSK:
544 default:
545 m = 0;
546 break;
547 case QAM_16:
548 m = 1;
549 break;
550 case QAM_64:
551 m = 2;
552 break;
553 }
554
555 switch (fec) {
556 default:
557 case FEC_1_2:
558 case FEC_AUTO:
559 f = 0;
560 break;
561 case FEC_2_3:
562 f = 1;
563 break;
564 case FEC_3_4:
565 f = 2;
566 break;
567 case FEC_5_6:
568 f = 3;
569 break;
570 case FEC_7_8:
571 f = 4;
572 break;
573 }
574
575 switch (interleaving) {
576 default:
577 case GUARD_INTERVAL_1_4:
578 i = 0;
579 break;
580 case GUARD_INTERVAL_1_8:
581 i = 1;
582 break;
583 case GUARD_INTERVAL_1_16:
584 i = 2;
585 break;
586 case GUARD_INTERVAL_1_32:
587 i = 3;
588 break;
589 }
590
591 /* Samples BER at BER_SAMPLING_RATE seconds */
592 rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
593
594 /* Avoids sampling too quickly or to overflow the register */
595 if (rate < 256)
596 rate = 256;
597 else if (rate > (1 << 24) - 1)
598 rate = (1 << 24) - 1;
599
600 dev_dbg(&state->i2c->dev,
601 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
602 __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
603 rate, rate);
604
605 state->estimated_rate[i] = rate;
606}
607
608
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300609static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300610{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200611 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300612 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200613 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300614
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200615 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
616
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300617 /* Reset frontend cache to default values */
618 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200619
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200620 /* Check for partial reception */
621 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300622 if (rc < 0)
623 return rc;
624 rc = mb86a20s_readreg(state, 0x6e);
625 if (rc < 0)
626 return rc;
627 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200628
629 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300630
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200631 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200632 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
633 __func__, 'A' + i);
634
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200635 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300636 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200637 goto noperlayer_error;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300638 if (rc >= 0 && rc < 14) {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300639 c->layer[i].segment_count = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300640 } else {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300641 c->layer[i].segment_count = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300642 state->estimated_rate[i] = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200643 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300644 }
645 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200646 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300647 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200648 goto noperlayer_error;
649 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
650 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300651 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200652 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300653 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200654 goto noperlayer_error;
655 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
656 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300657 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200658 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300659 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200660 goto noperlayer_error;
661 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
662 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300663 c->layer[i].interleaving = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300664 mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
665 c->layer[i].fec,
666 c->layer[i].interleaving,
667 c->layer[i].segment_count);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200668 }
669
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200670 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300671 if (rc < 0)
672 return rc;
673 if ((rc & 0x60) == 0x20) {
674 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200675 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300676 if (!c->isdbt_sb_segment_count)
677 c->isdbt_sb_segment_count = 1;
678 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200679
680 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200681 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300682 if (rc < 0)
683 return rc;
684 if ((rc & 0x60) == 0x20) {
685 switch (rc & 0x0c >> 2) {
686 case 0:
687 c->transmission_mode = TRANSMISSION_MODE_2K;
688 break;
689 case 1:
690 c->transmission_mode = TRANSMISSION_MODE_4K;
691 break;
692 case 2:
693 c->transmission_mode = TRANSMISSION_MODE_8K;
694 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200695 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300696 }
697 if (!(rc & 0x10)) {
698 switch (rc & 0x3) {
699 case 0:
700 c->guard_interval = GUARD_INTERVAL_1_4;
701 break;
702 case 1:
703 c->guard_interval = GUARD_INTERVAL_1_8;
704 break;
705 case 2:
706 c->guard_interval = GUARD_INTERVAL_1_16;
707 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200708 }
709 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300710 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200711
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200712noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300713
714 /* per-layer info is incomplete; discard all per-layer */
715 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300716
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300717 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300718}
719
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300720static int mb86a20s_reset_counters(struct dvb_frontend *fe)
721{
722 struct mb86a20s_state *state = fe->demodulator_priv;
723 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
724 int rc, val;
725
726 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
727
728 /* Reset the counters, if the channel changed */
729 if (state->last_frequency != c->frequency) {
730 memset(&c->strength, 0, sizeof(c->strength));
731 memset(&c->cnr, 0, sizeof(c->cnr));
732 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
733 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
734 memset(&c->block_error, 0, sizeof(c->block_error));
735 memset(&c->block_count, 0, sizeof(c->block_count));
736
737 state->last_frequency = c->frequency;
738 }
739
740 /* Clear status for most stats */
741
742 /* BER counter reset */
743 rc = mb86a20s_writeregdata(state, mb86a20s_vber_reset);
744 if (rc < 0)
745 goto err;
746
747 /* MER, PER counter reset */
748 rc = mb86a20s_writeregdata(state, mb86a20s_per_reset);
749 if (rc < 0)
750 goto err;
751
752 /* CNR counter reset */
753 rc = mb86a20s_readreg(state, 0x45);
754 if (rc < 0)
755 goto err;
756 val = rc;
757 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
758 if (rc < 0)
759 goto err;
760 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
761 if (rc < 0)
762 goto err;
763
764 /* MER counter reset */
765 rc = mb86a20s_writereg(state, 0x50, 0x50);
766 if (rc < 0)
767 goto err;
768 rc = mb86a20s_readreg(state, 0x51);
769 if (rc < 0)
770 goto err;
771 val = rc;
772 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
773 if (rc < 0)
774 goto err;
775 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
776 if (rc < 0)
777 goto err;
778
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300779 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300780err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300781 dev_err(&state->i2c->dev,
782 "%s: Can't reset FE statistics (error %d).\n",
783 __func__, rc);
784ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300785 return rc;
786}
787
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300788static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
789 unsigned layer,
790 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300791{
792 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300793 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300794
795 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
796
797 if (layer >= 3)
798 return -EINVAL;
799
800 /* Check if the BER measures are already available */
801 rc = mb86a20s_readreg(state, 0x54);
802 if (rc < 0)
803 return rc;
804
805 /* Check if data is available for that layer */
806 if (!(rc & (1 << layer))) {
807 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300808 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300809 __func__, 'A' + layer);
810 return -EBUSY;
811 }
812
813 /* Read Bit Error Count */
814 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
815 if (rc < 0)
816 return rc;
817 *error = rc << 16;
818 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
819 if (rc < 0)
820 return rc;
821 *error |= rc << 8;
822 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
823 if (rc < 0)
824 return rc;
825 *error |= rc;
826
827 dev_dbg(&state->i2c->dev,
828 "%s: bit error before Viterbi for layer %c: %d.\n",
829 __func__, 'A' + layer, *error);
830
831 /* Read Bit Count */
832 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
833 if (rc < 0)
834 return rc;
835 rc = mb86a20s_readreg(state, 0x51);
836 if (rc < 0)
837 return rc;
838 *count = rc << 16;
839 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
840 if (rc < 0)
841 return rc;
842 rc = mb86a20s_readreg(state, 0x51);
843 if (rc < 0)
844 return rc;
845 *count |= rc << 8;
846 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
847 if (rc < 0)
848 return rc;
849 rc = mb86a20s_readreg(state, 0x51);
850 if (rc < 0)
851 return rc;
852 *count |= rc;
853
854 dev_dbg(&state->i2c->dev,
855 "%s: bit count before Viterbi for layer %c: %d.\n",
856 __func__, 'A' + layer, *count);
857
858
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300859 /*
860 * As we get TMCC data from the frontend, we can better estimate the
861 * BER bit counters, in order to do the BER measure during a longer
862 * time. Use those data, if available, to update the bit count
863 * measure.
864 */
865
866 if (state->estimated_rate[layer]
867 && state->estimated_rate[layer] != *count) {
868 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300869 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300870 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300871
872 /* Turn off BER before Viterbi */
873 rc = mb86a20s_writereg(state, 0x52, 0x00);
874
875 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300876 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
877 if (rc < 0)
878 return rc;
879 rc = mb86a20s_writereg(state, 0x51,
880 state->estimated_rate[layer] >> 16);
881 if (rc < 0)
882 return rc;
883 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
884 if (rc < 0)
885 return rc;
886 rc = mb86a20s_writereg(state, 0x51,
887 state->estimated_rate[layer] >> 8);
888 if (rc < 0)
889 return rc;
890 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
891 if (rc < 0)
892 return rc;
893 rc = mb86a20s_writereg(state, 0x51,
894 state->estimated_rate[layer]);
895 if (rc < 0)
896 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300897
898 /* Turn on BER before Viterbi */
899 rc = mb86a20s_writereg(state, 0x52, 0x01);
900
901 /* Reset all preBER counters */
902 rc = mb86a20s_writereg(state, 0x53, 0x00);
903 if (rc < 0)
904 return rc;
905 rc = mb86a20s_writereg(state, 0x53, 0x07);
906 } else {
907 /* Reset counter to collect new data */
908 rc = mb86a20s_readreg(state, 0x53);
909 if (rc < 0)
910 return rc;
911 val = rc;
912 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
913 if (rc < 0)
914 return rc;
915 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300916 }
917
918
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300919 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300920 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300921 if (rc < 0)
922 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300923 val = rc;
924 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
925 if (rc < 0)
926 return rc;
927 rc = mb86a20s_writereg(state, 0x5f, val);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300928
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300929 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300930}
931
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300932static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
933 unsigned layer,
934 u32 *error, u32 *count)
935{
936 struct mb86a20s_state *state = fe->demodulator_priv;
937 int rc;
938 u32 collect_rate;
939 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
940
941 if (layer >= 3)
942 return -EINVAL;
943
944 /* Check if the PER measures are already available */
945 rc = mb86a20s_writereg(state, 0x50, 0xb8);
946 if (rc < 0)
947 return rc;
948 rc = mb86a20s_readreg(state, 0x51);
949 if (rc < 0)
950 return rc;
951
952 /* Check if data is available for that layer */
953
954 if (!(rc & (1 << layer))) {
955 dev_dbg(&state->i2c->dev,
956 "%s: block counts for layer %c aren't available yet.\n",
957 __func__, 'A' + layer);
958 return -EBUSY;
959 }
960
961 /* Read Packet error Count */
962 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
963 if (rc < 0)
964 return rc;
965 rc = mb86a20s_readreg(state, 0x51);
966 if (rc < 0)
967 return rc;
968 *error = rc << 8;
969 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
970 if (rc < 0)
971 return rc;
972 rc = mb86a20s_readreg(state, 0x51);
973 if (rc < 0)
974 return rc;
975 *error |= rc;
976 dev_err(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
977 __func__, 'A' + layer, *error);
978
979 /* Read Bit Count */
980 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
981 if (rc < 0)
982 return rc;
983 rc = mb86a20s_readreg(state, 0x51);
984 if (rc < 0)
985 return rc;
986 *count = rc << 8;
987 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
988 if (rc < 0)
989 return rc;
990 rc = mb86a20s_readreg(state, 0x51);
991 if (rc < 0)
992 return rc;
993 *count |= rc;
994
995 dev_dbg(&state->i2c->dev,
996 "%s: block count for layer %c: %d.\n",
997 __func__, 'A' + layer, *count);
998
999 /*
1000 * As we get TMCC data from the frontend, we can better estimate the
1001 * BER bit counters, in order to do the BER measure during a longer
1002 * time. Use those data, if available, to update the bit count
1003 * measure.
1004 */
1005
1006 if (!state->estimated_rate[layer])
1007 goto reset_measurement;
1008
1009 collect_rate = state->estimated_rate[layer] / 204 / 8;
1010
1011 if (collect_rate < 32)
1012 collect_rate = 32;
1013 if (collect_rate > 65535)
1014 collect_rate = 65535;
1015
1016 if (collect_rate != *count) {
1017 dev_dbg(&state->i2c->dev,
1018 "%s: updating PER counter on layer %c to %d.\n",
1019 __func__, 'A' + layer, collect_rate);
1020 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1021 if (rc < 0)
1022 return rc;
1023 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1024 if (rc < 0)
1025 return rc;
1026 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1027 if (rc < 0)
1028 return rc;
1029 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1030 if (rc < 0)
1031 return rc;
1032 }
1033
1034reset_measurement:
1035 /* Reset counter to collect new data */
1036 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1037 if (rc < 0)
1038 return rc;
1039 rc = mb86a20s_writereg(state, 0x51, (1 << layer));
1040 if (rc < 0)
1041 return rc;
1042 rc = mb86a20s_writereg(state, 0x51, 0x00);
1043 if (rc < 0)
1044 return rc;
1045
1046 return 0;
1047}
1048
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001049struct linear_segments {
1050 unsigned x, y;
1051};
1052
1053/*
1054 * All tables below return a dB/1000 measurement
1055 */
1056
1057static struct linear_segments cnr_to_db_table[] = {
1058 { 19648, 0},
1059 { 18187, 1000},
1060 { 16534, 2000},
1061 { 14823, 3000},
1062 { 13161, 4000},
1063 { 11622, 5000},
1064 { 10279, 6000},
1065 { 9089, 7000},
1066 { 8042, 8000},
1067 { 7137, 9000},
1068 { 6342, 10000},
1069 { 5641, 11000},
1070 { 5030, 12000},
1071 { 4474, 13000},
1072 { 3988, 14000},
1073 { 3556, 15000},
1074 { 3180, 16000},
1075 { 2841, 17000},
1076 { 2541, 18000},
1077 { 2276, 19000},
1078 { 2038, 20000},
1079 { 1800, 21000},
1080 { 1625, 22000},
1081 { 1462, 23000},
1082 { 1324, 24000},
1083 { 1175, 25000},
1084 { 1063, 26000},
1085 { 980, 27000},
1086 { 907, 28000},
1087 { 840, 29000},
1088 { 788, 30000},
1089};
1090
1091static struct linear_segments cnr_64qam_table[] = {
1092 { 3922688, 0},
1093 { 3920384, 1000},
1094 { 3902720, 2000},
1095 { 3894784, 3000},
1096 { 3882496, 4000},
1097 { 3872768, 5000},
1098 { 3858944, 6000},
1099 { 3851520, 7000},
1100 { 3838976, 8000},
1101 { 3829248, 9000},
1102 { 3818240, 10000},
1103 { 3806976, 11000},
1104 { 3791872, 12000},
1105 { 3767040, 13000},
1106 { 3720960, 14000},
1107 { 3637504, 15000},
1108 { 3498496, 16000},
1109 { 3296000, 17000},
1110 { 3031040, 18000},
1111 { 2715392, 19000},
1112 { 2362624, 20000},
1113 { 1963264, 21000},
1114 { 1649664, 22000},
1115 { 1366784, 23000},
1116 { 1120768, 24000},
1117 { 890880, 25000},
1118 { 723456, 26000},
1119 { 612096, 27000},
1120 { 518912, 28000},
1121 { 448256, 29000},
1122 { 388864, 30000},
1123};
1124
1125static struct linear_segments cnr_16qam_table[] = {
1126 { 5314816, 0},
1127 { 5219072, 1000},
1128 { 5118720, 2000},
1129 { 4998912, 3000},
1130 { 4875520, 4000},
1131 { 4736000, 5000},
1132 { 4604160, 6000},
1133 { 4458752, 7000},
1134 { 4300288, 8000},
1135 { 4092928, 9000},
1136 { 3836160, 10000},
1137 { 3521024, 11000},
1138 { 3155968, 12000},
1139 { 2756864, 13000},
1140 { 2347008, 14000},
1141 { 1955072, 15000},
1142 { 1593600, 16000},
1143 { 1297920, 17000},
1144 { 1043968, 18000},
1145 { 839680, 19000},
1146 { 672256, 20000},
1147 { 523008, 21000},
1148 { 424704, 22000},
1149 { 345088, 23000},
1150 { 280064, 24000},
1151 { 221440, 25000},
1152 { 179712, 26000},
1153 { 151040, 27000},
1154 { 128512, 28000},
1155 { 110080, 29000},
1156 { 95744, 30000},
1157};
1158
1159struct linear_segments cnr_qpsk_table[] = {
1160 { 2834176, 0},
1161 { 2683648, 1000},
1162 { 2536960, 2000},
1163 { 2391808, 3000},
1164 { 2133248, 4000},
1165 { 1906176, 5000},
1166 { 1666560, 6000},
1167 { 1422080, 7000},
1168 { 1189632, 8000},
1169 { 976384, 9000},
1170 { 790272, 10000},
1171 { 633344, 11000},
1172 { 505600, 12000},
1173 { 402944, 13000},
1174 { 320768, 14000},
1175 { 255488, 15000},
1176 { 204032, 16000},
1177 { 163072, 17000},
1178 { 130304, 18000},
1179 { 105216, 19000},
1180 { 83456, 20000},
1181 { 65024, 21000},
1182 { 52480, 22000},
1183 { 42752, 23000},
1184 { 34560, 24000},
1185 { 27136, 25000},
1186 { 22016, 26000},
1187 { 18432, 27000},
1188 { 15616, 28000},
1189 { 13312, 29000},
1190 { 11520, 30000},
1191};
1192
1193static u32 interpolate_value(u32 value, struct linear_segments *segments,
1194 unsigned len)
1195{
1196 u64 tmp64;
1197 u32 dx, dy;
1198 int i, ret;
1199
1200 if (value >= segments[0].x)
1201 return segments[0].y;
1202 if (value < segments[len-1].x)
1203 return segments[len-1].y;
1204
1205 for (i = 1; i < len - 1; i++) {
1206 /* If value is identical, no need to interpolate */
1207 if (value == segments[i].x)
1208 return segments[i].y;
1209 if (value > segments[i].x)
1210 break;
1211 }
1212
1213 /* Linear interpolation between the two (x,y) points */
1214 dy = segments[i].y - segments[i - 1].y;
1215 dx = segments[i - 1].x - segments[i].x;
1216 tmp64 = value - segments[i].x;
1217 tmp64 *= dy;
1218 do_div(tmp64, dx);
1219 ret = segments[i].y - tmp64;
1220
1221 return ret;
1222}
1223
1224static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1225{
1226 struct mb86a20s_state *state = fe->demodulator_priv;
1227 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1228 u32 cnr_linear, cnr;
1229 int rc, val;
1230
1231 /* Check if CNR is available */
1232 rc = mb86a20s_readreg(state, 0x45);
1233 if (rc < 0)
1234 return rc;
1235
1236 if (!(rc & 0x40)) {
1237 dev_info(&state->i2c->dev, "%s: CNR is not available yet.\n",
1238 __func__);
1239 return -EBUSY;
1240 }
1241 val = rc;
1242
1243 rc = mb86a20s_readreg(state, 0x46);
1244 if (rc < 0)
1245 return rc;
1246 cnr_linear = rc << 8;
1247
1248 rc = mb86a20s_readreg(state, 0x46);
1249 if (rc < 0)
1250 return rc;
1251 cnr_linear |= rc;
1252
1253 cnr = interpolate_value(cnr_linear,
1254 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1255
1256 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1257 c->cnr.stat[0].svalue = cnr;
1258
1259 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1260 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1261
1262 /* CNR counter reset */
1263 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1264 if (rc < 0)
1265 return rc;
1266 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1267
1268 return rc;
1269}
1270
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001271static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001272{
1273 struct mb86a20s_state *state = fe->demodulator_priv;
1274 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1275 u32 mer, cnr;
1276 int rc, val, i;
1277 struct linear_segments *segs;
1278 unsigned segs_len;
1279
1280 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1281
1282 /* Check if the measures are already available */
1283 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1284 if (rc < 0)
1285 return rc;
1286 rc = mb86a20s_readreg(state, 0x51);
1287 if (rc < 0)
1288 return rc;
1289
1290 /* Check if data is available */
1291 if (!(rc & 0x01)) {
1292 dev_info(&state->i2c->dev,
1293 "%s: MER measures aren't available yet.\n", __func__);
1294 return -EBUSY;
1295 }
1296
1297 /* Read all layers */
1298 for (i = 0; i < 3; i++) {
1299 if (!(c->isdbt_layer_enabled & (1 << i))) {
1300 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1301 continue;
1302 }
1303
1304 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1305 if (rc < 0)
1306 return rc;
1307 rc = mb86a20s_readreg(state, 0x51);
1308 if (rc < 0)
1309 return rc;
1310 mer = rc << 16;
1311 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1312 if (rc < 0)
1313 return rc;
1314 rc = mb86a20s_readreg(state, 0x51);
1315 if (rc < 0)
1316 return rc;
1317 mer |= rc << 8;
1318 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1319 if (rc < 0)
1320 return rc;
1321 rc = mb86a20s_readreg(state, 0x51);
1322 if (rc < 0)
1323 return rc;
1324 mer |= rc;
1325
1326 switch (c->layer[i].modulation) {
1327 case DQPSK:
1328 case QPSK:
1329 segs = cnr_qpsk_table;
1330 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1331 break;
1332 case QAM_16:
1333 segs = cnr_16qam_table;
1334 segs_len = ARRAY_SIZE(cnr_16qam_table);
1335 break;
1336 default:
1337 case QAM_64:
1338 segs = cnr_64qam_table;
1339 segs_len = ARRAY_SIZE(cnr_64qam_table);
1340 break;
1341 }
1342 cnr = interpolate_value(mer, segs, segs_len);
1343
1344 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1345 c->cnr.stat[1 + i].svalue = cnr;
1346
1347 dev_dbg(&state->i2c->dev,
1348 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1349 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1350
1351 }
1352
1353 /* Start a new MER measurement */
1354 /* MER counter reset */
1355 rc = mb86a20s_writereg(state, 0x50, 0x50);
1356 if (rc < 0)
1357 return rc;
1358 rc = mb86a20s_readreg(state, 0x51);
1359 if (rc < 0)
1360 return rc;
1361 val = rc;
1362
1363 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1364 if (rc < 0)
1365 return rc;
1366 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1367 if (rc < 0)
1368 return rc;
1369
1370 return 0;
1371}
1372
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001373static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1374{
1375 struct mb86a20s_state *state = fe->demodulator_priv;
1376 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1377 int i;
1378
1379 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1380
1381 /* Fill the length of each status counter */
1382
1383 /* Only global stats */
1384 c->strength.len = 1;
1385
1386 /* Per-layer stats - 3 layers + global */
1387 c->cnr.len = 4;
1388 c->pre_bit_error.len = 4;
1389 c->pre_bit_count.len = 4;
1390 c->block_error.len = 4;
1391 c->block_count.len = 4;
1392
1393 /* Signal is always available */
1394 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1395 c->strength.stat[0].uvalue = 0;
1396
1397 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1398 for (i = 0; i < 4; i++) {
1399 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1400 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1401 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1402 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1403 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1404 }
1405}
1406
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001407static int mb86a20s_get_stats(struct dvb_frontend *fe)
1408{
1409 struct mb86a20s_state *state = fe->demodulator_priv;
1410 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1411 int rc = 0, i;
1412 u32 bit_error = 0, bit_count = 0;
1413 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001414 u32 block_error = 0, block_count = 0;
1415 u32 t_block_error = 0, t_block_count = 0;
1416 int active_layers = 0, ber_layers = 0, per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001417
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001418 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1419
1420 mb86a20s_get_main_CNR(fe);
1421
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001422 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001423 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001424
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001425 for (i = 0; i < 3; i++) {
1426 if (c->isdbt_layer_enabled & (1 << i)) {
1427 /* Layer is active and has rc segments */
1428 active_layers++;
1429
1430 /* Read per-layer BER */
1431 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001432 rc = mb86a20s_get_pre_ber(fe, i,
1433 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001434 if (rc >= 0) {
1435 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1436 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1437 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1438 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1439 } else if (rc != -EBUSY) {
1440 /*
1441 * If an I/O error happened,
1442 * measures are now unavailable
1443 */
1444 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1445 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1446 dev_err(&state->i2c->dev,
1447 "%s: Can't get BER for layer %c (error %d).\n",
1448 __func__, 'A' + i, rc);
1449 }
1450
1451 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1452 ber_layers++;
1453
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001454 /* Handle Block errors for PER/UCB reports */
1455 rc = mb86a20s_get_blk_error(fe, i,
1456 &block_error,
1457 &block_count);
1458 if (rc >= 0) {
1459 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1460 c->block_error.stat[1 + i].uvalue += block_error;
1461 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1462 c->block_count.stat[1 + i].uvalue += block_count;
1463 } else if (rc != -EBUSY) {
1464 /*
1465 * If an I/O error happened,
1466 * measures are now unavailable
1467 */
1468 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1469 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1470 dev_err(&state->i2c->dev,
1471 "%s: Can't get PER for layer %c (error %d).\n",
1472 __func__, 'A' + i, rc);
1473
1474 }
1475
1476 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1477 per_layers++;
1478
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001479 /* Update total BER */
1480 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1481 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001482
1483 /* Update total PER */
1484 t_block_error += c->block_error.stat[1 + i].uvalue;
1485 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001486 }
1487 }
1488
1489 /*
1490 * Start showing global count if at least one error count is
1491 * available.
1492 */
1493 if (ber_layers) {
1494 /*
1495 * At least one per-layer BER measure was read. We can now
1496 * calculate the total BER
1497 *
1498 * Total Bit Error/Count is calculated as the sum of the
1499 * bit errors on all active layers.
1500 */
1501 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1502 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1503 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1504 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
1505 }
1506
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001507 if (per_layers) {
1508 /*
1509 * At least one per-layer UCB measure was read. We can now
1510 * calculate the total UCB
1511 *
1512 * Total block Error/Count is calculated as the sum of the
1513 * block errors on all active layers.
1514 */
1515 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1516 c->block_error.stat[0].uvalue = t_block_error;
1517 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1518 c->block_count.stat[0].uvalue = t_block_count;
1519 }
1520
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001521 return rc;
1522}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001523
1524/*
1525 * The functions below are called via DVB callbacks, so they need to
1526 * properly use the I2C gate control
1527 */
1528
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001529static int mb86a20s_initfe(struct dvb_frontend *fe)
1530{
1531 struct mb86a20s_state *state = fe->demodulator_priv;
1532 int rc;
1533 u8 regD5 = 1;
1534
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001535 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001536
1537 if (fe->ops.i2c_gate_ctrl)
1538 fe->ops.i2c_gate_ctrl(fe, 0);
1539
1540 /* Initialize the frontend */
1541 rc = mb86a20s_writeregdata(state, mb86a20s_init);
1542 if (rc < 0)
1543 goto err;
1544
1545 if (!state->config->is_serial) {
1546 regD5 &= ~1;
1547
1548 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1549 if (rc < 0)
1550 goto err;
1551 rc = mb86a20s_writereg(state, 0x51, regD5);
1552 if (rc < 0)
1553 goto err;
1554 }
1555
1556err:
1557 if (fe->ops.i2c_gate_ctrl)
1558 fe->ops.i2c_gate_ctrl(fe, 1);
1559
1560 if (rc < 0) {
1561 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001562 dev_info(&state->i2c->dev,
1563 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001564 } else {
1565 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001566 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001567 }
1568 return rc;
1569}
1570
1571static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1572{
1573 struct mb86a20s_state *state = fe->demodulator_priv;
1574 int rc;
1575#if 0
1576 /*
1577 * FIXME: Properly implement the set frontend properties
1578 */
1579 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1580#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001581 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001582
1583 /*
1584 * Gate should already be opened, but it doesn't hurt to
1585 * double-check
1586 */
1587 if (fe->ops.i2c_gate_ctrl)
1588 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001589 fe->ops.tuner_ops.set_params(fe);
1590
1591 /*
1592 * Make it more reliable: if, for some reason, the initial
1593 * device initialization doesn't happen, initialize it when
1594 * a SBTVD parameters are adjusted.
1595 *
1596 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1597 * the agc callback logic is not called during DVB attach time,
1598 * causing mb86a20s to not be initialized with Kworld SBTVD.
1599 * So, this hack is needed, in order to make Kworld SBTVD to work.
1600 */
1601 if (state->need_init)
1602 mb86a20s_initfe(fe);
1603
1604 if (fe->ops.i2c_gate_ctrl)
1605 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001606
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001607 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001608 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001609
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001610 if (fe->ops.i2c_gate_ctrl)
1611 fe->ops.i2c_gate_ctrl(fe, 1);
1612
1613 return rc;
1614}
1615
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001616static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1617 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001618{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001619 struct mb86a20s_state *state = fe->demodulator_priv;
1620 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1621 int rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001622
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001623 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001624
1625 if (fe->ops.i2c_gate_ctrl)
1626 fe->ops.i2c_gate_ctrl(fe, 0);
1627
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001628 /* Get lock */
1629 rc = mb86a20s_read_status(fe, status);
1630 if (!(*status & FE_HAS_LOCK)) {
1631 mb86a20s_stats_not_ready(fe);
1632 mb86a20s_reset_frontend_cache(fe);
1633 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001634 if (rc < 0) {
1635 dev_err(&state->i2c->dev,
1636 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001637 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001638 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001639
1640 /* Get signal strength */
1641 rc = mb86a20s_read_signal_strength(fe);
1642 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001643 dev_err(&state->i2c->dev,
1644 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001645 mb86a20s_stats_not_ready(fe);
1646 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001647
1648 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001649 goto error;
1650 }
1651 /* Fill signal strength */
1652 c->strength.stat[0].uvalue = rc;
1653
1654 if (*status & FE_HAS_LOCK) {
1655 /* Get TMCC info*/
1656 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001657 if (rc < 0) {
1658 dev_err(&state->i2c->dev,
1659 "%s: Can't get FE TMCC data.\n", __func__);
1660 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001661 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001662 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001663
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001664 /* Get statistics */
1665 rc = mb86a20s_get_stats(fe);
1666 if (rc < 0 && rc != -EBUSY) {
1667 dev_err(&state->i2c->dev,
1668 "%s: Can't get FE statistics.\n", __func__);
1669 rc = 0;
1670 goto error;
1671 }
1672 rc = 0; /* Don't return EBUSY to userspace */
1673 }
1674 goto ok;
1675
1676error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001677 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001678
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001679ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001680 if (fe->ops.i2c_gate_ctrl)
1681 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001682
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001683 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001684}
1685
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001686static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
1687 u16 *strength)
1688{
1689 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1690
1691
1692 *strength = c->strength.stat[0].uvalue;
1693
1694 return 0;
1695}
1696
1697static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
1698{
1699 /*
1700 * get_frontend is now handled together with other stats
1701 * retrival, when read_status() is called, as some statistics
1702 * will depend on the layers detection.
1703 */
1704 return 0;
1705};
1706
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001707static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001708 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001709 unsigned int mode_flags,
1710 unsigned int *delay,
1711 fe_status_t *status)
1712{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001713 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001714 int rc = 0;
1715
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001716 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001717
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001718 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001719 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001720
1721 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001722 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001723
1724 return rc;
1725}
1726
1727static void mb86a20s_release(struct dvb_frontend *fe)
1728{
1729 struct mb86a20s_state *state = fe->demodulator_priv;
1730
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001731 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001732
1733 kfree(state);
1734}
1735
1736static struct dvb_frontend_ops mb86a20s_ops;
1737
1738struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
1739 struct i2c_adapter *i2c)
1740{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001741 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001742 u8 rev;
1743
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001744 dev_dbg(&i2c->dev, "%s called.\n", __func__);
1745
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001746 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001747 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001748 if (state == NULL) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001749 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001750 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001751 goto error;
1752 }
1753
1754 /* setup the state */
1755 state->config = config;
1756 state->i2c = i2c;
1757
1758 /* create dvb_frontend */
1759 memcpy(&state->frontend.ops, &mb86a20s_ops,
1760 sizeof(struct dvb_frontend_ops));
1761 state->frontend.demodulator_priv = state;
1762
1763 /* Check if it is a mb86a20s frontend */
1764 rev = mb86a20s_readreg(state, 0);
1765
1766 if (rev == 0x13) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001767 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001768 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001769 } else {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001770 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001771 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001772 rev);
1773 goto error;
1774 }
1775
1776 return &state->frontend;
1777
1778error:
1779 kfree(state);
1780 return NULL;
1781}
1782EXPORT_SYMBOL(mb86a20s_attach);
1783
1784static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001785 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001786 /* Use dib8000 values per default */
1787 .info = {
1788 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001789 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
1790 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1791 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1792 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1793 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
1794 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
1795 /* Actually, those values depend on the used tuner */
1796 .frequency_min = 45000000,
1797 .frequency_max = 864000000,
1798 .frequency_stepsize = 62500,
1799 },
1800
1801 .release = mb86a20s_release,
1802
1803 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001804 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001805 .get_frontend = mb86a20s_get_frontend_dummy,
1806 .read_status = mb86a20s_read_status_and_stats,
1807 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001808 .tune = mb86a20s_tune,
1809};
1810
1811MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
1812MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1813MODULE_LICENSE("GPL");