blob: 1c135aa9df44ffbcce15f16fec9829b5fcab5129 [file] [log] [blame]
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001/*
2 * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
3 *
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -03004 * Copyright (C) 2010-2013 Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03005 * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
6 *
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03007 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation version 2.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17#include <linux/kernel.h>
18#include <asm/div64.h>
19
20#include "dvb_frontend.h"
21#include "mb86a20s.h"
22
23static int debug = 1;
24module_param(debug, int, 0644);
25MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
26
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030027struct mb86a20s_state {
28 struct i2c_adapter *i2c;
29 const struct mb86a20s_config *config;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030030 u32 last_frequency;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030031
32 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030033
Mauro Carvalho 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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-09-27 20:52:43 -030066 { 0x3b, 0x21 },
Mauro Carvalho Chehab17e67d42013-03-01 15:20:25 -030067 { 0x3c, 0x38 },
Mauro Carvalho Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-09-27 20:52:43 -0300293
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200294 *status = 0;
Mauro Carvalho Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-09-27 20:52:43 -0300375 }
376 } while (1);
Mauro Carvalho Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-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 Chehabb9ede792010-09-27 20:52:43 -0300743
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300744 return rc;
Mauro Carvalho Chehabb9ede792010-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) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300757 memset(&c->cnr, 0, sizeof(c->cnr));
758 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
759 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300760 memset(&c->post_bit_error, 0, sizeof(c->post_bit_error));
761 memset(&c->post_bit_count, 0, sizeof(c->post_bit_count));
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300762 memset(&c->block_error, 0, sizeof(c->block_error));
763 memset(&c->block_count, 0, sizeof(c->block_count));
764
765 state->last_frequency = c->frequency;
766 }
767
768 /* Clear status for most stats */
769
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300770 /* BER/PER counter reset */
771 rc = mb86a20s_writeregdata(state, mb86a20s_per_ber_reset);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300772 if (rc < 0)
773 goto err;
774
775 /* CNR counter reset */
776 rc = mb86a20s_readreg(state, 0x45);
777 if (rc < 0)
778 goto err;
779 val = rc;
780 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
781 if (rc < 0)
782 goto err;
783 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
784 if (rc < 0)
785 goto err;
786
787 /* MER counter reset */
788 rc = mb86a20s_writereg(state, 0x50, 0x50);
789 if (rc < 0)
790 goto err;
791 rc = mb86a20s_readreg(state, 0x51);
792 if (rc < 0)
793 goto err;
794 val = rc;
795 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
796 if (rc < 0)
797 goto err;
798 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
799 if (rc < 0)
800 goto err;
801
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300802 goto ok;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300803err:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300804 dev_err(&state->i2c->dev,
805 "%s: Can't reset FE statistics (error %d).\n",
806 __func__, rc);
807ok:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300808 return rc;
809}
810
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300811static int mb86a20s_get_pre_ber(struct dvb_frontend *fe,
812 unsigned layer,
813 u32 *error, u32 *count)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300814{
815 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300816 int rc, val;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300817
818 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
819
820 if (layer >= 3)
821 return -EINVAL;
822
823 /* Check if the BER measures are already available */
824 rc = mb86a20s_readreg(state, 0x54);
825 if (rc < 0)
826 return rc;
827
828 /* Check if data is available for that layer */
829 if (!(rc & (1 << layer))) {
830 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300831 "%s: preBER for layer %c is not available yet.\n",
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -0300832 __func__, 'A' + layer);
833 return -EBUSY;
834 }
835
836 /* Read Bit Error Count */
837 rc = mb86a20s_readreg(state, 0x55 + layer * 3);
838 if (rc < 0)
839 return rc;
840 *error = rc << 16;
841 rc = mb86a20s_readreg(state, 0x56 + layer * 3);
842 if (rc < 0)
843 return rc;
844 *error |= rc << 8;
845 rc = mb86a20s_readreg(state, 0x57 + layer * 3);
846 if (rc < 0)
847 return rc;
848 *error |= rc;
849
850 dev_dbg(&state->i2c->dev,
851 "%s: bit error before Viterbi for layer %c: %d.\n",
852 __func__, 'A' + layer, *error);
853
854 /* Read Bit Count */
855 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
856 if (rc < 0)
857 return rc;
858 rc = mb86a20s_readreg(state, 0x51);
859 if (rc < 0)
860 return rc;
861 *count = rc << 16;
862 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
863 if (rc < 0)
864 return rc;
865 rc = mb86a20s_readreg(state, 0x51);
866 if (rc < 0)
867 return rc;
868 *count |= rc << 8;
869 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
870 if (rc < 0)
871 return rc;
872 rc = mb86a20s_readreg(state, 0x51);
873 if (rc < 0)
874 return rc;
875 *count |= rc;
876
877 dev_dbg(&state->i2c->dev,
878 "%s: bit count before Viterbi for layer %c: %d.\n",
879 __func__, 'A' + layer, *count);
880
881
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300882 /*
883 * As we get TMCC data from the frontend, we can better estimate the
884 * BER bit counters, in order to do the BER measure during a longer
885 * time. Use those data, if available, to update the bit count
886 * measure.
887 */
888
889 if (state->estimated_rate[layer]
890 && state->estimated_rate[layer] != *count) {
891 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300892 "%s: updating layer %c preBER counter to %d.\n",
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300893 __func__, 'A' + layer, state->estimated_rate[layer]);
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300894
895 /* Turn off BER before Viterbi */
896 rc = mb86a20s_writereg(state, 0x52, 0x00);
897
898 /* Update counter for this layer */
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300899 rc = mb86a20s_writereg(state, 0x50, 0xa7 + layer * 3);
900 if (rc < 0)
901 return rc;
902 rc = mb86a20s_writereg(state, 0x51,
903 state->estimated_rate[layer] >> 16);
904 if (rc < 0)
905 return rc;
906 rc = mb86a20s_writereg(state, 0x50, 0xa8 + layer * 3);
907 if (rc < 0)
908 return rc;
909 rc = mb86a20s_writereg(state, 0x51,
910 state->estimated_rate[layer] >> 8);
911 if (rc < 0)
912 return rc;
913 rc = mb86a20s_writereg(state, 0x50, 0xa9 + layer * 3);
914 if (rc < 0)
915 return rc;
916 rc = mb86a20s_writereg(state, 0x51,
917 state->estimated_rate[layer]);
918 if (rc < 0)
919 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -0300920
921 /* Turn on BER before Viterbi */
922 rc = mb86a20s_writereg(state, 0x52, 0x01);
923
924 /* Reset all preBER counters */
925 rc = mb86a20s_writereg(state, 0x53, 0x00);
926 if (rc < 0)
927 return rc;
928 rc = mb86a20s_writereg(state, 0x53, 0x07);
929 } else {
930 /* Reset counter to collect new data */
931 rc = mb86a20s_readreg(state, 0x53);
932 if (rc < 0)
933 return rc;
934 val = rc;
935 rc = mb86a20s_writereg(state, 0x53, val & ~(1 << layer));
936 if (rc < 0)
937 return rc;
938 rc = mb86a20s_writereg(state, 0x53, val | (1 << layer));
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300939 }
940
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300941 return rc;
942}
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -0300943
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -0300944static int mb86a20s_get_post_ber(struct dvb_frontend *fe,
945 unsigned layer,
946 u32 *error, u32 *count)
947{
948 struct mb86a20s_state *state = fe->demodulator_priv;
949 u32 counter, collect_rate;
950 int rc, val;
951
952 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
953
954 if (layer >= 3)
955 return -EINVAL;
956
957 /* Check if the BER measures are already available */
958 rc = mb86a20s_readreg(state, 0x60);
959 if (rc < 0)
960 return rc;
961
962 /* Check if data is available for that layer */
963 if (!(rc & (1 << layer))) {
964 dev_dbg(&state->i2c->dev,
965 "%s: post BER for layer %c is not available yet.\n",
966 __func__, 'A' + layer);
967 return -EBUSY;
968 }
969
970 /* Read Bit Error Count */
971 rc = mb86a20s_readreg(state, 0x64 + layer * 3);
972 if (rc < 0)
973 return rc;
974 *error = rc << 16;
975 rc = mb86a20s_readreg(state, 0x65 + layer * 3);
976 if (rc < 0)
977 return rc;
978 *error |= rc << 8;
979 rc = mb86a20s_readreg(state, 0x66 + layer * 3);
980 if (rc < 0)
981 return rc;
982 *error |= rc;
983
984 dev_dbg(&state->i2c->dev,
985 "%s: post bit error for layer %c: %d.\n",
986 __func__, 'A' + layer, *error);
987
988 /* Read Bit Count */
989 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
990 if (rc < 0)
991 return rc;
992 rc = mb86a20s_readreg(state, 0x51);
993 if (rc < 0)
994 return rc;
995 counter = rc << 8;
996 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
997 if (rc < 0)
998 return rc;
999 rc = mb86a20s_readreg(state, 0x51);
1000 if (rc < 0)
1001 return rc;
1002 counter |= rc;
1003 *count = counter * 204 * 8;
1004
1005 dev_dbg(&state->i2c->dev,
1006 "%s: post bit count for layer %c: %d.\n",
1007 __func__, 'A' + layer, *count);
1008
1009 /*
1010 * As we get TMCC data from the frontend, we can better estimate the
1011 * BER bit counters, in order to do the BER measure during a longer
1012 * time. Use those data, if available, to update the bit count
1013 * measure.
1014 */
1015
1016 if (!state->estimated_rate[layer])
1017 goto reset_measurement;
1018
1019 collect_rate = state->estimated_rate[layer] / 204 / 8;
1020 if (collect_rate < 32)
1021 collect_rate = 32;
1022 if (collect_rate > 65535)
1023 collect_rate = 65535;
1024 if (collect_rate != counter) {
1025 dev_dbg(&state->i2c->dev,
1026 "%s: updating postBER counter on layer %c to %d.\n",
1027 __func__, 'A' + layer, collect_rate);
1028
1029 /* Turn off BER after Viterbi */
1030 rc = mb86a20s_writereg(state, 0x5e, 0x00);
1031
1032 /* Update counter for this layer */
1033 rc = mb86a20s_writereg(state, 0x50, 0xdc + layer * 2);
1034 if (rc < 0)
1035 return rc;
1036 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1037 if (rc < 0)
1038 return rc;
1039 rc = mb86a20s_writereg(state, 0x50, 0xdd + layer * 2);
1040 if (rc < 0)
1041 return rc;
1042 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1043 if (rc < 0)
1044 return rc;
1045
1046 /* Turn on BER after Viterbi */
1047 rc = mb86a20s_writereg(state, 0x5e, 0x07);
1048
1049 /* Reset all preBER counters */
1050 rc = mb86a20s_writereg(state, 0x5f, 0x00);
1051 if (rc < 0)
1052 return rc;
1053 rc = mb86a20s_writereg(state, 0x5f, 0x07);
1054
1055 return rc;
1056 }
1057
1058reset_measurement:
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001059 /* Reset counter to collect new data */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001060 rc = mb86a20s_readreg(state, 0x5f);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001061 if (rc < 0)
1062 return rc;
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001063 val = rc;
1064 rc = mb86a20s_writereg(state, 0x5f, val & ~(1 << layer));
1065 if (rc < 0)
1066 return rc;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001067 rc = mb86a20s_writereg(state, 0x5f, val | (1 << layer));
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001068
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001069 return rc;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001070}
1071
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001072static int mb86a20s_get_blk_error(struct dvb_frontend *fe,
1073 unsigned layer,
1074 u32 *error, u32 *count)
1075{
1076 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001077 int rc, val;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001078 u32 collect_rate;
1079 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1080
1081 if (layer >= 3)
1082 return -EINVAL;
1083
1084 /* Check if the PER measures are already available */
1085 rc = mb86a20s_writereg(state, 0x50, 0xb8);
1086 if (rc < 0)
1087 return rc;
1088 rc = mb86a20s_readreg(state, 0x51);
1089 if (rc < 0)
1090 return rc;
1091
1092 /* Check if data is available for that layer */
1093
1094 if (!(rc & (1 << layer))) {
1095 dev_dbg(&state->i2c->dev,
1096 "%s: block counts for layer %c aren't available yet.\n",
1097 __func__, 'A' + layer);
1098 return -EBUSY;
1099 }
1100
1101 /* Read Packet error Count */
1102 rc = mb86a20s_writereg(state, 0x50, 0xb9 + layer * 2);
1103 if (rc < 0)
1104 return rc;
1105 rc = mb86a20s_readreg(state, 0x51);
1106 if (rc < 0)
1107 return rc;
1108 *error = rc << 8;
1109 rc = mb86a20s_writereg(state, 0x50, 0xba + layer * 2);
1110 if (rc < 0)
1111 return rc;
1112 rc = mb86a20s_readreg(state, 0x51);
1113 if (rc < 0)
1114 return rc;
1115 *error |= rc;
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001116 dev_dbg(&state->i2c->dev, "%s: block error for layer %c: %d.\n",
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001117 __func__, 'A' + layer, *error);
1118
1119 /* Read Bit Count */
1120 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1121 if (rc < 0)
1122 return rc;
1123 rc = mb86a20s_readreg(state, 0x51);
1124 if (rc < 0)
1125 return rc;
1126 *count = rc << 8;
1127 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1128 if (rc < 0)
1129 return rc;
1130 rc = mb86a20s_readreg(state, 0x51);
1131 if (rc < 0)
1132 return rc;
1133 *count |= rc;
1134
1135 dev_dbg(&state->i2c->dev,
1136 "%s: block count for layer %c: %d.\n",
1137 __func__, 'A' + layer, *count);
1138
1139 /*
1140 * As we get TMCC data from the frontend, we can better estimate the
1141 * BER bit counters, in order to do the BER measure during a longer
1142 * time. Use those data, if available, to update the bit count
1143 * measure.
1144 */
1145
1146 if (!state->estimated_rate[layer])
1147 goto reset_measurement;
1148
1149 collect_rate = state->estimated_rate[layer] / 204 / 8;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001150 if (collect_rate < 32)
1151 collect_rate = 32;
1152 if (collect_rate > 65535)
1153 collect_rate = 65535;
1154
1155 if (collect_rate != *count) {
1156 dev_dbg(&state->i2c->dev,
1157 "%s: updating PER counter on layer %c to %d.\n",
1158 __func__, 'A' + layer, collect_rate);
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001159
1160 /* Stop PER measurement */
1161 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1162 if (rc < 0)
1163 return rc;
1164 rc = mb86a20s_writereg(state, 0x51, 0x00);
1165 if (rc < 0)
1166 return rc;
1167
1168 /* Update this layer's counter */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001169 rc = mb86a20s_writereg(state, 0x50, 0xb2 + layer * 2);
1170 if (rc < 0)
1171 return rc;
1172 rc = mb86a20s_writereg(state, 0x51, collect_rate >> 8);
1173 if (rc < 0)
1174 return rc;
1175 rc = mb86a20s_writereg(state, 0x50, 0xb3 + layer * 2);
1176 if (rc < 0)
1177 return rc;
1178 rc = mb86a20s_writereg(state, 0x51, collect_rate & 0xff);
1179 if (rc < 0)
1180 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001181
1182 /* start PER measurement */
1183 rc = mb86a20s_writereg(state, 0x50, 0xb0);
1184 if (rc < 0)
1185 return rc;
1186 rc = mb86a20s_writereg(state, 0x51, 0x07);
1187 if (rc < 0)
1188 return rc;
1189
1190 /* Reset all counters to collect new data */
1191 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1192 if (rc < 0)
1193 return rc;
1194 rc = mb86a20s_writereg(state, 0x51, 0x07);
1195 if (rc < 0)
1196 return rc;
1197 rc = mb86a20s_writereg(state, 0x51, 0x00);
1198
1199 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001200 }
1201
1202reset_measurement:
1203 /* Reset counter to collect new data */
1204 rc = mb86a20s_writereg(state, 0x50, 0xb1);
1205 if (rc < 0)
1206 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001207 rc = mb86a20s_readreg(state, 0x51);
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001208 if (rc < 0)
1209 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001210 val = rc;
1211 rc = mb86a20s_writereg(state, 0x51, val | (1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001212 if (rc < 0)
1213 return rc;
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001214 rc = mb86a20s_writereg(state, 0x51, val & ~(1 << layer));
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001215
Mauro Carvalho Chehab313cf4e2013-01-24 11:51:23 -03001216 return rc;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001217}
1218
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001219struct linear_segments {
1220 unsigned x, y;
1221};
1222
1223/*
1224 * All tables below return a dB/1000 measurement
1225 */
1226
1227static struct linear_segments cnr_to_db_table[] = {
1228 { 19648, 0},
1229 { 18187, 1000},
1230 { 16534, 2000},
1231 { 14823, 3000},
1232 { 13161, 4000},
1233 { 11622, 5000},
1234 { 10279, 6000},
1235 { 9089, 7000},
1236 { 8042, 8000},
1237 { 7137, 9000},
1238 { 6342, 10000},
1239 { 5641, 11000},
1240 { 5030, 12000},
1241 { 4474, 13000},
1242 { 3988, 14000},
1243 { 3556, 15000},
1244 { 3180, 16000},
1245 { 2841, 17000},
1246 { 2541, 18000},
1247 { 2276, 19000},
1248 { 2038, 20000},
1249 { 1800, 21000},
1250 { 1625, 22000},
1251 { 1462, 23000},
1252 { 1324, 24000},
1253 { 1175, 25000},
1254 { 1063, 26000},
1255 { 980, 27000},
1256 { 907, 28000},
1257 { 840, 29000},
1258 { 788, 30000},
1259};
1260
1261static struct linear_segments cnr_64qam_table[] = {
1262 { 3922688, 0},
1263 { 3920384, 1000},
1264 { 3902720, 2000},
1265 { 3894784, 3000},
1266 { 3882496, 4000},
1267 { 3872768, 5000},
1268 { 3858944, 6000},
1269 { 3851520, 7000},
1270 { 3838976, 8000},
1271 { 3829248, 9000},
1272 { 3818240, 10000},
1273 { 3806976, 11000},
1274 { 3791872, 12000},
1275 { 3767040, 13000},
1276 { 3720960, 14000},
1277 { 3637504, 15000},
1278 { 3498496, 16000},
1279 { 3296000, 17000},
1280 { 3031040, 18000},
1281 { 2715392, 19000},
1282 { 2362624, 20000},
1283 { 1963264, 21000},
1284 { 1649664, 22000},
1285 { 1366784, 23000},
1286 { 1120768, 24000},
1287 { 890880, 25000},
1288 { 723456, 26000},
1289 { 612096, 27000},
1290 { 518912, 28000},
1291 { 448256, 29000},
1292 { 388864, 30000},
1293};
1294
1295static struct linear_segments cnr_16qam_table[] = {
1296 { 5314816, 0},
1297 { 5219072, 1000},
1298 { 5118720, 2000},
1299 { 4998912, 3000},
1300 { 4875520, 4000},
1301 { 4736000, 5000},
1302 { 4604160, 6000},
1303 { 4458752, 7000},
1304 { 4300288, 8000},
1305 { 4092928, 9000},
1306 { 3836160, 10000},
1307 { 3521024, 11000},
1308 { 3155968, 12000},
1309 { 2756864, 13000},
1310 { 2347008, 14000},
1311 { 1955072, 15000},
1312 { 1593600, 16000},
1313 { 1297920, 17000},
1314 { 1043968, 18000},
1315 { 839680, 19000},
1316 { 672256, 20000},
1317 { 523008, 21000},
1318 { 424704, 22000},
1319 { 345088, 23000},
1320 { 280064, 24000},
1321 { 221440, 25000},
1322 { 179712, 26000},
1323 { 151040, 27000},
1324 { 128512, 28000},
1325 { 110080, 29000},
1326 { 95744, 30000},
1327};
1328
1329struct linear_segments cnr_qpsk_table[] = {
1330 { 2834176, 0},
1331 { 2683648, 1000},
1332 { 2536960, 2000},
1333 { 2391808, 3000},
1334 { 2133248, 4000},
1335 { 1906176, 5000},
1336 { 1666560, 6000},
1337 { 1422080, 7000},
1338 { 1189632, 8000},
1339 { 976384, 9000},
1340 { 790272, 10000},
1341 { 633344, 11000},
1342 { 505600, 12000},
1343 { 402944, 13000},
1344 { 320768, 14000},
1345 { 255488, 15000},
1346 { 204032, 16000},
1347 { 163072, 17000},
1348 { 130304, 18000},
1349 { 105216, 19000},
1350 { 83456, 20000},
1351 { 65024, 21000},
1352 { 52480, 22000},
1353 { 42752, 23000},
1354 { 34560, 24000},
1355 { 27136, 25000},
1356 { 22016, 26000},
1357 { 18432, 27000},
1358 { 15616, 28000},
1359 { 13312, 29000},
1360 { 11520, 30000},
1361};
1362
1363static u32 interpolate_value(u32 value, struct linear_segments *segments,
1364 unsigned len)
1365{
1366 u64 tmp64;
1367 u32 dx, dy;
1368 int i, ret;
1369
1370 if (value >= segments[0].x)
1371 return segments[0].y;
1372 if (value < segments[len-1].x)
1373 return segments[len-1].y;
1374
1375 for (i = 1; i < len - 1; i++) {
1376 /* If value is identical, no need to interpolate */
1377 if (value == segments[i].x)
1378 return segments[i].y;
1379 if (value > segments[i].x)
1380 break;
1381 }
1382
1383 /* Linear interpolation between the two (x,y) points */
1384 dy = segments[i].y - segments[i - 1].y;
1385 dx = segments[i - 1].x - segments[i].x;
1386 tmp64 = value - segments[i].x;
1387 tmp64 *= dy;
1388 do_div(tmp64, dx);
1389 ret = segments[i].y - tmp64;
1390
1391 return ret;
1392}
1393
1394static int mb86a20s_get_main_CNR(struct dvb_frontend *fe)
1395{
1396 struct mb86a20s_state *state = fe->demodulator_priv;
1397 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1398 u32 cnr_linear, cnr;
1399 int rc, val;
1400
1401 /* Check if CNR is available */
1402 rc = mb86a20s_readreg(state, 0x45);
1403 if (rc < 0)
1404 return rc;
1405
1406 if (!(rc & 0x40)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001407 dev_dbg(&state->i2c->dev, "%s: CNR is not available yet.\n",
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001408 __func__);
1409 return -EBUSY;
1410 }
1411 val = rc;
1412
1413 rc = mb86a20s_readreg(state, 0x46);
1414 if (rc < 0)
1415 return rc;
1416 cnr_linear = rc << 8;
1417
1418 rc = mb86a20s_readreg(state, 0x46);
1419 if (rc < 0)
1420 return rc;
1421 cnr_linear |= rc;
1422
1423 cnr = interpolate_value(cnr_linear,
1424 cnr_to_db_table, ARRAY_SIZE(cnr_to_db_table));
1425
1426 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
1427 c->cnr.stat[0].svalue = cnr;
1428
1429 dev_dbg(&state->i2c->dev, "%s: CNR is %d.%03d dB (%d)\n",
1430 __func__, cnr / 1000, cnr % 1000, cnr_linear);
1431
1432 /* CNR counter reset */
1433 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
1434 if (rc < 0)
1435 return rc;
1436 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
1437
1438 return rc;
1439}
1440
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001441static int mb86a20s_get_blk_error_layer_CNR(struct dvb_frontend *fe)
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001442{
1443 struct mb86a20s_state *state = fe->demodulator_priv;
1444 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1445 u32 mer, cnr;
1446 int rc, val, i;
1447 struct linear_segments *segs;
1448 unsigned segs_len;
1449
1450 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1451
1452 /* Check if the measures are already available */
1453 rc = mb86a20s_writereg(state, 0x50, 0x5b);
1454 if (rc < 0)
1455 return rc;
1456 rc = mb86a20s_readreg(state, 0x51);
1457 if (rc < 0)
1458 return rc;
1459
1460 /* Check if data is available */
1461 if (!(rc & 0x01)) {
Mauro Carvalho Chehabd56e3262013-03-02 09:05:42 -03001462 dev_dbg(&state->i2c->dev,
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001463 "%s: MER measures aren't available yet.\n", __func__);
1464 return -EBUSY;
1465 }
1466
1467 /* Read all layers */
1468 for (i = 0; i < 3; i++) {
1469 if (!(c->isdbt_layer_enabled & (1 << i))) {
1470 c->cnr.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1471 continue;
1472 }
1473
1474 rc = mb86a20s_writereg(state, 0x50, 0x52 + i * 3);
1475 if (rc < 0)
1476 return rc;
1477 rc = mb86a20s_readreg(state, 0x51);
1478 if (rc < 0)
1479 return rc;
1480 mer = rc << 16;
1481 rc = mb86a20s_writereg(state, 0x50, 0x53 + i * 3);
1482 if (rc < 0)
1483 return rc;
1484 rc = mb86a20s_readreg(state, 0x51);
1485 if (rc < 0)
1486 return rc;
1487 mer |= rc << 8;
1488 rc = mb86a20s_writereg(state, 0x50, 0x54 + i * 3);
1489 if (rc < 0)
1490 return rc;
1491 rc = mb86a20s_readreg(state, 0x51);
1492 if (rc < 0)
1493 return rc;
1494 mer |= rc;
1495
1496 switch (c->layer[i].modulation) {
1497 case DQPSK:
1498 case QPSK:
1499 segs = cnr_qpsk_table;
1500 segs_len = ARRAY_SIZE(cnr_qpsk_table);
1501 break;
1502 case QAM_16:
1503 segs = cnr_16qam_table;
1504 segs_len = ARRAY_SIZE(cnr_16qam_table);
1505 break;
1506 default:
1507 case QAM_64:
1508 segs = cnr_64qam_table;
1509 segs_len = ARRAY_SIZE(cnr_64qam_table);
1510 break;
1511 }
1512 cnr = interpolate_value(mer, segs, segs_len);
1513
1514 c->cnr.stat[1 + i].scale = FE_SCALE_DECIBEL;
1515 c->cnr.stat[1 + i].svalue = cnr;
1516
1517 dev_dbg(&state->i2c->dev,
1518 "%s: CNR for layer %c is %d.%03d dB (MER = %d).\n",
1519 __func__, 'A' + i, cnr / 1000, cnr % 1000, mer);
1520
1521 }
1522
1523 /* Start a new MER measurement */
1524 /* MER counter reset */
1525 rc = mb86a20s_writereg(state, 0x50, 0x50);
1526 if (rc < 0)
1527 return rc;
1528 rc = mb86a20s_readreg(state, 0x51);
1529 if (rc < 0)
1530 return rc;
1531 val = rc;
1532
1533 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
1534 if (rc < 0)
1535 return rc;
1536 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
1537 if (rc < 0)
1538 return rc;
1539
1540 return 0;
1541}
1542
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001543static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
1544{
1545 struct mb86a20s_state *state = fe->demodulator_priv;
1546 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1547 int i;
1548
1549 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1550
1551 /* Fill the length of each status counter */
1552
1553 /* Only global stats */
1554 c->strength.len = 1;
1555
1556 /* Per-layer stats - 3 layers + global */
1557 c->cnr.len = 4;
1558 c->pre_bit_error.len = 4;
1559 c->pre_bit_count.len = 4;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001560 c->post_bit_error.len = 4;
1561 c->post_bit_count.len = 4;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001562 c->block_error.len = 4;
1563 c->block_count.len = 4;
1564
1565 /* Signal is always available */
1566 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
1567 c->strength.stat[0].uvalue = 0;
1568
1569 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
1570 for (i = 0; i < 4; i++) {
1571 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1572 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1573 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001574 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1575 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001576 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1577 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
1578 }
1579}
1580
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001581static int mb86a20s_get_stats(struct dvb_frontend *fe, int status_nr)
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001582{
1583 struct mb86a20s_state *state = fe->demodulator_priv;
1584 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1585 int rc = 0, i;
1586 u32 bit_error = 0, bit_count = 0;
1587 u32 t_pre_bit_error = 0, t_pre_bit_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001588 u32 t_post_bit_error = 0, t_post_bit_count = 0;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001589 u32 block_error = 0, block_count = 0;
1590 u32 t_block_error = 0, t_block_count = 0;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001591 int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
1592 int per_layers = 0;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001593
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001594 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
1595
1596 mb86a20s_get_main_CNR(fe);
1597
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001598 /* Get per-layer stats */
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001599 mb86a20s_get_blk_error_layer_CNR(fe);
Mauro Carvalho Chehab25188bd2013-01-16 15:12:05 -03001600
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001601 /*
1602 * At state 7, only CNR is available
1603 * For BER measures, state=9 is required
1604 * FIXME: we may get MER measures with state=8
1605 */
1606 if (status_nr < 9)
1607 return 0;
1608
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001609 for (i = 0; i < 3; i++) {
1610 if (c->isdbt_layer_enabled & (1 << i)) {
1611 /* Layer is active and has rc segments */
1612 active_layers++;
1613
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001614 /* Handle BER before vterbi */
Mauro Carvalho Chehabad0abbf2013-01-24 11:48:44 -03001615 rc = mb86a20s_get_pre_ber(fe, i,
1616 &bit_error, &bit_count);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001617 if (rc >= 0) {
1618 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1619 c->pre_bit_error.stat[1 + i].uvalue += bit_error;
1620 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1621 c->pre_bit_count.stat[1 + i].uvalue += bit_count;
1622 } else if (rc != -EBUSY) {
1623 /*
1624 * If an I/O error happened,
1625 * measures are now unavailable
1626 */
1627 c->pre_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1628 c->pre_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1629 dev_err(&state->i2c->dev,
1630 "%s: Can't get BER for layer %c (error %d).\n",
1631 __func__, 'A' + i, rc);
1632 }
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001633 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001634 pre_ber_layers++;
1635
1636 /* Handle BER post vterbi */
1637 rc = mb86a20s_get_post_ber(fe, i,
1638 &bit_error, &bit_count);
1639 if (rc >= 0) {
1640 c->post_bit_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1641 c->post_bit_error.stat[1 + i].uvalue += bit_error;
1642 c->post_bit_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1643 c->post_bit_count.stat[1 + i].uvalue += bit_count;
1644 } else if (rc != -EBUSY) {
1645 /*
1646 * If an I/O error happened,
1647 * measures are now unavailable
1648 */
1649 c->post_bit_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1650 c->post_bit_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1651 dev_err(&state->i2c->dev,
1652 "%s: Can't get BER for layer %c (error %d).\n",
1653 __func__, 'A' + i, rc);
1654 }
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001655 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1656 post_ber_layers++;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001657
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001658 /* Handle Block errors for PER/UCB reports */
1659 rc = mb86a20s_get_blk_error(fe, i,
1660 &block_error,
1661 &block_count);
1662 if (rc >= 0) {
1663 c->block_error.stat[1 + i].scale = FE_SCALE_COUNTER;
1664 c->block_error.stat[1 + i].uvalue += block_error;
1665 c->block_count.stat[1 + i].scale = FE_SCALE_COUNTER;
1666 c->block_count.stat[1 + i].uvalue += block_count;
1667 } else if (rc != -EBUSY) {
1668 /*
1669 * If an I/O error happened,
1670 * measures are now unavailable
1671 */
1672 c->block_error.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1673 c->block_count.stat[1 + i].scale = FE_SCALE_NOT_AVAILABLE;
1674 dev_err(&state->i2c->dev,
1675 "%s: Can't get PER for layer %c (error %d).\n",
1676 __func__, 'A' + i, rc);
1677
1678 }
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001679 if (c->block_error.stat[1 + i].scale != FE_SCALE_NOT_AVAILABLE)
1680 per_layers++;
1681
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001682 /* Update total preBER */
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001683 t_pre_bit_error += c->pre_bit_error.stat[1 + i].uvalue;
1684 t_pre_bit_count += c->pre_bit_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001685
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001686 /* Update total postBER */
1687 t_post_bit_error += c->post_bit_error.stat[1 + i].uvalue;
1688 t_post_bit_count += c->post_bit_count.stat[1 + i].uvalue;
1689
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001690 /* Update total PER */
1691 t_block_error += c->block_error.stat[1 + i].uvalue;
1692 t_block_count += c->block_count.stat[1 + i].uvalue;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001693 }
1694 }
1695
1696 /*
1697 * Start showing global count if at least one error count is
1698 * available.
1699 */
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001700 if (pre_ber_layers) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001701 /*
1702 * At least one per-layer BER measure was read. We can now
1703 * calculate the total BER
1704 *
1705 * Total Bit Error/Count is calculated as the sum of the
1706 * bit errors on all active layers.
1707 */
1708 c->pre_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1709 c->pre_bit_error.stat[0].uvalue = t_pre_bit_error;
1710 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1711 c->pre_bit_count.stat[0].uvalue = t_pre_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001712 } else {
1713 c->pre_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1714 c->pre_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001715 }
1716
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001717 /*
1718 * Start showing global count if at least one error count is
1719 * available.
1720 */
1721 if (post_ber_layers) {
1722 /*
1723 * At least one per-layer BER measure was read. We can now
1724 * calculate the total BER
1725 *
1726 * Total Bit Error/Count is calculated as the sum of the
1727 * bit errors on all active layers.
1728 */
1729 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1730 c->post_bit_error.stat[0].uvalue = t_post_bit_error;
1731 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1732 c->post_bit_count.stat[0].uvalue = t_post_bit_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001733 } else {
1734 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1735 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd9b6f082013-01-24 10:25:16 -03001736 }
1737
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001738 if (per_layers) {
1739 /*
1740 * At least one per-layer UCB measure was read. We can now
1741 * calculate the total UCB
1742 *
1743 * Total block Error/Count is calculated as the sum of the
1744 * block errors on all active layers.
1745 */
1746 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
1747 c->block_error.stat[0].uvalue = t_block_error;
1748 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
1749 c->block_count.stat[0].uvalue = t_block_count;
Mauro Carvalho Chehabf67102c2013-01-24 13:16:17 -03001750 } else {
1751 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1752 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehab593ae892013-01-17 20:10:47 -03001753 }
1754
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001755 return rc;
1756}
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001757
1758/*
1759 * The functions below are called via DVB callbacks, so they need to
1760 * properly use the I2C gate control
1761 */
1762
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001763static int mb86a20s_initfe(struct dvb_frontend *fe)
1764{
1765 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001766 u64 pll;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001767 int rc;
1768 u8 regD5 = 1;
1769
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001770 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001771
1772 if (fe->ops.i2c_gate_ctrl)
1773 fe->ops.i2c_gate_ctrl(fe, 0);
1774
1775 /* Initialize the frontend */
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001776 rc = mb86a20s_writeregdata(state, mb86a20s_init1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001777 if (rc < 0)
1778 goto err;
1779
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001780 /* Adjust IF frequency to match tuner */
1781 if (fe->ops.tuner_ops.get_if_frequency)
1782 fe->ops.tuner_ops.get_if_frequency(fe, &state->if_freq);
1783
1784 if (!state->if_freq)
1785 state->if_freq = 3300000;
1786
1787 /* pll = freq[Hz] * 2^24/10^6 / 16.285714286 */
1788 pll = state->if_freq * 1677721600L;
1789 do_div(pll, 1628571429L);
1790 rc = mb86a20s_writereg(state, 0x28, 0x20);
1791 if (rc < 0)
1792 goto err;
1793 rc = mb86a20s_writereg(state, 0x29, (pll >> 16) & 0xff);
1794 if (rc < 0)
1795 goto err;
1796 rc = mb86a20s_writereg(state, 0x2a, (pll >> 8) & 0xff);
1797 if (rc < 0)
1798 goto err;
1799 rc = mb86a20s_writereg(state, 0x2b, pll & 0xff);
1800 if (rc < 0)
1801 goto err;
1802 dev_dbg(&state->i2c->dev, "%s: IF=%d, PLL=0x%06llx\n",
1803 __func__, state->if_freq, (long long)pll);
1804
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001805 if (!state->config->is_serial) {
1806 regD5 &= ~1;
1807
1808 rc = mb86a20s_writereg(state, 0x50, 0xd5);
1809 if (rc < 0)
1810 goto err;
1811 rc = mb86a20s_writereg(state, 0x51, regD5);
1812 if (rc < 0)
1813 goto err;
1814 }
1815
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001816 rc = mb86a20s_writeregdata(state, mb86a20s_init2);
1817 if (rc < 0)
1818 goto err;
1819
1820
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001821err:
1822 if (fe->ops.i2c_gate_ctrl)
1823 fe->ops.i2c_gate_ctrl(fe, 1);
1824
1825 if (rc < 0) {
1826 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001827 dev_info(&state->i2c->dev,
1828 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001829 } else {
1830 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001831 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001832 }
1833 return rc;
1834}
1835
1836static int mb86a20s_set_frontend(struct dvb_frontend *fe)
1837{
1838 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001839 int rc, if_freq;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001840#if 0
1841 /*
1842 * FIXME: Properly implement the set frontend properties
1843 */
1844 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1845#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001846 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001847
1848 /*
1849 * Gate should already be opened, but it doesn't hurt to
1850 * double-check
1851 */
1852 if (fe->ops.i2c_gate_ctrl)
1853 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001854 fe->ops.tuner_ops.set_params(fe);
1855
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001856 if (fe->ops.tuner_ops.get_if_frequency)
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001857 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
1858
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001859 /*
1860 * Make it more reliable: if, for some reason, the initial
1861 * device initialization doesn't happen, initialize it when
1862 * a SBTVD parameters are adjusted.
1863 *
1864 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
1865 * the agc callback logic is not called during DVB attach time,
1866 * causing mb86a20s to not be initialized with Kworld SBTVD.
1867 * So, this hack is needed, in order to make Kworld SBTVD to work.
Mauro Carvalho Chehab768e6dad2013-02-28 16:45:39 -03001868 *
1869 * It is also needed to change the IF after the initial init.
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001870 *
1871 * HACK: Always init the frontend when set_frontend is called:
1872 * it was noticed that, on some devices, it fails to lock on a
1873 * different channel. So, it is better to reset everything, even
1874 * wasting some time, than to loose channel lock.
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001875 */
Mauro Carvalho Chehaba78b41d2013-03-02 13:45:31 -03001876 mb86a20s_initfe(fe);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001877
1878 if (fe->ops.i2c_gate_ctrl)
1879 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001880
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001881 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001882 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehab3a2e4752013-03-02 15:02:23 -03001883 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd01a8ee2013-01-14 20:34:55 -03001884
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -02001885 if (fe->ops.i2c_gate_ctrl)
1886 fe->ops.i2c_gate_ctrl(fe, 1);
1887
1888 return rc;
1889}
1890
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001891static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
1892 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001893{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001894 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001895 int rc, status_nr;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001896
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001897 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001898
1899 if (fe->ops.i2c_gate_ctrl)
1900 fe->ops.i2c_gate_ctrl(fe, 0);
1901
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001902 /* Get lock */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001903 status_nr = mb86a20s_read_status(fe, status);
1904 if (status_nr < 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001905 mb86a20s_stats_not_ready(fe);
1906 mb86a20s_reset_frontend_cache(fe);
1907 }
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001908 if (status_nr < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001909 dev_err(&state->i2c->dev,
1910 "%s: Can't read frontend lock status\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001911 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001912 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001913
1914 /* Get signal strength */
1915 rc = mb86a20s_read_signal_strength(fe);
1916 if (rc < 0) {
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001917 dev_err(&state->i2c->dev,
1918 "%s: Can't reset VBER registers.\n", __func__);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001919 mb86a20s_stats_not_ready(fe);
1920 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001921
1922 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001923 goto error;
1924 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001925
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001926 if (status_nr >= 7) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001927 /* Get TMCC info*/
1928 rc = mb86a20s_get_frontend(fe);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001929 if (rc < 0) {
1930 dev_err(&state->i2c->dev,
1931 "%s: Can't get FE TMCC data.\n", __func__);
1932 rc = 0; /* Status is OK */
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001933 goto error;
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001934 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001935
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001936 /* Get statistics */
Mauro Carvalho Chehab15b1c5a2013-03-02 09:06:17 -03001937 rc = mb86a20s_get_stats(fe, status_nr);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001938 if (rc < 0 && rc != -EBUSY) {
1939 dev_err(&state->i2c->dev,
1940 "%s: Can't get FE statistics.\n", __func__);
1941 rc = 0;
1942 goto error;
1943 }
1944 rc = 0; /* Don't return EBUSY to userspace */
1945 }
1946 goto ok;
1947
1948error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001949 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001950
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001951ok:
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001952 if (fe->ops.i2c_gate_ctrl)
1953 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab149d5182013-01-22 12:30:07 -03001954
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001955 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -02001956}
1957
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001958static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
1959 u16 *strength)
1960{
1961 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1962
1963
1964 *strength = c->strength.stat[0].uvalue;
1965
1966 return 0;
1967}
1968
1969static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
1970{
1971 /*
1972 * get_frontend is now handled together with other stats
1973 * retrival, when read_status() is called, as some statistics
1974 * will depend on the layers detection.
1975 */
1976 return 0;
1977};
1978
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001979static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001980 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001981 unsigned int mode_flags,
1982 unsigned int *delay,
1983 fe_status_t *status)
1984{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001985 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001986 int rc = 0;
1987
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02001988 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001989
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -03001990 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03001991 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001992
1993 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03001994 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001995
1996 return rc;
1997}
1998
1999static void mb86a20s_release(struct dvb_frontend *fe)
2000{
2001 struct mb86a20s_state *state = fe->demodulator_priv;
2002
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002003 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002004
2005 kfree(state);
2006}
2007
2008static struct dvb_frontend_ops mb86a20s_ops;
2009
2010struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
2011 struct i2c_adapter *i2c)
2012{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002013 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002014 u8 rev;
2015
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002016 dev_dbg(&i2c->dev, "%s called.\n", __func__);
2017
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002018 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002019 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002020 if (state == NULL) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002021 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002022 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002023 goto error;
2024 }
2025
2026 /* setup the state */
2027 state->config = config;
2028 state->i2c = i2c;
2029
2030 /* create dvb_frontend */
2031 memcpy(&state->frontend.ops, &mb86a20s_ops,
2032 sizeof(struct dvb_frontend_ops));
2033 state->frontend.demodulator_priv = state;
2034
2035 /* Check if it is a mb86a20s frontend */
2036 rev = mb86a20s_readreg(state, 0);
2037
2038 if (rev == 0x13) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002039 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002040 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002041 } else {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -02002042 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -02002043 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002044 rev);
2045 goto error;
2046 }
2047
2048 return &state->frontend;
2049
2050error:
2051 kfree(state);
2052 return NULL;
2053}
2054EXPORT_SYMBOL(mb86a20s_attach);
2055
2056static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002057 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002058 /* Use dib8000 values per default */
2059 .info = {
2060 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002061 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
2062 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
2063 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
2064 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
2065 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
2066 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
2067 /* Actually, those values depend on the used tuner */
2068 .frequency_min = 45000000,
2069 .frequency_max = 864000000,
2070 .frequency_stepsize = 62500,
2071 },
2072
2073 .release = mb86a20s_release,
2074
2075 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -03002076 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -03002077 .get_frontend = mb86a20s_get_frontend_dummy,
2078 .read_status = mb86a20s_read_status_and_stats,
2079 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03002080 .tune = mb86a20s_tune,
2081};
2082
2083MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
2084MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2085MODULE_LICENSE("GPL");