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"
34 struct i2c_adapter *i2c;
36 /* configuration settings */
37 const struct au8522_config *config;
39 struct dvb_frontend frontend;
41 u32 current_frequency;
42 fe_modulation_t current_modulation;
48 #define dprintk(arg...) do { \
53 /* 16 bit registers, 8 bit values */
54 static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
57 u8 buf [] = { reg >> 8, reg & 0xff, data };
59 struct i2c_msg msg = { .addr = state->config->demod_address,
60 .flags = 0, .buf = buf, .len = 3 };
62 ret = i2c_transfer(state->i2c, &msg, 1);
65 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
66 "ret == %i)\n", __func__, reg, data, ret);
68 return (ret != 1) ? -1 : 0;
71 static u8 au8522_readreg(struct au8522_state *state, u16 reg)
74 u8 b0 [] = { reg >> 8, reg & 0xff };
77 struct i2c_msg msg [] = {
78 { .addr = state->config->demod_address, .flags = 0,
79 .buf = b0, .len = 2 },
80 { .addr = state->config->demod_address, .flags = I2C_M_RD,
81 .buf = b1, .len = 1 } };
83 ret = i2c_transfer(state->i2c, msg, 2);
86 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
91 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
93 struct au8522_state *state = fe->demodulator_priv;
95 dprintk("%s(%d)\n", __func__, enable);
98 return au8522_writereg(state, 0x106, 1);
100 return au8522_writereg(state, 0x106, 0);
108 /* VSB SNR lookup table */
109 static struct mse2snr_tab vsb_mse2snr_tab[] = {
142 /* QAM64 SNR lookup table */
143 static struct mse2snr_tab qam64_mse2snr_tab[] = {
223 /* QAM256 SNR lookup table */
224 static struct mse2snr_tab qam256_mse2snr_tab[] = {
291 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
294 int i, ret = -EINVAL;
295 dprintk("%s()\n", __func__);
297 for (i = 0; i < sz; i++) {
298 if (mse < tab[i].val) {
304 dprintk("%s() snr=%d\n", __func__, *snr);
308 /* VSB Modulation table */
345 /* QAM Modulation table */
427 static int au8522_enable_modulation(struct dvb_frontend *fe,
430 struct au8522_state *state = fe->demodulator_priv;
433 dprintk("%s(0x%08x)\n", __func__, m);
437 dprintk("%s() VSB_8\n", __func__);
438 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
439 au8522_writereg(state,
441 VSB_mod_tab[i].data);
445 dprintk("%s() QAM 64/256\n", __func__);
446 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
447 au8522_writereg(state,
449 QAM_mod_tab[i].data);
452 dprintk("%s() Invalid modulation\n", __func__);
456 state->current_modulation = m;
461 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
462 static int au8522_set_frontend(struct dvb_frontend *fe,
463 struct dvb_frontend_parameters *p)
465 struct au8522_state *state = fe->demodulator_priv;
467 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
469 state->current_frequency = p->frequency;
471 au8522_enable_modulation(fe, p->u.vsb.modulation);
473 /* Allow the demod to settle */
476 if (fe->ops.tuner_ops.set_params) {
477 if (fe->ops.i2c_gate_ctrl)
478 fe->ops.i2c_gate_ctrl(fe, 1);
479 fe->ops.tuner_ops.set_params(fe, p);
480 if (fe->ops.i2c_gate_ctrl)
481 fe->ops.i2c_gate_ctrl(fe, 0);
487 /* Reset the demod hardware and reset all of the configuration registers
488 to a default state. */
489 static int au8522_init(struct dvb_frontend *fe)
491 struct au8522_state *state = fe->demodulator_priv;
492 dprintk("%s()\n", __func__);
494 au8522_writereg(state, 0xa4, 1 << 5);
496 au8522_i2c_gate_ctrl(fe, 1);
501 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
503 struct au8522_state *state = fe->demodulator_priv;
505 u32 tuner_status = 0;
509 if (state->current_modulation == VSB_8) {
510 dprintk("%s() Checking VSB_8\n", __func__);
511 reg = au8522_readreg(state, 0x4088);
513 *status |= FE_HAS_VITERBI;
515 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
517 dprintk("%s() Checking QAM\n", __func__);
518 reg = au8522_readreg(state, 0x4541);
520 *status |= FE_HAS_VITERBI;
522 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
525 switch (state->config->status_mode) {
526 case AU8522_DEMODLOCKING:
527 dprintk("%s() DEMODLOCKING\n", __func__);
528 if (*status & FE_HAS_VITERBI)
529 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
531 case AU8522_TUNERLOCKING:
532 /* Get the tuner status */
533 dprintk("%s() TUNERLOCKING\n", __func__);
534 if (fe->ops.tuner_ops.get_status) {
535 if (fe->ops.i2c_gate_ctrl)
536 fe->ops.i2c_gate_ctrl(fe, 1);
538 fe->ops.tuner_ops.get_status(fe, &tuner_status);
540 if (fe->ops.i2c_gate_ctrl)
541 fe->ops.i2c_gate_ctrl(fe, 0);
544 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
548 dprintk("%s() status 0x%08x\n", __func__, *status);
553 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
555 struct au8522_state *state = fe->demodulator_priv;
558 dprintk("%s()\n", __func__);
560 if (state->current_modulation == QAM_256)
561 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
562 ARRAY_SIZE(qam256_mse2snr_tab),
563 au8522_readreg(state, 0x4522),
565 else if (state->current_modulation == QAM_64)
566 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
567 ARRAY_SIZE(qam64_mse2snr_tab),
568 au8522_readreg(state, 0x4522),
571 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
572 ARRAY_SIZE(vsb_mse2snr_tab),
573 au8522_readreg(state, 0x4311),
579 static int au8522_read_signal_strength(struct dvb_frontend *fe,
580 u16 *signal_strength)
582 return au8522_read_snr(fe, signal_strength);
585 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
587 struct au8522_state *state = fe->demodulator_priv;
589 if (state->current_modulation == VSB_8)
590 *ucblocks = au8522_readreg(state, 0x4087);
592 *ucblocks = au8522_readreg(state, 0x4543);
597 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
599 return au8522_read_ucblocks(fe, ber);
602 static int au8522_get_frontend(struct dvb_frontend *fe,
603 struct dvb_frontend_parameters *p)
605 struct au8522_state *state = fe->demodulator_priv;
607 p->frequency = state->current_frequency;
608 p->u.vsb.modulation = state->current_modulation;
613 static int au8522_get_tune_settings(struct dvb_frontend *fe,
614 struct dvb_frontend_tune_settings *tune)
616 tune->min_delay_ms = 1000;
620 static void au8522_release(struct dvb_frontend *fe)
622 struct au8522_state *state = fe->demodulator_priv;
626 static struct dvb_frontend_ops au8522_ops;
628 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
629 struct i2c_adapter *i2c)
631 struct au8522_state *state = NULL;
633 /* allocate memory for the internal state */
634 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
638 /* setup the state */
639 state->config = config;
641 /* create dvb_frontend */
642 memcpy(&state->frontend.ops, &au8522_ops,
643 sizeof(struct dvb_frontend_ops));
644 state->frontend.demodulator_priv = state;
646 if (au8522_init(&state->frontend) != 0) {
647 printk(KERN_ERR "%s: Failed to initialize correctly\n",
652 /* Note: Leaving the I2C gate open here. */
653 au8522_i2c_gate_ctrl(&state->frontend, 1);
655 return &state->frontend;
661 EXPORT_SYMBOL(au8522_attach);
663 static struct dvb_frontend_ops au8522_ops = {
666 .name = "Auvitek AU8522 QAM/8VSB Frontend",
668 .frequency_min = 54000000,
669 .frequency_max = 858000000,
670 .frequency_stepsize = 62500,
671 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
675 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
676 .set_frontend = au8522_set_frontend,
677 .get_frontend = au8522_get_frontend,
678 .get_tune_settings = au8522_get_tune_settings,
679 .read_status = au8522_read_status,
680 .read_ber = au8522_read_ber,
681 .read_signal_strength = au8522_read_signal_strength,
682 .read_snr = au8522_read_snr,
683 .read_ucblocks = au8522_read_ucblocks,
684 .release = au8522_release,
687 module_param(debug, int, 0644);
688 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
690 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
691 MODULE_AUTHOR("Steven Toth");
692 MODULE_LICENSE("GPL");