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);
99 /* VSB SNR lookup table */
103 } vsb_mse2snr_tab[] = {
136 /* QAM64 SNR lookup table */
140 } qam64_mse2snr_tab[] = {
220 /* QAM256 SNR lookup table */
224 } qam256_mse2snr_tab[] = {
291 static int au8522_vsb_mse2snr_lookup(int mse, u16 *snr)
293 int i, ret = -EINVAL;
294 dprintk("%s()\n", __func__);
296 for (i = 0; i < ARRAY_SIZE(vsb_mse2snr_tab); i++) {
297 if (mse < vsb_mse2snr_tab[i].val) {
298 *snr = vsb_mse2snr_tab[i].data;
303 dprintk("%s() snr=%d\n", __func__, *snr);
307 static int au8522_qam64_mse2snr_lookup(int mse, u16 *snr)
309 int i, ret = -EINVAL;
310 dprintk("%s()\n", __func__);
312 for (i = 0; i < ARRAY_SIZE(qam64_mse2snr_tab); i++) {
313 if (mse < qam64_mse2snr_tab[i].val) {
314 *snr = qam64_mse2snr_tab[i].data;
319 dprintk("%s() snr=%d\n", __func__, *snr);
323 static int au8522_qam256_mse2snr_lookup(int mse, u16 *snr)
325 int i, ret = -EINVAL;
326 dprintk("%s()\n", __func__);
328 for (i = 0; i < ARRAY_SIZE(qam256_mse2snr_tab); i++) {
329 if (mse < qam256_mse2snr_tab[i].val) {
330 *snr = qam256_mse2snr_tab[i].data;
335 dprintk("%s() snr=%d\n", __func__, *snr);
339 /* VSB Modulation table */
376 /* QAM Modulation table */
458 static int au8522_enable_modulation(struct dvb_frontend *fe,
461 struct au8522_state *state = fe->demodulator_priv;
464 dprintk("%s(0x%08x)\n", __func__, m);
468 dprintk("%s() VSB_8\n", __func__);
469 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
470 au8522_writereg(state,
472 VSB_mod_tab[i].data);
476 dprintk("%s() QAM 64/256\n", __func__);
477 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
478 au8522_writereg(state,
480 QAM_mod_tab[i].data);
483 dprintk("%s() Invalid modulation\n", __func__);
487 state->current_modulation = m;
492 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
493 static int au8522_set_frontend(struct dvb_frontend *fe,
494 struct dvb_frontend_parameters *p)
496 struct au8522_state *state = fe->demodulator_priv;
498 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
500 state->current_frequency = p->frequency;
502 au8522_enable_modulation(fe, p->u.vsb.modulation);
504 /* Allow the demod to settle */
507 if (fe->ops.tuner_ops.set_params) {
508 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
509 fe->ops.tuner_ops.set_params(fe, p);
510 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
516 /* Reset the demod hardware and reset all of the configuration registers
517 to a default state. */
518 static int au8522_init(struct dvb_frontend *fe)
520 struct au8522_state *state = fe->demodulator_priv;
521 dprintk("%s()\n", __func__);
523 au8522_writereg(state, 0xa4, 1 << 5);
525 au8522_i2c_gate_ctrl(fe, 1);
530 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
532 struct au8522_state *state = fe->demodulator_priv;
534 u32 tuner_status = 0;
538 if (state->current_modulation == VSB_8) {
539 dprintk("%s() Checking VSB_8\n", __func__);
540 //au8522_writereg(state, 0x80a4, 0x20);
541 reg = au8522_readreg(state, 0x4088);
543 *status |= FE_HAS_VITERBI;
545 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
547 dprintk("%s() Checking QAM\n", __func__);
548 reg = au8522_readreg(state, 0x4541);
550 *status |= FE_HAS_VITERBI;
552 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
555 switch(state->config->status_mode) {
556 case AU8522_DEMODLOCKING:
557 dprintk("%s() DEMODLOCKING\n", __func__);
558 if (*status & FE_HAS_VITERBI)
559 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
561 case AU8522_TUNERLOCKING:
562 /* Get the tuner status */
563 dprintk("%s() TUNERLOCKING\n", __func__);
564 if (fe->ops.tuner_ops.get_status) {
565 if (fe->ops.i2c_gate_ctrl)
566 fe->ops.i2c_gate_ctrl(fe, 1);
568 fe->ops.tuner_ops.get_status(fe, &tuner_status);
570 if (fe->ops.i2c_gate_ctrl)
571 fe->ops.i2c_gate_ctrl(fe, 0);
574 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
578 dprintk("%s() status 0x%08x\n", __func__, *status);
583 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
585 struct au8522_state *state = fe->demodulator_priv;
588 dprintk("%s()\n", __func__);
590 if (state->current_modulation == QAM_256)
591 ret = au8522_qam256_mse2snr_lookup(
592 au8522_readreg(state, 0x4522), snr);
593 else if (state->current_modulation == QAM_64)
594 ret = au8522_qam64_mse2snr_lookup(
595 au8522_readreg(state, 0x4522), snr);
597 ret = au8522_vsb_mse2snr_lookup(
598 au8522_readreg(state, 0x4311), snr);
603 static int au8522_read_signal_strength(struct dvb_frontend *fe,
604 u16 *signal_strength)
606 return au8522_read_snr(fe, signal_strength);
609 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
611 struct au8522_state *state = fe->demodulator_priv;
613 if (state->current_modulation == VSB_8)
614 *ucblocks = au8522_readreg(state, 0x4087);
616 *ucblocks = au8522_readreg(state, 0x4543);
621 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
623 return au8522_read_ucblocks(fe, ber);
626 static int au8522_get_frontend(struct dvb_frontend *fe,
627 struct dvb_frontend_parameters *p)
629 struct au8522_state *state = fe->demodulator_priv;
631 p->frequency = state->current_frequency;
632 p->u.vsb.modulation = state->current_modulation;
637 static int au8522_get_tune_settings(struct dvb_frontend *fe,
638 struct dvb_frontend_tune_settings *tune)
640 tune->min_delay_ms = 1000;
644 static void au8522_release(struct dvb_frontend *fe)
646 struct au8522_state *state = fe->demodulator_priv;
650 static struct dvb_frontend_ops au8522_ops;
652 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
653 struct i2c_adapter *i2c)
655 struct au8522_state *state = NULL;
657 /* allocate memory for the internal state */
658 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
662 /* setup the state */
663 state->config = config;
665 /* create dvb_frontend */
666 memcpy(&state->frontend.ops, &au8522_ops,
667 sizeof(struct dvb_frontend_ops));
668 state->frontend.demodulator_priv = state;
670 if (au8522_init(&state->frontend) != 0) {
671 printk(KERN_ERR "%s: Failed to initialize correctly\n",
676 /* Note: Leaving the I2C gate open here. */
677 au8522_i2c_gate_ctrl(&state->frontend, 1);
679 return &state->frontend;
686 static struct dvb_frontend_ops au8522_ops = {
689 .name = "Auvitek AU8522 QAM/8VSB Frontend",
691 .frequency_min = 54000000,
692 .frequency_max = 858000000,
693 .frequency_stepsize = 62500,
694 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
698 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
699 .set_frontend = au8522_set_frontend,
700 .get_frontend = au8522_get_frontend,
701 .get_tune_settings = au8522_get_tune_settings,
702 .read_status = au8522_read_status,
703 .read_ber = au8522_read_ber,
704 .read_signal_strength = au8522_read_signal_strength,
705 .read_snr = au8522_read_snr,
706 .read_ucblocks = au8522_read_ucblocks,
707 .release = au8522_release,
710 module_param(debug, int, 0644);
711 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
713 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
714 MODULE_AUTHOR("Steven Toth");
715 MODULE_LICENSE("GPL");
717 EXPORT_SYMBOL(au8522_attach);