blob: 68a88180b631c040fbea9f443b19de492c1cbf87 [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 Chehab17e67d42013-03-01 15:20:25 -030058 { 0x50, 0xd1 }, { 0x51, 0x20 },
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 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030067 { 0x3c, 0x38 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -030068 { 0x01, 0x0d },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030069 { 0x04, 0x08 }, { 0x05, 0x03 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020070 { 0x04, 0x0e }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030071 { 0x04, 0x0f }, { 0x05, 0x37 },
72 { 0x04, 0x0b }, { 0x05, 0x78 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020073 { 0x04, 0x00 }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030074 { 0x04, 0x01 }, { 0x05, 0x1e },
75 { 0x04, 0x02 }, { 0x05, 0x07 },
76 { 0x04, 0x03 }, { 0x05, 0xd0 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020077 { 0x04, 0x09 }, { 0x05, 0x00 },
78 { 0x04, 0x0a }, { 0x05, 0xff },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030079 { 0x04, 0x27 }, { 0x05, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020080 { 0x04, 0x28 }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030081 { 0x04, 0x1e }, { 0x05, 0x00 },
82 { 0x04, 0x29 }, { 0x05, 0x64 },
83 { 0x04, 0x32 }, { 0x05, 0x02 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020084 { 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 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300150 { 0x50, 0xd7 }, { 0x51, 0xbf },
151 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xff },
152 { 0x28, 0x46 }, { 0x29, 0x00 }, { 0x2a, 0x1a }, { 0x2b, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300153
154 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300155 { 0x28, 0x00 }, { 0x2b, 0x08 },
156 { 0x28, 0x05 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300157 { 0x1c, 0x01 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -0300158 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x1f },
159 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x18 },
160 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x12 },
161 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x30 },
162 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x37 },
163 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
164 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x09 },
165 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x06 },
166 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7b },
167 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x76 },
168 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x7d },
169 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x08 },
170 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0b },
171 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
172 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf2 },
173 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xf3 },
174 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x05 },
175 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
176 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
177 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xef },
178 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xd8 },
179 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xf1 },
180 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x3d },
181 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x94 },
182 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0xba },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200183 { 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 Chehab17e67d42013-03-01 15:20:25 -0300191 { 0xec, 0x0f },
192 { 0xeb, 0x1f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200193 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300194 { 0xd0, 0x00 },
195};
196
197static struct regdata mb86a20s_reset_reception[] = {
198 { 0x70, 0xf0 },
199 { 0x70, 0xff },
200 { 0x08, 0x01 },
201 { 0x08, 0x00 },
202};
203
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300204static struct regdata mb86a20s_per_ber_reset[] = {
205 { 0x53, 0x00 }, /* pre BER Counter reset */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300206 { 0x53, 0x07 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300207
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300208 { 0x5f, 0x00 }, /* post BER Counter reset */
209 { 0x5f, 0x07 },
210
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300211 { 0x50, 0xb1 }, /* PER Counter reset */
212 { 0x51, 0x07 },
213 { 0x51, 0x00 },
214};
215
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200216/*
217 * I2C read/write functions and macros
218 */
219
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300220static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300221 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300222{
223 u8 buf[] = { reg, data };
224 struct i2c_msg msg = {
225 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
226 };
227 int rc;
228
229 rc = i2c_transfer(state->i2c, &msg, 1);
230 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200231 dev_err(&state->i2c->dev,
232 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
233 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300234 return rc;
235 }
236
237 return 0;
238}
239
240static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
241 u8 i2c_addr, struct regdata *rd, int size)
242{
243 int i, rc;
244
245 for (i = 0; i < size; i++) {
246 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
247 rd[i].data);
248 if (rc < 0)
249 return rc;
250 }
251 return 0;
252}
253
254static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
255 u8 i2c_addr, u8 reg)
256{
257 u8 val;
258 int rc;
259 struct i2c_msg msg[] = {
260 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
261 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
262 };
263
264 rc = i2c_transfer(state->i2c, msg, 2);
265
266 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200267 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
268 __func__, reg, rc);
269 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300270 }
271
272 return val;
273}
274
275#define mb86a20s_readreg(state, reg) \
276 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
277#define mb86a20s_writereg(state, reg, val) \
278 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
279#define mb86a20s_writeregdata(state, regdata) \
280 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
281 regdata, ARRAY_SIZE(regdata))
282
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300283/*
284 * Ancillary internal routines (likely compiled inlined)
285 *
286 * The functions below assume that gateway lock has already obtained
287 */
288
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200289static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300290{
291 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200292 int val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300293
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200294 *status = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300295
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200296 val = mb86a20s_readreg(state, 0x0a) & 0xf;
297 if (val < 0)
298 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300299
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200300 if (val >= 2)
301 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300302
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200303 if (val >= 4)
304 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300305
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200306 if (val >= 5)
307 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300308
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200309 if (val >= 7)
310 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300311
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200312 if (val >= 8) /* Maybe 9? */
313 *status |= FE_HAS_LOCK;
314
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200315 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
316 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200317
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -0300318 return val;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300319}
320
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300321static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300322{
323 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300324 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300325 int rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300326 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300327
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300328 if (state->get_strength_time &&
329 (!time_after(jiffies, state->get_strength_time)))
330 return c->strength.stat[0].uvalue;
331
332 /* Reset its value if an error happen */
333 c->strength.stat[0].uvalue = 0;
334
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300335 /* Does a binary search to get RF strength */
336 rf_max = 0xfff;
337 rf_min = 0;
338 do {
339 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300340 rc = mb86a20s_writereg(state, 0x04, 0x1f);
341 if (rc < 0)
342 return rc;
343 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
344 if (rc < 0)
345 return rc;
346 rc = mb86a20s_writereg(state, 0x04, 0x20);
347 if (rc < 0)
348 return rc;
Mauro Carvalho Chehabdad78c52013-03-01 16:15:16 -0300349 rc = mb86a20s_writereg(state, 0x05, rf);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300350 if (rc < 0)
351 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300352
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300353 rc = mb86a20s_readreg(state, 0x02);
354 if (rc < 0)
355 return rc;
356 if (rc & 0x08)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300357 rf_min = (rf_max + rf_min) / 2;
358 else
359 rf_max = (rf_max + rf_min) / 2;
360 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300361 rf = (rf_max + rf_min) / 2;
362
363 /* Rescale it from 2^12 (4096) to 2^16 */
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300364 rf = rf << (16 - 12);
365 if (rf)
366 rf |= (1 << 12) - 1;
367
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200368 dev_dbg(&state->i2c->dev,
369 "%s: signal strength = %d (%d < RF=%d < %d)\n",
370 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab0921ecf2013-03-02 10:15:30 -0300371 c->strength.stat[0].uvalue = rf;
372 state->get_strength_time = jiffies +
373 msecs_to_jiffies(1000);
374 return 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300375 }
376 } while (1);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300377}
378
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200379static int mb86a20s_get_modulation(struct mb86a20s_state *state,
380 unsigned layer)
381{
382 int rc;
383 static unsigned char reg[] = {
384 [0] = 0x86, /* Layer A */
385 [1] = 0x8a, /* Layer B */
386 [2] = 0x8e, /* Layer C */
387 };
388
Dan Carpenter82033bc2012-01-13 02:28:34 -0300389 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200390 return -EINVAL;
391 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
392 if (rc < 0)
393 return rc;
394 rc = mb86a20s_readreg(state, 0x6e);
395 if (rc < 0)
396 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300397 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200398 case 0:
399 return DQPSK;
400 case 1:
401 return QPSK;
402 case 2:
403 return QAM_16;
404 case 3:
405 return QAM_64;
406 default:
407 return QAM_AUTO;
408 }
409}
410
411static int mb86a20s_get_fec(struct mb86a20s_state *state,
412 unsigned layer)
413{
414 int rc;
415
416 static unsigned char reg[] = {
417 [0] = 0x87, /* Layer A */
418 [1] = 0x8b, /* Layer B */
419 [2] = 0x8f, /* Layer C */
420 };
421
Dan Carpenter82033bc2012-01-13 02:28:34 -0300422 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200423 return -EINVAL;
424 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
425 if (rc < 0)
426 return rc;
427 rc = mb86a20s_readreg(state, 0x6e);
428 if (rc < 0)
429 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300430 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200431 case 0:
432 return FEC_1_2;
433 case 1:
434 return FEC_2_3;
435 case 2:
436 return FEC_3_4;
437 case 3:
438 return FEC_5_6;
439 case 4:
440 return FEC_7_8;
441 default:
442 return FEC_AUTO;
443 }
444}
445
446static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
447 unsigned layer)
448{
449 int rc;
450
451 static unsigned char reg[] = {
452 [0] = 0x88, /* Layer A */
453 [1] = 0x8c, /* Layer B */
454 [2] = 0x90, /* Layer C */
455 };
456
Dan Carpenter82033bc2012-01-13 02:28:34 -0300457 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200458 return -EINVAL;
459 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
460 if (rc < 0)
461 return rc;
462 rc = mb86a20s_readreg(state, 0x6e);
463 if (rc < 0)
464 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300465
466 switch ((rc >> 4) & 0x07) {
467 case 1:
468 return GUARD_INTERVAL_1_4;
469 case 2:
470 return GUARD_INTERVAL_1_8;
471 case 3:
472 return GUARD_INTERVAL_1_16;
473 case 4:
474 return GUARD_INTERVAL_1_32;
475
476 default:
477 case 0:
478 return GUARD_INTERVAL_AUTO;
479 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200480}
481
482static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
483 unsigned layer)
484{
485 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200486 static unsigned char reg[] = {
487 [0] = 0x89, /* Layer A */
488 [1] = 0x8d, /* Layer B */
489 [2] = 0x91, /* Layer C */
490 };
491
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200492 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
493
Dan Carpenter82033bc2012-01-13 02:28:34 -0300494 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200495 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200496
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200497 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
498 if (rc < 0)
499 return rc;
500 rc = mb86a20s_readreg(state, 0x6e);
501 if (rc < 0)
502 return rc;
503 count = (rc >> 4) & 0x0f;
504
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200505 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
506
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200507 return count;
508}
509
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300510static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
511{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200512 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300513 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
514
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200515 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
516
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300517 /* Fixed parameters */
518 c->delivery_system = SYS_ISDBT;
519 c->bandwidth_hz = 6000000;
520
521 /* Initialize values that will be later autodetected */
522 c->isdbt_layer_enabled = 0;
523 c->transmission_mode = TRANSMISSION_MODE_AUTO;
524 c->guard_interval = GUARD_INTERVAL_AUTO;
525 c->isdbt_sb_mode = 0;
526 c->isdbt_sb_segment_count = 0;
527}
528
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300529/*
530 * Estimates the bit rate using the per-segment bit rate given by
531 * ABNT/NBR 15601 spec (table 4).
532 */
533static u32 isdbt_rate[3][5][4] = {
534 { /* DQPSK/QPSK */
535 { 280850, 312060, 330420, 340430 }, /* 1/2 */
536 { 374470, 416080, 440560, 453910 }, /* 2/3 */
537 { 421280, 468090, 495630, 510650 }, /* 3/4 */
538 { 468090, 520100, 550700, 567390 }, /* 5/6 */
539 { 491500, 546110, 578230, 595760 }, /* 7/8 */
540 }, { /* QAM16 */
541 { 561710, 624130, 660840, 680870 }, /* 1/2 */
542 { 748950, 832170, 881120, 907820 }, /* 2/3 */
543 { 842570, 936190, 991260, 1021300 }, /* 3/4 */
544 { 936190, 1040210, 1101400, 1134780 }, /* 5/6 */
545 { 983000, 1092220, 1156470, 1191520 }, /* 7/8 */
546 }, { /* QAM64 */
547 { 842570, 936190, 991260, 1021300 }, /* 1/2 */
548 { 1123430, 1248260, 1321680, 1361740 }, /* 2/3 */
549 { 1263860, 1404290, 1486900, 1531950 }, /* 3/4 */
550 { 1404290, 1560320, 1652110, 1702170 }, /* 5/6 */
551 { 1474500, 1638340, 1734710, 1787280 }, /* 7/8 */
552 }
553};
554
555static void mb86a20s_layer_bitrate(struct dvb_frontend *fe, u32 layer,
556 u32 modulation, u32 fec, u32 interleaving,
557 u32 segment)
558{
559 struct mb86a20s_state *state = fe->demodulator_priv;
560 u32 rate;
561 int m, f, i;
562
563 /*
564 * If modulation/fec/interleaving is not detected, the default is
565 * to consider the lowest bit rate, to avoid taking too long time
566 * to get BER.
567 */
568 switch (modulation) {
569 case DQPSK:
570 case QPSK:
571 default:
572 m = 0;
573 break;
574 case QAM_16:
575 m = 1;
576 break;
577 case QAM_64:
578 m = 2;
579 break;
580 }
581
582 switch (fec) {
583 default:
584 case FEC_1_2:
585 case FEC_AUTO:
586 f = 0;
587 break;
588 case FEC_2_3:
589 f = 1;
590 break;
591 case FEC_3_4:
592 f = 2;
593 break;
594 case FEC_5_6:
595 f = 3;
596 break;
597 case FEC_7_8:
598 f = 4;
599 break;
600 }
601
602 switch (interleaving) {
603 default:
604 case GUARD_INTERVAL_1_4:
605 i = 0;
606 break;
607 case GUARD_INTERVAL_1_8:
608 i = 1;
609 break;
610 case GUARD_INTERVAL_1_16:
611 i = 2;
612 break;
613 case GUARD_INTERVAL_1_32:
614 i = 3;
615 break;
616 }
617
618 /* Samples BER at BER_SAMPLING_RATE seconds */
619 rate = isdbt_rate[m][f][i] * segment * BER_SAMPLING_RATE;
620
621 /* Avoids sampling too quickly or to overflow the register */
622 if (rate < 256)
623 rate = 256;
624 else if (rate > (1 << 24) - 1)
625 rate = (1 << 24) - 1;
626
627 dev_dbg(&state->i2c->dev,
628 "%s: layer %c bitrate: %d kbps; counter = %d (0x%06x)\n",
629 __func__, 'A' + layer, segment * isdbt_rate[m][f][i]/1000,
630 rate, rate);
631
632 state->estimated_rate[i] = rate;
633}
634
635
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300636static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300637{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200638 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300639 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200640 int i, rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300641
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200642 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
643
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300644 /* Reset frontend cache to default values */
645 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200646
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200647 /* Check for partial reception */
648 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300649 if (rc < 0)
650 return rc;
651 rc = mb86a20s_readreg(state, 0x6e);
652 if (rc < 0)
653 return rc;
654 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200655
656 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300657
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200658 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200659 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
660 __func__, 'A' + i);
661
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200662 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300663 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200664 goto noperlayer_error;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300665 if (rc >= 0 && rc < 14) {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300666 c->layer[i].segment_count = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300667 } else {
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300668 c->layer[i].segment_count = 0;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300669 state->estimated_rate[i] = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200670 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300671 }
672 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200673 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300674 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200675 goto noperlayer_error;
676 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
677 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300678 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200679 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300680 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200681 goto noperlayer_error;
682 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
683 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300684 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200685 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300686 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200687 goto noperlayer_error;
688 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
689 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300690 c->layer[i].interleaving = rc;
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300691 mb86a20s_layer_bitrate(fe, i, c->layer[i].modulation,
692 c->layer[i].fec,
693 c->layer[i].interleaving,
694 c->layer[i].segment_count);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200695 }
696
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200697 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300698 if (rc < 0)
699 return rc;
700 if ((rc & 0x60) == 0x20) {
701 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200702 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300703 if (!c->isdbt_sb_segment_count)
704 c->isdbt_sb_segment_count = 1;
705 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200706
707 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200708 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300709 if (rc < 0)
710 return rc;
711 if ((rc & 0x60) == 0x20) {
712 switch (rc & 0x0c >> 2) {
713 case 0:
714 c->transmission_mode = TRANSMISSION_MODE_2K;
715 break;
716 case 1:
717 c->transmission_mode = TRANSMISSION_MODE_4K;
718 break;
719 case 2:
720 c->transmission_mode = TRANSMISSION_MODE_8K;
721 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200722 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300723 }
724 if (!(rc & 0x10)) {
725 switch (rc & 0x3) {
726 case 0:
727 c->guard_interval = GUARD_INTERVAL_1_4;
728 break;
729 case 1:
730 c->guard_interval = GUARD_INTERVAL_1_8;
731 break;
732 case 2:
733 c->guard_interval = GUARD_INTERVAL_1_16;
734 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200735 }
736 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300737 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200738
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200739noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300740
741 /* per-layer info is incomplete; discard all per-layer */
742 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300743
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300744 return rc;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -0300745}
746
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300747static int mb86a20s_reset_counters(struct dvb_frontend *fe)
748{
749 struct mb86a20s_state *state = fe->demodulator_priv;
750 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
751 int rc, val;
752
753 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
754
755 /* Reset the counters, if the channel changed */
756 if (state->last_frequency != c->frequency) {
757 memset(&c->strength, 0, sizeof(c->strength));
758 memset(&c->cnr, 0, sizeof(c->cnr));
759 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
760 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300761 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
762 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300763 memset(&c->block_error, 0, sizeof(c->block_error));
764 memset(&c->block_count, 0, sizeof(c->block_count));
765
766 state->last_frequency = c->frequency;
767 }
768
769 /* Clear status for most stats */
770
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300771 /* BER/PER counter reset */
772 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300773 if (rc < 0)
774 goto err;
775
776 /* CNR counter reset */
777 rc = mb86a20s_readreg(state, 0x45);
778 if (rc < 0)
779 goto err;
780 val = rc;
781 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
782 if (rc < 0)
783 goto err;
784 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
785 if (rc < 0)
786 goto err;
787
788 /* MER counter reset */
789 rc = mb86a20s_writereg(state, 0x50, 0x50);
790 if (rc < 0)
791 goto err;
792 rc = mb86a20s_readreg(state, 0x51);
793 if (rc < 0)
794 goto err;
795 val = rc;
796 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
797 if (rc < 0)
798 goto err;
799 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
800 if (rc < 0)
801 goto err;
802
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300803 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300804err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300805 dev_err(&state->i2c->dev,
806 "%s: Can't reset FE statistics (error %d).\n",
807 __func__, rc);
808ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300809 return rc;
810}
811
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300812static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
813 unsigned layer,
814 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300815{
816 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300817 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300818
819 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
820
821 if (layer >= 3)
822 return -EINVAL;
823
824 /* Check if the BER measures are already available */
825 rc = mb86a20s_readreg(state, 0x54);
826 if (rc < 0)
827 return rc;
828
829 /* Check if data is available for that layer */
830 if (!(rc & (1 << layer))) {
831 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300832 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300833 __func__, 'A' + layer);
834 return -EBUSY;
835 }
836
837 /* Read Bit Error Count */
838 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
839 if (rc < 0)
840 return rc;
841 *error = rc << 16;
842 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
843 if (rc < 0)
844 return rc;
845 *error |= rc << 8;
846 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
847 if (rc < 0)
848 return rc;
849 *error |= rc;
850
851 dev_dbg(&state->i2c->dev,
852 "%s: bit error before Viterbi for layer %c: %d.\n",
853 __func__, 'A' + layer, *error);
854
855 /* Read Bit Count */
856 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
857 if (rc < 0)
858 return rc;
859 rc = mb86a20s_readreg(state, 0x51);
860 if (rc < 0)
861 return rc;
862 *count = rc << 16;
863 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
864 if (rc < 0)
865 return rc;
866 rc = mb86a20s_readreg(state, 0x51);
867 if (rc < 0)
868 return rc;
869 *count |= rc << 8;
870 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
871 if (rc < 0)
872 return rc;
873 rc = mb86a20s_readreg(state, 0x51);
874 if (rc < 0)
875 return rc;
876 *count |= rc;
877
878 dev_dbg(&state->i2c->dev,
879 "%s: bit count before Viterbi for layer %c: %d.\n",
880 __func__, 'A' + layer, *count);
881
882
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300883 /*
884 * As we get TMCC data from the frontend, we can better estimate the
885 * BER bit counters, in order to do the BER measure during a longer
886 * time. Use those data, if available, to update the bit count
887 * measure.
888 */
889
890 if (state->estimated_rate[layer]
891 && state->estimated_rate[layer] != *count) {
892 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300893 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300894 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300895
896 /* Turn off BER before Viterbi */
897 rc = mb86a20s_writereg(state, 0x52, 0x00);
898
899 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300900 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
901 if (rc < 0)
902 return rc;
903 rc = mb86a20s_writereg(state, 0x51,
904 state->estimated_rate[layer] >> 16);
905 if (rc < 0)
906 return rc;
907 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
908 if (rc < 0)
909 return rc;
910 rc = mb86a20s_writereg(state, 0x51,
911 state->estimated_rate[layer] >> 8);
912 if (rc < 0)
913 return rc;
914 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
915 if (rc < 0)
916 return rc;
917 rc = mb86a20s_writereg(state, 0x51,
918 state->estimated_rate[layer]);
919 if (rc < 0)
920 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300921
922 /* Turn on BER before Viterbi */
923 rc = mb86a20s_writereg(state, 0x52, 0x01);
924
925 /* Reset all preBER counters */
926 rc = mb86a20s_writereg(state, 0x53, 0x00);
927 if (rc < 0)
928 return rc;
929 rc = mb86a20s_writereg(state, 0x53, 0x07);
930 } else {
931 /* Reset counter to collect new data */
932 rc = mb86a20s_readreg(state, 0x53);
933 if (rc < 0)
934 return rc;
935 val = rc;
936 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
937 if (rc < 0)
938 return rc;
939 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300940 }
941
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300942 return rc;
943}
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300944
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300945static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
946 unsigned layer,
947 u32 *error, u32 *count)
948{
949 struct mb86a20s_state *state = fe->demodulator_priv;
950 u32 counter, collect_rate;
951 int rc, val;
952
953 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
954
955 if (layer >= 3)
956 return -EINVAL;
957
958 /* Check if the BER measures are already available */
959 rc = mb86a20s_readreg(state, 0x60);
960 if (rc < 0)
961 return rc;
962
963 /* Check if data is available for that layer */
964 if (!(rc & (1 << layer))) {
965 dev_dbg(&state->i2c->dev,
966 "%s: post BER for layer %c is not available yet.\n",
967 __func__, 'A' + layer);
968 return -EBUSY;
969 }
970
971 /* Read Bit Error Count */
972 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
973 if (rc < 0)
974 return rc;
975 *error = rc << 16;
976 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
977 if (rc < 0)
978 return rc;
979 *error |= rc << 8;
980 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
981 if (rc < 0)
982 return rc;
983 *error |= rc;
984
985 dev_dbg(&state->i2c->dev,
986 "%s: post bit error for layer %c: %d.\n",
987 __func__, 'A' + layer, *error);
988
989 /* Read Bit Count */
990 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
991 if (rc < 0)
992 return rc;
993 rc = mb86a20s_readreg(state, 0x51);
994 if (rc < 0)
995 return rc;
996 counter = rc << 8;
997 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
998 if (rc < 0)
999 return rc;
1000 rc = mb86a20s_readreg(state, 0x51);
1001 if (rc < 0)
1002 return rc;
1003 counter |= rc;
1004 *count = counter * 204 * 8;
1005
1006 dev_dbg(&state->i2c->dev,
1007 "%s: post bit count for layer %c: %d.\n",
1008 __func__, 'A' + layer, *count);
1009
1010 /*
1011 * As we get TMCC data from the frontend, we can better estimate the
1012 * BER bit counters, in order to do the BER measure during a longer
1013 * time. Use those data, if available, to update the bit count
1014 * measure.
1015 */
1016
1017 if (!state->estimated_rate[layer])
1018 goto reset_measurement;
1019
1020 collect_rate = state->estimated_rate[layer] / 204 / 8;
1021 if (collect_rate < 32)
1022 collect_rate = 32;
1023 if (collect_rate > 65535)
1024 collect_rate = 65535;
1025 if (collect_rate != counter) {
1026 dev_dbg(&state->i2c->dev,
1027 "%s: updating postBER counter on layer %c to %d.\n",
1028 __func__, 'A' + layer, collect_rate);
1029
1030 /* Turn off BER after Viterbi */
1031 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1032
1033 /* Update counter for this layer */
1034 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1035 if (rc < 0)
1036 return rc;
1037 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1038 if (rc < 0)
1039 return rc;
1040 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1041 if (rc < 0)
1042 return rc;
1043 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1044 if (rc < 0)
1045 return rc;
1046
1047 /* Turn on BER after Viterbi */
1048 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1049
1050 /* Reset all preBER counters */
1051 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1052 if (rc < 0)
1053 return rc;
1054 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1055
1056 return rc;
1057 }
1058
1059reset_measurement:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001060 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001061 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001062 if (rc < 0)
1063 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001064 val = rc;
1065 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1066 if (rc < 0)
1067 return rc;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001068 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001069
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001070 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001071}
1072
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001073static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1074 unsigned layer,
1075 u32 *error, u32 *count)
1076{
1077 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001078 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001079 u32 collect_rate;
1080 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1081
1082 if (layer >= 3)
1083 return -EINVAL;
1084
1085 /* Check if the PER measures are already available */
1086 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1087 if (rc < 0)
1088 return rc;
1089 rc = mb86a20s_readreg(state, 0x51);
1090 if (rc < 0)
1091 return rc;
1092
1093 /* Check if data is available for that layer */
1094
1095 if (!(rc & (1 << layer))) {
1096 dev_dbg(&state->i2c->dev,
1097 "%s: block counts for layer %c aren't available yet.\n",
1098 __func__, 'A' + layer);
1099 return -EBUSY;
1100 }
1101
1102 /* Read Packet error Count */
1103 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1104 if (rc < 0)
1105 return rc;
1106 rc = mb86a20s_readreg(state, 0x51);
1107 if (rc < 0)
1108 return rc;
1109 *error = rc << 8;
1110 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1111 if (rc < 0)
1112 return rc;
1113 rc = mb86a20s_readreg(state, 0x51);
1114 if (rc < 0)
1115 return rc;
1116 *error |= rc;
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001117 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001118 __func__, 'A' + layer, *error);
1119
1120 /* Read Bit Count */
1121 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1122 if (rc < 0)
1123 return rc;
1124 rc = mb86a20s_readreg(state, 0x51);
1125 if (rc < 0)
1126 return rc;
1127 *count = rc << 8;
1128 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1129 if (rc < 0)
1130 return rc;
1131 rc = mb86a20s_readreg(state, 0x51);
1132 if (rc < 0)
1133 return rc;
1134 *count |= rc;
1135
1136 dev_dbg(&state->i2c->dev,
1137 "%s: block count for layer %c: %d.\n",
1138 __func__, 'A' + layer, *count);
1139
1140 /*
1141 * As we get TMCC data from the frontend, we can better estimate the
1142 * BER bit counters, in order to do the BER measure during a longer
1143 * time. Use those data, if available, to update the bit count
1144 * measure.
1145 */
1146
1147 if (!state->estimated_rate[layer])
1148 goto reset_measurement;
1149
1150 collect_rate = state->estimated_rate[layer] / 204 / 8;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001151 if (collect_rate < 32)
1152 collect_rate = 32;
1153 if (collect_rate > 65535)
1154 collect_rate = 65535;
1155
1156 if (collect_rate != *count) {
1157 dev_dbg(&state->i2c->dev,
1158 "%s: updating PER counter on layer %c to %d.\n",
1159 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001160
1161 /* Stop PER measurement */
1162 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1163 if (rc < 0)
1164 return rc;
1165 rc = mb86a20s_writereg(state, 0x51, 0x00);
1166 if (rc < 0)
1167 return rc;
1168
1169 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001170 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1171 if (rc < 0)
1172 return rc;
1173 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1174 if (rc < 0)
1175 return rc;
1176 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1177 if (rc < 0)
1178 return rc;
1179 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1180 if (rc < 0)
1181 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001182
1183 /* start PER measurement */
1184 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1185 if (rc < 0)
1186 return rc;
1187 rc = mb86a20s_writereg(state, 0x51, 0x07);
1188 if (rc < 0)
1189 return rc;
1190
1191 /* Reset all counters to collect new data */
1192 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1193 if (rc < 0)
1194 return rc;
1195 rc = mb86a20s_writereg(state, 0x51, 0x07);
1196 if (rc < 0)
1197 return rc;
1198 rc = mb86a20s_writereg(state, 0x51, 0x00);
1199
1200 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001201 }
1202
1203reset_measurement:
1204 /* Reset counter to collect new data */
1205 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1206 if (rc < 0)
1207 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001208 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001209 if (rc < 0)
1210 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001211 val = rc;
1212 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001213 if (rc < 0)
1214 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001215 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001216
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001217 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001218}
1219
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001220struct linear_segments {
1221 unsigned x, y;
1222};
1223
1224/*
1225 * All tables below return a dB/1000 measurement
1226 */
1227
1228static struct linear_segments cnr_to_db_table[] = {
1229 { 19648, 0},
1230 { 18187, 1000},
1231 { 16534, 2000},
1232 { 14823, 3000},
1233 { 13161, 4000},
1234 { 11622, 5000},
1235 { 10279, 6000},
1236 { 9089, 7000},
1237 { 8042, 8000},
1238 { 7137, 9000},
1239 { 6342, 10000},
1240 { 5641, 11000},
1241 { 5030, 12000},
1242 { 4474, 13000},
1243 { 3988, 14000},
1244 { 3556, 15000},
1245 { 3180, 16000},
1246 { 2841, 17000},
1247 { 2541, 18000},
1248 { 2276, 19000},
1249 { 2038, 20000},
1250 { 1800, 21000},
1251 { 1625, 22000},
1252 { 1462, 23000},
1253 { 1324, 24000},
1254 { 1175, 25000},
1255 { 1063, 26000},
1256 { 980, 27000},
1257 { 907, 28000},
1258 { 840, 29000},
1259 { 788, 30000},
1260};
1261
1262static struct linear_segments cnr_64qam_table[] = {
1263 { 3922688, 0},
1264 { 3920384, 1000},
1265 { 3902720, 2000},
1266 { 3894784, 3000},
1267 { 3882496, 4000},
1268 { 3872768, 5000},
1269 { 3858944, 6000},
1270 { 3851520, 7000},
1271 { 3838976, 8000},
1272 { 3829248, 9000},
1273 { 3818240, 10000},
1274 { 3806976, 11000},
1275 { 3791872, 12000},
1276 { 3767040, 13000},
1277 { 3720960, 14000},
1278 { 3637504, 15000},
1279 { 3498496, 16000},
1280 { 3296000, 17000},
1281 { 3031040, 18000},
1282 { 2715392, 19000},
1283 { 2362624, 20000},
1284 { 1963264, 21000},
1285 { 1649664, 22000},
1286 { 1366784, 23000},
1287 { 1120768, 24000},
1288 { 890880, 25000},
1289 { 723456, 26000},
1290 { 612096, 27000},
1291 { 518912, 28000},
1292 { 448256, 29000},
1293 { 388864, 30000},
1294};
1295
1296static struct linear_segments cnr_16qam_table[] = {
1297 { 5314816, 0},
1298 { 5219072, 1000},
1299 { 5118720, 2000},
1300 { 4998912, 3000},
1301 { 4875520, 4000},
1302 { 4736000, 5000},
1303 { 4604160, 6000},
1304 { 4458752, 7000},
1305 { 4300288, 8000},
1306 { 4092928, 9000},
1307 { 3836160, 10000},
1308 { 3521024, 11000},
1309 { 3155968, 12000},
1310 { 2756864, 13000},
1311 { 2347008, 14000},
1312 { 1955072, 15000},
1313 { 1593600, 16000},
1314 { 1297920, 17000},
1315 { 1043968, 18000},
1316 { 839680, 19000},
1317 { 672256, 20000},
1318 { 523008, 21000},
1319 { 424704, 22000},
1320 { 345088, 23000},
1321 { 280064, 24000},
1322 { 221440, 25000},
1323 { 179712, 26000},
1324 { 151040, 27000},
1325 { 128512, 28000},
1326 { 110080, 29000},
1327 { 95744, 30000},
1328};
1329
1330struct linear_segments cnr_qpsk_table[] = {
1331 { 2834176, 0},
1332 { 2683648, 1000},
1333 { 2536960, 2000},
1334 { 2391808, 3000},
1335 { 2133248, 4000},
1336 { 1906176, 5000},
1337 { 1666560, 6000},
1338 { 1422080, 7000},
1339 { 1189632, 8000},
1340 { 976384, 9000},
1341 { 790272, 10000},
1342 { 633344, 11000},
1343 { 505600, 12000},
1344 { 402944, 13000},
1345 { 320768, 14000},
1346 { 255488, 15000},
1347 { 204032, 16000},
1348 { 163072, 17000},
1349 { 130304, 18000},
1350 { 105216, 19000},
1351 { 83456, 20000},
1352 { 65024, 21000},
1353 { 52480, 22000},
1354 { 42752, 23000},
1355 { 34560, 24000},
1356 { 27136, 25000},
1357 { 22016, 26000},
1358 { 18432, 27000},
1359 { 15616, 28000},
1360 { 13312, 29000},
1361 { 11520, 30000},
1362};
1363
1364static u32 interpolate_value(u32 value, struct linear_segments *segments,
1365 unsigned len)
1366{
1367 u64 tmp64;
1368 u32 dx, dy;
1369 int i, ret;
1370
1371 if (value >= segments[0].x)
1372 return segments[0].y;
1373 if (value < segments[len-1].x)
1374 return segments[len-1].y;
1375
1376 for (i = 1; i < len - 1; i++) {
1377 /* If value is identical, no need to interpolate */
1378 if (value == segments[i].x)
1379 return segments[i].y;
1380 if (value > segments[i].x)
1381 break;
1382 }
1383
1384 /* Linear interpolation between the two (x,y) points */
1385 dy = segments[i].y - segments[i - 1].y;
1386 dx = segments[i - 1].x - segments[i].x;
1387 tmp64 = value - segments[i].x;
1388 tmp64 *= dy;
1389 do_div(tmp64, dx);
1390 ret = segments[i].y - tmp64;
1391
1392 return ret;
1393}
1394
1395static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1396{
1397 struct mb86a20s_state *state = fe->demodulator_priv;
1398 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1399 u32 cnr_linear, cnr;
1400 int rc, val;
1401
1402 /* Check if CNR is available */
1403 rc = mb86a20s_readreg(state, 0x45);
1404 if (rc < 0)
1405 return rc;
1406
1407 if (!(rc & 0x40)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001408 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001409 __func__);
1410 return -EBUSY;
1411 }
1412 val = rc;
1413
1414 rc = mb86a20s_readreg(state, 0x46);
1415 if (rc < 0)
1416 return rc;
1417 cnr_linear = rc << 8;
1418
1419 rc = mb86a20s_readreg(state, 0x46);
1420 if (rc < 0)
1421 return rc;
1422 cnr_linear |= rc;
1423
1424 cnr = interpolate_value(cnr_linear,
1425 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1426
1427 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1428 c->cnr.stat[0].svalue = cnr;
1429
1430 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1431 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1432
1433 /* CNR counter reset */
1434 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1435 if (rc < 0)
1436 return rc;
1437 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1438
1439 return rc;
1440}
1441
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001442static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001443{
1444 struct mb86a20s_state *state = fe->demodulator_priv;
1445 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1446 u32 mer, cnr;
1447 int rc, val, i;
1448 struct linear_segments *segs;
1449 unsigned segs_len;
1450
1451 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1452
1453 /* Check if the measures are already available */
1454 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1455 if (rc < 0)
1456 return rc;
1457 rc = mb86a20s_readreg(state, 0x51);
1458 if (rc < 0)
1459 return rc;
1460
1461 /* Check if data is available */
1462 if (!(rc & 0x01)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001463 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001464 "%s: MER measures aren't available yet.\n", __func__);
1465 return -EBUSY;
1466 }
1467
1468 /* Read all layers */
1469 for (i = 0; i < 3; i++) {
1470 if (!(c->isdbt_layer_enabled & (1 << i))) {
1471 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1472 continue;
1473 }
1474
1475 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1476 if (rc < 0)
1477 return rc;
1478 rc = mb86a20s_readreg(state, 0x51);
1479 if (rc < 0)
1480 return rc;
1481 mer = rc << 16;
1482 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1483 if (rc < 0)
1484 return rc;
1485 rc = mb86a20s_readreg(state, 0x51);
1486 if (rc < 0)
1487 return rc;
1488 mer |= rc << 8;
1489 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1490 if (rc < 0)
1491 return rc;
1492 rc = mb86a20s_readreg(state, 0x51);
1493 if (rc < 0)
1494 return rc;
1495 mer |= rc;
1496
1497 switch (c->layer[i].modulation) {
1498 case DQPSK:
1499 case QPSK:
1500 segs = cnr_qpsk_table;
1501 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1502 break;
1503 case QAM_16:
1504 segs = cnr_16qam_table;
1505 segs_len = ARRAY_SIZE(cnr_16qam_table);
1506 break;
1507 default:
1508 case QAM_64:
1509 segs = cnr_64qam_table;
1510 segs_len = ARRAY_SIZE(cnr_64qam_table);
1511 break;
1512 }
1513 cnr = interpolate_value(mer, segs, segs_len);
1514
1515 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1516 c->cnr.stat[1 + i].svalue = cnr;
1517
1518 dev_dbg(&state->i2c->dev,
1519 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1520 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1521
1522 }
1523
1524 /* Start a new MER measurement */
1525 /* MER counter reset */
1526 rc = mb86a20s_writereg(state, 0x50, 0x50);
1527 if (rc < 0)
1528 return rc;
1529 rc = mb86a20s_readreg(state, 0x51);
1530 if (rc < 0)
1531 return rc;
1532 val = rc;
1533
1534 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1535 if (rc < 0)
1536 return rc;
1537 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1538 if (rc < 0)
1539 return rc;
1540
1541 return 0;
1542}
1543
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001544static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1545{
1546 struct mb86a20s_state *state = fe->demodulator_priv;
1547 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1548 int i;
1549
1550 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1551
1552 /* Fill the length of each status counter */
1553
1554 /* Only global stats */
1555 c->strength.len = 1;
1556
1557 /* Per-layer stats - 3 layers + global */
1558 c->cnr.len = 4;
1559 c->pre_bit_error.len = 4;
1560 c->pre_bit_count.len = 4;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001561 c->post_bit_error.len = 4;
1562 c->post_bit_count.len = 4;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001563 c->block_error.len = 4;
1564 c->block_count.len = 4;
1565
1566 /* Signal is always available */
1567 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1568 c->strength.stat[0].uvalue = 0;
1569
1570 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1571 for (i = 0; i < 4; i++) {
1572 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1573 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1574 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001575 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1576 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001577 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1578 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1579 }
1580}
1581
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001582static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001583{
1584 struct mb86a20s_state *state = fe->demodulator_priv;
1585 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1586 int rc = 0, i;
1587 u32 bit_error = 0, bit_count = 0;
1588 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001589 u32 t_post_bit_error = 0, t_post_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001590 u32 block_error = 0, block_count = 0;
1591 u32 t_block_error = 0, t_block_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001592 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1593 int per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001594
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001595 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1596
1597 mb86a20s_get_main_CNR(fe);
1598
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001599 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001600 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001601
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001602 /*
1603 * At state 7, only CNR is available
1604 * For BER measures, state=9 is required
1605 * FIXME: we may get MER measures with state=8
1606 */
1607 if (status_nr < 9)
1608 return 0;
1609
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001610 for (i = 0; i < 3; i++) {
1611 if (c->isdbt_layer_enabled & (1 << i)) {
1612 /* Layer is active and has rc segments */
1613 active_layers++;
1614
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001615 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001616 rc = mb86a20s_get_pre_ber(fe, i,
1617 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001618 if (rc >= 0) {
1619 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1620 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1621 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1622 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1623 } else if (rc != -EBUSY) {
1624 /*
1625 * If an I/O error happened,
1626 * measures are now unavailable
1627 */
1628 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1629 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1630 dev_err(&state->i2c->dev,
1631 "%s: Can't get BER for layer %c (error %d).\n",
1632 __func__, 'A' + i, rc);
1633 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001634 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001635 pre_ber_layers++;
1636
1637 /* Handle BER post vterbi */
1638 rc = mb86a20s_get_post_ber(fe, i,
1639 &bit_error, &bit_count);
1640 if (rc >= 0) {
1641 c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1642 c->post_bit_error.stat[1 + i].uvalue += bit_error;
1643 c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1644 c->post_bit_count.stat[1 + i].uvalue += bit_count;
1645 } else if (rc != -EBUSY) {
1646 /*
1647 * If an I/O error happened,
1648 * measures are now unavailable
1649 */
1650 c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1651 c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1652 dev_err(&state->i2c->dev,
1653 "%s: Can't get BER for layer %c (error %d).\n",
1654 __func__, 'A' + i, rc);
1655 }
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001656 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1657 post_ber_layers++;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001658
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001659 /* Handle Block errors for PER/UCB reports */
1660 rc = mb86a20s_get_blk_error(fe, i,
1661 &block_error,
1662 &block_count);
1663 if (rc >= 0) {
1664 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1665 c->block_error.stat[1 + i].uvalue += block_error;
1666 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1667 c->block_count.stat[1 + i].uvalue += block_count;
1668 } else if (rc != -EBUSY) {
1669 /*
1670 * If an I/O error happened,
1671 * measures are now unavailable
1672 */
1673 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1674 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1675 dev_err(&state->i2c->dev,
1676 "%s: Can't get PER for layer %c (error %d).\n",
1677 __func__, 'A' + i, rc);
1678
1679 }
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001680 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1681 per_layers++;
1682
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001683 /* Update total preBER */
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001684 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1685 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001686
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001687 /* Update total postBER */
1688 t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
1689 t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
1690
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001691 /* Update total PER */
1692 t_block_error += c->block_error.stat[1 + i].uvalue;
1693 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001694 }
1695 }
1696
1697 /*
1698 * Start showing global count if at least one error count is
1699 * available.
1700 */
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001701 if (pre_ber_layers) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001702 /*
1703 * At least one per-layer BER measure was read. We can now
1704 * calculate the total BER
1705 *
1706 * Total Bit Error/Count is calculated as the sum of the
1707 * bit errors on all active layers.
1708 */
1709 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1710 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1711 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1712 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001713 } else {
1714 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1715 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001716 }
1717
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001718 /*
1719 * Start showing global count if at least one error count is
1720 * available.
1721 */
1722 if (post_ber_layers) {
1723 /*
1724 * At least one per-layer BER measure was read. We can now
1725 * calculate the total BER
1726 *
1727 * Total Bit Error/Count is calculated as the sum of the
1728 * bit errors on all active layers.
1729 */
1730 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1731 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1732 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1733 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001734 } else {
1735 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1736 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001737 }
1738
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001739 if (per_layers) {
1740 /*
1741 * At least one per-layer UCB measure was read. We can now
1742 * calculate the total UCB
1743 *
1744 * Total block Error/Count is calculated as the sum of the
1745 * block errors on all active layers.
1746 */
1747 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1748 c->block_error.stat[0].uvalue = t_block_error;
1749 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1750 c->block_count.stat[0].uvalue = t_block_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001751 } else {
1752 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1753 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001754 }
1755
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001756 return rc;
1757}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001758
1759/*
1760 * The functions below are called via DVB callbacks, so they need to
1761 * properly use the I2C gate control
1762 */
1763
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001764static int mb86a20s_initfe(struct dvb_frontend *fe)
1765{
1766 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001767 u64 pll;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001768 int rc;
1769 u8 regD5 = 1;
1770
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001771 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001772
1773 if (fe->ops.i2c_gate_ctrl)
1774 fe->ops.i2c_gate_ctrl(fe, 0);
1775
1776 /* Initialize the frontend */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001777 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001778 if (rc < 0)
1779 goto err;
1780
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001781 /* Adjust IF frequency to match tuner */
1782 if (fe->ops.tuner_ops.get_if_frequency)
1783 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1784
1785 if (!state->if_freq)
1786 state->if_freq = 3300000;
1787
1788 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1789 pll = state->if_freq * 1677721600L;
1790 do_div(pll, 1628571429L);
1791 rc = mb86a20s_writereg(state, 0x28, 0x20);
1792 if (rc < 0)
1793 goto err;
1794 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1795 if (rc < 0)
1796 goto err;
1797 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1798 if (rc < 0)
1799 goto err;
1800 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1801 if (rc < 0)
1802 goto err;
1803 dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n",
1804 __func__, state->if_freq, (long long)pll);
1805
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001806 if (!state->config->is_serial) {
1807 regD5 &= ~1;
1808
1809 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1810 if (rc < 0)
1811 goto err;
1812 rc = mb86a20s_writereg(state, 0x51, regD5);
1813 if (rc < 0)
1814 goto err;
1815 }
1816
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001817 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1818 if (rc < 0)
1819 goto err;
1820
1821
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001822err:
1823 if (fe->ops.i2c_gate_ctrl)
1824 fe->ops.i2c_gate_ctrl(fe, 1);
1825
1826 if (rc < 0) {
1827 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001828 dev_info(&state->i2c->dev,
1829 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001830 } else {
1831 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001832 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001833 }
1834 return rc;
1835}
1836
1837static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1838{
1839 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001840 int rc, if_freq;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001841#if 0
1842 /*
1843 * FIXME: Properly implement the set frontend properties
1844 */
1845 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1846#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001847 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001848
1849 /*
1850 * Gate should already be opened, but it doesn't hurt to
1851 * double-check
1852 */
1853 if (fe->ops.i2c_gate_ctrl)
1854 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001855 fe->ops.tuner_ops.set_params(fe);
1856
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001857 if (fe->ops.tuner_ops.get_if_frequency) {
1858 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1859
1860 /*
1861 * If the IF frequency changed, re-initialize the
1862 * frontend. This is needed by some drivers like tda18271,
1863 * that only sets the IF after receiving a set_params() call
1864 */
1865 if (if_freq != state->if_freq)
1866 state->need_init = true;
1867 }
1868
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001869 /*
1870 * Make it more reliable: if, for some reason, the initial
1871 * device initialization doesn't happen, initialize it when
1872 * a SBTVD parameters are adjusted.
1873 *
1874 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1875 * the agc callback logic is not called during DVB attach time,
1876 * causing mb86a20s to not be initialized with Kworld SBTVD.
1877 * So, this hack is needed, in order to make Kworld SBTVD to work.
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001878 *
1879 * It is also needed to change the IF after the initial init.
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001880 */
1881 if (state->need_init)
1882 mb86a20s_initfe(fe);
1883
1884 if (fe->ops.i2c_gate_ctrl)
1885 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001886
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001887 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001888 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001889
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001890 if (fe->ops.i2c_gate_ctrl)
1891 fe->ops.i2c_gate_ctrl(fe, 1);
1892
1893 return rc;
1894}
1895
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001896static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1897 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001898{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001899 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001900 int rc, status_nr;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001901
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001902 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001903
1904 if (fe->ops.i2c_gate_ctrl)
1905 fe->ops.i2c_gate_ctrl(fe, 0);
1906
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001907 /* Get lock */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001908 status_nr = mb86a20s_read_status(fe, status);
1909 if (status_nr < 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001910 mb86a20s_stats_not_ready(fe);
1911 mb86a20s_reset_frontend_cache(fe);
1912 }
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001913 if (status_nr < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001914 dev_err(&state->i2c->dev,
1915 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001916 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001917 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001918
1919 /* Get signal strength */
1920 rc = mb86a20s_read_signal_strength(fe);
1921 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001922 dev_err(&state->i2c->dev,
1923 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001924 mb86a20s_stats_not_ready(fe);
1925 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001926
1927 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001928 goto error;
1929 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001930
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001931 if (status_nr >= 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001932 /* Get TMCC info*/
1933 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001934 if (rc < 0) {
1935 dev_err(&state->i2c->dev,
1936 "%s: Can't get FE TMCC data.\n", __func__);
1937 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001938 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001939 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001940
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001941 /* Get statistics */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001942 rc = mb86a20s_get_stats(fe, status_nr);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001943 if (rc < 0 && rc != -EBUSY) {
1944 dev_err(&state->i2c->dev,
1945 "%s: Can't get FE statistics.\n", __func__);
1946 rc = 0;
1947 goto error;
1948 }
1949 rc = 0; /* Don't return EBUSY to userspace */
1950 }
1951 goto ok;
1952
1953error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001954 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001955
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001956ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001957 if (fe->ops.i2c_gate_ctrl)
1958 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001959
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001960 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001961}
1962
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001963static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
1964 u16 *strength)
1965{
1966 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1967
1968
1969 *strength = c->strength.stat[0].uvalue;
1970
1971 return 0;
1972}
1973
1974static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
1975{
1976 /*
1977 * get_frontend is now handled together with other stats
1978 * retrival, when read_status() is called, as some statistics
1979 * will depend on the layers detection.
1980 */
1981 return 0;
1982};
1983
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001984static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001985 bool re_tune,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001986 unsigned int mode_flags,
1987 unsigned int *delay,
1988 fe_status_t *status)
1989{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001990 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001991 int rc = 0;
1992
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001993 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001994
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001995 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001996 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03001997
1998 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001999 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002000
2001 return rc;
2002}
2003
2004static void mb86a20s_release(struct dvb_frontend *fe)
2005{
2006 struct mb86a20s_state *state = fe->demodulator_priv;
2007
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002008 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002009
2010 kfree(state);
2011}
2012
2013static struct dvb_frontend_ops mb86a20s_ops;
2014
2015struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2016 struct i2c_adapter *i2c)
2017{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002018 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002019 u8 rev;
2020
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002021 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2022
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002023 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002024 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002025 if (state == NULL) {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002026 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002027 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002028 goto error;
2029 }
2030
2031 /* setup the state */
2032 state->config = config;
2033 state->i2c = i2c;
2034
2035 /* create dvb_frontend */
2036 memcpy(&state->frontend.ops, &mb86a20s_ops,
2037 sizeof(struct dvb_frontend_ops));
2038 state->frontend.demodulator_priv = state;
2039
2040 /* Check if it is a mb86a20s frontend */
2041 rev = mb86a20s_readreg(state, 0);
2042
2043 if (rev == 0x13) {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002044 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002045 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002046 } else {
Mauro Carvalho Chehabf167e3022013-01-23 13:22:22 -02002047 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002048 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002049 rev);
2050 goto error;
2051 }
2052
2053 return &state->frontend;
2054
2055error:
2056 kfree(state);
2057 return NULL;
2058}
2059EXPORT_SYMBOL(mb86a20s_attach);
2060
2061static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002062 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002063 /* Use dib8000 values per default */
2064 .info = {
2065 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002066 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
2067 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2068 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2069 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2070 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2071 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2072 /* Actually, those values depend on the used tuner */
2073 .frequency_min = 45000000,
2074 .frequency_max = 864000000,
2075 .frequency_stepsize = 62500,
2076 },
2077
2078 .release = mb86a20s_release,
2079
2080 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002081 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002082 .get_frontend = mb86a20s_get_frontend_dummy,
2083 .read_status = mb86a20s_read_status_and_stats,
2084 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede79a2010-09-27 20:52:43 -03002085 .tune = mb86a20s_tune,
2086};
2087
2088MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2089MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2090MODULE_LICENSE("GPL");