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"
30 #include "au8522_priv.h"
34 /* Despite the name "hybrid_tuner", the framework works just as well for
35 hybrid demodulators as well... */
36 static LIST_HEAD(hybrid_tuner_instance_list);
37 static DEFINE_MUTEX(au8522_list_mutex);
39 #define dprintk(arg...)\
44 /* 16 bit registers, 8 bit values */
45 int au8522_writereg(struct au8522_state *state, u16 reg, u8 data)
48 u8 buf[] = { (reg >> 8) | 0x80, reg & 0xff, data };
50 struct i2c_msg msg = { .addr = state->config->demod_address,
51 .flags = 0, .buf = buf, .len = 3 };
53 ret = i2c_transfer(state->i2c, &msg, 1);
56 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
57 "ret == %i)\n", __func__, reg, data, ret);
59 return (ret != 1) ? -1 : 0;
62 u8 au8522_readreg(struct au8522_state *state, u16 reg)
65 u8 b0[] = { (reg >> 8) | 0x40, reg & 0xff };
68 struct i2c_msg msg[] = {
69 { .addr = state->config->demod_address, .flags = 0,
70 .buf = b0, .len = 2 },
71 { .addr = state->config->demod_address, .flags = I2C_M_RD,
72 .buf = b1, .len = 1 } };
74 ret = i2c_transfer(state->i2c, msg, 2);
77 printk(KERN_ERR "%s: readreg error (ret == %i)\n",
82 static int au8522_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
84 struct au8522_state *state = fe->demodulator_priv;
86 dprintk("%s(%d)\n", __func__, enable);
89 return au8522_writereg(state, 0x106, 1);
91 return au8522_writereg(state, 0x106, 0);
99 /* VSB SNR lookup table */
100 static struct mse2snr_tab vsb_mse2snr_tab[] = {
133 /* QAM64 SNR lookup table */
134 static struct mse2snr_tab qam64_mse2snr_tab[] = {
214 /* QAM256 SNR lookup table */
215 static struct mse2snr_tab qam256_mse2snr_tab[] = {
282 static int au8522_mse2snr_lookup(struct mse2snr_tab *tab, int sz, int mse,
285 int i, ret = -EINVAL;
286 dprintk("%s()\n", __func__);
288 for (i = 0; i < sz; i++) {
289 if (mse < tab[i].val) {
295 dprintk("%s() snr=%d\n", __func__, *snr);
299 static int au8522_set_if(struct dvb_frontend *fe, enum au8522_if_freq if_freq)
301 struct au8522_state *state = fe->demodulator_priv;
306 case AU8522_IF_3_25MHZ:
325 dprintk("%s() IF Frequency not supported\n", __func__);
328 dprintk("%s() %s MHz\n", __func__, ifmhz);
329 au8522_writereg(state, 0x80b5, r0b5);
330 au8522_writereg(state, 0x80b6, r0b6);
331 au8522_writereg(state, 0x80b7, r0b7);
336 /* VSB Modulation table */
370 /* QAM64 Modulation table */
374 } QAM64_mod_tab[] = {
449 /* QAM256 Modulation table */
453 } QAM256_mod_tab[] = {
528 static int au8522_enable_modulation(struct dvb_frontend *fe,
531 struct au8522_state *state = fe->demodulator_priv;
534 dprintk("%s(0x%08x)\n", __func__, m);
538 dprintk("%s() VSB_8\n", __func__);
539 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
540 au8522_writereg(state,
542 VSB_mod_tab[i].data);
543 au8522_set_if(fe, state->config->vsb_if);
546 dprintk("%s() QAM 64\n", __func__);
547 for (i = 0; i < ARRAY_SIZE(QAM64_mod_tab); i++)
548 au8522_writereg(state,
549 QAM64_mod_tab[i].reg,
550 QAM64_mod_tab[i].data);
551 au8522_set_if(fe, state->config->qam_if);
554 dprintk("%s() QAM 256\n", __func__);
555 for (i = 0; i < ARRAY_SIZE(QAM256_mod_tab); i++)
556 au8522_writereg(state,
557 QAM256_mod_tab[i].reg,
558 QAM256_mod_tab[i].data);
559 au8522_set_if(fe, state->config->qam_if);
562 dprintk("%s() Invalid modulation\n", __func__);
566 state->current_modulation = m;
571 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
572 static int au8522_set_frontend(struct dvb_frontend *fe,
573 struct dvb_frontend_parameters *p)
575 struct au8522_state *state = fe->demodulator_priv;
578 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
580 if ((state->current_frequency == p->frequency) &&
581 (state->current_modulation == p->u.vsb.modulation))
584 au8522_enable_modulation(fe, p->u.vsb.modulation);
586 /* Allow the demod to settle */
589 if (fe->ops.tuner_ops.set_params) {
590 if (fe->ops.i2c_gate_ctrl)
591 fe->ops.i2c_gate_ctrl(fe, 1);
592 ret = fe->ops.tuner_ops.set_params(fe, p);
593 if (fe->ops.i2c_gate_ctrl)
594 fe->ops.i2c_gate_ctrl(fe, 0);
600 state->current_frequency = p->frequency;
605 /* Reset the demod hardware and reset all of the configuration registers
606 to a default state. */
607 int au8522_init(struct dvb_frontend *fe)
609 struct au8522_state *state = fe->demodulator_priv;
610 dprintk("%s()\n", __func__);
612 au8522_writereg(state, 0xa4, 1 << 5);
614 au8522_i2c_gate_ctrl(fe, 1);
619 static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
621 struct au8522_led_config *led_config = state->config->led_cfg;
624 /* bail out if we cant control an LED */
625 if (!led_config || !led_config->gpio_output ||
626 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
629 val = au8522_readreg(state, 0x4000 |
630 (led_config->gpio_output & ~0xc000));
632 /* enable GPIO output */
633 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
634 val |= (led_config->gpio_output_enable & 0xff);
636 /* disable GPIO output */
637 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
638 val |= (led_config->gpio_output_disable & 0xff);
640 return au8522_writereg(state, 0x8000 |
641 (led_config->gpio_output & ~0xc000), val);
645 * led = 1 | signal ok
646 * led = 2 | signal strong
647 * led < 0 | only light led if leds are currently off
649 static int au8522_led_ctrl(struct au8522_state *state, int led)
651 struct au8522_led_config *led_config = state->config->led_cfg;
654 /* bail out if we cant control an LED */
655 if (!led_config || !led_config->gpio_leds ||
656 !led_config->num_led_states || !led_config->led_states)
660 /* if LED is already lit, then leave it as-is */
661 if (state->led_state)
667 /* toggle LED if changing state */
668 if (state->led_state != led) {
671 dprintk("%s: %d\n", __func__, led);
673 au8522_led_gpio_enable(state, 1);
675 val = au8522_readreg(state, 0x4000 |
676 (led_config->gpio_leds & ~0xc000));
678 /* start with all leds off */
679 for (i = 0; i < led_config->num_led_states; i++)
680 val &= ~led_config->led_states[i];
682 /* set selected LED state */
683 if (led < led_config->num_led_states)
684 val |= led_config->led_states[led];
685 else if (led_config->num_led_states)
687 led_config->led_states[led_config->num_led_states - 1];
689 ret = au8522_writereg(state, 0x8000 |
690 (led_config->gpio_leds & ~0xc000), val);
694 state->led_state = led;
697 au8522_led_gpio_enable(state, 0);
703 int au8522_sleep(struct dvb_frontend *fe)
705 struct au8522_state *state = fe->demodulator_priv;
706 dprintk("%s()\n", __func__);
709 au8522_led_ctrl(state, 0);
711 /* Power down the chip */
712 au8522_writereg(state, 0xa4, 1 << 5);
714 state->current_frequency = 0;
719 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
721 struct au8522_state *state = fe->demodulator_priv;
723 u32 tuner_status = 0;
727 if (state->current_modulation == VSB_8) {
728 dprintk("%s() Checking VSB_8\n", __func__);
729 reg = au8522_readreg(state, 0x4088);
730 if ((reg & 0x03) == 0x03)
731 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
733 dprintk("%s() Checking QAM\n", __func__);
734 reg = au8522_readreg(state, 0x4541);
736 *status |= FE_HAS_VITERBI;
738 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
741 switch (state->config->status_mode) {
742 case AU8522_DEMODLOCKING:
743 dprintk("%s() DEMODLOCKING\n", __func__);
744 if (*status & FE_HAS_VITERBI)
745 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
747 case AU8522_TUNERLOCKING:
748 /* Get the tuner status */
749 dprintk("%s() TUNERLOCKING\n", __func__);
750 if (fe->ops.tuner_ops.get_status) {
751 if (fe->ops.i2c_gate_ctrl)
752 fe->ops.i2c_gate_ctrl(fe, 1);
754 fe->ops.tuner_ops.get_status(fe, &tuner_status);
756 if (fe->ops.i2c_gate_ctrl)
757 fe->ops.i2c_gate_ctrl(fe, 0);
760 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
763 state->fe_status = *status;
765 if (*status & FE_HAS_LOCK)
766 /* turn on LED, if it isn't on already */
767 au8522_led_ctrl(state, -1);
770 au8522_led_ctrl(state, 0);
772 dprintk("%s() status 0x%08x\n", __func__, *status);
777 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
779 struct au8522_led_config *led_config = state->config->led_cfg;
783 /* bail out if we cant control an LED */
787 if (0 == (state->fe_status & FE_HAS_LOCK))
788 return au8522_led_ctrl(state, 0);
789 else if (state->current_modulation == QAM_256)
790 strong = led_config->qam256_strong;
791 else if (state->current_modulation == QAM_64)
792 strong = led_config->qam64_strong;
793 else /* (state->current_modulation == VSB_8) */
794 strong = led_config->vsb8_strong;
801 if ((state->led_state) &&
802 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
803 /* snr didn't change enough to bother
804 * changing the color of the led */
807 return au8522_led_ctrl(state, led);
810 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
812 struct au8522_state *state = fe->demodulator_priv;
815 dprintk("%s()\n", __func__);
817 if (state->current_modulation == QAM_256)
818 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
819 ARRAY_SIZE(qam256_mse2snr_tab),
820 au8522_readreg(state, 0x4522),
822 else if (state->current_modulation == QAM_64)
823 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
824 ARRAY_SIZE(qam64_mse2snr_tab),
825 au8522_readreg(state, 0x4522),
828 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
829 ARRAY_SIZE(vsb_mse2snr_tab),
830 au8522_readreg(state, 0x4311),
833 if (state->config->led_cfg)
834 au8522_led_status(state, snr);
839 static int au8522_read_signal_strength(struct dvb_frontend *fe,
840 u16 *signal_strength)
842 return au8522_read_snr(fe, signal_strength);
845 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
847 struct au8522_state *state = fe->demodulator_priv;
849 if (state->current_modulation == VSB_8)
850 *ucblocks = au8522_readreg(state, 0x4087);
852 *ucblocks = au8522_readreg(state, 0x4543);
857 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
859 return au8522_read_ucblocks(fe, ber);
862 static int au8522_get_frontend(struct dvb_frontend *fe,
863 struct dvb_frontend_parameters *p)
865 struct au8522_state *state = fe->demodulator_priv;
867 p->frequency = state->current_frequency;
868 p->u.vsb.modulation = state->current_modulation;
873 static int au8522_get_tune_settings(struct dvb_frontend *fe,
874 struct dvb_frontend_tune_settings *tune)
876 tune->min_delay_ms = 1000;
880 static struct dvb_frontend_ops au8522_ops;
882 int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
887 mutex_lock(&au8522_list_mutex);
888 ret = hybrid_tuner_request_state(struct au8522_state, (*state),
889 hybrid_tuner_instance_list,
890 i2c, client_address, "au8522");
891 mutex_unlock(&au8522_list_mutex);
896 void au8522_release_state(struct au8522_state *state)
898 mutex_lock(&au8522_list_mutex);
900 hybrid_tuner_release_state(state);
901 mutex_unlock(&au8522_list_mutex);
905 static void au8522_release(struct dvb_frontend *fe)
907 struct au8522_state *state = fe->demodulator_priv;
908 au8522_release_state(state);
911 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
912 struct i2c_adapter *i2c)
914 struct au8522_state *state = NULL;
917 /* allocate memory for the internal state */
918 instance = au8522_get_state(&state, i2c, config->demod_address);
921 dprintk("%s state allocation failed\n", __func__);
924 /* new demod instance */
925 dprintk("%s using new instance\n", __func__);
928 /* existing demod instance */
929 dprintk("%s using existing instance\n", __func__);
933 /* setup the state */
934 state->config = config;
936 /* create dvb_frontend */
937 memcpy(&state->frontend.ops, &au8522_ops,
938 sizeof(struct dvb_frontend_ops));
939 state->frontend.demodulator_priv = state;
941 if (au8522_init(&state->frontend) != 0) {
942 printk(KERN_ERR "%s: Failed to initialize correctly\n",
947 /* Note: Leaving the I2C gate open here. */
948 au8522_i2c_gate_ctrl(&state->frontend, 1);
950 return &state->frontend;
953 au8522_release_state(state);
956 EXPORT_SYMBOL(au8522_attach);
958 static struct dvb_frontend_ops au8522_ops = {
961 .name = "Auvitek AU8522 QAM/8VSB Frontend",
963 .frequency_min = 54000000,
964 .frequency_max = 858000000,
965 .frequency_stepsize = 62500,
966 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
970 .sleep = au8522_sleep,
971 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
972 .set_frontend = au8522_set_frontend,
973 .get_frontend = au8522_get_frontend,
974 .get_tune_settings = au8522_get_tune_settings,
975 .read_status = au8522_read_status,
976 .read_ber = au8522_read_ber,
977 .read_signal_strength = au8522_read_signal_strength,
978 .read_snr = au8522_read_snr,
979 .read_ucblocks = au8522_read_ucblocks,
980 .release = au8522_release,
983 module_param(debug, int, 0644);
984 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
986 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
987 MODULE_AUTHOR("Steven Toth");
988 MODULE_LICENSE("GPL");