blob: 43925e219d8152f78b3c31d4967c36c6e4a64c54 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Davide Ferri8d009a02009-06-23 22:34:06 -03002/*
3 * Driver for Xceive XC4000 "QAM/8VSB single chip tuner"
4 *
5 * Copyright (c) 2007 Xceive Corporation
6 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
7 * Copyright (c) 2009 Devin Heitmueller <dheitmueller@kernellabs.com>
8 * Copyright (c) 2009 Davide Ferri <d.ferri@zero11.it>
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03009 * Copyright (c) 2010 Istvan Varga <istvan_v@mailbox.hu>
Davide Ferri8d009a02009-06-23 22:34:06 -030010 */
11
12#include <linux/module.h>
13#include <linux/moduleparam.h>
14#include <linux/videodev2.h>
15#include <linux/delay.h>
16#include <linux/dvb/frontend.h>
17#include <linux/i2c.h>
Istvan Varga56149422011-06-03 12:23:33 -030018#include <linux/mutex.h>
Devin Heitmueller11091a32009-07-20 00:54:57 -030019#include <asm/unaligned.h>
Davide Ferri8d009a02009-06-23 22:34:06 -030020
Mauro Carvalho Chehabfada1932017-12-28 13:03:51 -050021#include <media/dvb_frontend.h>
Davide Ferri8d009a02009-06-23 22:34:06 -030022
23#include "xc4000.h"
24#include "tuner-i2c.h"
Devin Heitmueller11091a32009-07-20 00:54:57 -030025#include "tuner-xc2028-types.h"
Davide Ferri8d009a02009-06-23 22:34:06 -030026
Devin Heitmueller4922cec2009-12-27 17:50:43 -030027static int debug;
Davide Ferri8d009a02009-06-23 22:34:06 -030028module_param(debug, int, 0644);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -030029MODULE_PARM_DESC(debug, "Debugging level (0 to 2, default: 0 (off)).");
Davide Ferri8d009a02009-06-23 22:34:06 -030030
31static int no_poweroff;
32module_param(no_poweroff, int, 0644);
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -020033MODULE_PARM_DESC(no_poweroff, "Power management (1: disabled, 2: enabled, 0 (default): use device-specific default mode).");
Istvan Varga923137a2011-06-04 12:15:51 -030034
35static int audio_std;
36module_param(audio_std, int, 0644);
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -020037MODULE_PARM_DESC(audio_std, "Audio standard. XC4000 audio decoder explicitly needs to know what audio standard is needed for some video standards with audio A2 or NICAM. The valid settings are a sum of:\n"
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -030038 " 1: use NICAM/B or A2/B instead of NICAM/A or A2/A\n"
39 " 2: use A2 instead of NICAM or BTSC\n"
40 " 4: use SECAM/K3 instead of K1\n"
41 " 8: use PAL-D/K audio for SECAM-D/K\n"
42 "16: use FM radio input 1 instead of input 2\n"
43 "32: use mono audio (the lower three bits are ignored)");
Istvan Vargafa285bc2011-06-04 11:48:16 -030044
45static char firmware_name[30];
46module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -020047MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the default firmware name.");
Istvan Vargafa285bc2011-06-04 11:48:16 -030048
Davide Ferri8d009a02009-06-23 22:34:06 -030049static DEFINE_MUTEX(xc4000_list_mutex);
50static LIST_HEAD(hybrid_tuner_instance_list);
51
52#define dprintk(level, fmt, arg...) if (debug >= level) \
53 printk(KERN_INFO "%s: " fmt, "xc4000", ## arg)
54
Devin Heitmueller11091a32009-07-20 00:54:57 -030055/* struct for storing firmware table */
56struct firmware_description {
57 unsigned int type;
58 v4l2_std_id id;
59 __u16 int_freq;
60 unsigned char *ptr;
61 unsigned int size;
62};
63
64struct firmware_properties {
65 unsigned int type;
66 v4l2_std_id id;
67 v4l2_std_id std_req;
68 __u16 int_freq;
69 unsigned int scode_table;
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -030070 int scode_nr;
Devin Heitmueller11091a32009-07-20 00:54:57 -030071};
Davide Ferri8d009a02009-06-23 22:34:06 -030072
73struct xc4000_priv {
74 struct tuner_i2c_props i2c_props;
75 struct list_head hybrid_tuner_instance_list;
Devin Heitmueller11091a32009-07-20 00:54:57 -030076 struct firmware_description *firm;
Istvan Vargafbe4a292011-06-03 10:11:48 -030077 int firm_size;
Istvan Vargafbe4a292011-06-03 10:11:48 -030078 u32 if_khz;
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -030079 u32 freq_hz, freq_offset;
Istvan Vargafbe4a292011-06-03 10:11:48 -030080 u32 bandwidth;
81 u8 video_standard;
82 u8 rf_mode;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -030083 u8 default_pm;
84 u8 dvb_amplitude;
85 u8 set_smoothedcvbs;
Istvan Vargafbe4a292011-06-03 10:11:48 -030086 u8 ignore_i2c_write_errors;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -030087 __u16 firm_version;
Devin Heitmuellerd0962382009-07-25 17:39:54 -030088 struct firmware_properties cur_fw;
Istvan Vargafbe4a292011-06-03 10:11:48 -030089 __u16 hwmodel;
90 __u16 hwvers;
Istvan Varga56149422011-06-03 12:23:33 -030091 struct mutex lock;
Davide Ferri8d009a02009-06-23 22:34:06 -030092};
93
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -030094#define XC4000_AUDIO_STD_B 1
95#define XC4000_AUDIO_STD_A2 2
96#define XC4000_AUDIO_STD_K3 4
97#define XC4000_AUDIO_STD_L 8
98#define XC4000_AUDIO_STD_INPUT1 16
99#define XC4000_AUDIO_STD_MONO 32
100
101#define XC4000_DEFAULT_FIRMWARE "dvb-fe-xc4000-1.4.fw"
Mauro Carvalho Chehabda7bfa22014-07-21 13:28:13 -0300102#define XC4000_DEFAULT_FIRMWARE_NEW "dvb-fe-xc4000-1.4.1.fw"
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300103
Davide Ferri8d009a02009-06-23 22:34:06 -0300104/* Misc Defines */
Istvan Varga49110852011-06-03 10:55:24 -0300105#define MAX_TV_STANDARD 24
Davide Ferri8d009a02009-06-23 22:34:06 -0300106#define XC_MAX_I2C_WRITE_LENGTH 64
Istvan Varga5272f6b2011-06-04 12:03:03 -0300107#define XC_POWERED_DOWN 0x80000000U
Davide Ferri8d009a02009-06-23 22:34:06 -0300108
109/* Signal Types */
110#define XC_RF_MODE_AIR 0
111#define XC_RF_MODE_CABLE 1
112
Davide Ferri8d009a02009-06-23 22:34:06 -0300113/* Product id */
114#define XC_PRODUCT_ID_FW_NOT_LOADED 0x2000
Istvan Varga7db98fe2011-06-04 12:25:19 -0300115#define XC_PRODUCT_ID_XC4000 0x0FA0
116#define XC_PRODUCT_ID_XC4100 0x1004
Davide Ferri8d009a02009-06-23 22:34:06 -0300117
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300118/* Registers (Write-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300119#define XREG_INIT 0x00
120#define XREG_VIDEO_MODE 0x01
121#define XREG_AUDIO_MODE 0x02
122#define XREG_RF_FREQ 0x03
123#define XREG_D_CODE 0x04
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300124#define XREG_DIRECTSITTING_MODE 0x05
125#define XREG_SEEK_MODE 0x06
126#define XREG_POWER_DOWN 0x08
127#define XREG_SIGNALSOURCE 0x0A
Istvan Varga30f544e2011-06-04 12:12:42 -0300128#define XREG_SMOOTHEDCVBS 0x0E
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300129#define XREG_AMPLITUDE 0x10
Davide Ferri8d009a02009-06-23 22:34:06 -0300130
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300131/* Registers (Read-only) */
Davide Ferri8d009a02009-06-23 22:34:06 -0300132#define XREG_ADC_ENV 0x00
133#define XREG_QUALITY 0x01
134#define XREG_FRAME_LINES 0x02
135#define XREG_HSYNC_FREQ 0x03
136#define XREG_LOCK 0x04
137#define XREG_FREQ_ERROR 0x05
138#define XREG_SNR 0x06
139#define XREG_VERSION 0x07
140#define XREG_PRODUCT_ID 0x08
Miroslav Slugen8a538a82011-12-20 21:18:38 -0300141#define XREG_SIGNAL_LEVEL 0x0A
142#define XREG_NOISE_LEVEL 0x0B
Davide Ferri8d009a02009-06-23 22:34:06 -0300143
144/*
145 Basic firmware description. This will remain with
146 the driver for documentation purposes.
147
148 This represents an I2C firmware file encoded as a
149 string of unsigned char. Format is as follows:
150
151 char[0 ]=len0_MSB -> len = len_MSB * 256 + len_LSB
152 char[1 ]=len0_LSB -> length of first write transaction
153 char[2 ]=data0 -> first byte to be sent
154 char[3 ]=data1
155 char[4 ]=data2
156 char[ ]=...
157 char[M ]=dataN -> last byte to be sent
158 char[M+1]=len1_MSB -> len = len_MSB * 256 + len_LSB
159 char[M+2]=len1_LSB -> length of second write transaction
160 char[M+3]=data0
161 char[M+4]=data1
162 ...
163 etc.
164
165 The [len] value should be interpreted as follows:
166
167 len= len_MSB _ len_LSB
168 len=1111_1111_1111_1111 : End of I2C_SEQUENCE
169 len=0000_0000_0000_0000 : Reset command: Do hardware reset
170 len=0NNN_NNNN_NNNN_NNNN : Normal transaction: number of bytes = {1:32767)
171 len=1WWW_WWWW_WWWW_WWWW : Wait command: wait for {1:32767} ms
172
173 For the RESET and WAIT commands, the two following bytes will contain
174 immediately the length of the following transaction.
Davide Ferri8d009a02009-06-23 22:34:06 -0300175*/
Istvan Vargafbe4a292011-06-03 10:11:48 -0300176
Davide Ferri8d009a02009-06-23 22:34:06 -0300177struct XC_TV_STANDARD {
Istvan Vargafbe4a292011-06-03 10:11:48 -0300178 const char *Name;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300179 u16 audio_mode;
180 u16 video_mode;
Istvan Varga49110852011-06-03 10:55:24 -0300181 u16 int_freq;
Davide Ferri8d009a02009-06-23 22:34:06 -0300182};
183
184/* Tuner standards */
Devin Heitmuellered23db32009-10-05 01:27:14 -0300185#define XC4000_MN_NTSC_PAL_BTSC 0
186#define XC4000_MN_NTSC_PAL_A2 1
187#define XC4000_MN_NTSC_PAL_EIAJ 2
188#define XC4000_MN_NTSC_PAL_Mono 3
189#define XC4000_BG_PAL_A2 4
190#define XC4000_BG_PAL_NICAM 5
191#define XC4000_BG_PAL_MONO 6
192#define XC4000_I_PAL_NICAM 7
193#define XC4000_I_PAL_NICAM_MONO 8
194#define XC4000_DK_PAL_A2 9
195#define XC4000_DK_PAL_NICAM 10
196#define XC4000_DK_PAL_MONO 11
197#define XC4000_DK_SECAM_A2DK1 12
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300198#define XC4000_DK_SECAM_A2LDK3 13
199#define XC4000_DK_SECAM_A2MONO 14
Istvan Varga49110852011-06-03 10:55:24 -0300200#define XC4000_DK_SECAM_NICAM 15
201#define XC4000_L_SECAM_NICAM 16
202#define XC4000_LC_SECAM_NICAM 17
203#define XC4000_DTV6 18
204#define XC4000_DTV8 19
205#define XC4000_DTV7_8 20
206#define XC4000_DTV7 21
207#define XC4000_FM_Radio_INPUT2 22
208#define XC4000_FM_Radio_INPUT1 23
Davide Ferri8d009a02009-06-23 22:34:06 -0300209
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300210static struct XC_TV_STANDARD xc4000_standard[MAX_TV_STANDARD] = {
Istvan Varga49110852011-06-03 10:55:24 -0300211 {"M/N-NTSC/PAL-BTSC", 0x0000, 0x80A0, 4500},
212 {"M/N-NTSC/PAL-A2", 0x0000, 0x80A0, 4600},
213 {"M/N-NTSC/PAL-EIAJ", 0x0040, 0x80A0, 4500},
214 {"M/N-NTSC/PAL-Mono", 0x0078, 0x80A0, 4500},
215 {"B/G-PAL-A2", 0x0000, 0x8159, 5640},
216 {"B/G-PAL-NICAM", 0x0004, 0x8159, 5740},
217 {"B/G-PAL-MONO", 0x0078, 0x8159, 5500},
218 {"I-PAL-NICAM", 0x0080, 0x8049, 6240},
219 {"I-PAL-NICAM-MONO", 0x0078, 0x8049, 6000},
220 {"D/K-PAL-A2", 0x0000, 0x8049, 6380},
221 {"D/K-PAL-NICAM", 0x0080, 0x8049, 6200},
222 {"D/K-PAL-MONO", 0x0078, 0x8049, 6500},
223 {"D/K-SECAM-A2 DK1", 0x0000, 0x8049, 6340},
224 {"D/K-SECAM-A2 L/DK3", 0x0000, 0x8049, 6000},
225 {"D/K-SECAM-A2 MONO", 0x0078, 0x8049, 6500},
226 {"D/K-SECAM-NICAM", 0x0080, 0x8049, 6200},
227 {"L-SECAM-NICAM", 0x8080, 0x0009, 6200},
228 {"L'-SECAM-NICAM", 0x8080, 0x4009, 6200},
229 {"DTV6", 0x00C0, 0x8002, 0},
230 {"DTV8", 0x00C0, 0x800B, 0},
231 {"DTV7/8", 0x00C0, 0x801B, 0},
232 {"DTV7", 0x00C0, 0x8007, 0},
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300233 {"FM Radio-INPUT2", 0x0008, 0x9800, 10700},
234 {"FM Radio-INPUT1", 0x0008, 0x9000, 10700}
Davide Ferri8d009a02009-06-23 22:34:06 -0300235};
236
Davide Ferri8d009a02009-06-23 22:34:06 -0300237static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300238static int xc4000_tuner_reset(struct dvb_frontend *fe);
Istvan Vargaf4312e2f2011-06-04 12:08:29 -0300239static void xc_debug_dump(struct xc4000_priv *priv);
Davide Ferri8d009a02009-06-23 22:34:06 -0300240
241static int xc_send_i2c_data(struct xc4000_priv *priv, u8 *buf, int len)
242{
243 struct i2c_msg msg = { .addr = priv->i2c_props.addr,
244 .flags = 0, .buf = buf, .len = len };
Davide Ferri8d009a02009-06-23 22:34:06 -0300245 if (i2c_transfer(priv->i2c_props.adap, &msg, 1) != 1) {
Devin Heitmueller799ed112009-10-04 23:09:18 -0300246 if (priv->ignore_i2c_write_errors == 0) {
247 printk(KERN_ERR "xc4000: I2C write failed (len=%i)\n",
248 len);
249 if (len == 4) {
Andy Shevchenkoc6480cc2012-08-07 12:43:03 -0300250 printk(KERN_ERR "bytes %*ph\n", 4, buf);
Devin Heitmueller799ed112009-10-04 23:09:18 -0300251 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300252 return -EREMOTEIO;
Devin Heitmueller799ed112009-10-04 23:09:18 -0300253 }
Davide Ferri8d009a02009-06-23 22:34:06 -0300254 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300255 return 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300256}
257
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300258static int xc4000_tuner_reset(struct dvb_frontend *fe)
Davide Ferri8d009a02009-06-23 22:34:06 -0300259{
260 struct xc4000_priv *priv = fe->tuner_priv;
261 int ret;
262
263 dprintk(1, "%s()\n", __func__);
264
265 if (fe->callback) {
266 ret = fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
267 fe->dvb->priv :
268 priv->i2c_props.adap->algo_data,
269 DVB_FRONTEND_COMPONENT_TUNER,
270 XC4000_TUNER_RESET, 0);
271 if (ret) {
272 printk(KERN_ERR "xc4000: reset failed\n");
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300273 return -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -0300274 }
275 } else {
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -0200276 printk(KERN_ERR "xc4000: no tuner reset callback function, fatal\n");
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300277 return -EINVAL;
Davide Ferri8d009a02009-06-23 22:34:06 -0300278 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300279 return 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300280}
281
282static int xc_write_reg(struct xc4000_priv *priv, u16 regAddr, u16 i2cData)
283{
284 u8 buf[4];
Davide Ferri8d009a02009-06-23 22:34:06 -0300285 int result;
286
287 buf[0] = (regAddr >> 8) & 0xFF;
288 buf[1] = regAddr & 0xFF;
289 buf[2] = (i2cData >> 8) & 0xFF;
290 buf[3] = i2cData & 0xFF;
291 result = xc_send_i2c_data(priv, buf, 4);
Davide Ferri8d009a02009-06-23 22:34:06 -0300292
293 return result;
294}
295
296static int xc_load_i2c_sequence(struct dvb_frontend *fe, const u8 *i2c_sequence)
297{
298 struct xc4000_priv *priv = fe->tuner_priv;
299
300 int i, nbytes_to_send, result;
301 unsigned int len, pos, index;
302 u8 buf[XC_MAX_I2C_WRITE_LENGTH];
303
304 index = 0;
305 while ((i2c_sequence[index] != 0xFF) ||
306 (i2c_sequence[index + 1] != 0xFF)) {
307 len = i2c_sequence[index] * 256 + i2c_sequence[index+1];
308 if (len == 0x0000) {
309 /* RESET command */
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300310 /* NOTE: this is ignored, as the reset callback was */
311 /* already called by check_firmware() */
Davide Ferri8d009a02009-06-23 22:34:06 -0300312 index += 2;
Davide Ferri8d009a02009-06-23 22:34:06 -0300313 } else if (len & 0x8000) {
314 /* WAIT command */
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300315 msleep(len & 0x7FFF);
Davide Ferri8d009a02009-06-23 22:34:06 -0300316 index += 2;
317 } else {
318 /* Send i2c data whilst ensuring individual transactions
319 * do not exceed XC_MAX_I2C_WRITE_LENGTH bytes.
320 */
321 index += 2;
322 buf[0] = i2c_sequence[index];
323 buf[1] = i2c_sequence[index + 1];
324 pos = 2;
325 while (pos < len) {
326 if ((len - pos) > XC_MAX_I2C_WRITE_LENGTH - 2)
327 nbytes_to_send =
328 XC_MAX_I2C_WRITE_LENGTH;
329 else
330 nbytes_to_send = (len - pos + 2);
331 for (i = 2; i < nbytes_to_send; i++) {
332 buf[i] = i2c_sequence[index + pos +
333 i - 2];
334 }
335 result = xc_send_i2c_data(priv, buf,
336 nbytes_to_send);
337
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300338 if (result != 0)
Davide Ferri8d009a02009-06-23 22:34:06 -0300339 return result;
340
341 pos += nbytes_to_send - 2;
342 }
343 index += len;
344 }
345 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300346 return 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300347}
348
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300349static int xc_set_tv_standard(struct xc4000_priv *priv,
350 u16 video_mode, u16 audio_mode)
Davide Ferri8d009a02009-06-23 22:34:06 -0300351{
352 int ret;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300353 dprintk(1, "%s(0x%04x,0x%04x)\n", __func__, video_mode, audio_mode);
Davide Ferri8d009a02009-06-23 22:34:06 -0300354 dprintk(1, "%s() Standard = %s\n",
355 __func__,
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300356 xc4000_standard[priv->video_standard].Name);
Davide Ferri8d009a02009-06-23 22:34:06 -0300357
Devin Heitmueller799ed112009-10-04 23:09:18 -0300358 /* Don't complain when the request fails because of i2c stretching */
359 priv->ignore_i2c_write_errors = 1;
360
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300361 ret = xc_write_reg(priv, XREG_VIDEO_MODE, video_mode);
362 if (ret == 0)
363 ret = xc_write_reg(priv, XREG_AUDIO_MODE, audio_mode);
Davide Ferri8d009a02009-06-23 22:34:06 -0300364
Devin Heitmueller799ed112009-10-04 23:09:18 -0300365 priv->ignore_i2c_write_errors = 0;
366
Davide Ferri8d009a02009-06-23 22:34:06 -0300367 return ret;
368}
369
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300370static int xc_set_signal_source(struct xc4000_priv *priv, u16 rf_mode)
Davide Ferri8d009a02009-06-23 22:34:06 -0300371{
372 dprintk(1, "%s(%d) Source = %s\n", __func__, rf_mode,
373 rf_mode == XC_RF_MODE_AIR ? "ANTENNA" : "CABLE");
374
375 if ((rf_mode != XC_RF_MODE_AIR) && (rf_mode != XC_RF_MODE_CABLE)) {
376 rf_mode = XC_RF_MODE_CABLE;
377 printk(KERN_ERR
378 "%s(), Invalid mode, defaulting to CABLE",
379 __func__);
380 }
381 return xc_write_reg(priv, XREG_SIGNALSOURCE, rf_mode);
382}
383
384static const struct dvb_tuner_ops xc4000_tuner_ops;
385
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300386static int xc_set_rf_frequency(struct xc4000_priv *priv, u32 freq_hz)
Davide Ferri8d009a02009-06-23 22:34:06 -0300387{
388 u16 freq_code;
389
390 dprintk(1, "%s(%u)\n", __func__, freq_hz);
391
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400392 if ((freq_hz > xc4000_tuner_ops.info.frequency_max_hz) ||
393 (freq_hz < xc4000_tuner_ops.info.frequency_min_hz))
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300394 return -EINVAL;
Davide Ferri8d009a02009-06-23 22:34:06 -0300395
396 freq_code = (u16)(freq_hz / 15625);
397
398 /* WAS: Starting in firmware version 1.1.44, Xceive recommends using the
399 FINERFREQ for all normal tuning (the doc indicates reg 0x03 should
400 only be used for fast scanning for channel lock) */
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300401 /* WAS: XREG_FINERFREQ */
402 return xc_write_reg(priv, XREG_RF_FREQ, freq_code);
Davide Ferri8d009a02009-06-23 22:34:06 -0300403}
404
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300405static int xc_get_adc_envelope(struct xc4000_priv *priv, u16 *adc_envelope)
Davide Ferri8d009a02009-06-23 22:34:06 -0300406{
407 return xc4000_readreg(priv, XREG_ADC_ENV, adc_envelope);
408}
409
410static int xc_get_frequency_error(struct xc4000_priv *priv, u32 *freq_error_hz)
411{
412 int result;
413 u16 regData;
414 u32 tmp;
415
416 result = xc4000_readreg(priv, XREG_FREQ_ERROR, &regData);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300417 if (result != 0)
Davide Ferri8d009a02009-06-23 22:34:06 -0300418 return result;
419
Istvan Varga1368ceb2011-06-03 12:27:30 -0300420 tmp = (u32)regData & 0xFFFFU;
421 tmp = (tmp < 0x8000U ? tmp : 0x10000U - tmp);
422 (*freq_error_hz) = tmp * 15625;
Davide Ferri8d009a02009-06-23 22:34:06 -0300423 return result;
424}
425
426static int xc_get_lock_status(struct xc4000_priv *priv, u16 *lock_status)
427{
428 return xc4000_readreg(priv, XREG_LOCK, lock_status);
429}
430
431static int xc_get_version(struct xc4000_priv *priv,
432 u8 *hw_majorversion, u8 *hw_minorversion,
433 u8 *fw_majorversion, u8 *fw_minorversion)
434{
435 u16 data;
436 int result;
437
438 result = xc4000_readreg(priv, XREG_VERSION, &data);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300439 if (result != 0)
Davide Ferri8d009a02009-06-23 22:34:06 -0300440 return result;
441
442 (*hw_majorversion) = (data >> 12) & 0x0F;
443 (*hw_minorversion) = (data >> 8) & 0x0F;
444 (*fw_majorversion) = (data >> 4) & 0x0F;
445 (*fw_minorversion) = data & 0x0F;
446
447 return 0;
448}
449
Davide Ferri8d009a02009-06-23 22:34:06 -0300450static int xc_get_hsync_freq(struct xc4000_priv *priv, u32 *hsync_freq_hz)
451{
452 u16 regData;
453 int result;
454
455 result = xc4000_readreg(priv, XREG_HSYNC_FREQ, &regData);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300456 if (result != 0)
Davide Ferri8d009a02009-06-23 22:34:06 -0300457 return result;
458
459 (*hsync_freq_hz) = ((regData & 0x0fff) * 763)/100;
460 return result;
461}
462
463static int xc_get_frame_lines(struct xc4000_priv *priv, u16 *frame_lines)
464{
465 return xc4000_readreg(priv, XREG_FRAME_LINES, frame_lines);
466}
467
468static int xc_get_quality(struct xc4000_priv *priv, u16 *quality)
469{
470 return xc4000_readreg(priv, XREG_QUALITY, quality);
471}
472
Miroslav Slugen8a538a82011-12-20 21:18:38 -0300473static int xc_get_signal_level(struct xc4000_priv *priv, u16 *signal)
474{
475 return xc4000_readreg(priv, XREG_SIGNAL_LEVEL, signal);
476}
477
478static int xc_get_noise_level(struct xc4000_priv *priv, u16 *noise)
479{
480 return xc4000_readreg(priv, XREG_NOISE_LEVEL, noise);
481}
482
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300483static u16 xc_wait_for_lock(struct xc4000_priv *priv)
Davide Ferri8d009a02009-06-23 22:34:06 -0300484{
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300485 u16 lock_state = 0;
486 int watchdog_count = 40;
Davide Ferri8d009a02009-06-23 22:34:06 -0300487
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300488 while ((lock_state == 0) && (watchdog_count > 0)) {
489 xc_get_lock_status(priv, &lock_state);
490 if (lock_state != 1) {
491 msleep(5);
492 watchdog_count--;
Davide Ferri8d009a02009-06-23 22:34:06 -0300493 }
494 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300495 return lock_state;
Davide Ferri8d009a02009-06-23 22:34:06 -0300496}
497
Istvan Vargae75873c2011-06-04 12:18:41 -0300498static int xc_tune_channel(struct xc4000_priv *priv, u32 freq_hz)
Davide Ferri8d009a02009-06-23 22:34:06 -0300499{
Istvan Vargae75873c2011-06-04 12:18:41 -0300500 int found = 1;
501 int result;
Davide Ferri8d009a02009-06-23 22:34:06 -0300502
503 dprintk(1, "%s(%u)\n", __func__, freq_hz);
504
Devin Heitmueller799ed112009-10-04 23:09:18 -0300505 /* Don't complain when the request fails because of i2c stretching */
506 priv->ignore_i2c_write_errors = 1;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300507 result = xc_set_rf_frequency(priv, freq_hz);
Devin Heitmueller799ed112009-10-04 23:09:18 -0300508 priv->ignore_i2c_write_errors = 0;
509
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300510 if (result != 0)
Davide Ferri8d009a02009-06-23 22:34:06 -0300511 return 0;
512
Istvan Vargae75873c2011-06-04 12:18:41 -0300513 /* wait for lock only in analog TV mode */
514 if ((priv->cur_fw.type & (FM | DTV6 | DTV7 | DTV78 | DTV8)) == 0) {
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300515 if (xc_wait_for_lock(priv) != 1)
Istvan Vargae75873c2011-06-04 12:18:41 -0300516 found = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300517 }
518
Istvan Vargaf4312e2f2011-06-04 12:08:29 -0300519 /* Wait for stats to stabilize.
520 * Frame Lines needs two frame times after initial lock
521 * before it is valid.
522 */
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300523 msleep(debug ? 100 : 10);
Istvan Vargaf4312e2f2011-06-04 12:08:29 -0300524
525 if (debug)
526 xc_debug_dump(priv);
527
Davide Ferri8d009a02009-06-23 22:34:06 -0300528 return found;
529}
530
531static int xc4000_readreg(struct xc4000_priv *priv, u16 reg, u16 *val)
532{
533 u8 buf[2] = { reg >> 8, reg & 0xff };
534 u8 bval[2] = { 0, 0 };
535 struct i2c_msg msg[2] = {
536 { .addr = priv->i2c_props.addr,
537 .flags = 0, .buf = &buf[0], .len = 2 },
538 { .addr = priv->i2c_props.addr,
539 .flags = I2C_M_RD, .buf = &bval[0], .len = 2 },
540 };
541
542 if (i2c_transfer(priv->i2c_props.adap, msg, 2) != 2) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300543 printk(KERN_ERR "xc4000: I2C read failed\n");
Davide Ferri8d009a02009-06-23 22:34:06 -0300544 return -EREMOTEIO;
545 }
546
547 *val = (bval[0] << 8) | bval[1];
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300548 return 0;
Davide Ferri8d009a02009-06-23 22:34:06 -0300549}
550
Mauro Carvalho Chehabe3bb7c62011-06-02 11:36:56 -0300551#define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300552static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
553{
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300554 if (type & BASE)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300555 printk(KERN_CONT "BASE ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300556 if (type & INIT1)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300557 printk(KERN_CONT "INIT1 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300558 if (type & F8MHZ)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300559 printk(KERN_CONT "F8MHZ ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300560 if (type & MTS)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300561 printk(KERN_CONT "MTS ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300562 if (type & D2620)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300563 printk(KERN_CONT "D2620 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300564 if (type & D2633)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300565 printk(KERN_CONT "D2633 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300566 if (type & DTV6)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300567 printk(KERN_CONT "DTV6 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300568 if (type & QAM)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300569 printk(KERN_CONT "QAM ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300570 if (type & DTV7)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300571 printk(KERN_CONT "DTV7 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300572 if (type & DTV78)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300573 printk(KERN_CONT "DTV78 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300574 if (type & DTV8)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300575 printk(KERN_CONT "DTV8 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300576 if (type & FM)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300577 printk(KERN_CONT "FM ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300578 if (type & INPUT1)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300579 printk(KERN_CONT "INPUT1 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300580 if (type & LCD)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300581 printk(KERN_CONT "LCD ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300582 if (type & NOGD)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300583 printk(KERN_CONT "NOGD ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300584 if (type & MONO)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300585 printk(KERN_CONT "MONO ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300586 if (type & ATSC)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300587 printk(KERN_CONT "ATSC ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300588 if (type & IF)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300589 printk(KERN_CONT "IF ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300590 if (type & LG60)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300591 printk(KERN_CONT "LG60 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300592 if (type & ATI638)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300593 printk(KERN_CONT "ATI638 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300594 if (type & OREN538)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300595 printk(KERN_CONT "OREN538 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300596 if (type & OREN36)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300597 printk(KERN_CONT "OREN36 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300598 if (type & TOYOTA388)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300599 printk(KERN_CONT "TOYOTA388 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300600 if (type & TOYOTA794)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300601 printk(KERN_CONT "TOYOTA794 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300602 if (type & DIBCOM52)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300603 printk(KERN_CONT "DIBCOM52 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300604 if (type & ZARLINK456)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300605 printk(KERN_CONT "ZARLINK456 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300606 if (type & CHINA)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300607 printk(KERN_CONT "CHINA ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300608 if (type & F6MHZ)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300609 printk(KERN_CONT "F6MHZ ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300610 if (type & INPUT2)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300611 printk(KERN_CONT "INPUT2 ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300612 if (type & SCODE)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300613 printk(KERN_CONT "SCODE ");
Mauro Carvalho Chehab97e0e1e2014-09-03 18:42:09 -0300614 if (type & HAS_IF)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300615 printk(KERN_CONT "HAS_IF_%d ", int_freq);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300616}
617
Devin Heitmueller11091a32009-07-20 00:54:57 -0300618static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
619 v4l2_std_id *id)
620{
621 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga3db95702011-06-04 11:52:34 -0300622 int i, best_i = -1;
623 unsigned int best_nr_diffs = 255U;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300624
Devin Heitmueller11091a32009-07-20 00:54:57 -0300625 if (!priv->firm) {
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300626 printk(KERN_ERR "Error! firmware not loaded\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300627 return -EINVAL;
628 }
629
630 if (((type & ~SCODE) == 0) && (*id == 0))
631 *id = V4L2_STD_PAL;
632
Devin Heitmueller11091a32009-07-20 00:54:57 -0300633 /* Seek for generic video standard match */
634 for (i = 0; i < priv->firm_size; i++) {
Istvan Varga3db95702011-06-04 11:52:34 -0300635 v4l2_std_id id_diff_mask =
636 (priv->firm[i].id ^ (*id)) & (*id);
637 unsigned int type_diff_mask =
638 (priv->firm[i].type ^ type)
639 & (BASE_TYPES | DTV_TYPES | LCD | NOGD | MONO | SCODE);
640 unsigned int nr_diffs;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300641
Istvan Varga3db95702011-06-04 11:52:34 -0300642 if (type_diff_mask
643 & (BASE | INIT1 | FM | DTV6 | DTV7 | DTV78 | DTV8 | SCODE))
Devin Heitmueller11091a32009-07-20 00:54:57 -0300644 continue;
645
Istvan Varga3db95702011-06-04 11:52:34 -0300646 nr_diffs = hweight64(id_diff_mask) + hweight32(type_diff_mask);
647 if (!nr_diffs) /* Supports all the requested standards */
648 goto found;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300649
Istvan Varga3db95702011-06-04 11:52:34 -0300650 if (nr_diffs < best_nr_diffs) {
651 best_nr_diffs = nr_diffs;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300652 best_i = i;
653 }
654 }
655
Istvan Varga3db95702011-06-04 11:52:34 -0300656 /* FIXME: Would make sense to seek for type "hint" match ? */
657 if (best_i < 0) {
658 i = -ENOENT;
659 goto ret;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300660 }
661
Istvan Varga3db95702011-06-04 11:52:34 -0300662 if (best_nr_diffs > 0U) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300663 printk(KERN_WARNING
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -0200664 "Selecting best matching firmware (%u bits differ) for type=(%x), id %016llx:\n",
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300665 best_nr_diffs, type, (unsigned long long)*id);
Istvan Varga3db95702011-06-04 11:52:34 -0300666 i = best_i;
667 }
Devin Heitmueller11091a32009-07-20 00:54:57 -0300668
669found:
670 *id = priv->firm[i].id;
671
672ret:
Devin Heitmueller11091a32009-07-20 00:54:57 -0300673 if (debug) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300674 printk(KERN_DEBUG "%s firmware for type=",
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300675 (i < 0) ? "Can't find" : "Found");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300676 dump_firm_type(type);
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300677 printk(KERN_DEBUG "(%x), id %016llx.\n", type, (unsigned long long)*id);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300678 }
679 return i;
680}
681
682static int load_firmware(struct dvb_frontend *fe, unsigned int type,
683 v4l2_std_id *id)
684{
685 struct xc4000_priv *priv = fe->tuner_priv;
686 int pos, rc;
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300687 unsigned char *p;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300688
Devin Heitmueller11091a32009-07-20 00:54:57 -0300689 pos = seek_firmware(fe, type, id);
690 if (pos < 0)
691 return pos;
692
Devin Heitmueller11091a32009-07-20 00:54:57 -0300693 p = priv->firm[pos].ptr;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300694
Devin Heitmueller799ed112009-10-04 23:09:18 -0300695 /* Don't complain when the request fails because of i2c stretching */
696 priv->ignore_i2c_write_errors = 1;
697
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300698 rc = xc_load_i2c_sequence(fe, p);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300699
Devin Heitmueller799ed112009-10-04 23:09:18 -0300700 priv->ignore_i2c_write_errors = 0;
701
Devin Heitmueller31f880e2009-07-20 02:15:31 -0300702 return rc;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300703}
704
Davide Ferri8d009a02009-06-23 22:34:06 -0300705static int xc4000_fwupload(struct dvb_frontend *fe)
706{
707 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300708 const struct firmware *fw = NULL;
709 const unsigned char *p, *endp;
710 int rc = 0;
711 int n, n_array;
712 char name[33];
Istvan Vargafbe4a292011-06-03 10:11:48 -0300713 const char *fname;
Davide Ferri8d009a02009-06-23 22:34:06 -0300714
Mauro Carvalho Chehabda7bfa22014-07-21 13:28:13 -0300715 if (firmware_name[0] != '\0') {
Istvan Vargafa285bc2011-06-04 11:48:16 -0300716 fname = firmware_name;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300717
Mauro Carvalho Chehabda7bfa22014-07-21 13:28:13 -0300718 dprintk(1, "Reading custom firmware %s\n", fname);
719 rc = request_firmware(&fw, fname,
720 priv->i2c_props.adap->dev.parent);
721 } else {
722 fname = XC4000_DEFAULT_FIRMWARE_NEW;
723 dprintk(1, "Trying to read firmware %s\n", fname);
724 rc = request_firmware(&fw, fname,
725 priv->i2c_props.adap->dev.parent);
726 if (rc == -ENOENT) {
727 fname = XC4000_DEFAULT_FIRMWARE;
728 dprintk(1, "Trying to read firmware %s\n", fname);
729 rc = request_firmware(&fw, fname,
730 priv->i2c_props.adap->dev.parent);
731 }
732 }
733
Devin Heitmueller11091a32009-07-20 00:54:57 -0300734 if (rc < 0) {
735 if (rc == -ENOENT)
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300736 printk(KERN_ERR "Error: firmware %s not found.\n", fname);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300737 else
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300738 printk(KERN_ERR "Error %d while requesting firmware %s\n",
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300739 rc, fname);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300740
741 return rc;
742 }
Mauro Carvalho Chehabda7bfa22014-07-21 13:28:13 -0300743 dprintk(1, "Loading Firmware: %s\n", fname);
744
Devin Heitmueller11091a32009-07-20 00:54:57 -0300745 p = fw->data;
746 endp = p + fw->size;
747
748 if (fw->size < sizeof(name) - 1 + 2 + 2) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300749 printk(KERN_ERR "Error: firmware file %s has invalid size!\n",
750 fname);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300751 goto corrupt;
Davide Ferri8d009a02009-06-23 22:34:06 -0300752 }
753
Devin Heitmueller11091a32009-07-20 00:54:57 -0300754 memcpy(name, p, sizeof(name) - 1);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300755 name[sizeof(name) - 1] = '\0';
Devin Heitmueller11091a32009-07-20 00:54:57 -0300756 p += sizeof(name) - 1;
757
758 priv->firm_version = get_unaligned_le16(p);
759 p += 2;
760
761 n_array = get_unaligned_le16(p);
762 p += 2;
763
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300764 dprintk(1, "Loading %d firmware images from %s, type: %s, ver %d.%d\n",
765 n_array, fname, name,
766 priv->firm_version >> 8, priv->firm_version & 0xff);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300767
Thomas Meyer1b7acf02011-11-29 17:08:00 -0300768 priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300769 if (priv->firm == NULL) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300770 printk(KERN_ERR "Not enough memory to load firmware file.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300771 rc = -ENOMEM;
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300772 goto done;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300773 }
774 priv->firm_size = n_array;
775
776 n = -1;
777 while (p < endp) {
778 __u32 type, size;
779 v4l2_std_id id;
780 __u16 int_freq = 0;
781
782 n++;
783 if (n >= n_array) {
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -0200784 printk(KERN_ERR "More firmware images in file than were expected!\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300785 goto corrupt;
786 }
787
788 /* Checks if there's enough bytes to read */
789 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
790 goto header;
791
792 type = get_unaligned_le32(p);
793 p += sizeof(type);
794
795 id = get_unaligned_le64(p);
796 p += sizeof(id);
797
798 if (type & HAS_IF) {
799 int_freq = get_unaligned_le16(p);
800 p += sizeof(int_freq);
801 if (endp - p < sizeof(size))
802 goto header;
803 }
804
805 size = get_unaligned_le32(p);
806 p += sizeof(size);
807
808 if (!size || size > endp - p) {
Mauro Carvalho Chehab82188402018-08-07 08:16:09 -0400809 printk(KERN_ERR "Firmware type (%x), id %llx is corrupted (size=%zd, expected %d)\n",
Devin Heitmueller11091a32009-07-20 00:54:57 -0300810 type, (unsigned long long)id,
Mauro Carvalho Chehab82188402018-08-07 08:16:09 -0400811 endp - p, size);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300812 goto corrupt;
813 }
814
815 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
816 if (priv->firm[n].ptr == NULL) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300817 printk(KERN_ERR "Not enough memory to load firmware file.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300818 rc = -ENOMEM;
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300819 goto done;
Devin Heitmueller11091a32009-07-20 00:54:57 -0300820 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300821
Devin Heitmueller11091a32009-07-20 00:54:57 -0300822 if (debug) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300823 printk(KERN_DEBUG "Reading firmware type ");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300824 dump_firm_type_and_int_freq(type, int_freq);
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300825 printk(KERN_DEBUG "(%x), id %llx, size=%d.\n",
Devin Heitmueller11091a32009-07-20 00:54:57 -0300826 type, (unsigned long long)id, size);
827 }
828
829 memcpy(priv->firm[n].ptr, p, size);
830 priv->firm[n].type = type;
831 priv->firm[n].id = id;
832 priv->firm[n].size = size;
833 priv->firm[n].int_freq = int_freq;
834
835 p += size;
Davide Ferri8d009a02009-06-23 22:34:06 -0300836 }
837
Devin Heitmueller11091a32009-07-20 00:54:57 -0300838 if (n + 1 != priv->firm_size) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300839 printk(KERN_ERR "Firmware file is incomplete!\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300840 goto corrupt;
841 }
842
843 goto done;
844
845header:
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300846 printk(KERN_ERR "Firmware header is incomplete!\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300847corrupt:
848 rc = -EINVAL;
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300849 printk(KERN_ERR "Error: firmware file is corrupted!\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300850
851done:
Davide Ferri8d009a02009-06-23 22:34:06 -0300852 release_firmware(fw);
Devin Heitmueller11091a32009-07-20 00:54:57 -0300853 if (rc == 0)
Devin Heitmuellerb6cdb5b2009-12-27 18:15:14 -0300854 dprintk(1, "Firmware files loaded.\n");
Devin Heitmueller11091a32009-07-20 00:54:57 -0300855
856 return rc;
Davide Ferri8d009a02009-06-23 22:34:06 -0300857}
858
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300859static int load_scode(struct dvb_frontend *fe, unsigned int type,
860 v4l2_std_id *id, __u16 int_freq, int scode)
861{
862 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Vargaffce6262011-06-04 11:56:18 -0300863 int pos, rc;
864 unsigned char *p;
865 u8 scode_buf[13];
866 u8 indirect_mode[5];
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300867
Devin Heitmuellerfe830362009-07-28 00:04:27 -0300868 dprintk(1, "%s called int_freq=%d\n", __func__, int_freq);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300869
870 if (!int_freq) {
871 pos = seek_firmware(fe, type, id);
872 if (pos < 0)
873 return pos;
874 } else {
875 for (pos = 0; pos < priv->firm_size; pos++) {
876 if ((priv->firm[pos].int_freq == int_freq) &&
877 (priv->firm[pos].type & HAS_IF))
878 break;
879 }
880 if (pos == priv->firm_size)
881 return -ENOENT;
882 }
883
884 p = priv->firm[pos].ptr;
885
Istvan Vargaffce6262011-06-04 11:56:18 -0300886 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
887 return -EINVAL;
888 p += 12 * scode;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300889
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300890 if (debug) {
891 tuner_info("Loading SCODE for type=");
892 dump_firm_type_and_int_freq(priv->firm[pos].type,
893 priv->firm[pos].int_freq);
894 printk(KERN_CONT "(%x), id %016llx.\n", priv->firm[pos].type,
895 (unsigned long long)*id);
896 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300897
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300898 scode_buf[0] = 0x00;
899 memcpy(&scode_buf[1], p, 12);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300900
901 /* Enter direct-mode */
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300902 rc = xc_write_reg(priv, XREG_DIRECTSITTING_MODE, 0);
903 if (rc < 0) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300904 printk(KERN_ERR "failed to put device into direct mode!\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300905 return -EIO;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300906 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300907
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300908 rc = xc_send_i2c_data(priv, scode_buf, 13);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300909 if (rc != 0) {
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300910 /* Even if the send failed, make sure we set back to indirect
911 mode */
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300912 printk(KERN_ERR "Failed to set scode %d\n", rc);
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300913 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300914
915 /* Switch back to indirect-mode */
916 memset(indirect_mode, 0, sizeof(indirect_mode));
917 indirect_mode[4] = 0x88;
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -0300918 xc_send_i2c_data(priv, indirect_mode, sizeof(indirect_mode));
919 msleep(10);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300920
921 return 0;
922}
923
924static int check_firmware(struct dvb_frontend *fe, unsigned int type,
925 v4l2_std_id std, __u16 int_freq)
926{
927 struct xc4000_priv *priv = fe->tuner_priv;
928 struct firmware_properties new_fw;
929 int rc = 0, is_retry = 0;
istvan_v@mailbox.hu09f46342011-06-06 13:00:17 -0300930 u16 hwmodel;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300931 v4l2_std_id std0;
Mauro Carvalho Chehab0d0d76e2012-10-29 08:02:31 -0200932 u8 hw_major = 0, hw_minor = 0, fw_major = 0, fw_minor = 0;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300933
934 dprintk(1, "%s called\n", __func__);
935
936 if (!priv->firm) {
937 rc = xc4000_fwupload(fe);
938 if (rc < 0)
939 return rc;
940 }
941
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300942retry:
943 new_fw.type = type;
944 new_fw.id = std;
945 new_fw.std_req = std;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300946 new_fw.scode_table = SCODE;
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300947 new_fw.scode_nr = 0;
948 new_fw.int_freq = int_freq;
949
950 dprintk(1, "checking firmware, user requested type=");
951 if (debug) {
952 dump_firm_type(new_fw.type);
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300953 printk(KERN_CONT "(%x), id %016llx, ", new_fw.type,
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300954 (unsigned long long)new_fw.std_req);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300955 if (!int_freq)
956 printk(KERN_CONT "scode_tbl ");
957 else
958 printk(KERN_CONT "int_freq %d, ", new_fw.int_freq);
959 printk(KERN_CONT "scode_nr %d\n", new_fw.scode_nr);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300960 }
961
962 /* No need to reload base firmware if it matches */
Istvan Varga595a83f2011-06-04 11:59:54 -0300963 if (priv->cur_fw.type & BASE) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300964 dprintk(1, "BASE firmware not changed.\n");
965 goto skip_base;
966 }
967
968 /* Updating BASE - forget about all currently loaded firmware */
969 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
970
971 /* Reset is needed before loading firmware */
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -0300972 rc = xc4000_tuner_reset(fe);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300973 if (rc < 0)
974 goto fail;
975
976 /* BASE firmwares are all std0 */
977 std0 = 0;
Istvan Varga595a83f2011-06-04 11:59:54 -0300978 rc = load_firmware(fe, BASE, &std0);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300979 if (rc < 0) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -0300980 printk(KERN_ERR "Error %d while loading base firmware\n", rc);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300981 goto fail;
982 }
983
984 /* Load INIT1, if needed */
985 dprintk(1, "Load init1 firmware, if exists\n");
986
Istvan Varga595a83f2011-06-04 11:59:54 -0300987 rc = load_firmware(fe, BASE | INIT1, &std0);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300988 if (rc == -ENOENT)
Istvan Varga595a83f2011-06-04 11:59:54 -0300989 rc = load_firmware(fe, BASE | INIT1, &std0);
Devin Heitmuellerd0962382009-07-25 17:39:54 -0300990 if (rc < 0 && rc != -ENOENT) {
991 tuner_err("Error %d while loading init1 firmware\n",
992 rc);
993 goto fail;
994 }
995
996skip_base:
997 /*
998 * No need to reload standard specific firmware if base firmware
999 * was not reloaded and requested video standards have not changed.
1000 */
1001 if (priv->cur_fw.type == (BASE | new_fw.type) &&
1002 priv->cur_fw.std_req == std) {
1003 dprintk(1, "Std-specific firmware already loaded.\n");
1004 goto skip_std_specific;
1005 }
1006
1007 /* Reloading std-specific firmware forces a SCODE update */
1008 priv->cur_fw.scode_table = 0;
1009
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001010 /* Load the standard firmware */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001011 rc = load_firmware(fe, new_fw.type, &new_fw.id);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001012
1013 if (rc < 0)
1014 goto fail;
1015
1016skip_std_specific:
1017 if (priv->cur_fw.scode_table == new_fw.scode_table &&
1018 priv->cur_fw.scode_nr == new_fw.scode_nr) {
1019 dprintk(1, "SCODE firmware already loaded.\n");
1020 goto check_device;
1021 }
1022
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001023 /* Load SCODE firmware, if exists */
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001024 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
1025 new_fw.int_freq, new_fw.scode_nr);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001026 if (rc != 0)
Devin Heitmuelleree4c3cd2009-07-27 23:51:54 -03001027 dprintk(1, "load scode failed %d\n", rc);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001028
1029check_device:
Mauro Carvalho Chehab8a08ef02017-11-01 17:05:47 -04001030 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &hwmodel) < 0) {
1031 printk(KERN_ERR "Unable to read tuner registers.\n");
1032 goto fail;
1033 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001034
Devin Heitmueller799ed112009-10-04 23:09:18 -03001035 if (xc_get_version(priv, &hw_major, &hw_minor, &fw_major,
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001036 &fw_minor) != 0) {
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -03001037 printk(KERN_ERR "Unable to read tuner registers.\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001038 goto fail;
1039 }
1040
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -02001041 dprintk(1, "Device is Xceive %d version %d.%d, firmware version %d.%d\n",
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001042 hwmodel, hw_major, hw_minor, fw_major, fw_minor);
1043
1044 /* Check firmware version against what we downloaded. */
istvan_v@mailbox.hu09f46342011-06-06 13:00:17 -03001045 if (priv->firm_version != ((fw_major << 8) | fw_minor)) {
1046 printk(KERN_WARNING
1047 "Incorrect readback of firmware version %d.%d.\n",
1048 fw_major, fw_minor);
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001049 goto fail;
1050 }
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001051
1052 /* Check that the tuner hardware model remains consistent over time. */
Istvan Varga7db98fe2011-06-04 12:25:19 -03001053 if (priv->hwmodel == 0 &&
1054 (hwmodel == XC_PRODUCT_ID_XC4000 ||
1055 hwmodel == XC_PRODUCT_ID_XC4100)) {
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001056 priv->hwmodel = hwmodel;
istvan_v@mailbox.hu09f46342011-06-06 13:00:17 -03001057 priv->hwvers = (hw_major << 8) | hw_minor;
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001058 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
istvan_v@mailbox.hu09f46342011-06-06 13:00:17 -03001059 priv->hwvers != ((hw_major << 8) | hw_minor)) {
1060 printk(KERN_WARNING
Mauro Carvalho Chehab8ca2e922016-10-18 17:44:26 -02001061 "Read invalid device hardware information - tuner hung?\n");
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001062 goto fail;
1063 }
1064
Ezequiel Garcia36628732012-10-23 15:57:15 -03001065 priv->cur_fw = new_fw;
Devin Heitmuellerd0962382009-07-25 17:39:54 -03001066
1067 /*
1068 * By setting BASE in cur_fw.type only after successfully loading all
1069 * firmwares, we can:
1070 * 1. Identify that BASE firmware with type=0 has been loaded;
1071 * 2. Tell whether BASE firmware was just changed the next time through.
1072 */
1073 priv->cur_fw.type |= BASE;
1074
1075 return 0;
1076
1077fail:
1078 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
1079 if (!is_retry) {
1080 msleep(50);
1081 is_retry = 1;
1082 dprintk(1, "Retrying firmware load\n");
1083 goto retry;
1084 }
1085
1086 if (rc == -ENOENT)
1087 rc = -EINVAL;
1088 return rc;
1089}
Devin Heitmueller11091a32009-07-20 00:54:57 -03001090
Davide Ferri8d009a02009-06-23 22:34:06 -03001091static void xc_debug_dump(struct xc4000_priv *priv)
1092{
Istvan Vargafbe4a292011-06-03 10:11:48 -03001093 u16 adc_envelope;
1094 u32 freq_error_hz = 0;
1095 u16 lock_status;
1096 u32 hsync_freq_hz = 0;
1097 u16 frame_lines;
1098 u16 quality;
Miroslav Slugen8a538a82011-12-20 21:18:38 -03001099 u16 signal = 0;
1100 u16 noise = 0;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001101 u8 hw_majorversion = 0, hw_minorversion = 0;
1102 u8 fw_majorversion = 0, fw_minorversion = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001103
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001104 xc_get_adc_envelope(priv, &adc_envelope);
Davide Ferri8d009a02009-06-23 22:34:06 -03001105 dprintk(1, "*** ADC envelope (0-1023) = %d\n", adc_envelope);
1106
1107 xc_get_frequency_error(priv, &freq_error_hz);
1108 dprintk(1, "*** Frequency error = %d Hz\n", freq_error_hz);
1109
Istvan Vargafbe4a292011-06-03 10:11:48 -03001110 xc_get_lock_status(priv, &lock_status);
Davide Ferri8d009a02009-06-23 22:34:06 -03001111 dprintk(1, "*** Lock status (0-Wait, 1-Locked, 2-No-signal) = %d\n",
1112 lock_status);
1113
Istvan Vargafbe4a292011-06-03 10:11:48 -03001114 xc_get_version(priv, &hw_majorversion, &hw_minorversion,
1115 &fw_majorversion, &fw_minorversion);
Davide Ferri8d009a02009-06-23 22:34:06 -03001116 dprintk(1, "*** HW: V%02x.%02x, FW: V%02x.%02x\n",
1117 hw_majorversion, hw_minorversion,
1118 fw_majorversion, fw_minorversion);
1119
Istvan Vargaf4312e2f2011-06-04 12:08:29 -03001120 if (priv->video_standard < XC4000_DTV6) {
1121 xc_get_hsync_freq(priv, &hsync_freq_hz);
1122 dprintk(1, "*** Horizontal sync frequency = %d Hz\n",
1123 hsync_freq_hz);
Davide Ferri8d009a02009-06-23 22:34:06 -03001124
Istvan Vargaf4312e2f2011-06-04 12:08:29 -03001125 xc_get_frame_lines(priv, &frame_lines);
1126 dprintk(1, "*** Frame lines = %d\n", frame_lines);
1127 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001128
Istvan Vargafbe4a292011-06-03 10:11:48 -03001129 xc_get_quality(priv, &quality);
Davide Ferri8d009a02009-06-23 22:34:06 -03001130 dprintk(1, "*** Quality (0:<8dB, 7:>56dB) = %d\n", quality);
Miroslav Slugen8a538a82011-12-20 21:18:38 -03001131
1132 xc_get_signal_level(priv, &signal);
1133 dprintk(1, "*** Signal level = -%ddB (%d)\n", signal >> 8, signal);
1134
1135 xc_get_noise_level(priv, &noise);
1136 dprintk(1, "*** Noise level = %ddB (%d)\n", noise >> 8, noise);
Davide Ferri8d009a02009-06-23 22:34:06 -03001137}
1138
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -03001139static int xc4000_set_params(struct dvb_frontend *fe)
Davide Ferri8d009a02009-06-23 22:34:06 -03001140{
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001141 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1142 u32 delsys = c->delivery_system;
1143 u32 bw = c->bandwidth_hz;
Davide Ferri8d009a02009-06-23 22:34:06 -03001144 struct xc4000_priv *priv = fe->tuner_priv;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001145 unsigned int type;
Istvan Varga56149422011-06-03 12:23:33 -03001146 int ret = -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -03001147
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001148 dprintk(1, "%s() frequency=%d (Hz)\n", __func__, c->frequency);
Davide Ferri8d009a02009-06-23 22:34:06 -03001149
Istvan Varga56149422011-06-03 12:23:33 -03001150 mutex_lock(&priv->lock);
1151
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001152 switch (delsys) {
1153 case SYS_ATSC:
1154 dprintk(1, "%s() VSB modulation\n", __func__);
1155 priv->rf_mode = XC_RF_MODE_AIR;
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001156 priv->freq_offset = 1750000;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001157 priv->video_standard = XC4000_DTV6;
1158 type = DTV6;
1159 break;
1160 case SYS_DVBC_ANNEX_B:
1161 dprintk(1, "%s() QAM modulation\n", __func__);
1162 priv->rf_mode = XC_RF_MODE_CABLE;
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001163 priv->freq_offset = 1750000;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001164 priv->video_standard = XC4000_DTV6;
1165 type = DTV6;
1166 break;
1167 case SYS_DVBT:
1168 case SYS_DVBT2:
Davide Ferri8d009a02009-06-23 22:34:06 -03001169 dprintk(1, "%s() OFDM\n", __func__);
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001170 if (bw == 0) {
1171 if (c->frequency < 400000000) {
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001172 priv->freq_offset = 2250000;
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001173 } else {
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001174 priv->freq_offset = 2750000;
Istvan Vargaf0ef7c82011-06-03 12:17:59 -03001175 }
1176 priv->video_standard = XC4000_DTV7_8;
1177 type = DTV78;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001178 } else if (bw <= 6000000) {
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001179 priv->video_standard = XC4000_DTV6;
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001180 priv->freq_offset = 1750000;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001181 type = DTV6;
1182 } else if (bw <= 7000000) {
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001183 priv->video_standard = XC4000_DTV7;
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001184 priv->freq_offset = 2250000;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001185 type = DTV7;
1186 } else {
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001187 priv->video_standard = XC4000_DTV8;
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001188 priv->freq_offset = 2750000;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001189 type = DTV8;
Davide Ferri8d009a02009-06-23 22:34:06 -03001190 }
1191 priv->rf_mode = XC_RF_MODE_AIR;
Mauro Carvalho Chehab40d6f2b2011-12-21 09:02:55 -03001192 break;
1193 default:
1194 printk(KERN_ERR "xc4000 delivery system not supported!\n");
Istvan Varga56149422011-06-03 12:23:33 -03001195 ret = -EINVAL;
1196 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001197 }
1198
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001199 priv->freq_hz = c->frequency - priv->freq_offset;
1200
Davide Ferri8d009a02009-06-23 22:34:06 -03001201 dprintk(1, "%s() frequency=%d (compensated)\n",
1202 __func__, priv->freq_hz);
1203
Devin Heitmuellered23db32009-10-05 01:27:14 -03001204 /* Make sure the correct firmware type is loaded */
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001205 if (check_firmware(fe, type, 0, priv->if_khz) != 0)
Istvan Varga56149422011-06-03 12:23:33 -03001206 goto fail;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001207
Mauro Carvalho Chehabc6f56e72011-12-26 20:02:28 -03001208 priv->bandwidth = c->bandwidth_hz;
1209
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001210 ret = xc_set_signal_source(priv, priv->rf_mode);
1211 if (ret != 0) {
1212 printk(KERN_ERR "xc4000: xc_set_signal_source(%d) failed\n",
Istvan Varga56149422011-06-03 12:23:33 -03001213 priv->rf_mode);
1214 goto fail;
Istvan Varga30f544e2011-06-04 12:12:42 -03001215 } else {
1216 u16 video_mode, audio_mode;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001217 video_mode = xc4000_standard[priv->video_standard].video_mode;
1218 audio_mode = xc4000_standard[priv->video_standard].audio_mode;
Istvan Varga30f544e2011-06-04 12:12:42 -03001219 if (type == DTV6 && priv->firm_version != 0x0102)
1220 video_mode |= 0x0001;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001221 ret = xc_set_tv_standard(priv, video_mode, audio_mode);
1222 if (ret != 0) {
1223 printk(KERN_ERR "xc4000: xc_set_tv_standard failed\n");
Istvan Varga30f544e2011-06-04 12:12:42 -03001224 /* DJH - do not return when it fails... */
1225 /* goto fail; */
1226 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001227 }
1228
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001229 if (xc_write_reg(priv, XREG_D_CODE, 0) == 0)
1230 ret = 0;
1231 if (priv->dvb_amplitude != 0) {
Istvan Varga30f544e2011-06-04 12:12:42 -03001232 if (xc_write_reg(priv, XREG_AMPLITUDE,
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001233 (priv->firm_version != 0x0102 ||
1234 priv->dvb_amplitude != 134 ?
1235 priv->dvb_amplitude : 132)) != 0)
Istvan Varga30f544e2011-06-04 12:12:42 -03001236 ret = -EREMOTEIO;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001237 }
1238 if (priv->set_smoothedcvbs != 0) {
Istvan Varga30f544e2011-06-04 12:12:42 -03001239 if (xc_write_reg(priv, XREG_SMOOTHEDCVBS, 1) != 0)
1240 ret = -EREMOTEIO;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001241 }
1242 if (ret != 0) {
1243 printk(KERN_ERR "xc4000: setting registers failed\n");
1244 /* goto fail; */
Davide Ferri8d009a02009-06-23 22:34:06 -03001245 }
Istvan Varga30f544e2011-06-04 12:12:42 -03001246
Istvan Vargae75873c2011-06-04 12:18:41 -03001247 xc_tune_channel(priv, priv->freq_hz);
Davide Ferri8d009a02009-06-23 22:34:06 -03001248
Istvan Varga56149422011-06-03 12:23:33 -03001249 ret = 0;
1250
1251fail:
1252 mutex_unlock(&priv->lock);
1253
1254 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001255}
1256
Davide Ferri8d009a02009-06-23 22:34:06 -03001257static int xc4000_set_analog_params(struct dvb_frontend *fe,
1258 struct analog_parameters *params)
1259{
1260 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Varga818a1772011-06-04 12:17:22 -03001261 unsigned int type = 0;
Istvan Varga56149422011-06-03 12:23:33 -03001262 int ret = -EREMOTEIO;
Davide Ferri8d009a02009-06-23 22:34:06 -03001263
Istvan Varga818a1772011-06-04 12:17:22 -03001264 if (params->mode == V4L2_TUNER_RADIO) {
1265 dprintk(1, "%s() frequency=%d (in units of 62.5Hz)\n",
1266 __func__, params->frequency);
1267
1268 mutex_lock(&priv->lock);
1269
1270 params->std = 0;
1271 priv->freq_hz = params->frequency * 125L / 2;
1272
1273 if (audio_std & XC4000_AUDIO_STD_INPUT1) {
1274 priv->video_standard = XC4000_FM_Radio_INPUT1;
1275 type = FM | INPUT1;
1276 } else {
1277 priv->video_standard = XC4000_FM_Radio_INPUT2;
1278 type = FM | INPUT2;
1279 }
1280
1281 goto tune_channel;
1282 }
1283
Davide Ferri8d009a02009-06-23 22:34:06 -03001284 dprintk(1, "%s() frequency=%d (in units of 62.5khz)\n",
1285 __func__, params->frequency);
1286
Istvan Varga56149422011-06-03 12:23:33 -03001287 mutex_lock(&priv->lock);
1288
Davide Ferri8d009a02009-06-23 22:34:06 -03001289 /* params->frequency is in units of 62.5khz */
1290 priv->freq_hz = params->frequency * 62500;
1291
Istvan Varga818a1772011-06-04 12:17:22 -03001292 params->std &= V4L2_STD_ALL;
1293 /* if std is not defined, choose one */
1294 if (!params->std)
1295 params->std = V4L2_STD_PAL_BG;
1296
1297 if (audio_std & XC4000_AUDIO_STD_MONO)
1298 type = MONO;
1299
Davide Ferri8d009a02009-06-23 22:34:06 -03001300 if (params->std & V4L2_STD_MN) {
Istvan Varga818a1772011-06-04 12:17:22 -03001301 params->std = V4L2_STD_MN;
1302 if (audio_std & XC4000_AUDIO_STD_MONO) {
1303 priv->video_standard = XC4000_MN_NTSC_PAL_Mono;
1304 } else if (audio_std & XC4000_AUDIO_STD_A2) {
1305 params->std |= V4L2_STD_A2;
1306 priv->video_standard = XC4000_MN_NTSC_PAL_A2;
1307 } else {
1308 params->std |= V4L2_STD_BTSC;
1309 priv->video_standard = XC4000_MN_NTSC_PAL_BTSC;
1310 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001311 goto tune_channel;
1312 }
1313
1314 if (params->std & V4L2_STD_PAL_BG) {
Istvan Varga818a1772011-06-04 12:17:22 -03001315 params->std = V4L2_STD_PAL_BG;
1316 if (audio_std & XC4000_AUDIO_STD_MONO) {
1317 priv->video_standard = XC4000_BG_PAL_MONO;
1318 } else if (!(audio_std & XC4000_AUDIO_STD_A2)) {
1319 if (!(audio_std & XC4000_AUDIO_STD_B)) {
1320 params->std |= V4L2_STD_NICAM_A;
1321 priv->video_standard = XC4000_BG_PAL_NICAM;
1322 } else {
1323 params->std |= V4L2_STD_NICAM_B;
1324 priv->video_standard = XC4000_BG_PAL_NICAM;
1325 }
1326 } else {
1327 if (!(audio_std & XC4000_AUDIO_STD_B)) {
1328 params->std |= V4L2_STD_A2_A;
1329 priv->video_standard = XC4000_BG_PAL_A2;
1330 } else {
1331 params->std |= V4L2_STD_A2_B;
1332 priv->video_standard = XC4000_BG_PAL_A2;
1333 }
1334 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001335 goto tune_channel;
1336 }
1337
1338 if (params->std & V4L2_STD_PAL_I) {
1339 /* default to NICAM audio standard */
Istvan Varga818a1772011-06-04 12:17:22 -03001340 params->std = V4L2_STD_PAL_I | V4L2_STD_NICAM;
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -03001341 if (audio_std & XC4000_AUDIO_STD_MONO)
Istvan Varga818a1772011-06-04 12:17:22 -03001342 priv->video_standard = XC4000_I_PAL_NICAM_MONO;
Mauro Carvalho Chehab941830c2011-06-06 16:51:11 -03001343 else
Istvan Varga818a1772011-06-04 12:17:22 -03001344 priv->video_standard = XC4000_I_PAL_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001345 goto tune_channel;
1346 }
1347
1348 if (params->std & V4L2_STD_PAL_DK) {
Istvan Varga818a1772011-06-04 12:17:22 -03001349 params->std = V4L2_STD_PAL_DK;
1350 if (audio_std & XC4000_AUDIO_STD_MONO) {
1351 priv->video_standard = XC4000_DK_PAL_MONO;
1352 } else if (audio_std & XC4000_AUDIO_STD_A2) {
1353 params->std |= V4L2_STD_A2;
1354 priv->video_standard = XC4000_DK_PAL_A2;
1355 } else {
1356 params->std |= V4L2_STD_NICAM;
1357 priv->video_standard = XC4000_DK_PAL_NICAM;
1358 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001359 goto tune_channel;
1360 }
1361
1362 if (params->std & V4L2_STD_SECAM_DK) {
Istvan Varga818a1772011-06-04 12:17:22 -03001363 /* default to A2 audio standard */
1364 params->std = V4L2_STD_SECAM_DK | V4L2_STD_A2;
1365 if (audio_std & XC4000_AUDIO_STD_L) {
1366 type = 0;
1367 priv->video_standard = XC4000_DK_SECAM_NICAM;
1368 } else if (audio_std & XC4000_AUDIO_STD_MONO) {
1369 priv->video_standard = XC4000_DK_SECAM_A2MONO;
1370 } else if (audio_std & XC4000_AUDIO_STD_K3) {
1371 params->std |= V4L2_STD_SECAM_K3;
1372 priv->video_standard = XC4000_DK_SECAM_A2LDK3;
1373 } else {
1374 priv->video_standard = XC4000_DK_SECAM_A2DK1;
1375 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001376 goto tune_channel;
1377 }
1378
1379 if (params->std & V4L2_STD_SECAM_L) {
Istvan Varga818a1772011-06-04 12:17:22 -03001380 /* default to NICAM audio standard */
1381 type = 0;
1382 params->std = V4L2_STD_SECAM_L | V4L2_STD_NICAM;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001383 priv->video_standard = XC4000_L_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001384 goto tune_channel;
1385 }
1386
1387 if (params->std & V4L2_STD_SECAM_LC) {
Istvan Varga818a1772011-06-04 12:17:22 -03001388 /* default to NICAM audio standard */
1389 type = 0;
1390 params->std = V4L2_STD_SECAM_LC | V4L2_STD_NICAM;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001391 priv->video_standard = XC4000_LC_SECAM_NICAM;
Davide Ferri8d009a02009-06-23 22:34:06 -03001392 goto tune_channel;
1393 }
1394
1395tune_channel:
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001396 /* FIXME: it could be air. */
Istvan Varga818a1772011-06-04 12:17:22 -03001397 priv->rf_mode = XC_RF_MODE_CABLE;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001398
Istvan Varga818a1772011-06-04 12:17:22 -03001399 if (check_firmware(fe, type, params->std,
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001400 xc4000_standard[priv->video_standard].int_freq) != 0)
Istvan Varga56149422011-06-03 12:23:33 -03001401 goto fail;
Devin Heitmuellered23db32009-10-05 01:27:14 -03001402
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001403 ret = xc_set_signal_source(priv, priv->rf_mode);
1404 if (ret != 0) {
Davide Ferri8d009a02009-06-23 22:34:06 -03001405 printk(KERN_ERR
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001406 "xc4000: xc_set_signal_source(%d) failed\n",
Istvan Varga56149422011-06-03 12:23:33 -03001407 priv->rf_mode);
1408 goto fail;
Istvan Varga30f544e2011-06-04 12:12:42 -03001409 } else {
1410 u16 video_mode, audio_mode;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001411 video_mode = xc4000_standard[priv->video_standard].video_mode;
1412 audio_mode = xc4000_standard[priv->video_standard].audio_mode;
Istvan Varga30f544e2011-06-04 12:12:42 -03001413 if (priv->video_standard < XC4000_BG_PAL_A2) {
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001414 if (type & NOGD)
Istvan Varga30f544e2011-06-04 12:12:42 -03001415 video_mode &= 0xFF7F;
1416 } else if (priv->video_standard < XC4000_I_PAL_NICAM) {
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001417 if (priv->firm_version == 0x0102)
Istvan Varga30f544e2011-06-04 12:12:42 -03001418 video_mode &= 0xFEFF;
Istvan Varga923137a2011-06-04 12:15:51 -03001419 if (audio_std & XC4000_AUDIO_STD_B)
1420 video_mode |= 0x0080;
Istvan Varga30f544e2011-06-04 12:12:42 -03001421 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001422 ret = xc_set_tv_standard(priv, video_mode, audio_mode);
1423 if (ret != 0) {
1424 printk(KERN_ERR "xc4000: xc_set_tv_standard failed\n");
Istvan Varga30f544e2011-06-04 12:12:42 -03001425 goto fail;
1426 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001427 }
1428
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001429 if (xc_write_reg(priv, XREG_D_CODE, 0) == 0)
1430 ret = 0;
1431 if (xc_write_reg(priv, XREG_AMPLITUDE, 1) != 0)
1432 ret = -EREMOTEIO;
1433 if (priv->set_smoothedcvbs != 0) {
Istvan Varga30f544e2011-06-04 12:12:42 -03001434 if (xc_write_reg(priv, XREG_SMOOTHEDCVBS, 1) != 0)
1435 ret = -EREMOTEIO;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001436 }
1437 if (ret != 0) {
1438 printk(KERN_ERR "xc4000: setting registers failed\n");
1439 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001440 }
1441
Istvan Vargae75873c2011-06-04 12:18:41 -03001442 xc_tune_channel(priv, priv->freq_hz);
Davide Ferri8d009a02009-06-23 22:34:06 -03001443
Istvan Varga56149422011-06-03 12:23:33 -03001444 ret = 0;
1445
1446fail:
1447 mutex_unlock(&priv->lock);
1448
1449 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001450}
1451
Miroslav Slugen8a538a82011-12-20 21:18:38 -03001452static int xc4000_get_signal(struct dvb_frontend *fe, u16 *strength)
1453{
1454 struct xc4000_priv *priv = fe->tuner_priv;
1455 u16 value = 0;
1456 int rc;
1457
1458 mutex_lock(&priv->lock);
1459 rc = xc4000_readreg(priv, XREG_SIGNAL_LEVEL, &value);
1460 mutex_unlock(&priv->lock);
1461
1462 if (rc < 0)
1463 goto ret;
1464
Mauro Carvalho Chehabdda1bb42019-02-18 14:29:02 -05001465 /* Information from real testing of DVB-T and radio part,
1466 coefficient for one dB is 0xff.
Miroslav Slugen8a538a82011-12-20 21:18:38 -03001467 */
1468 tuner_dbg("Signal strength: -%ddB (%05d)\n", value >> 8, value);
1469
1470 /* all known digital modes */
1471 if ((priv->video_standard == XC4000_DTV6) ||
1472 (priv->video_standard == XC4000_DTV7) ||
1473 (priv->video_standard == XC4000_DTV7_8) ||
1474 (priv->video_standard == XC4000_DTV8))
1475 goto digital;
1476
1477 /* Analog mode has NOISE LEVEL important, signal
1478 depends only on gain of antenna and amplifiers,
1479 but it doesn't tell anything about real quality
1480 of reception.
1481 */
1482 mutex_lock(&priv->lock);
1483 rc = xc4000_readreg(priv, XREG_NOISE_LEVEL, &value);
1484 mutex_unlock(&priv->lock);
1485
1486 tuner_dbg("Noise level: %ddB (%05d)\n", value >> 8, value);
1487
1488 /* highest noise level: 32dB */
1489 if (value >= 0x2000) {
1490 value = 0;
1491 } else {
Mauro Carvalho Chehabdfa7ba52016-02-19 12:22:30 -02001492 value = (~value << 3) & 0xffff;
Miroslav Slugen8a538a82011-12-20 21:18:38 -03001493 }
1494
1495 goto ret;
1496
1497 /* Digital mode has SIGNAL LEVEL important and real
1498 noise level is stored in demodulator registers.
1499 */
1500digital:
1501 /* best signal: -50dB */
1502 if (value <= 0x3200) {
1503 value = 0xffff;
1504 /* minimum: -114dB - should be 0x7200 but real zero is 0x713A */
1505 } else if (value >= 0x713A) {
1506 value = 0;
1507 } else {
1508 value = ~(value - 0x3200) << 2;
1509 }
1510
1511ret:
1512 *strength = value;
1513
1514 return rc;
1515}
1516
Davide Ferri8d009a02009-06-23 22:34:06 -03001517static int xc4000_get_frequency(struct dvb_frontend *fe, u32 *freq)
1518{
1519 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Vargaf4312e2f2011-06-04 12:08:29 -03001520
Mauro Carvalho Chehab4c07e322014-07-21 13:28:15 -03001521 *freq = priv->freq_hz + priv->freq_offset;
Istvan Vargaf4312e2f2011-06-04 12:08:29 -03001522
1523 if (debug) {
1524 mutex_lock(&priv->lock);
1525 if ((priv->cur_fw.type
1526 & (BASE | FM | DTV6 | DTV7 | DTV78 | DTV8)) == BASE) {
1527 u16 snr = 0;
1528 if (xc4000_readreg(priv, XREG_SNR, &snr) == 0) {
1529 mutex_unlock(&priv->lock);
1530 dprintk(1, "%s() freq = %u, SNR = %d\n",
1531 __func__, *freq, snr);
1532 return 0;
1533 }
1534 }
1535 mutex_unlock(&priv->lock);
1536 }
1537
1538 dprintk(1, "%s()\n", __func__);
1539
Davide Ferri8d009a02009-06-23 22:34:06 -03001540 return 0;
1541}
1542
1543static int xc4000_get_bandwidth(struct dvb_frontend *fe, u32 *bw)
1544{
1545 struct xc4000_priv *priv = fe->tuner_priv;
1546 dprintk(1, "%s()\n", __func__);
1547
1548 *bw = priv->bandwidth;
1549 return 0;
1550}
1551
1552static int xc4000_get_status(struct dvb_frontend *fe, u32 *status)
1553{
1554 struct xc4000_priv *priv = fe->tuner_priv;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001555 u16 lock_status = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001556
Istvan Varga56149422011-06-03 12:23:33 -03001557 mutex_lock(&priv->lock);
1558
Istvan Vargaf4312e2f2011-06-04 12:08:29 -03001559 if (priv->cur_fw.type & BASE)
1560 xc_get_lock_status(priv, &lock_status);
1561
1562 *status = (lock_status == 1 ?
1563 TUNER_STATUS_LOCKED | TUNER_STATUS_STEREO : 0);
1564 if (priv->cur_fw.type & (DTV6 | DTV7 | DTV78 | DTV8))
1565 *status &= (~TUNER_STATUS_STEREO);
Davide Ferri8d009a02009-06-23 22:34:06 -03001566
Istvan Varga56149422011-06-03 12:23:33 -03001567 mutex_unlock(&priv->lock);
1568
Istvan Vargaf4312e2f2011-06-04 12:08:29 -03001569 dprintk(2, "%s() lock_status = %d\n", __func__, lock_status);
Davide Ferri8d009a02009-06-23 22:34:06 -03001570
1571 return 0;
1572}
1573
Davide Ferri8d009a02009-06-23 22:34:06 -03001574static int xc4000_sleep(struct dvb_frontend *fe)
1575{
Istvan Varga5272f6b2011-06-04 12:03:03 -03001576 struct xc4000_priv *priv = fe->tuner_priv;
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001577 int ret = 0;
Istvan Varga5272f6b2011-06-04 12:03:03 -03001578
1579 dprintk(1, "%s()\n", __func__);
1580
1581 mutex_lock(&priv->lock);
1582
1583 /* Avoid firmware reload on slow devices */
1584 if ((no_poweroff == 2 ||
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001585 (no_poweroff == 0 && priv->default_pm != 0)) &&
Istvan Varga5272f6b2011-06-04 12:03:03 -03001586 (priv->cur_fw.type & BASE) != 0) {
1587 /* force reset and firmware reload */
1588 priv->cur_fw.type = XC_POWERED_DOWN;
1589
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001590 if (xc_write_reg(priv, XREG_POWER_DOWN, 0) != 0) {
Istvan Varga5272f6b2011-06-04 12:03:03 -03001591 printk(KERN_ERR
1592 "xc4000: %s() unable to shutdown tuner\n",
1593 __func__);
1594 ret = -EREMOTEIO;
1595 }
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001596 msleep(20);
Istvan Varga5272f6b2011-06-04 12:03:03 -03001597 }
1598
1599 mutex_unlock(&priv->lock);
1600
1601 return ret;
Davide Ferri8d009a02009-06-23 22:34:06 -03001602}
1603
1604static int xc4000_init(struct dvb_frontend *fe)
1605{
Davide Ferri8d009a02009-06-23 22:34:06 -03001606 dprintk(1, "%s()\n", __func__);
1607
Davide Ferri8d009a02009-06-23 22:34:06 -03001608 return 0;
1609}
1610
Max Kellermann194ced72016-08-09 18:32:31 -03001611static void xc4000_release(struct dvb_frontend *fe)
Davide Ferri8d009a02009-06-23 22:34:06 -03001612{
1613 struct xc4000_priv *priv = fe->tuner_priv;
1614
1615 dprintk(1, "%s()\n", __func__);
1616
1617 mutex_lock(&xc4000_list_mutex);
1618
1619 if (priv)
1620 hybrid_tuner_release_state(priv);
1621
1622 mutex_unlock(&xc4000_list_mutex);
1623
1624 fe->tuner_priv = NULL;
Davide Ferri8d009a02009-06-23 22:34:06 -03001625}
1626
1627static const struct dvb_tuner_ops xc4000_tuner_ops = {
1628 .info = {
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -04001629 .name = "Xceive XC4000",
1630 .frequency_min_hz = 1 * MHz,
1631 .frequency_max_hz = 1023 * MHz,
1632 .frequency_step_hz = 50 * kHz,
Davide Ferri8d009a02009-06-23 22:34:06 -03001633 },
1634
1635 .release = xc4000_release,
1636 .init = xc4000_init,
1637 .sleep = xc4000_sleep,
1638
1639 .set_params = xc4000_set_params,
1640 .set_analog_params = xc4000_set_analog_params,
1641 .get_frequency = xc4000_get_frequency,
Miroslav Slugen8a538a82011-12-20 21:18:38 -03001642 .get_rf_strength = xc4000_get_signal,
Davide Ferri8d009a02009-06-23 22:34:06 -03001643 .get_bandwidth = xc4000_get_bandwidth,
1644 .get_status = xc4000_get_status
1645};
1646
1647struct dvb_frontend *xc4000_attach(struct dvb_frontend *fe,
1648 struct i2c_adapter *i2c,
1649 struct xc4000_config *cfg)
1650{
1651 struct xc4000_priv *priv = NULL;
Istvan Vargafbe4a292011-06-03 10:11:48 -03001652 int instance;
1653 u16 id = 0;
Davide Ferri8d009a02009-06-23 22:34:06 -03001654
1655 dprintk(1, "%s(%d-%04x)\n", __func__,
1656 i2c ? i2c_adapter_id(i2c) : -1,
1657 cfg ? cfg->i2c_address : -1);
1658
1659 mutex_lock(&xc4000_list_mutex);
1660
1661 instance = hybrid_tuner_request_state(struct xc4000_priv, priv,
1662 hybrid_tuner_instance_list,
1663 i2c, cfg->i2c_address, "xc4000");
1664 switch (instance) {
1665 case 0:
1666 goto fail;
Davide Ferri8d009a02009-06-23 22:34:06 -03001667 case 1:
1668 /* new tuner instance */
Mauro Carvalho Chehabc6f56e72011-12-26 20:02:28 -03001669 priv->bandwidth = 6000000;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001670 /* set default configuration */
1671 priv->if_khz = 4560;
1672 priv->default_pm = 0;
1673 priv->dvb_amplitude = 134;
1674 priv->set_smoothedcvbs = 1;
Istvan Varga56149422011-06-03 12:23:33 -03001675 mutex_init(&priv->lock);
Davide Ferri8d009a02009-06-23 22:34:06 -03001676 fe->tuner_priv = priv;
1677 break;
1678 default:
1679 /* existing tuner instance */
1680 fe->tuner_priv = priv;
1681 break;
1682 }
1683
Istvan Varga0b402132011-06-03 09:38:04 -03001684 if (cfg->if_khz != 0) {
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001685 /* copy configuration if provided by the caller */
Davide Ferri8d009a02009-06-23 22:34:06 -03001686 priv->if_khz = cfg->if_khz;
istvan_v@mailbox.hu8edeb6e2011-06-06 13:03:44 -03001687 priv->default_pm = cfg->default_pm;
1688 priv->dvb_amplitude = cfg->dvb_amplitude;
1689 priv->set_smoothedcvbs = cfg->set_smoothedcvbs;
Davide Ferri8d009a02009-06-23 22:34:06 -03001690 }
1691
1692 /* Check if firmware has been loaded. It is possible that another
1693 instance of the driver has loaded the firmware.
1694 */
1695
Istvan Varga027fd362011-06-04 12:04:51 -03001696 if (instance == 1) {
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001697 if (xc4000_readreg(priv, XREG_PRODUCT_ID, &id) != 0)
Davide Ferri8d009a02009-06-23 22:34:06 -03001698 goto fail;
Istvan Varga027fd362011-06-04 12:04:51 -03001699 } else {
1700 id = ((priv->cur_fw.type & BASE) != 0 ?
1701 priv->hwmodel : XC_PRODUCT_ID_FW_NOT_LOADED);
1702 }
Davide Ferri8d009a02009-06-23 22:34:06 -03001703
1704 switch (id) {
Istvan Varga7db98fe2011-06-04 12:25:19 -03001705 case XC_PRODUCT_ID_XC4000:
1706 case XC_PRODUCT_ID_XC4100:
Davide Ferri8d009a02009-06-23 22:34:06 -03001707 printk(KERN_INFO
1708 "xc4000: Successfully identified at address 0x%02x\n",
1709 cfg->i2c_address);
1710 printk(KERN_INFO
1711 "xc4000: Firmware has been loaded previously\n");
1712 break;
1713 case XC_PRODUCT_ID_FW_NOT_LOADED:
1714 printk(KERN_INFO
1715 "xc4000: Successfully identified at address 0x%02x\n",
1716 cfg->i2c_address);
1717 printk(KERN_INFO
1718 "xc4000: Firmware has not been loaded previously\n");
1719 break;
1720 default:
1721 printk(KERN_ERR
1722 "xc4000: Device not found at addr 0x%02x (0x%x)\n",
1723 cfg->i2c_address, id);
1724 goto fail;
1725 }
1726
1727 mutex_unlock(&xc4000_list_mutex);
1728
1729 memcpy(&fe->ops.tuner_ops, &xc4000_tuner_ops,
1730 sizeof(struct dvb_tuner_ops));
1731
Istvan Varga027fd362011-06-04 12:04:51 -03001732 if (instance == 1) {
1733 int ret;
1734 mutex_lock(&priv->lock);
1735 ret = xc4000_fwupload(fe);
1736 mutex_unlock(&priv->lock);
istvan_v@mailbox.hu341747b2011-06-06 12:54:54 -03001737 if (ret != 0)
Istvan Varga027fd362011-06-04 12:04:51 -03001738 goto fail2;
1739 }
Devin Heitmueller11091a32009-07-20 00:54:57 -03001740
Davide Ferri8d009a02009-06-23 22:34:06 -03001741 return fe;
1742fail:
1743 mutex_unlock(&xc4000_list_mutex);
Istvan Varga027fd362011-06-04 12:04:51 -03001744fail2:
Davide Ferri8d009a02009-06-23 22:34:06 -03001745 xc4000_release(fe);
1746 return NULL;
1747}
1748EXPORT_SYMBOL(xc4000_attach);
1749
1750MODULE_AUTHOR("Steven Toth, Davide Ferri");
1751MODULE_DESCRIPTION("Xceive xc4000 silicon tuner driver");
1752MODULE_LICENSE("GPL");
Mauro Carvalho Chehab612ae142014-07-21 13:28:14 -03001753MODULE_FIRMWARE(XC4000_DEFAULT_FIRMWARE_NEW);
1754MODULE_FIRMWARE(XC4000_DEFAULT_FIRMWARE);