blob: b96f8e846fd31c4c5c20d252e0fe26a6a866e747 [file] [log] [blame]
Kirk Lapray04a45922005-11-08 21:35:46 -08001/*
2 * Support for NXT2002 and NXT2004 - VSB/QAM
3 *
Michael Krufky46365f32006-01-23 09:52:39 -02004 * Copyright (C) 2005 Kirk Lapray <kirk.lapray@gmail.com>
5 * Copyright (C) 2006 Michael Krufky <mkrufky@m1k.net>
Kirk Lapray04a45922005-11-08 21:35:46 -08006 * based on nxt2002 by Taylor Jacob <rtjacob@earthlink.net>
Michael Krufky46365f32006-01-23 09:52:39 -02007 * and nxt2004 by Jean-Francois Thibert <jeanfrancois@sagetv.com>
Kirk Lapray04a45922005-11-08 21:35:46 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23*/
24
25/*
26 * NOTES ABOUT THIS DRIVER
27 *
28 * This Linux driver supports:
29 * B2C2/BBTI Technisat Air2PC - ATSC (NXT2002)
30 * AverTVHD MCE A180 (NXT2004)
31 * ATI HDTV Wonder (NXT2004)
32 *
33 * This driver needs external firmware. Please use the command
34 * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2002" or
35 * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2004" to
36 * download/extract the appropriate firmware, and then copy it to
37 * /usr/lib/hotplug/firmware/ or /lib/firmware/
38 * (depending on configuration of firmware hotplug).
39 */
40#define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
41#define NXT2004_DEFAULT_FIRMWARE "dvb-fe-nxt2004.fw"
42#define CRC_CCIT_MASK 0x1021
43
44#include <linux/kernel.h>
45#include <linux/init.h>
46#include <linux/module.h>
47#include <linux/moduleparam.h>
Tim Schmielau18e55ee2005-12-01 00:51:51 -080048#include <linux/slab.h>
49#include <linux/string.h>
Kirk Lapray04a45922005-11-08 21:35:46 -080050
51#include "dvb_frontend.h"
52#include "dvb-pll.h"
53#include "nxt200x.h"
54
55struct nxt200x_state {
56
57 struct i2c_adapter* i2c;
Kirk Lapray04a45922005-11-08 21:35:46 -080058 const struct nxt200x_config* config;
59 struct dvb_frontend frontend;
60
61 /* demodulator private data */
62 nxt_chip_type demod_chip;
63 u8 initialised:1;
64};
65
66static int debug;
67#define dprintk(args...) \
68 do { \
69 if (debug) printk(KERN_DEBUG "nxt200x: " args); \
70 } while (0)
71
72static int i2c_writebytes (struct nxt200x_state* state, u8 addr, u8 *buf, u8 len)
73{
74 int err;
75 struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = buf, .len = len };
76
77 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
78 printk (KERN_WARNING "nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
79 __FUNCTION__, addr, err);
80 return -EREMOTEIO;
81 }
82 return 0;
83}
84
85static u8 i2c_readbytes (struct nxt200x_state* state, u8 addr, u8* buf, u8 len)
86{
87 int err;
88 struct i2c_msg msg = { .addr = addr, .flags = I2C_M_RD, .buf = buf, .len = len };
89
90 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
91 printk (KERN_WARNING "nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
92 __FUNCTION__, addr, err);
93 return -EREMOTEIO;
94 }
95 return 0;
96}
97
98static int nxt200x_writebytes (struct nxt200x_state* state, u8 reg, u8 *buf, u8 len)
99{
100 u8 buf2 [len+1];
101 int err;
102 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf2, .len = len + 1 };
103
104 buf2[0] = reg;
105 memcpy(&buf2[1], buf, len);
106
107 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
108 printk (KERN_WARNING "nxt200x: %s: i2c write error (addr 0x%02x, err == %i)\n",
109 __FUNCTION__, state->config->demod_address, err);
110 return -EREMOTEIO;
111 }
112 return 0;
113}
114
115static u8 nxt200x_readbytes (struct nxt200x_state* state, u8 reg, u8* buf, u8 len)
116{
117 u8 reg2 [] = { reg };
118
119 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = reg2, .len = 1 },
120 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = buf, .len = len } };
121
122 int err;
123
124 if ((err = i2c_transfer (state->i2c, msg, 2)) != 2) {
125 printk (KERN_WARNING "nxt200x: %s: i2c read error (addr 0x%02x, err == %i)\n",
126 __FUNCTION__, state->config->demod_address, err);
127 return -EREMOTEIO;
128 }
129 return 0;
130}
131
132static u16 nxt200x_crc(u16 crc, u8 c)
133{
134 u8 i;
135 u16 input = (u16) c & 0xFF;
136
137 input<<=8;
138 for(i=0; i<8; i++) {
139 if((crc^input) & 0x8000)
140 crc=(crc<<1)^CRC_CCIT_MASK;
141 else
142 crc<<=1;
143 input<<=1;
144 }
145 return crc;
146}
147
148static int nxt200x_writereg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
149{
150 u8 attr, len2, buf;
151 dprintk("%s\n", __FUNCTION__);
152
153 /* set mutli register register */
154 nxt200x_writebytes(state, 0x35, &reg, 1);
155
156 /* send the actual data */
157 nxt200x_writebytes(state, 0x36, data, len);
158
159 switch (state->demod_chip) {
160 case NXT2002:
161 len2 = len;
162 buf = 0x02;
163 break;
164 case NXT2004:
165 /* probably not right, but gives correct values */
166 attr = 0x02;
167 if (reg & 0x80) {
168 attr = attr << 1;
169 if (reg & 0x04)
170 attr = attr >> 1;
171 }
172 /* set write bit */
173 len2 = ((attr << 4) | 0x10) | len;
174 buf = 0x80;
175 break;
176 default:
177 return -EINVAL;
178 break;
179 }
180
181 /* set multi register length */
182 nxt200x_writebytes(state, 0x34, &len2, 1);
183
184 /* toggle the multireg write bit */
185 nxt200x_writebytes(state, 0x21, &buf, 1);
186
187 nxt200x_readbytes(state, 0x21, &buf, 1);
188
189 switch (state->demod_chip) {
190 case NXT2002:
191 if ((buf & 0x02) == 0)
192 return 0;
193 break;
194 case NXT2004:
195 if (buf == 0)
196 return 0;
197 break;
198 default:
199 return -EINVAL;
200 break;
201 }
202
203 printk(KERN_WARNING "nxt200x: Error writing multireg register 0x%02X\n",reg);
204
205 return 0;
206}
207
208static int nxt200x_readreg_multibyte (struct nxt200x_state* state, u8 reg, u8* data, u8 len)
209{
210 int i;
211 u8 buf, len2, attr;
212 dprintk("%s\n", __FUNCTION__);
213
214 /* set mutli register register */
215 nxt200x_writebytes(state, 0x35, &reg, 1);
216
217 switch (state->demod_chip) {
218 case NXT2002:
219 /* set multi register length */
220 len2 = len & 0x80;
221 nxt200x_writebytes(state, 0x34, &len2, 1);
222
223 /* read the actual data */
224 nxt200x_readbytes(state, reg, data, len);
225 return 0;
226 break;
227 case NXT2004:
228 /* probably not right, but gives correct values */
229 attr = 0x02;
230 if (reg & 0x80) {
231 attr = attr << 1;
232 if (reg & 0x04)
233 attr = attr >> 1;
234 }
235
236 /* set multi register length */
237 len2 = (attr << 4) | len;
238 nxt200x_writebytes(state, 0x34, &len2, 1);
239
240 /* toggle the multireg bit*/
241 buf = 0x80;
242 nxt200x_writebytes(state, 0x21, &buf, 1);
243
Kirk Laprayf93cf032005-11-08 21:35:51 -0800244 /* read the actual data */
245 for(i = 0; i < len; i++) {
246 nxt200x_readbytes(state, 0x36 + i, &data[i], 1);
Kirk Lapray04a45922005-11-08 21:35:46 -0800247 }
Kirk Laprayf93cf032005-11-08 21:35:51 -0800248 return 0;
Kirk Lapray04a45922005-11-08 21:35:46 -0800249 break;
250 default:
251 return -EINVAL;
252 break;
253 }
Kirk Lapray04a45922005-11-08 21:35:46 -0800254}
255
256static void nxt200x_microcontroller_stop (struct nxt200x_state* state)
257{
258 u8 buf, stopval, counter = 0;
259 dprintk("%s\n", __FUNCTION__);
260
261 /* set correct stop value */
262 switch (state->demod_chip) {
263 case NXT2002:
264 stopval = 0x40;
265 break;
266 case NXT2004:
267 stopval = 0x10;
268 break;
269 default:
270 stopval = 0;
271 break;
272 }
273
274 buf = 0x80;
275 nxt200x_writebytes(state, 0x22, &buf, 1);
276
277 while (counter < 20) {
278 nxt200x_readbytes(state, 0x31, &buf, 1);
279 if (buf & stopval)
280 return;
281 msleep(10);
282 counter++;
283 }
284
285 printk(KERN_WARNING "nxt200x: Timeout waiting for nxt200x to stop. This is ok after firmware upload.\n");
286 return;
287}
288
289static void nxt200x_microcontroller_start (struct nxt200x_state* state)
290{
291 u8 buf;
292 dprintk("%s\n", __FUNCTION__);
293
294 buf = 0x00;
295 nxt200x_writebytes(state, 0x22, &buf, 1);
296}
297
298static void nxt2004_microcontroller_init (struct nxt200x_state* state)
299{
300 u8 buf[9];
301 u8 counter = 0;
302 dprintk("%s\n", __FUNCTION__);
303
304 buf[0] = 0x00;
305 nxt200x_writebytes(state, 0x2b, buf, 1);
306 buf[0] = 0x70;
307 nxt200x_writebytes(state, 0x34, buf, 1);
308 buf[0] = 0x04;
309 nxt200x_writebytes(state, 0x35, buf, 1);
310 buf[0] = 0x01; buf[1] = 0x23; buf[2] = 0x45; buf[3] = 0x67; buf[4] = 0x89;
311 buf[5] = 0xAB; buf[6] = 0xCD; buf[7] = 0xEF; buf[8] = 0xC0;
312 nxt200x_writebytes(state, 0x36, buf, 9);
313 buf[0] = 0x80;
314 nxt200x_writebytes(state, 0x21, buf, 1);
315
316 while (counter < 20) {
317 nxt200x_readbytes(state, 0x21, buf, 1);
318 if (buf[0] == 0)
319 return;
320 msleep(10);
321 counter++;
322 }
323
324 printk(KERN_WARNING "nxt200x: Timeout waiting for nxt2004 to init.\n");
325
326 return;
327}
328
329static int nxt200x_writetuner (struct nxt200x_state* state, u8* data)
330{
331 u8 buf, count = 0;
332
333 dprintk("%s\n", __FUNCTION__);
334
Andrew de Quincey638a3fb2006-04-18 17:47:10 -0300335 dprintk("Tuner Bytes: %02X %02X %02X %02X\n", data[1], data[2], data[3], data[4]);
Kirk Lapray04a45922005-11-08 21:35:46 -0800336
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800337 /* if NXT2004, write directly to tuner. if NXT2002, write through NXT chip.
338 * direct write is required for Philips TUV1236D and ALPS TDHU2 */
339 switch (state->demod_chip) {
340 case NXT2004:
Andrew de Quincey638a3fb2006-04-18 17:47:10 -0300341 if (i2c_writebytes(state, data[0], data+1, 4))
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800342 printk(KERN_WARNING "nxt200x: error writing to tuner\n");
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800343 /* wait until we have a lock */
344 while (count < 20) {
Andrew de Quincey638a3fb2006-04-18 17:47:10 -0300345 i2c_readbytes(state, data[0], &buf, 1);
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800346 if (buf & 0x40)
347 return 0;
348 msleep(100);
349 count++;
350 }
351 printk("nxt2004: timeout waiting for tuner lock\n");
352 break;
353 case NXT2002:
354 /* set the i2c transfer speed to the tuner */
355 buf = 0x03;
356 nxt200x_writebytes(state, 0x20, &buf, 1);
Kirk Lapray04a45922005-11-08 21:35:46 -0800357
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800358 /* setup to transfer 4 bytes via i2c */
359 buf = 0x04;
360 nxt200x_writebytes(state, 0x34, &buf, 1);
Kirk Lapray04a45922005-11-08 21:35:46 -0800361
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800362 /* write actual tuner bytes */
Andrew de Quincey638a3fb2006-04-18 17:47:10 -0300363 nxt200x_writebytes(state, 0x36, data+1, 4);
Kirk Lapray04a45922005-11-08 21:35:46 -0800364
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800365 /* set tuner i2c address */
Andrew de Quincey638a3fb2006-04-18 17:47:10 -0300366 buf = data[0] << 1;
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800367 nxt200x_writebytes(state, 0x35, &buf, 1);
Kirk Lapray04a45922005-11-08 21:35:46 -0800368
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800369 /* write UC Opmode to begin transfer */
370 buf = 0x80;
371 nxt200x_writebytes(state, 0x21, &buf, 1);
Kirk Lapray04a45922005-11-08 21:35:46 -0800372
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800373 while (count < 20) {
374 nxt200x_readbytes(state, 0x21, &buf, 1);
375 if ((buf & 0x80)== 0x00)
376 return 0;
377 msleep(100);
378 count++;
379 }
380 printk("nxt2002: timeout error writing tuner\n");
381 break;
382 default:
383 return -EINVAL;
384 break;
Kirk Lapray04a45922005-11-08 21:35:46 -0800385 }
Michael Krufkyf0fa86a2005-11-08 21:35:49 -0800386 return 0;
Kirk Lapray04a45922005-11-08 21:35:46 -0800387}
388
389static void nxt200x_agc_reset(struct nxt200x_state* state)
390{
391 u8 buf;
392 dprintk("%s\n", __FUNCTION__);
393
394 switch (state->demod_chip) {
395 case NXT2002:
396 buf = 0x08;
397 nxt200x_writebytes(state, 0x08, &buf, 1);
398 buf = 0x00;
399 nxt200x_writebytes(state, 0x08, &buf, 1);
400 break;
401 case NXT2004:
402 nxt200x_readreg_multibyte(state, 0x08, &buf, 1);
403 buf = 0x08;
404 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
405 buf = 0x00;
406 nxt200x_writereg_multibyte(state, 0x08, &buf, 1);
407 break;
408 default:
409 break;
410 }
411 return;
412}
413
414static int nxt2002_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
415{
416
417 struct nxt200x_state* state = fe->demodulator_priv;
418 u8 buf[3], written = 0, chunkpos = 0;
419 u16 rambase, position, crc = 0;
420
421 dprintk("%s\n", __FUNCTION__);
422 dprintk("Firmware is %zu bytes\n", fw->size);
423
424 /* Get the RAM base for this nxt2002 */
425 nxt200x_readbytes(state, 0x10, buf, 1);
426
427 if (buf[0] & 0x10)
428 rambase = 0x1000;
429 else
430 rambase = 0x0000;
431
432 dprintk("rambase on this nxt2002 is %04X\n", rambase);
433
434 /* Hold the micro in reset while loading firmware */
435 buf[0] = 0x80;
436 nxt200x_writebytes(state, 0x2B, buf, 1);
437
438 for (position = 0; position < fw->size; position++) {
439 if (written == 0) {
440 crc = 0;
441 chunkpos = 0x28;
442 buf[0] = ((rambase + position) >> 8);
443 buf[1] = (rambase + position) & 0xFF;
444 buf[2] = 0x81;
445 /* write starting address */
446 nxt200x_writebytes(state, 0x29, buf, 3);
447 }
448 written++;
449 chunkpos++;
450
451 if ((written % 4) == 0)
452 nxt200x_writebytes(state, chunkpos, &fw->data[position-3], 4);
453
454 crc = nxt200x_crc(crc, fw->data[position]);
455
456 if ((written == 255) || (position+1 == fw->size)) {
457 /* write remaining bytes of firmware */
458 nxt200x_writebytes(state, chunkpos+4-(written %4),
459 &fw->data[position-(written %4) + 1],
460 written %4);
461 buf[0] = crc << 8;
462 buf[1] = crc & 0xFF;
463
464 /* write crc */
465 nxt200x_writebytes(state, 0x2C, buf, 2);
466
467 /* do a read to stop things */
468 nxt200x_readbytes(state, 0x2A, buf, 1);
469
470 /* set transfer mode to complete */
471 buf[0] = 0x80;
472 nxt200x_writebytes(state, 0x2B, buf, 1);
473
474 written = 0;
475 }
476 }
477
478 return 0;
479};
480
481static int nxt2004_load_firmware (struct dvb_frontend* fe, const struct firmware *fw)
482{
483
484 struct nxt200x_state* state = fe->demodulator_priv;
485 u8 buf[3];
486 u16 rambase, position, crc=0;
487
488 dprintk("%s\n", __FUNCTION__);
489 dprintk("Firmware is %zu bytes\n", fw->size);
490
491 /* set rambase */
492 rambase = 0x1000;
493
494 /* hold the micro in reset while loading firmware */
495 buf[0] = 0x80;
496 nxt200x_writebytes(state, 0x2B, buf,1);
497
498 /* calculate firmware CRC */
499 for (position = 0; position < fw->size; position++) {
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -0800500 crc = nxt200x_crc(crc, fw->data[position]);
Kirk Lapray04a45922005-11-08 21:35:46 -0800501 }
502
503 buf[0] = rambase >> 8;
504 buf[1] = rambase & 0xFF;
505 buf[2] = 0x81;
506 /* write starting address */
507 nxt200x_writebytes(state,0x29,buf,3);
508
509 for (position = 0; position < fw->size;) {
510 nxt200x_writebytes(state, 0x2C, &fw->data[position],
511 fw->size-position > 255 ? 255 : fw->size-position);
512 position += (fw->size-position > 255 ? 255 : fw->size-position);
513 }
514 buf[0] = crc >> 8;
515 buf[1] = crc & 0xFF;
516
517 dprintk("firmware crc is 0x%02X 0x%02X\n", buf[0], buf[1]);
518
519 /* write crc */
520 nxt200x_writebytes(state, 0x2C, buf,2);
521
522 /* do a read to stop things */
523 nxt200x_readbytes(state, 0x2C, buf, 1);
524
525 /* set transfer mode to complete */
526 buf[0] = 0x80;
527 nxt200x_writebytes(state, 0x2B, buf,1);
528
529 return 0;
530};
531
532static int nxt200x_setup_frontend_parameters (struct dvb_frontend* fe,
533 struct dvb_frontend_parameters *p)
534{
535 struct nxt200x_state* state = fe->demodulator_priv;
Andrew de Quincey638a3fb2006-04-18 17:47:10 -0300536 u8 buf[5];
Kirk Lapray04a45922005-11-08 21:35:46 -0800537
538 /* stop the micro first */
539 nxt200x_microcontroller_stop(state);
540
541 if (state->demod_chip == NXT2004) {
542 /* make sure demod is set to digital */
543 buf[0] = 0x04;
544 nxt200x_writebytes(state, 0x14, buf, 1);
545 buf[0] = 0x00;
546 nxt200x_writebytes(state, 0x17, buf, 1);
547 }
548
Kirk Lapray04a45922005-11-08 21:35:46 -0800549 /* set additional params */
550 switch (p->u.vsb.modulation) {
551 case QAM_64:
552 case QAM_256:
553 /* Set punctured clock for QAM */
554 /* This is just a guess since I am unable to test it */
Michael Krufkyc6dd2d52005-11-08 21:35:47 -0800555 if (state->config->set_ts_params)
556 state->config->set_ts_params(fe, 1);
Kirk Lapray04a45922005-11-08 21:35:46 -0800557 break;
558 case VSB_8:
559 /* Set non-punctured clock for VSB */
Michael Krufkyc6dd2d52005-11-08 21:35:47 -0800560 if (state->config->set_ts_params)
561 state->config->set_ts_params(fe, 0);
Kirk Lapray04a45922005-11-08 21:35:46 -0800562 break;
563 default:
564 return -EINVAL;
565 break;
566 }
567
Michael Krufky4abe9f92007-05-05 12:15:57 -0300568 if (fe->ops.tuner_ops.calc_regs) {
569 /* get tuning information */
570 fe->ops.tuner_ops.calc_regs(fe, p, buf, 5);
571
572 /* write frequency information */
573 nxt200x_writetuner(state, buf);
574 }
Kirk Lapray04a45922005-11-08 21:35:46 -0800575
576 /* reset the agc now that tuning has been completed */
577 nxt200x_agc_reset(state);
578
579 /* set target power level */
580 switch (p->u.vsb.modulation) {
581 case QAM_64:
582 case QAM_256:
583 buf[0] = 0x74;
584 break;
585 case VSB_8:
586 buf[0] = 0x70;
587 break;
588 default:
589 return -EINVAL;
590 break;
591 }
592 nxt200x_writebytes(state, 0x42, buf, 1);
593
594 /* configure sdm */
595 switch (state->demod_chip) {
596 case NXT2002:
597 buf[0] = 0x87;
598 break;
599 case NXT2004:
600 buf[0] = 0x07;
601 break;
602 default:
603 return -EINVAL;
604 break;
605 }
606 nxt200x_writebytes(state, 0x57, buf, 1);
607
608 /* write sdm1 input */
609 buf[0] = 0x10;
610 buf[1] = 0x00;
Michael Krufky46365f32006-01-23 09:52:39 -0200611 switch (state->demod_chip) {
612 case NXT2002:
613 nxt200x_writereg_multibyte(state, 0x58, buf, 2);
614 break;
615 case NXT2004:
616 nxt200x_writebytes(state, 0x58, buf, 2);
617 break;
618 default:
619 return -EINVAL;
620 break;
621 }
Kirk Lapray04a45922005-11-08 21:35:46 -0800622
623 /* write sdmx input */
624 switch (p->u.vsb.modulation) {
625 case QAM_64:
626 buf[0] = 0x68;
627 break;
628 case QAM_256:
629 buf[0] = 0x64;
630 break;
631 case VSB_8:
632 buf[0] = 0x60;
633 break;
634 default:
635 return -EINVAL;
636 break;
637 }
638 buf[1] = 0x00;
Michael Krufky46365f32006-01-23 09:52:39 -0200639 switch (state->demod_chip) {
640 case NXT2002:
641 nxt200x_writereg_multibyte(state, 0x5C, buf, 2);
642 break;
643 case NXT2004:
644 nxt200x_writebytes(state, 0x5C, buf, 2);
645 break;
646 default:
647 return -EINVAL;
648 break;
649 }
Kirk Lapray04a45922005-11-08 21:35:46 -0800650
651 /* write adc power lpf fc */
652 buf[0] = 0x05;
653 nxt200x_writebytes(state, 0x43, buf, 1);
654
655 if (state->demod_chip == NXT2004) {
656 /* write ??? */
657 buf[0] = 0x00;
658 buf[1] = 0x00;
659 nxt200x_writebytes(state, 0x46, buf, 2);
660 }
661
662 /* write accumulator2 input */
663 buf[0] = 0x80;
664 buf[1] = 0x00;
Michael Krufky46365f32006-01-23 09:52:39 -0200665 switch (state->demod_chip) {
666 case NXT2002:
667 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
668 break;
669 case NXT2004:
670 nxt200x_writebytes(state, 0x4B, buf, 2);
671 break;
672 default:
673 return -EINVAL;
674 break;
675 }
Kirk Lapray04a45922005-11-08 21:35:46 -0800676
677 /* write kg1 */
678 buf[0] = 0x00;
679 nxt200x_writebytes(state, 0x4D, buf, 1);
680
681 /* write sdm12 lpf fc */
682 buf[0] = 0x44;
683 nxt200x_writebytes(state, 0x55, buf, 1);
684
685 /* write agc control reg */
686 buf[0] = 0x04;
687 nxt200x_writebytes(state, 0x41, buf, 1);
688
689 if (state->demod_chip == NXT2004) {
690 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
691 buf[0] = 0x24;
692 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
693
694 /* soft reset? */
695 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
696 buf[0] = 0x10;
697 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
698 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
699 buf[0] = 0x00;
700 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
701
702 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
703 buf[0] = 0x04;
704 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
705 buf[0] = 0x00;
706 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
707 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
708 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
709 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
710 buf[0] = 0x11;
711 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
712 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
713 buf[0] = 0x44;
714 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
715 }
716
717 /* write agc ucgp0 */
718 switch (p->u.vsb.modulation) {
719 case QAM_64:
720 buf[0] = 0x02;
721 break;
722 case QAM_256:
723 buf[0] = 0x03;
724 break;
725 case VSB_8:
726 buf[0] = 0x00;
727 break;
728 default:
729 return -EINVAL;
730 break;
731 }
732 nxt200x_writebytes(state, 0x30, buf, 1);
733
734 /* write agc control reg */
735 buf[0] = 0x00;
736 nxt200x_writebytes(state, 0x41, buf, 1);
737
738 /* write accumulator2 input */
739 buf[0] = 0x80;
740 buf[1] = 0x00;
Michael Krufky46365f32006-01-23 09:52:39 -0200741 switch (state->demod_chip) {
742 case NXT2002:
743 nxt200x_writereg_multibyte(state, 0x49, buf, 2);
744 nxt200x_writereg_multibyte(state, 0x4B, buf, 2);
745 break;
746 case NXT2004:
747 nxt200x_writebytes(state, 0x49, buf, 2);
748 nxt200x_writebytes(state, 0x4B, buf, 2);
749 break;
750 default:
751 return -EINVAL;
752 break;
753 }
Kirk Lapray04a45922005-11-08 21:35:46 -0800754
755 /* write agc control reg */
756 buf[0] = 0x04;
757 nxt200x_writebytes(state, 0x41, buf, 1);
758
759 nxt200x_microcontroller_start(state);
760
761 if (state->demod_chip == NXT2004) {
762 nxt2004_microcontroller_init(state);
763
764 /* ???? */
765 buf[0] = 0xF0;
766 buf[1] = 0x00;
767 nxt200x_writebytes(state, 0x5C, buf, 2);
768 }
769
770 /* adjacent channel detection should be done here, but I don't
771 have any stations with this need so I cannot test it */
772
773 return 0;
774}
775
776static int nxt200x_read_status(struct dvb_frontend* fe, fe_status_t* status)
777{
778 struct nxt200x_state* state = fe->demodulator_priv;
779 u8 lock;
780 nxt200x_readbytes(state, 0x31, &lock, 1);
781
782 *status = 0;
783 if (lock & 0x20) {
784 *status |= FE_HAS_SIGNAL;
785 *status |= FE_HAS_CARRIER;
786 *status |= FE_HAS_VITERBI;
787 *status |= FE_HAS_SYNC;
788 *status |= FE_HAS_LOCK;
789 }
790 return 0;
791}
792
793static int nxt200x_read_ber(struct dvb_frontend* fe, u32* ber)
794{
795 struct nxt200x_state* state = fe->demodulator_priv;
796 u8 b[3];
797
798 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
799
800 *ber = ((b[0] << 8) + b[1]) * 8;
801
802 return 0;
803}
804
805static int nxt200x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
806{
807 struct nxt200x_state* state = fe->demodulator_priv;
808 u8 b[2];
809 u16 temp = 0;
810
811 /* setup to read cluster variance */
812 b[0] = 0x00;
813 nxt200x_writebytes(state, 0xA1, b, 1);
814
815 /* get multreg val */
816 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
817
818 temp = (b[0] << 8) | b[1];
819 *strength = ((0x7FFF - temp) & 0x0FFF) * 16;
820
821 return 0;
822}
823
824static int nxt200x_read_snr(struct dvb_frontend* fe, u16* snr)
825{
826
827 struct nxt200x_state* state = fe->demodulator_priv;
828 u8 b[2];
829 u16 temp = 0, temp2;
830 u32 snrdb = 0;
831
832 /* setup to read cluster variance */
833 b[0] = 0x00;
834 nxt200x_writebytes(state, 0xA1, b, 1);
835
836 /* get multreg val from 0xA6 */
837 nxt200x_readreg_multibyte(state, 0xA6, b, 2);
838
839 temp = (b[0] << 8) | b[1];
840 temp2 = 0x7FFF - temp;
841
842 /* snr will be in db */
843 if (temp2 > 0x7F00)
844 snrdb = 1000*24 + ( 1000*(30-24) * ( temp2 - 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
845 else if (temp2 > 0x7EC0)
846 snrdb = 1000*18 + ( 1000*(24-18) * ( temp2 - 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
847 else if (temp2 > 0x7C00)
848 snrdb = 1000*12 + ( 1000*(18-12) * ( temp2 - 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
849 else
850 snrdb = 1000*0 + ( 1000*(12-0) * ( temp2 - 0 ) / ( 0x7C00 - 0 ) );
851
852 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
853 *snr = snrdb * (0xFFFF/32000);
854
855 return 0;
856}
857
858static int nxt200x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
859{
860 struct nxt200x_state* state = fe->demodulator_priv;
861 u8 b[3];
862
863 nxt200x_readreg_multibyte(state, 0xE6, b, 3);
864 *ucblocks = b[2];
865
866 return 0;
867}
868
869static int nxt200x_sleep(struct dvb_frontend* fe)
870{
871 return 0;
872}
873
874static int nxt2002_init(struct dvb_frontend* fe)
875{
876 struct nxt200x_state* state = fe->demodulator_priv;
877 const struct firmware *fw;
878 int ret;
879 u8 buf[2];
880
881 /* request the firmware, this will block until someone uploads it */
882 printk("nxt2002: Waiting for firmware upload (%s)...\n", NXT2002_DEFAULT_FIRMWARE);
883 ret = request_firmware(&fw, NXT2002_DEFAULT_FIRMWARE, &state->i2c->dev);
884 printk("nxt2002: Waiting for firmware upload(2)...\n");
885 if (ret) {
886 printk("nxt2002: No firmware uploaded (timeout or file not found?)\n");
887 return ret;
888 }
889
890 ret = nxt2002_load_firmware(fe, fw);
Magnus Damm73ca66b2006-07-10 04:44:09 -0700891 release_firmware(fw);
Kirk Lapray04a45922005-11-08 21:35:46 -0800892 if (ret) {
893 printk("nxt2002: Writing firmware to device failed\n");
Kirk Lapray04a45922005-11-08 21:35:46 -0800894 return ret;
895 }
896 printk("nxt2002: Firmware upload complete\n");
897
898 /* Put the micro into reset */
899 nxt200x_microcontroller_stop(state);
900
901 /* ensure transfer is complete */
902 buf[0]=0x00;
903 nxt200x_writebytes(state, 0x2B, buf, 1);
904
905 /* Put the micro into reset for real this time */
906 nxt200x_microcontroller_stop(state);
907
908 /* soft reset everything (agc,frontend,eq,fec)*/
909 buf[0] = 0x0F;
910 nxt200x_writebytes(state, 0x08, buf, 1);
911 buf[0] = 0x00;
912 nxt200x_writebytes(state, 0x08, buf, 1);
913
914 /* write agc sdm configure */
915 buf[0] = 0xF1;
916 nxt200x_writebytes(state, 0x57, buf, 1);
917
918 /* write mod output format */
919 buf[0] = 0x20;
920 nxt200x_writebytes(state, 0x09, buf, 1);
921
922 /* write fec mpeg mode */
923 buf[0] = 0x7E;
924 buf[1] = 0x00;
925 nxt200x_writebytes(state, 0xE9, buf, 2);
926
927 /* write mux selection */
928 buf[0] = 0x00;
929 nxt200x_writebytes(state, 0xCC, buf, 1);
930
931 return 0;
932}
933
934static int nxt2004_init(struct dvb_frontend* fe)
935{
936 struct nxt200x_state* state = fe->demodulator_priv;
937 const struct firmware *fw;
938 int ret;
939 u8 buf[3];
940
941 /* ??? */
942 buf[0]=0x00;
943 nxt200x_writebytes(state, 0x1E, buf, 1);
944
945 /* request the firmware, this will block until someone uploads it */
946 printk("nxt2004: Waiting for firmware upload (%s)...\n", NXT2004_DEFAULT_FIRMWARE);
947 ret = request_firmware(&fw, NXT2004_DEFAULT_FIRMWARE, &state->i2c->dev);
948 printk("nxt2004: Waiting for firmware upload(2)...\n");
949 if (ret) {
950 printk("nxt2004: No firmware uploaded (timeout or file not found?)\n");
951 return ret;
952 }
953
954 ret = nxt2004_load_firmware(fe, fw);
Magnus Damm73ca66b2006-07-10 04:44:09 -0700955 release_firmware(fw);
Kirk Lapray04a45922005-11-08 21:35:46 -0800956 if (ret) {
957 printk("nxt2004: Writing firmware to device failed\n");
Kirk Lapray04a45922005-11-08 21:35:46 -0800958 return ret;
959 }
960 printk("nxt2004: Firmware upload complete\n");
961
962 /* ensure transfer is complete */
963 buf[0] = 0x01;
964 nxt200x_writebytes(state, 0x19, buf, 1);
965
966 nxt2004_microcontroller_init(state);
967 nxt200x_microcontroller_stop(state);
968 nxt200x_microcontroller_stop(state);
969 nxt2004_microcontroller_init(state);
970 nxt200x_microcontroller_stop(state);
971
972 /* soft reset everything (agc,frontend,eq,fec)*/
973 buf[0] = 0xFF;
974 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
975 buf[0] = 0x00;
976 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
977
978 /* write agc sdm configure */
979 buf[0] = 0xD7;
980 nxt200x_writebytes(state, 0x57, buf, 1);
981
982 /* ???*/
983 buf[0] = 0x07;
984 buf[1] = 0xfe;
985 nxt200x_writebytes(state, 0x35, buf, 2);
986 buf[0] = 0x12;
987 nxt200x_writebytes(state, 0x34, buf, 1);
988 buf[0] = 0x80;
989 nxt200x_writebytes(state, 0x21, buf, 1);
990
991 /* ???*/
992 buf[0] = 0x21;
993 nxt200x_writebytes(state, 0x0A, buf, 1);
994
995 /* ???*/
996 buf[0] = 0x01;
997 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
998
999 /* write fec mpeg mode */
1000 buf[0] = 0x7E;
1001 buf[1] = 0x00;
1002 nxt200x_writebytes(state, 0xE9, buf, 2);
1003
1004 /* write mux selection */
1005 buf[0] = 0x00;
1006 nxt200x_writebytes(state, 0xCC, buf, 1);
1007
1008 /* ???*/
1009 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1010 buf[0] = 0x00;
1011 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1012
1013 /* soft reset? */
1014 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1015 buf[0] = 0x10;
1016 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1017 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1018 buf[0] = 0x00;
1019 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1020
1021 /* ???*/
1022 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1023 buf[0] = 0x01;
1024 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1025 buf[0] = 0x70;
1026 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1027 buf[0] = 0x31; buf[1] = 0x5E; buf[2] = 0x66;
1028 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1029
1030 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1031 buf[0] = 0x11;
1032 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1033 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1034 buf[0] = 0x40;
1035 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1036
1037 nxt200x_readbytes(state, 0x10, buf, 1);
1038 buf[0] = 0x10;
1039 nxt200x_writebytes(state, 0x10, buf, 1);
1040 nxt200x_readbytes(state, 0x0A, buf, 1);
1041 buf[0] = 0x21;
1042 nxt200x_writebytes(state, 0x0A, buf, 1);
1043
1044 nxt2004_microcontroller_init(state);
1045
1046 buf[0] = 0x21;
1047 nxt200x_writebytes(state, 0x0A, buf, 1);
1048 buf[0] = 0x7E;
1049 nxt200x_writebytes(state, 0xE9, buf, 1);
1050 buf[0] = 0x00;
1051 nxt200x_writebytes(state, 0xEA, buf, 1);
1052
1053 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1054 buf[0] = 0x00;
1055 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1056 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1057 buf[0] = 0x00;
1058 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1059
1060 /* soft reset? */
1061 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1062 buf[0] = 0x10;
1063 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1064 nxt200x_readreg_multibyte(state, 0x08, buf, 1);
1065 buf[0] = 0x00;
1066 nxt200x_writereg_multibyte(state, 0x08, buf, 1);
1067
1068 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1069 buf[0] = 0x04;
1070 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1071 buf[0] = 0x00;
1072 nxt200x_writereg_multibyte(state, 0x81, buf, 1);
1073 buf[0] = 0x80; buf[1] = 0x00; buf[2] = 0x00;
1074 nxt200x_writereg_multibyte(state, 0x82, buf, 3);
1075
1076 nxt200x_readreg_multibyte(state, 0x88, buf, 1);
1077 buf[0] = 0x11;
1078 nxt200x_writereg_multibyte(state, 0x88, buf, 1);
1079
1080 nxt200x_readreg_multibyte(state, 0x80, buf, 1);
1081 buf[0] = 0x44;
1082 nxt200x_writereg_multibyte(state, 0x80, buf, 1);
1083
1084 /* initialize tuner */
1085 nxt200x_readbytes(state, 0x10, buf, 1);
1086 buf[0] = 0x12;
1087 nxt200x_writebytes(state, 0x10, buf, 1);
1088 buf[0] = 0x04;
1089 nxt200x_writebytes(state, 0x13, buf, 1);
1090 buf[0] = 0x00;
1091 nxt200x_writebytes(state, 0x16, buf, 1);
1092 buf[0] = 0x04;
1093 nxt200x_writebytes(state, 0x14, buf, 1);
1094 buf[0] = 0x00;
1095 nxt200x_writebytes(state, 0x14, buf, 1);
1096 nxt200x_writebytes(state, 0x17, buf, 1);
1097 nxt200x_writebytes(state, 0x14, buf, 1);
1098 nxt200x_writebytes(state, 0x17, buf, 1);
1099
1100 return 0;
1101}
1102
1103static int nxt200x_init(struct dvb_frontend* fe)
1104{
1105 struct nxt200x_state* state = fe->demodulator_priv;
1106 int ret = 0;
1107
1108 if (!state->initialised) {
1109 switch (state->demod_chip) {
1110 case NXT2002:
1111 ret = nxt2002_init(fe);
1112 break;
1113 case NXT2004:
1114 ret = nxt2004_init(fe);
1115 break;
1116 default:
1117 return -EINVAL;
1118 break;
1119 }
1120 state->initialised = 1;
1121 }
1122 return ret;
1123}
1124
1125static int nxt200x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
1126{
1127 fesettings->min_delay_ms = 500;
1128 fesettings->step_size = 0;
1129 fesettings->max_drift = 0;
1130 return 0;
1131}
1132
1133static void nxt200x_release(struct dvb_frontend* fe)
1134{
1135 struct nxt200x_state* state = fe->demodulator_priv;
1136 kfree(state);
1137}
1138
1139static struct dvb_frontend_ops nxt200x_ops;
1140
1141struct dvb_frontend* nxt200x_attach(const struct nxt200x_config* config,
1142 struct i2c_adapter* i2c)
1143{
1144 struct nxt200x_state* state = NULL;
1145 u8 buf [] = {0,0,0,0,0};
1146
1147 /* allocate memory for the internal state */
Panagiotis Issaris74081872006-01-11 19:40:56 -02001148 state = kzalloc(sizeof(struct nxt200x_state), GFP_KERNEL);
Kirk Lapray04a45922005-11-08 21:35:46 -08001149 if (state == NULL)
1150 goto error;
Kirk Lapray04a45922005-11-08 21:35:46 -08001151
1152 /* setup the state */
1153 state->config = config;
1154 state->i2c = i2c;
Kirk Lapray04a45922005-11-08 21:35:46 -08001155 state->initialised = 0;
1156
1157 /* read card id */
1158 nxt200x_readbytes(state, 0x00, buf, 5);
1159 dprintk("NXT info: %02X %02X %02X %02X %02X\n",
1160 buf[0], buf[1], buf[2], buf[3], buf[4]);
1161
1162 /* set demod chip */
1163 switch (buf[0]) {
1164 case 0x04:
1165 state->demod_chip = NXT2002;
1166 printk("nxt200x: NXT2002 Detected\n");
1167 break;
1168 case 0x05:
1169 state->demod_chip = NXT2004;
1170 printk("nxt200x: NXT2004 Detected\n");
1171 break;
1172 default:
1173 goto error;
1174 }
1175
1176 /* make sure demod chip is supported */
1177 switch (state->demod_chip) {
1178 case NXT2002:
1179 if (buf[0] != 0x04) goto error; /* device id */
1180 if (buf[1] != 0x02) goto error; /* fab id */
1181 if (buf[2] != 0x11) goto error; /* month */
1182 if (buf[3] != 0x20) goto error; /* year msb */
1183 if (buf[4] != 0x00) goto error; /* year lsb */
1184 break;
1185 case NXT2004:
1186 if (buf[0] != 0x05) goto error; /* device id */
1187 break;
1188 default:
1189 goto error;
1190 }
1191
1192 /* create dvb_frontend */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001193 memcpy(&state->frontend.ops, &nxt200x_ops, sizeof(struct dvb_frontend_ops));
Kirk Lapray04a45922005-11-08 21:35:46 -08001194 state->frontend.demodulator_priv = state;
1195 return &state->frontend;
1196
1197error:
Michael Krufky6d35ae32005-11-08 21:35:48 -08001198 kfree(state);
Kirk Lapray04a45922005-11-08 21:35:46 -08001199 printk("Unknown/Unsupported NXT chip: %02X %02X %02X %02X %02X\n",
1200 buf[0], buf[1], buf[2], buf[3], buf[4]);
1201 return NULL;
1202}
1203
1204static struct dvb_frontend_ops nxt200x_ops = {
1205
1206 .info = {
1207 .name = "Nextwave NXT200X VSB/QAM frontend",
1208 .type = FE_ATSC,
1209 .frequency_min = 54000000,
1210 .frequency_max = 860000000,
1211 .frequency_stepsize = 166666, /* stepsize is just a guess */
1212 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1213 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1214 FE_CAN_8VSB | FE_CAN_QAM_64 | FE_CAN_QAM_256
1215 },
1216
1217 .release = nxt200x_release,
1218
1219 .init = nxt200x_init,
1220 .sleep = nxt200x_sleep,
1221
1222 .set_frontend = nxt200x_setup_frontend_parameters,
1223 .get_tune_settings = nxt200x_get_tune_settings,
1224
1225 .read_status = nxt200x_read_status,
1226 .read_ber = nxt200x_read_ber,
1227 .read_signal_strength = nxt200x_read_signal_strength,
1228 .read_snr = nxt200x_read_snr,
1229 .read_ucblocks = nxt200x_read_ucblocks,
1230};
1231
1232module_param(debug, int, 0644);
1233MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1234
1235MODULE_DESCRIPTION("NXT200X (ATSC 8VSB & ITU-T J.83 AnnexB 64/256 QAM) Demodulator Driver");
Michael Krufky46365f32006-01-23 09:52:39 -02001236MODULE_AUTHOR("Kirk Lapray, Michael Krufky, Jean-Francois Thibert, and Taylor Jacob");
Kirk Lapray04a45922005-11-08 21:35:46 -08001237MODULE_LICENSE("GPL");
1238
1239EXPORT_SYMBOL(nxt200x_attach);
1240