2 Samsung S5H1409 VSB/QAM demodulator driver
4 Copyright (C) 2006 Steven Toth <stoth@hauppauge.com>
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"
32 struct s5h1409_state
{
34 struct i2c_adapter
* i2c
;
36 /* configuration settings */
37 const struct s5h1409_config
* config
;
39 struct dvb_frontend frontend
;
41 /* previous uncorrected block counter */
42 fe_modulation_t current_modulation
;
44 u32 current_frequency
;
52 #define dprintk if (debug) printk
54 /* Register values to initialise the demod, this will set VSB by default */
55 static struct init_tab
{
106 /* VSB SNR lookup table */
107 static struct vsb_snr_tab
{
153 /* QAM64 SNR lookup table */
154 static struct qam64_snr_tab
{
157 } qam64_snr_tab
[] = {
225 /* QAM256 SNR lookup table */
226 static struct qam256_snr_tab
{
229 } qam256_snr_tab
[] = {
302 /* 8 bit registers, 16 bit values */
303 static int s5h1409_writereg(struct s5h1409_state
* state
, u8 reg
, u16 data
)
306 u8 buf
[] = { reg
, data
>> 8, data
& 0xff };
308 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
,
309 .flags
= 0, .buf
= buf
, .len
= 3 };
311 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
314 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
315 "ret == %i)\n", __func__
, reg
, data
, ret
);
317 return (ret
!= 1) ? -1 : 0;
320 static u16
s5h1409_readreg(struct s5h1409_state
* state
, u8 reg
)
326 struct i2c_msg msg
[] = {
327 { .addr
= state
->config
->demod_address
, .flags
= 0,
328 .buf
= b0
, .len
= 1 },
329 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
,
330 .buf
= b1
, .len
= 2 } };
332 ret
= i2c_transfer(state
->i2c
, msg
, 2);
335 printk("%s: readreg error (ret == %i)\n", __func__
, ret
);
336 return (b1
[0] << 8) | b1
[1];
339 static int s5h1409_softreset(struct dvb_frontend
* fe
)
341 struct s5h1409_state
* state
= fe
->demodulator_priv
;
343 dprintk("%s()\n", __func__
);
345 s5h1409_writereg(state
, 0xf5, 0);
346 s5h1409_writereg(state
, 0xf5, 1);
347 state
->is_qam_locked
= 0;
348 state
->qam_state
= 0;
352 #define S5H1409_VSB_IF_FREQ 5380
353 #define S5H1409_QAM_IF_FREQ state->config->qam_if
355 static int s5h1409_set_if_freq(struct dvb_frontend
* fe
, int KHz
)
357 struct s5h1409_state
* state
= fe
->demodulator_priv
;
359 dprintk("%s(%d KHz)\n", __func__
, KHz
);
363 s5h1409_writereg(state
, 0x87, 0x014b);
364 s5h1409_writereg(state
, 0x88, 0x0cb5);
365 s5h1409_writereg(state
, 0x89, 0x03e2);
370 s5h1409_writereg(state
, 0x87, 0x01be);
371 s5h1409_writereg(state
, 0x88, 0x0436);
372 s5h1409_writereg(state
, 0x89, 0x054d);
375 state
->if_freq
= KHz
;
380 static int s5h1409_set_spectralinversion(struct dvb_frontend
* fe
, int inverted
)
382 struct s5h1409_state
* state
= fe
->demodulator_priv
;
384 dprintk("%s(%d)\n", __func__
, inverted
);
387 return s5h1409_writereg(state
, 0x1b, 0x1101); /* Inverted */
389 return s5h1409_writereg(state
, 0x1b, 0x0110); /* Normal */
392 static int s5h1409_enable_modulation(struct dvb_frontend
* fe
,
395 struct s5h1409_state
* state
= fe
->demodulator_priv
;
397 dprintk("%s(0x%08x)\n", __func__
, m
);
401 dprintk("%s() VSB_8\n", __func__
);
402 if (state
->if_freq
!= S5H1409_VSB_IF_FREQ
)
403 s5h1409_set_if_freq(fe
, S5H1409_VSB_IF_FREQ
);
404 s5h1409_writereg(state
, 0xf4, 0);
408 dprintk("%s() QAM_AUTO (64/256)\n", __func__
);
409 if (state
->if_freq
!= S5H1409_QAM_IF_FREQ
)
410 s5h1409_set_if_freq(fe
, S5H1409_QAM_IF_FREQ
);
411 s5h1409_writereg(state
, 0xf4, 1);
412 s5h1409_writereg(state
, 0x85, 0x110);
415 dprintk("%s() Invalid modulation\n", __func__
);
419 state
->current_modulation
= m
;
420 s5h1409_softreset(fe
);
425 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
427 struct s5h1409_state
* state
= fe
->demodulator_priv
;
429 dprintk("%s(%d)\n", __func__
, enable
);
432 return s5h1409_writereg(state
, 0xf3, 1);
434 return s5h1409_writereg(state
, 0xf3, 0);
437 static int s5h1409_set_gpio(struct dvb_frontend
* fe
, int enable
)
439 struct s5h1409_state
* state
= fe
->demodulator_priv
;
441 dprintk("%s(%d)\n", __func__
, enable
);
444 return s5h1409_writereg(state
, 0xe3,
445 s5h1409_readreg(state
, 0xe3) | 0x1100);
447 return s5h1409_writereg(state
, 0xe3,
448 s5h1409_readreg(state
, 0xe3) & 0xfeff);
451 static int s5h1409_sleep(struct dvb_frontend
* fe
, int enable
)
453 struct s5h1409_state
* state
= fe
->demodulator_priv
;
455 dprintk("%s(%d)\n", __func__
, enable
);
457 return s5h1409_writereg(state
, 0xf2, enable
);
460 static int s5h1409_register_reset(struct dvb_frontend
* fe
)
462 struct s5h1409_state
* state
= fe
->demodulator_priv
;
464 dprintk("%s()\n", __func__
);
466 return s5h1409_writereg(state
, 0xfa, 0);
469 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend
*fe
)
471 struct s5h1409_state
*state
= fe
->demodulator_priv
;
474 if (state
->is_qam_locked
)
477 /* QAM EQ lock check */
478 reg
= s5h1409_readreg(state
, 0xf0);
480 if ((reg
>> 13) & 0x1) {
482 state
->is_qam_locked
= 1;
485 s5h1409_writereg(state
, 0x96, 0x00c);
486 if ((reg
< 0x38) || (reg
> 0x68) ) {
487 s5h1409_writereg(state
, 0x93, 0x3332);
488 s5h1409_writereg(state
, 0x9e, 0x2c37);
490 s5h1409_writereg(state
, 0x93, 0x3130);
491 s5h1409_writereg(state
, 0x9e, 0x2836);
495 s5h1409_writereg(state
, 0x96, 0x0008);
496 s5h1409_writereg(state
, 0x93, 0x3332);
497 s5h1409_writereg(state
, 0x9e, 0x2c37);
501 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend
*fe
)
503 struct s5h1409_state
*state
= fe
->demodulator_priv
;
506 reg
= s5h1409_readreg(state
, 0xf1);
509 if ((reg
>> 15) & 0x1) {
510 if (state
->qam_state
!= 2) {
511 state
->qam_state
= 2;
512 reg1
= s5h1409_readreg(state
, 0xb2);
513 reg2
= s5h1409_readreg(state
, 0xad);
515 s5h1409_writereg(state
, 0x96, 0x20);
516 s5h1409_writereg(state
, 0xad,
517 ( ((reg1
& 0xf000) >> 4) | (reg2
& 0xf0ff)) );
518 s5h1409_writereg(state
, 0xab,
519 s5h1409_readreg(state
, 0xab) & 0xeffe);
522 if (state
->qam_state
!= 1) {
523 state
->qam_state
= 1;
524 s5h1409_writereg(state
, 0x96, 0x08);
525 s5h1409_writereg(state
, 0xab,
526 s5h1409_readreg(state
, 0xab) | 0x1001);
531 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
532 static int s5h1409_set_frontend (struct dvb_frontend
* fe
,
533 struct dvb_frontend_parameters
*p
)
535 struct s5h1409_state
* state
= fe
->demodulator_priv
;
537 dprintk("%s(frequency=%d)\n", __func__
, p
->frequency
);
539 s5h1409_softreset(fe
);
541 state
->current_frequency
= p
->frequency
;
543 s5h1409_enable_modulation(fe
, p
->u
.vsb
.modulation
);
545 /* Allow the demod to settle */
548 if (fe
->ops
.tuner_ops
.set_params
) {
549 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 1);
550 fe
->ops
.tuner_ops
.set_params(fe
, p
);
551 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
554 /* Optimize the demod for QAM */
555 if (p
->u
.vsb
.modulation
!= VSB_8
) {
556 s5h1409_set_qam_amhum_mode(fe
);
557 s5h1409_set_qam_interleave_mode(fe
);
563 static int s5h1409_set_mpeg_timing(struct dvb_frontend
*fe
, int mode
)
565 struct s5h1409_state
*state
= fe
->demodulator_priv
;
568 dprintk("%s(%d)\n", __func__
, mode
);
570 val
= s5h1409_readreg(state
, 0xac) & 0xcfff;
572 case S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK
:
575 case S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
:
576 dprintk("%s(%d) Mode1 or Defaulting\n", __func__
, mode
);
579 case S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK
:
582 case S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK
:
589 /* Configure MPEG Signal Timing charactistics */
590 return s5h1409_writereg(state
, 0xac, val
);
593 /* Reset the demod hardware and reset all of the configuration registers
594 to a default state. */
595 static int s5h1409_init (struct dvb_frontend
* fe
)
599 struct s5h1409_state
* state
= fe
->demodulator_priv
;
600 dprintk("%s()\n", __func__
);
602 s5h1409_sleep(fe
, 0);
603 s5h1409_register_reset(fe
);
605 for (i
=0; i
< ARRAY_SIZE(init_tab
); i
++)
606 s5h1409_writereg(state
, init_tab
[i
].reg
, init_tab
[i
].data
);
608 /* The datasheet says that after initialisation, VSB is default */
609 state
->current_modulation
= VSB_8
;
611 if (state
->config
->output_mode
== S5H1409_SERIAL_OUTPUT
)
612 s5h1409_writereg(state
, 0xab,
613 s5h1409_readreg(state
, 0xab) | 0x100); /* Serial */
615 s5h1409_writereg(state
, 0xab,
616 s5h1409_readreg(state
, 0xab) & 0xfeff); /* Parallel */
618 s5h1409_set_spectralinversion(fe
, state
->config
->inversion
);
619 s5h1409_set_if_freq(fe
, state
->if_freq
);
620 s5h1409_set_gpio(fe
, state
->config
->gpio
);
621 s5h1409_set_mpeg_timing(fe
, state
->config
->mpeg_timing
);
622 s5h1409_softreset(fe
);
624 /* Note: Leaving the I2C gate closed. */
625 s5h1409_i2c_gate_ctrl(fe
, 0);
630 static int s5h1409_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
632 struct s5h1409_state
* state
= fe
->demodulator_priv
;
634 u32 tuner_status
= 0;
638 /* Get the demodulator status */
639 reg
= s5h1409_readreg(state
, 0xf1);
641 *status
|= FE_HAS_VITERBI
;
643 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
645 switch(state
->config
->status_mode
) {
646 case S5H1409_DEMODLOCKING
:
647 if (*status
& FE_HAS_VITERBI
)
648 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
650 case S5H1409_TUNERLOCKING
:
651 /* Get the tuner status */
652 if (fe
->ops
.tuner_ops
.get_status
) {
653 if (fe
->ops
.i2c_gate_ctrl
)
654 fe
->ops
.i2c_gate_ctrl(fe
, 1);
656 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
658 if (fe
->ops
.i2c_gate_ctrl
)
659 fe
->ops
.i2c_gate_ctrl(fe
, 0);
662 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
666 dprintk("%s() status 0x%08x\n", __func__
, *status
);
671 static int s5h1409_qam256_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
673 int i
, ret
= -EINVAL
;
674 dprintk("%s()\n", __func__
);
676 for (i
=0; i
< ARRAY_SIZE(qam256_snr_tab
); i
++) {
677 if (v
< qam256_snr_tab
[i
].val
) {
678 *snr
= qam256_snr_tab
[i
].data
;
686 static int s5h1409_qam64_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
688 int i
, ret
= -EINVAL
;
689 dprintk("%s()\n", __func__
);
691 for (i
=0; i
< ARRAY_SIZE(qam64_snr_tab
); i
++) {
692 if (v
< qam64_snr_tab
[i
].val
) {
693 *snr
= qam64_snr_tab
[i
].data
;
701 static int s5h1409_vsb_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
703 int i
, ret
= -EINVAL
;
704 dprintk("%s()\n", __func__
);
706 for (i
=0; i
< ARRAY_SIZE(vsb_snr_tab
); i
++) {
707 if (v
> vsb_snr_tab
[i
].val
) {
708 *snr
= vsb_snr_tab
[i
].data
;
713 dprintk("%s() snr=%d\n", __func__
, *snr
);
717 static int s5h1409_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
719 struct s5h1409_state
* state
= fe
->demodulator_priv
;
721 dprintk("%s()\n", __func__
);
723 switch(state
->current_modulation
) {
725 reg
= s5h1409_readreg(state
, 0xf0) & 0xff;
726 return s5h1409_qam64_lookup_snr(fe
, snr
, reg
);
728 reg
= s5h1409_readreg(state
, 0xf0) & 0xff;
729 return s5h1409_qam256_lookup_snr(fe
, snr
, reg
);
731 reg
= s5h1409_readreg(state
, 0xf1) & 0x3ff;
732 return s5h1409_vsb_lookup_snr(fe
, snr
, reg
);
740 static int s5h1409_read_signal_strength(struct dvb_frontend
* fe
,
741 u16
* signal_strength
)
743 return s5h1409_read_snr(fe
, signal_strength
);
746 static int s5h1409_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
748 struct s5h1409_state
* state
= fe
->demodulator_priv
;
750 *ucblocks
= s5h1409_readreg(state
, 0xb5);
755 static int s5h1409_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
757 return s5h1409_read_ucblocks(fe
, ber
);
760 static int s5h1409_get_frontend(struct dvb_frontend
* fe
,
761 struct dvb_frontend_parameters
*p
)
763 struct s5h1409_state
* state
= fe
->demodulator_priv
;
765 p
->frequency
= state
->current_frequency
;
766 p
->u
.vsb
.modulation
= state
->current_modulation
;
771 static int s5h1409_get_tune_settings(struct dvb_frontend
* fe
,
772 struct dvb_frontend_tune_settings
*tune
)
774 tune
->min_delay_ms
= 1000;
778 static void s5h1409_release(struct dvb_frontend
* fe
)
780 struct s5h1409_state
* state
= fe
->demodulator_priv
;
784 static struct dvb_frontend_ops s5h1409_ops
;
786 struct dvb_frontend
* s5h1409_attach(const struct s5h1409_config
* config
,
787 struct i2c_adapter
* i2c
)
789 struct s5h1409_state
* state
= NULL
;
792 /* allocate memory for the internal state */
793 state
= kmalloc(sizeof(struct s5h1409_state
), GFP_KERNEL
);
797 /* setup the state */
798 state
->config
= config
;
800 state
->current_modulation
= 0;
801 state
->if_freq
= S5H1409_VSB_IF_FREQ
;
803 /* check if the demod exists */
804 reg
= s5h1409_readreg(state
, 0x04);
805 if ((reg
!= 0x0066) && (reg
!= 0x007f))
808 /* create dvb_frontend */
809 memcpy(&state
->frontend
.ops
, &s5h1409_ops
,
810 sizeof(struct dvb_frontend_ops
));
811 state
->frontend
.demodulator_priv
= state
;
813 if (s5h1409_init(&state
->frontend
) != 0) {
814 printk(KERN_ERR
"%s: Failed to initialize correctly\n",
819 /* Note: Leaving the I2C gate open here. */
820 s5h1409_i2c_gate_ctrl(&state
->frontend
, 1);
822 return &state
->frontend
;
829 static struct dvb_frontend_ops s5h1409_ops
= {
832 .name
= "Samsung S5H1409 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
840 .init
= s5h1409_init
,
841 .i2c_gate_ctrl
= s5h1409_i2c_gate_ctrl
,
842 .set_frontend
= s5h1409_set_frontend
,
843 .get_frontend
= s5h1409_get_frontend
,
844 .get_tune_settings
= s5h1409_get_tune_settings
,
845 .read_status
= s5h1409_read_status
,
846 .read_ber
= s5h1409_read_ber
,
847 .read_signal_strength
= s5h1409_read_signal_strength
,
848 .read_snr
= s5h1409_read_snr
,
849 .read_ucblocks
= s5h1409_read_ucblocks
,
850 .release
= s5h1409_release
,
853 module_param(debug
, int, 0644);
854 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
856 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
857 MODULE_AUTHOR("Steven Toth");
858 MODULE_LICENSE("GPL");
860 EXPORT_SYMBOL(s5h1409_attach
);