1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Auvitek AU8522 QAM/8VSB demodulator driver
5 Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/delay.h>
15 #include <media/dvb_frontend.h>
17 #include "au8522_priv.h"
20 static int zv_mode
= 1; /* default to on */
22 #define dprintk(arg...)\
32 /* VSB SNR lookup table */
33 static struct mse2snr_tab vsb_mse2snr_tab
[] = {
66 /* QAM64 SNR lookup table */
67 static struct mse2snr_tab qam64_mse2snr_tab
[] = {
147 /* QAM256 SNR lookup table */
148 static struct mse2snr_tab qam256_mse2snr_tab
[] = {
215 static int au8522_mse2snr_lookup(struct mse2snr_tab
*tab
, int sz
, int mse
,
218 int i
, ret
= -EINVAL
;
219 dprintk("%s()\n", __func__
);
221 for (i
= 0; i
< sz
; i
++) {
222 if (mse
< tab
[i
].val
) {
228 dprintk("%s() snr=%d\n", __func__
, *snr
);
232 static int au8522_set_if(struct dvb_frontend
*fe
, enum au8522_if_freq if_freq
)
234 struct au8522_state
*state
= fe
->demodulator_priv
;
239 case AU8522_IF_3_25MHZ
:
258 dprintk("%s() IF Frequency not supported\n", __func__
);
261 dprintk("%s() %s MHz\n", __func__
, ifmhz
);
262 au8522_writereg(state
, 0x00b5, r0b5
);
263 au8522_writereg(state
, 0x00b6, r0b6
);
264 au8522_writereg(state
, 0x00b7, r0b7
);
269 /* VSB Modulation table */
302 /* QAM64 Modulation table */
306 } QAM64_mod_tab
[] = {
381 /* QAM256 Modulation table */
385 } QAM256_mod_tab
[] = {
463 } QAM256_mod_tab_zv_mode
[] = {
541 static int au8522_enable_modulation(struct dvb_frontend
*fe
,
542 enum fe_modulation m
)
544 struct au8522_state
*state
= fe
->demodulator_priv
;
547 dprintk("%s(0x%08x)\n", __func__
, m
);
551 dprintk("%s() VSB_8\n", __func__
);
552 for (i
= 0; i
< ARRAY_SIZE(VSB_mod_tab
); i
++)
553 au8522_writereg(state
,
555 VSB_mod_tab
[i
].data
);
556 au8522_set_if(fe
, state
->config
.vsb_if
);
559 dprintk("%s() QAM 64\n", __func__
);
560 for (i
= 0; i
< ARRAY_SIZE(QAM64_mod_tab
); i
++)
561 au8522_writereg(state
,
562 QAM64_mod_tab
[i
].reg
,
563 QAM64_mod_tab
[i
].data
);
564 au8522_set_if(fe
, state
->config
.qam_if
);
568 dprintk("%s() QAM 256 (zv_mode)\n", __func__
);
569 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab_zv_mode
); i
++)
570 au8522_writereg(state
,
571 QAM256_mod_tab_zv_mode
[i
].reg
,
572 QAM256_mod_tab_zv_mode
[i
].data
);
573 au8522_set_if(fe
, state
->config
.qam_if
);
575 au8522_writereg(state
, 0x821a, 0x00);
577 dprintk("%s() QAM 256\n", __func__
);
578 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab
); i
++)
579 au8522_writereg(state
,
580 QAM256_mod_tab
[i
].reg
,
581 QAM256_mod_tab
[i
].data
);
582 au8522_set_if(fe
, state
->config
.qam_if
);
586 dprintk("%s() Invalid modulation\n", __func__
);
590 state
->current_modulation
= m
;
595 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
596 static int au8522_set_frontend(struct dvb_frontend
*fe
)
598 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
599 struct au8522_state
*state
= fe
->demodulator_priv
;
602 dprintk("%s(frequency=%d)\n", __func__
, c
->frequency
);
604 if ((state
->current_frequency
== c
->frequency
) &&
605 (state
->current_modulation
== c
->modulation
))
608 if (fe
->ops
.tuner_ops
.set_params
) {
609 if (fe
->ops
.i2c_gate_ctrl
)
610 fe
->ops
.i2c_gate_ctrl(fe
, 1);
611 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
612 if (fe
->ops
.i2c_gate_ctrl
)
613 fe
->ops
.i2c_gate_ctrl(fe
, 0);
619 /* Allow the tuner to settle */
621 dprintk("%s() increase tuner settling time for zv_mode\n",
627 au8522_enable_modulation(fe
, c
->modulation
);
629 state
->current_frequency
= c
->frequency
;
634 static int au8522_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
636 struct au8522_state
*state
= fe
->demodulator_priv
;
638 u32 tuner_status
= 0;
642 if (state
->current_modulation
== VSB_8
) {
643 dprintk("%s() Checking VSB_8\n", __func__
);
644 reg
= au8522_readreg(state
, 0x0088);
645 if ((reg
& 0x03) == 0x03)
646 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
| FE_HAS_VITERBI
;
648 dprintk("%s() Checking QAM\n", __func__
);
649 reg
= au8522_readreg(state
, 0x0541);
651 *status
|= FE_HAS_VITERBI
;
653 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
656 switch (state
->config
.status_mode
) {
657 case AU8522_DEMODLOCKING
:
658 dprintk("%s() DEMODLOCKING\n", __func__
);
659 if (*status
& FE_HAS_VITERBI
)
660 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
662 case AU8522_TUNERLOCKING
:
663 /* Get the tuner status */
664 dprintk("%s() TUNERLOCKING\n", __func__
);
665 if (fe
->ops
.tuner_ops
.get_status
) {
666 if (fe
->ops
.i2c_gate_ctrl
)
667 fe
->ops
.i2c_gate_ctrl(fe
, 1);
669 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
671 if (fe
->ops
.i2c_gate_ctrl
)
672 fe
->ops
.i2c_gate_ctrl(fe
, 0);
675 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
678 state
->fe_status
= *status
;
680 if (*status
& FE_HAS_LOCK
)
681 /* turn on LED, if it isn't on already */
682 au8522_led_ctrl(state
, -1);
685 au8522_led_ctrl(state
, 0);
687 dprintk("%s() status 0x%08x\n", __func__
, *status
);
692 static int au8522_led_status(struct au8522_state
*state
, const u16
*snr
)
694 struct au8522_led_config
*led_config
= state
->config
.led_cfg
;
698 /* bail out if we can't control an LED */
702 if (0 == (state
->fe_status
& FE_HAS_LOCK
))
703 return au8522_led_ctrl(state
, 0);
704 else if (state
->current_modulation
== QAM_256
)
705 strong
= led_config
->qam256_strong
;
706 else if (state
->current_modulation
== QAM_64
)
707 strong
= led_config
->qam64_strong
;
708 else /* (state->current_modulation == VSB_8) */
709 strong
= led_config
->vsb8_strong
;
716 if ((state
->led_state
) &&
717 (((strong
< *snr
) ? (*snr
- strong
) : (strong
- *snr
)) <= 10))
718 /* snr didn't change enough to bother
719 * changing the color of the led */
722 return au8522_led_ctrl(state
, led
);
725 static int au8522_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
727 struct au8522_state
*state
= fe
->demodulator_priv
;
730 dprintk("%s()\n", __func__
);
732 if (state
->current_modulation
== QAM_256
)
733 ret
= au8522_mse2snr_lookup(qam256_mse2snr_tab
,
734 ARRAY_SIZE(qam256_mse2snr_tab
),
735 au8522_readreg(state
, 0x0522),
737 else if (state
->current_modulation
== QAM_64
)
738 ret
= au8522_mse2snr_lookup(qam64_mse2snr_tab
,
739 ARRAY_SIZE(qam64_mse2snr_tab
),
740 au8522_readreg(state
, 0x0522),
743 ret
= au8522_mse2snr_lookup(vsb_mse2snr_tab
,
744 ARRAY_SIZE(vsb_mse2snr_tab
),
745 au8522_readreg(state
, 0x0311),
748 if (state
->config
.led_cfg
)
749 au8522_led_status(state
, snr
);
754 static int au8522_read_signal_strength(struct dvb_frontend
*fe
,
755 u16
*signal_strength
)
757 /* borrowed from lgdt330x.c
759 * Calculate strength from SNR up to 35dB
760 * Even though the SNR can go higher than 35dB,
761 * there is some comfort factor in having a range of
762 * strong signals that can show at 100%
766 int ret
= au8522_read_snr(fe
, &snr
);
768 *signal_strength
= 0;
771 /* The following calculation method was chosen
772 * purely for the sake of code re-use from the
773 * other demod drivers that use this method */
775 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
776 tmp
= (snr
* ((1 << 24) / 10));
778 /* Convert from 8.24 fixed-point to
779 * scale the range 0 - 35*2^24 into 0 - 65535*/
780 if (tmp
>= 8960 * 0x10000)
781 *signal_strength
= 0xffff;
783 *signal_strength
= tmp
/ 8960;
789 static int au8522_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
791 struct au8522_state
*state
= fe
->demodulator_priv
;
793 if (state
->current_modulation
== VSB_8
)
794 *ucblocks
= au8522_readreg(state
, 0x0087);
796 *ucblocks
= au8522_readreg(state
, 0x0543);
801 static int au8522_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
803 return au8522_read_ucblocks(fe
, ber
);
806 static int au8522_get_frontend(struct dvb_frontend
*fe
,
807 struct dtv_frontend_properties
*c
)
809 struct au8522_state
*state
= fe
->demodulator_priv
;
811 c
->frequency
= state
->current_frequency
;
812 c
->modulation
= state
->current_modulation
;
817 static int au8522_get_tune_settings(struct dvb_frontend
*fe
,
818 struct dvb_frontend_tune_settings
*tune
)
820 tune
->min_delay_ms
= 1000;
824 static const struct dvb_frontend_ops au8522_ops
;
827 static void au8522_release(struct dvb_frontend
*fe
)
829 struct au8522_state
*state
= fe
->demodulator_priv
;
830 au8522_release_state(state
);
833 struct dvb_frontend
*au8522_attach(const struct au8522_config
*config
,
834 struct i2c_adapter
*i2c
)
836 struct au8522_state
*state
= NULL
;
839 /* allocate memory for the internal state */
840 instance
= au8522_get_state(&state
, i2c
, config
->demod_address
);
843 dprintk("%s state allocation failed\n", __func__
);
846 /* new demod instance */
847 dprintk("%s using new instance\n", __func__
);
850 /* existing demod instance */
851 dprintk("%s using existing instance\n", __func__
);
855 /* setup the state */
856 state
->config
= *config
;
858 state
->operational_mode
= AU8522_DIGITAL_MODE
;
860 /* create dvb_frontend */
861 memcpy(&state
->frontend
.ops
, &au8522_ops
,
862 sizeof(struct dvb_frontend_ops
));
863 state
->frontend
.demodulator_priv
= state
;
865 state
->frontend
.ops
.analog_ops
.i2c_gate_ctrl
= au8522_analog_i2c_gate_ctrl
;
867 if (au8522_init(&state
->frontend
) != 0) {
868 printk(KERN_ERR
"%s: Failed to initialize correctly\n",
873 /* Note: Leaving the I2C gate open here. */
874 au8522_i2c_gate_ctrl(&state
->frontend
, 1);
876 return &state
->frontend
;
879 au8522_release_state(state
);
882 EXPORT_SYMBOL(au8522_attach
);
884 static const struct dvb_frontend_ops au8522_ops
= {
885 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
887 .name
= "Auvitek AU8522 QAM/8VSB Frontend",
888 .frequency_min_hz
= 54 * MHz
,
889 .frequency_max_hz
= 858 * MHz
,
890 .frequency_stepsize_hz
= 62500,
891 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
895 .sleep
= au8522_sleep
,
896 .i2c_gate_ctrl
= au8522_i2c_gate_ctrl
,
897 .set_frontend
= au8522_set_frontend
,
898 .get_frontend
= au8522_get_frontend
,
899 .get_tune_settings
= au8522_get_tune_settings
,
900 .read_status
= au8522_read_status
,
901 .read_ber
= au8522_read_ber
,
902 .read_signal_strength
= au8522_read_signal_strength
,
903 .read_snr
= au8522_read_snr
,
904 .read_ucblocks
= au8522_read_ucblocks
,
905 .release
= au8522_release
,
908 module_param(debug
, int, 0644);
909 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
911 module_param(zv_mode
, int, 0644);
912 MODULE_PARM_DESC(zv_mode
, "Turn on/off ZeeVee modulator compatibility mode (default:on).\n"
913 "\t\ton - modified AU8522 QAM256 initialization.\n"
914 "\t\tProvides faster lock when using ZeeVee modulator based sources");
916 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
917 MODULE_AUTHOR("Steven Toth");
918 MODULE_LICENSE("GPL");