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 const u8 init_tab[] = {
58 0x00, 0x00, /* Stop acquisition */
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);
95 if (unlikely(ret != 1)) {
97 "%s: error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
98 __func__, reg, data, ret);
105 static u8 cx22702_readreg(struct cx22702_state *state, u8 reg)
110 struct i2c_msg msg[] = {
111 { .addr = state->config->demod_address, .flags = 0,
112 .buf = ®, .len = 1 },
113 { .addr = state->config->demod_address, .flags = I2C_M_RD,
114 .buf = &data, .len = 1 } };
116 ret = i2c_transfer(state->i2c, msg, 2);
118 if (unlikely(ret != 2)) {
119 printk(KERN_ERR "%s: error (reg == 0x%02x, ret == %i)\n",
127 static int cx22702_set_inversion(struct cx22702_state *state, int inversion)
131 val = cx22702_readreg(state, 0x0C);
144 return cx22702_writereg(state, 0x0C, val);
147 /* Retrieve the demod settings */
148 static int cx22702_get_tps(struct cx22702_state *state,
149 struct dtv_frontend_properties *p)
153 /* Make sure the TPS regs are valid */
154 if (!(cx22702_readreg(state, 0x0A) & 0x20))
157 val = cx22702_readreg(state, 0x01);
158 switch ((val & 0x18) >> 3) {
160 p->modulation = QPSK;
163 p->modulation = QAM_16;
166 p->modulation = QAM_64;
169 switch (val & 0x07) {
171 p->hierarchy = HIERARCHY_NONE;
174 p->hierarchy = HIERARCHY_1;
177 p->hierarchy = HIERARCHY_2;
180 p->hierarchy = HIERARCHY_4;
185 val = cx22702_readreg(state, 0x02);
186 switch ((val & 0x38) >> 3) {
188 p->code_rate_HP = FEC_1_2;
191 p->code_rate_HP = FEC_2_3;
194 p->code_rate_HP = FEC_3_4;
197 p->code_rate_HP = FEC_5_6;
200 p->code_rate_HP = FEC_7_8;
203 switch (val & 0x07) {
205 p->code_rate_LP = FEC_1_2;
208 p->code_rate_LP = FEC_2_3;
211 p->code_rate_LP = FEC_3_4;
214 p->code_rate_LP = FEC_5_6;
217 p->code_rate_LP = FEC_7_8;
221 val = cx22702_readreg(state, 0x03);
222 switch ((val & 0x0c) >> 2) {
224 p->guard_interval = GUARD_INTERVAL_1_32;
227 p->guard_interval = GUARD_INTERVAL_1_16;
230 p->guard_interval = GUARD_INTERVAL_1_8;
233 p->guard_interval = GUARD_INTERVAL_1_4;
236 switch (val & 0x03) {
238 p->transmission_mode = TRANSMISSION_MODE_2K;
241 p->transmission_mode = TRANSMISSION_MODE_8K;
248 static int cx22702_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
250 struct cx22702_state *state = fe->demodulator_priv;
253 dprintk("%s(%d)\n", __func__, enable);
254 val = cx22702_readreg(state, 0x0D);
259 return cx22702_writereg(state, 0x0D, val);
262 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
263 static int cx22702_set_tps(struct dvb_frontend *fe)
265 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
267 struct cx22702_state *state = fe->demodulator_priv;
269 if (fe->ops.tuner_ops.set_params) {
270 fe->ops.tuner_ops.set_params(fe);
271 if (fe->ops.i2c_gate_ctrl)
272 fe->ops.i2c_gate_ctrl(fe, 0);
276 cx22702_set_inversion(state, p->inversion);
279 val = cx22702_readreg(state, 0x0C) & 0xcf;
280 switch (p->bandwidth_hz) {
290 dprintk("%s: invalid bandwidth\n", __func__);
293 cx22702_writereg(state, 0x0C, val);
295 p->code_rate_LP = FEC_AUTO; /* temp hack as manual not working */
297 /* use auto configuration? */
298 if ((p->hierarchy == HIERARCHY_AUTO) ||
299 (p->modulation == QAM_AUTO) ||
300 (p->code_rate_HP == FEC_AUTO) ||
301 (p->code_rate_LP == FEC_AUTO) ||
302 (p->guard_interval == GUARD_INTERVAL_AUTO) ||
303 (p->transmission_mode == TRANSMISSION_MODE_AUTO)) {
305 /* TPS Source - use hardware driven values */
306 cx22702_writereg(state, 0x06, 0x10);
307 cx22702_writereg(state, 0x07, 0x9);
308 cx22702_writereg(state, 0x08, 0xC1);
309 cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B)
311 cx22702_writereg(state, 0x0C,
312 (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
313 cx22702_writereg(state, 0x00, 0x01); /* Begin acquisition */
314 dprintk("%s: Autodetecting\n", __func__);
318 /* manually programmed values */
319 switch (p->modulation) { /* mask 0x18 */
330 dprintk("%s: invalid modulation\n", __func__);
333 switch (p->hierarchy) { /* mask 0x07 */
346 dprintk("%s: invalid hierarchy\n", __func__);
349 cx22702_writereg(state, 0x06, val);
351 switch (p->code_rate_HP) { /* mask 0x38 */
369 dprintk("%s: invalid code_rate_HP\n", __func__);
372 switch (p->code_rate_LP) { /* mask 0x07 */
389 dprintk("%s: invalid code_rate_LP\n", __func__);
392 cx22702_writereg(state, 0x07, val);
394 switch (p->guard_interval) { /* mask 0x0c */
395 case GUARD_INTERVAL_1_32:
398 case GUARD_INTERVAL_1_16:
401 case GUARD_INTERVAL_1_8:
404 case GUARD_INTERVAL_1_4:
408 dprintk("%s: invalid guard_interval\n", __func__);
411 switch (p->transmission_mode) { /* mask 0x03 */
412 case TRANSMISSION_MODE_2K:
414 case TRANSMISSION_MODE_8K:
418 dprintk("%s: invalid transmission_mode\n", __func__);
421 cx22702_writereg(state, 0x08, val);
422 cx22702_writereg(state, 0x0B,
423 (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02);
424 cx22702_writereg(state, 0x0C,
425 (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
427 /* Begin channel acquisition */
428 cx22702_writereg(state, 0x00, 0x01);
433 /* Reset the demod hardware and reset all of the configuration registers
434 to a default state. */
435 static int cx22702_init(struct dvb_frontend *fe)
438 struct cx22702_state *state = fe->demodulator_priv;
440 cx22702_writereg(state, 0x00, 0x02);
444 for (i = 0; i < ARRAY_SIZE(init_tab); i += 2)
445 cx22702_writereg(state, init_tab[i], init_tab[i + 1]);
447 cx22702_writereg(state, 0xf8, (state->config->output_mode << 1)
450 cx22702_i2c_gate_ctrl(fe, 0);
455 static int cx22702_read_status(struct dvb_frontend *fe, enum fe_status *status)
457 struct cx22702_state *state = fe->demodulator_priv;
463 reg0A = cx22702_readreg(state, 0x0A);
464 reg23 = cx22702_readreg(state, 0x23);
466 dprintk("%s: status demod=0x%02x agc=0x%02x\n"
467 , __func__, reg0A, reg23);
470 *status |= FE_HAS_LOCK;
471 *status |= FE_HAS_VITERBI;
472 *status |= FE_HAS_SYNC;
476 *status |= FE_HAS_CARRIER;
479 *status |= FE_HAS_SIGNAL;
484 static int cx22702_read_ber(struct dvb_frontend *fe, u32 *ber)
486 struct cx22702_state *state = fe->demodulator_priv;
488 if (cx22702_readreg(state, 0xE4) & 0x02) {
489 /* Realtime statistics */
490 *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
491 | (cx22702_readreg(state, 0xDF) & 0x7F);
493 /* Averagtine statistics */
494 *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
495 | cx22702_readreg(state, 0xDF);
501 static int cx22702_read_signal_strength(struct dvb_frontend *fe,
502 u16 *signal_strength)
504 struct cx22702_state *state = fe->demodulator_priv;
508 * Experience suggests that the strength signal register works as
510 * - In the absence of signal, value is 0xff.
511 * - In the presence of a weak signal, bit 7 is set, not sure what
512 * the lower 7 bits mean.
513 * - In the presence of a strong signal, the register holds a 7-bit
514 * value (bit 7 is cleared), with greater values standing for
517 reg23 = cx22702_readreg(state, 0x23);
519 *signal_strength = 0;
521 reg23 = ~reg23 & 0x7f;
522 /* Scale to 16 bit */
523 *signal_strength = (reg23 << 9) | (reg23 << 2) | (reg23 >> 5);
529 static int cx22702_read_snr(struct dvb_frontend *fe, u16 *snr)
531 struct cx22702_state *state = fe->demodulator_priv;
534 if (cx22702_readreg(state, 0xE4) & 0x02) {
535 /* Realtime statistics */
536 rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
537 | (cx22702_readreg(state, 0xDF) & 0x7F);
539 /* Averagine statistics */
540 rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 8
541 | cx22702_readreg(state, 0xDF);
548 static int cx22702_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
550 struct cx22702_state *state = fe->demodulator_priv;
554 /* RS Uncorrectable Packet Count then reset */
555 _ucblocks = cx22702_readreg(state, 0xE3);
556 if (state->prevUCBlocks < _ucblocks)
557 *ucblocks = (_ucblocks - state->prevUCBlocks);
559 *ucblocks = state->prevUCBlocks - _ucblocks;
560 state->prevUCBlocks = _ucblocks;
565 static int cx22702_get_frontend(struct dvb_frontend *fe)
567 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
568 struct cx22702_state *state = fe->demodulator_priv;
570 u8 reg0C = cx22702_readreg(state, 0x0C);
572 c->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF;
573 return cx22702_get_tps(state, c);
576 static int cx22702_get_tune_settings(struct dvb_frontend *fe,
577 struct dvb_frontend_tune_settings *tune)
579 tune->min_delay_ms = 1000;
583 static void cx22702_release(struct dvb_frontend *fe)
585 struct cx22702_state *state = fe->demodulator_priv;
589 static const struct dvb_frontend_ops cx22702_ops;
591 struct dvb_frontend *cx22702_attach(const struct cx22702_config *config,
592 struct i2c_adapter *i2c)
594 struct cx22702_state *state = NULL;
596 /* allocate memory for the internal state */
597 state = kzalloc(sizeof(struct cx22702_state), GFP_KERNEL);
601 /* setup the state */
602 state->config = config;
605 /* check if the demod is there */
606 if (cx22702_readreg(state, 0x1f) != 0x3)
609 /* create dvb_frontend */
610 memcpy(&state->frontend.ops, &cx22702_ops,
611 sizeof(struct dvb_frontend_ops));
612 state->frontend.demodulator_priv = state;
613 return &state->frontend;
619 EXPORT_SYMBOL(cx22702_attach);
621 static const struct dvb_frontend_ops cx22702_ops = {
622 .delsys = { SYS_DVBT },
624 .name = "Conexant CX22702 DVB-T",
625 .frequency_min = 177000000,
626 .frequency_max = 858000000,
627 .frequency_stepsize = 166666,
628 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
629 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
630 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
631 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
632 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
635 .release = cx22702_release,
637 .init = cx22702_init,
638 .i2c_gate_ctrl = cx22702_i2c_gate_ctrl,
640 .set_frontend = cx22702_set_tps,
641 .get_frontend = cx22702_get_frontend,
642 .get_tune_settings = cx22702_get_tune_settings,
644 .read_status = cx22702_read_status,
645 .read_ber = cx22702_read_ber,
646 .read_signal_strength = cx22702_read_signal_strength,
647 .read_snr = cx22702_read_snr,
648 .read_ucblocks = cx22702_read_ucblocks,
651 MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
652 MODULE_AUTHOR("Steven Toth");
653 MODULE_LICENSE("GPL");