r820t.c revision 50786ddfa8efef696ec4f72460874f3b46dc2401
1/* 2 * Rafael Micro R820T driver 3 * 4 * Copyright (C) 2013 Mauro Carvalho Chehab <mchehab@redhat.com> 5 * 6 * This driver was written from scratch, based on an existing driver 7 * that it is part of rtl-sdr git tree, released under GPLv2: 8 * https://groups.google.com/forum/#!topic/ultra-cheap-sdr/Y3rBEOFtHug 9 * https://github.com/n1gp/gr-baz 10 * 11 * From what I understood from the threads, the original driver was converted 12 * to userspace from a Realtek tree. I couldn't find the original tree. 13 * However, the original driver look awkward on my eyes. So, I decided to 14 * write a new version from it from the scratch, while trying to reproduce 15 * everything found there. 16 * 17 * TODO: 18 * After locking, the original driver seems to have some routines to 19 * improve reception. This was not implemented here yet. 20 * 21 * RF Gain set/get is not implemented. 22 * 23 * This program is free software; you can redistribute it and/or modify 24 * it under the terms of the GNU General Public License as published by 25 * the Free Software Foundation; either version 2 of the License, or 26 * (at your option) any later version. 27 * 28 * This program is distributed in the hope that it will be useful, 29 * but WITHOUT ANY WARRANTY; without even the implied warranty of 30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 * GNU General Public License for more details. 32 * 33 */ 34 35#include <linux/videodev2.h> 36#include <linux/mutex.h> 37#include <linux/slab.h> 38#include "tuner-i2c.h" 39#include <asm/div64.h> 40#include "r820t.h" 41 42/* 43 * FIXME: I think that there are only 32 registers, but better safe than 44 * sorry. After finishing the driver, we may review it. 45 */ 46#define REG_SHADOW_START 5 47#define NUM_REGS 27 48 49#define VER_NUM 49 50 51static int debug; 52module_param(debug, int, 0644); 53MODULE_PARM_DESC(debug, "enable verbose debug messages"); 54 55/* 56 * enums and structures 57 */ 58 59enum xtal_cap_value { 60 XTAL_LOW_CAP_30P = 0, 61 XTAL_LOW_CAP_20P, 62 XTAL_LOW_CAP_10P, 63 XTAL_LOW_CAP_0P, 64 XTAL_HIGH_CAP_0P 65}; 66 67struct r820t_priv { 68 struct list_head hybrid_tuner_instance_list; 69 const struct r820t_config *cfg; 70 struct tuner_i2c_props i2c_props; 71 struct mutex lock; 72 73 u8 regs[NUM_REGS]; 74 u8 buf[NUM_REGS + 1]; 75 enum xtal_cap_value xtal_cap_sel; 76 u16 pll; /* kHz */ 77 u32 int_freq; 78 u8 fil_cal_code; 79 bool imr_done; 80 81 /* Store current mode */ 82 u32 delsys; 83 enum v4l2_tuner_type type; 84 v4l2_std_id std; 85 u32 bw; /* in MHz */ 86 87 bool has_lock; 88}; 89 90struct r820t_freq_range { 91 u32 freq; 92 u8 open_d; 93 u8 rf_mux_ploy; 94 u8 tf_c; 95 u8 xtal_cap20p; 96 u8 xtal_cap10p; 97 u8 xtal_cap0p; 98 u8 imr_mem; /* Not used, currently */ 99}; 100 101#define VCO_POWER_REF 0x02 102 103/* 104 * Static constants 105 */ 106 107static LIST_HEAD(hybrid_tuner_instance_list); 108static DEFINE_MUTEX(r820t_list_mutex); 109 110/* Those initial values start from REG_SHADOW_START */ 111static const u8 r820t_init_array[NUM_REGS] = { 112 0x83, 0x32, 0x75, /* 05 to 07 */ 113 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */ 114 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */ 115 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */ 116 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */ 117 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */ 118 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */ 119}; 120 121/* Tuner frequency ranges */ 122static const struct r820t_freq_range freq_ranges[] = { 123 { 124 .freq = 0, 125 .open_d = 0x08, /* low */ 126 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 127 .tf_c = 0xdf, /* R27[7:0] band2,band0 */ 128 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 129 .xtal_cap10p = 0x01, 130 .xtal_cap0p = 0x00, 131 .imr_mem = 0, 132 }, { 133 .freq = 50, /* Start freq, in MHz */ 134 .open_d = 0x08, /* low */ 135 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 136 .tf_c = 0xbe, /* R27[7:0] band4,band1 */ 137 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 138 .xtal_cap10p = 0x01, 139 .xtal_cap0p = 0x00, 140 .imr_mem = 0, 141 }, { 142 .freq = 55, /* Start freq, in MHz */ 143 .open_d = 0x08, /* low */ 144 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 145 .tf_c = 0x8b, /* R27[7:0] band7,band4 */ 146 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 147 .xtal_cap10p = 0x01, 148 .xtal_cap0p = 0x00, 149 .imr_mem = 0, 150 }, { 151 .freq = 60, /* Start freq, in MHz */ 152 .open_d = 0x08, /* low */ 153 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 154 .tf_c = 0x7b, /* R27[7:0] band8,band4 */ 155 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 156 .xtal_cap10p = 0x01, 157 .xtal_cap0p = 0x00, 158 .imr_mem = 0, 159 }, { 160 .freq = 65, /* Start freq, in MHz */ 161 .open_d = 0x08, /* low */ 162 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 163 .tf_c = 0x69, /* R27[7:0] band9,band6 */ 164 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 165 .xtal_cap10p = 0x01, 166 .xtal_cap0p = 0x00, 167 .imr_mem = 0, 168 }, { 169 .freq = 70, /* Start freq, in MHz */ 170 .open_d = 0x08, /* low */ 171 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 172 .tf_c = 0x58, /* R27[7:0] band10,band7 */ 173 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 174 .xtal_cap10p = 0x01, 175 .xtal_cap0p = 0x00, 176 .imr_mem = 0, 177 }, { 178 .freq = 75, /* Start freq, in MHz */ 179 .open_d = 0x00, /* high */ 180 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 181 .tf_c = 0x44, /* R27[7:0] band11,band11 */ 182 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 183 .xtal_cap10p = 0x01, 184 .xtal_cap0p = 0x00, 185 .imr_mem = 0, 186 }, { 187 .freq = 80, /* Start freq, in MHz */ 188 .open_d = 0x00, /* high */ 189 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 190 .tf_c = 0x44, /* R27[7:0] band11,band11 */ 191 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */ 192 .xtal_cap10p = 0x01, 193 .xtal_cap0p = 0x00, 194 .imr_mem = 0, 195 }, { 196 .freq = 90, /* Start freq, in MHz */ 197 .open_d = 0x00, /* high */ 198 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 199 .tf_c = 0x34, /* R27[7:0] band12,band11 */ 200 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */ 201 .xtal_cap10p = 0x01, 202 .xtal_cap0p = 0x00, 203 .imr_mem = 0, 204 }, { 205 .freq = 100, /* Start freq, in MHz */ 206 .open_d = 0x00, /* high */ 207 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 208 .tf_c = 0x34, /* R27[7:0] band12,band11 */ 209 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */ 210 .xtal_cap10p = 0x01, 211 .xtal_cap0p = 0x00, 212 .imr_mem = 0, 213 }, { 214 .freq = 110, /* Start freq, in MHz */ 215 .open_d = 0x00, /* high */ 216 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 217 .tf_c = 0x24, /* R27[7:0] band13,band11 */ 218 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */ 219 .xtal_cap10p = 0x01, 220 .xtal_cap0p = 0x00, 221 .imr_mem = 1, 222 }, { 223 .freq = 120, /* Start freq, in MHz */ 224 .open_d = 0x00, /* high */ 225 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 226 .tf_c = 0x24, /* R27[7:0] band13,band11 */ 227 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */ 228 .xtal_cap10p = 0x01, 229 .xtal_cap0p = 0x00, 230 .imr_mem = 1, 231 }, { 232 .freq = 140, /* Start freq, in MHz */ 233 .open_d = 0x00, /* high */ 234 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 235 .tf_c = 0x14, /* R27[7:0] band14,band11 */ 236 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */ 237 .xtal_cap10p = 0x01, 238 .xtal_cap0p = 0x00, 239 .imr_mem = 1, 240 }, { 241 .freq = 180, /* Start freq, in MHz */ 242 .open_d = 0x00, /* high */ 243 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 244 .tf_c = 0x13, /* R27[7:0] band14,band12 */ 245 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 246 .xtal_cap10p = 0x00, 247 .xtal_cap0p = 0x00, 248 .imr_mem = 1, 249 }, { 250 .freq = 220, /* Start freq, in MHz */ 251 .open_d = 0x00, /* high */ 252 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 253 .tf_c = 0x13, /* R27[7:0] band14,band12 */ 254 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 255 .xtal_cap10p = 0x00, 256 .xtal_cap0p = 0x00, 257 .imr_mem = 2, 258 }, { 259 .freq = 250, /* Start freq, in MHz */ 260 .open_d = 0x00, /* high */ 261 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 262 .tf_c = 0x11, /* R27[7:0] highest,highest */ 263 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 264 .xtal_cap10p = 0x00, 265 .xtal_cap0p = 0x00, 266 .imr_mem = 2, 267 }, { 268 .freq = 280, /* Start freq, in MHz */ 269 .open_d = 0x00, /* high */ 270 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */ 271 .tf_c = 0x00, /* R27[7:0] highest,highest */ 272 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 273 .xtal_cap10p = 0x00, 274 .xtal_cap0p = 0x00, 275 .imr_mem = 2, 276 }, { 277 .freq = 310, /* Start freq, in MHz */ 278 .open_d = 0x00, /* high */ 279 .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */ 280 .tf_c = 0x00, /* R27[7:0] highest,highest */ 281 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 282 .xtal_cap10p = 0x00, 283 .xtal_cap0p = 0x00, 284 .imr_mem = 2, 285 }, { 286 .freq = 450, /* Start freq, in MHz */ 287 .open_d = 0x00, /* high */ 288 .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */ 289 .tf_c = 0x00, /* R27[7:0] highest,highest */ 290 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 291 .xtal_cap10p = 0x00, 292 .xtal_cap0p = 0x00, 293 .imr_mem = 3, 294 }, { 295 .freq = 588, /* Start freq, in MHz */ 296 .open_d = 0x00, /* high */ 297 .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */ 298 .tf_c = 0x00, /* R27[7:0] highest,highest */ 299 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 300 .xtal_cap10p = 0x00, 301 .xtal_cap0p = 0x00, 302 .imr_mem = 3, 303 }, { 304 .freq = 650, /* Start freq, in MHz */ 305 .open_d = 0x00, /* high */ 306 .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */ 307 .tf_c = 0x00, /* R27[7:0] highest,highest */ 308 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */ 309 .xtal_cap10p = 0x00, 310 .xtal_cap0p = 0x00, 311 .imr_mem = 4, 312 } 313}; 314 315static int r820t_xtal_capacitor[][2] = { 316 { 0x0b, XTAL_LOW_CAP_30P }, 317 { 0x02, XTAL_LOW_CAP_20P }, 318 { 0x01, XTAL_LOW_CAP_10P }, 319 { 0x00, XTAL_LOW_CAP_0P }, 320 { 0x10, XTAL_HIGH_CAP_0P }, 321}; 322 323/* 324 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm 325 * input power, for raw results see: 326 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/ 327 */ 328 329static const int r820t_lna_gain_steps[] = { 330 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13 331}; 332 333static const int r820t_mixer_gain_steps[] = { 334 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8 335}; 336 337/* 338 * I2C read/write code and shadow registers logic 339 */ 340static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val, 341 int len) 342{ 343 int r = reg - REG_SHADOW_START; 344 345 if (r < 0) { 346 len += r; 347 r = 0; 348 } 349 if (len <= 0) 350 return; 351 if (len > NUM_REGS) 352 len = NUM_REGS; 353 354 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n", 355 __func__, r + REG_SHADOW_START, len, len, val); 356 357 memcpy(&priv->regs[r], val, len); 358} 359 360static int r820t_write(struct r820t_priv *priv, u8 reg, const u8 *val, 361 int len) 362{ 363 int rc, size, pos = 0; 364 365 /* Store the shadow registers */ 366 shadow_store(priv, reg, val, len); 367 368 do { 369 if (len > priv->cfg->max_i2c_msg_len - 1) 370 size = priv->cfg->max_i2c_msg_len - 1; 371 else 372 size = len; 373 374 /* Fill I2C buffer */ 375 priv->buf[0] = reg; 376 memcpy(&priv->buf[1], &val[pos], size); 377 378 rc = tuner_i2c_xfer_send(&priv->i2c_props, priv->buf, size + 1); 379 if (rc != size + 1) { 380 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n", 381 __func__, rc, reg, size, size, &priv->buf[1]); 382 if (rc < 0) 383 return rc; 384 return -EREMOTEIO; 385 } 386 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n", 387 __func__, reg, size, size, &priv->buf[1]); 388 389 reg += size; 390 len -= size; 391 pos += size; 392 } while (len > 0); 393 394 return 0; 395} 396 397static int r820t_write_reg(struct r820t_priv *priv, u8 reg, u8 val) 398{ 399 return r820t_write(priv, reg, &val, 1); 400} 401 402static int r820t_write_reg_mask(struct r820t_priv *priv, u8 reg, u8 val, 403 u8 bit_mask) 404{ 405 int r = reg - REG_SHADOW_START; 406 407 if (r >= 0 && r < NUM_REGS) 408 val = (priv->regs[r] & ~bit_mask) | (val & bit_mask); 409 else 410 return -EINVAL; 411 412 return r820t_write(priv, reg, &val, 1); 413} 414 415static int r820_read(struct r820t_priv *priv, u8 reg, u8 *val, int len) 416{ 417 int rc; 418 u8 *p = &priv->buf[1]; 419 420 priv->buf[0] = reg; 421 422 rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, priv->buf, 1, p, len); 423 if (rc != len) { 424 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n", 425 __func__, rc, reg, len, len, p); 426 if (rc < 0) 427 return rc; 428 return -EREMOTEIO; 429 } 430 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n", 431 __func__, reg, len, len, p); 432 433 /* Copy data to the output buffer */ 434 memcpy(val, p, len); 435 436 return 0; 437} 438 439/* 440 * r820t tuning logic 441 */ 442 443static int r820t_set_mux(struct r820t_priv *priv, u32 freq) 444{ 445 const struct r820t_freq_range *range; 446 int i, rc; 447 u8 val; 448 449 /* Get the proper frequency range */ 450 freq = freq / 1000000; 451 for (i = 0; i < ARRAY_SIZE(freq_ranges) - 1; i++) { 452 if (freq < freq_ranges[i + 1].freq) 453 break; 454 } 455 range = &freq_ranges[i]; 456 457 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i, freq); 458 459 /* Open Drain */ 460 rc = r820t_write_reg_mask(priv, 0x17, range->open_d, 0x08); 461 if (rc < 0) 462 return rc; 463 464 /* RF_MUX,Polymux */ 465 rc = r820t_write_reg_mask(priv, 0x1a, range->rf_mux_ploy, 0xc3); 466 if (rc < 0) 467 return rc; 468 469 /* TF BAND */ 470 rc = r820t_write_reg(priv, 0x1b, range->tf_c); 471 if (rc < 0) 472 return rc; 473 474 /* XTAL CAP & Drive */ 475 switch (priv->xtal_cap_sel) { 476 case XTAL_LOW_CAP_30P: 477 case XTAL_LOW_CAP_20P: 478 val = range->xtal_cap20p | 0x08; 479 break; 480 case XTAL_LOW_CAP_10P: 481 val = range->xtal_cap10p | 0x08; 482 break; 483 case XTAL_HIGH_CAP_0P: 484 val = range->xtal_cap0p | 0x00; 485 break; 486 default: 487 case XTAL_LOW_CAP_0P: 488 val = range->xtal_cap0p | 0x08; 489 break; 490 } 491 rc = r820t_write_reg_mask(priv, 0x10, val, 0x0b); 492 if (rc < 0) 493 return rc; 494 495 /* 496 * FIXME: the original driver has a logic there with preserves 497 * gain/phase from registers 8 and 9 reading the data from the 498 * registers before writing, if "IMF done". That code was sort of 499 * commented there, as the flag is always false. 500 */ 501 rc = r820t_write_reg_mask(priv, 0x08, 0, 0x3f); 502 if (rc < 0) 503 return rc; 504 505 rc = r820t_write_reg_mask(priv, 0x09, 0, 0x3f); 506 507 return rc; 508} 509 510static int r820t_set_pll(struct r820t_priv *priv, u32 freq) 511{ 512 u64 tmp64, vco_freq; 513 int rc, i; 514 u32 vco_fra; /* VCO contribution by SDM (kHz) */ 515 u32 vco_min = 1770000; 516 u32 vco_max = vco_min * 2; 517 u32 pll_ref; 518 u16 n_sdm = 2; 519 u16 sdm = 0; 520 u8 mix_div = 2; 521 u8 div_buf = 0; 522 u8 div_num = 0; 523 u8 ni, si, nint, vco_fine_tune, val; 524 u8 data[5]; 525 526 freq = freq / 1000; /* Frequency in kHz */ 527 528 pll_ref = priv->cfg->xtal / 1000; 529 530 tuner_dbg("set r820t pll for frequency %d kHz = %d\n", freq, pll_ref); 531 532 /* FIXME: this seems to be a hack - probably it can be removed */ 533 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x00); 534 if (rc < 0) 535 return rc; 536 537 /* set pll autotune = 128kHz */ 538 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c); 539 if (rc < 0) 540 return rc; 541 542 /* set VCO current = 100 */ 543 rc = r820t_write_reg_mask(priv, 0x12, 0x80, 0xe0); 544 if (rc < 0) 545 return rc; 546 547 /* Calculate divider */ 548 while (mix_div <= 64) { 549 if (((freq * mix_div) >= vco_min) && 550 ((freq * mix_div) < vco_max)) { 551 div_buf = mix_div; 552 while (div_buf > 2) { 553 div_buf = div_buf >> 1; 554 div_num++; 555 } 556 break; 557 } 558 mix_div = mix_div << 1; 559 } 560 561 rc = r820_read(priv, 0x00, data, sizeof(data)); 562 if (rc < 0) 563 return rc; 564 565 vco_fine_tune = (data[4] & 0x30) >> 4; 566 567 if (vco_fine_tune > VCO_POWER_REF) 568 div_num = div_num - 1; 569 else if (vco_fine_tune < VCO_POWER_REF) 570 div_num = div_num + 1; 571 572 rc = r820t_write_reg_mask(priv, 0x10, div_num << 5, 0xe0); 573 if (rc < 0) 574 return rc; 575 576 vco_freq = (u64)(freq * (u64)mix_div); 577 578 tmp64 = vco_freq; 579 do_div(tmp64, 2 * pll_ref); 580 nint = (u8)tmp64; 581 582 tmp64 = vco_freq - ((u64)2) * pll_ref * nint; 583 do_div(tmp64, 1000); 584 vco_fra = (u16)(tmp64); 585 586 pll_ref /= 1000; 587 588 /* boundary spur prevention */ 589 if (vco_fra < pll_ref / 64) { 590 vco_fra = 0; 591 } else if (vco_fra > pll_ref * 127 / 64) { 592 vco_fra = 0; 593 nint++; 594 } else if ((vco_fra > pll_ref * 127 / 128) && (vco_fra < pll_ref)) { 595 vco_fra = pll_ref * 127 / 128; 596 } else if ((vco_fra > pll_ref) && (vco_fra < pll_ref * 129 / 128)) { 597 vco_fra = pll_ref * 129 / 128; 598 } 599 600 if (nint > 63) { 601 tuner_info("No valid PLL values for %u kHz!\n", freq); 602 return -EINVAL; 603 } 604 605 ni = (nint - 13) / 4; 606 si = nint - 4 * ni - 13; 607 608 rc = r820t_write_reg(priv, 0x14, ni + (si << 6)); 609 if (rc < 0) 610 return rc; 611 612 /* pw_sdm */ 613 if (!vco_fra) 614 val = 0x08; 615 else 616 val = 0x00; 617 618 rc = r820t_write_reg_mask(priv, 0x12, val, 0x08); 619 if (rc < 0) 620 return rc; 621 622 /* sdm calculator */ 623 while (vco_fra > 1) { 624 if (vco_fra > (2 * pll_ref / n_sdm)) { 625 sdm = sdm + 32768 / (n_sdm / 2); 626 vco_fra = vco_fra - 2 * pll_ref / n_sdm; 627 if (n_sdm >= 0x8000) 628 break; 629 } 630 n_sdm = n_sdm << 1; 631 } 632 633 rc = r820t_write_reg_mask(priv, 0x16, sdm >> 8, 0x08); 634 if (rc < 0) 635 return rc; 636 rc = r820t_write_reg_mask(priv, 0x15, sdm & 0xff, 0x08); 637 if (rc < 0) 638 return rc; 639 640 for (i = 0; i < 2; i++) { 641 /* 642 * FIXME: Rafael chips R620D, R828D and R828 seems to 643 * need 20 ms for analog TV 644 */ 645 msleep(10); 646 647 /* Check if PLL has locked */ 648 rc = r820_read(priv, 0x00, data, 3); 649 if (rc < 0) 650 return rc; 651 if (data[2] & 0x40) 652 break; 653 654 if (!i) { 655 /* Didn't lock. Increase VCO current */ 656 rc = r820t_write_reg_mask(priv, 0x12, 0x60, 0xe0); 657 if (rc < 0) 658 return rc; 659 } 660 } 661 662 if (!(data[2] & 0x40)) { 663 priv->has_lock = false; 664 return 0; 665 } 666 667 priv->has_lock = true; 668 tuner_dbg("tuner has lock at frequency %d kHz\n", freq); 669 670 /* set pll autotune = 8kHz */ 671 rc = r820t_write_reg_mask(priv, 0x1a, 0x08, 0x08); 672 673 return rc; 674} 675 676static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq, 677 enum v4l2_tuner_type type, 678 v4l2_std_id std, 679 u32 delsys) 680{ 681 int rc; 682 u8 mixer_top, lna_top, cp_cur, div_buf_cur, lna_vth_l, mixer_vth_l; 683 u8 air_cable1_in, cable2_in, pre_dect, lna_discharge, filter_cur; 684 685 tuner_dbg("adjusting tuner parameters for the standard\n"); 686 687 switch (delsys) { 688 case SYS_DVBT: 689 if ((freq == 506000000) || (freq == 666000000) || 690 (freq == 818000000)) { 691 mixer_top = 0x14; /* mixer top:14 , top-1, low-discharge */ 692 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */ 693 cp_cur = 0x28; /* 101, 0.2 */ 694 div_buf_cur = 0x20; /* 10, 200u */ 695 } else { 696 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */ 697 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */ 698 cp_cur = 0x38; /* 111, auto */ 699 div_buf_cur = 0x30; /* 11, 150u */ 700 } 701 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */ 702 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */ 703 air_cable1_in = 0x00; 704 cable2_in = 0x00; 705 pre_dect = 0x40; 706 lna_discharge = 14; 707 filter_cur = 0x40; /* 10, low */ 708 break; 709 case SYS_DVBT2: 710 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */ 711 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */ 712 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */ 713 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */ 714 air_cable1_in = 0x00; 715 cable2_in = 0x00; 716 pre_dect = 0x40; 717 lna_discharge = 14; 718 cp_cur = 0x38; /* 111, auto */ 719 div_buf_cur = 0x30; /* 11, 150u */ 720 filter_cur = 0x40; /* 10, low */ 721 break; 722 case SYS_ISDBT: 723 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */ 724 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */ 725 lna_vth_l = 0x75; /* lna vth 1.04 , vtl 0.84 */ 726 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */ 727 air_cable1_in = 0x00; 728 cable2_in = 0x00; 729 pre_dect = 0x40; 730 lna_discharge = 14; 731 cp_cur = 0x38; /* 111, auto */ 732 div_buf_cur = 0x30; /* 11, 150u */ 733 filter_cur = 0x40; /* 10, low */ 734 break; 735 default: /* DVB-T 8M */ 736 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */ 737 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */ 738 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */ 739 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */ 740 air_cable1_in = 0x00; 741 cable2_in = 0x00; 742 pre_dect = 0x40; 743 lna_discharge = 14; 744 cp_cur = 0x38; /* 111, auto */ 745 div_buf_cur = 0x30; /* 11, 150u */ 746 filter_cur = 0x40; /* 10, low */ 747 break; 748 } 749 750 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7); 751 if (rc < 0) 752 return rc; 753 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0xf8); 754 if (rc < 0) 755 return rc; 756 rc = r820t_write_reg(priv, 0x0d, lna_vth_l); 757 if (rc < 0) 758 return rc; 759 rc = r820t_write_reg(priv, 0x0e, mixer_vth_l); 760 if (rc < 0) 761 return rc; 762 763 /* Air-IN only for Astrometa */ 764 rc = r820t_write_reg_mask(priv, 0x05, air_cable1_in, 0x60); 765 if (rc < 0) 766 return rc; 767 rc = r820t_write_reg_mask(priv, 0x06, cable2_in, 0x08); 768 if (rc < 0) 769 return rc; 770 771 rc = r820t_write_reg_mask(priv, 0x11, cp_cur, 0x38); 772 if (rc < 0) 773 return rc; 774 rc = r820t_write_reg_mask(priv, 0x17, div_buf_cur, 0x30); 775 if (rc < 0) 776 return rc; 777 rc = r820t_write_reg_mask(priv, 0x0a, filter_cur, 0x60); 778 if (rc < 0) 779 return rc; 780 /* 781 * Original driver initializes regs 0x05 and 0x06 with the 782 * same value again on this point. Probably, it is just an 783 * error there 784 */ 785 786 /* 787 * Set LNA 788 */ 789 790 tuner_dbg("adjusting LNA parameters\n"); 791 if (type != V4L2_TUNER_ANALOG_TV) { 792 /* LNA TOP: lowest */ 793 rc = r820t_write_reg_mask(priv, 0x1d, 0, 0x38); 794 if (rc < 0) 795 return rc; 796 797 /* 0: normal mode */ 798 rc = r820t_write_reg_mask(priv, 0x1c, 0, 0x04); 799 if (rc < 0) 800 return rc; 801 802 /* 0: PRE_DECT off */ 803 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40); 804 if (rc < 0) 805 return rc; 806 807 /* agc clk 250hz */ 808 rc = r820t_write_reg_mask(priv, 0x1a, 0x30, 0x30); 809 if (rc < 0) 810 return rc; 811 812 msleep(250); 813 814 /* write LNA TOP = 3 */ 815 rc = r820t_write_reg_mask(priv, 0x1d, 0x18, 0x38); 816 if (rc < 0) 817 return rc; 818 819 /* 820 * write discharge mode 821 * FIXME: IMHO, the mask here is wrong, but it matches 822 * what's there at the original driver 823 */ 824 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04); 825 if (rc < 0) 826 return rc; 827 828 /* LNA discharge current */ 829 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f); 830 if (rc < 0) 831 return rc; 832 833 /* agc clk 60hz */ 834 rc = r820t_write_reg_mask(priv, 0x1a, 0x20, 0x30); 835 if (rc < 0) 836 return rc; 837 } else { 838 /* PRE_DECT off */ 839 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40); 840 if (rc < 0) 841 return rc; 842 843 /* write LNA TOP */ 844 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0x38); 845 if (rc < 0) 846 return rc; 847 848 /* 849 * write discharge mode 850 * FIXME: IMHO, the mask here is wrong, but it matches 851 * what's there at the original driver 852 */ 853 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04); 854 if (rc < 0) 855 return rc; 856 857 /* LNA discharge current */ 858 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f); 859 if (rc < 0) 860 return rc; 861 862 /* agc clk 1Khz, external det1 cap 1u */ 863 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x30); 864 if (rc < 0) 865 return rc; 866 867 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x04); 868 if (rc < 0) 869 return rc; 870 } 871 return 0; 872} 873 874static int r820t_set_tv_standard(struct r820t_priv *priv, 875 unsigned bw, 876 enum v4l2_tuner_type type, 877 v4l2_std_id std, u32 delsys) 878 879{ 880 int rc, i; 881 u32 if_khz, filt_cal_lo; 882 u8 data[5], val; 883 u8 filt_gain, img_r, filt_q, hp_cor, ext_enable, loop_through; 884 u8 lt_att, flt_ext_widest, polyfil_cur; 885 bool need_calibration; 886 887 tuner_dbg("selecting the delivery system\n"); 888 889 if (delsys == SYS_ISDBT) { 890 if_khz = 4063; 891 filt_cal_lo = 59000; 892 filt_gain = 0x10; /* +3db, 6mhz on */ 893 img_r = 0x00; /* image negative */ 894 filt_q = 0x10; /* r10[4]:low q(1'b1) */ 895 hp_cor = 0x6a; /* 1.7m disable, +2cap, 1.25mhz */ 896 ext_enable = 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */ 897 loop_through = 0x00; /* r5[7], lt on */ 898 lt_att = 0x00; /* r31[7], lt att enable */ 899 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */ 900 polyfil_cur = 0x60; /* r25[6:5]:min */ 901 } else { 902 if (bw <= 6) { 903 if_khz = 3570; 904 filt_cal_lo = 56000; /* 52000->56000 */ 905 filt_gain = 0x10; /* +3db, 6mhz on */ 906 img_r = 0x00; /* image negative */ 907 filt_q = 0x10; /* r10[4]:low q(1'b1) */ 908 hp_cor = 0x6b; /* 1.7m disable, +2cap, 1.0mhz */ 909 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */ 910 loop_through = 0x00; /* r5[7], lt on */ 911 lt_att = 0x00; /* r31[7], lt att enable */ 912 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */ 913 polyfil_cur = 0x60; /* r25[6:5]:min */ 914 } else if (bw == 7) { 915 if_khz = 4070; 916 filt_cal_lo = 60000; 917 filt_gain = 0x10; /* +3db, 6mhz on */ 918 img_r = 0x00; /* image negative */ 919 filt_q = 0x10; /* r10[4]:low q(1'b1) */ 920 hp_cor = 0x2b; /* 1.7m disable, +1cap, 1.0mhz */ 921 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */ 922 loop_through = 0x00; /* r5[7], lt on */ 923 lt_att = 0x00; /* r31[7], lt att enable */ 924 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */ 925 polyfil_cur = 0x60; /* r25[6:5]:min */ 926#if 0 /* 7 MHz type 2 - nor sure why/where this is used - Perhaps Australia? */ 927 if_khz = 4570; 928 filt_cal_lo = 63000; 929 filt_gain = 0x10; /* +3db, 6mhz on */ 930 img_r = 0x00; /* image negative */ 931 filt_q = 0x10; /* r10[4]:low q(1'b1) */ 932 hp_cor = 0x2a; /* 1.7m disable, +1cap, 1.25mhz */ 933 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */ 934 loop_through = 0x00; /* r5[7], lt on */ 935 lt_att = 0x00; /* r31[7], lt att enable */ 936 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */ 937 polyfil_cur = 0x60; /* r25[6:5]:min */ 938#endif 939 } else { 940 if_khz = 4570; 941 filt_cal_lo = 68500; 942 filt_gain = 0x10; /* +3db, 6mhz on */ 943 img_r = 0x00; /* image negative */ 944 filt_q = 0x10; /* r10[4]:low q(1'b1) */ 945 hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */ 946 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */ 947 loop_through = 0x00; /* r5[7], lt on */ 948 lt_att = 0x00; /* r31[7], lt att enable */ 949 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */ 950 polyfil_cur = 0x60; /* r25[6:5]:min */ 951 } 952 } 953 954 /* Initialize the shadow registers */ 955 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array)); 956 957 /* Init Flag & Xtal_check Result */ 958 if (priv->imr_done) 959 val = 1 | priv->xtal_cap_sel << 1; 960 else 961 val = 0; 962 rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f); 963 if (rc < 0) 964 return rc; 965 966 /* version */ 967 rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f); 968 if (rc < 0) 969 return rc; 970 971 /* for LT Gain test */ 972 if (type != V4L2_TUNER_ANALOG_TV) { 973 rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38); 974 if (rc < 0) 975 return rc; 976 msleep(1); 977 } 978 priv->int_freq = if_khz; 979 980 /* Check if standard changed. If so, filter calibration is needed */ 981 if (type != priv->type) 982 need_calibration = true; 983 else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std)) 984 need_calibration = true; 985 else if ((type == V4L2_TUNER_DIGITAL_TV) && 986 ((delsys != priv->delsys) || bw != priv->bw)) 987 need_calibration = true; 988 else 989 need_calibration = false; 990 991 if (need_calibration) { 992 tuner_dbg("calibrating the tuner\n"); 993 for (i = 0; i < 2; i++) { 994 /* Set filt_cap */ 995 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60); 996 if (rc < 0) 997 return rc; 998 999 /* set cali clk =on */ 1000 rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04); 1001 if (rc < 0) 1002 return rc; 1003 1004 /* X'tal cap 0pF for PLL */ 1005 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03); 1006 if (rc < 0) 1007 return rc; 1008 1009 rc = r820t_set_pll(priv, filt_cal_lo); 1010 if (rc < 0 || !priv->has_lock) 1011 return rc; 1012 1013 /* Start Trigger */ 1014 rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10); 1015 if (rc < 0) 1016 return rc; 1017 1018 msleep(1); 1019 1020 /* Stop Trigger */ 1021 rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10); 1022 if (rc < 0) 1023 return rc; 1024 1025 /* set cali clk =off */ 1026 rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04); 1027 if (rc < 0) 1028 return rc; 1029 1030 /* Check if calibration worked */ 1031 rc = r820_read(priv, 0x00, data, sizeof(data)); 1032 if (rc < 0) 1033 return rc; 1034 1035 priv->fil_cal_code = data[4] & 0x0f; 1036 if (priv->fil_cal_code && priv->fil_cal_code != 0x0f) 1037 break; 1038 } 1039 /* narrowest */ 1040 if (priv->fil_cal_code == 0x0f) 1041 priv->fil_cal_code = 0; 1042 } 1043 1044 rc = r820t_write_reg_mask(priv, 0x0a, 1045 filt_q | priv->fil_cal_code, 0x1f); 1046 if (rc < 0) 1047 return rc; 1048 1049 /* Set BW, Filter_gain, & HP corner */ 1050 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x10); 1051 if (rc < 0) 1052 return rc; 1053 1054 1055 /* Set Img_R */ 1056 rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80); 1057 if (rc < 0) 1058 return rc; 1059 1060 /* Set filt_3dB, V6MHz */ 1061 rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30); 1062 if (rc < 0) 1063 return rc; 1064 1065 /* channel filter extension */ 1066 rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60); 1067 if (rc < 0) 1068 return rc; 1069 1070 /* Loop through */ 1071 rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80); 1072 if (rc < 0) 1073 return rc; 1074 1075 /* Loop through attenuation */ 1076 rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80); 1077 if (rc < 0) 1078 return rc; 1079 1080 /* filter extension widest */ 1081 rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80); 1082 if (rc < 0) 1083 return rc; 1084 1085 /* RF poly filter current */ 1086 rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60); 1087 if (rc < 0) 1088 return rc; 1089 1090 /* Store current standard. If it changes, re-calibrate the tuner */ 1091 priv->delsys = delsys; 1092 priv->type = type; 1093 priv->std = std; 1094 priv->bw = bw; 1095 1096 return 0; 1097} 1098 1099static int r820t_read_gain(struct r820t_priv *priv) 1100{ 1101 u8 data[4]; 1102 int rc; 1103 1104 rc = r820_read(priv, 0x00, data, sizeof(data)); 1105 if (rc < 0) 1106 return rc; 1107 1108 return ((data[3] & 0x0f) << 1) + ((data[3] & 0xf0) >> 4); 1109} 1110 1111static int r820t_set_gain_mode(struct r820t_priv *priv, 1112 bool set_manual_gain, 1113 int gain) 1114{ 1115 int rc; 1116 1117 if (set_manual_gain) { 1118 int i, total_gain = 0; 1119 uint8_t mix_index = 0, lna_index = 0; 1120 u8 data[4]; 1121 1122 /* LNA auto off */ 1123 rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10); 1124 if (rc < 0) 1125 return rc; 1126 1127 /* Mixer auto off */ 1128 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10); 1129 if (rc < 0) 1130 return rc; 1131 1132 rc = r820_read(priv, 0x00, data, sizeof(data)); 1133 if (rc < 0) 1134 return rc; 1135 1136 /* set fixed VGA gain for now (16.3 dB) */ 1137 rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f); 1138 if (rc < 0) 1139 return rc; 1140 1141 for (i = 0; i < 15; i++) { 1142 if (total_gain >= gain) 1143 break; 1144 1145 total_gain += r820t_lna_gain_steps[++lna_index]; 1146 1147 if (total_gain >= gain) 1148 break; 1149 1150 total_gain += r820t_mixer_gain_steps[++mix_index]; 1151 } 1152 1153 /* set LNA gain */ 1154 rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f); 1155 if (rc < 0) 1156 return rc; 1157 1158 /* set Mixer gain */ 1159 rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f); 1160 if (rc < 0) 1161 return rc; 1162 } else { 1163 /* LNA */ 1164 rc = r820t_write_reg_mask(priv, 0x05, 0, 0xef); 1165 if (rc < 0) 1166 return rc; 1167 1168 /* Mixer */ 1169 rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0xef); 1170 if (rc < 0) 1171 return rc; 1172 1173 /* set fixed VGA gain for now (26.5 dB) */ 1174 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f); 1175 if (rc < 0) 1176 return rc; 1177 } 1178 1179 return 0; 1180} 1181 1182 1183static int generic_set_freq(struct dvb_frontend *fe, 1184 u32 freq /* in HZ */, 1185 unsigned bw, 1186 enum v4l2_tuner_type type, 1187 v4l2_std_id std, u32 delsys) 1188{ 1189 struct r820t_priv *priv = fe->tuner_priv; 1190 int rc = -EINVAL; 1191 u32 lo_freq; 1192 1193 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n", 1194 freq / 1000, bw); 1195 1196 mutex_lock(&priv->lock); 1197 1198 if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC)) 1199 lo_freq = freq - priv->int_freq; 1200 else 1201 lo_freq = freq + priv->int_freq; 1202 1203 rc = r820t_set_tv_standard(priv, bw, type, std, delsys); 1204 if (rc < 0) 1205 goto err; 1206 1207 rc = r820t_set_mux(priv, lo_freq); 1208 if (rc < 0) 1209 goto err; 1210 1211 rc = r820t_set_gain_mode(priv, true, 0); 1212 if (rc < 0) 1213 goto err; 1214 1215 rc = r820t_set_pll(priv, lo_freq); 1216 if (rc < 0 || !priv->has_lock) 1217 goto err; 1218 1219 rc = r820t_sysfreq_sel(priv, freq, type, std, delsys); 1220err: 1221 mutex_unlock(&priv->lock); 1222 1223 if (rc < 0) 1224 tuner_dbg("%s: failed=%d\n", __func__, rc); 1225 return rc; 1226} 1227 1228/* 1229 * r820t standby logic 1230 */ 1231 1232static int r820t_standby(struct r820t_priv *priv) 1233{ 1234 int rc; 1235 1236 rc = r820t_write_reg(priv, 0x06, 0xb1); 1237 if (rc < 0) 1238 return rc; 1239 rc = r820t_write_reg(priv, 0x05, 0x03); 1240 if (rc < 0) 1241 return rc; 1242 rc = r820t_write_reg(priv, 0x07, 0x3a); 1243 if (rc < 0) 1244 return rc; 1245 rc = r820t_write_reg(priv, 0x08, 0x40); 1246 if (rc < 0) 1247 return rc; 1248 rc = r820t_write_reg(priv, 0x09, 0xc0); 1249 if (rc < 0) 1250 return rc; 1251 rc = r820t_write_reg(priv, 0x0a, 0x36); 1252 if (rc < 0) 1253 return rc; 1254 rc = r820t_write_reg(priv, 0x0c, 0x35); 1255 if (rc < 0) 1256 return rc; 1257 rc = r820t_write_reg(priv, 0x0f, 0x68); 1258 if (rc < 0) 1259 return rc; 1260 rc = r820t_write_reg(priv, 0x11, 0x03); 1261 if (rc < 0) 1262 return rc; 1263 rc = r820t_write_reg(priv, 0x17, 0xf4); 1264 if (rc < 0) 1265 return rc; 1266 rc = r820t_write_reg(priv, 0x19, 0x0c); 1267 1268 /* Force initial calibration */ 1269 priv->type = -1; 1270 1271 return rc; 1272} 1273 1274/* 1275 * r820t device init logic 1276 */ 1277 1278static int r820t_xtal_check(struct r820t_priv *priv) 1279{ 1280 int rc, i; 1281 u8 data[3], val; 1282 1283 /* Initialize the shadow registers */ 1284 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array)); 1285 1286 /* cap 30pF & Drive Low */ 1287 rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b); 1288 if (rc < 0) 1289 return rc; 1290 1291 /* set pll autotune = 128kHz */ 1292 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c); 1293 if (rc < 0) 1294 return rc; 1295 1296 /* set manual initial reg = 111111; */ 1297 rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f); 1298 if (rc < 0) 1299 return rc; 1300 1301 /* set auto */ 1302 rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40); 1303 if (rc < 0) 1304 return rc; 1305 1306 /* Try several xtal capacitor alternatives */ 1307 for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) { 1308 rc = r820t_write_reg_mask(priv, 0x10, 1309 r820t_xtal_capacitor[i][0], 0x1b); 1310 if (rc < 0) 1311 return rc; 1312 1313 msleep(5); 1314 1315 rc = r820_read(priv, 0x00, data, sizeof(data)); 1316 if (rc < 0) 1317 return rc; 1318 if ((!data[2]) & 0x40) 1319 continue; 1320 1321 val = data[2] & 0x3f; 1322 1323 if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23)) 1324 break; 1325 1326 if (val != 0x3f) 1327 break; 1328 } 1329 1330 if (i == ARRAY_SIZE(r820t_xtal_capacitor)) 1331 return -EINVAL; 1332 1333 return r820t_xtal_capacitor[i][1]; 1334} 1335 1336/* 1337 * r820t frontend operations and tuner attach code 1338 */ 1339 1340static int r820t_init(struct dvb_frontend *fe) 1341{ 1342 struct r820t_priv *priv = fe->tuner_priv; 1343 int rc, i; 1344 int xtal_cap = 0; 1345 1346 tuner_dbg("%s:\n", __func__); 1347 1348 if (fe->ops.i2c_gate_ctrl) 1349 fe->ops.i2c_gate_ctrl(fe, 1); 1350 1351 mutex_lock(&priv->lock); 1352 1353 if ((priv->cfg->rafael_chip == CHIP_R820T) || 1354 (priv->cfg->rafael_chip == CHIP_R828S) || 1355 (priv->cfg->rafael_chip == CHIP_R820C)) { 1356 priv->xtal_cap_sel = XTAL_HIGH_CAP_0P; 1357 } else { 1358 for (i = 0; i < 3; i++) { 1359 rc = r820t_xtal_check(priv); 1360 if (rc < 0) 1361 goto err; 1362 if (!i || rc > xtal_cap) 1363 xtal_cap = rc; 1364 } 1365 priv->xtal_cap_sel = xtal_cap; 1366 } 1367 1368 /* Initialize registers */ 1369 rc = r820t_write(priv, 0x05, 1370 r820t_init_array, sizeof(r820t_init_array)); 1371 1372 mutex_unlock(&priv->lock); 1373 1374 if (fe->ops.i2c_gate_ctrl) 1375 fe->ops.i2c_gate_ctrl(fe, 0); 1376 1377 return rc; 1378err: 1379 if (fe->ops.i2c_gate_ctrl) 1380 fe->ops.i2c_gate_ctrl(fe, 0); 1381 1382 tuner_dbg("%s: failed=%d\n", __func__, rc); 1383 return rc; 1384} 1385 1386static int r820t_sleep(struct dvb_frontend *fe) 1387{ 1388 struct r820t_priv *priv = fe->tuner_priv; 1389 int rc; 1390 1391 tuner_dbg("%s:\n", __func__); 1392 1393 if (fe->ops.i2c_gate_ctrl) 1394 fe->ops.i2c_gate_ctrl(fe, 1); 1395 1396 mutex_lock(&priv->lock); 1397 rc = r820t_standby(priv); 1398 mutex_unlock(&priv->lock); 1399 1400 if (fe->ops.i2c_gate_ctrl) 1401 fe->ops.i2c_gate_ctrl(fe, 0); 1402 1403 tuner_dbg("%s: failed=%d\n", __func__, rc); 1404 return rc; 1405} 1406 1407static int r820t_set_analog_freq(struct dvb_frontend *fe, 1408 struct analog_parameters *p) 1409{ 1410 struct r820t_priv *priv = fe->tuner_priv; 1411 unsigned bw; 1412 1413 tuner_dbg("%s called\n", __func__); 1414 1415 /* if std is not defined, choose one */ 1416 if (!p->std) 1417 p->std = V4L2_STD_MN; 1418 1419 if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC)) 1420 bw = 6; 1421 else 1422 bw = 8; 1423 1424 return generic_set_freq(fe, 62500l * p->frequency, bw, 1425 V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED); 1426} 1427 1428static int r820t_set_params(struct dvb_frontend *fe) 1429{ 1430 struct r820t_priv *priv = fe->tuner_priv; 1431 struct dtv_frontend_properties *c = &fe->dtv_property_cache; 1432 int rc; 1433 unsigned bw; 1434 1435 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n", 1436 __func__, c->delivery_system, c->frequency, c->bandwidth_hz); 1437 1438 if (fe->ops.i2c_gate_ctrl) 1439 fe->ops.i2c_gate_ctrl(fe, 1); 1440 1441 bw = (c->bandwidth_hz + 500000) / 1000000; 1442 if (!bw) 1443 bw = 8; 1444 1445 rc = generic_set_freq(fe, c->frequency, bw, 1446 V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system); 1447 1448 if (fe->ops.i2c_gate_ctrl) 1449 fe->ops.i2c_gate_ctrl(fe, 0); 1450 1451 if (rc) 1452 tuner_dbg("%s: failed=%d\n", __func__, rc); 1453 return rc; 1454} 1455 1456static int r820t_signal(struct dvb_frontend *fe, u16 *strength) 1457{ 1458 struct r820t_priv *priv = fe->tuner_priv; 1459 int rc = 0; 1460 1461 if (priv->has_lock) { 1462 rc = r820t_read_gain(priv); 1463 if (rc < 0) 1464 return rc; 1465 1466 /* A higher gain at LNA means a lower signal strength */ 1467 *strength = (45 - rc) << 4 | 0xff; 1468 } else { 1469 *strength = 0; 1470 } 1471 1472 tuner_dbg("%s: %s, gain=%d strength=%d\n", 1473 __func__, 1474 priv->has_lock ? "PLL locked" : "no signal", 1475 rc, *strength); 1476 1477 return 0; 1478} 1479 1480static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency) 1481{ 1482 struct r820t_priv *priv = fe->tuner_priv; 1483 1484 tuner_dbg("%s:\n", __func__); 1485 1486 *frequency = priv->int_freq; 1487 1488 return 0; 1489} 1490 1491static int r820t_release(struct dvb_frontend *fe) 1492{ 1493 struct r820t_priv *priv = fe->tuner_priv; 1494 1495 tuner_dbg("%s:\n", __func__); 1496 1497 mutex_lock(&r820t_list_mutex); 1498 1499 if (priv) 1500 hybrid_tuner_release_state(priv); 1501 1502 mutex_unlock(&r820t_list_mutex); 1503 1504 fe->tuner_priv = NULL; 1505 1506 kfree(fe->tuner_priv); 1507 1508 return 0; 1509} 1510 1511static const struct dvb_tuner_ops r820t_tuner_ops = { 1512 .info = { 1513 .name = "Rafael Micro R820T", 1514 .frequency_min = 42000000, 1515 .frequency_max = 1002000000, 1516 }, 1517 .init = r820t_init, 1518 .release = r820t_release, 1519 .sleep = r820t_sleep, 1520 .set_params = r820t_set_params, 1521 .set_analog_params = r820t_set_analog_freq, 1522 .get_if_frequency = r820t_get_if_frequency, 1523 .get_rf_strength = r820t_signal, 1524}; 1525 1526struct dvb_frontend *r820t_attach(struct dvb_frontend *fe, 1527 struct i2c_adapter *i2c, 1528 const struct r820t_config *cfg) 1529{ 1530 struct r820t_priv *priv; 1531 int rc = -ENODEV; 1532 u8 data[5]; 1533 int instance; 1534 1535 mutex_lock(&r820t_list_mutex); 1536 1537 instance = hybrid_tuner_request_state(struct r820t_priv, priv, 1538 hybrid_tuner_instance_list, 1539 i2c, cfg->i2c_addr, 1540 "r820t"); 1541 switch (instance) { 1542 case 0: 1543 /* memory allocation failure */ 1544 goto err_no_gate; 1545 break; 1546 case 1: 1547 /* new tuner instance */ 1548 priv->cfg = cfg; 1549 1550 mutex_init(&priv->lock); 1551 1552 fe->tuner_priv = priv; 1553 break; 1554 case 2: 1555 /* existing tuner instance */ 1556 fe->tuner_priv = priv; 1557 break; 1558 } 1559 1560 memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops, sizeof(r820t_tuner_ops)); 1561 1562 if (fe->ops.i2c_gate_ctrl) 1563 fe->ops.i2c_gate_ctrl(fe, 1); 1564 1565 /* check if the tuner is there */ 1566 rc = r820_read(priv, 0x00, data, sizeof(data)); 1567 if (rc < 0) 1568 goto err; 1569 1570 rc = r820t_sleep(fe); 1571 if (rc < 0) 1572 goto err; 1573 1574 tuner_info("Rafael Micro r820t successfully identified\n"); 1575 1576 fe->tuner_priv = priv; 1577 memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops, 1578 sizeof(struct dvb_tuner_ops)); 1579 1580 if (fe->ops.i2c_gate_ctrl) 1581 fe->ops.i2c_gate_ctrl(fe, 0); 1582 1583 mutex_unlock(&r820t_list_mutex); 1584 1585 return fe; 1586err: 1587 if (fe->ops.i2c_gate_ctrl) 1588 fe->ops.i2c_gate_ctrl(fe, 0); 1589 1590err_no_gate: 1591 mutex_unlock(&r820t_list_mutex); 1592 1593 tuner_info("%s: failed=%d\n", __func__, rc); 1594 r820t_release(fe); 1595 return NULL; 1596} 1597EXPORT_SYMBOL_GPL(r820t_attach); 1598 1599MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver"); 1600MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); 1601MODULE_LICENSE("GPL"); 1602