25 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
34 static int it913x_debug;
39 #define dprintk(level, args...) do { \
40 if (level & it913x_debug) \
41 printk(KERN_DEBUG "it913x-fe: " args); \
44 #define deb_info(args...) dprintk(0x01, args)
45 #define debug_data_snipet(level, name, p) \
46 dprintk(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
47 *p, *(p+1), *(p+2), *(p+3), *(p+4), \
48 *(p+5), *(p+6), *(p+7));
49 #define info(format, arg...) \
50 printk(KERN_INFO "it913x-fe: " format "\n" , ## arg)
81 .
buf = b, .
len =
sizeof(b) },
83 .
buf = data, .
len = count }
85 b[0] = (
u8) reg >> 24;
86 b[1] = (
u8)(reg >> 16) & 0xff;
87 b[2] = (
u8)(reg >> 8) & 0xff;
88 b[3] = (
u8) reg & 0xff;
99 ret = it913x_read_reg(state, reg, &b[0],
sizeof(b));
100 return (ret < 0) ? -
ENODEV : b[0];
109 .buf = b, .
len = count + 4 }
113 b[0] = (
u8) reg >> 24;
114 b[1] = (
u8)(reg >> 16) & 0xff;
115 b[2] = (
u8)(reg >> 8) & 0xff;
116 b[3] = (
u8) reg & 0xff;
117 memcpy(&b[4], buf, count);
135 b[1] = (data >> 16) & 0xff;
136 b[2] = (data >> 8) & 0xff;
141 else if (data < 0x1000)
143 else if (data < 0x100000)
148 ret = it913x_write(state, pro, reg, &b[s],
sizeof(b) - s);
157 if (loadscript ==
NULL)
160 for (i = 0; i < 1000; ++
i) {
161 if (loadscript[i].pro == 0xff)
163 ret = it913x_write(state, loadscript[i].pro,
165 loadscript[i].reg, loadscript[i].count);
176 u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
179 reg = it913x_read_reg_u8(state, 0xec86);
198 reg = it913x_read_reg_u8(state, 0xed03);
207 for (i = 0; i < 50; i++) {
208 ret = it913x_read_reg(state, 0xed23, &b[0],
sizeof(b));
209 reg = (b[1] << 8) + b[0];
220 if (state->
config->chip_ver > 1)
223 for (i = 0; i < 50; i++) {
224 reg = it913x_read_reg_u8(state, 0xec82);
233 return it913x_write_reg(state,
PRO_DMOD, 0xed81, val);
239 struct it913xset *set_tuner = set_it9137_template;
250 if (state->
config->firmware_ver == 1)
251 set_tuner = set_it9135_template;
253 set_tuner = set_it9137_template;
255 deb_info(
"Tuner Frequency %d Bandwidth %d", frequency, bandwidth);
257 if (frequency >= 51000 && frequency <= 440000) {
260 }
else if (frequency > 440000 && frequency <= 484000) {
263 }
else if (frequency > 484000 && frequency <= 533000) {
266 }
else if (frequency > 533000 && frequency <= 587000) {
269 }
else if (frequency > 587000 && frequency <= 645000) {
272 }
else if (frequency > 645000 && frequency <= 710000) {
275 }
else if (frequency > 710000 && frequency <= 782000) {
278 }
else if (frequency > 782000 && frequency <= 860000) {
281 }
else if (frequency > 1450000 && frequency <= 1492000) {
284 }
else if (frequency > 1660000 && frequency <= 1685000) {
289 set_tuner[0].
reg[0] = lna_band;
307 set_tuner[1].
reg[0] = bw;
308 set_tuner[2].
reg[0] = 0xa0 | (l_band << 3);
310 if (frequency > 53000 && frequency <= 74000) {
313 }
else if (frequency > 74000 && frequency <= 111000) {
316 }
else if (frequency > 111000 && frequency <= 148000) {
319 }
else if (frequency > 148000 && frequency <= 222000) {
322 }
else if (frequency > 222000 && frequency <= 296000) {
325 }
else if (frequency > 296000 && frequency <= 445000) {
328 }
else if (frequency > 445000 && frequency <= state->tun_fn_min) {
331 }
else if (frequency > state->
tun_fn_min && frequency <= 950000) {
334 }
else if (frequency > 1450000 && frequency <= 1680000) {
340 reg = it913x_read_reg_u8(state, 0xed81);
341 iqik_m_cal = (
u16)reg * n_div;
345 iqik_m_cal = (iqik_m_cal * 9) >> 5;
349 iqik_m_cal = 0x40 - iqik_m_cal;
351 iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
353 iqik_m_cal = ~(iqik_m_cal >> 1);
360 if ((temp_f - tmp) >= (state->
tun_xtal >> 1))
363 freq += (
u32) n << 13;
365 temp_f = freq + (
u32)iqik_m_cal;
367 set_tuner[3].
reg[0] = temp_f & 0xff;
368 set_tuner[4].
reg[0] = (temp_f >> 8) & 0xff;
370 deb_info(
"High Frequency = %04x", temp_f);
373 set_tuner[5].
reg[0] = freq & 0xff;
374 set_tuner[6].
reg[0] = (freq >> 8) & 0xff;
376 deb_info(
"low Frequency = %04x", freq);
378 ret = it913x_fe_script_loader(state, set_tuner);
380 return (ret < 0) ? -
ENODEV : 0;
384 u32 bandwidth,
u32 adcFrequency)
389 u16 bfsfcw_fftinx_ratio;
390 u16 fftinx_bfsfcw_ratio;
395 deb_info(
"Bandwidth %d Adc %d", bandwidth, adcFrequency);
418 coeff[0] = state->
table[bw].coeff_1_2048;
419 coeff[1] = state->
table[bw].coeff_2_2k;
420 coeff[2] = state->
table[bw].coeff_1_8191;
421 coeff[3] = state->
table[bw].coeff_1_8192;
422 coeff[4] = state->
table[bw].coeff_1_8193;
423 coeff[5] = state->
table[bw].coeff_2_8k;
424 coeff[6] = state->
table[bw].coeff_1_4096;
425 coeff[7] = state->
table[bw].coeff_2_4k;
426 bfsfcw_fftinx_ratio = state->
table[bw].bfsfcw_fftinx_ratio;
427 fftinx_bfsfcw_ratio = state->
table[bw].fftinx_bfsfcw_ratio;
430 ret = it913x_read_reg_u8(state,
ADC_X_2);
439 for (i = 0; i < 8; i++) {
440 if (adcmultiplier == 1)
442 buffer[count++] = (coeff[
i] >> 24) & 0x3;
443 buffer[count++] = (coeff[
i] >> 16) & 0xff;
444 buffer[count++] = (coeff[
i] >> 8) & 0xff;
445 buffer[count++] = coeff[
i] & 0xff;
449 buffer[count++] = bfsfcw_fftinx_ratio & 0xff;
450 buffer[count++] = (bfsfcw_fftinx_ratio >> 8) & 0xff;
452 buffer[count++] = fftinx_bfsfcw_ratio & 0xff;
453 buffer[count++] = (fftinx_bfsfcw_ratio >> 8) & 0xff;
457 for (i = 0; i < 42; i += 8)
476 for (i = 0; i < 40; i++) {
491 ret = it913x_read_reg_u8(state,
TPSD_LOCK);
509 static int it913x_get_signal_strength(
struct dvb_frontend *fe)
527 temp = (ret - 100) - lna_gain_os;
534 if (code_rate >=
ARRAY_SIZE(it913x_qpsk_pval))
537 deb_info(
"Reg VAR_P_INBAND:%d Calc Offset Value:%d", ret, temp);
542 temp -= it913x_qpsk_pval[code_rate];
545 temp -= it913x_16qam_pval[code_rate];
548 temp -= it913x_64qam_pval[code_rate];
556 else if ((-15 <= temp) && (temp < 0))
557 ret = (2 * (temp + 15)) / 3;
558 else if ((0 <= temp) && (temp < 20))
560 else if ((20 <= temp) && (temp < 35))
561 ret = (2 * (temp - 20)) / 3 + 90;
565 deb_info(
"Signal Strength :%d", ret);
570 static int it913x_fe_read_signal_strength(
struct dvb_frontend *fe,
575 if (state->
config->read_slevel) {
579 ret = it913x_get_signal_strength(fe);
582 *strength = (
u16)((
u32)ret * 0xffff / 0x64);
584 return (ret < 0) ? -
ENODEV : 0;
592 u32 snr_val, snr_min, snr_max;
595 ret = it913x_read_reg(state, 0x2c, reg,
sizeof(reg));
597 snr_val = (
u32)(reg[2] << 16) | (reg[1] << 8) | reg[0];
599 ret |= it913x_read_reg(state, 0xf78b, reg, 1);
620 if (snr_val < snr_min)
622 else if (snr_val < snr_max) {
623 temp = (snr_val - snr_min) >> 5;
625 temp /= (snr_max - snr_min) >> 5;
630 return (ret < 0) ? -
ENODEV : 0;
640 *ber = (
u32)(reg[4] << 16) | (reg[3] << 8) | reg[2];
656 static int it913x_fe_get_frontend(
struct dvb_frontend *fe)
688 static int it913x_fe_set_frontend(
struct dvb_frontend *fe)
693 u8 empty_ch, last_ch;
731 it9137_set_tuner(state,
735 if (fe->
ops.tuner_ops.set_params) {
736 fe->
ops.tuner_ops.set_params(fe);
737 if (fe->
ops.i2c_gate_ctrl)
738 fe->
ops.i2c_gate_ctrl(fe, 0);
747 for (i = 0; i < 40; ++
i) {
749 if (last_ch == 1 && empty_ch == 1)
751 if (last_ch == 2 && empty_ch == 2)
756 for (i = 0; i < 40; ++
i) {
775 for (i = 0; i < 128; i++) {
788 ret |= it913x_fe_script_loader(state, it9137_tuner_off);
790 return (ret < 0) ? -
ENODEV : 0;
800 return it913x_fe_suspend(state);
814 for (i = 0; i <
x; i++) {
823 res = (c <<
x) + res;
833 u8 adf = (state->
config->adf & 0xf);
837 if (state->
config->chip_ver == 1)
838 ret = it913x_init_tuner(state);
840 info(
"ADF table value :%02x", adf);
844 state->
table = fe_clockTable[adf].table;
847 adc = compute_div(state->
adcFrequency, 1000000ul, 19ul);
859 ret |= it913x_write_reg(state,
PRO_DMOD, 0xf5ca, 0x01);
860 ret |= it913x_write_reg(state,
PRO_DMOD, 0xf715, 0x01);
863 b[1] = (xtal >> 8) & 0xff;
864 b[2] = (xtal >> 16) & 0xff;
869 b[1] = (adc >> 8) & 0xff;
870 b[2] = (adc >> 16) & 0xff;
873 if (state->
config->adc_x2)
878 ret |= it913x_write(state,
PRO_DMOD, 0x0029, b, 3);
880 info(
"Crystal Frequency :%d Adc Frequency :%d ADC X2: %02x",
883 deb_info(
"Xtal value :%04x Adc value :%04x", xtal, adc);
889 if (state->
config->chip_ver > 1)
890 ret = it913x_fe_script_loader(state, it9135_v2);
892 ret = it913x_fe_script_loader(state, it9135_v1);
919 ret = it913x_fe_script_loader(state, set_lna);
923 if (state->
config->chip_ver == 2) {
927 ret |= it913x_init_tuner(state);
933 set_mode = set_solo_fe;
934 ret |= it913x_fe_script_loader(state, set_mode);
936 ret |= it913x_fe_suspend(state);
937 return (ret < 0) ? -
ENODEV : 0;
945 ret = it913x_write_reg(state,
PRO_DMOD, 0xec40, 0x1);
947 ret |= it913x_fe_script_loader(state, init_1);
951 ret |= it913x_write_reg(state,
PRO_DMOD, 0xfba8, 0x0);
953 return (ret < 0) ? -
ENODEV : 0;
981 switch (state->
config->tuner_id_0) {
994 ret = it913x_fe_start(state);
1014 .name =
"it913x-fe DVB-T",
1015 .frequency_min = 51000000,
1016 .frequency_max = 1680000000,
1017 .frequency_stepsize = 62500,
1027 .release = it913x_fe_release,
1029 .init = it913x_fe_init,
1030 .sleep = it913x_fe_sleep,
1032 .set_frontend = it913x_fe_set_frontend,
1033 .get_frontend = it913x_fe_get_frontend,
1035 .read_status = it913x_fe_read_status,
1036 .read_signal_strength = it913x_fe_read_signal_strength,
1037 .read_snr = it913x_fe_read_snr,
1038 .read_ber = it913x_fe_read_ber,
1039 .read_ucblocks = it913x_fe_read_ucblocks,