blob: 0fbfae23d9040b8a8e9cc55e2fc7e8d95204d2c2 [file] [log] [blame]
Mauro Carvalho Chehabb9ede79a2010-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 Chehabb9ede79a2010-09-27 20:52:43 -03005 * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
6 *
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <asm/div64.h>
19
20#include "dvb_frontend.h"
21#include "mb86a20s.h"
22
23static int debug = 1;
24module_param(debug, int, 0644);
25MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
26
Mauro Carvalho Chehabb9ede79a2010-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 Chehabb9ede79a2010-09-27 20:52:43 -030031
32 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030033
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030034 u32 if_freq;
35
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030036 u32 estimated_rate[3];
37
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030038 bool need_init;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030039};
40
41struct regdata {
42 u8 reg;
43 u8 data;
44};
45
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030046#define BER_SAMPLING_RATE 1 /* Seconds */
47
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030048/*
49 * Initialization sequence: Use whatevere default values that PV SBTVD
50 * does on its initialisation, obtained via USB snoop
51 */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030052static struct regdata mb86a20s_init1[] = {
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030053 { 0x70, 0x0f },
54 { 0x70, 0xff },
55 { 0x08, 0x01 },
56 { 0x09, 0x3e },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020057 { 0x50, 0xd1 }, { 0x51, 0x22 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030058 { 0x39, 0x01 },
59 { 0x71, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020060 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030061};
62
63static struct regdata mb86a20s_init2[] = {
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020064 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030065 { 0x3b, 0x21 },
66 { 0x3c, 0x3a },
67 { 0x01, 0x0d },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020068 { 0x04, 0x08 }, { 0x05, 0x05 },
69 { 0x04, 0x0e }, { 0x05, 0x00 },
70 { 0x04, 0x0f }, { 0x05, 0x14 },
71 { 0x04, 0x0b }, { 0x05, 0x8c },
72 { 0x04, 0x00 }, { 0x05, 0x00 },
73 { 0x04, 0x01 }, { 0x05, 0x07 },
74 { 0x04, 0x02 }, { 0x05, 0x0f },
75 { 0x04, 0x03 }, { 0x05, 0xa0 },
76 { 0x04, 0x09 }, { 0x05, 0x00 },
77 { 0x04, 0x0a }, { 0x05, 0xff },
78 { 0x04, 0x27 }, { 0x05, 0x64 },
79 { 0x04, 0x28 }, { 0x05, 0x00 },
80 { 0x04, 0x1e }, { 0x05, 0xff },
81 { 0x04, 0x29 }, { 0x05, 0x0a },
82 { 0x04, 0x32 }, { 0x05, 0x0a },
83 { 0x04, 0x14 }, { 0x05, 0x02 },
84 { 0x04, 0x04 }, { 0x05, 0x00 },
85 { 0x04, 0x05 }, { 0x05, 0x22 },
86 { 0x04, 0x06 }, { 0x05, 0x0e },
87 { 0x04, 0x07 }, { 0x05, 0xd8 },
88 { 0x04, 0x12 }, { 0x05, 0x00 },
89 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020090 { 0x04, 0x15 }, { 0x05, 0x4e },
91 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030092
93 /*
94 * On this demod, when the bit count reaches the count below,
95 * it collects the bit error count. The bit counters are initialized
96 * to 65535 here. This warrants that all of them will be quickly
97 * calculated when device gets locked. As TMCC is parsed, the values
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030098 * will be adjusted later in the driver's code.
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030099 */
100 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
101 { 0x50, 0xa7 }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200102 { 0x50, 0xa8 }, { 0x51, 0xff },
103 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300104 { 0x50, 0xaa }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200105 { 0x50, 0xab }, { 0x51, 0xff },
106 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300107 { 0x50, 0xad }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200108 { 0x50, 0xae }, { 0x51, 0xff },
109 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300110
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300111 /*
112 * On this demod, post BER counts blocks. When the count reaches the
113 * value below, it collects the block error count. The block counters
114 * are initialized to 127 here. This warrants that all of them will be
115 * quickly calculated when device gets locked. As TMCC is parsed, the
116 * values will be adjusted later in the driver's code.
117 */
118 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
119 { 0x50, 0xdc }, { 0x51, 0x00 },
120 { 0x50, 0xdd }, { 0x51, 0x7f },
121 { 0x50, 0xde }, { 0x51, 0x00 },
122 { 0x50, 0xdf }, { 0x51, 0x7f },
123 { 0x50, 0xe0 }, { 0x51, 0x00 },
124 { 0x50, 0xe1 }, { 0x51, 0x7f },
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300125
126 /*
127 * On this demod, when the block count reaches the count below,
128 * it collects the block error count. The block counters are initialized
129 * to 127 here. This warrants that all of them will be quickly
130 * calculated when device gets locked. As TMCC is parsed, the values
131 * will be adjusted later in the driver's code.
132 */
133 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
134 { 0x50, 0xb2 }, { 0x51, 0x00 },
135 { 0x50, 0xb3 }, { 0x51, 0x7f },
136 { 0x50, 0xb4 }, { 0x51, 0x00 },
137 { 0x50, 0xb5 }, { 0x51, 0x7f },
138 { 0x50, 0xb6 }, { 0x51, 0x00 },
139 { 0x50, 0xb7 }, { 0x51, 0x7f },
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300140
141 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300142 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
143 { 0x45, 0x04 }, /* CN symbol 4 */
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300144 { 0x48, 0x04 }, /* CN manual mode */
145
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200146 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
147 { 0x50, 0xd6 }, { 0x51, 0x1f },
148 { 0x50, 0xd2 }, { 0x51, 0x03 },
149 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200150 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
151 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300152
153 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200154 { 0x28, 0x00 }, { 0x29, 0x10 },
155 { 0x28, 0x05 }, { 0x29, 0x02 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300156 { 0x1c, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200157 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
158 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
159 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
160 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
161 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
162 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
163 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
164 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
165 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
166 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
167 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
168 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
169 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
170 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
171 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
172 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
173 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
174 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
175 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
176 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
177 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
178 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
179 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
180 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
181 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
182 { 0x50, 0x1e }, { 0x51, 0x5d },
183 { 0x50, 0x22 }, { 0x51, 0x00 },
184 { 0x50, 0x23 }, { 0x51, 0xc8 },
185 { 0x50, 0x24 }, { 0x51, 0x00 },
186 { 0x50, 0x25 }, { 0x51, 0xf0 },
187 { 0x50, 0x26 }, { 0x51, 0x00 },
188 { 0x50, 0x27 }, { 0x51, 0xc3 },
189 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200190 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300191 { 0xd0, 0x00 },
192};
193
194static struct regdata mb86a20s_reset_reception[] = {
195 { 0x70, 0xf0 },
196 { 0x70, 0xff },
197 { 0x08, 0x01 },
198 { 0x08, 0x00 },
199};
200
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300201static struct regdata mb86a20s_per_ber_reset[] = {
202 { 0x53, 0x00 }, /* pre BER Counter reset */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300203 { 0x53, 0x07 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300204
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300205 { 0x5f, 0x00 }, /* post BER Counter reset */
206 { 0x5f, 0x07 },
207
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300208 { 0x50, 0xb1 }, /* PER Counter reset */
209 { 0x51, 0x07 },
210 { 0x51, 0x00 },
211};
212
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200213/*
214 * I2C read/write functions and macros
215 */
216
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300217static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300218 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300219{
220 u8 buf[] = { reg, data };
221 struct i2c_msg msg = {
222 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
223 };
224 int rc;
225
226 rc = i2c_transfer(state->i2c, &msg, 1);
227 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200228 dev_err(&state->i2c->dev,
229 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
230 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300231 return rc;
232 }
233
234 return 0;
235}
236
237static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
238 u8 i2c_addr, struct regdata *rd, int size)
239{
240 int i, rc;
241
242 for (i = 0; i < size; i++) {
243 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
244 rd[i].data);
245 if (rc < 0)
246 return rc;
247 }
248 return 0;
249}
250
251static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
252 u8 i2c_addr, u8 reg)
253{
254 u8 val;
255 int rc;
256 struct i2c_msg msg[] = {
257 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
258 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
259 };
260
261 rc = i2c_transfer(state->i2c, msg, 2);
262
263 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200264 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
265 __func__, reg, rc);
266 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300267 }
268
269 return val;
270}
271
272#define mb86a20s_readreg(state, reg) \
273 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
274#define mb86a20s_writereg(state, reg, val) \
275 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
276#define mb86a20s_writeregdata(state, regdata) \
277 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
278 regdata, ARRAY_SIZE(regdata))
279
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300280/*
281 * Ancillary internal routines (likely compiled inlined)
282 *
283 * The functions below assume that gateway lock has already obtained
284 */
285
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200286static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300287{
288 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200289 int val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300290
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200291 *status = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300292
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200293 val = mb86a20s_readreg(state, 0x0a) & 0xf;
294 if (val < 0)
295 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300296
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200297 if (val >= 2)
298 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300299
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200300 if (val >= 4)
301 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300302
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200303 if (val >= 5)
304 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300305
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200306 if (val >= 7)
307 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300308
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200309 if (val >= 8) /* Maybe 9? */
310 *status |= FE_HAS_LOCK;
311
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200312 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
313 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200314
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -0300315 return val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300316}
317
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300318static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300319{
320 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300321 int rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300322 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300323
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300324 /* Does a binary search to get RF strength */
325 rf_max = 0xfff;
326 rf_min = 0;
327 do {
328 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300329 rc = mb86a20s_writereg(state, 0x04, 0x1f);
330 if (rc < 0)
331 return rc;
332 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
333 if (rc < 0)
334 return rc;
335 rc = mb86a20s_writereg(state, 0x04, 0x20);
336 if (rc < 0)
337 return rc;
Mauro Carvalho Chehabdad78c52013-03-01 16:15:16 -0300338 rc = mb86a20s_writereg(state, 0x05, rf);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300339 if (rc < 0)
340 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300341
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300342 rc = mb86a20s_readreg(state, 0x02);
343 if (rc < 0)
344 return rc;
345 if (rc & 0x08)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300346 rf_min = (rf_max + rf_min) / 2;
347 else
348 rf_max = (rf_max + rf_min) / 2;
349 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300350 rf = (rf_max + rf_min) / 2;
351
352 /* Rescale it from 2^12 (4096) to 2^16 */
353 rf <<= (16 - 12);
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200354 dev_dbg(&state->i2c->dev,
355 "%s: signal strength = %d (%d < RF=%d < %d)\n",
356 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300357 return rf;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300358 }
359 } while (1);
360
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300361 return 0;
362}
363
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200364static int mb86a20s_get_modulation(struct mb86a20s_state *state,
365 unsigned layer)
366{
367 int rc;
368 static unsigned char reg[] = {
369 [0] = 0x86, /* Layer A */
370 [1] = 0x8a, /* Layer B */
371 [2] = 0x8e, /* Layer C */
372 };
373
Dan Carpenter82033bc2012-01-13 02:28:34 -0300374 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200375 return -EINVAL;
376 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
377 if (rc < 0)
378 return rc;
379 rc = mb86a20s_readreg(state, 0x6e);
380 if (rc < 0)
381 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300382 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200383 case 0:
384 return DQPSK;
385 case 1:
386 return QPSK;
387 case 2:
388 return QAM_16;
389 case 3:
390 return QAM_64;
391 default:
392 return QAM_AUTO;
393 }
394}
395
396static int mb86a20s_get_fec(struct mb86a20s_state *state,
397 unsigned layer)
398{
399 int rc;
400
401 static unsigned char reg[] = {
402 [0] = 0x87, /* Layer A */
403 [1] = 0x8b, /* Layer B */
404 [2] = 0x8f, /* Layer C */
405 };
406
Dan Carpenter82033bc2012-01-13 02:28:34 -0300407 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200408 return -EINVAL;
409 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
410 if (rc < 0)
411 return rc;
412 rc = mb86a20s_readreg(state, 0x6e);
413 if (rc < 0)
414 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300415 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200416 case 0:
417 return FEC_1_2;
418 case 1:
419 return FEC_2_3;
420 case 2:
421 return FEC_3_4;
422 case 3:
423 return FEC_5_6;
424 case 4:
425 return FEC_7_8;
426 default:
427 return FEC_AUTO;
428 }
429}
430
431static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
432 unsigned layer)
433{
434 int rc;
435
436 static unsigned char reg[] = {
437 [0] = 0x88, /* Layer A */
438 [1] = 0x8c, /* Layer B */
439 [2] = 0x90, /* Layer C */
440 };
441
Dan Carpenter82033bc2012-01-13 02:28:34 -0300442 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200443 return -EINVAL;
444 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
445 if (rc < 0)
446 return rc;
447 rc = mb86a20s_readreg(state, 0x6e);
448 if (rc < 0)
449 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300450
451 switch ((rc >> 4) & 0x07) {
452 case 1:
453 return GUARD_INTERVAL_1_4;
454 case 2:
455 return GUARD_INTERVAL_1_8;
456 case 3:
457 return GUARD_INTERVAL_1_16;
458 case 4:
459 return GUARD_INTERVAL_1_32;
460
461 default:
462 case 0:
463 return GUARD_INTERVAL_AUTO;
464 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200465}
466
467static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
468 unsigned layer)
469{
470 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200471 static unsigned char reg[] = {
472 [0] = 0x89, /* Layer A */
473 [1] = 0x8d, /* Layer B */
474 [2] = 0x91, /* Layer C */
475 };
476
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200477 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
478
Dan Carpenter82033bc2012-01-13 02:28:34 -0300479 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200480 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200481
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200482 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
483 if (rc < 0)
484 return rc;
485 rc = mb86a20s_readreg(state, 0x6e);
486 if (rc < 0)
487 return rc;
488 count = (rc >> 4) & 0x0f;
489
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200490 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
491
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200492 return count;
493}
494
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300495static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
496{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200497 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300498 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
499
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200500 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
501
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300502 /* Fixed parameters */
503 c->delivery_system = SYS_ISDBT;
504 c->bandwidth_hz = 6000000;
505
506 /* Initialize values that will be later autodetected */
507 c->isdbt_layer_enabled = 0;
508 c->transmission_mode = TRANSMISSION_MODE_AUTO;
509 c->guard_interval = GUARD_INTERVAL_AUTO;
510 c->isdbt_sb_mode = 0;
511 c->isdbt_sb_segment_count = 0;
512}
513
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300514/*
515 * Estimates the bit rate using the per-segment bit rate given by
516 * ABNT/NBR 15601 spec (table 4).
517 */
518static u32 isdbt_rate[3][5][4] = {
519 { /* DQPSK/QPSK */
520 { 280850, 312060, 330420, 340430 }, /* 1/2 */
521 { 374470, 416080, 440560, 453910 }, /* 2/3 */
522 { 421280, 468090, 495630, 510650 }, /* 3/4 */
523 { 468090, 520100, 550700, 567390 }, /* 5/6 */
524 { 491500, 546110, 578230, 595760 }, /* 7/8 */
525 }, { /* QAM16 */
526 { 561710, 624130, 660840, 680870 }, /* 1/2 */
527 { 748950, 832170, 881120, 907820 }, /* 2/3 */
528 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
529 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
530 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
531 }, { /* QAM64 */
532 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
533 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
534 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
535 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
536 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
537 }
538};
539
540static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
541 u32 modulation, u32 fec, u32 interleaving,
542 u32 segment)
543{
544 struct mb86a20s_state *state = fe->demodulator_priv;
545 u32 rate;
546 int m, f, i;
547
548 /*
549 * If modulation/fec/interleaving is not detected, the default is
550 * to consider the lowest bit rate, to avoid taking too long time
551 * to get BER.
552 */
553 switch (modulation) {
554 case DQPSK:
555 case QPSK:
556 default:
557 m = 0;
558 break;
559 case QAM_16:
560 m = 1;
561 break;
562 case QAM_64:
563 m = 2;
564 break;
565 }
566
567 switch (fec) {
568 default:
569 case FEC_1_2:
570 case FEC_AUTO:
571 f = 0;
572 break;
573 case FEC_2_3:
574 f = 1;
575 break;
576 case FEC_3_4:
577 f = 2;
578 break;
579 case FEC_5_6:
580 f = 3;
581 break;
582 case FEC_7_8:
583 f = 4;
584 break;
585 }
586
587 switch (interleaving) {
588 default:
589 case GUARD_INTERVAL_1_4:
590 i = 0;
591 break;
592 case GUARD_INTERVAL_1_8:
593 i = 1;
594 break;
595 case GUARD_INTERVAL_1_16:
596 i = 2;
597 break;
598 case GUARD_INTERVAL_1_32:
599 i = 3;
600 break;
601 }
602
603 /* Samples BER at BER_SAMPLING_RATE seconds */
604 rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
605
606 /* Avoids sampling too quickly or to overflow the register */
607 if (rate < 256)
608 rate = 256;
609 else if (rate > (1 << 24) - 1)
610 rate = (1 << 24) - 1;
611
612 dev_dbg(&state->i2c->dev,
613 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
614 __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
615 rate, rate);
616
617 state->estimated_rate[i] = rate;
618}
619
620
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300621static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300622{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200623 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300624 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200625 int i, rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300626
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200627 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
628
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300629 /* Reset frontend cache to default values */
630 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200631
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200632 /* Check for partial reception */
633 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300634 if (rc < 0)
635 return rc;
636 rc = mb86a20s_readreg(state, 0x6e);
637 if (rc < 0)
638 return rc;
639 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200640
641 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300642
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200643 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200644 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
645 __func__, 'A' + i);
646
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200647 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300648 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200649 goto noperlayer_error;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300650 if (rc >= 0 && rc < 14) {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300651 c->layer[i].segment_count = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300652 } else {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300653 c->layer[i].segment_count = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300654 state->estimated_rate[i] = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200655 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300656 }
657 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200658 rc = mb86a20s_get_modulation(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: modulation %d.\n",
662 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300663 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200664 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300665 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200666 goto noperlayer_error;
667 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
668 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300669 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200670 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300671 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200672 goto noperlayer_error;
673 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
674 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300675 c->layer[i].interleaving = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300676 mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
677 c->layer[i].fec,
678 c->layer[i].interleaving,
679 c->layer[i].segment_count);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200680 }
681
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200682 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300683 if (rc < 0)
684 return rc;
685 if ((rc & 0x60) == 0x20) {
686 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200687 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300688 if (!c->isdbt_sb_segment_count)
689 c->isdbt_sb_segment_count = 1;
690 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200691
692 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200693 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300694 if (rc < 0)
695 return rc;
696 if ((rc & 0x60) == 0x20) {
697 switch (rc & 0x0c >> 2) {
698 case 0:
699 c->transmission_mode = TRANSMISSION_MODE_2K;
700 break;
701 case 1:
702 c->transmission_mode = TRANSMISSION_MODE_4K;
703 break;
704 case 2:
705 c->transmission_mode = TRANSMISSION_MODE_8K;
706 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200707 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300708 }
709 if (!(rc & 0x10)) {
710 switch (rc & 0x3) {
711 case 0:
712 c->guard_interval = GUARD_INTERVAL_1_4;
713 break;
714 case 1:
715 c->guard_interval = GUARD_INTERVAL_1_8;
716 break;
717 case 2:
718 c->guard_interval = GUARD_INTERVAL_1_16;
719 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200720 }
721 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300722 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200723
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200724noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300725
726 /* per-layer info is incomplete; discard all per-layer */
727 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300728
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300729 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300730}
731
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300732static int mb86a20s_reset_counters(struct dvb_frontend *fe)
733{
734 struct mb86a20s_state *state = fe->demodulator_priv;
735 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
736 int rc, val;
737
738 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
739
740 /* Reset the counters, if the channel changed */
741 if (state->last_frequency != c->frequency) {
742 memset(&c->strength, 0, sizeof(c->strength));
743 memset(&c->cnr, 0, sizeof(c->cnr));
744 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
745 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300746 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
747 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300748 memset(&c->block_error, 0, sizeof(c->block_error));
749 memset(&c->block_count, 0, sizeof(c->block_count));
750
751 state->last_frequency = c->frequency;
752 }
753
754 /* Clear status for most stats */
755
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300756 /* BER/PER counter reset */
757 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300758 if (rc < 0)
759 goto err;
760
761 /* CNR counter reset */
762 rc = mb86a20s_readreg(state, 0x45);
763 if (rc < 0)
764 goto err;
765 val = rc;
766 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
767 if (rc < 0)
768 goto err;
769 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
770 if (rc < 0)
771 goto err;
772
773 /* MER counter reset */
774 rc = mb86a20s_writereg(state, 0x50, 0x50);
775 if (rc < 0)
776 goto err;
777 rc = mb86a20s_readreg(state, 0x51);
778 if (rc < 0)
779 goto err;
780 val = rc;
781 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
782 if (rc < 0)
783 goto err;
784 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
785 if (rc < 0)
786 goto err;
787
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300788 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300789err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300790 dev_err(&state->i2c->dev,
791 "%s: Can't reset FE statistics (error %d).\n",
792 __func__, rc);
793ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300794 return rc;
795}
796
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300797static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
798 unsigned layer,
799 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300800{
801 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300802 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300803
804 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
805
806 if (layer >= 3)
807 return -EINVAL;
808
809 /* Check if the BER measures are already available */
810 rc = mb86a20s_readreg(state, 0x54);
811 if (rc < 0)
812 return rc;
813
814 /* Check if data is available for that layer */
815 if (!(rc & (1 << layer))) {
816 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300817 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300818 __func__, 'A' + layer);
819 return -EBUSY;
820 }
821
822 /* Read Bit Error Count */
823 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
824 if (rc < 0)
825 return rc;
826 *error = rc << 16;
827 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
828 if (rc < 0)
829 return rc;
830 *error |= rc << 8;
831 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
832 if (rc < 0)
833 return rc;
834 *error |= rc;
835
836 dev_dbg(&state->i2c->dev,
837 "%s: bit error before Viterbi for layer %c: %d.\n",
838 __func__, 'A' + layer, *error);
839
840 /* Read Bit Count */
841 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
842 if (rc < 0)
843 return rc;
844 rc = mb86a20s_readreg(state, 0x51);
845 if (rc < 0)
846 return rc;
847 *count = rc << 16;
848 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
849 if (rc < 0)
850 return rc;
851 rc = mb86a20s_readreg(state, 0x51);
852 if (rc < 0)
853 return rc;
854 *count |= rc << 8;
855 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
856 if (rc < 0)
857 return rc;
858 rc = mb86a20s_readreg(state, 0x51);
859 if (rc < 0)
860 return rc;
861 *count |= rc;
862
863 dev_dbg(&state->i2c->dev,
864 "%s: bit count before Viterbi for layer %c: %d.\n",
865 __func__, 'A' + layer, *count);
866
867
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300868 /*
869 * As we get TMCC data from the frontend, we can better estimate the
870 * BER bit counters, in order to do the BER measure during a longer
871 * time. Use those data, if available, to update the bit count
872 * measure.
873 */
874
875 if (state->estimated_rate[layer]
876 && state->estimated_rate[layer] != *count) {
877 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300878 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300879 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300880
881 /* Turn off BER before Viterbi */
882 rc = mb86a20s_writereg(state, 0x52, 0x00);
883
884 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300885 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
886 if (rc < 0)
887 return rc;
888 rc = mb86a20s_writereg(state, 0x51,
889 state->estimated_rate[layer] >> 16);
890 if (rc < 0)
891 return rc;
892 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
893 if (rc < 0)
894 return rc;
895 rc = mb86a20s_writereg(state, 0x51,
896 state->estimated_rate[layer] >> 8);
897 if (rc < 0)
898 return rc;
899 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
900 if (rc < 0)
901 return rc;
902 rc = mb86a20s_writereg(state, 0x51,
903 state->estimated_rate[layer]);
904 if (rc < 0)
905 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300906
907 /* Turn on BER before Viterbi */
908 rc = mb86a20s_writereg(state, 0x52, 0x01);
909
910 /* Reset all preBER counters */
911 rc = mb86a20s_writereg(state, 0x53, 0x00);
912 if (rc < 0)
913 return rc;
914 rc = mb86a20s_writereg(state, 0x53, 0x07);
915 } else {
916 /* Reset counter to collect new data */
917 rc = mb86a20s_readreg(state, 0x53);
918 if (rc < 0)
919 return rc;
920 val = rc;
921 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
922 if (rc < 0)
923 return rc;
924 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300925 }
926
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300927 return rc;
928}
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300929
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300930static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
931 unsigned layer,
932 u32 *error, u32 *count)
933{
934 struct mb86a20s_state *state = fe->demodulator_priv;
935 u32 counter, collect_rate;
936 int rc, val;
937
938 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
939
940 if (layer >= 3)
941 return -EINVAL;
942
943 /* Check if the BER measures are already available */
944 rc = mb86a20s_readreg(state, 0x60);
945 if (rc < 0)
946 return rc;
947
948 /* Check if data is available for that layer */
949 if (!(rc & (1 << layer))) {
950 dev_dbg(&state->i2c->dev,
951 "%s: post BER for layer %c is not available yet.\n",
952 __func__, 'A' + layer);
953 return -EBUSY;
954 }
955
956 /* Read Bit Error Count */
957 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
958 if (rc < 0)
959 return rc;
960 *error = rc << 16;
961 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
962 if (rc < 0)
963 return rc;
964 *error |= rc << 8;
965 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
966 if (rc < 0)
967 return rc;
968 *error |= rc;
969
970 dev_dbg(&state->i2c->dev,
971 "%s: post bit error for layer %c: %d.\n",
972 __func__, 'A' + layer, *error);
973
974 /* Read Bit Count */
975 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
976 if (rc < 0)
977 return rc;
978 rc = mb86a20s_readreg(state, 0x51);
979 if (rc < 0)
980 return rc;
981 counter = rc << 8;
982 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
983 if (rc < 0)
984 return rc;
985 rc = mb86a20s_readreg(state, 0x51);
986 if (rc < 0)
987 return rc;
988 counter |= rc;
989 *count = counter * 204 * 8;
990
991 dev_dbg(&state->i2c->dev,
992 "%s: post bit count for layer %c: %d.\n",
993 __func__, 'A' + layer, *count);
994
995 /*
996 * As we get TMCC data from the frontend, we can better estimate the
997 * BER bit counters, in order to do the BER measure during a longer
998 * time. Use those data, if available, to update the bit count
999 * measure.
1000 */
1001
1002 if (!state->estimated_rate[layer])
1003 goto reset_measurement;
1004
1005 collect_rate = state->estimated_rate[layer] / 204 / 8;
1006 if (collect_rate < 32)
1007 collect_rate = 32;
1008 if (collect_rate > 65535)
1009 collect_rate = 65535;
1010 if (collect_rate != counter) {
1011 dev_dbg(&state->i2c->dev,
1012 "%s: updating postBER counter on layer %c to %d.\n",
1013 __func__, 'A' + layer, collect_rate);
1014
1015 /* Turn off BER after Viterbi */
1016 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1017
1018 /* Update counter for this layer */
1019 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1020 if (rc < 0)
1021 return rc;
1022 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1023 if (rc < 0)
1024 return rc;
1025 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1026 if (rc < 0)
1027 return rc;
1028 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1029 if (rc < 0)
1030 return rc;
1031
1032 /* Turn on BER after Viterbi */
1033 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1034
1035 /* Reset all preBER counters */
1036 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1037 if (rc < 0)
1038 return rc;
1039 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1040
1041 return rc;
1042 }
1043
1044reset_measurement:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001045 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001046 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001047 if (rc < 0)
1048 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001049 val = rc;
1050 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1051 if (rc < 0)
1052 return rc;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001053 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001054
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001055 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001056}
1057
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001058static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1059 unsigned layer,
1060 u32 *error, u32 *count)
1061{
1062 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001063 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001064 u32 collect_rate;
1065 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1066
1067 if (layer >= 3)
1068 return -EINVAL;
1069
1070 /* Check if the PER measures are already available */
1071 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1072 if (rc < 0)
1073 return rc;
1074 rc = mb86a20s_readreg(state, 0x51);
1075 if (rc < 0)
1076 return rc;
1077
1078 /* Check if data is available for that layer */
1079
1080 if (!(rc & (1 << layer))) {
1081 dev_dbg(&state->i2c->dev,
1082 "%s: block counts for layer %c aren't available yet.\n",
1083 __func__, 'A' + layer);
1084 return -EBUSY;
1085 }
1086
1087 /* Read Packet error Count */
1088 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1089 if (rc < 0)
1090 return rc;
1091 rc = mb86a20s_readreg(state, 0x51);
1092 if (rc < 0)
1093 return rc;
1094 *error = rc << 8;
1095 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1096 if (rc < 0)
1097 return rc;
1098 rc = mb86a20s_readreg(state, 0x51);
1099 if (rc < 0)
1100 return rc;
1101 *error |= rc;
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001102 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001103 __func__, 'A' + layer, *error);
1104
1105 /* Read Bit Count */
1106 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1107 if (rc < 0)
1108 return rc;
1109 rc = mb86a20s_readreg(state, 0x51);
1110 if (rc < 0)
1111 return rc;
1112 *count = rc << 8;
1113 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1114 if (rc < 0)
1115 return rc;
1116 rc = mb86a20s_readreg(state, 0x51);
1117 if (rc < 0)
1118 return rc;
1119 *count |= rc;
1120
1121 dev_dbg(&state->i2c->dev,
1122 "%s: block count for layer %c: %d.\n",
1123 __func__, 'A' + layer, *count);
1124
1125 /*
1126 * As we get TMCC data from the frontend, we can better estimate the
1127 * BER bit counters, in order to do the BER measure during a longer
1128 * time. Use those data, if available, to update the bit count
1129 * measure.
1130 */
1131
1132 if (!state->estimated_rate[layer])
1133 goto reset_measurement;
1134
1135 collect_rate = state->estimated_rate[layer] / 204 / 8;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001136 if (collect_rate < 32)
1137 collect_rate = 32;
1138 if (collect_rate > 65535)
1139 collect_rate = 65535;
1140
1141 if (collect_rate != *count) {
1142 dev_dbg(&state->i2c->dev,
1143 "%s: updating PER counter on layer %c to %d.\n",
1144 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001145
1146 /* Stop PER measurement */
1147 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1148 if (rc < 0)
1149 return rc;
1150 rc = mb86a20s_writereg(state, 0x51, 0x00);
1151 if (rc < 0)
1152 return rc;
1153
1154 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001155 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1156 if (rc < 0)
1157 return rc;
1158 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1159 if (rc < 0)
1160 return rc;
1161 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1162 if (rc < 0)
1163 return rc;
1164 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1165 if (rc < 0)
1166 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001167
1168 /* start PER measurement */
1169 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1170 if (rc < 0)
1171 return rc;
1172 rc = mb86a20s_writereg(state, 0x51, 0x07);
1173 if (rc < 0)
1174 return rc;
1175
1176 /* Reset all counters to collect new data */
1177 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1178 if (rc < 0)
1179 return rc;
1180 rc = mb86a20s_writereg(state, 0x51, 0x07);
1181 if (rc < 0)
1182 return rc;
1183 rc = mb86a20s_writereg(state, 0x51, 0x00);
1184
1185 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001186 }
1187
1188reset_measurement:
1189 /* Reset counter to collect new data */
1190 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1191 if (rc < 0)
1192 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001193 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001194 if (rc < 0)
1195 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001196 val = rc;
1197 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001198 if (rc < 0)
1199 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001200 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001201
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001202 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001203}
1204
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001205struct linear_segments {
1206 unsigned x, y;
1207};
1208
1209/*
1210 * All tables below return a dB/1000 measurement
1211 */
1212
1213static struct linear_segments cnr_to_db_table[] = {
1214 { 19648, 0},
1215 { 18187, 1000},
1216 { 16534, 2000},
1217 { 14823, 3000},
1218 { 13161, 4000},
1219 { 11622, 5000},
1220 { 10279, 6000},
1221 { 9089, 7000},
1222 { 8042, 8000},
1223 { 7137, 9000},
1224 { 6342, 10000},
1225 { 5641, 11000},
1226 { 5030, 12000},
1227 { 4474, 13000},
1228 { 3988, 14000},
1229 { 3556, 15000},
1230 { 3180, 16000},
1231 { 2841, 17000},
1232 { 2541, 18000},
1233 { 2276, 19000},
1234 { 2038, 20000},
1235 { 1800, 21000},
1236 { 1625, 22000},
1237 { 1462, 23000},
1238 { 1324, 24000},
1239 { 1175, 25000},
1240 { 1063, 26000},
1241 { 980, 27000},
1242 { 907, 28000},
1243 { 840, 29000},
1244 { 788, 30000},
1245};
1246
1247static struct linear_segments cnr_64qam_table[] = {
1248 { 3922688, 0},
1249 { 3920384, 1000},
1250 { 3902720, 2000},
1251 { 3894784, 3000},
1252 { 3882496, 4000},
1253 { 3872768, 5000},
1254 { 3858944, 6000},
1255 { 3851520, 7000},
1256 { 3838976, 8000},
1257 { 3829248, 9000},
1258 { 3818240, 10000},
1259 { 3806976, 11000},
1260 { 3791872, 12000},
1261 { 3767040, 13000},
1262 { 3720960, 14000},
1263 { 3637504, 15000},
1264 { 3498496, 16000},
1265 { 3296000, 17000},
1266 { 3031040, 18000},
1267 { 2715392, 19000},
1268 { 2362624, 20000},
1269 { 1963264, 21000},
1270 { 1649664, 22000},
1271 { 1366784, 23000},
1272 { 1120768, 24000},
1273 { 890880, 25000},
1274 { 723456, 26000},
1275 { 612096, 27000},
1276 { 518912, 28000},
1277 { 448256, 29000},
1278 { 388864, 30000},
1279};
1280
1281static struct linear_segments cnr_16qam_table[] = {
1282 { 5314816, 0},
1283 { 5219072, 1000},
1284 { 5118720, 2000},
1285 { 4998912, 3000},
1286 { 4875520, 4000},
1287 { 4736000, 5000},
1288 { 4604160, 6000},
1289 { 4458752, 7000},
1290 { 4300288, 8000},
1291 { 4092928, 9000},
1292 { 3836160, 10000},
1293 { 3521024, 11000},
1294 { 3155968, 12000},
1295 { 2756864, 13000},
1296 { 2347008, 14000},
1297 { 1955072, 15000},
1298 { 1593600, 16000},
1299 { 1297920, 17000},
1300 { 1043968, 18000},
1301 { 839680, 19000},
1302 { 672256, 20000},
1303 { 523008, 21000},
1304 { 424704, 22000},
1305 { 345088, 23000},
1306 { 280064, 24000},
1307 { 221440, 25000},
1308 { 179712, 26000},
1309 { 151040, 27000},
1310 { 128512, 28000},
1311 { 110080, 29000},
1312 { 95744, 30000},
1313};
1314
1315struct linear_segments cnr_qpsk_table[] = {
1316 { 2834176, 0},
1317 { 2683648, 1000},
1318 { 2536960, 2000},
1319 { 2391808, 3000},
1320 { 2133248, 4000},
1321 { 1906176, 5000},
1322 { 1666560, 6000},
1323 { 1422080, 7000},
1324 { 1189632, 8000},
1325 { 976384, 9000},
1326 { 790272, 10000},
1327 { 633344, 11000},
1328 { 505600, 12000},
1329 { 402944, 13000},
1330 { 320768, 14000},
1331 { 255488, 15000},
1332 { 204032, 16000},
1333 { 163072, 17000},
1334 { 130304, 18000},
1335 { 105216, 19000},
1336 { 83456, 20000},
1337 { 65024, 21000},
1338 { 52480, 22000},
1339 { 42752, 23000},
1340 { 34560, 24000},
1341 { 27136, 25000},
1342 { 22016, 26000},
1343 { 18432, 27000},
1344 { 15616, 28000},
1345 { 13312, 29000},
1346 { 11520, 30000},
1347};
1348
1349static u32 interpolate_value(u32 value, struct linear_segments *segments,
1350 unsigned len)
1351{
1352 u64 tmp64;
1353 u32 dx, dy;
1354 int i, ret;
1355
1356 if (value >= segments[0].x)
1357 return segments[0].y;
1358 if (value < segments[len-1].x)
1359 return segments[len-1].y;
1360
1361 for (i = 1; i < len - 1; i++) {
1362 /* If value is identical, no need to interpolate */
1363 if (value == segments[i].x)
1364 return segments[i].y;
1365 if (value > segments[i].x)
1366 break;
1367 }
1368
1369 /* Linear interpolation between the two (x,y) points */
1370 dy = segments[i].y - segments[i - 1].y;
1371 dx = segments[i - 1].x - segments[i].x;
1372 tmp64 = value - segments[i].x;
1373 tmp64 *= dy;
1374 do_div(tmp64, dx);
1375 ret = segments[i].y - tmp64;
1376
1377 return ret;
1378}
1379
1380static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1381{
1382 struct mb86a20s_state *state = fe->demodulator_priv;
1383 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1384 u32 cnr_linear, cnr;
1385 int rc, val;
1386
1387 /* Check if CNR is available */
1388 rc = mb86a20s_readreg(state, 0x45);
1389 if (rc < 0)
1390 return rc;
1391
1392 if (!(rc & 0x40)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001393 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001394 __func__);
1395 return -EBUSY;
1396 }
1397 val = rc;
1398
1399 rc = mb86a20s_readreg(state, 0x46);
1400 if (rc < 0)
1401 return rc;
1402 cnr_linear = rc << 8;
1403
1404 rc = mb86a20s_readreg(state, 0x46);
1405 if (rc < 0)
1406 return rc;
1407 cnr_linear |= rc;
1408
1409 cnr = interpolate_value(cnr_linear,
1410 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1411
1412 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1413 c->cnr.stat[0].svalue = cnr;
1414
1415 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1416 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1417
1418 /* CNR counter reset */
1419 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1420 if (rc < 0)
1421 return rc;
1422 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1423
1424 return rc;
1425}
1426
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001427static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001428{
1429 struct mb86a20s_state *state = fe->demodulator_priv;
1430 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1431 u32 mer, cnr;
1432 int rc, val, i;
1433 struct linear_segments *segs;
1434 unsigned segs_len;
1435
1436 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1437
1438 /* Check if the measures are already available */
1439 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1440 if (rc < 0)
1441 return rc;
1442 rc = mb86a20s_readreg(state, 0x51);
1443 if (rc < 0)
1444 return rc;
1445
1446 /* Check if data is available */
1447 if (!(rc & 0x01)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001448 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001449 "%s: MER measures aren't available yet.\n", __func__);
1450 return -EBUSY;
1451 }
1452
1453 /* Read all layers */
1454 for (i = 0; i < 3; i++) {
1455 if (!(c->isdbt_layer_enabled & (1 << i))) {
1456 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1457 continue;
1458 }
1459
1460 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1461 if (rc < 0)
1462 return rc;
1463 rc = mb86a20s_readreg(state, 0x51);
1464 if (rc < 0)
1465 return rc;
1466 mer = rc << 16;
1467 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1468 if (rc < 0)
1469 return rc;
1470 rc = mb86a20s_readreg(state, 0x51);
1471 if (rc < 0)
1472 return rc;
1473 mer |= rc << 8;
1474 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1475 if (rc < 0)
1476 return rc;
1477 rc = mb86a20s_readreg(state, 0x51);
1478 if (rc < 0)
1479 return rc;
1480 mer |= rc;
1481
1482 switch (c->layer[i].modulation) {
1483 case DQPSK:
1484 case QPSK:
1485 segs = cnr_qpsk_table;
1486 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1487 break;
1488 case QAM_16:
1489 segs = cnr_16qam_table;
1490 segs_len = ARRAY_SIZE(cnr_16qam_table);
1491 break;
1492 default:
1493 case QAM_64:
1494 segs = cnr_64qam_table;
1495 segs_len = ARRAY_SIZE(cnr_64qam_table);
1496 break;
1497 }
1498 cnr = interpolate_value(mer, segs, segs_len);
1499
1500 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1501 c->cnr.stat[1 + i].svalue = cnr;
1502
1503 dev_dbg(&state->i2c->dev,
1504 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1505 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1506
1507 }
1508
1509 /* Start a new MER measurement */
1510 /* MER counter reset */
1511 rc = mb86a20s_writereg(state, 0x50, 0x50);
1512 if (rc < 0)
1513 return rc;
1514 rc = mb86a20s_readreg(state, 0x51);
1515 if (rc < 0)
1516 return rc;
1517 val = rc;
1518
1519 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1520 if (rc < 0)
1521 return rc;
1522 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1523 if (rc < 0)
1524 return rc;
1525
1526 return 0;
1527}
1528
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001529static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1530{
1531 struct mb86a20s_state *state = fe->demodulator_priv;
1532 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1533 int i;
1534
1535 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1536
1537 /* Fill the length of each status counter */
1538
1539 /* Only global stats */
1540 c->strength.len = 1;
1541
1542 /* Per-layer stats - 3 layers + global */
1543 c->cnr.len = 4;
1544 c->pre_bit_error.len = 4;
1545 c->pre_bit_count.len = 4;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001546 c->post_bit_error.len = 4;
1547 c->post_bit_count.len = 4;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001548 c->block_error.len = 4;
1549 c->block_count.len = 4;
1550
1551 /* Signal is always available */
1552 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1553 c->strength.stat[0].uvalue = 0;
1554
1555 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1556 for (i = 0; i < 4; i++) {
1557 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1558 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1559 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001560 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1561 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001562 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1563 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1564 }
1565}
1566
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001567static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001568{
1569 struct mb86a20s_state *state = fe->demodulator_priv;
1570 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1571 int rc = 0, i;
1572 u32 bit_error = 0, bit_count = 0;
1573 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001574 u32 t_post_bit_error = 0, t_post_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001575 u32 block_error = 0, block_count = 0;
1576 u32 t_block_error = 0, t_block_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001577 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1578 int per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001579
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001580 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1581
1582 mb86a20s_get_main_CNR(fe);
1583
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001584 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001585 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001586
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001587 /*
1588 * At state 7, only CNR is available
1589 * For BER measures, state=9 is required
1590 * FIXME: we may get MER measures with state=8
1591 */
1592 if (status_nr < 9)
1593 return 0;
1594
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001595 for (i = 0; i < 3; i++) {
1596 if (c->isdbt_layer_enabled & (1 << i)) {
1597 /* Layer is active and has rc segments */
1598 active_layers++;
1599
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001600 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001601 rc = mb86a20s_get_pre_ber(fe, i,
1602 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001603 if (rc >= 0) {
1604 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1605 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1606 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1607 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1608 } else if (rc != -EBUSY) {
1609 /*
1610 * If an I/O error happened,
1611 * measures are now unavailable
1612 */
1613 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1614 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1615 dev_err(&state->i2c->dev,
1616 "%s: Can't get BER for layer %c (error %d).\n",
1617 __func__, 'A' + i, rc);
1618 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001619 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001620 pre_ber_layers++;
1621
1622 /* Handle BER post vterbi */
1623 rc = mb86a20s_get_post_ber(fe, i,
1624 &bit_error, &bit_count);
1625 if (rc >= 0) {
1626 c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1627 c->post_bit_error.stat[1 + i].uvalue += bit_error;
1628 c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1629 c->post_bit_count.stat[1 + i].uvalue += bit_count;
1630 } else if (rc != -EBUSY) {
1631 /*
1632 * If an I/O error happened,
1633 * measures are now unavailable
1634 */
1635 c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1636 c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1637 dev_err(&state->i2c->dev,
1638 "%s: Can't get BER for layer %c (error %d).\n",
1639 __func__, 'A' + i, rc);
1640 }
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001641 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1642 post_ber_layers++;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001643
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001644 /* Handle Block errors for PER/UCB reports */
1645 rc = mb86a20s_get_blk_error(fe, i,
1646 &block_error,
1647 &block_count);
1648 if (rc >= 0) {
1649 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1650 c->block_error.stat[1 + i].uvalue += block_error;
1651 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1652 c->block_count.stat[1 + i].uvalue += block_count;
1653 } else if (rc != -EBUSY) {
1654 /*
1655 * If an I/O error happened,
1656 * measures are now unavailable
1657 */
1658 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1659 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1660 dev_err(&state->i2c->dev,
1661 "%s: Can't get PER for layer %c (error %d).\n",
1662 __func__, 'A' + i, rc);
1663
1664 }
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001665 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1666 per_layers++;
1667
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001668 /* Update total preBER */
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001669 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1670 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001671
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001672 /* Update total postBER */
1673 t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
1674 t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
1675
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001676 /* Update total PER */
1677 t_block_error += c->block_error.stat[1 + i].uvalue;
1678 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001679 }
1680 }
1681
1682 /*
1683 * Start showing global count if at least one error count is
1684 * available.
1685 */
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001686 if (pre_ber_layers) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001687 /*
1688 * At least one per-layer BER measure was read. We can now
1689 * calculate the total BER
1690 *
1691 * Total Bit Error/Count is calculated as the sum of the
1692 * bit errors on all active layers.
1693 */
1694 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1695 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1696 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1697 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001698 } else {
1699 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1700 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001701 }
1702
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001703 /*
1704 * Start showing global count if at least one error count is
1705 * available.
1706 */
1707 if (post_ber_layers) {
1708 /*
1709 * At least one per-layer BER measure was read. We can now
1710 * calculate the total BER
1711 *
1712 * Total Bit Error/Count is calculated as the sum of the
1713 * bit errors on all active layers.
1714 */
1715 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1716 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1717 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1718 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001719 } else {
1720 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1721 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001722 }
1723
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001724 if (per_layers) {
1725 /*
1726 * At least one per-layer UCB measure was read. We can now
1727 * calculate the total UCB
1728 *
1729 * Total block Error/Count is calculated as the sum of the
1730 * block errors on all active layers.
1731 */
1732 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1733 c->block_error.stat[0].uvalue = t_block_error;
1734 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1735 c->block_count.stat[0].uvalue = t_block_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001736 } else {
1737 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1738 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001739 }
1740
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001741 return rc;
1742}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001743
1744/*
1745 * The functions below are called via DVB callbacks, so they need to
1746 * properly use the I2C gate control
1747 */
1748
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001749static int mb86a20s_initfe(struct dvb_frontend *fe)
1750{
1751 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001752 u64 pll;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001753 int rc;
1754 u8 regD5 = 1;
1755
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001756 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001757
1758 if (fe->ops.i2c_gate_ctrl)
1759 fe->ops.i2c_gate_ctrl(fe, 0);
1760
1761 /* Initialize the frontend */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001762 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001763 if (rc < 0)
1764 goto err;
1765
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001766 /* Adjust IF frequency to match tuner */
1767 if (fe->ops.tuner_ops.get_if_frequency)
1768 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1769
1770 if (!state->if_freq)
1771 state->if_freq = 3300000;
1772
1773 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1774 pll = state->if_freq * 1677721600L;
1775 do_div(pll, 1628571429L);
1776 rc = mb86a20s_writereg(state, 0x28, 0x20);
1777 if (rc < 0)
1778 goto err;
1779 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1780 if (rc < 0)
1781 goto err;
1782 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1783 if (rc < 0)
1784 goto err;
1785 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1786 if (rc < 0)
1787 goto err;
1788 dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n",
1789 __func__, state->if_freq, (long long)pll);
1790
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001791 if (!state->config->is_serial) {
1792 regD5 &= ~1;
1793
1794 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1795 if (rc < 0)
1796 goto err;
1797 rc = mb86a20s_writereg(state, 0x51, regD5);
1798 if (rc < 0)
1799 goto err;
1800 }
1801
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001802 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1803 if (rc < 0)
1804 goto err;
1805
1806
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001807err:
1808 if (fe->ops.i2c_gate_ctrl)
1809 fe->ops.i2c_gate_ctrl(fe, 1);
1810
1811 if (rc < 0) {
1812 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001813 dev_info(&state->i2c->dev,
1814 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001815 } else {
1816 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001817 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001818 }
1819 return rc;
1820}
1821
1822static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1823{
1824 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001825 int rc, if_freq;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001826#if 0
1827 /*
1828 * FIXME: Properly implement the set frontend properties
1829 */
1830 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1831#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001832 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001833
1834 /*
1835 * Gate should already be opened, but it doesn't hurt to
1836 * double-check
1837 */
1838 if (fe->ops.i2c_gate_ctrl)
1839 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001840 fe->ops.tuner_ops.set_params(fe);
1841
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001842 if (fe->ops.tuner_ops.get_if_frequency) {
1843 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1844
1845 /*
1846 * If the IF frequency changed, re-initialize the
1847 * frontend. This is needed by some drivers like tda18271,
1848 * that only sets the IF after receiving a set_params() call
1849 */
1850 if (if_freq != state->if_freq)
1851 state->need_init = true;
1852 }
1853
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001854 /*
1855 * Make it more reliable: if, for some reason, the initial
1856 * device initialization doesn't happen, initialize it when
1857 * a SBTVD parameters are adjusted.
1858 *
1859 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1860 * the agc callback logic is not called during DVB attach time,
1861 * causing mb86a20s to not be initialized with Kworld SBTVD.
1862 * So, this hack is needed, in order to make Kworld SBTVD to work.
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001863 *
1864 * It is also needed to change the IF after the initial init.
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001865 */
1866 if (state->need_init)
1867 mb86a20s_initfe(fe);
1868
1869 if (fe->ops.i2c_gate_ctrl)
1870 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001871
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001872 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001873 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001874
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001875 if (fe->ops.i2c_gate_ctrl)
1876 fe->ops.i2c_gate_ctrl(fe, 1);
1877
1878 return rc;
1879}
1880
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001881static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1882 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001883{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001884 struct mb86a20s_state *state = fe->demodulator_priv;
1885 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001886 int rc, status_nr;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001887
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001888 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001889
1890 if (fe->ops.i2c_gate_ctrl)
1891 fe->ops.i2c_gate_ctrl(fe, 0);
1892
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001893 /* Get lock */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001894 status_nr = mb86a20s_read_status(fe, status);
1895 if (status_nr < 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001896 mb86a20s_stats_not_ready(fe);
1897 mb86a20s_reset_frontend_cache(fe);
1898 }
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001899 if (status_nr < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001900 dev_err(&state->i2c->dev,
1901 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001902 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001903 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001904
1905 /* Get signal strength */
1906 rc = mb86a20s_read_signal_strength(fe);
1907 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001908 dev_err(&state->i2c->dev,
1909 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001910 mb86a20s_stats_not_ready(fe);
1911 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001912
1913 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001914 goto error;
1915 }
1916 /* Fill signal strength */
1917 c->strength.stat[0].uvalue = rc;
1918
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001919 if (status_nr >= 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001920 /* Get TMCC info*/
1921 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001922 if (rc < 0) {
1923 dev_err(&state->i2c->dev,
1924 "%s: Can't get FE TMCC data.\n", __func__);
1925 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001926 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001927 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001928
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001929 /* Get statistics */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001930 rc = mb86a20s_get_stats(fe, status_nr);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001931 if (rc < 0 && rc != -EBUSY) {
1932 dev_err(&state->i2c->dev,
1933 "%s: Can't get FE statistics.\n", __func__);
1934 rc = 0;
1935 goto error;
1936 }
1937 rc = 0; /* Don't return EBUSY to userspace */
1938 }
1939 goto ok;
1940
1941error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001942 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001943
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001944ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001945 if (fe->ops.i2c_gate_ctrl)
1946 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001947
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001948 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001949}
1950
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001951static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
1952 u16 *strength)
1953{
1954 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1955
1956
1957 *strength = c->strength.stat[0].uvalue;
1958
1959 return 0;
1960}
1961
1962static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
1963{
1964 /*
1965 * get_frontend is now handled together with other stats
1966 * retrival, when read_status() is called, as some statistics
1967 * will depend on the layers detection.
1968 */
1969 return 0;
1970};
1971
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001972static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001973 bool re_tune,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001974 unsigned int mode_flags,
1975 unsigned int *delay,
1976 fe_status_t *status)
1977{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001978 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001979 int rc = 0;
1980
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001981 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001982
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001983 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001984 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001985
1986 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001987 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001988
1989 return rc;
1990}
1991
1992static void mb86a20s_release(struct dvb_frontend *fe)
1993{
1994 struct mb86a20s_state *state = fe->demodulator_priv;
1995
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001996 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001997
1998 kfree(state);
1999}
2000
2001static struct dvb_frontend_ops mb86a20s_ops;
2002
2003struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2004 struct i2c_adapter *i2c)
2005{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002006 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002007 u8 rev;
2008
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002009 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2010
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002011 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002012 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002013 if (state == NULL) {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002014 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002015 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002016 goto error;
2017 }
2018
2019 /* setup the state */
2020 state->config = config;
2021 state->i2c = i2c;
2022
2023 /* create dvb_frontend */
2024 memcpy(&state->frontend.ops, &mb86a20s_ops,
2025 sizeof(struct dvb_frontend_ops));
2026 state->frontend.demodulator_priv = state;
2027
2028 /* Check if it is a mb86a20s frontend */
2029 rev = mb86a20s_readreg(state, 0);
2030
2031 if (rev == 0x13) {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002032 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002033 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002034 } else {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002035 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002036 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002037 rev);
2038 goto error;
2039 }
2040
2041 return &state->frontend;
2042
2043error:
2044 kfree(state);
2045 return NULL;
2046}
2047EXPORT_SYMBOL(mb86a20s_attach);
2048
2049static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002050 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002051 /* Use dib8000 values per default */
2052 .info = {
2053 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002054 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
2055 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2056 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2057 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2058 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2059 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2060 /* Actually, those values depend on the used tuner */
2061 .frequency_min = 45000000,
2062 .frequency_max = 864000000,
2063 .frequency_stepsize = 62500,
2064 },
2065
2066 .release = mb86a20s_release,
2067
2068 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002069 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002070 .get_frontend = mb86a20s_get_frontend_dummy,
2071 .read_status = mb86a20s_read_status_and_stats,
2072 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002073 .tune = mb86a20s_tune,
2074};
2075
2076MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2077MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2078MODULE_LICENSE("GPL");