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
26 * or /lib/firmware (depending on configuration of firmware hotplug).
28 #define NXT2002_DEFAULT_FIRMWARE "dvb-fe-nxt2002.fw"
29 #define CRC_CCIT_MASK 0x1021
31 #include <linux/init.h>
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/device.h>
35 #include <linux/firmware.h>
36 #include <linux/string.h>
37 #include <linux/slab.h>
39 #include "dvb_frontend.h"
42 struct nxt2002_state
{
44 struct i2c_adapter
* i2c
;
45 struct dvb_frontend_ops ops
;
46 const struct nxt2002_config
* config
;
47 struct dvb_frontend frontend
;
49 /* demodulator private data */
54 #define dprintk(args...) \
56 if (debug) printk(KERN_DEBUG "nxt2002: " args); \
59 static int i2c_writebytes (struct nxt2002_state
* state
, u8 reg
, u8
*buf
, u8 len
)
61 /* probbably a much better way or doing this */
64 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= buf2
, .len
= len
+ 1 };
67 for (x
= 0 ; x
< len
; x
++)
70 if ((err
= i2c_transfer (state
->i2c
, &msg
, 1)) != 1) {
71 printk ("%s: i2c write error (addr %02x, err == %i)\n",
72 __FUNCTION__
, state
->config
->demod_address
, err
);
79 static u8
i2c_readbytes (struct nxt2002_state
* state
, u8 reg
, u8
* buf
, u8 len
)
83 struct i2c_msg msg
[] = { { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= reg2
, .len
= 1 },
84 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= buf
, .len
= len
} };
88 if ((err
= i2c_transfer (state
->i2c
, msg
, 2)) != 2) {
89 printk ("%s: i2c read error (addr %02x, err == %i)\n",
90 __FUNCTION__
, state
->config
->demod_address
, err
);
97 static u16
nxt2002_crc(u16 crc
, u8 c
)
101 u16 input
= (u16
) c
& 0xFF;
105 if((crc
^ input
) & 0x8000)
106 crc
=(crc
<<1)^CRC_CCIT_MASK
;
114 static int nxt2002_writereg_multibyte (struct nxt2002_state
* state
, u8 reg
, u8
* data
, u8 len
)
117 dprintk("%s\n", __FUNCTION__
);
119 /* set multi register length */
120 i2c_writebytes(state
,0x34,&len
,1);
122 /* set mutli register register */
123 i2c_writebytes(state
,0x35,®
,1);
125 /* send the actual data */
126 i2c_writebytes(state
,0x36,data
,len
);
128 /* toggle the multireg write bit*/
130 i2c_writebytes(state
,0x21,&buf
,1);
132 i2c_readbytes(state
,0x21,&buf
,1);
134 if ((buf
& 0x02) == 0)
137 dprintk("Error writing multireg register %02X\n",reg
);
142 static int nxt2002_readreg_multibyte (struct nxt2002_state
* state
, u8 reg
, u8
* data
, u8 len
)
145 dprintk("%s\n", __FUNCTION__
);
147 /* set multi register length */
149 i2c_writebytes(state
,0x34,&len2
,1);
151 /* set mutli register register */
152 i2c_writebytes(state
,0x35,®
,1);
154 /* send the actual data */
155 i2c_readbytes(state
,reg
,data
,len
);
160 static void nxt2002_microcontroller_stop (struct nxt2002_state
* state
)
162 u8 buf
[2],counter
= 0;
163 dprintk("%s\n", __FUNCTION__
);
166 i2c_writebytes(state
,0x22,buf
,1);
168 while (counter
< 20) {
169 i2c_readbytes(state
,0x31,buf
,1);
176 dprintk("Timeout waiting for micro to stop.. This is ok after firmware upload\n");
180 static void nxt2002_microcontroller_start (struct nxt2002_state
* state
)
183 dprintk("%s\n", __FUNCTION__
);
186 i2c_writebytes(state
,0x22,&buf
,1);
189 static int nxt2002_writetuner (struct nxt2002_state
* state
, u8
* data
)
193 dprintk("Tuner Bytes: %02X %02X %02X %02X\n",data
[0],data
[1],data
[2],data
[3]);
195 dprintk("%s\n", __FUNCTION__
);
196 /* stop the micro first */
197 nxt2002_microcontroller_stop(state
);
199 /* set the i2c transfer speed to the tuner */
201 i2c_writebytes(state
,0x20,&buf
,1);
203 /* setup to transfer 4 bytes via i2c */
205 i2c_writebytes(state
,0x34,&buf
,1);
207 /* write actual tuner bytes */
208 i2c_writebytes(state
,0x36,data
,4);
210 /* set tuner i2c address */
212 i2c_writebytes(state
,0x35,&buf
,1);
214 /* write UC Opmode to begin transfer */
216 i2c_writebytes(state
,0x21,&buf
,1);
219 i2c_readbytes(state
,0x21,&buf
,1);
220 if ((buf
& 0x80)== 0x00)
226 printk("nxt2002: timeout error writing tuner\n");
230 static void nxt2002_agc_reset(struct nxt2002_state
* state
)
233 dprintk("%s\n", __FUNCTION__
);
236 i2c_writebytes(state
,0x08,&buf
,1);
239 i2c_writebytes(state
,0x08,&buf
,1);
244 static int nxt2002_load_firmware (struct dvb_frontend
* fe
, const struct firmware
*fw
)
247 struct nxt2002_state
* state
= fe
->demodulator_priv
;
248 u8 buf
[256],written
= 0,chunkpos
= 0;
249 u16 rambase
,position
,crc
= 0;
251 dprintk("%s\n", __FUNCTION__
);
252 dprintk("Firmware is %zu bytes\n",fw
->size
);
254 /* Get the RAM base for this nxt2002 */
255 i2c_readbytes(state
,0x10,buf
,1);
262 dprintk("rambase on this nxt2002 is %04X\n",rambase
);
264 /* Hold the micro in reset while loading firmware */
266 i2c_writebytes(state
,0x2B,buf
,1);
268 for (position
= 0; position
< fw
->size
; position
++) {
272 buf
[0] = ((rambase
+ position
) >> 8);
273 buf
[1] = (rambase
+ position
) & 0xFF;
275 /* write starting address */
276 i2c_writebytes(state
,0x29,buf
,3);
281 if ((written
% 4) == 0)
282 i2c_writebytes(state
,chunkpos
,&fw
->data
[position
-3],4);
284 crc
= nxt2002_crc(crc
,fw
->data
[position
]);
286 if ((written
== 255) || (position
+1 == fw
->size
)) {
287 /* write remaining bytes of firmware */
288 i2c_writebytes(state
, chunkpos
+4-(written
%4),
289 &fw
->data
[position
-(written
%4) + 1],
295 i2c_writebytes(state
,0x2C,buf
,2);
297 /* do a read to stop things */
298 i2c_readbytes(state
,0x2A,buf
,1);
300 /* set transfer mode to complete */
302 i2c_writebytes(state
,0x2B,buf
,1);
312 static int nxt2002_setup_frontend_parameters (struct dvb_frontend
* fe
,
313 struct dvb_frontend_parameters
*p
)
315 struct nxt2002_state
* state
= fe
->demodulator_priv
;
320 freq
= 44000 + ( p
->frequency
/ 1000 );
322 dprintk("freq = %d p->frequency = %d\n",freq
,p
->frequency
);
324 tunerfreq
= freq
* 24/4000;
326 buf
[0] = (tunerfreq
>> 8) & 0x7F;
327 buf
[1] = (tunerfreq
& 0xFF);
329 if (p
->frequency
<= 214000000) {
330 buf
[2] = 0x84 + (0x06 << 3);
331 buf
[3] = (p
->frequency
<= 172000000) ? 0x01 : 0x02;
332 } else if (p
->frequency
<= 721000000) {
333 buf
[2] = 0x84 + (0x07 << 3);
334 buf
[3] = (p
->frequency
<= 467000000) ? 0x02 : 0x08;
335 } else if (p
->frequency
<= 841000000) {
336 buf
[2] = 0x84 + (0x0E << 3);
339 buf
[2] = 0x84 + (0x0F << 3);
343 /* write frequency information */
344 nxt2002_writetuner(state
,buf
);
346 /* reset the agc now that tuning has been completed */
347 nxt2002_agc_reset(state
);
349 /* set target power level */
350 switch (p
->u
.vsb
.modulation
) {
362 i2c_writebytes(state
,0x42,buf
,1);
366 i2c_writebytes(state
,0x57,buf
,1);
368 /* write sdm1 input */
371 nxt2002_writereg_multibyte(state
,0x58,buf
,2);
373 /* write sdmx input */
374 switch (p
->u
.vsb
.modulation
) {
389 nxt2002_writereg_multibyte(state
,0x5C,buf
,2);
391 /* write adc power lpf fc */
393 i2c_writebytes(state
,0x43,buf
,1);
395 /* write adc power lpf fc */
397 i2c_writebytes(state
,0x43,buf
,1);
399 /* write accumulator2 input */
402 nxt2002_writereg_multibyte(state
,0x4B,buf
,2);
406 i2c_writebytes(state
,0x4D,buf
,1);
408 /* write sdm12 lpf fc */
410 i2c_writebytes(state
,0x55,buf
,1);
412 /* write agc control reg */
414 i2c_writebytes(state
,0x41,buf
,1);
416 /* write agc ucgp0 */
417 switch (p
->u
.vsb
.modulation
) {
431 i2c_writebytes(state
,0x30,buf
,1);
433 /* write agc control reg */
435 i2c_writebytes(state
,0x41,buf
,1);
437 /* write accumulator2 input */
440 nxt2002_writereg_multibyte(state
,0x49,buf
,2);
441 nxt2002_writereg_multibyte(state
,0x4B,buf
,2);
443 /* write agc control reg */
445 i2c_writebytes(state
,0x41,buf
,1);
447 nxt2002_microcontroller_start(state
);
449 /* adjacent channel detection should be done here, but I don't
450 have any stations with this need so I cannot test it */
455 static int nxt2002_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
457 struct nxt2002_state
* state
= fe
->demodulator_priv
;
459 i2c_readbytes(state
,0x31,&lock
,1);
463 *status
|= FE_HAS_SIGNAL
;
464 *status
|= FE_HAS_CARRIER
;
465 *status
|= FE_HAS_VITERBI
;
466 *status
|= FE_HAS_SYNC
;
467 *status
|= FE_HAS_LOCK
;
472 static int nxt2002_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
474 struct nxt2002_state
* state
= fe
->demodulator_priv
;
477 nxt2002_readreg_multibyte(state
,0xE6,b
,3);
479 *ber
= ((b
[0] << 8) + b
[1]) * 8;
484 static int nxt2002_read_signal_strength(struct dvb_frontend
* fe
, u16
* strength
)
486 struct nxt2002_state
* state
= fe
->demodulator_priv
;
490 /* setup to read cluster variance */
492 i2c_writebytes(state
,0xA1,b
,1);
494 /* get multreg val */
495 nxt2002_readreg_multibyte(state
,0xA6,b
,2);
497 temp
= (b
[0] << 8) | b
[1];
498 *strength
= ((0x7FFF - temp
) & 0x0FFF) * 16;
503 static int nxt2002_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
506 struct nxt2002_state
* state
= fe
->demodulator_priv
;
511 /* setup to read cluster variance */
513 i2c_writebytes(state
,0xA1,b
,1);
515 /* get multreg val from 0xA6 */
516 nxt2002_readreg_multibyte(state
,0xA6,b
,2);
518 temp
= (b
[0] << 8) | b
[1];
519 temp2
= 0x7FFF - temp
;
521 /* snr will be in db */
523 snrdb
= 1000*24 + ( 1000*(30-24) * ( temp2
- 0x7F00 ) / ( 0x7FFF - 0x7F00 ) );
524 else if (temp2
> 0x7EC0)
525 snrdb
= 1000*18 + ( 1000*(24-18) * ( temp2
- 0x7EC0 ) / ( 0x7F00 - 0x7EC0 ) );
526 else if (temp2
> 0x7C00)
527 snrdb
= 1000*12 + ( 1000*(18-12) * ( temp2
- 0x7C00 ) / ( 0x7EC0 - 0x7C00 ) );
529 snrdb
= 1000*0 + ( 1000*(12-0) * ( temp2
- 0 ) / ( 0x7C00 - 0 ) );
531 /* the value reported back from the frontend will be FFFF=32db 0000=0db */
533 *snr
= snrdb
* (0xFFFF/32000);
538 static int nxt2002_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
540 struct nxt2002_state
* state
= fe
->demodulator_priv
;
543 nxt2002_readreg_multibyte(state
,0xE6,b
,3);
549 static int nxt2002_sleep(struct dvb_frontend
* fe
)
554 static int nxt2002_init(struct dvb_frontend
* fe
)
556 struct nxt2002_state
* state
= fe
->demodulator_priv
;
557 const struct firmware
*fw
;
561 if (!state
->initialised
) {
562 /* request the firmware, this will block until someone uploads it */
563 printk("nxt2002: Waiting for firmware upload (%s)...\n", NXT2002_DEFAULT_FIRMWARE
);
564 ret
= state
->config
->request_firmware(fe
, &fw
, NXT2002_DEFAULT_FIRMWARE
);
565 printk("nxt2002: Waiting for firmware upload(2)...\n");
567 printk("nxt2002: no firmware upload (timeout or file not found?)\n");
571 ret
= nxt2002_load_firmware(fe
, fw
);
573 printk("nxt2002: writing firmware to device failed\n");
574 release_firmware(fw
);
577 printk("nxt2002: firmware upload complete\n");
579 /* Put the micro into reset */
580 nxt2002_microcontroller_stop(state
);
582 /* ensure transfer is complete */
584 i2c_writebytes(state
,0x2B,buf
,1);
586 /* Put the micro into reset for real this time */
587 nxt2002_microcontroller_stop(state
);
589 /* soft reset everything (agc,frontend,eq,fec)*/
591 i2c_writebytes(state
,0x08,buf
,1);
593 i2c_writebytes(state
,0x08,buf
,1);
595 /* write agc sdm configure */
597 i2c_writebytes(state
,0x57,buf
,1);
599 /* write mod output format */
601 i2c_writebytes(state
,0x09,buf
,1);
603 /* write fec mpeg mode */
606 i2c_writebytes(state
,0xE9,buf
,2);
608 /* write mux selection */
610 i2c_writebytes(state
,0xCC,buf
,1);
612 state
->initialised
= 1;
618 static int nxt2002_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
* fesettings
)
620 fesettings
->min_delay_ms
= 500;
621 fesettings
->step_size
= 0;
622 fesettings
->max_drift
= 0;
626 static void nxt2002_release(struct dvb_frontend
* fe
)
628 struct nxt2002_state
* state
= fe
->demodulator_priv
;
632 static struct dvb_frontend_ops nxt2002_ops
;
634 struct dvb_frontend
* nxt2002_attach(const struct nxt2002_config
* config
,
635 struct i2c_adapter
* i2c
)
637 struct nxt2002_state
* state
= NULL
;
638 u8 buf
[] = {0,0,0,0,0};
640 /* allocate memory for the internal state */
641 state
= kmalloc(sizeof(struct nxt2002_state
), GFP_KERNEL
);
642 if (state
== NULL
) goto error
;
644 /* setup the state */
645 state
->config
= config
;
647 memcpy(&state
->ops
, &nxt2002_ops
, sizeof(struct dvb_frontend_ops
));
648 state
->initialised
= 0;
650 /* Check the first 5 registers to ensure this a revision we can handle */
652 i2c_readbytes(state
, 0x00, buf
, 5);
653 if (buf
[0] != 0x04) goto error
; /* device id */
654 if (buf
[1] != 0x02) goto error
; /* fab id */
655 if (buf
[2] != 0x11) goto error
; /* month */
656 if (buf
[3] != 0x20) goto error
; /* year msb */
657 if (buf
[4] != 0x00) goto error
; /* year lsb */
659 /* create dvb_frontend */
660 state
->frontend
.ops
= &state
->ops
;
661 state
->frontend
.demodulator_priv
= state
;
662 return &state
->frontend
;
669 static struct dvb_frontend_ops nxt2002_ops
= {
672 .name
= "Nextwave nxt2002 VSB/QAM frontend",
674 .frequency_min
= 54000000,
675 .frequency_max
= 860000000,
676 /* stepsize is just a guess */
677 .frequency_stepsize
= 166666,
678 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
679 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
680 FE_CAN_8VSB
| FE_CAN_QAM_64
| FE_CAN_QAM_256
683 .release
= nxt2002_release
,
685 .init
= nxt2002_init
,
686 .sleep
= nxt2002_sleep
,
688 .set_frontend
= nxt2002_setup_frontend_parameters
,
689 .get_tune_settings
= nxt2002_get_tune_settings
,
691 .read_status
= nxt2002_read_status
,
692 .read_ber
= nxt2002_read_ber
,
693 .read_signal_strength
= nxt2002_read_signal_strength
,
694 .read_snr
= nxt2002_read_snr
,
695 .read_ucblocks
= nxt2002_read_ucblocks
,
699 module_param(debug
, int, 0644);
700 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
702 MODULE_DESCRIPTION("NXT2002 ATSC (8VSB & ITU J83 AnnexB FEC QAM64/256) demodulator driver");
703 MODULE_AUTHOR("Taylor Jacob");
704 MODULE_LICENSE("GPL");
706 EXPORT_SYMBOL(nxt2002_attach
);