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