blob: be43a6dfac6c41e3d0660d9eebe9259cf6dbf150 [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
50static DEFINE_MUTEX(xc4000_list_mutex);
51static LIST_HEAD(hybrid_tuner_instance_list);
52
53#define dprintk(level, fmt, arg...) if (debug >= level) \
54 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
55
Devin Heitmueller980029e2009-12-27 17:55:13 -030056/* Note that the last version digit is my internal build number (so I can
57 rev the firmware even if the core Xceive firmware was unchanged) */
58#define XC4000_DEFAULT_FIRMWARE "dvb-fe-xc4000-1.4.1.fw"
Devin Heitmueller11091a32009-07-20 00:54:57 -030059
60/* struct for storing firmware table */
61struct firmware_description {
62 unsigned int type;
63 v4l2_std_id id;
64 __u16 int_freq;
65 unsigned char *ptr;
66 unsigned int size;
67};
68
69struct firmware_properties {
70 unsigned int type;
71 v4l2_std_id id;
72 v4l2_std_id std_req;
73 __u16 int_freq;
74 unsigned int scode_table;
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -030075 int scode_nr;
Devin Heitmueller11091a32009-07-20 00:54:57 -030076};
Davide Ferri8d009a02009-06-23 22:34:06 -030077
78struct xc4000_priv {
79 struct tuner_i2c_props i2c_props;
80 struct list_head hybrid_tuner_instance_list;
Devin Heitmueller11091a32009-07-20 00:54:57 -030081 struct firmware_description *firm;
Istvan Vargafbe4a292011-06-03 10:11:48 -030082 int firm_size;
83 __u16 firm_version;
84 u32 if_khz;
85 u32 freq_hz;
86 u32 bandwidth;
87 u8 video_standard;
88 u8 rf_mode;
89 u8 ignore_i2c_write_errors;
90 /* struct xc2028_ctrl ctrl; */
Devin Heitmuellerd0962382009-07-25 17:39:54 -030091 struct firmware_properties cur_fw;
Istvan Vargafbe4a292011-06-03 10:11:48 -030092 __u16 hwmodel;
93 __u16 hwvers;
Istvan Varga56149422011-06-03 12:23:33 -030094 struct mutex lock;
Davide Ferri8d009a02009-06-23 22:34:06 -030095};
96
97/* Misc Defines */
Istvan Varga49110852011-06-03 10:55:24 -030098#define MAX_TV_STANDARD 24
Davide Ferri8d009a02009-06-23 22:34:06 -030099#define XC_MAX_I2C_WRITE_LENGTH 64
100
101/* Signal Types */
102#define XC_RF_MODE_AIR 0
103#define XC_RF_MODE_CABLE 1
104
105/* Result codes */
106#define XC_RESULT_SUCCESS 0
107#define XC_RESULT_RESET_FAILURE 1
108#define XC_RESULT_I2C_WRITE_FAILURE 2
109#define XC_RESULT_I2C_READ_FAILURE 3
110#define XC_RESULT_OUT_OF_RANGE 5
111
112/* Product id */
113#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300114#define XC_PRODUCT_ID_FW_LOADED 0x0FA0
Davide Ferri8d009a02009-06-23 22:34:06 -0300115
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300116/* Registers (Write-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300117#define XREG_INIT 0x00
118#define XREG_VIDEO_MODE 0x01
119#define XREG_AUDIO_MODE 0x02
120#define XREG_RF_FREQ 0x03
121#define XREG_D_CODE 0x04
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300122#define XREG_DIRECTSITTING_MODE 0x05
123#define XREG_SEEK_MODE 0x06
124#define XREG_POWER_DOWN 0x08
125#define XREG_SIGNALSOURCE 0x0A
126#define XREG_AMPLITUDE 0x10
Davide Ferri8d009a02009-06-23 22:34:06 -0300127
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300128/* Registers (Read-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300129#define XREG_ADC_ENV 0x00
130#define XREG_QUALITY 0x01
131#define XREG_FRAME_LINES 0x02
132#define XREG_HSYNC_FREQ 0x03
133#define XREG_LOCK 0x04
134#define XREG_FREQ_ERROR 0x05
135#define XREG_SNR 0x06
136#define XREG_VERSION 0x07
137#define XREG_PRODUCT_ID 0x08
Davide Ferri8d009a02009-06-23 22:34:06 -0300138
139/*
140 Basic firmware description. This will remain with
141 the driver for documentation purposes.
142
143 This represents an I2C firmware file encoded as a
144 string of unsigned char. Format is as follows:
145
146 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
147 char[1 ]=len0_LSB -> length of first write transaction
148 char[2 ]=data0 -> first byte to be sent
149 char[3 ]=data1
150 char[4 ]=data2
151 char[ ]=...
152 char[M ]=dataN -> last byte to be sent
153 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
154 char[M+2]=len1_LSB -> length of second write transaction
155 char[M+3]=data0
156 char[M+4]=data1
157 ...
158 etc.
159
160 The [len] value should be interpreted as follows:
161
162 len= len_MSB _ len_LSB
163 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
164 len=0000_0000_0000_0000 : Reset command: Do hardware reset
165 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
166 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
167
168 For the RESET and WAIT commands, the two following bytes will contain
169 immediately the length of the following transaction.
Davide Ferri8d009a02009-06-23 22:34:06 -0300170*/
Istvan Vargafbe4a292011-06-03 10:11:48 -0300171
Davide Ferri8d009a02009-06-23 22:34:06 -0300172struct XC_TV_STANDARD {
Istvan Vargafbe4a292011-06-03 10:11:48 -0300173 const char *Name;
174 u16 AudioMode;
175 u16 VideoMode;
Istvan Varga49110852011-06-03 10:55:24 -0300176 u16 int_freq;
Davide Ferri8d009a02009-06-23 22:34:06 -0300177};
178
179/* Tuner standards */
Devin Heitmuellered23db32009-10-05 01:27:14 -0300180#define XC4000_MN_NTSC_PAL_BTSC 0
181#define XC4000_MN_NTSC_PAL_A2 1
182#define XC4000_MN_NTSC_PAL_EIAJ 2
183#define XC4000_MN_NTSC_PAL_Mono 3
184#define XC4000_BG_PAL_A2 4
185#define XC4000_BG_PAL_NICAM 5
186#define XC4000_BG_PAL_MONO 6
187#define XC4000_I_PAL_NICAM 7
188#define XC4000_I_PAL_NICAM_MONO 8
189#define XC4000_DK_PAL_A2 9
190#define XC4000_DK_PAL_NICAM 10
191#define XC4000_DK_PAL_MONO 11
192#define XC4000_DK_SECAM_A2DK1 12
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300193#define XC4000_DK_SECAM_A2LDK3 13
194#define XC4000_DK_SECAM_A2MONO 14
Istvan Varga49110852011-06-03 10:55:24 -0300195#define XC4000_DK_SECAM_NICAM 15
196#define XC4000_L_SECAM_NICAM 16
197#define XC4000_LC_SECAM_NICAM 17
198#define XC4000_DTV6 18
199#define XC4000_DTV8 19
200#define XC4000_DTV7_8 20
201#define XC4000_DTV7 21
202#define XC4000_FM_Radio_INPUT2 22
203#define XC4000_FM_Radio_INPUT1 23
Davide Ferri8d009a02009-06-23 22:34:06 -0300204
Davide Ferri8d009a02009-06-23 22:34:06 -0300205static struct XC_TV_STANDARD XC4000_Standard[MAX_TV_STANDARD] = {
Istvan Varga49110852011-06-03 10:55:24 -0300206 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
207 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
208 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
209 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
210 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
211 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
212 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
213 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
214 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
215 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
216 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
217 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
218 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
219 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
220 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
221 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
222 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
223 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
224 {"DTV6", 0x00C0, 0x8002, 0},
225 {"DTV8", 0x00C0, 0x800B, 0},
226 {"DTV7/8", 0x00C0, 0x801B, 0},
227 {"DTV7", 0x00C0, 0x8007, 0},
228 {"FM Radio-INPUT2", 0x0008, 0x9800,10700},
229 {"FM Radio-INPUT1", 0x0008, 0x9000,10700}
Davide Ferri8d009a02009-06-23 22:34:06 -0300230};
231
Davide Ferri8d009a02009-06-23 22:34:06 -0300232static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
233static int xc4000_TunerReset(struct dvb_frontend *fe);
234
235static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
236{
237 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
238 .flags = 0, .buf = buf, .len = len };
Davide Ferri8d009a02009-06-23 22:34:06 -0300239 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
Devin Heitmueller799ed112009-10-04 23:09:18 -0300240 if (priv->ignore_i2c_write_errors == 0) {
241 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
242 len);
243 if (len == 4) {
244 printk("bytes %02x %02x %02x %02x\n", buf[0],
245 buf[1], buf[2], buf[3]);
246 }
247 return XC_RESULT_I2C_WRITE_FAILURE;
248 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300249 }
250 return XC_RESULT_SUCCESS;
251}
252
Davide Ferri8d009a02009-06-23 22:34:06 -0300253static void xc_wait(int wait_ms)
254{
255 msleep(wait_ms);
256}
257
258static int xc4000_TunerReset(struct dvb_frontend *fe)
259{
260 struct xc4000_priv *priv = fe->tuner_priv;
261 int ret;
262
263 dprintk(1, "%s()\n", __func__);
264
265 if (fe->callback) {
266 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
267 fe->dvb->priv :
268 priv->i2c_props.adap->algo_data,
269 DVB_FRONTEND_COMPONENT_TUNER,
270 XC4000_TUNER_RESET, 0);
271 if (ret) {
272 printk(KERN_ERR "xc4000: reset failed\n");
273 return XC_RESULT_RESET_FAILURE;
274 }
275 } else {
276 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
277 return XC_RESULT_RESET_FAILURE;
278 }
279 return XC_RESULT_SUCCESS;
280}
281
282static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
283{
284 u8 buf[4];
Davide Ferri8d009a02009-06-23 22:34:06 -0300285 int result;
286
287 buf[0] = (regAddr >> 8) & 0xFF;
288 buf[1] = regAddr & 0xFF;
289 buf[2] = (i2cData >> 8) & 0xFF;
290 buf[3] = i2cData & 0xFF;
291 result = xc_send_i2c_data(priv, buf, 4);
Davide Ferri8d009a02009-06-23 22:34:06 -0300292
293 return result;
294}
295
296static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
297{
298 struct xc4000_priv *priv = fe->tuner_priv;
299
300 int i, nbytes_to_send, result;
301 unsigned int len, pos, index;
302 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
303
304 index = 0;
305 while ((i2c_sequence[index] != 0xFF) ||
306 (i2c_sequence[index + 1] != 0xFF)) {
307 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
308 if (len == 0x0000) {
309 /* RESET command */
310 result = xc4000_TunerReset(fe);
311 index += 2;
312 if (result != XC_RESULT_SUCCESS)
313 return result;
314 } else if (len & 0x8000) {
315 /* WAIT command */
316 xc_wait(len & 0x7FFF);
317 index += 2;
318 } else {
319 /* Send i2c data whilst ensuring individual transactions
320 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
321 */
322 index += 2;
323 buf[0] = i2c_sequence[index];
324 buf[1] = i2c_sequence[index + 1];
325 pos = 2;
326 while (pos < len) {
327 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
328 nbytes_to_send =
329 XC_MAX_I2C_WRITE_LENGTH;
330 else
331 nbytes_to_send = (len - pos + 2);
332 for (i = 2; i < nbytes_to_send; i++) {
333 buf[i] = i2c_sequence[index + pos +
334 i - 2];
335 }
336 result = xc_send_i2c_data(priv, buf,
337 nbytes_to_send);
338
339 if (result != XC_RESULT_SUCCESS)
340 return result;
341
342 pos += nbytes_to_send - 2;
343 }
344 index += len;
345 }
346 }
347 return XC_RESULT_SUCCESS;
348}
349
Davide Ferri8d009a02009-06-23 22:34:06 -0300350static int xc_SetTVStandard(struct xc4000_priv *priv,
351 u16 VideoMode, u16 AudioMode)
352{
353 int ret;
354 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
355 dprintk(1, "%s() Standard = %s\n",
356 __func__,
357 XC4000_Standard[priv->video_standard].Name);
358
Devin Heitmueller799ed112009-10-04 23:09:18 -0300359 /* Don't complain when the request fails because of i2c stretching */
360 priv->ignore_i2c_write_errors = 1;
361
Davide Ferri8d009a02009-06-23 22:34:06 -0300362 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
363 if (ret == XC_RESULT_SUCCESS)
364 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
365
Devin Heitmueller799ed112009-10-04 23:09:18 -0300366 priv->ignore_i2c_write_errors = 0;
367
Davide Ferri8d009a02009-06-23 22:34:06 -0300368 return ret;
369}
370
371static int xc_SetSignalSource(struct xc4000_priv *priv, u16 rf_mode)
372{
373 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
374 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
375
376 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
377 rf_mode = XC_RF_MODE_CABLE;
378 printk(KERN_ERR
379 "%s(), Invalid mode, defaulting to CABLE",
380 __func__);
381 }
382 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
383}
384
385static const struct dvb_tuner_ops xc4000_tuner_ops;
386
387static int xc_set_RF_frequency(struct xc4000_priv *priv, u32 freq_hz)
388{
389 u16 freq_code;
390
391 dprintk(1, "%s(%u)\n", __func__, freq_hz);
392
393 if ((freq_hz > xc4000_tuner_ops.info.frequency_max) ||
394 (freq_hz < xc4000_tuner_ops.info.frequency_min))
395 return XC_RESULT_OUT_OF_RANGE;
396
397 freq_code = (u16)(freq_hz / 15625);
398
399 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
400 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
401 only be used for fast scanning for channel lock) */
402 return xc_write_reg(priv, XREG_RF_FREQ, freq_code); /* WAS: XREG_FINERFREQ */
403}
404
Davide Ferri8d009a02009-06-23 22:34:06 -0300405static int xc_get_ADC_Envelope(struct xc4000_priv *priv, u16 *adc_envelope)
406{
407 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
408}
409
410static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
411{
412 int result;
413 u16 regData;
414 u32 tmp;
415
416 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
417 if (result != XC_RESULT_SUCCESS)
418 return result;
419
420 tmp = (u32)regData;
421 (*freq_error_hz) = (tmp * 15625) / 1000;
422 return result;
423}
424
425static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
426{
427 return xc4000_readreg(priv, XREG_LOCK, lock_status);
428}
429
430static int xc_get_version(struct xc4000_priv *priv,
431 u8 *hw_majorversion, u8 *hw_minorversion,
432 u8 *fw_majorversion, u8 *fw_minorversion)
433{
434 u16 data;
435 int result;
436
437 result = xc4000_readreg(priv, XREG_VERSION, &data);
438 if (result != XC_RESULT_SUCCESS)
439 return result;
440
441 (*hw_majorversion) = (data >> 12) & 0x0F;
442 (*hw_minorversion) = (data >> 8) & 0x0F;
443 (*fw_majorversion) = (data >> 4) & 0x0F;
444 (*fw_minorversion) = data & 0x0F;
445
446 return 0;
447}
448
Davide Ferri8d009a02009-06-23 22:34:06 -0300449static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
450{
451 u16 regData;
452 int result;
453
454 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
455 if (result != XC_RESULT_SUCCESS)
456 return result;
457
458 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
459 return result;
460}
461
462static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
463{
464 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
465}
466
467static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
468{
469 return xc4000_readreg(priv, XREG_QUALITY, quality);
470}
471
472static u16 WaitForLock(struct xc4000_priv *priv)
473{
474 u16 lockState = 0;
475 int watchDogCount = 40;
476
477 while ((lockState == 0) && (watchDogCount > 0)) {
478 xc_get_lock_status(priv, &lockState);
479 if (lockState != 1) {
480 xc_wait(5);
481 watchDogCount--;
482 }
483 }
484 return lockState;
485}
486
487#define XC_TUNE_ANALOG 0
488#define XC_TUNE_DIGITAL 1
489static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz, int mode)
490{
Istvan Vargafbe4a292011-06-03 10:11:48 -0300491 int found = 0;
492 int result = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300493
494 dprintk(1, "%s(%u)\n", __func__, freq_hz);
495
Devin Heitmueller799ed112009-10-04 23:09:18 -0300496 /* Don't complain when the request fails because of i2c stretching */
497 priv->ignore_i2c_write_errors = 1;
498 result = xc_set_RF_frequency(priv, freq_hz);
499 priv->ignore_i2c_write_errors = 0;
500
501 if (result != XC_RESULT_SUCCESS)
Davide Ferri8d009a02009-06-23 22:34:06 -0300502 return 0;
503
504 if (mode == XC_TUNE_ANALOG) {
505 if (WaitForLock(priv) == 1)
506 found = 1;
507 }
508
509 return found;
510}
511
512static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
513{
514 u8 buf[2] = { reg >> 8, reg & 0xff };
515 u8 bval[2] = { 0, 0 };
516 struct i2c_msg msg[2] = {
517 { .addr = priv->i2c_props.addr,
518 .flags = 0, .buf = &buf[0], .len = 2 },
519 { .addr = priv->i2c_props.addr,
520 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
521 };
522
523 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
524 printk(KERN_WARNING "xc4000: I2C read failed\n");
525 return -EREMOTEIO;
526 }
527
528 *val = (bval[0] << 8) | bval[1];
529 return XC_RESULT_SUCCESS;
530}
531
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300532#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300533static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
534{
535 if (type & BASE)
536 printk("BASE ");
537 if (type & INIT1)
538 printk("INIT1 ");
539 if (type & F8MHZ)
540 printk("F8MHZ ");
541 if (type & MTS)
542 printk("MTS ");
543 if (type & D2620)
544 printk("D2620 ");
545 if (type & D2633)
546 printk("D2633 ");
547 if (type & DTV6)
548 printk("DTV6 ");
549 if (type & QAM)
550 printk("QAM ");
551 if (type & DTV7)
552 printk("DTV7 ");
553 if (type & DTV78)
554 printk("DTV78 ");
555 if (type & DTV8)
556 printk("DTV8 ");
557 if (type & FM)
558 printk("FM ");
559 if (type & INPUT1)
560 printk("INPUT1 ");
561 if (type & LCD)
562 printk("LCD ");
563 if (type & NOGD)
564 printk("NOGD ");
565 if (type & MONO)
566 printk("MONO ");
567 if (type & ATSC)
568 printk("ATSC ");
569 if (type & IF)
570 printk("IF ");
571 if (type & LG60)
572 printk("LG60 ");
573 if (type & ATI638)
574 printk("ATI638 ");
575 if (type & OREN538)
576 printk("OREN538 ");
577 if (type & OREN36)
578 printk("OREN36 ");
579 if (type & TOYOTA388)
580 printk("TOYOTA388 ");
581 if (type & TOYOTA794)
582 printk("TOYOTA794 ");
583 if (type & DIBCOM52)
584 printk("DIBCOM52 ");
585 if (type & ZARLINK456)
586 printk("ZARLINK456 ");
587 if (type & CHINA)
588 printk("CHINA ");
589 if (type & F6MHZ)
590 printk("F6MHZ ");
591 if (type & INPUT2)
592 printk("INPUT2 ");
593 if (type & SCODE)
594 printk("SCODE ");
595 if (type & HAS_IF)
596 printk("HAS_IF_%d ", int_freq);
597}
598
Devin Heitmueller11091a32009-07-20 00:54:57 -0300599static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
600 v4l2_std_id *id)
601{
602 struct xc4000_priv *priv = fe->tuner_priv;
603 int i, best_i = -1, best_nr_matches = 0;
604 unsigned int type_mask = 0;
605
Devin Heitmueller11091a32009-07-20 00:54:57 -0300606 if (!priv->firm) {
607 printk("Error! firmware not loaded\n");
608 return -EINVAL;
609 }
610
611 if (((type & ~SCODE) == 0) && (*id == 0))
612 *id = V4L2_STD_PAL;
613
614 if (type & BASE)
615 type_mask = BASE_TYPES;
616 else if (type & SCODE) {
617 type &= SCODE_TYPES;
618 type_mask = SCODE_TYPES & ~HAS_IF;
619 } else if (type & DTV_TYPES)
620 type_mask = DTV_TYPES;
621 else if (type & STD_SPECIFIC_TYPES)
622 type_mask = STD_SPECIFIC_TYPES;
623
624 type &= type_mask;
625
626 if (!(type & SCODE))
627 type_mask = ~0;
628
629 /* Seek for exact match */
630 for (i = 0; i < priv->firm_size; i++) {
631 if ((type == (priv->firm[i].type & type_mask)) &&
632 (*id == priv->firm[i].id))
633 goto found;
634 }
635
636 /* Seek for generic video standard match */
637 for (i = 0; i < priv->firm_size; i++) {
638 v4l2_std_id match_mask;
639 int nr_matches;
640
641 if (type != (priv->firm[i].type & type_mask))
642 continue;
643
644 match_mask = *id & priv->firm[i].id;
645 if (!match_mask)
646 continue;
647
648 if ((*id & match_mask) == *id)
649 goto found; /* Supports all the requested standards */
650
651 nr_matches = hweight64(match_mask);
652 if (nr_matches > best_nr_matches) {
653 best_nr_matches = nr_matches;
654 best_i = i;
655 }
656 }
657
658 if (best_nr_matches > 0) {
659 printk("Selecting best matching firmware (%d bits) for "
660 "type=", best_nr_matches);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300661 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
662 i = best_i;
663 goto found;
664 }
665
666 /*FIXME: Would make sense to seek for type "hint" match ? */
667
668 i = -ENOENT;
669 goto ret;
670
671found:
672 *id = priv->firm[i].id;
673
674ret:
Devin Heitmueller11091a32009-07-20 00:54:57 -0300675 if (debug) {
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300676 printk("%s firmware for type=", (i < 0) ? "Can't find" :
677 "Found");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300678 dump_firm_type(type);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300679 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
680 }
681 return i;
682}
683
684static int load_firmware(struct dvb_frontend *fe, unsigned int type,
685 v4l2_std_id *id)
686{
687 struct xc4000_priv *priv = fe->tuner_priv;
688 int pos, rc;
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300689 unsigned char *p;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300690
Devin Heitmueller11091a32009-07-20 00:54:57 -0300691 pos = seek_firmware(fe, type, id);
692 if (pos < 0)
693 return pos;
694
Devin Heitmueller11091a32009-07-20 00:54:57 -0300695 p = priv->firm[pos].ptr;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300696
Devin Heitmueller799ed112009-10-04 23:09:18 -0300697 /* Don't complain when the request fails because of i2c stretching */
698 priv->ignore_i2c_write_errors = 1;
699
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300700 rc = xc_load_i2c_sequence(fe, p);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300701
Devin Heitmueller799ed112009-10-04 23:09:18 -0300702 priv->ignore_i2c_write_errors = 0;
703
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300704 return rc;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300705}
706
Davide Ferri8d009a02009-06-23 22:34:06 -0300707static int xc4000_fwupload(struct dvb_frontend *fe)
708{
709 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300710 const struct firmware *fw = NULL;
711 const unsigned char *p, *endp;
712 int rc = 0;
713 int n, n_array;
714 char name[33];
Istvan Vargafbe4a292011-06-03 10:11:48 -0300715 const char *fname;
Davide Ferri8d009a02009-06-23 22:34:06 -0300716
Devin Heitmueller11091a32009-07-20 00:54:57 -0300717 fname = XC4000_DEFAULT_FIRMWARE;
718
719 printk("Reading firmware %s\n", fname);
720 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
721 if (rc < 0) {
722 if (rc == -ENOENT)
723 printk("Error: firmware %s not found.\n",
724 fname);
725 else
726 printk("Error %d while requesting firmware %s \n",
727 rc, fname);
728
729 return rc;
730 }
731 p = fw->data;
732 endp = p + fw->size;
733
734 if (fw->size < sizeof(name) - 1 + 2 + 2) {
735 printk("Error: firmware file %s has invalid size!\n",
Istvan Vargafbe4a292011-06-03 10:11:48 -0300736 fname);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300737 goto corrupt;
Davide Ferri8d009a02009-06-23 22:34:06 -0300738 }
739
Devin Heitmueller11091a32009-07-20 00:54:57 -0300740 memcpy(name, p, sizeof(name) - 1);
741 name[sizeof(name) - 1] = 0;
742 p += sizeof(name) - 1;
743
744 priv->firm_version = get_unaligned_le16(p);
745 p += 2;
746
747 n_array = get_unaligned_le16(p);
748 p += 2;
749
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300750 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
751 n_array, fname, name,
752 priv->firm_version >> 8, priv->firm_version & 0xff);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300753
754 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
755 if (priv->firm == NULL) {
756 printk("Not enough memory to load firmware file.\n");
757 rc = -ENOMEM;
758 goto err;
759 }
760 priv->firm_size = n_array;
761
762 n = -1;
763 while (p < endp) {
764 __u32 type, size;
765 v4l2_std_id id;
766 __u16 int_freq = 0;
767
768 n++;
769 if (n >= n_array) {
770 printk("More firmware images in file than "
Istvan Vargafbe4a292011-06-03 10:11:48 -0300771 "were expected!\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300772 goto corrupt;
773 }
774
775 /* Checks if there's enough bytes to read */
776 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
777 goto header;
778
779 type = get_unaligned_le32(p);
780 p += sizeof(type);
781
782 id = get_unaligned_le64(p);
783 p += sizeof(id);
784
785 if (type & HAS_IF) {
786 int_freq = get_unaligned_le16(p);
787 p += sizeof(int_freq);
788 if (endp - p < sizeof(size))
789 goto header;
790 }
791
792 size = get_unaligned_le32(p);
793 p += sizeof(size);
794
795 if (!size || size > endp - p) {
796 printk("Firmware type ");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300797 printk("(%x), id %llx is corrupted "
798 "(size=%d, expected %d)\n",
799 type, (unsigned long long)id,
800 (unsigned)(endp - p), size);
801 goto corrupt;
802 }
803
804 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
805 if (priv->firm[n].ptr == NULL) {
806 printk("Not enough memory to load firmware file.\n");
807 rc = -ENOMEM;
808 goto err;
809 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300810
Devin Heitmueller11091a32009-07-20 00:54:57 -0300811 if (debug) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300812 printk("Reading firmware type ");
813 dump_firm_type_and_int_freq(type, int_freq);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300814 printk("(%x), id %llx, size=%d.\n",
815 type, (unsigned long long)id, size);
816 }
817
818 memcpy(priv->firm[n].ptr, p, size);
819 priv->firm[n].type = type;
820 priv->firm[n].id = id;
821 priv->firm[n].size = size;
822 priv->firm[n].int_freq = int_freq;
823
824 p += size;
Davide Ferri8d009a02009-06-23 22:34:06 -0300825 }
826
Devin Heitmueller11091a32009-07-20 00:54:57 -0300827 if (n + 1 != priv->firm_size) {
828 printk("Firmware file is incomplete!\n");
829 goto corrupt;
830 }
831
832 goto done;
833
834header:
835 printk("Firmware header is incomplete!\n");
836corrupt:
837 rc = -EINVAL;
838 printk("Error: firmware file is corrupted!\n");
839
840err:
841 printk("Releasing partially loaded firmware file.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300842
843done:
Davide Ferri8d009a02009-06-23 22:34:06 -0300844 release_firmware(fw);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300845 if (rc == 0)
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300846 dprintk(1, "Firmware files loaded.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300847
848 return rc;
Davide Ferri8d009a02009-06-23 22:34:06 -0300849}
850
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300851static int load_scode(struct dvb_frontend *fe, unsigned int type,
852 v4l2_std_id *id, __u16 int_freq, int scode)
853{
854 struct xc4000_priv *priv = fe->tuner_priv;
855 int pos, rc;
856 unsigned char *p;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300857 u8 scode_buf[13];
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300858 u8 indirect_mode[5];
859
Devin Heitmuellerfe830362009-07-28 00:04:27 -0300860 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300861
862 if (!int_freq) {
863 pos = seek_firmware(fe, type, id);
864 if (pos < 0)
865 return pos;
866 } else {
867 for (pos = 0; pos < priv->firm_size; pos++) {
868 if ((priv->firm[pos].int_freq == int_freq) &&
869 (priv->firm[pos].type & HAS_IF))
870 break;
871 }
872 if (pos == priv->firm_size)
873 return -ENOENT;
874 }
875
876 p = priv->firm[pos].ptr;
877
878 if (priv->firm[pos].type & HAS_IF) {
879 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
880 return -EINVAL;
881 p += 12 * scode;
882 } else {
883 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
884 * has a 2-byte size header in the firmware format. */
885 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
886 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
887 return -EINVAL;
888 p += 14 * scode + 2;
889 }
890
891 tuner_info("Loading SCODE for type=");
892 dump_firm_type_and_int_freq(priv->firm[pos].type,
893 priv->firm[pos].int_freq);
894 printk("(%x), id %016llx.\n", priv->firm[pos].type,
895 (unsigned long long)*id);
896
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300897 scode_buf[0] = 0x00;
898 memcpy(&scode_buf[1], p, 12);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300899
900 /* Enter direct-mode */
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300901 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
902 if (rc < 0) {
903 printk("failed to put device into direct mode!\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300904 return -EIO;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300905 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300906
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300907 rc = xc_send_i2c_data(priv, scode_buf, 13);
908 if (rc != XC_RESULT_SUCCESS) {
909 /* Even if the send failed, make sure we set back to indirect
910 mode */
911 printk("Failed to set scode %d\n", rc);
912 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300913
914 /* Switch back to indirect-mode */
915 memset(indirect_mode, 0, sizeof(indirect_mode));
916 indirect_mode[4] = 0x88;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300917 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
918 msleep(10);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300919
920 return 0;
921}
922
923static int check_firmware(struct dvb_frontend *fe, unsigned int type,
924 v4l2_std_id std, __u16 int_freq)
925{
926 struct xc4000_priv *priv = fe->tuner_priv;
927 struct firmware_properties new_fw;
928 int rc = 0, is_retry = 0;
929 u16 version, hwmodel;
930 v4l2_std_id std0;
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300931 u8 hw_major, hw_minor, fw_major, fw_minor;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300932
933 dprintk(1, "%s called\n", __func__);
934
935 if (!priv->firm) {
936 rc = xc4000_fwupload(fe);
937 if (rc < 0)
938 return rc;
939 }
940
941#ifdef DJH_DEBUG
942 if (priv->ctrl.mts && !(type & FM))
943 type |= MTS;
944#endif
945
946retry:
947 new_fw.type = type;
948 new_fw.id = std;
949 new_fw.std_req = std;
Istvan Vargafbe4a292011-06-03 10:11:48 -0300950 new_fw.scode_table = SCODE /* | priv->ctrl.scode_table */;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300951 new_fw.scode_nr = 0;
952 new_fw.int_freq = int_freq;
953
954 dprintk(1, "checking firmware, user requested type=");
955 if (debug) {
956 dump_firm_type(new_fw.type);
957 printk("(%x), id %016llx, ", new_fw.type,
958 (unsigned long long)new_fw.std_req);
959 if (!int_freq) {
960 printk("scode_tbl ");
961#ifdef DJH_DEBUG
962 dump_firm_type(priv->ctrl.scode_table);
963 printk("(%x), ", priv->ctrl.scode_table);
964#endif
965 } else
966 printk("int_freq %d, ", new_fw.int_freq);
967 printk("scode_nr %d\n", new_fw.scode_nr);
968 }
969
970 /* No need to reload base firmware if it matches */
971 if (((BASE | new_fw.type) & BASE_TYPES) ==
972 (priv->cur_fw.type & BASE_TYPES)) {
973 dprintk(1, "BASE firmware not changed.\n");
974 goto skip_base;
975 }
976
977 /* Updating BASE - forget about all currently loaded firmware */
978 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
979
980 /* Reset is needed before loading firmware */
981 rc = xc4000_TunerReset(fe);
982 if (rc < 0)
983 goto fail;
984
985 /* BASE firmwares are all std0 */
986 std0 = 0;
987 rc = load_firmware(fe, BASE | new_fw.type, &std0);
988 if (rc < 0) {
989 printk("Error %d while loading base firmware\n", rc);
990 goto fail;
991 }
992
993 /* Load INIT1, if needed */
994 dprintk(1, "Load init1 firmware, if exists\n");
995
996 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
997 if (rc == -ENOENT)
998 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
999 &std0);
1000 if (rc < 0 && rc != -ENOENT) {
1001 tuner_err("Error %d while loading init1 firmware\n",
1002 rc);
1003 goto fail;
1004 }
1005
1006skip_base:
1007 /*
1008 * No need to reload standard specific firmware if base firmware
1009 * was not reloaded and requested video standards have not changed.
1010 */
1011 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1012 priv->cur_fw.std_req == std) {
1013 dprintk(1, "Std-specific firmware already loaded.\n");
1014 goto skip_std_specific;
1015 }
1016
1017 /* Reloading std-specific firmware forces a SCODE update */
1018 priv->cur_fw.scode_table = 0;
1019
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001020 /* Load the standard firmware */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001021 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001022
1023 if (rc < 0)
1024 goto fail;
1025
1026skip_std_specific:
1027 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1028 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1029 dprintk(1, "SCODE firmware already loaded.\n");
1030 goto check_device;
1031 }
1032
1033 if (new_fw.type & FM)
1034 goto check_device;
1035
1036 /* Load SCODE firmware, if exists */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001037 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1038 new_fw.int_freq, new_fw.scode_nr);
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001039 if (rc != XC_RESULT_SUCCESS)
1040 dprintk(1, "load scode failed %d\n", rc);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001041
1042check_device:
1043 rc = xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel);
1044
Devin Heitmueller799ed112009-10-04 23:09:18 -03001045 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001046 &fw_minor) != XC_RESULT_SUCCESS) {
1047 printk("Unable to read tuner registers.\n");
1048 goto fail;
1049 }
1050
1051 dprintk(1, "Device is Xceive %d version %d.%d, "
1052 "firmware version %d.%d\n",
1053 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1054
1055 /* Check firmware version against what we downloaded. */
1056#ifdef DJH_DEBUG
1057 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
1058 printk("Incorrect readback of firmware version %x.\n",
1059 (version & 0xff));
1060 goto fail;
1061 }
1062#endif
1063
1064 /* Check that the tuner hardware model remains consistent over time. */
1065 if (priv->hwmodel == 0 && hwmodel == 4000) {
1066 priv->hwmodel = hwmodel;
1067 priv->hwvers = version & 0xff00;
1068 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
1069 priv->hwvers != (version & 0xff00)) {
1070 printk("Read invalid device hardware information - tuner "
Istvan Vargafbe4a292011-06-03 10:11:48 -03001071 "hung?\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001072 goto fail;
1073 }
1074
1075 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
1076
1077 /*
1078 * By setting BASE in cur_fw.type only after successfully loading all
1079 * firmwares, we can:
1080 * 1. Identify that BASE firmware with type=0 has been loaded;
1081 * 2. Tell whether BASE firmware was just changed the next time through.
1082 */
1083 priv->cur_fw.type |= BASE;
1084
1085 return 0;
1086
1087fail:
1088 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1089 if (!is_retry) {
1090 msleep(50);
1091 is_retry = 1;
1092 dprintk(1, "Retrying firmware load\n");
1093 goto retry;
1094 }
1095
1096 if (rc == -ENOENT)
1097 rc = -EINVAL;
1098 return rc;
1099}
Devin Heitmueller11091a32009-07-20 00:54:57 -03001100
Davide Ferri8d009a02009-06-23 22:34:06 -03001101static void xc_debug_dump(struct xc4000_priv *priv)
1102{
Istvan Vargafbe4a292011-06-03 10:11:48 -03001103 u16 adc_envelope;
1104 u32 freq_error_hz = 0;
1105 u16 lock_status;
1106 u32 hsync_freq_hz = 0;
1107 u16 frame_lines;
1108 u16 quality;
1109 u8 hw_majorversion = 0, hw_minorversion = 0;
1110 u8 fw_majorversion = 0, fw_minorversion = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001111
1112 /* Wait for stats to stabilize.
1113 * Frame Lines needs two frame times after initial lock
1114 * before it is valid.
1115 */
1116 xc_wait(100);
1117
Istvan Vargafbe4a292011-06-03 10:11:48 -03001118 xc_get_ADC_Envelope(priv, &adc_envelope);
Davide Ferri8d009a02009-06-23 22:34:06 -03001119 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1120
1121 xc_get_frequency_error(priv, &freq_error_hz);
1122 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1123
Istvan Vargafbe4a292011-06-03 10:11:48 -03001124 xc_get_lock_status(priv, &lock_status);
Davide Ferri8d009a02009-06-23 22:34:06 -03001125 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1126 lock_status);
1127
Istvan Vargafbe4a292011-06-03 10:11:48 -03001128 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1129 &fw_majorversion, &fw_minorversion);
1130
Davide Ferri8d009a02009-06-23 22:34:06 -03001131 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1132 hw_majorversion, hw_minorversion,
1133 fw_majorversion, fw_minorversion);
1134
Istvan Vargafbe4a292011-06-03 10:11:48 -03001135 xc_get_hsync_freq(priv, &hsync_freq_hz);
Davide Ferri8d009a02009-06-23 22:34:06 -03001136 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
1137
Istvan Vargafbe4a292011-06-03 10:11:48 -03001138 xc_get_frame_lines(priv, &frame_lines);
Davide Ferri8d009a02009-06-23 22:34:06 -03001139 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1140
Istvan Vargafbe4a292011-06-03 10:11:48 -03001141 xc_get_quality(priv, &quality);
Davide Ferri8d009a02009-06-23 22:34:06 -03001142 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
1143}
1144
1145static int xc4000_set_params(struct dvb_frontend *fe,
1146 struct dvb_frontend_parameters *params)
1147{
1148 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001149 unsigned int type;
Istvan Varga56149422011-06-03 12:23:33 -03001150 int ret = -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -03001151
Davide Ferri8d009a02009-06-23 22:34:06 -03001152 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, params->frequency);
1153
Istvan Varga56149422011-06-03 12:23:33 -03001154 mutex_lock(&priv->lock);
1155
Davide Ferri8d009a02009-06-23 22:34:06 -03001156 if (fe->ops.info.type == FE_ATSC) {
1157 dprintk(1, "%s() ATSC\n", __func__);
1158 switch (params->u.vsb.modulation) {
1159 case VSB_8:
1160 case VSB_16:
1161 dprintk(1, "%s() VSB modulation\n", __func__);
1162 priv->rf_mode = XC_RF_MODE_AIR;
1163 priv->freq_hz = params->frequency - 1750000;
1164 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001165 priv->video_standard = XC4000_DTV6;
1166 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001167 break;
1168 case QAM_64:
1169 case QAM_256:
1170 case QAM_AUTO:
1171 dprintk(1, "%s() QAM modulation\n", __func__);
1172 priv->rf_mode = XC_RF_MODE_CABLE;
1173 priv->freq_hz = params->frequency - 1750000;
1174 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001175 priv->video_standard = XC4000_DTV6;
1176 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001177 break;
1178 default:
Istvan Varga56149422011-06-03 12:23:33 -03001179 ret = -EINVAL;
1180 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001181 }
1182 } else if (fe->ops.info.type == FE_OFDM) {
1183 dprintk(1, "%s() OFDM\n", __func__);
1184 switch (params->u.ofdm.bandwidth) {
1185 case BANDWIDTH_6_MHZ:
1186 priv->bandwidth = BANDWIDTH_6_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001187 priv->video_standard = XC4000_DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001188 priv->freq_hz = params->frequency - 1750000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001189 type = DTV6;
Davide Ferri8d009a02009-06-23 22:34:06 -03001190 break;
1191 case BANDWIDTH_7_MHZ:
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001192 priv->bandwidth = BANDWIDTH_7_MHZ;
1193 priv->video_standard = XC4000_DTV7;
1194 priv->freq_hz = params->frequency - 2250000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001195 type = DTV7;
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001196 break;
Davide Ferri8d009a02009-06-23 22:34:06 -03001197 case BANDWIDTH_8_MHZ:
1198 priv->bandwidth = BANDWIDTH_8_MHZ;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001199 priv->video_standard = XC4000_DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001200 priv->freq_hz = params->frequency - 2750000;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001201 type = DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001202 break;
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001203 case BANDWIDTH_AUTO:
1204 if (params->frequency < 400000000) {
1205 priv->bandwidth = BANDWIDTH_7_MHZ;
1206 priv->freq_hz = params->frequency - 2250000;
1207 } else {
1208 priv->bandwidth = BANDWIDTH_8_MHZ;
1209 priv->freq_hz = params->frequency - 2750000;
1210 }
1211 priv->video_standard = XC4000_DTV7_8;
1212 type = DTV78;
1213 break;
Davide Ferri8d009a02009-06-23 22:34:06 -03001214 default:
1215 printk(KERN_ERR "xc4000 bandwidth not set!\n");
Istvan Varga56149422011-06-03 12:23:33 -03001216 ret = -EINVAL;
1217 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001218 }
1219 priv->rf_mode = XC_RF_MODE_AIR;
1220 } else {
1221 printk(KERN_ERR "xc4000 modulation type not supported!\n");
Istvan Varga56149422011-06-03 12:23:33 -03001222 ret = -EINVAL;
1223 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001224 }
1225
1226 dprintk(1, "%s() frequency=%d (compensated)\n",
1227 __func__, priv->freq_hz);
1228
Devin Heitmuellered23db32009-10-05 01:27:14 -03001229 /* Make sure the correct firmware type is loaded */
Istvan Varga56149422011-06-03 12:23:33 -03001230 if (check_firmware(fe, type, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1231 goto fail;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001232
Davide Ferri8d009a02009-06-23 22:34:06 -03001233 ret = xc_SetSignalSource(priv, priv->rf_mode);
1234 if (ret != XC_RESULT_SUCCESS) {
1235 printk(KERN_ERR
Istvan Varga56149422011-06-03 12:23:33 -03001236 "xc4000: xc_SetSignalSource(%d) failed\n",
1237 priv->rf_mode);
1238 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001239 }
1240
1241 ret = xc_SetTVStandard(priv,
1242 XC4000_Standard[priv->video_standard].VideoMode,
1243 XC4000_Standard[priv->video_standard].AudioMode);
1244 if (ret != XC_RESULT_SUCCESS) {
1245 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
Istvan Varga56149422011-06-03 12:23:33 -03001246 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001247 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001248 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
1249
1250 if (debug)
1251 xc_debug_dump(priv);
1252
Istvan Varga56149422011-06-03 12:23:33 -03001253 ret = 0;
1254
1255fail:
1256 mutex_unlock(&priv->lock);
1257
1258 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001259}
1260
Davide Ferri8d009a02009-06-23 22:34:06 -03001261static int xc4000_set_analog_params(struct dvb_frontend *fe,
1262 struct analog_parameters *params)
1263{
1264 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga56149422011-06-03 12:23:33 -03001265 int ret = -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -03001266
Davide Ferri8d009a02009-06-23 22:34:06 -03001267 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1268 __func__, params->frequency);
1269
Istvan Varga56149422011-06-03 12:23:33 -03001270 mutex_lock(&priv->lock);
1271
Davide Ferri8d009a02009-06-23 22:34:06 -03001272 /* Fix me: it could be air. */
1273 priv->rf_mode = params->mode;
1274 if (params->mode > XC_RF_MODE_CABLE)
1275 priv->rf_mode = XC_RF_MODE_CABLE;
1276
1277 /* params->frequency is in units of 62.5khz */
1278 priv->freq_hz = params->frequency * 62500;
1279
1280 /* FIX ME: Some video standards may have several possible audio
1281 standards. We simply default to one of them here.
1282 */
1283 if (params->std & V4L2_STD_MN) {
1284 /* default to BTSC audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001285 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
Davide Ferri8d009a02009-06-23 22:34:06 -03001286 goto tune_channel;
1287 }
1288
1289 if (params->std & V4L2_STD_PAL_BG) {
1290 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001291 priv->video_standard = XC4000_BG_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001292 goto tune_channel;
1293 }
1294
1295 if (params->std & V4L2_STD_PAL_I) {
1296 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001297 priv->video_standard = XC4000_I_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001298 goto tune_channel;
1299 }
1300
1301 if (params->std & V4L2_STD_PAL_DK) {
1302 /* default to NICAM audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001303 priv->video_standard = XC4000_DK_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001304 goto tune_channel;
1305 }
1306
1307 if (params->std & V4L2_STD_SECAM_DK) {
1308 /* default to A2 DK1 audio standard */
Devin Heitmuellered23db32009-10-05 01:27:14 -03001309 priv->video_standard = XC4000_DK_SECAM_A2DK1;
Davide Ferri8d009a02009-06-23 22:34:06 -03001310 goto tune_channel;
1311 }
1312
1313 if (params->std & V4L2_STD_SECAM_L) {
Devin Heitmuellered23db32009-10-05 01:27:14 -03001314 priv->video_standard = XC4000_L_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001315 goto tune_channel;
1316 }
1317
1318 if (params->std & V4L2_STD_SECAM_LC) {
Devin Heitmuellered23db32009-10-05 01:27:14 -03001319 priv->video_standard = XC4000_LC_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001320 goto tune_channel;
1321 }
1322
1323tune_channel:
Devin Heitmuellered23db32009-10-05 01:27:14 -03001324
1325 /* FIXME - firmware type not being set properly */
Istvan Varga56149422011-06-03 12:23:33 -03001326 if (check_firmware(fe, DTV8, 0, priv->if_khz) != XC_RESULT_SUCCESS)
1327 goto fail;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001328
Davide Ferri8d009a02009-06-23 22:34:06 -03001329 ret = xc_SetSignalSource(priv, priv->rf_mode);
1330 if (ret != XC_RESULT_SUCCESS) {
1331 printk(KERN_ERR
Istvan Varga56149422011-06-03 12:23:33 -03001332 "xc4000: xc_SetSignalSource(%d) failed\n",
1333 priv->rf_mode);
1334 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001335 }
1336
1337 ret = xc_SetTVStandard(priv,
1338 XC4000_Standard[priv->video_standard].VideoMode,
1339 XC4000_Standard[priv->video_standard].AudioMode);
1340 if (ret != XC_RESULT_SUCCESS) {
1341 printk(KERN_ERR "xc4000: xc_SetTVStandard failed\n");
Istvan Varga56149422011-06-03 12:23:33 -03001342 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001343 }
1344
1345 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1346
1347 if (debug)
1348 xc_debug_dump(priv);
1349
Istvan Varga56149422011-06-03 12:23:33 -03001350 ret = 0;
1351
1352fail:
1353 mutex_unlock(&priv->lock);
1354
1355 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001356}
1357
1358static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1359{
1360 struct xc4000_priv *priv = fe->tuner_priv;
1361 dprintk(1, "%s()\n", __func__);
1362 *freq = priv->freq_hz;
1363 return 0;
1364}
1365
1366static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1367{
1368 struct xc4000_priv *priv = fe->tuner_priv;
1369 dprintk(1, "%s()\n", __func__);
1370
1371 *bw = priv->bandwidth;
1372 return 0;
1373}
1374
1375static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1376{
1377 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001378 u16 lock_status = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001379
Istvan Varga56149422011-06-03 12:23:33 -03001380 mutex_lock(&priv->lock);
1381
Davide Ferri8d009a02009-06-23 22:34:06 -03001382 xc_get_lock_status(priv, &lock_status);
1383
Istvan Varga56149422011-06-03 12:23:33 -03001384 mutex_unlock(&priv->lock);
1385
Davide Ferri8d009a02009-06-23 22:34:06 -03001386 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
1387
1388 *status = lock_status;
1389
1390 return 0;
1391}
1392
Davide Ferri8d009a02009-06-23 22:34:06 -03001393static int xc4000_sleep(struct dvb_frontend *fe)
1394{
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001395 /* FIXME: djh disable this for now... */
1396 return XC_RESULT_SUCCESS;
Davide Ferri8d009a02009-06-23 22:34:06 -03001397}
1398
1399static int xc4000_init(struct dvb_frontend *fe)
1400{
1401 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga56149422011-06-03 12:23:33 -03001402 int ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001403 dprintk(1, "%s()\n", __func__);
1404
Istvan Varga56149422011-06-03 12:23:33 -03001405 mutex_lock(&priv->lock);
1406 ret = check_firmware(fe, DTV8, 0, priv->if_khz);
1407 mutex_unlock(&priv->lock);
1408 if (ret != XC_RESULT_SUCCESS) {
Davide Ferri8d009a02009-06-23 22:34:06 -03001409 printk(KERN_ERR "xc4000: Unable to initialise tuner\n");
1410 return -EREMOTEIO;
1411 }
1412
1413 if (debug)
1414 xc_debug_dump(priv);
1415
1416 return 0;
1417}
1418
1419static int xc4000_release(struct dvb_frontend *fe)
1420{
1421 struct xc4000_priv *priv = fe->tuner_priv;
1422
1423 dprintk(1, "%s()\n", __func__);
1424
1425 mutex_lock(&xc4000_list_mutex);
1426
1427 if (priv)
1428 hybrid_tuner_release_state(priv);
1429
1430 mutex_unlock(&xc4000_list_mutex);
1431
1432 fe->tuner_priv = NULL;
1433
1434 return 0;
1435}
1436
1437static const struct dvb_tuner_ops xc4000_tuner_ops = {
1438 .info = {
1439 .name = "Xceive XC4000",
1440 .frequency_min = 1000000,
1441 .frequency_max = 1023000000,
1442 .frequency_step = 50000,
1443 },
1444
1445 .release = xc4000_release,
1446 .init = xc4000_init,
1447 .sleep = xc4000_sleep,
1448
1449 .set_params = xc4000_set_params,
1450 .set_analog_params = xc4000_set_analog_params,
1451 .get_frequency = xc4000_get_frequency,
1452 .get_bandwidth = xc4000_get_bandwidth,
1453 .get_status = xc4000_get_status
1454};
1455
1456struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1457 struct i2c_adapter *i2c,
1458 struct xc4000_config *cfg)
1459{
1460 struct xc4000_priv *priv = NULL;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001461 int instance;
1462 u16 id = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001463
1464 dprintk(1, "%s(%d-%04x)\n", __func__,
1465 i2c ? i2c_adapter_id(i2c) : -1,
1466 cfg ? cfg->i2c_address : -1);
1467
1468 mutex_lock(&xc4000_list_mutex);
1469
1470 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1471 hybrid_tuner_instance_list,
1472 i2c, cfg->i2c_address, "xc4000");
1473 switch (instance) {
1474 case 0:
1475 goto fail;
1476 break;
1477 case 1:
1478 /* new tuner instance */
1479 priv->bandwidth = BANDWIDTH_6_MHZ;
Istvan Varga56149422011-06-03 12:23:33 -03001480 mutex_init(&priv->lock);
Davide Ferri8d009a02009-06-23 22:34:06 -03001481 fe->tuner_priv = priv;
1482 break;
1483 default:
1484 /* existing tuner instance */
1485 fe->tuner_priv = priv;
1486 break;
1487 }
1488
1489 if (priv->if_khz == 0) {
1490 /* If the IF hasn't been set yet, use the value provided by
1491 the caller (occurs in hybrid devices where the analog
1492 call to xc4000_attach occurs before the digital side) */
1493 priv->if_khz = cfg->if_khz;
1494 }
1495
1496 /* Check if firmware has been loaded. It is possible that another
1497 instance of the driver has loaded the firmware.
1498 */
1499
1500 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
1501 goto fail;
1502
1503 switch (id) {
1504 case XC_PRODUCT_ID_FW_LOADED:
1505 printk(KERN_INFO
1506 "xc4000: Successfully identified at address 0x%02x\n",
1507 cfg->i2c_address);
1508 printk(KERN_INFO
1509 "xc4000: Firmware has been loaded previously\n");
1510 break;
1511 case XC_PRODUCT_ID_FW_NOT_LOADED:
1512 printk(KERN_INFO
1513 "xc4000: Successfully identified at address 0x%02x\n",
1514 cfg->i2c_address);
1515 printk(KERN_INFO
1516 "xc4000: Firmware has not been loaded previously\n");
1517 break;
1518 default:
1519 printk(KERN_ERR
1520 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1521 cfg->i2c_address, id);
1522 goto fail;
1523 }
1524
1525 mutex_unlock(&xc4000_list_mutex);
1526
1527 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1528 sizeof(struct dvb_tuner_ops));
1529
Devin Heitmueller11091a32009-07-20 00:54:57 -03001530 /* FIXME: For now, load the firmware at startup. We will remove this
1531 before the code goes to production... */
Istvan Varga56149422011-06-03 12:23:33 -03001532 mutex_lock(&priv->lock);
Devin Heitmuellerfe830362009-07-28 00:04:27 -03001533 check_firmware(fe, DTV8, 0, priv->if_khz);
Istvan Varga56149422011-06-03 12:23:33 -03001534 mutex_unlock(&priv->lock);
Devin Heitmueller11091a32009-07-20 00:54:57 -03001535
Davide Ferri8d009a02009-06-23 22:34:06 -03001536 return fe;
1537fail:
1538 mutex_unlock(&xc4000_list_mutex);
1539
1540 xc4000_release(fe);
1541 return NULL;
1542}
1543EXPORT_SYMBOL(xc4000_attach);
1544
1545MODULE_AUTHOR("Steven Toth, Davide Ferri");
1546MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1547MODULE_LICENSE("GPL");