2 Support for B2C2/BBTI Technisat Air2PC - ATSC
4 Copyright (C) 2004 Taylor Jacob <rtjacob@earthlink.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * This driver needs external firmware. Please use the command
24 * "<kerneldir>/Documentation/dvb/get_dvb_firmware nxt2002" to
25 * download/extract it, and then copy it to /usr/lib/hotplug/firmware.
27 #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
28 #define CRC_CCIT_MASK 0x1021
30 #include <linux/init.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/device.h>
34 #include <linux/firmware.h>
36 #include "dvb_frontend.h"
39 struct nxt2002_state
{
41 struct i2c_adapter
* i2c
;
42 struct dvb_frontend_ops ops
;
43 const struct nxt2002_config
* config
;
44 struct dvb_frontend frontend
;
46 /* demodulator private data */
51 #define dprintk(args...) \
53 if (debug) printk(KERN_DEBUG "nxt2002: " args); \
56 static int i2c_writebytes (struct nxt2002_state
* state
, u8 reg
, u8
*buf
, u8 len
)
58 /* probbably a much better way or doing this */
61 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf2
, .len
= len
+ 1 };
64 for (x
= 0 ; x
< len
; x
++)
67 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
68 printk ("%s: i2c write error (addr %02x, err == %i)\n",
69 __FUNCTION__
, state
->config
->demod_address
, err
);
76 static u8
i2c_readbytes (struct nxt2002_state
* state
, u8 reg
, u8
* buf
, u8 len
)
80 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= reg2
, .len
= 1 },
81 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= buf
, .len
= len
} };
85 if ((err
= i2c_transfer (state
->i2c
, msg
, 2)) != 2) {
86 printk ("%s: i2c read error (addr %02x, err == %i)\n",
87 __FUNCTION__
, state
->config
->demod_address
, err
);
94 static u16
nxt2002_crc(u16 crc
, u8 c
)
98 u16 input
= (u16
) c
& 0xFF;
102 if((crc
^ input
) & 0x8000)
103 crc
=(crc
<<1)^CRC_CCIT_MASK
;
111 static int nxt2002_writereg_multibyte (struct nxt2002_state
* state
, u8 reg
, u8
* data
, u8 len
)
114 dprintk("%s\n", __FUNCTION__
);
116 /* set multi register length */
117 i2c_writebytes(state
,0x34,&len
,1);
119 /* set mutli register register */
120 i2c_writebytes(state
,0x35,®
,1);
122 /* send the actual data */
123 i2c_writebytes(state
,0x36,data
,len
);
125 /* toggle the multireg write bit*/
127 i2c_writebytes(state
,0x21,&buf
,1);
129 i2c_readbytes(state
,0x21,&buf
,1);
131 if ((buf
& 0x02) == 0)
134 dprintk("Error writing multireg register %02X\n",reg
);
139 static int nxt2002_readreg_multibyte (struct nxt2002_state
* state
, u8 reg
, u8
* data
, u8 len
)
142 dprintk("%s\n", __FUNCTION__
);
144 /* set multi register length */
146 i2c_writebytes(state
,0x34,&len2
,1);
148 /* set mutli register register */
149 i2c_writebytes(state
,0x35,®
,1);
151 /* send the actual data */
152 i2c_readbytes(state
,reg
,data
,len
);
157 static void nxt2002_microcontroller_stop (struct nxt2002_state
* state
)
159 u8 buf
[2],counter
= 0;
160 dprintk("%s\n", __FUNCTION__
);
163 i2c_writebytes(state
,0x22,buf
,1);
165 while (counter
< 20) {
166 i2c_readbytes(state
,0x31,buf
,1);
173 dprintk("Timeout waiting for micro to stop.. This is ok after firmware upload\n");
177 static void nxt2002_microcontroller_start (struct nxt2002_state
* state
)
180 dprintk("%s\n", __FUNCTION__
);
183 i2c_writebytes(state
,0x22,&buf
,1);
186 static int nxt2002_writetuner (struct nxt2002_state
* state
, u8
* data
)
190 dprintk("Tuner Bytes: %02X %02X %02X %02X\n",data
[0],data
[1],data
[2],data
[3]);
192 dprintk("%s\n", __FUNCTION__
);
193 /* stop the micro first */
194 nxt2002_microcontroller_stop(state
);
196 /* set the i2c transfer speed to the tuner */
198 i2c_writebytes(state
,0x20,&buf
,1);
200 /* setup to transfer 4 bytes via i2c */
202 i2c_writebytes(state
,0x34,&buf
,1);
204 /* write actual tuner bytes */
205 i2c_writebytes(state
,0x36,data
,4);
207 /* set tuner i2c address */
209 i2c_writebytes(state
,0x35,&buf
,1);
211 /* write UC Opmode to begin transfer */
213 i2c_writebytes(state
,0x21,&buf
,1);
216 i2c_readbytes(state
,0x21,&buf
,1);
217 if ((buf
& 0x80)== 0x00)
223 printk("nxt2002: timeout error writing tuner\n");
227 static void nxt2002_agc_reset(struct nxt2002_state
* state
)
230 dprintk("%s\n", __FUNCTION__
);
233 i2c_writebytes(state
,0x08,&buf
,1);
236 i2c_writebytes(state
,0x08,&buf
,1);
241 static int nxt2002_load_firmware (struct dvb_frontend
* fe
, const struct firmware
*fw
)
244 struct nxt2002_state
* state
= fe
->demodulator_priv
;
245 u8 buf
[256],written
= 0,chunkpos
= 0;
246 u16 rambase
,position
,crc
= 0;
248 dprintk("%s\n", __FUNCTION__
);
249 dprintk("Firmware is %zu bytes\n",fw
->size
);
251 /* Get the RAM base for this nxt2002 */
252 i2c_readbytes(state
,0x10,buf
,1);
259 dprintk("rambase on this nxt2002 is %04X\n",rambase
);
261 /* Hold the micro in reset while loading firmware */
263 i2c_writebytes(state
,0x2B,buf
,1);
265 for (position
= 0; position
< fw
->size
; position
++) {
269 buf
[0] = ((rambase
+ position
) >> 8);
270 buf
[1] = (rambase
+ position
) & 0xFF;
272 /* write starting address */
273 i2c_writebytes(state
,0x29,buf
,3);
278 if ((written
% 4) == 0)
279 i2c_writebytes(state
,chunkpos
,&fw
->data
[position
-3],4);
281 crc
= nxt2002_crc(crc
,fw
->data
[position
]);
283 if ((written
== 255) || (position
+1 == fw
->size
)) {
284 /* write remaining bytes of firmware */
285 i2c_writebytes(state
, chunkpos
+4-(written
%4),
286 &fw
->data
[position
-(written
%4) + 1],
292 i2c_writebytes(state
,0x2C,buf
,2);
294 /* do a read to stop things */
295 i2c_readbytes(state
,0x2A,buf
,1);
297 /* set transfer mode to complete */
299 i2c_writebytes(state
,0x2B,buf
,1);
309 static int nxt2002_setup_frontend_parameters (struct dvb_frontend
* fe
,
310 struct dvb_frontend_parameters
*p
)
312 struct nxt2002_state
* state
= fe
->demodulator_priv
;
317 freq
= 44000 + ( p
->frequency
/ 1000 );
319 dprintk("freq = %d p->frequency = %d\n",freq
,p
->frequency
);
321 tunerfreq
= freq
* 24/4000;
323 buf
[0] = (tunerfreq
>> 8) & 0x7F;
324 buf
[1] = (tunerfreq
& 0xFF);
326 if (p
->frequency
<= 214000000) {
327 buf
[2] = 0x84 + (0x06 << 3);
328 buf
[3] = (p
->frequency
<= 172000000) ? 0x01 : 0x02;
329 } else if (p
->frequency
<= 721000000) {
330 buf
[2] = 0x84 + (0x07 << 3);
331 buf
[3] = (p
->frequency
<= 467000000) ? 0x02 : 0x08;
332 } else if (p
->frequency
<= 841000000) {
333 buf
[2] = 0x84 + (0x0E << 3);
336 buf
[2] = 0x84 + (0x0F << 3);
340 /* write frequency information */
341 nxt2002_writetuner(state
,buf
);
343 /* reset the agc now that tuning has been completed */
344 nxt2002_agc_reset(state
);
346 /* set target power level */
347 switch (p
->u
.vsb
.modulation
) {
359 i2c_writebytes(state
,0x42,buf
,1);
363 i2c_writebytes(state
,0x57,buf
,1);
365 /* write sdm1 input */
368 nxt2002_writereg_multibyte(state
,0x58,buf
,2);
370 /* write sdmx input */
371 switch (p
->u
.vsb
.modulation
) {
386 nxt2002_writereg_multibyte(state
,0x5C,buf
,2);
388 /* write adc power lpf fc */
390 i2c_writebytes(state
,0x43,buf
,1);
392 /* write adc power lpf fc */
394 i2c_writebytes(state
,0x43,buf
,1);
396 /* write accumulator2 input */
399 nxt2002_writereg_multibyte(state
,0x4B,buf
,2);
403 i2c_writebytes(state
,0x4D,buf
,1);
405 /* write sdm12 lpf fc */
407 i2c_writebytes(state
,0x55,buf
,1);
409 /* write agc control reg */
411 i2c_writebytes(state
,0x41,buf
,1);
413 /* write agc ucgp0 */
414 switch (p
->u
.vsb
.modulation
) {
428 i2c_writebytes(state
,0x30,buf
,1);
430 /* write agc control reg */
432 i2c_writebytes(state
,0x41,buf
,1);
434 /* write accumulator2 input */
437 nxt2002_writereg_multibyte(state
,0x49,buf
,2);
438 nxt2002_writereg_multibyte(state
,0x4B,buf
,2);
440 /* write agc control reg */
442 i2c_writebytes(state
,0x41,buf
,1);
444 nxt2002_microcontroller_start(state
);
446 /* adjacent channel detection should be done here, but I don't
447 have any stations with this need so I cannot test it */
452 static int nxt2002_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
454 struct nxt2002_state
* state
= fe
->demodulator_priv
;
456 i2c_readbytes(state
,0x31,&lock
,1);
460 *status
|= FE_HAS_SIGNAL
;
461 *status
|= FE_HAS_CARRIER
;
462 *status
|= FE_HAS_VITERBI
;
463 *status
|= FE_HAS_SYNC
;
464 *status
|= FE_HAS_LOCK
;
469 static int nxt2002_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
471 struct nxt2002_state
* state
= fe
->demodulator_priv
;
474 nxt2002_readreg_multibyte(state
,0xE6,b
,3);
476 *ber
= ((b
[0] << 8) + b
[1]) * 8;
481 static int nxt2002_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
483 struct nxt2002_state
* state
= fe
->demodulator_priv
;
487 /* setup to read cluster variance */
489 i2c_writebytes(state
,0xA1,b
,1);
491 /* get multreg val */
492 nxt2002_readreg_multibyte(state
,0xA6,b
,2);
494 temp
= (b
[0] << 8) | b
[1];
495 *strength
= ((0x7FFF - temp
) & 0x0FFF) * 16;
500 static int nxt2002_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
503 struct nxt2002_state
* state
= fe
->demodulator_priv
;
508 /* setup to read cluster variance */
510 i2c_writebytes(state
,0xA1,b
,1);
512 /* get multreg val from 0xA6 */
513 nxt2002_readreg_multibyte(state
,0xA6,b
,2);
515 temp
= (b
[0] << 8) | b
[1];
516 temp2
= 0x7FFF - temp
;
518 /* snr will be in db */
520 snrdb
= 1000*24 + ( 1000*(30-24) * ( temp2
- 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
521 else if (temp2
> 0x7EC0)
522 snrdb
= 1000*18 + ( 1000*(24-18) * ( temp2
- 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
523 else if (temp2
> 0x7C00)
524 snrdb
= 1000*12 + ( 1000*(18-12) * ( temp2
- 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
526 snrdb
= 1000*0 + ( 1000*(12-0) * ( temp2
- 0 ) / ( 0x7C00 - 0 ) );
528 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
530 *snr
= snrdb
* (0xFFFF/32000);
535 static int nxt2002_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
537 struct nxt2002_state
* state
= fe
->demodulator_priv
;
540 nxt2002_readreg_multibyte(state
,0xE6,b
,3);
546 static int nxt2002_sleep(struct dvb_frontend
* fe
)
551 static int nxt2002_init(struct dvb_frontend
* fe
)
553 struct nxt2002_state
* state
= fe
->demodulator_priv
;
554 const struct firmware
*fw
;
558 if (!state
->initialised
) {
559 /* request the firmware, this will block until someone uploads it */
560 printk("nxt2002: Waiting for firmware upload (%s)...\n", NXT2002_DEFAULT_FIRMWARE
);
561 ret
= state
->config
->request_firmware(fe
, &fw
, NXT2002_DEFAULT_FIRMWARE
);
562 printk("nxt2002: Waiting for firmware upload(2)...\n");
564 printk("nxt2002: no firmware upload (timeout or file not found?)\n");
568 ret
= nxt2002_load_firmware(fe
, fw
);
570 printk("nxt2002: writing firmware to device failed\n");
571 release_firmware(fw
);
574 printk("nxt2002: firmware upload complete\n");
576 /* Put the micro into reset */
577 nxt2002_microcontroller_stop(state
);
579 /* ensure transfer is complete */
581 i2c_writebytes(state
,0x2B,buf
,1);
583 /* Put the micro into reset for real this time */
584 nxt2002_microcontroller_stop(state
);
586 /* soft reset everything (agc,frontend,eq,fec)*/
588 i2c_writebytes(state
,0x08,buf
,1);
590 i2c_writebytes(state
,0x08,buf
,1);
592 /* write agc sdm configure */
594 i2c_writebytes(state
,0x57,buf
,1);
596 /* write mod output format */
598 i2c_writebytes(state
,0x09,buf
,1);
600 /* write fec mpeg mode */
603 i2c_writebytes(state
,0xE9,buf
,2);
605 /* write mux selection */
607 i2c_writebytes(state
,0xCC,buf
,1);
609 state
->initialised
= 1;
615 static int nxt2002_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fesettings
)
617 fesettings
->min_delay_ms
= 500;
618 fesettings
->step_size
= 0;
619 fesettings
->max_drift
= 0;
623 static void nxt2002_release(struct dvb_frontend
* fe
)
625 struct nxt2002_state
* state
= fe
->demodulator_priv
;
629 static struct dvb_frontend_ops nxt2002_ops
;
631 struct dvb_frontend
* nxt2002_attach(const struct nxt2002_config
* config
,
632 struct i2c_adapter
* i2c
)
634 struct nxt2002_state
* state
= NULL
;
635 u8 buf
[] = {0,0,0,0,0};
637 /* allocate memory for the internal state */
638 state
= kmalloc(sizeof(struct nxt2002_state
), GFP_KERNEL
);
639 if (state
== NULL
) goto error
;
641 /* setup the state */
642 state
->config
= config
;
644 memcpy(&state
->ops
, &nxt2002_ops
, sizeof(struct dvb_frontend_ops
));
645 state
->initialised
= 0;
647 /* Check the first 5 registers to ensure this a revision we can handle */
649 i2c_readbytes(state
, 0x00, buf
, 5);
650 if (buf
[0] != 0x04) goto error
; /* device id */
651 if (buf
[1] != 0x02) goto error
; /* fab id */
652 if (buf
[2] != 0x11) goto error
; /* month */
653 if (buf
[3] != 0x20) goto error
; /* year msb */
654 if (buf
[4] != 0x00) goto error
; /* year lsb */
656 /* create dvb_frontend */
657 state
->frontend
.ops
= &state
->ops
;
658 state
->frontend
.demodulator_priv
= state
;
659 return &state
->frontend
;
666 static struct dvb_frontend_ops nxt2002_ops
= {
669 .name
= "Nextwave nxt2002 VSB/QAM frontend",
671 .frequency_min
= 54000000,
672 .frequency_max
= 860000000,
673 /* stepsize is just a guess */
674 .frequency_stepsize
= 166666,
675 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
676 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
677 FE_CAN_8VSB
| FE_CAN_QAM_64
| FE_CAN_QAM_256
680 .release
= nxt2002_release
,
682 .init
= nxt2002_init
,
683 .sleep
= nxt2002_sleep
,
685 .set_frontend
= nxt2002_setup_frontend_parameters
,
686 .get_tune_settings
= nxt2002_get_tune_settings
,
688 .read_status
= nxt2002_read_status
,
689 .read_ber
= nxt2002_read_ber
,
690 .read_signal_strength
= nxt2002_read_signal_strength
,
691 .read_snr
= nxt2002_read_snr
,
692 .read_ucblocks
= nxt2002_read_ucblocks
,
696 module_param(debug
, int, 0644);
697 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
699 MODULE_DESCRIPTION("NXT2002 ATSC (8VSB & ITU J83 AnnexB FEC QAM64/256) demodulator driver");
700 MODULE_AUTHOR("Taylor Jacob");
701 MODULE_LICENSE("GPL");
703 EXPORT_SYMBOL(nxt2002_attach
);