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;
468 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
470 if ((state->current_frequency == p->frequency) &&
471 (state->current_modulation == p->u.vsb.modulation))
474 au8522_enable_modulation(fe, p->u.vsb.modulation);
476 /* Allow the demod to settle */
479 if (fe->ops.tuner_ops.set_params) {
480 if (fe->ops.i2c_gate_ctrl)
481 fe->ops.i2c_gate_ctrl(fe, 1);
482 ret = fe->ops.tuner_ops.set_params(fe, p);
483 if (fe->ops.i2c_gate_ctrl)
484 fe->ops.i2c_gate_ctrl(fe, 0);
490 state->current_frequency = p->frequency;
495 /* Reset the demod hardware and reset all of the configuration registers
496 to a default state. */
497 static int au8522_init(struct dvb_frontend *fe)
499 struct au8522_state *state = fe->demodulator_priv;
500 dprintk("%s()\n", __func__);
502 au8522_writereg(state, 0xa4, 1 << 5);
504 au8522_i2c_gate_ctrl(fe, 1);
509 static int au8522_sleep(struct dvb_frontend *fe)
511 struct au8522_state *state = fe->demodulator_priv;
512 dprintk("%s()\n", __func__);
514 state->current_frequency = 0;
519 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
521 struct au8522_state *state = fe->demodulator_priv;
523 u32 tuner_status = 0;
527 if (state->current_modulation == VSB_8) {
528 dprintk("%s() Checking VSB_8\n", __func__);
529 reg = au8522_readreg(state, 0x4088);
530 if ((reg & 0x03) == 0x03)
531 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
533 dprintk("%s() Checking QAM\n", __func__);
534 reg = au8522_readreg(state, 0x4541);
536 *status |= FE_HAS_VITERBI;
538 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
541 switch (state->config->status_mode) {
542 case AU8522_DEMODLOCKING:
543 dprintk("%s() DEMODLOCKING\n", __func__);
544 if (*status & FE_HAS_VITERBI)
545 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
547 case AU8522_TUNERLOCKING:
548 /* Get the tuner status */
549 dprintk("%s() TUNERLOCKING\n", __func__);
550 if (fe->ops.tuner_ops.get_status) {
551 if (fe->ops.i2c_gate_ctrl)
552 fe->ops.i2c_gate_ctrl(fe, 1);
554 fe->ops.tuner_ops.get_status(fe, &tuner_status);
556 if (fe->ops.i2c_gate_ctrl)
557 fe->ops.i2c_gate_ctrl(fe, 0);
560 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
564 dprintk("%s() status 0x%08x\n", __func__, *status);
569 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
571 struct au8522_state *state = fe->demodulator_priv;
574 dprintk("%s()\n", __func__);
576 if (state->current_modulation == QAM_256)
577 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
578 ARRAY_SIZE(qam256_mse2snr_tab),
579 au8522_readreg(state, 0x4522),
581 else if (state->current_modulation == QAM_64)
582 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
583 ARRAY_SIZE(qam64_mse2snr_tab),
584 au8522_readreg(state, 0x4522),
587 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
588 ARRAY_SIZE(vsb_mse2snr_tab),
589 au8522_readreg(state, 0x4311),
595 static int au8522_read_signal_strength(struct dvb_frontend *fe,
596 u16 *signal_strength)
598 return au8522_read_snr(fe, signal_strength);
601 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
603 struct au8522_state *state = fe->demodulator_priv;
605 if (state->current_modulation == VSB_8)
606 *ucblocks = au8522_readreg(state, 0x4087);
608 *ucblocks = au8522_readreg(state, 0x4543);
613 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
615 return au8522_read_ucblocks(fe, ber);
618 static int au8522_get_frontend(struct dvb_frontend *fe,
619 struct dvb_frontend_parameters *p)
621 struct au8522_state *state = fe->demodulator_priv;
623 p->frequency = state->current_frequency;
624 p->u.vsb.modulation = state->current_modulation;
629 static int au8522_get_tune_settings(struct dvb_frontend *fe,
630 struct dvb_frontend_tune_settings *tune)
632 tune->min_delay_ms = 1000;
636 static void au8522_release(struct dvb_frontend *fe)
638 struct au8522_state *state = fe->demodulator_priv;
642 static struct dvb_frontend_ops au8522_ops;
644 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
645 struct i2c_adapter *i2c)
647 struct au8522_state *state = NULL;
649 /* allocate memory for the internal state */
650 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
654 /* setup the state */
655 state->config = config;
657 /* create dvb_frontend */
658 memcpy(&state->frontend.ops, &au8522_ops,
659 sizeof(struct dvb_frontend_ops));
660 state->frontend.demodulator_priv = state;
662 if (au8522_init(&state->frontend) != 0) {
663 printk(KERN_ERR "%s: Failed to initialize correctly\n",
668 /* Note: Leaving the I2C gate open here. */
669 au8522_i2c_gate_ctrl(&state->frontend, 1);
671 return &state->frontend;
677 EXPORT_SYMBOL(au8522_attach);
679 static struct dvb_frontend_ops au8522_ops = {
682 .name = "Auvitek AU8522 QAM/8VSB Frontend",
684 .frequency_min = 54000000,
685 .frequency_max = 858000000,
686 .frequency_stepsize = 62500,
687 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
691 .sleep = au8522_sleep,
692 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
693 .set_frontend = au8522_set_frontend,
694 .get_frontend = au8522_get_frontend,
695 .get_tune_settings = au8522_get_tune_settings,
696 .read_status = au8522_read_status,
697 .read_ber = au8522_read_ber,
698 .read_signal_strength = au8522_read_signal_strength,
699 .read_snr = au8522_read_snr,
700 .read_ucblocks = au8522_read_ucblocks,
701 .release = au8522_release,
704 module_param(debug, int, 0644);
705 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
707 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
708 MODULE_AUTHOR("Steven Toth");
709 MODULE_LICENSE("GPL");