blob: d7668e6292a054465a11f5c5fb47ca959a8a2db7 [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
34 bool need_init;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030035};
36
37struct regdata {
38 u8 reg;
39 u8 data;
40};
41
42/*
43 * Initialization sequence: Use whatevere default values that PV SBTVD
44 * does on its initialisation, obtained via USB snoop
45 */
46static struct regdata mb86a20s_init[] = {
47 { 0x70, 0x0f },
48 { 0x70, 0xff },
49 { 0x08, 0x01 },
50 { 0x09, 0x3e },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020051 { 0x50, 0xd1 }, { 0x51, 0x22 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030052 { 0x39, 0x01 },
53 { 0x71, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020054 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
55 { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020056 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030057 { 0x3b, 0x21 },
58 { 0x3c, 0x3a },
59 { 0x01, 0x0d },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020060 { 0x04, 0x08 }, { 0x05, 0x05 },
61 { 0x04, 0x0e }, { 0x05, 0x00 },
62 { 0x04, 0x0f }, { 0x05, 0x14 },
63 { 0x04, 0x0b }, { 0x05, 0x8c },
64 { 0x04, 0x00 }, { 0x05, 0x00 },
65 { 0x04, 0x01 }, { 0x05, 0x07 },
66 { 0x04, 0x02 }, { 0x05, 0x0f },
67 { 0x04, 0x03 }, { 0x05, 0xa0 },
68 { 0x04, 0x09 }, { 0x05, 0x00 },
69 { 0x04, 0x0a }, { 0x05, 0xff },
70 { 0x04, 0x27 }, { 0x05, 0x64 },
71 { 0x04, 0x28 }, { 0x05, 0x00 },
72 { 0x04, 0x1e }, { 0x05, 0xff },
73 { 0x04, 0x29 }, { 0x05, 0x0a },
74 { 0x04, 0x32 }, { 0x05, 0x0a },
75 { 0x04, 0x14 }, { 0x05, 0x02 },
76 { 0x04, 0x04 }, { 0x05, 0x00 },
77 { 0x04, 0x05 }, { 0x05, 0x22 },
78 { 0x04, 0x06 }, { 0x05, 0x0e },
79 { 0x04, 0x07 }, { 0x05, 0xd8 },
80 { 0x04, 0x12 }, { 0x05, 0x00 },
81 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020082 { 0x04, 0x15 }, { 0x05, 0x4e },
83 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030084
85 /*
86 * On this demod, when the bit count reaches the count below,
87 * it collects the bit error count. The bit counters are initialized
88 * to 65535 here. This warrants that all of them will be quickly
89 * calculated when device gets locked. As TMCC is parsed, the values
90 * can be adjusted later in the driver's code.
91 */
92 { 0x52, 0x01 }, /* Turn on BER before Viterbi */
93 { 0x50, 0xa7 }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020094 { 0x50, 0xa8 }, { 0x51, 0xff },
95 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030096 { 0x50, 0xaa }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020097 { 0x50, 0xab }, { 0x51, 0xff },
98 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -030099 { 0x50, 0xad }, { 0x51, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200100 { 0x50, 0xae }, { 0x51, 0xff },
101 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300102
103 { 0x5e, 0x00 }, /* Turn off BER after Viterbi */
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200104 { 0x50, 0xdc }, { 0x51, 0x01 },
105 { 0x50, 0xdd }, { 0x51, 0xf4 },
106 { 0x50, 0xde }, { 0x51, 0x01 },
107 { 0x50, 0xdf }, { 0x51, 0xf4 },
108 { 0x50, 0xe0 }, { 0x51, 0x01 },
109 { 0x50, 0xe1 }, { 0x51, 0xf4 },
110 { 0x50, 0xb0 }, { 0x51, 0x07 },
111 { 0x50, 0xb2 }, { 0x51, 0xff },
112 { 0x50, 0xb3 }, { 0x51, 0xff },
113 { 0x50, 0xb4 }, { 0x51, 0xff },
114 { 0x50, 0xb5 }, { 0x51, 0xff },
115 { 0x50, 0xb6 }, { 0x51, 0xff },
116 { 0x50, 0xb7 }, { 0x51, 0xff },
117 { 0x50, 0x50 }, { 0x51, 0x02 },
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300118 { 0x50, 0x51 }, { 0x51, 0x04 }, /* MER symbol 4 */
119 { 0x45, 0x04 }, /* CN symbol 4 */
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300120 { 0x48, 0x04 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200121 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
122 { 0x50, 0xd6 }, { 0x51, 0x1f },
123 { 0x50, 0xd2 }, { 0x51, 0x03 },
124 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200125 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
126 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300127
128 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200129 { 0x28, 0x00 }, { 0x29, 0x10 },
130 { 0x28, 0x05 }, { 0x29, 0x02 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300131 { 0x1c, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200132 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
133 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
134 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
135 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
136 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
137 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
138 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
139 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
140 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
141 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
142 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
143 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
144 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
145 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
146 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
147 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
148 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
149 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
150 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
151 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
152 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
153 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
154 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
155 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
156 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
157 { 0x50, 0x1e }, { 0x51, 0x5d },
158 { 0x50, 0x22 }, { 0x51, 0x00 },
159 { 0x50, 0x23 }, { 0x51, 0xc8 },
160 { 0x50, 0x24 }, { 0x51, 0x00 },
161 { 0x50, 0x25 }, { 0x51, 0xf0 },
162 { 0x50, 0x26 }, { 0x51, 0x00 },
163 { 0x50, 0x27 }, { 0x51, 0xc3 },
164 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200165 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300166 { 0xd0, 0x00 },
167};
168
169static struct regdata mb86a20s_reset_reception[] = {
170 { 0x70, 0xf0 },
171 { 0x70, 0xff },
172 { 0x08, 0x01 },
173 { 0x08, 0x00 },
174};
175
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300176static struct regdata mb86a20s_vber_reset[] = {
177 { 0x53, 0x00 }, /* VBER Counter reset */
178 { 0x53, 0x07 },
179};
180
181static struct regdata mb86a20s_per_reset[] = {
182 { 0x50, 0xb1 }, /* PER Counter reset */
183 { 0x51, 0x07 },
184 { 0x51, 0x00 },
185};
186
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200187/*
188 * I2C read/write functions and macros
189 */
190
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300191static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300192 u8 i2c_addr, u8 reg, u8 data)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300193{
194 u8 buf[] = { reg, data };
195 struct i2c_msg msg = {
196 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
197 };
198 int rc;
199
200 rc = i2c_transfer(state->i2c, &msg, 1);
201 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200202 dev_err(&state->i2c->dev,
203 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
204 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300205 return rc;
206 }
207
208 return 0;
209}
210
211static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
212 u8 i2c_addr, struct regdata *rd, int size)
213{
214 int i, rc;
215
216 for (i = 0; i < size; i++) {
217 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
218 rd[i].data);
219 if (rc < 0)
220 return rc;
221 }
222 return 0;
223}
224
225static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
226 u8 i2c_addr, u8 reg)
227{
228 u8 val;
229 int rc;
230 struct i2c_msg msg[] = {
231 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
232 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
233 };
234
235 rc = i2c_transfer(state->i2c, msg, 2);
236
237 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200238 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
239 __func__, reg, rc);
240 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300241 }
242
243 return val;
244}
245
246#define mb86a20s_readreg(state, reg) \
247 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
248#define mb86a20s_writereg(state, reg, val) \
249 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
250#define mb86a20s_writeregdata(state, regdata) \
251 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
252 regdata, ARRAY_SIZE(regdata))
253
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300254/*
255 * Ancillary internal routines (likely compiled inlined)
256 *
257 * The functions below assume that gateway lock has already obtained
258 */
259
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200260static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300261{
262 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200263 int val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300264
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200265 *status = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300266
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200267 val = mb86a20s_readreg(state, 0x0a) & 0xf;
268 if (val < 0)
269 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300270
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200271 if (val >= 2)
272 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300273
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200274 if (val >= 4)
275 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300276
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200277 if (val >= 5)
278 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300279
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200280 if (val >= 7)
281 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300282
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200283 if (val >= 8) /* Maybe 9? */
284 *status |= FE_HAS_LOCK;
285
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200286 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
287 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200288
289 return 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300290}
291
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300292static int mb86a20s_read_signal_strength(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300293{
294 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300295 int rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300296 unsigned rf_max, rf_min, rf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300297
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300298 /* Does a binary search to get RF strength */
299 rf_max = 0xfff;
300 rf_min = 0;
301 do {
302 rf = (rf_max + rf_min) / 2;
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300303 rc = mb86a20s_writereg(state, 0x04, 0x1f);
304 if (rc < 0)
305 return rc;
306 rc = mb86a20s_writereg(state, 0x05, rf >> 8);
307 if (rc < 0)
308 return rc;
309 rc = mb86a20s_writereg(state, 0x04, 0x20);
310 if (rc < 0)
311 return rc;
312 rc = mb86a20s_writereg(state, 0x04, rf);
313 if (rc < 0)
314 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300315
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300316 rc = mb86a20s_readreg(state, 0x02);
317 if (rc < 0)
318 return rc;
319 if (rc & 0x08)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300320 rf_min = (rf_max + rf_min) / 2;
321 else
322 rf_max = (rf_max + rf_min) / 2;
323 if (rf_max - rf_min < 4) {
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300324 rf = (rf_max + rf_min) / 2;
325
326 /* Rescale it from 2^12 (4096) to 2^16 */
327 rf <<= (16 - 12);
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200328 dev_dbg(&state->i2c->dev,
329 "%s: signal strength = %d (%d < RF=%d < %d)\n",
330 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300331 return rf;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300332 }
333 } while (1);
334
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300335 return 0;
336}
337
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200338static int mb86a20s_get_modulation(struct mb86a20s_state *state,
339 unsigned layer)
340{
341 int rc;
342 static unsigned char reg[] = {
343 [0] = 0x86, /* Layer A */
344 [1] = 0x8a, /* Layer B */
345 [2] = 0x8e, /* Layer C */
346 };
347
Dan Carpenter82033bc2012-01-13 02:28:34 -0300348 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200349 return -EINVAL;
350 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
351 if (rc < 0)
352 return rc;
353 rc = mb86a20s_readreg(state, 0x6e);
354 if (rc < 0)
355 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300356 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200357 case 0:
358 return DQPSK;
359 case 1:
360 return QPSK;
361 case 2:
362 return QAM_16;
363 case 3:
364 return QAM_64;
365 default:
366 return QAM_AUTO;
367 }
368}
369
370static int mb86a20s_get_fec(struct mb86a20s_state *state,
371 unsigned layer)
372{
373 int rc;
374
375 static unsigned char reg[] = {
376 [0] = 0x87, /* Layer A */
377 [1] = 0x8b, /* Layer B */
378 [2] = 0x8f, /* Layer C */
379 };
380
Dan Carpenter82033bc2012-01-13 02:28:34 -0300381 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200382 return -EINVAL;
383 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
384 if (rc < 0)
385 return rc;
386 rc = mb86a20s_readreg(state, 0x6e);
387 if (rc < 0)
388 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300389 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200390 case 0:
391 return FEC_1_2;
392 case 1:
393 return FEC_2_3;
394 case 2:
395 return FEC_3_4;
396 case 3:
397 return FEC_5_6;
398 case 4:
399 return FEC_7_8;
400 default:
401 return FEC_AUTO;
402 }
403}
404
405static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
406 unsigned layer)
407{
408 int rc;
409
410 static unsigned char reg[] = {
411 [0] = 0x88, /* Layer A */
412 [1] = 0x8c, /* Layer B */
413 [2] = 0x90, /* Layer C */
414 };
415
Dan Carpenter82033bc2012-01-13 02:28:34 -0300416 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200417 return -EINVAL;
418 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
419 if (rc < 0)
420 return rc;
421 rc = mb86a20s_readreg(state, 0x6e);
422 if (rc < 0)
423 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300424
425 switch ((rc >> 4) & 0x07) {
426 case 1:
427 return GUARD_INTERVAL_1_4;
428 case 2:
429 return GUARD_INTERVAL_1_8;
430 case 3:
431 return GUARD_INTERVAL_1_16;
432 case 4:
433 return GUARD_INTERVAL_1_32;
434
435 default:
436 case 0:
437 return GUARD_INTERVAL_AUTO;
438 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200439}
440
441static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
442 unsigned layer)
443{
444 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200445 static unsigned char reg[] = {
446 [0] = 0x89, /* Layer A */
447 [1] = 0x8d, /* Layer B */
448 [2] = 0x91, /* Layer C */
449 };
450
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200451 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
452
Dan Carpenter82033bc2012-01-13 02:28:34 -0300453 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200454 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200455
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200456 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
457 if (rc < 0)
458 return rc;
459 rc = mb86a20s_readreg(state, 0x6e);
460 if (rc < 0)
461 return rc;
462 count = (rc >> 4) & 0x0f;
463
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200464 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
465
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200466 return count;
467}
468
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300469static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
470{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200471 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300472 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
473
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200474 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
475
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300476 /* Fixed parameters */
477 c->delivery_system = SYS_ISDBT;
478 c->bandwidth_hz = 6000000;
479
480 /* Initialize values that will be later autodetected */
481 c->isdbt_layer_enabled = 0;
482 c->transmission_mode = TRANSMISSION_MODE_AUTO;
483 c->guard_interval = GUARD_INTERVAL_AUTO;
484 c->isdbt_sb_mode = 0;
485 c->isdbt_sb_segment_count = 0;
486}
487
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300488static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300489{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200490 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300491 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200492 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300493
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200494 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
495
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300496 /* Reset frontend cache to default values */
497 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200498
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200499 /* Check for partial reception */
500 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300501 if (rc < 0)
502 return rc;
503 rc = mb86a20s_readreg(state, 0x6e);
504 if (rc < 0)
505 return rc;
506 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200507
508 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300509
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200510 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200511 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
512 __func__, 'A' + i);
513
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200514 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300515 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200516 goto noperlayer_error;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300517 if (rc >= 0 && rc < 14)
518 c->layer[i].segment_count = rc;
519 else {
520 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200521 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300522 }
523 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200524 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300525 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200526 goto noperlayer_error;
527 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
528 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300529 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200530 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300531 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200532 goto noperlayer_error;
533 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
534 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300535 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200536 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300537 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200538 goto noperlayer_error;
539 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
540 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300541 c->layer[i].interleaving = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200542 }
543
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200544 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300545 if (rc < 0)
546 return rc;
547 if ((rc & 0x60) == 0x20) {
548 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200549 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300550 if (!c->isdbt_sb_segment_count)
551 c->isdbt_sb_segment_count = 1;
552 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200553
554 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200555 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300556 if (rc < 0)
557 return rc;
558 if ((rc & 0x60) == 0x20) {
559 switch (rc & 0x0c >> 2) {
560 case 0:
561 c->transmission_mode = TRANSMISSION_MODE_2K;
562 break;
563 case 1:
564 c->transmission_mode = TRANSMISSION_MODE_4K;
565 break;
566 case 2:
567 c->transmission_mode = TRANSMISSION_MODE_8K;
568 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200569 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300570 }
571 if (!(rc & 0x10)) {
572 switch (rc & 0x3) {
573 case 0:
574 c->guard_interval = GUARD_INTERVAL_1_4;
575 break;
576 case 1:
577 c->guard_interval = GUARD_INTERVAL_1_8;
578 break;
579 case 2:
580 c->guard_interval = GUARD_INTERVAL_1_16;
581 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200582 }
583 }
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300584 return 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200585
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200586noperlayer_error:
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300587
588 /* per-layer info is incomplete; discard all per-layer */
589 c->isdbt_layer_enabled = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300590
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300591 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300592}
593
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300594static int mb86a20s_reset_counters(struct dvb_frontend *fe)
595{
596 struct mb86a20s_state *state = fe->demodulator_priv;
597 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
598 int rc, val;
599
600 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
601
602 /* Reset the counters, if the channel changed */
603 if (state->last_frequency != c->frequency) {
604 memset(&c->strength, 0, sizeof(c->strength));
605 memset(&c->cnr, 0, sizeof(c->cnr));
606 memset(&c->pre_bit_error, 0, sizeof(c->pre_bit_error));
607 memset(&c->pre_bit_count, 0, sizeof(c->pre_bit_count));
608 memset(&c->block_error, 0, sizeof(c->block_error));
609 memset(&c->block_count, 0, sizeof(c->block_count));
610
611 state->last_frequency = c->frequency;
612 }
613
614 /* Clear status for most stats */
615
616 /* BER counter reset */
617 rc = mb86a20s_writeregdata(state, mb86a20s_vber_reset);
618 if (rc < 0)
619 goto err;
620
621 /* MER, PER counter reset */
622 rc = mb86a20s_writeregdata(state, mb86a20s_per_reset);
623 if (rc < 0)
624 goto err;
625
626 /* CNR counter reset */
627 rc = mb86a20s_readreg(state, 0x45);
628 if (rc < 0)
629 goto err;
630 val = rc;
631 rc = mb86a20s_writereg(state, 0x45, val | 0x10);
632 if (rc < 0)
633 goto err;
634 rc = mb86a20s_writereg(state, 0x45, val & 0x6f);
635 if (rc < 0)
636 goto err;
637
638 /* MER counter reset */
639 rc = mb86a20s_writereg(state, 0x50, 0x50);
640 if (rc < 0)
641 goto err;
642 rc = mb86a20s_readreg(state, 0x51);
643 if (rc < 0)
644 goto err;
645 val = rc;
646 rc = mb86a20s_writereg(state, 0x51, val | 0x01);
647 if (rc < 0)
648 goto err;
649 rc = mb86a20s_writereg(state, 0x51, val & 0x06);
650 if (rc < 0)
651 goto err;
652
653err:
654 return rc;
655}
656
657static void mb86a20s_stats_not_ready(struct dvb_frontend *fe)
658{
659 struct mb86a20s_state *state = fe->demodulator_priv;
660 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
661 int i;
662
663 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
664
665 /* Fill the length of each status counter */
666
667 /* Only global stats */
668 c->strength.len = 1;
669
670 /* Per-layer stats - 3 layers + global */
671 c->cnr.len = 4;
672 c->pre_bit_error.len = 4;
673 c->pre_bit_count.len = 4;
674 c->block_error.len = 4;
675 c->block_count.len = 4;
676
677 /* Signal is always available */
678 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
679 c->strength.stat[0].uvalue = 0;
680
681 /* Put all of them at FE_SCALE_NOT_AVAILABLE */
682 for (i = 0; i < 4; i++) {
683 c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
684 c->pre_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
685 c->pre_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
686 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
687 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
688 }
689}
690
691
692/*
693 * The functions below are called via DVB callbacks, so they need to
694 * properly use the I2C gate control
695 */
696
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200697static int mb86a20s_initfe(struct dvb_frontend *fe)
698{
699 struct mb86a20s_state *state = fe->demodulator_priv;
700 int rc;
701 u8 regD5 = 1;
702
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200703 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200704
705 if (fe->ops.i2c_gate_ctrl)
706 fe->ops.i2c_gate_ctrl(fe, 0);
707
708 /* Initialize the frontend */
709 rc = mb86a20s_writeregdata(state, mb86a20s_init);
710 if (rc < 0)
711 goto err;
712
713 if (!state->config->is_serial) {
714 regD5 &= ~1;
715
716 rc = mb86a20s_writereg(state, 0x50, 0xd5);
717 if (rc < 0)
718 goto err;
719 rc = mb86a20s_writereg(state, 0x51, regD5);
720 if (rc < 0)
721 goto err;
722 }
723
724err:
725 if (fe->ops.i2c_gate_ctrl)
726 fe->ops.i2c_gate_ctrl(fe, 1);
727
728 if (rc < 0) {
729 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200730 dev_info(&state->i2c->dev,
731 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200732 } else {
733 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200734 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200735 }
736 return rc;
737}
738
739static int mb86a20s_set_frontend(struct dvb_frontend *fe)
740{
741 struct mb86a20s_state *state = fe->demodulator_priv;
742 int rc;
743#if 0
744 /*
745 * FIXME: Properly implement the set frontend properties
746 */
747 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
748#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200749 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200750
751 /*
752 * Gate should already be opened, but it doesn't hurt to
753 * double-check
754 */
755 if (fe->ops.i2c_gate_ctrl)
756 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200757 fe->ops.tuner_ops.set_params(fe);
758
759 /*
760 * Make it more reliable: if, for some reason, the initial
761 * device initialization doesn't happen, initialize it when
762 * a SBTVD parameters are adjusted.
763 *
764 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
765 * the agc callback logic is not called during DVB attach time,
766 * causing mb86a20s to not be initialized with Kworld SBTVD.
767 * So, this hack is needed, in order to make Kworld SBTVD to work.
768 */
769 if (state->need_init)
770 mb86a20s_initfe(fe);
771
772 if (fe->ops.i2c_gate_ctrl)
773 fe->ops.i2c_gate_ctrl(fe, 0);
774 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300775 mb86a20s_reset_counters(fe);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200776 if (fe->ops.i2c_gate_ctrl)
777 fe->ops.i2c_gate_ctrl(fe, 1);
778
779 return rc;
780}
781
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300782static int mb86a20s_read_status_and_stats(struct dvb_frontend *fe,
783 fe_status_t *status)
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200784{
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300785 struct mb86a20s_state *state = fe->demodulator_priv;
786 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
787 int rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200788
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300789 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200790
791 if (fe->ops.i2c_gate_ctrl)
792 fe->ops.i2c_gate_ctrl(fe, 0);
793
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300794 /* Get lock */
795 rc = mb86a20s_read_status(fe, status);
796 if (!(*status & FE_HAS_LOCK)) {
797 mb86a20s_stats_not_ready(fe);
798 mb86a20s_reset_frontend_cache(fe);
799 }
800 if (rc < 0)
801 goto error;
802
803 /* Get signal strength */
804 rc = mb86a20s_read_signal_strength(fe);
805 if (rc < 0) {
806 mb86a20s_stats_not_ready(fe);
807 mb86a20s_reset_frontend_cache(fe);
808 goto error;
809 }
810 /* Fill signal strength */
811 c->strength.stat[0].uvalue = rc;
812
813 if (*status & FE_HAS_LOCK) {
814 /* Get TMCC info*/
815 rc = mb86a20s_get_frontend(fe);
816 if (rc < 0)
817 goto error;
818 }
819
820 mb86a20s_stats_not_ready(fe);
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200821
822 if (fe->ops.i2c_gate_ctrl)
823 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300824error:
825 return rc;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200826}
827
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300828static int mb86a20s_read_signal_strength_from_cache(struct dvb_frontend *fe,
829 u16 *strength)
830{
831 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
832
833
834 *strength = c->strength.stat[0].uvalue;
835
836 return 0;
837}
838
839static int mb86a20s_get_frontend_dummy(struct dvb_frontend *fe)
840{
841 /*
842 * get_frontend is now handled together with other stats
843 * retrival, when read_status() is called, as some statistics
844 * will depend on the layers detection.
845 */
846 return 0;
847};
848
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300849static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300850 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300851 unsigned int mode_flags,
852 unsigned int *delay,
853 fe_status_t *status)
854{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200855 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300856 int rc = 0;
857
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200858 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300859
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300860 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300861 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300862
863 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300864 mb86a20s_read_status_and_stats(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300865
866 return rc;
867}
868
869static void mb86a20s_release(struct dvb_frontend *fe)
870{
871 struct mb86a20s_state *state = fe->demodulator_priv;
872
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200873 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300874
875 kfree(state);
876}
877
878static struct dvb_frontend_ops mb86a20s_ops;
879
880struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
881 struct i2c_adapter *i2c)
882{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200883 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300884 u8 rev;
885
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -0200886 dev_dbg(&i2c->dev, "%s called.\n", __func__);
887
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300888 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200889 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300890 if (state == NULL) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -0200891 dev_err(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200892 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300893 goto error;
894 }
895
896 /* setup the state */
897 state->config = config;
898 state->i2c = i2c;
899
900 /* create dvb_frontend */
901 memcpy(&state->frontend.ops, &mb86a20s_ops,
902 sizeof(struct dvb_frontend_ops));
903 state->frontend.demodulator_priv = state;
904
905 /* Check if it is a mb86a20s frontend */
906 rev = mb86a20s_readreg(state, 0);
907
908 if (rev == 0x13) {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -0200909 dev_info(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200910 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300911 } else {
Mauro Carvalho Chehabf167e302013-01-23 13:22:22 -0200912 dev_dbg(&i2c->dev,
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200913 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300914 rev);
915 goto error;
916 }
917
918 return &state->frontend;
919
920error:
921 kfree(state);
922 return NULL;
923}
924EXPORT_SYMBOL(mb86a20s_attach);
925
926static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300927 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300928 /* Use dib8000 values per default */
929 .info = {
930 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300931 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
932 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
933 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
934 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
935 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
936 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
937 /* Actually, those values depend on the used tuner */
938 .frequency_min = 45000000,
939 .frequency_max = 864000000,
940 .frequency_stepsize = 62500,
941 },
942
943 .release = mb86a20s_release,
944
945 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300946 .set_frontend = mb86a20s_set_frontend,
Mauro Carvalho Chehab09b6d212013-01-22 12:28:31 -0300947 .get_frontend = mb86a20s_get_frontend_dummy,
948 .read_status = mb86a20s_read_status_and_stats,
949 .read_signal_strength = mb86a20s_read_signal_strength_from_cache,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300950 .tune = mb86a20s_tune,
951};
952
953MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
954MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
955MODULE_LICENSE("GPL");