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"
33 #define dprintk(arg...)\
43 /* VSB SNR lookup table */
44 static struct mse2snr_tab vsb_mse2snr_tab
[] = {
77 /* QAM64 SNR lookup table */
78 static struct mse2snr_tab qam64_mse2snr_tab
[] = {
158 /* QAM256 SNR lookup table */
159 static struct mse2snr_tab qam256_mse2snr_tab
[] = {
226 static int au8522_mse2snr_lookup(struct mse2snr_tab
*tab
, int sz
, int mse
,
229 int i
, ret
= -EINVAL
;
230 dprintk("%s()\n", __func__
);
232 for (i
= 0; i
< sz
; i
++) {
233 if (mse
< tab
[i
].val
) {
239 dprintk("%s() snr=%d\n", __func__
, *snr
);
243 static int au8522_set_if(struct dvb_frontend
*fe
, enum au8522_if_freq if_freq
)
245 struct au8522_state
*state
= fe
->demodulator_priv
;
250 case AU8522_IF_3_25MHZ
:
269 dprintk("%s() IF Frequency not supported\n", __func__
);
272 dprintk("%s() %s MHz\n", __func__
, ifmhz
);
273 au8522_writereg(state
, 0x80b5, r0b5
);
274 au8522_writereg(state
, 0x80b6, r0b6
);
275 au8522_writereg(state
, 0x80b7, r0b7
);
280 /* VSB Modulation table */
314 /* QAM64 Modulation table */
318 } QAM64_mod_tab
[] = {
393 /* QAM256 Modulation table */
397 } QAM256_mod_tab
[] = {
472 static int au8522_enable_modulation(struct dvb_frontend
*fe
,
475 struct au8522_state
*state
= fe
->demodulator_priv
;
478 dprintk("%s(0x%08x)\n", __func__
, m
);
482 dprintk("%s() VSB_8\n", __func__
);
483 for (i
= 0; i
< ARRAY_SIZE(VSB_mod_tab
); i
++)
484 au8522_writereg(state
,
486 VSB_mod_tab
[i
].data
);
487 au8522_set_if(fe
, state
->config
->vsb_if
);
490 dprintk("%s() QAM 64\n", __func__
);
491 for (i
= 0; i
< ARRAY_SIZE(QAM64_mod_tab
); i
++)
492 au8522_writereg(state
,
493 QAM64_mod_tab
[i
].reg
,
494 QAM64_mod_tab
[i
].data
);
495 au8522_set_if(fe
, state
->config
->qam_if
);
498 dprintk("%s() QAM 256\n", __func__
);
499 for (i
= 0; i
< ARRAY_SIZE(QAM256_mod_tab
); i
++)
500 au8522_writereg(state
,
501 QAM256_mod_tab
[i
].reg
,
502 QAM256_mod_tab
[i
].data
);
503 au8522_set_if(fe
, state
->config
->qam_if
);
506 dprintk("%s() Invalid modulation\n", __func__
);
510 state
->current_modulation
= m
;
515 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
516 static int au8522_set_frontend(struct dvb_frontend
*fe
)
518 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
519 struct au8522_state
*state
= fe
->demodulator_priv
;
522 dprintk("%s(frequency=%d)\n", __func__
, c
->frequency
);
524 if ((state
->current_frequency
== c
->frequency
) &&
525 (state
->current_modulation
== c
->modulation
))
528 if (fe
->ops
.tuner_ops
.set_params
) {
529 if (fe
->ops
.i2c_gate_ctrl
)
530 fe
->ops
.i2c_gate_ctrl(fe
, 1);
531 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
532 if (fe
->ops
.i2c_gate_ctrl
)
533 fe
->ops
.i2c_gate_ctrl(fe
, 0);
539 /* Allow the tuner to settle */
542 au8522_enable_modulation(fe
, c
->modulation
);
544 state
->current_frequency
= c
->frequency
;
549 static int au8522_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
551 struct au8522_state
*state
= fe
->demodulator_priv
;
553 u32 tuner_status
= 0;
557 if (state
->current_modulation
== VSB_8
) {
558 dprintk("%s() Checking VSB_8\n", __func__
);
559 reg
= au8522_readreg(state
, 0x4088);
560 if ((reg
& 0x03) == 0x03)
561 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
| FE_HAS_VITERBI
;
563 dprintk("%s() Checking QAM\n", __func__
);
564 reg
= au8522_readreg(state
, 0x4541);
566 *status
|= FE_HAS_VITERBI
;
568 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
571 switch (state
->config
->status_mode
) {
572 case AU8522_DEMODLOCKING
:
573 dprintk("%s() DEMODLOCKING\n", __func__
);
574 if (*status
& FE_HAS_VITERBI
)
575 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
577 case AU8522_TUNERLOCKING
:
578 /* Get the tuner status */
579 dprintk("%s() TUNERLOCKING\n", __func__
);
580 if (fe
->ops
.tuner_ops
.get_status
) {
581 if (fe
->ops
.i2c_gate_ctrl
)
582 fe
->ops
.i2c_gate_ctrl(fe
, 1);
584 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
586 if (fe
->ops
.i2c_gate_ctrl
)
587 fe
->ops
.i2c_gate_ctrl(fe
, 0);
590 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
593 state
->fe_status
= *status
;
595 if (*status
& FE_HAS_LOCK
)
596 /* turn on LED, if it isn't on already */
597 au8522_led_ctrl(state
, -1);
600 au8522_led_ctrl(state
, 0);
602 dprintk("%s() status 0x%08x\n", __func__
, *status
);
607 static int au8522_led_status(struct au8522_state
*state
, const u16
*snr
)
609 struct au8522_led_config
*led_config
= state
->config
->led_cfg
;
613 /* bail out if we can't control an LED */
617 if (0 == (state
->fe_status
& FE_HAS_LOCK
))
618 return au8522_led_ctrl(state
, 0);
619 else if (state
->current_modulation
== QAM_256
)
620 strong
= led_config
->qam256_strong
;
621 else if (state
->current_modulation
== QAM_64
)
622 strong
= led_config
->qam64_strong
;
623 else /* (state->current_modulation == VSB_8) */
624 strong
= led_config
->vsb8_strong
;
631 if ((state
->led_state
) &&
632 (((strong
< *snr
) ? (*snr
- strong
) : (strong
- *snr
)) <= 10))
633 /* snr didn't change enough to bother
634 * changing the color of the led */
637 return au8522_led_ctrl(state
, led
);
640 static int au8522_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
642 struct au8522_state
*state
= fe
->demodulator_priv
;
645 dprintk("%s()\n", __func__
);
647 if (state
->current_modulation
== QAM_256
)
648 ret
= au8522_mse2snr_lookup(qam256_mse2snr_tab
,
649 ARRAY_SIZE(qam256_mse2snr_tab
),
650 au8522_readreg(state
, 0x4522),
652 else if (state
->current_modulation
== QAM_64
)
653 ret
= au8522_mse2snr_lookup(qam64_mse2snr_tab
,
654 ARRAY_SIZE(qam64_mse2snr_tab
),
655 au8522_readreg(state
, 0x4522),
658 ret
= au8522_mse2snr_lookup(vsb_mse2snr_tab
,
659 ARRAY_SIZE(vsb_mse2snr_tab
),
660 au8522_readreg(state
, 0x4311),
663 if (state
->config
->led_cfg
)
664 au8522_led_status(state
, snr
);
669 static int au8522_read_signal_strength(struct dvb_frontend
*fe
,
670 u16
*signal_strength
)
672 /* borrowed from lgdt330x.c
674 * Calculate strength from SNR up to 35dB
675 * Even though the SNR can go higher than 35dB,
676 * there is some comfort factor in having a range of
677 * strong signals that can show at 100%
681 int ret
= au8522_read_snr(fe
, &snr
);
683 *signal_strength
= 0;
686 /* The following calculation method was chosen
687 * purely for the sake of code re-use from the
688 * other demod drivers that use this method */
690 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
691 tmp
= (snr
* ((1 << 24) / 10));
693 /* Convert from 8.24 fixed-point to
694 * scale the range 0 - 35*2^24 into 0 - 65535*/
695 if (tmp
>= 8960 * 0x10000)
696 *signal_strength
= 0xffff;
698 *signal_strength
= tmp
/ 8960;
704 static int au8522_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
706 struct au8522_state
*state
= fe
->demodulator_priv
;
708 if (state
->current_modulation
== VSB_8
)
709 *ucblocks
= au8522_readreg(state
, 0x4087);
711 *ucblocks
= au8522_readreg(state
, 0x4543);
716 static int au8522_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
718 return au8522_read_ucblocks(fe
, ber
);
721 static int au8522_get_frontend(struct dvb_frontend
*fe
)
723 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
724 struct au8522_state
*state
= fe
->demodulator_priv
;
726 c
->frequency
= state
->current_frequency
;
727 c
->modulation
= state
->current_modulation
;
732 static int au8522_get_tune_settings(struct dvb_frontend
*fe
,
733 struct dvb_frontend_tune_settings
*tune
)
735 tune
->min_delay_ms
= 1000;
739 static struct dvb_frontend_ops au8522_ops
;
742 static void au8522_release(struct dvb_frontend
*fe
)
744 struct au8522_state
*state
= fe
->demodulator_priv
;
745 au8522_release_state(state
);
748 struct dvb_frontend
*au8522_attach(const struct au8522_config
*config
,
749 struct i2c_adapter
*i2c
)
751 struct au8522_state
*state
= NULL
;
754 /* allocate memory for the internal state */
755 instance
= au8522_get_state(&state
, i2c
, config
->demod_address
);
758 dprintk("%s state allocation failed\n", __func__
);
761 /* new demod instance */
762 dprintk("%s using new instance\n", __func__
);
765 /* existing demod instance */
766 dprintk("%s using existing instance\n", __func__
);
770 /* setup the state */
771 state
->config
= config
;
773 state
->operational_mode
= AU8522_DIGITAL_MODE
;
775 /* create dvb_frontend */
776 memcpy(&state
->frontend
.ops
, &au8522_ops
,
777 sizeof(struct dvb_frontend_ops
));
778 state
->frontend
.demodulator_priv
= state
;
780 state
->frontend
.ops
.analog_ops
.i2c_gate_ctrl
= au8522_analog_i2c_gate_ctrl
;
782 if (au8522_init(&state
->frontend
) != 0) {
783 printk(KERN_ERR
"%s: Failed to initialize correctly\n",
788 /* Note: Leaving the I2C gate open here. */
789 au8522_i2c_gate_ctrl(&state
->frontend
, 1);
791 return &state
->frontend
;
794 au8522_release_state(state
);
797 EXPORT_SYMBOL(au8522_attach
);
799 static struct dvb_frontend_ops au8522_ops
= {
800 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
802 .name
= "Auvitek AU8522 QAM/8VSB Frontend",
803 .frequency_min
= 54000000,
804 .frequency_max
= 858000000,
805 .frequency_stepsize
= 62500,
806 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
810 .sleep
= au8522_sleep
,
811 .i2c_gate_ctrl
= au8522_i2c_gate_ctrl
,
812 .set_frontend
= au8522_set_frontend
,
813 .get_frontend
= au8522_get_frontend
,
814 .get_tune_settings
= au8522_get_tune_settings
,
815 .read_status
= au8522_read_status
,
816 .read_ber
= au8522_read_ber
,
817 .read_signal_strength
= au8522_read_signal_strength
,
818 .read_snr
= au8522_read_snr
,
819 .read_ucblocks
= au8522_read_ucblocks
,
820 .release
= au8522_release
,
823 module_param(debug
, int, 0644);
824 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
826 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
827 MODULE_AUTHOR("Steven Toth");
828 MODULE_LICENSE("GPL");