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-01.fw"
56 #define XC4000_DEFAULT_FIRMWARE_SIZE 8434
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;
91 #define MAX_TV_STANDARD 23
92 #define XC_MAX_I2C_WRITE_LENGTH 64
95 #define XC_RF_MODE_AIR 0
96 #define XC_RF_MODE_CABLE 1
99 #define XC_RESULT_SUCCESS 0
100 #define XC_RESULT_RESET_FAILURE 1
101 #define XC_RESULT_I2C_WRITE_FAILURE 2
102 #define XC_RESULT_I2C_READ_FAILURE 3
103 #define XC_RESULT_OUT_OF_RANGE 5
106 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
107 #define XC_PRODUCT_ID_FW_LOADED 0x0FA0 /* WAS: 0x1388*/
110 #define XREG_INIT 0x00
111 #define XREG_VIDEO_MODE 0x01
112 #define XREG_AUDIO_MODE 0x02
113 #define XREG_RF_FREQ 0x03
114 #define XREG_D_CODE 0x04
115 #define XREG_IF_OUT 0x05 /* ?? */
116 #define XREG_SEEK_MODE 0x07 /* WAS: 0x06 */
117 #define XREG_POWER_DOWN 0x08 /* WAS: 0x0A Obsolete */
118 #define XREG_SIGNALSOURCE 0x0A /* WAS: 0x0D 0=Air, 1=Cable */
119 //#define XREG_SMOOTHEDCVBS 0x0E
120 //#define XREG_XTALFREQ 0x0F
121 //#define XREG_FINERFREQ 0x10
122 //#define XREG_DDIMODE 0x11
124 #define XREG_ADC_ENV 0x00
125 #define XREG_QUALITY 0x01
126 #define XREG_FRAME_LINES 0x02
127 #define XREG_HSYNC_FREQ 0x03
128 #define XREG_LOCK 0x04
129 #define XREG_FREQ_ERROR 0x05
130 #define XREG_SNR 0x06
131 #define XREG_VERSION 0x07
132 #define XREG_PRODUCT_ID 0x08
133 //#define XREG_BUSY 0x09
134 //#define XREG_BUILD 0x0D
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 xc_load_fw_and_init_tuner(struct dvb_frontend *fe);
250 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
251 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
252 static int xc4000_TunerReset(struct dvb_frontend *fe);
254 static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
256 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
257 .flags = 0, .buf = buf, .len = len };
259 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
260 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n", len);
261 return XC_RESULT_I2C_WRITE_FAILURE;
263 return XC_RESULT_SUCCESS;
266 /* This routine is never used because the only time we read data from the
267 i2c bus is when we read registers, and we want that to be an atomic i2c
268 transaction in case we are on a multi-master bus */
269 static int xc_read_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
271 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
272 .flags = I2C_M_RD, .buf = buf, .len = len };
274 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
275 printk(KERN_ERR "xc4000 I2C read failed (len=%i)\n", len);
281 static void xc_wait(int wait_ms)
286 static int xc4000_TunerReset(struct dvb_frontend *fe)
288 struct xc4000_priv *priv = fe->tuner_priv;
291 dprintk(1, "%s()\n", __func__);
294 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
296 priv->i2c_props.adap->algo_data,
297 DVB_FRONTEND_COMPONENT_TUNER,
298 XC4000_TUNER_RESET, 0);
300 printk(KERN_ERR "xc4000: reset failed\n");
301 return XC_RESULT_RESET_FAILURE;
304 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
305 return XC_RESULT_RESET_FAILURE;
307 return XC_RESULT_SUCCESS;
310 static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
313 // int WatchDogTimer = 100;
316 buf[0] = (regAddr >> 8) & 0xFF;
317 buf[1] = regAddr & 0xFF;
318 buf[2] = (i2cData >> 8) & 0xFF;
319 buf[3] = i2cData & 0xFF;
320 result = xc_send_i2c_data(priv, buf, 4);
322 // if (result == XC_RESULT_SUCCESS) {
323 // /* wait for busy flag to clear */
324 // while ((WatchDogTimer > 0) && (result == XC_RESULT_SUCCESS)) {
326 // buf[1] = XREG_BUSY;
328 // result = xc_send_i2c_data(priv, buf, 2);
329 // if (result == XC_RESULT_SUCCESS) {
330 // result = xc_read_i2c_data(priv, buf, 2);
331 // if (result == XC_RESULT_SUCCESS) {
332 // if ((buf[0] == 0) && (buf[1] == 0)) {
333 // /* busy flag cleared */
336 // xc_wait(5); /* wait 5 ms */
343 // if (WatchDogTimer < 0)
344 // result = XC_RESULT_I2C_WRITE_FAILURE;
349 static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
351 struct xc4000_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 = xc4000_TunerReset(fe);
365 if (result != XC_RESULT_SUCCESS)
367 } else if (len & 0x8000) {
369 xc_wait(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,
392 if (result != XC_RESULT_SUCCESS)
395 pos += nbytes_to_send - 2;
400 return XC_RESULT_SUCCESS;
403 static int xc_initialize(struct xc4000_priv *priv)
405 dprintk(1, "%s()\n", __func__);
406 return xc_write_reg(priv, XREG_INIT, 0);
409 static int xc_SetTVStandard(struct xc4000_priv *priv,
410 u16 VideoMode, u16 AudioMode)
413 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
414 dprintk(1, "%s() Standard = %s\n",
416 XC4000_Standard[priv->video_standard].Name);
418 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
419 if (ret == XC_RESULT_SUCCESS)
420 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
425 static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
427 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
428 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
430 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
431 rf_mode = XC_RF_MODE_CABLE;
433 "%s(), Invalid mode, defaulting to CABLE",
436 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
439 static const struct dvb_tuner_ops xc4000_tuner_ops;
441 static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
445 dprintk(1, "%s(%u)\n", __func__, freq_hz);
447 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
448 (freq_hz < xc4000_tuner_ops.info.frequency_min))
449 return XC_RESULT_OUT_OF_RANGE;
451 freq_code = (u16)(freq_hz / 15625);
453 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
454 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
455 only be used for fast scanning for channel lock) */
456 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
460 static int xc_set_IF_frequency(struct xc4000_priv *priv, u32 freq_khz)
462 u32 freq_code = (freq_khz * 1024)/1000;
463 dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
464 __func__, freq_khz, freq_code);
466 return xc_write_reg(priv, XREG_IF_OUT, freq_code);
470 static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
472 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
475 static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
481 result = xc4000_readreg(priv, XREG_FREQ_ERROR, ®Data);
482 if (result != XC_RESULT_SUCCESS)
486 (*freq_error_hz) = (tmp * 15625) / 1000;
490 static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
492 return xc4000_readreg(priv, XREG_LOCK, lock_status);
495 static int xc_get_version(struct xc4000_priv *priv,
496 u8 *hw_majorversion, u8 *hw_minorversion,
497 u8 *fw_majorversion, u8 *fw_minorversion)
502 result = xc4000_readreg(priv, XREG_VERSION, &data);
503 if (result != XC_RESULT_SUCCESS)
506 (*hw_majorversion) = (data >> 12) & 0x0F;
507 (*hw_minorversion) = (data >> 8) & 0x0F;
508 (*fw_majorversion) = (data >> 4) & 0x0F;
509 (*fw_minorversion) = data & 0x0F;
515 static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
517 return xc4000_readreg(priv, XREG_BUILD, buildrev);
520 static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
525 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, ®Data);
526 if (result != XC_RESULT_SUCCESS)
529 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
533 static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
535 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
538 static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
540 return xc4000_readreg(priv, XREG_QUALITY, quality);
543 static u16 WaitForLock(struct xc4000_priv *priv)
546 int watchDogCount = 40;
548 while ((lockState == 0) && (watchDogCount > 0)) {
549 xc_get_lock_status(priv, &lockState);
550 if (lockState != 1) {
558 #define XC_TUNE_ANALOG 0
559 #define XC_TUNE_DIGITAL 1
560 static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
564 dprintk(1, "%s(%u)\n", __func__, freq_hz);
566 if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
569 if (mode == XC_TUNE_ANALOG) {
570 if (WaitForLock(priv) == 1)
577 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
579 u8 buf[2] = { reg >> 8, reg & 0xff };
580 u8 bval[2] = { 0, 0 };
581 struct i2c_msg msg[2] = {
582 { .addr = priv->i2c_props.addr,
583 .flags = 0, .buf = &buf[0], .len = 2 },
584 { .addr = priv->i2c_props.addr,
585 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
588 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
589 printk(KERN_WARNING "xc4000: I2C read failed\n");
593 *val = (bval[0] << 8) | bval[1];
594 return XC_RESULT_SUCCESS;
598 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
601 struct xc4000_priv *priv = fe->tuner_priv;
602 int i, best_i = -1, best_nr_matches = 0;
603 unsigned int type_mask = 0;
605 printk("%s called, want type=", __func__);
607 // dump_firm_type(type);
608 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
612 printk("Error! firmware not loaded\n");
616 if (((type & ~SCODE) == 0) && (*id == 0))
620 type_mask = BASE_TYPES;
621 else if (type & SCODE) {
623 type_mask = SCODE_TYPES & ~HAS_IF;
624 } else if (type & DTV_TYPES)
625 type_mask = DTV_TYPES;
626 else if (type & STD_SPECIFIC_TYPES)
627 type_mask = STD_SPECIFIC_TYPES;
634 /* Seek for exact match */
635 for (i = 0; i < priv->firm_size; i++) {
636 if ((type == (priv->firm[i].type & type_mask)) &&
637 (*id == priv->firm[i].id))
641 /* Seek for generic video standard match */
642 for (i = 0; i < priv->firm_size; i++) {
643 v4l2_std_id match_mask;
646 if (type != (priv->firm[i].type & type_mask))
649 match_mask = *id & priv->firm[i].id;
653 if ((*id & match_mask) == *id)
654 goto found; /* Supports all the requested standards */
656 nr_matches = hweight64(match_mask);
657 if (nr_matches > best_nr_matches) {
658 best_nr_matches = nr_matches;
663 if (best_nr_matches > 0) {
664 printk("Selecting best matching firmware (%d bits) for "
665 "type=", best_nr_matches);
666 // dump_firm_type(type);
667 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
672 /*FIXME: Would make sense to seek for type "hint" match ? */
678 *id = priv->firm[i].id;
681 printk("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
683 // dump_firm_type(type);
684 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
689 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
692 struct xc4000_priv *priv = fe->tuner_priv;
696 printk("%s called\n", __func__);
698 pos = seek_firmware(fe, type, id);
702 printk("Loading firmware for type=");
703 // dump_firm_type(priv->firm[pos].type);
704 printk("(%x), id %016llx.\n", priv->firm[pos].type,
705 (unsigned long long)*id);
707 p = priv->firm[pos].ptr;
709 rc = xc_load_i2c_sequence(fe, p);
710 printk("load i2c sequence result=%d\n", rc);
715 //static int load_all_firmwares(struct dvb_frontend *fe)
716 static int xc4000_fwupload(struct dvb_frontend *fe)
718 struct xc4000_priv *priv = fe->tuner_priv;
719 const struct firmware *fw = NULL;
720 const unsigned char *p, *endp;
726 printk("%s called\n", __func__);
728 fname = XC4000_DEFAULT_FIRMWARE;
730 printk("Reading firmware %s\n", fname);
731 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
734 printk("Error: firmware %s not found.\n",
737 printk("Error %d while requesting firmware %s \n",
745 if (fw->size < sizeof(name) - 1 + 2 + 2) {
746 printk("Error: firmware file %s has invalid size!\n",
751 memcpy(name, p, sizeof(name) - 1);
752 name[sizeof(name) - 1] = 0;
753 p += sizeof(name) - 1;
755 priv->firm_version = get_unaligned_le16(p);
758 n_array = get_unaligned_le16(p);
761 printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
762 n_array, fname, name,
763 priv->firm_version >> 8, priv->firm_version & 0xff);
765 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
766 if (priv->firm == NULL) {
767 printk("Not enough memory to load firmware file.\n");
771 priv->firm_size = n_array;
781 printk("More firmware images in file than "
786 /* Checks if there's enough bytes to read */
787 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
790 type = get_unaligned_le32(p);
793 id = get_unaligned_le64(p);
797 int_freq = get_unaligned_le16(p);
798 p += sizeof(int_freq);
799 if (endp - p < sizeof(size))
803 size = get_unaligned_le32(p);
806 if (!size || size > endp - p) {
807 printk("Firmware type ");
808 // dump_firm_type(type);
809 printk("(%x), id %llx is corrupted "
810 "(size=%d, expected %d)\n",
811 type, (unsigned long long)id,
812 (unsigned)(endp - p), size);
816 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
817 if (priv->firm[n].ptr == NULL) {
818 printk("Not enough memory to load firmware file.\n");
822 printk("Reading firmware type ");
824 // dump_firm_type_and_int_freq(type, int_freq);
825 printk("(%x), id %llx, size=%d.\n",
826 type, (unsigned long long)id, size);
829 memcpy(priv->firm[n].ptr, p, size);
830 priv->firm[n].type = type;
831 priv->firm[n].id = id;
832 priv->firm[n].size = size;
833 priv->firm[n].int_freq = int_freq;
838 if (n + 1 != priv->firm_size) {
839 printk("Firmware file is incomplete!\n");
846 printk("Firmware header is incomplete!\n");
849 printk("Error: firmware file is corrupted!\n");
852 printk("Releasing partially loaded firmware file.\n");
853 // free_firmware(priv);
856 release_firmware(fw);
858 printk("Firmware files loaded.\n");
864 static void xc_debug_dump(struct xc4000_priv *priv)
867 u32 freq_error_hz = 0;
869 u32 hsync_freq_hz = 0;
872 u8 hw_majorversion = 0, hw_minorversion = 0;
873 u8 fw_majorversion = 0, fw_minorversion = 0;
874 // u16 fw_buildversion = 0;
876 /* Wait for stats to stabilize.
877 * Frame Lines needs two frame times after initial lock
878 * before it is valid.
882 xc_get_ADC_Envelope(priv, &adc_envelope);
883 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
885 xc_get_frequency_error(priv, &freq_error_hz);
886 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
888 xc_get_lock_status(priv, &lock_status);
889 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
892 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
893 &fw_majorversion, &fw_minorversion);
895 // xc_get_buildversion(priv, &fw_buildversion);
896 // dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
897 // hw_majorversion, hw_minorversion,
898 // fw_majorversion, fw_minorversion, fw_buildversion);
900 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
901 hw_majorversion, hw_minorversion,
902 fw_majorversion, fw_minorversion);
904 xc_get_hsync_freq(priv, &hsync_freq_hz);
905 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
907 xc_get_frame_lines(priv, &frame_lines);
908 dprintk(1, "*** Frame lines = %d\n", frame_lines);
910 xc_get_quality(priv, &quality);
911 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
914 static int xc4000_set_params(struct dvb_frontend *fe,
915 struct dvb_frontend_parameters *params)
917 struct xc4000_priv *priv = fe->tuner_priv;
920 if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
921 xc_load_fw_and_init_tuner(fe);
923 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
925 if (fe->ops.info.type == FE_ATSC) {
926 dprintk(1, "%s() ATSC\n", __func__);
927 switch (params->u.vsb.modulation) {
930 dprintk(1, "%s() VSB modulation\n", __func__);
931 priv->rf_mode = XC_RF_MODE_AIR;
932 priv->freq_hz = params->frequency - 1750000;
933 priv->bandwidth = BANDWIDTH_6_MHZ;
934 priv->video_standard = DTV6;
939 dprintk(1, "%s() QAM modulation\n", __func__);
940 priv->rf_mode = XC_RF_MODE_CABLE;
941 priv->freq_hz = params->frequency - 1750000;
942 priv->bandwidth = BANDWIDTH_6_MHZ;
943 priv->video_standard = DTV6;
948 } else if (fe->ops.info.type == FE_OFDM) {
949 dprintk(1, "%s() OFDM\n", __func__);
950 switch (params->u.ofdm.bandwidth) {
951 case BANDWIDTH_6_MHZ:
952 priv->bandwidth = BANDWIDTH_6_MHZ;
953 priv->video_standard = DTV6;
954 priv->freq_hz = params->frequency - 1750000;
956 case BANDWIDTH_7_MHZ:
957 printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
959 case BANDWIDTH_8_MHZ:
960 priv->bandwidth = BANDWIDTH_8_MHZ;
961 priv->video_standard = DTV8;
962 priv->freq_hz = params->frequency - 2750000;
965 printk(KERN_ERR "xc4000 bandwidth not set!\n");
968 priv->rf_mode = XC_RF_MODE_AIR;
970 printk(KERN_ERR "xc4000 modulation type not supported!\n");
974 dprintk(1, "%s() frequency=%d (compensated)\n",
975 __func__, priv->freq_hz);
977 ret = xc_SetSignalSource(priv, priv->rf_mode);
978 if (ret != XC_RESULT_SUCCESS) {
980 "xc4000: xc_SetSignalSource(%d) failed\n",
985 ret = xc_SetTVStandard(priv,
986 XC4000_Standard[priv->video_standard].VideoMode,
987 XC4000_Standard[priv->video_standard].AudioMode);
988 if (ret != XC_RESULT_SUCCESS) {
989 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
993 ret = xc_set_IF_frequency(priv, priv->if_khz);
994 if (ret != XC_RESULT_SUCCESS) {
995 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
1000 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1003 xc_debug_dump(priv);
1008 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1010 struct xc4000_priv *priv = fe->tuner_priv;
1014 ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1015 if (ret == XC_RESULT_SUCCESS) {
1016 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1017 ret = XC_RESULT_RESET_FAILURE;
1019 ret = XC_RESULT_SUCCESS;
1022 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1023 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1027 static int xc4000_set_analog_params(struct dvb_frontend *fe,
1028 struct analog_parameters *params)
1030 struct xc4000_priv *priv = fe->tuner_priv;
1033 if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
1034 xc_load_fw_and_init_tuner(fe);
1036 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1037 __func__, params->frequency);
1039 /* Fix me: it could be air. */
1040 priv->rf_mode = params->mode;
1041 if (params->mode > XC_RF_MODE_CABLE)
1042 priv->rf_mode = XC_RF_MODE_CABLE;
1044 /* params->frequency is in units of 62.5khz */
1045 priv->freq_hz = params->frequency * 62500;
1047 /* FIX ME: Some video standards may have several possible audio
1048 standards. We simply default to one of them here.
1050 if (params->std & V4L2_STD_MN) {
1051 /* default to BTSC audio standard */
1052 priv->video_standard = MN_NTSC_PAL_BTSC;
1056 if (params->std & V4L2_STD_PAL_BG) {
1057 /* default to NICAM audio standard */
1058 priv->video_standard = BG_PAL_NICAM;
1062 if (params->std & V4L2_STD_PAL_I) {
1063 /* default to NICAM audio standard */
1064 priv->video_standard = I_PAL_NICAM;
1068 if (params->std & V4L2_STD_PAL_DK) {
1069 /* default to NICAM audio standard */
1070 priv->video_standard = DK_PAL_NICAM;
1074 if (params->std & V4L2_STD_SECAM_DK) {
1075 /* default to A2 DK1 audio standard */
1076 priv->video_standard = DK_SECAM_A2DK1;
1080 if (params->std & V4L2_STD_SECAM_L) {
1081 priv->video_standard = L_SECAM_NICAM;
1085 if (params->std & V4L2_STD_SECAM_LC) {
1086 priv->video_standard = LC_SECAM_NICAM;
1091 ret = xc_SetSignalSource(priv, priv->rf_mode);
1092 if (ret != XC_RESULT_SUCCESS) {
1094 "xc4000: xc_SetSignalSource(%d) failed\n",
1099 ret = xc_SetTVStandard(priv,
1100 XC4000_Standard[priv->video_standard].VideoMode,
1101 XC4000_Standard[priv->video_standard].AudioMode);
1102 if (ret != XC_RESULT_SUCCESS) {
1103 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1107 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1110 xc_debug_dump(priv);
1115 static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1117 struct xc4000_priv *priv = fe->tuner_priv;
1118 dprintk(1, "%s()\n", __func__);
1119 *freq = priv->freq_hz;
1123 static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1125 struct xc4000_priv *priv = fe->tuner_priv;
1126 dprintk(1, "%s()\n", __func__);
1128 *bw = priv->bandwidth;
1132 static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1134 struct xc4000_priv *priv = fe->tuner_priv;
1135 u16 lock_status = 0;
1137 xc_get_lock_status(priv, &lock_status);
1139 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1141 *status = lock_status;
1146 static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe)
1148 struct xc4000_priv *priv = fe->tuner_priv;
1151 if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS) {
1152 ret = xc4000_fwupload(fe);
1153 if (ret != XC_RESULT_SUCCESS)
1157 /* Start the tuner self-calibration process */
1158 ret |= xc_initialize(priv);
1160 /* Wait for calibration to complete.
1161 * We could continue but XC4000 will clock stretch subsequent
1162 * I2C transactions until calibration is complete. This way we
1163 * don't have to rely on clock stretching working.
1167 /* Default to "CABLE" mode */
1168 ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
1173 static int xc4000_sleep(struct dvb_frontend *fe)
1177 dprintk(1, "%s()\n", __func__);
1179 /* Avoid firmware reload on slow devices */
1183 /* According to Xceive technical support, the "powerdown" register
1184 was removed in newer versions of the firmware. The "supported"
1185 way to sleep the tuner is to pull the reset pin low for 10ms */
1186 ret = xc4000_TunerReset(fe);
1187 if (ret != XC_RESULT_SUCCESS) {
1189 "xc4000: %s() unable to shutdown tuner\n",
1193 return XC_RESULT_SUCCESS;
1196 static int xc4000_init(struct dvb_frontend *fe)
1198 struct xc4000_priv *priv = fe->tuner_priv;
1199 dprintk(1, "%s()\n", __func__);
1201 if (xc_load_fw_and_init_tuner(fe) != XC_RESULT_SUCCESS) {
1202 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1207 xc_debug_dump(priv);
1212 static int xc4000_release(struct dvb_frontend *fe)
1214 struct xc4000_priv *priv = fe->tuner_priv;
1216 dprintk(1, "%s()\n", __func__);
1218 mutex_lock(&xc4000_list_mutex);
1221 hybrid_tuner_release_state(priv);
1223 mutex_unlock(&xc4000_list_mutex);
1225 fe->tuner_priv = NULL;
1230 static const struct dvb_tuner_ops xc4000_tuner_ops = {
1232 .name = "Xceive XC4000",
1233 .frequency_min = 1000000,
1234 .frequency_max = 1023000000,
1235 .frequency_step = 50000,
1238 .release = xc4000_release,
1239 .init = xc4000_init,
1240 .sleep = xc4000_sleep,
1242 .set_params = xc4000_set_params,
1243 .set_analog_params = xc4000_set_analog_params,
1244 .get_frequency = xc4000_get_frequency,
1245 .get_bandwidth = xc4000_get_bandwidth,
1246 .get_status = xc4000_get_status
1249 struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1250 struct i2c_adapter *i2c,
1251 struct xc4000_config *cfg)
1253 struct xc4000_priv *priv = NULL;
1259 dprintk(1, "%s(%d-%04x)\n", __func__,
1260 i2c ? i2c_adapter_id(i2c) : -1,
1261 cfg ? cfg->i2c_address : -1);
1263 mutex_lock(&xc4000_list_mutex);
1265 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1266 hybrid_tuner_instance_list,
1267 i2c, cfg->i2c_address, "xc4000");
1273 /* new tuner instance */
1274 priv->bandwidth = BANDWIDTH_6_MHZ;
1275 fe->tuner_priv = priv;
1278 /* existing tuner instance */
1279 fe->tuner_priv = priv;
1283 if (priv->if_khz == 0) {
1284 /* If the IF hasn't been set yet, use the value provided by
1285 the caller (occurs in hybrid devices where the analog
1286 call to xc4000_attach occurs before the digital side) */
1287 priv->if_khz = cfg->if_khz;
1290 /* Check if firmware has been loaded. It is possible that another
1291 instance of the driver has loaded the firmware.
1294 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1298 case XC_PRODUCT_ID_FW_LOADED:
1300 "xc4000: Successfully identified at address 0x%02x\n",
1303 "xc4000: Firmware has been loaded previously\n");
1305 case XC_PRODUCT_ID_FW_NOT_LOADED:
1307 "xc4000: Successfully identified at address 0x%02x\n",
1310 "xc4000: Firmware has not been loaded previously\n");
1314 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1315 cfg->i2c_address, id);
1319 mutex_unlock(&xc4000_list_mutex);
1321 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1322 sizeof(struct dvb_tuner_ops));
1324 /* FIXME: For now, load the firmware at startup. We will remove this
1325 before the code goes to production... */
1326 xc4000_fwupload(fe);
1327 printk("xc4000_fwupload done\n");
1330 // rc = load_firmware(fe, BASE | new_fw.type, &std0);
1331 rc = load_firmware(fe, BASE, &std0);
1332 if (rc != XC_RESULT_SUCCESS) {
1333 tuner_err("Error %d while loading base firmware\n",
1338 /* Load INIT1, if needed */
1339 tuner_dbg("Load init1 firmware, if exists\n");
1341 // rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1342 rc = load_firmware(fe, INIT1, &std0);
1343 printk("init1 load result %x\n", rc);
1345 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1347 printk("djh id is now %x\n", id);
1351 mutex_unlock(&xc4000_list_mutex);
1356 EXPORT_SYMBOL(xc4000_attach);
1358 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1359 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1360 MODULE_LICENSE("GPL");