1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Samsung S5H1409 VSB/QAM demodulator driver
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <media/dvb_frontend.h>
19 struct s5h1409_state {
21 struct i2c_adapter *i2c;
23 /* configuration settings */
24 const struct s5h1409_config *config;
26 struct dvb_frontend frontend;
28 /* previous uncorrected block counter */
29 enum fe_modulation current_modulation;
31 u32 current_frequency;
36 /* QAM tuning state goes through the following state transitions */
37 #define QAM_STATE_UNTUNED 0
38 #define QAM_STATE_TUNING_STARTED 1
39 #define QAM_STATE_INTERLEAVE_SET 2
40 #define QAM_STATE_QAM_OPTIMIZED_L1 3
41 #define QAM_STATE_QAM_OPTIMIZED_L2 4
42 #define QAM_STATE_QAM_OPTIMIZED_L3 5
47 module_param(debug, int, 0644);
48 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
50 #define dprintk if (debug) printk
52 /* Register values to initialise the demod, this will set VSB by default */
53 static struct init_tab {
104 /* VSB SNR lookup table */
105 static struct vsb_snr_tab {
151 /* QAM64 SNR lookup table */
152 static struct qam64_snr_tab {
155 } qam64_snr_tab[] = {
223 /* QAM256 SNR lookup table */
224 static struct qam256_snr_tab {
227 } qam256_snr_tab[] = {
300 /* 8 bit registers, 16 bit values */
301 static int s5h1409_writereg(struct s5h1409_state *state, u8 reg, u16 data)
304 u8 buf[] = { reg, data >> 8, data & 0xff };
306 struct i2c_msg msg = { .addr = state->config->demod_address,
307 .flags = 0, .buf = buf, .len = 3 };
309 ret = i2c_transfer(state->i2c, &msg, 1);
312 printk(KERN_ERR "%s: error (reg == 0x%02x, val == 0x%04x, ret == %i)\n",
313 __func__, reg, data, ret);
315 return (ret != 1) ? -1 : 0;
318 static u16 s5h1409_readreg(struct s5h1409_state *state, u8 reg)
324 struct i2c_msg msg[] = {
325 { .addr = state->config->demod_address, .flags = 0,
326 .buf = b0, .len = 1 },
327 { .addr = state->config->demod_address, .flags = I2C_M_RD,
328 .buf = b1, .len = 2 } };
330 ret = i2c_transfer(state->i2c, msg, 2);
333 printk("%s: readreg error (ret == %i)\n", __func__, ret);
334 return (b1[0] << 8) | b1[1];
337 static int s5h1409_softreset(struct dvb_frontend *fe)
339 struct s5h1409_state *state = fe->demodulator_priv;
341 dprintk("%s()\n", __func__);
343 s5h1409_writereg(state, 0xf5, 0);
344 s5h1409_writereg(state, 0xf5, 1);
345 state->is_qam_locked = 0;
346 state->qam_state = QAM_STATE_UNTUNED;
350 #define S5H1409_VSB_IF_FREQ 5380
351 #define S5H1409_QAM_IF_FREQ (state->config->qam_if)
353 static int s5h1409_set_if_freq(struct dvb_frontend *fe, int KHz)
355 struct s5h1409_state *state = fe->demodulator_priv;
357 dprintk("%s(%d KHz)\n", __func__, KHz);
361 s5h1409_writereg(state, 0x87, 0x014b);
362 s5h1409_writereg(state, 0x88, 0x0cb5);
363 s5h1409_writereg(state, 0x89, 0x03e2);
368 s5h1409_writereg(state, 0x87, 0x01be);
369 s5h1409_writereg(state, 0x88, 0x0436);
370 s5h1409_writereg(state, 0x89, 0x054d);
373 state->if_freq = KHz;
378 static int s5h1409_set_spectralinversion(struct dvb_frontend *fe, int inverted)
380 struct s5h1409_state *state = fe->demodulator_priv;
382 dprintk("%s(%d)\n", __func__, inverted);
385 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
387 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
390 static int s5h1409_enable_modulation(struct dvb_frontend *fe,
391 enum fe_modulation m)
393 struct s5h1409_state *state = fe->demodulator_priv;
395 dprintk("%s(0x%08x)\n", __func__, m);
399 dprintk("%s() VSB_8\n", __func__);
400 if (state->if_freq != S5H1409_VSB_IF_FREQ)
401 s5h1409_set_if_freq(fe, S5H1409_VSB_IF_FREQ);
402 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->qam_state < QAM_STATE_INTERLEAVE_SET) {
474 /* We should not perform amhum optimization until
475 the interleave mode has been configured */
479 if (state->qam_state == QAM_STATE_QAM_OPTIMIZED_L3) {
480 /* We've already reached the maximum optimization level, so
481 don't bother banging on the status registers */
485 /* QAM EQ lock check */
486 reg = s5h1409_readreg(state, 0xf0);
488 if ((reg >> 13) & 0x1) {
491 s5h1409_writereg(state, 0x96, 0x000c);
493 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L3) {
494 dprintk("%s() setting QAM state to OPT_L3\n",
496 s5h1409_writereg(state, 0x93, 0x3130);
497 s5h1409_writereg(state, 0x9e, 0x2836);
498 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L3;
501 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L2) {
502 dprintk("%s() setting QAM state to OPT_L2\n",
504 s5h1409_writereg(state, 0x93, 0x3332);
505 s5h1409_writereg(state, 0x9e, 0x2c37);
506 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L2;
511 if (state->qam_state < QAM_STATE_QAM_OPTIMIZED_L1) {
512 dprintk("%s() setting QAM state to OPT_L1\n", __func__);
513 s5h1409_writereg(state, 0x96, 0x0008);
514 s5h1409_writereg(state, 0x93, 0x3332);
515 s5h1409_writereg(state, 0x9e, 0x2c37);
516 state->qam_state = QAM_STATE_QAM_OPTIMIZED_L1;
521 static void s5h1409_set_qam_amhum_mode_legacy(struct dvb_frontend *fe)
523 struct s5h1409_state *state = fe->demodulator_priv;
526 if (state->is_qam_locked)
529 /* QAM EQ lock check */
530 reg = s5h1409_readreg(state, 0xf0);
532 if ((reg >> 13) & 0x1) {
534 state->is_qam_locked = 1;
537 s5h1409_writereg(state, 0x96, 0x00c);
538 if ((reg < 0x38) || (reg > 0x68)) {
539 s5h1409_writereg(state, 0x93, 0x3332);
540 s5h1409_writereg(state, 0x9e, 0x2c37);
542 s5h1409_writereg(state, 0x93, 0x3130);
543 s5h1409_writereg(state, 0x9e, 0x2836);
547 s5h1409_writereg(state, 0x96, 0x0008);
548 s5h1409_writereg(state, 0x93, 0x3332);
549 s5h1409_writereg(state, 0x9e, 0x2c37);
553 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
555 struct s5h1409_state *state = fe->demodulator_priv;
558 if (state->qam_state >= QAM_STATE_INTERLEAVE_SET) {
559 /* We've done the optimization already */
563 reg = s5h1409_readreg(state, 0xf1);
566 if ((reg >> 15) & 0x1) {
567 if (state->qam_state == QAM_STATE_UNTUNED ||
568 state->qam_state == QAM_STATE_TUNING_STARTED) {
569 dprintk("%s() setting QAM state to INTERLEAVE_SET\n",
571 reg1 = s5h1409_readreg(state, 0xb2);
572 reg2 = s5h1409_readreg(state, 0xad);
574 s5h1409_writereg(state, 0x96, 0x0020);
575 s5h1409_writereg(state, 0xad,
576 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
577 state->qam_state = QAM_STATE_INTERLEAVE_SET;
580 if (state->qam_state == QAM_STATE_UNTUNED) {
581 dprintk("%s() setting QAM state to TUNING_STARTED\n",
583 s5h1409_writereg(state, 0x96, 0x08);
584 s5h1409_writereg(state, 0xab,
585 s5h1409_readreg(state, 0xab) | 0x1001);
586 state->qam_state = QAM_STATE_TUNING_STARTED;
591 static void s5h1409_set_qam_interleave_mode_legacy(struct dvb_frontend *fe)
593 struct s5h1409_state *state = fe->demodulator_priv;
596 reg = s5h1409_readreg(state, 0xf1);
599 if ((reg >> 15) & 0x1) {
600 if (state->qam_state != 2) {
601 state->qam_state = 2;
602 reg1 = s5h1409_readreg(state, 0xb2);
603 reg2 = s5h1409_readreg(state, 0xad);
605 s5h1409_writereg(state, 0x96, 0x20);
606 s5h1409_writereg(state, 0xad,
607 (((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)));
608 s5h1409_writereg(state, 0xab,
609 s5h1409_readreg(state, 0xab) & 0xeffe);
612 if (state->qam_state != 1) {
613 state->qam_state = 1;
614 s5h1409_writereg(state, 0x96, 0x08);
615 s5h1409_writereg(state, 0xab,
616 s5h1409_readreg(state, 0xab) | 0x1001);
621 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
622 static int s5h1409_set_frontend(struct dvb_frontend *fe)
624 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
625 struct s5h1409_state *state = fe->demodulator_priv;
627 dprintk("%s(frequency=%d)\n", __func__, p->frequency);
629 s5h1409_softreset(fe);
631 state->current_frequency = p->frequency;
633 s5h1409_enable_modulation(fe, p->modulation);
635 if (fe->ops.tuner_ops.set_params) {
636 if (fe->ops.i2c_gate_ctrl)
637 fe->ops.i2c_gate_ctrl(fe, 1);
638 fe->ops.tuner_ops.set_params(fe);
639 if (fe->ops.i2c_gate_ctrl)
640 fe->ops.i2c_gate_ctrl(fe, 0);
643 /* Issue a reset to the demod so it knows to resync against the
644 newly tuned frequency */
645 s5h1409_softreset(fe);
647 /* Optimize the demod for QAM */
648 if (state->current_modulation != VSB_8) {
649 /* This almost certainly applies to all boards, but for now
650 only do it for the HVR-1600. Once the other boards are
651 tested, the "legacy" versions can just go away */
652 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
653 s5h1409_set_qam_interleave_mode(fe);
654 s5h1409_set_qam_amhum_mode(fe);
656 s5h1409_set_qam_amhum_mode_legacy(fe);
657 s5h1409_set_qam_interleave_mode_legacy(fe);
664 static int s5h1409_set_mpeg_timing(struct dvb_frontend *fe, int mode)
666 struct s5h1409_state *state = fe->demodulator_priv;
669 dprintk("%s(%d)\n", __func__, mode);
671 val = s5h1409_readreg(state, 0xac) & 0xcfff;
673 case S5H1409_MPEGTIMING_CONTINUOUS_INVERTING_CLOCK:
676 case S5H1409_MPEGTIMING_CONTINUOUS_NONINVERTING_CLOCK:
677 dprintk("%s(%d) Mode1 or Defaulting\n", __func__, mode);
680 case S5H1409_MPEGTIMING_NONCONTINUOUS_INVERTING_CLOCK:
683 case S5H1409_MPEGTIMING_NONCONTINUOUS_NONINVERTING_CLOCK:
690 /* Configure MPEG Signal Timing charactistics */
691 return s5h1409_writereg(state, 0xac, val);
694 /* Reset the demod hardware and reset all of the configuration registers
695 to a default state. */
696 static int s5h1409_init(struct dvb_frontend *fe)
700 struct s5h1409_state *state = fe->demodulator_priv;
701 dprintk("%s()\n", __func__);
703 s5h1409_sleep(fe, 0);
704 s5h1409_register_reset(fe);
706 for (i = 0; i < ARRAY_SIZE(init_tab); i++)
707 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
709 /* The datasheet says that after initialisation, VSB is default */
710 state->current_modulation = VSB_8;
712 /* Optimize for the HVR-1600 if appropriate. Note that some of these
713 may get folded into the generic case after testing with other
715 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
717 s5h1409_writereg(state, 0x09, 0x0050);
719 /* Unknown but Windows driver does it... */
720 s5h1409_writereg(state, 0x21, 0x0001);
721 s5h1409_writereg(state, 0x50, 0x030e);
724 s5h1409_writereg(state, 0x82, 0x0800);
727 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
728 s5h1409_writereg(state, 0xab,
729 s5h1409_readreg(state, 0xab) | 0x100); /* Serial */
731 s5h1409_writereg(state, 0xab,
732 s5h1409_readreg(state, 0xab) & 0xfeff); /* Parallel */
734 s5h1409_set_spectralinversion(fe, state->config->inversion);
735 s5h1409_set_if_freq(fe, state->if_freq);
736 s5h1409_set_gpio(fe, state->config->gpio);
737 s5h1409_set_mpeg_timing(fe, state->config->mpeg_timing);
738 s5h1409_softreset(fe);
740 /* Note: Leaving the I2C gate closed. */
741 s5h1409_i2c_gate_ctrl(fe, 0);
746 static int s5h1409_read_status(struct dvb_frontend *fe, enum fe_status *status)
748 struct s5h1409_state *state = fe->demodulator_priv;
750 u32 tuner_status = 0;
754 /* Optimize the demod for QAM */
755 if (state->current_modulation != VSB_8) {
756 /* This almost certainly applies to all boards, but for now
757 only do it for the HVR-1600. Once the other boards are
758 tested, the "legacy" versions can just go away */
759 if (state->config->hvr1600_opt == S5H1409_HVR1600_OPTIMIZE) {
760 s5h1409_set_qam_interleave_mode(fe);
761 s5h1409_set_qam_amhum_mode(fe);
765 /* Get the demodulator status */
766 reg = s5h1409_readreg(state, 0xf1);
768 *status |= FE_HAS_VITERBI;
770 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
772 switch (state->config->status_mode) {
773 case S5H1409_DEMODLOCKING:
774 if (*status & FE_HAS_VITERBI)
775 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
777 case S5H1409_TUNERLOCKING:
778 /* Get the tuner status */
779 if (fe->ops.tuner_ops.get_status) {
780 if (fe->ops.i2c_gate_ctrl)
781 fe->ops.i2c_gate_ctrl(fe, 1);
783 fe->ops.tuner_ops.get_status(fe, &tuner_status);
785 if (fe->ops.i2c_gate_ctrl)
786 fe->ops.i2c_gate_ctrl(fe, 0);
789 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
793 dprintk("%s() status 0x%08x\n", __func__, *status);
798 static int s5h1409_qam256_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
800 int i, ret = -EINVAL;
801 dprintk("%s()\n", __func__);
803 for (i = 0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
804 if (v < qam256_snr_tab[i].val) {
805 *snr = qam256_snr_tab[i].data;
813 static int s5h1409_qam64_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
815 int i, ret = -EINVAL;
816 dprintk("%s()\n", __func__);
818 for (i = 0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
819 if (v < qam64_snr_tab[i].val) {
820 *snr = qam64_snr_tab[i].data;
828 static int s5h1409_vsb_lookup_snr(struct dvb_frontend *fe, u16 *snr, u16 v)
830 int i, ret = -EINVAL;
831 dprintk("%s()\n", __func__);
833 for (i = 0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
834 if (v > vsb_snr_tab[i].val) {
835 *snr = vsb_snr_tab[i].data;
840 dprintk("%s() snr=%d\n", __func__, *snr);
844 static int s5h1409_read_snr(struct dvb_frontend *fe, u16 *snr)
846 struct s5h1409_state *state = fe->demodulator_priv;
848 dprintk("%s()\n", __func__);
850 switch (state->current_modulation) {
852 reg = s5h1409_readreg(state, 0xf0) & 0xff;
853 return s5h1409_qam64_lookup_snr(fe, snr, reg);
855 reg = s5h1409_readreg(state, 0xf0) & 0xff;
856 return s5h1409_qam256_lookup_snr(fe, snr, reg);
858 reg = s5h1409_readreg(state, 0xf1) & 0x3ff;
859 return s5h1409_vsb_lookup_snr(fe, snr, reg);
867 static int s5h1409_read_signal_strength(struct dvb_frontend *fe,
868 u16 *signal_strength)
870 /* borrowed from lgdt330x.c
872 * Calculate strength from SNR up to 35dB
873 * Even though the SNR can go higher than 35dB,
874 * there is some comfort factor in having a range of
875 * strong signals that can show at 100%
879 int ret = s5h1409_read_snr(fe, &snr);
881 *signal_strength = 0;
884 /* The following calculation method was chosen
885 * purely for the sake of code re-use from the
886 * other demod drivers that use this method */
888 /* Convert from SNR in dB * 10 to 8.24 fixed-point */
889 tmp = (snr * ((1 << 24) / 10));
891 /* Convert from 8.24 fixed-point to
892 * scale the range 0 - 35*2^24 into 0 - 65535*/
893 if (tmp >= 8960 * 0x10000)
894 *signal_strength = 0xffff;
896 *signal_strength = tmp / 8960;
902 static int s5h1409_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
904 struct s5h1409_state *state = fe->demodulator_priv;
906 *ucblocks = s5h1409_readreg(state, 0xb5);
911 static int s5h1409_read_ber(struct dvb_frontend *fe, u32 *ber)
913 return s5h1409_read_ucblocks(fe, ber);
916 static int s5h1409_get_frontend(struct dvb_frontend *fe,
917 struct dtv_frontend_properties *p)
919 struct s5h1409_state *state = fe->demodulator_priv;
921 p->frequency = state->current_frequency;
922 p->modulation = state->current_modulation;
927 static int s5h1409_get_tune_settings(struct dvb_frontend *fe,
928 struct dvb_frontend_tune_settings *tune)
930 tune->min_delay_ms = 1000;
934 static void s5h1409_release(struct dvb_frontend *fe)
936 struct s5h1409_state *state = fe->demodulator_priv;
940 static const struct dvb_frontend_ops s5h1409_ops;
942 struct dvb_frontend *s5h1409_attach(const struct s5h1409_config *config,
943 struct i2c_adapter *i2c)
945 struct s5h1409_state *state = NULL;
948 /* allocate memory for the internal state */
949 state = kzalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
953 /* setup the state */
954 state->config = config;
956 state->current_modulation = 0;
957 state->if_freq = S5H1409_VSB_IF_FREQ;
959 /* check if the demod exists */
960 reg = s5h1409_readreg(state, 0x04);
961 if ((reg != 0x0066) && (reg != 0x007f))
964 /* create dvb_frontend */
965 memcpy(&state->frontend.ops, &s5h1409_ops,
966 sizeof(struct dvb_frontend_ops));
967 state->frontend.demodulator_priv = state;
969 if (s5h1409_init(&state->frontend) != 0) {
970 printk(KERN_ERR "%s: Failed to initialize correctly\n",
975 /* Note: Leaving the I2C gate open here. */
976 s5h1409_i2c_gate_ctrl(&state->frontend, 1);
978 return &state->frontend;
984 EXPORT_SYMBOL(s5h1409_attach);
986 static const struct dvb_frontend_ops s5h1409_ops = {
987 .delsys = { SYS_ATSC, SYS_DVBC_ANNEX_B },
989 .name = "Samsung S5H1409 QAM/8VSB Frontend",
990 .frequency_min_hz = 54 * MHz,
991 .frequency_max_hz = 858 * MHz,
992 .frequency_stepsize_hz = 62500,
993 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
996 .init = s5h1409_init,
997 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
998 .set_frontend = s5h1409_set_frontend,
999 .get_frontend = s5h1409_get_frontend,
1000 .get_tune_settings = s5h1409_get_tune_settings,
1001 .read_status = s5h1409_read_status,
1002 .read_ber = s5h1409_read_ber,
1003 .read_signal_strength = s5h1409_read_signal_strength,
1004 .read_snr = s5h1409_read_snr,
1005 .read_ucblocks = s5h1409_read_ucblocks,
1006 .release = s5h1409_release,
1009 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
1010 MODULE_AUTHOR("Steven Toth");
1011 MODULE_LICENSE("GPL");