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/delay.h>
27 #include "dvb_frontend.h"
29 #include "au8522_priv.h"
32 static int zv_mode
= 1; /* default to on */
34 #define dprintk(arg...)\
44 /* VSB SNR lookup table */
45 static struct mse2snr_tab vsb_mse2snr_tab
[] = {
78 /* QAM64 SNR lookup table */
79 static struct mse2snr_tab qam64_mse2snr_tab
[] = {
159 /* QAM256 SNR lookup table */
160 static struct mse2snr_tab qam256_mse2snr_tab
[] = {
227 static int au8522_mse2snr_lookup(struct mse2snr_tab
*tab
, int sz
, int mse
,
230 int i
, ret
= -EINVAL
;
231 dprintk("%s()\n", __func__
);
233 for (i
= 0; i
< sz
; i
++) {
234 if (mse
< tab
[i
].val
) {
240 dprintk("%s() snr=%d\n", __func__
, *snr
);
244 static int au8522_set_if(struct dvb_frontend
*fe
, enum au8522_if_freq if_freq
)
246 struct au8522_state
*state
= fe
->demodulator_priv
;
251 case AU8522_IF_3_25MHZ
:
270 dprintk("%s() IF Frequency not supported\n", __func__
);
273 dprintk("%s() %s MHz\n", __func__
, ifmhz
);
274 au8522_writereg(state
, 0x80b5, r0b5
);
275 au8522_writereg(state
, 0x80b6, r0b6
);
276 au8522_writereg(state
, 0x80b7, r0b7
);
281 /* VSB Modulation table */
315 /* QAM64 Modulation table */
319 } QAM64_mod_tab
[] = {
394 /* QAM256 Modulation table */
398 } QAM256_mod_tab
[] = {
476 } QAM256_mod_tab_zv_mode
[] = {
554 static int au8522_enable_modulation(struct dvb_frontend
*fe
,
555 enum fe_modulation m
)
557 struct au8522_state
*state
= fe
->demodulator_priv
;
560 dprintk("%s(0x%08x)\n", __func__
, m
);
564 dprintk("%s() VSB_8\n", __func__
);
565 for (i
= 0; i
< ARRAY_SIZE(VSB_mod_tab
); i
++)
566 au8522_writereg(state
,
568 VSB_mod_tab
[i
].data
);
569 au8522_set_if(fe
, state
->config
.vsb_if
);
572 dprintk("%s() QAM 64\n", __func__
);
573 for (i
= 0; i
< ARRAY_SIZE(QAM64_mod_tab
); i
++)
574 au8522_writereg(state
,
575 QAM64_mod_tab
[i
].reg
,
576 QAM64_mod_tab
[i
].data
);
577 au8522_set_if(fe
, state
->config
.qam_if
);
581 dprintk("%s() QAM 256 (zv_mode)\n", __func__
);
582 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab_zv_mode
); i
++)
583 au8522_writereg(state
,
584 QAM256_mod_tab_zv_mode
[i
].reg
,
585 QAM256_mod_tab_zv_mode
[i
].data
);
586 au8522_set_if(fe
, state
->config
.qam_if
);
588 au8522_writereg(state
, 0x821a, 0x00);
590 dprintk("%s() QAM 256\n", __func__
);
591 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab
); i
++)
592 au8522_writereg(state
,
593 QAM256_mod_tab
[i
].reg
,
594 QAM256_mod_tab
[i
].data
);
595 au8522_set_if(fe
, state
->config
.qam_if
);
599 dprintk("%s() Invalid modulation\n", __func__
);
603 state
->current_modulation
= m
;
608 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
609 static int au8522_set_frontend(struct dvb_frontend
*fe
)
611 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
612 struct au8522_state
*state
= fe
->demodulator_priv
;
615 dprintk("%s(frequency=%d)\n", __func__
, c
->frequency
);
617 if ((state
->current_frequency
== c
->frequency
) &&
618 (state
->current_modulation
== c
->modulation
))
621 if (fe
->ops
.tuner_ops
.set_params
) {
622 if (fe
->ops
.i2c_gate_ctrl
)
623 fe
->ops
.i2c_gate_ctrl(fe
, 1);
624 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
625 if (fe
->ops
.i2c_gate_ctrl
)
626 fe
->ops
.i2c_gate_ctrl(fe
, 0);
632 /* Allow the tuner to settle */
634 dprintk("%s() increase tuner settling time for zv_mode\n",
640 au8522_enable_modulation(fe
, c
->modulation
);
642 state
->current_frequency
= c
->frequency
;
647 static int au8522_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
649 struct au8522_state
*state
= fe
->demodulator_priv
;
651 u32 tuner_status
= 0;
655 if (state
->current_modulation
== VSB_8
) {
656 dprintk("%s() Checking VSB_8\n", __func__
);
657 reg
= au8522_readreg(state
, 0x4088);
658 if ((reg
& 0x03) == 0x03)
659 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
| FE_HAS_VITERBI
;
661 dprintk("%s() Checking QAM\n", __func__
);
662 reg
= au8522_readreg(state
, 0x4541);
664 *status
|= FE_HAS_VITERBI
;
666 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
669 switch (state
->config
.status_mode
) {
670 case AU8522_DEMODLOCKING
:
671 dprintk("%s() DEMODLOCKING\n", __func__
);
672 if (*status
& FE_HAS_VITERBI
)
673 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
675 case AU8522_TUNERLOCKING
:
676 /* Get the tuner status */
677 dprintk("%s() TUNERLOCKING\n", __func__
);
678 if (fe
->ops
.tuner_ops
.get_status
) {
679 if (fe
->ops
.i2c_gate_ctrl
)
680 fe
->ops
.i2c_gate_ctrl(fe
, 1);
682 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
684 if (fe
->ops
.i2c_gate_ctrl
)
685 fe
->ops
.i2c_gate_ctrl(fe
, 0);
688 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
691 state
->fe_status
= *status
;
693 if (*status
& FE_HAS_LOCK
)
694 /* turn on LED, if it isn't on already */
695 au8522_led_ctrl(state
, -1);
698 au8522_led_ctrl(state
, 0);
700 dprintk("%s() status 0x%08x\n", __func__
, *status
);
705 static int au8522_led_status(struct au8522_state
*state
, const u16
*snr
)
707 struct au8522_led_config
*led_config
= state
->config
.led_cfg
;
711 /* bail out if we can't control an LED */
715 if (0 == (state
->fe_status
& FE_HAS_LOCK
))
716 return au8522_led_ctrl(state
, 0);
717 else if (state
->current_modulation
== QAM_256
)
718 strong
= led_config
->qam256_strong
;
719 else if (state
->current_modulation
== QAM_64
)
720 strong
= led_config
->qam64_strong
;
721 else /* (state->current_modulation == VSB_8) */
722 strong
= led_config
->vsb8_strong
;
729 if ((state
->led_state
) &&
730 (((strong
< *snr
) ? (*snr
- strong
) : (strong
- *snr
)) <= 10))
731 /* snr didn't change enough to bother
732 * changing the color of the led */
735 return au8522_led_ctrl(state
, led
);
738 static int au8522_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
740 struct au8522_state
*state
= fe
->demodulator_priv
;
743 dprintk("%s()\n", __func__
);
745 if (state
->current_modulation
== QAM_256
)
746 ret
= au8522_mse2snr_lookup(qam256_mse2snr_tab
,
747 ARRAY_SIZE(qam256_mse2snr_tab
),
748 au8522_readreg(state
, 0x4522),
750 else if (state
->current_modulation
== QAM_64
)
751 ret
= au8522_mse2snr_lookup(qam64_mse2snr_tab
,
752 ARRAY_SIZE(qam64_mse2snr_tab
),
753 au8522_readreg(state
, 0x4522),
756 ret
= au8522_mse2snr_lookup(vsb_mse2snr_tab
,
757 ARRAY_SIZE(vsb_mse2snr_tab
),
758 au8522_readreg(state
, 0x4311),
761 if (state
->config
.led_cfg
)
762 au8522_led_status(state
, snr
);
767 static int au8522_read_signal_strength(struct dvb_frontend
*fe
,
768 u16
*signal_strength
)
770 /* borrowed from lgdt330x.c
772 * Calculate strength from SNR up to 35dB
773 * Even though the SNR can go higher than 35dB,
774 * there is some comfort factor in having a range of
775 * strong signals that can show at 100%
779 int ret
= au8522_read_snr(fe
, &snr
);
781 *signal_strength
= 0;
784 /* The following calculation method was chosen
785 * purely for the sake of code re-use from the
786 * other demod drivers that use this method */
788 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
789 tmp
= (snr
* ((1 << 24) / 10));
791 /* Convert from 8.24 fixed-point to
792 * scale the range 0 - 35*2^24 into 0 - 65535*/
793 if (tmp
>= 8960 * 0x10000)
794 *signal_strength
= 0xffff;
796 *signal_strength
= tmp
/ 8960;
802 static int au8522_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
804 struct au8522_state
*state
= fe
->demodulator_priv
;
806 if (state
->current_modulation
== VSB_8
)
807 *ucblocks
= au8522_readreg(state
, 0x4087);
809 *ucblocks
= au8522_readreg(state
, 0x4543);
814 static int au8522_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
816 return au8522_read_ucblocks(fe
, ber
);
819 static int au8522_get_frontend(struct dvb_frontend
*fe
,
820 struct dtv_frontend_properties
*c
)
822 struct au8522_state
*state
= fe
->demodulator_priv
;
824 c
->frequency
= state
->current_frequency
;
825 c
->modulation
= state
->current_modulation
;
830 static int au8522_get_tune_settings(struct dvb_frontend
*fe
,
831 struct dvb_frontend_tune_settings
*tune
)
833 tune
->min_delay_ms
= 1000;
837 static const struct dvb_frontend_ops au8522_ops
;
840 static void au8522_release(struct dvb_frontend
*fe
)
842 struct au8522_state
*state
= fe
->demodulator_priv
;
843 au8522_release_state(state
);
846 struct dvb_frontend
*au8522_attach(const struct au8522_config
*config
,
847 struct i2c_adapter
*i2c
)
849 struct au8522_state
*state
= NULL
;
852 /* allocate memory for the internal state */
853 instance
= au8522_get_state(&state
, i2c
, config
->demod_address
);
856 dprintk("%s state allocation failed\n", __func__
);
859 /* new demod instance */
860 dprintk("%s using new instance\n", __func__
);
863 /* existing demod instance */
864 dprintk("%s using existing instance\n", __func__
);
868 /* setup the state */
869 state
->config
= *config
;
871 state
->operational_mode
= AU8522_DIGITAL_MODE
;
873 /* create dvb_frontend */
874 memcpy(&state
->frontend
.ops
, &au8522_ops
,
875 sizeof(struct dvb_frontend_ops
));
876 state
->frontend
.demodulator_priv
= state
;
878 state
->frontend
.ops
.analog_ops
.i2c_gate_ctrl
= au8522_analog_i2c_gate_ctrl
;
880 if (au8522_init(&state
->frontend
) != 0) {
881 printk(KERN_ERR
"%s: Failed to initialize correctly\n",
886 /* Note: Leaving the I2C gate open here. */
887 au8522_i2c_gate_ctrl(&state
->frontend
, 1);
889 return &state
->frontend
;
892 au8522_release_state(state
);
895 EXPORT_SYMBOL(au8522_attach
);
897 static const struct dvb_frontend_ops au8522_ops
= {
898 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
900 .name
= "Auvitek AU8522 QAM/8VSB Frontend",
901 .frequency_min
= 54000000,
902 .frequency_max
= 858000000,
903 .frequency_stepsize
= 62500,
904 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
908 .sleep
= au8522_sleep
,
909 .i2c_gate_ctrl
= au8522_i2c_gate_ctrl
,
910 .set_frontend
= au8522_set_frontend
,
911 .get_frontend
= au8522_get_frontend
,
912 .get_tune_settings
= au8522_get_tune_settings
,
913 .read_status
= au8522_read_status
,
914 .read_ber
= au8522_read_ber
,
915 .read_signal_strength
= au8522_read_signal_strength
,
916 .read_snr
= au8522_read_snr
,
917 .read_ucblocks
= au8522_read_ucblocks
,
918 .release
= au8522_release
,
921 module_param(debug
, int, 0644);
922 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
924 module_param(zv_mode
, int, 0644);
925 MODULE_PARM_DESC(zv_mode
, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
926 "\t\ton - modified AU8522 QAM256 initialization.\n"
927 "\t\tProvides faster lock when using ZeeVee modulator based sources");
929 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
930 MODULE_AUTHOR("Steven Toth");
931 MODULE_LICENSE("GPL");