0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/module.h>
0013 #include <linux/moduleparam.h>
0014 #include <linux/videodev2.h>
0015 #include <linux/delay.h>
0016 #include <linux/dvb/frontend.h>
0017 #include <linux/i2c.h>
0018 #include <linux/mutex.h>
0019 #include <asm/unaligned.h>
0020
0021 #include <media/dvb_frontend.h>
0022
0023 #include "xc4000.h"
0024 #include "tuner-i2c.h"
0025 #include "xc2028-types.h"
0026
0027 static int debug;
0028 module_param(debug, int, 0644);
0029 MODULE_PARM_DESC(debug, "Debugging level (0 to 2, default: 0 (off)).");
0030
0031 static int no_poweroff;
0032 module_param(no_poweroff, int, 0644);
0033 MODULE_PARM_DESC(no_poweroff, "Power management (1: disabled, 2: enabled, 0 (default): use device-specific default mode).");
0034
0035 static int audio_std;
0036 module_param(audio_std, int, 0644);
0037 MODULE_PARM_DESC(audio_std, "Audio standard. XC4000 audio decoder explicitly needs to know what audio standard is needed for some video standards with audio A2 or NICAM. The valid settings are a sum of:\n"
0038 " 1: use NICAM/B or A2/B instead of NICAM/A or A2/A\n"
0039 " 2: use A2 instead of NICAM or BTSC\n"
0040 " 4: use SECAM/K3 instead of K1\n"
0041 " 8: use PAL-D/K audio for SECAM-D/K\n"
0042 "16: use FM radio input 1 instead of input 2\n"
0043 "32: use mono audio (the lower three bits are ignored)");
0044
0045 static char firmware_name[30];
0046 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
0047 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the default firmware name.");
0048
0049 static DEFINE_MUTEX(xc4000_list_mutex);
0050 static LIST_HEAD(hybrid_tuner_instance_list);
0051
0052 #define dprintk(level, fmt, arg...) if (debug >= level) \
0053 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
0054
0055
0056 struct firmware_description {
0057 unsigned int type;
0058 v4l2_std_id id;
0059 __u16 int_freq;
0060 unsigned char *ptr;
0061 unsigned int size;
0062 };
0063
0064 struct firmware_properties {
0065 unsigned int type;
0066 v4l2_std_id id;
0067 v4l2_std_id std_req;
0068 __u16 int_freq;
0069 unsigned int scode_table;
0070 int scode_nr;
0071 };
0072
0073 struct xc4000_priv {
0074 struct tuner_i2c_props i2c_props;
0075 struct list_head hybrid_tuner_instance_list;
0076 struct firmware_description *firm;
0077 int firm_size;
0078 u32 if_khz;
0079 u32 freq_hz, freq_offset;
0080 u32 bandwidth;
0081 u8 video_standard;
0082 u8 rf_mode;
0083 u8 default_pm;
0084 u8 dvb_amplitude;
0085 u8 set_smoothedcvbs;
0086 u8 ignore_i2c_write_errors;
0087 __u16 firm_version;
0088 struct firmware_properties cur_fw;
0089 __u16 hwmodel;
0090 __u16 hwvers;
0091 struct mutex lock;
0092 };
0093
0094 #define XC4000_AUDIO_STD_B 1
0095 #define XC4000_AUDIO_STD_A2 2
0096 #define XC4000_AUDIO_STD_K3 4
0097 #define XC4000_AUDIO_STD_L 8
0098 #define XC4000_AUDIO_STD_INPUT1 16
0099 #define XC4000_AUDIO_STD_MONO 32
0100
0101 #define XC4000_DEFAULT_FIRMWARE "dvb-fe-xc4000-1.4.fw"
0102 #define XC4000_DEFAULT_FIRMWARE_NEW "dvb-fe-xc4000-1.4.1.fw"
0103
0104
0105 #define MAX_TV_STANDARD 24
0106 #define XC_MAX_I2C_WRITE_LENGTH 64
0107 #define XC_POWERED_DOWN 0x80000000U
0108
0109
0110 #define XC_RF_MODE_AIR 0
0111 #define XC_RF_MODE_CABLE 1
0112
0113
0114 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
0115 #define XC_PRODUCT_ID_XC4000 0x0FA0
0116 #define XC_PRODUCT_ID_XC4100 0x1004
0117
0118
0119 #define XREG_INIT 0x00
0120 #define XREG_VIDEO_MODE 0x01
0121 #define XREG_AUDIO_MODE 0x02
0122 #define XREG_RF_FREQ 0x03
0123 #define XREG_D_CODE 0x04
0124 #define XREG_DIRECTSITTING_MODE 0x05
0125 #define XREG_SEEK_MODE 0x06
0126 #define XREG_POWER_DOWN 0x08
0127 #define XREG_SIGNALSOURCE 0x0A
0128 #define XREG_SMOOTHEDCVBS 0x0E
0129 #define XREG_AMPLITUDE 0x10
0130
0131
0132 #define XREG_ADC_ENV 0x00
0133 #define XREG_QUALITY 0x01
0134 #define XREG_FRAME_LINES 0x02
0135 #define XREG_HSYNC_FREQ 0x03
0136 #define XREG_LOCK 0x04
0137 #define XREG_FREQ_ERROR 0x05
0138 #define XREG_SNR 0x06
0139 #define XREG_VERSION 0x07
0140 #define XREG_PRODUCT_ID 0x08
0141 #define XREG_SIGNAL_LEVEL 0x0A
0142 #define XREG_NOISE_LEVEL 0x0B
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 struct XC_TV_STANDARD {
0178 const char *Name;
0179 u16 audio_mode;
0180 u16 video_mode;
0181 u16 int_freq;
0182 };
0183
0184
0185 #define XC4000_MN_NTSC_PAL_BTSC 0
0186 #define XC4000_MN_NTSC_PAL_A2 1
0187 #define XC4000_MN_NTSC_PAL_EIAJ 2
0188 #define XC4000_MN_NTSC_PAL_Mono 3
0189 #define XC4000_BG_PAL_A2 4
0190 #define XC4000_BG_PAL_NICAM 5
0191 #define XC4000_BG_PAL_MONO 6
0192 #define XC4000_I_PAL_NICAM 7
0193 #define XC4000_I_PAL_NICAM_MONO 8
0194 #define XC4000_DK_PAL_A2 9
0195 #define XC4000_DK_PAL_NICAM 10
0196 #define XC4000_DK_PAL_MONO 11
0197 #define XC4000_DK_SECAM_A2DK1 12
0198 #define XC4000_DK_SECAM_A2LDK3 13
0199 #define XC4000_DK_SECAM_A2MONO 14
0200 #define XC4000_DK_SECAM_NICAM 15
0201 #define XC4000_L_SECAM_NICAM 16
0202 #define XC4000_LC_SECAM_NICAM 17
0203 #define XC4000_DTV6 18
0204 #define XC4000_DTV8 19
0205 #define XC4000_DTV7_8 20
0206 #define XC4000_DTV7 21
0207 #define XC4000_FM_Radio_INPUT2 22
0208 #define XC4000_FM_Radio_INPUT1 23
0209
0210 static struct XC_TV_STANDARD xc4000_standard[MAX_TV_STANDARD] = {
0211 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
0212 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
0213 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
0214 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
0215 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
0216 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
0217 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
0218 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
0219 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
0220 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
0221 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
0222 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
0223 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
0224 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
0225 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
0226 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
0227 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
0228 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
0229 {"DTV6", 0x00C0, 0x8002, 0},
0230 {"DTV8", 0x00C0, 0x800B, 0},
0231 {"DTV7/8", 0x00C0, 0x801B, 0},
0232 {"DTV7", 0x00C0, 0x8007, 0},
0233 {"FM Radio-INPUT2", 0x0008, 0x9800, 10700},
0234 {"FM Radio-INPUT1", 0x0008, 0x9000, 10700}
0235 };
0236
0237 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
0238 static int xc4000_tuner_reset(struct dvb_frontend *fe);
0239 static void xc_debug_dump(struct xc4000_priv *priv);
0240
0241 static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
0242 {
0243 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
0244 .flags = 0, .buf = buf, .len = len };
0245 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
0246 if (priv->ignore_i2c_write_errors == 0) {
0247 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
0248 len);
0249 if (len == 4) {
0250 printk(KERN_ERR "bytes %*ph\n", 4, buf);
0251 }
0252 return -EREMOTEIO;
0253 }
0254 }
0255 return 0;
0256 }
0257
0258 static int xc4000_tuner_reset(struct dvb_frontend *fe)
0259 {
0260 struct xc4000_priv *priv = fe->tuner_priv;
0261 int ret;
0262
0263 dprintk(1, "%s()\n", __func__);
0264
0265 if (fe->callback) {
0266 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
0267 fe->dvb->priv :
0268 priv->i2c_props.adap->algo_data,
0269 DVB_FRONTEND_COMPONENT_TUNER,
0270 XC4000_TUNER_RESET, 0);
0271 if (ret) {
0272 printk(KERN_ERR "xc4000: reset failed\n");
0273 return -EREMOTEIO;
0274 }
0275 } else {
0276 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
0277 return -EINVAL;
0278 }
0279 return 0;
0280 }
0281
0282 static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
0283 {
0284 u8 buf[4];
0285 int result;
0286
0287 buf[0] = (regAddr >> 8) & 0xFF;
0288 buf[1] = regAddr & 0xFF;
0289 buf[2] = (i2cData >> 8) & 0xFF;
0290 buf[3] = i2cData & 0xFF;
0291 result = xc_send_i2c_data(priv, buf, 4);
0292
0293 return result;
0294 }
0295
0296 static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
0297 {
0298 struct xc4000_priv *priv = fe->tuner_priv;
0299
0300 int i, nbytes_to_send, result;
0301 unsigned int len, pos, index;
0302 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
0303
0304 index = 0;
0305 while ((i2c_sequence[index] != 0xFF) ||
0306 (i2c_sequence[index + 1] != 0xFF)) {
0307 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
0308 if (len == 0x0000) {
0309
0310
0311
0312 index += 2;
0313 } else if (len & 0x8000) {
0314
0315 msleep(len & 0x7FFF);
0316 index += 2;
0317 } else {
0318
0319
0320
0321 index += 2;
0322 buf[0] = i2c_sequence[index];
0323 buf[1] = i2c_sequence[index + 1];
0324 pos = 2;
0325 while (pos < len) {
0326 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
0327 nbytes_to_send =
0328 XC_MAX_I2C_WRITE_LENGTH;
0329 else
0330 nbytes_to_send = (len - pos + 2);
0331 for (i = 2; i < nbytes_to_send; i++) {
0332 buf[i] = i2c_sequence[index + pos +
0333 i - 2];
0334 }
0335 result = xc_send_i2c_data(priv, buf,
0336 nbytes_to_send);
0337
0338 if (result != 0)
0339 return result;
0340
0341 pos += nbytes_to_send - 2;
0342 }
0343 index += len;
0344 }
0345 }
0346 return 0;
0347 }
0348
0349 static int xc_set_tv_standard(struct xc4000_priv *priv,
0350 u16 video_mode, u16 audio_mode)
0351 {
0352 int ret;
0353 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, video_mode, audio_mode);
0354 dprintk(1, "%s() Standard = %s\n",
0355 __func__,
0356 xc4000_standard[priv->video_standard].Name);
0357
0358
0359 priv->ignore_i2c_write_errors = 1;
0360
0361 ret = xc_write_reg(priv, XREG_VIDEO_MODE, video_mode);
0362 if (ret == 0)
0363 ret = xc_write_reg(priv, XREG_AUDIO_MODE, audio_mode);
0364
0365 priv->ignore_i2c_write_errors = 0;
0366
0367 return ret;
0368 }
0369
0370 static int xc_set_signal_source(struct xc4000_priv *priv, u16 rf_mode)
0371 {
0372 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
0373 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
0374
0375 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
0376 rf_mode = XC_RF_MODE_CABLE;
0377 printk(KERN_ERR
0378 "%s(), Invalid mode, defaulting to CABLE",
0379 __func__);
0380 }
0381 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
0382 }
0383
0384 static const struct dvb_tuner_ops xc4000_tuner_ops;
0385
0386 static int xc_set_rf_frequency(struct xc4000_priv *priv, u32 freq_hz)
0387 {
0388 u16 freq_code;
0389
0390 dprintk(1, "%s(%u)\n", __func__, freq_hz);
0391
0392 if ((freq_hz > xc4000_tuner_ops.info.frequency_max_hz) ||
0393 (freq_hz < xc4000_tuner_ops.info.frequency_min_hz))
0394 return -EINVAL;
0395
0396 freq_code = (u16)(freq_hz / 15625);
0397
0398
0399
0400
0401
0402 return xc_write_reg(priv, XREG_RF_FREQ, freq_code);
0403 }
0404
0405 static int xc_get_adc_envelope(struct xc4000_priv *priv, u16 *adc_envelope)
0406 {
0407 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
0408 }
0409
0410 static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
0411 {
0412 int result;
0413 u16 regData;
0414 u32 tmp;
0415
0416 result = xc4000_readreg(priv, XREG_FREQ_ERROR, ®Data);
0417 if (result != 0)
0418 return result;
0419
0420 tmp = (u32)regData & 0xFFFFU;
0421 tmp = (tmp < 0x8000U ? tmp : 0x10000U - tmp);
0422 (*freq_error_hz) = tmp * 15625;
0423 return result;
0424 }
0425
0426 static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
0427 {
0428 return xc4000_readreg(priv, XREG_LOCK, lock_status);
0429 }
0430
0431 static int xc_get_version(struct xc4000_priv *priv,
0432 u8 *hw_majorversion, u8 *hw_minorversion,
0433 u8 *fw_majorversion, u8 *fw_minorversion)
0434 {
0435 u16 data;
0436 int result;
0437
0438 result = xc4000_readreg(priv, XREG_VERSION, &data);
0439 if (result != 0)
0440 return result;
0441
0442 (*hw_majorversion) = (data >> 12) & 0x0F;
0443 (*hw_minorversion) = (data >> 8) & 0x0F;
0444 (*fw_majorversion) = (data >> 4) & 0x0F;
0445 (*fw_minorversion) = data & 0x0F;
0446
0447 return 0;
0448 }
0449
0450 static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
0451 {
0452 u16 regData;
0453 int result;
0454
0455 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, ®Data);
0456 if (result != 0)
0457 return result;
0458
0459 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
0460 return result;
0461 }
0462
0463 static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
0464 {
0465 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
0466 }
0467
0468 static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
0469 {
0470 return xc4000_readreg(priv, XREG_QUALITY, quality);
0471 }
0472
0473 static int xc_get_signal_level(struct xc4000_priv *priv, u16 *signal)
0474 {
0475 return xc4000_readreg(priv, XREG_SIGNAL_LEVEL, signal);
0476 }
0477
0478 static int xc_get_noise_level(struct xc4000_priv *priv, u16 *noise)
0479 {
0480 return xc4000_readreg(priv, XREG_NOISE_LEVEL, noise);
0481 }
0482
0483 static u16 xc_wait_for_lock(struct xc4000_priv *priv)
0484 {
0485 u16 lock_state = 0;
0486 int watchdog_count = 40;
0487
0488 while ((lock_state == 0) && (watchdog_count > 0)) {
0489 xc_get_lock_status(priv, &lock_state);
0490 if (lock_state != 1) {
0491 msleep(5);
0492 watchdog_count--;
0493 }
0494 }
0495 return lock_state;
0496 }
0497
0498 static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz)
0499 {
0500 int found = 1;
0501 int result;
0502
0503 dprintk(1, "%s(%u)\n", __func__, freq_hz);
0504
0505
0506 priv->ignore_i2c_write_errors = 1;
0507 result = xc_set_rf_frequency(priv, freq_hz);
0508 priv->ignore_i2c_write_errors = 0;
0509
0510 if (result != 0)
0511 return 0;
0512
0513
0514 if ((priv->cur_fw.type & (FM | DTV6 | DTV7 | DTV78 | DTV8)) == 0) {
0515 if (xc_wait_for_lock(priv) != 1)
0516 found = 0;
0517 }
0518
0519
0520
0521
0522
0523 msleep(debug ? 100 : 10);
0524
0525 if (debug)
0526 xc_debug_dump(priv);
0527
0528 return found;
0529 }
0530
0531 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
0532 {
0533 u8 buf[2] = { reg >> 8, reg & 0xff };
0534 u8 bval[2] = { 0, 0 };
0535 struct i2c_msg msg[2] = {
0536 { .addr = priv->i2c_props.addr,
0537 .flags = 0, .buf = &buf[0], .len = 2 },
0538 { .addr = priv->i2c_props.addr,
0539 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
0540 };
0541
0542 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
0543 printk(KERN_ERR "xc4000: I2C read failed\n");
0544 return -EREMOTEIO;
0545 }
0546
0547 *val = (bval[0] << 8) | bval[1];
0548 return 0;
0549 }
0550
0551 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
0552 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
0553 {
0554 if (type & BASE)
0555 printk(KERN_CONT "BASE ");
0556 if (type & INIT1)
0557 printk(KERN_CONT "INIT1 ");
0558 if (type & F8MHZ)
0559 printk(KERN_CONT "F8MHZ ");
0560 if (type & MTS)
0561 printk(KERN_CONT "MTS ");
0562 if (type & D2620)
0563 printk(KERN_CONT "D2620 ");
0564 if (type & D2633)
0565 printk(KERN_CONT "D2633 ");
0566 if (type & DTV6)
0567 printk(KERN_CONT "DTV6 ");
0568 if (type & QAM)
0569 printk(KERN_CONT "QAM ");
0570 if (type & DTV7)
0571 printk(KERN_CONT "DTV7 ");
0572 if (type & DTV78)
0573 printk(KERN_CONT "DTV78 ");
0574 if (type & DTV8)
0575 printk(KERN_CONT "DTV8 ");
0576 if (type & FM)
0577 printk(KERN_CONT "FM ");
0578 if (type & INPUT1)
0579 printk(KERN_CONT "INPUT1 ");
0580 if (type & LCD)
0581 printk(KERN_CONT "LCD ");
0582 if (type & NOGD)
0583 printk(KERN_CONT "NOGD ");
0584 if (type & MONO)
0585 printk(KERN_CONT "MONO ");
0586 if (type & ATSC)
0587 printk(KERN_CONT "ATSC ");
0588 if (type & IF)
0589 printk(KERN_CONT "IF ");
0590 if (type & LG60)
0591 printk(KERN_CONT "LG60 ");
0592 if (type & ATI638)
0593 printk(KERN_CONT "ATI638 ");
0594 if (type & OREN538)
0595 printk(KERN_CONT "OREN538 ");
0596 if (type & OREN36)
0597 printk(KERN_CONT "OREN36 ");
0598 if (type & TOYOTA388)
0599 printk(KERN_CONT "TOYOTA388 ");
0600 if (type & TOYOTA794)
0601 printk(KERN_CONT "TOYOTA794 ");
0602 if (type & DIBCOM52)
0603 printk(KERN_CONT "DIBCOM52 ");
0604 if (type & ZARLINK456)
0605 printk(KERN_CONT "ZARLINK456 ");
0606 if (type & CHINA)
0607 printk(KERN_CONT "CHINA ");
0608 if (type & F6MHZ)
0609 printk(KERN_CONT "F6MHZ ");
0610 if (type & INPUT2)
0611 printk(KERN_CONT "INPUT2 ");
0612 if (type & SCODE)
0613 printk(KERN_CONT "SCODE ");
0614 if (type & HAS_IF)
0615 printk(KERN_CONT "HAS_IF_%d ", int_freq);
0616 }
0617
0618 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
0619 v4l2_std_id *id)
0620 {
0621 struct xc4000_priv *priv = fe->tuner_priv;
0622 int i, best_i = -1;
0623 unsigned int best_nr_diffs = 255U;
0624
0625 if (!priv->firm) {
0626 printk(KERN_ERR "Error! firmware not loaded\n");
0627 return -EINVAL;
0628 }
0629
0630 if (((type & ~SCODE) == 0) && (*id == 0))
0631 *id = V4L2_STD_PAL;
0632
0633
0634 for (i = 0; i < priv->firm_size; i++) {
0635 v4l2_std_id id_diff_mask =
0636 (priv->firm[i].id ^ (*id)) & (*id);
0637 unsigned int type_diff_mask =
0638 (priv->firm[i].type ^ type)
0639 & (BASE_TYPES | DTV_TYPES | LCD | NOGD | MONO | SCODE);
0640 unsigned int nr_diffs;
0641
0642 if (type_diff_mask
0643 & (BASE | INIT1 | FM | DTV6 | DTV7 | DTV78 | DTV8 | SCODE))
0644 continue;
0645
0646 nr_diffs = hweight64(id_diff_mask) + hweight32(type_diff_mask);
0647 if (!nr_diffs)
0648 goto found;
0649
0650 if (nr_diffs < best_nr_diffs) {
0651 best_nr_diffs = nr_diffs;
0652 best_i = i;
0653 }
0654 }
0655
0656
0657 if (best_i < 0) {
0658 i = -ENOENT;
0659 goto ret;
0660 }
0661
0662 if (best_nr_diffs > 0U) {
0663 printk(KERN_WARNING
0664 "Selecting best matching firmware (%u bits differ) for type=(%x), id %016llx:\n",
0665 best_nr_diffs, type, (unsigned long long)*id);
0666 i = best_i;
0667 }
0668
0669 found:
0670 *id = priv->firm[i].id;
0671
0672 ret:
0673 if (debug) {
0674 printk(KERN_DEBUG "%s firmware for type=",
0675 (i < 0) ? "Can't find" : "Found");
0676 dump_firm_type(type);
0677 printk(KERN_DEBUG "(%x), id %016llx.\n", type, (unsigned long long)*id);
0678 }
0679 return i;
0680 }
0681
0682 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
0683 v4l2_std_id *id)
0684 {
0685 struct xc4000_priv *priv = fe->tuner_priv;
0686 int pos, rc;
0687 unsigned char *p;
0688
0689 pos = seek_firmware(fe, type, id);
0690 if (pos < 0)
0691 return pos;
0692
0693 p = priv->firm[pos].ptr;
0694
0695
0696 priv->ignore_i2c_write_errors = 1;
0697
0698 rc = xc_load_i2c_sequence(fe, p);
0699
0700 priv->ignore_i2c_write_errors = 0;
0701
0702 return rc;
0703 }
0704
0705 static int xc4000_fwupload(struct dvb_frontend *fe)
0706 {
0707 struct xc4000_priv *priv = fe->tuner_priv;
0708 const struct firmware *fw = NULL;
0709 const unsigned char *p, *endp;
0710 int rc = 0;
0711 int n, n_array;
0712 char name[33];
0713 const char *fname;
0714
0715 if (firmware_name[0] != '\0') {
0716 fname = firmware_name;
0717
0718 dprintk(1, "Reading custom firmware %s\n", fname);
0719 rc = request_firmware(&fw, fname,
0720 priv->i2c_props.adap->dev.parent);
0721 } else {
0722 fname = XC4000_DEFAULT_FIRMWARE_NEW;
0723 dprintk(1, "Trying to read firmware %s\n", fname);
0724 rc = request_firmware(&fw, fname,
0725 priv->i2c_props.adap->dev.parent);
0726 if (rc == -ENOENT) {
0727 fname = XC4000_DEFAULT_FIRMWARE;
0728 dprintk(1, "Trying to read firmware %s\n", fname);
0729 rc = request_firmware(&fw, fname,
0730 priv->i2c_props.adap->dev.parent);
0731 }
0732 }
0733
0734 if (rc < 0) {
0735 if (rc == -ENOENT)
0736 printk(KERN_ERR "Error: firmware %s not found.\n", fname);
0737 else
0738 printk(KERN_ERR "Error %d while requesting firmware %s\n",
0739 rc, fname);
0740
0741 return rc;
0742 }
0743 dprintk(1, "Loading Firmware: %s\n", fname);
0744
0745 p = fw->data;
0746 endp = p + fw->size;
0747
0748 if (fw->size < sizeof(name) - 1 + 2 + 2) {
0749 printk(KERN_ERR "Error: firmware file %s has invalid size!\n",
0750 fname);
0751 goto corrupt;
0752 }
0753
0754 memcpy(name, p, sizeof(name) - 1);
0755 name[sizeof(name) - 1] = '\0';
0756 p += sizeof(name) - 1;
0757
0758 priv->firm_version = get_unaligned_le16(p);
0759 p += 2;
0760
0761 n_array = get_unaligned_le16(p);
0762 p += 2;
0763
0764 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
0765 n_array, fname, name,
0766 priv->firm_version >> 8, priv->firm_version & 0xff);
0767
0768 priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
0769 if (priv->firm == NULL) {
0770 printk(KERN_ERR "Not enough memory to load firmware file.\n");
0771 rc = -ENOMEM;
0772 goto done;
0773 }
0774 priv->firm_size = n_array;
0775
0776 n = -1;
0777 while (p < endp) {
0778 __u32 type, size;
0779 v4l2_std_id id;
0780 __u16 int_freq = 0;
0781
0782 n++;
0783 if (n >= n_array) {
0784 printk(KERN_ERR "More firmware images in file than were expected!\n");
0785 goto corrupt;
0786 }
0787
0788
0789 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
0790 goto header;
0791
0792 type = get_unaligned_le32(p);
0793 p += sizeof(type);
0794
0795 id = get_unaligned_le64(p);
0796 p += sizeof(id);
0797
0798 if (type & HAS_IF) {
0799 int_freq = get_unaligned_le16(p);
0800 p += sizeof(int_freq);
0801 if (endp - p < sizeof(size))
0802 goto header;
0803 }
0804
0805 size = get_unaligned_le32(p);
0806 p += sizeof(size);
0807
0808 if (!size || size > endp - p) {
0809 printk(KERN_ERR "Firmware type (%x), id %llx is corrupted (size=%zd, expected %d)\n",
0810 type, (unsigned long long)id,
0811 endp - p, size);
0812 goto corrupt;
0813 }
0814
0815 priv->firm[n].ptr = kmemdup(p, size, GFP_KERNEL);
0816 if (priv->firm[n].ptr == NULL) {
0817 printk(KERN_ERR "Not enough memory to load firmware file.\n");
0818 rc = -ENOMEM;
0819 goto done;
0820 }
0821
0822 if (debug) {
0823 printk(KERN_DEBUG "Reading firmware type ");
0824 dump_firm_type_and_int_freq(type, int_freq);
0825 printk(KERN_DEBUG "(%x), id %llx, size=%d.\n",
0826 type, (unsigned long long)id, size);
0827 }
0828
0829 priv->firm[n].type = type;
0830 priv->firm[n].id = id;
0831 priv->firm[n].size = size;
0832 priv->firm[n].int_freq = int_freq;
0833
0834 p += size;
0835 }
0836
0837 if (n + 1 != priv->firm_size) {
0838 printk(KERN_ERR "Firmware file is incomplete!\n");
0839 goto corrupt;
0840 }
0841
0842 goto done;
0843
0844 header:
0845 printk(KERN_ERR "Firmware header is incomplete!\n");
0846 corrupt:
0847 rc = -EINVAL;
0848 printk(KERN_ERR "Error: firmware file is corrupted!\n");
0849
0850 done:
0851 release_firmware(fw);
0852 if (rc == 0)
0853 dprintk(1, "Firmware files loaded.\n");
0854
0855 return rc;
0856 }
0857
0858 static int load_scode(struct dvb_frontend *fe, unsigned int type,
0859 v4l2_std_id *id, __u16 int_freq, int scode)
0860 {
0861 struct xc4000_priv *priv = fe->tuner_priv;
0862 int pos, rc;
0863 unsigned char *p;
0864 u8 scode_buf[13];
0865 u8 indirect_mode[5];
0866
0867 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
0868
0869 if (!int_freq) {
0870 pos = seek_firmware(fe, type, id);
0871 if (pos < 0)
0872 return pos;
0873 } else {
0874 for (pos = 0; pos < priv->firm_size; pos++) {
0875 if ((priv->firm[pos].int_freq == int_freq) &&
0876 (priv->firm[pos].type & HAS_IF))
0877 break;
0878 }
0879 if (pos == priv->firm_size)
0880 return -ENOENT;
0881 }
0882
0883 p = priv->firm[pos].ptr;
0884
0885 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
0886 return -EINVAL;
0887 p += 12 * scode;
0888
0889 if (debug) {
0890 tuner_info("Loading SCODE for type=");
0891 dump_firm_type_and_int_freq(priv->firm[pos].type,
0892 priv->firm[pos].int_freq);
0893 printk(KERN_CONT "(%x), id %016llx.\n", priv->firm[pos].type,
0894 (unsigned long long)*id);
0895 }
0896
0897 scode_buf[0] = 0x00;
0898 memcpy(&scode_buf[1], p, 12);
0899
0900
0901 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
0902 if (rc < 0) {
0903 printk(KERN_ERR "failed to put device into direct mode!\n");
0904 return -EIO;
0905 }
0906
0907 rc = xc_send_i2c_data(priv, scode_buf, 13);
0908 if (rc != 0) {
0909
0910
0911 printk(KERN_ERR "Failed to set scode %d\n", rc);
0912 }
0913
0914
0915 memset(indirect_mode, 0, sizeof(indirect_mode));
0916 indirect_mode[4] = 0x88;
0917 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
0918 msleep(10);
0919
0920 return 0;
0921 }
0922
0923 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
0924 v4l2_std_id std, __u16 int_freq)
0925 {
0926 struct xc4000_priv *priv = fe->tuner_priv;
0927 struct firmware_properties new_fw;
0928 int rc = 0, is_retry = 0;
0929 u16 hwmodel;
0930 v4l2_std_id std0;
0931 u8 hw_major = 0, hw_minor = 0, fw_major = 0, fw_minor = 0;
0932
0933 dprintk(1, "%s called\n", __func__);
0934
0935 if (!priv->firm) {
0936 rc = xc4000_fwupload(fe);
0937 if (rc < 0)
0938 return rc;
0939 }
0940
0941 retry:
0942 new_fw.type = type;
0943 new_fw.id = std;
0944 new_fw.std_req = std;
0945 new_fw.scode_table = SCODE;
0946 new_fw.scode_nr = 0;
0947 new_fw.int_freq = int_freq;
0948
0949 dprintk(1, "checking firmware, user requested type=");
0950 if (debug) {
0951 dump_firm_type(new_fw.type);
0952 printk(KERN_CONT "(%x), id %016llx, ", new_fw.type,
0953 (unsigned long long)new_fw.std_req);
0954 if (!int_freq)
0955 printk(KERN_CONT "scode_tbl ");
0956 else
0957 printk(KERN_CONT "int_freq %d, ", new_fw.int_freq);
0958 printk(KERN_CONT "scode_nr %d\n", new_fw.scode_nr);
0959 }
0960
0961
0962 if (priv->cur_fw.type & BASE) {
0963 dprintk(1, "BASE firmware not changed.\n");
0964 goto skip_base;
0965 }
0966
0967
0968 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
0969
0970
0971 rc = xc4000_tuner_reset(fe);
0972 if (rc < 0)
0973 goto fail;
0974
0975
0976 std0 = 0;
0977 rc = load_firmware(fe, BASE, &std0);
0978 if (rc < 0) {
0979 printk(KERN_ERR "Error %d while loading base firmware\n", rc);
0980 goto fail;
0981 }
0982
0983
0984 dprintk(1, "Load init1 firmware, if exists\n");
0985
0986 rc = load_firmware(fe, BASE | INIT1, &std0);
0987 if (rc == -ENOENT)
0988 rc = load_firmware(fe, BASE | INIT1, &std0);
0989 if (rc < 0 && rc != -ENOENT) {
0990 tuner_err("Error %d while loading init1 firmware\n",
0991 rc);
0992 goto fail;
0993 }
0994
0995 skip_base:
0996
0997
0998
0999
1000 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1001 priv->cur_fw.std_req == std) {
1002 dprintk(1, "Std-specific firmware already loaded.\n");
1003 goto skip_std_specific;
1004 }
1005
1006
1007 priv->cur_fw.scode_table = 0;
1008
1009
1010 rc = load_firmware(fe, new_fw.type, &new_fw.id);
1011
1012 if (rc < 0)
1013 goto fail;
1014
1015 skip_std_specific:
1016 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1017 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1018 dprintk(1, "SCODE firmware already loaded.\n");
1019 goto check_device;
1020 }
1021
1022
1023 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1024 new_fw.int_freq, new_fw.scode_nr);
1025 if (rc != 0)
1026 dprintk(1, "load scode failed %d\n", rc);
1027
1028 check_device:
1029 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel) < 0) {
1030 printk(KERN_ERR "Unable to read tuner registers.\n");
1031 goto fail;
1032 }
1033
1034 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
1035 &fw_minor) != 0) {
1036 printk(KERN_ERR "Unable to read tuner registers.\n");
1037 goto fail;
1038 }
1039
1040 dprintk(1, "Device is Xceive %d version %d.%d, firmware version %d.%d\n",
1041 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1042
1043
1044 if (priv->firm_version != ((fw_major << 8) | fw_minor)) {
1045 printk(KERN_WARNING
1046 "Incorrect readback of firmware version %d.%d.\n",
1047 fw_major, fw_minor);
1048 goto fail;
1049 }
1050
1051
1052 if (priv->hwmodel == 0 &&
1053 (hwmodel == XC_PRODUCT_ID_XC4000 ||
1054 hwmodel == XC_PRODUCT_ID_XC4100)) {
1055 priv->hwmodel = hwmodel;
1056 priv->hwvers = (hw_major << 8) | hw_minor;
1057 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1058 priv->hwvers != ((hw_major << 8) | hw_minor)) {
1059 printk(KERN_WARNING
1060 "Read invalid device hardware information - tuner hung?\n");
1061 goto fail;
1062 }
1063
1064 priv->cur_fw = new_fw;
1065
1066
1067
1068
1069
1070
1071
1072 priv->cur_fw.type |= BASE;
1073
1074 return 0;
1075
1076 fail:
1077 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1078 if (!is_retry) {
1079 msleep(50);
1080 is_retry = 1;
1081 dprintk(1, "Retrying firmware load\n");
1082 goto retry;
1083 }
1084
1085 if (rc == -ENOENT)
1086 rc = -EINVAL;
1087 return rc;
1088 }
1089
1090 static void xc_debug_dump(struct xc4000_priv *priv)
1091 {
1092 u16 adc_envelope;
1093 u32 freq_error_hz = 0;
1094 u16 lock_status;
1095 u32 hsync_freq_hz = 0;
1096 u16 frame_lines;
1097 u16 quality;
1098 u16 signal = 0;
1099 u16 noise = 0;
1100 u8 hw_majorversion = 0, hw_minorversion = 0;
1101 u8 fw_majorversion = 0, fw_minorversion = 0;
1102
1103 xc_get_adc_envelope(priv, &adc_envelope);
1104 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1105
1106 xc_get_frequency_error(priv, &freq_error_hz);
1107 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1108
1109 xc_get_lock_status(priv, &lock_status);
1110 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1111 lock_status);
1112
1113 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1114 &fw_majorversion, &fw_minorversion);
1115 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1116 hw_majorversion, hw_minorversion,
1117 fw_majorversion, fw_minorversion);
1118
1119 if (priv->video_standard < XC4000_DTV6) {
1120 xc_get_hsync_freq(priv, &hsync_freq_hz);
1121 dprintk(1, "*** Horizontal sync frequency = %d Hz\n",
1122 hsync_freq_hz);
1123
1124 xc_get_frame_lines(priv, &frame_lines);
1125 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1126 }
1127
1128 xc_get_quality(priv, &quality);
1129 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1130
1131 xc_get_signal_level(priv, &signal);
1132 dprintk(1, "*** Signal level = -%ddB (%d)\n", signal >> 8, signal);
1133
1134 xc_get_noise_level(priv, &noise);
1135 dprintk(1, "*** Noise level = %ddB (%d)\n", noise >> 8, noise);
1136 }
1137
1138 static int xc4000_set_params(struct dvb_frontend *fe)
1139 {
1140 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1141 u32 delsys = c->delivery_system;
1142 u32 bw = c->bandwidth_hz;
1143 struct xc4000_priv *priv = fe->tuner_priv;
1144 unsigned int type;
1145 int ret = -EREMOTEIO;
1146
1147 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, c->frequency);
1148
1149 mutex_lock(&priv->lock);
1150
1151 switch (delsys) {
1152 case SYS_ATSC:
1153 dprintk(1, "%s() VSB modulation\n", __func__);
1154 priv->rf_mode = XC_RF_MODE_AIR;
1155 priv->freq_offset = 1750000;
1156 priv->video_standard = XC4000_DTV6;
1157 type = DTV6;
1158 break;
1159 case SYS_DVBC_ANNEX_B:
1160 dprintk(1, "%s() QAM modulation\n", __func__);
1161 priv->rf_mode = XC_RF_MODE_CABLE;
1162 priv->freq_offset = 1750000;
1163 priv->video_standard = XC4000_DTV6;
1164 type = DTV6;
1165 break;
1166 case SYS_DVBT:
1167 case SYS_DVBT2:
1168 dprintk(1, "%s() OFDM\n", __func__);
1169 if (bw == 0) {
1170 if (c->frequency < 400000000) {
1171 priv->freq_offset = 2250000;
1172 } else {
1173 priv->freq_offset = 2750000;
1174 }
1175 priv->video_standard = XC4000_DTV7_8;
1176 type = DTV78;
1177 } else if (bw <= 6000000) {
1178 priv->video_standard = XC4000_DTV6;
1179 priv->freq_offset = 1750000;
1180 type = DTV6;
1181 } else if (bw <= 7000000) {
1182 priv->video_standard = XC4000_DTV7;
1183 priv->freq_offset = 2250000;
1184 type = DTV7;
1185 } else {
1186 priv->video_standard = XC4000_DTV8;
1187 priv->freq_offset = 2750000;
1188 type = DTV8;
1189 }
1190 priv->rf_mode = XC_RF_MODE_AIR;
1191 break;
1192 default:
1193 printk(KERN_ERR "xc4000 delivery system not supported!\n");
1194 ret = -EINVAL;
1195 goto fail;
1196 }
1197
1198 priv->freq_hz = c->frequency - priv->freq_offset;
1199
1200 dprintk(1, "%s() frequency=%d (compensated)\n",
1201 __func__, priv->freq_hz);
1202
1203
1204 if (check_firmware(fe, type, 0, priv->if_khz) != 0)
1205 goto fail;
1206
1207 priv->bandwidth = c->bandwidth_hz;
1208
1209 ret = xc_set_signal_source(priv, priv->rf_mode);
1210 if (ret != 0) {
1211 printk(KERN_ERR "xc4000: xc_set_signal_source(%d) failed\n",
1212 priv->rf_mode);
1213 goto fail;
1214 } else {
1215 u16 video_mode, audio_mode;
1216 video_mode = xc4000_standard[priv->video_standard].video_mode;
1217 audio_mode = xc4000_standard[priv->video_standard].audio_mode;
1218 if (type == DTV6 && priv->firm_version != 0x0102)
1219 video_mode |= 0x0001;
1220 ret = xc_set_tv_standard(priv, video_mode, audio_mode);
1221 if (ret != 0) {
1222 printk(KERN_ERR "xc4000: xc_set_tv_standard failed\n");
1223
1224
1225 }
1226 }
1227
1228 if (xc_write_reg(priv, XREG_D_CODE, 0) == 0)
1229 ret = 0;
1230 if (priv->dvb_amplitude != 0) {
1231 if (xc_write_reg(priv, XREG_AMPLITUDE,
1232 (priv->firm_version != 0x0102 ||
1233 priv->dvb_amplitude != 134 ?
1234 priv->dvb_amplitude : 132)) != 0)
1235 ret = -EREMOTEIO;
1236 }
1237 if (priv->set_smoothedcvbs != 0) {
1238 if (xc_write_reg(priv, XREG_SMOOTHEDCVBS, 1) != 0)
1239 ret = -EREMOTEIO;
1240 }
1241 if (ret != 0) {
1242 printk(KERN_ERR "xc4000: setting registers failed\n");
1243
1244 }
1245
1246 xc_tune_channel(priv, priv->freq_hz);
1247
1248 ret = 0;
1249
1250 fail:
1251 mutex_unlock(&priv->lock);
1252
1253 return ret;
1254 }
1255
1256 static int xc4000_set_analog_params(struct dvb_frontend *fe,
1257 struct analog_parameters *params)
1258 {
1259 struct xc4000_priv *priv = fe->tuner_priv;
1260 unsigned int type = 0;
1261 int ret = -EREMOTEIO;
1262
1263 if (params->mode == V4L2_TUNER_RADIO) {
1264 dprintk(1, "%s() frequency=%d (in units of 62.5Hz)\n",
1265 __func__, params->frequency);
1266
1267 mutex_lock(&priv->lock);
1268
1269 params->std = 0;
1270 priv->freq_hz = params->frequency * 125L / 2;
1271
1272 if (audio_std & XC4000_AUDIO_STD_INPUT1) {
1273 priv->video_standard = XC4000_FM_Radio_INPUT1;
1274 type = FM | INPUT1;
1275 } else {
1276 priv->video_standard = XC4000_FM_Radio_INPUT2;
1277 type = FM | INPUT2;
1278 }
1279
1280 goto tune_channel;
1281 }
1282
1283 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1284 __func__, params->frequency);
1285
1286 mutex_lock(&priv->lock);
1287
1288
1289 priv->freq_hz = params->frequency * 62500;
1290
1291 params->std &= V4L2_STD_ALL;
1292
1293 if (!params->std)
1294 params->std = V4L2_STD_PAL_BG;
1295
1296 if (audio_std & XC4000_AUDIO_STD_MONO)
1297 type = MONO;
1298
1299 if (params->std & V4L2_STD_MN) {
1300 params->std = V4L2_STD_MN;
1301 if (audio_std & XC4000_AUDIO_STD_MONO) {
1302 priv->video_standard = XC4000_MN_NTSC_PAL_Mono;
1303 } else if (audio_std & XC4000_AUDIO_STD_A2) {
1304 params->std |= V4L2_STD_A2;
1305 priv->video_standard = XC4000_MN_NTSC_PAL_A2;
1306 } else {
1307 params->std |= V4L2_STD_BTSC;
1308 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
1309 }
1310 goto tune_channel;
1311 }
1312
1313 if (params->std & V4L2_STD_PAL_BG) {
1314 params->std = V4L2_STD_PAL_BG;
1315 if (audio_std & XC4000_AUDIO_STD_MONO) {
1316 priv->video_standard = XC4000_BG_PAL_MONO;
1317 } else if (!(audio_std & XC4000_AUDIO_STD_A2)) {
1318 if (!(audio_std & XC4000_AUDIO_STD_B)) {
1319 params->std |= V4L2_STD_NICAM_A;
1320 priv->video_standard = XC4000_BG_PAL_NICAM;
1321 } else {
1322 params->std |= V4L2_STD_NICAM_B;
1323 priv->video_standard = XC4000_BG_PAL_NICAM;
1324 }
1325 } else {
1326 if (!(audio_std & XC4000_AUDIO_STD_B)) {
1327 params->std |= V4L2_STD_A2_A;
1328 priv->video_standard = XC4000_BG_PAL_A2;
1329 } else {
1330 params->std |= V4L2_STD_A2_B;
1331 priv->video_standard = XC4000_BG_PAL_A2;
1332 }
1333 }
1334 goto tune_channel;
1335 }
1336
1337 if (params->std & V4L2_STD_PAL_I) {
1338
1339 params->std = V4L2_STD_PAL_I | V4L2_STD_NICAM;
1340 if (audio_std & XC4000_AUDIO_STD_MONO)
1341 priv->video_standard = XC4000_I_PAL_NICAM_MONO;
1342 else
1343 priv->video_standard = XC4000_I_PAL_NICAM;
1344 goto tune_channel;
1345 }
1346
1347 if (params->std & V4L2_STD_PAL_DK) {
1348 params->std = V4L2_STD_PAL_DK;
1349 if (audio_std & XC4000_AUDIO_STD_MONO) {
1350 priv->video_standard = XC4000_DK_PAL_MONO;
1351 } else if (audio_std & XC4000_AUDIO_STD_A2) {
1352 params->std |= V4L2_STD_A2;
1353 priv->video_standard = XC4000_DK_PAL_A2;
1354 } else {
1355 params->std |= V4L2_STD_NICAM;
1356 priv->video_standard = XC4000_DK_PAL_NICAM;
1357 }
1358 goto tune_channel;
1359 }
1360
1361 if (params->std & V4L2_STD_SECAM_DK) {
1362
1363 params->std = V4L2_STD_SECAM_DK | V4L2_STD_A2;
1364 if (audio_std & XC4000_AUDIO_STD_L) {
1365 type = 0;
1366 priv->video_standard = XC4000_DK_SECAM_NICAM;
1367 } else if (audio_std & XC4000_AUDIO_STD_MONO) {
1368 priv->video_standard = XC4000_DK_SECAM_A2MONO;
1369 } else if (audio_std & XC4000_AUDIO_STD_K3) {
1370 params->std |= V4L2_STD_SECAM_K3;
1371 priv->video_standard = XC4000_DK_SECAM_A2LDK3;
1372 } else {
1373 priv->video_standard = XC4000_DK_SECAM_A2DK1;
1374 }
1375 goto tune_channel;
1376 }
1377
1378 if (params->std & V4L2_STD_SECAM_L) {
1379
1380 type = 0;
1381 params->std = V4L2_STD_SECAM_L | V4L2_STD_NICAM;
1382 priv->video_standard = XC4000_L_SECAM_NICAM;
1383 goto tune_channel;
1384 }
1385
1386 if (params->std & V4L2_STD_SECAM_LC) {
1387
1388 type = 0;
1389 params->std = V4L2_STD_SECAM_LC | V4L2_STD_NICAM;
1390 priv->video_standard = XC4000_LC_SECAM_NICAM;
1391 goto tune_channel;
1392 }
1393
1394 tune_channel:
1395
1396 priv->rf_mode = XC_RF_MODE_CABLE;
1397
1398 if (check_firmware(fe, type, params->std,
1399 xc4000_standard[priv->video_standard].int_freq) != 0)
1400 goto fail;
1401
1402 ret = xc_set_signal_source(priv, priv->rf_mode);
1403 if (ret != 0) {
1404 printk(KERN_ERR
1405 "xc4000: xc_set_signal_source(%d) failed\n",
1406 priv->rf_mode);
1407 goto fail;
1408 } else {
1409 u16 video_mode, audio_mode;
1410 video_mode = xc4000_standard[priv->video_standard].video_mode;
1411 audio_mode = xc4000_standard[priv->video_standard].audio_mode;
1412 if (priv->video_standard < XC4000_BG_PAL_A2) {
1413 if (type & NOGD)
1414 video_mode &= 0xFF7F;
1415 } else if (priv->video_standard < XC4000_I_PAL_NICAM) {
1416 if (priv->firm_version == 0x0102)
1417 video_mode &= 0xFEFF;
1418 if (audio_std & XC4000_AUDIO_STD_B)
1419 video_mode |= 0x0080;
1420 }
1421 ret = xc_set_tv_standard(priv, video_mode, audio_mode);
1422 if (ret != 0) {
1423 printk(KERN_ERR "xc4000: xc_set_tv_standard failed\n");
1424 goto fail;
1425 }
1426 }
1427
1428 if (xc_write_reg(priv, XREG_D_CODE, 0) == 0)
1429 ret = 0;
1430 if (xc_write_reg(priv, XREG_AMPLITUDE, 1) != 0)
1431 ret = -EREMOTEIO;
1432 if (priv->set_smoothedcvbs != 0) {
1433 if (xc_write_reg(priv, XREG_SMOOTHEDCVBS, 1) != 0)
1434 ret = -EREMOTEIO;
1435 }
1436 if (ret != 0) {
1437 printk(KERN_ERR "xc4000: setting registers failed\n");
1438 goto fail;
1439 }
1440
1441 xc_tune_channel(priv, priv->freq_hz);
1442
1443 ret = 0;
1444
1445 fail:
1446 mutex_unlock(&priv->lock);
1447
1448 return ret;
1449 }
1450
1451 static int xc4000_get_signal(struct dvb_frontend *fe, u16 *strength)
1452 {
1453 struct xc4000_priv *priv = fe->tuner_priv;
1454 u16 value = 0;
1455 int rc;
1456
1457 mutex_lock(&priv->lock);
1458 rc = xc4000_readreg(priv, XREG_SIGNAL_LEVEL, &value);
1459 mutex_unlock(&priv->lock);
1460
1461 if (rc < 0)
1462 goto ret;
1463
1464
1465
1466
1467 tuner_dbg("Signal strength: -%ddB (%05d)\n", value >> 8, value);
1468
1469
1470 if ((priv->video_standard == XC4000_DTV6) ||
1471 (priv->video_standard == XC4000_DTV7) ||
1472 (priv->video_standard == XC4000_DTV7_8) ||
1473 (priv->video_standard == XC4000_DTV8))
1474 goto digital;
1475
1476
1477
1478
1479
1480
1481 mutex_lock(&priv->lock);
1482 rc = xc4000_readreg(priv, XREG_NOISE_LEVEL, &value);
1483 mutex_unlock(&priv->lock);
1484
1485 tuner_dbg("Noise level: %ddB (%05d)\n", value >> 8, value);
1486
1487
1488 if (value >= 0x2000) {
1489 value = 0;
1490 } else {
1491 value = (~value << 3) & 0xffff;
1492 }
1493
1494 goto ret;
1495
1496
1497
1498
1499 digital:
1500
1501 if (value <= 0x3200) {
1502 value = 0xffff;
1503
1504 } else if (value >= 0x713A) {
1505 value = 0;
1506 } else {
1507 value = ~(value - 0x3200) << 2;
1508 }
1509
1510 ret:
1511 *strength = value;
1512
1513 return rc;
1514 }
1515
1516 static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1517 {
1518 struct xc4000_priv *priv = fe->tuner_priv;
1519
1520 *freq = priv->freq_hz + priv->freq_offset;
1521
1522 if (debug) {
1523 mutex_lock(&priv->lock);
1524 if ((priv->cur_fw.type
1525 & (BASE | FM | DTV6 | DTV7 | DTV78 | DTV8)) == BASE) {
1526 u16 snr = 0;
1527 if (xc4000_readreg(priv, XREG_SNR, &snr) == 0) {
1528 mutex_unlock(&priv->lock);
1529 dprintk(1, "%s() freq = %u, SNR = %d\n",
1530 __func__, *freq, snr);
1531 return 0;
1532 }
1533 }
1534 mutex_unlock(&priv->lock);
1535 }
1536
1537 dprintk(1, "%s()\n", __func__);
1538
1539 return 0;
1540 }
1541
1542 static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1543 {
1544 struct xc4000_priv *priv = fe->tuner_priv;
1545 dprintk(1, "%s()\n", __func__);
1546
1547 *bw = priv->bandwidth;
1548 return 0;
1549 }
1550
1551 static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1552 {
1553 struct xc4000_priv *priv = fe->tuner_priv;
1554 u16 lock_status = 0;
1555
1556 mutex_lock(&priv->lock);
1557
1558 if (priv->cur_fw.type & BASE)
1559 xc_get_lock_status(priv, &lock_status);
1560
1561 *status = (lock_status == 1 ?
1562 TUNER_STATUS_LOCKED | TUNER_STATUS_STEREO : 0);
1563 if (priv->cur_fw.type & (DTV6 | DTV7 | DTV78 | DTV8))
1564 *status &= (~TUNER_STATUS_STEREO);
1565
1566 mutex_unlock(&priv->lock);
1567
1568 dprintk(2, "%s() lock_status = %d\n", __func__, lock_status);
1569
1570 return 0;
1571 }
1572
1573 static int xc4000_sleep(struct dvb_frontend *fe)
1574 {
1575 struct xc4000_priv *priv = fe->tuner_priv;
1576 int ret = 0;
1577
1578 dprintk(1, "%s()\n", __func__);
1579
1580 mutex_lock(&priv->lock);
1581
1582
1583 if ((no_poweroff == 2 ||
1584 (no_poweroff == 0 && priv->default_pm != 0)) &&
1585 (priv->cur_fw.type & BASE) != 0) {
1586
1587 priv->cur_fw.type = XC_POWERED_DOWN;
1588
1589 if (xc_write_reg(priv, XREG_POWER_DOWN, 0) != 0) {
1590 printk(KERN_ERR
1591 "xc4000: %s() unable to shutdown tuner\n",
1592 __func__);
1593 ret = -EREMOTEIO;
1594 }
1595 msleep(20);
1596 }
1597
1598 mutex_unlock(&priv->lock);
1599
1600 return ret;
1601 }
1602
1603 static int xc4000_init(struct dvb_frontend *fe)
1604 {
1605 dprintk(1, "%s()\n", __func__);
1606
1607 return 0;
1608 }
1609
1610 static void xc4000_release(struct dvb_frontend *fe)
1611 {
1612 struct xc4000_priv *priv = fe->tuner_priv;
1613
1614 dprintk(1, "%s()\n", __func__);
1615
1616 mutex_lock(&xc4000_list_mutex);
1617
1618 if (priv)
1619 hybrid_tuner_release_state(priv);
1620
1621 mutex_unlock(&xc4000_list_mutex);
1622
1623 fe->tuner_priv = NULL;
1624 }
1625
1626 static const struct dvb_tuner_ops xc4000_tuner_ops = {
1627 .info = {
1628 .name = "Xceive XC4000",
1629 .frequency_min_hz = 1 * MHz,
1630 .frequency_max_hz = 1023 * MHz,
1631 .frequency_step_hz = 50 * kHz,
1632 },
1633
1634 .release = xc4000_release,
1635 .init = xc4000_init,
1636 .sleep = xc4000_sleep,
1637
1638 .set_params = xc4000_set_params,
1639 .set_analog_params = xc4000_set_analog_params,
1640 .get_frequency = xc4000_get_frequency,
1641 .get_rf_strength = xc4000_get_signal,
1642 .get_bandwidth = xc4000_get_bandwidth,
1643 .get_status = xc4000_get_status
1644 };
1645
1646 struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1647 struct i2c_adapter *i2c,
1648 struct xc4000_config *cfg)
1649 {
1650 struct xc4000_priv *priv = NULL;
1651 int instance;
1652 u16 id = 0;
1653
1654 dprintk(1, "%s(%d-%04x)\n", __func__,
1655 i2c ? i2c_adapter_id(i2c) : -1,
1656 cfg ? cfg->i2c_address : -1);
1657
1658 mutex_lock(&xc4000_list_mutex);
1659
1660 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1661 hybrid_tuner_instance_list,
1662 i2c, cfg->i2c_address, "xc4000");
1663 switch (instance) {
1664 case 0:
1665 goto fail;
1666 case 1:
1667
1668 priv->bandwidth = 6000000;
1669
1670 priv->if_khz = 4560;
1671 priv->default_pm = 0;
1672 priv->dvb_amplitude = 134;
1673 priv->set_smoothedcvbs = 1;
1674 mutex_init(&priv->lock);
1675 fe->tuner_priv = priv;
1676 break;
1677 default:
1678
1679 fe->tuner_priv = priv;
1680 break;
1681 }
1682
1683 if (cfg->if_khz != 0) {
1684
1685 priv->if_khz = cfg->if_khz;
1686 priv->default_pm = cfg->default_pm;
1687 priv->dvb_amplitude = cfg->dvb_amplitude;
1688 priv->set_smoothedcvbs = cfg->set_smoothedcvbs;
1689 }
1690
1691
1692
1693
1694
1695 if (instance == 1) {
1696 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != 0)
1697 goto fail;
1698 } else {
1699 id = ((priv->cur_fw.type & BASE) != 0 ?
1700 priv->hwmodel : XC_PRODUCT_ID_FW_NOT_LOADED);
1701 }
1702
1703 switch (id) {
1704 case XC_PRODUCT_ID_XC4000:
1705 case XC_PRODUCT_ID_XC4100:
1706 printk(KERN_INFO
1707 "xc4000: Successfully identified at address 0x%02x\n",
1708 cfg->i2c_address);
1709 printk(KERN_INFO
1710 "xc4000: Firmware has been loaded previously\n");
1711 break;
1712 case XC_PRODUCT_ID_FW_NOT_LOADED:
1713 printk(KERN_INFO
1714 "xc4000: Successfully identified at address 0x%02x\n",
1715 cfg->i2c_address);
1716 printk(KERN_INFO
1717 "xc4000: Firmware has not been loaded previously\n");
1718 break;
1719 default:
1720 printk(KERN_ERR
1721 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1722 cfg->i2c_address, id);
1723 goto fail;
1724 }
1725
1726 mutex_unlock(&xc4000_list_mutex);
1727
1728 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1729 sizeof(struct dvb_tuner_ops));
1730
1731 if (instance == 1) {
1732 int ret;
1733 mutex_lock(&priv->lock);
1734 ret = xc4000_fwupload(fe);
1735 mutex_unlock(&priv->lock);
1736 if (ret != 0)
1737 goto fail2;
1738 }
1739
1740 return fe;
1741 fail:
1742 mutex_unlock(&xc4000_list_mutex);
1743 fail2:
1744 xc4000_release(fe);
1745 return NULL;
1746 }
1747 EXPORT_SYMBOL(xc4000_attach);
1748
1749 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1750 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1751 MODULE_LICENSE("GPL");
1752 MODULE_FIRMWARE(XC4000_DEFAULT_FIRMWARE_NEW);
1753 MODULE_FIRMWARE(XC4000_DEFAULT_FIRMWARE);