2 * Support for LG Electronics LGDT3304 and LGDT3305 - VSB/QAM
4 * Copyright (C) 2008, 2009, 2010 Michael Krufky <mkrufky@linuxtv.org>
6 * LGDT3304 support by Jarod Wilson <jarod@redhat.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <asm/div64.h>
21 #include <linux/dvb/frontend.h>
22 #include <linux/slab.h>
23 #include <media/dvb_math.h>
27 module_param(debug
, int, 0644);
28 MODULE_PARM_DESC(debug
, "set debug level (info=1, reg=2 (or-able))");
33 #define lg_printk(kern, fmt, arg...) \
34 printk(kern "%s: " fmt, __func__, ##arg)
36 #define lg_info(fmt, arg...) printk(KERN_INFO "lgdt3305: " fmt, ##arg)
37 #define lg_warn(fmt, arg...) lg_printk(KERN_WARNING, fmt, ##arg)
38 #define lg_err(fmt, arg...) lg_printk(KERN_ERR, fmt, ##arg)
39 #define lg_dbg(fmt, arg...) if (debug & DBG_INFO) \
40 lg_printk(KERN_DEBUG, fmt, ##arg)
41 #define lg_reg(fmt, arg...) if (debug & DBG_REG) \
42 lg_printk(KERN_DEBUG, fmt, ##arg)
44 #define lg_fail(ret) \
49 lg_err("error %d on line %d\n", ret, __LINE__); \
53 struct lgdt3305_state
{
54 struct i2c_adapter
*i2c_adap
;
55 const struct lgdt3305_config
*cfg
;
57 struct dvb_frontend frontend
;
59 enum fe_modulation current_modulation
;
60 u32 current_frequency
;
64 /* ------------------------------------------------------------------------ */
66 /* FIXME: verify & document the LGDT3304 registers */
68 #define LGDT3305_GEN_CTRL_1 0x0000
69 #define LGDT3305_GEN_CTRL_2 0x0001
70 #define LGDT3305_GEN_CTRL_3 0x0002
71 #define LGDT3305_GEN_STATUS 0x0003
72 #define LGDT3305_GEN_CONTROL 0x0007
73 #define LGDT3305_GEN_CTRL_4 0x000a
74 #define LGDT3305_DGTL_AGC_REF_1 0x0012
75 #define LGDT3305_DGTL_AGC_REF_2 0x0013
76 #define LGDT3305_CR_CTR_FREQ_1 0x0106
77 #define LGDT3305_CR_CTR_FREQ_2 0x0107
78 #define LGDT3305_CR_CTR_FREQ_3 0x0108
79 #define LGDT3305_CR_CTR_FREQ_4 0x0109
80 #define LGDT3305_CR_MSE_1 0x011b
81 #define LGDT3305_CR_MSE_2 0x011c
82 #define LGDT3305_CR_LOCK_STATUS 0x011d
83 #define LGDT3305_CR_CTRL_7 0x0126
84 #define LGDT3305_AGC_POWER_REF_1 0x0300
85 #define LGDT3305_AGC_POWER_REF_2 0x0301
86 #define LGDT3305_AGC_DELAY_PT_1 0x0302
87 #define LGDT3305_AGC_DELAY_PT_2 0x0303
88 #define LGDT3305_RFAGC_LOOP_FLTR_BW_1 0x0306
89 #define LGDT3305_RFAGC_LOOP_FLTR_BW_2 0x0307
90 #define LGDT3305_IFBW_1 0x0308
91 #define LGDT3305_IFBW_2 0x0309
92 #define LGDT3305_AGC_CTRL_1 0x030c
93 #define LGDT3305_AGC_CTRL_4 0x0314
94 #define LGDT3305_EQ_MSE_1 0x0413
95 #define LGDT3305_EQ_MSE_2 0x0414
96 #define LGDT3305_EQ_MSE_3 0x0415
97 #define LGDT3305_PT_MSE_1 0x0417
98 #define LGDT3305_PT_MSE_2 0x0418
99 #define LGDT3305_PT_MSE_3 0x0419
100 #define LGDT3305_FEC_BLOCK_CTRL 0x0504
101 #define LGDT3305_FEC_LOCK_STATUS 0x050a
102 #define LGDT3305_FEC_PKT_ERR_1 0x050c
103 #define LGDT3305_FEC_PKT_ERR_2 0x050d
104 #define LGDT3305_TP_CTRL_1 0x050e
105 #define LGDT3305_BERT_PERIOD 0x0801
106 #define LGDT3305_BERT_ERROR_COUNT_1 0x080a
107 #define LGDT3305_BERT_ERROR_COUNT_2 0x080b
108 #define LGDT3305_BERT_ERROR_COUNT_3 0x080c
109 #define LGDT3305_BERT_ERROR_COUNT_4 0x080d
111 static int lgdt3305_write_reg(struct lgdt3305_state
*state
, u16 reg
, u8 val
)
114 u8 buf
[] = { reg
>> 8, reg
& 0xff, val
};
115 struct i2c_msg msg
= {
116 .addr
= state
->cfg
->i2c_addr
, .flags
= 0,
117 .buf
= buf
, .len
= 3,
120 lg_reg("reg: 0x%04x, val: 0x%02x\n", reg
, val
);
122 ret
= i2c_transfer(state
->i2c_adap
, &msg
, 1);
125 lg_err("error (addr %02x %02x <- %02x, err = %i)\n",
126 msg
.buf
[0], msg
.buf
[1], msg
.buf
[2], ret
);
135 static int lgdt3305_read_reg(struct lgdt3305_state
*state
, u16 reg
, u8
*val
)
138 u8 reg_buf
[] = { reg
>> 8, reg
& 0xff };
139 struct i2c_msg msg
[] = {
140 { .addr
= state
->cfg
->i2c_addr
,
141 .flags
= 0, .buf
= reg_buf
, .len
= 2 },
142 { .addr
= state
->cfg
->i2c_addr
,
143 .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
146 lg_reg("reg: 0x%04x\n", reg
);
148 ret
= i2c_transfer(state
->i2c_adap
, msg
, 2);
151 lg_err("error (addr %02x reg %04x error (ret == %i)\n",
152 state
->cfg
->i2c_addr
, reg
, ret
);
161 #define read_reg(state, reg) \
164 int ret = lgdt3305_read_reg(state, reg, &__val); \
170 static int lgdt3305_set_reg_bit(struct lgdt3305_state
*state
,
171 u16 reg
, int bit
, int onoff
)
176 lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg
, bit
, onoff
);
178 ret
= lgdt3305_read_reg(state
, reg
, &val
);
183 val
|= (onoff
& 1) << bit
;
185 ret
= lgdt3305_write_reg(state
, reg
, val
);
190 struct lgdt3305_reg
{
195 static int lgdt3305_write_regs(struct lgdt3305_state
*state
,
196 struct lgdt3305_reg
*regs
, int len
)
200 lg_reg("writing %d registers...\n", len
);
202 for (i
= 0; i
< len
- 1; i
++) {
203 ret
= lgdt3305_write_reg(state
, regs
[i
].reg
, regs
[i
].val
);
210 /* ------------------------------------------------------------------------ */
212 static int lgdt3305_soft_reset(struct lgdt3305_state
*state
)
218 ret
= lgdt3305_set_reg_bit(state
, LGDT3305_GEN_CTRL_3
, 0, 0);
223 ret
= lgdt3305_set_reg_bit(state
, LGDT3305_GEN_CTRL_3
, 0, 1);
228 static inline int lgdt3305_mpeg_mode(struct lgdt3305_state
*state
,
229 enum lgdt3305_mpeg_mode mode
)
231 lg_dbg("(%d)\n", mode
);
232 return lgdt3305_set_reg_bit(state
, LGDT3305_TP_CTRL_1
, 5, mode
);
235 static int lgdt3305_mpeg_mode_polarity(struct lgdt3305_state
*state
)
239 enum lgdt3305_tp_clock_edge edge
= state
->cfg
->tpclk_edge
;
240 enum lgdt3305_tp_clock_mode mode
= state
->cfg
->tpclk_mode
;
241 enum lgdt3305_tp_valid_polarity valid
= state
->cfg
->tpvalid_polarity
;
243 lg_dbg("edge = %d, valid = %d\n", edge
, valid
);
245 ret
= lgdt3305_read_reg(state
, LGDT3305_TP_CTRL_1
, &val
);
258 ret
= lgdt3305_write_reg(state
, LGDT3305_TP_CTRL_1
, val
);
262 ret
= lgdt3305_soft_reset(state
);
267 static int lgdt3305_set_modulation(struct lgdt3305_state
*state
,
268 struct dtv_frontend_properties
*p
)
275 ret
= lgdt3305_read_reg(state
, LGDT3305_GEN_CTRL_1
, &opermode
);
281 switch (p
->modulation
) {
294 ret
= lgdt3305_write_reg(state
, LGDT3305_GEN_CTRL_1
, opermode
);
299 static int lgdt3305_set_filter_extension(struct lgdt3305_state
*state
,
300 struct dtv_frontend_properties
*p
)
304 switch (p
->modulation
) {
315 lg_dbg("val = %d\n", val
);
317 return lgdt3305_set_reg_bit(state
, 0x043f, 2, val
);
320 /* ------------------------------------------------------------------------ */
322 static int lgdt3305_passband_digital_agc(struct lgdt3305_state
*state
,
323 struct dtv_frontend_properties
*p
)
327 switch (p
->modulation
) {
341 lg_dbg("agc ref: 0x%04x\n", agc_ref
);
343 lgdt3305_write_reg(state
, LGDT3305_DGTL_AGC_REF_1
, agc_ref
>> 8);
344 lgdt3305_write_reg(state
, LGDT3305_DGTL_AGC_REF_2
, agc_ref
& 0xff);
349 static int lgdt3305_rfagc_loop(struct lgdt3305_state
*state
,
350 struct dtv_frontend_properties
*p
)
352 u16 ifbw
, rfbw
, agcdelay
;
354 switch (p
->modulation
) {
364 /* FIXME: investigate optimal ifbw & rfbw values for the
365 * DT3304 and re-write this switch..case block */
366 if (state
->cfg
->demod_chip
== LGDT3304
)
368 else /* (state->cfg->demod_chip == LGDT3305) */
375 if (state
->cfg
->rf_agc_loop
) {
376 lg_dbg("agcdelay: 0x%04x, rfbw: 0x%04x\n", agcdelay
, rfbw
);
378 /* rf agc loop filter bandwidth */
379 lgdt3305_write_reg(state
, LGDT3305_AGC_DELAY_PT_1
,
381 lgdt3305_write_reg(state
, LGDT3305_AGC_DELAY_PT_2
,
384 lgdt3305_write_reg(state
, LGDT3305_RFAGC_LOOP_FLTR_BW_1
,
386 lgdt3305_write_reg(state
, LGDT3305_RFAGC_LOOP_FLTR_BW_2
,
389 lg_dbg("ifbw: 0x%04x\n", ifbw
);
391 /* if agc loop filter bandwidth */
392 lgdt3305_write_reg(state
, LGDT3305_IFBW_1
, ifbw
>> 8);
393 lgdt3305_write_reg(state
, LGDT3305_IFBW_2
, ifbw
& 0xff);
399 static int lgdt3305_agc_setup(struct lgdt3305_state
*state
,
400 struct dtv_frontend_properties
*p
)
404 switch (p
->modulation
) {
418 lg_dbg("lockdten = %d, acqen = %d\n", lockdten
, acqen
);
420 /* control agc function */
421 switch (state
->cfg
->demod_chip
) {
423 lgdt3305_write_reg(state
, 0x0314, 0xe1 | lockdten
<< 1);
424 lgdt3305_set_reg_bit(state
, 0x030e, 2, acqen
);
427 lgdt3305_write_reg(state
, LGDT3305_AGC_CTRL_4
, 0xe1 | lockdten
<< 1);
428 lgdt3305_set_reg_bit(state
, LGDT3305_AGC_CTRL_1
, 2, acqen
);
434 return lgdt3305_rfagc_loop(state
, p
);
437 static int lgdt3305_set_agc_power_ref(struct lgdt3305_state
*state
,
438 struct dtv_frontend_properties
*p
)
442 switch (p
->modulation
) {
444 if (state
->cfg
->usref_8vsb
)
445 usref
= state
->cfg
->usref_8vsb
;
448 if (state
->cfg
->usref_qam64
)
449 usref
= state
->cfg
->usref_qam64
;
452 if (state
->cfg
->usref_qam256
)
453 usref
= state
->cfg
->usref_qam256
;
460 lg_dbg("set manual mode: 0x%04x\n", usref
);
462 lgdt3305_set_reg_bit(state
, LGDT3305_AGC_CTRL_1
, 3, 1);
464 lgdt3305_write_reg(state
, LGDT3305_AGC_POWER_REF_1
,
465 0xff & (usref
>> 8));
466 lgdt3305_write_reg(state
, LGDT3305_AGC_POWER_REF_2
,
467 0xff & (usref
>> 0));
472 /* ------------------------------------------------------------------------ */
474 static int lgdt3305_spectral_inversion(struct lgdt3305_state
*state
,
475 struct dtv_frontend_properties
*p
,
480 lg_dbg("(%d)\n", inversion
);
482 switch (p
->modulation
) {
484 ret
= lgdt3305_write_reg(state
, LGDT3305_CR_CTRL_7
,
485 inversion
? 0xf9 : 0x79);
489 ret
= lgdt3305_write_reg(state
, LGDT3305_FEC_BLOCK_CTRL
,
490 inversion
? 0xfd : 0xff);
498 static int lgdt3305_set_if(struct lgdt3305_state
*state
,
499 struct dtv_frontend_properties
*p
)
502 u8 nco1
, nco2
, nco3
, nco4
;
505 switch (p
->modulation
) {
507 if_freq_khz
= state
->cfg
->vsb_if_khz
;
511 if_freq_khz
= state
->cfg
->qam_if_khz
;
517 nco
= if_freq_khz
/ 10;
519 switch (p
->modulation
) {
533 nco1
= (nco
>> 24) & 0x3f;
535 nco2
= (nco
>> 16) & 0xff;
536 nco3
= (nco
>> 8) & 0xff;
539 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_1
, nco1
);
540 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_2
, nco2
);
541 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_3
, nco3
);
542 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_4
, nco4
);
544 lg_dbg("%d KHz -> [%02x%02x%02x%02x]\n",
545 if_freq_khz
, nco1
, nco2
, nco3
, nco4
);
550 /* ------------------------------------------------------------------------ */
552 static int lgdt3305_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
554 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
556 if (state
->cfg
->deny_i2c_rptr
)
559 lg_dbg("(%d)\n", enable
);
561 return lgdt3305_set_reg_bit(state
, LGDT3305_GEN_CTRL_2
, 5,
565 static int lgdt3305_sleep(struct dvb_frontend
*fe
)
567 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
568 u8 gen_ctrl_3
, gen_ctrl_4
;
572 gen_ctrl_3
= read_reg(state
, LGDT3305_GEN_CTRL_3
);
573 gen_ctrl_4
= read_reg(state
, LGDT3305_GEN_CTRL_4
);
575 /* hold in software reset while sleeping */
577 /* tristate the IF-AGC pin */
579 /* tristate the RF-AGC pin */
582 /* disable vsb/qam module */
584 /* disable adc module */
587 lgdt3305_write_reg(state
, LGDT3305_GEN_CTRL_3
, gen_ctrl_3
);
588 lgdt3305_write_reg(state
, LGDT3305_GEN_CTRL_4
, gen_ctrl_4
);
593 static int lgdt3305_init(struct dvb_frontend
*fe
)
595 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
598 static struct lgdt3305_reg lgdt3304_init_data
[] = {
599 { .reg
= LGDT3305_GEN_CTRL_1
, .val
= 0x03, },
600 { .reg
= 0x000d, .val
= 0x02, },
601 { .reg
= 0x000e, .val
= 0x02, },
602 { .reg
= LGDT3305_DGTL_AGC_REF_1
, .val
= 0x32, },
603 { .reg
= LGDT3305_DGTL_AGC_REF_2
, .val
= 0xc4, },
604 { .reg
= LGDT3305_CR_CTR_FREQ_1
, .val
= 0x00, },
605 { .reg
= LGDT3305_CR_CTR_FREQ_2
, .val
= 0x00, },
606 { .reg
= LGDT3305_CR_CTR_FREQ_3
, .val
= 0x00, },
607 { .reg
= LGDT3305_CR_CTR_FREQ_4
, .val
= 0x00, },
608 { .reg
= LGDT3305_CR_CTRL_7
, .val
= 0xf9, },
609 { .reg
= 0x0112, .val
= 0x17, },
610 { .reg
= 0x0113, .val
= 0x15, },
611 { .reg
= 0x0114, .val
= 0x18, },
612 { .reg
= 0x0115, .val
= 0xff, },
613 { .reg
= 0x0116, .val
= 0x3c, },
614 { .reg
= 0x0214, .val
= 0x67, },
615 { .reg
= 0x0424, .val
= 0x8d, },
616 { .reg
= 0x0427, .val
= 0x12, },
617 { .reg
= 0x0428, .val
= 0x4f, },
618 { .reg
= LGDT3305_IFBW_1
, .val
= 0x80, },
619 { .reg
= LGDT3305_IFBW_2
, .val
= 0x00, },
620 { .reg
= 0x030a, .val
= 0x08, },
621 { .reg
= 0x030b, .val
= 0x9b, },
622 { .reg
= 0x030d, .val
= 0x00, },
623 { .reg
= 0x030e, .val
= 0x1c, },
624 { .reg
= 0x0314, .val
= 0xe1, },
625 { .reg
= 0x000d, .val
= 0x82, },
626 { .reg
= LGDT3305_TP_CTRL_1
, .val
= 0x5b, },
627 { .reg
= LGDT3305_TP_CTRL_1
, .val
= 0x5b, },
630 static struct lgdt3305_reg lgdt3305_init_data
[] = {
631 { .reg
= LGDT3305_GEN_CTRL_1
, .val
= 0x03, },
632 { .reg
= LGDT3305_GEN_CTRL_2
, .val
= 0xb0, },
633 { .reg
= LGDT3305_GEN_CTRL_3
, .val
= 0x01, },
634 { .reg
= LGDT3305_GEN_CONTROL
, .val
= 0x6f, },
635 { .reg
= LGDT3305_GEN_CTRL_4
, .val
= 0x03, },
636 { .reg
= LGDT3305_DGTL_AGC_REF_1
, .val
= 0x32, },
637 { .reg
= LGDT3305_DGTL_AGC_REF_2
, .val
= 0xc4, },
638 { .reg
= LGDT3305_CR_CTR_FREQ_1
, .val
= 0x00, },
639 { .reg
= LGDT3305_CR_CTR_FREQ_2
, .val
= 0x00, },
640 { .reg
= LGDT3305_CR_CTR_FREQ_3
, .val
= 0x00, },
641 { .reg
= LGDT3305_CR_CTR_FREQ_4
, .val
= 0x00, },
642 { .reg
= LGDT3305_CR_CTRL_7
, .val
= 0x79, },
643 { .reg
= LGDT3305_AGC_POWER_REF_1
, .val
= 0x32, },
644 { .reg
= LGDT3305_AGC_POWER_REF_2
, .val
= 0xc4, },
645 { .reg
= LGDT3305_AGC_DELAY_PT_1
, .val
= 0x0d, },
646 { .reg
= LGDT3305_AGC_DELAY_PT_2
, .val
= 0x30, },
647 { .reg
= LGDT3305_RFAGC_LOOP_FLTR_BW_1
, .val
= 0x80, },
648 { .reg
= LGDT3305_RFAGC_LOOP_FLTR_BW_2
, .val
= 0x00, },
649 { .reg
= LGDT3305_IFBW_1
, .val
= 0x80, },
650 { .reg
= LGDT3305_IFBW_2
, .val
= 0x00, },
651 { .reg
= LGDT3305_AGC_CTRL_1
, .val
= 0x30, },
652 { .reg
= LGDT3305_AGC_CTRL_4
, .val
= 0x61, },
653 { .reg
= LGDT3305_FEC_BLOCK_CTRL
, .val
= 0xff, },
654 { .reg
= LGDT3305_TP_CTRL_1
, .val
= 0x1b, },
659 switch (state
->cfg
->demod_chip
) {
661 ret
= lgdt3305_write_regs(state
, lgdt3304_init_data
,
662 ARRAY_SIZE(lgdt3304_init_data
));
665 ret
= lgdt3305_write_regs(state
, lgdt3305_init_data
,
666 ARRAY_SIZE(lgdt3305_init_data
));
674 ret
= lgdt3305_soft_reset(state
);
679 static int lgdt3304_set_parameters(struct dvb_frontend
*fe
)
681 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
682 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
685 lg_dbg("(%d, %d)\n", p
->frequency
, p
->modulation
);
687 if (fe
->ops
.tuner_ops
.set_params
) {
688 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
689 if (fe
->ops
.i2c_gate_ctrl
)
690 fe
->ops
.i2c_gate_ctrl(fe
, 0);
693 state
->current_frequency
= p
->frequency
;
696 ret
= lgdt3305_set_modulation(state
, p
);
700 ret
= lgdt3305_passband_digital_agc(state
, p
);
704 ret
= lgdt3305_agc_setup(state
, p
);
708 /* reg 0x030d is 3304-only... seen in vsb and qam usbsnoops... */
709 switch (p
->modulation
) {
711 lgdt3305_write_reg(state
, 0x030d, 0x00);
712 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_1
, 0x4f);
713 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_2
, 0x0c);
714 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_3
, 0xac);
715 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_4
, 0xba);
719 lgdt3305_write_reg(state
, 0x030d, 0x14);
720 ret
= lgdt3305_set_if(state
, p
);
729 ret
= lgdt3305_spectral_inversion(state
, p
,
730 state
->cfg
->spectral_inversion
735 state
->current_modulation
= p
->modulation
;
737 ret
= lgdt3305_mpeg_mode(state
, state
->cfg
->mpeg_mode
);
741 /* lgdt3305_mpeg_mode_polarity calls lgdt3305_soft_reset */
742 ret
= lgdt3305_mpeg_mode_polarity(state
);
747 static int lgdt3305_set_parameters(struct dvb_frontend
*fe
)
749 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
750 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
753 lg_dbg("(%d, %d)\n", p
->frequency
, p
->modulation
);
755 if (fe
->ops
.tuner_ops
.set_params
) {
756 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
757 if (fe
->ops
.i2c_gate_ctrl
)
758 fe
->ops
.i2c_gate_ctrl(fe
, 0);
761 state
->current_frequency
= p
->frequency
;
764 ret
= lgdt3305_set_modulation(state
, p
);
768 ret
= lgdt3305_passband_digital_agc(state
, p
);
771 ret
= lgdt3305_set_agc_power_ref(state
, p
);
774 ret
= lgdt3305_agc_setup(state
, p
);
779 ret
= lgdt3305_write_reg(state
, LGDT3305_GEN_CONTROL
, 0x2f);
782 ret
= lgdt3305_set_reg_bit(state
, LGDT3305_CR_CTR_FREQ_1
, 6, 1);
786 ret
= lgdt3305_set_if(state
, p
);
789 ret
= lgdt3305_spectral_inversion(state
, p
,
790 state
->cfg
->spectral_inversion
795 ret
= lgdt3305_set_filter_extension(state
, p
);
799 state
->current_modulation
= p
->modulation
;
801 ret
= lgdt3305_mpeg_mode(state
, state
->cfg
->mpeg_mode
);
805 /* lgdt3305_mpeg_mode_polarity calls lgdt3305_soft_reset */
806 ret
= lgdt3305_mpeg_mode_polarity(state
);
811 static int lgdt3305_get_frontend(struct dvb_frontend
*fe
,
812 struct dtv_frontend_properties
*p
)
814 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
818 p
->modulation
= state
->current_modulation
;
819 p
->frequency
= state
->current_frequency
;
823 /* ------------------------------------------------------------------------ */
825 static int lgdt3305_read_cr_lock_status(struct lgdt3305_state
*state
,
830 char *cr_lock_state
= "";
834 ret
= lgdt3305_read_reg(state
, LGDT3305_CR_LOCK_STATUS
, &val
);
838 switch (state
->current_modulation
) {
844 switch (val
& 0x07) {
846 cr_lock_state
= "QAM UNLOCK";
849 cr_lock_state
= "QAM 1stLock";
852 cr_lock_state
= "QAM 2ndLock";
855 cr_lock_state
= "QAM FinalLock";
858 cr_lock_state
= "CLOCKQAM-INVALID!";
863 if (val
& (1 << 7)) {
865 cr_lock_state
= "CLOCKVSB";
871 lg_dbg("(%d) %s\n", *locked
, cr_lock_state
);
876 static int lgdt3305_read_fec_lock_status(struct lgdt3305_state
*state
,
880 int ret
, mpeg_lock
, fec_lock
, viterbi_lock
;
884 switch (state
->current_modulation
) {
887 ret
= lgdt3305_read_reg(state
,
888 LGDT3305_FEC_LOCK_STATUS
, &val
);
892 mpeg_lock
= (val
& (1 << 0)) ? 1 : 0;
893 fec_lock
= (val
& (1 << 2)) ? 1 : 0;
894 viterbi_lock
= (val
& (1 << 3)) ? 1 : 0;
896 *locked
= mpeg_lock
&& fec_lock
&& viterbi_lock
;
898 lg_dbg("(%d) %s%s%s\n", *locked
,
899 mpeg_lock
? "mpeg lock " : "",
900 fec_lock
? "fec lock " : "",
901 viterbi_lock
? "viterbi lock" : "");
911 static int lgdt3305_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
913 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
915 int ret
, signal
, inlock
, nofecerr
, snrgood
,
916 cr_lock
, fec_lock
, sync_lock
;
920 ret
= lgdt3305_read_reg(state
, LGDT3305_GEN_STATUS
, &val
);
924 signal
= (val
& (1 << 4)) ? 1 : 0;
925 inlock
= (val
& (1 << 3)) ? 0 : 1;
926 sync_lock
= (val
& (1 << 2)) ? 1 : 0;
927 nofecerr
= (val
& (1 << 1)) ? 1 : 0;
928 snrgood
= (val
& (1 << 0)) ? 1 : 0;
930 lg_dbg("%s%s%s%s%s\n",
931 signal
? "SIGNALEXIST " : "",
932 inlock
? "INLOCK " : "",
933 sync_lock
? "SYNCLOCK " : "",
934 nofecerr
? "NOFECERR " : "",
935 snrgood
? "SNRGOOD " : "");
937 ret
= lgdt3305_read_cr_lock_status(state
, &cr_lock
);
942 *status
|= FE_HAS_SIGNAL
;
944 *status
|= FE_HAS_CARRIER
;
946 *status
|= FE_HAS_VITERBI
;
948 *status
|= FE_HAS_SYNC
;
950 switch (state
->current_modulation
) {
953 /* signal bit is unreliable on the DT3304 in QAM mode */
954 if (((LGDT3304
== state
->cfg
->demod_chip
)) && (cr_lock
))
955 *status
|= FE_HAS_SIGNAL
;
957 ret
= lgdt3305_read_fec_lock_status(state
, &fec_lock
);
962 *status
|= FE_HAS_LOCK
;
966 *status
|= FE_HAS_LOCK
;
975 /* ------------------------------------------------------------------------ */
977 /* borrowed from lgdt330x.c */
978 static u32
calculate_snr(u32 mse
, u32 c
)
980 if (mse
== 0) /* no signal */
985 /* Negative SNR, which is possible, but realisticly the
986 demod will lose lock before the signal gets this bad. The
987 API only allows for unsigned values, so just return 0 */
993 static int lgdt3305_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
995 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
996 u32 noise
; /* noise value */
997 u32 c
; /* per-modulation SNR calculation constant */
999 switch (state
->current_modulation
) {
1002 /* Use Phase Tracker Mean-Square Error Register */
1003 /* SNR for ranges from -13.11 to +44.08 */
1004 noise
= ((read_reg(state
, LGDT3305_PT_MSE_1
) & 0x07) << 16) |
1005 (read_reg(state
, LGDT3305_PT_MSE_2
) << 8) |
1006 (read_reg(state
, LGDT3305_PT_MSE_3
) & 0xff);
1007 c
= 73957994; /* log10(25*32^2)*2^24 */
1009 /* Use Equalizer Mean-Square Error Register */
1010 /* SNR for ranges from -16.12 to +44.08 */
1011 noise
= ((read_reg(state
, LGDT3305_EQ_MSE_1
) & 0x0f) << 16) |
1012 (read_reg(state
, LGDT3305_EQ_MSE_2
) << 8) |
1013 (read_reg(state
, LGDT3305_EQ_MSE_3
) & 0xff);
1014 c
= 73957994; /* log10(25*32^2)*2^24 */
1019 noise
= (read_reg(state
, LGDT3305_CR_MSE_1
) << 8) |
1020 (read_reg(state
, LGDT3305_CR_MSE_2
) & 0xff);
1022 c
= (state
->current_modulation
== QAM_64
) ?
1023 97939837 : 98026066;
1024 /* log10(688128)*2^24 and log10(696320)*2^24 */
1029 state
->snr
= calculate_snr(noise
, c
);
1030 /* report SNR in dB * 10 */
1031 *snr
= (state
->snr
/ ((1 << 24) / 10));
1032 lg_dbg("noise = 0x%08x, snr = %d.%02d dB\n", noise
,
1033 state
->snr
>> 24, (((state
->snr
>> 8) & 0xffff) * 100) >> 16);
1038 static int lgdt3305_read_signal_strength(struct dvb_frontend
*fe
,
1041 /* borrowed from lgdt330x.c
1043 * Calculate strength from SNR up to 35dB
1044 * Even though the SNR can go higher than 35dB,
1045 * there is some comfort factor in having a range of
1046 * strong signals that can show at 100%
1048 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
1054 ret
= fe
->ops
.read_snr(fe
, &snr
);
1057 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
1058 /* scale the range 0 - 35*2^24 into 0 - 65535 */
1059 if (state
->snr
>= 8960 * 0x10000)
1062 *strength
= state
->snr
/ 8960;
1067 /* ------------------------------------------------------------------------ */
1069 static int lgdt3305_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
1075 static int lgdt3305_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
1077 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
1080 (read_reg(state
, LGDT3305_FEC_PKT_ERR_1
) << 8) |
1081 (read_reg(state
, LGDT3305_FEC_PKT_ERR_2
) & 0xff);
1086 static int lgdt3305_get_tune_settings(struct dvb_frontend
*fe
,
1087 struct dvb_frontend_tune_settings
1090 fe_tune_settings
->min_delay_ms
= 500;
1095 static void lgdt3305_release(struct dvb_frontend
*fe
)
1097 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
1102 static const struct dvb_frontend_ops lgdt3304_ops
;
1103 static const struct dvb_frontend_ops lgdt3305_ops
;
1105 struct dvb_frontend
*lgdt3305_attach(const struct lgdt3305_config
*config
,
1106 struct i2c_adapter
*i2c_adap
)
1108 struct lgdt3305_state
*state
= NULL
;
1112 lg_dbg("(%d-%04x)\n",
1113 i2c_adap
? i2c_adapter_id(i2c_adap
) : 0,
1114 config
? config
->i2c_addr
: 0);
1116 state
= kzalloc(sizeof(struct lgdt3305_state
), GFP_KERNEL
);
1120 state
->cfg
= config
;
1121 state
->i2c_adap
= i2c_adap
;
1123 switch (config
->demod_chip
) {
1125 memcpy(&state
->frontend
.ops
, &lgdt3304_ops
,
1126 sizeof(struct dvb_frontend_ops
));
1129 memcpy(&state
->frontend
.ops
, &lgdt3305_ops
,
1130 sizeof(struct dvb_frontend_ops
));
1135 state
->frontend
.demodulator_priv
= state
;
1137 /* verify that we're talking to a lg dt3304/5 */
1138 ret
= lgdt3305_read_reg(state
, LGDT3305_GEN_CTRL_2
, &val
);
1139 if ((lg_fail(ret
)) | (val
== 0))
1141 ret
= lgdt3305_write_reg(state
, 0x0808, 0x80);
1144 ret
= lgdt3305_read_reg(state
, 0x0808, &val
);
1145 if ((lg_fail(ret
)) | (val
!= 0x80))
1147 ret
= lgdt3305_write_reg(state
, 0x0808, 0x00);
1151 state
->current_frequency
= -1;
1152 state
->current_modulation
= -1;
1154 return &state
->frontend
;
1156 lg_warn("unable to detect %s hardware\n",
1157 config
->demod_chip
? "LGDT3304" : "LGDT3305");
1161 EXPORT_SYMBOL(lgdt3305_attach
);
1163 static const struct dvb_frontend_ops lgdt3304_ops
= {
1164 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
1166 .name
= "LG Electronics LGDT3304 VSB/QAM Frontend",
1167 .frequency_min
= 54000000,
1168 .frequency_max
= 858000000,
1169 .frequency_stepsize
= 62500,
1170 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
1172 .i2c_gate_ctrl
= lgdt3305_i2c_gate_ctrl
,
1173 .init
= lgdt3305_init
,
1174 .sleep
= lgdt3305_sleep
,
1175 .set_frontend
= lgdt3304_set_parameters
,
1176 .get_frontend
= lgdt3305_get_frontend
,
1177 .get_tune_settings
= lgdt3305_get_tune_settings
,
1178 .read_status
= lgdt3305_read_status
,
1179 .read_ber
= lgdt3305_read_ber
,
1180 .read_signal_strength
= lgdt3305_read_signal_strength
,
1181 .read_snr
= lgdt3305_read_snr
,
1182 .read_ucblocks
= lgdt3305_read_ucblocks
,
1183 .release
= lgdt3305_release
,
1186 static const struct dvb_frontend_ops lgdt3305_ops
= {
1187 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
1189 .name
= "LG Electronics LGDT3305 VSB/QAM Frontend",
1190 .frequency_min
= 54000000,
1191 .frequency_max
= 858000000,
1192 .frequency_stepsize
= 62500,
1193 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
1195 .i2c_gate_ctrl
= lgdt3305_i2c_gate_ctrl
,
1196 .init
= lgdt3305_init
,
1197 .sleep
= lgdt3305_sleep
,
1198 .set_frontend
= lgdt3305_set_parameters
,
1199 .get_frontend
= lgdt3305_get_frontend
,
1200 .get_tune_settings
= lgdt3305_get_tune_settings
,
1201 .read_status
= lgdt3305_read_status
,
1202 .read_ber
= lgdt3305_read_ber
,
1203 .read_signal_strength
= lgdt3305_read_signal_strength
,
1204 .read_snr
= lgdt3305_read_snr
,
1205 .read_ucblocks
= lgdt3305_read_ucblocks
,
1206 .release
= lgdt3305_release
,
1209 MODULE_DESCRIPTION("LG Electronics LGDT3304/5 ATSC/QAM-B Demodulator Driver");
1210 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1211 MODULE_LICENSE("GPL");
1212 MODULE_VERSION("0.2");