]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/common/tuners/xc4000.c
[media] xc4000: added firmware_name parameter
[mirror_ubuntu-artful-kernel.git] / drivers / media / common / tuners / xc4000.c
CommitLineData
8d009a0c
DF
1/*
2 * Driver for Xceive XC4000 "QAM/8VSB single chip tuner"
3 *
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>
8 *
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.
13 *
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
17 *
18 * GNU General Public License for more details.
19 *
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.
23 */
24
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>
5614942b 31#include <linux/mutex.h>
11091a31 32#include <asm/unaligned.h>
8d009a0c
DF
33
34#include "dvb_frontend.h"
35
36#include "xc4000.h"
37#include "tuner-i2c.h"
11091a31 38#include "tuner-xc2028-types.h"
8d009a0c 39
4922cec5 40static int debug;
8d009a0c
DF
41module_param(debug, int, 0644);
42MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
43
44static int no_poweroff;
45module_param(no_poweroff, int, 0644);
46MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
47 "\t\t1 keep device energized and with tuner ready all the times.\n"
48 "\t\tFaster, but consumes more power and keeps the device hotter");
49
fa285bc1
IV
50#define XC4000_DEFAULT_FIRMWARE "xc4000.fw"
51
52static char firmware_name[30];
53module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
54MODULE_PARM_DESC(firmware_name, "\n\t\tFirmware file name. Allows overriding "
55 "the default firmware\n"
56 "\t\tname.");
57
8d009a0c
DF
58static DEFINE_MUTEX(xc4000_list_mutex);
59static LIST_HEAD(hybrid_tuner_instance_list);
60
61#define dprintk(level, fmt, arg...) if (debug >= level) \
62 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
63
11091a31
DH
64/* struct for storing firmware table */
65struct firmware_description {
66 unsigned int type;
67 v4l2_std_id id;
68 __u16 int_freq;
69 unsigned char *ptr;
70 unsigned int size;
71};
72
73struct firmware_properties {
74 unsigned int type;
75 v4l2_std_id id;
76 v4l2_std_id std_req;
77 __u16 int_freq;
78 unsigned int scode_table;
e3bb7c60 79 int scode_nr;
11091a31 80};
8d009a0c
DF
81
82struct xc4000_priv {
83 struct tuner_i2c_props i2c_props;
84 struct list_head hybrid_tuner_instance_list;
11091a31 85 struct firmware_description *firm;
fbe4a29f
IV
86 int firm_size;
87 __u16 firm_version;
88 u32 if_khz;
89 u32 freq_hz;
90 u32 bandwidth;
91 u8 video_standard;
92 u8 rf_mode;
93 u8 ignore_i2c_write_errors;
94 /* struct xc2028_ctrl ctrl; */
d0962382 95 struct firmware_properties cur_fw;
fbe4a29f
IV
96 __u16 hwmodel;
97 __u16 hwvers;
5614942b 98 struct mutex lock;
8d009a0c
DF
99};
100
101/* Misc Defines */
4911085f 102#define MAX_TV_STANDARD 24
8d009a0c
DF
103#define XC_MAX_I2C_WRITE_LENGTH 64
104
105/* Signal Types */
106#define XC_RF_MODE_AIR 0
107#define XC_RF_MODE_CABLE 1
108
109/* Result codes */
110#define XC_RESULT_SUCCESS 0
111#define XC_RESULT_RESET_FAILURE 1
112#define XC_RESULT_I2C_WRITE_FAILURE 2
113#define XC_RESULT_I2C_READ_FAILURE 3
114#define XC_RESULT_OUT_OF_RANGE 5
115
116/* Product id */
117#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
e3bb7c60 118#define XC_PRODUCT_ID_FW_LOADED 0x0FA0
8d009a0c 119
ee4c3cd6 120/* Registers (Write-only) */
8d009a0c
DF
121#define XREG_INIT 0x00
122#define XREG_VIDEO_MODE 0x01
123#define XREG_AUDIO_MODE 0x02
124#define XREG_RF_FREQ 0x03
125#define XREG_D_CODE 0x04
ee4c3cd6
DH
126#define XREG_DIRECTSITTING_MODE 0x05
127#define XREG_SEEK_MODE 0x06
128#define XREG_POWER_DOWN 0x08
129#define XREG_SIGNALSOURCE 0x0A
130#define XREG_AMPLITUDE 0x10
8d009a0c 131
ee4c3cd6 132/* Registers (Read-only) */
8d009a0c
DF
133#define XREG_ADC_ENV 0x00
134#define XREG_QUALITY 0x01
135#define XREG_FRAME_LINES 0x02
136#define XREG_HSYNC_FREQ 0x03
137#define XREG_LOCK 0x04
138#define XREG_FREQ_ERROR 0x05
139#define XREG_SNR 0x06
140#define XREG_VERSION 0x07
141#define XREG_PRODUCT_ID 0x08
8d009a0c
DF
142
143/*
144 Basic firmware description. This will remain with
145 the driver for documentation purposes.
146
147 This represents an I2C firmware file encoded as a
148 string of unsigned char. Format is as follows:
149
150 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
151 char[1 ]=len0_LSB -> length of first write transaction
152 char[2 ]=data0 -> first byte to be sent
153 char[3 ]=data1
154 char[4 ]=data2
155 char[ ]=...
156 char[M ]=dataN -> last byte to be sent
157 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
158 char[M+2]=len1_LSB -> length of second write transaction
159 char[M+3]=data0
160 char[M+4]=data1
161 ...
162 etc.
163
164 The [len] value should be interpreted as follows:
165
166 len= len_MSB _ len_LSB
167 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
168 len=0000_0000_0000_0000 : Reset command: Do hardware reset
169 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
170 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
171
172 For the RESET and WAIT commands, the two following bytes will contain
173 immediately the length of the following transaction.
8d009a0c 174*/
fbe4a29f 175
8d009a0c 176struct XC_TV_STANDARD {
fbe4a29f
IV
177 const char *Name;
178 u16 AudioMode;
179 u16 VideoMode;
4911085f 180 u16 int_freq;
8d009a0c
DF
181};
182
183/* Tuner standards */
ed23db32
DH
184#define XC4000_MN_NTSC_PAL_BTSC 0
185#define XC4000_MN_NTSC_PAL_A2 1
186#define XC4000_MN_NTSC_PAL_EIAJ 2
187#define XC4000_MN_NTSC_PAL_Mono 3
188#define XC4000_BG_PAL_A2 4
189#define XC4000_BG_PAL_NICAM 5
190#define XC4000_BG_PAL_MONO 6
191#define XC4000_I_PAL_NICAM 7
192#define XC4000_I_PAL_NICAM_MONO 8
193#define XC4000_DK_PAL_A2 9
194#define XC4000_DK_PAL_NICAM 10
195#define XC4000_DK_PAL_MONO 11
196#define XC4000_DK_SECAM_A2DK1 12
e3bb7c60
MCC
197#define XC4000_DK_SECAM_A2LDK3 13
198#define XC4000_DK_SECAM_A2MONO 14
4911085f
IV
199#define XC4000_DK_SECAM_NICAM 15
200#define XC4000_L_SECAM_NICAM 16
201#define XC4000_LC_SECAM_NICAM 17
202#define XC4000_DTV6 18
203#define XC4000_DTV8 19
204#define XC4000_DTV7_8 20
205#define XC4000_DTV7 21
206#define XC4000_FM_Radio_INPUT2 22
207#define XC4000_FM_Radio_INPUT1 23
8d009a0c 208
8d009a0c 209static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
4911085f
IV
210 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
211 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
212 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
213 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
214 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
215 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
216 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
217 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
218 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
219 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
220 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
221 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
222 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
223 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
224 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
225 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
226 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
227 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
228 {"DTV6", 0x00C0, 0x8002, 0},
229 {"DTV8", 0x00C0, 0x800B, 0},
230 {"DTV7/8", 0x00C0, 0x801B, 0},
231 {"DTV7", 0x00C0, 0x8007, 0},
232 {"FM Radio-INPUT2", 0x0008, 0x9800,10700},
233 {"FM Radio-INPUT1", 0x0008, 0x9000,10700}
8d009a0c
DF
234};
235
8d009a0c
DF
236static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
237static int xc4000_TunerReset(struct dvb_frontend *fe);
238
239static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
240{
241 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
242 .flags = 0, .buf = buf, .len = len };
8d009a0c 243 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
799ed11a
DH
244 if (priv->ignore_i2c_write_errors == 0) {
245 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
246 len);
247 if (len == 4) {
248 printk("bytes %02x %02x %02x %02x\n", buf[0],
249 buf[1], buf[2], buf[3]);
250 }
251 return XC_RESULT_I2C_WRITE_FAILURE;
252 }
8d009a0c
DF
253 }
254 return XC_RESULT_SUCCESS;
255}
256
8d009a0c
DF
257static void xc_wait(int wait_ms)
258{
259 msleep(wait_ms);
260}
261
262static int xc4000_TunerReset(struct dvb_frontend *fe)
263{
264 struct xc4000_priv *priv = fe->tuner_priv;
265 int ret;
266
267 dprintk(1, "%s()\n", __func__);
268
269 if (fe->callback) {
270 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
271 fe->dvb->priv :
272 priv->i2c_props.adap->algo_data,
273 DVB_FRONTEND_COMPONENT_TUNER,
274 XC4000_TUNER_RESET, 0);
275 if (ret) {
276 printk(KERN_ERR "xc4000: reset failed\n");
277 return XC_RESULT_RESET_FAILURE;
278 }
279 } else {
280 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
281 return XC_RESULT_RESET_FAILURE;
282 }
283 return XC_RESULT_SUCCESS;
284}
285
286static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
287{
288 u8 buf[4];
8d009a0c
DF
289 int result;
290
291 buf[0] = (regAddr >> 8) & 0xFF;
292 buf[1] = regAddr & 0xFF;
293 buf[2] = (i2cData >> 8) & 0xFF;
294 buf[3] = i2cData & 0xFF;
295 result = xc_send_i2c_data(priv, buf, 4);
8d009a0c
DF
296
297 return result;
298}
299
300static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
301{
302 struct xc4000_priv *priv = fe->tuner_priv;
303
304 int i, nbytes_to_send, result;
305 unsigned int len, pos, index;
306 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
307
308 index = 0;
309 while ((i2c_sequence[index] != 0xFF) ||
310 (i2c_sequence[index + 1] != 0xFF)) {
311 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
312 if (len == 0x0000) {
313 /* RESET command */
314 result = xc4000_TunerReset(fe);
315 index += 2;
316 if (result != XC_RESULT_SUCCESS)
317 return result;
318 } else if (len & 0x8000) {
319 /* WAIT command */
320 xc_wait(len & 0x7FFF);
321 index += 2;
322 } else {
323 /* Send i2c data whilst ensuring individual transactions
324 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
325 */
326 index += 2;
327 buf[0] = i2c_sequence[index];
328 buf[1] = i2c_sequence[index + 1];
329 pos = 2;
330 while (pos < len) {
331 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
332 nbytes_to_send =
333 XC_MAX_I2C_WRITE_LENGTH;
334 else
335 nbytes_to_send = (len - pos + 2);
336 for (i = 2; i < nbytes_to_send; i++) {
337 buf[i] = i2c_sequence[index + pos +
338 i - 2];
339 }
340 result = xc_send_i2c_data(priv, buf,
341 nbytes_to_send);
342
343 if (result != XC_RESULT_SUCCESS)
344 return result;
345
346 pos += nbytes_to_send - 2;
347 }
348 index += len;
349 }
350 }
351 return XC_RESULT_SUCCESS;
352}
353
8d009a0c
DF
354static int xc_SetTVStandard(struct xc4000_priv *priv,
355 u16 VideoMode, u16 AudioMode)
356{
357 int ret;
358 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
359 dprintk(1, "%s() Standard = %s\n",
360 __func__,
361 XC4000_Standard[priv->video_standard].Name);
362
799ed11a
DH
363 /* Don't complain when the request fails because of i2c stretching */
364 priv->ignore_i2c_write_errors = 1;
365
8d009a0c
DF
366 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
367 if (ret == XC_RESULT_SUCCESS)
368 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
369
799ed11a
DH
370 priv->ignore_i2c_write_errors = 0;
371
8d009a0c
DF
372 return ret;
373}
374
375static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
376{
377 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
378 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
379
380 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
381 rf_mode = XC_RF_MODE_CABLE;
382 printk(KERN_ERR
383 "%s(), Invalid mode, defaulting to CABLE",
384 __func__);
385 }
386 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
387}
388
389static const struct dvb_tuner_ops xc4000_tuner_ops;
390
391static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
392{
393 u16 freq_code;
394
395 dprintk(1, "%s(%u)\n", __func__, freq_hz);
396
397 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
398 (freq_hz < xc4000_tuner_ops.info.frequency_min))
399 return XC_RESULT_OUT_OF_RANGE;
400
401 freq_code = (u16)(freq_hz / 15625);
402
403 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
404 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
405 only be used for fast scanning for channel lock) */
406 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
407}
408
8d009a0c
DF
409static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
410{
411 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
412}
413
414static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
415{
416 int result;
417 u16 regData;
418 u32 tmp;
419
420 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
421 if (result != XC_RESULT_SUCCESS)
422 return result;
423
1368ceb2
IV
424 tmp = (u32)regData & 0xFFFFU;
425 tmp = (tmp < 0x8000U ? tmp : 0x10000U - tmp);
426 (*freq_error_hz) = tmp * 15625;
8d009a0c
DF
427 return result;
428}
429
430static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
431{
432 return xc4000_readreg(priv, XREG_LOCK, lock_status);
433}
434
435static int xc_get_version(struct xc4000_priv *priv,
436 u8 *hw_majorversion, u8 *hw_minorversion,
437 u8 *fw_majorversion, u8 *fw_minorversion)
438{
439 u16 data;
440 int result;
441
442 result = xc4000_readreg(priv, XREG_VERSION, &data);
443 if (result != XC_RESULT_SUCCESS)
444 return result;
445
446 (*hw_majorversion) = (data >> 12) & 0x0F;
447 (*hw_minorversion) = (data >> 8) & 0x0F;
448 (*fw_majorversion) = (data >> 4) & 0x0F;
449 (*fw_minorversion) = data & 0x0F;
450
451 return 0;
452}
453
8d009a0c
DF
454static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
455{
456 u16 regData;
457 int result;
458
459 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
460 if (result != XC_RESULT_SUCCESS)
461 return result;
462
463 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
464 return result;
465}
466
467static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
468{
469 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
470}
471
472static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
473{
474 return xc4000_readreg(priv, XREG_QUALITY, quality);
475}
476
477static u16 WaitForLock(struct xc4000_priv *priv)
478{
479 u16 lockState = 0;
480 int watchDogCount = 40;
481
482 while ((lockState == 0) && (watchDogCount > 0)) {
483 xc_get_lock_status(priv, &lockState);
484 if (lockState != 1) {
485 xc_wait(5);
486 watchDogCount--;
487 }
488 }
489 return lockState;
490}
491
492#define XC_TUNE_ANALOG 0
493#define XC_TUNE_DIGITAL 1
494static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
495{
fbe4a29f
IV
496 int found = 0;
497 int result = 0;
8d009a0c
DF
498
499 dprintk(1, "%s(%u)\n", __func__, freq_hz);
500
799ed11a
DH
501 /* Don't complain when the request fails because of i2c stretching */
502 priv->ignore_i2c_write_errors = 1;
503 result = xc_set_RF_frequency(priv, freq_hz);
504 priv->ignore_i2c_write_errors = 0;
505
506 if (result != XC_RESULT_SUCCESS)
8d009a0c
DF
507 return 0;
508
509 if (mode == XC_TUNE_ANALOG) {
510 if (WaitForLock(priv) == 1)
511 found = 1;
512 }
513
514 return found;
515}
516
517static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
518{
519 u8 buf[2] = { reg >> 8, reg & 0xff };
520 u8 bval[2] = { 0, 0 };
521 struct i2c_msg msg[2] = {
522 { .addr = priv->i2c_props.addr,
523 .flags = 0, .buf = &buf[0], .len = 2 },
524 { .addr = priv->i2c_props.addr,
525 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
526 };
527
528 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
529 printk(KERN_WARNING "xc4000: I2C read failed\n");
530 return -EREMOTEIO;
531 }
532
533 *val = (bval[0] << 8) | bval[1];
534 return XC_RESULT_SUCCESS;
535}
536
e3bb7c60 537#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
d0962382
DH
538static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
539{
540 if (type & BASE)
541 printk("BASE ");
542 if (type & INIT1)
543 printk("INIT1 ");
544 if (type & F8MHZ)
545 printk("F8MHZ ");
546 if (type & MTS)
547 printk("MTS ");
548 if (type & D2620)
549 printk("D2620 ");
550 if (type & D2633)
551 printk("D2633 ");
552 if (type & DTV6)
553 printk("DTV6 ");
554 if (type & QAM)
555 printk("QAM ");
556 if (type & DTV7)
557 printk("DTV7 ");
558 if (type & DTV78)
559 printk("DTV78 ");
560 if (type & DTV8)
561 printk("DTV8 ");
562 if (type & FM)
563 printk("FM ");
564 if (type & INPUT1)
565 printk("INPUT1 ");
566 if (type & LCD)
567 printk("LCD ");
568 if (type & NOGD)
569 printk("NOGD ");
570 if (type & MONO)
571 printk("MONO ");
572 if (type & ATSC)
573 printk("ATSC ");
574 if (type & IF)
575 printk("IF ");
576 if (type & LG60)
577 printk("LG60 ");
578 if (type & ATI638)
579 printk("ATI638 ");
580 if (type & OREN538)
581 printk("OREN538 ");
582 if (type & OREN36)
583 printk("OREN36 ");
584 if (type & TOYOTA388)
585 printk("TOYOTA388 ");
586 if (type & TOYOTA794)
587 printk("TOYOTA794 ");
588 if (type & DIBCOM52)
589 printk("DIBCOM52 ");
590 if (type & ZARLINK456)
591 printk("ZARLINK456 ");
592 if (type & CHINA)
593 printk("CHINA ");
594 if (type & F6MHZ)
595 printk("F6MHZ ");
596 if (type & INPUT2)
597 printk("INPUT2 ");
598 if (type & SCODE)
599 printk("SCODE ");
600 if (type & HAS_IF)
601 printk("HAS_IF_%d ", int_freq);
602}
603
11091a31
DH
604static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
605 v4l2_std_id *id)
606{
607 struct xc4000_priv *priv = fe->tuner_priv;
608 int i, best_i = -1, best_nr_matches = 0;
609 unsigned int type_mask = 0;
610
11091a31
DH
611 if (!priv->firm) {
612 printk("Error! firmware not loaded\n");
613 return -EINVAL;
614 }
615
616 if (((type & ~SCODE) == 0) && (*id == 0))
617 *id = V4L2_STD_PAL;
618
619 if (type & BASE)
620 type_mask = BASE_TYPES;
621 else if (type & SCODE) {
622 type &= SCODE_TYPES;
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;
628
629 type &= type_mask;
630
631 if (!(type & SCODE))
632 type_mask = ~0;
633
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))
638 goto found;
639 }
640
641 /* Seek for generic video standard match */
642 for (i = 0; i < priv->firm_size; i++) {
643 v4l2_std_id match_mask;
644 int nr_matches;
645
646 if (type != (priv->firm[i].type & type_mask))
647 continue;
648
649 match_mask = *id & priv->firm[i].id;
650 if (!match_mask)
651 continue;
652
653 if ((*id & match_mask) == *id)
654 goto found; /* Supports all the requested standards */
655
656 nr_matches = hweight64(match_mask);
657 if (nr_matches > best_nr_matches) {
658 best_nr_matches = nr_matches;
659 best_i = i;
660 }
661 }
662
663 if (best_nr_matches > 0) {
664 printk("Selecting best matching firmware (%d bits) for "
665 "type=", best_nr_matches);
11091a31
DH
666 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
667 i = best_i;
668 goto found;
669 }
670
671 /*FIXME: Would make sense to seek for type "hint" match ? */
672
673 i = -ENOENT;
674 goto ret;
675
676found:
677 *id = priv->firm[i].id;
678
679ret:
11091a31 680 if (debug) {
b6cdb5be
DH
681 printk("%s firmware for type=", (i < 0) ? "Can't find" :
682 "Found");
d0962382 683 dump_firm_type(type);
11091a31
DH
684 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
685 }
686 return i;
687}
688
689static int load_firmware(struct dvb_frontend *fe, unsigned int type,
690 v4l2_std_id *id)
691{
692 struct xc4000_priv *priv = fe->tuner_priv;
693 int pos, rc;
31f880e2 694 unsigned char *p;
11091a31 695
11091a31
DH
696 pos = seek_firmware(fe, type, id);
697 if (pos < 0)
698 return pos;
699
11091a31 700 p = priv->firm[pos].ptr;
11091a31 701
799ed11a
DH
702 /* Don't complain when the request fails because of i2c stretching */
703 priv->ignore_i2c_write_errors = 1;
704
31f880e2 705 rc = xc_load_i2c_sequence(fe, p);
11091a31 706
799ed11a
DH
707 priv->ignore_i2c_write_errors = 0;
708
31f880e2 709 return rc;
11091a31
DH
710}
711
8d009a0c
DF
712static int xc4000_fwupload(struct dvb_frontend *fe)
713{
714 struct xc4000_priv *priv = fe->tuner_priv;
11091a31
DH
715 const struct firmware *fw = NULL;
716 const unsigned char *p, *endp;
717 int rc = 0;
718 int n, n_array;
719 char name[33];
fbe4a29f 720 const char *fname;
11091a31 721
fa285bc1
IV
722 if (firmware_name[0] != '\0')
723 fname = firmware_name;
724 else
725 fname = XC4000_DEFAULT_FIRMWARE;
11091a31
DH
726
727 printk("Reading firmware %s\n", fname);
728 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
729 if (rc < 0) {
730 if (rc == -ENOENT)
731 printk("Error: firmware %s not found.\n",
732 fname);
733 else
734 printk("Error %d while requesting firmware %s \n",
735 rc, fname);
8d009a0c 736
11091a31
DH
737 return rc;
738 }
739 p = fw->data;
740 endp = p + fw->size;
8d009a0c 741
11091a31
DH
742 if (fw->size < sizeof(name) - 1 + 2 + 2) {
743 printk("Error: firmware file %s has invalid size!\n",
fbe4a29f 744 fname);
11091a31 745 goto corrupt;
8d009a0c
DF
746 }
747
11091a31
DH
748 memcpy(name, p, sizeof(name) - 1);
749 name[sizeof(name) - 1] = 0;
750 p += sizeof(name) - 1;
751
752 priv->firm_version = get_unaligned_le16(p);
753 p += 2;
754
755 n_array = get_unaligned_le16(p);
756 p += 2;
757
b6cdb5be
DH
758 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
759 n_array, fname, name,
760 priv->firm_version >> 8, priv->firm_version & 0xff);
11091a31
DH
761
762 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
763 if (priv->firm == NULL) {
764 printk("Not enough memory to load firmware file.\n");
765 rc = -ENOMEM;
766 goto err;
767 }
768 priv->firm_size = n_array;
769
770 n = -1;
771 while (p < endp) {
772 __u32 type, size;
773 v4l2_std_id id;
774 __u16 int_freq = 0;
775
776 n++;
777 if (n >= n_array) {
778 printk("More firmware images in file than "
fbe4a29f 779 "were expected!\n");
11091a31
DH
780 goto corrupt;
781 }
782
783 /* Checks if there's enough bytes to read */
784 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
785 goto header;
786
787 type = get_unaligned_le32(p);
788 p += sizeof(type);
789
790 id = get_unaligned_le64(p);
791 p += sizeof(id);
792
793 if (type & HAS_IF) {
794 int_freq = get_unaligned_le16(p);
795 p += sizeof(int_freq);
796 if (endp - p < sizeof(size))
797 goto header;
798 }
799
800 size = get_unaligned_le32(p);
801 p += sizeof(size);
802
803 if (!size || size > endp - p) {
804 printk("Firmware type ");
11091a31
DH
805 printk("(%x), id %llx is corrupted "
806 "(size=%d, expected %d)\n",
807 type, (unsigned long long)id,
808 (unsigned)(endp - p), size);
809 goto corrupt;
810 }
811
812 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
813 if (priv->firm[n].ptr == NULL) {
814 printk("Not enough memory to load firmware file.\n");
815 rc = -ENOMEM;
816 goto err;
817 }
d0962382 818
11091a31 819 if (debug) {
d0962382
DH
820 printk("Reading firmware type ");
821 dump_firm_type_and_int_freq(type, int_freq);
11091a31
DH
822 printk("(%x), id %llx, size=%d.\n",
823 type, (unsigned long long)id, size);
824 }
825
826 memcpy(priv->firm[n].ptr, p, size);
827 priv->firm[n].type = type;
828 priv->firm[n].id = id;
829 priv->firm[n].size = size;
830 priv->firm[n].int_freq = int_freq;
831
832 p += size;
8d009a0c
DF
833 }
834
11091a31
DH
835 if (n + 1 != priv->firm_size) {
836 printk("Firmware file is incomplete!\n");
837 goto corrupt;
838 }
839
840 goto done;
841
842header:
843 printk("Firmware header is incomplete!\n");
844corrupt:
845 rc = -EINVAL;
846 printk("Error: firmware file is corrupted!\n");
847
848err:
849 printk("Releasing partially loaded firmware file.\n");
11091a31
DH
850
851done:
8d009a0c 852 release_firmware(fw);
11091a31 853 if (rc == 0)
b6cdb5be 854 dprintk(1, "Firmware files loaded.\n");
11091a31
DH
855
856 return rc;
8d009a0c
DF
857}
858
d0962382
DH
859static int load_scode(struct dvb_frontend *fe, unsigned int type,
860 v4l2_std_id *id, __u16 int_freq, int scode)
861{
862 struct xc4000_priv *priv = fe->tuner_priv;
863 int pos, rc;
864 unsigned char *p;
ee4c3cd6 865 u8 scode_buf[13];
d0962382
DH
866 u8 indirect_mode[5];
867
fe830364 868 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
d0962382
DH
869
870 if (!int_freq) {
871 pos = seek_firmware(fe, type, id);
872 if (pos < 0)
873 return pos;
874 } else {
875 for (pos = 0; pos < priv->firm_size; pos++) {
876 if ((priv->firm[pos].int_freq == int_freq) &&
877 (priv->firm[pos].type & HAS_IF))
878 break;
879 }
880 if (pos == priv->firm_size)
881 return -ENOENT;
882 }
883
884 p = priv->firm[pos].ptr;
885
886 if (priv->firm[pos].type & HAS_IF) {
887 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
888 return -EINVAL;
889 p += 12 * scode;
890 } else {
891 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
892 * has a 2-byte size header in the firmware format. */
893 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
894 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
895 return -EINVAL;
896 p += 14 * scode + 2;
897 }
898
899 tuner_info("Loading SCODE for type=");
900 dump_firm_type_and_int_freq(priv->firm[pos].type,
901 priv->firm[pos].int_freq);
902 printk("(%x), id %016llx.\n", priv->firm[pos].type,
903 (unsigned long long)*id);
904
ee4c3cd6
DH
905 scode_buf[0] = 0x00;
906 memcpy(&scode_buf[1], p, 12);
d0962382
DH
907
908 /* Enter direct-mode */
ee4c3cd6
DH
909 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
910 if (rc < 0) {
911 printk("failed to put device into direct mode!\n");
d0962382 912 return -EIO;
ee4c3cd6 913 }
d0962382 914
ee4c3cd6
DH
915 rc = xc_send_i2c_data(priv, scode_buf, 13);
916 if (rc != XC_RESULT_SUCCESS) {
917 /* Even if the send failed, make sure we set back to indirect
918 mode */
919 printk("Failed to set scode %d\n", rc);
920 }
d0962382
DH
921
922 /* Switch back to indirect-mode */
923 memset(indirect_mode, 0, sizeof(indirect_mode));
924 indirect_mode[4] = 0x88;
ee4c3cd6
DH
925 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
926 msleep(10);
d0962382
DH
927
928 return 0;
929}
930
931static int check_firmware(struct dvb_frontend *fe, unsigned int type,
932 v4l2_std_id std, __u16 int_freq)
933{
934 struct xc4000_priv *priv = fe->tuner_priv;
935 struct firmware_properties new_fw;
936 int rc = 0, is_retry = 0;
937 u16 version, hwmodel;
938 v4l2_std_id std0;
e3bb7c60 939 u8 hw_major, hw_minor, fw_major, fw_minor;
d0962382
DH
940
941 dprintk(1, "%s called\n", __func__);
942
943 if (!priv->firm) {
944 rc = xc4000_fwupload(fe);
945 if (rc < 0)
946 return rc;
947 }
948
949#ifdef DJH_DEBUG
950 if (priv->ctrl.mts && !(type & FM))
951 type |= MTS;
952#endif
953
954retry:
955 new_fw.type = type;
956 new_fw.id = std;
957 new_fw.std_req = std;
fbe4a29f 958 new_fw.scode_table = SCODE /* | priv->ctrl.scode_table */;
d0962382
DH
959 new_fw.scode_nr = 0;
960 new_fw.int_freq = int_freq;
961
962 dprintk(1, "checking firmware, user requested type=");
963 if (debug) {
964 dump_firm_type(new_fw.type);
965 printk("(%x), id %016llx, ", new_fw.type,
966 (unsigned long long)new_fw.std_req);
967 if (!int_freq) {
968 printk("scode_tbl ");
969#ifdef DJH_DEBUG
970 dump_firm_type(priv->ctrl.scode_table);
971 printk("(%x), ", priv->ctrl.scode_table);
972#endif
973 } else
974 printk("int_freq %d, ", new_fw.int_freq);
975 printk("scode_nr %d\n", new_fw.scode_nr);
976 }
977
978 /* No need to reload base firmware if it matches */
979 if (((BASE | new_fw.type) & BASE_TYPES) ==
980 (priv->cur_fw.type & BASE_TYPES)) {
981 dprintk(1, "BASE firmware not changed.\n");
982 goto skip_base;
983 }
984
985 /* Updating BASE - forget about all currently loaded firmware */
986 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
987
988 /* Reset is needed before loading firmware */
989 rc = xc4000_TunerReset(fe);
990 if (rc < 0)
991 goto fail;
992
993 /* BASE firmwares are all std0 */
994 std0 = 0;
995 rc = load_firmware(fe, BASE | new_fw.type, &std0);
996 if (rc < 0) {
997 printk("Error %d while loading base firmware\n", rc);
998 goto fail;
999 }
1000
1001 /* Load INIT1, if needed */
1002 dprintk(1, "Load init1 firmware, if exists\n");
1003
1004 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1005 if (rc == -ENOENT)
1006 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
1007 &std0);
1008 if (rc < 0 && rc != -ENOENT) {
1009 tuner_err("Error %d while loading init1 firmware\n",
1010 rc);
1011 goto fail;
1012 }
1013
1014skip_base:
1015 /*
1016 * No need to reload standard specific firmware if base firmware
1017 * was not reloaded and requested video standards have not changed.
1018 */
1019 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1020 priv->cur_fw.std_req == std) {
1021 dprintk(1, "Std-specific firmware already loaded.\n");
1022 goto skip_std_specific;
1023 }
1024
1025 /* Reloading std-specific firmware forces a SCODE update */
1026 priv->cur_fw.scode_table = 0;
1027
ee4c3cd6 1028 /* Load the standard firmware */
d0962382 1029 rc = load_firmware(fe, new_fw.type, &new_fw.id);
d0962382
DH
1030
1031 if (rc < 0)
1032 goto fail;
1033
1034skip_std_specific:
1035 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1036 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1037 dprintk(1, "SCODE firmware already loaded.\n");
1038 goto check_device;
1039 }
1040
1041 if (new_fw.type & FM)
1042 goto check_device;
1043
1044 /* Load SCODE firmware, if exists */
d0962382
DH
1045 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1046 new_fw.int_freq, new_fw.scode_nr);
ee4c3cd6
DH
1047 if (rc != XC_RESULT_SUCCESS)
1048 dprintk(1, "load scode failed %d\n", rc);
d0962382
DH
1049
1050check_device:
1051 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1052
799ed11a 1053 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
d0962382
DH
1054 &fw_minor) != XC_RESULT_SUCCESS) {
1055 printk("Unable to read tuner registers.\n");
1056 goto fail;
1057 }
1058
1059 dprintk(1, "Device is Xceive %d version %d.%d, "
1060 "firmware version %d.%d\n",
1061 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1062
1063 /* Check firmware version against what we downloaded. */
1064#ifdef DJH_DEBUG
1065 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1066 printk("Incorrect readback of firmware version %x.\n",
1067 (version & 0xff));
1068 goto fail;
1069 }
1070#endif
1071
1072 /* Check that the tuner hardware model remains consistent over time. */
1073 if (priv->hwmodel == 0 && hwmodel == 4000) {
1074 priv->hwmodel = hwmodel;
1075 priv->hwvers = version & 0xff00;
1076 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1077 priv->hwvers != (version & 0xff00)) {
1078 printk("Read invalid device hardware information - tuner "
fbe4a29f 1079 "hung?\n");
d0962382
DH
1080 goto fail;
1081 }
1082
1083 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1084
1085 /*
1086 * By setting BASE in cur_fw.type only after successfully loading all
1087 * firmwares, we can:
1088 * 1. Identify that BASE firmware with type=0 has been loaded;
1089 * 2. Tell whether BASE firmware was just changed the next time through.
1090 */
1091 priv->cur_fw.type |= BASE;
1092
1093 return 0;
1094
1095fail:
1096 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1097 if (!is_retry) {
1098 msleep(50);
1099 is_retry = 1;
1100 dprintk(1, "Retrying firmware load\n");
1101 goto retry;
1102 }
1103
1104 if (rc == -ENOENT)
1105 rc = -EINVAL;
1106 return rc;
1107}
11091a31 1108
8d009a0c
DF
1109static void xc_debug_dump(struct xc4000_priv *priv)
1110{
fbe4a29f
IV
1111 u16 adc_envelope;
1112 u32 freq_error_hz = 0;
1113 u16 lock_status;
1114 u32 hsync_freq_hz = 0;
1115 u16 frame_lines;
1116 u16 quality;
1117 u8 hw_majorversion = 0, hw_minorversion = 0;
1118 u8 fw_majorversion = 0, fw_minorversion = 0;
8d009a0c
DF
1119
1120 /* Wait for stats to stabilize.
1121 * Frame Lines needs two frame times after initial lock
1122 * before it is valid.
1123 */
1124 xc_wait(100);
1125
fbe4a29f 1126 xc_get_ADC_Envelope(priv, &adc_envelope);
8d009a0c
DF
1127 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1128
1129 xc_get_frequency_error(priv, &freq_error_hz);
1130 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1131
fbe4a29f 1132 xc_get_lock_status(priv, &lock_status);
8d009a0c
DF
1133 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1134 lock_status);
1135
fbe4a29f
IV
1136 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1137 &fw_majorversion, &fw_minorversion);
1138
8d009a0c
DF
1139 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1140 hw_majorversion, hw_minorversion,
1141 fw_majorversion, fw_minorversion);
1142
fbe4a29f 1143 xc_get_hsync_freq(priv, &hsync_freq_hz);
8d009a0c
DF
1144 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1145
fbe4a29f 1146 xc_get_frame_lines(priv, &frame_lines);
8d009a0c
DF
1147 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1148
fbe4a29f 1149 xc_get_quality(priv, &quality);
8d009a0c
DF
1150 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1151}
1152
1153static int xc4000_set_params(struct dvb_frontend *fe,
1154 struct dvb_frontend_parameters *params)
1155{
1156 struct xc4000_priv *priv = fe->tuner_priv;
ed23db32 1157 unsigned int type;
5614942b 1158 int ret = -EREMOTEIO;
8d009a0c 1159
8d009a0c
DF
1160 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1161
5614942b
IV
1162 mutex_lock(&priv->lock);
1163
8d009a0c
DF
1164 if (fe->ops.info.type == FE_ATSC) {
1165 dprintk(1, "%s() ATSC\n", __func__);
1166 switch (params->u.vsb.modulation) {
1167 case VSB_8:
1168 case VSB_16:
1169 dprintk(1, "%s() VSB modulation\n", __func__);
1170 priv->rf_mode = XC_RF_MODE_AIR;
1171 priv->freq_hz = params->frequency - 1750000;
1172 priv->bandwidth = BANDWIDTH_6_MHZ;
ed23db32
DH
1173 priv->video_standard = XC4000_DTV6;
1174 type = DTV6;
8d009a0c
DF
1175 break;
1176 case QAM_64:
1177 case QAM_256:
1178 case QAM_AUTO:
1179 dprintk(1, "%s() QAM modulation\n", __func__);
1180 priv->rf_mode = XC_RF_MODE_CABLE;
1181 priv->freq_hz = params->frequency - 1750000;
1182 priv->bandwidth = BANDWIDTH_6_MHZ;
ed23db32
DH
1183 priv->video_standard = XC4000_DTV6;
1184 type = DTV6;
8d009a0c
DF
1185 break;
1186 default:
5614942b
IV
1187 ret = -EINVAL;
1188 goto fail;
8d009a0c
DF
1189 }
1190 } else if (fe->ops.info.type == FE_OFDM) {
1191 dprintk(1, "%s() OFDM\n", __func__);
1192 switch (params->u.ofdm.bandwidth) {
1193 case BANDWIDTH_6_MHZ:
1194 priv->bandwidth = BANDWIDTH_6_MHZ;
ed23db32 1195 priv->video_standard = XC4000_DTV6;
8d009a0c 1196 priv->freq_hz = params->frequency - 1750000;
ed23db32 1197 type = DTV6;
8d009a0c
DF
1198 break;
1199 case BANDWIDTH_7_MHZ:
f0ef7c88
IV
1200 priv->bandwidth = BANDWIDTH_7_MHZ;
1201 priv->video_standard = XC4000_DTV7;
1202 priv->freq_hz = params->frequency - 2250000;
ed23db32 1203 type = DTV7;
f0ef7c88 1204 break;
8d009a0c
DF
1205 case BANDWIDTH_8_MHZ:
1206 priv->bandwidth = BANDWIDTH_8_MHZ;
ed23db32 1207 priv->video_standard = XC4000_DTV8;
8d009a0c 1208 priv->freq_hz = params->frequency - 2750000;
ed23db32 1209 type = DTV8;
8d009a0c 1210 break;
f0ef7c88
IV
1211 case BANDWIDTH_AUTO:
1212 if (params->frequency < 400000000) {
1213 priv->bandwidth = BANDWIDTH_7_MHZ;
1214 priv->freq_hz = params->frequency - 2250000;
1215 } else {
1216 priv->bandwidth = BANDWIDTH_8_MHZ;
1217 priv->freq_hz = params->frequency - 2750000;
1218 }
1219 priv->video_standard = XC4000_DTV7_8;
1220 type = DTV78;
1221 break;
8d009a0c
DF
1222 default:
1223 printk(KERN_ERR "xc4000 bandwidth not set!\n");
5614942b
IV
1224 ret = -EINVAL;
1225 goto fail;
8d009a0c
DF
1226 }
1227 priv->rf_mode = XC_RF_MODE_AIR;
1228 } else {
1229 printk(KERN_ERR "xc4000 modulation type not supported!\n");
5614942b
IV
1230 ret = -EINVAL;
1231 goto fail;
8d009a0c
DF
1232 }
1233
1234 dprintk(1, "%s() frequency=%d (compensated)\n",
1235 __func__, priv->freq_hz);
1236
ed23db32 1237 /* Make sure the correct firmware type is loaded */
5614942b
IV
1238 if (check_firmware(fe, type, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1239 goto fail;
ed23db32 1240
8d009a0c
DF
1241 ret = xc_SetSignalSource(priv, priv->rf_mode);
1242 if (ret != XC_RESULT_SUCCESS) {
1243 printk(KERN_ERR
5614942b
IV
1244 "xc4000: xc_SetSignalSource(%d) failed\n",
1245 priv->rf_mode);
1246 goto fail;
8d009a0c
DF
1247 }
1248
1249 ret = xc_SetTVStandard(priv,
1250 XC4000_Standard[priv->video_standard].VideoMode,
1251 XC4000_Standard[priv->video_standard].AudioMode);
1252 if (ret != XC_RESULT_SUCCESS) {
1253 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
5614942b 1254 goto fail;
8d009a0c 1255 }
8d009a0c
DF
1256 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1257
1258 if (debug)
1259 xc_debug_dump(priv);
1260
5614942b
IV
1261 ret = 0;
1262
1263fail:
1264 mutex_unlock(&priv->lock);
1265
1266 return ret;
8d009a0c
DF
1267}
1268
8d009a0c
DF
1269static int xc4000_set_analog_params(struct dvb_frontend *fe,
1270 struct analog_parameters *params)
1271{
1272 struct xc4000_priv *priv = fe->tuner_priv;
5614942b 1273 int ret = -EREMOTEIO;
8d009a0c 1274
8d009a0c
DF
1275 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1276 __func__, params->frequency);
1277
5614942b
IV
1278 mutex_lock(&priv->lock);
1279
8d009a0c
DF
1280 /* Fix me: it could be air. */
1281 priv->rf_mode = params->mode;
1282 if (params->mode > XC_RF_MODE_CABLE)
1283 priv->rf_mode = XC_RF_MODE_CABLE;
1284
1285 /* params->frequency is in units of 62.5khz */
1286 priv->freq_hz = params->frequency * 62500;
1287
1288 /* FIX ME: Some video standards may have several possible audio
1289 standards. We simply default to one of them here.
1290 */
1291 if (params->std & V4L2_STD_MN) {
1292 /* default to BTSC audio standard */
ed23db32 1293 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
8d009a0c
DF
1294 goto tune_channel;
1295 }
1296
1297 if (params->std & V4L2_STD_PAL_BG) {
1298 /* default to NICAM audio standard */
ed23db32 1299 priv->video_standard = XC4000_BG_PAL_NICAM;
8d009a0c
DF
1300 goto tune_channel;
1301 }
1302
1303 if (params->std & V4L2_STD_PAL_I) {
1304 /* default to NICAM audio standard */
ed23db32 1305 priv->video_standard = XC4000_I_PAL_NICAM;
8d009a0c
DF
1306 goto tune_channel;
1307 }
1308
1309 if (params->std & V4L2_STD_PAL_DK) {
1310 /* default to NICAM audio standard */
ed23db32 1311 priv->video_standard = XC4000_DK_PAL_NICAM;
8d009a0c
DF
1312 goto tune_channel;
1313 }
1314
1315 if (params->std & V4L2_STD_SECAM_DK) {
1316 /* default to A2 DK1 audio standard */
ed23db32 1317 priv->video_standard = XC4000_DK_SECAM_A2DK1;
8d009a0c
DF
1318 goto tune_channel;
1319 }
1320
1321 if (params->std & V4L2_STD_SECAM_L) {
ed23db32 1322 priv->video_standard = XC4000_L_SECAM_NICAM;
8d009a0c
DF
1323 goto tune_channel;
1324 }
1325
1326 if (params->std & V4L2_STD_SECAM_LC) {
ed23db32 1327 priv->video_standard = XC4000_LC_SECAM_NICAM;
8d009a0c
DF
1328 goto tune_channel;
1329 }
1330
1331tune_channel:
ed23db32
DH
1332
1333 /* FIXME - firmware type not being set properly */
5614942b
IV
1334 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1335 goto fail;
ed23db32 1336
8d009a0c
DF
1337 ret = xc_SetSignalSource(priv, priv->rf_mode);
1338 if (ret != XC_RESULT_SUCCESS) {
1339 printk(KERN_ERR
5614942b
IV
1340 "xc4000: xc_SetSignalSource(%d) failed\n",
1341 priv->rf_mode);
1342 goto fail;
8d009a0c
DF
1343 }
1344
1345 ret = xc_SetTVStandard(priv,
1346 XC4000_Standard[priv->video_standard].VideoMode,
1347 XC4000_Standard[priv->video_standard].AudioMode);
1348 if (ret != XC_RESULT_SUCCESS) {
1349 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
5614942b 1350 goto fail;
8d009a0c
DF
1351 }
1352
1353 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1354
1355 if (debug)
1356 xc_debug_dump(priv);
1357
5614942b
IV
1358 ret = 0;
1359
1360fail:
1361 mutex_unlock(&priv->lock);
1362
1363 return ret;
8d009a0c
DF
1364}
1365
1366static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1367{
1368 struct xc4000_priv *priv = fe->tuner_priv;
1369 dprintk(1, "%s()\n", __func__);
1370 *freq = priv->freq_hz;
1371 return 0;
1372}
1373
1374static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1375{
1376 struct xc4000_priv *priv = fe->tuner_priv;
1377 dprintk(1, "%s()\n", __func__);
1378
1379 *bw = priv->bandwidth;
1380 return 0;
1381}
1382
1383static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1384{
1385 struct xc4000_priv *priv = fe->tuner_priv;
fbe4a29f 1386 u16 lock_status = 0;
8d009a0c 1387
5614942b
IV
1388 mutex_lock(&priv->lock);
1389
8d009a0c
DF
1390 xc_get_lock_status(priv, &lock_status);
1391
5614942b
IV
1392 mutex_unlock(&priv->lock);
1393
8d009a0c
DF
1394 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1395
1396 *status = lock_status;
1397
1398 return 0;
1399}
1400
8d009a0c
DF
1401static int xc4000_sleep(struct dvb_frontend *fe)
1402{
ee4c3cd6
DH
1403 /* FIXME: djh disable this for now... */
1404 return XC_RESULT_SUCCESS;
8d009a0c
DF
1405}
1406
1407static int xc4000_init(struct dvb_frontend *fe)
1408{
1409 struct xc4000_priv *priv = fe->tuner_priv;
5614942b 1410 int ret;
8d009a0c
DF
1411 dprintk(1, "%s()\n", __func__);
1412
5614942b
IV
1413 mutex_lock(&priv->lock);
1414 ret = check_firmware(fe, DTV8, 0, priv->if_khz);
1415 mutex_unlock(&priv->lock);
1416 if (ret != XC_RESULT_SUCCESS) {
8d009a0c
DF
1417 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1418 return -EREMOTEIO;
1419 }
1420
1421 if (debug)
1422 xc_debug_dump(priv);
1423
1424 return 0;
1425}
1426
1427static int xc4000_release(struct dvb_frontend *fe)
1428{
1429 struct xc4000_priv *priv = fe->tuner_priv;
1430
1431 dprintk(1, "%s()\n", __func__);
1432
1433 mutex_lock(&xc4000_list_mutex);
1434
1435 if (priv)
1436 hybrid_tuner_release_state(priv);
1437
1438 mutex_unlock(&xc4000_list_mutex);
1439
1440 fe->tuner_priv = NULL;
1441
1442 return 0;
1443}
1444
1445static const struct dvb_tuner_ops xc4000_tuner_ops = {
1446 .info = {
1447 .name = "Xceive XC4000",
1448 .frequency_min = 1000000,
1449 .frequency_max = 1023000000,
1450 .frequency_step = 50000,
1451 },
1452
1453 .release = xc4000_release,
1454 .init = xc4000_init,
1455 .sleep = xc4000_sleep,
1456
1457 .set_params = xc4000_set_params,
1458 .set_analog_params = xc4000_set_analog_params,
1459 .get_frequency = xc4000_get_frequency,
1460 .get_bandwidth = xc4000_get_bandwidth,
1461 .get_status = xc4000_get_status
1462};
1463
1464struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1465 struct i2c_adapter *i2c,
1466 struct xc4000_config *cfg)
1467{
1468 struct xc4000_priv *priv = NULL;
fbe4a29f
IV
1469 int instance;
1470 u16 id = 0;
8d009a0c
DF
1471
1472 dprintk(1, "%s(%d-%04x)\n", __func__,
1473 i2c ? i2c_adapter_id(i2c) : -1,
1474 cfg ? cfg->i2c_address : -1);
1475
1476 mutex_lock(&xc4000_list_mutex);
1477
1478 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1479 hybrid_tuner_instance_list,
1480 i2c, cfg->i2c_address, "xc4000");
1481 switch (instance) {
1482 case 0:
1483 goto fail;
1484 break;
1485 case 1:
1486 /* new tuner instance */
1487 priv->bandwidth = BANDWIDTH_6_MHZ;
5614942b 1488 mutex_init(&priv->lock);
8d009a0c
DF
1489 fe->tuner_priv = priv;
1490 break;
1491 default:
1492 /* existing tuner instance */
1493 fe->tuner_priv = priv;
1494 break;
1495 }
1496
1497 if (priv->if_khz == 0) {
1498 /* If the IF hasn't been set yet, use the value provided by
1499 the caller (occurs in hybrid devices where the analog
1500 call to xc4000_attach occurs before the digital side) */
1501 priv->if_khz = cfg->if_khz;
1502 }
1503
1504 /* Check if firmware has been loaded. It is possible that another
1505 instance of the driver has loaded the firmware.
1506 */
1507
1508 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1509 goto fail;
1510
1511 switch (id) {
1512 case XC_PRODUCT_ID_FW_LOADED:
1513 printk(KERN_INFO
1514 "xc4000: Successfully identified at address 0x%02x\n",
1515 cfg->i2c_address);
1516 printk(KERN_INFO
1517 "xc4000: Firmware has been loaded previously\n");
1518 break;
1519 case XC_PRODUCT_ID_FW_NOT_LOADED:
1520 printk(KERN_INFO
1521 "xc4000: Successfully identified at address 0x%02x\n",
1522 cfg->i2c_address);
1523 printk(KERN_INFO
1524 "xc4000: Firmware has not been loaded previously\n");
1525 break;
1526 default:
1527 printk(KERN_ERR
1528 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1529 cfg->i2c_address, id);
1530 goto fail;
1531 }
1532
1533 mutex_unlock(&xc4000_list_mutex);
1534
1535 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1536 sizeof(struct dvb_tuner_ops));
1537
11091a31
DH
1538 /* FIXME: For now, load the firmware at startup. We will remove this
1539 before the code goes to production... */
5614942b 1540 mutex_lock(&priv->lock);
fe830364 1541 check_firmware(fe, DTV8, 0, priv->if_khz);
5614942b 1542 mutex_unlock(&priv->lock);
11091a31 1543
8d009a0c
DF
1544 return fe;
1545fail:
1546 mutex_unlock(&xc4000_list_mutex);
1547
1548 xc4000_release(fe);
1549 return NULL;
1550}
1551EXPORT_SYMBOL(xc4000_attach);
1552
1553MODULE_AUTHOR("Steven Toth, Davide Ferri");
1554MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1555MODULE_LICENSE("GPL");