blob: c52ae2ea0bf9e6ae3ebd89a1aec67006e1dc91eb [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;
30
31 struct dvb_frontend frontend;
Mauro Carvalho Chehabc736a5f2011-01-14 11:10:05 -030032
33 bool need_init;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030034};
35
36struct regdata {
37 u8 reg;
38 u8 data;
39};
40
41/*
42 * Initialization sequence: Use whatevere default values that PV SBTVD
43 * does on its initialisation, obtained via USB snoop
44 */
45static struct regdata mb86a20s_init[] = {
46 { 0x70, 0x0f },
47 { 0x70, 0xff },
48 { 0x08, 0x01 },
49 { 0x09, 0x3e },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020050 { 0x50, 0xd1 }, { 0x51, 0x22 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030051 { 0x39, 0x01 },
52 { 0x71, 0x00 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020053 { 0x28, 0x2a }, { 0x29, 0x00 }, { 0x2a, 0xff }, { 0x2b, 0x80 },
54 { 0x28, 0x20 }, { 0x29, 0x33 }, { 0x2a, 0xdf }, { 0x2b, 0xa9 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020055 { 0x28, 0x22 }, { 0x29, 0x00 }, { 0x2a, 0x1f }, { 0x2b, 0xf0 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030056 { 0x3b, 0x21 },
57 { 0x3c, 0x3a },
58 { 0x01, 0x0d },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020059 { 0x04, 0x08 }, { 0x05, 0x05 },
60 { 0x04, 0x0e }, { 0x05, 0x00 },
61 { 0x04, 0x0f }, { 0x05, 0x14 },
62 { 0x04, 0x0b }, { 0x05, 0x8c },
63 { 0x04, 0x00 }, { 0x05, 0x00 },
64 { 0x04, 0x01 }, { 0x05, 0x07 },
65 { 0x04, 0x02 }, { 0x05, 0x0f },
66 { 0x04, 0x03 }, { 0x05, 0xa0 },
67 { 0x04, 0x09 }, { 0x05, 0x00 },
68 { 0x04, 0x0a }, { 0x05, 0xff },
69 { 0x04, 0x27 }, { 0x05, 0x64 },
70 { 0x04, 0x28 }, { 0x05, 0x00 },
71 { 0x04, 0x1e }, { 0x05, 0xff },
72 { 0x04, 0x29 }, { 0x05, 0x0a },
73 { 0x04, 0x32 }, { 0x05, 0x0a },
74 { 0x04, 0x14 }, { 0x05, 0x02 },
75 { 0x04, 0x04 }, { 0x05, 0x00 },
76 { 0x04, 0x05 }, { 0x05, 0x22 },
77 { 0x04, 0x06 }, { 0x05, 0x0e },
78 { 0x04, 0x07 }, { 0x05, 0xd8 },
79 { 0x04, 0x12 }, { 0x05, 0x00 },
80 { 0x04, 0x13 }, { 0x05, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020081 { 0x04, 0x15 }, { 0x05, 0x4e },
82 { 0x04, 0x16 }, { 0x05, 0x20 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030083 { 0x52, 0x01 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020084 { 0x50, 0xa7 }, { 0x51, 0xff },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020085 { 0x50, 0xa8 }, { 0x51, 0xff },
86 { 0x50, 0xa9 }, { 0x51, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020087 { 0x50, 0xaa }, { 0x51, 0xff },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020088 { 0x50, 0xab }, { 0x51, 0xff },
89 { 0x50, 0xac }, { 0x51, 0xff },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -020090 { 0x50, 0xad }, { 0x51, 0xff },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020091 { 0x50, 0xae }, { 0x51, 0xff },
92 { 0x50, 0xaf }, { 0x51, 0xff },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -030093 { 0x5e, 0x07 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -020094 { 0x50, 0xdc }, { 0x51, 0x01 },
95 { 0x50, 0xdd }, { 0x51, 0xf4 },
96 { 0x50, 0xde }, { 0x51, 0x01 },
97 { 0x50, 0xdf }, { 0x51, 0xf4 },
98 { 0x50, 0xe0 }, { 0x51, 0x01 },
99 { 0x50, 0xe1 }, { 0x51, 0xf4 },
100 { 0x50, 0xb0 }, { 0x51, 0x07 },
101 { 0x50, 0xb2 }, { 0x51, 0xff },
102 { 0x50, 0xb3 }, { 0x51, 0xff },
103 { 0x50, 0xb4 }, { 0x51, 0xff },
104 { 0x50, 0xb5 }, { 0x51, 0xff },
105 { 0x50, 0xb6 }, { 0x51, 0xff },
106 { 0x50, 0xb7 }, { 0x51, 0xff },
107 { 0x50, 0x50 }, { 0x51, 0x02 },
108 { 0x50, 0x51 }, { 0x51, 0x04 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300109 { 0x45, 0x04 },
110 { 0x48, 0x04 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200111 { 0x50, 0xd5 }, { 0x51, 0x01 }, /* Serial */
112 { 0x50, 0xd6 }, { 0x51, 0x1f },
113 { 0x50, 0xd2 }, { 0x51, 0x03 },
114 { 0x50, 0xd7 }, { 0x51, 0x3f },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200115 { 0x28, 0x74 }, { 0x29, 0x00 }, { 0x28, 0x74 }, { 0x29, 0x40 },
116 { 0x28, 0x46 }, { 0x29, 0x2c }, { 0x28, 0x46 }, { 0x29, 0x0c },
Mauro Carvalho Chehabce77d122013-01-14 14:12:10 -0300117
118 { 0x04, 0x40 }, { 0x05, 0x00 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200119 { 0x28, 0x00 }, { 0x29, 0x10 },
120 { 0x28, 0x05 }, { 0x29, 0x02 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300121 { 0x1c, 0x01 },
Mauro Carvalho Chehaba7025ed2012-01-11 10:56:30 -0200122 { 0x28, 0x06 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x03 },
123 { 0x28, 0x07 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0d },
124 { 0x28, 0x08 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x02 },
125 { 0x28, 0x09 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x01 },
126 { 0x28, 0x0a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x21 },
127 { 0x28, 0x0b }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x29 },
128 { 0x28, 0x0c }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x16 },
129 { 0x28, 0x0d }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x31 },
130 { 0x28, 0x0e }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0e },
131 { 0x28, 0x0f }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x4e },
132 { 0x28, 0x10 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x46 },
133 { 0x28, 0x11 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x0f },
134 { 0x28, 0x12 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x56 },
135 { 0x28, 0x13 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x35 },
136 { 0x28, 0x14 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbe },
137 { 0x28, 0x15 }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0x84 },
138 { 0x28, 0x16 }, { 0x29, 0x00 }, { 0x2a, 0x03 }, { 0x2b, 0xee },
139 { 0x28, 0x17 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x98 },
140 { 0x28, 0x18 }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x9f },
141 { 0x28, 0x19 }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0xb2 },
142 { 0x28, 0x1a }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0xc2 },
143 { 0x28, 0x1b }, { 0x29, 0x00 }, { 0x2a, 0x07 }, { 0x2b, 0x4a },
144 { 0x28, 0x1c }, { 0x29, 0x00 }, { 0x2a, 0x01 }, { 0x2b, 0xbc },
145 { 0x28, 0x1d }, { 0x29, 0x00 }, { 0x2a, 0x04 }, { 0x2b, 0xba },
146 { 0x28, 0x1e }, { 0x29, 0x00 }, { 0x2a, 0x06 }, { 0x2b, 0x14 },
147 { 0x50, 0x1e }, { 0x51, 0x5d },
148 { 0x50, 0x22 }, { 0x51, 0x00 },
149 { 0x50, 0x23 }, { 0x51, 0xc8 },
150 { 0x50, 0x24 }, { 0x51, 0x00 },
151 { 0x50, 0x25 }, { 0x51, 0xf0 },
152 { 0x50, 0x26 }, { 0x51, 0x00 },
153 { 0x50, 0x27 }, { 0x51, 0xc3 },
154 { 0x50, 0x39 }, { 0x51, 0x02 },
Mauro Carvalho Chehabebe96742012-01-11 11:00:28 -0200155 { 0x28, 0x6a }, { 0x29, 0x00 }, { 0x2a, 0x00 }, { 0x2b, 0x00 },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300156 { 0xd0, 0x00 },
157};
158
159static struct regdata mb86a20s_reset_reception[] = {
160 { 0x70, 0xf0 },
161 { 0x70, 0xff },
162 { 0x08, 0x01 },
163 { 0x08, 0x00 },
164};
165
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200166/*
167 * I2C read/write functions and macros
168 */
169
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300170static int mb86a20s_i2c_writereg(struct mb86a20s_state *state,
171 u8 i2c_addr, int reg, int data)
172{
173 u8 buf[] = { reg, data };
174 struct i2c_msg msg = {
175 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
176 };
177 int rc;
178
179 rc = i2c_transfer(state->i2c, &msg, 1);
180 if (rc != 1) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200181 dev_err(&state->i2c->dev,
182 "%s: writereg error (rc == %i, reg == 0x%02x, data == 0x%02x)\n",
183 __func__, rc, reg, data);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300184 return rc;
185 }
186
187 return 0;
188}
189
190static int mb86a20s_i2c_writeregdata(struct mb86a20s_state *state,
191 u8 i2c_addr, struct regdata *rd, int size)
192{
193 int i, rc;
194
195 for (i = 0; i < size; i++) {
196 rc = mb86a20s_i2c_writereg(state, i2c_addr, rd[i].reg,
197 rd[i].data);
198 if (rc < 0)
199 return rc;
200 }
201 return 0;
202}
203
204static int mb86a20s_i2c_readreg(struct mb86a20s_state *state,
205 u8 i2c_addr, u8 reg)
206{
207 u8 val;
208 int rc;
209 struct i2c_msg msg[] = {
210 { .addr = i2c_addr, .flags = 0, .buf = &reg, .len = 1 },
211 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &val, .len = 1 }
212 };
213
214 rc = i2c_transfer(state->i2c, msg, 2);
215
216 if (rc != 2) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200217 dev_err(&state->i2c->dev, "%s: reg=0x%x (error=%d)\n",
218 __func__, reg, rc);
219 return (rc < 0) ? rc : -EIO;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300220 }
221
222 return val;
223}
224
225#define mb86a20s_readreg(state, reg) \
226 mb86a20s_i2c_readreg(state, state->config->demod_address, reg)
227#define mb86a20s_writereg(state, reg, val) \
228 mb86a20s_i2c_writereg(state, state->config->demod_address, reg, val)
229#define mb86a20s_writeregdata(state, regdata) \
230 mb86a20s_i2c_writeregdata(state, state->config->demod_address, \
231 regdata, ARRAY_SIZE(regdata))
232
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200233static int mb86a20s_read_status(struct dvb_frontend *fe, fe_status_t *status)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300234{
235 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200236 int val;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300237
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200238 *status = 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300239
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200240 val = mb86a20s_readreg(state, 0x0a) & 0xf;
241 if (val < 0)
242 return val;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300243
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200244 if (val >= 2)
245 *status |= FE_HAS_SIGNAL;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300246
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200247 if (val >= 4)
248 *status |= FE_HAS_CARRIER;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300249
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200250 if (val >= 5)
251 *status |= FE_HAS_VITERBI;
Mauro Carvalho Chehab7572f9c2010-10-03 16:48:49 -0300252
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200253 if (val >= 7)
254 *status |= FE_HAS_SYNC;
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300255
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200256 if (val >= 8) /* Maybe 9? */
257 *status |= FE_HAS_LOCK;
258
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200259 dev_dbg(&state->i2c->dev, "%s: Status = 0x%02x (state = %d)\n",
260 __func__, *status, val);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200261
262 return 0;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300263}
264
265static int mb86a20s_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
266{
267 struct mb86a20s_state *state = fe->demodulator_priv;
268 unsigned rf_max, rf_min, rf;
269 u8 val;
270
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300271 if (fe->ops.i2c_gate_ctrl)
272 fe->ops.i2c_gate_ctrl(fe, 0);
273
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300274 /* Does a binary search to get RF strength */
275 rf_max = 0xfff;
276 rf_min = 0;
277 do {
278 rf = (rf_max + rf_min) / 2;
279 mb86a20s_writereg(state, 0x04, 0x1f);
280 mb86a20s_writereg(state, 0x05, rf >> 8);
281 mb86a20s_writereg(state, 0x04, 0x20);
282 mb86a20s_writereg(state, 0x04, rf);
283
284 val = mb86a20s_readreg(state, 0x02);
285 if (val & 0x08)
286 rf_min = (rf_max + rf_min) / 2;
287 else
288 rf_max = (rf_max + rf_min) / 2;
289 if (rf_max - rf_min < 4) {
290 *strength = (((rf_max + rf_min) / 2) * 65535) / 4095;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200291 dev_dbg(&state->i2c->dev,
292 "%s: signal strength = %d (%d < RF=%d < %d)\n",
293 __func__, rf, rf_min, rf >> 4, rf_max);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300294 break;
295 }
296 } while (1);
297
Mauro Carvalho Chehab68541cd2010-10-03 05:27:59 -0300298 if (fe->ops.i2c_gate_ctrl)
299 fe->ops.i2c_gate_ctrl(fe, 1);
300
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300301 return 0;
302}
303
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200304static int mb86a20s_get_modulation(struct mb86a20s_state *state,
305 unsigned layer)
306{
307 int rc;
308 static unsigned char reg[] = {
309 [0] = 0x86, /* Layer A */
310 [1] = 0x8a, /* Layer B */
311 [2] = 0x8e, /* Layer C */
312 };
313
Dan Carpenter82033bc2012-01-13 02:28:34 -0300314 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200315 return -EINVAL;
316 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
317 if (rc < 0)
318 return rc;
319 rc = mb86a20s_readreg(state, 0x6e);
320 if (rc < 0)
321 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300322 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200323 case 0:
324 return DQPSK;
325 case 1:
326 return QPSK;
327 case 2:
328 return QAM_16;
329 case 3:
330 return QAM_64;
331 default:
332 return QAM_AUTO;
333 }
334}
335
336static int mb86a20s_get_fec(struct mb86a20s_state *state,
337 unsigned layer)
338{
339 int rc;
340
341 static unsigned char reg[] = {
342 [0] = 0x87, /* Layer A */
343 [1] = 0x8b, /* Layer B */
344 [2] = 0x8f, /* Layer C */
345 };
346
Dan Carpenter82033bc2012-01-13 02:28:34 -0300347 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200348 return -EINVAL;
349 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
350 if (rc < 0)
351 return rc;
352 rc = mb86a20s_readreg(state, 0x6e);
353 if (rc < 0)
354 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300355 switch ((rc >> 4) & 0x07) {
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200356 case 0:
357 return FEC_1_2;
358 case 1:
359 return FEC_2_3;
360 case 2:
361 return FEC_3_4;
362 case 3:
363 return FEC_5_6;
364 case 4:
365 return FEC_7_8;
366 default:
367 return FEC_AUTO;
368 }
369}
370
371static int mb86a20s_get_interleaving(struct mb86a20s_state *state,
372 unsigned layer)
373{
374 int rc;
375
376 static unsigned char reg[] = {
377 [0] = 0x88, /* Layer A */
378 [1] = 0x8c, /* Layer B */
379 [2] = 0x90, /* Layer C */
380 };
381
Dan Carpenter82033bc2012-01-13 02:28:34 -0300382 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200383 return -EINVAL;
384 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
385 if (rc < 0)
386 return rc;
387 rc = mb86a20s_readreg(state, 0x6e);
388 if (rc < 0)
389 return rc;
Mauro Carvalho Chehab04585922013-01-14 12:31:13 -0300390
391 switch ((rc >> 4) & 0x07) {
392 case 1:
393 return GUARD_INTERVAL_1_4;
394 case 2:
395 return GUARD_INTERVAL_1_8;
396 case 3:
397 return GUARD_INTERVAL_1_16;
398 case 4:
399 return GUARD_INTERVAL_1_32;
400
401 default:
402 case 0:
403 return GUARD_INTERVAL_AUTO;
404 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200405}
406
407static int mb86a20s_get_segment_count(struct mb86a20s_state *state,
408 unsigned layer)
409{
410 int rc, count;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200411 static unsigned char reg[] = {
412 [0] = 0x89, /* Layer A */
413 [1] = 0x8d, /* Layer B */
414 [2] = 0x91, /* Layer C */
415 };
416
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200417 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
418
Dan Carpenter82033bc2012-01-13 02:28:34 -0300419 if (layer >= ARRAY_SIZE(reg))
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200420 return -EINVAL;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200421
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200422 rc = mb86a20s_writereg(state, 0x6d, reg[layer]);
423 if (rc < 0)
424 return rc;
425 rc = mb86a20s_readreg(state, 0x6e);
426 if (rc < 0)
427 return rc;
428 count = (rc >> 4) & 0x0f;
429
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200430 dev_dbg(&state->i2c->dev, "%s: segments: %d.\n", __func__, count);
431
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200432 return count;
433}
434
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300435static void mb86a20s_reset_frontend_cache(struct dvb_frontend *fe)
436{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200437 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300438 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
439
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200440 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
441
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300442 /* Fixed parameters */
443 c->delivery_system = SYS_ISDBT;
444 c->bandwidth_hz = 6000000;
445
446 /* Initialize values that will be later autodetected */
447 c->isdbt_layer_enabled = 0;
448 c->transmission_mode = TRANSMISSION_MODE_AUTO;
449 c->guard_interval = GUARD_INTERVAL_AUTO;
450 c->isdbt_sb_mode = 0;
451 c->isdbt_sb_segment_count = 0;
452}
453
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300454static int mb86a20s_get_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300455{
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200456 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300457 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200458 int i, rc;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300459
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200460 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
461
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300462 /* Reset frontend cache to default values */
463 mb86a20s_reset_frontend_cache(fe);
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200464
465 if (fe->ops.i2c_gate_ctrl)
466 fe->ops.i2c_gate_ctrl(fe, 0);
467
468 /* Check for partial reception */
469 rc = mb86a20s_writereg(state, 0x6d, 0x85);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300470 if (rc < 0)
471 return rc;
472 rc = mb86a20s_readreg(state, 0x6e);
473 if (rc < 0)
474 return rc;
475 c->isdbt_partial_reception = (rc & 0x10) ? 1 : 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200476
477 /* Get per-layer data */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300478
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200479 for (i = 0; i < 3; i++) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200480 dev_dbg(&state->i2c->dev, "%s: getting data for layer %c.\n",
481 __func__, 'A' + i);
482
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200483 rc = mb86a20s_get_segment_count(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300484 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200485 goto noperlayer_error;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300486 if (rc >= 0 && rc < 14)
487 c->layer[i].segment_count = rc;
488 else {
489 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200490 continue;
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300491 }
492 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200493 rc = mb86a20s_get_modulation(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300494 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200495 goto noperlayer_error;
496 dev_dbg(&state->i2c->dev, "%s: modulation %d.\n",
497 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300498 c->layer[i].modulation = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200499 rc = mb86a20s_get_fec(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300500 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200501 goto noperlayer_error;
502 dev_dbg(&state->i2c->dev, "%s: FEC %d.\n",
503 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300504 c->layer[i].fec = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200505 rc = mb86a20s_get_interleaving(state, i);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300506 if (rc < 0)
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200507 goto noperlayer_error;
508 dev_dbg(&state->i2c->dev, "%s: interleaving %d.\n",
509 __func__, rc);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300510 c->layer[i].interleaving = rc;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200511 }
512
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200513 rc = mb86a20s_writereg(state, 0x6d, 0x84);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300514 if (rc < 0)
515 return rc;
516 if ((rc & 0x60) == 0x20) {
517 c->isdbt_sb_mode = 1;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200518 /* At least, one segment should exist */
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300519 if (!c->isdbt_sb_segment_count)
520 c->isdbt_sb_segment_count = 1;
521 }
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200522
523 /* Get transmission mode and guard interval */
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200524 rc = mb86a20s_readreg(state, 0x07);
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300525 if (rc < 0)
526 return rc;
527 if ((rc & 0x60) == 0x20) {
528 switch (rc & 0x0c >> 2) {
529 case 0:
530 c->transmission_mode = TRANSMISSION_MODE_2K;
531 break;
532 case 1:
533 c->transmission_mode = TRANSMISSION_MODE_4K;
534 break;
535 case 2:
536 c->transmission_mode = TRANSMISSION_MODE_8K;
537 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200538 }
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300539 }
540 if (!(rc & 0x10)) {
541 switch (rc & 0x3) {
542 case 0:
543 c->guard_interval = GUARD_INTERVAL_1_4;
544 break;
545 case 1:
546 c->guard_interval = GUARD_INTERVAL_1_8;
547 break;
548 case 2:
549 c->guard_interval = GUARD_INTERVAL_1_16;
550 break;
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200551 }
552 }
553
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200554noperlayer_error:
Mauro Carvalho Chehab959a1192012-01-09 18:16:36 -0200555 if (fe->ops.i2c_gate_ctrl)
556 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300557
Mauro Carvalho Chehaba77cfca2013-01-14 09:26:09 -0300558 return rc;
559
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300560}
561
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200562static int mb86a20s_initfe(struct dvb_frontend *fe)
563{
564 struct mb86a20s_state *state = fe->demodulator_priv;
565 int rc;
566 u8 regD5 = 1;
567
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200568 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200569
570 if (fe->ops.i2c_gate_ctrl)
571 fe->ops.i2c_gate_ctrl(fe, 0);
572
573 /* Initialize the frontend */
574 rc = mb86a20s_writeregdata(state, mb86a20s_init);
575 if (rc < 0)
576 goto err;
577
578 if (!state->config->is_serial) {
579 regD5 &= ~1;
580
581 rc = mb86a20s_writereg(state, 0x50, 0xd5);
582 if (rc < 0)
583 goto err;
584 rc = mb86a20s_writereg(state, 0x51, regD5);
585 if (rc < 0)
586 goto err;
587 }
588
589err:
590 if (fe->ops.i2c_gate_ctrl)
591 fe->ops.i2c_gate_ctrl(fe, 1);
592
593 if (rc < 0) {
594 state->need_init = true;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200595 dev_info(&state->i2c->dev,
596 "mb86a20s: Init failed. Will try again later\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200597 } else {
598 state->need_init = false;
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200599 dev_dbg(&state->i2c->dev, "Initialization succeeded.\n");
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200600 }
601 return rc;
602}
603
604static int mb86a20s_set_frontend(struct dvb_frontend *fe)
605{
606 struct mb86a20s_state *state = fe->demodulator_priv;
607 int rc;
608#if 0
609 /*
610 * FIXME: Properly implement the set frontend properties
611 */
612 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
613#endif
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200614 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200615
616 /*
617 * Gate should already be opened, but it doesn't hurt to
618 * double-check
619 */
620 if (fe->ops.i2c_gate_ctrl)
621 fe->ops.i2c_gate_ctrl(fe, 1);
Mauro Carvalho Chehabdd4493e2013-01-22 08:53:11 -0200622 fe->ops.tuner_ops.set_params(fe);
623
624 /*
625 * Make it more reliable: if, for some reason, the initial
626 * device initialization doesn't happen, initialize it when
627 * a SBTVD parameters are adjusted.
628 *
629 * Unfortunately, due to a hard to track bug at tda829x/tda18271,
630 * the agc callback logic is not called during DVB attach time,
631 * causing mb86a20s to not be initialized with Kworld SBTVD.
632 * So, this hack is needed, in order to make Kworld SBTVD to work.
633 */
634 if (state->need_init)
635 mb86a20s_initfe(fe);
636
637 if (fe->ops.i2c_gate_ctrl)
638 fe->ops.i2c_gate_ctrl(fe, 0);
639 rc = mb86a20s_writeregdata(state, mb86a20s_reset_reception);
640 if (fe->ops.i2c_gate_ctrl)
641 fe->ops.i2c_gate_ctrl(fe, 1);
642
643 return rc;
644}
645
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200646static int mb86a20s_read_status_gate(struct dvb_frontend *fe,
647 fe_status_t *status)
648{
649 int ret;
650
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200651 *status = 0;
652
653 if (fe->ops.i2c_gate_ctrl)
654 fe->ops.i2c_gate_ctrl(fe, 0);
655
656 ret = mb86a20s_read_status(fe, status);
657
658 if (fe->ops.i2c_gate_ctrl)
659 fe->ops.i2c_gate_ctrl(fe, 1);
660
661 return ret;
662}
663
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300664static int mb86a20s_tune(struct dvb_frontend *fe,
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300665 bool re_tune,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300666 unsigned int mode_flags,
667 unsigned int *delay,
668 fe_status_t *status)
669{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200670 struct mb86a20s_state *state = fe->demodulator_priv;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300671 int rc = 0;
672
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200673 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300674
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300675 if (re_tune)
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300676 rc = mb86a20s_set_frontend(fe);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300677
678 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200679 mb86a20s_read_status_gate(fe, status);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300680
681 return rc;
682}
683
684static void mb86a20s_release(struct dvb_frontend *fe)
685{
686 struct mb86a20s_state *state = fe->demodulator_priv;
687
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200688 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300689
690 kfree(state);
691}
692
693static struct dvb_frontend_ops mb86a20s_ops;
694
695struct dvb_frontend *mb86a20s_attach(const struct mb86a20s_config *config,
696 struct i2c_adapter *i2c)
697{
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200698 struct mb86a20s_state *state;
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300699 u8 rev;
700
701 /* allocate memory for the internal state */
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200702 state = kzalloc(sizeof(struct mb86a20s_state), GFP_KERNEL);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300703
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200704 dev_dbg(&state->i2c->dev, "%s called.\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300705 if (state == NULL) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200706 dev_err(&state->i2c->dev,
707 "%s: unable to allocate memory for state\n", __func__);
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300708 goto error;
709 }
710
711 /* setup the state */
712 state->config = config;
713 state->i2c = i2c;
714
715 /* create dvb_frontend */
716 memcpy(&state->frontend.ops, &mb86a20s_ops,
717 sizeof(struct dvb_frontend_ops));
718 state->frontend.demodulator_priv = state;
719
720 /* Check if it is a mb86a20s frontend */
721 rev = mb86a20s_readreg(state, 0);
722
723 if (rev == 0x13) {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200724 dev_info(&state->i2c->dev,
725 "Detected a Fujitsu mb86a20s frontend\n");
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300726 } else {
Mauro Carvalho Chehabf66d81b2013-01-22 09:13:08 -0200727 dev_dbg(&state->i2c->dev,
728 "Frontend revision %d is unknown - aborting.\n",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300729 rev);
730 goto error;
731 }
732
733 return &state->frontend;
734
735error:
736 kfree(state);
737 return NULL;
738}
739EXPORT_SYMBOL(mb86a20s_attach);
740
741static struct dvb_frontend_ops mb86a20s_ops = {
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300742 .delsys = { SYS_ISDBT },
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300743 /* Use dib8000 values per default */
744 .info = {
745 .name = "Fujitsu mb86A20s",
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300746 .caps = FE_CAN_INVERSION_AUTO | FE_CAN_RECOVER |
747 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
748 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
749 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
750 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_QAM_AUTO |
751 FE_CAN_GUARD_INTERVAL_AUTO | FE_CAN_HIERARCHY_AUTO,
752 /* Actually, those values depend on the used tuner */
753 .frequency_min = 45000000,
754 .frequency_max = 864000000,
755 .frequency_stepsize = 62500,
756 },
757
758 .release = mb86a20s_release,
759
760 .init = mb86a20s_initfe,
Mauro Carvalho Chehab2d76e22b2011-12-26 12:11:51 -0300761 .set_frontend = mb86a20s_set_frontend,
762 .get_frontend = mb86a20s_get_frontend,
Mauro Carvalho Chehabd36e4182013-01-22 08:49:39 -0200763 .read_status = mb86a20s_read_status_gate,
Mauro Carvalho Chehabb9ede792010-09-27 20:52:43 -0300764 .read_signal_strength = mb86a20s_read_signal_strength,
765 .tune = mb86a20s_tune,
766};
767
768MODULE_DESCRIPTION("DVB Frontend module for Fujitsu mb86A20s hardware");
769MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
770MODULE_LICENSE("GPL");