2 * Support for LGDT3305 - VSB/QAM
4 * Copyright (C) 2008, 2009 Michael Krufky <mkrufky@linuxtv.org>
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 <asm/div64.h>
23 #include <linux/dvb/frontend.h>
28 module_param(debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "set debug level (info=1, reg=2 (or-able))");
34 #define lg_printk(kern, fmt, arg...) \
35 printk(kern "%s: " fmt, __func__, ##arg)
37 #define lg_info(fmt, arg...) printk(KERN_INFO "lgdt3305: " fmt, ##arg)
38 #define lg_warn(fmt, arg...) lg_printk(KERN_WARNING, fmt, ##arg)
39 #define lg_err(fmt, arg...) lg_printk(KERN_ERR, fmt, ##arg)
40 #define lg_dbg(fmt, arg...) if (debug & DBG_INFO) \
41 lg_printk(KERN_DEBUG, fmt, ##arg)
42 #define lg_reg(fmt, arg...) if (debug & DBG_REG) \
43 lg_printk(KERN_DEBUG, fmt, ##arg)
45 #define lg_fail(ret) \
50 lg_err("error %d on line %d\n", ret, __LINE__); \
54 struct lgdt3305_state
{
55 struct i2c_adapter
*i2c_adap
;
56 const struct lgdt3305_config
*cfg
;
58 struct dvb_frontend frontend
;
60 fe_modulation_t current_modulation
;
61 u32 current_frequency
;
65 /* ------------------------------------------------------------------------ */
67 #define LGDT3305_GEN_CTRL_1 0x0000
68 #define LGDT3305_GEN_CTRL_2 0x0001
69 #define LGDT3305_GEN_CTRL_3 0x0002
70 #define LGDT3305_GEN_STATUS 0x0003
71 #define LGDT3305_GEN_CONTROL 0x0007
72 #define LGDT3305_GEN_CTRL_4 0x000a
73 #define LGDT3305_DGTL_AGC_REF_1 0x0012
74 #define LGDT3305_DGTL_AGC_REF_2 0x0013
75 #define LGDT3305_CR_CTR_FREQ_1 0x0106
76 #define LGDT3305_CR_CTR_FREQ_2 0x0107
77 #define LGDT3305_CR_CTR_FREQ_3 0x0108
78 #define LGDT3305_CR_CTR_FREQ_4 0x0109
79 #define LGDT3305_CR_MSE_1 0x011b
80 #define LGDT3305_CR_MSE_2 0x011c
81 #define LGDT3305_CR_LOCK_STATUS 0x011d
82 #define LGDT3305_CR_CTRL_7 0x0126
83 #define LGDT3305_AGC_POWER_REF_1 0x0300
84 #define LGDT3305_AGC_POWER_REF_2 0x0301
85 #define LGDT3305_AGC_DELAY_PT_1 0x0302
86 #define LGDT3305_AGC_DELAY_PT_2 0x0303
87 #define LGDT3305_RFAGC_LOOP_FLTR_BW_1 0x0306
88 #define LGDT3305_RFAGC_LOOP_FLTR_BW_2 0x0307
89 #define LGDT3305_IFBW_1 0x0308
90 #define LGDT3305_IFBW_2 0x0309
91 #define LGDT3305_AGC_CTRL_1 0x030c
92 #define LGDT3305_AGC_CTRL_4 0x0314
93 #define LGDT3305_EQ_MSE_1 0x0413
94 #define LGDT3305_EQ_MSE_2 0x0414
95 #define LGDT3305_EQ_MSE_3 0x0415
96 #define LGDT3305_PT_MSE_1 0x0417
97 #define LGDT3305_PT_MSE_2 0x0418
98 #define LGDT3305_PT_MSE_3 0x0419
99 #define LGDT3305_FEC_BLOCK_CTRL 0x0504
100 #define LGDT3305_FEC_LOCK_STATUS 0x050a
101 #define LGDT3305_FEC_PKT_ERR_1 0x050c
102 #define LGDT3305_FEC_PKT_ERR_2 0x050d
103 #define LGDT3305_TP_CTRL_1 0x050e
104 #define LGDT3305_BERT_PERIOD 0x0801
105 #define LGDT3305_BERT_ERROR_COUNT_1 0x080a
106 #define LGDT3305_BERT_ERROR_COUNT_2 0x080b
107 #define LGDT3305_BERT_ERROR_COUNT_3 0x080c
108 #define LGDT3305_BERT_ERROR_COUNT_4 0x080d
110 static int lgdt3305_write_reg(struct lgdt3305_state
*state
, u16 reg
, u8 val
)
113 u8 buf
[] = { reg
>> 8, reg
& 0xff, val
};
114 struct i2c_msg msg
= {
115 .addr
= state
->cfg
->i2c_addr
, .flags
= 0,
116 .buf
= buf
, .len
= 3,
119 lg_reg("reg: 0x%04x, val: 0x%02x\n", reg
, val
);
121 ret
= i2c_transfer(state
->i2c_adap
, &msg
, 1);
124 lg_err("error (addr %02x %02x <- %02x, err = %i)\n",
125 msg
.buf
[0], msg
.buf
[1], msg
.buf
[2], ret
);
134 static int lgdt3305_read_reg(struct lgdt3305_state
*state
, u16 reg
, u8
*val
)
137 u8 reg_buf
[] = { reg
>> 8, reg
& 0xff };
138 struct i2c_msg msg
[] = {
139 { .addr
= state
->cfg
->i2c_addr
,
140 .flags
= 0, .buf
= reg_buf
, .len
= 2 },
141 { .addr
= state
->cfg
->i2c_addr
,
142 .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
145 lg_reg("reg: 0x%04x\n", reg
);
147 ret
= i2c_transfer(state
->i2c_adap
, msg
, 2);
150 lg_err("error (addr %02x reg %04x error (ret == %i)\n",
151 state
->cfg
->i2c_addr
, reg
, ret
);
160 #define read_reg(state, reg) \
163 int ret = lgdt3305_read_reg(state, reg, &__val); \
169 static int lgdt3305_set_reg_bit(struct lgdt3305_state
*state
,
170 u16 reg
, int bit
, int onoff
)
175 lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg
, bit
, onoff
);
177 ret
= lgdt3305_read_reg(state
, reg
, &val
);
182 val
|= (onoff
& 1) << bit
;
184 ret
= lgdt3305_write_reg(state
, reg
, val
);
189 struct lgdt3305_reg
{
194 static int lgdt3305_write_regs(struct lgdt3305_state
*state
,
195 struct lgdt3305_reg
*regs
, int len
)
199 lg_reg("writing %d registers...\n", len
);
201 for (i
= 0; i
< len
- 1; i
++) {
202 ret
= lgdt3305_write_reg(state
, regs
[i
].reg
, regs
[i
].val
);
209 /* ------------------------------------------------------------------------ */
211 static int lgdt3305_soft_reset(struct lgdt3305_state
*state
)
217 ret
= lgdt3305_set_reg_bit(state
, LGDT3305_GEN_CTRL_3
, 0, 0);
222 ret
= lgdt3305_set_reg_bit(state
, LGDT3305_GEN_CTRL_3
, 0, 1);
227 static inline int lgdt3305_mpeg_mode(struct lgdt3305_state
*state
,
228 enum lgdt3305_mpeg_mode mode
)
230 lg_dbg("(%d)\n", mode
);
231 return lgdt3305_set_reg_bit(state
, LGDT3305_TP_CTRL_1
, 5, mode
);
234 static int lgdt3305_mpeg_mode_polarity(struct lgdt3305_state
*state
,
235 enum lgdt3305_tp_clock_edge edge
,
236 enum lgdt3305_tp_valid_polarity valid
)
241 lg_dbg("edge = %d, valid = %d\n", edge
, valid
);
243 ret
= lgdt3305_read_reg(state
, LGDT3305_TP_CTRL_1
, &val
);
254 ret
= lgdt3305_write_reg(state
, LGDT3305_TP_CTRL_1
, val
);
258 ret
= lgdt3305_soft_reset(state
);
263 static int lgdt3305_set_modulation(struct lgdt3305_state
*state
,
264 struct dvb_frontend_parameters
*param
)
271 ret
= lgdt3305_read_reg(state
, LGDT3305_GEN_CTRL_1
, &opermode
);
277 switch (param
->u
.vsb
.modulation
) {
290 ret
= lgdt3305_write_reg(state
, LGDT3305_GEN_CTRL_1
, opermode
);
295 static int lgdt3305_set_filter_extension(struct lgdt3305_state
*state
,
296 struct dvb_frontend_parameters
*param
)
300 switch (param
->u
.vsb
.modulation
) {
311 lg_dbg("val = %d\n", val
);
313 return lgdt3305_set_reg_bit(state
, 0x043f, 2, val
);
316 /* ------------------------------------------------------------------------ */
318 static int lgdt3305_passband_digital_agc(struct lgdt3305_state
*state
,
319 struct dvb_frontend_parameters
*param
)
323 switch (param
->u
.vsb
.modulation
) {
337 lg_dbg("agc ref: 0x%04x\n", agc_ref
);
339 lgdt3305_write_reg(state
, LGDT3305_DGTL_AGC_REF_1
, agc_ref
>> 8);
340 lgdt3305_write_reg(state
, LGDT3305_DGTL_AGC_REF_2
, agc_ref
& 0xff);
345 static int lgdt3305_rfagc_loop(struct lgdt3305_state
*state
,
346 struct dvb_frontend_parameters
*param
)
348 u16 ifbw
, rfbw
, agcdelay
;
350 switch (param
->u
.vsb
.modulation
) {
366 if (state
->cfg
->rf_agc_loop
) {
367 lg_dbg("agcdelay: 0x%04x, rfbw: 0x%04x\n", agcdelay
, rfbw
);
369 /* rf agc loop filter bandwidth */
370 lgdt3305_write_reg(state
, LGDT3305_AGC_DELAY_PT_1
,
372 lgdt3305_write_reg(state
, LGDT3305_AGC_DELAY_PT_2
,
375 lgdt3305_write_reg(state
, LGDT3305_RFAGC_LOOP_FLTR_BW_1
,
377 lgdt3305_write_reg(state
, LGDT3305_RFAGC_LOOP_FLTR_BW_2
,
380 lg_dbg("ifbw: 0x%04x\n", ifbw
);
382 /* if agc loop filter bandwidth */
383 lgdt3305_write_reg(state
, LGDT3305_IFBW_1
, ifbw
>> 8);
384 lgdt3305_write_reg(state
, LGDT3305_IFBW_2
, ifbw
& 0xff);
390 static int lgdt3305_agc_setup(struct lgdt3305_state
*state
,
391 struct dvb_frontend_parameters
*param
)
395 switch (param
->u
.vsb
.modulation
) {
409 lg_dbg("lockdten = %d, acqen = %d\n", lockdten
, acqen
);
411 /* control agc function */
412 lgdt3305_write_reg(state
, LGDT3305_AGC_CTRL_4
, 0xe1 | lockdten
<< 1);
413 lgdt3305_set_reg_bit(state
, LGDT3305_AGC_CTRL_1
, 2, acqen
);
415 return lgdt3305_rfagc_loop(state
, param
);
418 static int lgdt3305_set_agc_power_ref(struct lgdt3305_state
*state
,
419 struct dvb_frontend_parameters
*param
)
423 switch (param
->u
.vsb
.modulation
) {
425 if (state
->cfg
->usref_8vsb
)
426 usref
= state
->cfg
->usref_8vsb
;
429 if (state
->cfg
->usref_qam64
)
430 usref
= state
->cfg
->usref_qam64
;
433 if (state
->cfg
->usref_qam256
)
434 usref
= state
->cfg
->usref_qam256
;
441 lg_dbg("set manual mode: 0x%04x\n", usref
);
443 lgdt3305_set_reg_bit(state
, LGDT3305_AGC_CTRL_1
, 3, 1);
445 lgdt3305_write_reg(state
, LGDT3305_AGC_POWER_REF_1
,
446 0xff & (usref
>> 8));
447 lgdt3305_write_reg(state
, LGDT3305_AGC_POWER_REF_2
,
448 0xff & (usref
>> 0));
453 /* ------------------------------------------------------------------------ */
455 static int lgdt3305_spectral_inversion(struct lgdt3305_state
*state
,
456 struct dvb_frontend_parameters
*param
,
461 lg_dbg("(%d)\n", inversion
);
463 switch (param
->u
.vsb
.modulation
) {
465 ret
= lgdt3305_write_reg(state
, LGDT3305_CR_CTRL_7
,
466 inversion
? 0xf9 : 0x79);
470 ret
= lgdt3305_write_reg(state
, LGDT3305_FEC_BLOCK_CTRL
,
471 inversion
? 0xfd : 0xff);
479 static int lgdt3305_set_if(struct lgdt3305_state
*state
,
480 struct dvb_frontend_parameters
*param
)
483 u8 nco1
, nco2
, nco3
, nco4
;
486 switch (param
->u
.vsb
.modulation
) {
488 if_freq_khz
= state
->cfg
->vsb_if_khz
;
492 if_freq_khz
= state
->cfg
->qam_if_khz
;
498 nco
= if_freq_khz
/ 10;
500 switch (param
->u
.vsb
.modulation
) {
514 nco1
= (nco
>> 24) & 0x3f;
516 nco2
= (nco
>> 16) & 0xff;
517 nco3
= (nco
>> 8) & 0xff;
520 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_1
, nco1
);
521 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_2
, nco2
);
522 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_3
, nco3
);
523 lgdt3305_write_reg(state
, LGDT3305_CR_CTR_FREQ_4
, nco4
);
525 lg_dbg("%d KHz -> [%02x%02x%02x%02x]\n",
526 if_freq_khz
, nco1
, nco2
, nco3
, nco4
);
531 /* ------------------------------------------------------------------------ */
533 static int lgdt3305_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
535 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
537 if (state
->cfg
->deny_i2c_rptr
)
540 lg_dbg("(%d)\n", enable
);
542 return lgdt3305_set_reg_bit(state
, LGDT3305_GEN_CTRL_2
, 5,
546 static int lgdt3305_sleep(struct dvb_frontend
*fe
)
548 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
549 u8 gen_ctrl_3
, gen_ctrl_4
;
553 gen_ctrl_3
= read_reg(state
, LGDT3305_GEN_CTRL_3
);
554 gen_ctrl_4
= read_reg(state
, LGDT3305_GEN_CTRL_4
);
556 /* hold in software reset while sleeping */
558 /* tristate the IF-AGC pin */
560 /* tristate the RF-AGC pin */
563 /* disable vsb/qam module */
565 /* disable adc module */
568 lgdt3305_write_reg(state
, LGDT3305_GEN_CTRL_3
, gen_ctrl_3
);
569 lgdt3305_write_reg(state
, LGDT3305_GEN_CTRL_4
, gen_ctrl_4
);
574 static int lgdt3305_init(struct dvb_frontend
*fe
)
576 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
579 static struct lgdt3305_reg lgdt3305_init_data
[] = {
580 { .reg
= LGDT3305_GEN_CTRL_1
,
582 { .reg
= LGDT3305_GEN_CTRL_2
,
584 { .reg
= LGDT3305_GEN_CTRL_3
,
586 { .reg
= LGDT3305_GEN_CONTROL
,
588 { .reg
= LGDT3305_GEN_CTRL_4
,
590 { .reg
= LGDT3305_DGTL_AGC_REF_1
,
592 { .reg
= LGDT3305_DGTL_AGC_REF_2
,
594 { .reg
= LGDT3305_CR_CTR_FREQ_1
,
596 { .reg
= LGDT3305_CR_CTR_FREQ_2
,
598 { .reg
= LGDT3305_CR_CTR_FREQ_3
,
600 { .reg
= LGDT3305_CR_CTR_FREQ_4
,
602 { .reg
= LGDT3305_CR_CTRL_7
,
604 { .reg
= LGDT3305_AGC_POWER_REF_1
,
606 { .reg
= LGDT3305_AGC_POWER_REF_2
,
608 { .reg
= LGDT3305_AGC_DELAY_PT_1
,
610 { .reg
= LGDT3305_AGC_DELAY_PT_2
,
612 { .reg
= LGDT3305_RFAGC_LOOP_FLTR_BW_1
,
614 { .reg
= LGDT3305_RFAGC_LOOP_FLTR_BW_2
,
616 { .reg
= LGDT3305_IFBW_1
,
618 { .reg
= LGDT3305_IFBW_2
,
620 { .reg
= LGDT3305_AGC_CTRL_1
,
622 { .reg
= LGDT3305_AGC_CTRL_4
,
624 { .reg
= LGDT3305_FEC_BLOCK_CTRL
,
626 { .reg
= LGDT3305_TP_CTRL_1
,
632 ret
= lgdt3305_write_regs(state
, lgdt3305_init_data
,
633 ARRAY_SIZE(lgdt3305_init_data
));
637 ret
= lgdt3305_soft_reset(state
);
642 static int lgdt3305_set_parameters(struct dvb_frontend
*fe
,
643 struct dvb_frontend_parameters
*param
)
645 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
648 lg_dbg("(%d, %d)\n", param
->frequency
, param
->u
.vsb
.modulation
);
650 if (fe
->ops
.tuner_ops
.set_params
) {
651 ret
= fe
->ops
.tuner_ops
.set_params(fe
, param
);
652 if (fe
->ops
.i2c_gate_ctrl
)
653 fe
->ops
.i2c_gate_ctrl(fe
, 0);
656 state
->current_frequency
= param
->frequency
;
659 ret
= lgdt3305_set_modulation(state
, param
);
663 ret
= lgdt3305_passband_digital_agc(state
, param
);
666 ret
= lgdt3305_set_agc_power_ref(state
, param
);
669 ret
= lgdt3305_agc_setup(state
, param
);
674 ret
= lgdt3305_write_reg(state
, LGDT3305_GEN_CONTROL
, 0x2f);
677 ret
= lgdt3305_set_reg_bit(state
, LGDT3305_CR_CTR_FREQ_1
, 6, 1);
681 ret
= lgdt3305_set_if(state
, param
);
684 ret
= lgdt3305_spectral_inversion(state
, param
,
685 state
->cfg
->spectral_inversion
690 ret
= lgdt3305_set_filter_extension(state
, param
);
694 state
->current_modulation
= param
->u
.vsb
.modulation
;
696 ret
= lgdt3305_mpeg_mode(state
, state
->cfg
->mpeg_mode
);
700 /* lgdt3305_mpeg_mode_polarity calls lgdt3305_soft_reset */
701 ret
= lgdt3305_mpeg_mode_polarity(state
,
702 state
->cfg
->tpclk_edge
,
703 state
->cfg
->tpvalid_polarity
);
708 static int lgdt3305_get_frontend(struct dvb_frontend
*fe
,
709 struct dvb_frontend_parameters
*param
)
711 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
715 param
->u
.vsb
.modulation
= state
->current_modulation
;
716 param
->frequency
= state
->current_frequency
;
720 /* ------------------------------------------------------------------------ */
722 static int lgdt3305_read_cr_lock_status(struct lgdt3305_state
*state
,
727 char *cr_lock_state
= "";
731 ret
= lgdt3305_read_reg(state
, LGDT3305_CR_LOCK_STATUS
, &val
);
735 switch (state
->current_modulation
) {
741 switch (val
& 0x07) {
743 cr_lock_state
= "QAM UNLOCK";
746 cr_lock_state
= "QAM 1stLock";
749 cr_lock_state
= "QAM 2ndLock";
752 cr_lock_state
= "QAM FinalLock";
755 cr_lock_state
= "CLOCKQAM-INVALID!";
760 if (val
& (1 << 7)) {
762 cr_lock_state
= "CLOCKVSB";
768 lg_dbg("(%d) %s\n", *locked
, cr_lock_state
);
773 static int lgdt3305_read_fec_lock_status(struct lgdt3305_state
*state
,
777 int ret
, mpeg_lock
, fec_lock
, viterbi_lock
;
781 switch (state
->current_modulation
) {
784 ret
= lgdt3305_read_reg(state
,
785 LGDT3305_FEC_LOCK_STATUS
, &val
);
789 mpeg_lock
= (val
& (1 << 0)) ? 1 : 0;
790 fec_lock
= (val
& (1 << 2)) ? 1 : 0;
791 viterbi_lock
= (val
& (1 << 3)) ? 1 : 0;
793 *locked
= mpeg_lock
&& fec_lock
&& viterbi_lock
;
795 lg_dbg("(%d) %s%s%s\n", *locked
,
796 mpeg_lock
? "mpeg lock " : "",
797 fec_lock
? "fec lock " : "",
798 viterbi_lock
? "viterbi lock" : "");
808 static int lgdt3305_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
810 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
812 int ret
, signal
, inlock
, nofecerr
, snrgood
,
813 cr_lock
, fec_lock
, sync_lock
;
817 ret
= lgdt3305_read_reg(state
, LGDT3305_GEN_STATUS
, &val
);
821 signal
= (val
& (1 << 4)) ? 1 : 0;
822 inlock
= (val
& (1 << 3)) ? 0 : 1;
823 sync_lock
= (val
& (1 << 2)) ? 1 : 0;
824 nofecerr
= (val
& (1 << 1)) ? 1 : 0;
825 snrgood
= (val
& (1 << 0)) ? 1 : 0;
827 lg_dbg("%s%s%s%s%s\n",
828 signal
? "SIGNALEXIST " : "",
829 inlock
? "INLOCK " : "",
830 sync_lock
? "SYNCLOCK " : "",
831 nofecerr
? "NOFECERR " : "",
832 snrgood
? "SNRGOOD " : "");
834 ret
= lgdt3305_read_cr_lock_status(state
, &cr_lock
);
839 *status
|= FE_HAS_SIGNAL
;
841 *status
|= FE_HAS_CARRIER
;
843 *status
|= FE_HAS_VITERBI
;
845 *status
|= FE_HAS_SYNC
;
847 switch (state
->current_modulation
) {
850 ret
= lgdt3305_read_fec_lock_status(state
, &fec_lock
);
855 *status
|= FE_HAS_LOCK
;
859 *status
|= FE_HAS_LOCK
;
868 /* ------------------------------------------------------------------------ */
870 /* borrowed from lgdt330x.c */
871 static u32
calculate_snr(u32 mse
, u32 c
)
873 if (mse
== 0) /* no signal */
878 /* Negative SNR, which is possible, but realisticly the
879 demod will lose lock before the signal gets this bad. The
880 API only allows for unsigned values, so just return 0 */
886 static int lgdt3305_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
888 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
889 u32 noise
; /* noise value */
890 u32 c
; /* per-modulation SNR calculation constant */
892 switch (state
->current_modulation
) {
895 /* Use Phase Tracker Mean-Square Error Register */
896 /* SNR for ranges from -13.11 to +44.08 */
897 noise
= ((read_reg(state
, LGDT3305_PT_MSE_1
) & 0x07) << 16) |
898 (read_reg(state
, LGDT3305_PT_MSE_2
) << 8) |
899 (read_reg(state
, LGDT3305_PT_MSE_3
) & 0xff);
900 c
= 73957994; /* log10(25*32^2)*2^24 */
902 /* Use Equalizer Mean-Square Error Register */
903 /* SNR for ranges from -16.12 to +44.08 */
904 noise
= ((read_reg(state
, LGDT3305_EQ_MSE_1
) & 0x0f) << 16) |
905 (read_reg(state
, LGDT3305_EQ_MSE_2
) << 8) |
906 (read_reg(state
, LGDT3305_EQ_MSE_3
) & 0xff);
907 c
= 73957994; /* log10(25*32^2)*2^24 */
912 noise
= (read_reg(state
, LGDT3305_CR_MSE_1
) << 8) |
913 (read_reg(state
, LGDT3305_CR_MSE_2
) & 0xff);
915 c
= (state
->current_modulation
== QAM_64
) ?
917 /* log10(688128)*2^24 and log10(696320)*2^24 */
922 state
->snr
= calculate_snr(noise
, c
);
923 /* report SNR in dB * 10 */
924 *snr
= (state
->snr
/ ((1 << 24) / 10));
925 lg_dbg("noise = 0x%08x, snr = %d.%02d dB\n", noise
,
926 state
->snr
>> 24, (((state
->snr
>> 8) & 0xffff) * 100) >> 16);
931 static int lgdt3305_read_signal_strength(struct dvb_frontend
*fe
,
934 /* borrowed from lgdt330x.c
936 * Calculate strength from SNR up to 35dB
937 * Even though the SNR can go higher than 35dB,
938 * there is some comfort factor in having a range of
939 * strong signals that can show at 100%
941 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
947 ret
= fe
->ops
.read_snr(fe
, &snr
);
950 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
951 /* scale the range 0 - 35*2^24 into 0 - 65535 */
952 if (state
->snr
>= 8960 * 0x10000)
955 *strength
= state
->snr
/ 8960;
960 /* ------------------------------------------------------------------------ */
962 static int lgdt3305_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
968 static int lgdt3305_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
970 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
973 (read_reg(state
, LGDT3305_FEC_PKT_ERR_1
) << 8) |
974 (read_reg(state
, LGDT3305_FEC_PKT_ERR_2
) & 0xff);
979 static int lgdt3305_get_tune_settings(struct dvb_frontend
*fe
,
980 struct dvb_frontend_tune_settings
983 fe_tune_settings
->min_delay_ms
= 500;
988 static void lgdt3305_release(struct dvb_frontend
*fe
)
990 struct lgdt3305_state
*state
= fe
->demodulator_priv
;
995 static struct dvb_frontend_ops lgdt3305_ops
;
997 struct dvb_frontend
*lgdt3305_attach(const struct lgdt3305_config
*config
,
998 struct i2c_adapter
*i2c_adap
)
1000 struct lgdt3305_state
*state
= NULL
;
1004 lg_dbg("(%d-%04x)\n",
1005 i2c_adap
? i2c_adapter_id(i2c_adap
) : 0,
1006 config
? config
->i2c_addr
: 0);
1008 state
= kzalloc(sizeof(struct lgdt3305_state
), GFP_KERNEL
);
1012 state
->cfg
= config
;
1013 state
->i2c_adap
= i2c_adap
;
1015 memcpy(&state
->frontend
.ops
, &lgdt3305_ops
,
1016 sizeof(struct dvb_frontend_ops
));
1017 state
->frontend
.demodulator_priv
= state
;
1019 /* verify that we're talking to a lg dt3305 */
1020 ret
= lgdt3305_read_reg(state
, LGDT3305_GEN_CTRL_2
, &val
);
1021 if ((lg_fail(ret
)) | (val
== 0))
1023 ret
= lgdt3305_write_reg(state
, 0x0808, 0x80);
1026 ret
= lgdt3305_read_reg(state
, 0x0808, &val
);
1027 if ((lg_fail(ret
)) | (val
!= 0x80))
1029 ret
= lgdt3305_write_reg(state
, 0x0808, 0x00);
1033 state
->current_frequency
= -1;
1034 state
->current_modulation
= -1;
1036 return &state
->frontend
;
1038 lg_warn("unable to detect LGDT3305 hardware\n");
1042 EXPORT_SYMBOL(lgdt3305_attach
);
1044 static struct dvb_frontend_ops lgdt3305_ops
= {
1046 .name
= "LG Electronics LGDT3305 VSB/QAM Frontend",
1048 .frequency_min
= 54000000,
1049 .frequency_max
= 858000000,
1050 .frequency_stepsize
= 62500,
1051 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
1053 .i2c_gate_ctrl
= lgdt3305_i2c_gate_ctrl
,
1054 .init
= lgdt3305_init
,
1055 .sleep
= lgdt3305_sleep
,
1056 .set_frontend
= lgdt3305_set_parameters
,
1057 .get_frontend
= lgdt3305_get_frontend
,
1058 .get_tune_settings
= lgdt3305_get_tune_settings
,
1059 .read_status
= lgdt3305_read_status
,
1060 .read_ber
= lgdt3305_read_ber
,
1061 .read_signal_strength
= lgdt3305_read_signal_strength
,
1062 .read_snr
= lgdt3305_read_snr
,
1063 .read_ucblocks
= lgdt3305_read_ucblocks
,
1064 .release
= lgdt3305_release
,
1067 MODULE_DESCRIPTION("LG Electronics LGDT3305 ATSC/QAM-B Demodulator Driver");
1068 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1069 MODULE_LICENSE("GPL");
1070 MODULE_VERSION("0.1");