1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Xceive XC4000 "QAM/8VSB single chip tuner"
5 * Copyright (c) 2007 Xceive Corporation
6 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
7 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
8 * Copyright (c) 2009 Davide Ferri <d.ferri@zero11.it>
9 * Copyright (c) 2010 Istvan Varga <istvan_v@mailbox.hu>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/videodev2.h>
15 #include <linux/delay.h>
16 #include <linux/dvb/frontend.h>
17 #include <linux/i2c.h>
18 #include <linux/mutex.h>
19 #include <asm/unaligned.h>
21 #include <media/dvb_frontend.h>
24 #include "tuner-i2c.h"
25 #include "tuner-xc2028-types.h"
28 module_param(debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "Debugging level (0 to 2, default: 0 (off)).");
31 static int no_poweroff
;
32 module_param(no_poweroff
, int, 0644);
33 MODULE_PARM_DESC(no_poweroff
, "Power management (1: disabled, 2: enabled, 0 (default): use device-specific default mode).");
36 module_param(audio_std
, int, 0644);
37 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"
38 " 1: use NICAM/B or A2/B instead of NICAM/A or A2/A\n"
39 " 2: use A2 instead of NICAM or BTSC\n"
40 " 4: use SECAM/K3 instead of K1\n"
41 " 8: use PAL-D/K audio for SECAM-D/K\n"
42 "16: use FM radio input 1 instead of input 2\n"
43 "32: use mono audio (the lower three bits are ignored)");
45 static char firmware_name
[30];
46 module_param_string(firmware_name
, firmware_name
, sizeof(firmware_name
), 0);
47 MODULE_PARM_DESC(firmware_name
, "Firmware file name. Allows overriding the default firmware name.");
49 static DEFINE_MUTEX(xc4000_list_mutex
);
50 static LIST_HEAD(hybrid_tuner_instance_list
);
52 #define dprintk(level, fmt, arg...) if (debug >= level) \
53 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
55 /* struct for storing firmware table */
56 struct firmware_description
{
64 struct firmware_properties
{
69 unsigned int scode_table
;
74 struct tuner_i2c_props i2c_props
;
75 struct list_head hybrid_tuner_instance_list
;
76 struct firmware_description
*firm
;
79 u32 freq_hz
, freq_offset
;
86 u8 ignore_i2c_write_errors
;
88 struct firmware_properties cur_fw
;
94 #define XC4000_AUDIO_STD_B 1
95 #define XC4000_AUDIO_STD_A2 2
96 #define XC4000_AUDIO_STD_K3 4
97 #define XC4000_AUDIO_STD_L 8
98 #define XC4000_AUDIO_STD_INPUT1 16
99 #define XC4000_AUDIO_STD_MONO 32
101 #define XC4000_DEFAULT_FIRMWARE "dvb-fe-xc4000-1.4.fw"
102 #define XC4000_DEFAULT_FIRMWARE_NEW "dvb-fe-xc4000-1.4.1.fw"
105 #define MAX_TV_STANDARD 24
106 #define XC_MAX_I2C_WRITE_LENGTH 64
107 #define XC_POWERED_DOWN 0x80000000U
110 #define XC_RF_MODE_AIR 0
111 #define XC_RF_MODE_CABLE 1
114 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
115 #define XC_PRODUCT_ID_XC4000 0x0FA0
116 #define XC_PRODUCT_ID_XC4100 0x1004
118 /* Registers (Write-only) */
119 #define XREG_INIT 0x00
120 #define XREG_VIDEO_MODE 0x01
121 #define XREG_AUDIO_MODE 0x02
122 #define XREG_RF_FREQ 0x03
123 #define XREG_D_CODE 0x04
124 #define XREG_DIRECTSITTING_MODE 0x05
125 #define XREG_SEEK_MODE 0x06
126 #define XREG_POWER_DOWN 0x08
127 #define XREG_SIGNALSOURCE 0x0A
128 #define XREG_SMOOTHEDCVBS 0x0E
129 #define XREG_AMPLITUDE 0x10
131 /* Registers (Read-only) */
132 #define XREG_ADC_ENV 0x00
133 #define XREG_QUALITY 0x01
134 #define XREG_FRAME_LINES 0x02
135 #define XREG_HSYNC_FREQ 0x03
136 #define XREG_LOCK 0x04
137 #define XREG_FREQ_ERROR 0x05
138 #define XREG_SNR 0x06
139 #define XREG_VERSION 0x07
140 #define XREG_PRODUCT_ID 0x08
141 #define XREG_SIGNAL_LEVEL 0x0A
142 #define XREG_NOISE_LEVEL 0x0B
145 Basic firmware description. This will remain with
146 the driver for documentation purposes.
148 This represents an I2C firmware file encoded as a
149 string of unsigned char. Format is as follows:
151 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
152 char[1 ]=len0_LSB -> length of first write transaction
153 char[2 ]=data0 -> first byte to be sent
157 char[M ]=dataN -> last byte to be sent
158 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
159 char[M+2]=len1_LSB -> length of second write transaction
165 The [len] value should be interpreted as follows:
167 len= len_MSB _ len_LSB
168 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
169 len=0000_0000_0000_0000 : Reset command: Do hardware reset
170 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
171 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
173 For the RESET and WAIT commands, the two following bytes will contain
174 immediately the length of the following transaction.
177 struct XC_TV_STANDARD
{
184 /* Tuner standards */
185 #define XC4000_MN_NTSC_PAL_BTSC 0
186 #define XC4000_MN_NTSC_PAL_A2 1
187 #define XC4000_MN_NTSC_PAL_EIAJ 2
188 #define XC4000_MN_NTSC_PAL_Mono 3
189 #define XC4000_BG_PAL_A2 4
190 #define XC4000_BG_PAL_NICAM 5
191 #define XC4000_BG_PAL_MONO 6
192 #define XC4000_I_PAL_NICAM 7
193 #define XC4000_I_PAL_NICAM_MONO 8
194 #define XC4000_DK_PAL_A2 9
195 #define XC4000_DK_PAL_NICAM 10
196 #define XC4000_DK_PAL_MONO 11
197 #define XC4000_DK_SECAM_A2DK1 12
198 #define XC4000_DK_SECAM_A2LDK3 13
199 #define XC4000_DK_SECAM_A2MONO 14
200 #define XC4000_DK_SECAM_NICAM 15
201 #define XC4000_L_SECAM_NICAM 16
202 #define XC4000_LC_SECAM_NICAM 17
203 #define XC4000_DTV6 18
204 #define XC4000_DTV8 19
205 #define XC4000_DTV7_8 20
206 #define XC4000_DTV7 21
207 #define XC4000_FM_Radio_INPUT2 22
208 #define XC4000_FM_Radio_INPUT1 23
210 static struct XC_TV_STANDARD xc4000_standard
[MAX_TV_STANDARD
] = {
211 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
212 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
213 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
214 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
215 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
216 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
217 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
218 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
219 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
220 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
221 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
222 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
223 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
224 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
225 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
226 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
227 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
228 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
229 {"DTV6", 0x00C0, 0x8002, 0},
230 {"DTV8", 0x00C0, 0x800B, 0},
231 {"DTV7/8", 0x00C0, 0x801B, 0},
232 {"DTV7", 0x00C0, 0x8007, 0},
233 {"FM Radio-INPUT2", 0x0008, 0x9800, 10700},
234 {"FM Radio-INPUT1", 0x0008, 0x9000, 10700}
237 static int xc4000_readreg(struct xc4000_priv
*priv
, u16 reg
, u16
*val
);
238 static int xc4000_tuner_reset(struct dvb_frontend
*fe
);
239 static void xc_debug_dump(struct xc4000_priv
*priv
);
241 static int xc_send_i2c_data(struct xc4000_priv
*priv
, u8
*buf
, int len
)
243 struct i2c_msg msg
= { .addr
= priv
->i2c_props
.addr
,
244 .flags
= 0, .buf
= buf
, .len
= len
};
245 if (i2c_transfer(priv
->i2c_props
.adap
, &msg
, 1) != 1) {
246 if (priv
->ignore_i2c_write_errors
== 0) {
247 printk(KERN_ERR
"xc4000: I2C write failed (len=%i)\n",
250 printk(KERN_ERR
"bytes %*ph\n", 4, buf
);
258 static int xc4000_tuner_reset(struct dvb_frontend
*fe
)
260 struct xc4000_priv
*priv
= fe
->tuner_priv
;
263 dprintk(1, "%s()\n", __func__
);
266 ret
= fe
->callback(((fe
->dvb
) && (fe
->dvb
->priv
)) ?
268 priv
->i2c_props
.adap
->algo_data
,
269 DVB_FRONTEND_COMPONENT_TUNER
,
270 XC4000_TUNER_RESET
, 0);
272 printk(KERN_ERR
"xc4000: reset failed\n");
276 printk(KERN_ERR
"xc4000: no tuner reset callback function, fatal\n");
282 static int xc_write_reg(struct xc4000_priv
*priv
, u16 regAddr
, u16 i2cData
)
287 buf
[0] = (regAddr
>> 8) & 0xFF;
288 buf
[1] = regAddr
& 0xFF;
289 buf
[2] = (i2cData
>> 8) & 0xFF;
290 buf
[3] = i2cData
& 0xFF;
291 result
= xc_send_i2c_data(priv
, buf
, 4);
296 static int xc_load_i2c_sequence(struct dvb_frontend
*fe
, const u8
*i2c_sequence
)
298 struct xc4000_priv
*priv
= fe
->tuner_priv
;
300 int i
, nbytes_to_send
, result
;
301 unsigned int len
, pos
, index
;
302 u8 buf
[XC_MAX_I2C_WRITE_LENGTH
];
305 while ((i2c_sequence
[index
] != 0xFF) ||
306 (i2c_sequence
[index
+ 1] != 0xFF)) {
307 len
= i2c_sequence
[index
] * 256 + i2c_sequence
[index
+1];
310 /* NOTE: this is ignored, as the reset callback was */
311 /* already called by check_firmware() */
313 } else if (len
& 0x8000) {
315 msleep(len
& 0x7FFF);
318 /* Send i2c data whilst ensuring individual transactions
319 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
322 buf
[0] = i2c_sequence
[index
];
323 buf
[1] = i2c_sequence
[index
+ 1];
326 if ((len
- pos
) > XC_MAX_I2C_WRITE_LENGTH
- 2)
328 XC_MAX_I2C_WRITE_LENGTH
;
330 nbytes_to_send
= (len
- pos
+ 2);
331 for (i
= 2; i
< nbytes_to_send
; i
++) {
332 buf
[i
] = i2c_sequence
[index
+ pos
+
335 result
= xc_send_i2c_data(priv
, buf
,
341 pos
+= nbytes_to_send
- 2;
349 static int xc_set_tv_standard(struct xc4000_priv
*priv
,
350 u16 video_mode
, u16 audio_mode
)
353 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__
, video_mode
, audio_mode
);
354 dprintk(1, "%s() Standard = %s\n",
356 xc4000_standard
[priv
->video_standard
].Name
);
358 /* Don't complain when the request fails because of i2c stretching */
359 priv
->ignore_i2c_write_errors
= 1;
361 ret
= xc_write_reg(priv
, XREG_VIDEO_MODE
, video_mode
);
363 ret
= xc_write_reg(priv
, XREG_AUDIO_MODE
, audio_mode
);
365 priv
->ignore_i2c_write_errors
= 0;
370 static int xc_set_signal_source(struct xc4000_priv
*priv
, u16 rf_mode
)
372 dprintk(1, "%s(%d) Source = %s\n", __func__
, rf_mode
,
373 rf_mode
== XC_RF_MODE_AIR
? "ANTENNA" : "CABLE");
375 if ((rf_mode
!= XC_RF_MODE_AIR
) && (rf_mode
!= XC_RF_MODE_CABLE
)) {
376 rf_mode
= XC_RF_MODE_CABLE
;
378 "%s(), Invalid mode, defaulting to CABLE",
381 return xc_write_reg(priv
, XREG_SIGNALSOURCE
, rf_mode
);
384 static const struct dvb_tuner_ops xc4000_tuner_ops
;
386 static int xc_set_rf_frequency(struct xc4000_priv
*priv
, u32 freq_hz
)
390 dprintk(1, "%s(%u)\n", __func__
, freq_hz
);
392 if ((freq_hz
> xc4000_tuner_ops
.info
.frequency_max_hz
) ||
393 (freq_hz
< xc4000_tuner_ops
.info
.frequency_min_hz
))
396 freq_code
= (u16
)(freq_hz
/ 15625);
398 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
399 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
400 only be used for fast scanning for channel lock) */
401 /* WAS: XREG_FINERFREQ */
402 return xc_write_reg(priv
, XREG_RF_FREQ
, freq_code
);
405 static int xc_get_adc_envelope(struct xc4000_priv
*priv
, u16
*adc_envelope
)
407 return xc4000_readreg(priv
, XREG_ADC_ENV
, adc_envelope
);
410 static int xc_get_frequency_error(struct xc4000_priv
*priv
, u32
*freq_error_hz
)
416 result
= xc4000_readreg(priv
, XREG_FREQ_ERROR
, ®Data
);
420 tmp
= (u32
)regData
& 0xFFFFU
;
421 tmp
= (tmp
< 0x8000U
? tmp
: 0x10000U
- tmp
);
422 (*freq_error_hz
) = tmp
* 15625;
426 static int xc_get_lock_status(struct xc4000_priv
*priv
, u16
*lock_status
)
428 return xc4000_readreg(priv
, XREG_LOCK
, lock_status
);
431 static int xc_get_version(struct xc4000_priv
*priv
,
432 u8
*hw_majorversion
, u8
*hw_minorversion
,
433 u8
*fw_majorversion
, u8
*fw_minorversion
)
438 result
= xc4000_readreg(priv
, XREG_VERSION
, &data
);
442 (*hw_majorversion
) = (data
>> 12) & 0x0F;
443 (*hw_minorversion
) = (data
>> 8) & 0x0F;
444 (*fw_majorversion
) = (data
>> 4) & 0x0F;
445 (*fw_minorversion
) = data
& 0x0F;
450 static int xc_get_hsync_freq(struct xc4000_priv
*priv
, u32
*hsync_freq_hz
)
455 result
= xc4000_readreg(priv
, XREG_HSYNC_FREQ
, ®Data
);
459 (*hsync_freq_hz
) = ((regData
& 0x0fff) * 763)/100;
463 static int xc_get_frame_lines(struct xc4000_priv
*priv
, u16
*frame_lines
)
465 return xc4000_readreg(priv
, XREG_FRAME_LINES
, frame_lines
);
468 static int xc_get_quality(struct xc4000_priv
*priv
, u16
*quality
)
470 return xc4000_readreg(priv
, XREG_QUALITY
, quality
);
473 static int xc_get_signal_level(struct xc4000_priv
*priv
, u16
*signal
)
475 return xc4000_readreg(priv
, XREG_SIGNAL_LEVEL
, signal
);
478 static int xc_get_noise_level(struct xc4000_priv
*priv
, u16
*noise
)
480 return xc4000_readreg(priv
, XREG_NOISE_LEVEL
, noise
);
483 static u16
xc_wait_for_lock(struct xc4000_priv
*priv
)
486 int watchdog_count
= 40;
488 while ((lock_state
== 0) && (watchdog_count
> 0)) {
489 xc_get_lock_status(priv
, &lock_state
);
490 if (lock_state
!= 1) {
498 static int xc_tune_channel(struct xc4000_priv
*priv
, u32 freq_hz
)
503 dprintk(1, "%s(%u)\n", __func__
, freq_hz
);
505 /* Don't complain when the request fails because of i2c stretching */
506 priv
->ignore_i2c_write_errors
= 1;
507 result
= xc_set_rf_frequency(priv
, freq_hz
);
508 priv
->ignore_i2c_write_errors
= 0;
513 /* wait for lock only in analog TV mode */
514 if ((priv
->cur_fw
.type
& (FM
| DTV6
| DTV7
| DTV78
| DTV8
)) == 0) {
515 if (xc_wait_for_lock(priv
) != 1)
519 /* Wait for stats to stabilize.
520 * Frame Lines needs two frame times after initial lock
521 * before it is valid.
523 msleep(debug
? 100 : 10);
531 static int xc4000_readreg(struct xc4000_priv
*priv
, u16 reg
, u16
*val
)
533 u8 buf
[2] = { reg
>> 8, reg
& 0xff };
534 u8 bval
[2] = { 0, 0 };
535 struct i2c_msg msg
[2] = {
536 { .addr
= priv
->i2c_props
.addr
,
537 .flags
= 0, .buf
= &buf
[0], .len
= 2 },
538 { .addr
= priv
->i2c_props
.addr
,
539 .flags
= I2C_M_RD
, .buf
= &bval
[0], .len
= 2 },
542 if (i2c_transfer(priv
->i2c_props
.adap
, msg
, 2) != 2) {
543 printk(KERN_ERR
"xc4000: I2C read failed\n");
547 *val
= (bval
[0] << 8) | bval
[1];
551 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
552 static void dump_firm_type_and_int_freq(unsigned int type
, u16 int_freq
)
555 printk(KERN_CONT
"BASE ");
557 printk(KERN_CONT
"INIT1 ");
559 printk(KERN_CONT
"F8MHZ ");
561 printk(KERN_CONT
"MTS ");
563 printk(KERN_CONT
"D2620 ");
565 printk(KERN_CONT
"D2633 ");
567 printk(KERN_CONT
"DTV6 ");
569 printk(KERN_CONT
"QAM ");
571 printk(KERN_CONT
"DTV7 ");
573 printk(KERN_CONT
"DTV78 ");
575 printk(KERN_CONT
"DTV8 ");
577 printk(KERN_CONT
"FM ");
579 printk(KERN_CONT
"INPUT1 ");
581 printk(KERN_CONT
"LCD ");
583 printk(KERN_CONT
"NOGD ");
585 printk(KERN_CONT
"MONO ");
587 printk(KERN_CONT
"ATSC ");
589 printk(KERN_CONT
"IF ");
591 printk(KERN_CONT
"LG60 ");
593 printk(KERN_CONT
"ATI638 ");
595 printk(KERN_CONT
"OREN538 ");
597 printk(KERN_CONT
"OREN36 ");
598 if (type
& TOYOTA388
)
599 printk(KERN_CONT
"TOYOTA388 ");
600 if (type
& TOYOTA794
)
601 printk(KERN_CONT
"TOYOTA794 ");
603 printk(KERN_CONT
"DIBCOM52 ");
604 if (type
& ZARLINK456
)
605 printk(KERN_CONT
"ZARLINK456 ");
607 printk(KERN_CONT
"CHINA ");
609 printk(KERN_CONT
"F6MHZ ");
611 printk(KERN_CONT
"INPUT2 ");
613 printk(KERN_CONT
"SCODE ");
615 printk(KERN_CONT
"HAS_IF_%d ", int_freq
);
618 static int seek_firmware(struct dvb_frontend
*fe
, unsigned int type
,
621 struct xc4000_priv
*priv
= fe
->tuner_priv
;
623 unsigned int best_nr_diffs
= 255U;
626 printk(KERN_ERR
"Error! firmware not loaded\n");
630 if (((type
& ~SCODE
) == 0) && (*id
== 0))
633 /* Seek for generic video standard match */
634 for (i
= 0; i
< priv
->firm_size
; i
++) {
635 v4l2_std_id id_diff_mask
=
636 (priv
->firm
[i
].id
^ (*id
)) & (*id
);
637 unsigned int type_diff_mask
=
638 (priv
->firm
[i
].type
^ type
)
639 & (BASE_TYPES
| DTV_TYPES
| LCD
| NOGD
| MONO
| SCODE
);
640 unsigned int nr_diffs
;
643 & (BASE
| INIT1
| FM
| DTV6
| DTV7
| DTV78
| DTV8
| SCODE
))
646 nr_diffs
= hweight64(id_diff_mask
) + hweight32(type_diff_mask
);
647 if (!nr_diffs
) /* Supports all the requested standards */
650 if (nr_diffs
< best_nr_diffs
) {
651 best_nr_diffs
= nr_diffs
;
656 /* FIXME: Would make sense to seek for type "hint" match ? */
662 if (best_nr_diffs
> 0U) {
664 "Selecting best matching firmware (%u bits differ) for type=(%x), id %016llx:\n",
665 best_nr_diffs
, type
, (unsigned long long)*id
);
670 *id
= priv
->firm
[i
].id
;
674 printk(KERN_DEBUG
"%s firmware for type=",
675 (i
< 0) ? "Can't find" : "Found");
676 dump_firm_type(type
);
677 printk(KERN_DEBUG
"(%x), id %016llx.\n", type
, (unsigned long long)*id
);
682 static int load_firmware(struct dvb_frontend
*fe
, unsigned int type
,
685 struct xc4000_priv
*priv
= fe
->tuner_priv
;
689 pos
= seek_firmware(fe
, type
, id
);
693 p
= priv
->firm
[pos
].ptr
;
695 /* Don't complain when the request fails because of i2c stretching */
696 priv
->ignore_i2c_write_errors
= 1;
698 rc
= xc_load_i2c_sequence(fe
, p
);
700 priv
->ignore_i2c_write_errors
= 0;
705 static int xc4000_fwupload(struct dvb_frontend
*fe
)
707 struct xc4000_priv
*priv
= fe
->tuner_priv
;
708 const struct firmware
*fw
= NULL
;
709 const unsigned char *p
, *endp
;
715 if (firmware_name
[0] != '\0') {
716 fname
= firmware_name
;
718 dprintk(1, "Reading custom firmware %s\n", fname
);
719 rc
= request_firmware(&fw
, fname
,
720 priv
->i2c_props
.adap
->dev
.parent
);
722 fname
= XC4000_DEFAULT_FIRMWARE_NEW
;
723 dprintk(1, "Trying to read firmware %s\n", fname
);
724 rc
= request_firmware(&fw
, fname
,
725 priv
->i2c_props
.adap
->dev
.parent
);
727 fname
= XC4000_DEFAULT_FIRMWARE
;
728 dprintk(1, "Trying to read firmware %s\n", fname
);
729 rc
= request_firmware(&fw
, fname
,
730 priv
->i2c_props
.adap
->dev
.parent
);
736 printk(KERN_ERR
"Error: firmware %s not found.\n", fname
);
738 printk(KERN_ERR
"Error %d while requesting firmware %s\n",
743 dprintk(1, "Loading Firmware: %s\n", fname
);
748 if (fw
->size
< sizeof(name
) - 1 + 2 + 2) {
749 printk(KERN_ERR
"Error: firmware file %s has invalid size!\n",
754 memcpy(name
, p
, sizeof(name
) - 1);
755 name
[sizeof(name
) - 1] = '\0';
756 p
+= sizeof(name
) - 1;
758 priv
->firm_version
= get_unaligned_le16(p
);
761 n_array
= get_unaligned_le16(p
);
764 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
765 n_array
, fname
, name
,
766 priv
->firm_version
>> 8, priv
->firm_version
& 0xff);
768 priv
->firm
= kcalloc(n_array
, sizeof(*priv
->firm
), GFP_KERNEL
);
769 if (priv
->firm
== NULL
) {
770 printk(KERN_ERR
"Not enough memory to load firmware file.\n");
774 priv
->firm_size
= n_array
;
784 printk(KERN_ERR
"More firmware images in file than were expected!\n");
788 /* Checks if there's enough bytes to read */
789 if (endp
- p
< sizeof(type
) + sizeof(id
) + sizeof(size
))
792 type
= get_unaligned_le32(p
);
795 id
= get_unaligned_le64(p
);
799 int_freq
= get_unaligned_le16(p
);
800 p
+= sizeof(int_freq
);
801 if (endp
- p
< sizeof(size
))
805 size
= get_unaligned_le32(p
);
808 if (!size
|| size
> endp
- p
) {
809 printk(KERN_ERR
"Firmware type (%x), id %llx is corrupted (size=%zd, expected %d)\n",
810 type
, (unsigned long long)id
,
815 priv
->firm
[n
].ptr
= kmemdup(p
, size
, GFP_KERNEL
);
816 if (priv
->firm
[n
].ptr
== NULL
) {
817 printk(KERN_ERR
"Not enough memory to load firmware file.\n");
823 printk(KERN_DEBUG
"Reading firmware type ");
824 dump_firm_type_and_int_freq(type
, int_freq
);
825 printk(KERN_DEBUG
"(%x), id %llx, size=%d.\n",
826 type
, (unsigned long long)id
, size
);
829 priv
->firm
[n
].type
= type
;
830 priv
->firm
[n
].id
= id
;
831 priv
->firm
[n
].size
= size
;
832 priv
->firm
[n
].int_freq
= int_freq
;
837 if (n
+ 1 != priv
->firm_size
) {
838 printk(KERN_ERR
"Firmware file is incomplete!\n");
845 printk(KERN_ERR
"Firmware header is incomplete!\n");
848 printk(KERN_ERR
"Error: firmware file is corrupted!\n");
851 release_firmware(fw
);
853 dprintk(1, "Firmware files loaded.\n");
858 static int load_scode(struct dvb_frontend
*fe
, unsigned int type
,
859 v4l2_std_id
*id
, __u16 int_freq
, int scode
)
861 struct xc4000_priv
*priv
= fe
->tuner_priv
;
867 dprintk(1, "%s called int_freq=%d\n", __func__
, int_freq
);
870 pos
= seek_firmware(fe
, type
, id
);
874 for (pos
= 0; pos
< priv
->firm_size
; pos
++) {
875 if ((priv
->firm
[pos
].int_freq
== int_freq
) &&
876 (priv
->firm
[pos
].type
& HAS_IF
))
879 if (pos
== priv
->firm_size
)
883 p
= priv
->firm
[pos
].ptr
;
885 if (priv
->firm
[pos
].size
!= 12 * 16 || scode
>= 16)
890 tuner_info("Loading SCODE for type=");
891 dump_firm_type_and_int_freq(priv
->firm
[pos
].type
,
892 priv
->firm
[pos
].int_freq
);
893 printk(KERN_CONT
"(%x), id %016llx.\n", priv
->firm
[pos
].type
,
894 (unsigned long long)*id
);
898 memcpy(&scode_buf
[1], p
, 12);
900 /* Enter direct-mode */
901 rc
= xc_write_reg(priv
, XREG_DIRECTSITTING_MODE
, 0);
903 printk(KERN_ERR
"failed to put device into direct mode!\n");
907 rc
= xc_send_i2c_data(priv
, scode_buf
, 13);
909 /* Even if the send failed, make sure we set back to indirect
911 printk(KERN_ERR
"Failed to set scode %d\n", rc
);
914 /* Switch back to indirect-mode */
915 memset(indirect_mode
, 0, sizeof(indirect_mode
));
916 indirect_mode
[4] = 0x88;
917 xc_send_i2c_data(priv
, indirect_mode
, sizeof(indirect_mode
));
923 static int check_firmware(struct dvb_frontend
*fe
, unsigned int type
,
924 v4l2_std_id std
, __u16 int_freq
)
926 struct xc4000_priv
*priv
= fe
->tuner_priv
;
927 struct firmware_properties new_fw
;
928 int rc
= 0, is_retry
= 0;
931 u8 hw_major
= 0, hw_minor
= 0, fw_major
= 0, fw_minor
= 0;
933 dprintk(1, "%s called\n", __func__
);
936 rc
= xc4000_fwupload(fe
);
944 new_fw
.std_req
= std
;
945 new_fw
.scode_table
= SCODE
;
947 new_fw
.int_freq
= int_freq
;
949 dprintk(1, "checking firmware, user requested type=");
951 dump_firm_type(new_fw
.type
);
952 printk(KERN_CONT
"(%x), id %016llx, ", new_fw
.type
,
953 (unsigned long long)new_fw
.std_req
);
955 printk(KERN_CONT
"scode_tbl ");
957 printk(KERN_CONT
"int_freq %d, ", new_fw
.int_freq
);
958 printk(KERN_CONT
"scode_nr %d\n", new_fw
.scode_nr
);
961 /* No need to reload base firmware if it matches */
962 if (priv
->cur_fw
.type
& BASE
) {
963 dprintk(1, "BASE firmware not changed.\n");
967 /* Updating BASE - forget about all currently loaded firmware */
968 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
970 /* Reset is needed before loading firmware */
971 rc
= xc4000_tuner_reset(fe
);
975 /* BASE firmwares are all std0 */
977 rc
= load_firmware(fe
, BASE
, &std0
);
979 printk(KERN_ERR
"Error %d while loading base firmware\n", rc
);
983 /* Load INIT1, if needed */
984 dprintk(1, "Load init1 firmware, if exists\n");
986 rc
= load_firmware(fe
, BASE
| INIT1
, &std0
);
988 rc
= load_firmware(fe
, BASE
| INIT1
, &std0
);
989 if (rc
< 0 && rc
!= -ENOENT
) {
990 tuner_err("Error %d while loading init1 firmware\n",
997 * No need to reload standard specific firmware if base firmware
998 * was not reloaded and requested video standards have not changed.
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
;
1006 /* Reloading std-specific firmware forces a SCODE update */
1007 priv
->cur_fw
.scode_table
= 0;
1009 /* Load the standard firmware */
1010 rc
= load_firmware(fe
, new_fw
.type
, &new_fw
.id
);
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");
1022 /* Load SCODE firmware, if exists */
1023 rc
= load_scode(fe
, new_fw
.type
| new_fw
.scode_table
, &new_fw
.id
,
1024 new_fw
.int_freq
, new_fw
.scode_nr
);
1026 dprintk(1, "load scode failed %d\n", rc
);
1029 if (xc4000_readreg(priv
, XREG_PRODUCT_ID
, &hwmodel
) < 0) {
1030 printk(KERN_ERR
"Unable to read tuner registers.\n");
1034 if (xc_get_version(priv
, &hw_major
, &hw_minor
, &fw_major
,
1036 printk(KERN_ERR
"Unable to read tuner registers.\n");
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
);
1043 /* Check firmware version against what we downloaded. */
1044 if (priv
->firm_version
!= ((fw_major
<< 8) | fw_minor
)) {
1046 "Incorrect readback of firmware version %d.%d.\n",
1047 fw_major
, fw_minor
);
1051 /* Check that the tuner hardware model remains consistent over time. */
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
)) {
1060 "Read invalid device hardware information - tuner hung?\n");
1064 priv
->cur_fw
= new_fw
;
1067 * By setting BASE in cur_fw.type only after successfully loading all
1068 * firmwares, we can:
1069 * 1. Identify that BASE firmware with type=0 has been loaded;
1070 * 2. Tell whether BASE firmware was just changed the next time through.
1072 priv
->cur_fw
.type
|= BASE
;
1077 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
1081 dprintk(1, "Retrying firmware load\n");
1090 static void xc_debug_dump(struct xc4000_priv
*priv
)
1093 u32 freq_error_hz
= 0;
1095 u32 hsync_freq_hz
= 0;
1100 u8 hw_majorversion
= 0, hw_minorversion
= 0;
1101 u8 fw_majorversion
= 0, fw_minorversion
= 0;
1103 xc_get_adc_envelope(priv
, &adc_envelope
);
1104 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope
);
1106 xc_get_frequency_error(priv
, &freq_error_hz
);
1107 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz
);
1109 xc_get_lock_status(priv
, &lock_status
);
1110 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
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
);
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",
1124 xc_get_frame_lines(priv
, &frame_lines
);
1125 dprintk(1, "*** Frame lines = %d\n", frame_lines
);
1128 xc_get_quality(priv
, &quality
);
1129 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality
);
1131 xc_get_signal_level(priv
, &signal
);
1132 dprintk(1, "*** Signal level = -%ddB (%d)\n", signal
>> 8, signal
);
1134 xc_get_noise_level(priv
, &noise
);
1135 dprintk(1, "*** Noise level = %ddB (%d)\n", noise
>> 8, noise
);
1138 static int xc4000_set_params(struct dvb_frontend
*fe
)
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
;
1145 int ret
= -EREMOTEIO
;
1147 dprintk(1, "%s() frequency=%d (Hz)\n", __func__
, c
->frequency
);
1149 mutex_lock(&priv
->lock
);
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
;
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
;
1168 dprintk(1, "%s() OFDM\n", __func__
);
1170 if (c
->frequency
< 400000000) {
1171 priv
->freq_offset
= 2250000;
1173 priv
->freq_offset
= 2750000;
1175 priv
->video_standard
= XC4000_DTV7_8
;
1177 } else if (bw
<= 6000000) {
1178 priv
->video_standard
= XC4000_DTV6
;
1179 priv
->freq_offset
= 1750000;
1181 } else if (bw
<= 7000000) {
1182 priv
->video_standard
= XC4000_DTV7
;
1183 priv
->freq_offset
= 2250000;
1186 priv
->video_standard
= XC4000_DTV8
;
1187 priv
->freq_offset
= 2750000;
1190 priv
->rf_mode
= XC_RF_MODE_AIR
;
1193 printk(KERN_ERR
"xc4000 delivery system not supported!\n");
1198 priv
->freq_hz
= c
->frequency
- priv
->freq_offset
;
1200 dprintk(1, "%s() frequency=%d (compensated)\n",
1201 __func__
, priv
->freq_hz
);
1203 /* Make sure the correct firmware type is loaded */
1204 if (check_firmware(fe
, type
, 0, priv
->if_khz
) != 0)
1207 priv
->bandwidth
= c
->bandwidth_hz
;
1209 ret
= xc_set_signal_source(priv
, priv
->rf_mode
);
1211 printk(KERN_ERR
"xc4000: xc_set_signal_source(%d) failed\n",
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
);
1222 printk(KERN_ERR
"xc4000: xc_set_tv_standard failed\n");
1223 /* DJH - do not return when it fails... */
1228 if (xc_write_reg(priv
, XREG_D_CODE
, 0) == 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)
1237 if (priv
->set_smoothedcvbs
!= 0) {
1238 if (xc_write_reg(priv
, XREG_SMOOTHEDCVBS
, 1) != 0)
1242 printk(KERN_ERR
"xc4000: setting registers failed\n");
1246 xc_tune_channel(priv
, priv
->freq_hz
);
1251 mutex_unlock(&priv
->lock
);
1256 static int xc4000_set_analog_params(struct dvb_frontend
*fe
,
1257 struct analog_parameters
*params
)
1259 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1260 unsigned int type
= 0;
1261 int ret
= -EREMOTEIO
;
1263 if (params
->mode
== V4L2_TUNER_RADIO
) {
1264 dprintk(1, "%s() frequency=%d (in units of 62.5Hz)\n",
1265 __func__
, params
->frequency
);
1267 mutex_lock(&priv
->lock
);
1270 priv
->freq_hz
= params
->frequency
* 125L / 2;
1272 if (audio_std
& XC4000_AUDIO_STD_INPUT1
) {
1273 priv
->video_standard
= XC4000_FM_Radio_INPUT1
;
1276 priv
->video_standard
= XC4000_FM_Radio_INPUT2
;
1283 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1284 __func__
, params
->frequency
);
1286 mutex_lock(&priv
->lock
);
1288 /* params->frequency is in units of 62.5khz */
1289 priv
->freq_hz
= params
->frequency
* 62500;
1291 params
->std
&= V4L2_STD_ALL
;
1292 /* if std is not defined, choose one */
1294 params
->std
= V4L2_STD_PAL_BG
;
1296 if (audio_std
& XC4000_AUDIO_STD_MONO
)
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
;
1307 params
->std
|= V4L2_STD_BTSC
;
1308 priv
->video_standard
= XC4000_MN_NTSC_PAL_BTSC
;
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
;
1322 params
->std
|= V4L2_STD_NICAM_B
;
1323 priv
->video_standard
= XC4000_BG_PAL_NICAM
;
1326 if (!(audio_std
& XC4000_AUDIO_STD_B
)) {
1327 params
->std
|= V4L2_STD_A2_A
;
1328 priv
->video_standard
= XC4000_BG_PAL_A2
;
1330 params
->std
|= V4L2_STD_A2_B
;
1331 priv
->video_standard
= XC4000_BG_PAL_A2
;
1337 if (params
->std
& V4L2_STD_PAL_I
) {
1338 /* default to NICAM audio standard */
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
;
1343 priv
->video_standard
= XC4000_I_PAL_NICAM
;
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
;
1355 params
->std
|= V4L2_STD_NICAM
;
1356 priv
->video_standard
= XC4000_DK_PAL_NICAM
;
1361 if (params
->std
& V4L2_STD_SECAM_DK
) {
1362 /* default to A2 audio standard */
1363 params
->std
= V4L2_STD_SECAM_DK
| V4L2_STD_A2
;
1364 if (audio_std
& XC4000_AUDIO_STD_L
) {
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
;
1373 priv
->video_standard
= XC4000_DK_SECAM_A2DK1
;
1378 if (params
->std
& V4L2_STD_SECAM_L
) {
1379 /* default to NICAM audio standard */
1381 params
->std
= V4L2_STD_SECAM_L
| V4L2_STD_NICAM
;
1382 priv
->video_standard
= XC4000_L_SECAM_NICAM
;
1386 if (params
->std
& V4L2_STD_SECAM_LC
) {
1387 /* default to NICAM audio standard */
1389 params
->std
= V4L2_STD_SECAM_LC
| V4L2_STD_NICAM
;
1390 priv
->video_standard
= XC4000_LC_SECAM_NICAM
;
1395 /* FIXME: it could be air. */
1396 priv
->rf_mode
= XC_RF_MODE_CABLE
;
1398 if (check_firmware(fe
, type
, params
->std
,
1399 xc4000_standard
[priv
->video_standard
].int_freq
) != 0)
1402 ret
= xc_set_signal_source(priv
, priv
->rf_mode
);
1405 "xc4000: xc_set_signal_source(%d) failed\n",
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
) {
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;
1421 ret
= xc_set_tv_standard(priv
, video_mode
, audio_mode
);
1423 printk(KERN_ERR
"xc4000: xc_set_tv_standard failed\n");
1428 if (xc_write_reg(priv
, XREG_D_CODE
, 0) == 0)
1430 if (xc_write_reg(priv
, XREG_AMPLITUDE
, 1) != 0)
1432 if (priv
->set_smoothedcvbs
!= 0) {
1433 if (xc_write_reg(priv
, XREG_SMOOTHEDCVBS
, 1) != 0)
1437 printk(KERN_ERR
"xc4000: setting registers failed\n");
1441 xc_tune_channel(priv
, priv
->freq_hz
);
1446 mutex_unlock(&priv
->lock
);
1451 static int xc4000_get_signal(struct dvb_frontend
*fe
, u16
*strength
)
1453 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1457 mutex_lock(&priv
->lock
);
1458 rc
= xc4000_readreg(priv
, XREG_SIGNAL_LEVEL
, &value
);
1459 mutex_unlock(&priv
->lock
);
1464 /* Information from real testing of DVB-T and radio part,
1465 coefficient for one dB is 0xff.
1467 tuner_dbg("Signal strength: -%ddB (%05d)\n", value
>> 8, value
);
1469 /* all known digital modes */
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
))
1476 /* Analog mode has NOISE LEVEL important, signal
1477 depends only on gain of antenna and amplifiers,
1478 but it doesn't tell anything about real quality
1481 mutex_lock(&priv
->lock
);
1482 rc
= xc4000_readreg(priv
, XREG_NOISE_LEVEL
, &value
);
1483 mutex_unlock(&priv
->lock
);
1485 tuner_dbg("Noise level: %ddB (%05d)\n", value
>> 8, value
);
1487 /* highest noise level: 32dB */
1488 if (value
>= 0x2000) {
1491 value
= (~value
<< 3) & 0xffff;
1496 /* Digital mode has SIGNAL LEVEL important and real
1497 noise level is stored in demodulator registers.
1500 /* best signal: -50dB */
1501 if (value
<= 0x3200) {
1503 /* minimum: -114dB - should be 0x7200 but real zero is 0x713A */
1504 } else if (value
>= 0x713A) {
1507 value
= ~(value
- 0x3200) << 2;
1516 static int xc4000_get_frequency(struct dvb_frontend
*fe
, u32
*freq
)
1518 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1520 *freq
= priv
->freq_hz
+ priv
->freq_offset
;
1523 mutex_lock(&priv
->lock
);
1524 if ((priv
->cur_fw
.type
1525 & (BASE
| FM
| DTV6
| DTV7
| DTV78
| DTV8
)) == BASE
) {
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
);
1534 mutex_unlock(&priv
->lock
);
1537 dprintk(1, "%s()\n", __func__
);
1542 static int xc4000_get_bandwidth(struct dvb_frontend
*fe
, u32
*bw
)
1544 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1545 dprintk(1, "%s()\n", __func__
);
1547 *bw
= priv
->bandwidth
;
1551 static int xc4000_get_status(struct dvb_frontend
*fe
, u32
*status
)
1553 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1554 u16 lock_status
= 0;
1556 mutex_lock(&priv
->lock
);
1558 if (priv
->cur_fw
.type
& BASE
)
1559 xc_get_lock_status(priv
, &lock_status
);
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
);
1566 mutex_unlock(&priv
->lock
);
1568 dprintk(2, "%s() lock_status = %d\n", __func__
, lock_status
);
1573 static int xc4000_sleep(struct dvb_frontend
*fe
)
1575 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1578 dprintk(1, "%s()\n", __func__
);
1580 mutex_lock(&priv
->lock
);
1582 /* Avoid firmware reload on slow devices */
1583 if ((no_poweroff
== 2 ||
1584 (no_poweroff
== 0 && priv
->default_pm
!= 0)) &&
1585 (priv
->cur_fw
.type
& BASE
) != 0) {
1586 /* force reset and firmware reload */
1587 priv
->cur_fw
.type
= XC_POWERED_DOWN
;
1589 if (xc_write_reg(priv
, XREG_POWER_DOWN
, 0) != 0) {
1591 "xc4000: %s() unable to shutdown tuner\n",
1598 mutex_unlock(&priv
->lock
);
1603 static int xc4000_init(struct dvb_frontend
*fe
)
1605 dprintk(1, "%s()\n", __func__
);
1610 static void xc4000_release(struct dvb_frontend
*fe
)
1612 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1614 dprintk(1, "%s()\n", __func__
);
1616 mutex_lock(&xc4000_list_mutex
);
1619 hybrid_tuner_release_state(priv
);
1621 mutex_unlock(&xc4000_list_mutex
);
1623 fe
->tuner_priv
= NULL
;
1626 static const struct dvb_tuner_ops xc4000_tuner_ops
= {
1628 .name
= "Xceive XC4000",
1629 .frequency_min_hz
= 1 * MHz
,
1630 .frequency_max_hz
= 1023 * MHz
,
1631 .frequency_step_hz
= 50 * kHz
,
1634 .release
= xc4000_release
,
1635 .init
= xc4000_init
,
1636 .sleep
= xc4000_sleep
,
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
1646 struct dvb_frontend
*xc4000_attach(struct dvb_frontend
*fe
,
1647 struct i2c_adapter
*i2c
,
1648 struct xc4000_config
*cfg
)
1650 struct xc4000_priv
*priv
= NULL
;
1654 dprintk(1, "%s(%d-%04x)\n", __func__
,
1655 i2c
? i2c_adapter_id(i2c
) : -1,
1656 cfg
? cfg
->i2c_address
: -1);
1658 mutex_lock(&xc4000_list_mutex
);
1660 instance
= hybrid_tuner_request_state(struct xc4000_priv
, priv
,
1661 hybrid_tuner_instance_list
,
1662 i2c
, cfg
->i2c_address
, "xc4000");
1667 /* new tuner instance */
1668 priv
->bandwidth
= 6000000;
1669 /* set default configuration */
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
;
1678 /* existing tuner instance */
1679 fe
->tuner_priv
= priv
;
1683 if (cfg
->if_khz
!= 0) {
1684 /* copy configuration if provided by the caller */
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
;
1691 /* Check if firmware has been loaded. It is possible that another
1692 instance of the driver has loaded the firmware.
1695 if (instance
== 1) {
1696 if (xc4000_readreg(priv
, XREG_PRODUCT_ID
, &id
) != 0)
1699 id
= ((priv
->cur_fw
.type
& BASE
) != 0 ?
1700 priv
->hwmodel
: XC_PRODUCT_ID_FW_NOT_LOADED
);
1704 case XC_PRODUCT_ID_XC4000
:
1705 case XC_PRODUCT_ID_XC4100
:
1707 "xc4000: Successfully identified at address 0x%02x\n",
1710 "xc4000: Firmware has been loaded previously\n");
1712 case XC_PRODUCT_ID_FW_NOT_LOADED
:
1714 "xc4000: Successfully identified at address 0x%02x\n",
1717 "xc4000: Firmware has not been loaded previously\n");
1721 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1722 cfg
->i2c_address
, id
);
1726 mutex_unlock(&xc4000_list_mutex
);
1728 memcpy(&fe
->ops
.tuner_ops
, &xc4000_tuner_ops
,
1729 sizeof(struct dvb_tuner_ops
));
1731 if (instance
== 1) {
1733 mutex_lock(&priv
->lock
);
1734 ret
= xc4000_fwupload(fe
);
1735 mutex_unlock(&priv
->lock
);
1742 mutex_unlock(&xc4000_list_mutex
);
1747 EXPORT_SYMBOL(xc4000_attach
);
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
);