[media] xc4000: turn off debug logging by default
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / common / tuners / xc4000.c
blob49d5929389ff35e73ee74ef95542910b57b5f7df
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 <asm/unaligned.h>
33 #include "dvb_frontend.h"
35 #include "xc4000.h"
36 #include "tuner-i2c.h"
37 #include "tuner-xc2028-types.h"
39 static int debug;
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 {
61 unsigned int type;
62 v4l2_std_id id;
63 __u16 int_freq;
64 unsigned char *ptr;
65 unsigned int size;
68 struct firmware_properties {
69 unsigned int type;
70 v4l2_std_id id;
71 v4l2_std_id std_req;
72 __u16 int_freq;
73 unsigned int scode_table;
74 int scode_nr;
77 struct xc4000_priv {
78 struct tuner_i2c_props i2c_props;
79 struct list_head hybrid_tuner_instance_list;
80 struct firmware_description *firm;
81 int firm_size;
82 __u16 firm_version;
83 u32 if_khz;
84 u32 freq_hz;
85 u32 bandwidth;
86 u8 video_standard;
87 u8 rf_mode;
88 // struct xc2028_ctrl ctrl;
89 struct firmware_properties cur_fw;
90 __u16 hwmodel;
91 __u16 hwvers;
92 u8 ignore_i2c_write_errors;
95 /* Misc Defines */
96 #define MAX_TV_STANDARD 23
97 #define XC_MAX_I2C_WRITE_LENGTH 64
99 /* Signal Types */
100 #define XC_RF_MODE_AIR 0
101 #define XC_RF_MODE_CABLE 1
103 /* Result codes */
104 #define XC_RESULT_SUCCESS 0
105 #define XC_RESULT_RESET_FAILURE 1
106 #define XC_RESULT_I2C_WRITE_FAILURE 2
107 #define XC_RESULT_I2C_READ_FAILURE 3
108 #define XC_RESULT_OUT_OF_RANGE 5
110 /* Product id */
111 #define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
112 #define XC_PRODUCT_ID_FW_LOADED 0x0FA0
114 /* Registers (Write-only) */
115 #define XREG_INIT 0x00
116 #define XREG_VIDEO_MODE 0x01
117 #define XREG_AUDIO_MODE 0x02
118 #define XREG_RF_FREQ 0x03
119 #define XREG_D_CODE 0x04
120 #define XREG_DIRECTSITTING_MODE 0x05
121 #define XREG_SEEK_MODE 0x06
122 #define XREG_POWER_DOWN 0x08
123 #define XREG_SIGNALSOURCE 0x0A
124 #define XREG_AMPLITUDE 0x10
126 /* Registers (Read-only) */
127 #define XREG_ADC_ENV 0x00
128 #define XREG_QUALITY 0x01
129 #define XREG_FRAME_LINES 0x02
130 #define XREG_HSYNC_FREQ 0x03
131 #define XREG_LOCK 0x04
132 #define XREG_FREQ_ERROR 0x05
133 #define XREG_SNR 0x06
134 #define XREG_VERSION 0x07
135 #define XREG_PRODUCT_ID 0x08
138 Basic firmware description. This will remain with
139 the driver for documentation purposes.
141 This represents an I2C firmware file encoded as a
142 string of unsigned char. Format is as follows:
144 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
145 char[1 ]=len0_LSB -> length of first write transaction
146 char[2 ]=data0 -> first byte to be sent
147 char[3 ]=data1
148 char[4 ]=data2
149 char[ ]=...
150 char[M ]=dataN -> last byte to be sent
151 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
152 char[M+2]=len1_LSB -> length of second write transaction
153 char[M+3]=data0
154 char[M+4]=data1
156 etc.
158 The [len] value should be interpreted as follows:
160 len= len_MSB _ len_LSB
161 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
162 len=0000_0000_0000_0000 : Reset command: Do hardware reset
163 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
164 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
166 For the RESET and WAIT commands, the two following bytes will contain
167 immediately the length of the following transaction.
170 struct XC_TV_STANDARD {
171 char *Name;
172 u16 AudioMode;
173 u16 VideoMode;
176 /* Tuner standards */
177 #define XC4000_MN_NTSC_PAL_BTSC 0
178 #define XC4000_MN_NTSC_PAL_A2 1
179 #define XC4000_MN_NTSC_PAL_EIAJ 2
180 #define XC4000_MN_NTSC_PAL_Mono 3
181 #define XC4000_BG_PAL_A2 4
182 #define XC4000_BG_PAL_NICAM 5
183 #define XC4000_BG_PAL_MONO 6
184 #define XC4000_I_PAL_NICAM 7
185 #define XC4000_I_PAL_NICAM_MONO 8
186 #define XC4000_DK_PAL_A2 9
187 #define XC4000_DK_PAL_NICAM 10
188 #define XC4000_DK_PAL_MONO 11
189 #define XC4000_DK_SECAM_A2DK1 12
190 #define XC4000_DK_SECAM_A2LDK3 13
191 #define XC4000_DK_SECAM_A2MONO 14
192 #define XC4000_L_SECAM_NICAM 15
193 #define XC4000_LC_SECAM_NICAM 16
194 #define XC4000_DTV6 17
195 #define XC4000_DTV8 18
196 #define XC4000_DTV7_8 19
197 #define XC4000_DTV7 20
198 #define XC4000_FM_Radio_INPUT2 21
199 #define XC4000_FM_Radio_INPUT1 22
201 /* WAS :
202 static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
203 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
204 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
205 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
206 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
207 {"B/G-PAL-A2", 0x0A00, 0x8049},
208 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
209 {"B/G-PAL-MONO", 0x0878, 0x8059},
210 {"I-PAL-NICAM", 0x1080, 0x8009},
211 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
212 {"D/K-PAL-A2", 0x1600, 0x8009},
213 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
214 {"D/K-PAL-MONO", 0x1478, 0x8009},
215 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
216 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
217 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
218 {"L-SECAM-NICAM", 0x8E82, 0x0009},
219 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
220 {"DTV6", 0x00C0, 0x8002},
221 {"DTV8", 0x00C0, 0x800B},
222 {"DTV7/8", 0x00C0, 0x801B},
223 {"DTV7", 0x00C0, 0x8007},
224 {"FM Radio-INPUT2", 0x9802, 0x9002},
225 {"FM Radio-INPUT1", 0x0208, 0x9002}
226 };*/
228 static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
229 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x8020},
230 {"M/N-NTSC/PAL-A2", 0x0000, 0x8020},
231 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x8020},
232 {"M/N-NTSC/PAL-Mono", 0x0078, 0x8020},
233 {"B/G-PAL-A2", 0x0000, 0x8059},
234 {"B/G-PAL-NICAM", 0x0004, 0x8059},
235 {"B/G-PAL-MONO", 0x0078, 0x8059},
236 {"I-PAL-NICAM", 0x0080, 0x8049},
237 {"I-PAL-NICAM-MONO", 0x0078, 0x8049},
238 {"D/K-PAL-A2", 0x0000, 0x8049},
239 {"D/K-PAL-NICAM", 0x0080, 0x8049},
240 {"D/K-PAL-MONO", 0x0078, 0x8049},
241 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049},
242 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049},
243 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049},
244 {"L-SECAM-NICAM", 0x8080, 0x0009},
245 {"L'-SECAM-NICAM", 0x8080, 0x4009},
246 {"DTV6", 0x00C0, 0x8002},
247 {"DTV8", 0x00C0, 0x800B},
248 {"DTV7/8", 0x00C0, 0x801B},
249 {"DTV7", 0x00C0, 0x8007},
250 {"FM Radio-INPUT2", 0x0008, 0x9800},
251 {"FM Radio-INPUT1", 0x0008, 0x9000}
254 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
255 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
256 static int xc4000_TunerReset(struct dvb_frontend *fe);
258 static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
260 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
261 .flags = 0, .buf = buf, .len = len };
262 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
263 if (priv->ignore_i2c_write_errors == 0) {
264 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
265 len);
266 if (len == 4) {
267 printk("bytes %02x %02x %02x %02x\n", buf[0],
268 buf[1], buf[2], buf[3]);
270 return XC_RESULT_I2C_WRITE_FAILURE;
273 return XC_RESULT_SUCCESS;
276 /* This routine is never used because the only time we read data from the
277 i2c bus is when we read registers, and we want that to be an atomic i2c
278 transaction in case we are on a multi-master bus */
280 static void xc_wait(int wait_ms)
282 msleep(wait_ms);
285 static int xc4000_TunerReset(struct dvb_frontend *fe)
287 struct xc4000_priv *priv = fe->tuner_priv;
288 int ret;
290 dprintk(1, "%s()\n", __func__);
292 if (fe->callback) {
293 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
294 fe->dvb->priv :
295 priv->i2c_props.adap->algo_data,
296 DVB_FRONTEND_COMPONENT_TUNER,
297 XC4000_TUNER_RESET, 0);
298 if (ret) {
299 printk(KERN_ERR "xc4000: reset failed\n");
300 return XC_RESULT_RESET_FAILURE;
302 } else {
303 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
304 return XC_RESULT_RESET_FAILURE;
306 return XC_RESULT_SUCCESS;
309 static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
311 u8 buf[4];
312 int result;
314 buf[0] = (regAddr >> 8) & 0xFF;
315 buf[1] = regAddr & 0xFF;
316 buf[2] = (i2cData >> 8) & 0xFF;
317 buf[3] = i2cData & 0xFF;
318 result = xc_send_i2c_data(priv, buf, 4);
320 return result;
323 static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
325 struct xc4000_priv *priv = fe->tuner_priv;
327 int i, nbytes_to_send, result;
328 unsigned int len, pos, index;
329 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
331 index = 0;
332 while ((i2c_sequence[index] != 0xFF) ||
333 (i2c_sequence[index + 1] != 0xFF)) {
334 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
335 if (len == 0x0000) {
336 /* RESET command */
337 result = xc4000_TunerReset(fe);
338 index += 2;
339 if (result != XC_RESULT_SUCCESS)
340 return result;
341 } else if (len & 0x8000) {
342 /* WAIT command */
343 xc_wait(len & 0x7FFF);
344 index += 2;
345 } else {
346 /* Send i2c data whilst ensuring individual transactions
347 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
349 index += 2;
350 buf[0] = i2c_sequence[index];
351 buf[1] = i2c_sequence[index + 1];
352 pos = 2;
353 while (pos < len) {
354 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
355 nbytes_to_send =
356 XC_MAX_I2C_WRITE_LENGTH;
357 else
358 nbytes_to_send = (len - pos + 2);
359 for (i = 2; i < nbytes_to_send; i++) {
360 buf[i] = i2c_sequence[index + pos +
361 i - 2];
363 result = xc_send_i2c_data(priv, buf,
364 nbytes_to_send);
366 if (result != XC_RESULT_SUCCESS)
367 return result;
369 pos += nbytes_to_send - 2;
371 index += len;
374 return XC_RESULT_SUCCESS;
377 static int xc_SetTVStandard(struct xc4000_priv *priv,
378 u16 VideoMode, u16 AudioMode)
380 int ret;
381 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
382 dprintk(1, "%s() Standard = %s\n",
383 __func__,
384 XC4000_Standard[priv->video_standard].Name);
386 /* Don't complain when the request fails because of i2c stretching */
387 priv->ignore_i2c_write_errors = 1;
389 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
390 if (ret == XC_RESULT_SUCCESS)
391 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
393 priv->ignore_i2c_write_errors = 0;
395 return ret;
398 static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
400 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
401 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
403 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
404 rf_mode = XC_RF_MODE_CABLE;
405 printk(KERN_ERR
406 "%s(), Invalid mode, defaulting to CABLE",
407 __func__);
409 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
412 static const struct dvb_tuner_ops xc4000_tuner_ops;
414 static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
416 u16 freq_code;
418 dprintk(1, "%s(%u)\n", __func__, freq_hz);
420 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
421 (freq_hz < xc4000_tuner_ops.info.frequency_min))
422 return XC_RESULT_OUT_OF_RANGE;
424 freq_code = (u16)(freq_hz / 15625);
426 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
427 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
428 only be used for fast scanning for channel lock) */
429 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
433 static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
435 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
438 static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
440 int result;
441 u16 regData;
442 u32 tmp;
444 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
445 if (result != XC_RESULT_SUCCESS)
446 return result;
448 tmp = (u32)regData;
449 (*freq_error_hz) = (tmp * 15625) / 1000;
450 return result;
453 static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
455 return xc4000_readreg(priv, XREG_LOCK, lock_status);
458 static int xc_get_version(struct xc4000_priv *priv,
459 u8 *hw_majorversion, u8 *hw_minorversion,
460 u8 *fw_majorversion, u8 *fw_minorversion)
462 u16 data;
463 int result;
465 result = xc4000_readreg(priv, XREG_VERSION, &data);
466 if (result != XC_RESULT_SUCCESS)
467 return result;
469 (*hw_majorversion) = (data >> 12) & 0x0F;
470 (*hw_minorversion) = (data >> 8) & 0x0F;
471 (*fw_majorversion) = (data >> 4) & 0x0F;
472 (*fw_minorversion) = data & 0x0F;
474 return 0;
477 /* WAS THERE
478 static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
480 return xc4000_readreg(priv, XREG_BUILD, buildrev);
483 static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
485 u16 regData;
486 int result;
488 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
489 if (result != XC_RESULT_SUCCESS)
490 return result;
492 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
493 return result;
496 static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
498 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
501 static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
503 return xc4000_readreg(priv, XREG_QUALITY, quality);
506 static u16 WaitForLock(struct xc4000_priv *priv)
508 u16 lockState = 0;
509 int watchDogCount = 40;
511 while ((lockState == 0) && (watchDogCount > 0)) {
512 xc_get_lock_status(priv, &lockState);
513 if (lockState != 1) {
514 xc_wait(5);
515 watchDogCount--;
518 return lockState;
521 #define XC_TUNE_ANALOG 0
522 #define XC_TUNE_DIGITAL 1
523 static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
525 int found = 0;
526 int result = 0;
528 dprintk(1, "%s(%u)\n", __func__, freq_hz);
530 /* Don't complain when the request fails because of i2c stretching */
531 priv->ignore_i2c_write_errors = 1;
532 result = xc_set_RF_frequency(priv, freq_hz);
533 priv->ignore_i2c_write_errors = 0;
535 if (result != XC_RESULT_SUCCESS)
536 return 0;
538 if (mode == XC_TUNE_ANALOG) {
539 if (WaitForLock(priv) == 1)
540 found = 1;
543 return found;
546 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
548 u8 buf[2] = { reg >> 8, reg & 0xff };
549 u8 bval[2] = { 0, 0 };
550 struct i2c_msg msg[2] = {
551 { .addr = priv->i2c_props.addr,
552 .flags = 0, .buf = &buf[0], .len = 2 },
553 { .addr = priv->i2c_props.addr,
554 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
557 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
558 printk(KERN_WARNING "xc4000: I2C read failed\n");
559 return -EREMOTEIO;
562 *val = (bval[0] << 8) | bval[1];
563 return XC_RESULT_SUCCESS;
566 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
567 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
569 if (type & BASE)
570 printk("BASE ");
571 if (type & INIT1)
572 printk("INIT1 ");
573 if (type & F8MHZ)
574 printk("F8MHZ ");
575 if (type & MTS)
576 printk("MTS ");
577 if (type & D2620)
578 printk("D2620 ");
579 if (type & D2633)
580 printk("D2633 ");
581 if (type & DTV6)
582 printk("DTV6 ");
583 if (type & QAM)
584 printk("QAM ");
585 if (type & DTV7)
586 printk("DTV7 ");
587 if (type & DTV78)
588 printk("DTV78 ");
589 if (type & DTV8)
590 printk("DTV8 ");
591 if (type & FM)
592 printk("FM ");
593 if (type & INPUT1)
594 printk("INPUT1 ");
595 if (type & LCD)
596 printk("LCD ");
597 if (type & NOGD)
598 printk("NOGD ");
599 if (type & MONO)
600 printk("MONO ");
601 if (type & ATSC)
602 printk("ATSC ");
603 if (type & IF)
604 printk("IF ");
605 if (type & LG60)
606 printk("LG60 ");
607 if (type & ATI638)
608 printk("ATI638 ");
609 if (type & OREN538)
610 printk("OREN538 ");
611 if (type & OREN36)
612 printk("OREN36 ");
613 if (type & TOYOTA388)
614 printk("TOYOTA388 ");
615 if (type & TOYOTA794)
616 printk("TOYOTA794 ");
617 if (type & DIBCOM52)
618 printk("DIBCOM52 ");
619 if (type & ZARLINK456)
620 printk("ZARLINK456 ");
621 if (type & CHINA)
622 printk("CHINA ");
623 if (type & F6MHZ)
624 printk("F6MHZ ");
625 if (type & INPUT2)
626 printk("INPUT2 ");
627 if (type & SCODE)
628 printk("SCODE ");
629 if (type & HAS_IF)
630 printk("HAS_IF_%d ", int_freq);
633 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
634 v4l2_std_id *id)
636 struct xc4000_priv *priv = fe->tuner_priv;
637 int i, best_i = -1, best_nr_matches = 0;
638 unsigned int type_mask = 0;
640 printk("%s called, want type=", __func__);
641 if (debug) {
642 dump_firm_type(type);
643 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
646 if (!priv->firm) {
647 printk("Error! firmware not loaded\n");
648 return -EINVAL;
651 if (((type & ~SCODE) == 0) && (*id == 0))
652 *id = V4L2_STD_PAL;
654 if (type & BASE)
655 type_mask = BASE_TYPES;
656 else if (type & SCODE) {
657 type &= SCODE_TYPES;
658 type_mask = SCODE_TYPES & ~HAS_IF;
659 } else if (type & DTV_TYPES)
660 type_mask = DTV_TYPES;
661 else if (type & STD_SPECIFIC_TYPES)
662 type_mask = STD_SPECIFIC_TYPES;
664 type &= type_mask;
666 if (!(type & SCODE))
667 type_mask = ~0;
669 /* Seek for exact match */
670 for (i = 0; i < priv->firm_size; i++) {
671 if ((type == (priv->firm[i].type & type_mask)) &&
672 (*id == priv->firm[i].id))
673 goto found;
676 /* Seek for generic video standard match */
677 for (i = 0; i < priv->firm_size; i++) {
678 v4l2_std_id match_mask;
679 int nr_matches;
681 if (type != (priv->firm[i].type & type_mask))
682 continue;
684 match_mask = *id & priv->firm[i].id;
685 if (!match_mask)
686 continue;
688 if ((*id & match_mask) == *id)
689 goto found; /* Supports all the requested standards */
691 nr_matches = hweight64(match_mask);
692 if (nr_matches > best_nr_matches) {
693 best_nr_matches = nr_matches;
694 best_i = i;
698 if (best_nr_matches > 0) {
699 printk("Selecting best matching firmware (%d bits) for "
700 "type=", best_nr_matches);
701 // dump_firm_type(type);
702 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
703 i = best_i;
704 goto found;
707 /*FIXME: Would make sense to seek for type "hint" match ? */
709 i = -ENOENT;
710 goto ret;
712 found:
713 *id = priv->firm[i].id;
715 ret:
716 printk("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
717 if (debug) {
718 dump_firm_type(type);
719 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
720 if (i < 0)
721 dump_stack();
723 return i;
726 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
727 v4l2_std_id *id)
729 struct xc4000_priv *priv = fe->tuner_priv;
730 int pos, rc;
731 unsigned char *p;
733 printk("%s called\n", __func__);
735 pos = seek_firmware(fe, type, id);
736 if (pos < 0)
737 return pos;
739 printk("Loading firmware for type=");
740 // dump_firm_type(priv->firm[pos].type);
741 printk("(%x), id %016llx.\n", priv->firm[pos].type,
742 (unsigned long long)*id);
744 p = priv->firm[pos].ptr;
745 printk("firmware length = %d\n", priv->firm[pos].size);
747 /* Don't complain when the request fails because of i2c stretching */
748 priv->ignore_i2c_write_errors = 1;
750 rc = xc_load_i2c_sequence(fe, p);
752 priv->ignore_i2c_write_errors = 0;
754 return rc;
757 static int xc4000_fwupload(struct dvb_frontend *fe)
759 struct xc4000_priv *priv = fe->tuner_priv;
760 const struct firmware *fw = NULL;
761 const unsigned char *p, *endp;
762 int rc = 0;
763 int n, n_array;
764 char name[33];
765 char *fname;
767 printk("%s called\n", __func__);
769 fname = XC4000_DEFAULT_FIRMWARE;
771 printk("Reading firmware %s\n", fname);
772 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
773 if (rc < 0) {
774 if (rc == -ENOENT)
775 printk("Error: firmware %s not found.\n",
776 fname);
777 else
778 printk("Error %d while requesting firmware %s \n",
779 rc, fname);
781 return rc;
783 p = fw->data;
784 endp = p + fw->size;
786 if (fw->size < sizeof(name) - 1 + 2 + 2) {
787 printk("Error: firmware file %s has invalid size!\n",
788 fname);
789 goto corrupt;
792 memcpy(name, p, sizeof(name) - 1);
793 name[sizeof(name) - 1] = 0;
794 p += sizeof(name) - 1;
796 priv->firm_version = get_unaligned_le16(p);
797 p += 2;
799 n_array = get_unaligned_le16(p);
800 p += 2;
802 printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
803 n_array, fname, name,
804 priv->firm_version >> 8, priv->firm_version & 0xff);
806 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
807 if (priv->firm == NULL) {
808 printk("Not enough memory to load firmware file.\n");
809 rc = -ENOMEM;
810 goto err;
812 priv->firm_size = n_array;
814 n = -1;
815 while (p < endp) {
816 __u32 type, size;
817 v4l2_std_id id;
818 __u16 int_freq = 0;
820 n++;
821 if (n >= n_array) {
822 printk("More firmware images in file than "
823 "were expected!\n");
824 goto corrupt;
827 /* Checks if there's enough bytes to read */
828 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
829 goto header;
831 type = get_unaligned_le32(p);
832 p += sizeof(type);
834 id = get_unaligned_le64(p);
835 p += sizeof(id);
837 if (type & HAS_IF) {
838 int_freq = get_unaligned_le16(p);
839 p += sizeof(int_freq);
840 if (endp - p < sizeof(size))
841 goto header;
844 size = get_unaligned_le32(p);
845 p += sizeof(size);
847 if (!size || size > endp - p) {
848 printk("Firmware type ");
849 // dump_firm_type(type);
850 printk("(%x), id %llx is corrupted "
851 "(size=%d, expected %d)\n",
852 type, (unsigned long long)id,
853 (unsigned)(endp - p), size);
854 goto corrupt;
857 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
858 if (priv->firm[n].ptr == NULL) {
859 printk("Not enough memory to load firmware file.\n");
860 rc = -ENOMEM;
861 goto err;
864 if (debug) {
865 printk("Reading firmware type ");
866 dump_firm_type_and_int_freq(type, int_freq);
867 printk("(%x), id %llx, size=%d.\n",
868 type, (unsigned long long)id, size);
871 memcpy(priv->firm[n].ptr, p, size);
872 priv->firm[n].type = type;
873 priv->firm[n].id = id;
874 priv->firm[n].size = size;
875 priv->firm[n].int_freq = int_freq;
877 p += size;
880 if (n + 1 != priv->firm_size) {
881 printk("Firmware file is incomplete!\n");
882 goto corrupt;
885 goto done;
887 header:
888 printk("Firmware header is incomplete!\n");
889 corrupt:
890 rc = -EINVAL;
891 printk("Error: firmware file is corrupted!\n");
893 err:
894 printk("Releasing partially loaded firmware file.\n");
895 // free_firmware(priv);
897 done:
898 release_firmware(fw);
899 if (rc == 0)
900 printk("Firmware files loaded.\n");
902 return rc;
905 static int load_scode(struct dvb_frontend *fe, unsigned int type,
906 v4l2_std_id *id, __u16 int_freq, int scode)
908 struct xc4000_priv *priv = fe->tuner_priv;
909 int pos, rc;
910 unsigned char *p;
911 u8 scode_buf[13];
912 u8 indirect_mode[5];
914 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
916 if (!int_freq) {
917 pos = seek_firmware(fe, type, id);
918 if (pos < 0)
919 return pos;
920 } else {
921 for (pos = 0; pos < priv->firm_size; pos++) {
922 if ((priv->firm[pos].int_freq == int_freq) &&
923 (priv->firm[pos].type & HAS_IF))
924 break;
926 if (pos == priv->firm_size)
927 return -ENOENT;
930 p = priv->firm[pos].ptr;
932 if (priv->firm[pos].type & HAS_IF) {
933 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
934 return -EINVAL;
935 p += 12 * scode;
936 } else {
937 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
938 * has a 2-byte size header in the firmware format. */
939 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
940 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
941 return -EINVAL;
942 p += 14 * scode + 2;
945 tuner_info("Loading SCODE for type=");
946 dump_firm_type_and_int_freq(priv->firm[pos].type,
947 priv->firm[pos].int_freq);
948 printk("(%x), id %016llx.\n", priv->firm[pos].type,
949 (unsigned long long)*id);
951 scode_buf[0] = 0x00;
952 memcpy(&scode_buf[1], p, 12);
954 /* Enter direct-mode */
955 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
956 if (rc < 0) {
957 printk("failed to put device into direct mode!\n");
958 return -EIO;
961 rc = xc_send_i2c_data(priv, scode_buf, 13);
962 if (rc != XC_RESULT_SUCCESS) {
963 /* Even if the send failed, make sure we set back to indirect
964 mode */
965 printk("Failed to set scode %d\n", rc);
968 /* Switch back to indirect-mode */
969 memset(indirect_mode, 0, sizeof(indirect_mode));
970 indirect_mode[4] = 0x88;
971 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
972 msleep(10);
974 return 0;
977 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
978 v4l2_std_id std, __u16 int_freq)
980 struct xc4000_priv *priv = fe->tuner_priv;
981 struct firmware_properties new_fw;
982 int rc = 0, is_retry = 0;
983 u16 version, hwmodel;
984 v4l2_std_id std0;
985 u8 hw_major, hw_minor, fw_major, fw_minor;
987 dprintk(1, "%s called\n", __func__);
989 if (!priv->firm) {
990 rc = xc4000_fwupload(fe);
991 if (rc < 0)
992 return rc;
995 #ifdef DJH_DEBUG
996 if (priv->ctrl.mts && !(type & FM))
997 type |= MTS;
998 #endif
1000 retry:
1001 new_fw.type = type;
1002 new_fw.id = std;
1003 new_fw.std_req = std;
1004 // new_fw.scode_table = SCODE | priv->ctrl.scode_table;
1005 new_fw.scode_table = SCODE;
1006 new_fw.scode_nr = 0;
1007 new_fw.int_freq = int_freq;
1009 dprintk(1, "checking firmware, user requested type=");
1010 if (debug) {
1011 dump_firm_type(new_fw.type);
1012 printk("(%x), id %016llx, ", new_fw.type,
1013 (unsigned long long)new_fw.std_req);
1014 if (!int_freq) {
1015 printk("scode_tbl ");
1016 #ifdef DJH_DEBUG
1017 dump_firm_type(priv->ctrl.scode_table);
1018 printk("(%x), ", priv->ctrl.scode_table);
1019 #endif
1020 } else
1021 printk("int_freq %d, ", new_fw.int_freq);
1022 printk("scode_nr %d\n", new_fw.scode_nr);
1025 /* No need to reload base firmware if it matches */
1026 if (((BASE | new_fw.type) & BASE_TYPES) ==
1027 (priv->cur_fw.type & BASE_TYPES)) {
1028 dprintk(1, "BASE firmware not changed.\n");
1029 goto skip_base;
1032 /* Updating BASE - forget about all currently loaded firmware */
1033 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1035 /* Reset is needed before loading firmware */
1036 rc = xc4000_TunerReset(fe);
1037 if (rc < 0)
1038 goto fail;
1040 /* BASE firmwares are all std0 */
1041 std0 = 0;
1042 rc = load_firmware(fe, BASE | new_fw.type, &std0);
1043 if (rc < 0) {
1044 printk("Error %d while loading base firmware\n", rc);
1045 goto fail;
1048 /* Load INIT1, if needed */
1049 dprintk(1, "Load init1 firmware, if exists\n");
1051 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1052 if (rc == -ENOENT)
1053 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
1054 &std0);
1055 if (rc < 0 && rc != -ENOENT) {
1056 tuner_err("Error %d while loading init1 firmware\n",
1057 rc);
1058 goto fail;
1061 printk("Done with init1\n");
1063 skip_base:
1065 * No need to reload standard specific firmware if base firmware
1066 * was not reloaded and requested video standards have not changed.
1068 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1069 priv->cur_fw.std_req == std) {
1070 dprintk(1, "Std-specific firmware already loaded.\n");
1071 goto skip_std_specific;
1074 /* Reloading std-specific firmware forces a SCODE update */
1075 priv->cur_fw.scode_table = 0;
1077 /* Load the standard firmware */
1078 rc = load_firmware(fe, new_fw.type, &new_fw.id);
1080 if (rc < 0)
1081 goto fail;
1083 skip_std_specific:
1084 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1085 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1086 dprintk(1, "SCODE firmware already loaded.\n");
1087 goto check_device;
1090 if (new_fw.type & FM)
1091 goto check_device;
1093 /* Load SCODE firmware, if exists */
1094 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1095 new_fw.int_freq, new_fw.scode_nr);
1096 if (rc != XC_RESULT_SUCCESS)
1097 dprintk(1, "load scode failed %d\n", rc);
1099 check_device:
1100 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1102 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
1103 &fw_minor) != XC_RESULT_SUCCESS) {
1104 printk("Unable to read tuner registers.\n");
1105 goto fail;
1108 dprintk(1, "Device is Xceive %d version %d.%d, "
1109 "firmware version %d.%d\n",
1110 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1112 /* Check firmware version against what we downloaded. */
1113 #ifdef DJH_DEBUG
1114 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1115 printk("Incorrect readback of firmware version %x.\n",
1116 (version & 0xff));
1117 goto fail;
1119 #endif
1121 /* Check that the tuner hardware model remains consistent over time. */
1122 if (priv->hwmodel == 0 && hwmodel == 4000) {
1123 priv->hwmodel = hwmodel;
1124 priv->hwvers = version & 0xff00;
1125 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1126 priv->hwvers != (version & 0xff00)) {
1127 printk("Read invalid device hardware information - tuner "
1128 "hung?\n");
1129 goto fail;
1132 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1135 * By setting BASE in cur_fw.type only after successfully loading all
1136 * firmwares, we can:
1137 * 1. Identify that BASE firmware with type=0 has been loaded;
1138 * 2. Tell whether BASE firmware was just changed the next time through.
1140 priv->cur_fw.type |= BASE;
1142 return 0;
1144 fail:
1145 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1146 if (!is_retry) {
1147 msleep(50);
1148 is_retry = 1;
1149 dprintk(1, "Retrying firmware load\n");
1150 goto retry;
1153 if (rc == -ENOENT)
1154 rc = -EINVAL;
1155 return rc;
1158 static void xc_debug_dump(struct xc4000_priv *priv)
1160 u16 adc_envelope;
1161 u32 freq_error_hz = 0;
1162 u16 lock_status;
1163 u32 hsync_freq_hz = 0;
1164 u16 frame_lines;
1165 u16 quality;
1166 u8 hw_majorversion = 0, hw_minorversion = 0;
1167 u8 fw_majorversion = 0, fw_minorversion = 0;
1168 // u16 fw_buildversion = 0;
1170 /* Wait for stats to stabilize.
1171 * Frame Lines needs two frame times after initial lock
1172 * before it is valid.
1174 xc_wait(100);
1176 xc_get_ADC_Envelope(priv, &adc_envelope);
1177 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1179 xc_get_frequency_error(priv, &freq_error_hz);
1180 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1182 xc_get_lock_status(priv, &lock_status);
1183 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1184 lock_status);
1186 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1187 &fw_majorversion, &fw_minorversion);
1188 // WAS:
1189 // xc_get_buildversion(priv, &fw_buildversion);
1190 // dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
1191 // hw_majorversion, hw_minorversion,
1192 // fw_majorversion, fw_minorversion, fw_buildversion);
1193 // NOW:
1194 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1195 hw_majorversion, hw_minorversion,
1196 fw_majorversion, fw_minorversion);
1198 xc_get_hsync_freq(priv, &hsync_freq_hz);
1199 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1201 xc_get_frame_lines(priv, &frame_lines);
1202 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1204 xc_get_quality(priv, &quality);
1205 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1208 static int xc4000_set_params(struct dvb_frontend *fe,
1209 struct dvb_frontend_parameters *params)
1211 struct xc4000_priv *priv = fe->tuner_priv;
1212 unsigned int type;
1213 int ret;
1215 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1217 if (fe->ops.info.type == FE_ATSC) {
1218 dprintk(1, "%s() ATSC\n", __func__);
1219 switch (params->u.vsb.modulation) {
1220 case VSB_8:
1221 case VSB_16:
1222 dprintk(1, "%s() VSB modulation\n", __func__);
1223 priv->rf_mode = XC_RF_MODE_AIR;
1224 priv->freq_hz = params->frequency - 1750000;
1225 priv->bandwidth = BANDWIDTH_6_MHZ;
1226 priv->video_standard = XC4000_DTV6;
1227 type = DTV6;
1228 break;
1229 case QAM_64:
1230 case QAM_256:
1231 case QAM_AUTO:
1232 dprintk(1, "%s() QAM modulation\n", __func__);
1233 priv->rf_mode = XC_RF_MODE_CABLE;
1234 priv->freq_hz = params->frequency - 1750000;
1235 priv->bandwidth = BANDWIDTH_6_MHZ;
1236 priv->video_standard = XC4000_DTV6;
1237 type = DTV6;
1238 break;
1239 default:
1240 return -EINVAL;
1242 } else if (fe->ops.info.type == FE_OFDM) {
1243 dprintk(1, "%s() OFDM\n", __func__);
1244 switch (params->u.ofdm.bandwidth) {
1245 case BANDWIDTH_6_MHZ:
1246 priv->bandwidth = BANDWIDTH_6_MHZ;
1247 priv->video_standard = XC4000_DTV6;
1248 priv->freq_hz = params->frequency - 1750000;
1249 type = DTV6;
1250 break;
1251 case BANDWIDTH_7_MHZ:
1252 printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
1253 type = DTV7;
1254 return -EINVAL;
1255 case BANDWIDTH_8_MHZ:
1256 priv->bandwidth = BANDWIDTH_8_MHZ;
1257 priv->video_standard = XC4000_DTV8;
1258 priv->freq_hz = params->frequency - 2750000;
1259 type = DTV8;
1260 break;
1261 default:
1262 printk(KERN_ERR "xc4000 bandwidth not set!\n");
1263 return -EINVAL;
1265 priv->rf_mode = XC_RF_MODE_AIR;
1266 } else {
1267 printk(KERN_ERR "xc4000 modulation type not supported!\n");
1268 return -EINVAL;
1271 dprintk(1, "%s() frequency=%d (compensated)\n",
1272 __func__, priv->freq_hz);
1274 /* Make sure the correct firmware type is loaded */
1275 if (check_firmware(fe, type, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
1276 return -EREMOTEIO;
1279 ret = xc_SetSignalSource(priv, priv->rf_mode);
1280 if (ret != XC_RESULT_SUCCESS) {
1281 printk(KERN_ERR
1282 "xc4000: xc_SetSignalSource(%d) failed\n",
1283 priv->rf_mode);
1284 return -EREMOTEIO;
1287 ret = xc_SetTVStandard(priv,
1288 XC4000_Standard[priv->video_standard].VideoMode,
1289 XC4000_Standard[priv->video_standard].AudioMode);
1290 if (ret != XC_RESULT_SUCCESS) {
1291 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1292 return -EREMOTEIO;
1294 #ifdef DJH_DEBUG
1295 ret = xc_set_IF_frequency(priv, priv->if_khz);
1296 if (ret != XC_RESULT_SUCCESS) {
1297 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
1298 priv->if_khz);
1299 return -EIO;
1301 #endif
1302 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1304 if (debug)
1305 xc_debug_dump(priv);
1307 return 0;
1310 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1312 struct xc4000_priv *priv = fe->tuner_priv;
1313 int ret;
1314 u16 id;
1316 ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1317 if (ret == XC_RESULT_SUCCESS) {
1318 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1319 ret = XC_RESULT_RESET_FAILURE;
1320 else
1321 ret = XC_RESULT_SUCCESS;
1324 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1325 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1326 return ret;
1329 static int xc4000_set_analog_params(struct dvb_frontend *fe,
1330 struct analog_parameters *params)
1332 struct xc4000_priv *priv = fe->tuner_priv;
1333 int ret;
1335 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1336 __func__, params->frequency);
1338 /* Fix me: it could be air. */
1339 priv->rf_mode = params->mode;
1340 if (params->mode > XC_RF_MODE_CABLE)
1341 priv->rf_mode = XC_RF_MODE_CABLE;
1343 /* params->frequency is in units of 62.5khz */
1344 priv->freq_hz = params->frequency * 62500;
1346 /* FIX ME: Some video standards may have several possible audio
1347 standards. We simply default to one of them here.
1349 if (params->std & V4L2_STD_MN) {
1350 /* default to BTSC audio standard */
1351 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
1352 goto tune_channel;
1355 if (params->std & V4L2_STD_PAL_BG) {
1356 /* default to NICAM audio standard */
1357 priv->video_standard = XC4000_BG_PAL_NICAM;
1358 goto tune_channel;
1361 if (params->std & V4L2_STD_PAL_I) {
1362 /* default to NICAM audio standard */
1363 priv->video_standard = XC4000_I_PAL_NICAM;
1364 goto tune_channel;
1367 if (params->std & V4L2_STD_PAL_DK) {
1368 /* default to NICAM audio standard */
1369 priv->video_standard = XC4000_DK_PAL_NICAM;
1370 goto tune_channel;
1373 if (params->std & V4L2_STD_SECAM_DK) {
1374 /* default to A2 DK1 audio standard */
1375 priv->video_standard = XC4000_DK_SECAM_A2DK1;
1376 goto tune_channel;
1379 if (params->std & V4L2_STD_SECAM_L) {
1380 priv->video_standard = XC4000_L_SECAM_NICAM;
1381 goto tune_channel;
1384 if (params->std & V4L2_STD_SECAM_LC) {
1385 priv->video_standard = XC4000_LC_SECAM_NICAM;
1386 goto tune_channel;
1389 tune_channel:
1391 /* FIXME - firmware type not being set properly */
1392 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
1393 return -EREMOTEIO;
1396 ret = xc_SetSignalSource(priv, priv->rf_mode);
1397 if (ret != XC_RESULT_SUCCESS) {
1398 printk(KERN_ERR
1399 "xc4000: xc_SetSignalSource(%d) failed\n",
1400 priv->rf_mode);
1401 return -EREMOTEIO;
1404 ret = xc_SetTVStandard(priv,
1405 XC4000_Standard[priv->video_standard].VideoMode,
1406 XC4000_Standard[priv->video_standard].AudioMode);
1407 if (ret != XC_RESULT_SUCCESS) {
1408 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1409 return -EREMOTEIO;
1412 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1414 if (debug)
1415 xc_debug_dump(priv);
1417 return 0;
1420 static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1422 struct xc4000_priv *priv = fe->tuner_priv;
1423 dprintk(1, "%s()\n", __func__);
1424 *freq = priv->freq_hz;
1425 return 0;
1428 static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1430 struct xc4000_priv *priv = fe->tuner_priv;
1431 dprintk(1, "%s()\n", __func__);
1433 *bw = priv->bandwidth;
1434 return 0;
1437 static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1439 struct xc4000_priv *priv = fe->tuner_priv;
1440 u16 lock_status = 0;
1442 xc_get_lock_status(priv, &lock_status);
1444 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1446 *status = lock_status;
1448 return 0;
1451 static int xc4000_sleep(struct dvb_frontend *fe)
1453 /* FIXME: djh disable this for now... */
1454 return XC_RESULT_SUCCESS;
1457 static int xc4000_init(struct dvb_frontend *fe)
1459 struct xc4000_priv *priv = fe->tuner_priv;
1460 dprintk(1, "%s()\n", __func__);
1462 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
1463 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1464 return -EREMOTEIO;
1467 if (debug)
1468 xc_debug_dump(priv);
1470 return 0;
1473 static int xc4000_release(struct dvb_frontend *fe)
1475 struct xc4000_priv *priv = fe->tuner_priv;
1477 dprintk(1, "%s()\n", __func__);
1479 mutex_lock(&xc4000_list_mutex);
1481 if (priv)
1482 hybrid_tuner_release_state(priv);
1484 mutex_unlock(&xc4000_list_mutex);
1486 fe->tuner_priv = NULL;
1488 return 0;
1491 static const struct dvb_tuner_ops xc4000_tuner_ops = {
1492 .info = {
1493 .name = "Xceive XC4000",
1494 .frequency_min = 1000000,
1495 .frequency_max = 1023000000,
1496 .frequency_step = 50000,
1499 .release = xc4000_release,
1500 .init = xc4000_init,
1501 .sleep = xc4000_sleep,
1503 .set_params = xc4000_set_params,
1504 .set_analog_params = xc4000_set_analog_params,
1505 .get_frequency = xc4000_get_frequency,
1506 .get_bandwidth = xc4000_get_bandwidth,
1507 .get_status = xc4000_get_status
1510 struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1511 struct i2c_adapter *i2c,
1512 struct xc4000_config *cfg)
1514 struct xc4000_priv *priv = NULL;
1515 int instance;
1516 u16 id = 0;
1518 dprintk(1, "%s(%d-%04x)\n", __func__,
1519 i2c ? i2c_adapter_id(i2c) : -1,
1520 cfg ? cfg->i2c_address : -1);
1522 mutex_lock(&xc4000_list_mutex);
1524 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1525 hybrid_tuner_instance_list,
1526 i2c, cfg->i2c_address, "xc4000");
1527 switch (instance) {
1528 case 0:
1529 goto fail;
1530 break;
1531 case 1:
1532 /* new tuner instance */
1533 priv->bandwidth = BANDWIDTH_6_MHZ;
1534 fe->tuner_priv = priv;
1535 break;
1536 default:
1537 /* existing tuner instance */
1538 fe->tuner_priv = priv;
1539 break;
1542 if (priv->if_khz == 0) {
1543 /* If the IF hasn't been set yet, use the value provided by
1544 the caller (occurs in hybrid devices where the analog
1545 call to xc4000_attach occurs before the digital side) */
1546 priv->if_khz = cfg->if_khz;
1549 /* Check if firmware has been loaded. It is possible that another
1550 instance of the driver has loaded the firmware.
1553 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1554 goto fail;
1556 switch (id) {
1557 case XC_PRODUCT_ID_FW_LOADED:
1558 printk(KERN_INFO
1559 "xc4000: Successfully identified at address 0x%02x\n",
1560 cfg->i2c_address);
1561 printk(KERN_INFO
1562 "xc4000: Firmware has been loaded previously\n");
1563 break;
1564 case XC_PRODUCT_ID_FW_NOT_LOADED:
1565 printk(KERN_INFO
1566 "xc4000: Successfully identified at address 0x%02x\n",
1567 cfg->i2c_address);
1568 printk(KERN_INFO
1569 "xc4000: Firmware has not been loaded previously\n");
1570 break;
1571 default:
1572 printk(KERN_ERR
1573 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1574 cfg->i2c_address, id);
1575 goto fail;
1578 mutex_unlock(&xc4000_list_mutex);
1580 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1581 sizeof(struct dvb_tuner_ops));
1583 /* FIXME: For now, load the firmware at startup. We will remove this
1584 before the code goes to production... */
1585 check_firmware(fe, DTV8, 0, priv->if_khz);
1587 return fe;
1588 fail:
1589 mutex_unlock(&xc4000_list_mutex);
1591 xc4000_release(fe);
1592 return NULL;
1594 EXPORT_SYMBOL(xc4000_attach);
1596 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1597 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1598 MODULE_LICENSE("GPL");