2 * Driver for Xceive XC4000 "QAM/8VSB single chip tuner"
4 * Copyright (c) 2007 Xceive Corporation
5 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 * Copyright (c) 2009 Davide Ferri <d.ferri@zero11.it>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/videodev2.h>
28 #include <linux/delay.h>
29 #include <linux/dvb/frontend.h>
30 #include <linux/i2c.h>
31 #include <linux/mutex.h>
32 #include <asm/unaligned.h>
34 #include "dvb_frontend.h"
37 #include "tuner-i2c.h"
38 #include "tuner-xc2028-types.h"
41 module_param(debug
, int, 0644);
42 MODULE_PARM_DESC(debug
, "\n\t\tDebugging level (0 to 2, default: 0 (off)).");
44 static int no_poweroff
;
45 module_param(no_poweroff
, int, 0644);
46 MODULE_PARM_DESC(no_poweroff
, "\n\t\t1: keep device energized and with tuner "
47 "ready all the times.\n"
48 "\t\tFaster, but consumes more power and keeps the device hotter.\n"
49 "\t\t2: powers device off when not used.\n"
50 "\t\t0 (default): use device-specific default mode.");
52 #define XC4000_DEFAULT_FIRMWARE "xc4000.fw"
54 static char firmware_name
[30];
55 module_param_string(firmware_name
, firmware_name
, sizeof(firmware_name
), 0);
56 MODULE_PARM_DESC(firmware_name
, "\n\t\tFirmware file name. Allows overriding "
57 "the default firmware\n"
60 static DEFINE_MUTEX(xc4000_list_mutex
);
61 static LIST_HEAD(hybrid_tuner_instance_list
);
63 #define dprintk(level, fmt, arg...) if (debug >= level) \
64 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
66 /* struct for storing firmware table */
67 struct firmware_description
{
75 struct firmware_properties
{
80 unsigned int scode_table
;
85 struct tuner_i2c_props i2c_props
;
86 struct list_head hybrid_tuner_instance_list
;
87 struct firmware_description
*firm
;
96 u8 ignore_i2c_write_errors
;
97 /* struct xc2028_ctrl ctrl; */
98 struct firmware_properties cur_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_RESULT_SUCCESS 0
115 #define XC_RESULT_RESET_FAILURE 1
116 #define XC_RESULT_I2C_WRITE_FAILURE 2
117 #define XC_RESULT_I2C_READ_FAILURE 3
118 #define XC_RESULT_OUT_OF_RANGE 5
121 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
122 #define XC_PRODUCT_ID_FW_LOADED 0x0FA0
124 /* Registers (Write-only) */
125 #define XREG_INIT 0x00
126 #define XREG_VIDEO_MODE 0x01
127 #define XREG_AUDIO_MODE 0x02
128 #define XREG_RF_FREQ 0x03
129 #define XREG_D_CODE 0x04
130 #define XREG_DIRECTSITTING_MODE 0x05
131 #define XREG_SEEK_MODE 0x06
132 #define XREG_POWER_DOWN 0x08
133 #define XREG_SIGNALSOURCE 0x0A
134 #define XREG_AMPLITUDE 0x10
136 /* Registers (Read-only) */
137 #define XREG_ADC_ENV 0x00
138 #define XREG_QUALITY 0x01
139 #define XREG_FRAME_LINES 0x02
140 #define XREG_HSYNC_FREQ 0x03
141 #define XREG_LOCK 0x04
142 #define XREG_FREQ_ERROR 0x05
143 #define XREG_SNR 0x06
144 #define XREG_VERSION 0x07
145 #define XREG_PRODUCT_ID 0x08
148 Basic firmware description. This will remain with
149 the driver for documentation purposes.
151 This represents an I2C firmware file encoded as a
152 string of unsigned char. Format is as follows:
154 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
155 char[1 ]=len0_LSB -> length of first write transaction
156 char[2 ]=data0 -> first byte to be sent
160 char[M ]=dataN -> last byte to be sent
161 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
162 char[M+2]=len1_LSB -> length of second write transaction
168 The [len] value should be interpreted as follows:
170 len= len_MSB _ len_LSB
171 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
172 len=0000_0000_0000_0000 : Reset command: Do hardware reset
173 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
174 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
176 For the RESET and WAIT commands, the two following bytes will contain
177 immediately the length of the following transaction.
180 struct XC_TV_STANDARD
{
187 /* Tuner standards */
188 #define XC4000_MN_NTSC_PAL_BTSC 0
189 #define XC4000_MN_NTSC_PAL_A2 1
190 #define XC4000_MN_NTSC_PAL_EIAJ 2
191 #define XC4000_MN_NTSC_PAL_Mono 3
192 #define XC4000_BG_PAL_A2 4
193 #define XC4000_BG_PAL_NICAM 5
194 #define XC4000_BG_PAL_MONO 6
195 #define XC4000_I_PAL_NICAM 7
196 #define XC4000_I_PAL_NICAM_MONO 8
197 #define XC4000_DK_PAL_A2 9
198 #define XC4000_DK_PAL_NICAM 10
199 #define XC4000_DK_PAL_MONO 11
200 #define XC4000_DK_SECAM_A2DK1 12
201 #define XC4000_DK_SECAM_A2LDK3 13
202 #define XC4000_DK_SECAM_A2MONO 14
203 #define XC4000_DK_SECAM_NICAM 15
204 #define XC4000_L_SECAM_NICAM 16
205 #define XC4000_LC_SECAM_NICAM 17
206 #define XC4000_DTV6 18
207 #define XC4000_DTV8 19
208 #define XC4000_DTV7_8 20
209 #define XC4000_DTV7 21
210 #define XC4000_FM_Radio_INPUT2 22
211 #define XC4000_FM_Radio_INPUT1 23
213 static struct XC_TV_STANDARD XC4000_Standard
[MAX_TV_STANDARD
] = {
214 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
215 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
216 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
217 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
218 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
219 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
220 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
221 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
222 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
223 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
224 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
225 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
226 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
227 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
228 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
229 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
230 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
231 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
232 {"DTV6", 0x00C0, 0x8002, 0},
233 {"DTV8", 0x00C0, 0x800B, 0},
234 {"DTV7/8", 0x00C0, 0x801B, 0},
235 {"DTV7", 0x00C0, 0x8007, 0},
236 {"FM Radio-INPUT2", 0x0008, 0x9800,10700},
237 {"FM Radio-INPUT1", 0x0008, 0x9000,10700}
240 static int xc4000_readreg(struct xc4000_priv
*priv
, u16 reg
, u16
*val
);
241 static int xc4000_TunerReset(struct dvb_frontend
*fe
);
242 static void xc_debug_dump(struct xc4000_priv
*priv
);
244 static int xc_send_i2c_data(struct xc4000_priv
*priv
, u8
*buf
, int len
)
246 struct i2c_msg msg
= { .addr
= priv
->i2c_props
.addr
,
247 .flags
= 0, .buf
= buf
, .len
= len
};
248 if (i2c_transfer(priv
->i2c_props
.adap
, &msg
, 1) != 1) {
249 if (priv
->ignore_i2c_write_errors
== 0) {
250 printk(KERN_ERR
"xc4000: I2C write failed (len=%i)\n",
253 printk("bytes %02x %02x %02x %02x\n", buf
[0],
254 buf
[1], buf
[2], buf
[3]);
256 return XC_RESULT_I2C_WRITE_FAILURE
;
259 return XC_RESULT_SUCCESS
;
262 static void xc_wait(int wait_ms
)
267 static int xc4000_TunerReset(struct dvb_frontend
*fe
)
269 struct xc4000_priv
*priv
= fe
->tuner_priv
;
272 dprintk(1, "%s()\n", __func__
);
275 ret
= fe
->callback(((fe
->dvb
) && (fe
->dvb
->priv
)) ?
277 priv
->i2c_props
.adap
->algo_data
,
278 DVB_FRONTEND_COMPONENT_TUNER
,
279 XC4000_TUNER_RESET
, 0);
281 printk(KERN_ERR
"xc4000: reset failed\n");
282 return XC_RESULT_RESET_FAILURE
;
285 printk(KERN_ERR
"xc4000: no tuner reset callback function, fatal\n");
286 return XC_RESULT_RESET_FAILURE
;
288 return XC_RESULT_SUCCESS
;
291 static int xc_write_reg(struct xc4000_priv
*priv
, u16 regAddr
, u16 i2cData
)
296 buf
[0] = (regAddr
>> 8) & 0xFF;
297 buf
[1] = regAddr
& 0xFF;
298 buf
[2] = (i2cData
>> 8) & 0xFF;
299 buf
[3] = i2cData
& 0xFF;
300 result
= xc_send_i2c_data(priv
, buf
, 4);
305 static int xc_load_i2c_sequence(struct dvb_frontend
*fe
, const u8
*i2c_sequence
)
307 struct xc4000_priv
*priv
= fe
->tuner_priv
;
309 int i
, nbytes_to_send
, result
;
310 unsigned int len
, pos
, index
;
311 u8 buf
[XC_MAX_I2C_WRITE_LENGTH
];
314 while ((i2c_sequence
[index
] != 0xFF) ||
315 (i2c_sequence
[index
+ 1] != 0xFF)) {
316 len
= i2c_sequence
[index
] * 256 + i2c_sequence
[index
+1];
319 result
= xc4000_TunerReset(fe
);
321 if (result
!= XC_RESULT_SUCCESS
)
323 } else if (len
& 0x8000) {
325 xc_wait(len
& 0x7FFF);
328 /* Send i2c data whilst ensuring individual transactions
329 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
332 buf
[0] = i2c_sequence
[index
];
333 buf
[1] = i2c_sequence
[index
+ 1];
336 if ((len
- pos
) > XC_MAX_I2C_WRITE_LENGTH
- 2)
338 XC_MAX_I2C_WRITE_LENGTH
;
340 nbytes_to_send
= (len
- pos
+ 2);
341 for (i
= 2; i
< nbytes_to_send
; i
++) {
342 buf
[i
] = i2c_sequence
[index
+ pos
+
345 result
= xc_send_i2c_data(priv
, buf
,
348 if (result
!= XC_RESULT_SUCCESS
)
351 pos
+= nbytes_to_send
- 2;
356 return XC_RESULT_SUCCESS
;
359 static int xc_SetTVStandard(struct xc4000_priv
*priv
,
360 u16 VideoMode
, u16 AudioMode
)
363 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__
, VideoMode
, AudioMode
);
364 dprintk(1, "%s() Standard = %s\n",
366 XC4000_Standard
[priv
->video_standard
].Name
);
368 /* Don't complain when the request fails because of i2c stretching */
369 priv
->ignore_i2c_write_errors
= 1;
371 ret
= xc_write_reg(priv
, XREG_VIDEO_MODE
, VideoMode
);
372 if (ret
== XC_RESULT_SUCCESS
)
373 ret
= xc_write_reg(priv
, XREG_AUDIO_MODE
, AudioMode
);
375 priv
->ignore_i2c_write_errors
= 0;
380 static int xc_SetSignalSource(struct xc4000_priv
*priv
, u16 rf_mode
)
382 dprintk(1, "%s(%d) Source = %s\n", __func__
, rf_mode
,
383 rf_mode
== XC_RF_MODE_AIR
? "ANTENNA" : "CABLE");
385 if ((rf_mode
!= XC_RF_MODE_AIR
) && (rf_mode
!= XC_RF_MODE_CABLE
)) {
386 rf_mode
= XC_RF_MODE_CABLE
;
388 "%s(), Invalid mode, defaulting to CABLE",
391 return xc_write_reg(priv
, XREG_SIGNALSOURCE
, rf_mode
);
394 static const struct dvb_tuner_ops xc4000_tuner_ops
;
396 static int xc_set_RF_frequency(struct xc4000_priv
*priv
, u32 freq_hz
)
400 dprintk(1, "%s(%u)\n", __func__
, freq_hz
);
402 if ((freq_hz
> xc4000_tuner_ops
.info
.frequency_max
) ||
403 (freq_hz
< xc4000_tuner_ops
.info
.frequency_min
))
404 return XC_RESULT_OUT_OF_RANGE
;
406 freq_code
= (u16
)(freq_hz
/ 15625);
408 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
409 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
410 only be used for fast scanning for channel lock) */
411 return xc_write_reg(priv
, XREG_RF_FREQ
, freq_code
); /* WAS: XREG_FINERFREQ */
414 static int xc_get_ADC_Envelope(struct xc4000_priv
*priv
, u16
*adc_envelope
)
416 return xc4000_readreg(priv
, XREG_ADC_ENV
, adc_envelope
);
419 static int xc_get_frequency_error(struct xc4000_priv
*priv
, u32
*freq_error_hz
)
425 result
= xc4000_readreg(priv
, XREG_FREQ_ERROR
, ®Data
);
426 if (result
!= XC_RESULT_SUCCESS
)
429 tmp
= (u32
)regData
& 0xFFFFU
;
430 tmp
= (tmp
< 0x8000U
? tmp
: 0x10000U
- tmp
);
431 (*freq_error_hz
) = tmp
* 15625;
435 static int xc_get_lock_status(struct xc4000_priv
*priv
, u16
*lock_status
)
437 return xc4000_readreg(priv
, XREG_LOCK
, lock_status
);
440 static int xc_get_version(struct xc4000_priv
*priv
,
441 u8
*hw_majorversion
, u8
*hw_minorversion
,
442 u8
*fw_majorversion
, u8
*fw_minorversion
)
447 result
= xc4000_readreg(priv
, XREG_VERSION
, &data
);
448 if (result
!= XC_RESULT_SUCCESS
)
451 (*hw_majorversion
) = (data
>> 12) & 0x0F;
452 (*hw_minorversion
) = (data
>> 8) & 0x0F;
453 (*fw_majorversion
) = (data
>> 4) & 0x0F;
454 (*fw_minorversion
) = data
& 0x0F;
459 static int xc_get_hsync_freq(struct xc4000_priv
*priv
, u32
*hsync_freq_hz
)
464 result
= xc4000_readreg(priv
, XREG_HSYNC_FREQ
, ®Data
);
465 if (result
!= XC_RESULT_SUCCESS
)
468 (*hsync_freq_hz
) = ((regData
& 0x0fff) * 763)/100;
472 static int xc_get_frame_lines(struct xc4000_priv
*priv
, u16
*frame_lines
)
474 return xc4000_readreg(priv
, XREG_FRAME_LINES
, frame_lines
);
477 static int xc_get_quality(struct xc4000_priv
*priv
, u16
*quality
)
479 return xc4000_readreg(priv
, XREG_QUALITY
, quality
);
482 static u16
WaitForLock(struct xc4000_priv
*priv
)
485 int watchDogCount
= 40;
487 while ((lockState
== 0) && (watchDogCount
> 0)) {
488 xc_get_lock_status(priv
, &lockState
);
489 if (lockState
!= 1) {
497 #define XC_TUNE_ANALOG 0
498 #define XC_TUNE_DIGITAL 1
499 static int xc_tune_channel(struct xc4000_priv
*priv
, u32 freq_hz
, int mode
)
504 dprintk(1, "%s(%u)\n", __func__
, freq_hz
);
506 /* Don't complain when the request fails because of i2c stretching */
507 priv
->ignore_i2c_write_errors
= 1;
508 result
= xc_set_RF_frequency(priv
, freq_hz
);
509 priv
->ignore_i2c_write_errors
= 0;
511 if (result
!= XC_RESULT_SUCCESS
)
514 if (mode
== XC_TUNE_ANALOG
) {
515 if (WaitForLock(priv
) == 1)
519 /* Wait for stats to stabilize.
520 * Frame Lines needs two frame times after initial lock
521 * before it is valid.
523 xc_wait(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_WARNING
"xc4000: I2C read failed\n");
547 *val
= (bval
[0] << 8) | bval
[1];
548 return XC_RESULT_SUCCESS
;
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
)
598 if (type
& TOYOTA388
)
599 printk("TOYOTA388 ");
600 if (type
& TOYOTA794
)
601 printk("TOYOTA794 ");
604 if (type
& ZARLINK456
)
605 printk("ZARLINK456 ");
615 printk("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("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) {
663 printk("Selecting best matching firmware (%u bits differ) for "
664 "type=", best_nr_diffs
);
665 printk("(%x), id %016llx:\n", type
, (unsigned long long)*id
);
670 *id
= priv
->firm
[i
].id
;
674 printk("%s firmware for type=", (i
< 0) ? "Can't find" :
676 dump_firm_type(type
);
677 printk("(%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 fname
= XC4000_DEFAULT_FIRMWARE
;
720 printk("Reading firmware %s\n", fname
);
721 rc
= request_firmware(&fw
, fname
, priv
->i2c_props
.adap
->dev
.parent
);
724 printk("Error: firmware %s not found.\n",
727 printk("Error %d while requesting firmware %s \n",
735 if (fw
->size
< sizeof(name
) - 1 + 2 + 2) {
736 printk("Error: firmware file %s has invalid size!\n",
741 memcpy(name
, p
, sizeof(name
) - 1);
742 name
[sizeof(name
) - 1] = 0;
743 p
+= sizeof(name
) - 1;
745 priv
->firm_version
= get_unaligned_le16(p
);
748 n_array
= get_unaligned_le16(p
);
751 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
752 n_array
, fname
, name
,
753 priv
->firm_version
>> 8, priv
->firm_version
& 0xff);
755 priv
->firm
= kzalloc(sizeof(*priv
->firm
) * n_array
, GFP_KERNEL
);
756 if (priv
->firm
== NULL
) {
757 printk("Not enough memory to load firmware file.\n");
761 priv
->firm_size
= n_array
;
771 printk("More firmware images in file than "
776 /* Checks if there's enough bytes to read */
777 if (endp
- p
< sizeof(type
) + sizeof(id
) + sizeof(size
))
780 type
= get_unaligned_le32(p
);
783 id
= get_unaligned_le64(p
);
787 int_freq
= get_unaligned_le16(p
);
788 p
+= sizeof(int_freq
);
789 if (endp
- p
< sizeof(size
))
793 size
= get_unaligned_le32(p
);
796 if (!size
|| size
> endp
- p
) {
797 printk("Firmware type (%x), id %llx is corrupted "
798 "(size=%d, expected %d)\n",
799 type
, (unsigned long long)id
,
800 (unsigned)(endp
- p
), size
);
804 priv
->firm
[n
].ptr
= kzalloc(size
, GFP_KERNEL
);
805 if (priv
->firm
[n
].ptr
== NULL
) {
806 printk("Not enough memory to load firmware file.\n");
812 printk("Reading firmware type ");
813 dump_firm_type_and_int_freq(type
, int_freq
);
814 printk("(%x), id %llx, size=%d.\n",
815 type
, (unsigned long long)id
, size
);
818 memcpy(priv
->firm
[n
].ptr
, p
, size
);
819 priv
->firm
[n
].type
= type
;
820 priv
->firm
[n
].id
= id
;
821 priv
->firm
[n
].size
= size
;
822 priv
->firm
[n
].int_freq
= int_freq
;
827 if (n
+ 1 != priv
->firm_size
) {
828 printk("Firmware file is incomplete!\n");
835 printk("Firmware header is incomplete!\n");
838 printk("Error: firmware file is corrupted!\n");
841 printk("Releasing partially loaded firmware file.\n");
844 release_firmware(fw
);
846 dprintk(1, "Firmware files loaded.\n");
851 static int load_scode(struct dvb_frontend
*fe
, unsigned int type
,
852 v4l2_std_id
*id
, __u16 int_freq
, int scode
)
854 struct xc4000_priv
*priv
= fe
->tuner_priv
;
860 dprintk(1, "%s called int_freq=%d\n", __func__
, int_freq
);
863 pos
= seek_firmware(fe
, type
, id
);
867 for (pos
= 0; pos
< priv
->firm_size
; pos
++) {
868 if ((priv
->firm
[pos
].int_freq
== int_freq
) &&
869 (priv
->firm
[pos
].type
& HAS_IF
))
872 if (pos
== priv
->firm_size
)
876 p
= priv
->firm
[pos
].ptr
;
878 if (priv
->firm
[pos
].size
!= 12 * 16 || scode
>= 16)
882 tuner_info("Loading SCODE for type=");
883 dump_firm_type_and_int_freq(priv
->firm
[pos
].type
,
884 priv
->firm
[pos
].int_freq
);
885 printk("(%x), id %016llx.\n", priv
->firm
[pos
].type
,
886 (unsigned long long)*id
);
889 memcpy(&scode_buf
[1], p
, 12);
891 /* Enter direct-mode */
892 rc
= xc_write_reg(priv
, XREG_DIRECTSITTING_MODE
, 0);
894 printk("failed to put device into direct mode!\n");
898 rc
= xc_send_i2c_data(priv
, scode_buf
, 13);
899 if (rc
!= XC_RESULT_SUCCESS
) {
900 /* Even if the send failed, make sure we set back to indirect
902 printk("Failed to set scode %d\n", rc
);
905 /* Switch back to indirect-mode */
906 memset(indirect_mode
, 0, sizeof(indirect_mode
));
907 indirect_mode
[4] = 0x88;
908 xc_send_i2c_data(priv
, indirect_mode
, sizeof(indirect_mode
));
914 static int check_firmware(struct dvb_frontend
*fe
, unsigned int type
,
915 v4l2_std_id std
, __u16 int_freq
)
917 struct xc4000_priv
*priv
= fe
->tuner_priv
;
918 struct firmware_properties new_fw
;
919 int rc
= 0, is_retry
= 0;
920 u16 version
= 0, hwmodel
;
922 u8 hw_major
, hw_minor
, fw_major
, fw_minor
;
924 dprintk(1, "%s called\n", __func__
);
927 rc
= xc4000_fwupload(fe
);
933 if (priv
->ctrl
.mts
&& !(type
& FM
))
940 new_fw
.std_req
= std
;
941 new_fw
.scode_table
= SCODE
/* | priv->ctrl.scode_table */;
943 new_fw
.int_freq
= int_freq
;
945 dprintk(1, "checking firmware, user requested type=");
947 dump_firm_type(new_fw
.type
);
948 printk("(%x), id %016llx, ", new_fw
.type
,
949 (unsigned long long)new_fw
.std_req
);
951 printk("scode_tbl ");
953 dump_firm_type(priv
->ctrl
.scode_table
);
954 printk("(%x), ", priv
->ctrl
.scode_table
);
957 printk("int_freq %d, ", new_fw
.int_freq
);
958 printk("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_TunerReset(fe
);
975 /* BASE firmwares are all std0 */
977 rc
= load_firmware(fe
, BASE
, &std0
);
979 printk("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
);
1025 if (rc
!= XC_RESULT_SUCCESS
)
1026 dprintk(1, "load scode failed %d\n", rc
);
1029 rc
= xc4000_readreg(priv
, XREG_PRODUCT_ID
, &hwmodel
);
1031 if (xc_get_version(priv
, &hw_major
, &hw_minor
, &fw_major
,
1032 &fw_minor
) != XC_RESULT_SUCCESS
) {
1033 printk("Unable to read tuner registers.\n");
1037 dprintk(1, "Device is Xceive %d version %d.%d, "
1038 "firmware version %d.%d\n",
1039 hwmodel
, hw_major
, hw_minor
, fw_major
, fw_minor
);
1041 /* Check firmware version against what we downloaded. */
1043 if (priv
->firm_version
!= ((version
& 0xf0) << 4 | (version
& 0x0f))) {
1044 printk("Incorrect readback of firmware version %x.\n",
1050 /* Check that the tuner hardware model remains consistent over time. */
1051 if (priv
->hwmodel
== 0 && hwmodel
== 4000) {
1052 priv
->hwmodel
= hwmodel
;
1053 priv
->hwvers
= version
& 0xff00;
1054 } else if (priv
->hwmodel
== 0 || priv
->hwmodel
!= hwmodel
||
1055 priv
->hwvers
!= (version
& 0xff00)) {
1056 printk("Read invalid device hardware information - tuner "
1061 memcpy(&priv
->cur_fw
, &new_fw
, sizeof(priv
->cur_fw
));
1064 * By setting BASE in cur_fw.type only after successfully loading all
1065 * firmwares, we can:
1066 * 1. Identify that BASE firmware with type=0 has been loaded;
1067 * 2. Tell whether BASE firmware was just changed the next time through.
1069 priv
->cur_fw
.type
|= BASE
;
1074 memset(&priv
->cur_fw
, 0, sizeof(priv
->cur_fw
));
1078 dprintk(1, "Retrying firmware load\n");
1087 static void xc_debug_dump(struct xc4000_priv
*priv
)
1090 u32 freq_error_hz
= 0;
1092 u32 hsync_freq_hz
= 0;
1095 u8 hw_majorversion
= 0, hw_minorversion
= 0;
1096 u8 fw_majorversion
= 0, fw_minorversion
= 0;
1098 xc_get_ADC_Envelope(priv
, &adc_envelope
);
1099 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope
);
1101 xc_get_frequency_error(priv
, &freq_error_hz
);
1102 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz
);
1104 xc_get_lock_status(priv
, &lock_status
);
1105 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1108 xc_get_version(priv
, &hw_majorversion
, &hw_minorversion
,
1109 &fw_majorversion
, &fw_minorversion
);
1110 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1111 hw_majorversion
, hw_minorversion
,
1112 fw_majorversion
, fw_minorversion
);
1114 if (priv
->video_standard
< XC4000_DTV6
) {
1115 xc_get_hsync_freq(priv
, &hsync_freq_hz
);
1116 dprintk(1, "*** Horizontal sync frequency = %d Hz\n",
1119 xc_get_frame_lines(priv
, &frame_lines
);
1120 dprintk(1, "*** Frame lines = %d\n", frame_lines
);
1123 xc_get_quality(priv
, &quality
);
1124 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality
);
1127 static int xc4000_set_params(struct dvb_frontend
*fe
,
1128 struct dvb_frontend_parameters
*params
)
1130 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1132 int ret
= -EREMOTEIO
;
1134 dprintk(1, "%s() frequency=%d (Hz)\n", __func__
, params
->frequency
);
1136 mutex_lock(&priv
->lock
);
1138 if (fe
->ops
.info
.type
== FE_ATSC
) {
1139 dprintk(1, "%s() ATSC\n", __func__
);
1140 switch (params
->u
.vsb
.modulation
) {
1143 dprintk(1, "%s() VSB modulation\n", __func__
);
1144 priv
->rf_mode
= XC_RF_MODE_AIR
;
1145 priv
->freq_hz
= params
->frequency
- 1750000;
1146 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
1147 priv
->video_standard
= XC4000_DTV6
;
1153 dprintk(1, "%s() QAM modulation\n", __func__
);
1154 priv
->rf_mode
= XC_RF_MODE_CABLE
;
1155 priv
->freq_hz
= params
->frequency
- 1750000;
1156 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
1157 priv
->video_standard
= XC4000_DTV6
;
1164 } else if (fe
->ops
.info
.type
== FE_OFDM
) {
1165 dprintk(1, "%s() OFDM\n", __func__
);
1166 switch (params
->u
.ofdm
.bandwidth
) {
1167 case BANDWIDTH_6_MHZ
:
1168 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
1169 priv
->video_standard
= XC4000_DTV6
;
1170 priv
->freq_hz
= params
->frequency
- 1750000;
1173 case BANDWIDTH_7_MHZ
:
1174 priv
->bandwidth
= BANDWIDTH_7_MHZ
;
1175 priv
->video_standard
= XC4000_DTV7
;
1176 priv
->freq_hz
= params
->frequency
- 2250000;
1179 case BANDWIDTH_8_MHZ
:
1180 priv
->bandwidth
= BANDWIDTH_8_MHZ
;
1181 priv
->video_standard
= XC4000_DTV8
;
1182 priv
->freq_hz
= params
->frequency
- 2750000;
1185 case BANDWIDTH_AUTO
:
1186 if (params
->frequency
< 400000000) {
1187 priv
->bandwidth
= BANDWIDTH_7_MHZ
;
1188 priv
->freq_hz
= params
->frequency
- 2250000;
1190 priv
->bandwidth
= BANDWIDTH_8_MHZ
;
1191 priv
->freq_hz
= params
->frequency
- 2750000;
1193 priv
->video_standard
= XC4000_DTV7_8
;
1197 printk(KERN_ERR
"xc4000 bandwidth not set!\n");
1201 priv
->rf_mode
= XC_RF_MODE_AIR
;
1203 printk(KERN_ERR
"xc4000 modulation type not supported!\n");
1208 dprintk(1, "%s() frequency=%d (compensated)\n",
1209 __func__
, priv
->freq_hz
);
1211 /* Make sure the correct firmware type is loaded */
1212 if (check_firmware(fe
, type
, 0, priv
->if_khz
) != XC_RESULT_SUCCESS
)
1215 ret
= xc_SetSignalSource(priv
, priv
->rf_mode
);
1216 if (ret
!= XC_RESULT_SUCCESS
) {
1218 "xc4000: xc_SetSignalSource(%d) failed\n",
1223 ret
= xc_SetTVStandard(priv
,
1224 XC4000_Standard
[priv
->video_standard
].VideoMode
,
1225 XC4000_Standard
[priv
->video_standard
].AudioMode
);
1226 if (ret
!= XC_RESULT_SUCCESS
) {
1227 printk(KERN_ERR
"xc4000: xc_SetTVStandard failed\n");
1230 xc_tune_channel(priv
, priv
->freq_hz
, XC_TUNE_DIGITAL
);
1235 mutex_unlock(&priv
->lock
);
1240 static int xc4000_set_analog_params(struct dvb_frontend
*fe
,
1241 struct analog_parameters
*params
)
1243 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1244 int ret
= -EREMOTEIO
;
1246 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1247 __func__
, params
->frequency
);
1249 mutex_lock(&priv
->lock
);
1251 /* Fix me: it could be air. */
1252 priv
->rf_mode
= params
->mode
;
1253 if (params
->mode
> XC_RF_MODE_CABLE
)
1254 priv
->rf_mode
= XC_RF_MODE_CABLE
;
1256 /* params->frequency is in units of 62.5khz */
1257 priv
->freq_hz
= params
->frequency
* 62500;
1259 /* FIX ME: Some video standards may have several possible audio
1260 standards. We simply default to one of them here.
1262 if (params
->std
& V4L2_STD_MN
) {
1263 /* default to BTSC audio standard */
1264 priv
->video_standard
= XC4000_MN_NTSC_PAL_BTSC
;
1268 if (params
->std
& V4L2_STD_PAL_BG
) {
1269 /* default to NICAM audio standard */
1270 priv
->video_standard
= XC4000_BG_PAL_NICAM
;
1274 if (params
->std
& V4L2_STD_PAL_I
) {
1275 /* default to NICAM audio standard */
1276 priv
->video_standard
= XC4000_I_PAL_NICAM
;
1280 if (params
->std
& V4L2_STD_PAL_DK
) {
1281 /* default to NICAM audio standard */
1282 priv
->video_standard
= XC4000_DK_PAL_NICAM
;
1286 if (params
->std
& V4L2_STD_SECAM_DK
) {
1287 /* default to A2 DK1 audio standard */
1288 priv
->video_standard
= XC4000_DK_SECAM_A2DK1
;
1292 if (params
->std
& V4L2_STD_SECAM_L
) {
1293 priv
->video_standard
= XC4000_L_SECAM_NICAM
;
1297 if (params
->std
& V4L2_STD_SECAM_LC
) {
1298 priv
->video_standard
= XC4000_LC_SECAM_NICAM
;
1304 /* FIXME - firmware type not being set properly */
1305 if (check_firmware(fe
, DTV8
, 0, priv
->if_khz
) != XC_RESULT_SUCCESS
)
1308 ret
= xc_SetSignalSource(priv
, priv
->rf_mode
);
1309 if (ret
!= XC_RESULT_SUCCESS
) {
1311 "xc4000: xc_SetSignalSource(%d) failed\n",
1316 ret
= xc_SetTVStandard(priv
,
1317 XC4000_Standard
[priv
->video_standard
].VideoMode
,
1318 XC4000_Standard
[priv
->video_standard
].AudioMode
);
1319 if (ret
!= XC_RESULT_SUCCESS
) {
1320 printk(KERN_ERR
"xc4000: xc_SetTVStandard failed\n");
1324 xc_tune_channel(priv
, priv
->freq_hz
, XC_TUNE_ANALOG
);
1329 mutex_unlock(&priv
->lock
);
1334 static int xc4000_get_frequency(struct dvb_frontend
*fe
, u32
*freq
)
1336 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1338 *freq
= priv
->freq_hz
;
1341 mutex_lock(&priv
->lock
);
1342 if ((priv
->cur_fw
.type
1343 & (BASE
| FM
| DTV6
| DTV7
| DTV78
| DTV8
)) == BASE
) {
1345 if (xc4000_readreg(priv
, XREG_SNR
, &snr
) == 0) {
1346 mutex_unlock(&priv
->lock
);
1347 dprintk(1, "%s() freq = %u, SNR = %d\n",
1348 __func__
, *freq
, snr
);
1352 mutex_unlock(&priv
->lock
);
1355 dprintk(1, "%s()\n", __func__
);
1360 static int xc4000_get_bandwidth(struct dvb_frontend
*fe
, u32
*bw
)
1362 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1363 dprintk(1, "%s()\n", __func__
);
1365 *bw
= priv
->bandwidth
;
1369 static int xc4000_get_status(struct dvb_frontend
*fe
, u32
*status
)
1371 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1372 u16 lock_status
= 0;
1374 mutex_lock(&priv
->lock
);
1376 if (priv
->cur_fw
.type
& BASE
)
1377 xc_get_lock_status(priv
, &lock_status
);
1379 *status
= (lock_status
== 1 ?
1380 TUNER_STATUS_LOCKED
| TUNER_STATUS_STEREO
: 0);
1381 if (priv
->cur_fw
.type
& (DTV6
| DTV7
| DTV78
| DTV8
))
1382 *status
&= (~TUNER_STATUS_STEREO
);
1384 mutex_unlock(&priv
->lock
);
1386 dprintk(2, "%s() lock_status = %d\n", __func__
, lock_status
);
1391 static int xc4000_sleep(struct dvb_frontend
*fe
)
1393 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1394 int ret
= XC_RESULT_SUCCESS
;
1396 dprintk(1, "%s()\n", __func__
);
1398 mutex_lock(&priv
->lock
);
1400 /* Avoid firmware reload on slow devices */
1401 if ((no_poweroff
== 2 ||
1402 (no_poweroff
== 0 &&
1403 priv
->card_type
!= XC4000_CARD_WINFAST_CX88
)) &&
1404 (priv
->cur_fw
.type
& BASE
) != 0) {
1405 /* force reset and firmware reload */
1406 priv
->cur_fw
.type
= XC_POWERED_DOWN
;
1408 if (xc_write_reg(priv
, XREG_POWER_DOWN
, 0)
1409 != XC_RESULT_SUCCESS
) {
1411 "xc4000: %s() unable to shutdown tuner\n",
1418 mutex_unlock(&priv
->lock
);
1423 static int xc4000_init(struct dvb_frontend
*fe
)
1425 dprintk(1, "%s()\n", __func__
);
1430 static int xc4000_release(struct dvb_frontend
*fe
)
1432 struct xc4000_priv
*priv
= fe
->tuner_priv
;
1434 dprintk(1, "%s()\n", __func__
);
1436 mutex_lock(&xc4000_list_mutex
);
1439 hybrid_tuner_release_state(priv
);
1441 mutex_unlock(&xc4000_list_mutex
);
1443 fe
->tuner_priv
= NULL
;
1448 static const struct dvb_tuner_ops xc4000_tuner_ops
= {
1450 .name
= "Xceive XC4000",
1451 .frequency_min
= 1000000,
1452 .frequency_max
= 1023000000,
1453 .frequency_step
= 50000,
1456 .release
= xc4000_release
,
1457 .init
= xc4000_init
,
1458 .sleep
= xc4000_sleep
,
1460 .set_params
= xc4000_set_params
,
1461 .set_analog_params
= xc4000_set_analog_params
,
1462 .get_frequency
= xc4000_get_frequency
,
1463 .get_bandwidth
= xc4000_get_bandwidth
,
1464 .get_status
= xc4000_get_status
1467 struct dvb_frontend
*xc4000_attach(struct dvb_frontend
*fe
,
1468 struct i2c_adapter
*i2c
,
1469 struct xc4000_config
*cfg
)
1471 struct xc4000_priv
*priv
= NULL
;
1475 if (cfg
->card_type
!= XC4000_CARD_GENERIC
) {
1476 if (cfg
->card_type
== XC4000_CARD_WINFAST_CX88
) {
1477 cfg
->i2c_address
= 0x61;
1479 } else { /* default to PCTV 340E */
1480 cfg
->i2c_address
= 0x61;
1485 dprintk(1, "%s(%d-%04x)\n", __func__
,
1486 i2c
? i2c_adapter_id(i2c
) : -1,
1487 cfg
? cfg
->i2c_address
: -1);
1489 mutex_lock(&xc4000_list_mutex
);
1491 instance
= hybrid_tuner_request_state(struct xc4000_priv
, priv
,
1492 hybrid_tuner_instance_list
,
1493 i2c
, cfg
->i2c_address
, "xc4000");
1494 if (cfg
->card_type
!= XC4000_CARD_GENERIC
)
1495 priv
->card_type
= cfg
->card_type
;
1501 /* new tuner instance */
1502 priv
->bandwidth
= BANDWIDTH_6_MHZ
;
1503 mutex_init(&priv
->lock
);
1504 fe
->tuner_priv
= priv
;
1507 /* existing tuner instance */
1508 fe
->tuner_priv
= priv
;
1512 if (cfg
->if_khz
!= 0) {
1513 /* If the IF hasn't been set yet, use the value provided by
1514 the caller (occurs in hybrid devices where the analog
1515 call to xc4000_attach occurs before the digital side) */
1516 priv
->if_khz
= cfg
->if_khz
;
1519 /* Check if firmware has been loaded. It is possible that another
1520 instance of the driver has loaded the firmware.
1523 if (instance
== 1) {
1524 if (xc4000_readreg(priv
, XREG_PRODUCT_ID
, &id
)
1525 != XC_RESULT_SUCCESS
)
1528 id
= ((priv
->cur_fw
.type
& BASE
) != 0 ?
1529 priv
->hwmodel
: XC_PRODUCT_ID_FW_NOT_LOADED
);
1533 case XC_PRODUCT_ID_FW_LOADED
:
1535 "xc4000: Successfully identified at address 0x%02x\n",
1538 "xc4000: Firmware has been loaded previously\n");
1540 case XC_PRODUCT_ID_FW_NOT_LOADED
:
1542 "xc4000: Successfully identified at address 0x%02x\n",
1545 "xc4000: Firmware has not been loaded previously\n");
1549 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1550 cfg
->i2c_address
, id
);
1554 mutex_unlock(&xc4000_list_mutex
);
1556 memcpy(&fe
->ops
.tuner_ops
, &xc4000_tuner_ops
,
1557 sizeof(struct dvb_tuner_ops
));
1559 if (instance
== 1) {
1561 mutex_lock(&priv
->lock
);
1562 ret
= xc4000_fwupload(fe
);
1563 mutex_unlock(&priv
->lock
);
1564 if (ret
!= XC_RESULT_SUCCESS
)
1570 mutex_unlock(&xc4000_list_mutex
);
1575 EXPORT_SYMBOL(xc4000_attach
);
1577 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1578 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1579 MODULE_LICENSE("GPL");