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
;
51 #define dprintk if (debug) printk
53 /* Register values to initialise the demod, this will set VSB by default */
54 static struct init_tab
{
105 /* VSB SNR lookup table */
106 static struct vsb_snr_tab
{
152 /* QAM64 SNR lookup table */
153 static struct qam64_snr_tab
{
156 } qam64_snr_tab
[] = {
224 /* QAM256 SNR lookup table */
225 static struct qam256_snr_tab
{
228 } qam256_snr_tab
[] = {
301 /* 8 bit registers, 16 bit values */
302 static int s5h1409_writereg(struct s5h1409_state
* state
, u8 reg
, u16 data
)
305 u8 buf
[] = { reg
, data
>> 8, data
& 0xff };
307 struct i2c_msg msg
= { .addr
= state
->config
->demod_address
,
308 .flags
= 0, .buf
= buf
, .len
= 3 };
310 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
313 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
314 "ret == %i)\n", __FUNCTION__
, reg
, data
, ret
);
316 return (ret
!= 1) ? -1 : 0;
319 static u16
s5h1409_readreg(struct s5h1409_state
* state
, u8 reg
)
325 struct i2c_msg msg
[] = {
326 { .addr
= state
->config
->demod_address
, .flags
= 0,
327 .buf
= b0
, .len
= 1 },
328 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
,
329 .buf
= b1
, .len
= 2 } };
331 ret
= i2c_transfer(state
->i2c
, msg
, 2);
334 printk("%s: readreg error (ret == %i)\n", __FUNCTION__
, ret
);
335 return (b1
[0] << 8) | b1
[1];
338 static int s5h1409_softreset(struct dvb_frontend
* fe
)
340 struct s5h1409_state
* state
= fe
->demodulator_priv
;
342 dprintk("%s()\n", __FUNCTION__
);
344 s5h1409_writereg(state
, 0xf5, 0);
345 s5h1409_writereg(state
, 0xf5, 1);
346 state
->is_qam_locked
= 0;
347 state
->qam_state
= 0;
351 static int s5h1409_set_if_freq(struct dvb_frontend
* fe
, int KHz
)
353 struct s5h1409_state
* state
= fe
->demodulator_priv
;
356 dprintk("%s(%d KHz)\n", __FUNCTION__
, KHz
);
358 if( (KHz
== 44000) || (KHz
== 5380) ) {
359 s5h1409_writereg(state
, 0x87, 0x01be);
360 s5h1409_writereg(state
, 0x88, 0x0436);
361 s5h1409_writereg(state
, 0x89, 0x054d);
364 s5h1409_writereg(state
, 0x87, 0x014b);
365 s5h1409_writereg(state
, 0x88, 0x0cb5);
366 s5h1409_writereg(state
, 0x89, 0x03e2);
368 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__
, KHz
);
375 static int s5h1409_set_spectralinversion(struct dvb_frontend
* fe
, int inverted
)
377 struct s5h1409_state
* state
= fe
->demodulator_priv
;
379 dprintk("%s(%d)\n", __FUNCTION__
, inverted
);
382 return s5h1409_writereg(state
, 0x1b, 0x1101); /* Inverted */
384 return s5h1409_writereg(state
, 0x1b, 0x0110); /* Normal */
387 static int s5h1409_enable_modulation(struct dvb_frontend
* fe
,
390 struct s5h1409_state
* state
= fe
->demodulator_priv
;
392 dprintk("%s(0x%08x)\n", __FUNCTION__
, m
);
396 dprintk("%s() VSB_8\n", __FUNCTION__
);
397 s5h1409_writereg(state
, 0xf4, 0);
401 dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__
);
402 s5h1409_writereg(state
, 0xf4, 1);
403 s5h1409_writereg(state
, 0x85, 0x110);
406 dprintk("%s() Invalid modulation\n", __FUNCTION__
);
410 state
->current_modulation
= m
;
411 s5h1409_softreset(fe
);
416 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
418 struct s5h1409_state
* state
= fe
->demodulator_priv
;
420 dprintk("%s(%d)\n", __FUNCTION__
, enable
);
423 return s5h1409_writereg(state
, 0xf3, 1);
425 return s5h1409_writereg(state
, 0xf3, 0);
428 static int s5h1409_set_gpio(struct dvb_frontend
* fe
, int enable
)
430 struct s5h1409_state
* state
= fe
->demodulator_priv
;
432 dprintk("%s(%d)\n", __FUNCTION__
, enable
);
435 return s5h1409_writereg(state
, 0xe3, 0x1100);
437 return s5h1409_writereg(state
, 0xe3, 0x1000);
440 static int s5h1409_sleep(struct dvb_frontend
* fe
, int enable
)
442 struct s5h1409_state
* state
= fe
->demodulator_priv
;
444 dprintk("%s(%d)\n", __FUNCTION__
, enable
);
446 return s5h1409_writereg(state
, 0xf2, enable
);
449 static int s5h1409_register_reset(struct dvb_frontend
* fe
)
451 struct s5h1409_state
* state
= fe
->demodulator_priv
;
453 dprintk("%s()\n", __FUNCTION__
);
455 return s5h1409_writereg(state
, 0xfa, 0);
458 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend
*fe
)
460 struct s5h1409_state
*state
= fe
->demodulator_priv
;
463 if (state
->is_qam_locked
)
466 /* QAM EQ lock check */
467 reg
= s5h1409_readreg(state
, 0xf0);
469 if ((reg
>> 13) & 0x1) {
471 state
->is_qam_locked
= 1;
474 s5h1409_writereg(state
, 0x96, 0x00c);
475 if ((reg
< 0x38) || (reg
> 0x68) ) {
476 s5h1409_writereg(state
, 0x93, 0x3332);
477 s5h1409_writereg(state
, 0x9e, 0x2c37);
479 s5h1409_writereg(state
, 0x93, 0x3130);
480 s5h1409_writereg(state
, 0x9e, 0x2836);
484 s5h1409_writereg(state
, 0x96, 0x0008);
485 s5h1409_writereg(state
, 0x93, 0x3332);
486 s5h1409_writereg(state
, 0x9e, 0x2c37);
490 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend
*fe
)
492 struct s5h1409_state
*state
= fe
->demodulator_priv
;
495 reg
= s5h1409_readreg(state
, 0xf1);
498 if ((reg
>> 15) & 0x1) {
499 if (state
->qam_state
!= 2) {
500 state
->qam_state
= 2;
501 reg1
= s5h1409_readreg(state
, 0xb2);
502 reg2
= s5h1409_readreg(state
, 0xad);
504 s5h1409_writereg(state
, 0x96, 0x20);
505 s5h1409_writereg(state
, 0xad,
506 ( ((reg1
& 0xf000) >> 4) | (reg2
& 0xf0ff)) );
507 s5h1409_writereg(state
, 0xab, 0x1100);
510 if (state
->qam_state
!= 1) {
511 state
->qam_state
= 1;
512 s5h1409_writereg(state
, 0x96, 0x08);
513 s5h1409_writereg(state
, 0xab, 0x1101);
518 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
519 static int s5h1409_set_frontend (struct dvb_frontend
* fe
,
520 struct dvb_frontend_parameters
*p
)
522 struct s5h1409_state
* state
= fe
->demodulator_priv
;
524 dprintk("%s(frequency=%d)\n", __FUNCTION__
, p
->frequency
);
526 s5h1409_softreset(fe
);
528 state
->current_frequency
= p
->frequency
;
530 s5h1409_enable_modulation(fe
, p
->u
.vsb
.modulation
);
532 /* Allow the demod to settle */
535 if (fe
->ops
.tuner_ops
.set_params
) {
536 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 1);
537 fe
->ops
.tuner_ops
.set_params(fe
, p
);
538 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
541 /* Optimize the demod for QAM */
542 if (p
->u
.vsb
.modulation
!= VSB_8
) {
543 s5h1409_set_qam_amhum_mode(fe
);
544 s5h1409_set_qam_interleave_mode(fe
);
550 /* Reset the demod hardware and reset all of the configuration registers
551 to a default state. */
552 static int s5h1409_init (struct dvb_frontend
* fe
)
556 struct s5h1409_state
* state
= fe
->demodulator_priv
;
557 dprintk("%s()\n", __FUNCTION__
);
559 s5h1409_sleep(fe
, 0);
560 s5h1409_register_reset(fe
);
562 for (i
=0; i
< ARRAY_SIZE(init_tab
); i
++)
563 s5h1409_writereg(state
, init_tab
[i
].reg
, init_tab
[i
].data
);
565 /* The datasheet says that after initialisation, VSB is default */
566 state
->current_modulation
= VSB_8
;
568 if (state
->config
->output_mode
== S5H1409_SERIAL_OUTPUT
)
569 s5h1409_writereg(state
, 0xab, 0x100); /* Serial */
571 s5h1409_writereg(state
, 0xab, 0x0); /* Parallel */
573 s5h1409_set_spectralinversion(fe
, state
->config
->inversion
);
574 s5h1409_set_if_freq(fe
, state
->config
->if_freq
);
575 s5h1409_set_gpio(fe
, state
->config
->gpio
);
576 s5h1409_softreset(fe
);
578 /* Note: Leaving the I2C gate closed. */
579 s5h1409_i2c_gate_ctrl(fe
, 0);
584 static int s5h1409_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
586 struct s5h1409_state
* state
= fe
->demodulator_priv
;
588 u32 tuner_status
= 0;
592 /* Get the demodulator status */
593 reg
= s5h1409_readreg(state
, 0xf1);
595 *status
|= FE_HAS_VITERBI
;
597 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
599 switch(state
->config
->status_mode
) {
600 case S5H1409_DEMODLOCKING
:
601 if (*status
& FE_HAS_VITERBI
)
602 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
604 case S5H1409_TUNERLOCKING
:
605 /* Get the tuner status */
606 if (fe
->ops
.tuner_ops
.get_status
) {
607 if (fe
->ops
.i2c_gate_ctrl
)
608 fe
->ops
.i2c_gate_ctrl(fe
, 1);
610 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
612 if (fe
->ops
.i2c_gate_ctrl
)
613 fe
->ops
.i2c_gate_ctrl(fe
, 0);
616 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
620 dprintk("%s() status 0x%08x\n", __FUNCTION__
, *status
);
625 static int s5h1409_qam256_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
627 int i
, ret
= -EINVAL
;
628 dprintk("%s()\n", __FUNCTION__
);
630 for (i
=0; i
< ARRAY_SIZE(qam256_snr_tab
); i
++) {
631 if (v
< qam256_snr_tab
[i
].val
) {
632 *snr
= qam256_snr_tab
[i
].data
;
640 static int s5h1409_qam64_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
642 int i
, ret
= -EINVAL
;
643 dprintk("%s()\n", __FUNCTION__
);
645 for (i
=0; i
< ARRAY_SIZE(qam64_snr_tab
); i
++) {
646 if (v
< qam64_snr_tab
[i
].val
) {
647 *snr
= qam64_snr_tab
[i
].data
;
655 static int s5h1409_vsb_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
657 int i
, ret
= -EINVAL
;
658 dprintk("%s()\n", __FUNCTION__
);
660 for (i
=0; i
< ARRAY_SIZE(vsb_snr_tab
); i
++) {
661 if (v
> vsb_snr_tab
[i
].val
) {
662 *snr
= vsb_snr_tab
[i
].data
;
667 dprintk("%s() snr=%d\n", __FUNCTION__
, *snr
);
671 static int s5h1409_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
673 struct s5h1409_state
* state
= fe
->demodulator_priv
;
675 dprintk("%s()\n", __FUNCTION__
);
677 switch(state
->current_modulation
) {
679 reg
= s5h1409_readreg(state
, 0xf0) & 0xff;
680 return s5h1409_qam64_lookup_snr(fe
, snr
, reg
);
682 reg
= s5h1409_readreg(state
, 0xf0) & 0xff;
683 return s5h1409_qam256_lookup_snr(fe
, snr
, reg
);
685 reg
= s5h1409_readreg(state
, 0xf1) & 0x3ff;
686 return s5h1409_vsb_lookup_snr(fe
, snr
, reg
);
694 static int s5h1409_read_signal_strength(struct dvb_frontend
* fe
,
695 u16
* signal_strength
)
697 return s5h1409_read_snr(fe
, signal_strength
);
700 static int s5h1409_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
702 struct s5h1409_state
* state
= fe
->demodulator_priv
;
704 *ucblocks
= s5h1409_readreg(state
, 0xb5);
709 static int s5h1409_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
711 return s5h1409_read_ucblocks(fe
, ber
);
714 static int s5h1409_get_frontend(struct dvb_frontend
* fe
,
715 struct dvb_frontend_parameters
*p
)
717 struct s5h1409_state
* state
= fe
->demodulator_priv
;
719 p
->frequency
= state
->current_frequency
;
720 p
->u
.vsb
.modulation
= state
->current_modulation
;
725 static int s5h1409_get_tune_settings(struct dvb_frontend
* fe
,
726 struct dvb_frontend_tune_settings
*tune
)
728 tune
->min_delay_ms
= 1000;
732 static void s5h1409_release(struct dvb_frontend
* fe
)
734 struct s5h1409_state
* state
= fe
->demodulator_priv
;
738 static struct dvb_frontend_ops s5h1409_ops
;
740 struct dvb_frontend
* s5h1409_attach(const struct s5h1409_config
* config
,
741 struct i2c_adapter
* i2c
)
743 struct s5h1409_state
* state
= NULL
;
745 /* allocate memory for the internal state */
746 state
= kmalloc(sizeof(struct s5h1409_state
), GFP_KERNEL
);
750 /* setup the state */
751 state
->config
= config
;
753 state
->current_modulation
= 0;
755 /* check if the demod exists */
756 if (s5h1409_readreg(state
, 0x04) != 0x0066)
759 /* create dvb_frontend */
760 memcpy(&state
->frontend
.ops
, &s5h1409_ops
,
761 sizeof(struct dvb_frontend_ops
));
762 state
->frontend
.demodulator_priv
= state
;
764 /* Note: Leaving the I2C gate open here. */
765 s5h1409_writereg(state
, 0xf3, 1);
767 return &state
->frontend
;
774 static struct dvb_frontend_ops s5h1409_ops
= {
777 .name
= "Samsung S5H1409 QAM/8VSB Frontend",
779 .frequency_min
= 54000000,
780 .frequency_max
= 858000000,
781 .frequency_stepsize
= 62500,
782 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
785 .init
= s5h1409_init
,
786 .i2c_gate_ctrl
= s5h1409_i2c_gate_ctrl
,
787 .set_frontend
= s5h1409_set_frontend
,
788 .get_frontend
= s5h1409_get_frontend
,
789 .get_tune_settings
= s5h1409_get_tune_settings
,
790 .read_status
= s5h1409_read_status
,
791 .read_ber
= s5h1409_read_ber
,
792 .read_signal_strength
= s5h1409_read_signal_strength
,
793 .read_snr
= s5h1409_read_snr
,
794 .read_ucblocks
= s5h1409_read_ucblocks
,
795 .release
= s5h1409_release
,
798 module_param(debug
, int, 0644);
799 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
801 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
802 MODULE_AUTHOR("Steven Toth");
803 MODULE_LICENSE("GPL");
805 EXPORT_SYMBOL(s5h1409_attach
);