blob: 49d5929389ff35e73ee74ef95542910b57b5f7df [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 Heitmueller4922cec2009-12-27 17:50:43 -030039static int debug;
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 */
Devin Heitmuellered23db32009-10-05 01:27:14 -0300177#define XC4000_MN_NTSC_PAL_BTSC 0
178#define XC4000_MN_NTSC_PAL_A2 1
179#define XC4000_MN_NTSC_PAL_EIAJ 2
180#define XC4000_MN_NTSC_PAL_Mono 3
181#define XC4000_BG_PAL_A2 4
182#define XC4000_BG_PAL_NICAM 5
183#define XC4000_BG_PAL_MONO 6
184#define XC4000_I_PAL_NICAM 7
185#define XC4000_I_PAL_NICAM_MONO 8
186#define XC4000_DK_PAL_A2 9
187#define XC4000_DK_PAL_NICAM 10
188#define XC4000_DK_PAL_MONO 11
189#define XC4000_DK_SECAM_A2DK1 12
190#define XC4000_DK_SECAM_A2LDK3 13
191#define XC4000_DK_SECAM_A2MONO 14
192#define XC4000_L_SECAM_NICAM 15
193#define XC4000_LC_SECAM_NICAM 16
194#define XC4000_DTV6 17
195#define XC4000_DTV8 18
196#define XC4000_DTV7_8 19
197#define XC4000_DTV7 20
198#define XC4000_FM_Radio_INPUT2 21
199#define XC4000_FM_Radio_INPUT1 22
Davide Ferri8d009a02009-06-23 22:34:06 -0300200
201/* WAS :
202static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
203 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
204 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
205 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
206 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
207 {"B/G-PAL-A2", 0x0A00, 0x8049},
208 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
209 {"B/G-PAL-MONO", 0x0878, 0x8059},
210 {"I-PAL-NICAM", 0x1080, 0x8009},
211 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
212 {"D/K-PAL-A2", 0x1600, 0x8009},
213 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
214 {"D/K-PAL-MONO", 0x1478, 0x8009},
215 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
216 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
217 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
218 {"L-SECAM-NICAM", 0x8E82, 0x0009},
219 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
220 {"DTV6", 0x00C0, 0x8002},
221 {"DTV8", 0x00C0, 0x800B},
222 {"DTV7/8", 0x00C0, 0x801B},
223 {"DTV7", 0x00C0, 0x8007},
224 {"FM Radio-INPUT2", 0x9802, 0x9002},
225 {"FM Radio-INPUT1", 0x0208, 0x9002}
226};*/
227
228static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
229 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x8020},
230 {"M/N-NTSC/PAL-A2", 0x0000, 0x8020},
231 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x8020},
232 {"M/N-NTSC/PAL-Mono", 0x0078, 0x8020},
233 {"B/G-PAL-A2", 0x0000, 0x8059},
234 {"B/G-PAL-NICAM", 0x0004, 0x8059},
235 {"B/G-PAL-MONO", 0x0078, 0x8059},
236 {"I-PAL-NICAM", 0x0080, 0x8049},
237 {"I-PAL-NICAM-MONO", 0x0078, 0x8049},
238 {"D/K-PAL-A2", 0x0000, 0x8049},
239 {"D/K-PAL-NICAM", 0x0080, 0x8049},
240 {"D/K-PAL-MONO", 0x0078, 0x8049},
241 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049},
242 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049},
243 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049},
244 {"L-SECAM-NICAM", 0x8080, 0x0009},
245 {"L'-SECAM-NICAM", 0x8080, 0x4009},
246 {"DTV6", 0x00C0, 0x8002},
247 {"DTV8", 0x00C0, 0x800B},
248 {"DTV7/8", 0x00C0, 0x801B},
249 {"DTV7", 0x00C0, 0x8007},
250 {"FM Radio-INPUT2", 0x0008, 0x9800},
251 {"FM Radio-INPUT1", 0x0008, 0x9000}
252};
253
Davide Ferri8d009a02009-06-23 22:34:06 -0300254static int xc4000_is_firmware_loaded(struct dvb_frontend *fe);
255static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
256static int xc4000_TunerReset(struct dvb_frontend *fe);
257
258static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
259{
260 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
261 .flags = 0, .buf = buf, .len = len };
Davide Ferri8d009a02009-06-23 22:34:06 -0300262 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
Devin Heitmueller799ed112009-10-04 23:09:18 -0300263 if (priv->ignore_i2c_write_errors == 0) {
264 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
265 len);
266 if (len == 4) {
267 printk("bytes %02x %02x %02x %02x\n", buf[0],
268 buf[1], buf[2], buf[3]);
269 }
270 return XC_RESULT_I2C_WRITE_FAILURE;
271 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300272 }
273 return XC_RESULT_SUCCESS;
274}
275
276/* This routine is never used because the only time we read data from the
277 i2c bus is when we read registers, and we want that to be an atomic i2c
278 transaction in case we are on a multi-master bus */
Davide Ferri8d009a02009-06-23 22:34:06 -0300279
280static void xc_wait(int wait_ms)
281{
282 msleep(wait_ms);
283}
284
285static int xc4000_TunerReset(struct dvb_frontend *fe)
286{
287 struct xc4000_priv *priv = fe->tuner_priv;
288 int ret;
289
290 dprintk(1, "%s()\n", __func__);
291
292 if (fe->callback) {
293 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
294 fe->dvb->priv :
295 priv->i2c_props.adap->algo_data,
296 DVB_FRONTEND_COMPONENT_TUNER,
297 XC4000_TUNER_RESET, 0);
298 if (ret) {
299 printk(KERN_ERR "xc4000: reset failed\n");
300 return XC_RESULT_RESET_FAILURE;
301 }
302 } else {
303 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
304 return XC_RESULT_RESET_FAILURE;
305 }
306 return XC_RESULT_SUCCESS;
307}
308
309static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
310{
311 u8 buf[4];
Davide Ferri8d009a02009-06-23 22:34:06 -0300312 int result;
313
314 buf[0] = (regAddr >> 8) & 0xFF;
315 buf[1] = regAddr & 0xFF;
316 buf[2] = (i2cData >> 8) & 0xFF;
317 buf[3] = i2cData & 0xFF;
318 result = xc_send_i2c_data(priv, buf, 4);
Davide Ferri8d009a02009-06-23 22:34:06 -0300319
320 return result;
321}
322
323static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
324{
325 struct xc4000_priv *priv = fe->tuner_priv;
326
327 int i, nbytes_to_send, result;
328 unsigned int len, pos, index;
329 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
330
331 index = 0;
332 while ((i2c_sequence[index] != 0xFF) ||
333 (i2c_sequence[index + 1] != 0xFF)) {
334 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
335 if (len == 0x0000) {
336 /* RESET command */
337 result = xc4000_TunerReset(fe);
338 index += 2;
339 if (result != XC_RESULT_SUCCESS)
340 return result;
341 } else if (len & 0x8000) {
342 /* WAIT command */
343 xc_wait(len & 0x7FFF);
344 index += 2;
345 } else {
346 /* Send i2c data whilst ensuring individual transactions
347 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
348 */
349 index += 2;
350 buf[0] = i2c_sequence[index];
351 buf[1] = i2c_sequence[index + 1];
352 pos = 2;
353 while (pos < len) {
354 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
355 nbytes_to_send =
356 XC_MAX_I2C_WRITE_LENGTH;
357 else
358 nbytes_to_send = (len - pos + 2);
359 for (i = 2; i < nbytes_to_send; i++) {
360 buf[i] = i2c_sequence[index + pos +
361 i - 2];
362 }
363 result = xc_send_i2c_data(priv, buf,
364 nbytes_to_send);
365
366 if (result != XC_RESULT_SUCCESS)
367 return result;
368
369 pos += nbytes_to_send - 2;
370 }
371 index += len;
372 }
373 }
374 return XC_RESULT_SUCCESS;
375}
376
Davide Ferri8d009a02009-06-23 22:34:06 -0300377static int xc_SetTVStandard(struct xc4000_priv *priv,
378 u16 VideoMode, u16 AudioMode)
379{
380 int ret;
381 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
382 dprintk(1, "%s() Standard = %s\n",
383 __func__,
384 XC4000_Standard[priv->video_standard].Name);
385
Devin Heitmueller799ed112009-10-04 23:09:18 -0300386 /* Don't complain when the request fails because of i2c stretching */
387 priv->ignore_i2c_write_errors = 1;
388
Davide Ferri8d009a02009-06-23 22:34:06 -0300389 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
390 if (ret == XC_RESULT_SUCCESS)
391 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
392
Devin Heitmueller799ed112009-10-04 23:09:18 -0300393 priv->ignore_i2c_write_errors = 0;
394
Davide Ferri8d009a02009-06-23 22:34:06 -0300395 return ret;
396}
397
398static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
399{
400 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
401 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
402
403 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
404 rf_mode = XC_RF_MODE_CABLE;
405 printk(KERN_ERR
406 "%s(), Invalid mode, defaulting to CABLE",
407 __func__);
408 }
409 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
410}
411
412static const struct dvb_tuner_ops xc4000_tuner_ops;
413
414static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
415{
416 u16 freq_code;
417
418 dprintk(1, "%s(%u)\n", __func__, freq_hz);
419
420 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
421 (freq_hz < xc4000_tuner_ops.info.frequency_min))
422 return XC_RESULT_OUT_OF_RANGE;
423
424 freq_code = (u16)(freq_hz / 15625);
425
426 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
427 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
428 only be used for fast scanning for channel lock) */
429 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
430}
431
432
Davide Ferri8d009a02009-06-23 22:34:06 -0300433static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
434{
435 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
436}
437
438static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
439{
440 int result;
441 u16 regData;
442 u32 tmp;
443
444 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
445 if (result != XC_RESULT_SUCCESS)
446 return result;
447
448 tmp = (u32)regData;
449 (*freq_error_hz) = (tmp * 15625) / 1000;
450 return result;
451}
452
453static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
454{
455 return xc4000_readreg(priv, XREG_LOCK, lock_status);
456}
457
458static int xc_get_version(struct xc4000_priv *priv,
459 u8 *hw_majorversion, u8 *hw_minorversion,
460 u8 *fw_majorversion, u8 *fw_minorversion)
461{
462 u16 data;
463 int result;
464
465 result = xc4000_readreg(priv, XREG_VERSION, &data);
466 if (result != XC_RESULT_SUCCESS)
467 return result;
468
469 (*hw_majorversion) = (data >> 12) & 0x0F;
470 (*hw_minorversion) = (data >> 8) & 0x0F;
471 (*fw_majorversion) = (data >> 4) & 0x0F;
472 (*fw_minorversion) = data & 0x0F;
473
474 return 0;
475}
476
477/* WAS THERE
478static int xc_get_buildversion(struct xc4000_priv *priv, u16 *buildrev)
479{
480 return xc4000_readreg(priv, XREG_BUILD, buildrev);
481}*/
482
483static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
484{
485 u16 regData;
486 int result;
487
488 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
489 if (result != XC_RESULT_SUCCESS)
490 return result;
491
492 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
493 return result;
494}
495
496static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
497{
498 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
499}
500
501static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
502{
503 return xc4000_readreg(priv, XREG_QUALITY, quality);
504}
505
506static u16 WaitForLock(struct xc4000_priv *priv)
507{
508 u16 lockState = 0;
509 int watchDogCount = 40;
510
511 while ((lockState == 0) && (watchDogCount > 0)) {
512 xc_get_lock_status(priv, &lockState);
513 if (lockState != 1) {
514 xc_wait(5);
515 watchDogCount--;
516 }
517 }
518 return lockState;
519}
520
521#define XC_TUNE_ANALOG 0
522#define XC_TUNE_DIGITAL 1
523static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
524{
525 int found = 0;
Devin Heitmueller799ed112009-10-04 23:09:18 -0300526 int result = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300527
528 dprintk(1, "%s(%u)\n", __func__, freq_hz);
529
Devin Heitmueller799ed112009-10-04 23:09:18 -0300530 /* Don't complain when the request fails because of i2c stretching */
531 priv->ignore_i2c_write_errors = 1;
532 result = xc_set_RF_frequency(priv, freq_hz);
533 priv->ignore_i2c_write_errors = 0;
534
535 if (result != XC_RESULT_SUCCESS)
Davide Ferri8d009a02009-06-23 22:34:06 -0300536 return 0;
537
538 if (mode == XC_TUNE_ANALOG) {
539 if (WaitForLock(priv) == 1)
540 found = 1;
541 }
542
543 return found;
544}
545
546static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
547{
548 u8 buf[2] = { reg >> 8, reg & 0xff };
549 u8 bval[2] = { 0, 0 };
550 struct i2c_msg msg[2] = {
551 { .addr = priv->i2c_props.addr,
552 .flags = 0, .buf = &buf[0], .len = 2 },
553 { .addr = priv->i2c_props.addr,
554 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
555 };
556
557 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
558 printk(KERN_WARNING "xc4000: I2C read failed\n");
559 return -EREMOTEIO;
560 }
561
562 *val = (bval[0] << 8) | bval[1];
563 return XC_RESULT_SUCCESS;
564}
565
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300566#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
567static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
568{
569 if (type & BASE)
570 printk("BASE ");
571 if (type & INIT1)
572 printk("INIT1 ");
573 if (type & F8MHZ)
574 printk("F8MHZ ");
575 if (type & MTS)
576 printk("MTS ");
577 if (type & D2620)
578 printk("D2620 ");
579 if (type & D2633)
580 printk("D2633 ");
581 if (type & DTV6)
582 printk("DTV6 ");
583 if (type & QAM)
584 printk("QAM ");
585 if (type & DTV7)
586 printk("DTV7 ");
587 if (type & DTV78)
588 printk("DTV78 ");
589 if (type & DTV8)
590 printk("DTV8 ");
591 if (type & FM)
592 printk("FM ");
593 if (type & INPUT1)
594 printk("INPUT1 ");
595 if (type & LCD)
596 printk("LCD ");
597 if (type & NOGD)
598 printk("NOGD ");
599 if (type & MONO)
600 printk("MONO ");
601 if (type & ATSC)
602 printk("ATSC ");
603 if (type & IF)
604 printk("IF ");
605 if (type & LG60)
606 printk("LG60 ");
607 if (type & ATI638)
608 printk("ATI638 ");
609 if (type & OREN538)
610 printk("OREN538 ");
611 if (type & OREN36)
612 printk("OREN36 ");
613 if (type & TOYOTA388)
614 printk("TOYOTA388 ");
615 if (type & TOYOTA794)
616 printk("TOYOTA794 ");
617 if (type & DIBCOM52)
618 printk("DIBCOM52 ");
619 if (type & ZARLINK456)
620 printk("ZARLINK456 ");
621 if (type & CHINA)
622 printk("CHINA ");
623 if (type & F6MHZ)
624 printk("F6MHZ ");
625 if (type & INPUT2)
626 printk("INPUT2 ");
627 if (type & SCODE)
628 printk("SCODE ");
629 if (type & HAS_IF)
630 printk("HAS_IF_%d ", int_freq);
631}
632
Devin Heitmueller11091a32009-07-20 00:54:57 -0300633static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
634 v4l2_std_id *id)
635{
636 struct xc4000_priv *priv = fe->tuner_priv;
637 int i, best_i = -1, best_nr_matches = 0;
638 unsigned int type_mask = 0;
639
640 printk("%s called, want type=", __func__);
641 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300642 dump_firm_type(type);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300643 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
644 }
645
646 if (!priv->firm) {
647 printk("Error! firmware not loaded\n");
648 return -EINVAL;
649 }
650
651 if (((type & ~SCODE) == 0) && (*id == 0))
652 *id = V4L2_STD_PAL;
653
654 if (type & BASE)
655 type_mask = BASE_TYPES;
656 else if (type & SCODE) {
657 type &= SCODE_TYPES;
658 type_mask = SCODE_TYPES & ~HAS_IF;
659 } else if (type & DTV_TYPES)
660 type_mask = DTV_TYPES;
661 else if (type & STD_SPECIFIC_TYPES)
662 type_mask = STD_SPECIFIC_TYPES;
663
664 type &= type_mask;
665
666 if (!(type & SCODE))
667 type_mask = ~0;
668
669 /* Seek for exact match */
670 for (i = 0; i < priv->firm_size; i++) {
671 if ((type == (priv->firm[i].type & type_mask)) &&
672 (*id == priv->firm[i].id))
673 goto found;
674 }
675
676 /* Seek for generic video standard match */
677 for (i = 0; i < priv->firm_size; i++) {
678 v4l2_std_id match_mask;
679 int nr_matches;
680
681 if (type != (priv->firm[i].type & type_mask))
682 continue;
683
684 match_mask = *id & priv->firm[i].id;
685 if (!match_mask)
686 continue;
687
688 if ((*id & match_mask) == *id)
689 goto found; /* Supports all the requested standards */
690
691 nr_matches = hweight64(match_mask);
692 if (nr_matches > best_nr_matches) {
693 best_nr_matches = nr_matches;
694 best_i = i;
695 }
696 }
697
698 if (best_nr_matches > 0) {
699 printk("Selecting best matching firmware (%d bits) for "
700 "type=", best_nr_matches);
701// dump_firm_type(type);
702 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
703 i = best_i;
704 goto found;
705 }
706
707 /*FIXME: Would make sense to seek for type "hint" match ? */
708
709 i = -ENOENT;
710 goto ret;
711
712found:
713 *id = priv->firm[i].id;
714
715ret:
716 printk("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
717 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300718 dump_firm_type(type);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300719 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300720 if (i < 0)
721 dump_stack();
Devin Heitmueller11091a32009-07-20 00:54:57 -0300722 }
723 return i;
724}
725
726static int load_firmware(struct dvb_frontend *fe, unsigned int type,
727 v4l2_std_id *id)
728{
729 struct xc4000_priv *priv = fe->tuner_priv;
730 int pos, rc;
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300731 unsigned char *p;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300732
733 printk("%s called\n", __func__);
734
735 pos = seek_firmware(fe, type, id);
736 if (pos < 0)
737 return pos;
738
739 printk("Loading firmware for type=");
740// dump_firm_type(priv->firm[pos].type);
741 printk("(%x), id %016llx.\n", priv->firm[pos].type,
742 (unsigned long long)*id);
743
744 p = priv->firm[pos].ptr;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300745 printk("firmware length = %d\n", priv->firm[pos].size);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300746
Devin Heitmueller799ed112009-10-04 23:09:18 -0300747 /* Don't complain when the request fails because of i2c stretching */
748 priv->ignore_i2c_write_errors = 1;
749
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300750 rc = xc_load_i2c_sequence(fe, p);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300751
Devin Heitmueller799ed112009-10-04 23:09:18 -0300752 priv->ignore_i2c_write_errors = 0;
753
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300754 return rc;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300755}
756
Davide Ferri8d009a02009-06-23 22:34:06 -0300757static int xc4000_fwupload(struct dvb_frontend *fe)
758{
759 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300760 const struct firmware *fw = NULL;
761 const unsigned char *p, *endp;
762 int rc = 0;
763 int n, n_array;
764 char name[33];
765 char *fname;
Davide Ferri8d009a02009-06-23 22:34:06 -0300766
Devin Heitmueller11091a32009-07-20 00:54:57 -0300767 printk("%s called\n", __func__);
Davide Ferri8d009a02009-06-23 22:34:06 -0300768
Devin Heitmueller11091a32009-07-20 00:54:57 -0300769 fname = XC4000_DEFAULT_FIRMWARE;
770
771 printk("Reading firmware %s\n", fname);
772 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
773 if (rc < 0) {
774 if (rc == -ENOENT)
775 printk("Error: firmware %s not found.\n",
776 fname);
777 else
778 printk("Error %d while requesting firmware %s \n",
779 rc, fname);
780
781 return rc;
782 }
783 p = fw->data;
784 endp = p + fw->size;
785
786 if (fw->size < sizeof(name) - 1 + 2 + 2) {
787 printk("Error: firmware file %s has invalid size!\n",
788 fname);
789 goto corrupt;
Davide Ferri8d009a02009-06-23 22:34:06 -0300790 }
791
Devin Heitmueller11091a32009-07-20 00:54:57 -0300792 memcpy(name, p, sizeof(name) - 1);
793 name[sizeof(name) - 1] = 0;
794 p += sizeof(name) - 1;
795
796 priv->firm_version = get_unaligned_le16(p);
797 p += 2;
798
799 n_array = get_unaligned_le16(p);
800 p += 2;
801
802 printk("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
803 n_array, fname, name,
804 priv->firm_version >> 8, priv->firm_version & 0xff);
805
806 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
807 if (priv->firm == NULL) {
808 printk("Not enough memory to load firmware file.\n");
809 rc = -ENOMEM;
810 goto err;
811 }
812 priv->firm_size = n_array;
813
814 n = -1;
815 while (p < endp) {
816 __u32 type, size;
817 v4l2_std_id id;
818 __u16 int_freq = 0;
819
820 n++;
821 if (n >= n_array) {
822 printk("More firmware images in file than "
823 "were expected!\n");
824 goto corrupt;
825 }
826
827 /* Checks if there's enough bytes to read */
828 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
829 goto header;
830
831 type = get_unaligned_le32(p);
832 p += sizeof(type);
833
834 id = get_unaligned_le64(p);
835 p += sizeof(id);
836
837 if (type & HAS_IF) {
838 int_freq = get_unaligned_le16(p);
839 p += sizeof(int_freq);
840 if (endp - p < sizeof(size))
841 goto header;
842 }
843
844 size = get_unaligned_le32(p);
845 p += sizeof(size);
846
847 if (!size || size > endp - p) {
848 printk("Firmware type ");
849// dump_firm_type(type);
850 printk("(%x), id %llx is corrupted "
851 "(size=%d, expected %d)\n",
852 type, (unsigned long long)id,
853 (unsigned)(endp - p), size);
854 goto corrupt;
855 }
856
857 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
858 if (priv->firm[n].ptr == NULL) {
859 printk("Not enough memory to load firmware file.\n");
860 rc = -ENOMEM;
861 goto err;
862 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300863
Devin Heitmueller11091a32009-07-20 00:54:57 -0300864 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300865 printk("Reading firmware type ");
866 dump_firm_type_and_int_freq(type, int_freq);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300867 printk("(%x), id %llx, size=%d.\n",
868 type, (unsigned long long)id, size);
869 }
870
871 memcpy(priv->firm[n].ptr, p, size);
872 priv->firm[n].type = type;
873 priv->firm[n].id = id;
874 priv->firm[n].size = size;
875 priv->firm[n].int_freq = int_freq;
876
877 p += size;
Davide Ferri8d009a02009-06-23 22:34:06 -0300878 }
879
Devin Heitmueller11091a32009-07-20 00:54:57 -0300880 if (n + 1 != priv->firm_size) {
881 printk("Firmware file is incomplete!\n");
882 goto corrupt;
883 }
884
885 goto done;
886
887header:
888 printk("Firmware header is incomplete!\n");
889corrupt:
890 rc = -EINVAL;
891 printk("Error: firmware file is corrupted!\n");
892
893err:
894 printk("Releasing partially loaded firmware file.\n");
895// free_firmware(priv);
896
897done:
Davide Ferri8d009a02009-06-23 22:34:06 -0300898 release_firmware(fw);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300899 if (rc == 0)
900 printk("Firmware files loaded.\n");
901
902 return rc;
Davide Ferri8d009a02009-06-23 22:34:06 -0300903}
904
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300905static int load_scode(struct dvb_frontend *fe, unsigned int type,
906 v4l2_std_id *id, __u16 int_freq, int scode)
907{
908 struct xc4000_priv *priv = fe->tuner_priv;
909 int pos, rc;
910 unsigned char *p;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300911 u8 scode_buf[13];
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300912 u8 indirect_mode[5];
913
Devin Heitmuellerfe830362009-07-28 00:04:27 -0300914 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300915
916 if (!int_freq) {
917 pos = seek_firmware(fe, type, id);
918 if (pos < 0)
919 return pos;
920 } else {
921 for (pos = 0; pos < priv->firm_size; pos++) {
922 if ((priv->firm[pos].int_freq == int_freq) &&
923 (priv->firm[pos].type & HAS_IF))
924 break;
925 }
926 if (pos == priv->firm_size)
927 return -ENOENT;
928 }
929
930 p = priv->firm[pos].ptr;
931
932 if (priv->firm[pos].type & HAS_IF) {
933 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
934 return -EINVAL;
935 p += 12 * scode;
936 } else {
937 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
938 * has a 2-byte size header in the firmware format. */
939 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
940 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
941 return -EINVAL;
942 p += 14 * scode + 2;
943 }
944
945 tuner_info("Loading SCODE for type=");
946 dump_firm_type_and_int_freq(priv->firm[pos].type,
947 priv->firm[pos].int_freq);
948 printk("(%x), id %016llx.\n", priv->firm[pos].type,
949 (unsigned long long)*id);
950
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300951 scode_buf[0] = 0x00;
952 memcpy(&scode_buf[1], p, 12);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300953
954 /* Enter direct-mode */
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300955 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
956 if (rc < 0) {
957 printk("failed to put device into direct mode!\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300958 return -EIO;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300959 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300960
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300961 rc = xc_send_i2c_data(priv, scode_buf, 13);
962 if (rc != XC_RESULT_SUCCESS) {
963 /* Even if the send failed, make sure we set back to indirect
964 mode */
965 printk("Failed to set scode %d\n", rc);
966 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300967
968 /* Switch back to indirect-mode */
969 memset(indirect_mode, 0, sizeof(indirect_mode));
970 indirect_mode[4] = 0x88;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300971 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
972 msleep(10);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300973
974 return 0;
975}
976
977static int check_firmware(struct dvb_frontend *fe, unsigned int type,
978 v4l2_std_id std, __u16 int_freq)
979{
980 struct xc4000_priv *priv = fe->tuner_priv;
981 struct firmware_properties new_fw;
982 int rc = 0, is_retry = 0;
983 u16 version, hwmodel;
984 v4l2_std_id std0;
Devin Heitmueller799ed112009-10-04 23:09:18 -0300985 u8 hw_major, hw_minor, fw_major, fw_minor;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300986
987 dprintk(1, "%s called\n", __func__);
988
989 if (!priv->firm) {
990 rc = xc4000_fwupload(fe);
991 if (rc < 0)
992 return rc;
993 }
994
995#ifdef DJH_DEBUG
996 if (priv->ctrl.mts && !(type & FM))
997 type |= MTS;
998#endif
999
1000retry:
1001 new_fw.type = type;
1002 new_fw.id = std;
1003 new_fw.std_req = std;
1004// new_fw.scode_table = SCODE | priv->ctrl.scode_table;
1005 new_fw.scode_table = SCODE;
1006 new_fw.scode_nr = 0;
1007 new_fw.int_freq = int_freq;
1008
1009 dprintk(1, "checking firmware, user requested type=");
1010 if (debug) {
1011 dump_firm_type(new_fw.type);
1012 printk("(%x), id %016llx, ", new_fw.type,
1013 (unsigned long long)new_fw.std_req);
1014 if (!int_freq) {
1015 printk("scode_tbl ");
1016#ifdef DJH_DEBUG
1017 dump_firm_type(priv->ctrl.scode_table);
1018 printk("(%x), ", priv->ctrl.scode_table);
1019#endif
1020 } else
1021 printk("int_freq %d, ", new_fw.int_freq);
1022 printk("scode_nr %d\n", new_fw.scode_nr);
1023 }
1024
1025 /* No need to reload base firmware if it matches */
1026 if (((BASE | new_fw.type) & BASE_TYPES) ==
1027 (priv->cur_fw.type & BASE_TYPES)) {
1028 dprintk(1, "BASE firmware not changed.\n");
1029 goto skip_base;
1030 }
1031
1032 /* Updating BASE - forget about all currently loaded firmware */
1033 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1034
1035 /* Reset is needed before loading firmware */
1036 rc = xc4000_TunerReset(fe);
1037 if (rc < 0)
1038 goto fail;
1039
1040 /* BASE firmwares are all std0 */
1041 std0 = 0;
1042 rc = load_firmware(fe, BASE | new_fw.type, &std0);
1043 if (rc < 0) {
1044 printk("Error %d while loading base firmware\n", rc);
1045 goto fail;
1046 }
1047
1048 /* Load INIT1, if needed */
1049 dprintk(1, "Load init1 firmware, if exists\n");
1050
1051 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1052 if (rc == -ENOENT)
1053 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
1054 &std0);
1055 if (rc < 0 && rc != -ENOENT) {
1056 tuner_err("Error %d while loading init1 firmware\n",
1057 rc);
1058 goto fail;
1059 }
1060
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001061 printk("Done with init1\n");
1062
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001063skip_base:
1064 /*
1065 * No need to reload standard specific firmware if base firmware
1066 * was not reloaded and requested video standards have not changed.
1067 */
1068 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1069 priv->cur_fw.std_req == std) {
1070 dprintk(1, "Std-specific firmware already loaded.\n");
1071 goto skip_std_specific;
1072 }
1073
1074 /* Reloading std-specific firmware forces a SCODE update */
1075 priv->cur_fw.scode_table = 0;
1076
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001077 /* Load the standard firmware */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001078 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001079
1080 if (rc < 0)
1081 goto fail;
1082
1083skip_std_specific:
1084 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1085 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1086 dprintk(1, "SCODE firmware already loaded.\n");
1087 goto check_device;
1088 }
1089
1090 if (new_fw.type & FM)
1091 goto check_device;
1092
1093 /* Load SCODE firmware, if exists */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001094 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1095 new_fw.int_freq, new_fw.scode_nr);
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001096 if (rc != XC_RESULT_SUCCESS)
1097 dprintk(1, "load scode failed %d\n", rc);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001098
1099check_device:
1100 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1101
Devin Heitmueller799ed112009-10-04 23:09:18 -03001102 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001103 &fw_minor) != XC_RESULT_SUCCESS) {
1104 printk("Unable to read tuner registers.\n");
1105 goto fail;
1106 }
1107
1108 dprintk(1, "Device is Xceive %d version %d.%d, "
1109 "firmware version %d.%d\n",
1110 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1111
1112 /* Check firmware version against what we downloaded. */
1113#ifdef DJH_DEBUG
1114 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1115 printk("Incorrect readback of firmware version %x.\n",
1116 (version & 0xff));
1117 goto fail;
1118 }
1119#endif
1120
1121 /* Check that the tuner hardware model remains consistent over time. */
1122 if (priv->hwmodel == 0 && hwmodel == 4000) {
1123 priv->hwmodel = hwmodel;
1124 priv->hwvers = version & 0xff00;
1125 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1126 priv->hwvers != (version & 0xff00)) {
1127 printk("Read invalid device hardware information - tuner "
1128 "hung?\n");
1129 goto fail;
1130 }
1131
1132 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1133
1134 /*
1135 * By setting BASE in cur_fw.type only after successfully loading all
1136 * firmwares, we can:
1137 * 1. Identify that BASE firmware with type=0 has been loaded;
1138 * 2. Tell whether BASE firmware was just changed the next time through.
1139 */
1140 priv->cur_fw.type |= BASE;
1141
1142 return 0;
1143
1144fail:
1145 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1146 if (!is_retry) {
1147 msleep(50);
1148 is_retry = 1;
1149 dprintk(1, "Retrying firmware load\n");
1150 goto retry;
1151 }
1152
1153 if (rc == -ENOENT)
1154 rc = -EINVAL;
1155 return rc;
1156}
Devin Heitmueller11091a32009-07-20 00:54:57 -03001157
Davide Ferri8d009a02009-06-23 22:34:06 -03001158static void xc_debug_dump(struct xc4000_priv *priv)
1159{
1160 u16 adc_envelope;
1161 u32 freq_error_hz = 0;
1162 u16 lock_status;
1163 u32 hsync_freq_hz = 0;
1164 u16 frame_lines;
1165 u16 quality;
1166 u8 hw_majorversion = 0, hw_minorversion = 0;
1167 u8 fw_majorversion = 0, fw_minorversion = 0;
1168// u16 fw_buildversion = 0;
1169
1170 /* Wait for stats to stabilize.
1171 * Frame Lines needs two frame times after initial lock
1172 * before it is valid.
1173 */
1174 xc_wait(100);
1175
1176 xc_get_ADC_Envelope(priv, &adc_envelope);
1177 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1178
1179 xc_get_frequency_error(priv, &freq_error_hz);
1180 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1181
1182 xc_get_lock_status(priv, &lock_status);
1183 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1184 lock_status);
1185
1186 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1187 &fw_majorversion, &fw_minorversion);
1188// WAS:
1189// xc_get_buildversion(priv, &fw_buildversion);
1190// dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
1191// hw_majorversion, hw_minorversion,
1192// fw_majorversion, fw_minorversion, fw_buildversion);
1193// NOW:
1194 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1195 hw_majorversion, hw_minorversion,
1196 fw_majorversion, fw_minorversion);
1197
1198 xc_get_hsync_freq(priv, &hsync_freq_hz);
1199 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1200
1201 xc_get_frame_lines(priv, &frame_lines);
1202 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1203
1204 xc_get_quality(priv, &quality);
1205 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1206}
1207
1208static int xc4000_set_params(struct dvb_frontend *fe,
1209 struct dvb_frontend_parameters *params)
1210{
1211 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001212 unsigned int type;
Davide Ferri8d009a02009-06-23 22:34:06 -03001213 int ret;
1214
Davide Ferri8d009a02009-06-23 22:34:06 -03001215 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1216
1217 if (fe->ops.info.type == FE_ATSC) {
1218 dprintk(1, "%s() ATSC\n", __func__);
1219 switch (params->u.vsb.modulation) {
1220 case VSB_8:
1221 case VSB_16:
1222 dprintk(1, "%s() VSB modulation\n", __func__);
1223 priv->rf_mode = XC_RF_MODE_AIR;
1224 priv->freq_hz = params->frequency - 1750000;
1225 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001226 priv->video_standard = XC4000_DTV6;
1227 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001228 break;
1229 case QAM_64:
1230 case QAM_256:
1231 case QAM_AUTO:
1232 dprintk(1, "%s() QAM modulation\n", __func__);
1233 priv->rf_mode = XC_RF_MODE_CABLE;
1234 priv->freq_hz = params->frequency - 1750000;
1235 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001236 priv->video_standard = XC4000_DTV6;
1237 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001238 break;
1239 default:
1240 return -EINVAL;
1241 }
1242 } else if (fe->ops.info.type == FE_OFDM) {
1243 dprintk(1, "%s() OFDM\n", __func__);
1244 switch (params->u.ofdm.bandwidth) {
1245 case BANDWIDTH_6_MHZ:
1246 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001247 priv->video_standard = XC4000_DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001248 priv->freq_hz = params->frequency - 1750000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001249 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001250 break;
1251 case BANDWIDTH_7_MHZ:
1252 printk(KERN_ERR "xc4000 bandwidth 7MHz not supported\n");
Devin Heitmuellered23db32009-10-05 01:27:14 -03001253 type = DTV7;
Davide Ferri8d009a02009-06-23 22:34:06 -03001254 return -EINVAL;
1255 case BANDWIDTH_8_MHZ:
1256 priv->bandwidth = BANDWIDTH_8_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001257 priv->video_standard = XC4000_DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001258 priv->freq_hz = params->frequency - 2750000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001259 type = DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001260 break;
1261 default:
1262 printk(KERN_ERR "xc4000 bandwidth not set!\n");
1263 return -EINVAL;
1264 }
1265 priv->rf_mode = XC_RF_MODE_AIR;
1266 } else {
1267 printk(KERN_ERR "xc4000 modulation type not supported!\n");
1268 return -EINVAL;
1269 }
1270
1271 dprintk(1, "%s() frequency=%d (compensated)\n",
1272 __func__, priv->freq_hz);
1273
Devin Heitmuellered23db32009-10-05 01:27:14 -03001274 /* Make sure the correct firmware type is loaded */
1275 if (check_firmware(fe, type, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
1276 return -EREMOTEIO;
1277 }
1278
Davide Ferri8d009a02009-06-23 22:34:06 -03001279 ret = xc_SetSignalSource(priv, priv->rf_mode);
1280 if (ret != XC_RESULT_SUCCESS) {
1281 printk(KERN_ERR
1282 "xc4000: xc_SetSignalSource(%d) failed\n",
1283 priv->rf_mode);
1284 return -EREMOTEIO;
1285 }
1286
1287 ret = xc_SetTVStandard(priv,
1288 XC4000_Standard[priv->video_standard].VideoMode,
1289 XC4000_Standard[priv->video_standard].AudioMode);
1290 if (ret != XC_RESULT_SUCCESS) {
1291 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1292 return -EREMOTEIO;
1293 }
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001294#ifdef DJH_DEBUG
Davide Ferri8d009a02009-06-23 22:34:06 -03001295 ret = xc_set_IF_frequency(priv, priv->if_khz);
1296 if (ret != XC_RESULT_SUCCESS) {
1297 printk(KERN_ERR "xc4000: xc_Set_IF_frequency(%d) failed\n",
1298 priv->if_khz);
1299 return -EIO;
1300 }
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001301#endif
Davide Ferri8d009a02009-06-23 22:34:06 -03001302 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1303
1304 if (debug)
1305 xc_debug_dump(priv);
1306
1307 return 0;
1308}
1309
1310static int xc4000_is_firmware_loaded(struct dvb_frontend *fe)
1311{
1312 struct xc4000_priv *priv = fe->tuner_priv;
1313 int ret;
1314 u16 id;
1315
1316 ret = xc4000_readreg(priv, XREG_PRODUCT_ID, &id);
1317 if (ret == XC_RESULT_SUCCESS) {
1318 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
1319 ret = XC_RESULT_RESET_FAILURE;
1320 else
1321 ret = XC_RESULT_SUCCESS;
1322 }
1323
1324 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
1325 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
1326 return ret;
1327}
1328
1329static int xc4000_set_analog_params(struct dvb_frontend *fe,
1330 struct analog_parameters *params)
1331{
1332 struct xc4000_priv *priv = fe->tuner_priv;
1333 int ret;
1334
Davide Ferri8d009a02009-06-23 22:34:06 -03001335 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1336 __func__, params->frequency);
1337
1338 /* Fix me: it could be air. */
1339 priv->rf_mode = params->mode;
1340 if (params->mode > XC_RF_MODE_CABLE)
1341 priv->rf_mode = XC_RF_MODE_CABLE;
1342
1343 /* params->frequency is in units of 62.5khz */
1344 priv->freq_hz = params->frequency * 62500;
1345
1346 /* FIX ME: Some video standards may have several possible audio
1347 standards. We simply default to one of them here.
1348 */
1349 if (params->std & V4L2_STD_MN) {
1350 /* default to BTSC audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001351 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
Davide Ferri8d009a02009-06-23 22:34:06 -03001352 goto tune_channel;
1353 }
1354
1355 if (params->std & V4L2_STD_PAL_BG) {
1356 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001357 priv->video_standard = XC4000_BG_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001358 goto tune_channel;
1359 }
1360
1361 if (params->std & V4L2_STD_PAL_I) {
1362 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001363 priv->video_standard = XC4000_I_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001364 goto tune_channel;
1365 }
1366
1367 if (params->std & V4L2_STD_PAL_DK) {
1368 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001369 priv->video_standard = XC4000_DK_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001370 goto tune_channel;
1371 }
1372
1373 if (params->std & V4L2_STD_SECAM_DK) {
1374 /* default to A2 DK1 audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001375 priv->video_standard = XC4000_DK_SECAM_A2DK1;
Davide Ferri8d009a02009-06-23 22:34:06 -03001376 goto tune_channel;
1377 }
1378
1379 if (params->std & V4L2_STD_SECAM_L) {
Devin Heitmuellered23db32009-10-05 01:27:14 -03001380 priv->video_standard = XC4000_L_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001381 goto tune_channel;
1382 }
1383
1384 if (params->std & V4L2_STD_SECAM_LC) {
Devin Heitmuellered23db32009-10-05 01:27:14 -03001385 priv->video_standard = XC4000_LC_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001386 goto tune_channel;
1387 }
1388
1389tune_channel:
Devin Heitmuellered23db32009-10-05 01:27:14 -03001390
1391 /* FIXME - firmware type not being set properly */
1392 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
1393 return -EREMOTEIO;
1394 }
1395
Davide Ferri8d009a02009-06-23 22:34:06 -03001396 ret = xc_SetSignalSource(priv, priv->rf_mode);
1397 if (ret != XC_RESULT_SUCCESS) {
1398 printk(KERN_ERR
1399 "xc4000: xc_SetSignalSource(%d) failed\n",
1400 priv->rf_mode);
1401 return -EREMOTEIO;
1402 }
1403
1404 ret = xc_SetTVStandard(priv,
1405 XC4000_Standard[priv->video_standard].VideoMode,
1406 XC4000_Standard[priv->video_standard].AudioMode);
1407 if (ret != XC_RESULT_SUCCESS) {
1408 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
1409 return -EREMOTEIO;
1410 }
1411
1412 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1413
1414 if (debug)
1415 xc_debug_dump(priv);
1416
1417 return 0;
1418}
1419
1420static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1421{
1422 struct xc4000_priv *priv = fe->tuner_priv;
1423 dprintk(1, "%s()\n", __func__);
1424 *freq = priv->freq_hz;
1425 return 0;
1426}
1427
1428static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1429{
1430 struct xc4000_priv *priv = fe->tuner_priv;
1431 dprintk(1, "%s()\n", __func__);
1432
1433 *bw = priv->bandwidth;
1434 return 0;
1435}
1436
1437static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1438{
1439 struct xc4000_priv *priv = fe->tuner_priv;
1440 u16 lock_status = 0;
1441
1442 xc_get_lock_status(priv, &lock_status);
1443
1444 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1445
1446 *status = lock_status;
1447
1448 return 0;
1449}
1450
Davide Ferri8d009a02009-06-23 22:34:06 -03001451static int xc4000_sleep(struct dvb_frontend *fe)
1452{
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001453 /* FIXME: djh disable this for now... */
1454 return XC_RESULT_SUCCESS;
Davide Ferri8d009a02009-06-23 22:34:06 -03001455}
1456
1457static int xc4000_init(struct dvb_frontend *fe)
1458{
1459 struct xc4000_priv *priv = fe->tuner_priv;
1460 dprintk(1, "%s()\n", __func__);
1461
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001462 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS) {
Davide Ferri8d009a02009-06-23 22:34:06 -03001463 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1464 return -EREMOTEIO;
1465 }
1466
1467 if (debug)
1468 xc_debug_dump(priv);
1469
1470 return 0;
1471}
1472
1473static int xc4000_release(struct dvb_frontend *fe)
1474{
1475 struct xc4000_priv *priv = fe->tuner_priv;
1476
1477 dprintk(1, "%s()\n", __func__);
1478
1479 mutex_lock(&xc4000_list_mutex);
1480
1481 if (priv)
1482 hybrid_tuner_release_state(priv);
1483
1484 mutex_unlock(&xc4000_list_mutex);
1485
1486 fe->tuner_priv = NULL;
1487
1488 return 0;
1489}
1490
1491static const struct dvb_tuner_ops xc4000_tuner_ops = {
1492 .info = {
1493 .name = "Xceive XC4000",
1494 .frequency_min = 1000000,
1495 .frequency_max = 1023000000,
1496 .frequency_step = 50000,
1497 },
1498
1499 .release = xc4000_release,
1500 .init = xc4000_init,
1501 .sleep = xc4000_sleep,
1502
1503 .set_params = xc4000_set_params,
1504 .set_analog_params = xc4000_set_analog_params,
1505 .get_frequency = xc4000_get_frequency,
1506 .get_bandwidth = xc4000_get_bandwidth,
1507 .get_status = xc4000_get_status
1508};
1509
1510struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1511 struct i2c_adapter *i2c,
1512 struct xc4000_config *cfg)
1513{
1514 struct xc4000_priv *priv = NULL;
1515 int instance;
1516 u16 id = 0;
1517
1518 dprintk(1, "%s(%d-%04x)\n", __func__,
1519 i2c ? i2c_adapter_id(i2c) : -1,
1520 cfg ? cfg->i2c_address : -1);
1521
1522 mutex_lock(&xc4000_list_mutex);
1523
1524 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1525 hybrid_tuner_instance_list,
1526 i2c, cfg->i2c_address, "xc4000");
1527 switch (instance) {
1528 case 0:
1529 goto fail;
1530 break;
1531 case 1:
1532 /* new tuner instance */
1533 priv->bandwidth = BANDWIDTH_6_MHZ;
1534 fe->tuner_priv = priv;
1535 break;
1536 default:
1537 /* existing tuner instance */
1538 fe->tuner_priv = priv;
1539 break;
1540 }
1541
1542 if (priv->if_khz == 0) {
1543 /* If the IF hasn't been set yet, use the value provided by
1544 the caller (occurs in hybrid devices where the analog
1545 call to xc4000_attach occurs before the digital side) */
1546 priv->if_khz = cfg->if_khz;
1547 }
1548
1549 /* Check if firmware has been loaded. It is possible that another
1550 instance of the driver has loaded the firmware.
1551 */
1552
1553 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1554 goto fail;
1555
1556 switch (id) {
1557 case XC_PRODUCT_ID_FW_LOADED:
1558 printk(KERN_INFO
1559 "xc4000: Successfully identified at address 0x%02x\n",
1560 cfg->i2c_address);
1561 printk(KERN_INFO
1562 "xc4000: Firmware has been loaded previously\n");
1563 break;
1564 case XC_PRODUCT_ID_FW_NOT_LOADED:
1565 printk(KERN_INFO
1566 "xc4000: Successfully identified at address 0x%02x\n",
1567 cfg->i2c_address);
1568 printk(KERN_INFO
1569 "xc4000: Firmware has not been loaded previously\n");
1570 break;
1571 default:
1572 printk(KERN_ERR
1573 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1574 cfg->i2c_address, id);
1575 goto fail;
1576 }
1577
1578 mutex_unlock(&xc4000_list_mutex);
1579
1580 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1581 sizeof(struct dvb_tuner_ops));
1582
Devin Heitmueller11091a32009-07-20 00:54:57 -03001583 /* FIXME: For now, load the firmware at startup. We will remove this
1584 before the code goes to production... */
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001585 check_firmware(fe, DTV8, 0, priv->if_khz);
Devin Heitmueller11091a32009-07-20 00:54:57 -03001586
Davide Ferri8d009a02009-06-23 22:34:06 -03001587 return fe;
1588fail:
1589 mutex_unlock(&xc4000_list_mutex);
1590
1591 xc4000_release(fe);
1592 return NULL;
1593}
1594EXPORT_SYMBOL(xc4000_attach);
1595
1596MODULE_AUTHOR("Steven Toth, Davide Ferri");
1597MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1598MODULE_LICENSE("GPL");