blob: b3b33d266b7cdbb17bf984f30d274bd9fa418a4a [file] [log] [blame]
Davide Ferri8d009a02009-06-23 22:34:06 -03001/*
2 * Driver for Xceive XC4000 "QAM/8VSB single chip tuner"
3 *
4 * Copyright (c) 2007 Xceive Corporation
5 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
7 * Copyright (c) 2009 Davide Ferri <d.ferri@zero11.it>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 *
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/videodev2.h>
28#include <linux/delay.h>
29#include <linux/dvb/frontend.h>
30#include <linux/i2c.h>
Devin Heitmueller11091a32009-07-20 00:54:57 -030031#include <asm/unaligned.h>
Davide Ferri8d009a02009-06-23 22:34:06 -030032
33#include "dvb_frontend.h"
34
35#include "xc4000.h"
36#include "tuner-i2c.h"
Devin Heitmueller11091a32009-07-20 00:54:57 -030037#include "tuner-xc2028-types.h"
Davide Ferri8d009a02009-06-23 22:34:06 -030038
Devin Heitmuellerd0962382009-07-25 17:39:54 -030039static int debug=1;
Davide Ferri8d009a02009-06-23 22:34:06 -030040module_param(debug, int, 0644);
41MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
42
43static int no_poweroff;
44module_param(no_poweroff, int, 0644);
45MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
46 "\t\t1 keep device energized and with tuner ready all the times.\n"
47 "\t\tFaster, but consumes more power and keeps the device hotter");
48
49static DEFINE_MUTEX(xc4000_list_mutex);
50static LIST_HEAD(hybrid_tuner_instance_list);
51
52#define dprintk(level, fmt, arg...) if (debug >= level) \
53 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
54
Devin Heitmuellerd0962382009-07-25 17:39:54 -030055#define XC4000_DEFAULT_FIRMWARE "xc4000-02.fw"
56#define XC4000_DEFAULT_FIRMWARE_SIZE 18643
Devin Heitmueller11091a32009-07-20 00:54:57 -030057
58
59/* struct for storing firmware table */
60struct firmware_description {
61 unsigned int type;
62 v4l2_std_id id;
63 __u16 int_freq;
64 unsigned char *ptr;
65 unsigned int size;
66};
67
68struct firmware_properties {
69 unsigned int type;
70 v4l2_std_id id;
71 v4l2_std_id std_req;
72 __u16 int_freq;
73 unsigned int scode_table;
74 int scode_nr;
75};
Davide Ferri8d009a02009-06-23 22:34:06 -030076
77struct xc4000_priv {
78 struct tuner_i2c_props i2c_props;
79 struct list_head hybrid_tuner_instance_list;
Devin Heitmueller11091a32009-07-20 00:54:57 -030080 struct firmware_description *firm;
81 int firm_size;
82 __u16 firm_version;
Davide Ferri8d009a02009-06-23 22:34:06 -030083 u32 if_khz;
84 u32 freq_hz;
85 u32 bandwidth;
86 u8 video_standard;
87 u8 rf_mode;
Devin Heitmuellerd0962382009-07-25 17:39:54 -030088// struct xc2028_ctrl ctrl;
89 struct firmware_properties cur_fw;
90 __u16 hwmodel;
91 __u16 hwvers;
Devin Heitmueller799ed112009-10-04 23:09:18 -030092 u8 ignore_i2c_write_errors;
Davide Ferri8d009a02009-06-23 22:34:06 -030093};
94
95/* Misc Defines */
96#define MAX_TV_STANDARD 23
97#define XC_MAX_I2C_WRITE_LENGTH 64
98
99/* Signal Types */
100#define XC_RF_MODE_AIR 0
101#define XC_RF_MODE_CABLE 1
102
103/* Result codes */
104#define XC_RESULT_SUCCESS 0
105#define XC_RESULT_RESET_FAILURE 1
106#define XC_RESULT_I2C_WRITE_FAILURE 2
107#define XC_RESULT_I2C_READ_FAILURE 3
108#define XC_RESULT_OUT_OF_RANGE 5
109
110/* Product id */
111#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300112#define XC_PRODUCT_ID_FW_LOADED 0x0FA0
Davide Ferri8d009a02009-06-23 22:34:06 -0300113
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300114/* Registers (Write-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300115#define XREG_INIT 0x00
116#define XREG_VIDEO_MODE 0x01
117#define XREG_AUDIO_MODE 0x02
118#define XREG_RF_FREQ 0x03
119#define XREG_D_CODE 0x04
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300120#define XREG_DIRECTSITTING_MODE 0x05
121#define XREG_SEEK_MODE 0x06
122#define XREG_POWER_DOWN 0x08
123#define XREG_SIGNALSOURCE 0x0A
124#define XREG_AMPLITUDE 0x10
Davide Ferri8d009a02009-06-23 22:34:06 -0300125
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300126/* Registers (Read-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300127#define XREG_ADC_ENV 0x00
128#define XREG_QUALITY 0x01
129#define XREG_FRAME_LINES 0x02
130#define XREG_HSYNC_FREQ 0x03
131#define XREG_LOCK 0x04
132#define XREG_FREQ_ERROR 0x05
133#define XREG_SNR 0x06
134#define XREG_VERSION 0x07
135#define XREG_PRODUCT_ID 0x08
Davide Ferri8d009a02009-06-23 22:34:06 -0300136
137/*
138 Basic firmware description. This will remain with
139 the driver for documentation purposes.
140
141 This represents an I2C firmware file encoded as a
142 string of unsigned char. Format is as follows:
143
144 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
145 char[1 ]=len0_LSB -> length of first write transaction
146 char[2 ]=data0 -> first byte to be sent
147 char[3 ]=data1
148 char[4 ]=data2
149 char[ ]=...
150 char[M ]=dataN -> last byte to be sent
151 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
152 char[M+2]=len1_LSB -> length of second write transaction
153 char[M+3]=data0
154 char[M+4]=data1
155 ...
156 etc.
157
158 The [len] value should be interpreted as follows:
159
160 len= len_MSB _ len_LSB
161 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
162 len=0000_0000_0000_0000 : Reset command: Do hardware reset
163 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
164 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
165
166 For the RESET and WAIT commands, the two following bytes will contain
167 immediately the length of the following transaction.
168
169*/
170struct XC_TV_STANDARD {
171 char *Name;
172 u16 AudioMode;
173 u16 VideoMode;
174};
175
176/* Tuner standards */
177#define MN_NTSC_PAL_BTSC 0
178#define MN_NTSC_PAL_A2 1
179#define MN_NTSC_PAL_EIAJ 2
180#define MN_NTSC_PAL_Mono 3
181#define BG_PAL_A2 4
182#define BG_PAL_NICAM 5
183#define BG_PAL_MONO 6
184#define I_PAL_NICAM 7
185#define I_PAL_NICAM_MONO 8
186#define DK_PAL_A2 9
187#define DK_PAL_NICAM 10
188#define DK_PAL_MONO 11
189#define DK_SECAM_A2DK1 12
190#define DK_SECAM_A2LDK3 13
191#define DK_SECAM_A2MONO 14
192#define L_SECAM_NICAM 15
193#define LC_SECAM_NICAM 16
Davide Ferri8d009a02009-06-23 22:34:06 -0300194#define FM_Radio_INPUT2 21
195#define FM_Radio_INPUT1 22
196
197/* WAS :
198static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
199 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
200 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
201 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
202 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
203 {"B/G-PAL-A2", 0x0A00, 0x8049},
204 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
205 {"B/G-PAL-MONO", 0x0878, 0x8059},
206 {"I-PAL-NICAM", 0x1080, 0x8009},
207 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
208 {"D/K-PAL-A2", 0x1600, 0x8009},
209 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
210 {"D/K-PAL-MONO", 0x1478, 0x8009},
211 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
212 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
213 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
214 {"L-SECAM-NICAM", 0x8E82, 0x0009},
215 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
216 {"DTV6", 0x00C0, 0x8002},
217 {"DTV8", 0x00C0, 0x800B},
218 {"DTV7/8", 0x00C0, 0x801B},
219 {"DTV7", 0x00C0, 0x8007},
220 {"FM Radio-INPUT2", 0x9802, 0x9002},
221 {"FM Radio-INPUT1", 0x0208, 0x9002}
222};*/
223
224static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
225 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x8020},
226 {"M/N-NTSC/PAL-A2", 0x0000, 0x8020},
227 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x8020},
228 {"M/N-NTSC/PAL-Mono", 0x0078, 0x8020},
229 {"B/G-PAL-A2", 0x0000, 0x8059},
230 {"B/G-PAL-NICAM", 0x0004, 0x8059},
231 {"B/G-PAL-MONO", 0x0078, 0x8059},
232 {"I-PAL-NICAM", 0x0080, 0x8049},
233 {"I-PAL-NICAM-MONO", 0x0078, 0x8049},
234 {"D/K-PAL-A2", 0x0000, 0x8049},
235 {"D/K-PAL-NICAM", 0x0080, 0x8049},
236 {"D/K-PAL-MONO", 0x0078, 0x8049},
237 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049},
238 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049},
239 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049},
240 {"L-SECAM-NICAM", 0x8080, 0x0009},
241 {"L'-SECAM-NICAM", 0x8080, 0x4009},
242 {"DTV6", 0x00C0, 0x8002},
243 {"DTV8", 0x00C0, 0x800B},
244 {"DTV7/8", 0x00C0, 0x801B},
245 {"DTV7", 0x00C0, 0x8007},
246 {"FM Radio-INPUT2", 0x0008, 0x9800},
247 {"FM Radio-INPUT1", 0x0008, 0x9000}
248};
249
Davide Ferri8d009a02009-06-23 22:34:06 -0300250static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
251static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
252static int xc4000_TunerReset(struct dvb_frontend *fe);
253
254static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
255{
256 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
257 .flags = 0, .buf = buf, .len = len };
Davide Ferri8d009a02009-06-23 22:34:06 -0300258 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
Devin Heitmueller799ed112009-10-04 23:09:18 -0300259 if (priv->ignore_i2c_write_errors == 0) {
260 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
261 len);
262 if (len == 4) {
263 printk("bytes %02x %02x %02x %02x\n", buf[0],
264 buf[1], buf[2], buf[3]);
265 }
266 return XC_RESULT_I2C_WRITE_FAILURE;
267 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300268 }
269 return XC_RESULT_SUCCESS;
270}
271
272/* This routine is never used because the only time we read data from the
273 i2c bus is when we read registers, and we want that to be an atomic i2c
274 transaction in case we are on a multi-master bus */
Davide Ferri8d009a02009-06-23 22:34:06 -0300275
276static void xc_wait(int wait_ms)
277{
278 msleep(wait_ms);
279}
280
281static int xc4000_TunerReset(struct dvb_frontend *fe)
282{
283 struct xc4000_priv *priv = fe->tuner_priv;
284 int ret;
285
286 dprintk(1, "%s()\n", __func__);
287
288 if (fe->callback) {
289 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
290 fe->dvb->priv :
291 priv->i2c_props.adap->algo_data,
292 DVB_FRONTEND_COMPONENT_TUNER,
293 XC4000_TUNER_RESET, 0);
294 if (ret) {
295 printk(KERN_ERR "xc4000: reset failed\n");
296 return XC_RESULT_RESET_FAILURE;
297 }
298 } else {
299 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
300 return XC_RESULT_RESET_FAILURE;
301 }
302 return XC_RESULT_SUCCESS;
303}
304
305static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
306{
307 u8 buf[4];
Davide Ferri8d009a02009-06-23 22:34:06 -0300308 int result;
309
310 buf[0] = (regAddr >> 8) & 0xFF;
311 buf[1] = regAddr & 0xFF;
312 buf[2] = (i2cData >> 8) & 0xFF;
313 buf[3] = i2cData & 0xFF;
314 result = xc_send_i2c_data(priv, buf, 4);
Davide Ferri8d009a02009-06-23 22:34:06 -0300315
316 return result;
317}
318
319static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
320{
321 struct xc4000_priv *priv = fe->tuner_priv;
322
323 int i, nbytes_to_send, result;
324 unsigned int len, pos, index;
325 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
326
327 index = 0;
328 while ((i2c_sequence[index] != 0xFF) ||
329 (i2c_sequence[index + 1] != 0xFF)) {
330 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
331 if (len == 0x0000) {
332 /* RESET command */
333 result = xc4000_TunerReset(fe);
334 index += 2;
335 if (result != XC_RESULT_SUCCESS)
336 return result;
337 } else if (len & 0x8000) {
338 /* WAIT command */
339 xc_wait(len & 0x7FFF);
340 index += 2;
341 } else {
342 /* Send i2c data whilst ensuring individual transactions
343 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
344 */
345 index += 2;
346 buf[0] = i2c_sequence[index];
347 buf[1] = i2c_sequence[index + 1];
348 pos = 2;
349 while (pos < len) {
350 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
351 nbytes_to_send =
352 XC_MAX_I2C_WRITE_LENGTH;
353 else
354 nbytes_to_send = (len - pos + 2);
355 for (i = 2; i < nbytes_to_send; i++) {
356 buf[i] = i2c_sequence[index + pos +
357 i - 2];
358 }
359 result = xc_send_i2c_data(priv, buf,
360 nbytes_to_send);
361
362 if (result != XC_RESULT_SUCCESS)
363 return result;
364
365 pos += nbytes_to_send - 2;
366 }
367 index += len;
368 }
369 }
370 return XC_RESULT_SUCCESS;
371}
372
Davide Ferri8d009a02009-06-23 22:34:06 -0300373static int xc_SetTVStandard(struct xc4000_priv *priv,
374 u16 VideoMode, u16 AudioMode)
375{
376 int ret;
377 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
378 dprintk(1, "%s() Standard = %s\n",
379 __func__,
380 XC4000_Standard[priv->video_standard].Name);
381
Devin Heitmueller799ed112009-10-04 23:09:18 -0300382 /* Don't complain when the request fails because of i2c stretching */
383 priv->ignore_i2c_write_errors = 1;
384
Davide Ferri8d009a02009-06-23 22:34:06 -0300385 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
386 if (ret == XC_RESULT_SUCCESS)
387 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
388
Devin Heitmueller799ed112009-10-04 23:09:18 -0300389 priv->ignore_i2c_write_errors = 0;
390
Davide Ferri8d009a02009-06-23 22:34:06 -0300391 return ret;
392}
393
394static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
395{
396 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
397 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
398
399 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
400 rf_mode = XC_RF_MODE_CABLE;
401 printk(KERN_ERR
402 "%s(), Invalid mode, defaulting to CABLE",
403 __func__);
404 }
405 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
406}
407
408static const struct dvb_tuner_ops xc4000_tuner_ops;
409
410static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
411{
412 u16 freq_code;
413
414 dprintk(1, "%s(%u)\n", __func__, freq_hz);
415
416 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
417 (freq_hz < xc4000_tuner_ops.info.frequency_min))
418 return XC_RESULT_OUT_OF_RANGE;
419
420 freq_code = (u16)(freq_hz / 15625);
421
422 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
423 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
424 only be used for fast scanning for channel lock) */
425 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
426}
427
428
Davide Ferri8d009a02009-06-23 22:34:06 -0300429static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
430{
431 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
432}
433
434static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
435{
436 int result;
437 u16 regData;
438 u32 tmp;
439
440 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
441 if (result != XC_RESULT_SUCCESS)
442 return result;
443
444 tmp = (u32)regData;
445 (*freq_error_hz) = (tmp * 15625) / 1000;
446 return result;
447}
448
449static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
450{
451 return xc4000_readreg(priv, XREG_LOCK, lock_status);
452}
453
454static int xc_get_version(struct xc4000_priv *priv,
455 u8 *hw_majorversion, u8 *hw_minorversion,
456 u8 *fw_majorversion, u8 *fw_minorversion)
457{
458 u16 data;
459 int result;
460
461 result = xc4000_readreg(priv, XREG_VERSION, &data);
462 if (result != XC_RESULT_SUCCESS)
463 return result;
464
465 (*hw_majorversion) = (data >> 12) & 0x0F;
466 (*hw_minorversion) = (data >> 8) & 0x0F;
467 (*fw_majorversion) = (data >> 4) & 0x0F;
468 (*fw_minorversion) = data & 0x0F;
469
470 return 0;
471}
472
473/* WAS THERE
474static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
475{
476 return xc4000_readreg(priv, XREG_BUILD, buildrev);
477}*/
478
479static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
480{
481 u16 regData;
482 int result;
483
484 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
485 if (result != XC_RESULT_SUCCESS)
486 return result;
487
488 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
489 return result;
490}
491
492static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
493{
494 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
495}
496
497static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
498{
499 return xc4000_readreg(priv, XREG_QUALITY, quality);
500}
501
502static u16 WaitForLock(struct xc4000_priv *priv)
503{
504 u16 lockState = 0;
505 int watchDogCount = 40;
506
507 while ((lockState == 0) && (watchDogCount > 0)) {
508 xc_get_lock_status(priv, &lockState);
509 if (lockState != 1) {
510 xc_wait(5);
511 watchDogCount--;
512 }
513 }
514 return lockState;
515}
516
517#define XC_TUNE_ANALOG 0
518#define XC_TUNE_DIGITAL 1
519static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
520{
521 int found = 0;
Devin Heitmueller799ed112009-10-04 23:09:18 -0300522 int result = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300523
524 dprintk(1, "%s(%u)\n", __func__, freq_hz);
525
Devin Heitmueller799ed112009-10-04 23:09:18 -0300526 /* Don't complain when the request fails because of i2c stretching */
527 priv->ignore_i2c_write_errors = 1;
528 result = xc_set_RF_frequency(priv, freq_hz);
529 priv->ignore_i2c_write_errors = 0;
530
531 if (result != XC_RESULT_SUCCESS)
Davide Ferri8d009a02009-06-23 22:34:06 -0300532 return 0;
533
534 if (mode == XC_TUNE_ANALOG) {
535 if (WaitForLock(priv) == 1)
536 found = 1;
537 }
538
539 return found;
540}
541
542static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
543{
544 u8 buf[2] = { reg >> 8, reg & 0xff };
545 u8 bval[2] = { 0, 0 };
546 struct i2c_msg msg[2] = {
547 { .addr = priv->i2c_props.addr,
548 .flags = 0, .buf = &buf[0], .len = 2 },
549 { .addr = priv->i2c_props.addr,
550 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
551 };
552
553 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
554 printk(KERN_WARNING "xc4000: I2C read failed\n");
555 return -EREMOTEIO;
556 }
557
558 *val = (bval[0] << 8) | bval[1];
559 return XC_RESULT_SUCCESS;
560}
561
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300562#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
563static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
564{
565 if (type & BASE)
566 printk("BASE ");
567 if (type & INIT1)
568 printk("INIT1 ");
569 if (type & F8MHZ)
570 printk("F8MHZ ");
571 if (type & MTS)
572 printk("MTS ");
573 if (type & D2620)
574 printk("D2620 ");
575 if (type & D2633)
576 printk("D2633 ");
577 if (type & DTV6)
578 printk("DTV6 ");
579 if (type & QAM)
580 printk("QAM ");
581 if (type & DTV7)
582 printk("DTV7 ");
583 if (type & DTV78)
584 printk("DTV78 ");
585 if (type & DTV8)
586 printk("DTV8 ");
587 if (type & FM)
588 printk("FM ");
589 if (type & INPUT1)
590 printk("INPUT1 ");
591 if (type & LCD)
592 printk("LCD ");
593 if (type & NOGD)
594 printk("NOGD ");
595 if (type & MONO)
596 printk("MONO ");
597 if (type & ATSC)
598 printk("ATSC ");
599 if (type & IF)
600 printk("IF ");
601 if (type & LG60)
602 printk("LG60 ");
603 if (type & ATI638)
604 printk("ATI638 ");
605 if (type & OREN538)
606 printk("OREN538 ");
607 if (type & OREN36)
608 printk("OREN36 ");
609 if (type & TOYOTA388)
610 printk("TOYOTA388 ");
611 if (type & TOYOTA794)
612 printk("TOYOTA794 ");
613 if (type & DIBCOM52)
614 printk("DIBCOM52 ");
615 if (type & ZARLINK456)
616 printk("ZARLINK456 ");
617 if (type & CHINA)
618 printk("CHINA ");
619 if (type & F6MHZ)
620 printk("F6MHZ ");
621 if (type & INPUT2)
622 printk("INPUT2 ");
623 if (type & SCODE)
624 printk("SCODE ");
625 if (type & HAS_IF)
626 printk("HAS_IF_%d ", int_freq);
627}
628
Devin Heitmueller11091a32009-07-20 00:54:57 -0300629static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
630 v4l2_std_id *id)
631{
632 struct xc4000_priv *priv = fe->tuner_priv;
633 int i, best_i = -1, best_nr_matches = 0;
634 unsigned int type_mask = 0;
635
636 printk("%s called, want type=", __func__);
637 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300638 dump_firm_type(type);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300639 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
640 }
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
708found:
709 *id = priv->firm[i].id;
710
711ret:
712 printk("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
713 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300714 dump_firm_type(type);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300715 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300716 if (i < 0)
717 dump_stack();
Devin Heitmueller11091a32009-07-20 00:54:57 -0300718 }
719 return i;
720}
721
722static int load_firmware(struct dvb_frontend *fe, unsigned int type,
723 v4l2_std_id *id)
724{
725 struct xc4000_priv *priv = fe->tuner_priv;
726 int pos, rc;
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300727 unsigned char *p;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300728
729 printk("%s called\n", __func__);
730
731 pos = seek_firmware(fe, type, id);
732 if (pos < 0)
733 return pos;
734
735 printk("Loading firmware for type=");
736// dump_firm_type(priv->firm[pos].type);
737 printk("(%x), id %016llx.\n", priv->firm[pos].type,
738 (unsigned long long)*id);
739
740 p = priv->firm[pos].ptr;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300741 printk("firmware length = %d\n", priv->firm[pos].size);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300742
Devin Heitmueller799ed112009-10-04 23:09:18 -0300743 /* Don't complain when the request fails because of i2c stretching */
744 priv->ignore_i2c_write_errors = 1;
745
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300746 rc = xc_load_i2c_sequence(fe, p);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300747
Devin Heitmueller799ed112009-10-04 23:09:18 -0300748 priv->ignore_i2c_write_errors = 0;
749
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300750 return rc;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300751}
752
Davide Ferri8d009a02009-06-23 22:34:06 -0300753static int xc4000_fwupload(struct dvb_frontend *fe)
754{
755 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300756 const struct firmware *fw = NULL;
757 const unsigned char *p, *endp;
758 int rc = 0;
759 int n, n_array;
760 char name[33];
761 char *fname;
Davide Ferri8d009a02009-06-23 22:34:06 -0300762
Devin Heitmueller11091a32009-07-20 00:54:57 -0300763 printk("%s called\n", __func__);
Davide Ferri8d009a02009-06-23 22:34:06 -0300764
Devin Heitmueller11091a32009-07-20 00:54:57 -0300765 fname = XC4000_DEFAULT_FIRMWARE;
766
767 printk("Reading firmware %s\n", fname);
768 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
769 if (rc < 0) {
770 if (rc == -ENOENT)
771 printk("Error: firmware %s not found.\n",
772 fname);
773 else
774 printk("Error %d while requesting firmware %s \n",
775 rc, fname);
776
777 return rc;
778 }
779 p = fw->data;
780 endp = p + fw->size;
781
782 if (fw->size < sizeof(name) - 1 + 2 + 2) {
783 printk("Error: firmware file %s has invalid size!\n",
784 fname);
785 goto corrupt;
Davide Ferri8d009a02009-06-23 22:34:06 -0300786 }
787
Devin Heitmueller11091a32009-07-20 00:54:57 -0300788 memcpy(name, p, sizeof(name) - 1);
789 name[sizeof(name) - 1] = 0;
790 p += sizeof(name) - 1;
791
792 priv->firm_version = get_unaligned_le16(p);
793 p += 2;
794
795 n_array = get_unaligned_le16(p);
796 p += 2;
797
798 printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
799 n_array, fname, name,
800 priv->firm_version >> 8, priv->firm_version & 0xff);
801
802 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
803 if (priv->firm == NULL) {
804 printk("Not enough memory to load firmware file.\n");
805 rc = -ENOMEM;
806 goto err;
807 }
808 priv->firm_size = n_array;
809
810 n = -1;
811 while (p < endp) {
812 __u32 type, size;
813 v4l2_std_id id;
814 __u16 int_freq = 0;
815
816 n++;
817 if (n >= n_array) {
818 printk("More firmware images in file than "
819 "were expected!\n");
820 goto corrupt;
821 }
822
823 /* Checks if there's enough bytes to read */
824 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
825 goto header;
826
827 type = get_unaligned_le32(p);
828 p += sizeof(type);
829
830 id = get_unaligned_le64(p);
831 p += sizeof(id);
832
833 if (type & HAS_IF) {
834 int_freq = get_unaligned_le16(p);
835 p += sizeof(int_freq);
836 if (endp - p < sizeof(size))
837 goto header;
838 }
839
840 size = get_unaligned_le32(p);
841 p += sizeof(size);
842
843 if (!size || size > endp - p) {
844 printk("Firmware type ");
845// dump_firm_type(type);
846 printk("(%x), id %llx is corrupted "
847 "(size=%d, expected %d)\n",
848 type, (unsigned long long)id,
849 (unsigned)(endp - p), size);
850 goto corrupt;
851 }
852
853 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
854 if (priv->firm[n].ptr == NULL) {
855 printk("Not enough memory to load firmware file.\n");
856 rc = -ENOMEM;
857 goto err;
858 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300859
Devin Heitmueller11091a32009-07-20 00:54:57 -0300860 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300861 printk("Reading firmware type ");
862 dump_firm_type_and_int_freq(type, int_freq);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300863 printk("(%x), id %llx, size=%d.\n",
864 type, (unsigned long long)id, size);
865 }
866
867 memcpy(priv->firm[n].ptr, p, size);
868 priv->firm[n].type = type;
869 priv->firm[n].id = id;
870 priv->firm[n].size = size;
871 priv->firm[n].int_freq = int_freq;
872
873 p += size;
Davide Ferri8d009a02009-06-23 22:34:06 -0300874 }
875
Devin Heitmueller11091a32009-07-20 00:54:57 -0300876 if (n + 1 != priv->firm_size) {
877 printk("Firmware file is incomplete!\n");
878 goto corrupt;
879 }
880
881 goto done;
882
883header:
884 printk("Firmware header is incomplete!\n");
885corrupt:
886 rc = -EINVAL;
887 printk("Error: firmware file is corrupted!\n");
888
889err:
890 printk("Releasing partially loaded firmware file.\n");
891// free_firmware(priv);
892
893done:
Davide Ferri8d009a02009-06-23 22:34:06 -0300894 release_firmware(fw);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300895 if (rc == 0)
896 printk("Firmware files loaded.\n");
897
898 return rc;
Davide Ferri8d009a02009-06-23 22:34:06 -0300899}
900
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300901static int load_scode(struct dvb_frontend *fe, unsigned int type,
902 v4l2_std_id *id, __u16 int_freq, int scode)
903{
904 struct xc4000_priv *priv = fe->tuner_priv;
905 int pos, rc;
906 unsigned char *p;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300907 u8 scode_buf[13];
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300908 u8 indirect_mode[5];
909
Devin Heitmuellerfe830362009-07-28 00:04:27 -0300910 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300911
912 if (!int_freq) {
913 pos = seek_firmware(fe, type, id);
914 if (pos < 0)
915 return pos;
916 } else {
917 for (pos = 0; pos < priv->firm_size; pos++) {
918 if ((priv->firm[pos].int_freq == int_freq) &&
919 (priv->firm[pos].type & HAS_IF))
920 break;
921 }
922 if (pos == priv->firm_size)
923 return -ENOENT;
924 }
925
926 p = priv->firm[pos].ptr;
927
928 if (priv->firm[pos].type & HAS_IF) {
929 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
930 return -EINVAL;
931 p += 12 * scode;
932 } else {
933 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
934 * has a 2-byte size header in the firmware format. */
935 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
936 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
937 return -EINVAL;
938 p += 14 * scode + 2;
939 }
940
941 tuner_info("Loading SCODE for type=");
942 dump_firm_type_and_int_freq(priv->firm[pos].type,
943 priv->firm[pos].int_freq);
944 printk("(%x), id %016llx.\n", priv->firm[pos].type,
945 (unsigned long long)*id);
946
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300947 scode_buf[0] = 0x00;
948 memcpy(&scode_buf[1], p, 12);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300949
950 /* Enter direct-mode */
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300951 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
952 if (rc < 0) {
953 printk("failed to put device into direct mode!\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300954 return -EIO;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300955 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300956
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300957 rc = xc_send_i2c_data(priv, scode_buf, 13);
958 if (rc != XC_RESULT_SUCCESS) {
959 /* Even if the send failed, make sure we set back to indirect
960 mode */
961 printk("Failed to set scode %d\n", rc);
962 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300963
964 /* Switch back to indirect-mode */
965 memset(indirect_mode, 0, sizeof(indirect_mode));
966 indirect_mode[4] = 0x88;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300967 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
968 msleep(10);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300969
970 return 0;
971}
972
973static int check_firmware(struct dvb_frontend *fe, unsigned int type,
974 v4l2_std_id std, __u16 int_freq)
975{
976 struct xc4000_priv *priv = fe->tuner_priv;
977 struct firmware_properties new_fw;
978 int rc = 0, is_retry = 0;
979 u16 version, hwmodel;
980 v4l2_std_id std0;
Devin Heitmueller799ed112009-10-04 23:09:18 -0300981 u8 hw_major, hw_minor, fw_major, fw_minor;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300982
983 dprintk(1, "%s called\n", __func__);
984
985 if (!priv->firm) {
986 rc = xc4000_fwupload(fe);
987 if (rc < 0)
988 return rc;
989 }
990
991#ifdef DJH_DEBUG
992 if (priv->ctrl.mts && !(type & FM))
993 type |= MTS;
994#endif
995
996retry:
997 new_fw.type = type;
998 new_fw.id = std;
999 new_fw.std_req = std;
1000// new_fw.scode_table = SCODE | priv->ctrl.scode_table;
1001 new_fw.scode_table = SCODE;
1002 new_fw.scode_nr = 0;
1003 new_fw.int_freq = int_freq;
1004
1005 dprintk(1, "checking firmware, user requested type=");
1006 if (debug) {
1007 dump_firm_type(new_fw.type);
1008 printk("(%x), id %016llx, ", new_fw.type,
1009 (unsigned long long)new_fw.std_req);
1010 if (!int_freq) {
1011 printk("scode_tbl ");
1012#ifdef DJH_DEBUG
1013 dump_firm_type(priv->ctrl.scode_table);
1014 printk("(%x), ", priv->ctrl.scode_table);
1015#endif
1016 } else
1017 printk("int_freq %d, ", new_fw.int_freq);
1018 printk("scode_nr %d\n", new_fw.scode_nr);
1019 }
1020
1021 /* No need to reload base firmware if it matches */
1022 if (((BASE | new_fw.type) & BASE_TYPES) ==
1023 (priv->cur_fw.type & BASE_TYPES)) {
1024 dprintk(1, "BASE firmware not changed.\n");
1025 goto skip_base;
1026 }
1027
1028 /* Updating BASE - forget about all currently loaded firmware */
1029 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1030
1031 /* Reset is needed before loading firmware */
1032 rc = xc4000_TunerReset(fe);
1033 if (rc < 0)
1034 goto fail;
1035
1036 /* BASE firmwares are all std0 */
1037 std0 = 0;
1038 rc = load_firmware(fe, BASE | new_fw.type, &std0);
1039 if (rc < 0) {
1040 printk("Error %d while loading base firmware\n", rc);
1041 goto fail;
1042 }
1043
1044 /* Load INIT1, if needed */
1045 dprintk(1, "Load init1 firmware, if exists\n");
1046
1047 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1048 if (rc == -ENOENT)
1049 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
1050 &std0);
1051 if (rc < 0 && rc != -ENOENT) {
1052 tuner_err("Error %d while loading init1 firmware\n",
1053 rc);
1054 goto fail;
1055 }
1056
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001057 printk("Done with init1\n");
1058
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001059skip_base:
1060 /*
1061 * No need to reload standard specific firmware if base firmware
1062 * was not reloaded and requested video standards have not changed.
1063 */
1064 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1065 priv->cur_fw.std_req == std) {
1066 dprintk(1, "Std-specific firmware already loaded.\n");
1067 goto skip_std_specific;
1068 }
1069
1070 /* Reloading std-specific firmware forces a SCODE update */
1071 priv->cur_fw.scode_table = 0;
1072
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001073 /* Load the standard firmware */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001074 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001075
1076 if (rc < 0)
1077 goto fail;
1078
1079skip_std_specific:
1080 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1081 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1082 dprintk(1, "SCODE firmware already loaded.\n");
1083 goto check_device;
1084 }
1085
1086 if (new_fw.type & FM)
1087 goto check_device;
1088
1089 /* Load SCODE firmware, if exists */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001090 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1091 new_fw.int_freq, new_fw.scode_nr);
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001092 if (rc != XC_RESULT_SUCCESS)
1093 dprintk(1, "load scode failed %d\n", rc);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001094
1095check_device:
1096 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1097
Devin Heitmueller799ed112009-10-04 23:09:18 -03001098 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001099 &fw_minor) != XC_RESULT_SUCCESS) {
1100 printk("Unable to read tuner registers.\n");
1101 goto fail;
1102 }
1103
1104 dprintk(1, "Device is Xceive %d version %d.%d, "
1105 "firmware version %d.%d\n",
1106 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1107
1108 /* Check firmware version against what we downloaded. */
1109#ifdef DJH_DEBUG
1110 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1111 printk("Incorrect readback of firmware version %x.\n",
1112 (version & 0xff));
1113 goto fail;
1114 }
1115#endif
1116
1117 /* Check that the tuner hardware model remains consistent over time. */
1118 if (priv->hwmodel == 0 && hwmodel == 4000) {
1119 priv->hwmodel = hwmodel;
1120 priv->hwvers = version & 0xff00;
1121 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1122 priv->hwvers != (version & 0xff00)) {
1123 printk("Read invalid device hardware information - tuner "
1124 "hung?\n");
1125 goto fail;
1126 }
1127
1128 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1129
1130 /*
1131 * By setting BASE in cur_fw.type only after successfully loading all
1132 * firmwares, we can:
1133 * 1. Identify that BASE firmware with type=0 has been loaded;
1134 * 2. Tell whether BASE firmware was just changed the next time through.
1135 */
1136 priv->cur_fw.type |= BASE;
1137
1138 return 0;
1139
1140fail:
1141 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1142 if (!is_retry) {
1143 msleep(50);
1144 is_retry = 1;
1145 dprintk(1, "Retrying firmware load\n");
1146 goto retry;
1147 }
1148
1149 if (rc == -ENOENT)
1150 rc = -EINVAL;
1151 return rc;
1152}
Devin Heitmueller11091a32009-07-20 00:54:57 -03001153
Davide Ferri8d009a02009-06-23 22:34:06 -03001154static void xc_debug_dump(struct xc4000_priv *priv)
1155{
1156 u16 adc_envelope;
1157 u32 freq_error_hz = 0;
1158 u16 lock_status;
1159 u32 hsync_freq_hz = 0;
1160 u16 frame_lines;
1161 u16 quality;
1162 u8 hw_majorversion = 0, hw_minorversion = 0;
1163 u8 fw_majorversion = 0, fw_minorversion = 0;
1164// u16 fw_buildversion = 0;
1165
1166 /* Wait for stats to stabilize.
1167 * Frame Lines needs two frame times after initial lock
1168 * before it is valid.
1169 */
1170 xc_wait(100);
1171
1172 xc_get_ADC_Envelope(priv, &adc_envelope);
1173 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1174
1175 xc_get_frequency_error(priv, &freq_error_hz);
1176 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1177
1178 xc_get_lock_status(priv, &lock_status);
1179 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1180 lock_status);
1181
1182 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1183 &fw_majorversion, &fw_minorversion);
1184// WAS:
1185// xc_get_buildversion(priv, &fw_buildversion);
1186// dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
1187// hw_majorversion, hw_minorversion,
1188// fw_majorversion, fw_minorversion, fw_buildversion);
1189// NOW:
1190 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1191 hw_majorversion, hw_minorversion,
1192 fw_majorversion, fw_minorversion);
1193
1194 xc_get_hsync_freq(priv, &hsync_freq_hz);
1195 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1196
1197 xc_get_frame_lines(priv, &frame_lines);
1198 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1199
1200 xc_get_quality(priv, &quality);
1201 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1202}
1203
1204static int xc4000_set_params(struct dvb_frontend *fe,
1205 struct dvb_frontend_parameters *params)
1206{
1207 struct xc4000_priv *priv = fe->tuner_priv;
1208 int ret;
1209
Davide Ferri8d009a02009-06-23 22:34:06 -03001210 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1211
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001212 /* FIXME: setup proper parameters */
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001213 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001214 return -EREMOTEIO;
1215 }
1216
Davide Ferri8d009a02009-06-23 22:34:06 -03001217 if (fe->ops.info.type == FE_ATSC) {
1218 dprintk(1, "%s() ATSC\n", __func__);
1219 switch (params->u.vsb.modulation) {
1220 case VSB_8:
1221 case VSB_16:
1222 dprintk(1, "%s() VSB modulation\n", __func__);
1223 priv->rf_mode = XC_RF_MODE_AIR;
1224 priv->freq_hz = params->frequency - 1750000;
1225 priv->bandwidth = BANDWIDTH_6_MHZ;
1226 priv->video_standard = DTV6;
1227 break;
1228 case QAM_64:
1229 case QAM_256:
1230 case QAM_AUTO:
1231 dprintk(1, "%s() QAM modulation\n", __func__);
1232 priv->rf_mode = XC_RF_MODE_CABLE;
1233 priv->freq_hz = params->frequency - 1750000;
1234 priv->bandwidth = BANDWIDTH_6_MHZ;
1235 priv->video_standard = DTV6;
1236 break;
1237 default:
1238 return -EINVAL;
1239 }
1240 } else if (fe->ops.info.type == FE_OFDM) {
1241 dprintk(1, "%s() OFDM\n", __func__);
1242 switch (params->u.ofdm.bandwidth) {
1243 case BANDWIDTH_6_MHZ:
1244 priv->bandwidth = BANDWIDTH_6_MHZ;
1245 priv->video_standard = DTV6;
1246 priv->freq_hz = params->frequency - 1750000;
1247 break;
1248 case BANDWIDTH_7_MHZ:
1249 printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
1250 return -EINVAL;
1251 case BANDWIDTH_8_MHZ:
1252 priv->bandwidth = BANDWIDTH_8_MHZ;
1253 priv->video_standard = DTV8;
1254 priv->freq_hz = params->frequency - 2750000;
1255 break;
1256 default:
1257 printk(KERN_ERR "xc4000 bandwidth not set!\n");
1258 return -EINVAL;
1259 }
1260 priv->rf_mode = XC_RF_MODE_AIR;
1261 } else {
1262 printk(KERN_ERR "xc4000 modulation type not supported!\n");
1263 return -EINVAL;
1264 }
1265
1266 dprintk(1, "%s() frequency=%d (compensated)\n",
1267 __func__, priv->freq_hz);
1268
1269 ret = xc_SetSignalSource(priv, priv->rf_mode);
1270 if (ret != XC_RESULT_SUCCESS) {
1271 printk(KERN_ERR
1272 "xc4000: xc_SetSignalSource(%d) failed\n",
1273 priv->rf_mode);
1274 return -EREMOTEIO;
1275 }
1276
1277 ret = xc_SetTVStandard(priv,
1278 XC4000_Standard[priv->video_standard].VideoMode,
1279 XC4000_Standard[priv->video_standard].AudioMode);
1280 if (ret != XC_RESULT_SUCCESS) {
1281 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1282 return -EREMOTEIO;
1283 }
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001284#ifdef DJH_DEBUG
Davide Ferri8d009a02009-06-23 22:34:06 -03001285 ret = xc_set_IF_frequency(priv, priv->if_khz);
1286 if (ret != XC_RESULT_SUCCESS) {
1287 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
1288 priv->if_khz);
1289 return -EIO;
1290 }
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001291#endif
Davide Ferri8d009a02009-06-23 22:34:06 -03001292 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1293
1294 if (debug)
1295 xc_debug_dump(priv);
1296
1297 return 0;
1298}
1299
1300static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1301{
1302 struct xc4000_priv *priv = fe->tuner_priv;
1303 int ret;
1304 u16 id;
1305
1306 ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1307 if (ret == XC_RESULT_SUCCESS) {
1308 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1309 ret = XC_RESULT_RESET_FAILURE;
1310 else
1311 ret = XC_RESULT_SUCCESS;
1312 }
1313
1314 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1315 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1316 return ret;
1317}
1318
1319static int xc4000_set_analog_params(struct dvb_frontend *fe,
1320 struct analog_parameters *params)
1321{
1322 struct xc4000_priv *priv = fe->tuner_priv;
1323 int ret;
1324
Davide Ferri8d009a02009-06-23 22:34:06 -03001325 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1326 __func__, params->frequency);
1327
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001328 /* FIXME: setup proper parameters */
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001329 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001330 return -EREMOTEIO;
1331 }
1332
Davide Ferri8d009a02009-06-23 22:34:06 -03001333 /* Fix me: it could be air. */
1334 priv->rf_mode = params->mode;
1335 if (params->mode > XC_RF_MODE_CABLE)
1336 priv->rf_mode = XC_RF_MODE_CABLE;
1337
1338 /* params->frequency is in units of 62.5khz */
1339 priv->freq_hz = params->frequency * 62500;
1340
1341 /* FIX ME: Some video standards may have several possible audio
1342 standards. We simply default to one of them here.
1343 */
1344 if (params->std & V4L2_STD_MN) {
1345 /* default to BTSC audio standard */
1346 priv->video_standard = MN_NTSC_PAL_BTSC;
1347 goto tune_channel;
1348 }
1349
1350 if (params->std & V4L2_STD_PAL_BG) {
1351 /* default to NICAM audio standard */
1352 priv->video_standard = BG_PAL_NICAM;
1353 goto tune_channel;
1354 }
1355
1356 if (params->std & V4L2_STD_PAL_I) {
1357 /* default to NICAM audio standard */
1358 priv->video_standard = I_PAL_NICAM;
1359 goto tune_channel;
1360 }
1361
1362 if (params->std & V4L2_STD_PAL_DK) {
1363 /* default to NICAM audio standard */
1364 priv->video_standard = DK_PAL_NICAM;
1365 goto tune_channel;
1366 }
1367
1368 if (params->std & V4L2_STD_SECAM_DK) {
1369 /* default to A2 DK1 audio standard */
1370 priv->video_standard = DK_SECAM_A2DK1;
1371 goto tune_channel;
1372 }
1373
1374 if (params->std & V4L2_STD_SECAM_L) {
1375 priv->video_standard = L_SECAM_NICAM;
1376 goto tune_channel;
1377 }
1378
1379 if (params->std & V4L2_STD_SECAM_LC) {
1380 priv->video_standard = LC_SECAM_NICAM;
1381 goto tune_channel;
1382 }
1383
1384tune_channel:
1385 ret = xc_SetSignalSource(priv, priv->rf_mode);
1386 if (ret != XC_RESULT_SUCCESS) {
1387 printk(KERN_ERR
1388 "xc4000: xc_SetSignalSource(%d) failed\n",
1389 priv->rf_mode);
1390 return -EREMOTEIO;
1391 }
1392
1393 ret = xc_SetTVStandard(priv,
1394 XC4000_Standard[priv->video_standard].VideoMode,
1395 XC4000_Standard[priv->video_standard].AudioMode);
1396 if (ret != XC_RESULT_SUCCESS) {
1397 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1398 return -EREMOTEIO;
1399 }
1400
1401 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1402
1403 if (debug)
1404 xc_debug_dump(priv);
1405
1406 return 0;
1407}
1408
1409static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1410{
1411 struct xc4000_priv *priv = fe->tuner_priv;
1412 dprintk(1, "%s()\n", __func__);
1413 *freq = priv->freq_hz;
1414 return 0;
1415}
1416
1417static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1418{
1419 struct xc4000_priv *priv = fe->tuner_priv;
1420 dprintk(1, "%s()\n", __func__);
1421
1422 *bw = priv->bandwidth;
1423 return 0;
1424}
1425
1426static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1427{
1428 struct xc4000_priv *priv = fe->tuner_priv;
1429 u16 lock_status = 0;
1430
1431 xc_get_lock_status(priv, &lock_status);
1432
1433 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1434
1435 *status = lock_status;
1436
1437 return 0;
1438}
1439
Davide Ferri8d009a02009-06-23 22:34:06 -03001440static int xc4000_sleep(struct dvb_frontend *fe)
1441{
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001442 /* FIXME: djh disable this for now... */
1443 return XC_RESULT_SUCCESS;
Davide Ferri8d009a02009-06-23 22:34:06 -03001444}
1445
1446static int xc4000_init(struct dvb_frontend *fe)
1447{
1448 struct xc4000_priv *priv = fe->tuner_priv;
1449 dprintk(1, "%s()\n", __func__);
1450
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001451 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
Davide Ferri8d009a02009-06-23 22:34:06 -03001452 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1453 return -EREMOTEIO;
1454 }
1455
1456 if (debug)
1457 xc_debug_dump(priv);
1458
1459 return 0;
1460}
1461
1462static int xc4000_release(struct dvb_frontend *fe)
1463{
1464 struct xc4000_priv *priv = fe->tuner_priv;
1465
1466 dprintk(1, "%s()\n", __func__);
1467
1468 mutex_lock(&xc4000_list_mutex);
1469
1470 if (priv)
1471 hybrid_tuner_release_state(priv);
1472
1473 mutex_unlock(&xc4000_list_mutex);
1474
1475 fe->tuner_priv = NULL;
1476
1477 return 0;
1478}
1479
1480static const struct dvb_tuner_ops xc4000_tuner_ops = {
1481 .info = {
1482 .name = "Xceive XC4000",
1483 .frequency_min = 1000000,
1484 .frequency_max = 1023000000,
1485 .frequency_step = 50000,
1486 },
1487
1488 .release = xc4000_release,
1489 .init = xc4000_init,
1490 .sleep = xc4000_sleep,
1491
1492 .set_params = xc4000_set_params,
1493 .set_analog_params = xc4000_set_analog_params,
1494 .get_frequency = xc4000_get_frequency,
1495 .get_bandwidth = xc4000_get_bandwidth,
1496 .get_status = xc4000_get_status
1497};
1498
1499struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1500 struct i2c_adapter *i2c,
1501 struct xc4000_config *cfg)
1502{
1503 struct xc4000_priv *priv = NULL;
1504 int instance;
1505 u16 id = 0;
1506
1507 dprintk(1, "%s(%d-%04x)\n", __func__,
1508 i2c ? i2c_adapter_id(i2c) : -1,
1509 cfg ? cfg->i2c_address : -1);
1510
1511 mutex_lock(&xc4000_list_mutex);
1512
1513 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1514 hybrid_tuner_instance_list,
1515 i2c, cfg->i2c_address, "xc4000");
1516 switch (instance) {
1517 case 0:
1518 goto fail;
1519 break;
1520 case 1:
1521 /* new tuner instance */
1522 priv->bandwidth = BANDWIDTH_6_MHZ;
1523 fe->tuner_priv = priv;
1524 break;
1525 default:
1526 /* existing tuner instance */
1527 fe->tuner_priv = priv;
1528 break;
1529 }
1530
1531 if (priv->if_khz == 0) {
1532 /* If the IF hasn't been set yet, use the value provided by
1533 the caller (occurs in hybrid devices where the analog
1534 call to xc4000_attach occurs before the digital side) */
1535 priv->if_khz = cfg->if_khz;
1536 }
1537
1538 /* Check if firmware has been loaded. It is possible that another
1539 instance of the driver has loaded the firmware.
1540 */
1541
1542 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1543 goto fail;
1544
1545 switch (id) {
1546 case XC_PRODUCT_ID_FW_LOADED:
1547 printk(KERN_INFO
1548 "xc4000: Successfully identified at address 0x%02x\n",
1549 cfg->i2c_address);
1550 printk(KERN_INFO
1551 "xc4000: Firmware has been loaded previously\n");
1552 break;
1553 case XC_PRODUCT_ID_FW_NOT_LOADED:
1554 printk(KERN_INFO
1555 "xc4000: Successfully identified at address 0x%02x\n",
1556 cfg->i2c_address);
1557 printk(KERN_INFO
1558 "xc4000: Firmware has not been loaded previously\n");
1559 break;
1560 default:
1561 printk(KERN_ERR
1562 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1563 cfg->i2c_address, id);
1564 goto fail;
1565 }
1566
1567 mutex_unlock(&xc4000_list_mutex);
1568
1569 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1570 sizeof(struct dvb_tuner_ops));
1571
Devin Heitmueller11091a32009-07-20 00:54:57 -03001572 /* FIXME: For now, load the firmware at startup. We will remove this
1573 before the code goes to production... */
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001574 check_firmware(fe, DTV8, 0, priv->if_khz);
Devin Heitmueller11091a32009-07-20 00:54:57 -03001575
Davide Ferri8d009a02009-06-23 22:34:06 -03001576 return fe;
1577fail:
1578 mutex_unlock(&xc4000_list_mutex);
1579
1580 xc4000_release(fe);
1581 return NULL;
1582}
1583EXPORT_SYMBOL(xc4000_attach);
1584
1585MODULE_AUTHOR("Steven Toth, Davide Ferri");
1586MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1587MODULE_LICENSE("GPL");