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