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;
48 #define dprintk if (debug) printk
50 /* Register values to initialise the demod, this will set VSB by default */
51 static struct init_tab {
101 /* VSB SNR lookup table */
102 static struct vsb_snr_tab {
148 /* QAM64 SNR lookup table */
149 static struct qam64_snr_tab {
152 } qam64_snr_tab[] = {
218 /* QAM256 SNR lookup table */
219 static struct qam256_snr_tab {
222 } qam256_snr_tab[] = {
293 /* 8 bit registers, 16 bit values */
294 static int s5h1409_writereg(struct s5h1409_state* state, u8 reg, u16 data)
297 u8 buf [] = { reg, data >> 8, data & 0xff };
299 struct i2c_msg msg = { .addr = state->config->demod_address,
300 .flags = 0, .buf = buf, .len = 3 };
302 ret = i2c_transfer(state->i2c, &msg, 1);
305 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, "
306 "ret == %i)\n", __FUNCTION__, reg, data, ret);
308 return (ret != 1) ? -1 : 0;
311 static u16 s5h1409_readreg(struct s5h1409_state* state, u8 reg)
317 struct i2c_msg msg [] = {
318 { .addr = state->config->demod_address, .flags = 0,
319 .buf = b0, .len = 1 },
320 { .addr = state->config->demod_address, .flags = I2C_M_RD,
321 .buf = b1, .len = 2 } };
323 ret = i2c_transfer(state->i2c, msg, 2);
326 printk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
327 return (b1[0] << 8) | b1[1];
330 static int s5h1409_softreset(struct dvb_frontend* fe)
332 struct s5h1409_state* state = fe->demodulator_priv;
334 dprintk("%s()\n", __FUNCTION__);
336 s5h1409_writereg(state, 0xf5, 0);
337 s5h1409_writereg(state, 0xf5, 1);
341 static int s5h1409_set_if_freq(struct dvb_frontend* fe, int KHz)
343 struct s5h1409_state* state = fe->demodulator_priv;
346 dprintk("%s(%d KHz)\n", __FUNCTION__, KHz);
348 if( (KHz == 44000) || (KHz == 5380) ) {
349 s5h1409_writereg(state, 0x87, 0x01be);
350 s5h1409_writereg(state, 0x88, 0x0436);
351 s5h1409_writereg(state, 0x89, 0x054d);
353 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__, KHz);
360 static int s5h1409_set_spectralinversion(struct dvb_frontend* fe, int inverted)
362 struct s5h1409_state* state = fe->demodulator_priv;
364 dprintk("%s()\n", __FUNCTION__);
367 return s5h1409_writereg(state, 0x1b, 0x1101); /* Inverted */
369 return s5h1409_writereg(state, 0x1b, 0x0110); /* Normal */
372 static int s5h1409_enable_modulation(struct dvb_frontend* fe,
375 struct s5h1409_state* state = fe->demodulator_priv;
377 dprintk("%s(0x%08x)\n", __FUNCTION__, m);
381 dprintk("%s() VSB_8\n", __FUNCTION__);
382 s5h1409_writereg(state, 0xf4, 0);
385 dprintk("%s() QAM_64\n", __FUNCTION__);
386 s5h1409_writereg(state, 0xf4, 1);
387 s5h1409_writereg(state, 0x85, 0x100);
390 dprintk("%s() QAM_256\n", __FUNCTION__);
391 s5h1409_writereg(state, 0xf4, 1);
392 s5h1409_writereg(state, 0x85, 0x101);
395 dprintk("%s() Invalid modulation\n", __FUNCTION__);
399 state->current_modulation = m;
400 s5h1409_softreset(fe);
405 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
407 struct s5h1409_state* state = fe->demodulator_priv;
409 dprintk("%s(%d)\n", __FUNCTION__, enable);
412 return s5h1409_writereg(state, 0xf3, 1);
414 return s5h1409_writereg(state, 0xf3, 0);
417 static int s5h1409_set_gpio(struct dvb_frontend* fe, int enable)
419 struct s5h1409_state* state = fe->demodulator_priv;
421 dprintk("%s(%d)\n", __FUNCTION__, enable);
424 return s5h1409_writereg(state, 0xe3, 0x1100);
426 return s5h1409_writereg(state, 0xe3, 0);
429 static int s5h1409_sleep(struct dvb_frontend* fe, int enable)
431 struct s5h1409_state* state = fe->demodulator_priv;
433 dprintk("%s(%d)\n", __FUNCTION__, enable);
435 return s5h1409_writereg(state, 0xf2, enable);
438 static int s5h1409_register_reset(struct dvb_frontend* fe)
440 struct s5h1409_state* state = fe->demodulator_priv;
442 dprintk("%s()\n", __FUNCTION__);
444 return s5h1409_writereg(state, 0xfa, 0);
447 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
448 static int s5h1409_set_frontend (struct dvb_frontend* fe,
449 struct dvb_frontend_parameters *p)
451 struct s5h1409_state* state = fe->demodulator_priv;
453 dprintk("%s(frequency=%d)\n", __FUNCTION__, p->frequency);
455 s5h1409_softreset(fe);
457 state->current_frequency = p->frequency;
459 s5h1409_enable_modulation(fe, p->u.vsb.modulation);
461 if (fe->ops.tuner_ops.set_params) {
462 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 1);
463 fe->ops.tuner_ops.set_params(fe, p);
464 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
470 /* Reset the demod hardware and reset all of the configuration registers
471 to a default state. */
472 static int s5h1409_init (struct dvb_frontend* fe)
476 struct s5h1409_state* state = fe->demodulator_priv;
477 dprintk("%s()\n", __FUNCTION__);
479 s5h1409_sleep(fe, 0);
480 s5h1409_register_reset(fe);
482 for (i=0; i < ARRAY_SIZE(init_tab); i++)
483 s5h1409_writereg(state, init_tab[i].reg, init_tab[i].data);
485 /* The datasheet says that after initialisation, VSB is default */
486 state->current_modulation = VSB_8;
488 if (state->config->output_mode == S5H1409_SERIAL_OUTPUT)
489 s5h1409_writereg(state, 0xab, 0x100); /* Serial */
491 s5h1409_writereg(state, 0xab, 0x0); /* Parallel */
493 s5h1409_set_spectralinversion(fe, state->config->inversion);
494 s5h1409_set_if_freq(fe, state->config->if_freq);
495 s5h1409_set_gpio(fe, state->config->gpio);
496 s5h1409_softreset(fe);
498 /* Note: Leaving the I2C gate open here. */
499 s5h1409_i2c_gate_ctrl(fe, 1);
504 static int s5h1409_read_status(struct dvb_frontend* fe, fe_status_t* status)
506 struct s5h1409_state* state = fe->demodulator_priv;
508 u32 tuner_status = 0;
512 /* Get the demodulator status */
513 reg = s5h1409_readreg(state, 0xf1);
515 *status |= FE_HAS_VITERBI;
517 *status |= FE_HAS_LOCK | FE_HAS_SYNC;
519 switch(state->config->status_mode) {
520 case S5H1409_DEMODLOCKING:
521 if (*status & FE_HAS_VITERBI)
522 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
524 case S5H1409_TUNERLOCKING:
525 /* Get the tuner status */
526 if (fe->ops.tuner_ops.get_status) {
527 if (fe->ops.i2c_gate_ctrl)
528 fe->ops.i2c_gate_ctrl(fe, 1);
530 fe->ops.tuner_ops.get_status(fe, &tuner_status);
532 if (fe->ops.i2c_gate_ctrl)
533 fe->ops.i2c_gate_ctrl(fe, 0);
536 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
540 dprintk("%s() status 0x%08x\n", __FUNCTION__, *status);
545 static int s5h1409_qam256_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
547 int i, ret = -EINVAL;
548 dprintk("%s()\n", __FUNCTION__);
550 for (i=0; i < ARRAY_SIZE(qam256_snr_tab); i++) {
551 if (v < qam256_snr_tab[i].val) {
552 *snr = qam256_snr_tab[i].data;
560 static int s5h1409_qam64_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
562 int i, ret = -EINVAL;
563 dprintk("%s()\n", __FUNCTION__);
565 for (i=0; i < ARRAY_SIZE(qam64_snr_tab); i++) {
566 if (v < qam64_snr_tab[i].val) {
567 *snr = qam64_snr_tab[i].data;
575 static int s5h1409_vsb_lookup_snr(struct dvb_frontend* fe, u16* snr, u16 v)
577 int i, ret = -EINVAL;
578 dprintk("%s()\n", __FUNCTION__);
580 for (i=0; i < ARRAY_SIZE(vsb_snr_tab); i++) {
581 if (v > vsb_snr_tab[i].val) {
582 *snr = vsb_snr_tab[i].data;
587 dprintk("%s() snr=%d\n", __FUNCTION__, *snr);
591 static int s5h1409_read_snr(struct dvb_frontend* fe, u16* snr)
593 struct s5h1409_state* state = fe->demodulator_priv;
595 dprintk("%s()\n", __FUNCTION__);
597 reg = s5h1409_readreg(state, 0xf1) & 0x1ff;
599 switch(state->current_modulation) {
601 return s5h1409_qam64_lookup_snr(fe, snr, reg);
603 return s5h1409_qam256_lookup_snr(fe, snr, reg);
605 return s5h1409_vsb_lookup_snr(fe, snr, reg);
613 static int s5h1409_read_signal_strength(struct dvb_frontend* fe,
614 u16* signal_strength)
616 return s5h1409_read_snr(fe, signal_strength);
619 static int s5h1409_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
621 struct s5h1409_state* state = fe->demodulator_priv;
623 *ucblocks = s5h1409_readreg(state, 0xb5);
628 static int s5h1409_read_ber(struct dvb_frontend* fe, u32* ber)
630 return s5h1409_read_ucblocks(fe, ber);
633 static int s5h1409_get_frontend(struct dvb_frontend* fe,
634 struct dvb_frontend_parameters *p)
636 struct s5h1409_state* state = fe->demodulator_priv;
638 p->frequency = state->current_frequency;
639 p->u.vsb.modulation = state->current_modulation;
644 static int s5h1409_get_tune_settings(struct dvb_frontend* fe,
645 struct dvb_frontend_tune_settings *tune)
647 tune->min_delay_ms = 1000;
651 static void s5h1409_release(struct dvb_frontend* fe)
653 struct s5h1409_state* state = fe->demodulator_priv;
657 static struct dvb_frontend_ops s5h1409_ops;
659 struct dvb_frontend* s5h1409_attach(const struct s5h1409_config* config,
660 struct i2c_adapter* i2c)
662 struct s5h1409_state* state = NULL;
664 /* allocate memory for the internal state */
665 state = kmalloc(sizeof(struct s5h1409_state), GFP_KERNEL);
669 /* setup the state */
670 state->config = config;
672 state->current_modulation = 0;
674 /* check if the demod exists */
675 if (s5h1409_readreg(state, 0x04) != 0x0066)
678 /* create dvb_frontend */
679 memcpy(&state->frontend.ops, &s5h1409_ops,
680 sizeof(struct dvb_frontend_ops));
681 state->frontend.demodulator_priv = state;
683 /* Note: Leaving the I2C gate open here. */
684 s5h1409_writereg(state, 0xf3, 1);
686 return &state->frontend;
693 static struct dvb_frontend_ops s5h1409_ops = {
696 .name = "Samsung S5H1409 QAM/8VSB Frontend",
698 .frequency_min = 54000000,
699 .frequency_max = 858000000,
700 .frequency_stepsize = 62500,
701 .caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB
704 .init = s5h1409_init,
705 .i2c_gate_ctrl = s5h1409_i2c_gate_ctrl,
706 .set_frontend = s5h1409_set_frontend,
707 .get_frontend = s5h1409_get_frontend,
708 .get_tune_settings = s5h1409_get_tune_settings,
709 .read_status = s5h1409_read_status,
710 .read_ber = s5h1409_read_ber,
711 .read_signal_strength = s5h1409_read_signal_strength,
712 .read_snr = s5h1409_read_snr,
713 .read_ucblocks = s5h1409_read_ucblocks,
714 .release = s5h1409_release,
717 module_param(debug, int, 0644);
718 MODULE_PARM_DESC(debug, "Enable verbose debug messages");
720 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
721 MODULE_AUTHOR("Steven Toth");
722 MODULE_LICENSE("GPL");
724 EXPORT_SYMBOL(s5h1409_attach);