[media] xc4000: cut over to using xc5000 version for loading i2c sequences
[pandora-kernel.git] / drivers / media / common / tuners / xc4000.c
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>
31 #include <asm/unaligned.h>
32
33 #include "dvb_frontend.h"
34
35 #include "xc4000.h"
36 #include "tuner-i2c.h"
37 #include "tuner-xc2028-types.h"
38
39 static int debug;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
42
43 static int no_poweroff;
44 module_param(no_poweroff, int, 0644);
45 MODULE_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
49 static DEFINE_MUTEX(xc4000_list_mutex);
50 static 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
55 #define XC4000_DEFAULT_FIRMWARE "xc4000-01.fw"
56 #define XC4000_DEFAULT_FIRMWARE_SIZE 8434
57
58
59 /* struct for storing firmware table */
60 struct 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
68 struct 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 };
76
77 struct xc4000_priv {
78         struct tuner_i2c_props i2c_props;
79         struct list_head hybrid_tuner_instance_list;
80         struct firmware_description *firm;
81         int                     firm_size;
82         __u16                   firm_version;
83         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 */
169 struct 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
193 #define FM_Radio_INPUT2         21
194 #define FM_Radio_INPUT1         22
195
196 /* WAS :
197 static 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
223 static 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
249 static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe);
250 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
251 static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
252 static int xc4000_TunerReset(struct dvb_frontend *fe);
253
254 static 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 */
269 static 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
281 static void xc_wait(int wait_ms)
282 {
283         msleep(wait_ms);
284 }
285
286 static 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
310 static 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
349 static 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
403 static 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
409 static 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
425 static 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
439 static const struct dvb_tuner_ops xc4000_tuner_ops;
440
441 static 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
460 static 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
470 static 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
475 static 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
490 static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
491 {
492         return xc4000_readreg(priv, XREG_LOCK, lock_status);
493 }
494
495 static 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
515 static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
516 {
517         return xc4000_readreg(priv, XREG_BUILD, buildrev);
518 }*/
519
520 static 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
533 static 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
538 static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
539 {
540         return xc4000_readreg(priv, XREG_QUALITY, quality);
541 }
542
543 static 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
560 static 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
577 static 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
597
598 static 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
677 found:
678         *id = priv->firm[i].id;
679
680 ret:
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
689 static 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;
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
709         rc = xc_load_i2c_sequence(fe, p);
710         printk("load i2c sequence result=%d\n", rc);
711
712         return rc;
713 }
714
715 //static int load_all_firmwares(struct dvb_frontend *fe)
716 static int xc4000_fwupload(struct dvb_frontend *fe)
717 {
718         struct xc4000_priv *priv = fe->tuner_priv;
719         const struct firmware *fw   = NULL;
720         const unsigned char   *p, *endp;
721         int                   rc = 0;
722         int                   n, n_array;
723         char                  name[33];
724         char                  *fname;
725
726         printk("%s called\n", __func__);
727
728         fname = XC4000_DEFAULT_FIRMWARE;
729
730         printk("Reading firmware %s\n",  fname);
731         rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
732         if (rc < 0) {
733                 if (rc == -ENOENT)
734                         printk("Error: firmware %s not found.\n",
735                                    fname);
736                 else
737                         printk("Error %d while requesting firmware %s \n",
738                                    rc, fname);
739
740                 return rc;
741         }
742         p = fw->data;
743         endp = p + fw->size;
744
745         if (fw->size < sizeof(name) - 1 + 2 + 2) {
746                 printk("Error: firmware file %s has invalid size!\n",
747                           fname);
748                 goto corrupt;
749         }
750
751         memcpy(name, p, sizeof(name) - 1);
752         name[sizeof(name) - 1] = 0;
753         p += sizeof(name) - 1;
754
755         priv->firm_version = get_unaligned_le16(p);
756         p += 2;
757
758         n_array = get_unaligned_le16(p);
759         p += 2;
760
761         printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
762                    n_array, fname, name,
763                    priv->firm_version >> 8, priv->firm_version & 0xff);
764
765         priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
766         if (priv->firm == NULL) {
767                 printk("Not enough memory to load firmware file.\n");
768                 rc = -ENOMEM;
769                 goto err;
770         }
771         priv->firm_size = n_array;
772
773         n = -1;
774         while (p < endp) {
775                 __u32 type, size;
776                 v4l2_std_id id;
777                 __u16 int_freq = 0;
778
779                 n++;
780                 if (n >= n_array) {
781                         printk("More firmware images in file than "
782                                   "were expected!\n");
783                         goto corrupt;
784                 }
785
786                 /* Checks if there's enough bytes to read */
787                 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
788                         goto header;
789
790                 type = get_unaligned_le32(p);
791                 p += sizeof(type);
792
793                 id = get_unaligned_le64(p);
794                 p += sizeof(id);
795
796                 if (type & HAS_IF) {
797                         int_freq = get_unaligned_le16(p);
798                         p += sizeof(int_freq);
799                         if (endp - p < sizeof(size))
800                                 goto header;
801                 }
802
803                 size = get_unaligned_le32(p);
804                 p += sizeof(size);
805
806                 if (!size || size > endp - p) {
807                         printk("Firmware type ");
808 //                      dump_firm_type(type);
809                         printk("(%x), id %llx is corrupted "
810                                "(size=%d, expected %d)\n",
811                                type, (unsigned long long)id,
812                                (unsigned)(endp - p), size);
813                         goto corrupt;
814                 }
815
816                 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
817                 if (priv->firm[n].ptr == NULL) {
818                         printk("Not enough memory to load firmware file.\n");
819                         rc = -ENOMEM;
820                         goto err;
821                 }
822                 printk("Reading firmware type ");
823                 if (debug) {
824 //                      dump_firm_type_and_int_freq(type, int_freq);
825                         printk("(%x), id %llx, size=%d.\n",
826                                type, (unsigned long long)id, size);
827                 }
828
829                 memcpy(priv->firm[n].ptr, p, size);
830                 priv->firm[n].type = type;
831                 priv->firm[n].id   = id;
832                 priv->firm[n].size = size;
833                 priv->firm[n].int_freq = int_freq;
834
835                 p += size;
836         }
837
838         if (n + 1 != priv->firm_size) {
839                 printk("Firmware file is incomplete!\n");
840                 goto corrupt;
841         }
842
843         goto done;
844
845 header:
846         printk("Firmware header is incomplete!\n");
847 corrupt:
848         rc = -EINVAL;
849         printk("Error: firmware file is corrupted!\n");
850
851 err:
852         printk("Releasing partially loaded firmware file.\n");
853 //      free_firmware(priv);
854
855 done:
856         release_firmware(fw);
857         if (rc == 0)
858                 printk("Firmware files loaded.\n");
859
860         return rc;
861 }
862
863
864 static void xc_debug_dump(struct xc4000_priv *priv)
865 {
866         u16 adc_envelope;
867         u32 freq_error_hz = 0;
868         u16 lock_status;
869         u32 hsync_freq_hz = 0;
870         u16 frame_lines;
871         u16 quality;
872         u8 hw_majorversion = 0, hw_minorversion = 0;
873         u8 fw_majorversion = 0, fw_minorversion = 0;
874 //      u16 fw_buildversion = 0;
875
876         /* Wait for stats to stabilize.
877          * Frame Lines needs two frame times after initial lock
878          * before it is valid.
879          */
880         xc_wait(100);
881
882         xc_get_ADC_Envelope(priv,  &adc_envelope);
883         dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
884
885         xc_get_frequency_error(priv, &freq_error_hz);
886         dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
887
888         xc_get_lock_status(priv,  &lock_status);
889         dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
890                 lock_status);
891
892         xc_get_version(priv,  &hw_majorversion, &hw_minorversion,
893                 &fw_majorversion, &fw_minorversion);
894 // WAS:
895 //      xc_get_buildversion(priv,  &fw_buildversion);
896 //      dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
897 //              hw_majorversion, hw_minorversion,
898 //              fw_majorversion, fw_minorversion, fw_buildversion);
899 // NOW:
900         dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
901                 hw_majorversion, hw_minorversion,
902                 fw_majorversion, fw_minorversion);
903
904         xc_get_hsync_freq(priv,  &hsync_freq_hz);
905         dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
906
907         xc_get_frame_lines(priv,  &frame_lines);
908         dprintk(1, "*** Frame lines = %d\n", frame_lines);
909
910         xc_get_quality(priv,  &quality);
911         dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
912 }
913
914 static int xc4000_set_params(struct dvb_frontend *fe,
915         struct dvb_frontend_parameters *params)
916 {
917         struct xc4000_priv *priv = fe->tuner_priv;
918         int ret;
919
920         if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
921                 xc_load_fw_and_init_tuner(fe);
922
923         dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
924
925         if (fe->ops.info.type == FE_ATSC) {
926                 dprintk(1, "%s() ATSC\n", __func__);
927                 switch (params->u.vsb.modulation) {
928                 case VSB_8:
929                 case VSB_16:
930                         dprintk(1, "%s() VSB modulation\n", __func__);
931                         priv->rf_mode = XC_RF_MODE_AIR;
932                         priv->freq_hz = params->frequency - 1750000;
933                         priv->bandwidth = BANDWIDTH_6_MHZ;
934                         priv->video_standard = DTV6;
935                         break;
936                 case QAM_64:
937                 case QAM_256:
938                 case QAM_AUTO:
939                         dprintk(1, "%s() QAM modulation\n", __func__);
940                         priv->rf_mode = XC_RF_MODE_CABLE;
941                         priv->freq_hz = params->frequency - 1750000;
942                         priv->bandwidth = BANDWIDTH_6_MHZ;
943                         priv->video_standard = DTV6;
944                         break;
945                 default:
946                         return -EINVAL;
947                 }
948         } else if (fe->ops.info.type == FE_OFDM) {
949                 dprintk(1, "%s() OFDM\n", __func__);
950                 switch (params->u.ofdm.bandwidth) {
951                 case BANDWIDTH_6_MHZ:
952                         priv->bandwidth = BANDWIDTH_6_MHZ;
953                         priv->video_standard = DTV6;
954                         priv->freq_hz = params->frequency - 1750000;
955                         break;
956                 case BANDWIDTH_7_MHZ:
957                         printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
958                         return -EINVAL;
959                 case BANDWIDTH_8_MHZ:
960                         priv->bandwidth = BANDWIDTH_8_MHZ;
961                         priv->video_standard = DTV8;
962                         priv->freq_hz = params->frequency - 2750000;
963                         break;
964                 default:
965                         printk(KERN_ERR "xc4000 bandwidth not set!\n");
966                         return -EINVAL;
967                 }
968                 priv->rf_mode = XC_RF_MODE_AIR;
969         } else {
970                 printk(KERN_ERR "xc4000 modulation type not supported!\n");
971                 return -EINVAL;
972         }
973
974         dprintk(1, "%s() frequency=%d (compensated)\n",
975                 __func__, priv->freq_hz);
976
977         ret = xc_SetSignalSource(priv, priv->rf_mode);
978         if (ret != XC_RESULT_SUCCESS) {
979                 printk(KERN_ERR
980                         "xc4000: xc_SetSignalSource(%d) failed\n",
981                         priv->rf_mode);
982                 return -EREMOTEIO;
983         }
984
985         ret = xc_SetTVStandard(priv,
986                 XC4000_Standard[priv->video_standard].VideoMode,
987                 XC4000_Standard[priv->video_standard].AudioMode);
988         if (ret != XC_RESULT_SUCCESS) {
989                 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
990                 return -EREMOTEIO;
991         }
992
993         ret = xc_set_IF_frequency(priv, priv->if_khz);
994         if (ret != XC_RESULT_SUCCESS) {
995                 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
996                        priv->if_khz);
997                 return -EIO;
998         }
999
1000         xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1001
1002         if (debug)
1003                 xc_debug_dump(priv);
1004
1005         return 0;
1006 }
1007
1008 static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1009 {
1010         struct xc4000_priv *priv = fe->tuner_priv;
1011         int ret;
1012         u16 id;
1013
1014         ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1015         if (ret == XC_RESULT_SUCCESS) {
1016                 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1017                         ret = XC_RESULT_RESET_FAILURE;
1018                 else
1019                         ret = XC_RESULT_SUCCESS;
1020         }
1021
1022         dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1023                 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1024         return ret;
1025 }
1026
1027 static int xc4000_set_analog_params(struct dvb_frontend *fe,
1028         struct analog_parameters *params)
1029 {
1030         struct xc4000_priv *priv = fe->tuner_priv;
1031         int ret;
1032
1033         if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS)
1034                 xc_load_fw_and_init_tuner(fe);
1035
1036         dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1037                 __func__, params->frequency);
1038
1039         /* Fix me: it could be air. */
1040         priv->rf_mode = params->mode;
1041         if (params->mode > XC_RF_MODE_CABLE)
1042                 priv->rf_mode = XC_RF_MODE_CABLE;
1043
1044         /* params->frequency is in units of 62.5khz */
1045         priv->freq_hz = params->frequency * 62500;
1046
1047         /* FIX ME: Some video standards may have several possible audio
1048                    standards. We simply default to one of them here.
1049          */
1050         if (params->std & V4L2_STD_MN) {
1051                 /* default to BTSC audio standard */
1052                 priv->video_standard = MN_NTSC_PAL_BTSC;
1053                 goto tune_channel;
1054         }
1055
1056         if (params->std & V4L2_STD_PAL_BG) {
1057                 /* default to NICAM audio standard */
1058                 priv->video_standard = BG_PAL_NICAM;
1059                 goto tune_channel;
1060         }
1061
1062         if (params->std & V4L2_STD_PAL_I) {
1063                 /* default to NICAM audio standard */
1064                 priv->video_standard = I_PAL_NICAM;
1065                 goto tune_channel;
1066         }
1067
1068         if (params->std & V4L2_STD_PAL_DK) {
1069                 /* default to NICAM audio standard */
1070                 priv->video_standard = DK_PAL_NICAM;
1071                 goto tune_channel;
1072         }
1073
1074         if (params->std & V4L2_STD_SECAM_DK) {
1075                 /* default to A2 DK1 audio standard */
1076                 priv->video_standard = DK_SECAM_A2DK1;
1077                 goto tune_channel;
1078         }
1079
1080         if (params->std & V4L2_STD_SECAM_L) {
1081                 priv->video_standard = L_SECAM_NICAM;
1082                 goto tune_channel;
1083         }
1084
1085         if (params->std & V4L2_STD_SECAM_LC) {
1086                 priv->video_standard = LC_SECAM_NICAM;
1087                 goto tune_channel;
1088         }
1089
1090 tune_channel:
1091         ret = xc_SetSignalSource(priv, priv->rf_mode);
1092         if (ret != XC_RESULT_SUCCESS) {
1093                 printk(KERN_ERR
1094                         "xc4000: xc_SetSignalSource(%d) failed\n",
1095                         priv->rf_mode);
1096                 return -EREMOTEIO;
1097         }
1098
1099         ret = xc_SetTVStandard(priv,
1100                 XC4000_Standard[priv->video_standard].VideoMode,
1101                 XC4000_Standard[priv->video_standard].AudioMode);
1102         if (ret != XC_RESULT_SUCCESS) {
1103                 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1104                 return -EREMOTEIO;
1105         }
1106
1107         xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1108
1109         if (debug)
1110                 xc_debug_dump(priv);
1111
1112         return 0;
1113 }
1114
1115 static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1116 {
1117         struct xc4000_priv *priv = fe->tuner_priv;
1118         dprintk(1, "%s()\n", __func__);
1119         *freq = priv->freq_hz;
1120         return 0;
1121 }
1122
1123 static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1124 {
1125         struct xc4000_priv *priv = fe->tuner_priv;
1126         dprintk(1, "%s()\n", __func__);
1127
1128         *bw = priv->bandwidth;
1129         return 0;
1130 }
1131
1132 static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1133 {
1134         struct xc4000_priv *priv = fe->tuner_priv;
1135         u16 lock_status = 0;
1136
1137         xc_get_lock_status(priv, &lock_status);
1138
1139         dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1140
1141         *status = lock_status;
1142
1143         return 0;
1144 }
1145
1146 static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe)
1147 {
1148         struct xc4000_priv *priv = fe->tuner_priv;
1149         int ret = 0;
1150
1151         if (xc4000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS) {
1152                 ret = xc4000_fwupload(fe);
1153                 if (ret != XC_RESULT_SUCCESS)
1154                         return ret;
1155         }
1156
1157         /* Start the tuner self-calibration process */
1158         ret |= xc_initialize(priv);
1159
1160         /* Wait for calibration to complete.
1161          * We could continue but XC4000 will clock stretch subsequent
1162          * I2C transactions until calibration is complete.  This way we
1163          * don't have to rely on clock stretching working.
1164          */
1165         xc_wait(100);
1166
1167         /* Default to "CABLE" mode */
1168         ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
1169
1170         return ret;
1171 }
1172
1173 static int xc4000_sleep(struct dvb_frontend *fe)
1174 {
1175         int ret;
1176
1177         dprintk(1, "%s()\n", __func__);
1178
1179         /* Avoid firmware reload on slow devices */
1180         if (no_poweroff)
1181                 return 0;
1182
1183         /* According to Xceive technical support, the "powerdown" register
1184            was removed in newer versions of the firmware.  The "supported"
1185            way to sleep the tuner is to pull the reset pin low for 10ms */
1186         ret = xc4000_TunerReset(fe);
1187         if (ret != XC_RESULT_SUCCESS) {
1188                 printk(KERN_ERR
1189                         "xc4000: %s() unable to shutdown tuner\n",
1190                         __func__);
1191                 return -EREMOTEIO;
1192         } else
1193                 return XC_RESULT_SUCCESS;
1194 }
1195
1196 static int xc4000_init(struct dvb_frontend *fe)
1197 {
1198         struct xc4000_priv *priv = fe->tuner_priv;
1199         dprintk(1, "%s()\n", __func__);
1200
1201         if (xc_load_fw_and_init_tuner(fe) != XC_RESULT_SUCCESS) {
1202                 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1203                 return -EREMOTEIO;
1204         }
1205
1206         if (debug)
1207                 xc_debug_dump(priv);
1208
1209         return 0;
1210 }
1211
1212 static int xc4000_release(struct dvb_frontend *fe)
1213 {
1214         struct xc4000_priv *priv = fe->tuner_priv;
1215
1216         dprintk(1, "%s()\n", __func__);
1217
1218         mutex_lock(&xc4000_list_mutex);
1219
1220         if (priv)
1221                 hybrid_tuner_release_state(priv);
1222
1223         mutex_unlock(&xc4000_list_mutex);
1224
1225         fe->tuner_priv = NULL;
1226
1227         return 0;
1228 }
1229
1230 static const struct dvb_tuner_ops xc4000_tuner_ops = {
1231         .info = {
1232                 .name           = "Xceive XC4000",
1233                 .frequency_min  =    1000000,
1234                 .frequency_max  = 1023000000,
1235                 .frequency_step =      50000,
1236         },
1237
1238         .release           = xc4000_release,
1239         .init              = xc4000_init,
1240         .sleep             = xc4000_sleep,
1241
1242         .set_params        = xc4000_set_params,
1243         .set_analog_params = xc4000_set_analog_params,
1244         .get_frequency     = xc4000_get_frequency,
1245         .get_bandwidth     = xc4000_get_bandwidth,
1246         .get_status        = xc4000_get_status
1247 };
1248
1249 struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1250                                    struct i2c_adapter *i2c,
1251                                    struct xc4000_config *cfg)
1252 {
1253         struct xc4000_priv *priv = NULL;
1254         int instance;
1255         v4l2_std_id std0;
1256         u16 id = 0;
1257         int rc;
1258
1259         dprintk(1, "%s(%d-%04x)\n", __func__,
1260                 i2c ? i2c_adapter_id(i2c) : -1,
1261                 cfg ? cfg->i2c_address : -1);
1262
1263         mutex_lock(&xc4000_list_mutex);
1264
1265         instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1266                                               hybrid_tuner_instance_list,
1267                                               i2c, cfg->i2c_address, "xc4000");
1268         switch (instance) {
1269         case 0:
1270                 goto fail;
1271                 break;
1272         case 1:
1273                 /* new tuner instance */
1274                 priv->bandwidth = BANDWIDTH_6_MHZ;
1275                 fe->tuner_priv = priv;
1276                 break;
1277         default:
1278                 /* existing tuner instance */
1279                 fe->tuner_priv = priv;
1280                 break;
1281         }
1282
1283         if (priv->if_khz == 0) {
1284                 /* If the IF hasn't been set yet, use the value provided by
1285                    the caller (occurs in hybrid devices where the analog
1286                    call to xc4000_attach occurs before the digital side) */
1287                 priv->if_khz = cfg->if_khz;
1288         }
1289
1290         /* Check if firmware has been loaded. It is possible that another
1291            instance of the driver has loaded the firmware.
1292          */
1293
1294         if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1295                         goto fail;
1296
1297         switch (id) {
1298         case XC_PRODUCT_ID_FW_LOADED:
1299                 printk(KERN_INFO
1300                         "xc4000: Successfully identified at address 0x%02x\n",
1301                         cfg->i2c_address);
1302                 printk(KERN_INFO
1303                         "xc4000: Firmware has been loaded previously\n");
1304                 break;
1305         case XC_PRODUCT_ID_FW_NOT_LOADED:
1306                 printk(KERN_INFO
1307                         "xc4000: Successfully identified at address 0x%02x\n",
1308                         cfg->i2c_address);
1309                 printk(KERN_INFO
1310                         "xc4000: Firmware has not been loaded previously\n");
1311                 break;
1312         default:
1313                 printk(KERN_ERR
1314                         "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1315                         cfg->i2c_address, id);
1316                 goto fail;
1317         }
1318
1319         mutex_unlock(&xc4000_list_mutex);
1320
1321         memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1322                 sizeof(struct dvb_tuner_ops));
1323
1324         /* FIXME: For now, load the firmware at startup.  We will remove this
1325            before the code goes to production... */
1326         xc4000_fwupload(fe);
1327         printk("xc4000_fwupload done\n");
1328
1329         std0 = 0;
1330 //      rc = load_firmware(fe, BASE | new_fw.type, &std0);
1331         rc = load_firmware(fe, BASE, &std0);
1332         if (rc != XC_RESULT_SUCCESS) {
1333                 tuner_err("Error %d while loading base firmware\n",
1334                           rc);
1335                 goto fail;
1336         }
1337
1338         /* Load INIT1, if needed */
1339         tuner_dbg("Load init1 firmware, if exists\n");
1340
1341 //      rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1342         rc = load_firmware(fe, INIT1, &std0);
1343         printk("init1 load result %x\n", rc);
1344
1345         if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1346                         goto fail;
1347         printk("djh id is now %x\n", id);
1348
1349         return fe;
1350 fail:
1351         mutex_unlock(&xc4000_list_mutex);
1352
1353         xc4000_release(fe);
1354         return NULL;
1355 }
1356 EXPORT_SYMBOL(xc4000_attach);
1357
1358 MODULE_AUTHOR("Steven Toth, Davide Ferri");
1359 MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1360 MODULE_LICENSE("GPL");