24 #include <asm/div64.h>
26 #include <linux/slab.h>
37 #define lg_printk(kern, fmt, arg...) \
38 printk(kern "%s: " fmt, __func__, ##arg)
40 #define lg_info(fmt, arg...) printk(KERN_INFO "lgdt3305: " fmt, ##arg)
41 #define lg_warn(fmt, arg...) lg_printk(KERN_WARNING, fmt, ##arg)
42 #define lg_err(fmt, arg...) lg_printk(KERN_ERR, fmt, ##arg)
43 #define lg_dbg(fmt, arg...) if (debug & DBG_INFO) \
44 lg_printk(KERN_DEBUG, fmt, ##arg)
45 #define lg_reg(fmt, arg...) if (debug & DBG_REG) \
46 lg_printk(KERN_DEBUG, fmt, ##arg)
48 #define lg_fail(ret) \
53 lg_err("error %d on line %d\n", ret, __LINE__); \
72 #define LGDT3305_GEN_CTRL_1 0x0000
73 #define LGDT3305_GEN_CTRL_2 0x0001
74 #define LGDT3305_GEN_CTRL_3 0x0002
75 #define LGDT3305_GEN_STATUS 0x0003
76 #define LGDT3305_GEN_CONTROL 0x0007
77 #define LGDT3305_GEN_CTRL_4 0x000a
78 #define LGDT3305_DGTL_AGC_REF_1 0x0012
79 #define LGDT3305_DGTL_AGC_REF_2 0x0013
80 #define LGDT3305_CR_CTR_FREQ_1 0x0106
81 #define LGDT3305_CR_CTR_FREQ_2 0x0107
82 #define LGDT3305_CR_CTR_FREQ_3 0x0108
83 #define LGDT3305_CR_CTR_FREQ_4 0x0109
84 #define LGDT3305_CR_MSE_1 0x011b
85 #define LGDT3305_CR_MSE_2 0x011c
86 #define LGDT3305_CR_LOCK_STATUS 0x011d
87 #define LGDT3305_CR_CTRL_7 0x0126
88 #define LGDT3305_AGC_POWER_REF_1 0x0300
89 #define LGDT3305_AGC_POWER_REF_2 0x0301
90 #define LGDT3305_AGC_DELAY_PT_1 0x0302
91 #define LGDT3305_AGC_DELAY_PT_2 0x0303
92 #define LGDT3305_RFAGC_LOOP_FLTR_BW_1 0x0306
93 #define LGDT3305_RFAGC_LOOP_FLTR_BW_2 0x0307
94 #define LGDT3305_IFBW_1 0x0308
95 #define LGDT3305_IFBW_2 0x0309
96 #define LGDT3305_AGC_CTRL_1 0x030c
97 #define LGDT3305_AGC_CTRL_4 0x0314
98 #define LGDT3305_EQ_MSE_1 0x0413
99 #define LGDT3305_EQ_MSE_2 0x0414
100 #define LGDT3305_EQ_MSE_3 0x0415
101 #define LGDT3305_PT_MSE_1 0x0417
102 #define LGDT3305_PT_MSE_2 0x0418
103 #define LGDT3305_PT_MSE_3 0x0419
104 #define LGDT3305_FEC_BLOCK_CTRL 0x0504
105 #define LGDT3305_FEC_LOCK_STATUS 0x050a
106 #define LGDT3305_FEC_PKT_ERR_1 0x050c
107 #define LGDT3305_FEC_PKT_ERR_2 0x050d
108 #define LGDT3305_TP_CTRL_1 0x050e
109 #define LGDT3305_BERT_PERIOD 0x0801
110 #define LGDT3305_BERT_ERROR_COUNT_1 0x080a
111 #define LGDT3305_BERT_ERROR_COUNT_2 0x080b
112 #define LGDT3305_BERT_ERROR_COUNT_3 0x080c
113 #define LGDT3305_BERT_ERROR_COUNT_4 0x080d
118 u8 buf[] = { reg >> 8, reg & 0xff, val };
120 .
addr = state->
cfg->i2c_addr, .flags = 0,
121 .buf =
buf, .len = 3,
124 lg_reg(
"reg: 0x%04x, val: 0x%02x\n", reg, val);
129 lg_err(
"error (addr %02x %02x <- %02x, err = %i)\n",
130 msg.
buf[0], msg.
buf[1], msg.
buf[2], ret);
142 u8 reg_buf[] = { reg >> 8, reg & 0xff };
144 { .addr = state->
cfg->i2c_addr,
145 .flags = 0, .buf = reg_buf, .len = 2 },
146 { .addr = state->
cfg->i2c_addr,
150 lg_reg(
"reg: 0x%04x\n", reg);
155 lg_err(
"error (addr %02x reg %04x error (ret == %i)\n",
156 state->
cfg->i2c_addr, reg, ret);
165 #define read_reg(state, reg) \
168 int ret = lgdt3305_read_reg(state, reg, &__val); \
180 lg_reg(
"reg: 0x%04x, bit: %d, level: %d\n", reg, bit, onoff);
182 ret = lgdt3305_read_reg(state, reg, &val);
187 val |= (onoff & 1) << bit;
189 ret = lgdt3305_write_reg(state, reg, val);
204 lg_reg(
"writing %d registers...\n", len);
206 for (i = 0; i < len - 1; i++) {
207 ret = lgdt3305_write_reg(state, regs[i].reg, regs[i].val);
239 static int lgdt3305_mpeg_mode_polarity(
struct lgdt3305_state *state,
246 lg_dbg(
"edge = %d, valid = %d\n", edge, valid);
263 ret = lgdt3305_soft_reset(state);
300 static int lgdt3305_set_filter_extension(
struct lgdt3305_state *state,
316 lg_dbg(
"val = %d\n", val);
318 return lgdt3305_set_reg_bit(state, 0x043f, 2, val);
323 static int lgdt3305_passband_digital_agc(
struct lgdt3305_state *state,
342 lg_dbg(
"agc ref: 0x%04x\n", agc_ref);
353 u16 ifbw, rfbw, agcdelay;
376 if (state->
cfg->rf_agc_loop) {
377 lg_dbg(
"agcdelay: 0x%04x, rfbw: 0x%04x\n", agcdelay, rfbw);
390 lg_dbg(
"ifbw: 0x%04x\n", ifbw);
419 lg_dbg(
"lockdten = %d, acqen = %d\n", lockdten, acqen);
422 switch (state->
cfg->demod_chip) {
424 lgdt3305_write_reg(state, 0x0314, 0xe1 | lockdten << 1);
425 lgdt3305_set_reg_bit(state, 0x030e, 2, acqen);
435 return lgdt3305_rfagc_loop(state, p);
438 static int lgdt3305_set_agc_power_ref(
struct lgdt3305_state *state,
445 if (state->
cfg->usref_8vsb)
446 usref = state->
cfg->usref_8vsb;
449 if (state->
cfg->usref_qam64)
450 usref = state->
cfg->usref_qam64;
453 if (state->
cfg->usref_qam256)
454 usref = state->
cfg->usref_qam256;
461 lg_dbg(
"set manual mode: 0x%04x\n", usref);
466 0xff & (usref >> 8));
468 0xff & (usref >> 0));
475 static int lgdt3305_spectral_inversion(
struct lgdt3305_state *state,
481 lg_dbg(
"(%d)\n", inversion);
486 inversion ? 0xf9 : 0x79);
491 inversion ? 0xfd : 0xff);
503 u8 nco1, nco2, nco3, nco4;
508 if_freq_khz = state->
cfg->vsb_if_khz;
512 if_freq_khz = state->
cfg->qam_if_khz;
518 nco = if_freq_khz / 10;
534 nco1 = (nco >> 24) & 0x3f;
536 nco2 = (nco >> 16) & 0xff;
537 nco3 = (nco >> 8) & 0xff;
545 lg_dbg(
"%d KHz -> [%02x%02x%02x%02x]\n",
546 if_freq_khz, nco1, nco2, nco3, nco4);
557 if (state->
cfg->deny_i2c_rptr)
569 u8 gen_ctrl_3, gen_ctrl_4;
601 { .reg = 0x000d, .val = 0x02, },
602 { .reg = 0x000e, .val = 0x02, },
610 { .reg = 0x0112, .val = 0x17, },
611 { .reg = 0x0113, .val = 0x15, },
612 { .reg = 0x0114, .val = 0x18, },
613 { .reg = 0x0115, .val = 0xff, },
614 { .reg = 0x0116, .val = 0x3c, },
615 { .reg = 0x0214, .val = 0x67, },
616 { .reg = 0x0424, .val = 0x8d, },
617 { .reg = 0x0427, .val = 0x12, },
618 { .reg = 0x0428, .val = 0x4f, },
621 { .reg = 0x030a, .val = 0x08, },
622 { .reg = 0x030b, .val = 0x9b, },
623 { .reg = 0x030d, .val = 0x00, },
624 { .reg = 0x030e, .val = 0x1c, },
625 { .reg = 0x0314, .val = 0xe1, },
626 { .reg = 0x000d, .val = 0x82, },
660 switch (state->
cfg->demod_chip) {
662 ret = lgdt3305_write_regs(state, lgdt3304_init_data,
666 ret = lgdt3305_write_regs(state, lgdt3305_init_data,
675 ret = lgdt3305_soft_reset(state);
680 static int lgdt3304_set_parameters(
struct dvb_frontend *fe)
688 if (fe->
ops.tuner_ops.set_params) {
689 ret = fe->
ops.tuner_ops.set_params(fe);
690 if (fe->
ops.i2c_gate_ctrl)
691 fe->
ops.i2c_gate_ctrl(fe, 0);
697 ret = lgdt3305_set_modulation(state, p);
701 ret = lgdt3305_passband_digital_agc(state, p);
705 ret = lgdt3305_agc_setup(state, p);
712 lgdt3305_write_reg(state, 0x030d, 0x00);
720 lgdt3305_write_reg(state, 0x030d, 0x14);
721 ret = lgdt3305_set_if(state, p);
730 ret = lgdt3305_spectral_inversion(state, p,
731 state->
cfg->spectral_inversion
743 ret = lgdt3305_mpeg_mode_polarity(state,
744 state->
cfg->tpclk_edge,
745 state->
cfg->tpvalid_polarity);
750 static int lgdt3305_set_parameters(
struct dvb_frontend *fe)
758 if (fe->
ops.tuner_ops.set_params) {
759 ret = fe->
ops.tuner_ops.set_params(fe);
760 if (fe->
ops.i2c_gate_ctrl)
761 fe->
ops.i2c_gate_ctrl(fe, 0);
767 ret = lgdt3305_set_modulation(state, p);
771 ret = lgdt3305_passband_digital_agc(state, p);
774 ret = lgdt3305_set_agc_power_ref(state, p);
777 ret = lgdt3305_agc_setup(state, p);
789 ret = lgdt3305_set_if(state, p);
792 ret = lgdt3305_spectral_inversion(state, p,
793 state->
cfg->spectral_inversion
798 ret = lgdt3305_set_filter_extension(state, p);
809 ret = lgdt3305_mpeg_mode_polarity(state,
810 state->
cfg->tpclk_edge,
811 state->
cfg->tpvalid_polarity);
816 static int lgdt3305_get_frontend(
struct dvb_frontend *fe)
830 static int lgdt3305_read_cr_lock_status(
struct lgdt3305_state *state,
835 char *cr_lock_state =
"";
849 switch (val & 0x07) {
851 cr_lock_state =
"QAM UNLOCK";
854 cr_lock_state =
"QAM 1stLock";
857 cr_lock_state =
"QAM 2ndLock";
860 cr_lock_state =
"QAM FinalLock";
863 cr_lock_state =
"CLOCKQAM-INVALID!";
868 if (val & (1 << 7)) {
870 cr_lock_state =
"CLOCKVSB";
876 lg_dbg(
"(%d) %s\n", *locked, cr_lock_state);
881 static int lgdt3305_read_fec_lock_status(
struct lgdt3305_state *state,
885 int ret, mpeg_lock, fec_lock, viterbi_lock;
892 ret = lgdt3305_read_reg(state,
897 mpeg_lock = (val & (1 << 0)) ? 1 : 0;
898 fec_lock = (val & (1 << 2)) ? 1 : 0;
899 viterbi_lock = (val & (1 << 3)) ? 1 : 0;
901 *locked = mpeg_lock && fec_lock && viterbi_lock;
903 lg_dbg(
"(%d) %s%s%s\n", *locked,
904 mpeg_lock ?
"mpeg lock " :
"",
905 fec_lock ?
"fec lock " :
"",
906 viterbi_lock ?
"viterbi lock" :
"");
920 int ret, signal, inlock, nofecerr, snrgood,
921 cr_lock, fec_lock, sync_lock;
929 signal = (val & (1 << 4)) ? 1 : 0;
930 inlock = (val & (1 << 3)) ? 0 : 1;
931 sync_lock = (val & (1 << 2)) ? 1 : 0;
932 nofecerr = (val & (1 << 1)) ? 1 : 0;
933 snrgood = (val & (1 << 0)) ? 1 : 0;
936 signal ?
"SIGNALEXIST " :
"",
937 inlock ?
"INLOCK " :
"",
938 sync_lock ?
"SYNCLOCK " :
"",
939 nofecerr ?
"NOFECERR " :
"",
940 snrgood ?
"SNRGOOD " :
"");
942 ret = lgdt3305_read_cr_lock_status(state, &cr_lock);
959 if (((
LGDT3304 == state->
cfg->demod_chip)) && (cr_lock))
962 ret = lgdt3305_read_fec_lock_status(state, &fec_lock);
1028 97939837 : 98026066;
1034 state->
snr = calculate_snr(noise, c);
1036 *snr = (state->
snr / ((1 << 24) / 10));
1037 lg_dbg(
"noise = 0x%08x, snr = %d.%02d dB\n", noise,
1038 state->
snr >> 24, (((state->
snr >> 8) & 0xffff) * 100) >> 16);
1043 static int lgdt3305_read_signal_strength(
struct dvb_frontend *fe,
1059 ret = fe->
ops.read_snr(fe, &snr);
1064 if (state->
snr >= 8960 * 0x10000)
1067 *strength = state->
snr / 8960;
1080 static int lgdt3305_read_ucblocks(
struct dvb_frontend *fe,
u32 *ucblocks)
1091 static int lgdt3305_get_tune_settings(
struct dvb_frontend *fe,
1118 i2c_adap ? i2c_adapter_id(i2c_adap) : 0,
1144 if ((
lg_fail(ret)) | (val == 0))
1146 ret = lgdt3305_write_reg(state, 0x0808, 0x80);
1149 ret = lgdt3305_read_reg(state, 0x0808, &val);
1150 if ((
lg_fail(ret)) | (val != 0x80))
1152 ret = lgdt3305_write_reg(state, 0x0808, 0x00);
1161 lg_warn(
"unable to detect %s hardware\n",
1162 config->
demod_chip ?
"LGDT3304" :
"LGDT3305");
1171 .name =
"LG Electronics LGDT3304 VSB/QAM Frontend",
1172 .frequency_min = 54000000,
1173 .frequency_max = 858000000,
1174 .frequency_stepsize = 62500,
1177 .i2c_gate_ctrl = lgdt3305_i2c_gate_ctrl,
1178 .init = lgdt3305_init,
1179 .set_frontend = lgdt3304_set_parameters,
1180 .get_frontend = lgdt3305_get_frontend,
1181 .get_tune_settings = lgdt3305_get_tune_settings,
1182 .read_status = lgdt3305_read_status,
1183 .read_ber = lgdt3305_read_ber,
1184 .read_signal_strength = lgdt3305_read_signal_strength,
1185 .read_snr = lgdt3305_read_snr,
1186 .read_ucblocks = lgdt3305_read_ucblocks,
1187 .release = lgdt3305_release,
1193 .name =
"LG Electronics LGDT3305 VSB/QAM Frontend",
1194 .frequency_min = 54000000,
1195 .frequency_max = 858000000,
1196 .frequency_stepsize = 62500,
1199 .i2c_gate_ctrl = lgdt3305_i2c_gate_ctrl,
1200 .init = lgdt3305_init,
1201 .sleep = lgdt3305_sleep,
1202 .set_frontend = lgdt3305_set_parameters,
1203 .get_frontend = lgdt3305_get_frontend,
1204 .get_tune_settings = lgdt3305_get_tune_settings,
1205 .read_status = lgdt3305_read_status,
1206 .read_ber = lgdt3305_read_ber,
1207 .read_signal_strength = lgdt3305_read_signal_strength,
1208 .read_snr = lgdt3305_read_snr,
1209 .read_ucblocks = lgdt3305_read_ucblocks,
1210 .release = lgdt3305_release,