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"
31 struct s5h1409_state {
33 struct i2c_adapter* i2c;
35 /* configuration settings */
36 const struct s5h1409_config* config;
38 struct dvb_frontend frontend;
40 /* previous uncorrected block counter */
41 fe_modulation_t current_modulation;
43 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", __func__, 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", __func__, 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", __func__);
344 s5h1409_writereg(state, 0xf5, 0);
345 s5h1409_writereg(state, 0xf5, 1);
346 state->is_qam_locked = 0;
347 state->qam_state = 0;
351 #define S5H1409_VSB_IF_FREQ 5380
352 #define S5H1409_QAM_IF_FREQ state->config->qam_if
354 static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
356 struct s5h1409_state* state = fe->demodulator_priv;
358 dprintk("%s(%d KHz)\n", __func__, KHz);
362 s5h1409_writereg(state, 0x87, 0x014b);
363 s5h1409_writereg(state, 0x88, 0x0cb5);
364 s5h1409_writereg(state, 0x89, 0x03e2);
369 s5h1409_writereg(state, 0x87, 0x01be);
370 s5h1409_writereg(state, 0x88, 0x0436);
371 s5h1409_writereg(state, 0x89, 0x054d);
374 state->if_freq = KHz;
379 static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
381 struct s5h1409_state* state = fe->demodulator_priv;
383 dprintk("%s(%d)\n", __func__, inverted);
386 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
388 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
391 static int s5h1409_enable_modulation(struct dvb_frontend* fe,
394 struct s5h1409_state* state = fe->demodulator_priv;
396 dprintk("%s(0x%08x)\n", __func__, m);
400 dprintk("%s() VSB_8\n", __func__);
401 if (state->if_freq != S5H1409_VSB_IF_FREQ)
402 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
403 s5h1409_writereg(state, 0xf4, 0);
407 dprintk("%s() QAM_AUTO (64/256)\n", __func__);
408 if (state->if_freq != S5H1409_QAM_IF_FREQ)
409 s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
410 s5h1409_writereg(state, 0xf4, 1);
411 s5h1409_writereg(state, 0x85, 0x110);
414 dprintk("%s() Invalid modulation\n", __func__);
418 state->current_modulation = m;
419 s5h1409_softreset(fe);
424 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
426 struct s5h1409_state* state = fe->demodulator_priv;
428 dprintk("%s(%d)\n", __func__, enable);
431 return s5h1409_writereg(state, 0xf3, 1);
433 return s5h1409_writereg(state, 0xf3, 0);
436 static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
438 struct s5h1409_state* state = fe->demodulator_priv;
440 dprintk("%s(%d)\n", __func__, enable);
443 return s5h1409_writereg(state, 0xe3,
444 s5h1409_readreg(state, 0xe3) | 0x1100);
446 return s5h1409_writereg(state, 0xe3,
447 s5h1409_readreg(state, 0xe3) & 0xfeff);
450 static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
452 struct s5h1409_state* state = fe->demodulator_priv;
454 dprintk("%s(%d)\n", __func__, enable);
456 return s5h1409_writereg(state, 0xf2, enable);
459 static int s5h1409_register_reset(struct dvb_frontend* fe)
461 struct s5h1409_state* state = fe->demodulator_priv;
463 dprintk("%s()\n", __func__);
465 return s5h1409_writereg(state, 0xfa, 0);
468 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
470 struct s5h1409_state *state = fe->demodulator_priv;
473 if (state->is_qam_locked)
476 /* QAM EQ lock check */
477 reg = s5h1409_readreg(state, 0xf0);
479 if ((reg >> 13) & 0x1) {
481 state->is_qam_locked = 1;
484 s5h1409_writereg(state, 0x96, 0x00c);
485 if ((reg < 0x38) || (reg > 0x68) ) {
486 s5h1409_writereg(state, 0x93, 0x3332);
487 s5h1409_writereg(state, 0x9e, 0x2c37);
489 s5h1409_writereg(state, 0x93, 0x3130);
490 s5h1409_writereg(state, 0x9e, 0x2836);
494 s5h1409_writereg(state, 0x96, 0x0008);
495 s5h1409_writereg(state, 0x93, 0x3332);
496 s5h1409_writereg(state, 0x9e, 0x2c37);
500 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
502 struct s5h1409_state *state = fe->demodulator_priv;
505 reg = s5h1409_readreg(state, 0xf1);
508 if ((reg >> 15) & 0x1) {
509 if (state->qam_state != 2) {
510 state->qam_state = 2;
511 reg1 = s5h1409_readreg(state, 0xb2);
512 reg2 = s5h1409_readreg(state, 0xad);
514 s5h1409_writereg(state, 0x96, 0x20);
515 s5h1409_writereg(state, 0xad,
516 ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
517 s5h1409_writereg(state, 0xab,
518 s5h1409_readreg(state, 0xab) & 0xeffe);
521 if (state->qam_state != 1) {
522 state->qam_state = 1;
523 s5h1409_writereg(state, 0x96, 0x08);
524 s5h1409_writereg(state, 0xab,
525 s5h1409_readreg(state, 0xab) | 0x1001);
530 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
531 static int s5h1409_set_frontend (struct dvb_frontend* fe,
532 struct dvb_frontend_parameters *p)
534 struct s5h1409_state* state = fe->demodulator_priv;
536 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
538 s5h1409_softreset(fe);
540 state->current_frequency = p->frequency;
542 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
544 /* Allow the demod to settle */
547 if (fe->ops.tuner_ops.set_params) {
548 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
549 fe->ops.tuner_ops.set_params(fe, p);
550 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
553 /* Optimize the demod for QAM */
554 if (p->u.vsb.modulation != VSB_8) {
555 s5h1409_set_qam_amhum_mode(fe);
556 s5h1409_set_qam_interleave_mode(fe);
562 static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
564 struct s5h1409_state *state = fe->demodulator_priv;
567 dprintk("%s(%d)\n", __func__, mode);
569 val = s5h1409_readreg(state, 0xac) & 0xcfff;
571 case S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
574 case S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
575 dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
578 case S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
581 case S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
588 /* Configure MPEG Signal Timing charactistics */
589 return s5h1409_writereg(state, 0xac, val);
592 /* Reset the demod hardware and reset all of the configuration registers
593 to a default state. */
594 static int s5h1409_init (struct dvb_frontend* fe)
598 struct s5h1409_state* state = fe->demodulator_priv;
599 dprintk("%s()\n", __func__);
601 s5h1409_sleep(fe, 0);
602 s5h1409_register_reset(fe);
604 for (i=0; i < ARRAY_SIZE(init_tab); i++)
605 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
607 /* The datasheet says that after initialisation, VSB is default */
608 state->current_modulation = VSB_8;
610 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
611 s5h1409_writereg(state, 0xab,
612 s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
614 s5h1409_writereg(state, 0xab,
615 s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
617 s5h1409_set_spectralinversion(fe, state->config->inversion);
618 s5h1409_set_if_freq(fe, state->if_freq);
619 s5h1409_set_gpio(fe, state->config->gpio);
620 s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
621 s5h1409_softreset(fe);
623 /* Note: Leaving the I2C gate closed. */
624 s5h1409_i2c_gate_ctrl(fe, 0);
629 static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
631 struct s5h1409_state* state = fe->demodulator_priv;
633 u32 tuner_status = 0;
637 /* Get the demodulator status */
638 reg = s5h1409_readreg(state, 0xf1);
640 *status |= FE_HAS_VITERBI;
642 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
644 switch(state->config->status_mode) {
645 case S5H1409_DEMODLOCKING:
646 if (*status & FE_HAS_VITERBI)
647 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
649 case S5H1409_TUNERLOCKING:
650 /* Get the tuner status */
651 if (fe->ops.tuner_ops.get_status) {
652 if (fe->ops.i2c_gate_ctrl)
653 fe->ops.i2c_gate_ctrl(fe, 1);
655 fe->ops.tuner_ops.get_status(fe, &tuner_status);
657 if (fe->ops.i2c_gate_ctrl)
658 fe->ops.i2c_gate_ctrl(fe, 0);
661 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
665 dprintk("%s() status 0x%08x\n", __func__, *status);
670 static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
672 int i, ret = -EINVAL;
673 dprintk("%s()\n", __func__);
675 for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
676 if (v < qam256_snr_tab[i].val) {
677 *snr = qam256_snr_tab[i].data;
685 static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
687 int i, ret = -EINVAL;
688 dprintk("%s()\n", __func__);
690 for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
691 if (v < qam64_snr_tab[i].val) {
692 *snr = qam64_snr_tab[i].data;
700 static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
702 int i, ret = -EINVAL;
703 dprintk("%s()\n", __func__);
705 for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
706 if (v > vsb_snr_tab[i].val) {
707 *snr = vsb_snr_tab[i].data;
712 dprintk("%s() snr=%d\n", __func__, *snr);
716 static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
718 struct s5h1409_state* state = fe->demodulator_priv;
720 dprintk("%s()\n", __func__);
722 switch(state->current_modulation) {
724 reg = s5h1409_readreg(state, 0xf0) & 0xff;
725 return s5h1409_qam64_lookup_snr(fe, snr, reg);
727 reg = s5h1409_readreg(state, 0xf0) & 0xff;
728 return s5h1409_qam256_lookup_snr(fe, snr, reg);
730 reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
731 return s5h1409_vsb_lookup_snr(fe, snr, reg);
739 static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
740 u16* signal_strength)
742 return s5h1409_read_snr(fe, signal_strength);
745 static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
747 struct s5h1409_state* state = fe->demodulator_priv;
749 *ucblocks = s5h1409_readreg(state, 0xb5);
754 static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
756 return s5h1409_read_ucblocks(fe, ber);
759 static int s5h1409_get_frontend(struct dvb_frontend* fe,
760 struct dvb_frontend_parameters *p)
762 struct s5h1409_state* state = fe->demodulator_priv;
764 p->frequency = state->current_frequency;
765 p->u.vsb.modulation = state->current_modulation;
770 static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
771 struct dvb_frontend_tune_settings *tune)
773 tune->min_delay_ms = 1000;
777 static void s5h1409_release(struct dvb_frontend* fe)
779 struct s5h1409_state* state = fe->demodulator_priv;
783 static struct dvb_frontend_ops s5h1409_ops;
785 struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
786 struct i2c_adapter* i2c)
788 struct s5h1409_state* state = NULL;
791 /* allocate memory for the internal state */
792 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
796 /* setup the state */
797 state->config = config;
799 state->current_modulation = 0;
800 state->if_freq = S5H1409_VSB_IF_FREQ;
802 /* check if the demod exists */
803 reg = s5h1409_readreg(state, 0x04);
804 if ((reg != 0x0066) && (reg != 0x007f))
807 /* create dvb_frontend */
808 memcpy(&state->frontend.ops, &s5h1409_ops,
809 sizeof(struct dvb_frontend_ops));
810 state->frontend.demodulator_priv = state;
812 if (s5h1409_init(&state->frontend) != 0) {
813 printk(KERN_ERR "%s: Failed to initialize correctly\n",
818 /* Note: Leaving the I2C gate open here. */
819 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
821 return &state->frontend;
828 static struct dvb_frontend_ops s5h1409_ops = {
831 .name = "Samsung S5H1409 QAM/8VSB Frontend",
833 .frequency_min = 54000000,
834 .frequency_max = 858000000,
835 .frequency_stepsize = 62500,
836 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
839 .init = s5h1409_init,
840 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
841 .set_frontend = s5h1409_set_frontend,
842 .get_frontend = s5h1409_get_frontend,
843 .get_tune_settings = s5h1409_get_tune_settings,
844 .read_status = s5h1409_read_status,
845 .read_ber = s5h1409_read_ber,
846 .read_signal_strength = s5h1409_read_signal_strength,
847 .read_snr = s5h1409_read_snr,
848 .read_ucblocks = s5h1409_read_ucblocks,
849 .release = s5h1409_release,
852 module_param(debug, int, 0644);
853 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
855 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
856 MODULE_AUTHOR("Steven Toth");
857 MODULE_LICENSE("GPL");
859 EXPORT_SYMBOL(s5h1409_attach);