blob: 38778a87702e76f19fc6378fefadab255807cb48 [file] [log] [blame]
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -03001/*
2 * Fujitu mb86a20s ISDB-T/ISDB-Tsb Module driver
3 *
4 * Copyright (C) 2010 Mauro Carvalho Chehab <mchehab@redhat.com>
5 * Copyright (C) 2009-2010 Douglas Landgraf <dougsland@redhat.com>
6 *
7 * FIXME: Need to port to DVB v5.2 API
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation version 2.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 */
18
19#include <linux/kernel.h>
20#include <asm/div64.h>
21
22#include "dvb_frontend.h"
23#include "mb86a20s.h"
24
25static int debug = 1;
26module_param(debug, int, 0644);
27MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
28
29#define rc(args...) do { \
30 printk(KERN_ERR "mb86a20s: " args); \
31} while (0)
32
33#define dprintk(args...) \
34 do { \
35 if (debug) { \
36 printk(KERN_DEBUG "mb86a20s: %s: ", __func__); \
37 printk(args); \
38 } \
39 } while (0)
40
41struct mb86a20s_state {
42 struct i2c_adapter *i2c;
43 const struct mb86a20s_config *config;
44
45 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030046
47 bool need_init;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030048};
49
50struct regdata {
51 u8 reg;
52 u8 data;
53};
54
55/*
56 * Initialization sequence: Use whatevere default values that PV SBTVD
57 * does on its initialisation, obtained via USB snoop
58 */
59static struct regdata mb86a20s_init[] = {
60 { 0x70, 0x0f },
61 { 0x70, 0xff },
62 { 0x08, 0x01 },
63 { 0x09, 0x3e },
64 { 0x50, 0xd1 },
65 { 0x51, 0x22 },
66 { 0x39, 0x01 },
67 { 0x71, 0x00 },
68 { 0x28, 0x2a },
69 { 0x29, 0x00 },
70 { 0x2a, 0xff },
71 { 0x2b, 0x80 },
72 { 0x28, 0x20 },
73 { 0x29, 0x33 },
74 { 0x2a, 0xdf },
75 { 0x2b, 0xa9 },
76 { 0x3b, 0x21 },
77 { 0x3c, 0x3a },
78 { 0x01, 0x0d },
79 { 0x04, 0x08 },
80 { 0x05, 0x05 },
81 { 0x04, 0x0e },
82 { 0x05, 0x00 },
83 { 0x04, 0x0f },
84 { 0x05, 0x14 },
85 { 0x04, 0x0b },
86 { 0x05, 0x8c },
87 { 0x04, 0x00 },
88 { 0x05, 0x00 },
89 { 0x04, 0x01 },
90 { 0x05, 0x07 },
91 { 0x04, 0x02 },
92 { 0x05, 0x0f },
93 { 0x04, 0x03 },
94 { 0x05, 0xa0 },
95 { 0x04, 0x09 },
96 { 0x05, 0x00 },
97 { 0x04, 0x0a },
98 { 0x05, 0xff },
99 { 0x04, 0x27 },
100 { 0x05, 0x64 },
101 { 0x04, 0x28 },
102 { 0x05, 0x00 },
103 { 0x04, 0x1e },
104 { 0x05, 0xff },
105 { 0x04, 0x29 },
106 { 0x05, 0x0a },
107 { 0x04, 0x32 },
108 { 0x05, 0x0a },
109 { 0x04, 0x14 },
110 { 0x05, 0x02 },
111 { 0x04, 0x04 },
112 { 0x05, 0x00 },
113 { 0x04, 0x05 },
114 { 0x05, 0x22 },
115 { 0x04, 0x06 },
116 { 0x05, 0x0e },
117 { 0x04, 0x07 },
118 { 0x05, 0xd8 },
119 { 0x04, 0x12 },
120 { 0x05, 0x00 },
121 { 0x04, 0x13 },
122 { 0x05, 0xff },
123 { 0x52, 0x01 },
124 { 0x50, 0xa7 },
125 { 0x51, 0x00 },
126 { 0x50, 0xa8 },
127 { 0x51, 0xff },
128 { 0x50, 0xa9 },
129 { 0x51, 0xff },
130 { 0x50, 0xaa },
131 { 0x51, 0x00 },
132 { 0x50, 0xab },
133 { 0x51, 0xff },
134 { 0x50, 0xac },
135 { 0x51, 0xff },
136 { 0x50, 0xad },
137 { 0x51, 0x00 },
138 { 0x50, 0xae },
139 { 0x51, 0xff },
140 { 0x50, 0xaf },
141 { 0x51, 0xff },
142 { 0x5e, 0x07 },
143 { 0x50, 0xdc },
144 { 0x51, 0x01 },
145 { 0x50, 0xdd },
146 { 0x51, 0xf4 },
147 { 0x50, 0xde },
148 { 0x51, 0x01 },
149 { 0x50, 0xdf },
150 { 0x51, 0xf4 },
151 { 0x50, 0xe0 },
152 { 0x51, 0x01 },
153 { 0x50, 0xe1 },
154 { 0x51, 0xf4 },
155 { 0x50, 0xb0 },
156 { 0x51, 0x07 },
157 { 0x50, 0xb2 },
158 { 0x51, 0xff },
159 { 0x50, 0xb3 },
160 { 0x51, 0xff },
161 { 0x50, 0xb4 },
162 { 0x51, 0xff },
163 { 0x50, 0xb5 },
164 { 0x51, 0xff },
165 { 0x50, 0xb6 },
166 { 0x51, 0xff },
167 { 0x50, 0xb7 },
168 { 0x51, 0xff },
169 { 0x50, 0x50 },
170 { 0x51, 0x02 },
171 { 0x50, 0x51 },
172 { 0x51, 0x04 },
173 { 0x45, 0x04 },
174 { 0x48, 0x04 },
175 { 0x50, 0xd5 },
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300176 { 0x51, 0x01 }, /* Serial */
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300177 { 0x50, 0xd6 },
178 { 0x51, 0x1f },
179 { 0x50, 0xd2 },
180 { 0x51, 0x03 },
181 { 0x50, 0xd7 },
182 { 0x51, 0x3f },
183 { 0x1c, 0x01 },
184 { 0x28, 0x06 },
185 { 0x29, 0x00 },
186 { 0x2a, 0x00 },
187 { 0x2b, 0x03 },
188 { 0x28, 0x07 },
189 { 0x29, 0x00 },
190 { 0x2a, 0x00 },
191 { 0x2b, 0x0d },
192 { 0x28, 0x08 },
193 { 0x29, 0x00 },
194 { 0x2a, 0x00 },
195 { 0x2b, 0x02 },
196 { 0x28, 0x09 },
197 { 0x29, 0x00 },
198 { 0x2a, 0x00 },
199 { 0x2b, 0x01 },
200 { 0x28, 0x0a },
201 { 0x29, 0x00 },
202 { 0x2a, 0x00 },
203 { 0x2b, 0x21 },
204 { 0x28, 0x0b },
205 { 0x29, 0x00 },
206 { 0x2a, 0x00 },
207 { 0x2b, 0x29 },
208 { 0x28, 0x0c },
209 { 0x29, 0x00 },
210 { 0x2a, 0x00 },
211 { 0x2b, 0x16 },
212 { 0x28, 0x0d },
213 { 0x29, 0x00 },
214 { 0x2a, 0x00 },
215 { 0x2b, 0x31 },
216 { 0x28, 0x0e },
217 { 0x29, 0x00 },
218 { 0x2a, 0x00 },
219 { 0x2b, 0x0e },
220 { 0x28, 0x0f },
221 { 0x29, 0x00 },
222 { 0x2a, 0x00 },
223 { 0x2b, 0x4e },
224 { 0x28, 0x10 },
225 { 0x29, 0x00 },
226 { 0x2a, 0x00 },
227 { 0x2b, 0x46 },
228 { 0x28, 0x11 },
229 { 0x29, 0x00 },
230 { 0x2a, 0x00 },
231 { 0x2b, 0x0f },
232 { 0x28, 0x12 },
233 { 0x29, 0x00 },
234 { 0x2a, 0x00 },
235 { 0x2b, 0x56 },
236 { 0x28, 0x13 },
237 { 0x29, 0x00 },
238 { 0x2a, 0x00 },
239 { 0x2b, 0x35 },
240 { 0x28, 0x14 },
241 { 0x29, 0x00 },
242 { 0x2a, 0x01 },
243 { 0x2b, 0xbe },
244 { 0x28, 0x15 },
245 { 0x29, 0x00 },
246 { 0x2a, 0x01 },
247 { 0x2b, 0x84 },
248 { 0x28, 0x16 },
249 { 0x29, 0x00 },
250 { 0x2a, 0x03 },
251 { 0x2b, 0xee },
252 { 0x28, 0x17 },
253 { 0x29, 0x00 },
254 { 0x2a, 0x00 },
255 { 0x2b, 0x98 },
256 { 0x28, 0x18 },
257 { 0x29, 0x00 },
258 { 0x2a, 0x00 },
259 { 0x2b, 0x9f },
260 { 0x28, 0x19 },
261 { 0x29, 0x00 },
262 { 0x2a, 0x07 },
263 { 0x2b, 0xb2 },
264 { 0x28, 0x1a },
265 { 0x29, 0x00 },
266 { 0x2a, 0x06 },
267 { 0x2b, 0xc2 },
268 { 0x28, 0x1b },
269 { 0x29, 0x00 },
270 { 0x2a, 0x07 },
271 { 0x2b, 0x4a },
272 { 0x28, 0x1c },
273 { 0x29, 0x00 },
274 { 0x2a, 0x01 },
275 { 0x2b, 0xbc },
276 { 0x28, 0x1d },
277 { 0x29, 0x00 },
278 { 0x2a, 0x04 },
279 { 0x2b, 0xba },
280 { 0x28, 0x1e },
281 { 0x29, 0x00 },
282 { 0x2a, 0x06 },
283 { 0x2b, 0x14 },
284 { 0x50, 0x1e },
285 { 0x51, 0x5d },
286 { 0x50, 0x22 },
287 { 0x51, 0x00 },
288 { 0x50, 0x23 },
289 { 0x51, 0xc8 },
290 { 0x50, 0x24 },
291 { 0x51, 0x00 },
292 { 0x50, 0x25 },
293 { 0x51, 0xf0 },
294 { 0x50, 0x26 },
295 { 0x51, 0x00 },
296 { 0x50, 0x27 },
297 { 0x51, 0xc3 },
298 { 0x50, 0x39 },
299 { 0x51, 0x02 },
300 { 0x50, 0xd5 },
301 { 0x51, 0x01 },
302 { 0xd0, 0x00 },
303};
304
305static struct regdata mb86a20s_reset_reception[] = {
306 { 0x70, 0xf0 },
307 { 0x70, 0xff },
308 { 0x08, 0x01 },
309 { 0x08, 0x00 },
310};
311
312static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
313 u8 i2c_addr, int reg, int data)
314{
315 u8 buf[] = { reg, data };
316 struct i2c_msg msg = {
317 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
318 };
319 int rc;
320
321 rc = i2c_transfer(state->i2c, &msg, 1);
322 if (rc != 1) {
Mauro Carvalho Chehab75708002011-01-14 08:50:00 -0300323 printk("%s: writereg error (rc == %i, reg == 0x%02x,"
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300324 " data == 0x%02x)\n", __func__, rc, reg, data);
325 return rc;
326 }
327
328 return 0;
329}
330
331static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
332 u8 i2c_addr, struct regdata *rd, int size)
333{
334 int i, rc;
335
336 for (i = 0; i < size; i++) {
337 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
338 rd[i].data);
339 if (rc < 0)
340 return rc;
341 }
342 return 0;
343}
344
345static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
346 u8 i2c_addr, u8 reg)
347{
348 u8 val;
349 int rc;
350 struct i2c_msg msg[] = {
351 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
352 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
353 };
354
355 rc = i2c_transfer(state->i2c, msg, 2);
356
357 if (rc != 2) {
Mauro Carvalho Chehab75708002011-01-14 08:50:00 -0300358 rc("%s: reg=0x%x (error=%d)\n", __func__, reg, rc);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300359 return rc;
360 }
361
362 return val;
363}
364
365#define mb86a20s_readreg(state, reg) \
366 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
367#define mb86a20s_writereg(state, reg, val) \
368 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
369#define mb86a20s_writeregdata(state, regdata) \
370 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
371 regdata, ARRAY_SIZE(regdata))
372
373static int mb86a20s_initfe(struct dvb_frontend *fe)
374{
375 struct mb86a20s_state *state = fe->demodulator_priv;
376 int rc;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300377 u8 regD5 = 1;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300378
379 dprintk("\n");
380
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300381 if (fe->ops.i2c_gate_ctrl)
382 fe->ops.i2c_gate_ctrl(fe, 0);
383
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300384 /* Initialize the frontend */
385 rc = mb86a20s_writeregdata(state, mb86a20s_init);
386 if (rc < 0)
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300387 goto err;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300388
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300389 if (!state->config->is_serial) {
390 regD5 &= ~1;
391
392 rc = mb86a20s_writereg(state, 0x50, 0xd5);
393 if (rc < 0)
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300394 goto err;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300395 rc = mb86a20s_writereg(state, 0x51, regD5);
396 if (rc < 0)
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300397 goto err;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300398 }
399
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300400 if (fe->ops.i2c_gate_ctrl)
401 fe->ops.i2c_gate_ctrl(fe, 1);
402
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300403err:
404 if (rc < 0) {
405 state->need_init = true;
406 printk(KERN_INFO "mb86a20s: Init failed. Will try again later\n");
407 } else {
408 state->need_init = false;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300409 dprintk("Initialization succeeded.\n");
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300410 }
411 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300412}
413
414static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
415{
416 struct mb86a20s_state *state = fe->demodulator_priv;
417 unsigned rf_max, rf_min, rf;
418 u8 val;
419
420 dprintk("\n");
421
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300422 if (fe->ops.i2c_gate_ctrl)
423 fe->ops.i2c_gate_ctrl(fe, 0);
424
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300425 /* Does a binary search to get RF strength */
426 rf_max = 0xfff;
427 rf_min = 0;
428 do {
429 rf = (rf_max + rf_min) / 2;
430 mb86a20s_writereg(state, 0x04, 0x1f);
431 mb86a20s_writereg(state, 0x05, rf >> 8);
432 mb86a20s_writereg(state, 0x04, 0x20);
433 mb86a20s_writereg(state, 0x04, rf);
434
435 val = mb86a20s_readreg(state, 0x02);
436 if (val & 0x08)
437 rf_min = (rf_max + rf_min) / 2;
438 else
439 rf_max = (rf_max + rf_min) / 2;
440 if (rf_max - rf_min < 4) {
441 *strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
442 break;
443 }
444 } while (1);
445
446 dprintk("signal strength = %d\n", *strength);
447
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300448 if (fe->ops.i2c_gate_ctrl)
449 fe->ops.i2c_gate_ctrl(fe, 1);
450
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300451 return 0;
452}
453
454static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
455{
456 struct mb86a20s_state *state = fe->demodulator_priv;
457 u8 val;
458
459 dprintk("\n");
460 *status = 0;
461
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300462 if (fe->ops.i2c_gate_ctrl)
463 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300464 val = mb86a20s_readreg(state, 0x0a) & 0xf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300465 if (fe->ops.i2c_gate_ctrl)
466 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300467
468 if (val >= 2)
469 *status |= FE_HAS_SIGNAL;
470
471 if (val >= 4)
472 *status |= FE_HAS_CARRIER;
473
474 if (val >= 5)
475 *status |= FE_HAS_VITERBI;
476
477 if (val >= 7)
478 *status |= FE_HAS_SYNC;
479
480 if (val >= 8) /* Maybe 9? */
481 *status |= FE_HAS_LOCK;
482
483 dprintk("val = %d, status = 0x%02x\n", val, *status);
484
485 return 0;
486}
487
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300488static int mb86a20s_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300489{
490 struct mb86a20s_state *state = fe->demodulator_priv;
491 int rc;
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300492#if 0
493 /*
494 * FIXME: Properly implement the set frontend properties
495 */
496 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
497#endif
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300498
499 dprintk("\n");
500
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300501 if (fe->ops.i2c_gate_ctrl)
502 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300503 dprintk("Calling tuner set parameters\n");
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300504 fe->ops.tuner_ops.set_params(fe);
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300505
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300506 /*
507 * Make it more reliable: if, for some reason, the initial
508 * device initialization doesn't happen, initialize it when
509 * a SBTVD parameters are adjusted.
510 *
511 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
512 * the agc callback logic is not called during DVB attach time,
513 * causing mb86a20s to not be initialized with Kworld SBTVD.
514 * So, this hack is needed, in order to make Kworld SBTVD to work.
515 */
516 if (state->need_init)
517 mb86a20s_initfe(fe);
518
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300519 if (fe->ops.i2c_gate_ctrl)
520 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300521 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300522 if (fe->ops.i2c_gate_ctrl)
523 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300524
525 return rc;
526}
527
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200528static int mb86a20s_get_modulation(struct mb86a20s_state *state,
529 unsigned layer)
530{
531 int rc;
532 static unsigned char reg[] = {
533 [0] = 0x86, /* Layer A */
534 [1] = 0x8a, /* Layer B */
535 [2] = 0x8e, /* Layer C */
536 };
537
538 if (layer > ARRAY_SIZE(reg))
539 return -EINVAL;
540 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
541 if (rc < 0)
542 return rc;
543 rc = mb86a20s_readreg(state, 0x6e);
544 if (rc < 0)
545 return rc;
546 switch ((rc & 0x70) >> 4) {
547 case 0:
548 return DQPSK;
549 case 1:
550 return QPSK;
551 case 2:
552 return QAM_16;
553 case 3:
554 return QAM_64;
555 default:
556 return QAM_AUTO;
557 }
558}
559
560static int mb86a20s_get_fec(struct mb86a20s_state *state,
561 unsigned layer)
562{
563 int rc;
564
565 static unsigned char reg[] = {
566 [0] = 0x87, /* Layer A */
567 [1] = 0x8b, /* Layer B */
568 [2] = 0x8f, /* Layer C */
569 };
570
571 if (layer > ARRAY_SIZE(reg))
572 return -EINVAL;
573 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
574 if (rc < 0)
575 return rc;
576 rc = mb86a20s_readreg(state, 0x6e);
577 if (rc < 0)
578 return rc;
579 switch (rc) {
580 case 0:
581 return FEC_1_2;
582 case 1:
583 return FEC_2_3;
584 case 2:
585 return FEC_3_4;
586 case 3:
587 return FEC_5_6;
588 case 4:
589 return FEC_7_8;
590 default:
591 return FEC_AUTO;
592 }
593}
594
595static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
596 unsigned layer)
597{
598 int rc;
599
600 static unsigned char reg[] = {
601 [0] = 0x88, /* Layer A */
602 [1] = 0x8c, /* Layer B */
603 [2] = 0x90, /* Layer C */
604 };
605
606 if (layer > ARRAY_SIZE(reg))
607 return -EINVAL;
608 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
609 if (rc < 0)
610 return rc;
611 rc = mb86a20s_readreg(state, 0x6e);
612 if (rc < 0)
613 return rc;
614 if (rc > 3)
615 return -EINVAL; /* Not used */
616 return rc;
617}
618
619static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
620 unsigned layer)
621{
622 int rc, count;
623
624 static unsigned char reg[] = {
625 [0] = 0x89, /* Layer A */
626 [1] = 0x8d, /* Layer B */
627 [2] = 0x91, /* Layer C */
628 };
629
630 if (layer > ARRAY_SIZE(reg))
631 return -EINVAL;
632 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
633 if (rc < 0)
634 return rc;
635 rc = mb86a20s_readreg(state, 0x6e);
636 if (rc < 0)
637 return rc;
638 count = (rc >> 4) & 0x0f;
639
640 return count;
641}
642
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300643static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300644{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200645 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300646 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200647 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300648
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200649 /* Fixed parameters */
650 p->delivery_system = SYS_ISDBT;
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300651 p->bandwidth_hz = 6000000;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200652
653 if (fe->ops.i2c_gate_ctrl)
654 fe->ops.i2c_gate_ctrl(fe, 0);
655
656 /* Check for partial reception */
657 rc = mb86a20s_writereg(state, 0x6d, 0x85);
658 if (rc >= 0)
659 rc = mb86a20s_readreg(state, 0x6e);
660 if (rc >= 0)
661 p->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
662
663 /* Get per-layer data */
664 p->isdbt_layer_enabled = 0;
665 for (i = 0; i < 3; i++) {
666 rc = mb86a20s_get_segment_count(state, i);
667 if (rc >= 0 && rc < 14)
668 p->layer[i].segment_count = rc;
669 if (rc == 0x0f)
670 continue;
671 p->isdbt_layer_enabled |= 1 << i;
672 rc = mb86a20s_get_modulation(state, i);
673 if (rc >= 0)
674 p->layer[i].modulation = rc;
675 rc = mb86a20s_get_fec(state, i);
676 if (rc >= 0)
677 p->layer[i].fec = rc;
678 rc = mb86a20s_get_interleaving(state, i);
679 if (rc >= 0)
680 p->layer[i].interleaving = rc;
681 }
682
683 p->isdbt_sb_mode = 0;
684 rc = mb86a20s_writereg(state, 0x6d, 0x84);
685 if ((rc >= 0) && ((rc & 0x60) == 0x20)) {
686 p->isdbt_sb_mode = 1;
687 /* At least, one segment should exist */
688 if (!p->isdbt_sb_segment_count)
689 p->isdbt_sb_segment_count = 1;
690 } else
691 p->isdbt_sb_segment_count = 0;
692
693 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300694 p->transmission_mode = TRANSMISSION_MODE_AUTO;
695 p->guard_interval = GUARD_INTERVAL_AUTO;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200696 rc = mb86a20s_readreg(state, 0x07);
697 if (rc >= 0) {
698 if ((rc & 0x60) == 0x20) {
699 switch (rc & 0x0c >> 2) {
700 case 0:
701 p->transmission_mode = TRANSMISSION_MODE_2K;
702 break;
703 case 1:
704 p->transmission_mode = TRANSMISSION_MODE_4K;
705 break;
706 case 2:
707 p->transmission_mode = TRANSMISSION_MODE_8K;
708 break;
709 }
710 }
711 if (!(rc & 0x10)) {
712 switch (rc & 0x3) {
713 case 0:
714 p->guard_interval = GUARD_INTERVAL_1_4;
715 break;
716 case 1:
717 p->guard_interval = GUARD_INTERVAL_1_8;
718 break;
719 case 2:
720 p->guard_interval = GUARD_INTERVAL_1_16;
721 break;
722 }
723 }
724 }
725
726 if (fe->ops.i2c_gate_ctrl)
727 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300728
729 return 0;
730}
731
732static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300733 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300734 unsigned int mode_flags,
735 unsigned int *delay,
736 fe_status_t *status)
737{
738 int rc = 0;
739
740 dprintk("\n");
741
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300742 if (re_tune)
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300743 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300744
745 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
746 mb86a20s_read_status(fe, status);
747
748 return rc;
749}
750
751static void mb86a20s_release(struct dvb_frontend *fe)
752{
753 struct mb86a20s_state *state = fe->demodulator_priv;
754
755 dprintk("\n");
756
757 kfree(state);
758}
759
760static struct dvb_frontend_ops mb86a20s_ops;
761
762struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
763 struct i2c_adapter *i2c)
764{
765 u8 rev;
766
767 /* allocate memory for the internal state */
768 struct mb86a20s_state *state =
769 kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
770
771 dprintk("\n");
772 if (state == NULL) {
773 rc("Unable to kzalloc\n");
774 goto error;
775 }
776
777 /* setup the state */
778 state->config = config;
779 state->i2c = i2c;
780
781 /* create dvb_frontend */
782 memcpy(&state->frontend.ops, &mb86a20s_ops,
783 sizeof(struct dvb_frontend_ops));
784 state->frontend.demodulator_priv = state;
785
786 /* Check if it is a mb86a20s frontend */
787 rev = mb86a20s_readreg(state, 0);
788
789 if (rev == 0x13) {
790 printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
791 } else {
792 printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
793 rev);
794 goto error;
795 }
796
797 return &state->frontend;
798
799error:
800 kfree(state);
801 return NULL;
802}
803EXPORT_SYMBOL(mb86a20s_attach);
804
805static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300806 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300807 /* Use dib8000 values per default */
808 .info = {
809 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300810 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
811 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
812 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
813 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
814 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
815 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
816 /* Actually, those values depend on the used tuner */
817 .frequency_min = 45000000,
818 .frequency_max = 864000000,
819 .frequency_stepsize = 62500,
820 },
821
822 .release = mb86a20s_release,
823
824 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e222011-12-26 12:11:51 -0300825 .set_frontend = mb86a20s_set_frontend,
826 .get_frontend = mb86a20s_get_frontend,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300827 .read_status = mb86a20s_read_status,
828 .read_signal_strength = mb86a20s_read_signal_strength,
829 .tune = mb86a20s_tune,
830};
831
832MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
833MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
834MODULE_LICENSE("GPL");