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 #define dprintk(arg...) do { \
39 /* 16 bit registers, 8 bit values */
40 int au8522_writereg(struct au8522_state
*state
, u16 reg
, u8 data
)
43 u8 buf
[] = { reg
>> 8, reg
& 0xff, data
};
45 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
,
46 .flags
= 0, .buf
= buf
, .len
= 3 };
48 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
51 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
52 "ret == %i)\n", __func__
, reg
, data
, ret
);
54 return (ret
!= 1) ? -1 : 0;
57 u8
au8522_readreg(struct au8522_state
*state
, u16 reg
)
60 u8 b0
[] = { reg
>> 8, reg
& 0xff };
63 struct i2c_msg msg
[] = {
64 { .addr
= state
->config
->demod_address
, .flags
= 0,
65 .buf
= b0
, .len
= 2 },
66 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
,
67 .buf
= b1
, .len
= 1 } };
69 ret
= i2c_transfer(state
->i2c
, msg
, 2);
72 printk(KERN_ERR
"%s: readreg error (ret == %i)\n",
77 static int au8522_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
79 struct au8522_state
*state
= fe
->demodulator_priv
;
81 dprintk("%s(%d)\n", __func__
, enable
);
84 return au8522_writereg(state
, 0x106, 1);
86 return au8522_writereg(state
, 0x106, 0);
94 /* VSB SNR lookup table */
95 static struct mse2snr_tab vsb_mse2snr_tab
[] = {
128 /* QAM64 SNR lookup table */
129 static struct mse2snr_tab qam64_mse2snr_tab
[] = {
209 /* QAM256 SNR lookup table */
210 static struct mse2snr_tab qam256_mse2snr_tab
[] = {
277 static int au8522_mse2snr_lookup(struct mse2snr_tab
*tab
, int sz
, int mse
,
280 int i
, ret
= -EINVAL
;
281 dprintk("%s()\n", __func__
);
283 for (i
= 0; i
< sz
; i
++) {
284 if (mse
< tab
[i
].val
) {
290 dprintk("%s() snr=%d\n", __func__
, *snr
);
294 static int au8522_set_if(struct dvb_frontend
*fe
, enum au8522_if_freq if_freq
)
296 struct au8522_state
*state
= fe
->demodulator_priv
;
301 case AU8522_IF_3_25MHZ
:
320 dprintk("%s() IF Frequency not supported\n", __func__
);
323 dprintk("%s() %s MHz\n", __func__
, ifmhz
);
324 au8522_writereg(state
, 0x80b5, r0b5
);
325 au8522_writereg(state
, 0x80b6, r0b6
);
326 au8522_writereg(state
, 0x80b7, r0b7
);
331 /* VSB Modulation table */
365 /* QAM Modulation table */
444 static int au8522_enable_modulation(struct dvb_frontend
*fe
,
447 struct au8522_state
*state
= fe
->demodulator_priv
;
450 dprintk("%s(0x%08x)\n", __func__
, m
);
454 dprintk("%s() VSB_8\n", __func__
);
455 for (i
= 0; i
< ARRAY_SIZE(VSB_mod_tab
); i
++)
456 au8522_writereg(state
,
458 VSB_mod_tab
[i
].data
);
459 au8522_set_if(fe
, state
->config
->vsb_if
);
463 dprintk("%s() QAM 64/256\n", __func__
);
464 for (i
= 0; i
< ARRAY_SIZE(QAM_mod_tab
); i
++)
465 au8522_writereg(state
,
467 QAM_mod_tab
[i
].data
);
468 au8522_set_if(fe
, state
->config
->qam_if
);
471 dprintk("%s() Invalid modulation\n", __func__
);
475 state
->current_modulation
= m
;
480 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
481 static int au8522_set_frontend(struct dvb_frontend
*fe
,
482 struct dvb_frontend_parameters
*p
)
484 struct au8522_state
*state
= fe
->demodulator_priv
;
487 dprintk("%s(frequency=%d)\n", __func__
, p
->frequency
);
489 if ((state
->current_frequency
== p
->frequency
) &&
490 (state
->current_modulation
== p
->u
.vsb
.modulation
))
493 au8522_enable_modulation(fe
, p
->u
.vsb
.modulation
);
495 /* Allow the demod to settle */
498 if (fe
->ops
.tuner_ops
.set_params
) {
499 if (fe
->ops
.i2c_gate_ctrl
)
500 fe
->ops
.i2c_gate_ctrl(fe
, 1);
501 ret
= fe
->ops
.tuner_ops
.set_params(fe
, p
);
502 if (fe
->ops
.i2c_gate_ctrl
)
503 fe
->ops
.i2c_gate_ctrl(fe
, 0);
509 state
->current_frequency
= p
->frequency
;
514 /* Reset the demod hardware and reset all of the configuration registers
515 to a default state. */
516 int au8522_init(struct dvb_frontend
*fe
)
518 struct au8522_state
*state
= fe
->demodulator_priv
;
519 dprintk("%s()\n", __func__
);
521 au8522_writereg(state
, 0xa4, 1 << 5);
523 au8522_i2c_gate_ctrl(fe
, 1);
528 static int au8522_led_gpio_enable(struct au8522_state
*state
, int onoff
)
530 struct au8522_led_config
*led_config
= state
->config
->led_cfg
;
533 /* bail out if we cant control an LED */
534 if (!led_config
|| !led_config
->gpio_output
||
535 !led_config
->gpio_output_enable
|| !led_config
->gpio_output_disable
)
538 val
= au8522_readreg(state
, 0x4000 |
539 (led_config
->gpio_output
& ~0xc000));
541 /* enable GPIO output */
542 val
&= ~((led_config
->gpio_output_enable
>> 8) & 0xff);
543 val
|= (led_config
->gpio_output_enable
& 0xff);
545 /* disable GPIO output */
546 val
&= ~((led_config
->gpio_output_disable
>> 8) & 0xff);
547 val
|= (led_config
->gpio_output_disable
& 0xff);
549 return au8522_writereg(state
, 0x8000 |
550 (led_config
->gpio_output
& ~0xc000), val
);
554 * led = 1 | signal ok
555 * led = 2 | signal strong
556 * led < 0 | only light led if leds are currently off
558 static int au8522_led_ctrl(struct au8522_state
*state
, int led
)
560 struct au8522_led_config
*led_config
= state
->config
->led_cfg
;
563 /* bail out if we cant control an LED */
564 if (!led_config
|| !led_config
->gpio_leds
||
565 !led_config
->num_led_states
|| !led_config
->led_states
)
569 /* if LED is already lit, then leave it as-is */
570 if (state
->led_state
)
576 /* toggle LED if changing state */
577 if (state
->led_state
!= led
) {
580 dprintk("%s: %d\n", __func__
, led
);
582 au8522_led_gpio_enable(state
, 1);
584 val
= au8522_readreg(state
, 0x4000 |
585 (led_config
->gpio_leds
& ~0xc000));
587 /* start with all leds off */
588 for (i
= 0; i
< led_config
->num_led_states
; i
++)
589 val
&= ~led_config
->led_states
[i
];
591 /* set selected LED state */
592 if (led
< led_config
->num_led_states
)
593 val
|= led_config
->led_states
[led
];
594 else if (led_config
->num_led_states
)
596 led_config
->led_states
[led_config
->num_led_states
- 1];
598 ret
= au8522_writereg(state
, 0x8000 |
599 (led_config
->gpio_leds
& ~0xc000), val
);
603 state
->led_state
= led
;
606 au8522_led_gpio_enable(state
, 0);
612 int au8522_sleep(struct dvb_frontend
*fe
)
614 struct au8522_state
*state
= fe
->demodulator_priv
;
615 dprintk("%s()\n", __func__
);
618 au8522_led_ctrl(state
, 0);
620 state
->current_frequency
= 0;
625 static int au8522_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
627 struct au8522_state
*state
= fe
->demodulator_priv
;
629 u32 tuner_status
= 0;
633 if (state
->current_modulation
== VSB_8
) {
634 dprintk("%s() Checking VSB_8\n", __func__
);
635 reg
= au8522_readreg(state
, 0x4088);
636 if ((reg
& 0x03) == 0x03)
637 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
| FE_HAS_VITERBI
;
639 dprintk("%s() Checking QAM\n", __func__
);
640 reg
= au8522_readreg(state
, 0x4541);
642 *status
|= FE_HAS_VITERBI
;
644 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
647 switch (state
->config
->status_mode
) {
648 case AU8522_DEMODLOCKING
:
649 dprintk("%s() DEMODLOCKING\n", __func__
);
650 if (*status
& FE_HAS_VITERBI
)
651 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
653 case AU8522_TUNERLOCKING
:
654 /* Get the tuner status */
655 dprintk("%s() TUNERLOCKING\n", __func__
);
656 if (fe
->ops
.tuner_ops
.get_status
) {
657 if (fe
->ops
.i2c_gate_ctrl
)
658 fe
->ops
.i2c_gate_ctrl(fe
, 1);
660 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
662 if (fe
->ops
.i2c_gate_ctrl
)
663 fe
->ops
.i2c_gate_ctrl(fe
, 0);
666 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
669 state
->fe_status
= *status
;
671 if (*status
& FE_HAS_LOCK
)
672 /* turn on LED, if it isn't on already */
673 au8522_led_ctrl(state
, -1);
676 au8522_led_ctrl(state
, 0);
678 dprintk("%s() status 0x%08x\n", __func__
, *status
);
683 static int au8522_led_status(struct au8522_state
*state
, const u16
*snr
)
685 struct au8522_led_config
*led_config
= state
->config
->led_cfg
;
689 /* bail out if we cant control an LED */
693 if (0 == (state
->fe_status
& FE_HAS_LOCK
))
694 return au8522_led_ctrl(state
, 0);
695 else if (state
->current_modulation
== QAM_256
)
696 strong
= led_config
->qam256_strong
;
697 else if (state
->current_modulation
== QAM_64
)
698 strong
= led_config
->qam64_strong
;
699 else /* (state->current_modulation == VSB_8) */
700 strong
= led_config
->vsb8_strong
;
707 if ((state
->led_state
) &&
708 (((strong
< *snr
) ? (*snr
- strong
) : (strong
- *snr
)) <= 10))
709 /* snr didn't change enough to bother
710 * changing the color of the led */
713 return au8522_led_ctrl(state
, led
);
716 static int au8522_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
718 struct au8522_state
*state
= fe
->demodulator_priv
;
721 dprintk("%s()\n", __func__
);
723 if (state
->current_modulation
== QAM_256
)
724 ret
= au8522_mse2snr_lookup(qam256_mse2snr_tab
,
725 ARRAY_SIZE(qam256_mse2snr_tab
),
726 au8522_readreg(state
, 0x4522),
728 else if (state
->current_modulation
== QAM_64
)
729 ret
= au8522_mse2snr_lookup(qam64_mse2snr_tab
,
730 ARRAY_SIZE(qam64_mse2snr_tab
),
731 au8522_readreg(state
, 0x4522),
734 ret
= au8522_mse2snr_lookup(vsb_mse2snr_tab
,
735 ARRAY_SIZE(vsb_mse2snr_tab
),
736 au8522_readreg(state
, 0x4311),
739 if (state
->config
->led_cfg
)
740 au8522_led_status(state
, snr
);
745 static int au8522_read_signal_strength(struct dvb_frontend
*fe
,
746 u16
*signal_strength
)
748 return au8522_read_snr(fe
, signal_strength
);
751 static int au8522_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
753 struct au8522_state
*state
= fe
->demodulator_priv
;
755 if (state
->current_modulation
== VSB_8
)
756 *ucblocks
= au8522_readreg(state
, 0x4087);
758 *ucblocks
= au8522_readreg(state
, 0x4543);
763 static int au8522_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
765 return au8522_read_ucblocks(fe
, ber
);
768 static int au8522_get_frontend(struct dvb_frontend
*fe
,
769 struct dvb_frontend_parameters
*p
)
771 struct au8522_state
*state
= fe
->demodulator_priv
;
773 p
->frequency
= state
->current_frequency
;
774 p
->u
.vsb
.modulation
= state
->current_modulation
;
779 static int au8522_get_tune_settings(struct dvb_frontend
*fe
,
780 struct dvb_frontend_tune_settings
*tune
)
782 tune
->min_delay_ms
= 1000;
786 static void au8522_release(struct dvb_frontend
*fe
)
788 struct au8522_state
*state
= fe
->demodulator_priv
;
792 static struct dvb_frontend_ops au8522_ops
;
794 struct dvb_frontend
*au8522_attach(const struct au8522_config
*config
,
795 struct i2c_adapter
*i2c
)
797 struct au8522_state
*state
= NULL
;
799 /* allocate memory for the internal state */
800 state
= kmalloc(sizeof(struct au8522_state
), GFP_KERNEL
);
804 /* setup the state */
805 state
->config
= config
;
807 /* create dvb_frontend */
808 memcpy(&state
->frontend
.ops
, &au8522_ops
,
809 sizeof(struct dvb_frontend_ops
));
810 state
->frontend
.demodulator_priv
= state
;
812 if (au8522_init(&state
->frontend
) != 0) {
813 printk(KERN_ERR
"%s: Failed to initialize correctly\n",
818 /* Note: Leaving the I2C gate open here. */
819 au8522_i2c_gate_ctrl(&state
->frontend
, 1);
821 return &state
->frontend
;
827 EXPORT_SYMBOL(au8522_attach
);
829 static struct dvb_frontend_ops au8522_ops
= {
832 .name
= "Auvitek AU8522 QAM/8VSB Frontend",
834 .frequency_min
= 54000000,
835 .frequency_max
= 858000000,
836 .frequency_stepsize
= 62500,
837 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
841 .sleep
= au8522_sleep
,
842 .i2c_gate_ctrl
= au8522_i2c_gate_ctrl
,
843 .set_frontend
= au8522_set_frontend
,
844 .get_frontend
= au8522_get_frontend
,
845 .get_tune_settings
= au8522_get_tune_settings
,
846 .read_status
= au8522_read_status
,
847 .read_ber
= au8522_read_ber
,
848 .read_signal_strength
= au8522_read_signal_strength
,
849 .read_snr
= au8522_read_snr
,
850 .read_ucblocks
= au8522_read_ucblocks
,
851 .release
= au8522_release
,
854 module_param(debug
, int, 0644);
855 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
857 MODULE_DESCRIPTION("Auvitek AU8522 QAM-B/ATSC Demodulator driver");
858 MODULE_AUTHOR("Steven Toth");
859 MODULE_LICENSE("GPL");