]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/tuners/xc5000.c
[media] xc5000: Don't wrap msleep()
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / tuners / xc5000.c
CommitLineData
aacb9d31
ST
1/*
2 * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
3 *
4 * Copyright (c) 2007 Xceive Corporation
6d897616 5 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
e80858e8 6 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
aacb9d31
ST
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24#include <linux/module.h>
25#include <linux/moduleparam.h>
4917019d 26#include <linux/videodev2.h>
aacb9d31
ST
27#include <linux/delay.h>
28#include <linux/dvb/frontend.h>
29#include <linux/i2c.h>
30
31#include "dvb_frontend.h"
32
33#include "xc5000.h"
89fd2854 34#include "tuner-i2c.h"
aacb9d31
ST
35
36static int debug;
37module_param(debug, int, 0644);
38MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
39
b6bd5eb8
DH
40static int no_poweroff;
41module_param(no_poweroff, int, 0644);
42MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
43 "\t\t1 keep device energized and with tuner ready all the times.\n"
44 "\t\tFaster, but consumes more power and keeps the device hotter");
45
89fd2854
MK
46static DEFINE_MUTEX(xc5000_list_mutex);
47static LIST_HEAD(hybrid_tuner_instance_list);
48
8f3cd530 49#define dprintk(level, fmt, arg...) if (debug >= level) \
aacb9d31
ST
50 printk(KERN_INFO "%s: " fmt, "xc5000", ## arg)
51
ffb41234 52struct xc5000_priv {
89fd2854
MK
53 struct tuner_i2c_props i2c_props;
54 struct list_head hybrid_tuner_instance_list;
ffb41234 55
2a6003c2 56 u32 if_khz;
409328a4 57 u16 xtal_khz;
ffb41234
MK
58 u32 freq_hz;
59 u32 bandwidth;
60 u8 video_standard;
61 u8 rf_mode;
496e9057 62 u8 radio_input;
76efb0ba 63
6fab81df 64 int chip_id;
de49bc6e 65 u16 pll_register_no;
22d5c6f5
DH
66 u8 init_status_supported;
67 u8 fw_checksum_supported;
ffb41234
MK
68};
69
aacb9d31 70/* Misc Defines */
724dcbfa 71#define MAX_TV_STANDARD 24
aacb9d31
ST
72#define XC_MAX_I2C_WRITE_LENGTH 64
73
74/* Signal Types */
75#define XC_RF_MODE_AIR 0
76#define XC_RF_MODE_CABLE 1
77
27c685a4
ST
78/* Product id */
79#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
80#define XC_PRODUCT_ID_FW_LOADED 0x1388
81
aacb9d31
ST
82/* Registers */
83#define XREG_INIT 0x00
84#define XREG_VIDEO_MODE 0x01
85#define XREG_AUDIO_MODE 0x02
86#define XREG_RF_FREQ 0x03
87#define XREG_D_CODE 0x04
88#define XREG_IF_OUT 0x05
89#define XREG_SEEK_MODE 0x07
7f05b530 90#define XREG_POWER_DOWN 0x0A /* Obsolete */
724dcbfa
DB
91/* Set the output amplitude - SIF for analog, DTVP/DTVN for digital */
92#define XREG_OUTPUT_AMP 0x0B
aacb9d31
ST
93#define XREG_SIGNALSOURCE 0x0D /* 0=Air, 1=Cable */
94#define XREG_SMOOTHEDCVBS 0x0E
95#define XREG_XTALFREQ 0x0F
81c4dfe7 96#define XREG_FINERFREQ 0x10
aacb9d31
ST
97#define XREG_DDIMODE 0x11
98
99#define XREG_ADC_ENV 0x00
100#define XREG_QUALITY 0x01
101#define XREG_FRAME_LINES 0x02
102#define XREG_HSYNC_FREQ 0x03
103#define XREG_LOCK 0x04
104#define XREG_FREQ_ERROR 0x05
105#define XREG_SNR 0x06
106#define XREG_VERSION 0x07
107#define XREG_PRODUCT_ID 0x08
108#define XREG_BUSY 0x09
bae7b7d7 109#define XREG_BUILD 0x0D
7c287f18 110#define XREG_TOTALGAIN 0x0F
22d5c6f5
DH
111#define XREG_FW_CHECKSUM 0x12
112#define XREG_INIT_STATUS 0x13
aacb9d31
ST
113
114/*
115 Basic firmware description. This will remain with
116 the driver for documentation purposes.
117
118 This represents an I2C firmware file encoded as a
119 string of unsigned char. Format is as follows:
120
121 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
122 char[1 ]=len0_LSB -> length of first write transaction
123 char[2 ]=data0 -> first byte to be sent
124 char[3 ]=data1
125 char[4 ]=data2
126 char[ ]=...
127 char[M ]=dataN -> last byte to be sent
128 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
129 char[M+2]=len1_LSB -> length of second write transaction
130 char[M+3]=data0
131 char[M+4]=data1
132 ...
133 etc.
134
135 The [len] value should be interpreted as follows:
136
137 len= len_MSB _ len_LSB
138 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
139 len=0000_0000_0000_0000 : Reset command: Do hardware reset
140 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
141 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
142
143 For the RESET and WAIT commands, the two following bytes will contain
144 immediately the length of the following transaction.
145
146*/
8f3cd530 147struct XC_TV_STANDARD {
aacb9d31 148 char *Name;
e12671cf
ST
149 u16 AudioMode;
150 u16 VideoMode;
8f3cd530 151};
aacb9d31
ST
152
153/* Tuner standards */
27c685a4
ST
154#define MN_NTSC_PAL_BTSC 0
155#define MN_NTSC_PAL_A2 1
156#define MN_NTSC_PAL_EIAJ 2
157#define MN_NTSC_PAL_Mono 3
158#define BG_PAL_A2 4
159#define BG_PAL_NICAM 5
160#define BG_PAL_MONO 6
161#define I_PAL_NICAM 7
162#define I_PAL_NICAM_MONO 8
163#define DK_PAL_A2 9
164#define DK_PAL_NICAM 10
165#define DK_PAL_MONO 11
166#define DK_SECAM_A2DK1 12
167#define DK_SECAM_A2LDK3 13
168#define DK_SECAM_A2MONO 14
169#define L_SECAM_NICAM 15
170#define LC_SECAM_NICAM 16
171#define DTV6 17
172#define DTV8 18
173#define DTV7_8 19
174#define DTV7 20
175#define FM_Radio_INPUT2 21
176#define FM_Radio_INPUT1 22
724dcbfa 177#define FM_Radio_INPUT1_MONO 23
aacb9d31 178
8f3cd530 179static struct XC_TV_STANDARD XC5000_Standard[MAX_TV_STANDARD] = {
aacb9d31
ST
180 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
181 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
182 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
183 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
184 {"B/G-PAL-A2", 0x0A00, 0x8049},
185 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
186 {"B/G-PAL-MONO", 0x0878, 0x8059},
187 {"I-PAL-NICAM", 0x1080, 0x8009},
188 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
189 {"D/K-PAL-A2", 0x1600, 0x8009},
190 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
191 {"D/K-PAL-MONO", 0x1478, 0x8009},
192 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
8f3cd530 193 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
aacb9d31
ST
194 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
195 {"L-SECAM-NICAM", 0x8E82, 0x0009},
196 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
197 {"DTV6", 0x00C0, 0x8002},
198 {"DTV8", 0x00C0, 0x800B},
199 {"DTV7/8", 0x00C0, 0x801B},
200 {"DTV7", 0x00C0, 0x8007},
201 {"FM Radio-INPUT2", 0x9802, 0x9002},
724dcbfa
DB
202 {"FM Radio-INPUT1", 0x0208, 0x9002},
203 {"FM Radio-INPUT1_MONO", 0x0278, 0x9002}
aacb9d31
ST
204};
205
ddea427f
MK
206
207struct xc5000_fw_cfg {
208 char *name;
209 u16 size;
de49bc6e 210 u16 pll_reg;
22d5c6f5
DH
211 u8 init_status_supported;
212 u8 fw_checksum_supported;
ddea427f
MK
213};
214
3422f2a6 215#define XC5000A_FIRMWARE "dvb-fe-xc5000-1.6.114.fw"
a3db60bc 216static const struct xc5000_fw_cfg xc5000a_1_6_114 = {
3422f2a6 217 .name = XC5000A_FIRMWARE,
76efb0ba 218 .size = 12401,
de49bc6e 219 .pll_reg = 0x806c,
76efb0ba
MK
220};
221
3de5bffd 222#define XC5000C_FIRMWARE "dvb-fe-xc5000c-4.1.30.7.fw"
7d3d0d8d 223static const struct xc5000_fw_cfg xc5000c_41_024_5 = {
3422f2a6 224 .name = XC5000C_FIRMWARE,
7d3d0d8d 225 .size = 16497,
de49bc6e 226 .pll_reg = 0x13,
22d5c6f5
DH
227 .init_status_supported = 1,
228 .fw_checksum_supported = 1,
d8398805
MK
229};
230
a3db60bc 231static inline const struct xc5000_fw_cfg *xc5000_assign_firmware(int chip_id)
ddea427f 232{
6fab81df 233 switch (chip_id) {
ddea427f 234 default:
6fab81df 235 case XC5000A:
ddea427f 236 return &xc5000a_1_6_114;
6fab81df 237 case XC5000C:
7d3d0d8d 238 return &xc5000c_41_024_5;
ddea427f
MK
239 }
240}
241
de49bc6e 242static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe, int force);
91bd625e 243static int xc5000_is_firmware_loaded(struct dvb_frontend *fe);
bdd33563 244static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val);
91bd625e 245static int xc5000_TunerReset(struct dvb_frontend *fe);
aacb9d31 246
e12671cf 247static int xc_send_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
aacb9d31 248{
d7800d4e
DH
249 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
250 .flags = 0, .buf = buf, .len = len };
251
252 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
253 printk(KERN_ERR "xc5000: I2C write failed (len=%i)\n", len);
859ae7f0 254 return -EREMOTEIO;
d7800d4e 255 }
859ae7f0 256 return 0;
aacb9d31
ST
257}
258
1cdffda7 259#if 0
bdd33563
DH
260/* This routine is never used because the only time we read data from the
261 i2c bus is when we read registers, and we want that to be an atomic i2c
262 transaction in case we are on a multi-master bus */
e12671cf 263static int xc_read_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
aacb9d31 264{
bdd33563
DH
265 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
266 .flags = I2C_M_RD, .buf = buf, .len = len };
267
268 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
269 printk(KERN_ERR "xc5000 I2C read failed (len=%i)\n", len);
270 return -EREMOTEIO;
271 }
272 return 0;
aacb9d31 273}
1cdffda7 274#endif
aacb9d31 275
4743319f
DB
276static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val)
277{
278 u8 buf[2] = { reg >> 8, reg & 0xff };
279 u8 bval[2] = { 0, 0 };
280 struct i2c_msg msg[2] = {
281 { .addr = priv->i2c_props.addr,
282 .flags = 0, .buf = &buf[0], .len = 2 },
283 { .addr = priv->i2c_props.addr,
284 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
285 };
286
287 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
288 printk(KERN_WARNING "xc5000: I2C read failed\n");
289 return -EREMOTEIO;
290 }
291
292 *val = (bval[0] << 8) | bval[1];
859ae7f0 293 return 0;
4743319f
DB
294}
295
91bd625e 296static int xc5000_TunerReset(struct dvb_frontend *fe)
aacb9d31
ST
297{
298 struct xc5000_priv *priv = fe->tuner_priv;
299 int ret;
300
271ddbf7 301 dprintk(1, "%s()\n", __func__);
aacb9d31 302
d7cba043
MK
303 if (fe->callback) {
304 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
30650961
MK
305 fe->dvb->priv :
306 priv->i2c_props.adap->algo_data,
d7cba043 307 DVB_FRONTEND_COMPONENT_TUNER,
30650961 308 XC5000_TUNER_RESET, 0);
91bd625e 309 if (ret) {
aacb9d31 310 printk(KERN_ERR "xc5000: reset failed\n");
859ae7f0 311 return ret;
91bd625e
DH
312 }
313 } else {
27c685a4 314 printk(KERN_ERR "xc5000: no tuner reset callback function, fatal\n");
859ae7f0 315 return -EINVAL;
91bd625e 316 }
859ae7f0 317 return 0;
aacb9d31
ST
318}
319
e12671cf 320static int xc_write_reg(struct xc5000_priv *priv, u16 regAddr, u16 i2cData)
aacb9d31 321{
e12671cf 322 u8 buf[4];
a37791c5 323 int WatchDogTimer = 100;
aacb9d31
ST
324 int result;
325
326 buf[0] = (regAddr >> 8) & 0xFF;
327 buf[1] = regAddr & 0xFF;
328 buf[2] = (i2cData >> 8) & 0xFF;
329 buf[3] = i2cData & 0xFF;
330 result = xc_send_i2c_data(priv, buf, 4);
859ae7f0 331 if (result == 0) {
aacb9d31 332 /* wait for busy flag to clear */
859ae7f0 333 while ((WatchDogTimer > 0) && (result == 0)) {
1cdffda7 334 result = xc5000_readreg(priv, XREG_BUSY, (u16 *)buf);
859ae7f0 335 if (result == 0) {
4743319f
DB
336 if ((buf[0] == 0) && (buf[1] == 0)) {
337 /* busy flag cleared */
aacb9d31 338 break;
4743319f 339 } else {
e5bf4a11 340 msleep(5); /* wait 5 ms */
4743319f 341 WatchDogTimer--;
aacb9d31
ST
342 }
343 }
344 }
345 }
0a3dabb1 346 if (WatchDogTimer <= 0)
859ae7f0 347 result = -EREMOTEIO;
aacb9d31
ST
348
349 return result;
350}
351
c63e87e9 352static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
aacb9d31
ST
353{
354 struct xc5000_priv *priv = fe->tuner_priv;
355
356 int i, nbytes_to_send, result;
357 unsigned int len, pos, index;
e12671cf 358 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
aacb9d31 359
8f3cd530
ST
360 index = 0;
361 while ((i2c_sequence[index] != 0xFF) ||
362 (i2c_sequence[index + 1] != 0xFF)) {
363 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
e12671cf 364 if (len == 0x0000) {
aacb9d31 365 /* RESET command */
91bd625e 366 result = xc5000_TunerReset(fe);
aacb9d31 367 index += 2;
859ae7f0 368 if (result != 0)
aacb9d31
ST
369 return result;
370 } else if (len & 0x8000) {
371 /* WAIT command */
e5bf4a11 372 msleep(len & 0x7FFF);
aacb9d31
ST
373 index += 2;
374 } else {
375 /* Send i2c data whilst ensuring individual transactions
376 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
377 */
378 index += 2;
379 buf[0] = i2c_sequence[index];
380 buf[1] = i2c_sequence[index + 1];
381 pos = 2;
382 while (pos < len) {
8f3cd530
ST
383 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
384 nbytes_to_send =
385 XC_MAX_I2C_WRITE_LENGTH;
386 else
aacb9d31 387 nbytes_to_send = (len - pos + 2);
8f3cd530
ST
388 for (i = 2; i < nbytes_to_send; i++) {
389 buf[i] = i2c_sequence[index + pos +
390 i - 2];
aacb9d31 391 }
8f3cd530
ST
392 result = xc_send_i2c_data(priv, buf,
393 nbytes_to_send);
aacb9d31 394
859ae7f0 395 if (result != 0)
aacb9d31
ST
396 return result;
397
398 pos += nbytes_to_send - 2;
399 }
400 index += len;
401 }
402 }
859ae7f0 403 return 0;
aacb9d31
ST
404}
405
e12671cf 406static int xc_initialize(struct xc5000_priv *priv)
aacb9d31 407{
271ddbf7 408 dprintk(1, "%s()\n", __func__);
aacb9d31
ST
409 return xc_write_reg(priv, XREG_INIT, 0);
410}
411
e12671cf 412static int xc_SetTVStandard(struct xc5000_priv *priv,
01ae7286 413 u16 VideoMode, u16 AudioMode, u8 RadioMode)
aacb9d31
ST
414{
415 int ret;
271ddbf7 416 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
01ae7286
DB
417 if (RadioMode) {
418 dprintk(1, "%s() Standard = %s\n",
419 __func__,
420 XC5000_Standard[RadioMode].Name);
421 } else {
422 dprintk(1, "%s() Standard = %s\n",
423 __func__,
424 XC5000_Standard[priv->video_standard].Name);
425 }
aacb9d31
ST
426
427 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
859ae7f0 428 if (ret == 0)
aacb9d31
ST
429 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
430
431 return ret;
432}
433
e12671cf 434static int xc_SetSignalSource(struct xc5000_priv *priv, u16 rf_mode)
aacb9d31 435{
271ddbf7 436 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
aacb9d31
ST
437 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
438
8f3cd530 439 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
aacb9d31
ST
440 rf_mode = XC_RF_MODE_CABLE;
441 printk(KERN_ERR
442 "%s(), Invalid mode, defaulting to CABLE",
271ddbf7 443 __func__);
aacb9d31
ST
444 }
445 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
446}
447
e12671cf 448static const struct dvb_tuner_ops xc5000_tuner_ops;
aacb9d31 449
e12671cf
ST
450static int xc_set_RF_frequency(struct xc5000_priv *priv, u32 freq_hz)
451{
452 u16 freq_code;
aacb9d31 453
271ddbf7 454 dprintk(1, "%s(%u)\n", __func__, freq_hz);
aacb9d31 455
e12671cf
ST
456 if ((freq_hz > xc5000_tuner_ops.info.frequency_max) ||
457 (freq_hz < xc5000_tuner_ops.info.frequency_min))
859ae7f0 458 return -EINVAL;
aacb9d31 459
e12671cf
ST
460 freq_code = (u16)(freq_hz / 15625);
461
81c4dfe7
DH
462 /* Starting in firmware version 1.1.44, Xceive recommends using the
463 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
464 only be used for fast scanning for channel lock) */
465 return xc_write_reg(priv, XREG_FINERFREQ, freq_code);
aacb9d31
ST
466}
467
aacb9d31 468
e12671cf
ST
469static int xc_set_IF_frequency(struct xc5000_priv *priv, u32 freq_khz)
470{
471 u32 freq_code = (freq_khz * 1024)/1000;
472 dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
271ddbf7 473 __func__, freq_khz, freq_code);
aacb9d31 474
e12671cf 475 return xc_write_reg(priv, XREG_IF_OUT, freq_code);
aacb9d31
ST
476}
477
aacb9d31 478
e12671cf 479static int xc_get_ADC_Envelope(struct xc5000_priv *priv, u16 *adc_envelope)
aacb9d31 480{
bdd33563 481 return xc5000_readreg(priv, XREG_ADC_ENV, adc_envelope);
aacb9d31
ST
482}
483
e12671cf 484static int xc_get_frequency_error(struct xc5000_priv *priv, u32 *freq_error_hz)
aacb9d31
ST
485{
486 int result;
e12671cf 487 u16 regData;
aacb9d31
ST
488 u32 tmp;
489
bdd33563 490 result = xc5000_readreg(priv, XREG_FREQ_ERROR, &regData);
859ae7f0 491 if (result != 0)
aacb9d31
ST
492 return result;
493
494 tmp = (u32)regData;
e12671cf 495 (*freq_error_hz) = (tmp * 15625) / 1000;
aacb9d31
ST
496 return result;
497}
498
e12671cf 499static int xc_get_lock_status(struct xc5000_priv *priv, u16 *lock_status)
aacb9d31 500{
bdd33563 501 return xc5000_readreg(priv, XREG_LOCK, lock_status);
aacb9d31
ST
502}
503
e12671cf
ST
504static int xc_get_version(struct xc5000_priv *priv,
505 u8 *hw_majorversion, u8 *hw_minorversion,
506 u8 *fw_majorversion, u8 *fw_minorversion)
aacb9d31 507{
e12671cf 508 u16 data;
aacb9d31
ST
509 int result;
510
bdd33563 511 result = xc5000_readreg(priv, XREG_VERSION, &data);
859ae7f0 512 if (result != 0)
aacb9d31
ST
513 return result;
514
e12671cf
ST
515 (*hw_majorversion) = (data >> 12) & 0x0F;
516 (*hw_minorversion) = (data >> 8) & 0x0F;
517 (*fw_majorversion) = (data >> 4) & 0x0F;
518 (*fw_minorversion) = data & 0x0F;
aacb9d31
ST
519
520 return 0;
521}
522
bae7b7d7
DH
523static int xc_get_buildversion(struct xc5000_priv *priv, u16 *buildrev)
524{
525 return xc5000_readreg(priv, XREG_BUILD, buildrev);
526}
527
e12671cf 528static int xc_get_hsync_freq(struct xc5000_priv *priv, u32 *hsync_freq_hz)
aacb9d31 529{
e12671cf 530 u16 regData;
aacb9d31
ST
531 int result;
532
bdd33563 533 result = xc5000_readreg(priv, XREG_HSYNC_FREQ, &regData);
859ae7f0 534 if (result != 0)
aacb9d31
ST
535 return result;
536
537 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
538 return result;
539}
540
e12671cf 541static int xc_get_frame_lines(struct xc5000_priv *priv, u16 *frame_lines)
aacb9d31 542{
bdd33563 543 return xc5000_readreg(priv, XREG_FRAME_LINES, frame_lines);
aacb9d31
ST
544}
545
e12671cf 546static int xc_get_quality(struct xc5000_priv *priv, u16 *quality)
aacb9d31 547{
bdd33563 548 return xc5000_readreg(priv, XREG_QUALITY, quality);
aacb9d31
ST
549}
550
7c287f18
DH
551static int xc_get_analogsnr(struct xc5000_priv *priv, u16 *snr)
552{
553 return xc5000_readreg(priv, XREG_SNR, snr);
554}
555
556static int xc_get_totalgain(struct xc5000_priv *priv, u16 *totalgain)
557{
558 return xc5000_readreg(priv, XREG_TOTALGAIN, totalgain);
559}
560
e12671cf 561static u16 WaitForLock(struct xc5000_priv *priv)
aacb9d31 562{
e12671cf 563 u16 lockState = 0;
aacb9d31 564 int watchDogCount = 40;
e12671cf
ST
565
566 while ((lockState == 0) && (watchDogCount > 0)) {
aacb9d31 567 xc_get_lock_status(priv, &lockState);
e12671cf 568 if (lockState != 1) {
e5bf4a11 569 msleep(5);
aacb9d31
ST
570 watchDogCount--;
571 }
572 }
573 return lockState;
574}
575
a78baacf
DH
576#define XC_TUNE_ANALOG 0
577#define XC_TUNE_DIGITAL 1
578static int xc_tune_channel(struct xc5000_priv *priv, u32 freq_hz, int mode)
aacb9d31
ST
579{
580 int found = 0;
581
271ddbf7 582 dprintk(1, "%s(%u)\n", __func__, freq_hz);
aacb9d31 583
859ae7f0 584 if (xc_set_RF_frequency(priv, freq_hz) != 0)
aacb9d31
ST
585 return 0;
586
a78baacf
DH
587 if (mode == XC_TUNE_ANALOG) {
588 if (WaitForLock(priv) == 1)
589 found = 1;
590 }
aacb9d31
ST
591
592 return found;
593}
594
7d3d0d8d
MK
595static int xc_set_xtal(struct dvb_frontend *fe)
596{
597 struct xc5000_priv *priv = fe->tuner_priv;
859ae7f0 598 int ret = 0;
7d3d0d8d
MK
599
600 switch (priv->chip_id) {
601 default:
602 case XC5000A:
603 /* 32.000 MHz xtal is default */
604 break;
605 case XC5000C:
606 switch (priv->xtal_khz) {
607 default:
608 case 32000:
609 /* 32.000 MHz xtal is default */
610 break;
611 case 31875:
612 /* 31.875 MHz xtal configuration */
613 ret = xc_write_reg(priv, 0x000f, 0x8081);
614 break;
615 }
616 break;
617 }
618 return ret;
619}
aacb9d31 620
8f3cd530 621static int xc5000_fwupload(struct dvb_frontend *fe)
aacb9d31
ST
622{
623 struct xc5000_priv *priv = fe->tuner_priv;
624 const struct firmware *fw;
625 int ret;
a3db60bc
MK
626 const struct xc5000_fw_cfg *desired_fw =
627 xc5000_assign_firmware(priv->chip_id);
de49bc6e 628 priv->pll_register_no = desired_fw->pll_reg;
22d5c6f5
DH
629 priv->init_status_supported = desired_fw->init_status_supported;
630 priv->fw_checksum_supported = desired_fw->fw_checksum_supported;
aacb9d31 631
e12671cf
ST
632 /* request the firmware, this will block and timeout */
633 printk(KERN_INFO "xc5000: waiting for firmware upload (%s)...\n",
6fab81df 634 desired_fw->name);
e12671cf 635
6fab81df 636 ret = request_firmware(&fw, desired_fw->name,
e9785250 637 priv->i2c_props.adap->dev.parent);
aacb9d31
ST
638 if (ret) {
639 printk(KERN_ERR "xc5000: Upload failed. (file not found?)\n");
5ea60531 640 goto out;
aacb9d31 641 } else {
34a0db92 642 printk(KERN_DEBUG "xc5000: firmware read %Zu bytes.\n",
3f51451b 643 fw->size);
859ae7f0 644 ret = 0;
aacb9d31
ST
645 }
646
6fab81df 647 if (fw->size != desired_fw->size) {
aacb9d31 648 printk(KERN_ERR "xc5000: firmware incorrect size\n");
859ae7f0 649 ret = -EINVAL;
aacb9d31 650 } else {
34a0db92 651 printk(KERN_INFO "xc5000: firmware uploading...\n");
8f3cd530 652 ret = xc_load_i2c_sequence(fe, fw->data);
859ae7f0 653 if (0 == ret)
7d3d0d8d 654 ret = xc_set_xtal(fe);
859ae7f0 655 if (0 == ret)
35320676
MK
656 printk(KERN_INFO "xc5000: firmware upload complete...\n");
657 else
658 printk(KERN_ERR "xc5000: firmware upload failed...\n");
aacb9d31
ST
659 }
660
5ea60531 661out:
aacb9d31
ST
662 release_firmware(fw);
663 return ret;
664}
665
e12671cf 666static void xc_debug_dump(struct xc5000_priv *priv)
aacb9d31 667{
e12671cf
ST
668 u16 adc_envelope;
669 u32 freq_error_hz = 0;
670 u16 lock_status;
671 u32 hsync_freq_hz = 0;
672 u16 frame_lines;
673 u16 quality;
7c287f18
DH
674 u16 snr;
675 u16 totalgain;
e12671cf
ST
676 u8 hw_majorversion = 0, hw_minorversion = 0;
677 u8 fw_majorversion = 0, fw_minorversion = 0;
bae7b7d7 678 u16 fw_buildversion = 0;
de49bc6e 679 u16 regval;
aacb9d31
ST
680
681 /* Wait for stats to stabilize.
682 * Frame Lines needs two frame times after initial lock
683 * before it is valid.
684 */
e5bf4a11 685 msleep(100);
aacb9d31 686
e12671cf
ST
687 xc_get_ADC_Envelope(priv, &adc_envelope);
688 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
aacb9d31 689
e12671cf
ST
690 xc_get_frequency_error(priv, &freq_error_hz);
691 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
aacb9d31 692
e12671cf
ST
693 xc_get_lock_status(priv, &lock_status);
694 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
aacb9d31
ST
695 lock_status);
696
697 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
e12671cf 698 &fw_majorversion, &fw_minorversion);
bae7b7d7 699 xc_get_buildversion(priv, &fw_buildversion);
ca60a45d 700 dprintk(1, "*** HW: V%d.%d, FW: V %d.%d.%d\n",
aacb9d31 701 hw_majorversion, hw_minorversion,
bae7b7d7 702 fw_majorversion, fw_minorversion, fw_buildversion);
aacb9d31 703
e12671cf
ST
704 xc_get_hsync_freq(priv, &hsync_freq_hz);
705 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
aacb9d31 706
e12671cf
ST
707 xc_get_frame_lines(priv, &frame_lines);
708 dprintk(1, "*** Frame lines = %d\n", frame_lines);
aacb9d31 709
e12671cf 710 xc_get_quality(priv, &quality);
1aa9c487 711 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality & 0x07);
7c287f18
DH
712
713 xc_get_analogsnr(priv, &snr);
714 dprintk(1, "*** Unweighted analog SNR = %d dB\n", snr & 0x3f);
715
716 xc_get_totalgain(priv, &totalgain);
717 dprintk(1, "*** Total gain = %d.%d dB\n", totalgain / 256,
718 (totalgain % 256) * 100 / 256);
de49bc6e
DH
719
720 if (priv->pll_register_no) {
721 xc5000_readreg(priv, priv->pll_register_no, &regval);
722 dprintk(1, "*** PLL lock status = 0x%04x\n", regval);
723 }
aacb9d31
ST
724}
725
14d24d14 726static int xc5000_set_params(struct dvb_frontend *fe)
aacb9d31 727{
fd66c45d 728 int ret, b;
aacb9d31 729 struct xc5000_priv *priv = fe->tuner_priv;
fd66c45d
MCC
730 u32 bw = fe->dtv_property_cache.bandwidth_hz;
731 u32 freq = fe->dtv_property_cache.frequency;
732 u32 delsys = fe->dtv_property_cache.delivery_system;
aacb9d31 733
859ae7f0 734 if (xc_load_fw_and_init_tuner(fe, 0) != 0) {
fc7a74ba
DH
735 dprintk(1, "Unable to load firmware and init tuner\n");
736 return -EINVAL;
760c466c 737 }
8e4c6797 738
fd66c45d 739 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, freq);
aacb9d31 740
fd66c45d
MCC
741 switch (delsys) {
742 case SYS_ATSC:
743 dprintk(1, "%s() VSB modulation\n", __func__);
744 priv->rf_mode = XC_RF_MODE_AIR;
745 priv->freq_hz = freq - 1750000;
fd66c45d
MCC
746 priv->video_standard = DTV6;
747 break;
748 case SYS_DVBC_ANNEX_B:
749 dprintk(1, "%s() QAM modulation\n", __func__);
750 priv->rf_mode = XC_RF_MODE_CABLE;
751 priv->freq_hz = freq - 1750000;
fd66c45d
MCC
752 priv->video_standard = DTV6;
753 break;
5cf73ce1
MCC
754 case SYS_ISDBT:
755 /* All ISDB-T are currently for 6 MHz bw */
756 if (!bw)
757 bw = 6000000;
758 /* fall to OFDM handling */
759 case SYS_DMBTH:
fd66c45d
MCC
760 case SYS_DVBT:
761 case SYS_DVBT2:
6c99080d 762 dprintk(1, "%s() OFDM\n", __func__);
fd66c45d
MCC
763 switch (bw) {
764 case 6000000:
6c99080d 765 priv->video_standard = DTV6;
fd66c45d 766 priv->freq_hz = freq - 1750000;
6c99080d 767 break;
fd66c45d 768 case 7000000:
0433cd28 769 priv->video_standard = DTV7;
fd66c45d 770 priv->freq_hz = freq - 2250000;
0433cd28 771 break;
fd66c45d 772 case 8000000:
6c99080d 773 priv->video_standard = DTV8;
fd66c45d 774 priv->freq_hz = freq - 2750000;
6c99080d
DW
775 break;
776 default:
777 printk(KERN_ERR "xc5000 bandwidth not set!\n");
778 return -EINVAL;
779 }
aacb9d31 780 priv->rf_mode = XC_RF_MODE_AIR;
cf1364b1 781 break;
fd66c45d
MCC
782 case SYS_DVBC_ANNEX_A:
783 case SYS_DVBC_ANNEX_C:
784 dprintk(1, "%s() QAM modulation\n", __func__);
785 priv->rf_mode = XC_RF_MODE_CABLE;
786 if (bw <= 6000000) {
fd66c45d
MCC
787 priv->video_standard = DTV6;
788 priv->freq_hz = freq - 1750000;
789 b = 6;
790 } else if (bw <= 7000000) {
fd66c45d
MCC
791 priv->video_standard = DTV7;
792 priv->freq_hz = freq - 2250000;
793 b = 7;
794 } else {
fd66c45d
MCC
795 priv->video_standard = DTV7_8;
796 priv->freq_hz = freq - 2750000;
797 b = 8;
e80edce1 798 }
fd66c45d
MCC
799 dprintk(1, "%s() Bandwidth %dMHz (%d)\n", __func__,
800 b, bw);
801 break;
802 default:
803 printk(KERN_ERR "xc5000: delivery system is not supported!\n");
aacb9d31
ST
804 return -EINVAL;
805 }
806
fd66c45d
MCC
807 dprintk(1, "%s() frequency=%d (compensated to %d)\n",
808 __func__, freq, priv->freq_hz);
aacb9d31 809
e12671cf 810 ret = xc_SetSignalSource(priv, priv->rf_mode);
859ae7f0 811 if (ret != 0) {
e12671cf
ST
812 printk(KERN_ERR
813 "xc5000: xc_SetSignalSource(%d) failed\n",
814 priv->rf_mode);
815 return -EREMOTEIO;
816 }
aacb9d31 817
e12671cf 818 ret = xc_SetTVStandard(priv,
aacb9d31 819 XC5000_Standard[priv->video_standard].VideoMode,
01ae7286 820 XC5000_Standard[priv->video_standard].AudioMode, 0);
859ae7f0 821 if (ret != 0) {
e12671cf
ST
822 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
823 return -EREMOTEIO;
824 }
825
2a6003c2 826 ret = xc_set_IF_frequency(priv, priv->if_khz);
859ae7f0 827 if (ret != 0) {
e12671cf 828 printk(KERN_ERR "xc5000: xc_Set_IF_frequency(%d) failed\n",
2a6003c2 829 priv->if_khz);
e12671cf
ST
830 return -EIO;
831 }
832
724dcbfa
DB
833 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x8a);
834
a78baacf 835 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
aacb9d31 836
e12671cf
ST
837 if (debug)
838 xc_debug_dump(priv);
aacb9d31 839
c6f56e7d
MCC
840 priv->bandwidth = bw;
841
aacb9d31
ST
842 return 0;
843}
844
e470d817
ST
845static int xc5000_is_firmware_loaded(struct dvb_frontend *fe)
846{
847 struct xc5000_priv *priv = fe->tuner_priv;
848 int ret;
849 u16 id;
850
851 ret = xc5000_readreg(priv, XREG_PRODUCT_ID, &id);
859ae7f0 852 if (ret == 0) {
e470d817 853 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
859ae7f0 854 ret = -ENOENT;
e470d817 855 else
859ae7f0 856 ret = 0;
e470d817
ST
857 }
858
859 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
859ae7f0 860 ret == 0 ? "True" : "False", id);
e470d817
ST
861 return ret;
862}
863
d7009cdc 864static int xc5000_set_tv_freq(struct dvb_frontend *fe,
27c685a4
ST
865 struct analog_parameters *params)
866{
867 struct xc5000_priv *priv = fe->tuner_priv;
de49bc6e 868 u16 pll_lock_status;
27c685a4
ST
869 int ret;
870
27c685a4 871 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
271ddbf7 872 __func__, params->frequency);
27c685a4 873
1fab14ed
MCC
874 /* Fix me: it could be air. */
875 priv->rf_mode = params->mode;
876 if (params->mode > XC_RF_MODE_CABLE)
877 priv->rf_mode = XC_RF_MODE_CABLE;
27c685a4
ST
878
879 /* params->frequency is in units of 62.5khz */
880 priv->freq_hz = params->frequency * 62500;
881
882 /* FIX ME: Some video standards may have several possible audio
883 standards. We simply default to one of them here.
884 */
8f3cd530 885 if (params->std & V4L2_STD_MN) {
27c685a4
ST
886 /* default to BTSC audio standard */
887 priv->video_standard = MN_NTSC_PAL_BTSC;
888 goto tune_channel;
889 }
890
8f3cd530 891 if (params->std & V4L2_STD_PAL_BG) {
27c685a4
ST
892 /* default to NICAM audio standard */
893 priv->video_standard = BG_PAL_NICAM;
894 goto tune_channel;
895 }
896
8f3cd530 897 if (params->std & V4L2_STD_PAL_I) {
27c685a4
ST
898 /* default to NICAM audio standard */
899 priv->video_standard = I_PAL_NICAM;
900 goto tune_channel;
901 }
902
8f3cd530 903 if (params->std & V4L2_STD_PAL_DK) {
27c685a4
ST
904 /* default to NICAM audio standard */
905 priv->video_standard = DK_PAL_NICAM;
906 goto tune_channel;
907 }
908
8f3cd530 909 if (params->std & V4L2_STD_SECAM_DK) {
27c685a4
ST
910 /* default to A2 DK1 audio standard */
911 priv->video_standard = DK_SECAM_A2DK1;
912 goto tune_channel;
913 }
914
8f3cd530 915 if (params->std & V4L2_STD_SECAM_L) {
27c685a4
ST
916 priv->video_standard = L_SECAM_NICAM;
917 goto tune_channel;
918 }
919
8f3cd530 920 if (params->std & V4L2_STD_SECAM_LC) {
27c685a4
ST
921 priv->video_standard = LC_SECAM_NICAM;
922 goto tune_channel;
923 }
924
925tune_channel:
926 ret = xc_SetSignalSource(priv, priv->rf_mode);
859ae7f0 927 if (ret != 0) {
8f3cd530 928 printk(KERN_ERR
27c685a4
ST
929 "xc5000: xc_SetSignalSource(%d) failed\n",
930 priv->rf_mode);
931 return -EREMOTEIO;
932 }
933
934 ret = xc_SetTVStandard(priv,
935 XC5000_Standard[priv->video_standard].VideoMode,
01ae7286 936 XC5000_Standard[priv->video_standard].AudioMode, 0);
859ae7f0 937 if (ret != 0) {
27c685a4
ST
938 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
939 return -EREMOTEIO;
940 }
941
724dcbfa
DB
942 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x09);
943
a78baacf 944 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
27c685a4
ST
945
946 if (debug)
947 xc_debug_dump(priv);
948
de49bc6e
DH
949 if (priv->pll_register_no != 0) {
950 msleep(20);
951 xc5000_readreg(priv, priv->pll_register_no, &pll_lock_status);
952 if (pll_lock_status > 63) {
953 /* PLL is unlocked, force reload of the firmware */
954 dprintk(1, "xc5000: PLL not locked (0x%x). Reloading...\n",
955 pll_lock_status);
859ae7f0 956 if (xc_load_fw_and_init_tuner(fe, 1) != 0) {
de49bc6e
DH
957 printk(KERN_ERR "xc5000: Unable to reload fw\n");
958 return -EREMOTEIO;
959 }
960 goto tune_channel;
961 }
962 }
963
27c685a4
ST
964 return 0;
965}
966
d7009cdc
BILDB
967static int xc5000_set_radio_freq(struct dvb_frontend *fe,
968 struct analog_parameters *params)
969{
970 struct xc5000_priv *priv = fe->tuner_priv;
971 int ret = -EINVAL;
496e9057 972 u8 radio_input;
d7009cdc
BILDB
973
974 dprintk(1, "%s() frequency=%d (in units of khz)\n",
975 __func__, params->frequency);
976
496e9057
DH
977 if (priv->radio_input == XC5000_RADIO_NOT_CONFIGURED) {
978 dprintk(1, "%s() radio input not configured\n", __func__);
979 return -EINVAL;
980 }
981
982 if (priv->radio_input == XC5000_RADIO_FM1)
983 radio_input = FM_Radio_INPUT1;
984 else if (priv->radio_input == XC5000_RADIO_FM2)
985 radio_input = FM_Radio_INPUT2;
724dcbfa
DB
986 else if (priv->radio_input == XC5000_RADIO_FM1_MONO)
987 radio_input = FM_Radio_INPUT1_MONO;
496e9057
DH
988 else {
989 dprintk(1, "%s() unknown radio input %d\n", __func__,
990 priv->radio_input);
991 return -EINVAL;
992 }
993
d7009cdc
BILDB
994 priv->freq_hz = params->frequency * 125 / 2;
995
996 priv->rf_mode = XC_RF_MODE_AIR;
997
496e9057 998 ret = xc_SetTVStandard(priv, XC5000_Standard[radio_input].VideoMode,
01ae7286 999 XC5000_Standard[radio_input].AudioMode, radio_input);
d7009cdc 1000
859ae7f0 1001 if (ret != 0) {
d7009cdc
BILDB
1002 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
1003 return -EREMOTEIO;
1004 }
1005
1006 ret = xc_SetSignalSource(priv, priv->rf_mode);
859ae7f0 1007 if (ret != 0) {
d7009cdc
BILDB
1008 printk(KERN_ERR
1009 "xc5000: xc_SetSignalSource(%d) failed\n",
1010 priv->rf_mode);
1011 return -EREMOTEIO;
1012 }
1013
724dcbfa
DB
1014 if ((priv->radio_input == XC5000_RADIO_FM1) ||
1015 (priv->radio_input == XC5000_RADIO_FM2))
1016 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x09);
1017 else if (priv->radio_input == XC5000_RADIO_FM1_MONO)
1018 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x06);
1019
d7009cdc
BILDB
1020 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1021
1022 return 0;
1023}
1024
1025static int xc5000_set_analog_params(struct dvb_frontend *fe,
1026 struct analog_parameters *params)
1027{
1028 struct xc5000_priv *priv = fe->tuner_priv;
1029 int ret = -EINVAL;
1030
1031 if (priv->i2c_props.adap == NULL)
1032 return -EINVAL;
1033
859ae7f0 1034 if (xc_load_fw_and_init_tuner(fe, 0) != 0) {
fc7a74ba
DH
1035 dprintk(1, "Unable to load firmware and init tuner\n");
1036 return -EINVAL;
760c466c 1037 }
d7009cdc
BILDB
1038
1039 switch (params->mode) {
1040 case V4L2_TUNER_RADIO:
1041 ret = xc5000_set_radio_freq(fe, params);
1042 break;
1043 case V4L2_TUNER_ANALOG_TV:
1044 case V4L2_TUNER_DIGITAL_TV:
1045 ret = xc5000_set_tv_freq(fe, params);
1046 break;
1047 }
1048
1049 return ret;
1050}
1051
1052
aacb9d31
ST
1053static int xc5000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1054{
1055 struct xc5000_priv *priv = fe->tuner_priv;
271ddbf7 1056 dprintk(1, "%s()\n", __func__);
e12671cf 1057 *freq = priv->freq_hz;
aacb9d31
ST
1058 return 0;
1059}
1060
35621030
MCC
1061static int xc5000_get_if_frequency(struct dvb_frontend *fe, u32 *freq)
1062{
1063 struct xc5000_priv *priv = fe->tuner_priv;
1064 dprintk(1, "%s()\n", __func__);
1065 *freq = priv->if_khz * 1000;
1066 return 0;
1067}
1068
aacb9d31
ST
1069static int xc5000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1070{
1071 struct xc5000_priv *priv = fe->tuner_priv;
271ddbf7 1072 dprintk(1, "%s()\n", __func__);
27c685a4 1073
aacb9d31
ST
1074 *bw = priv->bandwidth;
1075 return 0;
1076}
1077
1078static int xc5000_get_status(struct dvb_frontend *fe, u32 *status)
1079{
1080 struct xc5000_priv *priv = fe->tuner_priv;
e12671cf 1081 u16 lock_status = 0;
aacb9d31
ST
1082
1083 xc_get_lock_status(priv, &lock_status);
1084
271ddbf7 1085 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
aacb9d31
ST
1086
1087 *status = lock_status;
1088
1089 return 0;
1090}
1091
de49bc6e 1092static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe, int force)
aacb9d31
ST
1093{
1094 struct xc5000_priv *priv = fe->tuner_priv;
859ae7f0 1095 int ret = 0;
de49bc6e 1096 u16 pll_lock_status;
22d5c6f5 1097 u16 fw_ck;
de49bc6e 1098
859ae7f0 1099 if (force || xc5000_is_firmware_loaded(fe) != 0) {
de49bc6e
DH
1100
1101fw_retry:
aacb9d31 1102
aacb9d31 1103 ret = xc5000_fwupload(fe);
859ae7f0 1104 if (ret != 0)
e12671cf 1105 return ret;
aacb9d31 1106
de49bc6e
DH
1107 msleep(20);
1108
22d5c6f5
DH
1109 if (priv->fw_checksum_supported) {
1110 if (xc5000_readreg(priv, XREG_FW_CHECKSUM, &fw_ck)
859ae7f0 1111 != 0) {
22d5c6f5
DH
1112 dprintk(1, "%s() FW checksum reading failed.\n",
1113 __func__);
1114 goto fw_retry;
1115 }
1116
1117 if (fw_ck == 0) {
1118 dprintk(1, "%s() FW checksum failed = 0x%04x\n",
1119 __func__, fw_ck);
1120 goto fw_retry;
1121 }
1122 }
1123
fc7a74ba
DH
1124 /* Start the tuner self-calibration process */
1125 ret |= xc_initialize(priv);
aacb9d31 1126
859ae7f0 1127 if (ret != 0)
de49bc6e
DH
1128 goto fw_retry;
1129
fc7a74ba
DH
1130 /* Wait for calibration to complete.
1131 * We could continue but XC5000 will clock stretch subsequent
1132 * I2C transactions until calibration is complete. This way we
1133 * don't have to rely on clock stretching working.
1134 */
e5bf4a11 1135 msleep(100);
aacb9d31 1136
22d5c6f5 1137 if (priv->init_status_supported) {
859ae7f0 1138 if (xc5000_readreg(priv, XREG_INIT_STATUS, &fw_ck) != 0) {
22d5c6f5
DH
1139 dprintk(1, "%s() FW failed reading init status.\n",
1140 __func__);
1141 goto fw_retry;
1142 }
1143
1144 if (fw_ck == 0) {
1145 dprintk(1, "%s() FW init status failed = 0x%04x\n", __func__, fw_ck);
1146 goto fw_retry;
1147 }
1148 }
1149
de49bc6e
DH
1150 if (priv->pll_register_no) {
1151 xc5000_readreg(priv, priv->pll_register_no,
1152 &pll_lock_status);
1153 if (pll_lock_status > 63) {
1154 /* PLL is unlocked, force reload of the firmware */
1155 printk(KERN_ERR "xc5000: PLL not running after fwload.\n");
1156 goto fw_retry;
1157 }
1158 }
1159
fc7a74ba
DH
1160 /* Default to "CABLE" mode */
1161 ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
1162 }
aacb9d31
ST
1163
1164 return ret;
1165}
1166
e12671cf
ST
1167static int xc5000_sleep(struct dvb_frontend *fe)
1168{
27c685a4
ST
1169 int ret;
1170
271ddbf7 1171 dprintk(1, "%s()\n", __func__);
e12671cf 1172
b6bd5eb8
DH
1173 /* Avoid firmware reload on slow devices */
1174 if (no_poweroff)
1175 return 0;
1176
7f05b530
DH
1177 /* According to Xceive technical support, the "powerdown" register
1178 was removed in newer versions of the firmware. The "supported"
1179 way to sleep the tuner is to pull the reset pin low for 10ms */
1180 ret = xc5000_TunerReset(fe);
859ae7f0 1181 if (ret != 0) {
27c685a4
ST
1182 printk(KERN_ERR
1183 "xc5000: %s() unable to shutdown tuner\n",
271ddbf7 1184 __func__);
27c685a4 1185 return -EREMOTEIO;
8f3cd530 1186 } else
859ae7f0 1187 return 0;
e12671cf
ST
1188}
1189
aacb9d31
ST
1190static int xc5000_init(struct dvb_frontend *fe)
1191{
1192 struct xc5000_priv *priv = fe->tuner_priv;
271ddbf7 1193 dprintk(1, "%s()\n", __func__);
aacb9d31 1194
859ae7f0 1195 if (xc_load_fw_and_init_tuner(fe, 0) != 0) {
e12671cf
ST
1196 printk(KERN_ERR "xc5000: Unable to initialise tuner\n");
1197 return -EREMOTEIO;
1198 }
1199
1200 if (debug)
1201 xc_debug_dump(priv);
aacb9d31
ST
1202
1203 return 0;
1204}
1205
1206static int xc5000_release(struct dvb_frontend *fe)
1207{
89fd2854
MK
1208 struct xc5000_priv *priv = fe->tuner_priv;
1209
271ddbf7 1210 dprintk(1, "%s()\n", __func__);
89fd2854
MK
1211
1212 mutex_lock(&xc5000_list_mutex);
1213
1214 if (priv)
1215 hybrid_tuner_release_state(priv);
1216
1217 mutex_unlock(&xc5000_list_mutex);
1218
aacb9d31 1219 fe->tuner_priv = NULL;
89fd2854 1220
aacb9d31
ST
1221 return 0;
1222}
1223
724dcbfa
DB
1224static int xc5000_set_config(struct dvb_frontend *fe, void *priv_cfg)
1225{
1226 struct xc5000_priv *priv = fe->tuner_priv;
1227 struct xc5000_config *p = priv_cfg;
1228
1229 dprintk(1, "%s()\n", __func__);
1230
1231 if (p->if_khz)
1232 priv->if_khz = p->if_khz;
1233
1234 if (p->radio_input)
1235 priv->radio_input = p->radio_input;
1236
1237 return 0;
1238}
1239
1240
aacb9d31
ST
1241static const struct dvb_tuner_ops xc5000_tuner_ops = {
1242 .info = {
1243 .name = "Xceive XC5000",
1244 .frequency_min = 1000000,
1245 .frequency_max = 1023000000,
1246 .frequency_step = 50000,
1247 },
1248
27c685a4
ST
1249 .release = xc5000_release,
1250 .init = xc5000_init,
1251 .sleep = xc5000_sleep,
aacb9d31 1252
724dcbfa 1253 .set_config = xc5000_set_config,
27c685a4
ST
1254 .set_params = xc5000_set_params,
1255 .set_analog_params = xc5000_set_analog_params,
1256 .get_frequency = xc5000_get_frequency,
35621030 1257 .get_if_frequency = xc5000_get_if_frequency,
27c685a4
ST
1258 .get_bandwidth = xc5000_get_bandwidth,
1259 .get_status = xc5000_get_status
aacb9d31
ST
1260};
1261
48723543
MK
1262struct dvb_frontend *xc5000_attach(struct dvb_frontend *fe,
1263 struct i2c_adapter *i2c,
2e4e98e7 1264 const struct xc5000_config *cfg)
aacb9d31
ST
1265{
1266 struct xc5000_priv *priv = NULL;
89fd2854 1267 int instance;
aacb9d31
ST
1268 u16 id = 0;
1269
89fd2854
MK
1270 dprintk(1, "%s(%d-%04x)\n", __func__,
1271 i2c ? i2c_adapter_id(i2c) : -1,
1272 cfg ? cfg->i2c_address : -1);
aacb9d31 1273
89fd2854 1274 mutex_lock(&xc5000_list_mutex);
aacb9d31 1275
89fd2854
MK
1276 instance = hybrid_tuner_request_state(struct xc5000_priv, priv,
1277 hybrid_tuner_instance_list,
1278 i2c, cfg->i2c_address, "xc5000");
1279 switch (instance) {
1280 case 0:
1281 goto fail;
1282 break;
1283 case 1:
1284 /* new tuner instance */
c6f56e7d 1285 priv->bandwidth = 6000000;
89fd2854
MK
1286 fe->tuner_priv = priv;
1287 break;
1288 default:
1289 /* existing tuner instance */
1290 fe->tuner_priv = priv;
1291 break;
1292 }
aacb9d31 1293
ea227863
DH
1294 if (priv->if_khz == 0) {
1295 /* If the IF hasn't been set yet, use the value provided by
1296 the caller (occurs in hybrid devices where the analog
1297 call to xc5000_attach occurs before the digital side) */
1298 priv->if_khz = cfg->if_khz;
1299 }
1300
7d3d0d8d
MK
1301 if (priv->xtal_khz == 0)
1302 priv->xtal_khz = cfg->xtal_khz;
1303
496e9057
DH
1304 if (priv->radio_input == 0)
1305 priv->radio_input = cfg->radio_input;
1306
6fab81df 1307 /* don't override chip id if it's already been set
76efb0ba 1308 unless explicitly specified */
6fab81df
MK
1309 if ((priv->chip_id == 0) || (cfg->chip_id))
1310 /* use default chip id if none specified, set to 0 so
1311 it can be overridden if this is a hybrid driver */
1312 priv->chip_id = (cfg->chip_id) ? cfg->chip_id : 0;
76efb0ba 1313
27c685a4
ST
1314 /* Check if firmware has been loaded. It is possible that another
1315 instance of the driver has loaded the firmware.
1316 */
859ae7f0 1317 if (xc5000_readreg(priv, XREG_PRODUCT_ID, &id) != 0)
89fd2854 1318 goto fail;
aacb9d31 1319
8f3cd530 1320 switch (id) {
27c685a4
ST
1321 case XC_PRODUCT_ID_FW_LOADED:
1322 printk(KERN_INFO
1323 "xc5000: Successfully identified at address 0x%02x\n",
1324 cfg->i2c_address);
1325 printk(KERN_INFO
1326 "xc5000: Firmware has been loaded previously\n");
27c685a4
ST
1327 break;
1328 case XC_PRODUCT_ID_FW_NOT_LOADED:
1329 printk(KERN_INFO
1330 "xc5000: Successfully identified at address 0x%02x\n",
1331 cfg->i2c_address);
1332 printk(KERN_INFO
1333 "xc5000: Firmware has not been loaded previously\n");
27c685a4
ST
1334 break;
1335 default:
aacb9d31
ST
1336 printk(KERN_ERR
1337 "xc5000: Device not found at addr 0x%02x (0x%x)\n",
1338 cfg->i2c_address, id);
89fd2854 1339 goto fail;
aacb9d31
ST
1340 }
1341
89fd2854
MK
1342 mutex_unlock(&xc5000_list_mutex);
1343
aacb9d31
ST
1344 memcpy(&fe->ops.tuner_ops, &xc5000_tuner_ops,
1345 sizeof(struct dvb_tuner_ops));
1346
aacb9d31 1347 return fe;
89fd2854
MK
1348fail:
1349 mutex_unlock(&xc5000_list_mutex);
1350
1351 xc5000_release(fe);
1352 return NULL;
aacb9d31
ST
1353}
1354EXPORT_SYMBOL(xc5000_attach);
1355
1356MODULE_AUTHOR("Steven Toth");
e12671cf 1357MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
aacb9d31 1358MODULE_LICENSE("GPL");
3422f2a6
TG
1359MODULE_FIRMWARE(XC5000A_FIRMWARE);
1360MODULE_FIRMWARE(XC5000C_FIRMWARE);