blob: 811519a49f59c0706ca9ceae6a1a89993e421788 [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>
Istvan Varga56149422011-06-03 12:23:33 -030031#include <linux/mutex.h>
Devin Heitmueller11091a32009-07-20 00:54:57 -030032#include <asm/unaligned.h>
Davide Ferri8d009a02009-06-23 22:34:06 -030033
34#include "dvb_frontend.h"
35
36#include "xc4000.h"
37#include "tuner-i2c.h"
Devin Heitmueller11091a32009-07-20 00:54:57 -030038#include "tuner-xc2028-types.h"
Davide Ferri8d009a02009-06-23 22:34:06 -030039
Devin Heitmueller4922cec2009-12-27 17:50:43 -030040static int debug;
Davide Ferri8d009a02009-06-23 22:34:06 -030041module_param(debug, int, 0644);
42MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
43
44static int no_poweroff;
45module_param(no_poweroff, int, 0644);
46MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
47 "\t\t1 keep device energized and with tuner ready all the times.\n"
48 "\t\tFaster, but consumes more power and keeps the device hotter");
49
Istvan Vargafa285bc2011-06-04 11:48:16 -030050#define XC4000_DEFAULT_FIRMWARE "xc4000.fw"
51
52static char firmware_name[30];
53module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
54MODULE_PARM_DESC(firmware_name, "\n\t\tFirmware file name. Allows overriding "
55 "the default firmware\n"
56 "\t\tname.");
57
Davide Ferri8d009a02009-06-23 22:34:06 -030058static DEFINE_MUTEX(xc4000_list_mutex);
59static LIST_HEAD(hybrid_tuner_instance_list);
60
61#define dprintk(level, fmt, arg...) if (debug >= level) \
62 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
63
Devin Heitmueller11091a32009-07-20 00:54:57 -030064/* struct for storing firmware table */
65struct firmware_description {
66 unsigned int type;
67 v4l2_std_id id;
68 __u16 int_freq;
69 unsigned char *ptr;
70 unsigned int size;
71};
72
73struct firmware_properties {
74 unsigned int type;
75 v4l2_std_id id;
76 v4l2_std_id std_req;
77 __u16 int_freq;
78 unsigned int scode_table;
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -030079 int scode_nr;
Devin Heitmueller11091a32009-07-20 00:54:57 -030080};
Davide Ferri8d009a02009-06-23 22:34:06 -030081
82struct xc4000_priv {
83 struct tuner_i2c_props i2c_props;
84 struct list_head hybrid_tuner_instance_list;
Devin Heitmueller11091a32009-07-20 00:54:57 -030085 struct firmware_description *firm;
Istvan Vargafbe4a292011-06-03 10:11:48 -030086 int firm_size;
87 __u16 firm_version;
88 u32 if_khz;
89 u32 freq_hz;
90 u32 bandwidth;
91 u8 video_standard;
92 u8 rf_mode;
93 u8 ignore_i2c_write_errors;
94 /* struct xc2028_ctrl ctrl; */
Devin Heitmuellerd0962382009-07-25 17:39:54 -030095 struct firmware_properties cur_fw;
Istvan Vargafbe4a292011-06-03 10:11:48 -030096 __u16 hwmodel;
97 __u16 hwvers;
Istvan Varga56149422011-06-03 12:23:33 -030098 struct mutex lock;
Davide Ferri8d009a02009-06-23 22:34:06 -030099};
100
101/* Misc Defines */
Istvan Varga49110852011-06-03 10:55:24 -0300102#define MAX_TV_STANDARD 24
Davide Ferri8d009a02009-06-23 22:34:06 -0300103#define XC_MAX_I2C_WRITE_LENGTH 64
104
105/* Signal Types */
106#define XC_RF_MODE_AIR 0
107#define XC_RF_MODE_CABLE 1
108
109/* Result codes */
110#define XC_RESULT_SUCCESS 0
111#define XC_RESULT_RESET_FAILURE 1
112#define XC_RESULT_I2C_WRITE_FAILURE 2
113#define XC_RESULT_I2C_READ_FAILURE 3
114#define XC_RESULT_OUT_OF_RANGE 5
115
116/* Product id */
117#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300118#define XC_PRODUCT_ID_FW_LOADED 0x0FA0
Davide Ferri8d009a02009-06-23 22:34:06 -0300119
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300120/* Registers (Write-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300121#define XREG_INIT 0x00
122#define XREG_VIDEO_MODE 0x01
123#define XREG_AUDIO_MODE 0x02
124#define XREG_RF_FREQ 0x03
125#define XREG_D_CODE 0x04
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300126#define XREG_DIRECTSITTING_MODE 0x05
127#define XREG_SEEK_MODE 0x06
128#define XREG_POWER_DOWN 0x08
129#define XREG_SIGNALSOURCE 0x0A
130#define XREG_AMPLITUDE 0x10
Davide Ferri8d009a02009-06-23 22:34:06 -0300131
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300132/* Registers (Read-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300133#define XREG_ADC_ENV 0x00
134#define XREG_QUALITY 0x01
135#define XREG_FRAME_LINES 0x02
136#define XREG_HSYNC_FREQ 0x03
137#define XREG_LOCK 0x04
138#define XREG_FREQ_ERROR 0x05
139#define XREG_SNR 0x06
140#define XREG_VERSION 0x07
141#define XREG_PRODUCT_ID 0x08
Davide Ferri8d009a02009-06-23 22:34:06 -0300142
143/*
144 Basic firmware description. This will remain with
145 the driver for documentation purposes.
146
147 This represents an I2C firmware file encoded as a
148 string of unsigned char. Format is as follows:
149
150 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
151 char[1 ]=len0_LSB -> length of first write transaction
152 char[2 ]=data0 -> first byte to be sent
153 char[3 ]=data1
154 char[4 ]=data2
155 char[ ]=...
156 char[M ]=dataN -> last byte to be sent
157 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
158 char[M+2]=len1_LSB -> length of second write transaction
159 char[M+3]=data0
160 char[M+4]=data1
161 ...
162 etc.
163
164 The [len] value should be interpreted as follows:
165
166 len= len_MSB _ len_LSB
167 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
168 len=0000_0000_0000_0000 : Reset command: Do hardware reset
169 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
170 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
171
172 For the RESET and WAIT commands, the two following bytes will contain
173 immediately the length of the following transaction.
Davide Ferri8d009a02009-06-23 22:34:06 -0300174*/
Istvan Vargafbe4a292011-06-03 10:11:48 -0300175
Davide Ferri8d009a02009-06-23 22:34:06 -0300176struct XC_TV_STANDARD {
Istvan Vargafbe4a292011-06-03 10:11:48 -0300177 const char *Name;
178 u16 AudioMode;
179 u16 VideoMode;
Istvan Varga49110852011-06-03 10:55:24 -0300180 u16 int_freq;
Davide Ferri8d009a02009-06-23 22:34:06 -0300181};
182
183/* Tuner standards */
Devin Heitmuellered23db32009-10-05 01:27:14 -0300184#define XC4000_MN_NTSC_PAL_BTSC 0
185#define XC4000_MN_NTSC_PAL_A2 1
186#define XC4000_MN_NTSC_PAL_EIAJ 2
187#define XC4000_MN_NTSC_PAL_Mono 3
188#define XC4000_BG_PAL_A2 4
189#define XC4000_BG_PAL_NICAM 5
190#define XC4000_BG_PAL_MONO 6
191#define XC4000_I_PAL_NICAM 7
192#define XC4000_I_PAL_NICAM_MONO 8
193#define XC4000_DK_PAL_A2 9
194#define XC4000_DK_PAL_NICAM 10
195#define XC4000_DK_PAL_MONO 11
196#define XC4000_DK_SECAM_A2DK1 12
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300197#define XC4000_DK_SECAM_A2LDK3 13
198#define XC4000_DK_SECAM_A2MONO 14
Istvan Varga49110852011-06-03 10:55:24 -0300199#define XC4000_DK_SECAM_NICAM 15
200#define XC4000_L_SECAM_NICAM 16
201#define XC4000_LC_SECAM_NICAM 17
202#define XC4000_DTV6 18
203#define XC4000_DTV8 19
204#define XC4000_DTV7_8 20
205#define XC4000_DTV7 21
206#define XC4000_FM_Radio_INPUT2 22
207#define XC4000_FM_Radio_INPUT1 23
Davide Ferri8d009a02009-06-23 22:34:06 -0300208
Davide Ferri8d009a02009-06-23 22:34:06 -0300209static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
Istvan Varga49110852011-06-03 10:55:24 -0300210 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
211 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
212 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
213 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
214 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
215 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
216 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
217 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
218 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
219 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
220 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
221 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
222 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
223 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
224 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
225 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
226 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
227 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
228 {"DTV6", 0x00C0, 0x8002, 0},
229 {"DTV8", 0x00C0, 0x800B, 0},
230 {"DTV7/8", 0x00C0, 0x801B, 0},
231 {"DTV7", 0x00C0, 0x8007, 0},
232 {"FM Radio-INPUT2", 0x0008, 0x9800,10700},
233 {"FM Radio-INPUT1", 0x0008, 0x9000,10700}
Davide Ferri8d009a02009-06-23 22:34:06 -0300234};
235
Davide Ferri8d009a02009-06-23 22:34:06 -0300236static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
237static int xc4000_TunerReset(struct dvb_frontend *fe);
238
239static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
240{
241 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
242 .flags = 0, .buf = buf, .len = len };
Davide Ferri8d009a02009-06-23 22:34:06 -0300243 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
Devin Heitmueller799ed112009-10-04 23:09:18 -0300244 if (priv->ignore_i2c_write_errors == 0) {
245 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
246 len);
247 if (len == 4) {
248 printk("bytes %02x %02x %02x %02x\n", buf[0],
249 buf[1], buf[2], buf[3]);
250 }
251 return XC_RESULT_I2C_WRITE_FAILURE;
252 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300253 }
254 return XC_RESULT_SUCCESS;
255}
256
Davide Ferri8d009a02009-06-23 22:34:06 -0300257static void xc_wait(int wait_ms)
258{
259 msleep(wait_ms);
260}
261
262static int xc4000_TunerReset(struct dvb_frontend *fe)
263{
264 struct xc4000_priv *priv = fe->tuner_priv;
265 int ret;
266
267 dprintk(1, "%s()\n", __func__);
268
269 if (fe->callback) {
270 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
271 fe->dvb->priv :
272 priv->i2c_props.adap->algo_data,
273 DVB_FRONTEND_COMPONENT_TUNER,
274 XC4000_TUNER_RESET, 0);
275 if (ret) {
276 printk(KERN_ERR "xc4000: reset failed\n");
277 return XC_RESULT_RESET_FAILURE;
278 }
279 } else {
280 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
281 return XC_RESULT_RESET_FAILURE;
282 }
283 return XC_RESULT_SUCCESS;
284}
285
286static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
287{
288 u8 buf[4];
Davide Ferri8d009a02009-06-23 22:34:06 -0300289 int result;
290
291 buf[0] = (regAddr >> 8) & 0xFF;
292 buf[1] = regAddr & 0xFF;
293 buf[2] = (i2cData >> 8) & 0xFF;
294 buf[3] = i2cData & 0xFF;
295 result = xc_send_i2c_data(priv, buf, 4);
Davide Ferri8d009a02009-06-23 22:34:06 -0300296
297 return result;
298}
299
300static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
301{
302 struct xc4000_priv *priv = fe->tuner_priv;
303
304 int i, nbytes_to_send, result;
305 unsigned int len, pos, index;
306 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
307
308 index = 0;
309 while ((i2c_sequence[index] != 0xFF) ||
310 (i2c_sequence[index + 1] != 0xFF)) {
311 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
312 if (len == 0x0000) {
313 /* RESET command */
314 result = xc4000_TunerReset(fe);
315 index += 2;
316 if (result != XC_RESULT_SUCCESS)
317 return result;
318 } else if (len & 0x8000) {
319 /* WAIT command */
320 xc_wait(len & 0x7FFF);
321 index += 2;
322 } else {
323 /* Send i2c data whilst ensuring individual transactions
324 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
325 */
326 index += 2;
327 buf[0] = i2c_sequence[index];
328 buf[1] = i2c_sequence[index + 1];
329 pos = 2;
330 while (pos < len) {
331 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
332 nbytes_to_send =
333 XC_MAX_I2C_WRITE_LENGTH;
334 else
335 nbytes_to_send = (len - pos + 2);
336 for (i = 2; i < nbytes_to_send; i++) {
337 buf[i] = i2c_sequence[index + pos +
338 i - 2];
339 }
340 result = xc_send_i2c_data(priv, buf,
341 nbytes_to_send);
342
343 if (result != XC_RESULT_SUCCESS)
344 return result;
345
346 pos += nbytes_to_send - 2;
347 }
348 index += len;
349 }
350 }
351 return XC_RESULT_SUCCESS;
352}
353
Davide Ferri8d009a02009-06-23 22:34:06 -0300354static int xc_SetTVStandard(struct xc4000_priv *priv,
355 u16 VideoMode, u16 AudioMode)
356{
357 int ret;
358 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
359 dprintk(1, "%s() Standard = %s\n",
360 __func__,
361 XC4000_Standard[priv->video_standard].Name);
362
Devin Heitmueller799ed112009-10-04 23:09:18 -0300363 /* Don't complain when the request fails because of i2c stretching */
364 priv->ignore_i2c_write_errors = 1;
365
Davide Ferri8d009a02009-06-23 22:34:06 -0300366 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
367 if (ret == XC_RESULT_SUCCESS)
368 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
369
Devin Heitmueller799ed112009-10-04 23:09:18 -0300370 priv->ignore_i2c_write_errors = 0;
371
Davide Ferri8d009a02009-06-23 22:34:06 -0300372 return ret;
373}
374
375static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
376{
377 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
378 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
379
380 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
381 rf_mode = XC_RF_MODE_CABLE;
382 printk(KERN_ERR
383 "%s(), Invalid mode, defaulting to CABLE",
384 __func__);
385 }
386 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
387}
388
389static const struct dvb_tuner_ops xc4000_tuner_ops;
390
391static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
392{
393 u16 freq_code;
394
395 dprintk(1, "%s(%u)\n", __func__, freq_hz);
396
397 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
398 (freq_hz < xc4000_tuner_ops.info.frequency_min))
399 return XC_RESULT_OUT_OF_RANGE;
400
401 freq_code = (u16)(freq_hz / 15625);
402
403 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
404 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
405 only be used for fast scanning for channel lock) */
406 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
407}
408
Davide Ferri8d009a02009-06-23 22:34:06 -0300409static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
410{
411 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
412}
413
414static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
415{
416 int result;
417 u16 regData;
418 u32 tmp;
419
420 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
421 if (result != XC_RESULT_SUCCESS)
422 return result;
423
Istvan Varga1368ceb2011-06-03 12:27:30 -0300424 tmp = (u32)regData & 0xFFFFU;
425 tmp = (tmp < 0x8000U ? tmp : 0x10000U - tmp);
426 (*freq_error_hz) = tmp * 15625;
Davide Ferri8d009a02009-06-23 22:34:06 -0300427 return result;
428}
429
430static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
431{
432 return xc4000_readreg(priv, XREG_LOCK, lock_status);
433}
434
435static int xc_get_version(struct xc4000_priv *priv,
436 u8 *hw_majorversion, u8 *hw_minorversion,
437 u8 *fw_majorversion, u8 *fw_minorversion)
438{
439 u16 data;
440 int result;
441
442 result = xc4000_readreg(priv, XREG_VERSION, &data);
443 if (result != XC_RESULT_SUCCESS)
444 return result;
445
446 (*hw_majorversion) = (data >> 12) & 0x0F;
447 (*hw_minorversion) = (data >> 8) & 0x0F;
448 (*fw_majorversion) = (data >> 4) & 0x0F;
449 (*fw_minorversion) = data & 0x0F;
450
451 return 0;
452}
453
Davide Ferri8d009a02009-06-23 22:34:06 -0300454static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
455{
456 u16 regData;
457 int result;
458
459 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
460 if (result != XC_RESULT_SUCCESS)
461 return result;
462
463 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
464 return result;
465}
466
467static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
468{
469 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
470}
471
472static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
473{
474 return xc4000_readreg(priv, XREG_QUALITY, quality);
475}
476
477static u16 WaitForLock(struct xc4000_priv *priv)
478{
479 u16 lockState = 0;
480 int watchDogCount = 40;
481
482 while ((lockState == 0) && (watchDogCount > 0)) {
483 xc_get_lock_status(priv, &lockState);
484 if (lockState != 1) {
485 xc_wait(5);
486 watchDogCount--;
487 }
488 }
489 return lockState;
490}
491
492#define XC_TUNE_ANALOG 0
493#define XC_TUNE_DIGITAL 1
494static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
495{
Istvan Vargafbe4a292011-06-03 10:11:48 -0300496 int found = 0;
497 int result = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300498
499 dprintk(1, "%s(%u)\n", __func__, freq_hz);
500
Devin Heitmueller799ed112009-10-04 23:09:18 -0300501 /* Don't complain when the request fails because of i2c stretching */
502 priv->ignore_i2c_write_errors = 1;
503 result = xc_set_RF_frequency(priv, freq_hz);
504 priv->ignore_i2c_write_errors = 0;
505
506 if (result != XC_RESULT_SUCCESS)
Davide Ferri8d009a02009-06-23 22:34:06 -0300507 return 0;
508
509 if (mode == XC_TUNE_ANALOG) {
510 if (WaitForLock(priv) == 1)
511 found = 1;
512 }
513
514 return found;
515}
516
517static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
518{
519 u8 buf[2] = { reg >> 8, reg & 0xff };
520 u8 bval[2] = { 0, 0 };
521 struct i2c_msg msg[2] = {
522 { .addr = priv->i2c_props.addr,
523 .flags = 0, .buf = &buf[0], .len = 2 },
524 { .addr = priv->i2c_props.addr,
525 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
526 };
527
528 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
529 printk(KERN_WARNING "xc4000: I2C read failed\n");
530 return -EREMOTEIO;
531 }
532
533 *val = (bval[0] << 8) | bval[1];
534 return XC_RESULT_SUCCESS;
535}
536
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300537#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300538static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
539{
540 if (type & BASE)
541 printk("BASE ");
542 if (type & INIT1)
543 printk("INIT1 ");
544 if (type & F8MHZ)
545 printk("F8MHZ ");
546 if (type & MTS)
547 printk("MTS ");
548 if (type & D2620)
549 printk("D2620 ");
550 if (type & D2633)
551 printk("D2633 ");
552 if (type & DTV6)
553 printk("DTV6 ");
554 if (type & QAM)
555 printk("QAM ");
556 if (type & DTV7)
557 printk("DTV7 ");
558 if (type & DTV78)
559 printk("DTV78 ");
560 if (type & DTV8)
561 printk("DTV8 ");
562 if (type & FM)
563 printk("FM ");
564 if (type & INPUT1)
565 printk("INPUT1 ");
566 if (type & LCD)
567 printk("LCD ");
568 if (type & NOGD)
569 printk("NOGD ");
570 if (type & MONO)
571 printk("MONO ");
572 if (type & ATSC)
573 printk("ATSC ");
574 if (type & IF)
575 printk("IF ");
576 if (type & LG60)
577 printk("LG60 ");
578 if (type & ATI638)
579 printk("ATI638 ");
580 if (type & OREN538)
581 printk("OREN538 ");
582 if (type & OREN36)
583 printk("OREN36 ");
584 if (type & TOYOTA388)
585 printk("TOYOTA388 ");
586 if (type & TOYOTA794)
587 printk("TOYOTA794 ");
588 if (type & DIBCOM52)
589 printk("DIBCOM52 ");
590 if (type & ZARLINK456)
591 printk("ZARLINK456 ");
592 if (type & CHINA)
593 printk("CHINA ");
594 if (type & F6MHZ)
595 printk("F6MHZ ");
596 if (type & INPUT2)
597 printk("INPUT2 ");
598 if (type & SCODE)
599 printk("SCODE ");
600 if (type & HAS_IF)
601 printk("HAS_IF_%d ", int_freq);
602}
603
Devin Heitmueller11091a32009-07-20 00:54:57 -0300604static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
605 v4l2_std_id *id)
606{
607 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga3db95702011-06-04 11:52:34 -0300608 int i, best_i = -1;
609 unsigned int best_nr_diffs = 255U;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300610
Devin Heitmueller11091a32009-07-20 00:54:57 -0300611 if (!priv->firm) {
612 printk("Error! firmware not loaded\n");
613 return -EINVAL;
614 }
615
616 if (((type & ~SCODE) == 0) && (*id == 0))
617 *id = V4L2_STD_PAL;
618
Devin Heitmueller11091a32009-07-20 00:54:57 -0300619 /* Seek for generic video standard match */
620 for (i = 0; i < priv->firm_size; i++) {
Istvan Varga3db95702011-06-04 11:52:34 -0300621 v4l2_std_id id_diff_mask =
622 (priv->firm[i].id ^ (*id)) & (*id);
623 unsigned int type_diff_mask =
624 (priv->firm[i].type ^ type)
625 & (BASE_TYPES | DTV_TYPES | LCD | NOGD | MONO | SCODE);
626 unsigned int nr_diffs;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300627
Istvan Varga3db95702011-06-04 11:52:34 -0300628 if (type_diff_mask
629 & (BASE | INIT1 | FM | DTV6 | DTV7 | DTV78 | DTV8 | SCODE))
Devin Heitmueller11091a32009-07-20 00:54:57 -0300630 continue;
631
Istvan Varga3db95702011-06-04 11:52:34 -0300632 nr_diffs = hweight64(id_diff_mask) + hweight32(type_diff_mask);
633 if (!nr_diffs) /* Supports all the requested standards */
634 goto found;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300635
Istvan Varga3db95702011-06-04 11:52:34 -0300636 if (nr_diffs < best_nr_diffs) {
637 best_nr_diffs = nr_diffs;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300638 best_i = i;
639 }
640 }
641
Istvan Varga3db95702011-06-04 11:52:34 -0300642 /* FIXME: Would make sense to seek for type "hint" match ? */
643 if (best_i < 0) {
644 i = -ENOENT;
645 goto ret;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300646 }
647
Istvan Varga3db95702011-06-04 11:52:34 -0300648 if (best_nr_diffs > 0U) {
649 printk("Selecting best matching firmware (%u bits differ) for "
650 "type=", best_nr_diffs);
651 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
652 i = best_i;
653 }
Devin Heitmueller11091a32009-07-20 00:54:57 -0300654
655found:
656 *id = priv->firm[i].id;
657
658ret:
Devin Heitmueller11091a32009-07-20 00:54:57 -0300659 if (debug) {
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300660 printk("%s firmware for type=", (i < 0) ? "Can't find" :
661 "Found");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300662 dump_firm_type(type);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300663 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
664 }
665 return i;
666}
667
668static int load_firmware(struct dvb_frontend *fe, unsigned int type,
669 v4l2_std_id *id)
670{
671 struct xc4000_priv *priv = fe->tuner_priv;
672 int pos, rc;
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300673 unsigned char *p;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300674
Devin Heitmueller11091a32009-07-20 00:54:57 -0300675 pos = seek_firmware(fe, type, id);
676 if (pos < 0)
677 return pos;
678
Devin Heitmueller11091a32009-07-20 00:54:57 -0300679 p = priv->firm[pos].ptr;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300680
Devin Heitmueller799ed112009-10-04 23:09:18 -0300681 /* Don't complain when the request fails because of i2c stretching */
682 priv->ignore_i2c_write_errors = 1;
683
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300684 rc = xc_load_i2c_sequence(fe, p);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300685
Devin Heitmueller799ed112009-10-04 23:09:18 -0300686 priv->ignore_i2c_write_errors = 0;
687
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300688 return rc;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300689}
690
Davide Ferri8d009a02009-06-23 22:34:06 -0300691static int xc4000_fwupload(struct dvb_frontend *fe)
692{
693 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300694 const struct firmware *fw = NULL;
695 const unsigned char *p, *endp;
696 int rc = 0;
697 int n, n_array;
698 char name[33];
Istvan Vargafbe4a292011-06-03 10:11:48 -0300699 const char *fname;
Davide Ferri8d009a02009-06-23 22:34:06 -0300700
Istvan Vargafa285bc2011-06-04 11:48:16 -0300701 if (firmware_name[0] != '\0')
702 fname = firmware_name;
703 else
704 fname = XC4000_DEFAULT_FIRMWARE;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300705
706 printk("Reading firmware %s\n", fname);
707 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
708 if (rc < 0) {
709 if (rc == -ENOENT)
710 printk("Error: firmware %s not found.\n",
711 fname);
712 else
713 printk("Error %d while requesting firmware %s \n",
714 rc, fname);
715
716 return rc;
717 }
718 p = fw->data;
719 endp = p + fw->size;
720
721 if (fw->size < sizeof(name) - 1 + 2 + 2) {
722 printk("Error: firmware file %s has invalid size!\n",
Istvan Vargafbe4a292011-06-03 10:11:48 -0300723 fname);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300724 goto corrupt;
Davide Ferri8d009a02009-06-23 22:34:06 -0300725 }
726
Devin Heitmueller11091a32009-07-20 00:54:57 -0300727 memcpy(name, p, sizeof(name) - 1);
728 name[sizeof(name) - 1] = 0;
729 p += sizeof(name) - 1;
730
731 priv->firm_version = get_unaligned_le16(p);
732 p += 2;
733
734 n_array = get_unaligned_le16(p);
735 p += 2;
736
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300737 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
738 n_array, fname, name,
739 priv->firm_version >> 8, priv->firm_version & 0xff);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300740
741 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
742 if (priv->firm == NULL) {
743 printk("Not enough memory to load firmware file.\n");
744 rc = -ENOMEM;
745 goto err;
746 }
747 priv->firm_size = n_array;
748
749 n = -1;
750 while (p < endp) {
751 __u32 type, size;
752 v4l2_std_id id;
753 __u16 int_freq = 0;
754
755 n++;
756 if (n >= n_array) {
757 printk("More firmware images in file than "
Istvan Vargafbe4a292011-06-03 10:11:48 -0300758 "were expected!\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300759 goto corrupt;
760 }
761
762 /* Checks if there's enough bytes to read */
763 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
764 goto header;
765
766 type = get_unaligned_le32(p);
767 p += sizeof(type);
768
769 id = get_unaligned_le64(p);
770 p += sizeof(id);
771
772 if (type & HAS_IF) {
773 int_freq = get_unaligned_le16(p);
774 p += sizeof(int_freq);
775 if (endp - p < sizeof(size))
776 goto header;
777 }
778
779 size = get_unaligned_le32(p);
780 p += sizeof(size);
781
782 if (!size || size > endp - p) {
783 printk("Firmware type ");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300784 printk("(%x), id %llx is corrupted "
785 "(size=%d, expected %d)\n",
786 type, (unsigned long long)id,
787 (unsigned)(endp - p), size);
788 goto corrupt;
789 }
790
791 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
792 if (priv->firm[n].ptr == NULL) {
793 printk("Not enough memory to load firmware file.\n");
794 rc = -ENOMEM;
795 goto err;
796 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300797
Devin Heitmueller11091a32009-07-20 00:54:57 -0300798 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300799 printk("Reading firmware type ");
800 dump_firm_type_and_int_freq(type, int_freq);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300801 printk("(%x), id %llx, size=%d.\n",
802 type, (unsigned long long)id, size);
803 }
804
805 memcpy(priv->firm[n].ptr, p, size);
806 priv->firm[n].type = type;
807 priv->firm[n].id = id;
808 priv->firm[n].size = size;
809 priv->firm[n].int_freq = int_freq;
810
811 p += size;
Davide Ferri8d009a02009-06-23 22:34:06 -0300812 }
813
Devin Heitmueller11091a32009-07-20 00:54:57 -0300814 if (n + 1 != priv->firm_size) {
815 printk("Firmware file is incomplete!\n");
816 goto corrupt;
817 }
818
819 goto done;
820
821header:
822 printk("Firmware header is incomplete!\n");
823corrupt:
824 rc = -EINVAL;
825 printk("Error: firmware file is corrupted!\n");
826
827err:
828 printk("Releasing partially loaded firmware file.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300829
830done:
Davide Ferri8d009a02009-06-23 22:34:06 -0300831 release_firmware(fw);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300832 if (rc == 0)
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300833 dprintk(1, "Firmware files loaded.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300834
835 return rc;
Davide Ferri8d009a02009-06-23 22:34:06 -0300836}
837
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300838static int load_scode(struct dvb_frontend *fe, unsigned int type,
839 v4l2_std_id *id, __u16 int_freq, int scode)
840{
841 struct xc4000_priv *priv = fe->tuner_priv;
842 int pos, rc;
843 unsigned char *p;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300844 u8 scode_buf[13];
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300845 u8 indirect_mode[5];
846
Devin Heitmuellerfe830362009-07-28 00:04:27 -0300847 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300848
849 if (!int_freq) {
850 pos = seek_firmware(fe, type, id);
851 if (pos < 0)
852 return pos;
853 } else {
854 for (pos = 0; pos < priv->firm_size; pos++) {
855 if ((priv->firm[pos].int_freq == int_freq) &&
856 (priv->firm[pos].type & HAS_IF))
857 break;
858 }
859 if (pos == priv->firm_size)
860 return -ENOENT;
861 }
862
863 p = priv->firm[pos].ptr;
864
865 if (priv->firm[pos].type & HAS_IF) {
866 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
867 return -EINVAL;
868 p += 12 * scode;
869 } else {
870 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
871 * has a 2-byte size header in the firmware format. */
872 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
873 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
874 return -EINVAL;
875 p += 14 * scode + 2;
876 }
877
878 tuner_info("Loading SCODE for type=");
879 dump_firm_type_and_int_freq(priv->firm[pos].type,
880 priv->firm[pos].int_freq);
881 printk("(%x), id %016llx.\n", priv->firm[pos].type,
882 (unsigned long long)*id);
883
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300884 scode_buf[0] = 0x00;
885 memcpy(&scode_buf[1], p, 12);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300886
887 /* Enter direct-mode */
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300888 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
889 if (rc < 0) {
890 printk("failed to put device into direct mode!\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300891 return -EIO;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300892 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300893
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300894 rc = xc_send_i2c_data(priv, scode_buf, 13);
895 if (rc != XC_RESULT_SUCCESS) {
896 /* Even if the send failed, make sure we set back to indirect
897 mode */
898 printk("Failed to set scode %d\n", rc);
899 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300900
901 /* Switch back to indirect-mode */
902 memset(indirect_mode, 0, sizeof(indirect_mode));
903 indirect_mode[4] = 0x88;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300904 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
905 msleep(10);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300906
907 return 0;
908}
909
910static int check_firmware(struct dvb_frontend *fe, unsigned int type,
911 v4l2_std_id std, __u16 int_freq)
912{
913 struct xc4000_priv *priv = fe->tuner_priv;
914 struct firmware_properties new_fw;
915 int rc = 0, is_retry = 0;
916 u16 version, hwmodel;
917 v4l2_std_id std0;
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300918 u8 hw_major, hw_minor, fw_major, fw_minor;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300919
920 dprintk(1, "%s called\n", __func__);
921
922 if (!priv->firm) {
923 rc = xc4000_fwupload(fe);
924 if (rc < 0)
925 return rc;
926 }
927
928#ifdef DJH_DEBUG
929 if (priv->ctrl.mts && !(type & FM))
930 type |= MTS;
931#endif
932
933retry:
934 new_fw.type = type;
935 new_fw.id = std;
936 new_fw.std_req = std;
Istvan Vargafbe4a292011-06-03 10:11:48 -0300937 new_fw.scode_table = SCODE /* | priv->ctrl.scode_table */;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300938 new_fw.scode_nr = 0;
939 new_fw.int_freq = int_freq;
940
941 dprintk(1, "checking firmware, user requested type=");
942 if (debug) {
943 dump_firm_type(new_fw.type);
944 printk("(%x), id %016llx, ", new_fw.type,
945 (unsigned long long)new_fw.std_req);
946 if (!int_freq) {
947 printk("scode_tbl ");
948#ifdef DJH_DEBUG
949 dump_firm_type(priv->ctrl.scode_table);
950 printk("(%x), ", priv->ctrl.scode_table);
951#endif
952 } else
953 printk("int_freq %d, ", new_fw.int_freq);
954 printk("scode_nr %d\n", new_fw.scode_nr);
955 }
956
957 /* No need to reload base firmware if it matches */
958 if (((BASE | new_fw.type) & BASE_TYPES) ==
959 (priv->cur_fw.type & BASE_TYPES)) {
960 dprintk(1, "BASE firmware not changed.\n");
961 goto skip_base;
962 }
963
964 /* Updating BASE - forget about all currently loaded firmware */
965 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
966
967 /* Reset is needed before loading firmware */
968 rc = xc4000_TunerReset(fe);
969 if (rc < 0)
970 goto fail;
971
972 /* BASE firmwares are all std0 */
973 std0 = 0;
974 rc = load_firmware(fe, BASE | new_fw.type, &std0);
975 if (rc < 0) {
976 printk("Error %d while loading base firmware\n", rc);
977 goto fail;
978 }
979
980 /* Load INIT1, if needed */
981 dprintk(1, "Load init1 firmware, if exists\n");
982
983 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
984 if (rc == -ENOENT)
985 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
986 &std0);
987 if (rc < 0 && rc != -ENOENT) {
988 tuner_err("Error %d while loading init1 firmware\n",
989 rc);
990 goto fail;
991 }
992
993skip_base:
994 /*
995 * No need to reload standard specific firmware if base firmware
996 * was not reloaded and requested video standards have not changed.
997 */
998 if (priv->cur_fw.type == (BASE | new_fw.type) &&
999 priv->cur_fw.std_req == std) {
1000 dprintk(1, "Std-specific firmware already loaded.\n");
1001 goto skip_std_specific;
1002 }
1003
1004 /* Reloading std-specific firmware forces a SCODE update */
1005 priv->cur_fw.scode_table = 0;
1006
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001007 /* Load the standard firmware */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001008 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001009
1010 if (rc < 0)
1011 goto fail;
1012
1013skip_std_specific:
1014 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1015 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1016 dprintk(1, "SCODE firmware already loaded.\n");
1017 goto check_device;
1018 }
1019
1020 if (new_fw.type & FM)
1021 goto check_device;
1022
1023 /* Load SCODE firmware, if exists */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001024 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1025 new_fw.int_freq, new_fw.scode_nr);
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001026 if (rc != XC_RESULT_SUCCESS)
1027 dprintk(1, "load scode failed %d\n", rc);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001028
1029check_device:
1030 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1031
Devin Heitmueller799ed112009-10-04 23:09:18 -03001032 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001033 &fw_minor) != XC_RESULT_SUCCESS) {
1034 printk("Unable to read tuner registers.\n");
1035 goto fail;
1036 }
1037
1038 dprintk(1, "Device is Xceive %d version %d.%d, "
1039 "firmware version %d.%d\n",
1040 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1041
1042 /* Check firmware version against what we downloaded. */
1043#ifdef DJH_DEBUG
1044 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1045 printk("Incorrect readback of firmware version %x.\n",
1046 (version & 0xff));
1047 goto fail;
1048 }
1049#endif
1050
1051 /* Check that the tuner hardware model remains consistent over time. */
1052 if (priv->hwmodel == 0 && hwmodel == 4000) {
1053 priv->hwmodel = hwmodel;
1054 priv->hwvers = version & 0xff00;
1055 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1056 priv->hwvers != (version & 0xff00)) {
1057 printk("Read invalid device hardware information - tuner "
Istvan Vargafbe4a292011-06-03 10:11:48 -03001058 "hung?\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001059 goto fail;
1060 }
1061
1062 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1063
1064 /*
1065 * By setting BASE in cur_fw.type only after successfully loading all
1066 * firmwares, we can:
1067 * 1. Identify that BASE firmware with type=0 has been loaded;
1068 * 2. Tell whether BASE firmware was just changed the next time through.
1069 */
1070 priv->cur_fw.type |= BASE;
1071
1072 return 0;
1073
1074fail:
1075 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1076 if (!is_retry) {
1077 msleep(50);
1078 is_retry = 1;
1079 dprintk(1, "Retrying firmware load\n");
1080 goto retry;
1081 }
1082
1083 if (rc == -ENOENT)
1084 rc = -EINVAL;
1085 return rc;
1086}
Devin Heitmueller11091a32009-07-20 00:54:57 -03001087
Davide Ferri8d009a02009-06-23 22:34:06 -03001088static void xc_debug_dump(struct xc4000_priv *priv)
1089{
Istvan Vargafbe4a292011-06-03 10:11:48 -03001090 u16 adc_envelope;
1091 u32 freq_error_hz = 0;
1092 u16 lock_status;
1093 u32 hsync_freq_hz = 0;
1094 u16 frame_lines;
1095 u16 quality;
1096 u8 hw_majorversion = 0, hw_minorversion = 0;
1097 u8 fw_majorversion = 0, fw_minorversion = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001098
1099 /* Wait for stats to stabilize.
1100 * Frame Lines needs two frame times after initial lock
1101 * before it is valid.
1102 */
1103 xc_wait(100);
1104
Istvan Vargafbe4a292011-06-03 10:11:48 -03001105 xc_get_ADC_Envelope(priv, &adc_envelope);
Davide Ferri8d009a02009-06-23 22:34:06 -03001106 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1107
1108 xc_get_frequency_error(priv, &freq_error_hz);
1109 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1110
Istvan Vargafbe4a292011-06-03 10:11:48 -03001111 xc_get_lock_status(priv, &lock_status);
Davide Ferri8d009a02009-06-23 22:34:06 -03001112 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1113 lock_status);
1114
Istvan Vargafbe4a292011-06-03 10:11:48 -03001115 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1116 &fw_majorversion, &fw_minorversion);
1117
Davide Ferri8d009a02009-06-23 22:34:06 -03001118 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1119 hw_majorversion, hw_minorversion,
1120 fw_majorversion, fw_minorversion);
1121
Istvan Vargafbe4a292011-06-03 10:11:48 -03001122 xc_get_hsync_freq(priv, &hsync_freq_hz);
Davide Ferri8d009a02009-06-23 22:34:06 -03001123 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1124
Istvan Vargafbe4a292011-06-03 10:11:48 -03001125 xc_get_frame_lines(priv, &frame_lines);
Davide Ferri8d009a02009-06-23 22:34:06 -03001126 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1127
Istvan Vargafbe4a292011-06-03 10:11:48 -03001128 xc_get_quality(priv, &quality);
Davide Ferri8d009a02009-06-23 22:34:06 -03001129 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1130}
1131
1132static int xc4000_set_params(struct dvb_frontend *fe,
1133 struct dvb_frontend_parameters *params)
1134{
1135 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001136 unsigned int type;
Istvan Varga56149422011-06-03 12:23:33 -03001137 int ret = -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -03001138
Davide Ferri8d009a02009-06-23 22:34:06 -03001139 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1140
Istvan Varga56149422011-06-03 12:23:33 -03001141 mutex_lock(&priv->lock);
1142
Davide Ferri8d009a02009-06-23 22:34:06 -03001143 if (fe->ops.info.type == FE_ATSC) {
1144 dprintk(1, "%s() ATSC\n", __func__);
1145 switch (params->u.vsb.modulation) {
1146 case VSB_8:
1147 case VSB_16:
1148 dprintk(1, "%s() VSB modulation\n", __func__);
1149 priv->rf_mode = XC_RF_MODE_AIR;
1150 priv->freq_hz = params->frequency - 1750000;
1151 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001152 priv->video_standard = XC4000_DTV6;
1153 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001154 break;
1155 case QAM_64:
1156 case QAM_256:
1157 case QAM_AUTO:
1158 dprintk(1, "%s() QAM modulation\n", __func__);
1159 priv->rf_mode = XC_RF_MODE_CABLE;
1160 priv->freq_hz = params->frequency - 1750000;
1161 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001162 priv->video_standard = XC4000_DTV6;
1163 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001164 break;
1165 default:
Istvan Varga56149422011-06-03 12:23:33 -03001166 ret = -EINVAL;
1167 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001168 }
1169 } else if (fe->ops.info.type == FE_OFDM) {
1170 dprintk(1, "%s() OFDM\n", __func__);
1171 switch (params->u.ofdm.bandwidth) {
1172 case BANDWIDTH_6_MHZ:
1173 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001174 priv->video_standard = XC4000_DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001175 priv->freq_hz = params->frequency - 1750000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001176 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001177 break;
1178 case BANDWIDTH_7_MHZ:
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001179 priv->bandwidth = BANDWIDTH_7_MHZ;
1180 priv->video_standard = XC4000_DTV7;
1181 priv->freq_hz = params->frequency - 2250000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001182 type = DTV7;
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001183 break;
Davide Ferri8d009a02009-06-23 22:34:06 -03001184 case BANDWIDTH_8_MHZ:
1185 priv->bandwidth = BANDWIDTH_8_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001186 priv->video_standard = XC4000_DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001187 priv->freq_hz = params->frequency - 2750000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001188 type = DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001189 break;
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001190 case BANDWIDTH_AUTO:
1191 if (params->frequency < 400000000) {
1192 priv->bandwidth = BANDWIDTH_7_MHZ;
1193 priv->freq_hz = params->frequency - 2250000;
1194 } else {
1195 priv->bandwidth = BANDWIDTH_8_MHZ;
1196 priv->freq_hz = params->frequency - 2750000;
1197 }
1198 priv->video_standard = XC4000_DTV7_8;
1199 type = DTV78;
1200 break;
Davide Ferri8d009a02009-06-23 22:34:06 -03001201 default:
1202 printk(KERN_ERR "xc4000 bandwidth not set!\n");
Istvan Varga56149422011-06-03 12:23:33 -03001203 ret = -EINVAL;
1204 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001205 }
1206 priv->rf_mode = XC_RF_MODE_AIR;
1207 } else {
1208 printk(KERN_ERR "xc4000 modulation type not supported!\n");
Istvan Varga56149422011-06-03 12:23:33 -03001209 ret = -EINVAL;
1210 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001211 }
1212
1213 dprintk(1, "%s() frequency=%d (compensated)\n",
1214 __func__, priv->freq_hz);
1215
Devin Heitmuellered23db32009-10-05 01:27:14 -03001216 /* Make sure the correct firmware type is loaded */
Istvan Varga56149422011-06-03 12:23:33 -03001217 if (check_firmware(fe, type, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1218 goto fail;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001219
Davide Ferri8d009a02009-06-23 22:34:06 -03001220 ret = xc_SetSignalSource(priv, priv->rf_mode);
1221 if (ret != XC_RESULT_SUCCESS) {
1222 printk(KERN_ERR
Istvan Varga56149422011-06-03 12:23:33 -03001223 "xc4000: xc_SetSignalSource(%d) failed\n",
1224 priv->rf_mode);
1225 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001226 }
1227
1228 ret = xc_SetTVStandard(priv,
1229 XC4000_Standard[priv->video_standard].VideoMode,
1230 XC4000_Standard[priv->video_standard].AudioMode);
1231 if (ret != XC_RESULT_SUCCESS) {
1232 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
Istvan Varga56149422011-06-03 12:23:33 -03001233 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001234 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001235 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1236
1237 if (debug)
1238 xc_debug_dump(priv);
1239
Istvan Varga56149422011-06-03 12:23:33 -03001240 ret = 0;
1241
1242fail:
1243 mutex_unlock(&priv->lock);
1244
1245 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001246}
1247
Davide Ferri8d009a02009-06-23 22:34:06 -03001248static int xc4000_set_analog_params(struct dvb_frontend *fe,
1249 struct analog_parameters *params)
1250{
1251 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga56149422011-06-03 12:23:33 -03001252 int ret = -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -03001253
Davide Ferri8d009a02009-06-23 22:34:06 -03001254 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1255 __func__, params->frequency);
1256
Istvan Varga56149422011-06-03 12:23:33 -03001257 mutex_lock(&priv->lock);
1258
Davide Ferri8d009a02009-06-23 22:34:06 -03001259 /* Fix me: it could be air. */
1260 priv->rf_mode = params->mode;
1261 if (params->mode > XC_RF_MODE_CABLE)
1262 priv->rf_mode = XC_RF_MODE_CABLE;
1263
1264 /* params->frequency is in units of 62.5khz */
1265 priv->freq_hz = params->frequency * 62500;
1266
1267 /* FIX ME: Some video standards may have several possible audio
1268 standards. We simply default to one of them here.
1269 */
1270 if (params->std & V4L2_STD_MN) {
1271 /* default to BTSC audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001272 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
Davide Ferri8d009a02009-06-23 22:34:06 -03001273 goto tune_channel;
1274 }
1275
1276 if (params->std & V4L2_STD_PAL_BG) {
1277 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001278 priv->video_standard = XC4000_BG_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001279 goto tune_channel;
1280 }
1281
1282 if (params->std & V4L2_STD_PAL_I) {
1283 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001284 priv->video_standard = XC4000_I_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001285 goto tune_channel;
1286 }
1287
1288 if (params->std & V4L2_STD_PAL_DK) {
1289 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001290 priv->video_standard = XC4000_DK_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001291 goto tune_channel;
1292 }
1293
1294 if (params->std & V4L2_STD_SECAM_DK) {
1295 /* default to A2 DK1 audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001296 priv->video_standard = XC4000_DK_SECAM_A2DK1;
Davide Ferri8d009a02009-06-23 22:34:06 -03001297 goto tune_channel;
1298 }
1299
1300 if (params->std & V4L2_STD_SECAM_L) {
Devin Heitmuellered23db32009-10-05 01:27:14 -03001301 priv->video_standard = XC4000_L_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001302 goto tune_channel;
1303 }
1304
1305 if (params->std & V4L2_STD_SECAM_LC) {
Devin Heitmuellered23db32009-10-05 01:27:14 -03001306 priv->video_standard = XC4000_LC_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001307 goto tune_channel;
1308 }
1309
1310tune_channel:
Devin Heitmuellered23db32009-10-05 01:27:14 -03001311
1312 /* FIXME - firmware type not being set properly */
Istvan Varga56149422011-06-03 12:23:33 -03001313 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1314 goto fail;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001315
Davide Ferri8d009a02009-06-23 22:34:06 -03001316 ret = xc_SetSignalSource(priv, priv->rf_mode);
1317 if (ret != XC_RESULT_SUCCESS) {
1318 printk(KERN_ERR
Istvan Varga56149422011-06-03 12:23:33 -03001319 "xc4000: xc_SetSignalSource(%d) failed\n",
1320 priv->rf_mode);
1321 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001322 }
1323
1324 ret = xc_SetTVStandard(priv,
1325 XC4000_Standard[priv->video_standard].VideoMode,
1326 XC4000_Standard[priv->video_standard].AudioMode);
1327 if (ret != XC_RESULT_SUCCESS) {
1328 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
Istvan Varga56149422011-06-03 12:23:33 -03001329 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001330 }
1331
1332 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1333
1334 if (debug)
1335 xc_debug_dump(priv);
1336
Istvan Varga56149422011-06-03 12:23:33 -03001337 ret = 0;
1338
1339fail:
1340 mutex_unlock(&priv->lock);
1341
1342 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001343}
1344
1345static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1346{
1347 struct xc4000_priv *priv = fe->tuner_priv;
1348 dprintk(1, "%s()\n", __func__);
1349 *freq = priv->freq_hz;
1350 return 0;
1351}
1352
1353static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1354{
1355 struct xc4000_priv *priv = fe->tuner_priv;
1356 dprintk(1, "%s()\n", __func__);
1357
1358 *bw = priv->bandwidth;
1359 return 0;
1360}
1361
1362static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1363{
1364 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001365 u16 lock_status = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001366
Istvan Varga56149422011-06-03 12:23:33 -03001367 mutex_lock(&priv->lock);
1368
Davide Ferri8d009a02009-06-23 22:34:06 -03001369 xc_get_lock_status(priv, &lock_status);
1370
Istvan Varga56149422011-06-03 12:23:33 -03001371 mutex_unlock(&priv->lock);
1372
Davide Ferri8d009a02009-06-23 22:34:06 -03001373 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1374
1375 *status = lock_status;
1376
1377 return 0;
1378}
1379
Davide Ferri8d009a02009-06-23 22:34:06 -03001380static int xc4000_sleep(struct dvb_frontend *fe)
1381{
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001382 /* FIXME: djh disable this for now... */
1383 return XC_RESULT_SUCCESS;
Davide Ferri8d009a02009-06-23 22:34:06 -03001384}
1385
1386static int xc4000_init(struct dvb_frontend *fe)
1387{
1388 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga56149422011-06-03 12:23:33 -03001389 int ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001390 dprintk(1, "%s()\n", __func__);
1391
Istvan Varga56149422011-06-03 12:23:33 -03001392 mutex_lock(&priv->lock);
1393 ret = check_firmware(fe, DTV8, 0, priv->if_khz);
1394 mutex_unlock(&priv->lock);
1395 if (ret != XC_RESULT_SUCCESS) {
Davide Ferri8d009a02009-06-23 22:34:06 -03001396 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1397 return -EREMOTEIO;
1398 }
1399
1400 if (debug)
1401 xc_debug_dump(priv);
1402
1403 return 0;
1404}
1405
1406static int xc4000_release(struct dvb_frontend *fe)
1407{
1408 struct xc4000_priv *priv = fe->tuner_priv;
1409
1410 dprintk(1, "%s()\n", __func__);
1411
1412 mutex_lock(&xc4000_list_mutex);
1413
1414 if (priv)
1415 hybrid_tuner_release_state(priv);
1416
1417 mutex_unlock(&xc4000_list_mutex);
1418
1419 fe->tuner_priv = NULL;
1420
1421 return 0;
1422}
1423
1424static const struct dvb_tuner_ops xc4000_tuner_ops = {
1425 .info = {
1426 .name = "Xceive XC4000",
1427 .frequency_min = 1000000,
1428 .frequency_max = 1023000000,
1429 .frequency_step = 50000,
1430 },
1431
1432 .release = xc4000_release,
1433 .init = xc4000_init,
1434 .sleep = xc4000_sleep,
1435
1436 .set_params = xc4000_set_params,
1437 .set_analog_params = xc4000_set_analog_params,
1438 .get_frequency = xc4000_get_frequency,
1439 .get_bandwidth = xc4000_get_bandwidth,
1440 .get_status = xc4000_get_status
1441};
1442
1443struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1444 struct i2c_adapter *i2c,
1445 struct xc4000_config *cfg)
1446{
1447 struct xc4000_priv *priv = NULL;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001448 int instance;
1449 u16 id = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001450
1451 dprintk(1, "%s(%d-%04x)\n", __func__,
1452 i2c ? i2c_adapter_id(i2c) : -1,
1453 cfg ? cfg->i2c_address : -1);
1454
1455 mutex_lock(&xc4000_list_mutex);
1456
1457 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1458 hybrid_tuner_instance_list,
1459 i2c, cfg->i2c_address, "xc4000");
1460 switch (instance) {
1461 case 0:
1462 goto fail;
1463 break;
1464 case 1:
1465 /* new tuner instance */
1466 priv->bandwidth = BANDWIDTH_6_MHZ;
Istvan Varga56149422011-06-03 12:23:33 -03001467 mutex_init(&priv->lock);
Davide Ferri8d009a02009-06-23 22:34:06 -03001468 fe->tuner_priv = priv;
1469 break;
1470 default:
1471 /* existing tuner instance */
1472 fe->tuner_priv = priv;
1473 break;
1474 }
1475
1476 if (priv->if_khz == 0) {
1477 /* If the IF hasn't been set yet, use the value provided by
1478 the caller (occurs in hybrid devices where the analog
1479 call to xc4000_attach occurs before the digital side) */
1480 priv->if_khz = cfg->if_khz;
1481 }
1482
1483 /* Check if firmware has been loaded. It is possible that another
1484 instance of the driver has loaded the firmware.
1485 */
1486
1487 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1488 goto fail;
1489
1490 switch (id) {
1491 case XC_PRODUCT_ID_FW_LOADED:
1492 printk(KERN_INFO
1493 "xc4000: Successfully identified at address 0x%02x\n",
1494 cfg->i2c_address);
1495 printk(KERN_INFO
1496 "xc4000: Firmware has been loaded previously\n");
1497 break;
1498 case XC_PRODUCT_ID_FW_NOT_LOADED:
1499 printk(KERN_INFO
1500 "xc4000: Successfully identified at address 0x%02x\n",
1501 cfg->i2c_address);
1502 printk(KERN_INFO
1503 "xc4000: Firmware has not been loaded previously\n");
1504 break;
1505 default:
1506 printk(KERN_ERR
1507 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1508 cfg->i2c_address, id);
1509 goto fail;
1510 }
1511
1512 mutex_unlock(&xc4000_list_mutex);
1513
1514 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1515 sizeof(struct dvb_tuner_ops));
1516
Devin Heitmueller11091a32009-07-20 00:54:57 -03001517 /* FIXME: For now, load the firmware at startup. We will remove this
1518 before the code goes to production... */
Istvan Varga56149422011-06-03 12:23:33 -03001519 mutex_lock(&priv->lock);
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001520 check_firmware(fe, DTV8, 0, priv->if_khz);
Istvan Varga56149422011-06-03 12:23:33 -03001521 mutex_unlock(&priv->lock);
Devin Heitmueller11091a32009-07-20 00:54:57 -03001522
Davide Ferri8d009a02009-06-23 22:34:06 -03001523 return fe;
1524fail:
1525 mutex_unlock(&xc4000_list_mutex);
1526
1527 xc4000_release(fe);
1528 return NULL;
1529}
1530EXPORT_SYMBOL(xc4000_attach);
1531
1532MODULE_AUTHOR("Steven Toth, Davide Ferri");
1533MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1534MODULE_LICENSE("GPL");