blob: 98ec80abb0477e8d5e2a949b599df8598475f1c7 [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
39static int debug;
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
Devin Heitmueller11091a32009-07-20 00:54:57 -030055#define XC4000_DEFAULT_FIRMWARE "xc4000-01.fw"
56#define XC4000_DEFAULT_FIRMWARE_SIZE 8434
57
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;
88};
89
90/* Misc Defines */
91#define MAX_TV_STANDARD 23
92#define XC_MAX_I2C_WRITE_LENGTH 64
93
94/* Signal Types */
95#define XC_RF_MODE_AIR 0
96#define XC_RF_MODE_CABLE 1
97
98/* Result codes */
99#define XC_RESULT_SUCCESS 0
100#define XC_RESULT_RESET_FAILURE 1
101#define XC_RESULT_I2C_WRITE_FAILURE 2
102#define XC_RESULT_I2C_READ_FAILURE 3
103#define XC_RESULT_OUT_OF_RANGE 5
104
105/* Product id */
106#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
107#define XC_PRODUCT_ID_FW_LOADED 0x0FA0 /* WAS: 0x1388*/
108
109/* Registers */
110#define XREG_INIT 0x00
111#define XREG_VIDEO_MODE 0x01
112#define XREG_AUDIO_MODE 0x02
113#define XREG_RF_FREQ 0x03
114#define XREG_D_CODE 0x04
115#define XREG_IF_OUT 0x05 /* ?? */
116#define XREG_SEEK_MODE 0x07 /* WAS: 0x06 */
117#define XREG_POWER_DOWN 0x08 /* WAS: 0x0A Obsolete */
118#define XREG_SIGNALSOURCE 0x0A /* WAS: 0x0D 0=Air, 1=Cable */
119//#define XREG_SMOOTHEDCVBS 0x0E
120//#define XREG_XTALFREQ 0x0F
121//#define XREG_FINERFREQ 0x10
122//#define XREG_DDIMODE 0x11
123
124#define XREG_ADC_ENV 0x00
125#define XREG_QUALITY 0x01
126#define XREG_FRAME_LINES 0x02
127#define XREG_HSYNC_FREQ 0x03
128#define XREG_LOCK 0x04
129#define XREG_FREQ_ERROR 0x05
130#define XREG_SNR 0x06
131#define XREG_VERSION 0x07
132#define XREG_PRODUCT_ID 0x08
133//#define XREG_BUSY 0x09
134//#define XREG_BUILD 0x0D
135
136/*
137 Basic firmware description. This will remain with
138 the driver for documentation purposes.
139
140 This represents an I2C firmware file encoded as a
141 string of unsigned char. Format is as follows:
142
143 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
144 char[1 ]=len0_LSB -> length of first write transaction
145 char[2 ]=data0 -> first byte to be sent
146 char[3 ]=data1
147 char[4 ]=data2
148 char[ ]=...
149 char[M ]=dataN -> last byte to be sent
150 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
151 char[M+2]=len1_LSB -> length of second write transaction
152 char[M+3]=data0
153 char[M+4]=data1
154 ...
155 etc.
156
157 The [len] value should be interpreted as follows:
158
159 len= len_MSB _ len_LSB
160 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
161 len=0000_0000_0000_0000 : Reset command: Do hardware reset
162 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
163 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
164
165 For the RESET and WAIT commands, the two following bytes will contain
166 immediately the length of the following transaction.
167
168*/
169struct XC_TV_STANDARD {
170 char *Name;
171 u16 AudioMode;
172 u16 VideoMode;
173};
174
175/* Tuner standards */
176#define MN_NTSC_PAL_BTSC 0
177#define MN_NTSC_PAL_A2 1
178#define MN_NTSC_PAL_EIAJ 2
179#define MN_NTSC_PAL_Mono 3
180#define BG_PAL_A2 4
181#define BG_PAL_NICAM 5
182#define BG_PAL_MONO 6
183#define I_PAL_NICAM 7
184#define I_PAL_NICAM_MONO 8
185#define DK_PAL_A2 9
186#define DK_PAL_NICAM 10
187#define DK_PAL_MONO 11
188#define DK_SECAM_A2DK1 12
189#define DK_SECAM_A2LDK3 13
190#define DK_SECAM_A2MONO 14
191#define L_SECAM_NICAM 15
192#define LC_SECAM_NICAM 16
Davide Ferri8d009a02009-06-23 22:34:06 -0300193#define FM_Radio_INPUT2 21
194#define FM_Radio_INPUT1 22
195
196/* WAS :
197static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
198 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
199 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
200 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
201 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
202 {"B/G-PAL-A2", 0x0A00, 0x8049},
203 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
204 {"B/G-PAL-MONO", 0x0878, 0x8059},
205 {"I-PAL-NICAM", 0x1080, 0x8009},
206 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
207 {"D/K-PAL-A2", 0x1600, 0x8009},
208 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
209 {"D/K-PAL-MONO", 0x1478, 0x8009},
210 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
211 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
212 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
213 {"L-SECAM-NICAM", 0x8E82, 0x0009},
214 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
215 {"DTV6", 0x00C0, 0x8002},
216 {"DTV8", 0x00C0, 0x800B},
217 {"DTV7/8", 0x00C0, 0x801B},
218 {"DTV7", 0x00C0, 0x8007},
219 {"FM Radio-INPUT2", 0x9802, 0x9002},
220 {"FM Radio-INPUT1", 0x0208, 0x9002}
221};*/
222
223static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
224 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x8020},
225 {"M/N-NTSC/PAL-A2", 0x0000, 0x8020},
226 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x8020},
227 {"M/N-NTSC/PAL-Mono", 0x0078, 0x8020},
228 {"B/G-PAL-A2", 0x0000, 0x8059},
229 {"B/G-PAL-NICAM", 0x0004, 0x8059},
230 {"B/G-PAL-MONO", 0x0078, 0x8059},
231 {"I-PAL-NICAM", 0x0080, 0x8049},
232 {"I-PAL-NICAM-MONO", 0x0078, 0x8049},
233 {"D/K-PAL-A2", 0x0000, 0x8049},
234 {"D/K-PAL-NICAM", 0x0080, 0x8049},
235 {"D/K-PAL-MONO", 0x0078, 0x8049},
236 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049},
237 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049},
238 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049},
239 {"L-SECAM-NICAM", 0x8080, 0x0009},
240 {"L'-SECAM-NICAM", 0x8080, 0x4009},
241 {"DTV6", 0x00C0, 0x8002},
242 {"DTV8", 0x00C0, 0x800B},
243 {"DTV7/8", 0x00C0, 0x801B},
244 {"DTV7", 0x00C0, 0x8007},
245 {"FM Radio-INPUT2", 0x0008, 0x9800},
246 {"FM Radio-INPUT1", 0x0008, 0x9000}
247};
248
249static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe);
250static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
251static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
252static int xc4000_TunerReset(struct dvb_frontend *fe);
253
254static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
255{
256 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
257 .flags = 0, .buf = buf, .len = len };
258
259 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
260 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n", len);
261 return XC_RESULT_I2C_WRITE_FAILURE;
262 }
263 return XC_RESULT_SUCCESS;
264}
265
266/* This routine is never used because the only time we read data from the
267 i2c bus is when we read registers, and we want that to be an atomic i2c
268 transaction in case we are on a multi-master bus */
269static int xc_read_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
270{
271 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
272 .flags = I2C_M_RD, .buf = buf, .len = len };
273
274 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
275 printk(KERN_ERR "xc4000 I2C read failed (len=%i)\n", len);
276 return -EREMOTEIO;
277 }
278 return 0;
279}
280
281static void xc_wait(int wait_ms)
282{
283 msleep(wait_ms);
284}
285
286static int xc4000_TunerReset(struct dvb_frontend *fe)
287{
288 struct xc4000_priv *priv = fe->tuner_priv;
289 int ret;
290
291 dprintk(1, "%s()\n", __func__);
292
293 if (fe->callback) {
294 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
295 fe->dvb->priv :
296 priv->i2c_props.adap->algo_data,
297 DVB_FRONTEND_COMPONENT_TUNER,
298 XC4000_TUNER_RESET, 0);
299 if (ret) {
300 printk(KERN_ERR "xc4000: reset failed\n");
301 return XC_RESULT_RESET_FAILURE;
302 }
303 } else {
304 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
305 return XC_RESULT_RESET_FAILURE;
306 }
307 return XC_RESULT_SUCCESS;
308}
309
310static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
311{
312 u8 buf[4];
313// int WatchDogTimer = 100;
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);
321//WAS THERE
322// if (result == XC_RESULT_SUCCESS) {
323// /* wait for busy flag to clear */
324// while ((WatchDogTimer > 0) && (result == XC_RESULT_SUCCESS)) {
325// buf[0] = 0;
326// buf[1] = XREG_BUSY;
327//
328// result = xc_send_i2c_data(priv, buf, 2);
329// if (result == XC_RESULT_SUCCESS) {
330// result = xc_read_i2c_data(priv, buf, 2);
331// if (result == XC_RESULT_SUCCESS) {
332// if ((buf[0] == 0) && (buf[1] == 0)) {
333// /* busy flag cleared */
334// break;
335// } else {
336// xc_wait(5); /* wait 5 ms */
337// WatchDogTimer--;
338// }
339// }
340// }
341// }
342// }
343// if (WatchDogTimer < 0)
344// result = XC_RESULT_I2C_WRITE_FAILURE;
345
346 return result;
347}
348
349static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
350{
351 struct xc4000_priv *priv = fe->tuner_priv;
352
353 int i, nbytes_to_send, result;
354 unsigned int len, pos, index;
355 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
356
357 index = 0;
358 while ((i2c_sequence[index] != 0xFF) ||
359 (i2c_sequence[index + 1] != 0xFF)) {
360 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
361 if (len == 0x0000) {
362 /* RESET command */
363 result = xc4000_TunerReset(fe);
364 index += 2;
365 if (result != XC_RESULT_SUCCESS)
366 return result;
367 } else if (len & 0x8000) {
368 /* WAIT command */
369 xc_wait(len & 0x7FFF);
370 index += 2;
371 } else {
372 /* Send i2c data whilst ensuring individual transactions
373 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
374 */
375 index += 2;
376 buf[0] = i2c_sequence[index];
377 buf[1] = i2c_sequence[index + 1];
378 pos = 2;
379 while (pos < len) {
380 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
381 nbytes_to_send =
382 XC_MAX_I2C_WRITE_LENGTH;
383 else
384 nbytes_to_send = (len - pos + 2);
385 for (i = 2; i < nbytes_to_send; i++) {
386 buf[i] = i2c_sequence[index + pos +
387 i - 2];
388 }
389 result = xc_send_i2c_data(priv, buf,
390 nbytes_to_send);
391
392 if (result != XC_RESULT_SUCCESS)
393 return result;
394
395 pos += nbytes_to_send - 2;
396 }
397 index += len;
398 }
399 }
400 return XC_RESULT_SUCCESS;
401}
402
403static int xc_initialize(struct xc4000_priv *priv)
404{
405 dprintk(1, "%s()\n", __func__);
406 return xc_write_reg(priv, XREG_INIT, 0);
407}
408
409static int xc_SetTVStandard(struct xc4000_priv *priv,
410 u16 VideoMode, u16 AudioMode)
411{
412 int ret;
413 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
414 dprintk(1, "%s() Standard = %s\n",
415 __func__,
416 XC4000_Standard[priv->video_standard].Name);
417
418 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
419 if (ret == XC_RESULT_SUCCESS)
420 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
421
422 return ret;
423}
424
425static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
426{
427 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
428 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
429
430 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
431 rf_mode = XC_RF_MODE_CABLE;
432 printk(KERN_ERR
433 "%s(), Invalid mode, defaulting to CABLE",
434 __func__);
435 }
436 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
437}
438
439static const struct dvb_tuner_ops xc4000_tuner_ops;
440
441static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
442{
443 u16 freq_code;
444
445 dprintk(1, "%s(%u)\n", __func__, freq_hz);
446
447 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
448 (freq_hz < xc4000_tuner_ops.info.frequency_min))
449 return XC_RESULT_OUT_OF_RANGE;
450
451 freq_code = (u16)(freq_hz / 15625);
452
453 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
454 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
455 only be used for fast scanning for channel lock) */
456 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
457}
458
459
460static int xc_set_IF_frequency(struct xc4000_priv *priv, u32 freq_khz)
461{
462 u32 freq_code = (freq_khz * 1024)/1000;
463 dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
464 __func__, freq_khz, freq_code);
465
466 return xc_write_reg(priv, XREG_IF_OUT, freq_code);
467}
468
469
470static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
471{
472 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
473}
474
475static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
476{
477 int result;
478 u16 regData;
479 u32 tmp;
480
481 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
482 if (result != XC_RESULT_SUCCESS)
483 return result;
484
485 tmp = (u32)regData;
486 (*freq_error_hz) = (tmp * 15625) / 1000;
487 return result;
488}
489
490static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
491{
492 return xc4000_readreg(priv, XREG_LOCK, lock_status);
493}
494
495static int xc_get_version(struct xc4000_priv *priv,
496 u8 *hw_majorversion, u8 *hw_minorversion,
497 u8 *fw_majorversion, u8 *fw_minorversion)
498{
499 u16 data;
500 int result;
501
502 result = xc4000_readreg(priv, XREG_VERSION, &data);
503 if (result != XC_RESULT_SUCCESS)
504 return result;
505
506 (*hw_majorversion) = (data >> 12) & 0x0F;
507 (*hw_minorversion) = (data >> 8) & 0x0F;
508 (*fw_majorversion) = (data >> 4) & 0x0F;
509 (*fw_minorversion) = data & 0x0F;
510
511 return 0;
512}
513
514/* WAS THERE
515static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
516{
517 return xc4000_readreg(priv, XREG_BUILD, buildrev);
518}*/
519
520static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
521{
522 u16 regData;
523 int result;
524
525 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
526 if (result != XC_RESULT_SUCCESS)
527 return result;
528
529 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
530 return result;
531}
532
533static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
534{
535 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
536}
537
538static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
539{
540 return xc4000_readreg(priv, XREG_QUALITY, quality);
541}
542
543static u16 WaitForLock(struct xc4000_priv *priv)
544{
545 u16 lockState = 0;
546 int watchDogCount = 40;
547
548 while ((lockState == 0) && (watchDogCount > 0)) {
549 xc_get_lock_status(priv, &lockState);
550 if (lockState != 1) {
551 xc_wait(5);
552 watchDogCount--;
553 }
554 }
555 return lockState;
556}
557
558#define XC_TUNE_ANALOG 0
559#define XC_TUNE_DIGITAL 1
560static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
561{
562 int found = 0;
563
564 dprintk(1, "%s(%u)\n", __func__, freq_hz);
565
566 if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
567 return 0;
568
569 if (mode == XC_TUNE_ANALOG) {
570 if (WaitForLock(priv) == 1)
571 found = 1;
572 }
573
574 return found;
575}
576
577static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
578{
579 u8 buf[2] = { reg >> 8, reg & 0xff };
580 u8 bval[2] = { 0, 0 };
581 struct i2c_msg msg[2] = {
582 { .addr = priv->i2c_props.addr,
583 .flags = 0, .buf = &buf[0], .len = 2 },
584 { .addr = priv->i2c_props.addr,
585 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
586 };
587
588 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
589 printk(KERN_WARNING "xc4000: I2C read failed\n");
590 return -EREMOTEIO;
591 }
592
593 *val = (bval[0] << 8) | bval[1];
594 return XC_RESULT_SUCCESS;
595}
596
Devin Heitmueller11091a32009-07-20 00:54:57 -0300597
598static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
599 v4l2_std_id *id)
600{
601 struct xc4000_priv *priv = fe->tuner_priv;
602 int i, best_i = -1, best_nr_matches = 0;
603 unsigned int type_mask = 0;
604
605 printk("%s called, want type=", __func__);
606 if (debug) {
607// dump_firm_type(type);
608 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
609 }
610
611 if (!priv->firm) {
612 printk("Error! firmware not loaded\n");
613 return -EINVAL;
614 }
615
616 if (((type & ~SCODE) == 0) && (*id == 0))
617 *id = V4L2_STD_PAL;
618
619 if (type & BASE)
620 type_mask = BASE_TYPES;
621 else if (type & SCODE) {
622 type &= SCODE_TYPES;
623 type_mask = SCODE_TYPES & ~HAS_IF;
624 } else if (type & DTV_TYPES)
625 type_mask = DTV_TYPES;
626 else if (type & STD_SPECIFIC_TYPES)
627 type_mask = STD_SPECIFIC_TYPES;
628
629 type &= type_mask;
630
631 if (!(type & SCODE))
632 type_mask = ~0;
633
634 /* Seek for exact match */
635 for (i = 0; i < priv->firm_size; i++) {
636 if ((type == (priv->firm[i].type & type_mask)) &&
637 (*id == priv->firm[i].id))
638 goto found;
639 }
640
641 /* Seek for generic video standard match */
642 for (i = 0; i < priv->firm_size; i++) {
643 v4l2_std_id match_mask;
644 int nr_matches;
645
646 if (type != (priv->firm[i].type & type_mask))
647 continue;
648
649 match_mask = *id & priv->firm[i].id;
650 if (!match_mask)
651 continue;
652
653 if ((*id & match_mask) == *id)
654 goto found; /* Supports all the requested standards */
655
656 nr_matches = hweight64(match_mask);
657 if (nr_matches > best_nr_matches) {
658 best_nr_matches = nr_matches;
659 best_i = i;
660 }
661 }
662
663 if (best_nr_matches > 0) {
664 printk("Selecting best matching firmware (%d bits) for "
665 "type=", best_nr_matches);
666// dump_firm_type(type);
667 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
668 i = best_i;
669 goto found;
670 }
671
672 /*FIXME: Would make sense to seek for type "hint" match ? */
673
674 i = -ENOENT;
675 goto ret;
676
677found:
678 *id = priv->firm[i].id;
679
680ret:
681 printk("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
682 if (debug) {
683// dump_firm_type(type);
684 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
685 }
686 return i;
687}
688
689static int load_firmware(struct dvb_frontend *fe, unsigned int type,
690 v4l2_std_id *id)
691{
692 struct xc4000_priv *priv = fe->tuner_priv;
693 int pos, rc;
694 unsigned char *p, *endp, buf[XC_MAX_I2C_WRITE_LENGTH];
695
696 printk("%s called\n", __func__);
697
698 pos = seek_firmware(fe, type, id);
699 if (pos < 0)
700 return pos;
701
702 printk("Loading firmware for type=");
703// dump_firm_type(priv->firm[pos].type);
704 printk("(%x), id %016llx.\n", priv->firm[pos].type,
705 (unsigned long long)*id);
706
707 p = priv->firm[pos].ptr;
708 endp = p + priv->firm[pos].size;
709
710 while (p < endp) {
711 __u16 size;
712
713 printk("block %02x %02x %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3], p[4], p[5]);
714
715 /* Checks if there's enough bytes to read */
716 if (p + sizeof(size) > endp) {
717 printk("Firmware chunk size is wrong\n");
718 return -EINVAL;
719 }
720
721 size = be16_to_cpu(*(__u16 *) p);
722 p += sizeof(size);
723
724 printk("djh size=%x\n", size);
725
726 if (size == 0xffff)
727 return 0;
728
729 if (!size) {
730 /* Special callback command received */
731 rc = xc4000_TunerReset(fe);
732 if (rc != XC_RESULT_SUCCESS) {
733 printk("Error at RESET code %d\n",
734 (*p) & 0x7f);
735 return -EINVAL;
736 }
737 continue;
738 }
739 if (size >= 0xff00) {
740 switch (size) {
741#ifdef DJH_XXX
742 case 0xff00:
743 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
744 if (rc < 0) {
745 printk("Error at RESET code %d\n",
746 (*p) & 0x7f);
747 return -EINVAL;
748 }
749 break;
750#endif
751 default:
752 printk("Invalid RESET code %d\n",
753 size & 0x7f);
754 return -EINVAL;
755
756 }
757 continue;
758 }
759
760 /* Checks for a sleep command */
761 if (size & 0x8000) {
762 printk("djh doing msleep for %x\n", (size & 0x7fff));
763 msleep(size & 0x7fff);
764 continue;
765 }
766
767 if ((size + p > endp)) {
768 printk("missing bytes: need %d, have %d\n",
769 size, (int)(endp - p));
770 return -EINVAL;
771 }
772
773 buf[0] = *p;
774 p++;
775 size--;
776
777 /* Sends message chunks */
778 printk("djh final size %d\n", size);
779 while (size > 0) {
780 int len = (size < XC_MAX_I2C_WRITE_LENGTH - 1) ?
781 size : XC_MAX_I2C_WRITE_LENGTH - 1;
782
783 memcpy(buf + 1, p, len);
784
785// rc = i2c_send(priv, buf, len + 1);
786 printk("djh sending %d\n", len + 1);
787 rc = xc_send_i2c_data(priv, buf, len + 1);
788 if (rc < 0) {
789 printk("%d returned from send\n", rc);
790 return -EINVAL;
791 }
792
793 p += len;
794 size -= len;
795 }
796 }
797 return 0;
798}
799
800//static int load_all_firmwares(struct dvb_frontend *fe)
Davide Ferri8d009a02009-06-23 22:34:06 -0300801static int xc4000_fwupload(struct dvb_frontend *fe)
802{
803 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300804 const struct firmware *fw = NULL;
805 const unsigned char *p, *endp;
806 int rc = 0;
807 int n, n_array;
808 char name[33];
809 char *fname;
Davide Ferri8d009a02009-06-23 22:34:06 -0300810
Devin Heitmueller11091a32009-07-20 00:54:57 -0300811 printk("%s called\n", __func__);
Davide Ferri8d009a02009-06-23 22:34:06 -0300812
Devin Heitmueller11091a32009-07-20 00:54:57 -0300813 fname = XC4000_DEFAULT_FIRMWARE;
814
815 printk("Reading firmware %s\n", fname);
816 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
817 if (rc < 0) {
818 if (rc == -ENOENT)
819 printk("Error: firmware %s not found.\n",
820 fname);
821 else
822 printk("Error %d while requesting firmware %s \n",
823 rc, fname);
824
825 return rc;
826 }
827 p = fw->data;
828 endp = p + fw->size;
829
830 if (fw->size < sizeof(name) - 1 + 2 + 2) {
831 printk("Error: firmware file %s has invalid size!\n",
832 fname);
833 goto corrupt;
Davide Ferri8d009a02009-06-23 22:34:06 -0300834 }
835
Devin Heitmueller11091a32009-07-20 00:54:57 -0300836 memcpy(name, p, sizeof(name) - 1);
837 name[sizeof(name) - 1] = 0;
838 p += sizeof(name) - 1;
839
840 priv->firm_version = get_unaligned_le16(p);
841 p += 2;
842
843 n_array = get_unaligned_le16(p);
844 p += 2;
845
846 printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
847 n_array, fname, name,
848 priv->firm_version >> 8, priv->firm_version & 0xff);
849
850 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
851 if (priv->firm == NULL) {
852 printk("Not enough memory to load firmware file.\n");
853 rc = -ENOMEM;
854 goto err;
855 }
856 priv->firm_size = n_array;
857
858 n = -1;
859 while (p < endp) {
860 __u32 type, size;
861 v4l2_std_id id;
862 __u16 int_freq = 0;
863
864 n++;
865 if (n >= n_array) {
866 printk("More firmware images in file than "
867 "were expected!\n");
868 goto corrupt;
869 }
870
871 /* Checks if there's enough bytes to read */
872 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
873 goto header;
874
875 type = get_unaligned_le32(p);
876 p += sizeof(type);
877
878 id = get_unaligned_le64(p);
879 p += sizeof(id);
880
881 if (type & HAS_IF) {
882 int_freq = get_unaligned_le16(p);
883 p += sizeof(int_freq);
884 if (endp - p < sizeof(size))
885 goto header;
886 }
887
888 size = get_unaligned_le32(p);
889 p += sizeof(size);
890
891 if (!size || size > endp - p) {
892 printk("Firmware type ");
893// dump_firm_type(type);
894 printk("(%x), id %llx is corrupted "
895 "(size=%d, expected %d)\n",
896 type, (unsigned long long)id,
897 (unsigned)(endp - p), size);
898 goto corrupt;
899 }
900
901 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
902 if (priv->firm[n].ptr == NULL) {
903 printk("Not enough memory to load firmware file.\n");
904 rc = -ENOMEM;
905 goto err;
906 }
907 printk("Reading firmware type ");
908 if (debug) {
909// dump_firm_type_and_int_freq(type, int_freq);
910 printk("(%x), id %llx, size=%d.\n",
911 type, (unsigned long long)id, size);
912 }
913
914 memcpy(priv->firm[n].ptr, p, size);
915 priv->firm[n].type = type;
916 priv->firm[n].id = id;
917 priv->firm[n].size = size;
918 priv->firm[n].int_freq = int_freq;
919
920 p += size;
Davide Ferri8d009a02009-06-23 22:34:06 -0300921 }
922
Devin Heitmueller11091a32009-07-20 00:54:57 -0300923 if (n + 1 != priv->firm_size) {
924 printk("Firmware file is incomplete!\n");
925 goto corrupt;
926 }
927
928 goto done;
929
930header:
931 printk("Firmware header is incomplete!\n");
932corrupt:
933 rc = -EINVAL;
934 printk("Error: firmware file is corrupted!\n");
935
936err:
937 printk("Releasing partially loaded firmware file.\n");
938// free_firmware(priv);
939
940done:
Davide Ferri8d009a02009-06-23 22:34:06 -0300941 release_firmware(fw);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300942 if (rc == 0)
943 printk("Firmware files loaded.\n");
944
945 return rc;
Davide Ferri8d009a02009-06-23 22:34:06 -0300946}
947
Devin Heitmueller11091a32009-07-20 00:54:57 -0300948
Davide Ferri8d009a02009-06-23 22:34:06 -0300949static void xc_debug_dump(struct xc4000_priv *priv)
950{
951 u16 adc_envelope;
952 u32 freq_error_hz = 0;
953 u16 lock_status;
954 u32 hsync_freq_hz = 0;
955 u16 frame_lines;
956 u16 quality;
957 u8 hw_majorversion = 0, hw_minorversion = 0;
958 u8 fw_majorversion = 0, fw_minorversion = 0;
959// u16 fw_buildversion = 0;
960
961 /* Wait for stats to stabilize.
962 * Frame Lines needs two frame times after initial lock
963 * before it is valid.
964 */
965 xc_wait(100);
966
967 xc_get_ADC_Envelope(priv, &adc_envelope);
968 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
969
970 xc_get_frequency_error(priv, &freq_error_hz);
971 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
972
973 xc_get_lock_status(priv, &lock_status);
974 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
975 lock_status);
976
977 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
978 &fw_majorversion, &fw_minorversion);
979// WAS:
980// xc_get_buildversion(priv, &fw_buildversion);
981// dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
982// hw_majorversion, hw_minorversion,
983// fw_majorversion, fw_minorversion, fw_buildversion);
984// NOW:
985 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
986 hw_majorversion, hw_minorversion,
987 fw_majorversion, fw_minorversion);
988
989 xc_get_hsync_freq(priv, &hsync_freq_hz);
990 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
991
992 xc_get_frame_lines(priv, &frame_lines);
993 dprintk(1, "*** Frame lines = %d\n", frame_lines);
994
995 xc_get_quality(priv, &quality);
996 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
997}
998
999static int xc4000_set_params(struct dvb_frontend *fe,
1000 struct dvb_frontend_parameters *params)
1001{
1002 struct xc4000_priv *priv = fe->tuner_priv;
1003 int ret;
1004
1005 if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
1006 xc_load_fw_and_init_tuner(fe);
1007
1008 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1009
1010 if (fe->ops.info.type == FE_ATSC) {
1011 dprintk(1, "%s() ATSC\n", __func__);
1012 switch (params->u.vsb.modulation) {
1013 case VSB_8:
1014 case VSB_16:
1015 dprintk(1, "%s() VSB modulation\n", __func__);
1016 priv->rf_mode = XC_RF_MODE_AIR;
1017 priv->freq_hz = params->frequency - 1750000;
1018 priv->bandwidth = BANDWIDTH_6_MHZ;
1019 priv->video_standard = DTV6;
1020 break;
1021 case QAM_64:
1022 case QAM_256:
1023 case QAM_AUTO:
1024 dprintk(1, "%s() QAM modulation\n", __func__);
1025 priv->rf_mode = XC_RF_MODE_CABLE;
1026 priv->freq_hz = params->frequency - 1750000;
1027 priv->bandwidth = BANDWIDTH_6_MHZ;
1028 priv->video_standard = DTV6;
1029 break;
1030 default:
1031 return -EINVAL;
1032 }
1033 } else if (fe->ops.info.type == FE_OFDM) {
1034 dprintk(1, "%s() OFDM\n", __func__);
1035 switch (params->u.ofdm.bandwidth) {
1036 case BANDWIDTH_6_MHZ:
1037 priv->bandwidth = BANDWIDTH_6_MHZ;
1038 priv->video_standard = DTV6;
1039 priv->freq_hz = params->frequency - 1750000;
1040 break;
1041 case BANDWIDTH_7_MHZ:
1042 printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
1043 return -EINVAL;
1044 case BANDWIDTH_8_MHZ:
1045 priv->bandwidth = BANDWIDTH_8_MHZ;
1046 priv->video_standard = DTV8;
1047 priv->freq_hz = params->frequency - 2750000;
1048 break;
1049 default:
1050 printk(KERN_ERR "xc4000 bandwidth not set!\n");
1051 return -EINVAL;
1052 }
1053 priv->rf_mode = XC_RF_MODE_AIR;
1054 } else {
1055 printk(KERN_ERR "xc4000 modulation type not supported!\n");
1056 return -EINVAL;
1057 }
1058
1059 dprintk(1, "%s() frequency=%d (compensated)\n",
1060 __func__, priv->freq_hz);
1061
1062 ret = xc_SetSignalSource(priv, priv->rf_mode);
1063 if (ret != XC_RESULT_SUCCESS) {
1064 printk(KERN_ERR
1065 "xc4000: xc_SetSignalSource(%d) failed\n",
1066 priv->rf_mode);
1067 return -EREMOTEIO;
1068 }
1069
1070 ret = xc_SetTVStandard(priv,
1071 XC4000_Standard[priv->video_standard].VideoMode,
1072 XC4000_Standard[priv->video_standard].AudioMode);
1073 if (ret != XC_RESULT_SUCCESS) {
1074 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1075 return -EREMOTEIO;
1076 }
1077
1078 ret = xc_set_IF_frequency(priv, priv->if_khz);
1079 if (ret != XC_RESULT_SUCCESS) {
1080 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
1081 priv->if_khz);
1082 return -EIO;
1083 }
1084
1085 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1086
1087 if (debug)
1088 xc_debug_dump(priv);
1089
1090 return 0;
1091}
1092
1093static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1094{
1095 struct xc4000_priv *priv = fe->tuner_priv;
1096 int ret;
1097 u16 id;
1098
1099 ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1100 if (ret == XC_RESULT_SUCCESS) {
1101 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1102 ret = XC_RESULT_RESET_FAILURE;
1103 else
1104 ret = XC_RESULT_SUCCESS;
1105 }
1106
1107 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1108 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1109 return ret;
1110}
1111
1112static int xc4000_set_analog_params(struct dvb_frontend *fe,
1113 struct analog_parameters *params)
1114{
1115 struct xc4000_priv *priv = fe->tuner_priv;
1116 int ret;
1117
1118 if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
1119 xc_load_fw_and_init_tuner(fe);
1120
1121 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1122 __func__, params->frequency);
1123
1124 /* Fix me: it could be air. */
1125 priv->rf_mode = params->mode;
1126 if (params->mode > XC_RF_MODE_CABLE)
1127 priv->rf_mode = XC_RF_MODE_CABLE;
1128
1129 /* params->frequency is in units of 62.5khz */
1130 priv->freq_hz = params->frequency * 62500;
1131
1132 /* FIX ME: Some video standards may have several possible audio
1133 standards. We simply default to one of them here.
1134 */
1135 if (params->std & V4L2_STD_MN) {
1136 /* default to BTSC audio standard */
1137 priv->video_standard = MN_NTSC_PAL_BTSC;
1138 goto tune_channel;
1139 }
1140
1141 if (params->std & V4L2_STD_PAL_BG) {
1142 /* default to NICAM audio standard */
1143 priv->video_standard = BG_PAL_NICAM;
1144 goto tune_channel;
1145 }
1146
1147 if (params->std & V4L2_STD_PAL_I) {
1148 /* default to NICAM audio standard */
1149 priv->video_standard = I_PAL_NICAM;
1150 goto tune_channel;
1151 }
1152
1153 if (params->std & V4L2_STD_PAL_DK) {
1154 /* default to NICAM audio standard */
1155 priv->video_standard = DK_PAL_NICAM;
1156 goto tune_channel;
1157 }
1158
1159 if (params->std & V4L2_STD_SECAM_DK) {
1160 /* default to A2 DK1 audio standard */
1161 priv->video_standard = DK_SECAM_A2DK1;
1162 goto tune_channel;
1163 }
1164
1165 if (params->std & V4L2_STD_SECAM_L) {
1166 priv->video_standard = L_SECAM_NICAM;
1167 goto tune_channel;
1168 }
1169
1170 if (params->std & V4L2_STD_SECAM_LC) {
1171 priv->video_standard = LC_SECAM_NICAM;
1172 goto tune_channel;
1173 }
1174
1175tune_channel:
1176 ret = xc_SetSignalSource(priv, priv->rf_mode);
1177 if (ret != XC_RESULT_SUCCESS) {
1178 printk(KERN_ERR
1179 "xc4000: xc_SetSignalSource(%d) failed\n",
1180 priv->rf_mode);
1181 return -EREMOTEIO;
1182 }
1183
1184 ret = xc_SetTVStandard(priv,
1185 XC4000_Standard[priv->video_standard].VideoMode,
1186 XC4000_Standard[priv->video_standard].AudioMode);
1187 if (ret != XC_RESULT_SUCCESS) {
1188 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1189 return -EREMOTEIO;
1190 }
1191
1192 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1193
1194 if (debug)
1195 xc_debug_dump(priv);
1196
1197 return 0;
1198}
1199
1200static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1201{
1202 struct xc4000_priv *priv = fe->tuner_priv;
1203 dprintk(1, "%s()\n", __func__);
1204 *freq = priv->freq_hz;
1205 return 0;
1206}
1207
1208static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1209{
1210 struct xc4000_priv *priv = fe->tuner_priv;
1211 dprintk(1, "%s()\n", __func__);
1212
1213 *bw = priv->bandwidth;
1214 return 0;
1215}
1216
1217static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1218{
1219 struct xc4000_priv *priv = fe->tuner_priv;
1220 u16 lock_status = 0;
1221
1222 xc_get_lock_status(priv, &lock_status);
1223
1224 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1225
1226 *status = lock_status;
1227
1228 return 0;
1229}
1230
1231static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe)
1232{
1233 struct xc4000_priv *priv = fe->tuner_priv;
1234 int ret = 0;
1235
1236 if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS) {
1237 ret = xc4000_fwupload(fe);
1238 if (ret != XC_RESULT_SUCCESS)
1239 return ret;
1240 }
1241
1242 /* Start the tuner self-calibration process */
1243 ret |= xc_initialize(priv);
1244
1245 /* Wait for calibration to complete.
1246 * We could continue but XC4000 will clock stretch subsequent
1247 * I2C transactions until calibration is complete. This way we
1248 * don't have to rely on clock stretching working.
1249 */
1250 xc_wait(100);
1251
1252 /* Default to "CABLE" mode */
1253 ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
1254
1255 return ret;
1256}
1257
1258static int xc4000_sleep(struct dvb_frontend *fe)
1259{
1260 int ret;
1261
1262 dprintk(1, "%s()\n", __func__);
1263
1264 /* Avoid firmware reload on slow devices */
1265 if (no_poweroff)
1266 return 0;
1267
1268 /* According to Xceive technical support, the "powerdown" register
1269 was removed in newer versions of the firmware. The "supported"
1270 way to sleep the tuner is to pull the reset pin low for 10ms */
1271 ret = xc4000_TunerReset(fe);
1272 if (ret != XC_RESULT_SUCCESS) {
1273 printk(KERN_ERR
1274 "xc4000: %s() unable to shutdown tuner\n",
1275 __func__);
1276 return -EREMOTEIO;
1277 } else
1278 return XC_RESULT_SUCCESS;
1279}
1280
1281static int xc4000_init(struct dvb_frontend *fe)
1282{
1283 struct xc4000_priv *priv = fe->tuner_priv;
1284 dprintk(1, "%s()\n", __func__);
1285
1286 if (xc_load_fw_and_init_tuner(fe) != XC_RESULT_SUCCESS) {
1287 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1288 return -EREMOTEIO;
1289 }
1290
1291 if (debug)
1292 xc_debug_dump(priv);
1293
1294 return 0;
1295}
1296
1297static int xc4000_release(struct dvb_frontend *fe)
1298{
1299 struct xc4000_priv *priv = fe->tuner_priv;
1300
1301 dprintk(1, "%s()\n", __func__);
1302
1303 mutex_lock(&xc4000_list_mutex);
1304
1305 if (priv)
1306 hybrid_tuner_release_state(priv);
1307
1308 mutex_unlock(&xc4000_list_mutex);
1309
1310 fe->tuner_priv = NULL;
1311
1312 return 0;
1313}
1314
1315static const struct dvb_tuner_ops xc4000_tuner_ops = {
1316 .info = {
1317 .name = "Xceive XC4000",
1318 .frequency_min = 1000000,
1319 .frequency_max = 1023000000,
1320 .frequency_step = 50000,
1321 },
1322
1323 .release = xc4000_release,
1324 .init = xc4000_init,
1325 .sleep = xc4000_sleep,
1326
1327 .set_params = xc4000_set_params,
1328 .set_analog_params = xc4000_set_analog_params,
1329 .get_frequency = xc4000_get_frequency,
1330 .get_bandwidth = xc4000_get_bandwidth,
1331 .get_status = xc4000_get_status
1332};
1333
1334struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1335 struct i2c_adapter *i2c,
1336 struct xc4000_config *cfg)
1337{
1338 struct xc4000_priv *priv = NULL;
1339 int instance;
Devin Heitmueller11091a32009-07-20 00:54:57 -03001340 v4l2_std_id std0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001341 u16 id = 0;
Devin Heitmueller11091a32009-07-20 00:54:57 -03001342 int rc;
Davide Ferri8d009a02009-06-23 22:34:06 -03001343
1344 dprintk(1, "%s(%d-%04x)\n", __func__,
1345 i2c ? i2c_adapter_id(i2c) : -1,
1346 cfg ? cfg->i2c_address : -1);
1347
1348 mutex_lock(&xc4000_list_mutex);
1349
1350 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1351 hybrid_tuner_instance_list,
1352 i2c, cfg->i2c_address, "xc4000");
1353 switch (instance) {
1354 case 0:
1355 goto fail;
1356 break;
1357 case 1:
1358 /* new tuner instance */
1359 priv->bandwidth = BANDWIDTH_6_MHZ;
1360 fe->tuner_priv = priv;
1361 break;
1362 default:
1363 /* existing tuner instance */
1364 fe->tuner_priv = priv;
1365 break;
1366 }
1367
1368 if (priv->if_khz == 0) {
1369 /* If the IF hasn't been set yet, use the value provided by
1370 the caller (occurs in hybrid devices where the analog
1371 call to xc4000_attach occurs before the digital side) */
1372 priv->if_khz = cfg->if_khz;
1373 }
1374
1375 /* Check if firmware has been loaded. It is possible that another
1376 instance of the driver has loaded the firmware.
1377 */
1378
1379 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1380 goto fail;
1381
1382 switch (id) {
1383 case XC_PRODUCT_ID_FW_LOADED:
1384 printk(KERN_INFO
1385 "xc4000: Successfully identified at address 0x%02x\n",
1386 cfg->i2c_address);
1387 printk(KERN_INFO
1388 "xc4000: Firmware has been loaded previously\n");
1389 break;
1390 case XC_PRODUCT_ID_FW_NOT_LOADED:
1391 printk(KERN_INFO
1392 "xc4000: Successfully identified at address 0x%02x\n",
1393 cfg->i2c_address);
1394 printk(KERN_INFO
1395 "xc4000: Firmware has not been loaded previously\n");
1396 break;
1397 default:
1398 printk(KERN_ERR
1399 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1400 cfg->i2c_address, id);
1401 goto fail;
1402 }
1403
1404 mutex_unlock(&xc4000_list_mutex);
1405
1406 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1407 sizeof(struct dvb_tuner_ops));
1408
Devin Heitmueller11091a32009-07-20 00:54:57 -03001409 /* FIXME: For now, load the firmware at startup. We will remove this
1410 before the code goes to production... */
1411 xc4000_fwupload(fe);
1412 printk("xc4000_fwupload done\n");
1413
1414 std0 = 0;
1415// rc = load_firmware(fe, BASE | new_fw.type, &std0);
1416 rc = load_firmware(fe, BASE, &std0);
1417 if (rc < 0) {
1418 tuner_err("Error %d while loading base firmware\n",
1419 rc);
1420 goto fail;
1421 }
1422
1423 /* Load INIT1, if needed */
1424 tuner_dbg("Load init1 firmware, if exists\n");
1425
1426// rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1427 rc = load_firmware(fe, INIT1, &std0);
1428 printk("init1 load result %x\n", rc);
1429
1430 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1431 goto fail;
1432 printk("djh id is now %x\n", id);
1433
Davide Ferri8d009a02009-06-23 22:34:06 -03001434 return fe;
1435fail:
1436 mutex_unlock(&xc4000_list_mutex);
1437
1438 xc4000_release(fe);
1439 return NULL;
1440}
1441EXPORT_SYMBOL(xc4000_attach);
1442
1443MODULE_AUTHOR("Steven Toth, Davide Ferri");
1444MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1445MODULE_LICENSE("GPL");