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 <asm/unaligned.h>
33 #include "dvb_frontend.h"
36 #include "tuner-i2c.h"
37 #include "tuner-xc2028-types.h"
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
43 static int no_poweroff;
44 module_param(no_poweroff, int, 0644);
45 MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
46 "\t\t1 keep device energized and with tuner ready all the times.\n"
47 "\t\tFaster, but consumes more power and keeps the device hotter");
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 #define XC4000_DEFAULT_FIRMWARE "xc4000-02.fw"
56 #define XC4000_DEFAULT_FIRMWARE_SIZE 18643
59 /* struct for storing firmware table */
60 struct firmware_description {
68 struct firmware_properties {
73 unsigned int scode_table;
78 struct tuner_i2c_props i2c_props;
79 struct list_head hybrid_tuner_instance_list;
80 struct firmware_description *firm;
88 // struct xc2028_ctrl ctrl;
89 struct firmware_properties cur_fw;
95 #define MAX_TV_STANDARD 23
96 #define XC_MAX_I2C_WRITE_LENGTH 64
99 #define XC_RF_MODE_AIR 0
100 #define XC_RF_MODE_CABLE 1
103 #define XC_RESULT_SUCCESS 0
104 #define XC_RESULT_RESET_FAILURE 1
105 #define XC_RESULT_I2C_WRITE_FAILURE 2
106 #define XC_RESULT_I2C_READ_FAILURE 3
107 #define XC_RESULT_OUT_OF_RANGE 5
110 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
111 #define XC_PRODUCT_ID_FW_LOADED 0x0FA0
113 /* Registers (Write-only) */
114 #define XREG_INIT 0x00
115 #define XREG_VIDEO_MODE 0x01
116 #define XREG_AUDIO_MODE 0x02
117 #define XREG_RF_FREQ 0x03
118 #define XREG_D_CODE 0x04
119 #define XREG_DIRECTSITTING_MODE 0x05
120 #define XREG_SEEK_MODE 0x06
121 #define XREG_POWER_DOWN 0x08
122 #define XREG_SIGNALSOURCE 0x0A
123 #define XREG_AMPLITUDE 0x10
125 /* Registers (Read-only) */
126 #define XREG_ADC_ENV 0x00
127 #define XREG_QUALITY 0x01
128 #define XREG_FRAME_LINES 0x02
129 #define XREG_HSYNC_FREQ 0x03
130 #define XREG_LOCK 0x04
131 #define XREG_FREQ_ERROR 0x05
132 #define XREG_SNR 0x06
133 #define XREG_VERSION 0x07
134 #define XREG_PRODUCT_ID 0x08
137 Basic firmware description. This will remain with
138 the driver for documentation purposes.
140 This represents an I2C firmware file encoded as a
141 string of unsigned char. Format is as follows:
143 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
144 char[1 ]=len0_LSB -> length of first write transaction
145 char[2 ]=data0 -> first byte to be sent
149 char[M ]=dataN -> last byte to be sent
150 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
151 char[M+2]=len1_LSB -> length of second write transaction
157 The [len] value should be interpreted as follows:
159 len= len_MSB _ len_LSB
160 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
161 len=0000_0000_0000_0000 : Reset command: Do hardware reset
162 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
163 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
165 For the RESET and WAIT commands, the two following bytes will contain
166 immediately the length of the following transaction.
169 struct XC_TV_STANDARD {
175 /* Tuner standards */
176 #define MN_NTSC_PAL_BTSC 0
177 #define MN_NTSC_PAL_A2 1
178 #define MN_NTSC_PAL_EIAJ 2
179 #define MN_NTSC_PAL_Mono 3
181 #define BG_PAL_NICAM 5
182 #define BG_PAL_MONO 6
183 #define I_PAL_NICAM 7
184 #define I_PAL_NICAM_MONO 8
186 #define DK_PAL_NICAM 10
187 #define DK_PAL_MONO 11
188 #define DK_SECAM_A2DK1 12
189 #define DK_SECAM_A2LDK3 13
190 #define DK_SECAM_A2MONO 14
191 #define L_SECAM_NICAM 15
192 #define LC_SECAM_NICAM 16
193 #define FM_Radio_INPUT2 21
194 #define FM_Radio_INPUT1 22
197 static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
198 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
199 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
200 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
201 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
202 {"B/G-PAL-A2", 0x0A00, 0x8049},
203 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
204 {"B/G-PAL-MONO", 0x0878, 0x8059},
205 {"I-PAL-NICAM", 0x1080, 0x8009},
206 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
207 {"D/K-PAL-A2", 0x1600, 0x8009},
208 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
209 {"D/K-PAL-MONO", 0x1478, 0x8009},
210 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
211 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
212 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
213 {"L-SECAM-NICAM", 0x8E82, 0x0009},
214 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
215 {"DTV6", 0x00C0, 0x8002},
216 {"DTV8", 0x00C0, 0x800B},
217 {"DTV7/8", 0x00C0, 0x801B},
218 {"DTV7", 0x00C0, 0x8007},
219 {"FM Radio-INPUT2", 0x9802, 0x9002},
220 {"FM Radio-INPUT1", 0x0208, 0x9002}
223 static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
224 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x8020},
225 {"M/N-NTSC/PAL-A2", 0x0000, 0x8020},
226 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x8020},
227 {"M/N-NTSC/PAL-Mono", 0x0078, 0x8020},
228 {"B/G-PAL-A2", 0x0000, 0x8059},
229 {"B/G-PAL-NICAM", 0x0004, 0x8059},
230 {"B/G-PAL-MONO", 0x0078, 0x8059},
231 {"I-PAL-NICAM", 0x0080, 0x8049},
232 {"I-PAL-NICAM-MONO", 0x0078, 0x8049},
233 {"D/K-PAL-A2", 0x0000, 0x8049},
234 {"D/K-PAL-NICAM", 0x0080, 0x8049},
235 {"D/K-PAL-MONO", 0x0078, 0x8049},
236 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049},
237 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049},
238 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049},
239 {"L-SECAM-NICAM", 0x8080, 0x0009},
240 {"L'-SECAM-NICAM", 0x8080, 0x4009},
241 {"DTV6", 0x00C0, 0x8002},
242 {"DTV8", 0x00C0, 0x800B},
243 {"DTV7/8", 0x00C0, 0x801B},
244 {"DTV7", 0x00C0, 0x8007},
245 {"FM Radio-INPUT2", 0x0008, 0x9800},
246 {"FM Radio-INPUT1", 0x0008, 0x9000}
249 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
250 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
251 static int xc4000_TunerReset(struct dvb_frontend *fe);
253 static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
255 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
256 .flags = 0, .buf = buf, .len = len };
257 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
258 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n", len);
259 return XC_RESULT_I2C_WRITE_FAILURE;
261 return XC_RESULT_SUCCESS;
264 /* This routine is never used because the only time we read data from the
265 i2c bus is when we read registers, and we want that to be an atomic i2c
266 transaction in case we are on a multi-master bus */
268 static void xc_wait(int wait_ms)
273 static int xc4000_TunerReset(struct dvb_frontend *fe)
275 struct xc4000_priv *priv = fe->tuner_priv;
278 dprintk(1, "%s()\n", __func__);
281 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
283 priv->i2c_props.adap->algo_data,
284 DVB_FRONTEND_COMPONENT_TUNER,
285 XC4000_TUNER_RESET, 0);
287 printk(KERN_ERR "xc4000: reset failed\n");
288 return XC_RESULT_RESET_FAILURE;
291 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
292 return XC_RESULT_RESET_FAILURE;
294 return XC_RESULT_SUCCESS;
297 static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
302 buf[0] = (regAddr >> 8) & 0xFF;
303 buf[1] = regAddr & 0xFF;
304 buf[2] = (i2cData >> 8) & 0xFF;
305 buf[3] = i2cData & 0xFF;
306 result = xc_send_i2c_data(priv, buf, 4);
311 static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
313 struct xc4000_priv *priv = fe->tuner_priv;
315 int i, nbytes_to_send, result;
316 unsigned int len, pos, index;
317 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
320 while ((i2c_sequence[index] != 0xFF) ||
321 (i2c_sequence[index + 1] != 0xFF)) {
322 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
325 result = xc4000_TunerReset(fe);
327 if (result != XC_RESULT_SUCCESS)
329 } else if (len & 0x8000) {
331 xc_wait(len & 0x7FFF);
334 /* Send i2c data whilst ensuring individual transactions
335 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
338 buf[0] = i2c_sequence[index];
339 buf[1] = i2c_sequence[index + 1];
342 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
344 XC_MAX_I2C_WRITE_LENGTH;
346 nbytes_to_send = (len - pos + 2);
347 for (i = 2; i < nbytes_to_send; i++) {
348 buf[i] = i2c_sequence[index + pos +
351 result = xc_send_i2c_data(priv, buf,
354 if (result != XC_RESULT_SUCCESS)
357 pos += nbytes_to_send - 2;
362 return XC_RESULT_SUCCESS;
365 static int xc_SetTVStandard(struct xc4000_priv *priv,
366 u16 VideoMode, u16 AudioMode)
369 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
370 dprintk(1, "%s() Standard = %s\n",
372 XC4000_Standard[priv->video_standard].Name);
374 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
375 if (ret == XC_RESULT_SUCCESS)
376 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
381 static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
383 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
384 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
386 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
387 rf_mode = XC_RF_MODE_CABLE;
389 "%s(), Invalid mode, defaulting to CABLE",
392 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
395 static const struct dvb_tuner_ops xc4000_tuner_ops;
397 static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
401 dprintk(1, "%s(%u)\n", __func__, freq_hz);
403 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
404 (freq_hz < xc4000_tuner_ops.info.frequency_min))
405 return XC_RESULT_OUT_OF_RANGE;
407 freq_code = (u16)(freq_hz / 15625);
409 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
410 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
411 only be used for fast scanning for channel lock) */
412 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
416 static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
418 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
421 static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
427 result = xc4000_readreg(priv, XREG_FREQ_ERROR, ®Data);
428 if (result != XC_RESULT_SUCCESS)
432 (*freq_error_hz) = (tmp * 15625) / 1000;
436 static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
438 return xc4000_readreg(priv, XREG_LOCK, lock_status);
441 static int xc_get_version(struct xc4000_priv *priv,
442 u8 *hw_majorversion, u8 *hw_minorversion,
443 u8 *fw_majorversion, u8 *fw_minorversion)
448 result = xc4000_readreg(priv, XREG_VERSION, &data);
449 if (result != XC_RESULT_SUCCESS)
452 (*hw_majorversion) = (data >> 12) & 0x0F;
453 (*hw_minorversion) = (data >> 8) & 0x0F;
454 (*fw_majorversion) = (data >> 4) & 0x0F;
455 (*fw_minorversion) = data & 0x0F;
461 static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
463 return xc4000_readreg(priv, XREG_BUILD, buildrev);
466 static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
471 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, ®Data);
472 if (result != XC_RESULT_SUCCESS)
475 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
479 static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
481 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
484 static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
486 return xc4000_readreg(priv, XREG_QUALITY, quality);
489 static u16 WaitForLock(struct xc4000_priv *priv)
492 int watchDogCount = 40;
494 while ((lockState == 0) && (watchDogCount > 0)) {
495 xc_get_lock_status(priv, &lockState);
496 if (lockState != 1) {
504 #define XC_TUNE_ANALOG 0
505 #define XC_TUNE_DIGITAL 1
506 static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
510 dprintk(1, "%s(%u)\n", __func__, freq_hz);
512 if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
515 if (mode == XC_TUNE_ANALOG) {
516 if (WaitForLock(priv) == 1)
523 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
525 u8 buf[2] = { reg >> 8, reg & 0xff };
526 u8 bval[2] = { 0, 0 };
527 struct i2c_msg msg[2] = {
528 { .addr = priv->i2c_props.addr,
529 .flags = 0, .buf = &buf[0], .len = 2 },
530 { .addr = priv->i2c_props.addr,
531 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
534 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
535 printk(KERN_WARNING "xc4000: I2C read failed\n");
539 *val = (bval[0] << 8) | bval[1];
540 return XC_RESULT_SUCCESS;
543 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
544 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
590 if (type & TOYOTA388)
591 printk("TOYOTA388 ");
592 if (type & TOYOTA794)
593 printk("TOYOTA794 ");
596 if (type & ZARLINK456)
597 printk("ZARLINK456 ");
607 printk("HAS_IF_%d ", int_freq);
610 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
613 struct xc4000_priv *priv = fe->tuner_priv;
614 int i, best_i = -1, best_nr_matches = 0;
615 unsigned int type_mask = 0;
617 printk("%s called, want type=", __func__);
619 dump_firm_type(type);
620 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
624 printk("Error! firmware not loaded\n");
628 if (((type & ~SCODE) == 0) && (*id == 0))
632 type_mask = BASE_TYPES;
633 else if (type & SCODE) {
635 type_mask = SCODE_TYPES & ~HAS_IF;
636 } else if (type & DTV_TYPES)
637 type_mask = DTV_TYPES;
638 else if (type & STD_SPECIFIC_TYPES)
639 type_mask = STD_SPECIFIC_TYPES;
646 /* Seek for exact match */
647 for (i = 0; i < priv->firm_size; i++) {
648 if ((type == (priv->firm[i].type & type_mask)) &&
649 (*id == priv->firm[i].id))
653 /* Seek for generic video standard match */
654 for (i = 0; i < priv->firm_size; i++) {
655 v4l2_std_id match_mask;
658 if (type != (priv->firm[i].type & type_mask))
661 match_mask = *id & priv->firm[i].id;
665 if ((*id & match_mask) == *id)
666 goto found; /* Supports all the requested standards */
668 nr_matches = hweight64(match_mask);
669 if (nr_matches > best_nr_matches) {
670 best_nr_matches = nr_matches;
675 if (best_nr_matches > 0) {
676 printk("Selecting best matching firmware (%d bits) for "
677 "type=", best_nr_matches);
678 // dump_firm_type(type);
679 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
684 /*FIXME: Would make sense to seek for type "hint" match ? */
690 *id = priv->firm[i].id;
693 printk("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
695 dump_firm_type(type);
696 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
703 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
706 struct xc4000_priv *priv = fe->tuner_priv;
710 printk("%s called\n", __func__);
712 pos = seek_firmware(fe, type, id);
716 printk("Loading firmware for type=");
717 // dump_firm_type(priv->firm[pos].type);
718 printk("(%x), id %016llx.\n", priv->firm[pos].type,
719 (unsigned long long)*id);
721 p = priv->firm[pos].ptr;
722 printk("firmware length = %d\n", priv->firm[pos].size);
724 rc = xc_load_i2c_sequence(fe, p);
729 static int xc4000_fwupload(struct dvb_frontend *fe)
731 struct xc4000_priv *priv = fe->tuner_priv;
732 const struct firmware *fw = NULL;
733 const unsigned char *p, *endp;
739 printk("%s called\n", __func__);
741 fname = XC4000_DEFAULT_FIRMWARE;
743 printk("Reading firmware %s\n", fname);
744 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
747 printk("Error: firmware %s not found.\n",
750 printk("Error %d while requesting firmware %s \n",
758 if (fw->size < sizeof(name) - 1 + 2 + 2) {
759 printk("Error: firmware file %s has invalid size!\n",
764 memcpy(name, p, sizeof(name) - 1);
765 name[sizeof(name) - 1] = 0;
766 p += sizeof(name) - 1;
768 priv->firm_version = get_unaligned_le16(p);
771 n_array = get_unaligned_le16(p);
774 printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
775 n_array, fname, name,
776 priv->firm_version >> 8, priv->firm_version & 0xff);
778 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
779 if (priv->firm == NULL) {
780 printk("Not enough memory to load firmware file.\n");
784 priv->firm_size = n_array;
794 printk("More firmware images in file than "
799 /* Checks if there's enough bytes to read */
800 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
803 type = get_unaligned_le32(p);
806 id = get_unaligned_le64(p);
810 int_freq = get_unaligned_le16(p);
811 p += sizeof(int_freq);
812 if (endp - p < sizeof(size))
816 size = get_unaligned_le32(p);
819 if (!size || size > endp - p) {
820 printk("Firmware type ");
821 // dump_firm_type(type);
822 printk("(%x), id %llx is corrupted "
823 "(size=%d, expected %d)\n",
824 type, (unsigned long long)id,
825 (unsigned)(endp - p), size);
829 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
830 if (priv->firm[n].ptr == NULL) {
831 printk("Not enough memory to load firmware file.\n");
837 printk("Reading firmware type ");
838 dump_firm_type_and_int_freq(type, int_freq);
839 printk("(%x), id %llx, size=%d.\n",
840 type, (unsigned long long)id, size);
843 memcpy(priv->firm[n].ptr, p, size);
844 priv->firm[n].type = type;
845 priv->firm[n].id = id;
846 priv->firm[n].size = size;
847 priv->firm[n].int_freq = int_freq;
852 if (n + 1 != priv->firm_size) {
853 printk("Firmware file is incomplete!\n");
860 printk("Firmware header is incomplete!\n");
863 printk("Error: firmware file is corrupted!\n");
866 printk("Releasing partially loaded firmware file.\n");
867 // free_firmware(priv);
870 release_firmware(fw);
872 printk("Firmware files loaded.\n");
877 static int load_scode(struct dvb_frontend *fe, unsigned int type,
878 v4l2_std_id *id, __u16 int_freq, int scode)
880 struct xc4000_priv *priv = fe->tuner_priv;
886 dprintk(1, "%s called\n", __func__);
889 pos = seek_firmware(fe, type, id);
893 for (pos = 0; pos < priv->firm_size; pos++) {
894 if ((priv->firm[pos].int_freq == int_freq) &&
895 (priv->firm[pos].type & HAS_IF))
898 if (pos == priv->firm_size)
902 p = priv->firm[pos].ptr;
904 if (priv->firm[pos].type & HAS_IF) {
905 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
909 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
910 * has a 2-byte size header in the firmware format. */
911 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
912 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
917 tuner_info("Loading SCODE for type=");
918 dump_firm_type_and_int_freq(priv->firm[pos].type,
919 priv->firm[pos].int_freq);
920 printk("(%x), id %016llx.\n", priv->firm[pos].type,
921 (unsigned long long)*id);
924 memcpy(&scode_buf[1], p, 12);
926 /* Enter direct-mode */
927 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
929 printk("failed to put device into direct mode!\n");
933 rc = xc_send_i2c_data(priv, scode_buf, 13);
934 if (rc != XC_RESULT_SUCCESS) {
935 /* Even if the send failed, make sure we set back to indirect
937 printk("Failed to set scode %d\n", rc);
940 /* Switch back to indirect-mode */
941 memset(indirect_mode, 0, sizeof(indirect_mode));
942 indirect_mode[4] = 0x88;
943 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
949 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
950 v4l2_std_id std, __u16 int_freq)
952 struct xc4000_priv *priv = fe->tuner_priv;
953 struct firmware_properties new_fw;
954 int rc = 0, is_retry = 0;
955 u16 version, hwmodel;
957 u8 hw_major, hw_minor, fw_major, fw_minor;
959 dprintk(1, "%s called\n", __func__);
962 rc = xc4000_fwupload(fe);
968 if (priv->ctrl.mts && !(type & FM))
975 new_fw.std_req = std;
976 // new_fw.scode_table = SCODE | priv->ctrl.scode_table;
977 new_fw.scode_table = SCODE;
979 new_fw.int_freq = int_freq;
981 dprintk(1, "checking firmware, user requested type=");
983 dump_firm_type(new_fw.type);
984 printk("(%x), id %016llx, ", new_fw.type,
985 (unsigned long long)new_fw.std_req);
987 printk("scode_tbl ");
989 dump_firm_type(priv->ctrl.scode_table);
990 printk("(%x), ", priv->ctrl.scode_table);
993 printk("int_freq %d, ", new_fw.int_freq);
994 printk("scode_nr %d\n", new_fw.scode_nr);
997 /* No need to reload base firmware if it matches */
998 if (((BASE | new_fw.type) & BASE_TYPES) ==
999 (priv->cur_fw.type & BASE_TYPES)) {
1000 dprintk(1, "BASE firmware not changed.\n");
1004 /* Updating BASE - forget about all currently loaded firmware */
1005 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1007 /* Reset is needed before loading firmware */
1008 rc = xc4000_TunerReset(fe);
1012 /* BASE firmwares are all std0 */
1014 rc = load_firmware(fe, BASE | new_fw.type, &std0);
1016 printk("Error %d while loading base firmware\n", rc);
1020 /* Load INIT1, if needed */
1021 dprintk(1, "Load init1 firmware, if exists\n");
1023 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1025 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
1027 if (rc < 0 && rc != -ENOENT) {
1028 tuner_err("Error %d while loading init1 firmware\n",
1033 printk("Done with init1\n");
1037 * No need to reload standard specific firmware if base firmware
1038 * was not reloaded and requested video standards have not changed.
1040 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1041 priv->cur_fw.std_req == std) {
1042 dprintk(1, "Std-specific firmware already loaded.\n");
1043 goto skip_std_specific;
1046 /* Reloading std-specific firmware forces a SCODE update */
1047 priv->cur_fw.scode_table = 0;
1049 /* Load the standard firmware */
1050 rc = load_firmware(fe, new_fw.type, &new_fw.id);
1056 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1057 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1058 dprintk(1, "SCODE firmware already loaded.\n");
1062 if (new_fw.type & FM)
1065 /* Load SCODE firmware, if exists */
1066 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1067 new_fw.int_freq, new_fw.scode_nr);
1068 if (rc != XC_RESULT_SUCCESS)
1069 dprintk(1, "load scode failed %d\n", rc);
1072 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1074 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
1075 &fw_minor) != XC_RESULT_SUCCESS) {
1076 printk("Unable to read tuner registers.\n");
1080 dprintk(1, "Device is Xceive %d version %d.%d, "
1081 "firmware version %d.%d\n",
1082 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1084 /* Check firmware version against what we downloaded. */
1086 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1087 printk("Incorrect readback of firmware version %x.\n",
1093 /* Check that the tuner hardware model remains consistent over time. */
1094 if (priv->hwmodel == 0 && hwmodel == 4000) {
1095 priv->hwmodel = hwmodel;
1096 priv->hwvers = version & 0xff00;
1097 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1098 priv->hwvers != (version & 0xff00)) {
1099 printk("Read invalid device hardware information - tuner "
1104 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1107 * By setting BASE in cur_fw.type only after successfully loading all
1108 * firmwares, we can:
1109 * 1. Identify that BASE firmware with type=0 has been loaded;
1110 * 2. Tell whether BASE firmware was just changed the next time through.
1112 priv->cur_fw.type |= BASE;
1117 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1121 dprintk(1, "Retrying firmware load\n");
1130 static void xc_debug_dump(struct xc4000_priv *priv)
1133 u32 freq_error_hz = 0;
1135 u32 hsync_freq_hz = 0;
1138 u8 hw_majorversion = 0, hw_minorversion = 0;
1139 u8 fw_majorversion = 0, fw_minorversion = 0;
1140 // u16 fw_buildversion = 0;
1142 /* Wait for stats to stabilize.
1143 * Frame Lines needs two frame times after initial lock
1144 * before it is valid.
1148 xc_get_ADC_Envelope(priv, &adc_envelope);
1149 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1151 xc_get_frequency_error(priv, &freq_error_hz);
1152 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1154 xc_get_lock_status(priv, &lock_status);
1155 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1158 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1159 &fw_majorversion, &fw_minorversion);
1161 // xc_get_buildversion(priv, &fw_buildversion);
1162 // dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
1163 // hw_majorversion, hw_minorversion,
1164 // fw_majorversion, fw_minorversion, fw_buildversion);
1166 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1167 hw_majorversion, hw_minorversion,
1168 fw_majorversion, fw_minorversion);
1170 xc_get_hsync_freq(priv, &hsync_freq_hz);
1171 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1173 xc_get_frame_lines(priv, &frame_lines);
1174 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1176 xc_get_quality(priv, &quality);
1177 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1180 static int xc4000_set_params(struct dvb_frontend *fe,
1181 struct dvb_frontend_parameters *params)
1183 struct xc4000_priv *priv = fe->tuner_priv;
1186 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1188 /* FIXME: setup proper parameters */
1189 if (check_firmware(fe, DTV8, 0, 5400) != XC_RESULT_SUCCESS) {
1193 if (fe->ops.info.type == FE_ATSC) {
1194 dprintk(1, "%s() ATSC\n", __func__);
1195 switch (params->u.vsb.modulation) {
1198 dprintk(1, "%s() VSB modulation\n", __func__);
1199 priv->rf_mode = XC_RF_MODE_AIR;
1200 priv->freq_hz = params->frequency - 1750000;
1201 priv->bandwidth = BANDWIDTH_6_MHZ;
1202 priv->video_standard = DTV6;
1207 dprintk(1, "%s() QAM modulation\n", __func__);
1208 priv->rf_mode = XC_RF_MODE_CABLE;
1209 priv->freq_hz = params->frequency - 1750000;
1210 priv->bandwidth = BANDWIDTH_6_MHZ;
1211 priv->video_standard = DTV6;
1216 } else if (fe->ops.info.type == FE_OFDM) {
1217 dprintk(1, "%s() OFDM\n", __func__);
1218 switch (params->u.ofdm.bandwidth) {
1219 case BANDWIDTH_6_MHZ:
1220 priv->bandwidth = BANDWIDTH_6_MHZ;
1221 priv->video_standard = DTV6;
1222 priv->freq_hz = params->frequency - 1750000;
1224 case BANDWIDTH_7_MHZ:
1225 printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
1227 case BANDWIDTH_8_MHZ:
1228 priv->bandwidth = BANDWIDTH_8_MHZ;
1229 priv->video_standard = DTV8;
1230 priv->freq_hz = params->frequency - 2750000;
1233 printk(KERN_ERR "xc4000 bandwidth not set!\n");
1236 priv->rf_mode = XC_RF_MODE_AIR;
1238 printk(KERN_ERR "xc4000 modulation type not supported!\n");
1242 dprintk(1, "%s() frequency=%d (compensated)\n",
1243 __func__, priv->freq_hz);
1245 ret = xc_SetSignalSource(priv, priv->rf_mode);
1246 if (ret != XC_RESULT_SUCCESS) {
1248 "xc4000: xc_SetSignalSource(%d) failed\n",
1253 ret = xc_SetTVStandard(priv,
1254 XC4000_Standard[priv->video_standard].VideoMode,
1255 XC4000_Standard[priv->video_standard].AudioMode);
1256 if (ret != XC_RESULT_SUCCESS) {
1257 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1261 ret = xc_set_IF_frequency(priv, priv->if_khz);
1262 if (ret != XC_RESULT_SUCCESS) {
1263 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
1268 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1271 xc_debug_dump(priv);
1276 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1278 struct xc4000_priv *priv = fe->tuner_priv;
1282 ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1283 if (ret == XC_RESULT_SUCCESS) {
1284 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1285 ret = XC_RESULT_RESET_FAILURE;
1287 ret = XC_RESULT_SUCCESS;
1290 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1291 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1295 static int xc4000_set_analog_params(struct dvb_frontend *fe,
1296 struct analog_parameters *params)
1298 struct xc4000_priv *priv = fe->tuner_priv;
1301 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1302 __func__, params->frequency);
1304 /* FIXME: setup proper parameters */
1305 if (check_firmware(fe, DTV8, 0, 5400) != XC_RESULT_SUCCESS) {
1309 /* Fix me: it could be air. */
1310 priv->rf_mode = params->mode;
1311 if (params->mode > XC_RF_MODE_CABLE)
1312 priv->rf_mode = XC_RF_MODE_CABLE;
1314 /* params->frequency is in units of 62.5khz */
1315 priv->freq_hz = params->frequency * 62500;
1317 /* FIX ME: Some video standards may have several possible audio
1318 standards. We simply default to one of them here.
1320 if (params->std & V4L2_STD_MN) {
1321 /* default to BTSC audio standard */
1322 priv->video_standard = MN_NTSC_PAL_BTSC;
1326 if (params->std & V4L2_STD_PAL_BG) {
1327 /* default to NICAM audio standard */
1328 priv->video_standard = BG_PAL_NICAM;
1332 if (params->std & V4L2_STD_PAL_I) {
1333 /* default to NICAM audio standard */
1334 priv->video_standard = I_PAL_NICAM;
1338 if (params->std & V4L2_STD_PAL_DK) {
1339 /* default to NICAM audio standard */
1340 priv->video_standard = DK_PAL_NICAM;
1344 if (params->std & V4L2_STD_SECAM_DK) {
1345 /* default to A2 DK1 audio standard */
1346 priv->video_standard = DK_SECAM_A2DK1;
1350 if (params->std & V4L2_STD_SECAM_L) {
1351 priv->video_standard = L_SECAM_NICAM;
1355 if (params->std & V4L2_STD_SECAM_LC) {
1356 priv->video_standard = LC_SECAM_NICAM;
1361 ret = xc_SetSignalSource(priv, priv->rf_mode);
1362 if (ret != XC_RESULT_SUCCESS) {
1364 "xc4000: xc_SetSignalSource(%d) failed\n",
1369 ret = xc_SetTVStandard(priv,
1370 XC4000_Standard[priv->video_standard].VideoMode,
1371 XC4000_Standard[priv->video_standard].AudioMode);
1372 if (ret != XC_RESULT_SUCCESS) {
1373 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1377 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1380 xc_debug_dump(priv);
1385 static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1387 struct xc4000_priv *priv = fe->tuner_priv;
1388 dprintk(1, "%s()\n", __func__);
1389 *freq = priv->freq_hz;
1393 static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1395 struct xc4000_priv *priv = fe->tuner_priv;
1396 dprintk(1, "%s()\n", __func__);
1398 *bw = priv->bandwidth;
1402 static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1404 struct xc4000_priv *priv = fe->tuner_priv;
1405 u16 lock_status = 0;
1407 xc_get_lock_status(priv, &lock_status);
1409 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1411 *status = lock_status;
1416 static int xc4000_sleep(struct dvb_frontend *fe)
1418 /* FIXME: djh disable this for now... */
1419 return XC_RESULT_SUCCESS;
1422 static int xc4000_init(struct dvb_frontend *fe)
1424 struct xc4000_priv *priv = fe->tuner_priv;
1425 dprintk(1, "%s()\n", __func__);
1427 if (check_firmware(fe, DTV8, 0, 5400) != XC_RESULT_SUCCESS) {
1428 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1433 xc_debug_dump(priv);
1438 static int xc4000_release(struct dvb_frontend *fe)
1440 struct xc4000_priv *priv = fe->tuner_priv;
1442 dprintk(1, "%s()\n", __func__);
1444 mutex_lock(&xc4000_list_mutex);
1447 hybrid_tuner_release_state(priv);
1449 mutex_unlock(&xc4000_list_mutex);
1451 fe->tuner_priv = NULL;
1456 static const struct dvb_tuner_ops xc4000_tuner_ops = {
1458 .name = "Xceive XC4000",
1459 .frequency_min = 1000000,
1460 .frequency_max = 1023000000,
1461 .frequency_step = 50000,
1464 .release = xc4000_release,
1465 .init = xc4000_init,
1466 .sleep = xc4000_sleep,
1468 .set_params = xc4000_set_params,
1469 .set_analog_params = xc4000_set_analog_params,
1470 .get_frequency = xc4000_get_frequency,
1471 .get_bandwidth = xc4000_get_bandwidth,
1472 .get_status = xc4000_get_status
1475 struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1476 struct i2c_adapter *i2c,
1477 struct xc4000_config *cfg)
1479 struct xc4000_priv *priv = NULL;
1483 dprintk(1, "%s(%d-%04x)\n", __func__,
1484 i2c ? i2c_adapter_id(i2c) : -1,
1485 cfg ? cfg->i2c_address : -1);
1487 mutex_lock(&xc4000_list_mutex);
1489 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1490 hybrid_tuner_instance_list,
1491 i2c, cfg->i2c_address, "xc4000");
1497 /* new tuner instance */
1498 priv->bandwidth = BANDWIDTH_6_MHZ;
1499 fe->tuner_priv = priv;
1502 /* existing tuner instance */
1503 fe->tuner_priv = priv;
1507 if (priv->if_khz == 0) {
1508 /* If the IF hasn't been set yet, use the value provided by
1509 the caller (occurs in hybrid devices where the analog
1510 call to xc4000_attach occurs before the digital side) */
1511 priv->if_khz = cfg->if_khz;
1514 /* Check if firmware has been loaded. It is possible that another
1515 instance of the driver has loaded the firmware.
1518 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1522 case XC_PRODUCT_ID_FW_LOADED:
1524 "xc4000: Successfully identified at address 0x%02x\n",
1527 "xc4000: Firmware has been loaded previously\n");
1529 case XC_PRODUCT_ID_FW_NOT_LOADED:
1531 "xc4000: Successfully identified at address 0x%02x\n",
1534 "xc4000: Firmware has not been loaded previously\n");
1538 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1539 cfg->i2c_address, id);
1543 mutex_unlock(&xc4000_list_mutex);
1545 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1546 sizeof(struct dvb_tuner_ops));
1548 /* FIXME: For now, load the firmware at startup. We will remove this
1549 before the code goes to production... */
1550 check_firmware(fe, DTV8, 0, 5400);
1554 mutex_unlock(&xc4000_list_mutex);
1559 EXPORT_SYMBOL(xc4000_attach);
1561 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1562 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1563 MODULE_LICENSE("GPL");