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
, .flags
= 0, .buf
= buf
, .len
= 3 };
301 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
304 printk("%s: writereg error (reg == 0x%02x, val == 0x%04x, ret == %i)\n",
305 __FUNCTION__
, reg
, data
, ret
);
307 return (ret
!= 1) ? -1 : 0;
310 static u16
s5h1409_readreg(struct s5h1409_state
* state
, u8 reg
)
316 struct i2c_msg msg
[] = {
317 { .addr
= state
->config
->demod_address
, .flags
= 0, .buf
= b0
, .len
= 1 },
318 { .addr
= state
->config
->demod_address
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 2 } };
320 ret
= i2c_transfer(state
->i2c
, msg
, 2);
323 printk("%s: readreg error (ret == %i)\n", __FUNCTION__
, ret
)
325 return (b1
[0] << 8) | b1
[1];
328 static int s5h1409_softreset(struct dvb_frontend
* fe
)
330 struct s5h1409_state
* state
= fe
->demodulator_priv
;
332 dprintk("%s()\n", __FUNCTION__
);
334 s5h1409_writereg(state
, 0xf5, 0);
335 s5h1409_writereg(state
, 0xf5, 1);
339 static int s5h1409_set_if_freq(struct dvb_frontend
* fe
, int KHz
)
341 struct s5h1409_state
* state
= fe
->demodulator_priv
;
344 dprintk("%s(%d KHz)\n", __FUNCTION__
, KHz
);
346 if( (KHz
== 44000) || (KHz
== 5380) )
348 s5h1409_writereg(state
, 0x87, 0x01be);
349 s5h1409_writereg(state
, 0x88, 0x0436);
350 s5h1409_writereg(state
, 0x89, 0x054d);
352 printk("%s() Invalid arg = %d KHz\n", __FUNCTION__
, KHz
);
359 static int s5h1409_set_spectralinversion(struct dvb_frontend
* fe
, int inverted
)
361 struct s5h1409_state
* state
= fe
->demodulator_priv
;
363 dprintk("%s()\n", __FUNCTION__
);
366 return s5h1409_writereg(state
, 0x1b, 0x1101); /* Inverted */
368 return s5h1409_writereg(state
, 0x1b, 0x0110); /* Normal */
371 static int s5h1409_enable_modulation(struct dvb_frontend
* fe
, fe_modulation_t m
)
373 struct s5h1409_state
* state
= fe
->demodulator_priv
;
375 dprintk("%s(0x%08x)\n", __FUNCTION__
, m
);
379 dprintk("%s() VSB_8\n", __FUNCTION__
);
380 s5h1409_writereg(state
, 0xf4, 0);
383 dprintk("%s() QAM_64\n", __FUNCTION__
);
384 s5h1409_writereg(state
, 0xf4, 1);
385 s5h1409_writereg(state
, 0x85, 0x100);
388 dprintk("%s() QAM_256\n", __FUNCTION__
);
389 s5h1409_writereg(state
, 0xf4, 1);
390 s5h1409_writereg(state
, 0x85, 0x101);
393 dprintk("%s() Invalid modulation\n", __FUNCTION__
);
397 state
->current_modulation
= m
;
398 s5h1409_softreset(fe
);
403 static int s5h1409_i2c_gate_ctrl(struct dvb_frontend
* fe
, int enable
)
405 struct s5h1409_state
* state
= fe
->demodulator_priv
;
407 dprintk("%s(%d)\n", __FUNCTION__
, enable
);
410 return s5h1409_writereg(state
, 0xf3, 1);
412 return s5h1409_writereg(state
, 0xf3, 0);
415 static int s5h1409_set_gpio(struct dvb_frontend
* fe
, int enable
)
417 struct s5h1409_state
* state
= fe
->demodulator_priv
;
419 dprintk("%s(%d)\n", __FUNCTION__
, enable
);
422 return s5h1409_writereg(state
, 0xe3, 0x1100);
424 return s5h1409_writereg(state
, 0xe3, 0);
427 static int s5h1409_sleep(struct dvb_frontend
* fe
, int enable
)
429 struct s5h1409_state
* state
= fe
->demodulator_priv
;
431 dprintk("%s(%d)\n", __FUNCTION__
, enable
);
433 return s5h1409_writereg(state
, 0xf2, enable
);
436 static int s5h1409_register_reset(struct dvb_frontend
* fe
)
438 struct s5h1409_state
* state
= fe
->demodulator_priv
;
440 dprintk("%s()\n", __FUNCTION__
);
442 return s5h1409_writereg(state
, 0xfa, 0);
445 /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
446 static int s5h1409_set_frontend (struct dvb_frontend
* fe
, struct dvb_frontend_parameters
*p
)
448 struct s5h1409_state
* state
= fe
->demodulator_priv
;
450 dprintk("%s(frequency=%d)\n", __FUNCTION__
, p
->frequency
);
452 s5h1409_softreset(fe
);
454 state
->current_frequency
= p
->frequency
;
456 s5h1409_enable_modulation(fe
, p
->u
.vsb
.modulation
);
458 if (fe
->ops
.tuner_ops
.set_params
) {
459 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 1);
460 fe
->ops
.tuner_ops
.set_params(fe
, p
);
461 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
467 /* Reset the demod hardware and reset all of the configuration registers
468 to a default state. */
469 static int s5h1409_init (struct dvb_frontend
* fe
)
473 struct s5h1409_state
* state
= fe
->demodulator_priv
;
474 dprintk("%s()\n", __FUNCTION__
);
476 s5h1409_sleep(fe
, 0);
477 s5h1409_register_reset(fe
);
479 for (i
=0; i
<sizeof(init_tab
) / sizeof (struct init_tab
); i
++)
480 s5h1409_writereg(state
, init_tab
[i
].reg
, init_tab
[i
].data
);
482 /* The datasheet says that after initialisation, VSB is default */
483 state
->current_modulation
= VSB_8
;
485 if (state
->config
->output_mode
== S5H1409_SERIAL_OUTPUT
)
486 s5h1409_writereg(state
, 0xab, 0x100); /* Serial */
488 s5h1409_writereg(state
, 0xab, 0x0); /* Parallel */
490 s5h1409_set_spectralinversion(fe
, state
->config
->inversion
);
491 s5h1409_set_if_freq(fe
, state
->config
->if_freq
);
492 s5h1409_set_gpio(fe
, state
->config
->gpio
);
493 s5h1409_softreset(fe
);
495 /* Note: Leaving the I2C gate open here. */
496 s5h1409_i2c_gate_ctrl(fe
, 1);
501 static int s5h1409_read_status(struct dvb_frontend
* fe
, fe_status_t
* status
)
503 struct s5h1409_state
* state
= fe
->demodulator_priv
;
505 u32 tuner_status
= 0;
509 /* Get the demodulator status */
510 reg
= s5h1409_readreg(state
, 0xf1);
512 *status
|= FE_HAS_VITERBI
;
514 *status
|= FE_HAS_LOCK
| FE_HAS_SYNC
;
516 switch(state
->config
->status_mode
) {
517 case S5H1409_DEMODLOCKING
:
518 if (*status
& FE_HAS_VITERBI
)
519 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
521 case S5H1409_TUNERLOCKING
:
522 /* Get the tuner status */
523 if (fe
->ops
.tuner_ops
.get_status
) {
524 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 1);
526 fe
->ops
.tuner_ops
.get_status(fe
, &tuner_status
);
528 if (fe
->ops
.i2c_gate_ctrl
) fe
->ops
.i2c_gate_ctrl(fe
, 0);
531 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
535 dprintk("%s() status 0x%08x\n", __FUNCTION__
, *status
);
540 static int s5h1409_qam256_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
542 int i
, ret
= -EINVAL
;
543 dprintk("%s()\n", __FUNCTION__
);
545 for (i
=0; i
<sizeof(qam256_snr_tab
) / (sizeof(struct qam256_snr_tab
)); i
++) {
546 if (v
< qam256_snr_tab
[i
].val
) {
547 *snr
= qam256_snr_tab
[i
].data
;
555 static int s5h1409_qam64_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
557 int i
, ret
= -EINVAL
;
558 dprintk("%s()\n", __FUNCTION__
);
560 for (i
=0; i
<sizeof(qam64_snr_tab
) / (sizeof(struct qam64_snr_tab
)); i
++) {
561 if (v
< qam64_snr_tab
[i
].val
) {
562 *snr
= qam64_snr_tab
[i
].data
;
570 static int s5h1409_vsb_lookup_snr(struct dvb_frontend
* fe
, u16
* snr
, u16 v
)
572 int i
, ret
= -EINVAL
;
573 dprintk("%s()\n", __FUNCTION__
);
575 for (i
=0; i
<sizeof(vsb_snr_tab
) / (sizeof(struct vsb_snr_tab
)); i
++) {
576 if (v
> vsb_snr_tab
[i
].val
) {
577 *snr
= vsb_snr_tab
[i
].data
;
582 dprintk("%s() snr=%d\n", __FUNCTION__
, *snr
);
586 static int s5h1409_read_snr(struct dvb_frontend
* fe
, u16
* snr
)
588 struct s5h1409_state
* state
= fe
->demodulator_priv
;
590 dprintk("%s()\n", __FUNCTION__
);
592 reg
= s5h1409_readreg(state
, 0xf1) & 0x1ff;
594 switch(state
->current_modulation
) {
596 return s5h1409_qam64_lookup_snr(fe
, snr
, reg
);
598 return s5h1409_qam256_lookup_snr(fe
, snr
, reg
);
600 return s5h1409_vsb_lookup_snr(fe
, snr
, reg
);
608 static int s5h1409_read_signal_strength(struct dvb_frontend
* fe
, u16
* signal_strength
)
610 return s5h1409_read_snr(fe
, signal_strength
);
613 static int s5h1409_read_ucblocks(struct dvb_frontend
* fe
, u32
* ucblocks
)
615 struct s5h1409_state
* state
= fe
->demodulator_priv
;
617 *ucblocks
= s5h1409_readreg(state
, 0xb5);
622 static int s5h1409_read_ber(struct dvb_frontend
* fe
, u32
* ber
)
624 return s5h1409_read_ucblocks(fe
, ber
);
627 static int s5h1409_get_frontend(struct dvb_frontend
* fe
, struct dvb_frontend_parameters
*p
)
629 struct s5h1409_state
* state
= fe
->demodulator_priv
;
631 p
->frequency
= state
->current_frequency
;
632 p
->u
.vsb
.modulation
= state
->current_modulation
;
637 static int s5h1409_get_tune_settings(struct dvb_frontend
* fe
, struct dvb_frontend_tune_settings
*tune
)
639 tune
->min_delay_ms
= 1000;
643 static void s5h1409_release(struct dvb_frontend
* fe
)
645 struct s5h1409_state
* state
= fe
->demodulator_priv
;
649 static struct dvb_frontend_ops s5h1409_ops
;
651 struct dvb_frontend
* s5h1409_attach(const struct s5h1409_config
* config
,
652 struct i2c_adapter
* i2c
)
654 struct s5h1409_state
* state
= NULL
;
656 /* allocate memory for the internal state */
657 state
= kmalloc(sizeof(struct s5h1409_state
), GFP_KERNEL
);
661 /* setup the state */
662 state
->config
= config
;
664 state
->current_modulation
= 0;
666 /* check if the demod exists */
667 if (s5h1409_readreg(state
, 0x04) != 0x0066)
670 /* create dvb_frontend */
671 memcpy(&state
->frontend
.ops
, &s5h1409_ops
, sizeof(struct dvb_frontend_ops
));
672 state
->frontend
.demodulator_priv
= state
;
674 /* Note: Leaving the I2C gate open here. */
675 s5h1409_writereg(state
, 0xf3, 1);
677 return &state
->frontend
;
684 static struct dvb_frontend_ops s5h1409_ops
= {
687 .name
= "Samsung S5H1409 QAM/8VSB Frontend",
689 .frequency_min
= 54000000,
690 .frequency_max
= 858000000,
691 .frequency_stepsize
= 62500,
692 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
695 .init
= s5h1409_init
,
696 .i2c_gate_ctrl
= s5h1409_i2c_gate_ctrl
,
697 .set_frontend
= s5h1409_set_frontend
,
698 .get_frontend
= s5h1409_get_frontend
,
699 .get_tune_settings
= s5h1409_get_tune_settings
,
700 .read_status
= s5h1409_read_status
,
701 .read_ber
= s5h1409_read_ber
,
702 .read_signal_strength
= s5h1409_read_signal_strength
,
703 .read_snr
= s5h1409_read_snr
,
704 .read_ucblocks
= s5h1409_read_ucblocks
,
705 .release
= s5h1409_release
,
708 module_param(debug
, int, 0644);
709 MODULE_PARM_DESC(debug
, "Enable verbose debug messages");
711 MODULE_DESCRIPTION("Samsung S5H1409 QAM-B/ATSC Demodulator driver");
712 MODULE_AUTHOR("Steven Toth");
713 MODULE_LICENSE("GPL");
715 EXPORT_SYMBOL(s5h1409_attach
);