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