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