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 /* QAM Modulation table */
449 static int au8522_enable_modulation(struct dvb_frontend *fe,
452 struct au8522_state *state = fe->demodulator_priv;
455 dprintk("%s(0x%08x)\n", __func__, m);
459 dprintk("%s() VSB_8\n", __func__);
460 for (i = 0; i < ARRAY_SIZE(VSB_mod_tab); i++)
461 au8522_writereg(state,
463 VSB_mod_tab[i].data);
464 au8522_set_if(fe, state->config->vsb_if);
468 dprintk("%s() QAM 64/256\n", __func__);
469 for (i = 0; i < ARRAY_SIZE(QAM_mod_tab); i++)
470 au8522_writereg(state,
472 QAM_mod_tab[i].data);
473 au8522_set_if(fe, state->config->qam_if);
476 dprintk("%s() Invalid modulation\n", __func__);
480 state->current_modulation = m;
485 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
486 static int au8522_set_frontend(struct dvb_frontend *fe,
487 struct dvb_frontend_parameters *p)
489 struct au8522_state *state = fe->demodulator_priv;
492 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
494 if ((state->current_frequency == p->frequency) &&
495 (state->current_modulation == p->u.vsb.modulation))
498 au8522_enable_modulation(fe, p->u.vsb.modulation);
500 /* Allow the demod to settle */
503 if (fe->ops.tuner_ops.set_params) {
504 if (fe->ops.i2c_gate_ctrl)
505 fe->ops.i2c_gate_ctrl(fe, 1);
506 ret = fe->ops.tuner_ops.set_params(fe, p);
507 if (fe->ops.i2c_gate_ctrl)
508 fe->ops.i2c_gate_ctrl(fe, 0);
514 state->current_frequency = p->frequency;
519 /* Reset the demod hardware and reset all of the configuration registers
520 to a default state. */
521 int au8522_init(struct dvb_frontend *fe)
523 struct au8522_state *state = fe->demodulator_priv;
524 dprintk("%s()\n", __func__);
526 au8522_writereg(state, 0xa4, 1 << 5);
528 au8522_i2c_gate_ctrl(fe, 1);
533 static int au8522_led_gpio_enable(struct au8522_state *state, int onoff)
535 struct au8522_led_config *led_config = state->config->led_cfg;
538 /* bail out if we cant control an LED */
539 if (!led_config || !led_config->gpio_output ||
540 !led_config->gpio_output_enable || !led_config->gpio_output_disable)
543 val = au8522_readreg(state, 0x4000 |
544 (led_config->gpio_output & ~0xc000));
546 /* enable GPIO output */
547 val &= ~((led_config->gpio_output_enable >> 8) & 0xff);
548 val |= (led_config->gpio_output_enable & 0xff);
550 /* disable GPIO output */
551 val &= ~((led_config->gpio_output_disable >> 8) & 0xff);
552 val |= (led_config->gpio_output_disable & 0xff);
554 return au8522_writereg(state, 0x8000 |
555 (led_config->gpio_output & ~0xc000), val);
559 * led = 1 | signal ok
560 * led = 2 | signal strong
561 * led < 0 | only light led if leds are currently off
563 static int au8522_led_ctrl(struct au8522_state *state, int led)
565 struct au8522_led_config *led_config = state->config->led_cfg;
568 /* bail out if we cant control an LED */
569 if (!led_config || !led_config->gpio_leds ||
570 !led_config->num_led_states || !led_config->led_states)
574 /* if LED is already lit, then leave it as-is */
575 if (state->led_state)
581 /* toggle LED if changing state */
582 if (state->led_state != led) {
585 dprintk("%s: %d\n", __func__, led);
587 au8522_led_gpio_enable(state, 1);
589 val = au8522_readreg(state, 0x4000 |
590 (led_config->gpio_leds & ~0xc000));
592 /* start with all leds off */
593 for (i = 0; i < led_config->num_led_states; i++)
594 val &= ~led_config->led_states[i];
596 /* set selected LED state */
597 if (led < led_config->num_led_states)
598 val |= led_config->led_states[led];
599 else if (led_config->num_led_states)
601 led_config->led_states[led_config->num_led_states - 1];
603 ret = au8522_writereg(state, 0x8000 |
604 (led_config->gpio_leds & ~0xc000), val);
608 state->led_state = led;
611 au8522_led_gpio_enable(state, 0);
617 int au8522_sleep(struct dvb_frontend *fe)
619 struct au8522_state *state = fe->demodulator_priv;
620 dprintk("%s()\n", __func__);
623 au8522_led_ctrl(state, 0);
625 /* Power down the chip */
626 au8522_writereg(state, 0xa4, 1 << 5);
628 state->current_frequency = 0;
633 static int au8522_read_status(struct dvb_frontend *fe, fe_status_t *status)
635 struct au8522_state *state = fe->demodulator_priv;
637 u32 tuner_status = 0;
641 if (state->current_modulation == VSB_8) {
642 dprintk("%s() Checking VSB_8\n", __func__);
643 reg = au8522_readreg(state, 0x4088);
644 if ((reg & 0x03) == 0x03)
645 *status |= FE_HAS_LOCK | FE_HAS_SYNC | FE_HAS_VITERBI;
647 dprintk("%s() Checking QAM\n", __func__);
648 reg = au8522_readreg(state, 0x4541);
650 *status |= FE_HAS_VITERBI;
652 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
655 switch (state->config->status_mode) {
656 case AU8522_DEMODLOCKING:
657 dprintk("%s() DEMODLOCKING\n", __func__);
658 if (*status & FE_HAS_VITERBI)
659 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
661 case AU8522_TUNERLOCKING:
662 /* Get the tuner status */
663 dprintk("%s() TUNERLOCKING\n", __func__);
664 if (fe->ops.tuner_ops.get_status) {
665 if (fe->ops.i2c_gate_ctrl)
666 fe->ops.i2c_gate_ctrl(fe, 1);
668 fe->ops.tuner_ops.get_status(fe, &tuner_status);
670 if (fe->ops.i2c_gate_ctrl)
671 fe->ops.i2c_gate_ctrl(fe, 0);
674 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
677 state->fe_status = *status;
679 if (*status & FE_HAS_LOCK)
680 /* turn on LED, if it isn't on already */
681 au8522_led_ctrl(state, -1);
684 au8522_led_ctrl(state, 0);
686 dprintk("%s() status 0x%08x\n", __func__, *status);
691 static int au8522_led_status(struct au8522_state *state, const u16 *snr)
693 struct au8522_led_config *led_config = state->config->led_cfg;
697 /* bail out if we cant control an LED */
701 if (0 == (state->fe_status & FE_HAS_LOCK))
702 return au8522_led_ctrl(state, 0);
703 else if (state->current_modulation == QAM_256)
704 strong = led_config->qam256_strong;
705 else if (state->current_modulation == QAM_64)
706 strong = led_config->qam64_strong;
707 else /* (state->current_modulation == VSB_8) */
708 strong = led_config->vsb8_strong;
715 if ((state->led_state) &&
716 (((strong < *snr) ? (*snr - strong) : (strong - *snr)) <= 10))
717 /* snr didn't change enough to bother
718 * changing the color of the led */
721 return au8522_led_ctrl(state, led);
724 static int au8522_read_snr(struct dvb_frontend *fe, u16 *snr)
726 struct au8522_state *state = fe->demodulator_priv;
729 dprintk("%s()\n", __func__);
731 if (state->current_modulation == QAM_256)
732 ret = au8522_mse2snr_lookup(qam256_mse2snr_tab,
733 ARRAY_SIZE(qam256_mse2snr_tab),
734 au8522_readreg(state, 0x4522),
736 else if (state->current_modulation == QAM_64)
737 ret = au8522_mse2snr_lookup(qam64_mse2snr_tab,
738 ARRAY_SIZE(qam64_mse2snr_tab),
739 au8522_readreg(state, 0x4522),
742 ret = au8522_mse2snr_lookup(vsb_mse2snr_tab,
743 ARRAY_SIZE(vsb_mse2snr_tab),
744 au8522_readreg(state, 0x4311),
747 if (state->config->led_cfg)
748 au8522_led_status(state, snr);
753 static int au8522_read_signal_strength(struct dvb_frontend *fe,
754 u16 *signal_strength)
756 return au8522_read_snr(fe, signal_strength);
759 static int au8522_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
761 struct au8522_state *state = fe->demodulator_priv;
763 if (state->current_modulation == VSB_8)
764 *ucblocks = au8522_readreg(state, 0x4087);
766 *ucblocks = au8522_readreg(state, 0x4543);
771 static int au8522_read_ber(struct dvb_frontend *fe, u32 *ber)
773 return au8522_read_ucblocks(fe, ber);
776 static int au8522_get_frontend(struct dvb_frontend *fe,
777 struct dvb_frontend_parameters *p)
779 struct au8522_state *state = fe->demodulator_priv;
781 p->frequency = state->current_frequency;
782 p->u.vsb.modulation = state->current_modulation;
787 static int au8522_get_tune_settings(struct dvb_frontend *fe,
788 struct dvb_frontend_tune_settings *tune)
790 tune->min_delay_ms = 1000;
794 static struct dvb_frontend_ops au8522_ops;
796 int au8522_get_state(struct au8522_state **state, struct i2c_adapter *i2c,
801 mutex_lock(&au8522_list_mutex);
802 ret = hybrid_tuner_request_state(struct au8522_state, (*state),
803 hybrid_tuner_instance_list,
804 i2c, client_address, "au8522");
805 mutex_unlock(&au8522_list_mutex);
810 void au8522_release_state(struct au8522_state *state)
812 mutex_lock(&au8522_list_mutex);
814 hybrid_tuner_release_state(state);
815 mutex_unlock(&au8522_list_mutex);
819 static void au8522_release(struct dvb_frontend *fe)
821 struct au8522_state *state = fe->demodulator_priv;
822 au8522_release_state(state);
825 struct dvb_frontend *au8522_attach(const struct au8522_config *config,
826 struct i2c_adapter *i2c)
828 struct au8522_state *state = NULL;
831 /* allocate memory for the internal state */
832 instance = au8522_get_state(&state, i2c, config->demod_address);
835 dprintk("%s state allocation failed\n", __func__);
838 /* new demod instance */
839 dprintk("%s using new instance\n", __func__);
842 /* existing demod instance */
843 dprintk("%s using existing instance\n", __func__);
847 /* setup the state */
848 state->config = config;
850 /* create dvb_frontend */
851 memcpy(&state->frontend.ops, &au8522_ops,
852 sizeof(struct dvb_frontend_ops));
853 state->frontend.demodulator_priv = state;
855 if (au8522_init(&state->frontend) != 0) {
856 printk(KERN_ERR "%s: Failed to initialize correctly\n",
861 /* Note: Leaving the I2C gate open here. */
862 au8522_i2c_gate_ctrl(&state->frontend, 1);
864 return &state->frontend;
867 au8522_release_state(state);
870 EXPORT_SYMBOL(au8522_attach);
872 static struct dvb_frontend_ops au8522_ops = {
875 .name = "Auvitek AU8522 QAM/8VSB Frontend",
877 .frequency_min = 54000000,
878 .frequency_max = 858000000,
879 .frequency_stepsize = 62500,
880 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
884 .sleep = au8522_sleep,
885 .i2c_gate_ctrl = au8522_i2c_gate_ctrl,
886 .set_frontend = au8522_set_frontend,
887 .get_frontend = au8522_get_frontend,
888 .get_tune_settings = au8522_get_tune_settings,
889 .read_status = au8522_read_status,
890 .read_ber = au8522_read_ber,
891 .read_signal_strength = au8522_read_signal_strength,
892 .read_snr = au8522_read_snr,
893 .read_ucblocks = au8522_read_ucblocks,
894 .release = au8522_release,
897 module_param(debug, int, 0644);
898 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
900 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
901 MODULE_AUTHOR("Steven Toth");
902 MODULE_LICENSE("GPL");