1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Xceive XC5000 "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>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/videodev2.h>
13 #include <linux/delay.h>
14 #include <linux/workqueue.h>
15 #include <linux/dvb/frontend.h>
16 #include <linux/i2c.h>
18 #include <media/dvb_frontend.h>
21 #include "tuner-i2c.h"
24 module_param(debug
, int, 0644);
25 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
27 static int no_poweroff
;
28 module_param(no_poweroff
, int, 0644);
29 MODULE_PARM_DESC(no_poweroff
, "0 (default) powers device off when not used.\n"
30 "\t\t1 keep device energized and with tuner ready all the times.\n"
31 "\t\tFaster, but consumes more power and keeps the device hotter");
33 static DEFINE_MUTEX(xc5000_list_mutex
);
34 static LIST_HEAD(hybrid_tuner_instance_list
);
36 #define dprintk(level, fmt, arg...) if (debug >= level) \
37 printk(KERN_INFO "%s: " fmt, "xc5000", ## arg)
40 struct tuner_i2c_props i2c_props
;
41 struct list_head hybrid_tuner_instance_list
;
45 u32 freq_hz
, freq_offset
;
55 u8 init_status_supported
;
56 u8 fw_checksum_supported
;
58 struct dvb_frontend
*fe
;
59 struct delayed_work timer_sleep
;
61 const struct firmware
*firmware
;
65 #define MAX_TV_STANDARD 24
66 #define XC_MAX_I2C_WRITE_LENGTH 64
68 /* Time to suspend after the .sleep callback is called */
69 #define XC5000_SLEEP_TIME 5000 /* ms */
72 #define XC_RF_MODE_AIR 0
73 #define XC_RF_MODE_CABLE 1
76 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
77 #define XC_PRODUCT_ID_FW_LOADED 0x1388
80 #define XREG_INIT 0x00
81 #define XREG_VIDEO_MODE 0x01
82 #define XREG_AUDIO_MODE 0x02
83 #define XREG_RF_FREQ 0x03
84 #define XREG_D_CODE 0x04
85 #define XREG_IF_OUT 0x05
86 #define XREG_SEEK_MODE 0x07
87 #define XREG_POWER_DOWN 0x0A /* Obsolete */
88 /* Set the output amplitude - SIF for analog, DTVP/DTVN for digital */
89 #define XREG_OUTPUT_AMP 0x0B
90 #define XREG_SIGNALSOURCE 0x0D /* 0=Air, 1=Cable */
91 #define XREG_SMOOTHEDCVBS 0x0E
92 #define XREG_XTALFREQ 0x0F
93 #define XREG_FINERFREQ 0x10
94 #define XREG_DDIMODE 0x11
96 #define XREG_ADC_ENV 0x00
97 #define XREG_QUALITY 0x01
98 #define XREG_FRAME_LINES 0x02
99 #define XREG_HSYNC_FREQ 0x03
100 #define XREG_LOCK 0x04
101 #define XREG_FREQ_ERROR 0x05
102 #define XREG_SNR 0x06
103 #define XREG_VERSION 0x07
104 #define XREG_PRODUCT_ID 0x08
105 #define XREG_BUSY 0x09
106 #define XREG_BUILD 0x0D
107 #define XREG_TOTALGAIN 0x0F
108 #define XREG_FW_CHECKSUM 0x12
109 #define XREG_INIT_STATUS 0x13
112 Basic firmware description. This will remain with
113 the driver for documentation purposes.
115 This represents an I2C firmware file encoded as a
116 string of unsigned char. Format is as follows:
118 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
119 char[1 ]=len0_LSB -> length of first write transaction
120 char[2 ]=data0 -> first byte to be sent
124 char[M ]=dataN -> last byte to be sent
125 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
126 char[M+2]=len1_LSB -> length of second write transaction
132 The [len] value should be interpreted as follows:
134 len= len_MSB _ len_LSB
135 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
136 len=0000_0000_0000_0000 : Reset command: Do hardware reset
137 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
138 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
140 For the RESET and WAIT commands, the two following bytes will contain
141 immediately the length of the following transaction.
144 struct XC_TV_STANDARD
{
150 /* Tuner standards */
151 #define MN_NTSC_PAL_BTSC 0
152 #define MN_NTSC_PAL_A2 1
153 #define MN_NTSC_PAL_EIAJ 2
154 #define MN_NTSC_PAL_MONO 3
156 #define BG_PAL_NICAM 5
157 #define BG_PAL_MONO 6
158 #define I_PAL_NICAM 7
159 #define I_PAL_NICAM_MONO 8
161 #define DK_PAL_NICAM 10
162 #define DK_PAL_MONO 11
163 #define DK_SECAM_A2DK1 12
164 #define DK_SECAM_A2LDK3 13
165 #define DK_SECAM_A2MONO 14
166 #define L_SECAM_NICAM 15
167 #define LC_SECAM_NICAM 16
172 #define FM_RADIO_INPUT2 21
173 #define FM_RADIO_INPUT1 22
174 #define FM_RADIO_INPUT1_MONO 23
176 static struct XC_TV_STANDARD xc5000_standard
[MAX_TV_STANDARD
] = {
177 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
178 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
179 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
180 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
181 {"B/G-PAL-A2", 0x0A00, 0x8049},
182 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
183 {"B/G-PAL-MONO", 0x0878, 0x8059},
184 {"I-PAL-NICAM", 0x1080, 0x8009},
185 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
186 {"D/K-PAL-A2", 0x1600, 0x8009},
187 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
188 {"D/K-PAL-MONO", 0x1478, 0x8009},
189 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
190 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
191 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
192 {"L-SECAM-NICAM", 0x8E82, 0x0009},
193 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
194 {"DTV6", 0x00C0, 0x8002},
195 {"DTV8", 0x00C0, 0x800B},
196 {"DTV7/8", 0x00C0, 0x801B},
197 {"DTV7", 0x00C0, 0x8007},
198 {"FM Radio-INPUT2", 0x9802, 0x9002},
199 {"FM Radio-INPUT1", 0x0208, 0x9002},
200 {"FM Radio-INPUT1_MONO", 0x0278, 0x9002}
204 struct xc5000_fw_cfg
{
208 u8 init_status_supported
;
209 u8 fw_checksum_supported
;
212 #define XC5000A_FIRMWARE "dvb-fe-xc5000-1.6.114.fw"
213 static const struct xc5000_fw_cfg xc5000a_1_6_114
= {
214 .name
= XC5000A_FIRMWARE
,
219 #define XC5000C_FIRMWARE "dvb-fe-xc5000c-4.1.30.7.fw"
220 static const struct xc5000_fw_cfg xc5000c_41_024_5
= {
221 .name
= XC5000C_FIRMWARE
,
224 .init_status_supported
= 1,
225 .fw_checksum_supported
= 1,
228 static inline const struct xc5000_fw_cfg
*xc5000_assign_firmware(int chip_id
)
233 return &xc5000a_1_6_114
;
235 return &xc5000c_41_024_5
;
239 static int xc_load_fw_and_init_tuner(struct dvb_frontend
*fe
, int force
);
240 static int xc5000_is_firmware_loaded(struct dvb_frontend
*fe
);
241 static int xc5000_readreg(struct xc5000_priv
*priv
, u16 reg
, u16
*val
);
242 static int xc5000_tuner_reset(struct dvb_frontend
*fe
);
244 static int xc_send_i2c_data(struct xc5000_priv
*priv
, u8
*buf
, int len
)
246 struct i2c_msg msg
= { .addr
= priv
->i2c_props
.addr
,
247 .flags
= 0, .buf
= buf
, .len
= len
};
249 if (i2c_transfer(priv
->i2c_props
.adap
, &msg
, 1) != 1) {
250 printk(KERN_ERR
"xc5000: I2C write failed (len=%i)\n", len
);
257 /* This routine is never used because the only time we read data from the
258 i2c bus is when we read registers, and we want that to be an atomic i2c
259 transaction in case we are on a multi-master bus */
260 static int xc_read_i2c_data(struct xc5000_priv
*priv
, u8
*buf
, int len
)
262 struct i2c_msg msg
= { .addr
= priv
->i2c_props
.addr
,
263 .flags
= I2C_M_RD
, .buf
= buf
, .len
= len
};
265 if (i2c_transfer(priv
->i2c_props
.adap
, &msg
, 1) != 1) {
266 printk(KERN_ERR
"xc5000 I2C read failed (len=%i)\n", len
);
273 static int xc5000_readreg(struct xc5000_priv
*priv
, u16 reg
, u16
*val
)
275 u8 buf
[2] = { reg
>> 8, reg
& 0xff };
276 u8 bval
[2] = { 0, 0 };
277 struct i2c_msg msg
[2] = {
278 { .addr
= priv
->i2c_props
.addr
,
279 .flags
= 0, .buf
= &buf
[0], .len
= 2 },
280 { .addr
= priv
->i2c_props
.addr
,
281 .flags
= I2C_M_RD
, .buf
= &bval
[0], .len
= 2 },
284 if (i2c_transfer(priv
->i2c_props
.adap
, msg
, 2) != 2) {
285 printk(KERN_WARNING
"xc5000: I2C read failed\n");
289 *val
= (bval
[0] << 8) | bval
[1];
293 static int xc5000_tuner_reset(struct dvb_frontend
*fe
)
295 struct xc5000_priv
*priv
= fe
->tuner_priv
;
298 dprintk(1, "%s()\n", __func__
);
301 ret
= fe
->callback(((fe
->dvb
) && (fe
->dvb
->priv
)) ?
303 priv
->i2c_props
.adap
->algo_data
,
304 DVB_FRONTEND_COMPONENT_TUNER
,
305 XC5000_TUNER_RESET
, 0);
307 printk(KERN_ERR
"xc5000: reset failed\n");
311 printk(KERN_ERR
"xc5000: no tuner reset callback function, fatal\n");
317 static int xc_write_reg(struct xc5000_priv
*priv
, u16 reg_addr
, u16 i2c_data
)
320 int watch_dog_timer
= 100;
323 buf
[0] = (reg_addr
>> 8) & 0xFF;
324 buf
[1] = reg_addr
& 0xFF;
325 buf
[2] = (i2c_data
>> 8) & 0xFF;
326 buf
[3] = i2c_data
& 0xFF;
327 result
= xc_send_i2c_data(priv
, buf
, 4);
329 /* wait for busy flag to clear */
330 while ((watch_dog_timer
> 0) && (result
== 0)) {
331 result
= xc5000_readreg(priv
, XREG_BUSY
, (u16
*)buf
);
333 if ((buf
[0] == 0) && (buf
[1] == 0)) {
334 /* busy flag cleared */
337 msleep(5); /* wait 5 ms */
343 if (watch_dog_timer
<= 0)
349 static int xc_load_i2c_sequence(struct dvb_frontend
*fe
, const u8
*i2c_sequence
)
351 struct xc5000_priv
*priv
= fe
->tuner_priv
;
353 int i
, nbytes_to_send
, result
;
354 unsigned int len
, pos
, index
;
355 u8 buf
[XC_MAX_I2C_WRITE_LENGTH
];
358 while ((i2c_sequence
[index
] != 0xFF) ||
359 (i2c_sequence
[index
+ 1] != 0xFF)) {
360 len
= i2c_sequence
[index
] * 256 + i2c_sequence
[index
+1];
363 result
= xc5000_tuner_reset(fe
);
367 } else if (len
& 0x8000) {
369 msleep(len
& 0x7FFF);
372 /* Send i2c data whilst ensuring individual transactions
373 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
376 buf
[0] = i2c_sequence
[index
];
377 buf
[1] = i2c_sequence
[index
+ 1];
380 if ((len
- pos
) > XC_MAX_I2C_WRITE_LENGTH
- 2)
382 XC_MAX_I2C_WRITE_LENGTH
;
384 nbytes_to_send
= (len
- pos
+ 2);
385 for (i
= 2; i
< nbytes_to_send
; i
++) {
386 buf
[i
] = i2c_sequence
[index
+ pos
+
389 result
= xc_send_i2c_data(priv
, buf
,
395 pos
+= nbytes_to_send
- 2;
403 static int xc_initialize(struct xc5000_priv
*priv
)
405 dprintk(1, "%s()\n", __func__
);
406 return xc_write_reg(priv
, XREG_INIT
, 0);
409 static int xc_set_tv_standard(struct xc5000_priv
*priv
,
410 u16 video_mode
, u16 audio_mode
, u8 radio_mode
)
413 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__
, video_mode
, audio_mode
);
415 dprintk(1, "%s() Standard = %s\n",
417 xc5000_standard
[radio_mode
].name
);
419 dprintk(1, "%s() Standard = %s\n",
421 xc5000_standard
[priv
->video_standard
].name
);
424 ret
= xc_write_reg(priv
, XREG_VIDEO_MODE
, video_mode
);
426 ret
= xc_write_reg(priv
, XREG_AUDIO_MODE
, audio_mode
);
431 static int xc_set_signal_source(struct xc5000_priv
*priv
, u16 rf_mode
)
433 dprintk(1, "%s(%d) Source = %s\n", __func__
, rf_mode
,
434 rf_mode
== XC_RF_MODE_AIR
? "ANTENNA" : "CABLE");
436 if ((rf_mode
!= XC_RF_MODE_AIR
) && (rf_mode
!= XC_RF_MODE_CABLE
)) {
437 rf_mode
= XC_RF_MODE_CABLE
;
439 "%s(), Invalid mode, defaulting to CABLE",
442 return xc_write_reg(priv
, XREG_SIGNALSOURCE
, rf_mode
);
445 static const struct dvb_tuner_ops xc5000_tuner_ops
;
447 static int xc_set_rf_frequency(struct xc5000_priv
*priv
, u32 freq_hz
)
451 dprintk(1, "%s(%u)\n", __func__
, freq_hz
);
453 if ((freq_hz
> xc5000_tuner_ops
.info
.frequency_max_hz
) ||
454 (freq_hz
< xc5000_tuner_ops
.info
.frequency_min_hz
))
457 freq_code
= (u16
)(freq_hz
/ 15625);
459 /* Starting in firmware version 1.1.44, Xceive recommends using the
460 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
461 only be used for fast scanning for channel lock) */
462 return xc_write_reg(priv
, XREG_FINERFREQ
, freq_code
);
466 static int xc_set_IF_frequency(struct xc5000_priv
*priv
, u32 freq_khz
)
468 u32 freq_code
= (freq_khz
* 1024)/1000;
469 dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
470 __func__
, freq_khz
, freq_code
);
472 return xc_write_reg(priv
, XREG_IF_OUT
, freq_code
);
476 static int xc_get_adc_envelope(struct xc5000_priv
*priv
, u16
*adc_envelope
)
478 return xc5000_readreg(priv
, XREG_ADC_ENV
, adc_envelope
);
481 static int xc_get_frequency_error(struct xc5000_priv
*priv
, u32
*freq_error_hz
)
487 result
= xc5000_readreg(priv
, XREG_FREQ_ERROR
, ®_data
);
492 (*freq_error_hz
) = (tmp
* 15625) / 1000;
496 static int xc_get_lock_status(struct xc5000_priv
*priv
, u16
*lock_status
)
498 return xc5000_readreg(priv
, XREG_LOCK
, lock_status
);
501 static int xc_get_version(struct xc5000_priv
*priv
,
502 u8
*hw_majorversion
, u8
*hw_minorversion
,
503 u8
*fw_majorversion
, u8
*fw_minorversion
)
508 result
= xc5000_readreg(priv
, XREG_VERSION
, &data
);
512 (*hw_majorversion
) = (data
>> 12) & 0x0F;
513 (*hw_minorversion
) = (data
>> 8) & 0x0F;
514 (*fw_majorversion
) = (data
>> 4) & 0x0F;
515 (*fw_minorversion
) = data
& 0x0F;
520 static int xc_get_buildversion(struct xc5000_priv
*priv
, u16
*buildrev
)
522 return xc5000_readreg(priv
, XREG_BUILD
, buildrev
);
525 static int xc_get_hsync_freq(struct xc5000_priv
*priv
, u32
*hsync_freq_hz
)
530 result
= xc5000_readreg(priv
, XREG_HSYNC_FREQ
, ®_data
);
534 (*hsync_freq_hz
) = ((reg_data
& 0x0fff) * 763)/100;
538 static int xc_get_frame_lines(struct xc5000_priv
*priv
, u16
*frame_lines
)
540 return xc5000_readreg(priv
, XREG_FRAME_LINES
, frame_lines
);
543 static int xc_get_quality(struct xc5000_priv
*priv
, u16
*quality
)
545 return xc5000_readreg(priv
, XREG_QUALITY
, quality
);
548 static int xc_get_analogsnr(struct xc5000_priv
*priv
, u16
*snr
)
550 return xc5000_readreg(priv
, XREG_SNR
, snr
);
553 static int xc_get_totalgain(struct xc5000_priv
*priv
, u16
*totalgain
)
555 return xc5000_readreg(priv
, XREG_TOTALGAIN
, totalgain
);
558 #define XC_TUNE_ANALOG 0
559 #define XC_TUNE_DIGITAL 1
560 static int xc_tune_channel(struct xc5000_priv
*priv
, u32 freq_hz
, int mode
)
562 dprintk(1, "%s(%u)\n", __func__
, freq_hz
);
564 if (xc_set_rf_frequency(priv
, freq_hz
) != 0)
570 static int xc_set_xtal(struct dvb_frontend
*fe
)
572 struct xc5000_priv
*priv
= fe
->tuner_priv
;
575 switch (priv
->chip_id
) {
578 /* 32.000 MHz xtal is default */
581 switch (priv
->xtal_khz
) {
584 /* 32.000 MHz xtal is default */
587 /* 31.875 MHz xtal configuration */
588 ret
= xc_write_reg(priv
, 0x000f, 0x8081);
596 static int xc5000_fwupload(struct dvb_frontend
*fe
,
597 const struct xc5000_fw_cfg
*desired_fw
,
598 const struct firmware
*fw
)
600 struct xc5000_priv
*priv
= fe
->tuner_priv
;
603 /* request the firmware, this will block and timeout */
604 dprintk(1, "waiting for firmware upload (%s)...\n",
607 priv
->pll_register_no
= desired_fw
->pll_reg
;
608 priv
->init_status_supported
= desired_fw
->init_status_supported
;
609 priv
->fw_checksum_supported
= desired_fw
->fw_checksum_supported
;
612 dprintk(1, "firmware uploading...\n");
613 ret
= xc_load_i2c_sequence(fe
, fw
->data
);
615 ret
= xc_set_xtal(fe
);
616 dprintk(1, "Firmware upload complete...\n");
618 printk(KERN_ERR
"xc5000: firmware upload failed...\n");
623 static void xc_debug_dump(struct xc5000_priv
*priv
)
626 u32 freq_error_hz
= 0;
628 u32 hsync_freq_hz
= 0;
633 u8 hw_majorversion
= 0, hw_minorversion
= 0;
634 u8 fw_majorversion
= 0, fw_minorversion
= 0;
635 u16 fw_buildversion
= 0;
638 /* Wait for stats to stabilize.
639 * Frame Lines needs two frame times after initial lock
640 * before it is valid.
644 xc_get_adc_envelope(priv
, &adc_envelope
);
645 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope
);
647 xc_get_frequency_error(priv
, &freq_error_hz
);
648 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz
);
650 xc_get_lock_status(priv
, &lock_status
);
651 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
654 xc_get_version(priv
, &hw_majorversion
, &hw_minorversion
,
655 &fw_majorversion
, &fw_minorversion
);
656 xc_get_buildversion(priv
, &fw_buildversion
);
657 dprintk(1, "*** HW: V%d.%d, FW: V %d.%d.%d\n",
658 hw_majorversion
, hw_minorversion
,
659 fw_majorversion
, fw_minorversion
, fw_buildversion
);
661 xc_get_hsync_freq(priv
, &hsync_freq_hz
);
662 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz
);
664 xc_get_frame_lines(priv
, &frame_lines
);
665 dprintk(1, "*** Frame lines = %d\n", frame_lines
);
667 xc_get_quality(priv
, &quality
);
668 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality
& 0x07);
670 xc_get_analogsnr(priv
, &snr
);
671 dprintk(1, "*** Unweighted analog SNR = %d dB\n", snr
& 0x3f);
673 xc_get_totalgain(priv
, &totalgain
);
674 dprintk(1, "*** Total gain = %d.%d dB\n", totalgain
/ 256,
675 (totalgain
% 256) * 100 / 256);
677 if (priv
->pll_register_no
) {
678 if (!xc5000_readreg(priv
, priv
->pll_register_no
, ®val
))
679 dprintk(1, "*** PLL lock status = 0x%04x\n", regval
);
683 static int xc5000_tune_digital(struct dvb_frontend
*fe
)
685 struct xc5000_priv
*priv
= fe
->tuner_priv
;
687 u32 bw
= fe
->dtv_property_cache
.bandwidth_hz
;
689 ret
= xc_set_signal_source(priv
, priv
->rf_mode
);
692 "xc5000: xc_set_signal_source(%d) failed\n",
697 ret
= xc_set_tv_standard(priv
,
698 xc5000_standard
[priv
->video_standard
].video_mode
,
699 xc5000_standard
[priv
->video_standard
].audio_mode
, 0);
701 printk(KERN_ERR
"xc5000: xc_set_tv_standard failed\n");
705 ret
= xc_set_IF_frequency(priv
, priv
->if_khz
);
707 printk(KERN_ERR
"xc5000: xc_Set_IF_frequency(%d) failed\n",
712 dprintk(1, "%s() setting OUTPUT_AMP to 0x%x\n",
713 __func__
, priv
->output_amp
);
714 xc_write_reg(priv
, XREG_OUTPUT_AMP
, priv
->output_amp
);
716 xc_tune_channel(priv
, priv
->freq_hz
, XC_TUNE_DIGITAL
);
721 priv
->bandwidth
= bw
;
726 static int xc5000_set_digital_params(struct dvb_frontend
*fe
)
729 struct xc5000_priv
*priv
= fe
->tuner_priv
;
730 u32 bw
= fe
->dtv_property_cache
.bandwidth_hz
;
731 u32 freq
= fe
->dtv_property_cache
.frequency
;
732 u32 delsys
= fe
->dtv_property_cache
.delivery_system
;
734 if (xc_load_fw_and_init_tuner(fe
, 0) != 0) {
735 dprintk(1, "Unable to load firmware and init tuner\n");
739 dprintk(1, "%s() frequency=%d (Hz)\n", __func__
, freq
);
743 dprintk(1, "%s() VSB modulation\n", __func__
);
744 priv
->rf_mode
= XC_RF_MODE_AIR
;
745 priv
->freq_offset
= 1750000;
746 priv
->video_standard
= DTV6
;
748 case SYS_DVBC_ANNEX_B
:
749 dprintk(1, "%s() QAM modulation\n", __func__
);
750 priv
->rf_mode
= XC_RF_MODE_CABLE
;
751 priv
->freq_offset
= 1750000;
752 priv
->video_standard
= DTV6
;
755 /* All ISDB-T are currently for 6 MHz bw */
758 /* fall to OFDM handling */
763 dprintk(1, "%s() OFDM\n", __func__
);
766 priv
->video_standard
= DTV6
;
767 priv
->freq_offset
= 1750000;
770 priv
->video_standard
= DTV7
;
771 priv
->freq_offset
= 2250000;
774 priv
->video_standard
= DTV8
;
775 priv
->freq_offset
= 2750000;
778 printk(KERN_ERR
"xc5000 bandwidth not set!\n");
781 priv
->rf_mode
= XC_RF_MODE_AIR
;
783 case SYS_DVBC_ANNEX_A
:
784 case SYS_DVBC_ANNEX_C
:
785 dprintk(1, "%s() QAM modulation\n", __func__
);
786 priv
->rf_mode
= XC_RF_MODE_CABLE
;
788 priv
->video_standard
= DTV6
;
789 priv
->freq_offset
= 1750000;
791 } else if (bw
<= 7000000) {
792 priv
->video_standard
= DTV7
;
793 priv
->freq_offset
= 2250000;
796 priv
->video_standard
= DTV7_8
;
797 priv
->freq_offset
= 2750000;
800 dprintk(1, "%s() Bandwidth %dMHz (%d)\n", __func__
,
804 printk(KERN_ERR
"xc5000: delivery system is not supported!\n");
808 priv
->freq_hz
= freq
- priv
->freq_offset
;
809 priv
->mode
= V4L2_TUNER_DIGITAL_TV
;
811 dprintk(1, "%s() frequency=%d (compensated to %d)\n",
812 __func__
, freq
, priv
->freq_hz
);
814 return xc5000_tune_digital(fe
);
817 static int xc5000_is_firmware_loaded(struct dvb_frontend
*fe
)
819 struct xc5000_priv
*priv
= fe
->tuner_priv
;
823 ret
= xc5000_readreg(priv
, XREG_PRODUCT_ID
, &id
);
825 if (id
== XC_PRODUCT_ID_FW_NOT_LOADED
)
829 dprintk(1, "%s() returns id = 0x%x\n", __func__
, id
);
831 dprintk(1, "%s() returns error %d\n", __func__
, ret
);
837 static void xc5000_config_tv(struct dvb_frontend
*fe
,
838 struct analog_parameters
*params
)
840 struct xc5000_priv
*priv
= fe
->tuner_priv
;
842 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
843 __func__
, params
->frequency
);
845 /* Fix me: it could be air. */
846 priv
->rf_mode
= params
->mode
;
847 if (params
->mode
> XC_RF_MODE_CABLE
)
848 priv
->rf_mode
= XC_RF_MODE_CABLE
;
850 /* params->frequency is in units of 62.5khz */
851 priv
->freq_hz
= params
->frequency
* 62500;
853 /* FIX ME: Some video standards may have several possible audio
854 standards. We simply default to one of them here.
856 if (params
->std
& V4L2_STD_MN
) {
857 /* default to BTSC audio standard */
858 priv
->video_standard
= MN_NTSC_PAL_BTSC
;
862 if (params
->std
& V4L2_STD_PAL_BG
) {
863 /* default to NICAM audio standard */
864 priv
->video_standard
= BG_PAL_NICAM
;
868 if (params
->std
& V4L2_STD_PAL_I
) {
869 /* default to NICAM audio standard */
870 priv
->video_standard
= I_PAL_NICAM
;
874 if (params
->std
& V4L2_STD_PAL_DK
) {
875 /* default to NICAM audio standard */
876 priv
->video_standard
= DK_PAL_NICAM
;
880 if (params
->std
& V4L2_STD_SECAM_DK
) {
881 /* default to A2 DK1 audio standard */
882 priv
->video_standard
= DK_SECAM_A2DK1
;
886 if (params
->std
& V4L2_STD_SECAM_L
) {
887 priv
->video_standard
= L_SECAM_NICAM
;
891 if (params
->std
& V4L2_STD_SECAM_LC
) {
892 priv
->video_standard
= LC_SECAM_NICAM
;
897 static int xc5000_set_tv_freq(struct dvb_frontend
*fe
)
899 struct xc5000_priv
*priv
= fe
->tuner_priv
;
904 ret
= xc_set_signal_source(priv
, priv
->rf_mode
);
907 "xc5000: xc_set_signal_source(%d) failed\n",
912 ret
= xc_set_tv_standard(priv
,
913 xc5000_standard
[priv
->video_standard
].video_mode
,
914 xc5000_standard
[priv
->video_standard
].audio_mode
, 0);
916 printk(KERN_ERR
"xc5000: xc_set_tv_standard failed\n");
920 xc_write_reg(priv
, XREG_OUTPUT_AMP
, 0x09);
922 xc_tune_channel(priv
, priv
->freq_hz
, XC_TUNE_ANALOG
);
927 if (priv
->pll_register_no
!= 0) {
929 ret
= xc5000_readreg(priv
, priv
->pll_register_no
,
933 if (pll_lock_status
> 63) {
934 /* PLL is unlocked, force reload of the firmware */
935 dprintk(1, "xc5000: PLL not locked (0x%x). Reloading...\n",
937 if (xc_load_fw_and_init_tuner(fe
, 1) != 0) {
938 printk(KERN_ERR
"xc5000: Unable to reload fw\n");
948 static int xc5000_config_radio(struct dvb_frontend
*fe
,
949 struct analog_parameters
*params
)
952 struct xc5000_priv
*priv
= fe
->tuner_priv
;
954 dprintk(1, "%s() frequency=%d (in units of khz)\n",
955 __func__
, params
->frequency
);
957 if (priv
->radio_input
== XC5000_RADIO_NOT_CONFIGURED
) {
958 dprintk(1, "%s() radio input not configured\n", __func__
);
962 priv
->freq_hz
= params
->frequency
* 125 / 2;
963 priv
->rf_mode
= XC_RF_MODE_AIR
;
968 static int xc5000_set_radio_freq(struct dvb_frontend
*fe
)
970 struct xc5000_priv
*priv
= fe
->tuner_priv
;
974 if (priv
->radio_input
== XC5000_RADIO_FM1
)
975 radio_input
= FM_RADIO_INPUT1
;
976 else if (priv
->radio_input
== XC5000_RADIO_FM2
)
977 radio_input
= FM_RADIO_INPUT2
;
978 else if (priv
->radio_input
== XC5000_RADIO_FM1_MONO
)
979 radio_input
= FM_RADIO_INPUT1_MONO
;
981 dprintk(1, "%s() unknown radio input %d\n", __func__
,
986 ret
= xc_set_tv_standard(priv
, xc5000_standard
[radio_input
].video_mode
,
987 xc5000_standard
[radio_input
].audio_mode
, radio_input
);
990 printk(KERN_ERR
"xc5000: xc_set_tv_standard failed\n");
994 ret
= xc_set_signal_source(priv
, priv
->rf_mode
);
997 "xc5000: xc_set_signal_source(%d) failed\n",
1002 if ((priv
->radio_input
== XC5000_RADIO_FM1
) ||
1003 (priv
->radio_input
== XC5000_RADIO_FM2
))
1004 xc_write_reg(priv
, XREG_OUTPUT_AMP
, 0x09);
1005 else if (priv
->radio_input
== XC5000_RADIO_FM1_MONO
)
1006 xc_write_reg(priv
, XREG_OUTPUT_AMP
, 0x06);
1008 xc_tune_channel(priv
, priv
->freq_hz
, XC_TUNE_ANALOG
);
1013 static int xc5000_set_params(struct dvb_frontend
*fe
)
1015 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1017 if (xc_load_fw_and_init_tuner(fe
, 0) != 0) {
1018 dprintk(1, "Unable to load firmware and init tuner\n");
1022 switch (priv
->mode
) {
1023 case V4L2_TUNER_RADIO
:
1024 return xc5000_set_radio_freq(fe
);
1025 case V4L2_TUNER_ANALOG_TV
:
1026 return xc5000_set_tv_freq(fe
);
1027 case V4L2_TUNER_DIGITAL_TV
:
1028 return xc5000_tune_digital(fe
);
1034 static int xc5000_set_analog_params(struct dvb_frontend
*fe
,
1035 struct analog_parameters
*params
)
1037 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1040 if (priv
->i2c_props
.adap
== NULL
)
1043 switch (params
->mode
) {
1044 case V4L2_TUNER_RADIO
:
1045 ret
= xc5000_config_radio(fe
, params
);
1049 case V4L2_TUNER_ANALOG_TV
:
1050 xc5000_config_tv(fe
, params
);
1055 priv
->mode
= params
->mode
;
1057 return xc5000_set_params(fe
);
1060 static int xc5000_get_frequency(struct dvb_frontend
*fe
, u32
*freq
)
1062 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1063 dprintk(1, "%s()\n", __func__
);
1064 *freq
= priv
->freq_hz
+ priv
->freq_offset
;
1068 static int xc5000_get_if_frequency(struct dvb_frontend
*fe
, u32
*freq
)
1070 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1071 dprintk(1, "%s()\n", __func__
);
1072 *freq
= priv
->if_khz
* 1000;
1076 static int xc5000_get_bandwidth(struct dvb_frontend
*fe
, u32
*bw
)
1078 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1079 dprintk(1, "%s()\n", __func__
);
1081 *bw
= priv
->bandwidth
;
1085 static int xc5000_get_status(struct dvb_frontend
*fe
, u32
*status
)
1087 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1088 u16 lock_status
= 0;
1090 xc_get_lock_status(priv
, &lock_status
);
1092 dprintk(1, "%s() lock_status = 0x%08x\n", __func__
, lock_status
);
1094 *status
= lock_status
;
1099 static int xc_load_fw_and_init_tuner(struct dvb_frontend
*fe
, int force
)
1101 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1102 const struct xc5000_fw_cfg
*desired_fw
= xc5000_assign_firmware(priv
->chip_id
);
1103 const struct firmware
*fw
;
1105 u16 pll_lock_status
;
1108 cancel_delayed_work(&priv
->timer_sleep
);
1110 if (!force
&& xc5000_is_firmware_loaded(fe
) == 0)
1113 if (!priv
->firmware
) {
1114 ret
= request_firmware(&fw
, desired_fw
->name
,
1115 priv
->i2c_props
.adap
->dev
.parent
);
1117 pr_err("xc5000: Upload failed. rc %d\n", ret
);
1120 dprintk(1, "firmware read %zu bytes.\n", fw
->size
);
1122 if (fw
->size
!= desired_fw
->size
) {
1123 pr_err("xc5000: Firmware file with incorrect size\n");
1124 release_firmware(fw
);
1127 priv
->firmware
= fw
;
1129 fw
= priv
->firmware
;
1131 /* Try up to 5 times to load firmware */
1132 for (i
= 0; i
< 5; i
++) {
1134 printk(KERN_CONT
" - retrying to upload firmware.\n");
1136 ret
= xc5000_fwupload(fe
, desired_fw
, fw
);
1142 if (priv
->fw_checksum_supported
) {
1143 if (xc5000_readreg(priv
, XREG_FW_CHECKSUM
, &fw_ck
)) {
1145 "xc5000: FW checksum reading failed.");
1151 "xc5000: FW checksum failed = 0x%04x.",
1157 /* Start the tuner self-calibration process */
1158 ret
= xc_initialize(priv
);
1160 printk(KERN_ERR
"xc5000: Can't request self-calibration.");
1164 /* Wait for calibration to complete.
1165 * We could continue but XC5000 will clock stretch subsequent
1166 * I2C transactions until calibration is complete. This way we
1167 * don't have to rely on clock stretching working.
1171 if (priv
->init_status_supported
) {
1172 if (xc5000_readreg(priv
, XREG_INIT_STATUS
, &fw_ck
)) {
1174 "xc5000: FW failed reading init status.");
1180 "xc5000: FW init status failed = 0x%04x.",
1186 if (priv
->pll_register_no
) {
1187 ret
= xc5000_readreg(priv
, priv
->pll_register_no
,
1191 if (pll_lock_status
> 63) {
1192 /* PLL is unlocked, force reload of the firmware */
1194 "xc5000: PLL not running after fwload.");
1199 /* Default to "CABLE" mode */
1200 ret
= xc_write_reg(priv
, XREG_SIGNALSOURCE
, XC_RF_MODE_CABLE
);
1203 printk(KERN_ERR
"xc5000: can't set to cable mode.");
1208 printk(KERN_INFO
"xc5000: Firmware %s loaded and running.\n",
1211 printk(KERN_CONT
" - too many retries. Giving up\n");
1216 static void xc5000_do_timer_sleep(struct work_struct
*timer_sleep
)
1218 struct xc5000_priv
*priv
=container_of(timer_sleep
, struct xc5000_priv
,
1220 struct dvb_frontend
*fe
= priv
->fe
;
1223 dprintk(1, "%s()\n", __func__
);
1225 /* According to Xceive technical support, the "powerdown" register
1226 was removed in newer versions of the firmware. The "supported"
1227 way to sleep the tuner is to pull the reset pin low for 10ms */
1228 ret
= xc5000_tuner_reset(fe
);
1231 "xc5000: %s() unable to shutdown tuner\n",
1235 static int xc5000_sleep(struct dvb_frontend
*fe
)
1237 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1239 dprintk(1, "%s()\n", __func__
);
1241 /* Avoid firmware reload on slow devices */
1245 schedule_delayed_work(&priv
->timer_sleep
,
1246 msecs_to_jiffies(XC5000_SLEEP_TIME
));
1251 static int xc5000_suspend(struct dvb_frontend
*fe
)
1253 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1256 dprintk(1, "%s()\n", __func__
);
1258 cancel_delayed_work(&priv
->timer_sleep
);
1260 ret
= xc5000_tuner_reset(fe
);
1263 "xc5000: %s() unable to shutdown tuner\n",
1269 static int xc5000_resume(struct dvb_frontend
*fe
)
1271 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1273 dprintk(1, "%s()\n", __func__
);
1275 /* suspended before firmware is loaded.
1276 Avoid firmware load in resume path. */
1277 if (!priv
->firmware
)
1280 return xc5000_set_params(fe
);
1283 static int xc5000_init(struct dvb_frontend
*fe
)
1285 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1286 dprintk(1, "%s()\n", __func__
);
1288 if (xc_load_fw_and_init_tuner(fe
, 0) != 0) {
1289 printk(KERN_ERR
"xc5000: Unable to initialise tuner\n");
1294 xc_debug_dump(priv
);
1299 static void xc5000_release(struct dvb_frontend
*fe
)
1301 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1303 dprintk(1, "%s()\n", __func__
);
1305 mutex_lock(&xc5000_list_mutex
);
1308 cancel_delayed_work(&priv
->timer_sleep
);
1309 if (priv
->firmware
) {
1310 release_firmware(priv
->firmware
);
1311 priv
->firmware
= NULL
;
1313 hybrid_tuner_release_state(priv
);
1316 mutex_unlock(&xc5000_list_mutex
);
1318 fe
->tuner_priv
= NULL
;
1321 static int xc5000_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
1323 struct xc5000_priv
*priv
= fe
->tuner_priv
;
1324 struct xc5000_config
*p
= priv_cfg
;
1326 dprintk(1, "%s()\n", __func__
);
1329 priv
->if_khz
= p
->if_khz
;
1332 priv
->radio_input
= p
->radio_input
;
1335 priv
->output_amp
= p
->output_amp
;
1341 static const struct dvb_tuner_ops xc5000_tuner_ops
= {
1343 .name
= "Xceive XC5000",
1344 .frequency_min_hz
= 1 * MHz
,
1345 .frequency_max_hz
= 1023 * MHz
,
1346 .frequency_step_hz
= 50 * kHz
,
1349 .release
= xc5000_release
,
1350 .init
= xc5000_init
,
1351 .sleep
= xc5000_sleep
,
1352 .suspend
= xc5000_suspend
,
1353 .resume
= xc5000_resume
,
1355 .set_config
= xc5000_set_config
,
1356 .set_params
= xc5000_set_digital_params
,
1357 .set_analog_params
= xc5000_set_analog_params
,
1358 .get_frequency
= xc5000_get_frequency
,
1359 .get_if_frequency
= xc5000_get_if_frequency
,
1360 .get_bandwidth
= xc5000_get_bandwidth
,
1361 .get_status
= xc5000_get_status
1364 struct dvb_frontend
*xc5000_attach(struct dvb_frontend
*fe
,
1365 struct i2c_adapter
*i2c
,
1366 const struct xc5000_config
*cfg
)
1368 struct xc5000_priv
*priv
= NULL
;
1372 dprintk(1, "%s(%d-%04x)\n", __func__
,
1373 i2c
? i2c_adapter_id(i2c
) : -1,
1374 cfg
? cfg
->i2c_address
: -1);
1376 mutex_lock(&xc5000_list_mutex
);
1378 instance
= hybrid_tuner_request_state(struct xc5000_priv
, priv
,
1379 hybrid_tuner_instance_list
,
1380 i2c
, cfg
->i2c_address
, "xc5000");
1385 /* new tuner instance */
1386 priv
->bandwidth
= 6000000;
1387 fe
->tuner_priv
= priv
;
1389 INIT_DELAYED_WORK(&priv
->timer_sleep
, xc5000_do_timer_sleep
);
1392 /* existing tuner instance */
1393 fe
->tuner_priv
= priv
;
1397 if (priv
->if_khz
== 0) {
1398 /* If the IF hasn't been set yet, use the value provided by
1399 the caller (occurs in hybrid devices where the analog
1400 call to xc5000_attach occurs before the digital side) */
1401 priv
->if_khz
= cfg
->if_khz
;
1404 if (priv
->xtal_khz
== 0)
1405 priv
->xtal_khz
= cfg
->xtal_khz
;
1407 if (priv
->radio_input
== 0)
1408 priv
->radio_input
= cfg
->radio_input
;
1410 /* don't override chip id if it's already been set
1411 unless explicitly specified */
1412 if ((priv
->chip_id
== 0) || (cfg
->chip_id
))
1413 /* use default chip id if none specified, set to 0 so
1414 it can be overridden if this is a hybrid driver */
1415 priv
->chip_id
= (cfg
->chip_id
) ? cfg
->chip_id
: 0;
1417 /* don't override output_amp if it's already been set
1418 unless explicitly specified */
1419 if ((priv
->output_amp
== 0) || (cfg
->output_amp
))
1420 /* use default output_amp value if none specified */
1421 priv
->output_amp
= (cfg
->output_amp
) ? cfg
->output_amp
: 0x8a;
1423 /* Check if firmware has been loaded. It is possible that another
1424 instance of the driver has loaded the firmware.
1426 if (xc5000_readreg(priv
, XREG_PRODUCT_ID
, &id
) != 0)
1430 case XC_PRODUCT_ID_FW_LOADED
:
1432 "xc5000: Successfully identified at address 0x%02x\n",
1435 "xc5000: Firmware has been loaded previously\n");
1437 case XC_PRODUCT_ID_FW_NOT_LOADED
:
1439 "xc5000: Successfully identified at address 0x%02x\n",
1442 "xc5000: Firmware has not been loaded previously\n");
1446 "xc5000: Device not found at addr 0x%02x (0x%x)\n",
1447 cfg
->i2c_address
, id
);
1451 mutex_unlock(&xc5000_list_mutex
);
1453 memcpy(&fe
->ops
.tuner_ops
, &xc5000_tuner_ops
,
1454 sizeof(struct dvb_tuner_ops
));
1458 mutex_unlock(&xc5000_list_mutex
);
1463 EXPORT_SYMBOL(xc5000_attach
);
1465 MODULE_AUTHOR("Steven Toth");
1466 MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
1467 MODULE_LICENSE("GPL");
1468 MODULE_FIRMWARE(XC5000A_FIRMWARE
);
1469 MODULE_FIRMWARE(XC5000C_FIRMWARE
);