2 Conexant 22702 DVB OFDM demodulator driver
5 Alps TDMB7 DVB OFDM demodulator driver
7 Copyright (C) 2001-2002 Convergence Integrated Media GmbH
8 Holger Waechtler <holger@convergence.de>
10 Copyright (C) 2004 Steven Toth <stoth@linuxtv.org>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/delay.h>
34 #include "dvb_frontend.h"
37 struct cx22702_state
{
39 struct i2c_adapter
*i2c
;
41 /* configuration settings */
42 const struct cx22702_config
*config
;
44 struct dvb_frontend frontend
;
46 /* previous uncorrected block counter */
51 module_param(debug
, int, 0644);
52 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
54 #define dprintk if (debug) printk
56 /* Register values to initialise the demod */
57 static u8 init_tab
[] = {
58 0x00, 0x00, /* Stop aquisition */
85 static int cx22702_writereg(struct cx22702_state
*state
, u8 reg
, u8 data
)
88 u8 buf
[] = { reg
, data
};
89 struct i2c_msg msg
= {
90 .addr
= state
->config
->demod_address
, .flags
= 0,
91 .buf
= buf
, .len
= 2 };
93 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
97 "%s: error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
98 __func__
, reg
, data
, ret
);
100 return (ret
!= 1) ? -1 : 0;
103 static u8
cx22702_readreg(struct cx22702_state
*state
, u8 reg
)
109 struct i2c_msg msg
[] = {
110 { .addr
= state
->config
->demod_address
, .flags
= 0,
111 .buf
= b0
, .len
= 1 },
112 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
,
113 .buf
= b1
, .len
= 1 } };
115 ret
= i2c_transfer(state
->i2c
, msg
, 2);
118 printk(KERN_ERR
"%s: readreg error (ret == %i)\n",
124 static int cx22702_set_inversion(struct cx22702_state
*state
, int inversion
)
132 val
= cx22702_readreg(state
, 0x0C);
133 return cx22702_writereg(state
, 0x0C, val
| 0x01);
135 val
= cx22702_readreg(state
, 0x0C);
136 return cx22702_writereg(state
, 0x0C, val
& 0xfe);
143 /* Retrieve the demod settings */
144 static int cx22702_get_tps(struct cx22702_state
*state
,
145 struct dvb_ofdm_parameters
*p
)
149 /* Make sure the TPS regs are valid */
150 if (!(cx22702_readreg(state
, 0x0A) & 0x20))
153 val
= cx22702_readreg(state
, 0x01);
154 switch ((val
& 0x18) >> 3) {
156 p
->constellation
= QPSK
;
159 p
->constellation
= QAM_16
;
162 p
->constellation
= QAM_64
;
165 switch (val
& 0x07) {
167 p
->hierarchy_information
= HIERARCHY_NONE
;
170 p
->hierarchy_information
= HIERARCHY_1
;
173 p
->hierarchy_information
= HIERARCHY_2
;
176 p
->hierarchy_information
= HIERARCHY_4
;
181 val
= cx22702_readreg(state
, 0x02);
182 switch ((val
& 0x38) >> 3) {
184 p
->code_rate_HP
= FEC_1_2
;
187 p
->code_rate_HP
= FEC_2_3
;
190 p
->code_rate_HP
= FEC_3_4
;
193 p
->code_rate_HP
= FEC_5_6
;
196 p
->code_rate_HP
= FEC_7_8
;
199 switch (val
& 0x07) {
201 p
->code_rate_LP
= FEC_1_2
;
204 p
->code_rate_LP
= FEC_2_3
;
207 p
->code_rate_LP
= FEC_3_4
;
210 p
->code_rate_LP
= FEC_5_6
;
213 p
->code_rate_LP
= FEC_7_8
;
217 val
= cx22702_readreg(state
, 0x03);
218 switch ((val
& 0x0c) >> 2) {
220 p
->guard_interval
= GUARD_INTERVAL_1_32
;
223 p
->guard_interval
= GUARD_INTERVAL_1_16
;
226 p
->guard_interval
= GUARD_INTERVAL_1_8
;
229 p
->guard_interval
= GUARD_INTERVAL_1_4
;
232 switch (val
& 0x03) {
234 p
->transmission_mode
= TRANSMISSION_MODE_2K
;
237 p
->transmission_mode
= TRANSMISSION_MODE_8K
;
244 static int cx22702_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
246 struct cx22702_state
*state
= fe
->demodulator_priv
;
247 dprintk("%s(%d)\n", __func__
, enable
);
249 return cx22702_writereg(state
, 0x0D,
250 cx22702_readreg(state
, 0x0D) & 0xfe);
252 return cx22702_writereg(state
, 0x0D,
253 cx22702_readreg(state
, 0x0D) | 1);
256 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
257 static int cx22702_set_tps(struct dvb_frontend
*fe
,
258 struct dvb_frontend_parameters
*p
)
261 struct cx22702_state
*state
= fe
->demodulator_priv
;
263 if (fe
->ops
.tuner_ops
.set_params
) {
264 fe
->ops
.tuner_ops
.set_params(fe
, p
);
265 if (fe
->ops
.i2c_gate_ctrl
)
266 fe
->ops
.i2c_gate_ctrl(fe
, 0);
270 cx22702_set_inversion(state
, p
->inversion
);
273 switch (p
->u
.ofdm
.bandwidth
) {
274 case BANDWIDTH_6_MHZ
:
275 cx22702_writereg(state
, 0x0C,
276 (cx22702_readreg(state
, 0x0C) & 0xcf) | 0x20);
278 case BANDWIDTH_7_MHZ
:
279 cx22702_writereg(state
, 0x0C,
280 (cx22702_readreg(state
, 0x0C) & 0xcf) | 0x10);
282 case BANDWIDTH_8_MHZ
:
283 cx22702_writereg(state
, 0x0C,
284 cx22702_readreg(state
, 0x0C) & 0xcf);
287 dprintk("%s: invalid bandwidth\n", __func__
);
291 p
->u
.ofdm
.code_rate_LP
= FEC_AUTO
; /* temp hack as manual not working */
293 /* use auto configuration? */
294 if ((p
->u
.ofdm
.hierarchy_information
== HIERARCHY_AUTO
) ||
295 (p
->u
.ofdm
.constellation
== QAM_AUTO
) ||
296 (p
->u
.ofdm
.code_rate_HP
== FEC_AUTO
) ||
297 (p
->u
.ofdm
.code_rate_LP
== FEC_AUTO
) ||
298 (p
->u
.ofdm
.guard_interval
== GUARD_INTERVAL_AUTO
) ||
299 (p
->u
.ofdm
.transmission_mode
== TRANSMISSION_MODE_AUTO
)) {
301 /* TPS Source - use hardware driven values */
302 cx22702_writereg(state
, 0x06, 0x10);
303 cx22702_writereg(state
, 0x07, 0x9);
304 cx22702_writereg(state
, 0x08, 0xC1);
305 cx22702_writereg(state
, 0x0B, cx22702_readreg(state
, 0x0B)
307 cx22702_writereg(state
, 0x0C,
308 (cx22702_readreg(state
, 0x0C) & 0xBF) | 0x40);
309 cx22702_writereg(state
, 0x00, 0x01); /* Begin aquisition */
310 dprintk("%s: Autodetecting\n", __func__
);
314 /* manually programmed values */
316 switch (p
->u
.ofdm
.constellation
) {
321 val
= (val
& 0xe7) | 0x08;
324 val
= (val
& 0xe7) | 0x10;
327 dprintk("%s: invalid constellation\n", __func__
);
330 switch (p
->u
.ofdm
.hierarchy_information
) {
335 val
= (val
& 0xf8) | 1;
338 val
= (val
& 0xf8) | 2;
341 val
= (val
& 0xf8) | 3;
344 dprintk("%s: invalid hierarchy\n", __func__
);
347 cx22702_writereg(state
, 0x06, val
);
350 switch (p
->u
.ofdm
.code_rate_HP
) {
356 val
= (val
& 0xc7) | 0x08;
359 val
= (val
& 0xc7) | 0x10;
362 val
= (val
& 0xc7) | 0x18;
365 val
= (val
& 0xc7) | 0x20;
368 dprintk("%s: invalid code_rate_HP\n", __func__
);
371 switch (p
->u
.ofdm
.code_rate_LP
) {
377 val
= (val
& 0xf8) | 1;
380 val
= (val
& 0xf8) | 2;
383 val
= (val
& 0xf8) | 3;
386 val
= (val
& 0xf8) | 4;
389 dprintk("%s: invalid code_rate_LP\n", __func__
);
392 cx22702_writereg(state
, 0x07, val
);
395 switch (p
->u
.ofdm
.guard_interval
) {
396 case GUARD_INTERVAL_1_32
:
399 case GUARD_INTERVAL_1_16
:
400 val
= (val
& 0xf3) | 0x04;
402 case GUARD_INTERVAL_1_8
:
403 val
= (val
& 0xf3) | 0x08;
405 case GUARD_INTERVAL_1_4
:
406 val
= (val
& 0xf3) | 0x0c;
409 dprintk("%s: invalid guard_interval\n", __func__
);
412 switch (p
->u
.ofdm
.transmission_mode
) {
413 case TRANSMISSION_MODE_2K
:
416 case TRANSMISSION_MODE_8K
:
417 val
= (val
& 0xfc) | 1;
420 dprintk("%s: invalid transmission_mode\n", __func__
);
423 cx22702_writereg(state
, 0x08, val
);
424 cx22702_writereg(state
, 0x0B,
425 (cx22702_readreg(state
, 0x0B) & 0xfc) | 0x02);
426 cx22702_writereg(state
, 0x0C,
427 (cx22702_readreg(state
, 0x0C) & 0xBF) | 0x40);
429 /* Begin channel aquisition */
430 cx22702_writereg(state
, 0x00, 0x01);
435 /* Reset the demod hardware and reset all of the configuration registers
436 to a default state. */
437 static int cx22702_init(struct dvb_frontend
*fe
)
440 struct cx22702_state
*state
= fe
->demodulator_priv
;
442 cx22702_writereg(state
, 0x00, 0x02);
446 for (i
= 0; i
< ARRAY_SIZE(init_tab
); i
+= 2)
447 cx22702_writereg(state
, init_tab
[i
], init_tab
[i
+ 1]);
449 cx22702_writereg(state
, 0xf8, (state
->config
->output_mode
<< 1)
452 cx22702_i2c_gate_ctrl(fe
, 0);
457 static int cx22702_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
459 struct cx22702_state
*state
= fe
->demodulator_priv
;
465 reg0A
= cx22702_readreg(state
, 0x0A);
466 reg23
= cx22702_readreg(state
, 0x23);
468 dprintk("%s: status demod=0x%02x agc=0x%02x\n"
469 , __func__
, reg0A
, reg23
);
472 *status
|= FE_HAS_LOCK
;
473 *status
|= FE_HAS_VITERBI
;
474 *status
|= FE_HAS_SYNC
;
478 *status
|= FE_HAS_CARRIER
;
481 *status
|= FE_HAS_SIGNAL
;
486 static int cx22702_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
488 struct cx22702_state
*state
= fe
->demodulator_priv
;
490 if (cx22702_readreg(state
, 0xE4) & 0x02) {
491 /* Realtime statistics */
492 *ber
= (cx22702_readreg(state
, 0xDE) & 0x7F) << 7
493 | (cx22702_readreg(state
, 0xDF) & 0x7F);
495 /* Averagtine statistics */
496 *ber
= (cx22702_readreg(state
, 0xDE) & 0x7F) << 7
497 | cx22702_readreg(state
, 0xDF);
503 static int cx22702_read_signal_strength(struct dvb_frontend
*fe
,
504 u16
*signal_strength
)
506 struct cx22702_state
*state
= fe
->demodulator_priv
;
509 rs_ber
= cx22702_readreg(state
, 0x23);
510 *signal_strength
= (rs_ber
<< 8) | rs_ber
;
515 static int cx22702_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
517 struct cx22702_state
*state
= fe
->demodulator_priv
;
520 if (cx22702_readreg(state
, 0xE4) & 0x02) {
521 /* Realtime statistics */
522 rs_ber
= (cx22702_readreg(state
, 0xDE) & 0x7F) << 7
523 | (cx22702_readreg(state
, 0xDF) & 0x7F);
525 /* Averagine statistics */
526 rs_ber
= (cx22702_readreg(state
, 0xDE) & 0x7F) << 8
527 | cx22702_readreg(state
, 0xDF);
534 static int cx22702_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
536 struct cx22702_state
*state
= fe
->demodulator_priv
;
540 /* RS Uncorrectable Packet Count then reset */
541 _ucblocks
= cx22702_readreg(state
, 0xE3);
542 if (state
->prevUCBlocks
< _ucblocks
)
543 *ucblocks
= (_ucblocks
- state
->prevUCBlocks
);
545 *ucblocks
= state
->prevUCBlocks
- _ucblocks
;
546 state
->prevUCBlocks
= _ucblocks
;
551 static int cx22702_get_frontend(struct dvb_frontend
*fe
,
552 struct dvb_frontend_parameters
*p
)
554 struct cx22702_state
*state
= fe
->demodulator_priv
;
556 u8 reg0C
= cx22702_readreg(state
, 0x0C);
558 p
->inversion
= reg0C
& 0x1 ? INVERSION_ON
: INVERSION_OFF
;
559 return cx22702_get_tps(state
, &p
->u
.ofdm
);
562 static int cx22702_get_tune_settings(struct dvb_frontend
*fe
,
563 struct dvb_frontend_tune_settings
*tune
)
565 tune
->min_delay_ms
= 1000;
569 static void cx22702_release(struct dvb_frontend
*fe
)
571 struct cx22702_state
*state
= fe
->demodulator_priv
;
575 static struct dvb_frontend_ops cx22702_ops
;
577 struct dvb_frontend
*cx22702_attach(const struct cx22702_config
*config
,
578 struct i2c_adapter
*i2c
)
580 struct cx22702_state
*state
= NULL
;
582 /* allocate memory for the internal state */
583 state
= kzalloc(sizeof(struct cx22702_state
), GFP_KERNEL
);
587 /* setup the state */
588 state
->config
= config
;
590 state
->prevUCBlocks
= 0;
592 /* check if the demod is there */
593 if (cx22702_readreg(state
, 0x1f) != 0x3)
596 /* create dvb_frontend */
597 memcpy(&state
->frontend
.ops
, &cx22702_ops
,
598 sizeof(struct dvb_frontend_ops
));
599 state
->frontend
.demodulator_priv
= state
;
600 return &state
->frontend
;
606 EXPORT_SYMBOL(cx22702_attach
);
608 static struct dvb_frontend_ops cx22702_ops
= {
611 .name
= "Conexant CX22702 DVB-T",
613 .frequency_min
= 177000000,
614 .frequency_max
= 858000000,
615 .frequency_stepsize
= 166666,
616 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
617 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
618 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
619 FE_CAN_HIERARCHY_AUTO
| FE_CAN_GUARD_INTERVAL_AUTO
|
620 FE_CAN_TRANSMISSION_MODE_AUTO
| FE_CAN_RECOVER
623 .release
= cx22702_release
,
625 .init
= cx22702_init
,
626 .i2c_gate_ctrl
= cx22702_i2c_gate_ctrl
,
628 .set_frontend
= cx22702_set_tps
,
629 .get_frontend
= cx22702_get_frontend
,
630 .get_tune_settings
= cx22702_get_tune_settings
,
632 .read_status
= cx22702_read_status
,
633 .read_ber
= cx22702_read_ber
,
634 .read_signal_strength
= cx22702_read_signal_strength
,
635 .read_snr
= cx22702_read_snr
,
636 .read_ucblocks
= cx22702_read_ucblocks
,
639 MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
640 MODULE_AUTHOR("Steven Toth");
641 MODULE_LICENSE("GPL");