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