25 #include <linux/module.h>
27 #include <linux/videodev2.h>
30 #include <linux/i2c.h>
32 #include <asm/unaligned.h>
44 static int no_poweroff;
47 "0 (default): use device-specific default mode).");
51 MODULE_PARM_DESC(audio_std,
"Audio standard. XC4000 audio decoder explicitly "
52 "needs to know what audio standard is needed for some video standards "
53 "with audio A2 or NICAM. The valid settings are a sum of:\n"
54 " 1: use NICAM/B or A2/B instead of NICAM/A or A2/A\n"
55 " 2: use A2 instead of NICAM or BTSC\n"
56 " 4: use SECAM/K3 instead of K1\n"
57 " 8: use PAL-D/K audio for SECAM-D/K\n"
58 "16: use FM radio input 1 instead of input 2\n"
59 "32: use mono audio (the lower three bits are ignored)");
61 static char firmware_name[30];
64 "default firmware name.");
67 static LIST_HEAD(hybrid_tuner_instance_list);
69 #define dprintk(level, fmt, arg...) if (debug >= level) \
70 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
111 #define XC4000_AUDIO_STD_B 1
112 #define XC4000_AUDIO_STD_A2 2
113 #define XC4000_AUDIO_STD_K3 4
114 #define XC4000_AUDIO_STD_L 8
115 #define XC4000_AUDIO_STD_INPUT1 16
116 #define XC4000_AUDIO_STD_MONO 32
118 #define XC4000_DEFAULT_FIRMWARE "dvb-fe-xc4000-1.4.fw"
121 #define MAX_TV_STANDARD 24
122 #define XC_MAX_I2C_WRITE_LENGTH 64
123 #define XC_POWERED_DOWN 0x80000000U
126 #define XC_RF_MODE_AIR 0
127 #define XC_RF_MODE_CABLE 1
130 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
131 #define XC_PRODUCT_ID_XC4000 0x0FA0
132 #define XC_PRODUCT_ID_XC4100 0x1004
135 #define XREG_INIT 0x00
136 #define XREG_VIDEO_MODE 0x01
137 #define XREG_AUDIO_MODE 0x02
138 #define XREG_RF_FREQ 0x03
139 #define XREG_D_CODE 0x04
140 #define XREG_DIRECTSITTING_MODE 0x05
141 #define XREG_SEEK_MODE 0x06
142 #define XREG_POWER_DOWN 0x08
143 #define XREG_SIGNALSOURCE 0x0A
144 #define XREG_SMOOTHEDCVBS 0x0E
145 #define XREG_AMPLITUDE 0x10
148 #define XREG_ADC_ENV 0x00
149 #define XREG_QUALITY 0x01
150 #define XREG_FRAME_LINES 0x02
151 #define XREG_HSYNC_FREQ 0x03
152 #define XREG_LOCK 0x04
153 #define XREG_FREQ_ERROR 0x05
154 #define XREG_SNR 0x06
155 #define XREG_VERSION 0x07
156 #define XREG_PRODUCT_ID 0x08
157 #define XREG_SIGNAL_LEVEL 0x0A
158 #define XREG_NOISE_LEVEL 0x0B
201 #define XC4000_MN_NTSC_PAL_BTSC 0
202 #define XC4000_MN_NTSC_PAL_A2 1
203 #define XC4000_MN_NTSC_PAL_EIAJ 2
204 #define XC4000_MN_NTSC_PAL_Mono 3
205 #define XC4000_BG_PAL_A2 4
206 #define XC4000_BG_PAL_NICAM 5
207 #define XC4000_BG_PAL_MONO 6
208 #define XC4000_I_PAL_NICAM 7
209 #define XC4000_I_PAL_NICAM_MONO 8
210 #define XC4000_DK_PAL_A2 9
211 #define XC4000_DK_PAL_NICAM 10
212 #define XC4000_DK_PAL_MONO 11
213 #define XC4000_DK_SECAM_A2DK1 12
214 #define XC4000_DK_SECAM_A2LDK3 13
215 #define XC4000_DK_SECAM_A2MONO 14
216 #define XC4000_DK_SECAM_NICAM 15
217 #define XC4000_L_SECAM_NICAM 16
218 #define XC4000_LC_SECAM_NICAM 17
219 #define XC4000_DTV6 18
220 #define XC4000_DTV8 19
221 #define XC4000_DTV7_8 20
222 #define XC4000_DTV7 21
223 #define XC4000_FM_Radio_INPUT2 22
224 #define XC4000_FM_Radio_INPUT1 23
227 {
"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
228 {
"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
229 {
"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
230 {
"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
231 {
"B/G-PAL-A2", 0x0000, 0x8159, 5640},
232 {
"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
233 {
"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
234 {
"I-PAL-NICAM", 0x0080, 0x8049, 6240},
235 {
"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
236 {
"D/K-PAL-A2", 0x0000, 0x8049, 6380},
237 {
"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
238 {
"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
239 {
"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
240 {
"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
241 {
"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
242 {
"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
243 {
"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
244 {
"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
245 {
"DTV6", 0x00C0, 0x8002, 0},
246 {
"DTV8", 0x00C0, 0x800B, 0},
247 {
"DTV7/8", 0x00C0, 0x801B, 0},
248 {
"DTV7", 0x00C0, 0x8007, 0},
249 {
"FM Radio-INPUT2", 0x0008, 0x9800, 10700},
250 {
"FM Radio-INPUT1", 0x0008, 0x9000, 10700}
260 .flags = 0, .buf =
buf, .len = len };
279 dprintk(1,
"%s()\n", __func__);
304 buf[0] = (regAddr >> 8) & 0xFF;
305 buf[1] = regAddr & 0xFF;
306 buf[2] = (i2cData >> 8) & 0xFF;
307 buf[3] = i2cData & 0xFF;
308 result = xc_send_i2c_data(priv, buf, 4);
313 static int xc_load_i2c_sequence(
struct dvb_frontend *fe,
const u8 *i2c_sequence)
322 while ((i2c_sequence[index] != 0xFF) ||
323 (i2c_sequence[index + 1] != 0xFF)) {
324 len = i2c_sequence[
index] * 256 + i2c_sequence[index+1];
330 }
else if (len & 0x8000) {
339 buf[0] = i2c_sequence[
index];
340 buf[1] = i2c_sequence[index + 1];
347 nbytes_to_send = (len - pos + 2);
348 for (i = 2; i < nbytes_to_send; i++) {
349 buf[
i] = i2c_sequence[index + pos +
352 result = xc_send_i2c_data(priv, buf,
358 pos += nbytes_to_send - 2;
366 static int xc_set_tv_standard(
struct xc4000_priv *priv,
370 dprintk(1,
"%s(0x%04x,0x%04x)\n", __func__, video_mode, audio_mode);
371 dprintk(1,
"%s() Standard = %s\n",
389 dprintk(1,
"%s(%d) Source = %s\n", __func__, rf_mode,
395 "%s(), Invalid mode, defaulting to CABLE",
403 static int xc_set_rf_frequency(
struct xc4000_priv *priv,
u32 freq_hz)
407 dprintk(1,
"%s(%u)\n", __func__, freq_hz);
409 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
410 (freq_hz < xc4000_tuner_ops.info.frequency_min))
413 freq_code = (
u16)(freq_hz / 15625);
422 static int xc_get_adc_envelope(
struct xc4000_priv *priv,
u16 *adc_envelope)
424 return xc4000_readreg(priv,
XREG_ADC_ENV, adc_envelope);
427 static int xc_get_frequency_error(
struct xc4000_priv *priv,
u32 *freq_error_hz)
437 tmp = (
u32)regData & 0xFFFFU;
438 tmp = (tmp < 0x8000
U ? tmp : 0x10000
U -
tmp);
439 (*freq_error_hz) = tmp * 15625;
443 static int xc_get_lock_status(
struct xc4000_priv *priv,
u16 *lock_status)
445 return xc4000_readreg(priv,
XREG_LOCK, lock_status);
448 static int xc_get_version(
struct xc4000_priv *priv,
449 u8 *hw_majorversion,
u8 *hw_minorversion,
450 u8 *fw_majorversion,
u8 *fw_minorversion)
459 (*hw_majorversion) = (data >> 12) & 0x0F;
460 (*hw_minorversion) = (data >> 8) & 0x0F;
461 (*fw_majorversion) = (data >> 4) & 0x0F;
462 (*fw_minorversion) = data & 0x0F;
467 static int xc_get_hsync_freq(
struct xc4000_priv *priv,
u32 *hsync_freq_hz)
476 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
480 static int xc_get_frame_lines(
struct xc4000_priv *priv,
u16 *frame_lines)
490 static int xc_get_signal_level(
struct xc4000_priv *priv,
u16 *signal)
495 static int xc_get_noise_level(
struct xc4000_priv *priv,
u16 *noise)
503 int watchdog_count = 40;
505 while ((lock_state == 0) && (watchdog_count > 0)) {
506 xc_get_lock_status(priv, &lock_state);
507 if (lock_state != 1) {
520 dprintk(1,
"%s(%u)\n", __func__, freq_hz);
524 result = xc_set_rf_frequency(priv, freq_hz);
532 if (xc_wait_for_lock(priv) != 1)
550 u8 buf[2] = { reg >> 8, reg & 0xff };
551 u8 bval[2] = { 0, 0 };
554 .flags = 0, .buf = &buf[0], .len = 2 },
556 .flags =
I2C_M_RD, .buf = &bval[0], .len = 2 },
564 *val = (bval[0] << 8) | bval[1];
568 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
569 static void dump_firm_type_and_int_freq(
unsigned int type,
u16 int_freq)
640 unsigned int best_nr_diffs = 255
U;
647 if (((type & ~
SCODE) == 0) && (*
id == 0))
653 (priv->
firm[
i].id ^ (*id)) & (*
id);
654 unsigned int type_diff_mask =
657 unsigned int nr_diffs;
667 if (nr_diffs < best_nr_diffs) {
668 best_nr_diffs = nr_diffs;
679 if (best_nr_diffs > 0
U) {
681 "Selecting best matching firmware (%u bits differ) for "
682 "type=(%x), id %016llx:\n",
683 best_nr_diffs, type, (
unsigned long long)*
id);
688 *
id = priv->
firm[
i].id;
693 (i < 0) ?
"Can't find" :
"Found");
700 static int load_firmware(
struct dvb_frontend *fe,
unsigned int type,
707 pos = seek_firmware(fe, type,
id);
716 rc = xc_load_i2c_sequence(fe, p);
727 const unsigned char *
p, *endp;
733 if (firmware_name[0] !=
'\0')
734 fname = firmware_name;
738 dprintk(1,
"Reading firmware %s\n", fname);
752 if (fw->
size <
sizeof(name) - 1 + 2 + 2) {
758 memcpy(name, p,
sizeof(name) - 1);
759 name[
sizeof(
name) - 1] =
'\0';
760 p +=
sizeof(
name) - 1;
765 n_array = get_unaligned_le16(p);
768 dprintk(1,
"Loading %d firmware images from %s, type: %s, ver %d.%d\n",
769 n_array, fname, name,
794 if (endp - p <
sizeof(type) +
sizeof(
id) +
sizeof(size))
804 int_freq = get_unaligned_le16(p);
805 p +=
sizeof(int_freq);
806 if (endp - p <
sizeof(size))
813 if (!size || size > endp - p) {
814 printk(
KERN_ERR "Firmware type (%x), id %llx is corrupted (size=%d, expected %d)\n",
815 type, (
unsigned long long)
id,
816 (
unsigned)(endp - p), size);
829 dump_firm_type_and_int_freq(type, int_freq);
831 type, (
unsigned long long)
id, size);
838 priv->
firm[
n].int_freq = int_freq;
859 dprintk(1,
"Firmware files loaded.\n");
864 static int load_scode(
struct dvb_frontend *fe,
unsigned int type,
873 dprintk(1,
"%s called int_freq=%d\n", __func__, int_freq);
876 pos = seek_firmware(fe, type,
id);
880 for (pos = 0; pos < priv->
firm_size; pos++) {
881 if ((priv->
firm[pos].int_freq == int_freq) &&
882 (priv->
firm[pos].type & HAS_IF))
891 if (priv->
firm[pos].size != 12 * 16 || scode >= 16)
897 dump_firm_type_and_int_freq(priv->
firm[pos].type,
898 priv->
firm[pos].int_freq);
900 (
unsigned long long)*
id);
904 memcpy(&scode_buf[1], p, 12);
913 rc = xc_send_i2c_data(priv, scode_buf, 13);
921 memset(indirect_mode, 0,
sizeof(indirect_mode));
922 indirect_mode[4] = 0x88;
923 xc_send_i2c_data(priv, indirect_mode,
sizeof(indirect_mode));
929 static int check_firmware(
struct dvb_frontend *fe,
unsigned int type,
934 int rc = 0, is_retry = 0;
937 u8 hw_major, hw_minor, fw_major, fw_minor;
939 dprintk(1,
"%s called\n", __func__);
942 rc = xc4000_fwupload(fe);
950 new_fw.std_req =
std;
951 new_fw.scode_table =
SCODE;
955 dprintk(1,
"checking firmware, user requested type=");
959 (
unsigned long long)new_fw.std_req);
969 dprintk(1,
"BASE firmware not changed.\n");
977 rc = xc4000_tuner_reset(fe);
983 rc = load_firmware(fe,
BASE, &std0);
990 dprintk(1,
"Load init1 firmware, if exists\n");
992 rc = load_firmware(fe,
BASE |
INIT1, &std0);
994 rc = load_firmware(fe,
BASE |
INIT1, &std0);
995 if (rc < 0 && rc != -
ENOENT) {
996 tuner_err(
"Error %d while loading init1 firmware\n",
1006 if (priv->
cur_fw.type == (
BASE | new_fw.type) &&
1007 priv->
cur_fw.std_req == std) {
1008 dprintk(1,
"Std-specific firmware already loaded.\n");
1009 goto skip_std_specific;
1013 priv->
cur_fw.scode_table = 0;
1016 rc = load_firmware(fe, new_fw.type, &new_fw.
id);
1022 if (priv->
cur_fw.scode_table == new_fw.scode_table &&
1023 priv->
cur_fw.scode_nr == new_fw.scode_nr) {
1024 dprintk(1,
"SCODE firmware already loaded.\n");
1029 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.
id,
1030 new_fw.int_freq, new_fw.scode_nr);
1032 dprintk(1,
"load scode failed %d\n", rc);
1037 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
1043 dprintk(1,
"Device is Xceive %d version %d.%d, "
1044 "firmware version %d.%d\n",
1045 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1048 if (priv->
firm_version != ((fw_major << 8) | fw_minor)) {
1050 "Incorrect readback of firmware version %d.%d.\n",
1051 fw_major, fw_minor);
1060 priv->
hwvers = (hw_major << 8) | hw_minor;
1062 priv->
hwvers != ((hw_major << 8) | hw_minor)) {
1064 "Read invalid device hardware information - tuner "
1086 dprintk(1,
"Retrying firmware load\n");
1095 static void xc_debug_dump(
struct xc4000_priv *priv)
1098 u32 freq_error_hz = 0;
1100 u32 hsync_freq_hz = 0;
1105 u8 hw_majorversion = 0, hw_minorversion = 0;
1106 u8 fw_majorversion = 0, fw_minorversion = 0;
1108 xc_get_adc_envelope(priv, &adc_envelope);
1109 dprintk(1,
"*** ADC envelope (0-1023) = %d\n", adc_envelope);
1111 xc_get_frequency_error(priv, &freq_error_hz);
1112 dprintk(1,
"*** Frequency error = %d Hz\n", freq_error_hz);
1114 xc_get_lock_status(priv, &lock_status);
1115 dprintk(1,
"*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1118 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1119 &fw_majorversion, &fw_minorversion);
1120 dprintk(1,
"*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1121 hw_majorversion, hw_minorversion,
1122 fw_majorversion, fw_minorversion);
1125 xc_get_hsync_freq(priv, &hsync_freq_hz);
1126 dprintk(1,
"*** Horizontal sync frequency = %d Hz\n",
1129 xc_get_frame_lines(priv, &frame_lines);
1130 dprintk(1,
"*** Frame lines = %d\n", frame_lines);
1133 xc_get_quality(priv, &quality);
1134 dprintk(1,
"*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1136 xc_get_signal_level(priv, &signal);
1137 dprintk(1,
"*** Signal level = -%ddB (%d)\n", signal >> 8, signal);
1139 xc_get_noise_level(priv, &noise);
1140 dprintk(1,
"*** Noise level = %ddB (%d)\n", noise >> 8, noise);
1158 dprintk(1,
"%s() VSB modulation\n", __func__);
1165 dprintk(1,
"%s() QAM modulation\n", __func__);
1173 dprintk(1,
"%s() OFDM\n", __func__);
1182 }
else if (bw <= 6000000) {
1186 }
else if (bw <= 7000000) {
1203 dprintk(1,
"%s() frequency=%d (compensated)\n",
1207 if (check_firmware(fe, type, 0, priv->
if_khz) != 0)
1212 ret = xc_set_signal_source(priv, priv->
rf_mode);
1222 video_mode |= 0x0001;
1223 ret = xc_set_tv_standard(priv, video_mode, audio_mode);
1249 xc_tune_channel(priv, priv->
freq_hz);
1259 static int xc4000_set_analog_params(
struct dvb_frontend *fe,
1263 unsigned int type = 0;
1267 dprintk(1,
"%s() frequency=%d (in units of 62.5Hz)\n",
1286 dprintk(1,
"%s() frequency=%d (in units of 62.5khz)\n",
1304 if (audio_std & XC4000_AUDIO_STD_MONO) {
1318 if (audio_std & XC4000_AUDIO_STD_MONO) {
1343 if (audio_std & XC4000_AUDIO_STD_MONO)
1352 if (audio_std & XC4000_AUDIO_STD_MONO) {
1354 }
else if (audio_std & XC4000_AUDIO_STD_A2) {
1370 }
else if (audio_std & XC4000_AUDIO_STD_MONO) {
1401 if (check_firmware(fe, type, params->
std,
1405 ret = xc_set_signal_source(priv, priv->
rf_mode);
1408 "xc4000: xc_set_signal_source(%d) failed\n",
1417 video_mode &= 0xFF7F;
1420 video_mode &= 0xFEFF;
1422 video_mode |= 0x0080;
1424 ret = xc_set_tv_standard(priv, video_mode, audio_mode);
1444 xc_tune_channel(priv, priv->
freq_hz);
1470 tuner_dbg(
"Signal strength: -%ddB (%05d)\n", value >> 8, value);
1488 tuner_dbg(
"Noise level: %ddB (%05d)\n", value >> 8, value);
1491 if (value >= 0x2000) {
1494 value = ~value << 3;
1504 if (value <= 0x3200) {
1507 }
else if (value >= 0x713A) {
1510 value = ~(value - 0x3200) << 2;
1530 if (xc4000_readreg(priv,
XREG_SNR, &snr) == 0) {
1532 dprintk(1,
"%s() freq = %u, SNR = %d\n",
1533 __func__, *freq, snr);
1540 dprintk(1,
"%s()\n", __func__);
1548 dprintk(1,
"%s()\n", __func__);
1557 u16 lock_status = 0;
1562 xc_get_lock_status(priv, &lock_status);
1564 *status = (lock_status == 1 ?
1571 dprintk(2,
"%s() lock_status = %d\n", __func__, lock_status);
1581 dprintk(1,
"%s()\n", __func__);
1586 if ((no_poweroff == 2 ||
1587 (no_poweroff == 0 && priv->
default_pm != 0)) &&
1594 "xc4000: %s() unable to shutdown tuner\n",
1608 dprintk(1,
"%s()\n", __func__);
1617 dprintk(1,
"%s()\n", __func__);
1633 .name =
"Xceive XC4000",
1634 .frequency_min = 1000000,
1635 .frequency_max = 1023000000,
1636 .frequency_step = 50000,
1639 .release = xc4000_release,
1640 .init = xc4000_init,
1641 .sleep = xc4000_sleep,
1643 .set_params = xc4000_set_params,
1644 .set_analog_params = xc4000_set_analog_params,
1645 .get_frequency = xc4000_get_frequency,
1646 .get_rf_strength = xc4000_get_signal,
1647 .get_bandwidth = xc4000_get_bandwidth,
1648 .get_status = xc4000_get_status
1659 dprintk(1,
"%s(%d-%04x)\n", __func__,
1660 i2c ? i2c_adapter_id(i2c) : -1,
1701 if (instance == 1) {
1713 "xc4000: Successfully identified at address 0x%02x\n",
1716 "xc4000: Firmware has been loaded previously\n");
1720 "xc4000: Successfully identified at address 0x%02x\n",
1723 "xc4000: Firmware has not been loaded previously\n");
1727 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1734 memcpy(&fe->
ops.tuner_ops, &xc4000_tuner_ops,
1737 if (instance == 1) {
1740 ret = xc4000_fwupload(fe);