2 Auvitek AU8522 QAM/8VSB demodulator driver
4 Copyright (C) 2008 Steven Toth <stoth@hauppauge.com>
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.
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include "dvb_frontend.h"
33 struct i2c_adapter *i2c;
35 /* configuration settings */
36 const struct au8522_config *config;
38 struct dvb_frontend frontend;
40 u32 current_frequency;
41 fe_modulation_t current_modulation;
47 #define dprintk(arg...) do { \
52 /* 16 bit registers, 8 bit values */
53 static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
56 u8 buf [] = { reg >> 8, reg & 0xff, data };
58 struct i2c_msg msg = { .addr = state->config->demod_address,
59 .flags = 0, .buf = buf, .len = 3 };
61 ret = i2c_transfer(state->i2c, &msg, 1);
64 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
65 "ret == %i)\n", __func__, reg, data, ret);
67 return (ret != 1) ? -1 : 0;
70 static u8 au8522_readreg(struct au8522_state *state, u16 reg)
73 u8 b0 [] = { reg >> 8, reg & 0xff };
76 struct i2c_msg msg [] = {
77 { .addr = state->config->demod_address, .flags = 0,
78 .buf = b0, .len = 2 },
79 { .addr = state->config->demod_address, .flags = I2C_M_RD,
80 .buf = b1, .len = 1 } };
82 ret = i2c_transfer(state->i2c, msg, 2);
85 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
90 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
92 struct au8522_state *state = fe->demodulator_priv;
94 dprintk("%s(%d)\n", __func__, enable);
97 return au8522_writereg(state, 0x106, 1);
99 return au8522_writereg(state, 0x106, 0);
107 /* VSB SNR lookup table */
108 static struct mse2snr_tab vsb_mse2snr_tab[] = {
141 /* QAM64 SNR lookup table */
142 static struct mse2snr_tab qam64_mse2snr_tab[] = {
222 /* QAM256 SNR lookup table */
223 static struct mse2snr_tab qam256_mse2snr_tab[] = {
290 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
293 int i, ret = -EINVAL;
294 dprintk("%s()\n", __func__);
296 for (i = 0; i < sz; i++) {
297 if (mse < tab[i].val) {
303 dprintk("%s() snr=%d\n", __func__, *snr);
307 /* 3.25 MHz IF Frequency table */
311 } if_3_25_mhz_tab[] = {
317 /* 4.00 MHz IF Frequency table */
327 /* 6.00 MHz IF Frequency table */
337 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
339 struct au8522_state *state = fe->demodulator_priv;
343 case AU8522_IF_3_25MHZ:
344 dprintk("%s() 3.25 MHz\n", __func__);
345 for (i = 0; i < ARRAY_SIZE(if_3_25_mhz_tab); i++)
346 au8522_writereg(state,
347 if_3_25_mhz_tab[i].reg,
348 if_3_25_mhz_tab[i].data);
351 dprintk("%s() 4.00 MHz\n", __func__);
352 for (i = 0; i < ARRAY_SIZE(if_4_mhz_tab); i++)
353 au8522_writereg(state,
355 if_4_mhz_tab[i].data);
358 dprintk("%s() 6.00 MHz\n", __func__);
359 for (i = 0; i < ARRAY_SIZE(if_6_mhz_tab); i++)
360 au8522_writereg(state,
362 if_6_mhz_tab[i].data);
365 dprintk("%s() IF Frequency not supported\n", __func__);
371 /* VSB Modulation table */
408 /* QAM Modulation table */
490 static int au8522_enable_modulation(struct dvb_frontend *fe,
493 struct au8522_state *state = fe->demodulator_priv;
496 dprintk("%s(0x%08x)\n", __func__, m);
500 dprintk("%s() VSB_8\n", __func__);
501 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
502 au8522_writereg(state,
504 VSB_mod_tab[i].data);
505 au8522_set_if(fe, state->config->vsb_if);
509 dprintk("%s() QAM 64/256\n", __func__);
510 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
511 au8522_writereg(state,
513 QAM_mod_tab[i].data);
514 au8522_set_if(fe, state->config->qam_if);
517 dprintk("%s() Invalid modulation\n", __func__);
521 state->current_modulation = m;
526 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
527 static int au8522_set_frontend(struct dvb_frontend *fe,
528 struct dvb_frontend_parameters *p)
530 struct au8522_state *state = fe->demodulator_priv;
533 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
535 if ((state->current_frequency == p->frequency) &&
536 (state->current_modulation == p->u.vsb.modulation))
539 au8522_enable_modulation(fe, p->u.vsb.modulation);
541 /* Allow the demod to settle */
544 if (fe->ops.tuner_ops.set_params) {
545 if (fe->ops.i2c_gate_ctrl)
546 fe->ops.i2c_gate_ctrl(fe, 1);
547 ret = fe->ops.tuner_ops.set_params(fe, p);
548 if (fe->ops.i2c_gate_ctrl)
549 fe->ops.i2c_gate_ctrl(fe, 0);
555 state->current_frequency = p->frequency;
560 /* Reset the demod hardware and reset all of the configuration registers
561 to a default state. */
562 static int au8522_init(struct dvb_frontend *fe)
564 struct au8522_state *state = fe->demodulator_priv;
565 dprintk("%s()\n", __func__);
567 au8522_writereg(state, 0xa4, 1 << 5);
569 au8522_i2c_gate_ctrl(fe, 1);
574 static int au8522_sleep(struct dvb_frontend *fe)
576 struct au8522_state *state = fe->demodulator_priv;
577 dprintk("%s()\n", __func__);
579 state->current_frequency = 0;
584 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
586 struct au8522_state *state = fe->demodulator_priv;
588 u32 tuner_status = 0;
592 if (state->current_modulation == VSB_8) {
593 dprintk("%s() Checking VSB_8\n", __func__);
594 reg = au8522_readreg(state, 0x4088);
595 if ((reg & 0x03) == 0x03)
596 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
598 dprintk("%s() Checking QAM\n", __func__);
599 reg = au8522_readreg(state, 0x4541);
601 *status |= FE_HAS_VITERBI;
603 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
606 switch (state->config->status_mode) {
607 case AU8522_DEMODLOCKING:
608 dprintk("%s() DEMODLOCKING\n", __func__);
609 if (*status & FE_HAS_VITERBI)
610 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
612 case AU8522_TUNERLOCKING:
613 /* Get the tuner status */
614 dprintk("%s() TUNERLOCKING\n", __func__);
615 if (fe->ops.tuner_ops.get_status) {
616 if (fe->ops.i2c_gate_ctrl)
617 fe->ops.i2c_gate_ctrl(fe, 1);
619 fe->ops.tuner_ops.get_status(fe, &tuner_status);
621 if (fe->ops.i2c_gate_ctrl)
622 fe->ops.i2c_gate_ctrl(fe, 0);
625 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
629 dprintk("%s() status 0x%08x\n", __func__, *status);
634 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
636 struct au8522_state *state = fe->demodulator_priv;
639 dprintk("%s()\n", __func__);
641 if (state->current_modulation == QAM_256)
642 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
643 ARRAY_SIZE(qam256_mse2snr_tab),
644 au8522_readreg(state, 0x4522),
646 else if (state->current_modulation == QAM_64)
647 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
648 ARRAY_SIZE(qam64_mse2snr_tab),
649 au8522_readreg(state, 0x4522),
652 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
653 ARRAY_SIZE(vsb_mse2snr_tab),
654 au8522_readreg(state, 0x4311),
660 static int au8522_read_signal_strength(struct dvb_frontend *fe,
661 u16 *signal_strength)
663 return au8522_read_snr(fe, signal_strength);
666 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
668 struct au8522_state *state = fe->demodulator_priv;
670 if (state->current_modulation == VSB_8)
671 *ucblocks = au8522_readreg(state, 0x4087);
673 *ucblocks = au8522_readreg(state, 0x4543);
678 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
680 return au8522_read_ucblocks(fe, ber);
683 static int au8522_get_frontend(struct dvb_frontend *fe,
684 struct dvb_frontend_parameters *p)
686 struct au8522_state *state = fe->demodulator_priv;
688 p->frequency = state->current_frequency;
689 p->u.vsb.modulation = state->current_modulation;
694 static int au8522_get_tune_settings(struct dvb_frontend *fe,
695 struct dvb_frontend_tune_settings *tune)
697 tune->min_delay_ms = 1000;
701 static void au8522_release(struct dvb_frontend *fe)
703 struct au8522_state *state = fe->demodulator_priv;
707 static struct dvb_frontend_ops au8522_ops;
709 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
710 struct i2c_adapter *i2c)
712 struct au8522_state *state = NULL;
714 /* allocate memory for the internal state */
715 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
719 /* setup the state */
720 state->config = config;
722 /* create dvb_frontend */
723 memcpy(&state->frontend.ops, &au8522_ops,
724 sizeof(struct dvb_frontend_ops));
725 state->frontend.demodulator_priv = state;
727 if (au8522_init(&state->frontend) != 0) {
728 printk(KERN_ERR "%s: Failed to initialize correctly\n",
733 /* Note: Leaving the I2C gate open here. */
734 au8522_i2c_gate_ctrl(&state->frontend, 1);
736 return &state->frontend;
742 EXPORT_SYMBOL(au8522_attach);
744 static struct dvb_frontend_ops au8522_ops = {
747 .name = "Auvitek AU8522 QAM/8VSB Frontend",
749 .frequency_min = 54000000,
750 .frequency_max = 858000000,
751 .frequency_stepsize = 62500,
752 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
756 .sleep = au8522_sleep,
757 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
758 .set_frontend = au8522_set_frontend,
759 .get_frontend = au8522_get_frontend,
760 .get_tune_settings = au8522_get_tune_settings,
761 .read_status = au8522_read_status,
762 .read_ber = au8522_read_ber,
763 .read_signal_strength = au8522_read_signal_strength,
764 .read_snr = au8522_read_snr,
765 .read_ucblocks = au8522_read_ucblocks,
766 .release = au8522_release,
769 module_param(debug, int, 0644);
770 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
772 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
773 MODULE_AUTHOR("Steven Toth");
774 MODULE_LICENSE("GPL");