blob: cd92b9e1ccdd6e0309e3efecd302edeb2054f959 [file] [log] [blame]
Steven Tothaacb9d32007-12-18 01:55:51 -03001/*
2 * Driver for Xceive XC5000 "QAM/8VSB single chip tuner"
3 *
4 * Copyright (c) 2007 Xceive Corporation
Steven Toth6d897612008-09-03 17:12:12 -03005 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
Devin Heitmuellere80858e2009-05-14 21:31:11 -03006 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
Steven Tothaacb9d32007-12-18 01:55:51 -03007 *
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>
Steven Toth49170192008-01-15 21:57:14 -030026#include <linux/videodev2.h>
Steven Tothaacb9d32007-12-18 01:55:51 -030027#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"
Michael Krufky89fd2852008-09-06 11:44:53 -030034#include "tuner-i2c.h"
Steven Tothaacb9d32007-12-18 01:55:51 -030035
36static int debug;
37module_param(debug, int, 0644);
38MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
39
Devin Heitmuellerb6bd5eb2009-04-28 13:53:38 -030040static 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
Michael Krufky89fd2852008-09-06 11:44:53 -030046static DEFINE_MUTEX(xc5000_list_mutex);
47static LIST_HEAD(hybrid_tuner_instance_list);
48
Steven Toth8f3cd532008-10-16 20:29:38 -030049#define dprintk(level, fmt, arg...) if (debug >= level) \
Steven Tothaacb9d32007-12-18 01:55:51 -030050 printk(KERN_INFO "%s: " fmt, "xc5000", ## arg)
51
Michael Krufkyffb41232008-09-06 10:52:30 -030052struct xc5000_priv {
Michael Krufky89fd2852008-09-06 11:44:53 -030053 struct tuner_i2c_props i2c_props;
54 struct list_head hybrid_tuner_instance_list;
Michael Krufkyffb41232008-09-06 10:52:30 -030055
Michael Krufky2a6003c2008-09-06 13:54:45 -030056 u32 if_khz;
Michael Krufky409328a2012-04-16 15:21:51 -030057 u16 xtal_khz;
Michael Krufkyffb41232008-09-06 10:52:30 -030058 u32 freq_hz;
59 u32 bandwidth;
60 u8 video_standard;
61 u8 rf_mode;
Devin Heitmueller496e9052009-09-24 13:27:24 -030062 u8 radio_input;
Michael Krufky76efb0ba2012-02-06 19:40:32 -030063
Michael Krufky6fab81d2012-02-08 14:57:39 -030064 int chip_id;
Devin Heitmuellerde49bc62012-08-06 22:47:08 -030065 u16 pll_register_no;
Michael Krufkyffb41232008-09-06 10:52:30 -030066};
67
Steven Tothaacb9d32007-12-18 01:55:51 -030068/* Misc Defines */
Dmitri Belimov724dcbf2011-02-01 05:25:19 -030069#define MAX_TV_STANDARD 24
Steven Tothaacb9d32007-12-18 01:55:51 -030070#define XC_MAX_I2C_WRITE_LENGTH 64
71
72/* Signal Types */
73#define XC_RF_MODE_AIR 0
74#define XC_RF_MODE_CABLE 1
75
76/* Result codes */
77#define XC_RESULT_SUCCESS 0
78#define XC_RESULT_RESET_FAILURE 1
79#define XC_RESULT_I2C_WRITE_FAILURE 2
80#define XC_RESULT_I2C_READ_FAILURE 3
81#define XC_RESULT_OUT_OF_RANGE 5
82
Steven Toth27c685a2008-01-05 16:50:14 -030083/* Product id */
84#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
85#define XC_PRODUCT_ID_FW_LOADED 0x1388
86
Steven Tothaacb9d32007-12-18 01:55:51 -030087/* Registers */
88#define XREG_INIT 0x00
89#define XREG_VIDEO_MODE 0x01
90#define XREG_AUDIO_MODE 0x02
91#define XREG_RF_FREQ 0x03
92#define XREG_D_CODE 0x04
93#define XREG_IF_OUT 0x05
94#define XREG_SEEK_MODE 0x07
Devin Heitmueller7f05b532009-04-02 22:02:39 -030095#define XREG_POWER_DOWN 0x0A /* Obsolete */
Dmitri Belimov724dcbf2011-02-01 05:25:19 -030096/* Set the output amplitude - SIF for analog, DTVP/DTVN for digital */
97#define XREG_OUTPUT_AMP 0x0B
Steven Tothaacb9d32007-12-18 01:55:51 -030098#define XREG_SIGNALSOURCE 0x0D /* 0=Air, 1=Cable */
99#define XREG_SMOOTHEDCVBS 0x0E
100#define XREG_XTALFREQ 0x0F
Devin Heitmueller81c4dfe72009-04-02 22:40:29 -0300101#define XREG_FINERFREQ 0x10
Steven Tothaacb9d32007-12-18 01:55:51 -0300102#define XREG_DDIMODE 0x11
103
104#define XREG_ADC_ENV 0x00
105#define XREG_QUALITY 0x01
106#define XREG_FRAME_LINES 0x02
107#define XREG_HSYNC_FREQ 0x03
108#define XREG_LOCK 0x04
109#define XREG_FREQ_ERROR 0x05
110#define XREG_SNR 0x06
111#define XREG_VERSION 0x07
112#define XREG_PRODUCT_ID 0x08
113#define XREG_BUSY 0x09
Devin Heitmuellerbae7b7d2009-04-02 22:24:38 -0300114#define XREG_BUILD 0x0D
Devin Heitmueller7c287f12012-08-06 22:46:56 -0300115#define XREG_TOTALGAIN 0x0F
Steven Tothaacb9d32007-12-18 01:55:51 -0300116
117/*
118 Basic firmware description. This will remain with
119 the driver for documentation purposes.
120
121 This represents an I2C firmware file encoded as a
122 string of unsigned char. Format is as follows:
123
124 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
125 char[1 ]=len0_LSB -> length of first write transaction
126 char[2 ]=data0 -> first byte to be sent
127 char[3 ]=data1
128 char[4 ]=data2
129 char[ ]=...
130 char[M ]=dataN -> last byte to be sent
131 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
132 char[M+2]=len1_LSB -> length of second write transaction
133 char[M+3]=data0
134 char[M+4]=data1
135 ...
136 etc.
137
138 The [len] value should be interpreted as follows:
139
140 len= len_MSB _ len_LSB
141 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
142 len=0000_0000_0000_0000 : Reset command: Do hardware reset
143 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
144 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
145
146 For the RESET and WAIT commands, the two following bytes will contain
147 immediately the length of the following transaction.
148
149*/
Steven Toth8f3cd532008-10-16 20:29:38 -0300150struct XC_TV_STANDARD {
Steven Tothaacb9d32007-12-18 01:55:51 -0300151 char *Name;
Steven Tothe12671c2007-12-20 01:14:43 -0300152 u16 AudioMode;
153 u16 VideoMode;
Steven Toth8f3cd532008-10-16 20:29:38 -0300154};
Steven Tothaacb9d32007-12-18 01:55:51 -0300155
156/* Tuner standards */
Steven Toth27c685a2008-01-05 16:50:14 -0300157#define MN_NTSC_PAL_BTSC 0
158#define MN_NTSC_PAL_A2 1
159#define MN_NTSC_PAL_EIAJ 2
160#define MN_NTSC_PAL_Mono 3
161#define BG_PAL_A2 4
162#define BG_PAL_NICAM 5
163#define BG_PAL_MONO 6
164#define I_PAL_NICAM 7
165#define I_PAL_NICAM_MONO 8
166#define DK_PAL_A2 9
167#define DK_PAL_NICAM 10
168#define DK_PAL_MONO 11
169#define DK_SECAM_A2DK1 12
170#define DK_SECAM_A2LDK3 13
171#define DK_SECAM_A2MONO 14
172#define L_SECAM_NICAM 15
173#define LC_SECAM_NICAM 16
174#define DTV6 17
175#define DTV8 18
176#define DTV7_8 19
177#define DTV7 20
178#define FM_Radio_INPUT2 21
179#define FM_Radio_INPUT1 22
Dmitri Belimov724dcbf2011-02-01 05:25:19 -0300180#define FM_Radio_INPUT1_MONO 23
Steven Tothaacb9d32007-12-18 01:55:51 -0300181
Steven Toth8f3cd532008-10-16 20:29:38 -0300182static struct XC_TV_STANDARD XC5000_Standard[MAX_TV_STANDARD] = {
Steven Tothaacb9d32007-12-18 01:55:51 -0300183 {"M/N-NTSC/PAL-BTSC", 0x0400, 0x8020},
184 {"M/N-NTSC/PAL-A2", 0x0600, 0x8020},
185 {"M/N-NTSC/PAL-EIAJ", 0x0440, 0x8020},
186 {"M/N-NTSC/PAL-Mono", 0x0478, 0x8020},
187 {"B/G-PAL-A2", 0x0A00, 0x8049},
188 {"B/G-PAL-NICAM", 0x0C04, 0x8049},
189 {"B/G-PAL-MONO", 0x0878, 0x8059},
190 {"I-PAL-NICAM", 0x1080, 0x8009},
191 {"I-PAL-NICAM-MONO", 0x0E78, 0x8009},
192 {"D/K-PAL-A2", 0x1600, 0x8009},
193 {"D/K-PAL-NICAM", 0x0E80, 0x8009},
194 {"D/K-PAL-MONO", 0x1478, 0x8009},
195 {"D/K-SECAM-A2 DK1", 0x1200, 0x8009},
Steven Toth8f3cd532008-10-16 20:29:38 -0300196 {"D/K-SECAM-A2 L/DK3", 0x0E00, 0x8009},
Steven Tothaacb9d32007-12-18 01:55:51 -0300197 {"D/K-SECAM-A2 MONO", 0x1478, 0x8009},
198 {"L-SECAM-NICAM", 0x8E82, 0x0009},
199 {"L'-SECAM-NICAM", 0x8E82, 0x4009},
200 {"DTV6", 0x00C0, 0x8002},
201 {"DTV8", 0x00C0, 0x800B},
202 {"DTV7/8", 0x00C0, 0x801B},
203 {"DTV7", 0x00C0, 0x8007},
204 {"FM Radio-INPUT2", 0x9802, 0x9002},
Dmitri Belimov724dcbf2011-02-01 05:25:19 -0300205 {"FM Radio-INPUT1", 0x0208, 0x9002},
206 {"FM Radio-INPUT1_MONO", 0x0278, 0x9002}
Steven Tothaacb9d32007-12-18 01:55:51 -0300207};
208
Michael Krufkyddea4272012-02-07 02:39:36 -0300209
210struct xc5000_fw_cfg {
211 char *name;
212 u16 size;
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300213 u16 pll_reg;
Michael Krufkyddea4272012-02-07 02:39:36 -0300214};
215
Tim Gardner3422f2a2012-07-25 09:15:19 -0300216#define XC5000A_FIRMWARE "dvb-fe-xc5000-1.6.114.fw"
Michael Krufkya3db60b2012-02-08 15:03:07 -0300217static const struct xc5000_fw_cfg xc5000a_1_6_114 = {
Tim Gardner3422f2a2012-07-25 09:15:19 -0300218 .name = XC5000A_FIRMWARE,
Michael Krufky76efb0ba2012-02-06 19:40:32 -0300219 .size = 12401,
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300220 .pll_reg = 0x806c,
Michael Krufky76efb0ba2012-02-06 19:40:32 -0300221};
222
Tim Gardner3422f2a2012-07-25 09:15:19 -0300223#define XC5000C_FIRMWARE "dvb-fe-xc5000c-41.024.5.fw"
Michael Krufky7d3d0d82012-04-16 14:59:32 -0300224static const struct xc5000_fw_cfg xc5000c_41_024_5 = {
Tim Gardner3422f2a2012-07-25 09:15:19 -0300225 .name = XC5000C_FIRMWARE,
Michael Krufky7d3d0d82012-04-16 14:59:32 -0300226 .size = 16497,
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300227 .pll_reg = 0x13,
Michael Krufkyd8398802012-02-07 01:16:27 -0300228};
229
Michael Krufkya3db60b2012-02-08 15:03:07 -0300230static inline const struct xc5000_fw_cfg *xc5000_assign_firmware(int chip_id)
Michael Krufkyddea4272012-02-07 02:39:36 -0300231{
Michael Krufky6fab81d2012-02-08 14:57:39 -0300232 switch (chip_id) {
Michael Krufkyddea4272012-02-07 02:39:36 -0300233 default:
Michael Krufky6fab81d2012-02-08 14:57:39 -0300234 case XC5000A:
Michael Krufkyddea4272012-02-07 02:39:36 -0300235 return &xc5000a_1_6_114;
Michael Krufky6fab81d2012-02-08 14:57:39 -0300236 case XC5000C:
Michael Krufky7d3d0d82012-04-16 14:59:32 -0300237 return &xc5000c_41_024_5;
Michael Krufkyddea4272012-02-07 02:39:36 -0300238 }
239}
240
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300241static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe, int force);
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300242static int xc5000_is_firmware_loaded(struct dvb_frontend *fe);
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300243static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val);
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300244static int xc5000_TunerReset(struct dvb_frontend *fe);
Steven Tothaacb9d32007-12-18 01:55:51 -0300245
Steven Tothe12671c2007-12-20 01:14:43 -0300246static int xc_send_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
Steven Tothaacb9d32007-12-18 01:55:51 -0300247{
Devin Heitmuellerd7800d42008-11-16 20:20:06 -0300248 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
249 .flags = 0, .buf = buf, .len = len };
250
251 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
252 printk(KERN_ERR "xc5000: I2C write failed (len=%i)\n", len);
253 return XC_RESULT_I2C_WRITE_FAILURE;
254 }
255 return XC_RESULT_SUCCESS;
Steven Tothaacb9d32007-12-18 01:55:51 -0300256}
257
Mauro Carvalho Chehab1cdffda2010-07-05 18:38:46 -0300258#if 0
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300259/* This routine is never used because the only time we read data from the
260 i2c bus is when we read registers, and we want that to be an atomic i2c
261 transaction in case we are on a multi-master bus */
Steven Tothe12671c2007-12-20 01:14:43 -0300262static int xc_read_i2c_data(struct xc5000_priv *priv, u8 *buf, int len)
Steven Tothaacb9d32007-12-18 01:55:51 -0300263{
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300264 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
265 .flags = I2C_M_RD, .buf = buf, .len = len };
266
267 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
268 printk(KERN_ERR "xc5000 I2C read failed (len=%i)\n", len);
269 return -EREMOTEIO;
270 }
271 return 0;
Steven Tothaacb9d32007-12-18 01:55:51 -0300272}
Mauro Carvalho Chehab1cdffda2010-07-05 18:38:46 -0300273#endif
Steven Tothaacb9d32007-12-18 01:55:51 -0300274
Dmitri Belimov47433192010-05-18 04:30:11 -0300275static int xc5000_readreg(struct xc5000_priv *priv, u16 reg, u16 *val)
276{
277 u8 buf[2] = { reg >> 8, reg & 0xff };
278 u8 bval[2] = { 0, 0 };
279 struct i2c_msg msg[2] = {
280 { .addr = priv->i2c_props.addr,
281 .flags = 0, .buf = &buf[0], .len = 2 },
282 { .addr = priv->i2c_props.addr,
283 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
284 };
285
286 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
287 printk(KERN_WARNING "xc5000: I2C read failed\n");
288 return -EREMOTEIO;
289 }
290
291 *val = (bval[0] << 8) | bval[1];
292 return XC_RESULT_SUCCESS;
293}
294
Steven Tothe12671c2007-12-20 01:14:43 -0300295static void xc_wait(int wait_ms)
Steven Tothaacb9d32007-12-18 01:55:51 -0300296{
Steven Tothe12671c2007-12-20 01:14:43 -0300297 msleep(wait_ms);
Steven Tothaacb9d32007-12-18 01:55:51 -0300298}
299
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300300static int xc5000_TunerReset(struct dvb_frontend *fe)
Steven Tothaacb9d32007-12-18 01:55:51 -0300301{
302 struct xc5000_priv *priv = fe->tuner_priv;
303 int ret;
304
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300305 dprintk(1, "%s()\n", __func__);
Steven Tothaacb9d32007-12-18 01:55:51 -0300306
Michael Krufkyd7cba042008-09-12 13:31:45 -0300307 if (fe->callback) {
308 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
Michael Krufky30650962008-09-06 14:56:58 -0300309 fe->dvb->priv :
310 priv->i2c_props.adap->algo_data,
Michael Krufkyd7cba042008-09-12 13:31:45 -0300311 DVB_FRONTEND_COMPONENT_TUNER,
Michael Krufky30650962008-09-06 14:56:58 -0300312 XC5000_TUNER_RESET, 0);
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300313 if (ret) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300314 printk(KERN_ERR "xc5000: reset failed\n");
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300315 return XC_RESULT_RESET_FAILURE;
316 }
317 } else {
Steven Toth27c685a2008-01-05 16:50:14 -0300318 printk(KERN_ERR "xc5000: no tuner reset callback function, fatal\n");
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300319 return XC_RESULT_RESET_FAILURE;
320 }
321 return XC_RESULT_SUCCESS;
Steven Tothaacb9d32007-12-18 01:55:51 -0300322}
323
Steven Tothe12671c2007-12-20 01:14:43 -0300324static int xc_write_reg(struct xc5000_priv *priv, u16 regAddr, u16 i2cData)
Steven Tothaacb9d32007-12-18 01:55:51 -0300325{
Steven Tothe12671c2007-12-20 01:14:43 -0300326 u8 buf[4];
Devin Heitmuellera37791c2009-05-04 23:29:17 -0300327 int WatchDogTimer = 100;
Steven Tothaacb9d32007-12-18 01:55:51 -0300328 int result;
329
330 buf[0] = (regAddr >> 8) & 0xFF;
331 buf[1] = regAddr & 0xFF;
332 buf[2] = (i2cData >> 8) & 0xFF;
333 buf[3] = i2cData & 0xFF;
334 result = xc_send_i2c_data(priv, buf, 4);
Steven Tothe12671c2007-12-20 01:14:43 -0300335 if (result == XC_RESULT_SUCCESS) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300336 /* wait for busy flag to clear */
337 while ((WatchDogTimer > 0) && (result == XC_RESULT_SUCCESS)) {
Mauro Carvalho Chehab1cdffda2010-07-05 18:38:46 -0300338 result = xc5000_readreg(priv, XREG_BUSY, (u16 *)buf);
Steven Tothaacb9d32007-12-18 01:55:51 -0300339 if (result == XC_RESULT_SUCCESS) {
Dmitri Belimov47433192010-05-18 04:30:11 -0300340 if ((buf[0] == 0) && (buf[1] == 0)) {
341 /* busy flag cleared */
Steven Tothaacb9d32007-12-18 01:55:51 -0300342 break;
Dmitri Belimov47433192010-05-18 04:30:11 -0300343 } else {
344 xc_wait(5); /* wait 5 ms */
345 WatchDogTimer--;
Steven Tothaacb9d32007-12-18 01:55:51 -0300346 }
347 }
348 }
349 }
Devin Heitmueller0a3dabb2012-08-06 22:46:57 -0300350 if (WatchDogTimer <= 0)
Steven Tothaacb9d32007-12-18 01:55:51 -0300351 result = XC_RESULT_I2C_WRITE_FAILURE;
352
353 return result;
354}
355
David Woodhousec63e87e2008-05-24 00:13:34 +0100356static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
Steven Tothaacb9d32007-12-18 01:55:51 -0300357{
358 struct xc5000_priv *priv = fe->tuner_priv;
359
360 int i, nbytes_to_send, result;
361 unsigned int len, pos, index;
Steven Tothe12671c2007-12-20 01:14:43 -0300362 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
Steven Tothaacb9d32007-12-18 01:55:51 -0300363
Steven Toth8f3cd532008-10-16 20:29:38 -0300364 index = 0;
365 while ((i2c_sequence[index] != 0xFF) ||
366 (i2c_sequence[index + 1] != 0xFF)) {
367 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
Steven Tothe12671c2007-12-20 01:14:43 -0300368 if (len == 0x0000) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300369 /* RESET command */
Devin Heitmueller91bd6252008-11-15 23:29:11 -0300370 result = xc5000_TunerReset(fe);
Steven Tothaacb9d32007-12-18 01:55:51 -0300371 index += 2;
Steven Tothe12671c2007-12-20 01:14:43 -0300372 if (result != XC_RESULT_SUCCESS)
Steven Tothaacb9d32007-12-18 01:55:51 -0300373 return result;
374 } else if (len & 0x8000) {
375 /* WAIT command */
376 xc_wait(len & 0x7FFF);
377 index += 2;
378 } else {
379 /* Send i2c data whilst ensuring individual transactions
380 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
381 */
382 index += 2;
383 buf[0] = i2c_sequence[index];
384 buf[1] = i2c_sequence[index + 1];
385 pos = 2;
386 while (pos < len) {
Steven Toth8f3cd532008-10-16 20:29:38 -0300387 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
388 nbytes_to_send =
389 XC_MAX_I2C_WRITE_LENGTH;
390 else
Steven Tothaacb9d32007-12-18 01:55:51 -0300391 nbytes_to_send = (len - pos + 2);
Steven Toth8f3cd532008-10-16 20:29:38 -0300392 for (i = 2; i < nbytes_to_send; i++) {
393 buf[i] = i2c_sequence[index + pos +
394 i - 2];
Steven Tothaacb9d32007-12-18 01:55:51 -0300395 }
Steven Toth8f3cd532008-10-16 20:29:38 -0300396 result = xc_send_i2c_data(priv, buf,
397 nbytes_to_send);
Steven Tothaacb9d32007-12-18 01:55:51 -0300398
Steven Tothe12671c2007-12-20 01:14:43 -0300399 if (result != XC_RESULT_SUCCESS)
Steven Tothaacb9d32007-12-18 01:55:51 -0300400 return result;
401
402 pos += nbytes_to_send - 2;
403 }
404 index += len;
405 }
406 }
407 return XC_RESULT_SUCCESS;
408}
409
Steven Tothe12671c2007-12-20 01:14:43 -0300410static int xc_initialize(struct xc5000_priv *priv)
Steven Tothaacb9d32007-12-18 01:55:51 -0300411{
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300412 dprintk(1, "%s()\n", __func__);
Steven Tothaacb9d32007-12-18 01:55:51 -0300413 return xc_write_reg(priv, XREG_INIT, 0);
414}
415
Steven Tothe12671c2007-12-20 01:14:43 -0300416static int xc_SetTVStandard(struct xc5000_priv *priv,
417 u16 VideoMode, u16 AudioMode)
Steven Tothaacb9d32007-12-18 01:55:51 -0300418{
419 int ret;
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300420 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, VideoMode, AudioMode);
Steven Tothaacb9d32007-12-18 01:55:51 -0300421 dprintk(1, "%s() Standard = %s\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300422 __func__,
Steven Tothaacb9d32007-12-18 01:55:51 -0300423 XC5000_Standard[priv->video_standard].Name);
424
425 ret = xc_write_reg(priv, XREG_VIDEO_MODE, VideoMode);
426 if (ret == XC_RESULT_SUCCESS)
427 ret = xc_write_reg(priv, XREG_AUDIO_MODE, AudioMode);
428
429 return ret;
430}
431
Steven Tothe12671c2007-12-20 01:14:43 -0300432static int xc_SetSignalSource(struct xc5000_priv *priv, u16 rf_mode)
Steven Tothaacb9d32007-12-18 01:55:51 -0300433{
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300434 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
Steven Tothaacb9d32007-12-18 01:55:51 -0300435 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
436
Steven Toth8f3cd532008-10-16 20:29:38 -0300437 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300438 rf_mode = XC_RF_MODE_CABLE;
439 printk(KERN_ERR
440 "%s(), Invalid mode, defaulting to CABLE",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300441 __func__);
Steven Tothaacb9d32007-12-18 01:55:51 -0300442 }
443 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
444}
445
Steven Tothe12671c2007-12-20 01:14:43 -0300446static const struct dvb_tuner_ops xc5000_tuner_ops;
447
448static int xc_set_RF_frequency(struct xc5000_priv *priv, u32 freq_hz)
Steven Tothaacb9d32007-12-18 01:55:51 -0300449{
Steven Tothe12671c2007-12-20 01:14:43 -0300450 u16 freq_code;
Steven Tothaacb9d32007-12-18 01:55:51 -0300451
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300452 dprintk(1, "%s(%u)\n", __func__, freq_hz);
Steven Tothaacb9d32007-12-18 01:55:51 -0300453
Steven Tothe12671c2007-12-20 01:14:43 -0300454 if ((freq_hz > xc5000_tuner_ops.info.frequency_max) ||
455 (freq_hz < xc5000_tuner_ops.info.frequency_min))
456 return XC_RESULT_OUT_OF_RANGE;
Steven Tothaacb9d32007-12-18 01:55:51 -0300457
Steven Tothe12671c2007-12-20 01:14:43 -0300458 freq_code = (u16)(freq_hz / 15625);
459
Devin Heitmueller81c4dfe72009-04-02 22:40:29 -0300460 /* Starting in firmware version 1.1.44, Xceive recommends using the
461 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
462 only be used for fast scanning for channel lock) */
463 return xc_write_reg(priv, XREG_FINERFREQ, freq_code);
Steven Tothaacb9d32007-12-18 01:55:51 -0300464}
465
Steven Tothe12671c2007-12-20 01:14:43 -0300466
467static int xc_set_IF_frequency(struct xc5000_priv *priv, u32 freq_khz)
Steven Tothaacb9d32007-12-18 01:55:51 -0300468{
Steven Tothe12671c2007-12-20 01:14:43 -0300469 u32 freq_code = (freq_khz * 1024)/1000;
470 dprintk(1, "%s(freq_khz = %d) freq_code = 0x%x\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300471 __func__, freq_khz, freq_code);
Steven Tothe12671c2007-12-20 01:14:43 -0300472
473 return xc_write_reg(priv, XREG_IF_OUT, freq_code);
Steven Tothaacb9d32007-12-18 01:55:51 -0300474}
475
Steven Tothe12671c2007-12-20 01:14:43 -0300476
477static int xc_get_ADC_Envelope(struct xc5000_priv *priv, u16 *adc_envelope)
Steven Tothaacb9d32007-12-18 01:55:51 -0300478{
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300479 return xc5000_readreg(priv, XREG_ADC_ENV, adc_envelope);
Steven Tothaacb9d32007-12-18 01:55:51 -0300480}
481
Steven Tothe12671c2007-12-20 01:14:43 -0300482static int xc_get_frequency_error(struct xc5000_priv *priv, u32 *freq_error_hz)
Steven Tothaacb9d32007-12-18 01:55:51 -0300483{
484 int result;
Steven Tothe12671c2007-12-20 01:14:43 -0300485 u16 regData;
Steven Tothaacb9d32007-12-18 01:55:51 -0300486 u32 tmp;
487
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300488 result = xc5000_readreg(priv, XREG_FREQ_ERROR, &regData);
Devin Heitmueller7988fc22008-11-16 20:23:19 -0300489 if (result != XC_RESULT_SUCCESS)
Steven Tothaacb9d32007-12-18 01:55:51 -0300490 return result;
491
492 tmp = (u32)regData;
Steven Tothe12671c2007-12-20 01:14:43 -0300493 (*freq_error_hz) = (tmp * 15625) / 1000;
Steven Tothaacb9d32007-12-18 01:55:51 -0300494 return result;
495}
496
Steven Tothe12671c2007-12-20 01:14:43 -0300497static int xc_get_lock_status(struct xc5000_priv *priv, u16 *lock_status)
Steven Tothaacb9d32007-12-18 01:55:51 -0300498{
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300499 return xc5000_readreg(priv, XREG_LOCK, lock_status);
Steven Tothaacb9d32007-12-18 01:55:51 -0300500}
501
Steven Tothe12671c2007-12-20 01:14:43 -0300502static int xc_get_version(struct xc5000_priv *priv,
503 u8 *hw_majorversion, u8 *hw_minorversion,
504 u8 *fw_majorversion, u8 *fw_minorversion)
Steven Tothaacb9d32007-12-18 01:55:51 -0300505{
Steven Tothe12671c2007-12-20 01:14:43 -0300506 u16 data;
Steven Tothaacb9d32007-12-18 01:55:51 -0300507 int result;
508
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300509 result = xc5000_readreg(priv, XREG_VERSION, &data);
Devin Heitmueller7988fc22008-11-16 20:23:19 -0300510 if (result != XC_RESULT_SUCCESS)
Steven Tothaacb9d32007-12-18 01:55:51 -0300511 return result;
512
Steven Tothe12671c2007-12-20 01:14:43 -0300513 (*hw_majorversion) = (data >> 12) & 0x0F;
514 (*hw_minorversion) = (data >> 8) & 0x0F;
515 (*fw_majorversion) = (data >> 4) & 0x0F;
516 (*fw_minorversion) = data & 0x0F;
Steven Tothaacb9d32007-12-18 01:55:51 -0300517
518 return 0;
519}
520
Devin Heitmuellerbae7b7d2009-04-02 22:24:38 -0300521static int xc_get_buildversion(struct xc5000_priv *priv, u16 *buildrev)
522{
523 return xc5000_readreg(priv, XREG_BUILD, buildrev);
524}
525
Steven Tothe12671c2007-12-20 01:14:43 -0300526static int xc_get_hsync_freq(struct xc5000_priv *priv, u32 *hsync_freq_hz)
Steven Tothaacb9d32007-12-18 01:55:51 -0300527{
Steven Tothe12671c2007-12-20 01:14:43 -0300528 u16 regData;
Steven Tothaacb9d32007-12-18 01:55:51 -0300529 int result;
530
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300531 result = xc5000_readreg(priv, XREG_HSYNC_FREQ, &regData);
Devin Heitmueller7988fc22008-11-16 20:23:19 -0300532 if (result != XC_RESULT_SUCCESS)
Steven Tothaacb9d32007-12-18 01:55:51 -0300533 return result;
534
535 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
536 return result;
537}
538
Steven Tothe12671c2007-12-20 01:14:43 -0300539static int xc_get_frame_lines(struct xc5000_priv *priv, u16 *frame_lines)
Steven Tothaacb9d32007-12-18 01:55:51 -0300540{
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300541 return xc5000_readreg(priv, XREG_FRAME_LINES, frame_lines);
Steven Tothaacb9d32007-12-18 01:55:51 -0300542}
543
Steven Tothe12671c2007-12-20 01:14:43 -0300544static int xc_get_quality(struct xc5000_priv *priv, u16 *quality)
Steven Tothaacb9d32007-12-18 01:55:51 -0300545{
Devin Heitmuellerbdd33562008-11-16 20:17:14 -0300546 return xc5000_readreg(priv, XREG_QUALITY, quality);
Steven Tothaacb9d32007-12-18 01:55:51 -0300547}
548
Devin Heitmueller7c287f12012-08-06 22:46:56 -0300549static int xc_get_analogsnr(struct xc5000_priv *priv, u16 *snr)
550{
551 return xc5000_readreg(priv, XREG_SNR, snr);
552}
553
554static int xc_get_totalgain(struct xc5000_priv *priv, u16 *totalgain)
555{
556 return xc5000_readreg(priv, XREG_TOTALGAIN, totalgain);
557}
558
Steven Tothe12671c2007-12-20 01:14:43 -0300559static u16 WaitForLock(struct xc5000_priv *priv)
Steven Tothaacb9d32007-12-18 01:55:51 -0300560{
Steven Tothe12671c2007-12-20 01:14:43 -0300561 u16 lockState = 0;
Steven Tothaacb9d32007-12-18 01:55:51 -0300562 int watchDogCount = 40;
Steven Tothe12671c2007-12-20 01:14:43 -0300563
564 while ((lockState == 0) && (watchDogCount > 0)) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300565 xc_get_lock_status(priv, &lockState);
Steven Tothe12671c2007-12-20 01:14:43 -0300566 if (lockState != 1) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300567 xc_wait(5);
568 watchDogCount--;
569 }
570 }
571 return lockState;
572}
573
Devin Heitmuellera78baac2008-11-16 20:48:31 -0300574#define XC_TUNE_ANALOG 0
575#define XC_TUNE_DIGITAL 1
576static int xc_tune_channel(struct xc5000_priv *priv, u32 freq_hz, int mode)
Steven Tothaacb9d32007-12-18 01:55:51 -0300577{
578 int found = 0;
579
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300580 dprintk(1, "%s(%u)\n", __func__, freq_hz);
Steven Tothaacb9d32007-12-18 01:55:51 -0300581
Steven Tothe12671c2007-12-20 01:14:43 -0300582 if (xc_set_RF_frequency(priv, freq_hz) != XC_RESULT_SUCCESS)
Steven Tothaacb9d32007-12-18 01:55:51 -0300583 return 0;
584
Devin Heitmuellera78baac2008-11-16 20:48:31 -0300585 if (mode == XC_TUNE_ANALOG) {
586 if (WaitForLock(priv) == 1)
587 found = 1;
588 }
Steven Tothaacb9d32007-12-18 01:55:51 -0300589
590 return found;
591}
592
Michael Krufky7d3d0d82012-04-16 14:59:32 -0300593static int xc_set_xtal(struct dvb_frontend *fe)
594{
595 struct xc5000_priv *priv = fe->tuner_priv;
596 int ret = XC_RESULT_SUCCESS;
597
598 switch (priv->chip_id) {
599 default:
600 case XC5000A:
601 /* 32.000 MHz xtal is default */
602 break;
603 case XC5000C:
604 switch (priv->xtal_khz) {
605 default:
606 case 32000:
607 /* 32.000 MHz xtal is default */
608 break;
609 case 31875:
610 /* 31.875 MHz xtal configuration */
611 ret = xc_write_reg(priv, 0x000f, 0x8081);
612 break;
613 }
614 break;
615 }
616 return ret;
617}
Steven Tothaacb9d32007-12-18 01:55:51 -0300618
Steven Toth8f3cd532008-10-16 20:29:38 -0300619static int xc5000_fwupload(struct dvb_frontend *fe)
Steven Tothaacb9d32007-12-18 01:55:51 -0300620{
621 struct xc5000_priv *priv = fe->tuner_priv;
622 const struct firmware *fw;
623 int ret;
Michael Krufkya3db60b2012-02-08 15:03:07 -0300624 const struct xc5000_fw_cfg *desired_fw =
625 xc5000_assign_firmware(priv->chip_id);
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300626 priv->pll_register_no = desired_fw->pll_reg;
Steven Tothaacb9d32007-12-18 01:55:51 -0300627
Steven Tothe12671c2007-12-20 01:14:43 -0300628 /* request the firmware, this will block and timeout */
629 printk(KERN_INFO "xc5000: waiting for firmware upload (%s)...\n",
Michael Krufky6fab81d2012-02-08 14:57:39 -0300630 desired_fw->name);
Steven Tothe12671c2007-12-20 01:14:43 -0300631
Michael Krufky6fab81d2012-02-08 14:57:39 -0300632 ret = request_firmware(&fw, desired_fw->name,
Jean Delvaree9785252009-04-26 05:43:59 -0300633 priv->i2c_props.adap->dev.parent);
Steven Tothaacb9d32007-12-18 01:55:51 -0300634 if (ret) {
635 printk(KERN_ERR "xc5000: Upload failed. (file not found?)\n");
636 ret = XC_RESULT_RESET_FAILURE;
Steven Toth5ea60532008-01-24 22:29:46 -0300637 goto out;
Steven Tothaacb9d32007-12-18 01:55:51 -0300638 } else {
Devin Heitmueller34a0db92009-04-02 22:45:17 -0300639 printk(KERN_DEBUG "xc5000: firmware read %Zu bytes.\n",
Michael Krufky3f514512007-12-21 16:20:23 -0300640 fw->size);
Steven Tothaacb9d32007-12-18 01:55:51 -0300641 ret = XC_RESULT_SUCCESS;
642 }
643
Michael Krufky6fab81d2012-02-08 14:57:39 -0300644 if (fw->size != desired_fw->size) {
Steven Tothaacb9d32007-12-18 01:55:51 -0300645 printk(KERN_ERR "xc5000: firmware incorrect size\n");
646 ret = XC_RESULT_RESET_FAILURE;
647 } else {
Devin Heitmueller34a0db92009-04-02 22:45:17 -0300648 printk(KERN_INFO "xc5000: firmware uploading...\n");
Steven Toth8f3cd532008-10-16 20:29:38 -0300649 ret = xc_load_i2c_sequence(fe, fw->data);
Michael Krufky7d3d0d82012-04-16 14:59:32 -0300650 if (XC_RESULT_SUCCESS == ret)
651 ret = xc_set_xtal(fe);
Michael Krufky35320672012-04-16 15:06:45 -0300652 if (XC_RESULT_SUCCESS == ret)
653 printk(KERN_INFO "xc5000: firmware upload complete...\n");
654 else
655 printk(KERN_ERR "xc5000: firmware upload failed...\n");
Steven Tothaacb9d32007-12-18 01:55:51 -0300656 }
657
Steven Toth5ea60532008-01-24 22:29:46 -0300658out:
Steven Tothaacb9d32007-12-18 01:55:51 -0300659 release_firmware(fw);
660 return ret;
661}
662
Steven Tothe12671c2007-12-20 01:14:43 -0300663static void xc_debug_dump(struct xc5000_priv *priv)
Steven Tothaacb9d32007-12-18 01:55:51 -0300664{
Steven Tothe12671c2007-12-20 01:14:43 -0300665 u16 adc_envelope;
666 u32 freq_error_hz = 0;
667 u16 lock_status;
668 u32 hsync_freq_hz = 0;
669 u16 frame_lines;
670 u16 quality;
Devin Heitmueller7c287f12012-08-06 22:46:56 -0300671 u16 snr;
672 u16 totalgain;
Steven Tothe12671c2007-12-20 01:14:43 -0300673 u8 hw_majorversion = 0, hw_minorversion = 0;
674 u8 fw_majorversion = 0, fw_minorversion = 0;
Devin Heitmuellerbae7b7d2009-04-02 22:24:38 -0300675 u16 fw_buildversion = 0;
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300676 u16 regval;
Steven Tothaacb9d32007-12-18 01:55:51 -0300677
678 /* Wait for stats to stabilize.
679 * Frame Lines needs two frame times after initial lock
680 * before it is valid.
681 */
Steven Tothe12671c2007-12-20 01:14:43 -0300682 xc_wait(100);
Steven Tothaacb9d32007-12-18 01:55:51 -0300683
Steven Tothe12671c2007-12-20 01:14:43 -0300684 xc_get_ADC_Envelope(priv, &adc_envelope);
685 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
Steven Tothaacb9d32007-12-18 01:55:51 -0300686
Steven Tothe12671c2007-12-20 01:14:43 -0300687 xc_get_frequency_error(priv, &freq_error_hz);
688 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
Steven Tothaacb9d32007-12-18 01:55:51 -0300689
Steven Tothe12671c2007-12-20 01:14:43 -0300690 xc_get_lock_status(priv, &lock_status);
691 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
Steven Tothaacb9d32007-12-18 01:55:51 -0300692 lock_status);
693
694 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
Steven Tothe12671c2007-12-20 01:14:43 -0300695 &fw_majorversion, &fw_minorversion);
Devin Heitmuellerbae7b7d2009-04-02 22:24:38 -0300696 xc_get_buildversion(priv, &fw_buildversion);
697 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x.%04x\n",
Steven Tothaacb9d32007-12-18 01:55:51 -0300698 hw_majorversion, hw_minorversion,
Devin Heitmuellerbae7b7d2009-04-02 22:24:38 -0300699 fw_majorversion, fw_minorversion, fw_buildversion);
Steven Tothaacb9d32007-12-18 01:55:51 -0300700
Steven Tothe12671c2007-12-20 01:14:43 -0300701 xc_get_hsync_freq(priv, &hsync_freq_hz);
702 dprintk(1, "*** Horizontal sync frequency = %d Hz\n", hsync_freq_hz);
Steven Tothaacb9d32007-12-18 01:55:51 -0300703
Steven Tothe12671c2007-12-20 01:14:43 -0300704 xc_get_frame_lines(priv, &frame_lines);
705 dprintk(1, "*** Frame lines = %d\n", frame_lines);
Steven Tothaacb9d32007-12-18 01:55:51 -0300706
Steven Tothe12671c2007-12-20 01:14:43 -0300707 xc_get_quality(priv, &quality);
Devin Heitmueller1aa9c4872012-08-06 22:46:55 -0300708 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality & 0x07);
Devin Heitmueller7c287f12012-08-06 22:46:56 -0300709
710 xc_get_analogsnr(priv, &snr);
711 dprintk(1, "*** Unweighted analog SNR = %d dB\n", snr & 0x3f);
712
713 xc_get_totalgain(priv, &totalgain);
714 dprintk(1, "*** Total gain = %d.%d dB\n", totalgain / 256,
715 (totalgain % 256) * 100 / 256);
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300716
717 if (priv->pll_register_no) {
718 xc5000_readreg(priv, priv->pll_register_no, &regval);
719 dprintk(1, "*** PLL lock status = 0x%04x\n", regval);
720 }
Steven Tothaacb9d32007-12-18 01:55:51 -0300721}
722
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300723static int xc5000_set_params(struct dvb_frontend *fe)
Steven Tothaacb9d32007-12-18 01:55:51 -0300724{
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300725 int ret, b;
Steven Tothaacb9d32007-12-18 01:55:51 -0300726 struct xc5000_priv *priv = fe->tuner_priv;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300727 u32 bw = fe->dtv_property_cache.bandwidth_hz;
728 u32 freq = fe->dtv_property_cache.frequency;
729 u32 delsys = fe->dtv_property_cache.delivery_system;
Steven Tothaacb9d32007-12-18 01:55:51 -0300730
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300731 if (xc_load_fw_and_init_tuner(fe, 0) != XC_RESULT_SUCCESS) {
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -0300732 dprintk(1, "Unable to load firmware and init tuner\n");
733 return -EINVAL;
Devin Heitmueller760c4662009-10-13 23:44:14 -0300734 }
Devin Heitmueller8e4c6792008-11-16 20:41:07 -0300735
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300736 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, freq);
Steven Tothaacb9d32007-12-18 01:55:51 -0300737
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300738 switch (delsys) {
739 case SYS_ATSC:
740 dprintk(1, "%s() VSB modulation\n", __func__);
741 priv->rf_mode = XC_RF_MODE_AIR;
742 priv->freq_hz = freq - 1750000;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300743 priv->video_standard = DTV6;
744 break;
745 case SYS_DVBC_ANNEX_B:
746 dprintk(1, "%s() QAM modulation\n", __func__);
747 priv->rf_mode = XC_RF_MODE_CABLE;
748 priv->freq_hz = freq - 1750000;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300749 priv->video_standard = DTV6;
750 break;
Mauro Carvalho Chehab5cf73ce2012-07-05 14:07:59 -0300751 case SYS_ISDBT:
752 /* All ISDB-T are currently for 6 MHz bw */
753 if (!bw)
754 bw = 6000000;
755 /* fall to OFDM handling */
756 case SYS_DMBTH:
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300757 case SYS_DVBT:
758 case SYS_DVBT2:
David T.L. Wong6c990802009-05-04 22:59:58 -0300759 dprintk(1, "%s() OFDM\n", __func__);
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300760 switch (bw) {
761 case 6000000:
David T.L. Wong6c990802009-05-04 22:59:58 -0300762 priv->video_standard = DTV6;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300763 priv->freq_hz = freq - 1750000;
David T.L. Wong6c990802009-05-04 22:59:58 -0300764 break;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300765 case 7000000:
Mauro Carvalho Chehab0433cd22011-12-09 08:01:01 -0200766 priv->video_standard = DTV7;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300767 priv->freq_hz = freq - 2250000;
Mauro Carvalho Chehab0433cd22011-12-09 08:01:01 -0200768 break;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300769 case 8000000:
David T.L. Wong6c990802009-05-04 22:59:58 -0300770 priv->video_standard = DTV8;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300771 priv->freq_hz = freq - 2750000;
David T.L. Wong6c990802009-05-04 22:59:58 -0300772 break;
773 default:
774 printk(KERN_ERR "xc5000 bandwidth not set!\n");
775 return -EINVAL;
776 }
Steven Tothaacb9d32007-12-18 01:55:51 -0300777 priv->rf_mode = XC_RF_MODE_AIR;
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300778 case SYS_DVBC_ANNEX_A:
779 case SYS_DVBC_ANNEX_C:
780 dprintk(1, "%s() QAM modulation\n", __func__);
781 priv->rf_mode = XC_RF_MODE_CABLE;
782 if (bw <= 6000000) {
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300783 priv->video_standard = DTV6;
784 priv->freq_hz = freq - 1750000;
785 b = 6;
786 } else if (bw <= 7000000) {
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300787 priv->video_standard = DTV7;
788 priv->freq_hz = freq - 2250000;
789 b = 7;
790 } else {
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300791 priv->video_standard = DTV7_8;
792 priv->freq_hz = freq - 2750000;
793 b = 8;
Igor M. Liplianine80edce2011-01-25 17:03:00 -0300794 }
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300795 dprintk(1, "%s() Bandwidth %dMHz (%d)\n", __func__,
796 b, bw);
797 break;
798 default:
799 printk(KERN_ERR "xc5000: delivery system is not supported!\n");
Steven Tothaacb9d32007-12-18 01:55:51 -0300800 return -EINVAL;
801 }
802
Mauro Carvalho Chehabfd66c452011-12-17 20:36:57 -0300803 dprintk(1, "%s() frequency=%d (compensated to %d)\n",
804 __func__, freq, priv->freq_hz);
Steven Tothaacb9d32007-12-18 01:55:51 -0300805
Steven Tothe12671c2007-12-20 01:14:43 -0300806 ret = xc_SetSignalSource(priv, priv->rf_mode);
807 if (ret != XC_RESULT_SUCCESS) {
808 printk(KERN_ERR
809 "xc5000: xc_SetSignalSource(%d) failed\n",
810 priv->rf_mode);
811 return -EREMOTEIO;
812 }
Steven Tothaacb9d32007-12-18 01:55:51 -0300813
Steven Tothe12671c2007-12-20 01:14:43 -0300814 ret = xc_SetTVStandard(priv,
Steven Tothaacb9d32007-12-18 01:55:51 -0300815 XC5000_Standard[priv->video_standard].VideoMode,
816 XC5000_Standard[priv->video_standard].AudioMode);
Steven Tothe12671c2007-12-20 01:14:43 -0300817 if (ret != XC_RESULT_SUCCESS) {
818 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
819 return -EREMOTEIO;
820 }
Steven Tothaacb9d32007-12-18 01:55:51 -0300821
Michael Krufky2a6003c2008-09-06 13:54:45 -0300822 ret = xc_set_IF_frequency(priv, priv->if_khz);
Steven Tothe12671c2007-12-20 01:14:43 -0300823 if (ret != XC_RESULT_SUCCESS) {
824 printk(KERN_ERR "xc5000: xc_Set_IF_frequency(%d) failed\n",
Michael Krufky2a6003c2008-09-06 13:54:45 -0300825 priv->if_khz);
Steven Tothe12671c2007-12-20 01:14:43 -0300826 return -EIO;
827 }
828
Dmitri Belimov724dcbf2011-02-01 05:25:19 -0300829 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x8a);
830
Devin Heitmuellera78baac2008-11-16 20:48:31 -0300831 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_DIGITAL);
Steven Tothe12671c2007-12-20 01:14:43 -0300832
833 if (debug)
834 xc_debug_dump(priv);
Steven Tothaacb9d32007-12-18 01:55:51 -0300835
Mauro Carvalho Chehabc6f56e72011-12-26 20:02:28 -0300836 priv->bandwidth = bw;
837
Steven Tothaacb9d32007-12-18 01:55:51 -0300838 return 0;
839}
840
Steven Tothe470d812008-06-21 21:06:02 -0300841static int xc5000_is_firmware_loaded(struct dvb_frontend *fe)
842{
843 struct xc5000_priv *priv = fe->tuner_priv;
844 int ret;
845 u16 id;
846
847 ret = xc5000_readreg(priv, XREG_PRODUCT_ID, &id);
848 if (ret == XC_RESULT_SUCCESS) {
849 if (id == XC_PRODUCT_ID_FW_NOT_LOADED)
850 ret = XC_RESULT_RESET_FAILURE;
851 else
852 ret = XC_RESULT_SUCCESS;
853 }
854
855 dprintk(1, "%s() returns %s id = 0x%x\n", __func__,
856 ret == XC_RESULT_SUCCESS ? "True" : "False", id);
857 return ret;
858}
859
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -0300860static int xc5000_set_tv_freq(struct dvb_frontend *fe,
Steven Toth27c685a2008-01-05 16:50:14 -0300861 struct analog_parameters *params)
862{
863 struct xc5000_priv *priv = fe->tuner_priv;
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300864 u16 pll_lock_status;
Steven Toth27c685a2008-01-05 16:50:14 -0300865 int ret;
866
Steven Toth27c685a2008-01-05 16:50:14 -0300867 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300868 __func__, params->frequency);
Steven Toth27c685a2008-01-05 16:50:14 -0300869
Mauro Carvalho Chehab1fab14e2009-03-03 14:35:41 -0300870 /* Fix me: it could be air. */
871 priv->rf_mode = params->mode;
872 if (params->mode > XC_RF_MODE_CABLE)
873 priv->rf_mode = XC_RF_MODE_CABLE;
Steven Toth27c685a2008-01-05 16:50:14 -0300874
875 /* params->frequency is in units of 62.5khz */
876 priv->freq_hz = params->frequency * 62500;
877
878 /* FIX ME: Some video standards may have several possible audio
879 standards. We simply default to one of them here.
880 */
Steven Toth8f3cd532008-10-16 20:29:38 -0300881 if (params->std & V4L2_STD_MN) {
Steven Toth27c685a2008-01-05 16:50:14 -0300882 /* default to BTSC audio standard */
883 priv->video_standard = MN_NTSC_PAL_BTSC;
884 goto tune_channel;
885 }
886
Steven Toth8f3cd532008-10-16 20:29:38 -0300887 if (params->std & V4L2_STD_PAL_BG) {
Steven Toth27c685a2008-01-05 16:50:14 -0300888 /* default to NICAM audio standard */
889 priv->video_standard = BG_PAL_NICAM;
890 goto tune_channel;
891 }
892
Steven Toth8f3cd532008-10-16 20:29:38 -0300893 if (params->std & V4L2_STD_PAL_I) {
Steven Toth27c685a2008-01-05 16:50:14 -0300894 /* default to NICAM audio standard */
895 priv->video_standard = I_PAL_NICAM;
896 goto tune_channel;
897 }
898
Steven Toth8f3cd532008-10-16 20:29:38 -0300899 if (params->std & V4L2_STD_PAL_DK) {
Steven Toth27c685a2008-01-05 16:50:14 -0300900 /* default to NICAM audio standard */
901 priv->video_standard = DK_PAL_NICAM;
902 goto tune_channel;
903 }
904
Steven Toth8f3cd532008-10-16 20:29:38 -0300905 if (params->std & V4L2_STD_SECAM_DK) {
Steven Toth27c685a2008-01-05 16:50:14 -0300906 /* default to A2 DK1 audio standard */
907 priv->video_standard = DK_SECAM_A2DK1;
908 goto tune_channel;
909 }
910
Steven Toth8f3cd532008-10-16 20:29:38 -0300911 if (params->std & V4L2_STD_SECAM_L) {
Steven Toth27c685a2008-01-05 16:50:14 -0300912 priv->video_standard = L_SECAM_NICAM;
913 goto tune_channel;
914 }
915
Steven Toth8f3cd532008-10-16 20:29:38 -0300916 if (params->std & V4L2_STD_SECAM_LC) {
Steven Toth27c685a2008-01-05 16:50:14 -0300917 priv->video_standard = LC_SECAM_NICAM;
918 goto tune_channel;
919 }
920
921tune_channel:
922 ret = xc_SetSignalSource(priv, priv->rf_mode);
923 if (ret != XC_RESULT_SUCCESS) {
Steven Toth8f3cd532008-10-16 20:29:38 -0300924 printk(KERN_ERR
Steven Toth27c685a2008-01-05 16:50:14 -0300925 "xc5000: xc_SetSignalSource(%d) failed\n",
926 priv->rf_mode);
927 return -EREMOTEIO;
928 }
929
930 ret = xc_SetTVStandard(priv,
931 XC5000_Standard[priv->video_standard].VideoMode,
932 XC5000_Standard[priv->video_standard].AudioMode);
933 if (ret != XC_RESULT_SUCCESS) {
934 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
935 return -EREMOTEIO;
936 }
937
Dmitri Belimov724dcbf2011-02-01 05:25:19 -0300938 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x09);
939
Devin Heitmuellera78baac2008-11-16 20:48:31 -0300940 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
Steven Toth27c685a2008-01-05 16:50:14 -0300941
942 if (debug)
943 xc_debug_dump(priv);
944
Devin Heitmuellerde49bc62012-08-06 22:47:08 -0300945 if (priv->pll_register_no != 0) {
946 msleep(20);
947 xc5000_readreg(priv, priv->pll_register_no, &pll_lock_status);
948 if (pll_lock_status > 63) {
949 /* PLL is unlocked, force reload of the firmware */
950 dprintk(1, "xc5000: PLL not locked (0x%x). Reloading...\n",
951 pll_lock_status);
952 if (xc_load_fw_and_init_tuner(fe, 1) != XC_RESULT_SUCCESS) {
953 printk(KERN_ERR "xc5000: Unable to reload fw\n");
954 return -EREMOTEIO;
955 }
956 goto tune_channel;
957 }
958 }
959
Steven Toth27c685a2008-01-05 16:50:14 -0300960 return 0;
961}
962
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -0300963static int xc5000_set_radio_freq(struct dvb_frontend *fe,
964 struct analog_parameters *params)
965{
966 struct xc5000_priv *priv = fe->tuner_priv;
967 int ret = -EINVAL;
Devin Heitmueller496e9052009-09-24 13:27:24 -0300968 u8 radio_input;
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -0300969
970 dprintk(1, "%s() frequency=%d (in units of khz)\n",
971 __func__, params->frequency);
972
Devin Heitmueller496e9052009-09-24 13:27:24 -0300973 if (priv->radio_input == XC5000_RADIO_NOT_CONFIGURED) {
974 dprintk(1, "%s() radio input not configured\n", __func__);
975 return -EINVAL;
976 }
977
978 if (priv->radio_input == XC5000_RADIO_FM1)
979 radio_input = FM_Radio_INPUT1;
980 else if (priv->radio_input == XC5000_RADIO_FM2)
981 radio_input = FM_Radio_INPUT2;
Dmitri Belimov724dcbf2011-02-01 05:25:19 -0300982 else if (priv->radio_input == XC5000_RADIO_FM1_MONO)
983 radio_input = FM_Radio_INPUT1_MONO;
Devin Heitmueller496e9052009-09-24 13:27:24 -0300984 else {
985 dprintk(1, "%s() unknown radio input %d\n", __func__,
986 priv->radio_input);
987 return -EINVAL;
988 }
989
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -0300990 priv->freq_hz = params->frequency * 125 / 2;
991
992 priv->rf_mode = XC_RF_MODE_AIR;
993
Devin Heitmueller496e9052009-09-24 13:27:24 -0300994 ret = xc_SetTVStandard(priv, XC5000_Standard[radio_input].VideoMode,
995 XC5000_Standard[radio_input].AudioMode);
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -0300996
997 if (ret != XC_RESULT_SUCCESS) {
998 printk(KERN_ERR "xc5000: xc_SetTVStandard failed\n");
999 return -EREMOTEIO;
1000 }
1001
1002 ret = xc_SetSignalSource(priv, priv->rf_mode);
1003 if (ret != XC_RESULT_SUCCESS) {
1004 printk(KERN_ERR
1005 "xc5000: xc_SetSignalSource(%d) failed\n",
1006 priv->rf_mode);
1007 return -EREMOTEIO;
1008 }
1009
Dmitri Belimov724dcbf2011-02-01 05:25:19 -03001010 if ((priv->radio_input == XC5000_RADIO_FM1) ||
1011 (priv->radio_input == XC5000_RADIO_FM2))
1012 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x09);
1013 else if (priv->radio_input == XC5000_RADIO_FM1_MONO)
1014 xc_write_reg(priv, XREG_OUTPUT_AMP, 0x06);
1015
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -03001016 xc_tune_channel(priv, priv->freq_hz, XC_TUNE_ANALOG);
1017
1018 return 0;
1019}
1020
1021static int xc5000_set_analog_params(struct dvb_frontend *fe,
1022 struct analog_parameters *params)
1023{
1024 struct xc5000_priv *priv = fe->tuner_priv;
1025 int ret = -EINVAL;
1026
1027 if (priv->i2c_props.adap == NULL)
1028 return -EINVAL;
1029
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001030 if (xc_load_fw_and_init_tuner(fe, 0) != XC_RESULT_SUCCESS) {
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -03001031 dprintk(1, "Unable to load firmware and init tuner\n");
1032 return -EINVAL;
Devin Heitmueller760c4662009-10-13 23:44:14 -03001033 }
Beholder Intl. Ltd. Dmitry Belimovd7009cd2009-09-24 13:13:28 -03001034
1035 switch (params->mode) {
1036 case V4L2_TUNER_RADIO:
1037 ret = xc5000_set_radio_freq(fe, params);
1038 break;
1039 case V4L2_TUNER_ANALOG_TV:
1040 case V4L2_TUNER_DIGITAL_TV:
1041 ret = xc5000_set_tv_freq(fe, params);
1042 break;
1043 }
1044
1045 return ret;
1046}
1047
1048
Steven Tothaacb9d32007-12-18 01:55:51 -03001049static int xc5000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1050{
1051 struct xc5000_priv *priv = fe->tuner_priv;
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001052 dprintk(1, "%s()\n", __func__);
Steven Tothe12671c2007-12-20 01:14:43 -03001053 *freq = priv->freq_hz;
Steven Tothaacb9d32007-12-18 01:55:51 -03001054 return 0;
1055}
1056
Mauro Carvalho Chehab35621032011-09-23 13:03:42 -03001057static int xc5000_get_if_frequency(struct dvb_frontend *fe, u32 *freq)
1058{
1059 struct xc5000_priv *priv = fe->tuner_priv;
1060 dprintk(1, "%s()\n", __func__);
1061 *freq = priv->if_khz * 1000;
1062 return 0;
1063}
1064
Steven Tothaacb9d32007-12-18 01:55:51 -03001065static int xc5000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1066{
1067 struct xc5000_priv *priv = fe->tuner_priv;
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001068 dprintk(1, "%s()\n", __func__);
Steven Toth27c685a2008-01-05 16:50:14 -03001069
Steven Tothaacb9d32007-12-18 01:55:51 -03001070 *bw = priv->bandwidth;
1071 return 0;
1072}
1073
1074static int xc5000_get_status(struct dvb_frontend *fe, u32 *status)
1075{
1076 struct xc5000_priv *priv = fe->tuner_priv;
Steven Tothe12671c2007-12-20 01:14:43 -03001077 u16 lock_status = 0;
Steven Tothaacb9d32007-12-18 01:55:51 -03001078
1079 xc_get_lock_status(priv, &lock_status);
1080
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001081 dprintk(1, "%s() lock_status = 0x%08x\n", __func__, lock_status);
Steven Tothaacb9d32007-12-18 01:55:51 -03001082
1083 *status = lock_status;
1084
1085 return 0;
1086}
1087
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001088static int xc_load_fw_and_init_tuner(struct dvb_frontend *fe, int force)
Steven Tothaacb9d32007-12-18 01:55:51 -03001089{
1090 struct xc5000_priv *priv = fe->tuner_priv;
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -03001091 int ret = XC_RESULT_SUCCESS;
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001092 u16 pll_lock_status;
Steven Tothaacb9d32007-12-18 01:55:51 -03001093
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001094 if (force || xc5000_is_firmware_loaded(fe) != XC_RESULT_SUCCESS) {
1095
1096fw_retry:
1097
Steven Tothaacb9d32007-12-18 01:55:51 -03001098 ret = xc5000_fwupload(fe);
Steven Tothe12671c2007-12-20 01:14:43 -03001099 if (ret != XC_RESULT_SUCCESS)
1100 return ret;
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -03001101
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001102 msleep(20);
1103
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -03001104 /* Start the tuner self-calibration process */
1105 ret |= xc_initialize(priv);
1106
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001107 if (ret != XC_RESULT_SUCCESS)
1108 goto fw_retry;
1109
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -03001110 /* Wait for calibration to complete.
1111 * We could continue but XC5000 will clock stretch subsequent
1112 * I2C transactions until calibration is complete. This way we
1113 * don't have to rely on clock stretching working.
1114 */
1115 xc_wait(100);
1116
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001117 if (priv->pll_register_no) {
1118 xc5000_readreg(priv, priv->pll_register_no,
1119 &pll_lock_status);
1120 if (pll_lock_status > 63) {
1121 /* PLL is unlocked, force reload of the firmware */
1122 printk(KERN_ERR "xc5000: PLL not running after fwload.\n");
1123 goto fw_retry;
1124 }
1125 }
1126
Devin Heitmuellerfc7a74b2012-08-06 22:47:01 -03001127 /* Default to "CABLE" mode */
1128 ret |= xc_write_reg(priv, XREG_SIGNALSOURCE, XC_RF_MODE_CABLE);
Steven Tothaacb9d32007-12-18 01:55:51 -03001129 }
1130
Steven Tothaacb9d32007-12-18 01:55:51 -03001131 return ret;
1132}
1133
Steven Tothe12671c2007-12-20 01:14:43 -03001134static int xc5000_sleep(struct dvb_frontend *fe)
1135{
Steven Toth27c685a2008-01-05 16:50:14 -03001136 int ret;
1137
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001138 dprintk(1, "%s()\n", __func__);
Steven Tothe12671c2007-12-20 01:14:43 -03001139
Devin Heitmuellerb6bd5eb2009-04-28 13:53:38 -03001140 /* Avoid firmware reload on slow devices */
1141 if (no_poweroff)
1142 return 0;
1143
Devin Heitmueller7f05b532009-04-02 22:02:39 -03001144 /* According to Xceive technical support, the "powerdown" register
1145 was removed in newer versions of the firmware. The "supported"
1146 way to sleep the tuner is to pull the reset pin low for 10ms */
1147 ret = xc5000_TunerReset(fe);
Steven Toth8f3cd532008-10-16 20:29:38 -03001148 if (ret != XC_RESULT_SUCCESS) {
Steven Toth27c685a2008-01-05 16:50:14 -03001149 printk(KERN_ERR
1150 "xc5000: %s() unable to shutdown tuner\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001151 __func__);
Steven Toth27c685a2008-01-05 16:50:14 -03001152 return -EREMOTEIO;
Steven Toth8f3cd532008-10-16 20:29:38 -03001153 } else
Steven Toth27c685a2008-01-05 16:50:14 -03001154 return XC_RESULT_SUCCESS;
Steven Tothe12671c2007-12-20 01:14:43 -03001155}
1156
Steven Tothaacb9d32007-12-18 01:55:51 -03001157static int xc5000_init(struct dvb_frontend *fe)
1158{
1159 struct xc5000_priv *priv = fe->tuner_priv;
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001160 dprintk(1, "%s()\n", __func__);
Steven Tothaacb9d32007-12-18 01:55:51 -03001161
Devin Heitmuellerde49bc62012-08-06 22:47:08 -03001162 if (xc_load_fw_and_init_tuner(fe, 0) != XC_RESULT_SUCCESS) {
Steven Tothe12671c2007-12-20 01:14:43 -03001163 printk(KERN_ERR "xc5000: Unable to initialise tuner\n");
1164 return -EREMOTEIO;
1165 }
1166
1167 if (debug)
1168 xc_debug_dump(priv);
Steven Tothaacb9d32007-12-18 01:55:51 -03001169
1170 return 0;
1171}
1172
1173static int xc5000_release(struct dvb_frontend *fe)
1174{
Michael Krufky89fd2852008-09-06 11:44:53 -03001175 struct xc5000_priv *priv = fe->tuner_priv;
1176
Harvey Harrison271ddbf2008-04-08 23:20:00 -03001177 dprintk(1, "%s()\n", __func__);
Michael Krufky89fd2852008-09-06 11:44:53 -03001178
1179 mutex_lock(&xc5000_list_mutex);
1180
1181 if (priv)
1182 hybrid_tuner_release_state(priv);
1183
1184 mutex_unlock(&xc5000_list_mutex);
1185
Steven Tothaacb9d32007-12-18 01:55:51 -03001186 fe->tuner_priv = NULL;
Michael Krufky89fd2852008-09-06 11:44:53 -03001187
Steven Tothaacb9d32007-12-18 01:55:51 -03001188 return 0;
1189}
1190
Dmitri Belimov724dcbf2011-02-01 05:25:19 -03001191static int xc5000_set_config(struct dvb_frontend *fe, void *priv_cfg)
1192{
1193 struct xc5000_priv *priv = fe->tuner_priv;
1194 struct xc5000_config *p = priv_cfg;
1195
1196 dprintk(1, "%s()\n", __func__);
1197
1198 if (p->if_khz)
1199 priv->if_khz = p->if_khz;
1200
1201 if (p->radio_input)
1202 priv->radio_input = p->radio_input;
1203
1204 return 0;
1205}
1206
1207
Steven Tothaacb9d32007-12-18 01:55:51 -03001208static const struct dvb_tuner_ops xc5000_tuner_ops = {
1209 .info = {
1210 .name = "Xceive XC5000",
1211 .frequency_min = 1000000,
1212 .frequency_max = 1023000000,
1213 .frequency_step = 50000,
1214 },
1215
Steven Toth27c685a2008-01-05 16:50:14 -03001216 .release = xc5000_release,
1217 .init = xc5000_init,
1218 .sleep = xc5000_sleep,
Steven Tothaacb9d32007-12-18 01:55:51 -03001219
Dmitri Belimov724dcbf2011-02-01 05:25:19 -03001220 .set_config = xc5000_set_config,
Steven Toth27c685a2008-01-05 16:50:14 -03001221 .set_params = xc5000_set_params,
1222 .set_analog_params = xc5000_set_analog_params,
1223 .get_frequency = xc5000_get_frequency,
Mauro Carvalho Chehab35621032011-09-23 13:03:42 -03001224 .get_if_frequency = xc5000_get_if_frequency,
Steven Toth27c685a2008-01-05 16:50:14 -03001225 .get_bandwidth = xc5000_get_bandwidth,
1226 .get_status = xc5000_get_status
Steven Tothaacb9d32007-12-18 01:55:51 -03001227};
1228
Michael Krufky48723542008-05-10 14:34:09 -03001229struct dvb_frontend *xc5000_attach(struct dvb_frontend *fe,
1230 struct i2c_adapter *i2c,
lawrence rust2e4e98e2010-08-25 09:50:20 -03001231 const struct xc5000_config *cfg)
Steven Tothaacb9d32007-12-18 01:55:51 -03001232{
1233 struct xc5000_priv *priv = NULL;
Michael Krufky89fd2852008-09-06 11:44:53 -03001234 int instance;
Steven Tothaacb9d32007-12-18 01:55:51 -03001235 u16 id = 0;
1236
Michael Krufky89fd2852008-09-06 11:44:53 -03001237 dprintk(1, "%s(%d-%04x)\n", __func__,
1238 i2c ? i2c_adapter_id(i2c) : -1,
1239 cfg ? cfg->i2c_address : -1);
Steven Tothaacb9d32007-12-18 01:55:51 -03001240
Michael Krufky89fd2852008-09-06 11:44:53 -03001241 mutex_lock(&xc5000_list_mutex);
Steven Tothaacb9d32007-12-18 01:55:51 -03001242
Michael Krufky89fd2852008-09-06 11:44:53 -03001243 instance = hybrid_tuner_request_state(struct xc5000_priv, priv,
1244 hybrid_tuner_instance_list,
1245 i2c, cfg->i2c_address, "xc5000");
1246 switch (instance) {
1247 case 0:
1248 goto fail;
1249 break;
1250 case 1:
1251 /* new tuner instance */
Mauro Carvalho Chehabc6f56e72011-12-26 20:02:28 -03001252 priv->bandwidth = 6000000;
Michael Krufky89fd2852008-09-06 11:44:53 -03001253 fe->tuner_priv = priv;
1254 break;
1255 default:
1256 /* existing tuner instance */
1257 fe->tuner_priv = priv;
1258 break;
1259 }
Steven Tothaacb9d32007-12-18 01:55:51 -03001260
Devin Heitmuellerea227862009-03-11 02:58:53 -03001261 if (priv->if_khz == 0) {
1262 /* If the IF hasn't been set yet, use the value provided by
1263 the caller (occurs in hybrid devices where the analog
1264 call to xc5000_attach occurs before the digital side) */
1265 priv->if_khz = cfg->if_khz;
1266 }
1267
Michael Krufky7d3d0d82012-04-16 14:59:32 -03001268 if (priv->xtal_khz == 0)
1269 priv->xtal_khz = cfg->xtal_khz;
1270
Devin Heitmueller496e9052009-09-24 13:27:24 -03001271 if (priv->radio_input == 0)
1272 priv->radio_input = cfg->radio_input;
1273
Michael Krufky6fab81d2012-02-08 14:57:39 -03001274 /* don't override chip id if it's already been set
Michael Krufky76efb0ba2012-02-06 19:40:32 -03001275 unless explicitly specified */
Michael Krufky6fab81d2012-02-08 14:57:39 -03001276 if ((priv->chip_id == 0) || (cfg->chip_id))
1277 /* use default chip id if none specified, set to 0 so
1278 it can be overridden if this is a hybrid driver */
1279 priv->chip_id = (cfg->chip_id) ? cfg->chip_id : 0;
Michael Krufky76efb0ba2012-02-06 19:40:32 -03001280
Steven Toth27c685a2008-01-05 16:50:14 -03001281 /* Check if firmware has been loaded. It is possible that another
1282 instance of the driver has loaded the firmware.
1283 */
Devin Heitmueller7988fc22008-11-16 20:23:19 -03001284 if (xc5000_readreg(priv, XREG_PRODUCT_ID, &id) != XC_RESULT_SUCCESS)
Michael Krufky89fd2852008-09-06 11:44:53 -03001285 goto fail;
Steven Tothaacb9d32007-12-18 01:55:51 -03001286
Steven Toth8f3cd532008-10-16 20:29:38 -03001287 switch (id) {
Steven Toth27c685a2008-01-05 16:50:14 -03001288 case XC_PRODUCT_ID_FW_LOADED:
1289 printk(KERN_INFO
1290 "xc5000: Successfully identified at address 0x%02x\n",
1291 cfg->i2c_address);
1292 printk(KERN_INFO
1293 "xc5000: Firmware has been loaded previously\n");
Steven Toth27c685a2008-01-05 16:50:14 -03001294 break;
1295 case XC_PRODUCT_ID_FW_NOT_LOADED:
1296 printk(KERN_INFO
1297 "xc5000: Successfully identified at address 0x%02x\n",
1298 cfg->i2c_address);
1299 printk(KERN_INFO
1300 "xc5000: Firmware has not been loaded previously\n");
Steven Toth27c685a2008-01-05 16:50:14 -03001301 break;
1302 default:
Steven Tothaacb9d32007-12-18 01:55:51 -03001303 printk(KERN_ERR
1304 "xc5000: Device not found at addr 0x%02x (0x%x)\n",
1305 cfg->i2c_address, id);
Michael Krufky89fd2852008-09-06 11:44:53 -03001306 goto fail;
Steven Tothaacb9d32007-12-18 01:55:51 -03001307 }
1308
Michael Krufky89fd2852008-09-06 11:44:53 -03001309 mutex_unlock(&xc5000_list_mutex);
1310
Steven Tothaacb9d32007-12-18 01:55:51 -03001311 memcpy(&fe->ops.tuner_ops, &xc5000_tuner_ops,
1312 sizeof(struct dvb_tuner_ops));
1313
Steven Tothaacb9d32007-12-18 01:55:51 -03001314 return fe;
Michael Krufky89fd2852008-09-06 11:44:53 -03001315fail:
1316 mutex_unlock(&xc5000_list_mutex);
1317
1318 xc5000_release(fe);
1319 return NULL;
Steven Tothaacb9d32007-12-18 01:55:51 -03001320}
1321EXPORT_SYMBOL(xc5000_attach);
1322
1323MODULE_AUTHOR("Steven Toth");
Steven Tothe12671c2007-12-20 01:14:43 -03001324MODULE_DESCRIPTION("Xceive xc5000 silicon tuner driver");
Steven Tothaacb9d32007-12-18 01:55:51 -03001325MODULE_LICENSE("GPL");
Tim Gardner3422f2a2012-07-25 09:15:19 -03001326MODULE_FIRMWARE(XC5000A_FIRMWARE);
1327MODULE_FIRMWARE(XC5000C_FIRMWARE);