blob: 7d4e9119632dd1d76f2cfb0cebcb2fb857ba48c1 [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;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -0300937 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300938 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;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001010 if (collect_rate < 32)
1011 collect_rate = 32;
1012 if (collect_rate > 65535)
1013 collect_rate = 65535;
1014
1015 if (collect_rate != *count) {
1016 dev_dbg(&state->i2c->dev,
1017 "%s: updating PER counter on layer %c to %d.\n",
1018 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001019
1020 /* Stop PER measurement */
1021 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1022 if (rc < 0)
1023 return rc;
1024 rc = mb86a20s_writereg(state, 0x51, 0x00);
1025 if (rc < 0)
1026 return rc;
1027
1028 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001029 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1030 if (rc < 0)
1031 return rc;
1032 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1033 if (rc < 0)
1034 return rc;
1035 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1036 if (rc < 0)
1037 return rc;
1038 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1039 if (rc < 0)
1040 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001041
1042 /* start PER measurement */
1043 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1044 if (rc < 0)
1045 return rc;
1046 rc = mb86a20s_writereg(state, 0x51, 0x07);
1047 if (rc < 0)
1048 return rc;
1049
1050 /* Reset all counters to collect new data */
1051 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1052 if (rc < 0)
1053 return rc;
1054 rc = mb86a20s_writereg(state, 0x51, 0x07);
1055 if (rc < 0)
1056 return rc;
1057 rc = mb86a20s_writereg(state, 0x51, 0x00);
1058
1059 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001060 }
1061
1062reset_measurement:
1063 /* Reset counter to collect new data */
1064 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1065 if (rc < 0)
1066 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001067 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001068 if (rc < 0)
1069 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001070 val = rc;
1071 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001072 if (rc < 0)
1073 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001074 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001075
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001076 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001077}
1078
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001079struct linear_segments {
1080 unsigned x, y;
1081};
1082
1083/*
1084 * All tables below return a dB/1000 measurement
1085 */
1086
1087static struct linear_segments cnr_to_db_table[] = {
1088 { 19648, 0},
1089 { 18187, 1000},
1090 { 16534, 2000},
1091 { 14823, 3000},
1092 { 13161, 4000},
1093 { 11622, 5000},
1094 { 10279, 6000},
1095 { 9089, 7000},
1096 { 8042, 8000},
1097 { 7137, 9000},
1098 { 6342, 10000},
1099 { 5641, 11000},
1100 { 5030, 12000},
1101 { 4474, 13000},
1102 { 3988, 14000},
1103 { 3556, 15000},
1104 { 3180, 16000},
1105 { 2841, 17000},
1106 { 2541, 18000},
1107 { 2276, 19000},
1108 { 2038, 20000},
1109 { 1800, 21000},
1110 { 1625, 22000},
1111 { 1462, 23000},
1112 { 1324, 24000},
1113 { 1175, 25000},
1114 { 1063, 26000},
1115 { 980, 27000},
1116 { 907, 28000},
1117 { 840, 29000},
1118 { 788, 30000},
1119};
1120
1121static struct linear_segments cnr_64qam_table[] = {
1122 { 3922688, 0},
1123 { 3920384, 1000},
1124 { 3902720, 2000},
1125 { 3894784, 3000},
1126 { 3882496, 4000},
1127 { 3872768, 5000},
1128 { 3858944, 6000},
1129 { 3851520, 7000},
1130 { 3838976, 8000},
1131 { 3829248, 9000},
1132 { 3818240, 10000},
1133 { 3806976, 11000},
1134 { 3791872, 12000},
1135 { 3767040, 13000},
1136 { 3720960, 14000},
1137 { 3637504, 15000},
1138 { 3498496, 16000},
1139 { 3296000, 17000},
1140 { 3031040, 18000},
1141 { 2715392, 19000},
1142 { 2362624, 20000},
1143 { 1963264, 21000},
1144 { 1649664, 22000},
1145 { 1366784, 23000},
1146 { 1120768, 24000},
1147 { 890880, 25000},
1148 { 723456, 26000},
1149 { 612096, 27000},
1150 { 518912, 28000},
1151 { 448256, 29000},
1152 { 388864, 30000},
1153};
1154
1155static struct linear_segments cnr_16qam_table[] = {
1156 { 5314816, 0},
1157 { 5219072, 1000},
1158 { 5118720, 2000},
1159 { 4998912, 3000},
1160 { 4875520, 4000},
1161 { 4736000, 5000},
1162 { 4604160, 6000},
1163 { 4458752, 7000},
1164 { 4300288, 8000},
1165 { 4092928, 9000},
1166 { 3836160, 10000},
1167 { 3521024, 11000},
1168 { 3155968, 12000},
1169 { 2756864, 13000},
1170 { 2347008, 14000},
1171 { 1955072, 15000},
1172 { 1593600, 16000},
1173 { 1297920, 17000},
1174 { 1043968, 18000},
1175 { 839680, 19000},
1176 { 672256, 20000},
1177 { 523008, 21000},
1178 { 424704, 22000},
1179 { 345088, 23000},
1180 { 280064, 24000},
1181 { 221440, 25000},
1182 { 179712, 26000},
1183 { 151040, 27000},
1184 { 128512, 28000},
1185 { 110080, 29000},
1186 { 95744, 30000},
1187};
1188
1189struct linear_segments cnr_qpsk_table[] = {
1190 { 2834176, 0},
1191 { 2683648, 1000},
1192 { 2536960, 2000},
1193 { 2391808, 3000},
1194 { 2133248, 4000},
1195 { 1906176, 5000},
1196 { 1666560, 6000},
1197 { 1422080, 7000},
1198 { 1189632, 8000},
1199 { 976384, 9000},
1200 { 790272, 10000},
1201 { 633344, 11000},
1202 { 505600, 12000},
1203 { 402944, 13000},
1204 { 320768, 14000},
1205 { 255488, 15000},
1206 { 204032, 16000},
1207 { 163072, 17000},
1208 { 130304, 18000},
1209 { 105216, 19000},
1210 { 83456, 20000},
1211 { 65024, 21000},
1212 { 52480, 22000},
1213 { 42752, 23000},
1214 { 34560, 24000},
1215 { 27136, 25000},
1216 { 22016, 26000},
1217 { 18432, 27000},
1218 { 15616, 28000},
1219 { 13312, 29000},
1220 { 11520, 30000},
1221};
1222
1223static u32 interpolate_value(u32 value, struct linear_segments *segments,
1224 unsigned len)
1225{
1226 u64 tmp64;
1227 u32 dx, dy;
1228 int i, ret;
1229
1230 if (value >= segments[0].x)
1231 return segments[0].y;
1232 if (value < segments[len-1].x)
1233 return segments[len-1].y;
1234
1235 for (i = 1; i < len - 1; i++) {
1236 /* If value is identical, no need to interpolate */
1237 if (value == segments[i].x)
1238 return segments[i].y;
1239 if (value > segments[i].x)
1240 break;
1241 }
1242
1243 /* Linear interpolation between the two (x,y) points */
1244 dy = segments[i].y - segments[i - 1].y;
1245 dx = segments[i - 1].x - segments[i].x;
1246 tmp64 = value - segments[i].x;
1247 tmp64 *= dy;
1248 do_div(tmp64, dx);
1249 ret = segments[i].y - tmp64;
1250
1251 return ret;
1252}
1253
1254static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1255{
1256 struct mb86a20s_state *state = fe->demodulator_priv;
1257 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1258 u32 cnr_linear, cnr;
1259 int rc, val;
1260
1261 /* Check if CNR is available */
1262 rc = mb86a20s_readreg(state, 0x45);
1263 if (rc < 0)
1264 return rc;
1265
1266 if (!(rc & 0x40)) {
1267 dev_info(&state->i2c->dev, "%s: CNR is not available yet.\n",
1268 __func__);
1269 return -EBUSY;
1270 }
1271 val = rc;
1272
1273 rc = mb86a20s_readreg(state, 0x46);
1274 if (rc < 0)
1275 return rc;
1276 cnr_linear = rc << 8;
1277
1278 rc = mb86a20s_readreg(state, 0x46);
1279 if (rc < 0)
1280 return rc;
1281 cnr_linear |= rc;
1282
1283 cnr = interpolate_value(cnr_linear,
1284 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1285
1286 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1287 c->cnr.stat[0].svalue = cnr;
1288
1289 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1290 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1291
1292 /* CNR counter reset */
1293 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1294 if (rc < 0)
1295 return rc;
1296 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1297
1298 return rc;
1299}
1300
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001301static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001302{
1303 struct mb86a20s_state *state = fe->demodulator_priv;
1304 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1305 u32 mer, cnr;
1306 int rc, val, i;
1307 struct linear_segments *segs;
1308 unsigned segs_len;
1309
1310 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1311
1312 /* Check if the measures are already available */
1313 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1314 if (rc < 0)
1315 return rc;
1316 rc = mb86a20s_readreg(state, 0x51);
1317 if (rc < 0)
1318 return rc;
1319
1320 /* Check if data is available */
1321 if (!(rc & 0x01)) {
1322 dev_info(&state->i2c->dev,
1323 "%s: MER measures aren't available yet.\n", __func__);
1324 return -EBUSY;
1325 }
1326
1327 /* Read all layers */
1328 for (i = 0; i < 3; i++) {
1329 if (!(c->isdbt_layer_enabled & (1 << i))) {
1330 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1331 continue;
1332 }
1333
1334 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1335 if (rc < 0)
1336 return rc;
1337 rc = mb86a20s_readreg(state, 0x51);
1338 if (rc < 0)
1339 return rc;
1340 mer = rc << 16;
1341 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1342 if (rc < 0)
1343 return rc;
1344 rc = mb86a20s_readreg(state, 0x51);
1345 if (rc < 0)
1346 return rc;
1347 mer |= rc << 8;
1348 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1349 if (rc < 0)
1350 return rc;
1351 rc = mb86a20s_readreg(state, 0x51);
1352 if (rc < 0)
1353 return rc;
1354 mer |= rc;
1355
1356 switch (c->layer[i].modulation) {
1357 case DQPSK:
1358 case QPSK:
1359 segs = cnr_qpsk_table;
1360 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1361 break;
1362 case QAM_16:
1363 segs = cnr_16qam_table;
1364 segs_len = ARRAY_SIZE(cnr_16qam_table);
1365 break;
1366 default:
1367 case QAM_64:
1368 segs = cnr_64qam_table;
1369 segs_len = ARRAY_SIZE(cnr_64qam_table);
1370 break;
1371 }
1372 cnr = interpolate_value(mer, segs, segs_len);
1373
1374 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1375 c->cnr.stat[1 + i].svalue = cnr;
1376
1377 dev_dbg(&state->i2c->dev,
1378 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1379 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1380
1381 }
1382
1383 /* Start a new MER measurement */
1384 /* MER counter reset */
1385 rc = mb86a20s_writereg(state, 0x50, 0x50);
1386 if (rc < 0)
1387 return rc;
1388 rc = mb86a20s_readreg(state, 0x51);
1389 if (rc < 0)
1390 return rc;
1391 val = rc;
1392
1393 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1394 if (rc < 0)
1395 return rc;
1396 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1397 if (rc < 0)
1398 return rc;
1399
1400 return 0;
1401}
1402
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001403static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1404{
1405 struct mb86a20s_state *state = fe->demodulator_priv;
1406 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1407 int i;
1408
1409 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1410
1411 /* Fill the length of each status counter */
1412
1413 /* Only global stats */
1414 c->strength.len = 1;
1415
1416 /* Per-layer stats - 3 layers + global */
1417 c->cnr.len = 4;
1418 c->pre_bit_error.len = 4;
1419 c->pre_bit_count.len = 4;
1420 c->block_error.len = 4;
1421 c->block_count.len = 4;
1422
1423 /* Signal is always available */
1424 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1425 c->strength.stat[0].uvalue = 0;
1426
1427 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1428 for (i = 0; i < 4; i++) {
1429 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1430 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1431 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1432 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1433 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1434 }
1435}
1436
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001437static int mb86a20s_get_stats(struct dvb_frontend *fe)
1438{
1439 struct mb86a20s_state *state = fe->demodulator_priv;
1440 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1441 int rc = 0, i;
1442 u32 bit_error = 0, bit_count = 0;
1443 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001444 u32 block_error = 0, block_count = 0;
1445 u32 t_block_error = 0, t_block_count = 0;
1446 int active_layers = 0, ber_layers = 0, per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001447
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001448 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1449
1450 mb86a20s_get_main_CNR(fe);
1451
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001452 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001453 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001454
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001455 for (i = 0; i < 3; i++) {
1456 if (c->isdbt_layer_enabled & (1 << i)) {
1457 /* Layer is active and has rc segments */
1458 active_layers++;
1459
1460 /* Read per-layer BER */
1461 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001462 rc = mb86a20s_get_pre_ber(fe, i,
1463 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001464 if (rc >= 0) {
1465 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1466 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1467 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1468 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1469 } else if (rc != -EBUSY) {
1470 /*
1471 * If an I/O error happened,
1472 * measures are now unavailable
1473 */
1474 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1475 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1476 dev_err(&state->i2c->dev,
1477 "%s: Can't get BER for layer %c (error %d).\n",
1478 __func__, 'A' + i, rc);
1479 }
1480
1481 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1482 ber_layers++;
1483
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001484 /* Handle Block errors for PER/UCB reports */
1485 rc = mb86a20s_get_blk_error(fe, i,
1486 &block_error,
1487 &block_count);
1488 if (rc >= 0) {
1489 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1490 c->block_error.stat[1 + i].uvalue += block_error;
1491 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1492 c->block_count.stat[1 + i].uvalue += block_count;
1493 } else if (rc != -EBUSY) {
1494 /*
1495 * If an I/O error happened,
1496 * measures are now unavailable
1497 */
1498 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1499 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1500 dev_err(&state->i2c->dev,
1501 "%s: Can't get PER for layer %c (error %d).\n",
1502 __func__, 'A' + i, rc);
1503
1504 }
1505
1506 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1507 per_layers++;
1508
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001509 /* Update total BER */
1510 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1511 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001512
1513 /* Update total PER */
1514 t_block_error += c->block_error.stat[1 + i].uvalue;
1515 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001516 }
1517 }
1518
1519 /*
1520 * Start showing global count if at least one error count is
1521 * available.
1522 */
1523 if (ber_layers) {
1524 /*
1525 * At least one per-layer BER measure was read. We can now
1526 * calculate the total BER
1527 *
1528 * Total Bit Error/Count is calculated as the sum of the
1529 * bit errors on all active layers.
1530 */
1531 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1532 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1533 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1534 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
1535 }
1536
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001537 if (per_layers) {
1538 /*
1539 * At least one per-layer UCB measure was read. We can now
1540 * calculate the total UCB
1541 *
1542 * Total block Error/Count is calculated as the sum of the
1543 * block errors on all active layers.
1544 */
1545 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1546 c->block_error.stat[0].uvalue = t_block_error;
1547 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1548 c->block_count.stat[0].uvalue = t_block_count;
1549 }
1550
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001551 return rc;
1552}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001553
1554/*
1555 * The functions below are called via DVB callbacks, so they need to
1556 * properly use the I2C gate control
1557 */
1558
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001559static int mb86a20s_initfe(struct dvb_frontend *fe)
1560{
1561 struct mb86a20s_state *state = fe->demodulator_priv;
1562 int rc;
1563 u8 regD5 = 1;
1564
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001565 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001566
1567 if (fe->ops.i2c_gate_ctrl)
1568 fe->ops.i2c_gate_ctrl(fe, 0);
1569
1570 /* Initialize the frontend */
1571 rc = mb86a20s_writeregdata(state, mb86a20s_init);
1572 if (rc < 0)
1573 goto err;
1574
1575 if (!state->config->is_serial) {
1576 regD5 &= ~1;
1577
1578 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1579 if (rc < 0)
1580 goto err;
1581 rc = mb86a20s_writereg(state, 0x51, regD5);
1582 if (rc < 0)
1583 goto err;
1584 }
1585
1586err:
1587 if (fe->ops.i2c_gate_ctrl)
1588 fe->ops.i2c_gate_ctrl(fe, 1);
1589
1590 if (rc < 0) {
1591 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001592 dev_info(&state->i2c->dev,
1593 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001594 } else {
1595 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001596 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001597 }
1598 return rc;
1599}
1600
1601static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1602{
1603 struct mb86a20s_state *state = fe->demodulator_priv;
1604 int rc;
1605#if 0
1606 /*
1607 * FIXME: Properly implement the set frontend properties
1608 */
1609 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1610#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001611 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001612
1613 /*
1614 * Gate should already be opened, but it doesn't hurt to
1615 * double-check
1616 */
1617 if (fe->ops.i2c_gate_ctrl)
1618 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001619 fe->ops.tuner_ops.set_params(fe);
1620
1621 /*
1622 * Make it more reliable: if, for some reason, the initial
1623 * device initialization doesn't happen, initialize it when
1624 * a SBTVD parameters are adjusted.
1625 *
1626 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1627 * the agc callback logic is not called during DVB attach time,
1628 * causing mb86a20s to not be initialized with Kworld SBTVD.
1629 * So, this hack is needed, in order to make Kworld SBTVD to work.
1630 */
1631 if (state->need_init)
1632 mb86a20s_initfe(fe);
1633
1634 if (fe->ops.i2c_gate_ctrl)
1635 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001636
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001637 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001638 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001639
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001640 if (fe->ops.i2c_gate_ctrl)
1641 fe->ops.i2c_gate_ctrl(fe, 1);
1642
1643 return rc;
1644}
1645
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001646static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1647 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001648{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001649 struct mb86a20s_state *state = fe->demodulator_priv;
1650 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1651 int rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001652
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001653 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001654
1655 if (fe->ops.i2c_gate_ctrl)
1656 fe->ops.i2c_gate_ctrl(fe, 0);
1657
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001658 /* Get lock */
1659 rc = mb86a20s_read_status(fe, status);
1660 if (!(*status & FE_HAS_LOCK)) {
1661 mb86a20s_stats_not_ready(fe);
1662 mb86a20s_reset_frontend_cache(fe);
1663 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001664 if (rc < 0) {
1665 dev_err(&state->i2c->dev,
1666 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001667 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001668 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001669
1670 /* Get signal strength */
1671 rc = mb86a20s_read_signal_strength(fe);
1672 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001673 dev_err(&state->i2c->dev,
1674 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001675 mb86a20s_stats_not_ready(fe);
1676 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001677
1678 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001679 goto error;
1680 }
1681 /* Fill signal strength */
1682 c->strength.stat[0].uvalue = rc;
1683
1684 if (*status & FE_HAS_LOCK) {
1685 /* Get TMCC info*/
1686 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001687 if (rc < 0) {
1688 dev_err(&state->i2c->dev,
1689 "%s: Can't get FE TMCC data.\n", __func__);
1690 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001691 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001692 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001693
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001694 /* Get statistics */
1695 rc = mb86a20s_get_stats(fe);
1696 if (rc < 0 && rc != -EBUSY) {
1697 dev_err(&state->i2c->dev,
1698 "%s: Can't get FE statistics.\n", __func__);
1699 rc = 0;
1700 goto error;
1701 }
1702 rc = 0; /* Don't return EBUSY to userspace */
1703 }
1704 goto ok;
1705
1706error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001707 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001708
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001709ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001710 if (fe->ops.i2c_gate_ctrl)
1711 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001712
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001713 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001714}
1715
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001716static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
1717 u16 *strength)
1718{
1719 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1720
1721
1722 *strength = c->strength.stat[0].uvalue;
1723
1724 return 0;
1725}
1726
1727static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
1728{
1729 /*
1730 * get_frontend is now handled together with other stats
1731 * retrival, when read_status() is called, as some statistics
1732 * will depend on the layers detection.
1733 */
1734 return 0;
1735};
1736
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001737static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001738 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001739 unsigned int mode_flags,
1740 unsigned int *delay,
1741 fe_status_t *status)
1742{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001743 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001744 int rc = 0;
1745
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001746 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001747
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001748 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001749 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001750
1751 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001752 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001753
1754 return rc;
1755}
1756
1757static void mb86a20s_release(struct dvb_frontend *fe)
1758{
1759 struct mb86a20s_state *state = fe->demodulator_priv;
1760
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001761 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001762
1763 kfree(state);
1764}
1765
1766static struct dvb_frontend_ops mb86a20s_ops;
1767
1768struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
1769 struct i2c_adapter *i2c)
1770{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001771 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001772 u8 rev;
1773
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001774 dev_dbg(&i2c->dev, "%s called.\n", __func__);
1775
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001776 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001777 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001778 if (state == NULL) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001779 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001780 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001781 goto error;
1782 }
1783
1784 /* setup the state */
1785 state->config = config;
1786 state->i2c = i2c;
1787
1788 /* create dvb_frontend */
1789 memcpy(&state->frontend.ops, &mb86a20s_ops,
1790 sizeof(struct dvb_frontend_ops));
1791 state->frontend.demodulator_priv = state;
1792
1793 /* Check if it is a mb86a20s frontend */
1794 rev = mb86a20s_readreg(state, 0);
1795
1796 if (rev == 0x13) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001797 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001798 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001799 } else {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02001800 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001801 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001802 rev);
1803 goto error;
1804 }
1805
1806 return &state->frontend;
1807
1808error:
1809 kfree(state);
1810 return NULL;
1811}
1812EXPORT_SYMBOL(mb86a20s_attach);
1813
1814static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001815 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001816 /* Use dib8000 values per default */
1817 .info = {
1818 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001819 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
1820 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1821 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1822 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1823 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
1824 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
1825 /* Actually, those values depend on the used tuner */
1826 .frequency_min = 45000000,
1827 .frequency_max = 864000000,
1828 .frequency_stepsize = 62500,
1829 },
1830
1831 .release = mb86a20s_release,
1832
1833 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001834 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001835 .get_frontend = mb86a20s_get_frontend_dummy,
1836 .read_status = mb86a20s_read_status_and_stats,
1837 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001838 .tune = mb86a20s_tune,
1839};
1840
1841MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
1842MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1843MODULE_LICENSE("GPL");