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 <media/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
, 0x00b5, r0b5
);
275 au8522_writereg(state
, 0x00b6, r0b6
);
276 au8522_writereg(state
, 0x00b7, r0b7
);
281 /* VSB Modulation table */
314 /* QAM64 Modulation table */
318 } QAM64_mod_tab
[] = {
393 /* QAM256 Modulation table */
397 } QAM256_mod_tab
[] = {
475 } QAM256_mod_tab_zv_mode
[] = {
553 static int au8522_enable_modulation(struct dvb_frontend
*fe
,
554 enum fe_modulation m
)
556 struct au8522_state
*state
= fe
->demodulator_priv
;
559 dprintk("%s(0x%08x)\n", __func__
, m
);
563 dprintk("%s() VSB_8\n", __func__
);
564 for (i
= 0; i
< ARRAY_SIZE(VSB_mod_tab
); i
++)
565 au8522_writereg(state
,
567 VSB_mod_tab
[i
].data
);
568 au8522_set_if(fe
, state
->config
.vsb_if
);
571 dprintk("%s() QAM 64\n", __func__
);
572 for (i
= 0; i
< ARRAY_SIZE(QAM64_mod_tab
); i
++)
573 au8522_writereg(state
,
574 QAM64_mod_tab
[i
].reg
,
575 QAM64_mod_tab
[i
].data
);
576 au8522_set_if(fe
, state
->config
.qam_if
);
580 dprintk("%s() QAM 256 (zv_mode)\n", __func__
);
581 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab_zv_mode
); i
++)
582 au8522_writereg(state
,
583 QAM256_mod_tab_zv_mode
[i
].reg
,
584 QAM256_mod_tab_zv_mode
[i
].data
);
585 au8522_set_if(fe
, state
->config
.qam_if
);
587 au8522_writereg(state
, 0x821a, 0x00);
589 dprintk("%s() QAM 256\n", __func__
);
590 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab
); i
++)
591 au8522_writereg(state
,
592 QAM256_mod_tab
[i
].reg
,
593 QAM256_mod_tab
[i
].data
);
594 au8522_set_if(fe
, state
->config
.qam_if
);
598 dprintk("%s() Invalid modulation\n", __func__
);
602 state
->current_modulation
= m
;
607 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
608 static int au8522_set_frontend(struct dvb_frontend
*fe
)
610 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
611 struct au8522_state
*state
= fe
->demodulator_priv
;
614 dprintk("%s(frequency=%d)\n", __func__
, c
->frequency
);
616 if ((state
->current_frequency
== c
->frequency
) &&
617 (state
->current_modulation
== c
->modulation
))
620 if (fe
->ops
.tuner_ops
.set_params
) {
621 if (fe
->ops
.i2c_gate_ctrl
)
622 fe
->ops
.i2c_gate_ctrl(fe
, 1);
623 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
624 if (fe
->ops
.i2c_gate_ctrl
)
625 fe
->ops
.i2c_gate_ctrl(fe
, 0);
631 /* Allow the tuner to settle */
633 dprintk("%s() increase tuner settling time for zv_mode\n",
639 au8522_enable_modulation(fe
, c
->modulation
);
641 state
->current_frequency
= c
->frequency
;
646 static int au8522_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
648 struct au8522_state
*state
= fe
->demodulator_priv
;
650 u32 tuner_status
= 0;
654 if (state
->current_modulation
== VSB_8
) {
655 dprintk("%s() Checking VSB_8\n", __func__
);
656 reg
= au8522_readreg(state
, 0x0088);
657 if ((reg
& 0x03) == 0x03)
658 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
| FE_HAS_VITERBI
;
660 dprintk("%s() Checking QAM\n", __func__
);
661 reg
= au8522_readreg(state
, 0x0541);
663 *status
|= FE_HAS_VITERBI
;
665 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
668 switch (state
->config
.status_mode
) {
669 case AU8522_DEMODLOCKING
:
670 dprintk("%s() DEMODLOCKING\n", __func__
);
671 if (*status
& FE_HAS_VITERBI
)
672 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
674 case AU8522_TUNERLOCKING
:
675 /* Get the tuner status */
676 dprintk("%s() TUNERLOCKING\n", __func__
);
677 if (fe
->ops
.tuner_ops
.get_status
) {
678 if (fe
->ops
.i2c_gate_ctrl
)
679 fe
->ops
.i2c_gate_ctrl(fe
, 1);
681 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
683 if (fe
->ops
.i2c_gate_ctrl
)
684 fe
->ops
.i2c_gate_ctrl(fe
, 0);
687 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
690 state
->fe_status
= *status
;
692 if (*status
& FE_HAS_LOCK
)
693 /* turn on LED, if it isn't on already */
694 au8522_led_ctrl(state
, -1);
697 au8522_led_ctrl(state
, 0);
699 dprintk("%s() status 0x%08x\n", __func__
, *status
);
704 static int au8522_led_status(struct au8522_state
*state
, const u16
*snr
)
706 struct au8522_led_config
*led_config
= state
->config
.led_cfg
;
710 /* bail out if we can't control an LED */
714 if (0 == (state
->fe_status
& FE_HAS_LOCK
))
715 return au8522_led_ctrl(state
, 0);
716 else if (state
->current_modulation
== QAM_256
)
717 strong
= led_config
->qam256_strong
;
718 else if (state
->current_modulation
== QAM_64
)
719 strong
= led_config
->qam64_strong
;
720 else /* (state->current_modulation == VSB_8) */
721 strong
= led_config
->vsb8_strong
;
728 if ((state
->led_state
) &&
729 (((strong
< *snr
) ? (*snr
- strong
) : (strong
- *snr
)) <= 10))
730 /* snr didn't change enough to bother
731 * changing the color of the led */
734 return au8522_led_ctrl(state
, led
);
737 static int au8522_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
739 struct au8522_state
*state
= fe
->demodulator_priv
;
742 dprintk("%s()\n", __func__
);
744 if (state
->current_modulation
== QAM_256
)
745 ret
= au8522_mse2snr_lookup(qam256_mse2snr_tab
,
746 ARRAY_SIZE(qam256_mse2snr_tab
),
747 au8522_readreg(state
, 0x0522),
749 else if (state
->current_modulation
== QAM_64
)
750 ret
= au8522_mse2snr_lookup(qam64_mse2snr_tab
,
751 ARRAY_SIZE(qam64_mse2snr_tab
),
752 au8522_readreg(state
, 0x0522),
755 ret
= au8522_mse2snr_lookup(vsb_mse2snr_tab
,
756 ARRAY_SIZE(vsb_mse2snr_tab
),
757 au8522_readreg(state
, 0x0311),
760 if (state
->config
.led_cfg
)
761 au8522_led_status(state
, snr
);
766 static int au8522_read_signal_strength(struct dvb_frontend
*fe
,
767 u16
*signal_strength
)
769 /* borrowed from lgdt330x.c
771 * Calculate strength from SNR up to 35dB
772 * Even though the SNR can go higher than 35dB,
773 * there is some comfort factor in having a range of
774 * strong signals that can show at 100%
778 int ret
= au8522_read_snr(fe
, &snr
);
780 *signal_strength
= 0;
783 /* The following calculation method was chosen
784 * purely for the sake of code re-use from the
785 * other demod drivers that use this method */
787 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
788 tmp
= (snr
* ((1 << 24) / 10));
790 /* Convert from 8.24 fixed-point to
791 * scale the range 0 - 35*2^24 into 0 - 65535*/
792 if (tmp
>= 8960 * 0x10000)
793 *signal_strength
= 0xffff;
795 *signal_strength
= tmp
/ 8960;
801 static int au8522_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
803 struct au8522_state
*state
= fe
->demodulator_priv
;
805 if (state
->current_modulation
== VSB_8
)
806 *ucblocks
= au8522_readreg(state
, 0x0087);
808 *ucblocks
= au8522_readreg(state
, 0x0543);
813 static int au8522_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
815 return au8522_read_ucblocks(fe
, ber
);
818 static int au8522_get_frontend(struct dvb_frontend
*fe
,
819 struct dtv_frontend_properties
*c
)
821 struct au8522_state
*state
= fe
->demodulator_priv
;
823 c
->frequency
= state
->current_frequency
;
824 c
->modulation
= state
->current_modulation
;
829 static int au8522_get_tune_settings(struct dvb_frontend
*fe
,
830 struct dvb_frontend_tune_settings
*tune
)
832 tune
->min_delay_ms
= 1000;
836 static const struct dvb_frontend_ops au8522_ops
;
839 static void au8522_release(struct dvb_frontend
*fe
)
841 struct au8522_state
*state
= fe
->demodulator_priv
;
842 au8522_release_state(state
);
845 struct dvb_frontend
*au8522_attach(const struct au8522_config
*config
,
846 struct i2c_adapter
*i2c
)
848 struct au8522_state
*state
= NULL
;
851 /* allocate memory for the internal state */
852 instance
= au8522_get_state(&state
, i2c
, config
->demod_address
);
855 dprintk("%s state allocation failed\n", __func__
);
858 /* new demod instance */
859 dprintk("%s using new instance\n", __func__
);
862 /* existing demod instance */
863 dprintk("%s using existing instance\n", __func__
);
867 /* setup the state */
868 state
->config
= *config
;
870 state
->operational_mode
= AU8522_DIGITAL_MODE
;
872 /* create dvb_frontend */
873 memcpy(&state
->frontend
.ops
, &au8522_ops
,
874 sizeof(struct dvb_frontend_ops
));
875 state
->frontend
.demodulator_priv
= state
;
877 state
->frontend
.ops
.analog_ops
.i2c_gate_ctrl
= au8522_analog_i2c_gate_ctrl
;
879 if (au8522_init(&state
->frontend
) != 0) {
880 printk(KERN_ERR
"%s: Failed to initialize correctly\n",
885 /* Note: Leaving the I2C gate open here. */
886 au8522_i2c_gate_ctrl(&state
->frontend
, 1);
888 return &state
->frontend
;
891 au8522_release_state(state
);
894 EXPORT_SYMBOL(au8522_attach
);
896 static const struct dvb_frontend_ops au8522_ops
= {
897 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
899 .name
= "Auvitek AU8522 QAM/8VSB Frontend",
900 .frequency_min_hz
= 54 * MHz
,
901 .frequency_max_hz
= 858 * MHz
,
902 .frequency_stepsize_hz
= 62500,
903 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
907 .sleep
= au8522_sleep
,
908 .i2c_gate_ctrl
= au8522_i2c_gate_ctrl
,
909 .set_frontend
= au8522_set_frontend
,
910 .get_frontend
= au8522_get_frontend
,
911 .get_tune_settings
= au8522_get_tune_settings
,
912 .read_status
= au8522_read_status
,
913 .read_ber
= au8522_read_ber
,
914 .read_signal_strength
= au8522_read_signal_strength
,
915 .read_snr
= au8522_read_snr
,
916 .read_ucblocks
= au8522_read_ucblocks
,
917 .release
= au8522_release
,
920 module_param(debug
, int, 0644);
921 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
923 module_param(zv_mode
, int, 0644);
924 MODULE_PARM_DESC(zv_mode
, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
925 "\t\ton - modified AU8522 QAM256 initialization.\n"
926 "\t\tProvides faster lock when using ZeeVee modulator based sources");
928 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
929 MODULE_AUTHOR("Steven Toth");
930 MODULE_LICENSE("GPL");