blob: 9948fcbf1596f597c67f6103327d7ac51146225f [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];
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -030037 unsigned long get_strength_time;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030038
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030039 bool need_init;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030040};
41
42struct regdata {
43 u8 reg;
44 u8 data;
45};
46
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030047#define BER_SAMPLING_RATE 1 /* Seconds */
48
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030049/*
50 * Initialization sequence: Use whatevere default values that PV SBTVD
51 * does on its initialisation, obtained via USB snoop
52 */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030053static struct regdata mb86a20s_init1[] = {
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030054 { 0x70, 0x0f },
55 { 0x70, 0xff },
56 { 0x08, 0x01 },
57 { 0x09, 0x3e },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020058 { 0x50, 0xd1 }, { 0x51, 0x22 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030059 { 0x39, 0x01 },
60 { 0x71, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020061 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -030062};
63
64static struct regdata mb86a20s_init2[] = {
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020065 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030066 { 0x3b, 0x21 },
67 { 0x3c, 0x3a },
68 { 0x01, 0x0d },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020069 { 0x04, 0x08 }, { 0x05, 0x05 },
70 { 0x04, 0x0e }, { 0x05, 0x00 },
71 { 0x04, 0x0f }, { 0x05, 0x14 },
72 { 0x04, 0x0b }, { 0x05, 0x8c },
73 { 0x04, 0x00 }, { 0x05, 0x00 },
74 { 0x04, 0x01 }, { 0x05, 0x07 },
75 { 0x04, 0x02 }, { 0x05, 0x0f },
76 { 0x04, 0x03 }, { 0x05, 0xa0 },
77 { 0x04, 0x09 }, { 0x05, 0x00 },
78 { 0x04, 0x0a }, { 0x05, 0xff },
79 { 0x04, 0x27 }, { 0x05, 0x64 },
80 { 0x04, 0x28 }, { 0x05, 0x00 },
81 { 0x04, 0x1e }, { 0x05, 0xff },
82 { 0x04, 0x29 }, { 0x05, 0x0a },
83 { 0x04, 0x32 }, { 0x05, 0x0a },
84 { 0x04, 0x14 }, { 0x05, 0x02 },
85 { 0x04, 0x04 }, { 0x05, 0x00 },
86 { 0x04, 0x05 }, { 0x05, 0x22 },
87 { 0x04, 0x06 }, { 0x05, 0x0e },
88 { 0x04, 0x07 }, { 0x05, 0xd8 },
89 { 0x04, 0x12 }, { 0x05, 0x00 },
90 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020091 { 0x04, 0x15 }, { 0x05, 0x4e },
92 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030093
94 /*
95 * On this demod, when the bit count reaches the count below,
96 * it collects the bit error count. The bit counters are initialized
97 * to 65535 here. This warrants that all of them will be quickly
98 * calculated when device gets locked. As TMCC is parsed, the values
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -030099 * will be adjusted later in the driver's code.
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300100 */
101 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
102 { 0x50, 0xa7 }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200103 { 0x50, 0xa8 }, { 0x51, 0xff },
104 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300105 { 0x50, 0xaa }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200106 { 0x50, 0xab }, { 0x51, 0xff },
107 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300108 { 0x50, 0xad }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200109 { 0x50, 0xae }, { 0x51, 0xff },
110 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300111
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300112 /*
113 * On this demod, post BER counts blocks. When the count reaches the
114 * value below, it collects the block error count. The block counters
115 * are initialized to 127 here. This warrants that all of them will be
116 * quickly calculated when device gets locked. As TMCC is parsed, the
117 * values will be adjusted later in the driver's code.
118 */
119 { 0x5e, 0x07 }, /* Turn on BER after Viterbi */
120 { 0x50, 0xdc }, { 0x51, 0x00 },
121 { 0x50, 0xdd }, { 0x51, 0x7f },
122 { 0x50, 0xde }, { 0x51, 0x00 },
123 { 0x50, 0xdf }, { 0x51, 0x7f },
124 { 0x50, 0xe0 }, { 0x51, 0x00 },
125 { 0x50, 0xe1 }, { 0x51, 0x7f },
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -0300126
127 /*
128 * On this demod, when the block count reaches the count below,
129 * it collects the block error count. The block counters are initialized
130 * to 127 here. This warrants that all of them will be quickly
131 * calculated when device gets locked. As TMCC is parsed, the values
132 * will be adjusted later in the driver's code.
133 */
134 { 0x50, 0xb0 }, { 0x51, 0x07 }, /* Enable PER */
135 { 0x50, 0xb2 }, { 0x51, 0x00 },
136 { 0x50, 0xb3 }, { 0x51, 0x7f },
137 { 0x50, 0xb4 }, { 0x51, 0x00 },
138 { 0x50, 0xb5 }, { 0x51, 0x7f },
139 { 0x50, 0xb6 }, { 0x51, 0x00 },
140 { 0x50, 0xb7 }, { 0x51, 0x7f },
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300141
142 { 0x50, 0x50 }, { 0x51, 0x02 }, /* MER manual mode */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300143 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
144 { 0x45, 0x04 }, /* CN symbol 4 */
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -0300145 { 0x48, 0x04 }, /* CN manual mode */
146
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200147 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
148 { 0x50, 0xd6 }, { 0x51, 0x1f },
149 { 0x50, 0xd2 }, { 0x51, 0x03 },
150 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200151 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
152 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300153
154 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200155 { 0x28, 0x00 }, { 0x29, 0x10 },
156 { 0x28, 0x05 }, { 0x29, 0x02 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300157 { 0x1c, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200158 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
159 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
160 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
161 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
162 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
163 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
164 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
165 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
166 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
167 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
168 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
169 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
170 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
171 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
172 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
173 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
174 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
175 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
176 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
177 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
178 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
179 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
180 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
181 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
182 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
183 { 0x50, 0x1e }, { 0x51, 0x5d },
184 { 0x50, 0x22 }, { 0x51, 0x00 },
185 { 0x50, 0x23 }, { 0x51, 0xc8 },
186 { 0x50, 0x24 }, { 0x51, 0x00 },
187 { 0x50, 0x25 }, { 0x51, 0xf0 },
188 { 0x50, 0x26 }, { 0x51, 0x00 },
189 { 0x50, 0x27 }, { 0x51, 0xc3 },
190 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200191 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300192 { 0xd0, 0x00 },
193};
194
195static struct regdata mb86a20s_reset_reception[] = {
196 { 0x70, 0xf0 },
197 { 0x70, 0xff },
198 { 0x08, 0x01 },
199 { 0x08, 0x00 },
200};
201
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300202static struct regdata mb86a20s_per_ber_reset[] = {
203 { 0x53, 0x00 }, /* pre BER Counter reset */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300204 { 0x53, 0x07 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300205
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300206 { 0x5f, 0x00 }, /* post BER Counter reset */
207 { 0x5f, 0x07 },
208
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300209 { 0x50, 0xb1 }, /* PER Counter reset */
210 { 0x51, 0x07 },
211 { 0x51, 0x00 },
212};
213
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200214/*
215 * I2C read/write functions and macros
216 */
217
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300218static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300219 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300220{
221 u8 buf[] = { reg, data };
222 struct i2c_msg msg = {
223 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
224 };
225 int rc;
226
227 rc = i2c_transfer(state->i2c, &msg, 1);
228 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200229 dev_err(&state->i2c->dev,
230 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
231 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300232 return rc;
233 }
234
235 return 0;
236}
237
238static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
239 u8 i2c_addr, struct regdata *rd, int size)
240{
241 int i, rc;
242
243 for (i = 0; i < size; i++) {
244 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
245 rd[i].data);
246 if (rc < 0)
247 return rc;
248 }
249 return 0;
250}
251
252static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
253 u8 i2c_addr, u8 reg)
254{
255 u8 val;
256 int rc;
257 struct i2c_msg msg[] = {
258 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
259 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
260 };
261
262 rc = i2c_transfer(state->i2c, msg, 2);
263
264 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200265 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
266 __func__, reg, rc);
267 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300268 }
269
270 return val;
271}
272
273#define mb86a20s_readreg(state, reg) \
274 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
275#define mb86a20s_writereg(state, reg, val) \
276 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
277#define mb86a20s_writeregdata(state, regdata) \
278 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
279 regdata, ARRAY_SIZE(regdata))
280
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300281/*
282 * Ancillary internal routines (likely compiled inlined)
283 *
284 * The functions below assume that gateway lock has already obtained
285 */
286
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200287static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300288{
289 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200290 int val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300291
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200292 *status = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300293
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200294 val = mb86a20s_readreg(state, 0x0a) & 0xf;
295 if (val < 0)
296 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300297
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200298 if (val >= 2)
299 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300300
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200301 if (val >= 4)
302 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300303
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200304 if (val >= 5)
305 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300306
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200307 if (val >= 7)
308 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300309
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200310 if (val >= 8) /* Maybe 9? */
311 *status |= FE_HAS_LOCK;
312
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200313 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
314 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200315
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -0300316 return val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300317}
318
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300319static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300320{
321 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300322 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300323 int rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300324 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300325
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300326 if (state->get_strength_time &&
327 (!time_after(jiffies, state->get_strength_time)))
328 return c->strength.stat[0].uvalue;
329
330 /* Reset its value if an error happen */
331 c->strength.stat[0].uvalue = 0;
332
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300333 /* Does a binary search to get RF strength */
334 rf_max = 0xfff;
335 rf_min = 0;
336 do {
337 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300338 rc = mb86a20s_writereg(state, 0x04, 0x1f);
339 if (rc < 0)
340 return rc;
341 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
342 if (rc < 0)
343 return rc;
344 rc = mb86a20s_writereg(state, 0x04, 0x20);
345 if (rc < 0)
346 return rc;
Mauro Carvalho Chehabdad78c52013-03-01 16:15:16 -0300347 rc = mb86a20s_writereg(state, 0x05, rf);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300348 if (rc < 0)
349 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300350
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300351 rc = mb86a20s_readreg(state, 0x02);
352 if (rc < 0)
353 return rc;
354 if (rc & 0x08)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300355 rf_min = (rf_max + rf_min) / 2;
356 else
357 rf_max = (rf_max + rf_min) / 2;
358 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300359 rf = (rf_max + rf_min) / 2;
360
361 /* Rescale it from 2^12 (4096) to 2^16 */
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300362 rf = rf << (16 - 12);
363 if (rf)
364 rf |= (1 << 12) - 1;
365
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200366 dev_dbg(&state->i2c->dev,
367 "%s: signal strength = %d (%d < RF=%d < %d)\n",
368 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300369 c->strength.stat[0].uvalue = rf;
370 state->get_strength_time = jiffies +
371 msecs_to_jiffies(1000);
372 return 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300373 }
374 } while (1);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300375}
376
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200377static int mb86a20s_get_modulation(struct mb86a20s_state *state,
378 unsigned layer)
379{
380 int rc;
381 static unsigned char reg[] = {
382 [0] = 0x86, /* Layer A */
383 [1] = 0x8a, /* Layer B */
384 [2] = 0x8e, /* Layer C */
385 };
386
Dan Carpenter82033bc2012-01-13 02:28:34 -0300387 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200388 return -EINVAL;
389 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
390 if (rc < 0)
391 return rc;
392 rc = mb86a20s_readreg(state, 0x6e);
393 if (rc < 0)
394 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300395 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200396 case 0:
397 return DQPSK;
398 case 1:
399 return QPSK;
400 case 2:
401 return QAM_16;
402 case 3:
403 return QAM_64;
404 default:
405 return QAM_AUTO;
406 }
407}
408
409static int mb86a20s_get_fec(struct mb86a20s_state *state,
410 unsigned layer)
411{
412 int rc;
413
414 static unsigned char reg[] = {
415 [0] = 0x87, /* Layer A */
416 [1] = 0x8b, /* Layer B */
417 [2] = 0x8f, /* Layer C */
418 };
419
Dan Carpenter82033bc2012-01-13 02:28:34 -0300420 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200421 return -EINVAL;
422 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
423 if (rc < 0)
424 return rc;
425 rc = mb86a20s_readreg(state, 0x6e);
426 if (rc < 0)
427 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300428 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200429 case 0:
430 return FEC_1_2;
431 case 1:
432 return FEC_2_3;
433 case 2:
434 return FEC_3_4;
435 case 3:
436 return FEC_5_6;
437 case 4:
438 return FEC_7_8;
439 default:
440 return FEC_AUTO;
441 }
442}
443
444static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
445 unsigned layer)
446{
447 int rc;
448
449 static unsigned char reg[] = {
450 [0] = 0x88, /* Layer A */
451 [1] = 0x8c, /* Layer B */
452 [2] = 0x90, /* Layer C */
453 };
454
Dan Carpenter82033bc2012-01-13 02:28:34 -0300455 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200456 return -EINVAL;
457 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
458 if (rc < 0)
459 return rc;
460 rc = mb86a20s_readreg(state, 0x6e);
461 if (rc < 0)
462 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300463
464 switch ((rc >> 4) & 0x07) {
465 case 1:
466 return GUARD_INTERVAL_1_4;
467 case 2:
468 return GUARD_INTERVAL_1_8;
469 case 3:
470 return GUARD_INTERVAL_1_16;
471 case 4:
472 return GUARD_INTERVAL_1_32;
473
474 default:
475 case 0:
476 return GUARD_INTERVAL_AUTO;
477 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200478}
479
480static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
481 unsigned layer)
482{
483 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200484 static unsigned char reg[] = {
485 [0] = 0x89, /* Layer A */
486 [1] = 0x8d, /* Layer B */
487 [2] = 0x91, /* Layer C */
488 };
489
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200490 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
491
Dan Carpenter82033bc2012-01-13 02:28:34 -0300492 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200493 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200494
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200495 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
496 if (rc < 0)
497 return rc;
498 rc = mb86a20s_readreg(state, 0x6e);
499 if (rc < 0)
500 return rc;
501 count = (rc >> 4) & 0x0f;
502
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200503 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
504
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200505 return count;
506}
507
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300508static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
509{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200510 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300511 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
512
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200513 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
514
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300515 /* Fixed parameters */
516 c->delivery_system = SYS_ISDBT;
517 c->bandwidth_hz = 6000000;
518
519 /* Initialize values that will be later autodetected */
520 c->isdbt_layer_enabled = 0;
521 c->transmission_mode = TRANSMISSION_MODE_AUTO;
522 c->guard_interval = GUARD_INTERVAL_AUTO;
523 c->isdbt_sb_mode = 0;
524 c->isdbt_sb_segment_count = 0;
525}
526
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300527/*
528 * Estimates the bit rate using the per-segment bit rate given by
529 * ABNT/NBR 15601 spec (table 4).
530 */
531static u32 isdbt_rate[3][5][4] = {
532 { /* DQPSK/QPSK */
533 { 280850, 312060, 330420, 340430 }, /* 1/2 */
534 { 374470, 416080, 440560, 453910 }, /* 2/3 */
535 { 421280, 468090, 495630, 510650 }, /* 3/4 */
536 { 468090, 520100, 550700, 567390 }, /* 5/6 */
537 { 491500, 546110, 578230, 595760 }, /* 7/8 */
538 }, { /* QAM16 */
539 { 561710, 624130, 660840, 680870 }, /* 1/2 */
540 { 748950, 832170, 881120, 907820 }, /* 2/3 */
541 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
542 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
543 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
544 }, { /* QAM64 */
545 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
546 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
547 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
548 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
549 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
550 }
551};
552
553static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
554 u32 modulation, u32 fec, u32 interleaving,
555 u32 segment)
556{
557 struct mb86a20s_state *state = fe->demodulator_priv;
558 u32 rate;
559 int m, f, i;
560
561 /*
562 * If modulation/fec/interleaving is not detected, the default is
563 * to consider the lowest bit rate, to avoid taking too long time
564 * to get BER.
565 */
566 switch (modulation) {
567 case DQPSK:
568 case QPSK:
569 default:
570 m = 0;
571 break;
572 case QAM_16:
573 m = 1;
574 break;
575 case QAM_64:
576 m = 2;
577 break;
578 }
579
580 switch (fec) {
581 default:
582 case FEC_1_2:
583 case FEC_AUTO:
584 f = 0;
585 break;
586 case FEC_2_3:
587 f = 1;
588 break;
589 case FEC_3_4:
590 f = 2;
591 break;
592 case FEC_5_6:
593 f = 3;
594 break;
595 case FEC_7_8:
596 f = 4;
597 break;
598 }
599
600 switch (interleaving) {
601 default:
602 case GUARD_INTERVAL_1_4:
603 i = 0;
604 break;
605 case GUARD_INTERVAL_1_8:
606 i = 1;
607 break;
608 case GUARD_INTERVAL_1_16:
609 i = 2;
610 break;
611 case GUARD_INTERVAL_1_32:
612 i = 3;
613 break;
614 }
615
616 /* Samples BER at BER_SAMPLING_RATE seconds */
617 rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
618
619 /* Avoids sampling too quickly or to overflow the register */
620 if (rate < 256)
621 rate = 256;
622 else if (rate > (1 << 24) - 1)
623 rate = (1 << 24) - 1;
624
625 dev_dbg(&state->i2c->dev,
626 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
627 __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
628 rate, rate);
629
630 state->estimated_rate[i] = rate;
631}
632
633
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300634static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300635{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200636 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300637 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200638 int i, rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300639
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200640 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
641
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300642 /* Reset frontend cache to default values */
643 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200644
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200645 /* Check for partial reception */
646 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300647 if (rc < 0)
648 return rc;
649 rc = mb86a20s_readreg(state, 0x6e);
650 if (rc < 0)
651 return rc;
652 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200653
654 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300655
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200656 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200657 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
658 __func__, 'A' + i);
659
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200660 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300661 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200662 goto noperlayer_error;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300663 if (rc >= 0 && rc < 14) {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300664 c->layer[i].segment_count = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300665 } else {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300666 c->layer[i].segment_count = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300667 state->estimated_rate[i] = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200668 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300669 }
670 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200671 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300672 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200673 goto noperlayer_error;
674 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
675 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300676 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200677 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300678 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200679 goto noperlayer_error;
680 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
681 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300682 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200683 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300684 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200685 goto noperlayer_error;
686 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
687 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300688 c->layer[i].interleaving = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300689 mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
690 c->layer[i].fec,
691 c->layer[i].interleaving,
692 c->layer[i].segment_count);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200693 }
694
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200695 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300696 if (rc < 0)
697 return rc;
698 if ((rc & 0x60) == 0x20) {
699 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200700 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300701 if (!c->isdbt_sb_segment_count)
702 c->isdbt_sb_segment_count = 1;
703 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200704
705 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200706 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300707 if (rc < 0)
708 return rc;
709 if ((rc & 0x60) == 0x20) {
710 switch (rc & 0x0c >> 2) {
711 case 0:
712 c->transmission_mode = TRANSMISSION_MODE_2K;
713 break;
714 case 1:
715 c->transmission_mode = TRANSMISSION_MODE_4K;
716 break;
717 case 2:
718 c->transmission_mode = TRANSMISSION_MODE_8K;
719 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200720 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300721 }
722 if (!(rc & 0x10)) {
723 switch (rc & 0x3) {
724 case 0:
725 c->guard_interval = GUARD_INTERVAL_1_4;
726 break;
727 case 1:
728 c->guard_interval = GUARD_INTERVAL_1_8;
729 break;
730 case 2:
731 c->guard_interval = GUARD_INTERVAL_1_16;
732 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200733 }
734 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300735 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200736
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200737noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300738
739 /* per-layer info is incomplete; discard all per-layer */
740 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300741
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300742 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300743}
744
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300745static int mb86a20s_reset_counters(struct dvb_frontend *fe)
746{
747 struct mb86a20s_state *state = fe->demodulator_priv;
748 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
749 int rc, val;
750
751 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
752
753 /* Reset the counters, if the channel changed */
754 if (state->last_frequency != c->frequency) {
755 memset(&c->strength, 0, sizeof(c->strength));
756 memset(&c->cnr, 0, sizeof(c->cnr));
757 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
758 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300759 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
760 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300761 memset(&c->block_error, 0, sizeof(c->block_error));
762 memset(&c->block_count, 0, sizeof(c->block_count));
763
764 state->last_frequency = c->frequency;
765 }
766
767 /* Clear status for most stats */
768
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300769 /* BER/PER counter reset */
770 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300771 if (rc < 0)
772 goto err;
773
774 /* CNR counter reset */
775 rc = mb86a20s_readreg(state, 0x45);
776 if (rc < 0)
777 goto err;
778 val = rc;
779 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
780 if (rc < 0)
781 goto err;
782 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
783 if (rc < 0)
784 goto err;
785
786 /* MER counter reset */
787 rc = mb86a20s_writereg(state, 0x50, 0x50);
788 if (rc < 0)
789 goto err;
790 rc = mb86a20s_readreg(state, 0x51);
791 if (rc < 0)
792 goto err;
793 val = rc;
794 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
795 if (rc < 0)
796 goto err;
797 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
798 if (rc < 0)
799 goto err;
800
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300801 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300802err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300803 dev_err(&state->i2c->dev,
804 "%s: Can't reset FE statistics (error %d).\n",
805 __func__, rc);
806ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300807 return rc;
808}
809
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300810static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
811 unsigned layer,
812 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300813{
814 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300815 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300816
817 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
818
819 if (layer >= 3)
820 return -EINVAL;
821
822 /* Check if the BER measures are already available */
823 rc = mb86a20s_readreg(state, 0x54);
824 if (rc < 0)
825 return rc;
826
827 /* Check if data is available for that layer */
828 if (!(rc & (1 << layer))) {
829 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300830 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300831 __func__, 'A' + layer);
832 return -EBUSY;
833 }
834
835 /* Read Bit Error Count */
836 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
837 if (rc < 0)
838 return rc;
839 *error = rc << 16;
840 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
841 if (rc < 0)
842 return rc;
843 *error |= rc << 8;
844 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
845 if (rc < 0)
846 return rc;
847 *error |= rc;
848
849 dev_dbg(&state->i2c->dev,
850 "%s: bit error before Viterbi for layer %c: %d.\n",
851 __func__, 'A' + layer, *error);
852
853 /* Read Bit Count */
854 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
855 if (rc < 0)
856 return rc;
857 rc = mb86a20s_readreg(state, 0x51);
858 if (rc < 0)
859 return rc;
860 *count = rc << 16;
861 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
862 if (rc < 0)
863 return rc;
864 rc = mb86a20s_readreg(state, 0x51);
865 if (rc < 0)
866 return rc;
867 *count |= rc << 8;
868 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
869 if (rc < 0)
870 return rc;
871 rc = mb86a20s_readreg(state, 0x51);
872 if (rc < 0)
873 return rc;
874 *count |= rc;
875
876 dev_dbg(&state->i2c->dev,
877 "%s: bit count before Viterbi for layer %c: %d.\n",
878 __func__, 'A' + layer, *count);
879
880
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300881 /*
882 * As we get TMCC data from the frontend, we can better estimate the
883 * BER bit counters, in order to do the BER measure during a longer
884 * time. Use those data, if available, to update the bit count
885 * measure.
886 */
887
888 if (state->estimated_rate[layer]
889 && state->estimated_rate[layer] != *count) {
890 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300891 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300892 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300893
894 /* Turn off BER before Viterbi */
895 rc = mb86a20s_writereg(state, 0x52, 0x00);
896
897 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300898 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
899 if (rc < 0)
900 return rc;
901 rc = mb86a20s_writereg(state, 0x51,
902 state->estimated_rate[layer] >> 16);
903 if (rc < 0)
904 return rc;
905 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
906 if (rc < 0)
907 return rc;
908 rc = mb86a20s_writereg(state, 0x51,
909 state->estimated_rate[layer] >> 8);
910 if (rc < 0)
911 return rc;
912 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
913 if (rc < 0)
914 return rc;
915 rc = mb86a20s_writereg(state, 0x51,
916 state->estimated_rate[layer]);
917 if (rc < 0)
918 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300919
920 /* Turn on BER before Viterbi */
921 rc = mb86a20s_writereg(state, 0x52, 0x01);
922
923 /* Reset all preBER counters */
924 rc = mb86a20s_writereg(state, 0x53, 0x00);
925 if (rc < 0)
926 return rc;
927 rc = mb86a20s_writereg(state, 0x53, 0x07);
928 } else {
929 /* Reset counter to collect new data */
930 rc = mb86a20s_readreg(state, 0x53);
931 if (rc < 0)
932 return rc;
933 val = rc;
934 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
935 if (rc < 0)
936 return rc;
937 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300938 }
939
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300940 return rc;
941}
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300942
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300943static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
944 unsigned layer,
945 u32 *error, u32 *count)
946{
947 struct mb86a20s_state *state = fe->demodulator_priv;
948 u32 counter, collect_rate;
949 int rc, val;
950
951 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
952
953 if (layer >= 3)
954 return -EINVAL;
955
956 /* Check if the BER measures are already available */
957 rc = mb86a20s_readreg(state, 0x60);
958 if (rc < 0)
959 return rc;
960
961 /* Check if data is available for that layer */
962 if (!(rc & (1 << layer))) {
963 dev_dbg(&state->i2c->dev,
964 "%s: post BER for layer %c is not available yet.\n",
965 __func__, 'A' + layer);
966 return -EBUSY;
967 }
968
969 /* Read Bit Error Count */
970 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
971 if (rc < 0)
972 return rc;
973 *error = rc << 16;
974 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
975 if (rc < 0)
976 return rc;
977 *error |= rc << 8;
978 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
979 if (rc < 0)
980 return rc;
981 *error |= rc;
982
983 dev_dbg(&state->i2c->dev,
984 "%s: post bit error for layer %c: %d.\n",
985 __func__, 'A' + layer, *error);
986
987 /* Read Bit Count */
988 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
989 if (rc < 0)
990 return rc;
991 rc = mb86a20s_readreg(state, 0x51);
992 if (rc < 0)
993 return rc;
994 counter = rc << 8;
995 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
996 if (rc < 0)
997 return rc;
998 rc = mb86a20s_readreg(state, 0x51);
999 if (rc < 0)
1000 return rc;
1001 counter |= rc;
1002 *count = counter * 204 * 8;
1003
1004 dev_dbg(&state->i2c->dev,
1005 "%s: post bit count for layer %c: %d.\n",
1006 __func__, 'A' + layer, *count);
1007
1008 /*
1009 * As we get TMCC data from the frontend, we can better estimate the
1010 * BER bit counters, in order to do the BER measure during a longer
1011 * time. Use those data, if available, to update the bit count
1012 * measure.
1013 */
1014
1015 if (!state->estimated_rate[layer])
1016 goto reset_measurement;
1017
1018 collect_rate = state->estimated_rate[layer] / 204 / 8;
1019 if (collect_rate < 32)
1020 collect_rate = 32;
1021 if (collect_rate > 65535)
1022 collect_rate = 65535;
1023 if (collect_rate != counter) {
1024 dev_dbg(&state->i2c->dev,
1025 "%s: updating postBER counter on layer %c to %d.\n",
1026 __func__, 'A' + layer, collect_rate);
1027
1028 /* Turn off BER after Viterbi */
1029 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1030
1031 /* Update counter for this layer */
1032 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1033 if (rc < 0)
1034 return rc;
1035 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1036 if (rc < 0)
1037 return rc;
1038 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1039 if (rc < 0)
1040 return rc;
1041 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1042 if (rc < 0)
1043 return rc;
1044
1045 /* Turn on BER after Viterbi */
1046 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1047
1048 /* Reset all preBER counters */
1049 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1050 if (rc < 0)
1051 return rc;
1052 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1053
1054 return rc;
1055 }
1056
1057reset_measurement:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001058 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001059 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001060 if (rc < 0)
1061 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001062 val = rc;
1063 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1064 if (rc < 0)
1065 return rc;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001066 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001067
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001068 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001069}
1070
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001071static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1072 unsigned layer,
1073 u32 *error, u32 *count)
1074{
1075 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001076 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001077 u32 collect_rate;
1078 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1079
1080 if (layer >= 3)
1081 return -EINVAL;
1082
1083 /* Check if the PER measures are already available */
1084 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1085 if (rc < 0)
1086 return rc;
1087 rc = mb86a20s_readreg(state, 0x51);
1088 if (rc < 0)
1089 return rc;
1090
1091 /* Check if data is available for that layer */
1092
1093 if (!(rc & (1 << layer))) {
1094 dev_dbg(&state->i2c->dev,
1095 "%s: block counts for layer %c aren't available yet.\n",
1096 __func__, 'A' + layer);
1097 return -EBUSY;
1098 }
1099
1100 /* Read Packet error Count */
1101 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1102 if (rc < 0)
1103 return rc;
1104 rc = mb86a20s_readreg(state, 0x51);
1105 if (rc < 0)
1106 return rc;
1107 *error = rc << 8;
1108 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1109 if (rc < 0)
1110 return rc;
1111 rc = mb86a20s_readreg(state, 0x51);
1112 if (rc < 0)
1113 return rc;
1114 *error |= rc;
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001115 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001116 __func__, 'A' + layer, *error);
1117
1118 /* Read Bit Count */
1119 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1120 if (rc < 0)
1121 return rc;
1122 rc = mb86a20s_readreg(state, 0x51);
1123 if (rc < 0)
1124 return rc;
1125 *count = rc << 8;
1126 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1127 if (rc < 0)
1128 return rc;
1129 rc = mb86a20s_readreg(state, 0x51);
1130 if (rc < 0)
1131 return rc;
1132 *count |= rc;
1133
1134 dev_dbg(&state->i2c->dev,
1135 "%s: block count for layer %c: %d.\n",
1136 __func__, 'A' + layer, *count);
1137
1138 /*
1139 * As we get TMCC data from the frontend, we can better estimate the
1140 * BER bit counters, in order to do the BER measure during a longer
1141 * time. Use those data, if available, to update the bit count
1142 * measure.
1143 */
1144
1145 if (!state->estimated_rate[layer])
1146 goto reset_measurement;
1147
1148 collect_rate = state->estimated_rate[layer] / 204 / 8;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001149 if (collect_rate < 32)
1150 collect_rate = 32;
1151 if (collect_rate > 65535)
1152 collect_rate = 65535;
1153
1154 if (collect_rate != *count) {
1155 dev_dbg(&state->i2c->dev,
1156 "%s: updating PER counter on layer %c to %d.\n",
1157 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001158
1159 /* Stop PER measurement */
1160 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1161 if (rc < 0)
1162 return rc;
1163 rc = mb86a20s_writereg(state, 0x51, 0x00);
1164 if (rc < 0)
1165 return rc;
1166
1167 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001168 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1169 if (rc < 0)
1170 return rc;
1171 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1172 if (rc < 0)
1173 return rc;
1174 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1175 if (rc < 0)
1176 return rc;
1177 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1178 if (rc < 0)
1179 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001180
1181 /* start PER measurement */
1182 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1183 if (rc < 0)
1184 return rc;
1185 rc = mb86a20s_writereg(state, 0x51, 0x07);
1186 if (rc < 0)
1187 return rc;
1188
1189 /* Reset all counters to collect new data */
1190 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1191 if (rc < 0)
1192 return rc;
1193 rc = mb86a20s_writereg(state, 0x51, 0x07);
1194 if (rc < 0)
1195 return rc;
1196 rc = mb86a20s_writereg(state, 0x51, 0x00);
1197
1198 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001199 }
1200
1201reset_measurement:
1202 /* Reset counter to collect new data */
1203 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1204 if (rc < 0)
1205 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001206 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001207 if (rc < 0)
1208 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001209 val = rc;
1210 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001211 if (rc < 0)
1212 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001213 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001214
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001215 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001216}
1217
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001218struct linear_segments {
1219 unsigned x, y;
1220};
1221
1222/*
1223 * All tables below return a dB/1000 measurement
1224 */
1225
1226static struct linear_segments cnr_to_db_table[] = {
1227 { 19648, 0},
1228 { 18187, 1000},
1229 { 16534, 2000},
1230 { 14823, 3000},
1231 { 13161, 4000},
1232 { 11622, 5000},
1233 { 10279, 6000},
1234 { 9089, 7000},
1235 { 8042, 8000},
1236 { 7137, 9000},
1237 { 6342, 10000},
1238 { 5641, 11000},
1239 { 5030, 12000},
1240 { 4474, 13000},
1241 { 3988, 14000},
1242 { 3556, 15000},
1243 { 3180, 16000},
1244 { 2841, 17000},
1245 { 2541, 18000},
1246 { 2276, 19000},
1247 { 2038, 20000},
1248 { 1800, 21000},
1249 { 1625, 22000},
1250 { 1462, 23000},
1251 { 1324, 24000},
1252 { 1175, 25000},
1253 { 1063, 26000},
1254 { 980, 27000},
1255 { 907, 28000},
1256 { 840, 29000},
1257 { 788, 30000},
1258};
1259
1260static struct linear_segments cnr_64qam_table[] = {
1261 { 3922688, 0},
1262 { 3920384, 1000},
1263 { 3902720, 2000},
1264 { 3894784, 3000},
1265 { 3882496, 4000},
1266 { 3872768, 5000},
1267 { 3858944, 6000},
1268 { 3851520, 7000},
1269 { 3838976, 8000},
1270 { 3829248, 9000},
1271 { 3818240, 10000},
1272 { 3806976, 11000},
1273 { 3791872, 12000},
1274 { 3767040, 13000},
1275 { 3720960, 14000},
1276 { 3637504, 15000},
1277 { 3498496, 16000},
1278 { 3296000, 17000},
1279 { 3031040, 18000},
1280 { 2715392, 19000},
1281 { 2362624, 20000},
1282 { 1963264, 21000},
1283 { 1649664, 22000},
1284 { 1366784, 23000},
1285 { 1120768, 24000},
1286 { 890880, 25000},
1287 { 723456, 26000},
1288 { 612096, 27000},
1289 { 518912, 28000},
1290 { 448256, 29000},
1291 { 388864, 30000},
1292};
1293
1294static struct linear_segments cnr_16qam_table[] = {
1295 { 5314816, 0},
1296 { 5219072, 1000},
1297 { 5118720, 2000},
1298 { 4998912, 3000},
1299 { 4875520, 4000},
1300 { 4736000, 5000},
1301 { 4604160, 6000},
1302 { 4458752, 7000},
1303 { 4300288, 8000},
1304 { 4092928, 9000},
1305 { 3836160, 10000},
1306 { 3521024, 11000},
1307 { 3155968, 12000},
1308 { 2756864, 13000},
1309 { 2347008, 14000},
1310 { 1955072, 15000},
1311 { 1593600, 16000},
1312 { 1297920, 17000},
1313 { 1043968, 18000},
1314 { 839680, 19000},
1315 { 672256, 20000},
1316 { 523008, 21000},
1317 { 424704, 22000},
1318 { 345088, 23000},
1319 { 280064, 24000},
1320 { 221440, 25000},
1321 { 179712, 26000},
1322 { 151040, 27000},
1323 { 128512, 28000},
1324 { 110080, 29000},
1325 { 95744, 30000},
1326};
1327
1328struct linear_segments cnr_qpsk_table[] = {
1329 { 2834176, 0},
1330 { 2683648, 1000},
1331 { 2536960, 2000},
1332 { 2391808, 3000},
1333 { 2133248, 4000},
1334 { 1906176, 5000},
1335 { 1666560, 6000},
1336 { 1422080, 7000},
1337 { 1189632, 8000},
1338 { 976384, 9000},
1339 { 790272, 10000},
1340 { 633344, 11000},
1341 { 505600, 12000},
1342 { 402944, 13000},
1343 { 320768, 14000},
1344 { 255488, 15000},
1345 { 204032, 16000},
1346 { 163072, 17000},
1347 { 130304, 18000},
1348 { 105216, 19000},
1349 { 83456, 20000},
1350 { 65024, 21000},
1351 { 52480, 22000},
1352 { 42752, 23000},
1353 { 34560, 24000},
1354 { 27136, 25000},
1355 { 22016, 26000},
1356 { 18432, 27000},
1357 { 15616, 28000},
1358 { 13312, 29000},
1359 { 11520, 30000},
1360};
1361
1362static u32 interpolate_value(u32 value, struct linear_segments *segments,
1363 unsigned len)
1364{
1365 u64 tmp64;
1366 u32 dx, dy;
1367 int i, ret;
1368
1369 if (value >= segments[0].x)
1370 return segments[0].y;
1371 if (value < segments[len-1].x)
1372 return segments[len-1].y;
1373
1374 for (i = 1; i < len - 1; i++) {
1375 /* If value is identical, no need to interpolate */
1376 if (value == segments[i].x)
1377 return segments[i].y;
1378 if (value > segments[i].x)
1379 break;
1380 }
1381
1382 /* Linear interpolation between the two (x,y) points */
1383 dy = segments[i].y - segments[i - 1].y;
1384 dx = segments[i - 1].x - segments[i].x;
1385 tmp64 = value - segments[i].x;
1386 tmp64 *= dy;
1387 do_div(tmp64, dx);
1388 ret = segments[i].y - tmp64;
1389
1390 return ret;
1391}
1392
1393static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1394{
1395 struct mb86a20s_state *state = fe->demodulator_priv;
1396 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1397 u32 cnr_linear, cnr;
1398 int rc, val;
1399
1400 /* Check if CNR is available */
1401 rc = mb86a20s_readreg(state, 0x45);
1402 if (rc < 0)
1403 return rc;
1404
1405 if (!(rc & 0x40)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001406 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001407 __func__);
1408 return -EBUSY;
1409 }
1410 val = rc;
1411
1412 rc = mb86a20s_readreg(state, 0x46);
1413 if (rc < 0)
1414 return rc;
1415 cnr_linear = rc << 8;
1416
1417 rc = mb86a20s_readreg(state, 0x46);
1418 if (rc < 0)
1419 return rc;
1420 cnr_linear |= rc;
1421
1422 cnr = interpolate_value(cnr_linear,
1423 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1424
1425 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1426 c->cnr.stat[0].svalue = cnr;
1427
1428 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1429 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1430
1431 /* CNR counter reset */
1432 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1433 if (rc < 0)
1434 return rc;
1435 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1436
1437 return rc;
1438}
1439
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001440static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001441{
1442 struct mb86a20s_state *state = fe->demodulator_priv;
1443 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1444 u32 mer, cnr;
1445 int rc, val, i;
1446 struct linear_segments *segs;
1447 unsigned segs_len;
1448
1449 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1450
1451 /* Check if the measures are already available */
1452 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1453 if (rc < 0)
1454 return rc;
1455 rc = mb86a20s_readreg(state, 0x51);
1456 if (rc < 0)
1457 return rc;
1458
1459 /* Check if data is available */
1460 if (!(rc & 0x01)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001461 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001462 "%s: MER measures aren't available yet.\n", __func__);
1463 return -EBUSY;
1464 }
1465
1466 /* Read all layers */
1467 for (i = 0; i < 3; i++) {
1468 if (!(c->isdbt_layer_enabled & (1 << i))) {
1469 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1470 continue;
1471 }
1472
1473 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1474 if (rc < 0)
1475 return rc;
1476 rc = mb86a20s_readreg(state, 0x51);
1477 if (rc < 0)
1478 return rc;
1479 mer = rc << 16;
1480 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1481 if (rc < 0)
1482 return rc;
1483 rc = mb86a20s_readreg(state, 0x51);
1484 if (rc < 0)
1485 return rc;
1486 mer |= rc << 8;
1487 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1488 if (rc < 0)
1489 return rc;
1490 rc = mb86a20s_readreg(state, 0x51);
1491 if (rc < 0)
1492 return rc;
1493 mer |= rc;
1494
1495 switch (c->layer[i].modulation) {
1496 case DQPSK:
1497 case QPSK:
1498 segs = cnr_qpsk_table;
1499 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1500 break;
1501 case QAM_16:
1502 segs = cnr_16qam_table;
1503 segs_len = ARRAY_SIZE(cnr_16qam_table);
1504 break;
1505 default:
1506 case QAM_64:
1507 segs = cnr_64qam_table;
1508 segs_len = ARRAY_SIZE(cnr_64qam_table);
1509 break;
1510 }
1511 cnr = interpolate_value(mer, segs, segs_len);
1512
1513 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1514 c->cnr.stat[1 + i].svalue = cnr;
1515
1516 dev_dbg(&state->i2c->dev,
1517 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1518 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1519
1520 }
1521
1522 /* Start a new MER measurement */
1523 /* MER counter reset */
1524 rc = mb86a20s_writereg(state, 0x50, 0x50);
1525 if (rc < 0)
1526 return rc;
1527 rc = mb86a20s_readreg(state, 0x51);
1528 if (rc < 0)
1529 return rc;
1530 val = rc;
1531
1532 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1533 if (rc < 0)
1534 return rc;
1535 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1536 if (rc < 0)
1537 return rc;
1538
1539 return 0;
1540}
1541
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001542static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1543{
1544 struct mb86a20s_state *state = fe->demodulator_priv;
1545 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1546 int i;
1547
1548 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1549
1550 /* Fill the length of each status counter */
1551
1552 /* Only global stats */
1553 c->strength.len = 1;
1554
1555 /* Per-layer stats - 3 layers + global */
1556 c->cnr.len = 4;
1557 c->pre_bit_error.len = 4;
1558 c->pre_bit_count.len = 4;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001559 c->post_bit_error.len = 4;
1560 c->post_bit_count.len = 4;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001561 c->block_error.len = 4;
1562 c->block_count.len = 4;
1563
1564 /* Signal is always available */
1565 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1566 c->strength.stat[0].uvalue = 0;
1567
1568 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1569 for (i = 0; i < 4; i++) {
1570 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1571 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1572 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001573 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1574 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001575 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1576 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1577 }
1578}
1579
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001580static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001581{
1582 struct mb86a20s_state *state = fe->demodulator_priv;
1583 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1584 int rc = 0, i;
1585 u32 bit_error = 0, bit_count = 0;
1586 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001587 u32 t_post_bit_error = 0, t_post_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001588 u32 block_error = 0, block_count = 0;
1589 u32 t_block_error = 0, t_block_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001590 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1591 int per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001592
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001593 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1594
1595 mb86a20s_get_main_CNR(fe);
1596
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001597 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001598 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001599
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001600 /*
1601 * At state 7, only CNR is available
1602 * For BER measures, state=9 is required
1603 * FIXME: we may get MER measures with state=8
1604 */
1605 if (status_nr < 9)
1606 return 0;
1607
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001608 for (i = 0; i < 3; i++) {
1609 if (c->isdbt_layer_enabled & (1 << i)) {
1610 /* Layer is active and has rc segments */
1611 active_layers++;
1612
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001613 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001614 rc = mb86a20s_get_pre_ber(fe, i,
1615 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001616 if (rc >= 0) {
1617 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1618 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1619 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1620 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1621 } else if (rc != -EBUSY) {
1622 /*
1623 * If an I/O error happened,
1624 * measures are now unavailable
1625 */
1626 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1627 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1628 dev_err(&state->i2c->dev,
1629 "%s: Can't get BER for layer %c (error %d).\n",
1630 __func__, 'A' + i, rc);
1631 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001632 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001633 pre_ber_layers++;
1634
1635 /* Handle BER post vterbi */
1636 rc = mb86a20s_get_post_ber(fe, i,
1637 &bit_error, &bit_count);
1638 if (rc >= 0) {
1639 c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1640 c->post_bit_error.stat[1 + i].uvalue += bit_error;
1641 c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1642 c->post_bit_count.stat[1 + i].uvalue += bit_count;
1643 } else if (rc != -EBUSY) {
1644 /*
1645 * If an I/O error happened,
1646 * measures are now unavailable
1647 */
1648 c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1649 c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1650 dev_err(&state->i2c->dev,
1651 "%s: Can't get BER for layer %c (error %d).\n",
1652 __func__, 'A' + i, rc);
1653 }
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001654 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1655 post_ber_layers++;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001656
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001657 /* Handle Block errors for PER/UCB reports */
1658 rc = mb86a20s_get_blk_error(fe, i,
1659 &block_error,
1660 &block_count);
1661 if (rc >= 0) {
1662 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1663 c->block_error.stat[1 + i].uvalue += block_error;
1664 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1665 c->block_count.stat[1 + i].uvalue += block_count;
1666 } else if (rc != -EBUSY) {
1667 /*
1668 * If an I/O error happened,
1669 * measures are now unavailable
1670 */
1671 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1672 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1673 dev_err(&state->i2c->dev,
1674 "%s: Can't get PER for layer %c (error %d).\n",
1675 __func__, 'A' + i, rc);
1676
1677 }
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001678 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1679 per_layers++;
1680
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001681 /* Update total preBER */
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001682 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1683 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001684
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001685 /* Update total postBER */
1686 t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
1687 t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
1688
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001689 /* Update total PER */
1690 t_block_error += c->block_error.stat[1 + i].uvalue;
1691 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001692 }
1693 }
1694
1695 /*
1696 * Start showing global count if at least one error count is
1697 * available.
1698 */
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001699 if (pre_ber_layers) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001700 /*
1701 * At least one per-layer BER measure was read. We can now
1702 * calculate the total BER
1703 *
1704 * Total Bit Error/Count is calculated as the sum of the
1705 * bit errors on all active layers.
1706 */
1707 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1708 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1709 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1710 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001711 } else {
1712 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1713 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001714 }
1715
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001716 /*
1717 * Start showing global count if at least one error count is
1718 * available.
1719 */
1720 if (post_ber_layers) {
1721 /*
1722 * At least one per-layer BER measure was read. We can now
1723 * calculate the total BER
1724 *
1725 * Total Bit Error/Count is calculated as the sum of the
1726 * bit errors on all active layers.
1727 */
1728 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1729 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1730 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1731 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001732 } else {
1733 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1734 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001735 }
1736
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001737 if (per_layers) {
1738 /*
1739 * At least one per-layer UCB measure was read. We can now
1740 * calculate the total UCB
1741 *
1742 * Total block Error/Count is calculated as the sum of the
1743 * block errors on all active layers.
1744 */
1745 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1746 c->block_error.stat[0].uvalue = t_block_error;
1747 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1748 c->block_count.stat[0].uvalue = t_block_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001749 } else {
1750 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1751 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001752 }
1753
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001754 return rc;
1755}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001756
1757/*
1758 * The functions below are called via DVB callbacks, so they need to
1759 * properly use the I2C gate control
1760 */
1761
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001762static int mb86a20s_initfe(struct dvb_frontend *fe)
1763{
1764 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001765 u64 pll;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001766 int rc;
1767 u8 regD5 = 1;
1768
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001769 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001770
1771 if (fe->ops.i2c_gate_ctrl)
1772 fe->ops.i2c_gate_ctrl(fe, 0);
1773
1774 /* Initialize the frontend */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001775 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001776 if (rc < 0)
1777 goto err;
1778
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001779 /* Adjust IF frequency to match tuner */
1780 if (fe->ops.tuner_ops.get_if_frequency)
1781 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1782
1783 if (!state->if_freq)
1784 state->if_freq = 3300000;
1785
1786 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1787 pll = state->if_freq * 1677721600L;
1788 do_div(pll, 1628571429L);
1789 rc = mb86a20s_writereg(state, 0x28, 0x20);
1790 if (rc < 0)
1791 goto err;
1792 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1793 if (rc < 0)
1794 goto err;
1795 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1796 if (rc < 0)
1797 goto err;
1798 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1799 if (rc < 0)
1800 goto err;
1801 dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n",
1802 __func__, state->if_freq, (long long)pll);
1803
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001804 if (!state->config->is_serial) {
1805 regD5 &= ~1;
1806
1807 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1808 if (rc < 0)
1809 goto err;
1810 rc = mb86a20s_writereg(state, 0x51, regD5);
1811 if (rc < 0)
1812 goto err;
1813 }
1814
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001815 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1816 if (rc < 0)
1817 goto err;
1818
1819
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001820err:
1821 if (fe->ops.i2c_gate_ctrl)
1822 fe->ops.i2c_gate_ctrl(fe, 1);
1823
1824 if (rc < 0) {
1825 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001826 dev_info(&state->i2c->dev,
1827 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001828 } else {
1829 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001830 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001831 }
1832 return rc;
1833}
1834
1835static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1836{
1837 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001838 int rc, if_freq;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001839#if 0
1840 /*
1841 * FIXME: Properly implement the set frontend properties
1842 */
1843 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1844#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001845 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001846
1847 /*
1848 * Gate should already be opened, but it doesn't hurt to
1849 * double-check
1850 */
1851 if (fe->ops.i2c_gate_ctrl)
1852 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001853 fe->ops.tuner_ops.set_params(fe);
1854
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001855 if (fe->ops.tuner_ops.get_if_frequency) {
1856 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1857
1858 /*
1859 * If the IF frequency changed, re-initialize the
1860 * frontend. This is needed by some drivers like tda18271,
1861 * that only sets the IF after receiving a set_params() call
1862 */
1863 if (if_freq != state->if_freq)
1864 state->need_init = true;
1865 }
1866
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001867 /*
1868 * Make it more reliable: if, for some reason, the initial
1869 * device initialization doesn't happen, initialize it when
1870 * a SBTVD parameters are adjusted.
1871 *
1872 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1873 * the agc callback logic is not called during DVB attach time,
1874 * causing mb86a20s to not be initialized with Kworld SBTVD.
1875 * So, this hack is needed, in order to make Kworld SBTVD to work.
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001876 *
1877 * It is also needed to change the IF after the initial init.
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001878 */
1879 if (state->need_init)
1880 mb86a20s_initfe(fe);
1881
1882 if (fe->ops.i2c_gate_ctrl)
1883 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001884
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001885 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001886 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001887
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001888 if (fe->ops.i2c_gate_ctrl)
1889 fe->ops.i2c_gate_ctrl(fe, 1);
1890
1891 return rc;
1892}
1893
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001894static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1895 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001896{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001897 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001898 int rc, status_nr;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001899
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001900 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001901
1902 if (fe->ops.i2c_gate_ctrl)
1903 fe->ops.i2c_gate_ctrl(fe, 0);
1904
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001905 /* Get lock */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001906 status_nr = mb86a20s_read_status(fe, status);
1907 if (status_nr < 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001908 mb86a20s_stats_not_ready(fe);
1909 mb86a20s_reset_frontend_cache(fe);
1910 }
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001911 if (status_nr < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001912 dev_err(&state->i2c->dev,
1913 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001914 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001915 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001916
1917 /* Get signal strength */
1918 rc = mb86a20s_read_signal_strength(fe);
1919 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001920 dev_err(&state->i2c->dev,
1921 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001922 mb86a20s_stats_not_ready(fe);
1923 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001924
1925 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001926 goto error;
1927 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001928
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001929 if (status_nr >= 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001930 /* Get TMCC info*/
1931 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001932 if (rc < 0) {
1933 dev_err(&state->i2c->dev,
1934 "%s: Can't get FE TMCC data.\n", __func__);
1935 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001936 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001937 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001938
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001939 /* Get statistics */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001940 rc = mb86a20s_get_stats(fe, status_nr);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001941 if (rc < 0 && rc != -EBUSY) {
1942 dev_err(&state->i2c->dev,
1943 "%s: Can't get FE statistics.\n", __func__);
1944 rc = 0;
1945 goto error;
1946 }
1947 rc = 0; /* Don't return EBUSY to userspace */
1948 }
1949 goto ok;
1950
1951error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001952 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001953
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001954ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001955 if (fe->ops.i2c_gate_ctrl)
1956 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001957
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001958 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001959}
1960
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001961static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
1962 u16 *strength)
1963{
1964 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1965
1966
1967 *strength = c->strength.stat[0].uvalue;
1968
1969 return 0;
1970}
1971
1972static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
1973{
1974 /*
1975 * get_frontend is now handled together with other stats
1976 * retrival, when read_status() is called, as some statistics
1977 * will depend on the layers detection.
1978 */
1979 return 0;
1980};
1981
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001982static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001983 bool re_tune,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001984 unsigned int mode_flags,
1985 unsigned int *delay,
1986 fe_status_t *status)
1987{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001988 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001989 int rc = 0;
1990
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001991 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001992
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001993 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001994 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001995
1996 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001997 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001998
1999 return rc;
2000}
2001
2002static void mb86a20s_release(struct dvb_frontend *fe)
2003{
2004 struct mb86a20s_state *state = fe->demodulator_priv;
2005
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002006 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002007
2008 kfree(state);
2009}
2010
2011static struct dvb_frontend_ops mb86a20s_ops;
2012
2013struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2014 struct i2c_adapter *i2c)
2015{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002016 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002017 u8 rev;
2018
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002019 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2020
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002021 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002022 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002023 if (state == NULL) {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002024 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002025 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002026 goto error;
2027 }
2028
2029 /* setup the state */
2030 state->config = config;
2031 state->i2c = i2c;
2032
2033 /* create dvb_frontend */
2034 memcpy(&state->frontend.ops, &mb86a20s_ops,
2035 sizeof(struct dvb_frontend_ops));
2036 state->frontend.demodulator_priv = state;
2037
2038 /* Check if it is a mb86a20s frontend */
2039 rev = mb86a20s_readreg(state, 0);
2040
2041 if (rev == 0x13) {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002042 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002043 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002044 } else {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002045 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002046 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002047 rev);
2048 goto error;
2049 }
2050
2051 return &state->frontend;
2052
2053error:
2054 kfree(state);
2055 return NULL;
2056}
2057EXPORT_SYMBOL(mb86a20s_attach);
2058
2059static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002060 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002061 /* Use dib8000 values per default */
2062 .info = {
2063 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002064 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
2065 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2066 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2067 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2068 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2069 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2070 /* Actually, those values depend on the used tuner */
2071 .frequency_min = 45000000,
2072 .frequency_max = 864000000,
2073 .frequency_stepsize = 62500,
2074 },
2075
2076 .release = mb86a20s_release,
2077
2078 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002079 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002080 .get_frontend = mb86a20s_get_frontend_dummy,
2081 .read_status = mb86a20s_read_status_and_stats,
2082 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002083 .tune = mb86a20s_tune,
2084};
2085
2086MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2087MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2088MODULE_LICENSE("GPL");