2 NxtWave Communications - NXT6000 demodulator driver
4 Copyright (C) 2002-2003 Florian Schirmer <jolt@tuxbox.org>
5 Copyright (C) 2003 Paul Andreassen <paul@andreassen.com.au>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
30 #include "dvb_frontend.h"
31 #include "nxt6000_priv.h"
36 struct nxt6000_state
{
37 struct i2c_adapter
* i2c
;
38 /* configuration settings */
39 const struct nxt6000_config
* config
;
40 struct dvb_frontend frontend
;
44 #define dprintk(fmt, arg...) do { \
46 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
50 static int nxt6000_writereg(struct nxt6000_state
* state
, u8 reg
, u8 data
)
52 u8 buf
[] = { reg
, data
};
53 struct i2c_msg msg
= {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= buf
,.len
= 2 };
56 if ((ret
= i2c_transfer(state
->i2c
, &msg
, 1)) != 1)
57 dprintk("nxt6000: nxt6000_write error (reg: 0x%02X, data: 0x%02X, ret: %d)\n", reg
, data
, ret
);
59 return (ret
!= 1) ? -EIO
: 0;
62 static u8
nxt6000_readreg(struct nxt6000_state
* state
, u8 reg
)
67 struct i2c_msg msgs
[] = {
68 {.addr
= state
->config
->demod_address
,.flags
= 0,.buf
= b0
,.len
= 1},
69 {.addr
= state
->config
->demod_address
,.flags
= I2C_M_RD
,.buf
= b1
,.len
= 1}
72 ret
= i2c_transfer(state
->i2c
, msgs
, 2);
75 dprintk("nxt6000: nxt6000_read error (reg: 0x%02X, ret: %d)\n", reg
, ret
);
80 static void nxt6000_reset(struct nxt6000_state
* state
)
84 val
= nxt6000_readreg(state
, OFDM_COR_CTL
);
86 nxt6000_writereg(state
, OFDM_COR_CTL
, val
& ~COREACT
);
87 nxt6000_writereg(state
, OFDM_COR_CTL
, val
| COREACT
);
90 static int nxt6000_set_bandwidth(struct nxt6000_state
*state
, u32 bandwidth
)
97 nominal_rate
= 0x55B7;
101 nominal_rate
= 0x6400;
105 nominal_rate
= 0x7249;
112 if ((result
= nxt6000_writereg(state
, OFDM_TRL_NOMINALRATE_1
, nominal_rate
& 0xFF)) < 0)
115 return nxt6000_writereg(state
, OFDM_TRL_NOMINALRATE_2
, (nominal_rate
>> 8) & 0xFF);
118 static int nxt6000_set_guard_interval(struct nxt6000_state
*state
,
119 enum fe_guard_interval guard_interval
)
121 switch (guard_interval
) {
123 case GUARD_INTERVAL_1_32
:
124 return nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, 0x00 | (nxt6000_readreg(state
, OFDM_COR_MODEGUARD
) & ~0x03));
126 case GUARD_INTERVAL_1_16
:
127 return nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, 0x01 | (nxt6000_readreg(state
, OFDM_COR_MODEGUARD
) & ~0x03));
129 case GUARD_INTERVAL_AUTO
:
130 case GUARD_INTERVAL_1_8
:
131 return nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, 0x02 | (nxt6000_readreg(state
, OFDM_COR_MODEGUARD
) & ~0x03));
133 case GUARD_INTERVAL_1_4
:
134 return nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, 0x03 | (nxt6000_readreg(state
, OFDM_COR_MODEGUARD
) & ~0x03));
141 static int nxt6000_set_inversion(struct nxt6000_state
*state
,
142 enum fe_spectral_inversion inversion
)
147 return nxt6000_writereg(state
, OFDM_ITB_CTL
, 0x00);
150 return nxt6000_writereg(state
, OFDM_ITB_CTL
, ITBINV
);
159 nxt6000_set_transmission_mode(struct nxt6000_state
*state
,
160 enum fe_transmit_mode transmission_mode
)
164 switch (transmission_mode
) {
166 case TRANSMISSION_MODE_2K
:
167 if ((result
= nxt6000_writereg(state
, EN_DMD_RACQ
, 0x00 | (nxt6000_readreg(state
, EN_DMD_RACQ
) & ~0x03))) < 0)
170 return nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, (0x00 << 2) | (nxt6000_readreg(state
, OFDM_COR_MODEGUARD
) & ~0x04));
172 case TRANSMISSION_MODE_8K
:
173 case TRANSMISSION_MODE_AUTO
:
174 if ((result
= nxt6000_writereg(state
, EN_DMD_RACQ
, 0x02 | (nxt6000_readreg(state
, EN_DMD_RACQ
) & ~0x03))) < 0)
177 return nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, (0x01 << 2) | (nxt6000_readreg(state
, OFDM_COR_MODEGUARD
) & ~0x04));
185 static void nxt6000_setup(struct dvb_frontend
* fe
)
187 struct nxt6000_state
* state
= fe
->demodulator_priv
;
189 nxt6000_writereg(state
, RS_COR_SYNC_PARAM
, SYNC_PARAM
);
190 nxt6000_writereg(state
, BER_CTRL
, /*(1 << 2) | */ (0x01 << 1) | 0x01);
191 nxt6000_writereg(state
, VIT_BERTIME_2
, 0x00); // BER Timer = 0x000200 * 256 = 131072 bits
192 nxt6000_writereg(state
, VIT_BERTIME_1
, 0x02); //
193 nxt6000_writereg(state
, VIT_BERTIME_0
, 0x00); //
194 nxt6000_writereg(state
, VIT_COR_INTEN
, 0x98); // Enable BER interrupts
195 nxt6000_writereg(state
, VIT_COR_CTL
, 0x82); // Enable BER measurement
196 nxt6000_writereg(state
, VIT_COR_CTL
, VIT_COR_RESYNC
| 0x02 );
197 nxt6000_writereg(state
, OFDM_COR_CTL
, (0x01 << 5) | (nxt6000_readreg(state
, OFDM_COR_CTL
) & 0x0F));
198 nxt6000_writereg(state
, OFDM_COR_MODEGUARD
, FORCEMODE8K
| 0x02);
199 nxt6000_writereg(state
, OFDM_AGC_CTL
, AGCLAST
| INITIAL_AGC_BW
);
200 nxt6000_writereg(state
, OFDM_ITB_FREQ_1
, 0x06);
201 nxt6000_writereg(state
, OFDM_ITB_FREQ_2
, 0x31);
202 nxt6000_writereg(state
, OFDM_CAS_CTL
, (0x01 << 7) | (0x02 << 3) | 0x04);
203 nxt6000_writereg(state
, CAS_FREQ
, 0xBB); /* CHECKME */
204 nxt6000_writereg(state
, OFDM_SYR_CTL
, 1 << 2);
205 nxt6000_writereg(state
, OFDM_PPM_CTL_1
, PPM256
);
206 nxt6000_writereg(state
, OFDM_TRL_NOMINALRATE_1
, 0x49);
207 nxt6000_writereg(state
, OFDM_TRL_NOMINALRATE_2
, 0x72);
208 nxt6000_writereg(state
, ANALOG_CONTROL_0
, 1 << 5);
209 nxt6000_writereg(state
, EN_DMD_RACQ
, (1 << 7) | (3 << 4) | 2);
210 nxt6000_writereg(state
, DIAG_CONFIG
, TB_SET
);
212 if (state
->config
->clock_inversion
)
213 nxt6000_writereg(state
, SUB_DIAG_MODE_SEL
, CLKINVERSION
);
215 nxt6000_writereg(state
, SUB_DIAG_MODE_SEL
, 0);
217 nxt6000_writereg(state
, TS_FORMAT
, 0);
220 static void nxt6000_dump_status(struct nxt6000_state
*state
)
225 pr_info("RS_COR_STAT: 0x%02X\n",
226 nxt6000_readreg(fe
, RS_COR_STAT
));
227 pr_info("VIT_SYNC_STATUS: 0x%02X\n",
228 nxt6000_readreg(fe
, VIT_SYNC_STATUS
));
229 pr_info("OFDM_COR_STAT: 0x%02X\n",
230 nxt6000_readreg(fe
, OFDM_COR_STAT
));
231 pr_info("OFDM_SYR_STAT: 0x%02X\n",
232 nxt6000_readreg(fe
, OFDM_SYR_STAT
));
233 pr_info("OFDM_TPS_RCVD_1: 0x%02X\n",
234 nxt6000_readreg(fe
, OFDM_TPS_RCVD_1
));
235 pr_info("OFDM_TPS_RCVD_2: 0x%02X\n",
236 nxt6000_readreg(fe
, OFDM_TPS_RCVD_2
));
237 pr_info("OFDM_TPS_RCVD_3: 0x%02X\n",
238 nxt6000_readreg(fe
, OFDM_TPS_RCVD_3
));
239 pr_info("OFDM_TPS_RCVD_4: 0x%02X\n",
240 nxt6000_readreg(fe
, OFDM_TPS_RCVD_4
));
241 pr_info("OFDM_TPS_RESERVED_1: 0x%02X\n",
242 nxt6000_readreg(fe
, OFDM_TPS_RESERVED_1
));
243 pr_info("OFDM_TPS_RESERVED_2: 0x%02X\n",
244 nxt6000_readreg(fe
, OFDM_TPS_RESERVED_2
));
246 pr_info("NXT6000 status:");
248 val
= nxt6000_readreg(state
, RS_COR_STAT
);
250 pr_cont(" DATA DESCR LOCK: %d,", val
& 0x01);
251 pr_cont(" DATA SYNC LOCK: %d,", (val
>> 1) & 0x01);
253 val
= nxt6000_readreg(state
, VIT_SYNC_STATUS
);
255 pr_cont(" VITERBI LOCK: %d,", (val
>> 7) & 0x01);
257 switch ((val
>> 4) & 0x07) {
260 pr_cont(" VITERBI CODERATE: 1/2,");
264 pr_cont(" VITERBI CODERATE: 2/3,");
268 pr_cont(" VITERBI CODERATE: 3/4,");
272 pr_cont(" VITERBI CODERATE: 5/6,");
276 pr_cont(" VITERBI CODERATE: 7/8,");
280 pr_cont(" VITERBI CODERATE: Reserved,");
284 val
= nxt6000_readreg(state
, OFDM_COR_STAT
);
286 pr_cont(" CHCTrack: %d,", (val
>> 7) & 0x01);
287 pr_cont(" TPSLock: %d,", (val
>> 6) & 0x01);
288 pr_cont(" SYRLock: %d,", (val
>> 5) & 0x01);
289 pr_cont(" AGCLock: %d,", (val
>> 4) & 0x01);
291 switch (val
& 0x0F) {
294 pr_cont(" CoreState: IDLE,");
298 pr_cont(" CoreState: WAIT_AGC,");
302 pr_cont(" CoreState: WAIT_SYR,");
306 pr_cont(" CoreState: WAIT_PPM,");
310 pr_cont(" CoreState: WAIT_TRL,");
314 pr_cont(" CoreState: WAIT_TPS,");
318 pr_cont(" CoreState: MONITOR_TPS,");
322 pr_cont(" CoreState: Reserved,");
326 val
= nxt6000_readreg(state
, OFDM_SYR_STAT
);
328 pr_cont(" SYRLock: %d,", (val
>> 4) & 0x01);
329 pr_cont(" SYRMode: %s,", (val
>> 2) & 0x01 ? "8K" : "2K");
331 switch ((val
>> 4) & 0x03) {
334 pr_cont(" SYRGuard: 1/32,");
338 pr_cont(" SYRGuard: 1/16,");
342 pr_cont(" SYRGuard: 1/8,");
346 pr_cont(" SYRGuard: 1/4,");
350 val
= nxt6000_readreg(state
, OFDM_TPS_RCVD_3
);
352 switch ((val
>> 4) & 0x07) {
355 pr_cont(" TPSLP: 1/2,");
359 pr_cont(" TPSLP: 2/3,");
363 pr_cont(" TPSLP: 3/4,");
367 pr_cont(" TPSLP: 5/6,");
371 pr_cont(" TPSLP: 7/8,");
375 pr_cont(" TPSLP: Reserved,");
379 switch (val
& 0x07) {
382 pr_cont(" TPSHP: 1/2,");
386 pr_cont(" TPSHP: 2/3,");
390 pr_cont(" TPSHP: 3/4,");
394 pr_cont(" TPSHP: 5/6,");
398 pr_cont(" TPSHP: 7/8,");
402 pr_cont(" TPSHP: Reserved,");
406 val
= nxt6000_readreg(state
, OFDM_TPS_RCVD_4
);
408 pr_cont(" TPSMode: %s,", val
& 0x01 ? "8K" : "2K");
410 switch ((val
>> 4) & 0x03) {
413 pr_cont(" TPSGuard: 1/32,");
417 pr_cont(" TPSGuard: 1/16,");
421 pr_cont(" TPSGuard: 1/8,");
425 pr_cont(" TPSGuard: 1/4,");
430 /* Strange magic required to gain access to RF_AGC_STATUS */
431 nxt6000_readreg(state
, RF_AGC_VAL_1
);
432 val
= nxt6000_readreg(state
, RF_AGC_STATUS
);
433 val
= nxt6000_readreg(state
, RF_AGC_STATUS
);
435 pr_cont(" RF AGC LOCK: %d,", (val
>> 4) & 0x01);
439 static int nxt6000_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
442 struct nxt6000_state
* state
= fe
->demodulator_priv
;
446 core_status
= nxt6000_readreg(state
, OFDM_COR_STAT
);
448 if (core_status
& AGCLOCKED
)
449 *status
|= FE_HAS_SIGNAL
;
451 if (nxt6000_readreg(state
, OFDM_SYR_STAT
) & GI14_SYR_LOCK
)
452 *status
|= FE_HAS_CARRIER
;
454 if (nxt6000_readreg(state
, VIT_SYNC_STATUS
) & VITINSYNC
)
455 *status
|= FE_HAS_VITERBI
;
457 if (nxt6000_readreg(state
, RS_COR_STAT
) & RSCORESTATUS
)
458 *status
|= FE_HAS_SYNC
;
460 if ((core_status
& TPSLOCKED
) && (*status
== (FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_SYNC
)))
461 *status
|= FE_HAS_LOCK
;
464 nxt6000_dump_status(state
);
469 static int nxt6000_init(struct dvb_frontend
* fe
)
471 struct nxt6000_state
* state
= fe
->demodulator_priv
;
473 nxt6000_reset(state
);
479 static int nxt6000_set_frontend(struct dvb_frontend
*fe
)
481 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
482 struct nxt6000_state
* state
= fe
->demodulator_priv
;
485 if (fe
->ops
.tuner_ops
.set_params
) {
486 fe
->ops
.tuner_ops
.set_params(fe
);
487 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
490 result
= nxt6000_set_bandwidth(state
, p
->bandwidth_hz
);
494 result
= nxt6000_set_guard_interval(state
, p
->guard_interval
);
498 result
= nxt6000_set_transmission_mode(state
, p
->transmission_mode
);
502 result
= nxt6000_set_inversion(state
, p
->inversion
);
510 static void nxt6000_release(struct dvb_frontend
* fe
)
512 struct nxt6000_state
* state
= fe
->demodulator_priv
;
516 static int nxt6000_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
518 struct nxt6000_state
* state
= fe
->demodulator_priv
;
520 *snr
= nxt6000_readreg( state
, OFDM_CHC_SNR
) / 8;
525 static int nxt6000_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
527 struct nxt6000_state
* state
= fe
->demodulator_priv
;
529 nxt6000_writereg( state
, VIT_COR_INTSTAT
, 0x18 );
531 *ber
= (nxt6000_readreg( state
, VIT_BER_1
) << 8 ) |
532 nxt6000_readreg( state
, VIT_BER_0
);
534 nxt6000_writereg( state
, VIT_COR_INTSTAT
, 0x18); // Clear BER Done interrupts
539 static int nxt6000_read_signal_strength(struct dvb_frontend
* fe
, u16
* signal_strength
)
541 struct nxt6000_state
* state
= fe
->demodulator_priv
;
543 *signal_strength
= (short) (511 -
544 (nxt6000_readreg(state
, AGC_GAIN_1
) +
545 ((nxt6000_readreg(state
, AGC_GAIN_2
) & 0x03) << 8)));
550 static int nxt6000_fe_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
*tune
)
552 tune
->min_delay_ms
= 500;
556 static int nxt6000_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
558 struct nxt6000_state
* state
= fe
->demodulator_priv
;
561 return nxt6000_writereg(state
, ENABLE_TUNER_IIC
, 0x01);
563 return nxt6000_writereg(state
, ENABLE_TUNER_IIC
, 0x00);
567 static const struct dvb_frontend_ops nxt6000_ops
;
569 struct dvb_frontend
* nxt6000_attach(const struct nxt6000_config
* config
,
570 struct i2c_adapter
* i2c
)
572 struct nxt6000_state
* state
= NULL
;
574 /* allocate memory for the internal state */
575 state
= kzalloc(sizeof(struct nxt6000_state
), GFP_KERNEL
);
576 if (state
== NULL
) goto error
;
578 /* setup the state */
579 state
->config
= config
;
582 /* check if the demod is there */
583 if (nxt6000_readreg(state
, OFDM_MSC_REV
) != NXT6000ASICDEVICE
) goto error
;
585 /* create dvb_frontend */
586 memcpy(&state
->frontend
.ops
, &nxt6000_ops
, sizeof(struct dvb_frontend_ops
));
587 state
->frontend
.demodulator_priv
= state
;
588 return &state
->frontend
;
595 static const struct dvb_frontend_ops nxt6000_ops
= {
596 .delsys
= { SYS_DVBT
},
598 .name
= "NxtWave NXT6000 DVB-T",
600 .frequency_max
= 863250000,
601 .frequency_stepsize
= 62500,
602 /*.frequency_tolerance = *//* FIXME: 12% of SR */
603 .symbol_rate_min
= 0, /* FIXME */
604 .symbol_rate_max
= 9360000, /* FIXME */
605 .symbol_rate_tolerance
= 4000,
606 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
607 FE_CAN_FEC_4_5
| FE_CAN_FEC_5_6
| FE_CAN_FEC_6_7
|
608 FE_CAN_FEC_7_8
| FE_CAN_FEC_8_9
| FE_CAN_FEC_AUTO
|
609 FE_CAN_QAM_16
| FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
610 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_GUARD_INTERVAL_AUTO
|
611 FE_CAN_HIERARCHY_AUTO
,
614 .release
= nxt6000_release
,
616 .init
= nxt6000_init
,
617 .i2c_gate_ctrl
= nxt6000_i2c_gate_ctrl
,
619 .get_tune_settings
= nxt6000_fe_get_tune_settings
,
621 .set_frontend
= nxt6000_set_frontend
,
623 .read_status
= nxt6000_read_status
,
624 .read_ber
= nxt6000_read_ber
,
625 .read_signal_strength
= nxt6000_read_signal_strength
,
626 .read_snr
= nxt6000_read_snr
,
629 module_param(debug
, int, 0644);
630 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
632 MODULE_DESCRIPTION("NxtWave NXT6000 DVB-T demodulator driver");
633 MODULE_AUTHOR("Florian Schirmer");
634 MODULE_LICENSE("GPL");
636 EXPORT_SYMBOL(nxt6000_attach
);