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;
47 #define dprintk if (debug) printk
49 /* 16 bit registers, 8 bit values */
50 static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
53 u8 buf [] = { reg >> 8, reg & 0xff, data };
55 struct i2c_msg msg = { .addr = state->config->demod_address,
56 .flags = 0, .buf = buf, .len = 3 };
58 ret = i2c_transfer(state->i2c, &msg, 1);
61 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
62 "ret == %i)\n", __func__, reg, data, ret);
64 return (ret != 1) ? -1 : 0;
67 static u8 au8522_readreg(struct au8522_state *state, u16 reg)
70 u8 b0 [] = { reg >> 8, reg & 0xff };
73 struct i2c_msg msg [] = {
74 { .addr = state->config->demod_address, .flags = 0,
75 .buf = b0, .len = 2 },
76 { .addr = state->config->demod_address, .flags = I2C_M_RD,
77 .buf = b1, .len = 1 } };
79 ret = i2c_transfer(state->i2c, msg, 2);
82 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
87 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
89 struct au8522_state *state = fe->demodulator_priv;
91 dprintk("%s(%d)\n", __func__, enable);
94 return au8522_writereg(state, 0x106, 1);
96 return au8522_writereg(state, 0x106, 0);
104 /* VSB SNR lookup table */
105 static struct mse2snr_tab vsb_mse2snr_tab[] = {
138 /* QAM64 SNR lookup table */
139 static struct mse2snr_tab qam64_mse2snr_tab[] = {
219 /* QAM256 SNR lookup table */
220 static struct mse2snr_tab qam256_mse2snr_tab[] = {
287 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse, u16 *snr)
289 int i, ret = -EINVAL;
290 dprintk("%s()\n", __func__);
292 for (i = 0; i < sz; i++) {
293 if (mse < tab[i].val) {
299 dprintk("%s() snr=%d\n", __func__, *snr);
303 /* VSB Modulation table */
340 /* QAM Modulation table */
422 static int au8522_enable_modulation(struct dvb_frontend *fe,
425 struct au8522_state *state = fe->demodulator_priv;
428 dprintk("%s(0x%08x)\n", __func__, m);
432 dprintk("%s() VSB_8\n", __func__);
433 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
434 au8522_writereg(state,
436 VSB_mod_tab[i].data);
440 dprintk("%s() QAM 64/256\n", __func__);
441 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
442 au8522_writereg(state,
444 QAM_mod_tab[i].data);
447 dprintk("%s() Invalid modulation\n", __func__);
451 state->current_modulation = m;
456 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
457 static int au8522_set_frontend(struct dvb_frontend *fe,
458 struct dvb_frontend_parameters *p)
460 struct au8522_state *state = fe->demodulator_priv;
462 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
464 state->current_frequency = p->frequency;
466 au8522_enable_modulation(fe, p->u.vsb.modulation);
468 /* Allow the demod to settle */
471 if (fe->ops.tuner_ops.set_params) {
472 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
473 fe->ops.tuner_ops.set_params(fe, p);
474 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
480 /* Reset the demod hardware and reset all of the configuration registers
481 to a default state. */
482 static int au8522_init(struct dvb_frontend *fe)
484 struct au8522_state *state = fe->demodulator_priv;
485 dprintk("%s()\n", __func__);
487 au8522_writereg(state, 0xa4, 1 << 5);
489 au8522_i2c_gate_ctrl(fe, 1);
494 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
496 struct au8522_state *state = fe->demodulator_priv;
498 u32 tuner_status = 0;
502 if (state->current_modulation == VSB_8) {
503 dprintk("%s() Checking VSB_8\n", __func__);
504 //au8522_writereg(state, 0x80a4, 0x20);
505 reg = au8522_readreg(state, 0x4088);
507 *status |= FE_HAS_VITERBI;
509 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
511 dprintk("%s() Checking QAM\n", __func__);
512 reg = au8522_readreg(state, 0x4541);
514 *status |= FE_HAS_VITERBI;
516 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
519 switch(state->config->status_mode) {
520 case AU8522_DEMODLOCKING:
521 dprintk("%s() DEMODLOCKING\n", __func__);
522 if (*status & FE_HAS_VITERBI)
523 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
525 case AU8522_TUNERLOCKING:
526 /* Get the tuner status */
527 dprintk("%s() TUNERLOCKING\n", __func__);
528 if (fe->ops.tuner_ops.get_status) {
529 if (fe->ops.i2c_gate_ctrl)
530 fe->ops.i2c_gate_ctrl(fe, 1);
532 fe->ops.tuner_ops.get_status(fe, &tuner_status);
534 if (fe->ops.i2c_gate_ctrl)
535 fe->ops.i2c_gate_ctrl(fe, 0);
538 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
542 dprintk("%s() status 0x%08x\n", __func__, *status);
547 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
549 struct au8522_state *state = fe->demodulator_priv;
552 dprintk("%s()\n", __func__);
554 if (state->current_modulation == QAM_256)
555 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
556 ARRAY_SIZE(qam256_mse2snr_tab),
557 au8522_readreg(state, 0x4522),
559 else if (state->current_modulation == QAM_64)
560 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
561 ARRAY_SIZE(qam64_mse2snr_tab),
562 au8522_readreg(state, 0x4522),
565 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
566 ARRAY_SIZE(vsb_mse2snr_tab),
567 au8522_readreg(state, 0x4311),
573 static int au8522_read_signal_strength(struct dvb_frontend *fe,
574 u16 *signal_strength)
576 return au8522_read_snr(fe, signal_strength);
579 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
581 struct au8522_state *state = fe->demodulator_priv;
583 if (state->current_modulation == VSB_8)
584 *ucblocks = au8522_readreg(state, 0x4087);
586 *ucblocks = au8522_readreg(state, 0x4543);
591 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
593 return au8522_read_ucblocks(fe, ber);
596 static int au8522_get_frontend(struct dvb_frontend *fe,
597 struct dvb_frontend_parameters *p)
599 struct au8522_state *state = fe->demodulator_priv;
601 p->frequency = state->current_frequency;
602 p->u.vsb.modulation = state->current_modulation;
607 static int au8522_get_tune_settings(struct dvb_frontend *fe,
608 struct dvb_frontend_tune_settings *tune)
610 tune->min_delay_ms = 1000;
614 static void au8522_release(struct dvb_frontend *fe)
616 struct au8522_state *state = fe->demodulator_priv;
620 static struct dvb_frontend_ops au8522_ops;
622 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
623 struct i2c_adapter *i2c)
625 struct au8522_state *state = NULL;
627 /* allocate memory for the internal state */
628 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
632 /* setup the state */
633 state->config = config;
635 /* create dvb_frontend */
636 memcpy(&state->frontend.ops, &au8522_ops,
637 sizeof(struct dvb_frontend_ops));
638 state->frontend.demodulator_priv = state;
640 if (au8522_init(&state->frontend) != 0) {
641 printk(KERN_ERR "%s: Failed to initialize correctly\n",
646 /* Note: Leaving the I2C gate open here. */
647 au8522_i2c_gate_ctrl(&state->frontend, 1);
649 return &state->frontend;
656 static struct dvb_frontend_ops au8522_ops = {
659 .name = "Auvitek AU8522 QAM/8VSB Frontend",
661 .frequency_min = 54000000,
662 .frequency_max = 858000000,
663 .frequency_stepsize = 62500,
664 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
668 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
669 .set_frontend = au8522_set_frontend,
670 .get_frontend = au8522_get_frontend,
671 .get_tune_settings = au8522_get_tune_settings,
672 .read_status = au8522_read_status,
673 .read_ber = au8522_read_ber,
674 .read_signal_strength = au8522_read_signal_strength,
675 .read_snr = au8522_read_snr,
676 .read_ucblocks = au8522_read_ucblocks,
677 .release = au8522_release,
680 module_param(debug, int, 0644);
681 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
683 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
684 MODULE_AUTHOR("Steven Toth");
685 MODULE_LICENSE("GPL");
687 EXPORT_SYMBOL(au8522_attach);