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