[media] xc4000: debug message improvements
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / common / tuners / xc4000.c
blob24afebb933316d248be6924ffdd881ebf61c698c
1 /*
2 * Driver for Xceive XC4000 "QAM/8VSB single chip tuner"
4 * Copyright (c) 2007 Xceive Corporation
5 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 * Copyright (c) 2009 Davide Ferri <d.ferri@zero11.it>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/videodev2.h>
28 #include <linux/delay.h>
29 #include <linux/dvb/frontend.h>
30 #include <linux/i2c.h>
31 #include <linux/mutex.h>
32 #include <asm/unaligned.h>
34 #include "dvb_frontend.h"
36 #include "xc4000.h"
37 #include "tuner-i2c.h"
38 #include "tuner-xc2028-types.h"
40 static int debug;
41 module_param(debug, int, 0644);
42 MODULE_PARM_DESC(debug, "\n\t\tDebugging level (0 to 2, default: 0 (off)).");
44 static int no_poweroff;
45 module_param(no_poweroff, int, 0644);
46 MODULE_PARM_DESC(no_poweroff, "\n\t\t1: keep device energized and with tuner "
47 "ready all the times.\n"
48 "\t\tFaster, but consumes more power and keeps the device hotter.\n"
49 "\t\t2: powers device off when not used.\n"
50 "\t\t0 (default): use device-specific default mode.");
52 #define XC4000_DEFAULT_FIRMWARE "xc4000.fw"
54 static char firmware_name[30];
55 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
56 MODULE_PARM_DESC(firmware_name, "\n\t\tFirmware file name. Allows overriding "
57 "the default firmware\n"
58 "\t\tname.");
60 static DEFINE_MUTEX(xc4000_list_mutex);
61 static LIST_HEAD(hybrid_tuner_instance_list);
63 #define dprintk(level, fmt, arg...) if (debug >= level) \
64 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
66 /* struct for storing firmware table */
67 struct firmware_description {
68 unsigned int type;
69 v4l2_std_id id;
70 __u16 int_freq;
71 unsigned char *ptr;
72 unsigned int size;
75 struct firmware_properties {
76 unsigned int type;
77 v4l2_std_id id;
78 v4l2_std_id std_req;
79 __u16 int_freq;
80 unsigned int scode_table;
81 int scode_nr;
84 struct xc4000_priv {
85 struct tuner_i2c_props i2c_props;
86 struct list_head hybrid_tuner_instance_list;
87 struct firmware_description *firm;
88 int firm_size;
89 __u16 firm_version;
90 u32 if_khz;
91 u32 freq_hz;
92 u32 bandwidth;
93 u8 video_standard;
94 u8 rf_mode;
95 u8 card_type;
96 u8 ignore_i2c_write_errors;
97 /* struct xc2028_ctrl ctrl; */
98 struct firmware_properties cur_fw;
99 __u16 hwmodel;
100 __u16 hwvers;
101 struct mutex lock;
104 /* Misc Defines */
105 #define MAX_TV_STANDARD 24
106 #define XC_MAX_I2C_WRITE_LENGTH 64
107 #define XC_POWERED_DOWN 0x80000000U
109 /* Signal Types */
110 #define XC_RF_MODE_AIR 0
111 #define XC_RF_MODE_CABLE 1
113 /* Result codes */
114 #define XC_RESULT_SUCCESS 0
115 #define XC_RESULT_RESET_FAILURE 1
116 #define XC_RESULT_I2C_WRITE_FAILURE 2
117 #define XC_RESULT_I2C_READ_FAILURE 3
118 #define XC_RESULT_OUT_OF_RANGE 5
120 /* Product id */
121 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
122 #define XC_PRODUCT_ID_FW_LOADED 0x0FA0
124 /* Registers (Write-only) */
125 #define XREG_INIT 0x00
126 #define XREG_VIDEO_MODE 0x01
127 #define XREG_AUDIO_MODE 0x02
128 #define XREG_RF_FREQ 0x03
129 #define XREG_D_CODE 0x04
130 #define XREG_DIRECTSITTING_MODE 0x05
131 #define XREG_SEEK_MODE 0x06
132 #define XREG_POWER_DOWN 0x08
133 #define XREG_SIGNALSOURCE 0x0A
134 #define XREG_AMPLITUDE 0x10
136 /* Registers (Read-only) */
137 #define XREG_ADC_ENV 0x00
138 #define XREG_QUALITY 0x01
139 #define XREG_FRAME_LINES 0x02
140 #define XREG_HSYNC_FREQ 0x03
141 #define XREG_LOCK 0x04
142 #define XREG_FREQ_ERROR 0x05
143 #define XREG_SNR 0x06
144 #define XREG_VERSION 0x07
145 #define XREG_PRODUCT_ID 0x08
148 Basic firmware description. This will remain with
149 the driver for documentation purposes.
151 This represents an I2C firmware file encoded as a
152 string of unsigned char. Format is as follows:
154 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
155 char[1 ]=len0_LSB -> length of first write transaction
156 char[2 ]=data0 -> first byte to be sent
157 char[3 ]=data1
158 char[4 ]=data2
159 char[ ]=...
160 char[M ]=dataN -> last byte to be sent
161 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
162 char[M+2]=len1_LSB -> length of second write transaction
163 char[M+3]=data0
164 char[M+4]=data1
166 etc.
168 The [len] value should be interpreted as follows:
170 len= len_MSB _ len_LSB
171 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
172 len=0000_0000_0000_0000 : Reset command: Do hardware reset
173 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
174 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
176 For the RESET and WAIT commands, the two following bytes will contain
177 immediately the length of the following transaction.
180 struct XC_TV_STANDARD {
181 const char *Name;
182 u16 AudioMode;
183 u16 VideoMode;
184 u16 int_freq;
187 /* Tuner standards */
188 #define XC4000_MN_NTSC_PAL_BTSC 0
189 #define XC4000_MN_NTSC_PAL_A2 1
190 #define XC4000_MN_NTSC_PAL_EIAJ 2
191 #define XC4000_MN_NTSC_PAL_Mono 3
192 #define XC4000_BG_PAL_A2 4
193 #define XC4000_BG_PAL_NICAM 5
194 #define XC4000_BG_PAL_MONO 6
195 #define XC4000_I_PAL_NICAM 7
196 #define XC4000_I_PAL_NICAM_MONO 8
197 #define XC4000_DK_PAL_A2 9
198 #define XC4000_DK_PAL_NICAM 10
199 #define XC4000_DK_PAL_MONO 11
200 #define XC4000_DK_SECAM_A2DK1 12
201 #define XC4000_DK_SECAM_A2LDK3 13
202 #define XC4000_DK_SECAM_A2MONO 14
203 #define XC4000_DK_SECAM_NICAM 15
204 #define XC4000_L_SECAM_NICAM 16
205 #define XC4000_LC_SECAM_NICAM 17
206 #define XC4000_DTV6 18
207 #define XC4000_DTV8 19
208 #define XC4000_DTV7_8 20
209 #define XC4000_DTV7 21
210 #define XC4000_FM_Radio_INPUT2 22
211 #define XC4000_FM_Radio_INPUT1 23
213 static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
214 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
215 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
216 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
217 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
218 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
219 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
220 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
221 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
222 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
223 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
224 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
225 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
226 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
227 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
228 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
229 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
230 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
231 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
232 {"DTV6", 0x00C0, 0x8002, 0},
233 {"DTV8", 0x00C0, 0x800B, 0},
234 {"DTV7/8", 0x00C0, 0x801B, 0},
235 {"DTV7", 0x00C0, 0x8007, 0},
236 {"FM Radio-INPUT2", 0x0008, 0x9800,10700},
237 {"FM Radio-INPUT1", 0x0008, 0x9000,10700}
240 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
241 static int xc4000_TunerReset(struct dvb_frontend *fe);
242 static void xc_debug_dump(struct xc4000_priv *priv);
244 static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
246 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
247 .flags = 0, .buf = buf, .len = len };
248 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
249 if (priv->ignore_i2c_write_errors == 0) {
250 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
251 len);
252 if (len == 4) {
253 printk("bytes %02x %02x %02x %02x\n", buf[0],
254 buf[1], buf[2], buf[3]);
256 return XC_RESULT_I2C_WRITE_FAILURE;
259 return XC_RESULT_SUCCESS;
262 static void xc_wait(int wait_ms)
264 msleep(wait_ms);
267 static int xc4000_TunerReset(struct dvb_frontend *fe)
269 struct xc4000_priv *priv = fe->tuner_priv;
270 int ret;
272 dprintk(1, "%s()\n", __func__);
274 if (fe->callback) {
275 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
276 fe->dvb->priv :
277 priv->i2c_props.adap->algo_data,
278 DVB_FRONTEND_COMPONENT_TUNER,
279 XC4000_TUNER_RESET, 0);
280 if (ret) {
281 printk(KERN_ERR "xc4000: reset failed\n");
282 return XC_RESULT_RESET_FAILURE;
284 } else {
285 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
286 return XC_RESULT_RESET_FAILURE;
288 return XC_RESULT_SUCCESS;
291 static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
293 u8 buf[4];
294 int result;
296 buf[0] = (regAddr >> 8) & 0xFF;
297 buf[1] = regAddr & 0xFF;
298 buf[2] = (i2cData >> 8) & 0xFF;
299 buf[3] = i2cData & 0xFF;
300 result = xc_send_i2c_data(priv, buf, 4);
302 return result;
305 static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
307 struct xc4000_priv *priv = fe->tuner_priv;
309 int i, nbytes_to_send, result;
310 unsigned int len, pos, index;
311 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
313 index = 0;
314 while ((i2c_sequence[index] != 0xFF) ||
315 (i2c_sequence[index + 1] != 0xFF)) {
316 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
317 if (len == 0x0000) {
318 /* RESET command */
319 result = xc4000_TunerReset(fe);
320 index += 2;
321 if (result != XC_RESULT_SUCCESS)
322 return result;
323 } else if (len & 0x8000) {
324 /* WAIT command */
325 xc_wait(len & 0x7FFF);
326 index += 2;
327 } else {
328 /* Send i2c data whilst ensuring individual transactions
329 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
331 index += 2;
332 buf[0] = i2c_sequence[index];
333 buf[1] = i2c_sequence[index + 1];
334 pos = 2;
335 while (pos < len) {
336 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
337 nbytes_to_send =
338 XC_MAX_I2C_WRITE_LENGTH;
339 else
340 nbytes_to_send = (len - pos + 2);
341 for (i = 2; i < nbytes_to_send; i++) {
342 buf[i] = i2c_sequence[index + pos +
343 i - 2];
345 result = xc_send_i2c_data(priv, buf,
346 nbytes_to_send);
348 if (result != XC_RESULT_SUCCESS)
349 return result;
351 pos += nbytes_to_send - 2;
353 index += len;
356 return XC_RESULT_SUCCESS;
359 static int xc_SetTVStandard(struct xc4000_priv *priv,
360 u16 VideoMode, u16 AudioMode)
362 int ret;
363 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
364 dprintk(1, "%s() Standard = %s\n",
365 __func__,
366 XC4000_Standard[priv->video_standard].Name);
368 /* Don't complain when the request fails because of i2c stretching */
369 priv->ignore_i2c_write_errors = 1;
371 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
372 if (ret == XC_RESULT_SUCCESS)
373 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
375 priv->ignore_i2c_write_errors = 0;
377 return ret;
380 static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
382 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
383 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
385 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
386 rf_mode = XC_RF_MODE_CABLE;
387 printk(KERN_ERR
388 "%s(), Invalid mode, defaulting to CABLE",
389 __func__);
391 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
394 static const struct dvb_tuner_ops xc4000_tuner_ops;
396 static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
398 u16 freq_code;
400 dprintk(1, "%s(%u)\n", __func__, freq_hz);
402 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
403 (freq_hz < xc4000_tuner_ops.info.frequency_min))
404 return XC_RESULT_OUT_OF_RANGE;
406 freq_code = (u16)(freq_hz / 15625);
408 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
409 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
410 only be used for fast scanning for channel lock) */
411 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
414 static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
416 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
419 static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
421 int result;
422 u16 regData;
423 u32 tmp;
425 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
426 if (result != XC_RESULT_SUCCESS)
427 return result;
429 tmp = (u32)regData & 0xFFFFU;
430 tmp = (tmp < 0x8000U ? tmp : 0x10000U - tmp);
431 (*freq_error_hz) = tmp * 15625;
432 return result;
435 static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
437 return xc4000_readreg(priv, XREG_LOCK, lock_status);
440 static int xc_get_version(struct xc4000_priv *priv,
441 u8 *hw_majorversion, u8 *hw_minorversion,
442 u8 *fw_majorversion, u8 *fw_minorversion)
444 u16 data;
445 int result;
447 result = xc4000_readreg(priv, XREG_VERSION, &data);
448 if (result != XC_RESULT_SUCCESS)
449 return result;
451 (*hw_majorversion) = (data >> 12) & 0x0F;
452 (*hw_minorversion) = (data >> 8) & 0x0F;
453 (*fw_majorversion) = (data >> 4) & 0x0F;
454 (*fw_minorversion) = data & 0x0F;
456 return 0;
459 static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
461 u16 regData;
462 int result;
464 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
465 if (result != XC_RESULT_SUCCESS)
466 return result;
468 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
469 return result;
472 static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
474 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
477 static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
479 return xc4000_readreg(priv, XREG_QUALITY, quality);
482 static u16 WaitForLock(struct xc4000_priv *priv)
484 u16 lockState = 0;
485 int watchDogCount = 40;
487 while ((lockState == 0) && (watchDogCount > 0)) {
488 xc_get_lock_status(priv, &lockState);
489 if (lockState != 1) {
490 xc_wait(5);
491 watchDogCount--;
494 return lockState;
497 #define XC_TUNE_ANALOG 0
498 #define XC_TUNE_DIGITAL 1
499 static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
501 int found = 0;
502 int result = 0;
504 dprintk(1, "%s(%u)\n", __func__, freq_hz);
506 /* Don't complain when the request fails because of i2c stretching */
507 priv->ignore_i2c_write_errors = 1;
508 result = xc_set_RF_frequency(priv, freq_hz);
509 priv->ignore_i2c_write_errors = 0;
511 if (result != XC_RESULT_SUCCESS)
512 return 0;
514 if (mode == XC_TUNE_ANALOG) {
515 if (WaitForLock(priv) == 1)
516 found = 1;
519 /* Wait for stats to stabilize.
520 * Frame Lines needs two frame times after initial lock
521 * before it is valid.
523 xc_wait(debug ? 100 : 10);
525 if (debug)
526 xc_debug_dump(priv);
528 return found;
531 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
533 u8 buf[2] = { reg >> 8, reg & 0xff };
534 u8 bval[2] = { 0, 0 };
535 struct i2c_msg msg[2] = {
536 { .addr = priv->i2c_props.addr,
537 .flags = 0, .buf = &buf[0], .len = 2 },
538 { .addr = priv->i2c_props.addr,
539 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
542 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
543 printk(KERN_WARNING "xc4000: I2C read failed\n");
544 return -EREMOTEIO;
547 *val = (bval[0] << 8) | bval[1];
548 return XC_RESULT_SUCCESS;
551 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
552 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
554 if (type & BASE)
555 printk("BASE ");
556 if (type & INIT1)
557 printk("INIT1 ");
558 if (type & F8MHZ)
559 printk("F8MHZ ");
560 if (type & MTS)
561 printk("MTS ");
562 if (type & D2620)
563 printk("D2620 ");
564 if (type & D2633)
565 printk("D2633 ");
566 if (type & DTV6)
567 printk("DTV6 ");
568 if (type & QAM)
569 printk("QAM ");
570 if (type & DTV7)
571 printk("DTV7 ");
572 if (type & DTV78)
573 printk("DTV78 ");
574 if (type & DTV8)
575 printk("DTV8 ");
576 if (type & FM)
577 printk("FM ");
578 if (type & INPUT1)
579 printk("INPUT1 ");
580 if (type & LCD)
581 printk("LCD ");
582 if (type & NOGD)
583 printk("NOGD ");
584 if (type & MONO)
585 printk("MONO ");
586 if (type & ATSC)
587 printk("ATSC ");
588 if (type & IF)
589 printk("IF ");
590 if (type & LG60)
591 printk("LG60 ");
592 if (type & ATI638)
593 printk("ATI638 ");
594 if (type & OREN538)
595 printk("OREN538 ");
596 if (type & OREN36)
597 printk("OREN36 ");
598 if (type & TOYOTA388)
599 printk("TOYOTA388 ");
600 if (type & TOYOTA794)
601 printk("TOYOTA794 ");
602 if (type & DIBCOM52)
603 printk("DIBCOM52 ");
604 if (type & ZARLINK456)
605 printk("ZARLINK456 ");
606 if (type & CHINA)
607 printk("CHINA ");
608 if (type & F6MHZ)
609 printk("F6MHZ ");
610 if (type & INPUT2)
611 printk("INPUT2 ");
612 if (type & SCODE)
613 printk("SCODE ");
614 if (type & HAS_IF)
615 printk("HAS_IF_%d ", int_freq);
618 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
619 v4l2_std_id *id)
621 struct xc4000_priv *priv = fe->tuner_priv;
622 int i, best_i = -1;
623 unsigned int best_nr_diffs = 255U;
625 if (!priv->firm) {
626 printk("Error! firmware not loaded\n");
627 return -EINVAL;
630 if (((type & ~SCODE) == 0) && (*id == 0))
631 *id = V4L2_STD_PAL;
633 /* Seek for generic video standard match */
634 for (i = 0; i < priv->firm_size; i++) {
635 v4l2_std_id id_diff_mask =
636 (priv->firm[i].id ^ (*id)) & (*id);
637 unsigned int type_diff_mask =
638 (priv->firm[i].type ^ type)
639 & (BASE_TYPES | DTV_TYPES | LCD | NOGD | MONO | SCODE);
640 unsigned int nr_diffs;
642 if (type_diff_mask
643 & (BASE | INIT1 | FM | DTV6 | DTV7 | DTV78 | DTV8 | SCODE))
644 continue;
646 nr_diffs = hweight64(id_diff_mask) + hweight32(type_diff_mask);
647 if (!nr_diffs) /* Supports all the requested standards */
648 goto found;
650 if (nr_diffs < best_nr_diffs) {
651 best_nr_diffs = nr_diffs;
652 best_i = i;
656 /* FIXME: Would make sense to seek for type "hint" match ? */
657 if (best_i < 0) {
658 i = -ENOENT;
659 goto ret;
662 if (best_nr_diffs > 0U) {
663 printk("Selecting best matching firmware (%u bits differ) for "
664 "type=", best_nr_diffs);
665 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
666 i = best_i;
669 found:
670 *id = priv->firm[i].id;
672 ret:
673 if (debug) {
674 printk("%s firmware for type=", (i < 0) ? "Can't find" :
675 "Found");
676 dump_firm_type(type);
677 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
679 return i;
682 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
683 v4l2_std_id *id)
685 struct xc4000_priv *priv = fe->tuner_priv;
686 int pos, rc;
687 unsigned char *p;
689 pos = seek_firmware(fe, type, id);
690 if (pos < 0)
691 return pos;
693 p = priv->firm[pos].ptr;
695 /* Don't complain when the request fails because of i2c stretching */
696 priv->ignore_i2c_write_errors = 1;
698 rc = xc_load_i2c_sequence(fe, p);
700 priv->ignore_i2c_write_errors = 0;
702 return rc;
705 static int xc4000_fwupload(struct dvb_frontend *fe)
707 struct xc4000_priv *priv = fe->tuner_priv;
708 const struct firmware *fw = NULL;
709 const unsigned char *p, *endp;
710 int rc = 0;
711 int n, n_array;
712 char name[33];
713 const char *fname;
715 if (firmware_name[0] != '\0')
716 fname = firmware_name;
717 else
718 fname = XC4000_DEFAULT_FIRMWARE;
720 printk("Reading firmware %s\n", fname);
721 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
722 if (rc < 0) {
723 if (rc == -ENOENT)
724 printk("Error: firmware %s not found.\n",
725 fname);
726 else
727 printk("Error %d while requesting firmware %s \n",
728 rc, fname);
730 return rc;
732 p = fw->data;
733 endp = p + fw->size;
735 if (fw->size < sizeof(name) - 1 + 2 + 2) {
736 printk("Error: firmware file %s has invalid size!\n",
737 fname);
738 goto corrupt;
741 memcpy(name, p, sizeof(name) - 1);
742 name[sizeof(name) - 1] = 0;
743 p += sizeof(name) - 1;
745 priv->firm_version = get_unaligned_le16(p);
746 p += 2;
748 n_array = get_unaligned_le16(p);
749 p += 2;
751 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
752 n_array, fname, name,
753 priv->firm_version >> 8, priv->firm_version & 0xff);
755 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
756 if (priv->firm == NULL) {
757 printk("Not enough memory to load firmware file.\n");
758 rc = -ENOMEM;
759 goto err;
761 priv->firm_size = n_array;
763 n = -1;
764 while (p < endp) {
765 __u32 type, size;
766 v4l2_std_id id;
767 __u16 int_freq = 0;
769 n++;
770 if (n >= n_array) {
771 printk("More firmware images in file than "
772 "were expected!\n");
773 goto corrupt;
776 /* Checks if there's enough bytes to read */
777 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
778 goto header;
780 type = get_unaligned_le32(p);
781 p += sizeof(type);
783 id = get_unaligned_le64(p);
784 p += sizeof(id);
786 if (type & HAS_IF) {
787 int_freq = get_unaligned_le16(p);
788 p += sizeof(int_freq);
789 if (endp - p < sizeof(size))
790 goto header;
793 size = get_unaligned_le32(p);
794 p += sizeof(size);
796 if (!size || size > endp - p) {
797 printk("Firmware type (%x), id %llx is corrupted "
798 "(size=%d, expected %d)\n",
799 type, (unsigned long long)id,
800 (unsigned)(endp - p), size);
801 goto corrupt;
804 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
805 if (priv->firm[n].ptr == NULL) {
806 printk("Not enough memory to load firmware file.\n");
807 rc = -ENOMEM;
808 goto err;
811 if (debug) {
812 printk("Reading firmware type ");
813 dump_firm_type_and_int_freq(type, int_freq);
814 printk("(%x), id %llx, size=%d.\n",
815 type, (unsigned long long)id, size);
818 memcpy(priv->firm[n].ptr, p, size);
819 priv->firm[n].type = type;
820 priv->firm[n].id = id;
821 priv->firm[n].size = size;
822 priv->firm[n].int_freq = int_freq;
824 p += size;
827 if (n + 1 != priv->firm_size) {
828 printk("Firmware file is incomplete!\n");
829 goto corrupt;
832 goto done;
834 header:
835 printk("Firmware header is incomplete!\n");
836 corrupt:
837 rc = -EINVAL;
838 printk("Error: firmware file is corrupted!\n");
840 err:
841 printk("Releasing partially loaded firmware file.\n");
843 done:
844 release_firmware(fw);
845 if (rc == 0)
846 dprintk(1, "Firmware files loaded.\n");
848 return rc;
851 static int load_scode(struct dvb_frontend *fe, unsigned int type,
852 v4l2_std_id *id, __u16 int_freq, int scode)
854 struct xc4000_priv *priv = fe->tuner_priv;
855 int pos, rc;
856 unsigned char *p;
857 u8 scode_buf[13];
858 u8 indirect_mode[5];
860 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
862 if (!int_freq) {
863 pos = seek_firmware(fe, type, id);
864 if (pos < 0)
865 return pos;
866 } else {
867 for (pos = 0; pos < priv->firm_size; pos++) {
868 if ((priv->firm[pos].int_freq == int_freq) &&
869 (priv->firm[pos].type & HAS_IF))
870 break;
872 if (pos == priv->firm_size)
873 return -ENOENT;
876 p = priv->firm[pos].ptr;
878 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
879 return -EINVAL;
880 p += 12 * scode;
882 tuner_info("Loading SCODE for type=");
883 dump_firm_type_and_int_freq(priv->firm[pos].type,
884 priv->firm[pos].int_freq);
885 printk("(%x), id %016llx.\n", priv->firm[pos].type,
886 (unsigned long long)*id);
888 scode_buf[0] = 0x00;
889 memcpy(&scode_buf[1], p, 12);
891 /* Enter direct-mode */
892 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
893 if (rc < 0) {
894 printk("failed to put device into direct mode!\n");
895 return -EIO;
898 rc = xc_send_i2c_data(priv, scode_buf, 13);
899 if (rc != XC_RESULT_SUCCESS) {
900 /* Even if the send failed, make sure we set back to indirect
901 mode */
902 printk("Failed to set scode %d\n", rc);
905 /* Switch back to indirect-mode */
906 memset(indirect_mode, 0, sizeof(indirect_mode));
907 indirect_mode[4] = 0x88;
908 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
909 msleep(10);
911 return 0;
914 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
915 v4l2_std_id std, __u16 int_freq)
917 struct xc4000_priv *priv = fe->tuner_priv;
918 struct firmware_properties new_fw;
919 int rc = 0, is_retry = 0;
920 u16 version = 0, hwmodel;
921 v4l2_std_id std0;
922 u8 hw_major, hw_minor, fw_major, fw_minor;
924 dprintk(1, "%s called\n", __func__);
926 if (!priv->firm) {
927 rc = xc4000_fwupload(fe);
928 if (rc < 0)
929 return rc;
932 #ifdef DJH_DEBUG
933 if (priv->ctrl.mts && !(type & FM))
934 type |= MTS;
935 #endif
937 retry:
938 new_fw.type = type;
939 new_fw.id = std;
940 new_fw.std_req = std;
941 new_fw.scode_table = SCODE /* | priv->ctrl.scode_table */;
942 new_fw.scode_nr = 0;
943 new_fw.int_freq = int_freq;
945 dprintk(1, "checking firmware, user requested type=");
946 if (debug) {
947 dump_firm_type(new_fw.type);
948 printk("(%x), id %016llx, ", new_fw.type,
949 (unsigned long long)new_fw.std_req);
950 if (!int_freq) {
951 printk("scode_tbl ");
952 #ifdef DJH_DEBUG
953 dump_firm_type(priv->ctrl.scode_table);
954 printk("(%x), ", priv->ctrl.scode_table);
955 #endif
956 } else
957 printk("int_freq %d, ", new_fw.int_freq);
958 printk("scode_nr %d\n", new_fw.scode_nr);
961 /* No need to reload base firmware if it matches */
962 if (priv->cur_fw.type & BASE) {
963 dprintk(1, "BASE firmware not changed.\n");
964 goto skip_base;
967 /* Updating BASE - forget about all currently loaded firmware */
968 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
970 /* Reset is needed before loading firmware */
971 rc = xc4000_TunerReset(fe);
972 if (rc < 0)
973 goto fail;
975 /* BASE firmwares are all std0 */
976 std0 = 0;
977 rc = load_firmware(fe, BASE, &std0);
978 if (rc < 0) {
979 printk("Error %d while loading base firmware\n", rc);
980 goto fail;
983 /* Load INIT1, if needed */
984 dprintk(1, "Load init1 firmware, if exists\n");
986 rc = load_firmware(fe, BASE | INIT1, &std0);
987 if (rc == -ENOENT)
988 rc = load_firmware(fe, BASE | INIT1, &std0);
989 if (rc < 0 && rc != -ENOENT) {
990 tuner_err("Error %d while loading init1 firmware\n",
991 rc);
992 goto fail;
995 skip_base:
997 * No need to reload standard specific firmware if base firmware
998 * was not reloaded and requested video standards have not changed.
1000 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1001 priv->cur_fw.std_req == std) {
1002 dprintk(1, "Std-specific firmware already loaded.\n");
1003 goto skip_std_specific;
1006 /* Reloading std-specific firmware forces a SCODE update */
1007 priv->cur_fw.scode_table = 0;
1009 /* Load the standard firmware */
1010 rc = load_firmware(fe, new_fw.type, &new_fw.id);
1012 if (rc < 0)
1013 goto fail;
1015 skip_std_specific:
1016 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1017 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1018 dprintk(1, "SCODE firmware already loaded.\n");
1019 goto check_device;
1022 /* Load SCODE firmware, if exists */
1023 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1024 new_fw.int_freq, new_fw.scode_nr);
1025 if (rc != XC_RESULT_SUCCESS)
1026 dprintk(1, "load scode failed %d\n", rc);
1028 check_device:
1029 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1031 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
1032 &fw_minor) != XC_RESULT_SUCCESS) {
1033 printk("Unable to read tuner registers.\n");
1034 goto fail;
1037 dprintk(1, "Device is Xceive %d version %d.%d, "
1038 "firmware version %d.%d\n",
1039 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1041 /* Check firmware version against what we downloaded. */
1042 #ifdef DJH_DEBUG
1043 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1044 printk("Incorrect readback of firmware version %x.\n",
1045 (version & 0xff));
1046 goto fail;
1048 #endif
1050 /* Check that the tuner hardware model remains consistent over time. */
1051 if (priv->hwmodel == 0 && hwmodel == 4000) {
1052 priv->hwmodel = hwmodel;
1053 priv->hwvers = version & 0xff00;
1054 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1055 priv->hwvers != (version & 0xff00)) {
1056 printk("Read invalid device hardware information - tuner "
1057 "hung?\n");
1058 goto fail;
1061 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1064 * By setting BASE in cur_fw.type only after successfully loading all
1065 * firmwares, we can:
1066 * 1. Identify that BASE firmware with type=0 has been loaded;
1067 * 2. Tell whether BASE firmware was just changed the next time through.
1069 priv->cur_fw.type |= BASE;
1071 return 0;
1073 fail:
1074 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1075 if (!is_retry) {
1076 msleep(50);
1077 is_retry = 1;
1078 dprintk(1, "Retrying firmware load\n");
1079 goto retry;
1082 if (rc == -ENOENT)
1083 rc = -EINVAL;
1084 return rc;
1087 static void xc_debug_dump(struct xc4000_priv *priv)
1089 u16 adc_envelope;
1090 u32 freq_error_hz = 0;
1091 u16 lock_status;
1092 u32 hsync_freq_hz = 0;
1093 u16 frame_lines;
1094 u16 quality;
1095 u8 hw_majorversion = 0, hw_minorversion = 0;
1096 u8 fw_majorversion = 0, fw_minorversion = 0;
1098 xc_get_ADC_Envelope(priv, &adc_envelope);
1099 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1101 xc_get_frequency_error(priv, &freq_error_hz);
1102 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1104 xc_get_lock_status(priv, &lock_status);
1105 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1106 lock_status);
1108 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1109 &fw_majorversion, &fw_minorversion);
1110 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1111 hw_majorversion, hw_minorversion,
1112 fw_majorversion, fw_minorversion);
1114 if (priv->video_standard < XC4000_DTV6) {
1115 xc_get_hsync_freq(priv, &hsync_freq_hz);
1116 dprintk(1, "*** Horizontal sync frequency = %d Hz\n",
1117 hsync_freq_hz);
1119 xc_get_frame_lines(priv, &frame_lines);
1120 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1123 xc_get_quality(priv, &quality);
1124 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1127 static int xc4000_set_params(struct dvb_frontend *fe,
1128 struct dvb_frontend_parameters *params)
1130 struct xc4000_priv *priv = fe->tuner_priv;
1131 unsigned int type;
1132 int ret = -EREMOTEIO;
1134 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1136 mutex_lock(&priv->lock);
1138 if (fe->ops.info.type == FE_ATSC) {
1139 dprintk(1, "%s() ATSC\n", __func__);
1140 switch (params->u.vsb.modulation) {
1141 case VSB_8:
1142 case VSB_16:
1143 dprintk(1, "%s() VSB modulation\n", __func__);
1144 priv->rf_mode = XC_RF_MODE_AIR;
1145 priv->freq_hz = params->frequency - 1750000;
1146 priv->bandwidth = BANDWIDTH_6_MHZ;
1147 priv->video_standard = XC4000_DTV6;
1148 type = DTV6;
1149 break;
1150 case QAM_64:
1151 case QAM_256:
1152 case QAM_AUTO:
1153 dprintk(1, "%s() QAM modulation\n", __func__);
1154 priv->rf_mode = XC_RF_MODE_CABLE;
1155 priv->freq_hz = params->frequency - 1750000;
1156 priv->bandwidth = BANDWIDTH_6_MHZ;
1157 priv->video_standard = XC4000_DTV6;
1158 type = DTV6;
1159 break;
1160 default:
1161 ret = -EINVAL;
1162 goto fail;
1164 } else if (fe->ops.info.type == FE_OFDM) {
1165 dprintk(1, "%s() OFDM\n", __func__);
1166 switch (params->u.ofdm.bandwidth) {
1167 case BANDWIDTH_6_MHZ:
1168 priv->bandwidth = BANDWIDTH_6_MHZ;
1169 priv->video_standard = XC4000_DTV6;
1170 priv->freq_hz = params->frequency - 1750000;
1171 type = DTV6;
1172 break;
1173 case BANDWIDTH_7_MHZ:
1174 priv->bandwidth = BANDWIDTH_7_MHZ;
1175 priv->video_standard = XC4000_DTV7;
1176 priv->freq_hz = params->frequency - 2250000;
1177 type = DTV7;
1178 break;
1179 case BANDWIDTH_8_MHZ:
1180 priv->bandwidth = BANDWIDTH_8_MHZ;
1181 priv->video_standard = XC4000_DTV8;
1182 priv->freq_hz = params->frequency - 2750000;
1183 type = DTV8;
1184 break;
1185 case BANDWIDTH_AUTO:
1186 if (params->frequency < 400000000) {
1187 priv->bandwidth = BANDWIDTH_7_MHZ;
1188 priv->freq_hz = params->frequency - 2250000;
1189 } else {
1190 priv->bandwidth = BANDWIDTH_8_MHZ;
1191 priv->freq_hz = params->frequency - 2750000;
1193 priv->video_standard = XC4000_DTV7_8;
1194 type = DTV78;
1195 break;
1196 default:
1197 printk(KERN_ERR "xc4000 bandwidth not set!\n");
1198 ret = -EINVAL;
1199 goto fail;
1201 priv->rf_mode = XC_RF_MODE_AIR;
1202 } else {
1203 printk(KERN_ERR "xc4000 modulation type not supported!\n");
1204 ret = -EINVAL;
1205 goto fail;
1208 dprintk(1, "%s() frequency=%d (compensated)\n",
1209 __func__, priv->freq_hz);
1211 /* Make sure the correct firmware type is loaded */
1212 if (check_firmware(fe, type, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1213 goto fail;
1215 ret = xc_SetSignalSource(priv, priv->rf_mode);
1216 if (ret != XC_RESULT_SUCCESS) {
1217 printk(KERN_ERR
1218 "xc4000: xc_SetSignalSource(%d) failed\n",
1219 priv->rf_mode);
1220 goto fail;
1223 ret = xc_SetTVStandard(priv,
1224 XC4000_Standard[priv->video_standard].VideoMode,
1225 XC4000_Standard[priv->video_standard].AudioMode);
1226 if (ret != XC_RESULT_SUCCESS) {
1227 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1228 goto fail;
1230 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1232 ret = 0;
1234 fail:
1235 mutex_unlock(&priv->lock);
1237 return ret;
1240 static int xc4000_set_analog_params(struct dvb_frontend *fe,
1241 struct analog_parameters *params)
1243 struct xc4000_priv *priv = fe->tuner_priv;
1244 int ret = -EREMOTEIO;
1246 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1247 __func__, params->frequency);
1249 mutex_lock(&priv->lock);
1251 /* Fix me: it could be air. */
1252 priv->rf_mode = params->mode;
1253 if (params->mode > XC_RF_MODE_CABLE)
1254 priv->rf_mode = XC_RF_MODE_CABLE;
1256 /* params->frequency is in units of 62.5khz */
1257 priv->freq_hz = params->frequency * 62500;
1259 /* FIX ME: Some video standards may have several possible audio
1260 standards. We simply default to one of them here.
1262 if (params->std & V4L2_STD_MN) {
1263 /* default to BTSC audio standard */
1264 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
1265 goto tune_channel;
1268 if (params->std & V4L2_STD_PAL_BG) {
1269 /* default to NICAM audio standard */
1270 priv->video_standard = XC4000_BG_PAL_NICAM;
1271 goto tune_channel;
1274 if (params->std & V4L2_STD_PAL_I) {
1275 /* default to NICAM audio standard */
1276 priv->video_standard = XC4000_I_PAL_NICAM;
1277 goto tune_channel;
1280 if (params->std & V4L2_STD_PAL_DK) {
1281 /* default to NICAM audio standard */
1282 priv->video_standard = XC4000_DK_PAL_NICAM;
1283 goto tune_channel;
1286 if (params->std & V4L2_STD_SECAM_DK) {
1287 /* default to A2 DK1 audio standard */
1288 priv->video_standard = XC4000_DK_SECAM_A2DK1;
1289 goto tune_channel;
1292 if (params->std & V4L2_STD_SECAM_L) {
1293 priv->video_standard = XC4000_L_SECAM_NICAM;
1294 goto tune_channel;
1297 if (params->std & V4L2_STD_SECAM_LC) {
1298 priv->video_standard = XC4000_LC_SECAM_NICAM;
1299 goto tune_channel;
1302 tune_channel:
1304 /* FIXME - firmware type not being set properly */
1305 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1306 goto fail;
1308 ret = xc_SetSignalSource(priv, priv->rf_mode);
1309 if (ret != XC_RESULT_SUCCESS) {
1310 printk(KERN_ERR
1311 "xc4000: xc_SetSignalSource(%d) failed\n",
1312 priv->rf_mode);
1313 goto fail;
1316 ret = xc_SetTVStandard(priv,
1317 XC4000_Standard[priv->video_standard].VideoMode,
1318 XC4000_Standard[priv->video_standard].AudioMode);
1319 if (ret != XC_RESULT_SUCCESS) {
1320 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1321 goto fail;
1324 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1326 ret = 0;
1328 fail:
1329 mutex_unlock(&priv->lock);
1331 return ret;
1334 static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1336 struct xc4000_priv *priv = fe->tuner_priv;
1338 *freq = priv->freq_hz;
1340 if (debug) {
1341 mutex_lock(&priv->lock);
1342 if ((priv->cur_fw.type
1343 & (BASE | FM | DTV6 | DTV7 | DTV78 | DTV8)) == BASE) {
1344 u16 snr = 0;
1345 if (xc4000_readreg(priv, XREG_SNR, &snr) == 0) {
1346 mutex_unlock(&priv->lock);
1347 dprintk(1, "%s() freq = %u, SNR = %d\n",
1348 __func__, *freq, snr);
1349 return 0;
1352 mutex_unlock(&priv->lock);
1355 dprintk(1, "%s()\n", __func__);
1357 return 0;
1360 static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1362 struct xc4000_priv *priv = fe->tuner_priv;
1363 dprintk(1, "%s()\n", __func__);
1365 *bw = priv->bandwidth;
1366 return 0;
1369 static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1371 struct xc4000_priv *priv = fe->tuner_priv;
1372 u16 lock_status = 0;
1374 mutex_lock(&priv->lock);
1376 if (priv->cur_fw.type & BASE)
1377 xc_get_lock_status(priv, &lock_status);
1379 *status = (lock_status == 1 ?
1380 TUNER_STATUS_LOCKED | TUNER_STATUS_STEREO : 0);
1381 if (priv->cur_fw.type & (DTV6 | DTV7 | DTV78 | DTV8))
1382 *status &= (~TUNER_STATUS_STEREO);
1384 mutex_unlock(&priv->lock);
1386 dprintk(2, "%s() lock_status = %d\n", __func__, lock_status);
1388 return 0;
1391 static int xc4000_sleep(struct dvb_frontend *fe)
1393 struct xc4000_priv *priv = fe->tuner_priv;
1394 int ret = XC_RESULT_SUCCESS;
1396 dprintk(1, "%s()\n", __func__);
1398 mutex_lock(&priv->lock);
1400 /* Avoid firmware reload on slow devices */
1401 if ((no_poweroff == 2 ||
1402 (no_poweroff == 0 &&
1403 priv->card_type != XC4000_CARD_WINFAST_CX88)) &&
1404 (priv->cur_fw.type & BASE) != 0) {
1405 /* force reset and firmware reload */
1406 priv->cur_fw.type = XC_POWERED_DOWN;
1408 if (xc_write_reg(priv, XREG_POWER_DOWN, 0)
1409 != XC_RESULT_SUCCESS) {
1410 printk(KERN_ERR
1411 "xc4000: %s() unable to shutdown tuner\n",
1412 __func__);
1413 ret = -EREMOTEIO;
1415 xc_wait(20);
1418 mutex_unlock(&priv->lock);
1420 return ret;
1423 static int xc4000_init(struct dvb_frontend *fe)
1425 dprintk(1, "%s()\n", __func__);
1427 return 0;
1430 static int xc4000_release(struct dvb_frontend *fe)
1432 struct xc4000_priv *priv = fe->tuner_priv;
1434 dprintk(1, "%s()\n", __func__);
1436 mutex_lock(&xc4000_list_mutex);
1438 if (priv)
1439 hybrid_tuner_release_state(priv);
1441 mutex_unlock(&xc4000_list_mutex);
1443 fe->tuner_priv = NULL;
1445 return 0;
1448 static const struct dvb_tuner_ops xc4000_tuner_ops = {
1449 .info = {
1450 .name = "Xceive XC4000",
1451 .frequency_min = 1000000,
1452 .frequency_max = 1023000000,
1453 .frequency_step = 50000,
1456 .release = xc4000_release,
1457 .init = xc4000_init,
1458 .sleep = xc4000_sleep,
1460 .set_params = xc4000_set_params,
1461 .set_analog_params = xc4000_set_analog_params,
1462 .get_frequency = xc4000_get_frequency,
1463 .get_bandwidth = xc4000_get_bandwidth,
1464 .get_status = xc4000_get_status
1467 struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1468 struct i2c_adapter *i2c,
1469 struct xc4000_config *cfg)
1471 struct xc4000_priv *priv = NULL;
1472 int instance;
1473 u16 id = 0;
1475 if (cfg->card_type != XC4000_CARD_GENERIC) {
1476 if (cfg->card_type == XC4000_CARD_WINFAST_CX88) {
1477 cfg->i2c_address = 0x61;
1478 cfg->if_khz = 4560;
1479 } else { /* default to PCTV 340E */
1480 cfg->i2c_address = 0x61;
1481 cfg->if_khz = 5400;
1485 dprintk(1, "%s(%d-%04x)\n", __func__,
1486 i2c ? i2c_adapter_id(i2c) : -1,
1487 cfg ? cfg->i2c_address : -1);
1489 mutex_lock(&xc4000_list_mutex);
1491 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1492 hybrid_tuner_instance_list,
1493 i2c, cfg->i2c_address, "xc4000");
1494 if (cfg->card_type != XC4000_CARD_GENERIC)
1495 priv->card_type = cfg->card_type;
1496 switch (instance) {
1497 case 0:
1498 goto fail;
1499 break;
1500 case 1:
1501 /* new tuner instance */
1502 priv->bandwidth = BANDWIDTH_6_MHZ;
1503 mutex_init(&priv->lock);
1504 fe->tuner_priv = priv;
1505 break;
1506 default:
1507 /* existing tuner instance */
1508 fe->tuner_priv = priv;
1509 break;
1512 if (cfg->if_khz != 0) {
1513 /* If the IF hasn't been set yet, use the value provided by
1514 the caller (occurs in hybrid devices where the analog
1515 call to xc4000_attach occurs before the digital side) */
1516 priv->if_khz = cfg->if_khz;
1519 /* Check if firmware has been loaded. It is possible that another
1520 instance of the driver has loaded the firmware.
1523 if (instance == 1) {
1524 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id)
1525 != XC_RESULT_SUCCESS)
1526 goto fail;
1527 } else {
1528 id = ((priv->cur_fw.type & BASE) != 0 ?
1529 priv->hwmodel : XC_PRODUCT_ID_FW_NOT_LOADED);
1532 switch (id) {
1533 case XC_PRODUCT_ID_FW_LOADED:
1534 printk(KERN_INFO
1535 "xc4000: Successfully identified at address 0x%02x\n",
1536 cfg->i2c_address);
1537 printk(KERN_INFO
1538 "xc4000: Firmware has been loaded previously\n");
1539 break;
1540 case XC_PRODUCT_ID_FW_NOT_LOADED:
1541 printk(KERN_INFO
1542 "xc4000: Successfully identified at address 0x%02x\n",
1543 cfg->i2c_address);
1544 printk(KERN_INFO
1545 "xc4000: Firmware has not been loaded previously\n");
1546 break;
1547 default:
1548 printk(KERN_ERR
1549 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1550 cfg->i2c_address, id);
1551 goto fail;
1554 mutex_unlock(&xc4000_list_mutex);
1556 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1557 sizeof(struct dvb_tuner_ops));
1559 if (instance == 1) {
1560 int ret;
1561 mutex_lock(&priv->lock);
1562 ret = xc4000_fwupload(fe);
1563 mutex_unlock(&priv->lock);
1564 if (ret != XC_RESULT_SUCCESS)
1565 goto fail2;
1568 return fe;
1569 fail:
1570 mutex_unlock(&xc4000_list_mutex);
1571 fail2:
1572 xc4000_release(fe);
1573 return NULL;
1575 EXPORT_SYMBOL(xc4000_attach);
1577 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1578 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1579 MODULE_LICENSE("GPL");