2 Samsung S5H1409 VSB/QAM demodulator driver
4 Copyright (C) 2006 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"
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 module_param(debug, int, 0644);
52 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
54 #define dprintk if (debug) printk
56 /* Register values to initialise the demod, this will set VSB by default */
57 static struct init_tab {
108 /* VSB SNR lookup table */
109 static struct vsb_snr_tab {
155 /* QAM64 SNR lookup table */
156 static struct qam64_snr_tab {
159 } qam64_snr_tab[] = {
227 /* QAM256 SNR lookup table */
228 static struct qam256_snr_tab {
231 } qam256_snr_tab[] = {
304 /* 8 bit registers, 16 bit values */
305 static int s5h1409_writereg(struct s5h1409_state *state, u8 reg, u16 data)
308 u8 buf[] = { reg, data >> 8, data & 0xff };
310 struct i2c_msg msg = { .addr = state->config->demod_address,
311 .flags = 0, .buf = buf, .len = 3 };
313 ret = i2c_transfer(state->i2c, &msg, 1);
316 printk(KERN_ERR "%s: error (reg == 0x%02x, val == 0x%04x, "
317 "ret == %i)\n", __func__, reg, data, ret);
319 return (ret != 1) ? -1 : 0;
322 static u16 s5h1409_readreg(struct s5h1409_state *state, u8 reg)
328 struct i2c_msg msg[] = {
329 { .addr = state->config->demod_address, .flags = 0,
330 .buf = b0, .len = 1 },
331 { .addr = state->config->demod_address, .flags = I2C_M_RD,
332 .buf = b1, .len = 2 } };
334 ret = i2c_transfer(state->i2c, msg, 2);
337 printk("%s: readreg error (ret == %i)\n", __func__, ret);
338 return (b1[0] << 8) | b1[1];
341 static int s5h1409_softreset(struct dvb_frontend *fe)
343 struct s5h1409_state *state = fe->demodulator_priv;
345 dprintk("%s()\n", __func__);
347 s5h1409_writereg(state, 0xf5, 0);
348 s5h1409_writereg(state, 0xf5, 1);
349 state->is_qam_locked = 0;
350 state->qam_state = 0;
354 #define S5H1409_VSB_IF_FREQ 5380
355 #define S5H1409_QAM_IF_FREQ (state->config->qam_if)
357 static int s5h1409_set_if_freq(struct dvb_frontend *fe, int KHz)
359 struct s5h1409_state *state = fe->demodulator_priv;
361 dprintk("%s(%d KHz)\n", __func__, KHz);
365 s5h1409_writereg(state, 0x87, 0x014b);
366 s5h1409_writereg(state, 0x88, 0x0cb5);
367 s5h1409_writereg(state, 0x89, 0x03e2);
372 s5h1409_writereg(state, 0x87, 0x01be);
373 s5h1409_writereg(state, 0x88, 0x0436);
374 s5h1409_writereg(state, 0x89, 0x054d);
377 state->if_freq = KHz;
382 static int s5h1409_set_spectralinversion(struct dvb_frontend *fe, int inverted)
384 struct s5h1409_state *state = fe->demodulator_priv;
386 dprintk("%s(%d)\n", __func__, inverted);
389 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
391 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
394 static int s5h1409_enable_modulation(struct dvb_frontend *fe,
397 struct s5h1409_state *state = fe->demodulator_priv;
399 dprintk("%s(0x%08x)\n", __func__, m);
403 dprintk("%s() VSB_8\n", __func__);
404 if (state->if_freq != S5H1409_VSB_IF_FREQ)
405 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
406 s5h1409_writereg(state, 0xf4, 0);
411 dprintk("%s() QAM_AUTO (64/256)\n", __func__);
412 if (state->if_freq != S5H1409_QAM_IF_FREQ)
413 s5h1409_set_if_freq(fe, S5H1409_QAM_IF_FREQ);
414 s5h1409_writereg(state, 0xf4, 1);
415 s5h1409_writereg(state, 0x85, 0x110);
418 dprintk("%s() Invalid modulation\n", __func__);
422 state->current_modulation = m;
423 s5h1409_softreset(fe);
428 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
430 struct s5h1409_state *state = fe->demodulator_priv;
432 dprintk("%s(%d)\n", __func__, enable);
435 return s5h1409_writereg(state, 0xf3, 1);
437 return s5h1409_writereg(state, 0xf3, 0);
440 static int s5h1409_set_gpio(struct dvb_frontend *fe, int enable)
442 struct s5h1409_state *state = fe->demodulator_priv;
444 dprintk("%s(%d)\n", __func__, enable);
447 return s5h1409_writereg(state, 0xe3,
448 s5h1409_readreg(state, 0xe3) | 0x1100);
450 return s5h1409_writereg(state, 0xe3,
451 s5h1409_readreg(state, 0xe3) & 0xfeff);
454 static int s5h1409_sleep(struct dvb_frontend *fe, int enable)
456 struct s5h1409_state *state = fe->demodulator_priv;
458 dprintk("%s(%d)\n", __func__, enable);
460 return s5h1409_writereg(state, 0xf2, enable);
463 static int s5h1409_register_reset(struct dvb_frontend *fe)
465 struct s5h1409_state *state = fe->demodulator_priv;
467 dprintk("%s()\n", __func__);
469 return s5h1409_writereg(state, 0xfa, 0);
472 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
474 struct s5h1409_state *state = fe->demodulator_priv;
477 if (state->is_qam_locked)
480 /* QAM EQ lock check */
481 reg = s5h1409_readreg(state, 0xf0);
483 if ((reg >> 13) & 0x1) {
485 state->is_qam_locked = 1;
488 s5h1409_writereg(state, 0x96, 0x00c);
489 if ((reg < 0x38) || (reg > 0x68)) {
490 s5h1409_writereg(state, 0x93, 0x3332);
491 s5h1409_writereg(state, 0x9e, 0x2c37);
493 s5h1409_writereg(state, 0x93, 0x3130);
494 s5h1409_writereg(state, 0x9e, 0x2836);
498 s5h1409_writereg(state, 0x96, 0x0008);
499 s5h1409_writereg(state, 0x93, 0x3332);
500 s5h1409_writereg(state, 0x9e, 0x2c37);
504 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
506 struct s5h1409_state *state = fe->demodulator_priv;
509 reg = s5h1409_readreg(state, 0xf1);
512 if ((reg >> 15) & 0x1) {
513 if (state->qam_state != 2) {
514 state->qam_state = 2;
515 reg1 = s5h1409_readreg(state, 0xb2);
516 reg2 = s5h1409_readreg(state, 0xad);
518 s5h1409_writereg(state, 0x96, 0x20);
519 s5h1409_writereg(state, 0xad,
520 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
521 s5h1409_writereg(state, 0xab,
522 s5h1409_readreg(state, 0xab) & 0xeffe);
525 if (state->qam_state != 1) {
526 state->qam_state = 1;
527 s5h1409_writereg(state, 0x96, 0x08);
528 s5h1409_writereg(state, 0xab,
529 s5h1409_readreg(state, 0xab) | 0x1001);
534 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
535 static int s5h1409_set_frontend(struct dvb_frontend *fe,
536 struct dvb_frontend_parameters *p)
538 struct s5h1409_state *state = fe->demodulator_priv;
540 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
542 s5h1409_softreset(fe);
544 state->current_frequency = p->frequency;
546 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
548 /* Allow the demod to settle */
551 if (fe->ops.tuner_ops.set_params) {
552 if (fe->ops.i2c_gate_ctrl)
553 fe->ops.i2c_gate_ctrl(fe, 1);
554 fe->ops.tuner_ops.set_params(fe, p);
555 if (fe->ops.i2c_gate_ctrl)
556 fe->ops.i2c_gate_ctrl(fe, 0);
559 /* Optimize the demod for QAM */
560 if (p->u.vsb.modulation != VSB_8) {
561 s5h1409_set_qam_amhum_mode(fe);
562 s5h1409_set_qam_interleave_mode(fe);
568 static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
570 struct s5h1409_state *state = fe->demodulator_priv;
573 dprintk("%s(%d)\n", __func__, mode);
575 val = s5h1409_readreg(state, 0xac) & 0xcfff;
577 case S5H1409_MPEGTIMING_CONTINOUS_INVERTING_CLOCK:
580 case S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK:
581 dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
584 case S5H1409_MPEGTIMING_NONCONTINOUS_INVERTING_CLOCK:
587 case S5H1409_MPEGTIMING_NONCONTINOUS_NONINVERTING_CLOCK:
594 /* Configure MPEG Signal Timing charactistics */
595 return s5h1409_writereg(state, 0xac, val);
598 /* Reset the demod hardware and reset all of the configuration registers
599 to a default state. */
600 static int s5h1409_init(struct dvb_frontend *fe)
604 struct s5h1409_state *state = fe->demodulator_priv;
605 dprintk("%s()\n", __func__);
607 s5h1409_sleep(fe, 0);
608 s5h1409_register_reset(fe);
610 for (i = 0; i < ARRAY_SIZE(init_tab); i++)
611 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
613 /* The datasheet says that after initialisation, VSB is default */
614 state->current_modulation = VSB_8;
616 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
617 s5h1409_writereg(state, 0xab,
618 s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
620 s5h1409_writereg(state, 0xab,
621 s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
623 s5h1409_set_spectralinversion(fe, state->config->inversion);
624 s5h1409_set_if_freq(fe, state->if_freq);
625 s5h1409_set_gpio(fe, state->config->gpio);
626 s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
627 s5h1409_softreset(fe);
629 /* Note: Leaving the I2C gate closed. */
630 s5h1409_i2c_gate_ctrl(fe, 0);
635 static int s5h1409_read_status(struct dvb_frontend *fe, fe_status_t *status)
637 struct s5h1409_state *state = fe->demodulator_priv;
639 u32 tuner_status = 0;
643 /* Get the demodulator status */
644 reg = s5h1409_readreg(state, 0xf1);
646 *status |= FE_HAS_VITERBI;
648 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
650 switch (state->config->status_mode) {
651 case S5H1409_DEMODLOCKING:
652 if (*status & FE_HAS_VITERBI)
653 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
655 case S5H1409_TUNERLOCKING:
656 /* Get the tuner status */
657 if (fe->ops.tuner_ops.get_status) {
658 if (fe->ops.i2c_gate_ctrl)
659 fe->ops.i2c_gate_ctrl(fe, 1);
661 fe->ops.tuner_ops.get_status(fe, &tuner_status);
663 if (fe->ops.i2c_gate_ctrl)
664 fe->ops.i2c_gate_ctrl(fe, 0);
667 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
671 dprintk("%s() status 0x%08x\n", __func__, *status);
676 static int s5h1409_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
678 int i, ret = -EINVAL;
679 dprintk("%s()\n", __func__);
681 for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
682 if (v < qam256_snr_tab[i].val) {
683 *snr = qam256_snr_tab[i].data;
691 static int s5h1409_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
693 int i, ret = -EINVAL;
694 dprintk("%s()\n", __func__);
696 for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
697 if (v < qam64_snr_tab[i].val) {
698 *snr = qam64_snr_tab[i].data;
706 static int s5h1409_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
708 int i, ret = -EINVAL;
709 dprintk("%s()\n", __func__);
711 for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
712 if (v > vsb_snr_tab[i].val) {
713 *snr = vsb_snr_tab[i].data;
718 dprintk("%s() snr=%d\n", __func__, *snr);
722 static int s5h1409_read_snr(struct dvb_frontend *fe, u16 *snr)
724 struct s5h1409_state *state = fe->demodulator_priv;
726 dprintk("%s()\n", __func__);
728 switch (state->current_modulation) {
730 reg = s5h1409_readreg(state, 0xf0) & 0xff;
731 return s5h1409_qam64_lookup_snr(fe, snr, reg);
733 reg = s5h1409_readreg(state, 0xf0) & 0xff;
734 return s5h1409_qam256_lookup_snr(fe, snr, reg);
736 reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
737 return s5h1409_vsb_lookup_snr(fe, snr, reg);
745 static int s5h1409_read_signal_strength(struct dvb_frontend *fe,
746 u16 *signal_strength)
748 return s5h1409_read_snr(fe, signal_strength);
751 static int s5h1409_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
753 struct s5h1409_state *state = fe->demodulator_priv;
755 *ucblocks = s5h1409_readreg(state, 0xb5);
760 static int s5h1409_read_ber(struct dvb_frontend *fe, u32 *ber)
762 return s5h1409_read_ucblocks(fe, ber);
765 static int s5h1409_get_frontend(struct dvb_frontend *fe,
766 struct dvb_frontend_parameters *p)
768 struct s5h1409_state *state = fe->demodulator_priv;
770 p->frequency = state->current_frequency;
771 p->u.vsb.modulation = state->current_modulation;
776 static int s5h1409_get_tune_settings(struct dvb_frontend *fe,
777 struct dvb_frontend_tune_settings *tune)
779 tune->min_delay_ms = 1000;
783 static void s5h1409_release(struct dvb_frontend *fe)
785 struct s5h1409_state *state = fe->demodulator_priv;
789 static struct dvb_frontend_ops s5h1409_ops;
791 struct dvb_frontend *s5h1409_attach(const struct s5h1409_config *config,
792 struct i2c_adapter *i2c)
794 struct s5h1409_state *state = NULL;
797 /* allocate memory for the internal state */
798 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
802 /* setup the state */
803 state->config = config;
805 state->current_modulation = 0;
806 state->if_freq = S5H1409_VSB_IF_FREQ;
808 /* check if the demod exists */
809 reg = s5h1409_readreg(state, 0x04);
810 if ((reg != 0x0066) && (reg != 0x007f))
813 /* create dvb_frontend */
814 memcpy(&state->frontend.ops, &s5h1409_ops,
815 sizeof(struct dvb_frontend_ops));
816 state->frontend.demodulator_priv = state;
818 if (s5h1409_init(&state->frontend) != 0) {
819 printk(KERN_ERR "%s: Failed to initialize correctly\n",
824 /* Note: Leaving the I2C gate open here. */
825 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
827 return &state->frontend;
833 EXPORT_SYMBOL(s5h1409_attach);
835 static struct dvb_frontend_ops s5h1409_ops = {
838 .name = "Samsung S5H1409 QAM/8VSB Frontend",
840 .frequency_min = 54000000,
841 .frequency_max = 858000000,
842 .frequency_stepsize = 62500,
843 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
846 .init = s5h1409_init,
847 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
848 .set_frontend = s5h1409_set_frontend,
849 .get_frontend = s5h1409_get_frontend,
850 .get_tune_settings = s5h1409_get_tune_settings,
851 .read_status = s5h1409_read_status,
852 .read_ber = s5h1409_read_ber,
853 .read_signal_strength = s5h1409_read_signal_strength,
854 .read_snr = s5h1409_read_snr,
855 .read_ucblocks = s5h1409_read_ucblocks,
856 .release = s5h1409_release,
859 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
860 MODULE_AUTHOR("Steven Toth");
861 MODULE_LICENSE("GPL");