blob: 8f4fff1f0fa5372e391a7471fc5a695e2448612b [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;
323 u8 val;
324
325 dprintk("\n");
326 *status = 0;
327
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300328 if (fe->ops.i2c_gate_ctrl)
329 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300330 val = mb86a20s_readreg(state, 0x0a) & 0xf;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300331 if (fe->ops.i2c_gate_ctrl)
332 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300333
334 if (val >= 2)
335 *status |= FE_HAS_SIGNAL;
336
337 if (val >= 4)
338 *status |= FE_HAS_CARRIER;
339
340 if (val >= 5)
341 *status |= FE_HAS_VITERBI;
342
343 if (val >= 7)
344 *status |= FE_HAS_SYNC;
345
346 if (val >= 8) /* Maybe 9? */
347 *status |= FE_HAS_LOCK;
348
349 dprintk("val = %d, status = 0x%02x\n", val, *status);
350
351 return 0;
352}
353
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300354static int mb86a20s_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300355{
356 struct mb86a20s_state *state = fe->demodulator_priv;
357 int rc;
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300358#if 0
359 /*
360 * FIXME: Properly implement the set frontend properties
361 */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300362 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300363#endif
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300364
365 dprintk("\n");
366
Mauro Carvalho Chehabfd537442013-01-14 10:16:07 -0300367 /*
368 * Gate should already be opened, but it doesn't hurt to
369 * double-check
370 */
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300371 if (fe->ops.i2c_gate_ctrl)
372 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300373 dprintk("Calling tuner set parameters\n");
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300374 fe->ops.tuner_ops.set_params(fe);
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300375
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -0300376 /*
377 * Make it more reliable: if, for some reason, the initial
378 * device initialization doesn't happen, initialize it when
379 * a SBTVD parameters are adjusted.
380 *
381 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
382 * the agc callback logic is not called during DVB attach time,
383 * causing mb86a20s to not be initialized with Kworld SBTVD.
384 * So, this hack is needed, in order to make Kworld SBTVD to work.
385 */
386 if (state->need_init)
387 mb86a20s_initfe(fe);
388
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300389 if (fe->ops.i2c_gate_ctrl)
390 fe->ops.i2c_gate_ctrl(fe, 0);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300391 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300392 if (fe->ops.i2c_gate_ctrl)
393 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300394
395 return rc;
396}
397
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200398static int mb86a20s_get_modulation(struct mb86a20s_state *state,
399 unsigned layer)
400{
401 int rc;
402 static unsigned char reg[] = {
403 [0] = 0x86, /* Layer A */
404 [1] = 0x8a, /* Layer B */
405 [2] = 0x8e, /* Layer C */
406 };
407
Dan Carpenter82033bc2012-01-13 02:28:34 -0300408 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200409 return -EINVAL;
410 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
411 if (rc < 0)
412 return rc;
413 rc = mb86a20s_readreg(state, 0x6e);
414 if (rc < 0)
415 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300416 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200417 case 0:
418 return DQPSK;
419 case 1:
420 return QPSK;
421 case 2:
422 return QAM_16;
423 case 3:
424 return QAM_64;
425 default:
426 return QAM_AUTO;
427 }
428}
429
430static int mb86a20s_get_fec(struct mb86a20s_state *state,
431 unsigned layer)
432{
433 int rc;
434
435 static unsigned char reg[] = {
436 [0] = 0x87, /* Layer A */
437 [1] = 0x8b, /* Layer B */
438 [2] = 0x8f, /* Layer C */
439 };
440
Dan Carpenter82033bc2012-01-13 02:28:34 -0300441 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200442 return -EINVAL;
443 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
444 if (rc < 0)
445 return rc;
446 rc = mb86a20s_readreg(state, 0x6e);
447 if (rc < 0)
448 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300449 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200450 case 0:
451 return FEC_1_2;
452 case 1:
453 return FEC_2_3;
454 case 2:
455 return FEC_3_4;
456 case 3:
457 return FEC_5_6;
458 case 4:
459 return FEC_7_8;
460 default:
461 return FEC_AUTO;
462 }
463}
464
465static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
466 unsigned layer)
467{
468 int rc;
469
470 static unsigned char reg[] = {
471 [0] = 0x88, /* Layer A */
472 [1] = 0x8c, /* Layer B */
473 [2] = 0x90, /* Layer C */
474 };
475
Dan Carpenter82033bc2012-01-13 02:28:34 -0300476 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200477 return -EINVAL;
478 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
479 if (rc < 0)
480 return rc;
481 rc = mb86a20s_readreg(state, 0x6e);
482 if (rc < 0)
483 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300484
485 switch ((rc >> 4) & 0x07) {
486 case 1:
487 return GUARD_INTERVAL_1_4;
488 case 2:
489 return GUARD_INTERVAL_1_8;
490 case 3:
491 return GUARD_INTERVAL_1_16;
492 case 4:
493 return GUARD_INTERVAL_1_32;
494
495 default:
496 case 0:
497 return GUARD_INTERVAL_AUTO;
498 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200499}
500
501static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
502 unsigned layer)
503{
504 int rc, count;
505
506 static unsigned char reg[] = {
507 [0] = 0x89, /* Layer A */
508 [1] = 0x8d, /* Layer B */
509 [2] = 0x91, /* Layer C */
510 };
511
Dan Carpenter82033bc2012-01-13 02:28:34 -0300512 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200513 return -EINVAL;
514 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
515 if (rc < 0)
516 return rc;
517 rc = mb86a20s_readreg(state, 0x6e);
518 if (rc < 0)
519 return rc;
520 count = (rc >> 4) & 0x0f;
521
522 return count;
523}
524
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300525static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
526{
527 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
528
529 /* Fixed parameters */
530 c->delivery_system = SYS_ISDBT;
531 c->bandwidth_hz = 6000000;
532
533 /* Initialize values that will be later autodetected */
534 c->isdbt_layer_enabled = 0;
535 c->transmission_mode = TRANSMISSION_MODE_AUTO;
536 c->guard_interval = GUARD_INTERVAL_AUTO;
537 c->isdbt_sb_mode = 0;
538 c->isdbt_sb_segment_count = 0;
539}
540
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300541static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300542{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200543 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300544 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200545 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300546
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300547 /* Reset frontend cache to default values */
548 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200549
550 if (fe->ops.i2c_gate_ctrl)
551 fe->ops.i2c_gate_ctrl(fe, 0);
552
553 /* Check for partial reception */
554 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300555 if (rc < 0)
556 return rc;
557 rc = mb86a20s_readreg(state, 0x6e);
558 if (rc < 0)
559 return rc;
560 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200561
562 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300563
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200564 for (i = 0; i < 3; i++) {
565 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300566 if (rc < 0)
567 goto error;
568 if (rc >= 0 && rc < 14)
569 c->layer[i].segment_count = rc;
570 else {
571 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200572 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300573 }
574 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200575 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300576 if (rc < 0)
577 goto error;
578 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200579 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300580 if (rc < 0)
581 goto error;
582 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200583 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300584 if (rc < 0)
585 goto error;
586 c->layer[i].interleaving = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200587 }
588
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200589 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300590 if (rc < 0)
591 return rc;
592 if ((rc & 0x60) == 0x20) {
593 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200594 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300595 if (!c->isdbt_sb_segment_count)
596 c->isdbt_sb_segment_count = 1;
597 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200598
599 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200600 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300601 if (rc < 0)
602 return rc;
603 if ((rc & 0x60) == 0x20) {
604 switch (rc & 0x0c >> 2) {
605 case 0:
606 c->transmission_mode = TRANSMISSION_MODE_2K;
607 break;
608 case 1:
609 c->transmission_mode = TRANSMISSION_MODE_4K;
610 break;
611 case 2:
612 c->transmission_mode = TRANSMISSION_MODE_8K;
613 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200614 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300615 }
616 if (!(rc & 0x10)) {
617 switch (rc & 0x3) {
618 case 0:
619 c->guard_interval = GUARD_INTERVAL_1_4;
620 break;
621 case 1:
622 c->guard_interval = GUARD_INTERVAL_1_8;
623 break;
624 case 2:
625 c->guard_interval = GUARD_INTERVAL_1_16;
626 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200627 }
628 }
629
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300630error:
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200631 if (fe->ops.i2c_gate_ctrl)
632 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300633
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300634 return rc;
635
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300636}
637
638static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300639 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300640 unsigned int mode_flags,
641 unsigned int *delay,
642 fe_status_t *status)
643{
644 int rc = 0;
645
646 dprintk("\n");
647
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300648 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300649 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300650
651 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
652 mb86a20s_read_status(fe, status);
653
654 return rc;
655}
656
657static void mb86a20s_release(struct dvb_frontend *fe)
658{
659 struct mb86a20s_state *state = fe->demodulator_priv;
660
661 dprintk("\n");
662
663 kfree(state);
664}
665
666static struct dvb_frontend_ops mb86a20s_ops;
667
668struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
669 struct i2c_adapter *i2c)
670{
671 u8 rev;
672
673 /* allocate memory for the internal state */
674 struct mb86a20s_state *state =
675 kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
676
677 dprintk("\n");
678 if (state == NULL) {
679 rc("Unable to kzalloc\n");
680 goto error;
681 }
682
683 /* setup the state */
684 state->config = config;
685 state->i2c = i2c;
686
687 /* create dvb_frontend */
688 memcpy(&state->frontend.ops, &mb86a20s_ops,
689 sizeof(struct dvb_frontend_ops));
690 state->frontend.demodulator_priv = state;
691
692 /* Check if it is a mb86a20s frontend */
693 rev = mb86a20s_readreg(state, 0);
694
695 if (rev == 0x13) {
696 printk(KERN_INFO "Detected a Fujitsu mb86a20s frontend\n");
697 } else {
698 printk(KERN_ERR "Frontend revision %d is unknown - aborting.\n",
699 rev);
700 goto error;
701 }
702
703 return &state->frontend;
704
705error:
706 kfree(state);
707 return NULL;
708}
709EXPORT_SYMBOL(mb86a20s_attach);
710
711static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300712 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300713 /* Use dib8000 values per default */
714 .info = {
715 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300716 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
717 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
718 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
719 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
720 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
721 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
722 /* Actually, those values depend on the used tuner */
723 .frequency_min = 45000000,
724 .frequency_max = 864000000,
725 .frequency_stepsize = 62500,
726 },
727
728 .release = mb86a20s_release,
729
730 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300731 .set_frontend = mb86a20s_set_frontend,
732 .get_frontend = mb86a20s_get_frontend,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300733 .read_status = mb86a20s_read_status,
734 .read_signal_strength = mb86a20s_read_signal_strength,
735 .tune = mb86a20s_tune,
736};
737
738MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
739MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
740MODULE_LICENSE("GPL");