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[] = {
222 /* QAM256 SNR lookup table */
223 static struct qam256_snr_tab {
226 } qam256_snr_tab[] = {
297 /* 8 bit registers, 16 bit values */
298 static int s5h1409_writereg(struct s5h1409_state* state, u8 reg, u16 data)
301 u8 buf [] = { reg, data >> 8, data & 0xff };
303 struct i2c_msg msg = { .addr = state->config->demod_address,
304 .flags = 0, .buf = buf, .len = 3 };
306 ret = i2c_transfer(state->i2c, &msg, 1);
309 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
310 "ret == %i)\n", __FUNCTION__, reg, data, ret);
312 return (ret != 1) ? -1 : 0;
315 static u16 s5h1409_readreg(struct s5h1409_state* state, u8 reg)
321 struct i2c_msg msg [] = {
322 { .addr = state->config->demod_address, .flags = 0,
323 .buf = b0, .len = 1 },
324 { .addr = state->config->demod_address, .flags = I2C_M_RD,
325 .buf = b1, .len = 2 } };
327 ret = i2c_transfer(state->i2c, msg, 2);
330 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
331 return (b1[0] << 8) | b1[1];
334 static int s5h1409_softreset(struct dvb_frontend* fe)
336 struct s5h1409_state* state = fe->demodulator_priv;
338 dprintk("%s()\n", __FUNCTION__);
340 s5h1409_writereg(state, 0xf5, 0);
341 s5h1409_writereg(state, 0xf5, 1);
342 state->is_qam_locked = 0;
343 state->qam_state = 0;
347 static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
349 struct s5h1409_state* state = fe->demodulator_priv;
352 dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
354 if( (KHz == 44000) || (KHz == 5380) ) {
355 s5h1409_writereg(state, 0x87, 0x01be);
356 s5h1409_writereg(state, 0x88, 0x0436);
357 s5h1409_writereg(state, 0x89, 0x054d);
360 s5h1409_writereg(state, 0x87, 0x014b);
361 s5h1409_writereg(state, 0x88, 0x0cb5);
362 s5h1409_writereg(state, 0x89, 0x03e2);
364 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__, KHz);
371 static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
373 struct s5h1409_state* state = fe->demodulator_priv;
375 dprintk("%s(%d)\n", __FUNCTION__, inverted);
378 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
380 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
383 static int s5h1409_enable_modulation(struct dvb_frontend* fe,
386 struct s5h1409_state* state = fe->demodulator_priv;
388 dprintk("%s(0x%08x)\n", __FUNCTION__, m);
392 dprintk("%s() VSB_8\n", __FUNCTION__);
393 s5h1409_writereg(state, 0xf4, 0);
397 dprintk("%s() QAM_AUTO (64/256)\n", __FUNCTION__);
398 s5h1409_writereg(state, 0xf4, 1);
399 s5h1409_writereg(state, 0x85, 0x110);
402 dprintk("%s() Invalid modulation\n", __FUNCTION__);
406 state->current_modulation = m;
407 s5h1409_softreset(fe);
412 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
414 struct s5h1409_state* state = fe->demodulator_priv;
416 dprintk("%s(%d)\n", __FUNCTION__, enable);
419 return s5h1409_writereg(state, 0xf3, 1);
421 return s5h1409_writereg(state, 0xf3, 0);
424 static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
426 struct s5h1409_state* state = fe->demodulator_priv;
428 dprintk("%s(%d)\n", __FUNCTION__, enable);
431 return s5h1409_writereg(state, 0xe3, 0x1100);
433 return s5h1409_writereg(state, 0xe3, 0x1000);
436 static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
438 struct s5h1409_state* state = fe->demodulator_priv;
440 dprintk("%s(%d)\n", __FUNCTION__, enable);
442 return s5h1409_writereg(state, 0xf2, enable);
445 static int s5h1409_register_reset(struct dvb_frontend* fe)
447 struct s5h1409_state* state = fe->demodulator_priv;
449 dprintk("%s()\n", __FUNCTION__);
451 return s5h1409_writereg(state, 0xfa, 0);
454 static void s5h1409_set_qam_amhum_mode(struct dvb_frontend *fe)
456 struct s5h1409_state *state = fe->demodulator_priv;
459 if (state->is_qam_locked)
462 /* QAM EQ lock check */
463 reg = s5h1409_readreg(state, 0xf0);
465 if ((reg >> 13) & 0x1) {
467 state->is_qam_locked = 1;
470 s5h1409_writereg(state, 0x96, 0x00c);
471 if ((reg < 0x38) || (reg > 0x68) ) {
472 s5h1409_writereg(state, 0x93, 0x3332);
473 s5h1409_writereg(state, 0x9e, 0x2c37);
475 s5h1409_writereg(state, 0x93, 0x3130);
476 s5h1409_writereg(state, 0x9e, 0x2836);
480 s5h1409_writereg(state, 0x96, 0x0008);
481 s5h1409_writereg(state, 0x93, 0x3332);
482 s5h1409_writereg(state, 0x9e, 0x2c37);
486 static void s5h1409_set_qam_interleave_mode(struct dvb_frontend *fe)
488 struct s5h1409_state *state = fe->demodulator_priv;
491 reg = s5h1409_readreg(state, 0xf1);
494 if ((reg >> 15) & 0x1) {
495 if (state->qam_state != 2) {
496 state->qam_state = 2;
497 reg1 = s5h1409_readreg(state, 0xb2);
498 reg2 = s5h1409_readreg(state, 0xad);
500 s5h1409_writereg(state, 0x96, 0x20);
501 s5h1409_writereg(state, 0xad,
502 ( ((reg1 & 0xf000) >> 4) | (reg2 & 0xf0ff)) );
503 s5h1409_writereg(state, 0xab, 0x1100);
506 if (state->qam_state != 1) {
507 state->qam_state = 1;
508 s5h1409_writereg(state, 0x96, 0x08);
509 s5h1409_writereg(state, 0xab, 0x1101);
514 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
515 static int s5h1409_set_frontend (struct dvb_frontend* fe,
516 struct dvb_frontend_parameters *p)
518 struct s5h1409_state* state = fe->demodulator_priv;
520 dprintk("%s(frequency=%d)\n", __FUNCTION__, p->frequency);
522 s5h1409_softreset(fe);
524 state->current_frequency = p->frequency;
526 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
528 /* Allow the demod to settle */
531 if (fe->ops.tuner_ops.set_params) {
532 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
533 fe->ops.tuner_ops.set_params(fe, p);
534 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
537 /* Optimize the demod for QAM */
538 if (p->u.vsb.modulation != VSB_8) {
539 s5h1409_set_qam_amhum_mode(fe);
540 s5h1409_set_qam_interleave_mode(fe);
546 /* Reset the demod hardware and reset all of the configuration registers
547 to a default state. */
548 static int s5h1409_init (struct dvb_frontend* fe)
552 struct s5h1409_state* state = fe->demodulator_priv;
553 dprintk("%s()\n", __FUNCTION__);
555 s5h1409_sleep(fe, 0);
556 s5h1409_register_reset(fe);
558 for (i=0; i < ARRAY_SIZE(init_tab); i++)
559 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
561 /* The datasheet says that after initialisation, VSB is default */
562 state->current_modulation = VSB_8;
564 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
565 s5h1409_writereg(state, 0xab, 0x100); /* Serial */
567 s5h1409_writereg(state, 0xab, 0x0); /* Parallel */
569 s5h1409_set_spectralinversion(fe, state->config->inversion);
570 s5h1409_set_if_freq(fe, state->config->if_freq);
571 s5h1409_set_gpio(fe, state->config->gpio);
572 s5h1409_softreset(fe);
574 /* Note: Leaving the I2C gate closed. */
575 s5h1409_i2c_gate_ctrl(fe, 0);
580 static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
582 struct s5h1409_state* state = fe->demodulator_priv;
584 u32 tuner_status = 0;
588 /* Get the demodulator status */
589 reg = s5h1409_readreg(state, 0xf1);
591 *status |= FE_HAS_VITERBI;
593 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
595 switch(state->config->status_mode) {
596 case S5H1409_DEMODLOCKING:
597 if (*status & FE_HAS_VITERBI)
598 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
600 case S5H1409_TUNERLOCKING:
601 /* Get the tuner status */
602 if (fe->ops.tuner_ops.get_status) {
603 if (fe->ops.i2c_gate_ctrl)
604 fe->ops.i2c_gate_ctrl(fe, 1);
606 fe->ops.tuner_ops.get_status(fe, &tuner_status);
608 if (fe->ops.i2c_gate_ctrl)
609 fe->ops.i2c_gate_ctrl(fe, 0);
612 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
616 dprintk("%s() status 0x%08x\n", __FUNCTION__, *status);
621 static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
623 int i, ret = -EINVAL;
624 dprintk("%s()\n", __FUNCTION__);
626 for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
627 if (v < qam256_snr_tab[i].val) {
628 *snr = qam256_snr_tab[i].data;
636 static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
638 int i, ret = -EINVAL;
639 dprintk("%s()\n", __FUNCTION__);
641 for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
642 if (v < qam64_snr_tab[i].val) {
643 *snr = qam64_snr_tab[i].data;
651 static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
653 int i, ret = -EINVAL;
654 dprintk("%s()\n", __FUNCTION__);
656 for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
657 if (v > vsb_snr_tab[i].val) {
658 *snr = vsb_snr_tab[i].data;
663 dprintk("%s() snr=%d\n", __FUNCTION__, *snr);
667 static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
669 struct s5h1409_state* state = fe->demodulator_priv;
671 dprintk("%s()\n", __FUNCTION__);
673 reg = s5h1409_readreg(state, 0xf1) & 0x1ff;
675 switch(state->current_modulation) {
677 return s5h1409_qam64_lookup_snr(fe, snr, reg);
679 return s5h1409_qam256_lookup_snr(fe, snr, reg);
681 return s5h1409_vsb_lookup_snr(fe, snr, reg);
689 static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
690 u16* signal_strength)
692 return s5h1409_read_snr(fe, signal_strength);
695 static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
697 struct s5h1409_state* state = fe->demodulator_priv;
699 *ucblocks = s5h1409_readreg(state, 0xb5);
704 static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
706 return s5h1409_read_ucblocks(fe, ber);
709 static int s5h1409_get_frontend(struct dvb_frontend* fe,
710 struct dvb_frontend_parameters *p)
712 struct s5h1409_state* state = fe->demodulator_priv;
714 p->frequency = state->current_frequency;
715 p->u.vsb.modulation = state->current_modulation;
720 static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
721 struct dvb_frontend_tune_settings *tune)
723 tune->min_delay_ms = 1000;
727 static void s5h1409_release(struct dvb_frontend* fe)
729 struct s5h1409_state* state = fe->demodulator_priv;
733 static struct dvb_frontend_ops s5h1409_ops;
735 struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
736 struct i2c_adapter* i2c)
738 struct s5h1409_state* state = NULL;
740 /* allocate memory for the internal state */
741 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
745 /* setup the state */
746 state->config = config;
748 state->current_modulation = 0;
750 /* check if the demod exists */
751 if (s5h1409_readreg(state, 0x04) != 0x0066)
754 /* create dvb_frontend */
755 memcpy(&state->frontend.ops, &s5h1409_ops,
756 sizeof(struct dvb_frontend_ops));
757 state->frontend.demodulator_priv = state;
759 /* Note: Leaving the I2C gate open here. */
760 s5h1409_writereg(state, 0xf3, 1);
762 return &state->frontend;
769 static struct dvb_frontend_ops s5h1409_ops = {
772 .name = "Samsung S5H1409 QAM/8VSB Frontend",
774 .frequency_min = 54000000,
775 .frequency_max = 858000000,
776 .frequency_stepsize = 62500,
777 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
780 .init = s5h1409_init,
781 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
782 .set_frontend = s5h1409_set_frontend,
783 .get_frontend = s5h1409_get_frontend,
784 .get_tune_settings = s5h1409_get_tune_settings,
785 .read_status = s5h1409_read_status,
786 .read_ber = s5h1409_read_ber,
787 .read_signal_strength = s5h1409_read_signal_strength,
788 .read_snr = s5h1409_read_snr,
789 .read_ucblocks = s5h1409_read_ucblocks,
790 .release = s5h1409_release,
793 module_param(debug, int, 0644);
794 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
796 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
797 MODULE_AUTHOR("Steven Toth");
798 MODULE_LICENSE("GPL");
800 EXPORT_SYMBOL(s5h1409_attach);