blob: 03b74d3afd8ff9fb269c7f62a0d559363883c41c [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
27#define rc(args...) do { \
28 printk(KERN_ERR "mb86a20s: " args); \
29} while (0)
30
31#define dprintk(args...) \
32 do { \
33 if (debug) { \
34 printk(KERN_DEBUG "mb86a20s: %s: ", __func__); \
35 printk(args); \
36 } \
37 } while (0)
38
39struct mb86a20s_state {
40 struct i2c_adapter *i2c;
41 const struct mb86a20s_config *config;
42
43 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030044
45 bool need_init;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030046};
47
48struct regdata {
49 u8 reg;
50 u8 data;
51};
52
53/*
54 * Initialization sequence: Use whatevere default values that PV SBTVD
55 * does on its initialisation, obtained via USB snoop
56 */
57static struct regdata mb86a20s_init[] = {
58 { 0x70, 0x0f },
59 { 0x70, 0xff },
60 { 0x08, 0x01 },
61 { 0x09, 0x3e },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020062 { 0x50, 0xd1 }, { 0x51, 0x22 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030063 { 0x39, 0x01 },
64 { 0x71, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020065 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
66 { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020067 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030068 { 0x3b, 0x21 },
69 { 0x3c, 0x3a },
70 { 0x01, 0x0d },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020071 { 0x04, 0x08 }, { 0x05, 0x05 },
72 { 0x04, 0x0e }, { 0x05, 0x00 },
73 { 0x04, 0x0f }, { 0x05, 0x14 },
74 { 0x04, 0x0b }, { 0x05, 0x8c },
75 { 0x04, 0x00 }, { 0x05, 0x00 },
76 { 0x04, 0x01 }, { 0x05, 0x07 },
77 { 0x04, 0x02 }, { 0x05, 0x0f },
78 { 0x04, 0x03 }, { 0x05, 0xa0 },
79 { 0x04, 0x09 }, { 0x05, 0x00 },
80 { 0x04, 0x0a }, { 0x05, 0xff },
81 { 0x04, 0x27 }, { 0x05, 0x64 },
82 { 0x04, 0x28 }, { 0x05, 0x00 },
83 { 0x04, 0x1e }, { 0x05, 0xff },
84 { 0x04, 0x29 }, { 0x05, 0x0a },
85 { 0x04, 0x32 }, { 0x05, 0x0a },
86 { 0x04, 0x14 }, { 0x05, 0x02 },
87 { 0x04, 0x04 }, { 0x05, 0x00 },
88 { 0x04, 0x05 }, { 0x05, 0x22 },
89 { 0x04, 0x06 }, { 0x05, 0x0e },
90 { 0x04, 0x07 }, { 0x05, 0xd8 },
91 { 0x04, 0x12 }, { 0x05, 0x00 },
92 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020093 { 0x04, 0x15 }, { 0x05, 0x4e },
94 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030095 { 0x52, 0x01 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020096 { 0x50, 0xa7 }, { 0x51, 0xff },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020097 { 0x50, 0xa8 }, { 0x51, 0xff },
98 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020099 { 0x50, 0xaa }, { 0x51, 0xff },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200100 { 0x50, 0xab }, { 0x51, 0xff },
101 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200102 { 0x50, 0xad }, { 0x51, 0xff },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200103 { 0x50, 0xae }, { 0x51, 0xff },
104 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300105 { 0x5e, 0x07 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200106 { 0x50, 0xdc }, { 0x51, 0x01 },
107 { 0x50, 0xdd }, { 0x51, 0xf4 },
108 { 0x50, 0xde }, { 0x51, 0x01 },
109 { 0x50, 0xdf }, { 0x51, 0xf4 },
110 { 0x50, 0xe0 }, { 0x51, 0x01 },
111 { 0x50, 0xe1 }, { 0x51, 0xf4 },
112 { 0x50, 0xb0 }, { 0x51, 0x07 },
113 { 0x50, 0xb2 }, { 0x51, 0xff },
114 { 0x50, 0xb3 }, { 0x51, 0xff },
115 { 0x50, 0xb4 }, { 0x51, 0xff },
116 { 0x50, 0xb5 }, { 0x51, 0xff },
117 { 0x50, 0xb6 }, { 0x51, 0xff },
118 { 0x50, 0xb7 }, { 0x51, 0xff },
119 { 0x50, 0x50 }, { 0x51, 0x02 },
120 { 0x50, 0x51 }, { 0x51, 0x04 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300121 { 0x45, 0x04 },
122 { 0x48, 0x04 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200123 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
124 { 0x50, 0xd6 }, { 0x51, 0x1f },
125 { 0x50, 0xd2 }, { 0x51, 0x03 },
126 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200127 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
128 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300129
130 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200131 { 0x28, 0x00 }, { 0x29, 0x10 },
132 { 0x28, 0x05 }, { 0x29, 0x02 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300133 { 0x1c, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200134 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
135 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
136 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
137 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
138 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
139 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
140 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
141 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
142 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
143 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
144 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
145 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
146 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
147 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
148 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
149 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
150 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
151 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
152 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
153 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
154 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
155 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
156 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
157 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
158 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
159 { 0x50, 0x1e }, { 0x51, 0x5d },
160 { 0x50, 0x22 }, { 0x51, 0x00 },
161 { 0x50, 0x23 }, { 0x51, 0xc8 },
162 { 0x50, 0x24 }, { 0x51, 0x00 },
163 { 0x50, 0x25 }, { 0x51, 0xf0 },
164 { 0x50, 0x26 }, { 0x51, 0x00 },
165 { 0x50, 0x27 }, { 0x51, 0xc3 },
166 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200167 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300168 { 0xd0, 0x00 },
169};
170
171static struct regdata mb86a20s_reset_reception[] = {
172 { 0x70, 0xf0 },
173 { 0x70, 0xff },
174 { 0x08, 0x01 },
175 { 0x08, 0x00 },
176};
177
178static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
179 u8 i2c_addr, int reg, int data)
180{
181 u8 buf[] = { reg, data };
182 struct i2c_msg msg = {
183 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
184 };
185 int rc;
186
187 rc = i2c_transfer(state->i2c, &msg, 1);
188 if (rc != 1) {
Mauro Carvalho Chehab75708002011-01-14 08:50:00 -0300189 printk("%s: writereg error (rc == %i, reg == 0x%02x,"
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300190 " data == 0x%02x)\n", __func__, rc, reg, data);
191 return rc;
192 }
193
194 return 0;
195}
196
197static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
198 u8 i2c_addr, struct regdata *rd, int size)
199{
200 int i, rc;
201
202 for (i = 0; i < size; i++) {
203 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
204 rd[i].data);
205 if (rc < 0)
206 return rc;
207 }
208 return 0;
209}
210
211static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
212 u8 i2c_addr, u8 reg)
213{
214 u8 val;
215 int rc;
216 struct i2c_msg msg[] = {
217 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
218 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
219 };
220
221 rc = i2c_transfer(state->i2c, msg, 2);
222
223 if (rc != 2) {
Mauro Carvalho Chehab75708002011-01-14 08:50:00 -0300224 rc("%s: reg=0x%x (error=%d)\n", __func__, reg, rc);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300225 return rc;
226 }
227
228 return val;
229}
230
231#define mb86a20s_readreg(state, reg) \
232 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
233#define mb86a20s_writereg(state, reg, val) \
234 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
235#define mb86a20s_writeregdata(state, regdata) \
236 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
237 regdata, ARRAY_SIZE(regdata))
238
239static int mb86a20s_initfe(struct dvb_frontend *fe)
240{
241 struct mb86a20s_state *state = fe->demodulator_priv;
242 int rc;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300243 u8 regD5 = 1;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300244
245 dprintk("\n");
246
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300247 if (fe->ops.i2c_gate_ctrl)
248 fe->ops.i2c_gate_ctrl(fe, 0);
249
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300250 /* Initialize the frontend */
251 rc = mb86a20s_writeregdata(state, mb86a20s_init);
252 if (rc < 0)
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300253 goto err;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300254
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300255 if (!state->config->is_serial) {
256 regD5 &= ~1;
257
258 rc = mb86a20s_writereg(state, 0x50, 0xd5);
259 if (rc < 0)
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300260 goto err;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300261 rc = mb86a20s_writereg(state, 0x51, regD5);
262 if (rc < 0)
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300263 goto err;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300264 }
265
Mauro Carvalho Chehabfd537442013-01-14 10:16:07 -0300266err:
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300267 if (fe->ops.i2c_gate_ctrl)
268 fe->ops.i2c_gate_ctrl(fe, 1);
269
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300270 if (rc < 0) {
271 state->need_init = true;
272 printk(KERN_INFO "mb86a20s: Init failed. Will try again later\n");
273 } else {
274 state->need_init = false;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300275 dprintk("Initialization succeeded.\n");
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300276 }
277 return rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300278}
279
280static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
281{
282 struct mb86a20s_state *state = fe->demodulator_priv;
283 unsigned rf_max, rf_min, rf;
284 u8 val;
285
286 dprintk("\n");
287
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300288 if (fe->ops.i2c_gate_ctrl)
289 fe->ops.i2c_gate_ctrl(fe, 0);
290
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300291 /* Does a binary search to get RF strength */
292 rf_max = 0xfff;
293 rf_min = 0;
294 do {
295 rf = (rf_max + rf_min) / 2;
296 mb86a20s_writereg(state, 0x04, 0x1f);
297 mb86a20s_writereg(state, 0x05, rf >> 8);
298 mb86a20s_writereg(state, 0x04, 0x20);
299 mb86a20s_writereg(state, 0x04, rf);
300
301 val = mb86a20s_readreg(state, 0x02);
302 if (val & 0x08)
303 rf_min = (rf_max + rf_min) / 2;
304 else
305 rf_max = (rf_max + rf_min) / 2;
306 if (rf_max - rf_min < 4) {
307 *strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
308 break;
309 }
310 } while (1);
311
312 dprintk("signal strength = %d\n", *strength);
313
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300314 if (fe->ops.i2c_gate_ctrl)
315 fe->ops.i2c_gate_ctrl(fe, 1);
316
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300317 return 0;
318}
319
320static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
321{
322 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200323 int val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300324
325 dprintk("\n");
326 *status = 0;
327
328 val = mb86a20s_readreg(state, 0x0a) & 0xf;
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200329 if (val < 0)
330 return val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300331
332 if (val >= 2)
333 *status |= FE_HAS_SIGNAL;
334
335 if (val >= 4)
336 *status |= FE_HAS_CARRIER;
337
338 if (val >= 5)
339 *status |= FE_HAS_VITERBI;
340
341 if (val >= 7)
342 *status |= FE_HAS_SYNC;
343
344 if (val >= 8) /* Maybe 9? */
345 *status |= FE_HAS_LOCK;
346
347 dprintk("val = %d, status = 0x%02x\n", val, *status);
348
349 return 0;
350}
351
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300352static int mb86a20s_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300353{
354 struct mb86a20s_state *state = fe->demodulator_priv;
355 int rc;
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300356#if 0
357 /*
358 * FIXME: Properly implement the set frontend properties
359 */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300360 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300361#endif
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300362
363 dprintk("\n");
364
Mauro Carvalho Chehabfd537442013-01-14 10:16:07 -0300365 /*
366 * Gate should already be opened, but it doesn't hurt to
367 * double-check
368 */
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300369 if (fe->ops.i2c_gate_ctrl)
370 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300371 dprintk("Calling tuner set parameters\n");
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300372 fe->ops.tuner_ops.set_params(fe);
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300373
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300374 /*
375 * Make it more reliable: if, for some reason, the initial
376 * device initialization doesn't happen, initialize it when
377 * a SBTVD parameters are adjusted.
378 *
379 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
380 * the agc callback logic is not called during DVB attach time,
381 * causing mb86a20s to not be initialized with Kworld SBTVD.
382 * So, this hack is needed, in order to make Kworld SBTVD to work.
383 */
384 if (state->need_init)
385 mb86a20s_initfe(fe);
386
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300387 if (fe->ops.i2c_gate_ctrl)
388 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300389 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300390 if (fe->ops.i2c_gate_ctrl)
391 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300392
393 return rc;
394}
395
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200396static int mb86a20s_get_modulation(struct mb86a20s_state *state,
397 unsigned layer)
398{
399 int rc;
400 static unsigned char reg[] = {
401 [0] = 0x86, /* Layer A */
402 [1] = 0x8a, /* Layer B */
403 [2] = 0x8e, /* Layer C */
404 };
405
Dan Carpenter82033bc2012-01-13 02:28:34 -0300406 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200407 return -EINVAL;
408 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
409 if (rc < 0)
410 return rc;
411 rc = mb86a20s_readreg(state, 0x6e);
412 if (rc < 0)
413 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300414 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200415 case 0:
416 return DQPSK;
417 case 1:
418 return QPSK;
419 case 2:
420 return QAM_16;
421 case 3:
422 return QAM_64;
423 default:
424 return QAM_AUTO;
425 }
426}
427
428static int mb86a20s_get_fec(struct mb86a20s_state *state,
429 unsigned layer)
430{
431 int rc;
432
433 static unsigned char reg[] = {
434 [0] = 0x87, /* Layer A */
435 [1] = 0x8b, /* Layer B */
436 [2] = 0x8f, /* Layer C */
437 };
438
Dan Carpenter82033bc2012-01-13 02:28:34 -0300439 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200440 return -EINVAL;
441 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
442 if (rc < 0)
443 return rc;
444 rc = mb86a20s_readreg(state, 0x6e);
445 if (rc < 0)
446 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300447 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200448 case 0:
449 return FEC_1_2;
450 case 1:
451 return FEC_2_3;
452 case 2:
453 return FEC_3_4;
454 case 3:
455 return FEC_5_6;
456 case 4:
457 return FEC_7_8;
458 default:
459 return FEC_AUTO;
460 }
461}
462
463static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
464 unsigned layer)
465{
466 int rc;
467
468 static unsigned char reg[] = {
469 [0] = 0x88, /* Layer A */
470 [1] = 0x8c, /* Layer B */
471 [2] = 0x90, /* Layer C */
472 };
473
Dan Carpenter82033bc2012-01-13 02:28:34 -0300474 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200475 return -EINVAL;
476 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
477 if (rc < 0)
478 return rc;
479 rc = mb86a20s_readreg(state, 0x6e);
480 if (rc < 0)
481 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300482
483 switch ((rc >> 4) & 0x07) {
484 case 1:
485 return GUARD_INTERVAL_1_4;
486 case 2:
487 return GUARD_INTERVAL_1_8;
488 case 3:
489 return GUARD_INTERVAL_1_16;
490 case 4:
491 return GUARD_INTERVAL_1_32;
492
493 default:
494 case 0:
495 return GUARD_INTERVAL_AUTO;
496 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200497}
498
499static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
500 unsigned layer)
501{
502 int rc, count;
503
504 static unsigned char reg[] = {
505 [0] = 0x89, /* Layer A */
506 [1] = 0x8d, /* Layer B */
507 [2] = 0x91, /* Layer C */
508 };
509
Dan Carpenter82033bc2012-01-13 02:28:34 -0300510 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200511 return -EINVAL;
512 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
513 if (rc < 0)
514 return rc;
515 rc = mb86a20s_readreg(state, 0x6e);
516 if (rc < 0)
517 return rc;
518 count = (rc >> 4) & 0x0f;
519
520 return count;
521}
522
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300523static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
524{
525 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
526
527 /* Fixed parameters */
528 c->delivery_system = SYS_ISDBT;
529 c->bandwidth_hz = 6000000;
530
531 /* Initialize values that will be later autodetected */
532 c->isdbt_layer_enabled = 0;
533 c->transmission_mode = TRANSMISSION_MODE_AUTO;
534 c->guard_interval = GUARD_INTERVAL_AUTO;
535 c->isdbt_sb_mode = 0;
536 c->isdbt_sb_segment_count = 0;
537}
538
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300539static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300540{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200541 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300542 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200543 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300544
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300545 /* Reset frontend cache to default values */
546 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200547
548 if (fe->ops.i2c_gate_ctrl)
549 fe->ops.i2c_gate_ctrl(fe, 0);
550
551 /* Check for partial reception */
552 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300553 if (rc < 0)
554 return rc;
555 rc = mb86a20s_readreg(state, 0x6e);
556 if (rc < 0)
557 return rc;
558 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200559
560 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300561
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200562 for (i = 0; i < 3; i++) {
563 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300564 if (rc < 0)
565 goto error;
566 if (rc >= 0 && rc < 14)
567 c->layer[i].segment_count = rc;
568 else {
569 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200570 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300571 }
572 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200573 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300574 if (rc < 0)
575 goto error;
576 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200577 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300578 if (rc < 0)
579 goto error;
580 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200581 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300582 if (rc < 0)
583 goto error;
584 c->layer[i].interleaving = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200585 }
586
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200587 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300588 if (rc < 0)
589 return rc;
590 if ((rc & 0x60) == 0x20) {
591 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200592 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300593 if (!c->isdbt_sb_segment_count)
594 c->isdbt_sb_segment_count = 1;
595 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200596
597 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200598 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300599 if (rc < 0)
600 return rc;
601 if ((rc & 0x60) == 0x20) {
602 switch (rc & 0x0c >> 2) {
603 case 0:
604 c->transmission_mode = TRANSMISSION_MODE_2K;
605 break;
606 case 1:
607 c->transmission_mode = TRANSMISSION_MODE_4K;
608 break;
609 case 2:
610 c->transmission_mode = TRANSMISSION_MODE_8K;
611 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200612 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300613 }
614 if (!(rc & 0x10)) {
615 switch (rc & 0x3) {
616 case 0:
617 c->guard_interval = GUARD_INTERVAL_1_4;
618 break;
619 case 1:
620 c->guard_interval = GUARD_INTERVAL_1_8;
621 break;
622 case 2:
623 c->guard_interval = GUARD_INTERVAL_1_16;
624 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200625 }
626 }
627
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300628error:
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200629 if (fe->ops.i2c_gate_ctrl)
630 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300631
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300632 return rc;
633
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300634}
635
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200636static int mb86a20s_read_status_gate(struct dvb_frontend *fe,
637 fe_status_t *status)
638{
639 int ret;
640
641 dprintk("\n");
642 *status = 0;
643
644 if (fe->ops.i2c_gate_ctrl)
645 fe->ops.i2c_gate_ctrl(fe, 0);
646
647 ret = mb86a20s_read_status(fe, status);
648
649 if (fe->ops.i2c_gate_ctrl)
650 fe->ops.i2c_gate_ctrl(fe, 1);
651
652 return ret;
653}
654
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300655static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300656 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300657 unsigned int mode_flags,
658 unsigned int *delay,
659 fe_status_t *status)
660{
661 int rc = 0;
662
663 dprintk("\n");
664
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300665 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300666 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300667
668 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200669 mb86a20s_read_status_gate(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300670
671 return rc;
672}
673
674static void mb86a20s_release(struct dvb_frontend *fe)
675{
676 struct mb86a20s_state *state = fe->demodulator_priv;
677
678 dprintk("\n");
679
680 kfree(state);
681}
682
683static struct dvb_frontend_ops mb86a20s_ops;
684
685struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
686 struct i2c_adapter *i2c)
687{
688 u8 rev;
689
690 /* allocate memory for the internal state */
691 struct mb86a20s_state *state =
692 kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
693
694 dprintk("\n");
695 if (state == NULL) {
696 rc("Unable to kzalloc\n");
697 goto error;
698 }
699
700 /* setup the state */
701 state->config = config;
702 state->i2c = i2c;
703
704 /* create dvb_frontend */
705 memcpy(&state->frontend.ops, &mb86a20s_ops,
706 sizeof(struct dvb_frontend_ops));
707 state->frontend.demodulator_priv = state;
708
709 /* Check if it is a mb86a20s frontend */
710 rev = mb86a20s_readreg(state, 0);
711
712 if (rev == 0x13) {
713 printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
714 } else {
715 printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
716 rev);
717 goto error;
718 }
719
720 return &state->frontend;
721
722error:
723 kfree(state);
724 return NULL;
725}
726EXPORT_SYMBOL(mb86a20s_attach);
727
728static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300729 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300730 /* Use dib8000 values per default */
731 .info = {
732 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300733 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
734 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
735 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
736 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
737 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
738 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
739 /* Actually, those values depend on the used tuner */
740 .frequency_min = 45000000,
741 .frequency_max = 864000000,
742 .frequency_stepsize = 62500,
743 },
744
745 .release = mb86a20s_release,
746
747 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300748 .set_frontend = mb86a20s_set_frontend,
749 .get_frontend = mb86a20s_get_frontend,
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200750 .read_status = mb86a20s_read_status_gate,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300751 .read_signal_strength = mb86a20s_read_signal_strength,
752 .tune = mb86a20s_tune,
753};
754
755MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
756MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
757MODULE_LICENSE("GPL");