2 Auvitek AU8522 QAM/8VSB demodulator driver
4 Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
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;
44 unsigned int led_state;
49 #define dprintk(arg...) do { \
54 /* 16 bit registers, 8 bit values */
55 static int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
58 u8 buf [] = { reg >> 8, reg & 0xff, data };
60 struct i2c_msg msg = { .addr = state->config->demod_address,
61 .flags = 0, .buf = buf, .len = 3 };
63 ret = i2c_transfer(state->i2c, &msg, 1);
66 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
67 "ret == %i)\n", __func__, reg, data, ret);
69 return (ret != 1) ? -1 : 0;
72 static u8 au8522_readreg(struct au8522_state *state, u16 reg)
75 u8 b0 [] = { reg >> 8, reg & 0xff };
78 struct i2c_msg msg [] = {
79 { .addr = state->config->demod_address, .flags = 0,
80 .buf = b0, .len = 2 },
81 { .addr = state->config->demod_address, .flags = I2C_M_RD,
82 .buf = b1, .len = 1 } };
84 ret = i2c_transfer(state->i2c, msg, 2);
87 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
92 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
94 struct au8522_state *state = fe->demodulator_priv;
96 dprintk("%s(%d)\n", __func__, enable);
99 return au8522_writereg(state, 0x106, 1);
101 return au8522_writereg(state, 0x106, 0);
109 /* VSB SNR lookup table */
110 static struct mse2snr_tab vsb_mse2snr_tab[] = {
143 /* QAM64 SNR lookup table */
144 static struct mse2snr_tab qam64_mse2snr_tab[] = {
224 /* QAM256 SNR lookup table */
225 static struct mse2snr_tab qam256_mse2snr_tab[] = {
292 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
295 int i, ret = -EINVAL;
296 dprintk("%s()\n", __func__);
298 for (i = 0; i < sz; i++) {
299 if (mse < tab[i].val) {
305 dprintk("%s() snr=%d\n", __func__, *snr);
309 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
311 struct au8522_state *state = fe->demodulator_priv;
316 case AU8522_IF_3_25MHZ:
335 dprintk("%s() IF Frequency not supported\n", __func__);
338 dprintk("%s() %s MHz\n", __func__, ifmhz);
339 au8522_writereg(state, 0x80b5, r0b5);
340 au8522_writereg(state, 0x80b6, r0b6);
341 au8522_writereg(state, 0x80b7, r0b7);
346 /* VSB Modulation table */
380 /* QAM Modulation table */
459 static int au8522_enable_modulation(struct dvb_frontend *fe,
462 struct au8522_state *state = fe->demodulator_priv;
465 dprintk("%s(0x%08x)\n", __func__, m);
469 dprintk("%s() VSB_8\n", __func__);
470 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
471 au8522_writereg(state,
473 VSB_mod_tab[i].data);
474 au8522_set_if(fe, state->config->vsb_if);
478 dprintk("%s() QAM 64/256\n", __func__);
479 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
480 au8522_writereg(state,
482 QAM_mod_tab[i].data);
483 au8522_set_if(fe, state->config->qam_if);
486 dprintk("%s() Invalid modulation\n", __func__);
490 state->current_modulation = m;
495 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
496 static int au8522_set_frontend(struct dvb_frontend *fe,
497 struct dvb_frontend_parameters *p)
499 struct au8522_state *state = fe->demodulator_priv;
502 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
504 if ((state->current_frequency == p->frequency) &&
505 (state->current_modulation == p->u.vsb.modulation))
508 au8522_enable_modulation(fe, p->u.vsb.modulation);
510 /* Allow the demod to settle */
513 if (fe->ops.tuner_ops.set_params) {
514 if (fe->ops.i2c_gate_ctrl)
515 fe->ops.i2c_gate_ctrl(fe, 1);
516 ret = fe->ops.tuner_ops.set_params(fe, p);
517 if (fe->ops.i2c_gate_ctrl)
518 fe->ops.i2c_gate_ctrl(fe, 0);
524 state->current_frequency = p->frequency;
529 /* Reset the demod hardware and reset all of the configuration registers
530 to a default state. */
531 static int au8522_init(struct dvb_frontend *fe)
533 struct au8522_state *state = fe->demodulator_priv;
534 dprintk("%s()\n", __func__);
536 au8522_writereg(state, 0xa4, 1 << 5);
538 au8522_i2c_gate_ctrl(fe, 1);
543 static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
545 struct au8522_led_config *led_config = state->config->led_cfg;
548 /* bail out if we cant control an LED */
549 if (!led_config || !led_config->gpio_output ||
550 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
553 val = au8522_readreg(state, 0x4000 |
554 (led_config->gpio_output & ~0xc000));
556 /* enable GPIO output */
557 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
558 val |= (led_config->gpio_output_enable & 0xff);
560 /* disable GPIO output */
561 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
562 val |= (led_config->gpio_output_disable & 0xff);
564 return au8522_writereg(state, 0x8000 |
565 (led_config->gpio_output & ~0xc000), val);
569 * led = 1 | signal ok
570 * led = 2 | signal strong
571 * led < 0 | only light led if leds are currently off
573 static int au8522_led_ctrl(struct au8522_state *state, int led)
575 struct au8522_led_config *led_config = state->config->led_cfg;
578 /* bail out if we cant control an LED */
579 if (!led_config || !led_config->gpio_leds ||
580 !led_config->num_led_states || !led_config->led_states)
584 /* if LED is already lit, then leave it as-is */
585 if (state->led_state)
591 /* toggle LED if changing state */
592 if (state->led_state != led) {
595 dprintk("%s: %d\n", __func__, led);
597 au8522_led_gpio_enable(state, 1);
599 val = au8522_readreg(state, 0x4000 |
600 (led_config->gpio_leds & ~0xc000));
602 /* start with all leds off */
603 for (i = 0; i < led_config->num_led_states; i++)
604 val &= ~led_config->led_states[i];
606 /* set selected LED state */
607 if (led < led_config->num_led_states)
608 val |= led_config->led_states[led];
609 else if (led_config->num_led_states)
611 led_config->led_states[led_config->num_led_states - 1];
613 ret = au8522_writereg(state, 0x8000 |
614 (led_config->gpio_leds & ~0xc000), val);
618 state->led_state = led;
621 au8522_led_gpio_enable(state, 0);
627 static int au8522_sleep(struct dvb_frontend *fe)
629 struct au8522_state *state = fe->demodulator_priv;
630 dprintk("%s()\n", __func__);
633 au8522_led_ctrl(state, 0);
635 state->current_frequency = 0;
640 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
642 struct au8522_state *state = fe->demodulator_priv;
644 u32 tuner_status = 0;
648 if (state->current_modulation == VSB_8) {
649 dprintk("%s() Checking VSB_8\n", __func__);
650 reg = au8522_readreg(state, 0x4088);
651 if ((reg & 0x03) == 0x03)
652 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
654 dprintk("%s() Checking QAM\n", __func__);
655 reg = au8522_readreg(state, 0x4541);
657 *status |= FE_HAS_VITERBI;
659 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
662 switch (state->config->status_mode) {
663 case AU8522_DEMODLOCKING:
664 dprintk("%s() DEMODLOCKING\n", __func__);
665 if (*status & FE_HAS_VITERBI)
666 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
668 case AU8522_TUNERLOCKING:
669 /* Get the tuner status */
670 dprintk("%s() TUNERLOCKING\n", __func__);
671 if (fe->ops.tuner_ops.get_status) {
672 if (fe->ops.i2c_gate_ctrl)
673 fe->ops.i2c_gate_ctrl(fe, 1);
675 fe->ops.tuner_ops.get_status(fe, &tuner_status);
677 if (fe->ops.i2c_gate_ctrl)
678 fe->ops.i2c_gate_ctrl(fe, 0);
681 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
684 state->fe_status = *status;
686 if (*status & FE_HAS_LOCK)
687 /* turn on LED, if it isn't on already */
688 au8522_led_ctrl(state, -1);
691 au8522_led_ctrl(state, 0);
693 dprintk("%s() status 0x%08x\n", __func__, *status);
698 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
700 struct au8522_led_config *led_config = state->config->led_cfg;
704 /* bail out if we cant control an LED */
708 if (0 == (state->fe_status & FE_HAS_LOCK))
709 return au8522_led_ctrl(state, 0);
710 else if (state->current_modulation == QAM_256)
711 strong = led_config->qam256_strong;
712 else if (state->current_modulation == QAM_64)
713 strong = led_config->qam64_strong;
714 else /* (state->current_modulation == VSB_8) */
715 strong = led_config->vsb8_strong;
722 if ((state->led_state) &&
723 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
724 /* snr didn't change enough to bother
725 * changing the color of the led */
728 return au8522_led_ctrl(state, led);
731 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
733 struct au8522_state *state = fe->demodulator_priv;
736 dprintk("%s()\n", __func__);
738 if (state->current_modulation == QAM_256)
739 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
740 ARRAY_SIZE(qam256_mse2snr_tab),
741 au8522_readreg(state, 0x4522),
743 else if (state->current_modulation == QAM_64)
744 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
745 ARRAY_SIZE(qam64_mse2snr_tab),
746 au8522_readreg(state, 0x4522),
749 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
750 ARRAY_SIZE(vsb_mse2snr_tab),
751 au8522_readreg(state, 0x4311),
754 if (state->config->led_cfg)
755 au8522_led_status(state, snr);
760 static int au8522_read_signal_strength(struct dvb_frontend *fe,
761 u16 *signal_strength)
763 return au8522_read_snr(fe, signal_strength);
766 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
768 struct au8522_state *state = fe->demodulator_priv;
770 if (state->current_modulation == VSB_8)
771 *ucblocks = au8522_readreg(state, 0x4087);
773 *ucblocks = au8522_readreg(state, 0x4543);
778 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
780 return au8522_read_ucblocks(fe, ber);
783 static int au8522_get_frontend(struct dvb_frontend *fe,
784 struct dvb_frontend_parameters *p)
786 struct au8522_state *state = fe->demodulator_priv;
788 p->frequency = state->current_frequency;
789 p->u.vsb.modulation = state->current_modulation;
794 static int au8522_get_tune_settings(struct dvb_frontend *fe,
795 struct dvb_frontend_tune_settings *tune)
797 tune->min_delay_ms = 1000;
801 static void au8522_release(struct dvb_frontend *fe)
803 struct au8522_state *state = fe->demodulator_priv;
807 static struct dvb_frontend_ops au8522_ops;
809 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
810 struct i2c_adapter *i2c)
812 struct au8522_state *state = NULL;
814 /* allocate memory for the internal state */
815 state = kmalloc(sizeof(struct au8522_state), GFP_KERNEL);
819 /* setup the state */
820 state->config = config;
822 /* create dvb_frontend */
823 memcpy(&state->frontend.ops, &au8522_ops,
824 sizeof(struct dvb_frontend_ops));
825 state->frontend.demodulator_priv = state;
827 if (au8522_init(&state->frontend) != 0) {
828 printk(KERN_ERR "%s: Failed to initialize correctly\n",
833 /* Note: Leaving the I2C gate open here. */
834 au8522_i2c_gate_ctrl(&state->frontend, 1);
836 return &state->frontend;
842 EXPORT_SYMBOL(au8522_attach);
844 static struct dvb_frontend_ops au8522_ops = {
847 .name = "Auvitek AU8522 QAM/8VSB Frontend",
849 .frequency_min = 54000000,
850 .frequency_max = 858000000,
851 .frequency_stepsize = 62500,
852 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
856 .sleep = au8522_sleep,
857 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
858 .set_frontend = au8522_set_frontend,
859 .get_frontend = au8522_get_frontend,
860 .get_tune_settings = au8522_get_tune_settings,
861 .read_status = au8522_read_status,
862 .read_ber = au8522_read_ber,
863 .read_signal_strength = au8522_read_signal_strength,
864 .read_snr = au8522_read_snr,
865 .read_ucblocks = au8522_read_ucblocks,
866 .release = au8522_release,
869 module_param(debug, int, 0644);
870 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
872 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
873 MODULE_AUTHOR("Steven Toth");
874 MODULE_LICENSE("GPL");