2 * Rafael Micro R820T driver
4 * Copyright (C) 2013 Mauro Carvalho Chehab
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
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.
18 * After locking, the original driver seems to have some routines to
19 * improve reception. This was not implemented here yet.
21 * RF Gain set/get is not implemented.
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.
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.
35 #include <linux/videodev2.h>
36 #include <linux/mutex.h>
37 #include <linux/slab.h>
38 #include <linux/bitrev.h>
40 #include "tuner-i2c.h"
44 * FIXME: I think that there are only 32 registers, but better safe than
45 * sorry. After finishing the driver, we may review it.
47 #define REG_SHADOW_START 5
55 module_param(debug, int, 0644);
56 MODULE_PARM_DESC(debug, "enable verbose debug messages");
58 static int no_imr_cal;
59 module_param(no_imr_cal, int, 0444);
60 MODULE_PARM_DESC(no_imr_cal, "Disable IMR calibration at module init");
64 * enums and structures
75 struct r820t_sect_type {
82 struct list_head hybrid_tuner_instance_list;
83 const struct r820t_config *cfg;
84 struct tuner_i2c_props i2c_props;
89 enum xtal_cap_value xtal_cap_sel;
96 struct r820t_sect_type imr_data[NUM_IMR];
98 /* Store current mode */
100 enum v4l2_tuner_type type;
105 struct r820t_freq_range {
113 u8 imr_mem; /* Not used, currently */
116 #define VCO_POWER_REF 0x02
117 #define DIP_FREQ 32000000
123 static LIST_HEAD(hybrid_tuner_instance_list);
124 static DEFINE_MUTEX(r820t_list_mutex);
126 /* Those initial values start from REG_SHADOW_START */
127 static const u8 r820t_init_array[NUM_REGS] = {
128 0x83, 0x32, 0x75, /* 05 to 07 */
129 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
130 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
131 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
132 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
133 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
134 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
137 /* Tuner frequency ranges */
138 static const struct r820t_freq_range freq_ranges[] = {
141 .open_d = 0x08, /* low */
142 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
143 .tf_c = 0xdf, /* R27[7:0] band2,band0 */
144 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
149 .freq = 50, /* Start freq, in MHz */
150 .open_d = 0x08, /* low */
151 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
152 .tf_c = 0xbe, /* R27[7:0] band4,band1 */
153 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
158 .freq = 55, /* Start freq, in MHz */
159 .open_d = 0x08, /* low */
160 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
161 .tf_c = 0x8b, /* R27[7:0] band7,band4 */
162 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
167 .freq = 60, /* Start freq, in MHz */
168 .open_d = 0x08, /* low */
169 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
170 .tf_c = 0x7b, /* R27[7:0] band8,band4 */
171 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
176 .freq = 65, /* Start freq, in MHz */
177 .open_d = 0x08, /* low */
178 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
179 .tf_c = 0x69, /* R27[7:0] band9,band6 */
180 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
185 .freq = 70, /* Start freq, in MHz */
186 .open_d = 0x08, /* low */
187 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
188 .tf_c = 0x58, /* R27[7:0] band10,band7 */
189 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
194 .freq = 75, /* Start freq, in MHz */
195 .open_d = 0x00, /* high */
196 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
197 .tf_c = 0x44, /* R27[7:0] band11,band11 */
198 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
203 .freq = 80, /* Start freq, in MHz */
204 .open_d = 0x00, /* high */
205 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
206 .tf_c = 0x44, /* R27[7:0] band11,band11 */
207 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
212 .freq = 90, /* Start freq, in MHz */
213 .open_d = 0x00, /* high */
214 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
215 .tf_c = 0x34, /* R27[7:0] band12,band11 */
216 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
221 .freq = 100, /* Start freq, in MHz */
222 .open_d = 0x00, /* high */
223 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
224 .tf_c = 0x34, /* R27[7:0] band12,band11 */
225 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
230 .freq = 110, /* Start freq, in MHz */
231 .open_d = 0x00, /* high */
232 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
233 .tf_c = 0x24, /* R27[7:0] band13,band11 */
234 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
239 .freq = 120, /* Start freq, in MHz */
240 .open_d = 0x00, /* high */
241 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
242 .tf_c = 0x24, /* R27[7:0] band13,band11 */
243 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
248 .freq = 140, /* Start freq, in MHz */
249 .open_d = 0x00, /* high */
250 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
251 .tf_c = 0x14, /* R27[7:0] band14,band11 */
252 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
257 .freq = 180, /* Start freq, in MHz */
258 .open_d = 0x00, /* high */
259 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
260 .tf_c = 0x13, /* R27[7:0] band14,band12 */
261 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
266 .freq = 220, /* Start freq, in MHz */
267 .open_d = 0x00, /* high */
268 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
269 .tf_c = 0x13, /* R27[7:0] band14,band12 */
270 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
275 .freq = 250, /* Start freq, in MHz */
276 .open_d = 0x00, /* high */
277 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
278 .tf_c = 0x11, /* R27[7:0] highest,highest */
279 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
284 .freq = 280, /* Start freq, in MHz */
285 .open_d = 0x00, /* high */
286 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
287 .tf_c = 0x00, /* R27[7:0] highest,highest */
288 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
293 .freq = 310, /* Start freq, in MHz */
294 .open_d = 0x00, /* high */
295 .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
296 .tf_c = 0x00, /* R27[7:0] highest,highest */
297 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
302 .freq = 450, /* Start freq, in MHz */
303 .open_d = 0x00, /* high */
304 .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
305 .tf_c = 0x00, /* R27[7:0] highest,highest */
306 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
311 .freq = 588, /* Start freq, in MHz */
312 .open_d = 0x00, /* high */
313 .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
314 .tf_c = 0x00, /* R27[7:0] highest,highest */
315 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
320 .freq = 650, /* Start freq, in MHz */
321 .open_d = 0x00, /* high */
322 .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
323 .tf_c = 0x00, /* R27[7:0] highest,highest */
324 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
331 static int r820t_xtal_capacitor[][2] = {
332 { 0x0b, XTAL_LOW_CAP_30P },
333 { 0x02, XTAL_LOW_CAP_20P },
334 { 0x01, XTAL_LOW_CAP_10P },
335 { 0x00, XTAL_LOW_CAP_0P },
336 { 0x10, XTAL_HIGH_CAP_0P },
340 * I2C read/write code and shadow registers logic
342 static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
345 int r = reg - REG_SHADOW_START;
353 if (len > NUM_REGS - r)
356 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
357 __func__, r + REG_SHADOW_START, len, len, val);
359 memcpy(&priv->regs[r], val, len);
362 static int r820t_write(struct r820t_priv *priv, u8 reg, const u8 *val,
365 int rc, size, pos = 0;
367 /* Store the shadow registers */
368 shadow_store(priv, reg, val, len);
371 if (len > priv->cfg->max_i2c_msg_len - 1)
372 size = priv->cfg->max_i2c_msg_len - 1;
376 /* Fill I2C buffer */
378 memcpy(&priv->buf[1], &val[pos], size);
380 rc = tuner_i2c_xfer_send(&priv->i2c_props, priv->buf, size + 1);
381 if (rc != size + 1) {
382 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
383 __func__, rc, reg, size, size, &priv->buf[1]);
388 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
389 __func__, reg, size, size, &priv->buf[1]);
399 static int r820t_write_reg(struct r820t_priv *priv, u8 reg, u8 val)
401 return r820t_write(priv, reg, &val, 1);
404 static int r820t_read_cache_reg(struct r820t_priv *priv, int reg)
406 reg -= REG_SHADOW_START;
408 if (reg >= 0 && reg < NUM_REGS)
409 return priv->regs[reg];
414 static int r820t_write_reg_mask(struct r820t_priv *priv, u8 reg, u8 val,
417 int rc = r820t_read_cache_reg(priv, reg);
422 val = (rc & ~bit_mask) | (val & bit_mask);
424 return r820t_write(priv, reg, &val, 1);
427 static int r820t_read(struct r820t_priv *priv, u8 reg, u8 *val, int len)
430 u8 *p = &priv->buf[1];
434 rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, priv->buf, 1, p, len);
436 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
437 __func__, rc, reg, len, len, p);
443 /* Copy data to the output buffer */
444 for (i = 0; i < len; i++)
445 val[i] = bitrev8(p[i]);
447 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
448 __func__, reg, len, len, val);
457 static int r820t_set_mux(struct r820t_priv *priv, u32 freq)
459 const struct r820t_freq_range *range;
461 u8 val, reg08, reg09;
463 /* Get the proper frequency range */
464 freq = freq / 1000000;
465 for (i = 0; i < ARRAY_SIZE(freq_ranges) - 1; i++) {
466 if (freq < freq_ranges[i + 1].freq)
469 range = &freq_ranges[i];
471 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i, freq);
474 rc = r820t_write_reg_mask(priv, 0x17, range->open_d, 0x08);
479 rc = r820t_write_reg_mask(priv, 0x1a, range->rf_mux_ploy, 0xc3);
484 rc = r820t_write_reg(priv, 0x1b, range->tf_c);
488 /* XTAL CAP & Drive */
489 switch (priv->xtal_cap_sel) {
490 case XTAL_LOW_CAP_30P:
491 case XTAL_LOW_CAP_20P:
492 val = range->xtal_cap20p | 0x08;
494 case XTAL_LOW_CAP_10P:
495 val = range->xtal_cap10p | 0x08;
497 case XTAL_HIGH_CAP_0P:
498 val = range->xtal_cap0p | 0x00;
501 case XTAL_LOW_CAP_0P:
502 val = range->xtal_cap0p | 0x08;
505 rc = r820t_write_reg_mask(priv, 0x10, val, 0x0b);
509 if (priv->imr_done) {
510 reg08 = priv->imr_data[range->imr_mem].gain_x;
511 reg09 = priv->imr_data[range->imr_mem].phase_y;
516 rc = r820t_write_reg_mask(priv, 0x08, reg08, 0x3f);
520 rc = r820t_write_reg_mask(priv, 0x09, reg09, 0x3f);
525 static int r820t_set_pll(struct r820t_priv *priv, enum v4l2_tuner_type type,
530 unsigned sleep_time = 10000;
531 u32 vco_fra; /* VCO contribution by SDM (kHz) */
532 u32 vco_min = 1770000;
533 u32 vco_max = vco_min * 2;
541 u8 ni, si, nint, vco_fine_tune, val;
544 /* Frequency in kHz */
546 pll_ref = priv->cfg->xtal / 1000;
549 /* Doesn't exist on rtl-sdk, and on field tests, caused troubles */
550 if ((priv->cfg->rafael_chip == CHIP_R620D) ||
551 (priv->cfg->rafael_chip == CHIP_R828D) ||
552 (priv->cfg->rafael_chip == CHIP_R828)) {
553 /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
554 if (type != V4L2_TUNER_DIGITAL_TV) {
560 if (priv->cfg->xtal > 24000000) {
567 rc = r820t_write_reg_mask(priv, 0x10, refdiv2, 0x10);
571 /* set pll autotune = 128kHz */
572 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
576 /* set VCO current = 100 */
577 rc = r820t_write_reg_mask(priv, 0x12, 0x80, 0xe0);
581 /* Calculate divider */
582 while (mix_div <= 64) {
583 if (((freq * mix_div) >= vco_min) &&
584 ((freq * mix_div) < vco_max)) {
586 while (div_buf > 2) {
587 div_buf = div_buf >> 1;
592 mix_div = mix_div << 1;
595 rc = r820t_read(priv, 0x00, data, sizeof(data));
599 vco_fine_tune = (data[4] & 0x30) >> 4;
601 tuner_dbg("mix_div=%d div_num=%d vco_fine_tune=%d\n",
602 mix_div, div_num, vco_fine_tune);
605 * XXX: R828D/16MHz seems to have always vco_fine_tune=1.
606 * Due to that, this calculation goes wrong.
608 if (priv->cfg->rafael_chip != CHIP_R828D) {
609 if (vco_fine_tune > VCO_POWER_REF)
610 div_num = div_num - 1;
611 else if (vco_fine_tune < VCO_POWER_REF)
612 div_num = div_num + 1;
615 rc = r820t_write_reg_mask(priv, 0x10, div_num << 5, 0xe0);
619 vco_freq = freq * mix_div;
620 nint = vco_freq / (2 * pll_ref);
621 vco_fra = vco_freq - 2 * pll_ref * nint;
623 /* boundary spur prevention */
624 if (vco_fra < pll_ref / 64) {
626 } else if (vco_fra > pll_ref * 127 / 64) {
629 } else if ((vco_fra > pll_ref * 127 / 128) && (vco_fra < pll_ref)) {
630 vco_fra = pll_ref * 127 / 128;
631 } else if ((vco_fra > pll_ref) && (vco_fra < pll_ref * 129 / 128)) {
632 vco_fra = pll_ref * 129 / 128;
635 ni = (nint - 13) / 4;
636 si = nint - 4 * ni - 13;
638 rc = r820t_write_reg(priv, 0x14, ni + (si << 6));
648 rc = r820t_write_reg_mask(priv, 0x12, val, 0x08);
653 while (vco_fra > 1) {
654 if (vco_fra > (2 * pll_ref / n_sdm)) {
655 sdm = sdm + 32768 / (n_sdm / 2);
656 vco_fra = vco_fra - 2 * pll_ref / n_sdm;
663 tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
664 freq, pll_ref, refdiv2 ? " / 2" : "", sdm);
666 rc = r820t_write_reg(priv, 0x16, sdm >> 8);
669 rc = r820t_write_reg(priv, 0x15, sdm & 0xff);
673 for (i = 0; i < 2; i++) {
674 usleep_range(sleep_time, sleep_time + 1000);
676 /* Check if PLL has locked */
677 rc = r820t_read(priv, 0x00, data, 3);
684 /* Didn't lock. Increase VCO current */
685 rc = r820t_write_reg_mask(priv, 0x12, 0x60, 0xe0);
691 if (!(data[2] & 0x40)) {
692 priv->has_lock = false;
696 priv->has_lock = true;
697 tuner_dbg("tuner has lock at frequency %d kHz\n", freq);
699 /* set pll autotune = 8kHz */
700 rc = r820t_write_reg_mask(priv, 0x1a, 0x08, 0x08);
705 static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq,
706 enum v4l2_tuner_type type,
711 u8 mixer_top, lna_top, cp_cur, div_buf_cur, lna_vth_l, mixer_vth_l;
712 u8 air_cable1_in, cable2_in, pre_dect, lna_discharge, filter_cur;
714 tuner_dbg("adjusting tuner parameters for the standard\n");
718 if ((freq == 506000000) || (freq == 666000000) ||
719 (freq == 818000000)) {
720 mixer_top = 0x14; /* mixer top:14 , top-1, low-discharge */
721 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
722 cp_cur = 0x28; /* 101, 0.2 */
723 div_buf_cur = 0x20; /* 10, 200u */
725 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
726 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
727 cp_cur = 0x38; /* 111, auto */
728 div_buf_cur = 0x30; /* 11, 150u */
730 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
731 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
732 air_cable1_in = 0x00;
736 filter_cur = 0x40; /* 10, low */
739 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
740 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
741 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
742 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
743 air_cable1_in = 0x00;
747 cp_cur = 0x38; /* 111, auto */
748 div_buf_cur = 0x30; /* 11, 150u */
749 filter_cur = 0x40; /* 10, low */
752 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
753 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
754 lna_vth_l = 0x75; /* lna vth 1.04 , vtl 0.84 */
755 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
756 air_cable1_in = 0x00;
760 cp_cur = 0x38; /* 111, auto */
761 div_buf_cur = 0x30; /* 11, 150u */
762 filter_cur = 0x40; /* 10, low */
764 case SYS_DVBC_ANNEX_A:
765 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
769 air_cable1_in = 0x60;
773 cp_cur = 0x38; /* 111, auto */
774 div_buf_cur = 0x30; /* 11, 150u */
775 filter_cur = 0x40; /* 10, low */
777 default: /* DVB-T 8M */
778 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
779 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
780 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
781 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
782 air_cable1_in = 0x00;
786 cp_cur = 0x38; /* 111, auto */
787 div_buf_cur = 0x30; /* 11, 150u */
788 filter_cur = 0x40; /* 10, low */
792 if (priv->cfg->use_diplexer &&
793 ((priv->cfg->rafael_chip == CHIP_R820T) ||
794 (priv->cfg->rafael_chip == CHIP_R828S) ||
795 (priv->cfg->rafael_chip == CHIP_R820C))) {
797 air_cable1_in = 0x00;
799 air_cable1_in = 0x60;
804 if (priv->cfg->use_predetect) {
805 rc = r820t_write_reg_mask(priv, 0x06, pre_dect, 0x40);
810 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7);
813 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0xf8);
816 rc = r820t_write_reg(priv, 0x0d, lna_vth_l);
819 rc = r820t_write_reg(priv, 0x0e, mixer_vth_l);
823 /* Air-IN only for Astrometa */
824 rc = r820t_write_reg_mask(priv, 0x05, air_cable1_in, 0x60);
827 rc = r820t_write_reg_mask(priv, 0x06, cable2_in, 0x08);
831 rc = r820t_write_reg_mask(priv, 0x11, cp_cur, 0x38);
834 rc = r820t_write_reg_mask(priv, 0x17, div_buf_cur, 0x30);
837 rc = r820t_write_reg_mask(priv, 0x0a, filter_cur, 0x60);
841 * Original driver initializes regs 0x05 and 0x06 with the
842 * same value again on this point. Probably, it is just an
850 tuner_dbg("adjusting LNA parameters\n");
851 if (type != V4L2_TUNER_ANALOG_TV) {
852 /* LNA TOP: lowest */
853 rc = r820t_write_reg_mask(priv, 0x1d, 0, 0x38);
858 rc = r820t_write_reg_mask(priv, 0x1c, 0, 0x04);
862 /* 0: PRE_DECT off */
863 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
868 rc = r820t_write_reg_mask(priv, 0x1a, 0x30, 0x30);
874 /* write LNA TOP = 3 */
875 rc = r820t_write_reg_mask(priv, 0x1d, 0x18, 0x38);
880 * write discharge mode
881 * FIXME: IMHO, the mask here is wrong, but it matches
882 * what's there at the original driver
884 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
888 /* LNA discharge current */
889 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
894 rc = r820t_write_reg_mask(priv, 0x1a, 0x20, 0x30);
899 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
904 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0x38);
909 * write discharge mode
910 * FIXME: IMHO, the mask here is wrong, but it matches
911 * what's there at the original driver
913 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
917 /* LNA discharge current */
918 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
922 /* agc clk 1Khz, external det1 cap 1u */
923 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x30);
927 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x04);
934 static int r820t_set_tv_standard(struct r820t_priv *priv,
936 enum v4l2_tuner_type type,
937 v4l2_std_id std, u32 delsys)
941 u32 if_khz, filt_cal_lo;
943 u8 filt_gain, img_r, filt_q, hp_cor, ext_enable, loop_through;
944 u8 lt_att, flt_ext_widest, polyfil_cur;
945 bool need_calibration;
947 tuner_dbg("selecting the delivery system\n");
949 if (delsys == SYS_ISDBT) {
952 filt_gain = 0x10; /* +3db, 6mhz on */
953 img_r = 0x00; /* image negative */
954 filt_q = 0x10; /* r10[4]:low q(1'b1) */
955 hp_cor = 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
956 ext_enable = 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
957 loop_through = 0x00; /* r5[7], lt on */
958 lt_att = 0x00; /* r31[7], lt att enable */
959 flt_ext_widest = 0x80; /* r15[7]: flt_ext_wide on */
960 polyfil_cur = 0x60; /* r25[6:5]:min */
961 } else if (delsys == SYS_DVBC_ANNEX_A) {
964 filt_gain = 0x10; /* +3db, 6mhz on */
965 img_r = 0x00; /* image negative */
966 filt_q = 0x10; /* r10[4]:low q(1'b1) */
967 hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
968 ext_enable = 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
969 loop_through = 0x00; /* r5[7], lt on */
970 lt_att = 0x00; /* r31[7], lt att enable */
971 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
972 polyfil_cur = 0x60; /* r25[6:5]:min */
973 } else if (delsys == SYS_DVBC_ANNEX_C) {
976 filt_gain = 0x10; /* +3db, 6mhz on */
977 img_r = 0x00; /* image negative */
978 filt_q = 0x10; /* r10[4]:low q(1'b1) */
979 hp_cor = 0x6a; /* 1.7m disable, +0cap, 1.0mhz */
980 ext_enable = 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
981 loop_through = 0x00; /* r5[7], lt on */
982 lt_att = 0x00; /* r31[7], lt att enable */
983 flt_ext_widest = 0x80; /* r15[7]: flt_ext_wide on */
984 polyfil_cur = 0x60; /* r25[6:5]:min */
988 filt_cal_lo = 56000; /* 52000->56000 */
989 filt_gain = 0x10; /* +3db, 6mhz on */
990 img_r = 0x00; /* image negative */
991 filt_q = 0x10; /* r10[4]:low q(1'b1) */
992 hp_cor = 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
993 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
994 loop_through = 0x00; /* r5[7], lt on */
995 lt_att = 0x00; /* r31[7], lt att enable */
996 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
997 polyfil_cur = 0x60; /* r25[6:5]:min */
998 } else if (bw == 7) {
1001 * There are two 7 MHz tables defined on the original
1002 * driver, but just the second one seems to be visible
1003 * by rtl2832. Keep this one here commented, as it
1004 * might be needed in the future
1008 filt_cal_lo = 60000;
1009 filt_gain = 0x10; /* +3db, 6mhz on */
1010 img_r = 0x00; /* image negative */
1011 filt_q = 0x10; /* r10[4]:low q(1'b1) */
1012 hp_cor = 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
1013 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1014 loop_through = 0x00; /* r5[7], lt on */
1015 lt_att = 0x00; /* r31[7], lt att enable */
1016 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
1017 polyfil_cur = 0x60; /* r25[6:5]:min */
1019 /* 7 MHz, second table */
1021 filt_cal_lo = 63000;
1022 filt_gain = 0x10; /* +3db, 6mhz on */
1023 img_r = 0x00; /* image negative */
1024 filt_q = 0x10; /* r10[4]:low q(1'b1) */
1025 hp_cor = 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
1026 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1027 loop_through = 0x00; /* r5[7], lt on */
1028 lt_att = 0x00; /* r31[7], lt att enable */
1029 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
1030 polyfil_cur = 0x60; /* r25[6:5]:min */
1033 filt_cal_lo = 68500;
1034 filt_gain = 0x10; /* +3db, 6mhz on */
1035 img_r = 0x00; /* image negative */
1036 filt_q = 0x10; /* r10[4]:low q(1'b1) */
1037 hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
1038 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1039 loop_through = 0x00; /* r5[7], lt on */
1040 lt_att = 0x00; /* r31[7], lt att enable */
1041 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
1042 polyfil_cur = 0x60; /* r25[6:5]:min */
1046 /* Initialize the shadow registers */
1047 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1049 /* Init Flag & Xtal_check Result */
1051 val = 1 | priv->xtal_cap_sel << 1;
1054 rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f);
1059 rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f);
1063 /* for LT Gain test */
1064 if (type != V4L2_TUNER_ANALOG_TV) {
1065 rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38);
1068 usleep_range(1000, 2000);
1070 priv->int_freq = if_khz * 1000;
1072 /* Check if standard changed. If so, filter calibration is needed */
1073 if (type != priv->type)
1074 need_calibration = true;
1075 else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std))
1076 need_calibration = true;
1077 else if ((type == V4L2_TUNER_DIGITAL_TV) &&
1078 ((delsys != priv->delsys) || bw != priv->bw))
1079 need_calibration = true;
1081 need_calibration = false;
1083 if (need_calibration) {
1084 tuner_dbg("calibrating the tuner\n");
1085 for (i = 0; i < 2; i++) {
1087 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60);
1091 /* set cali clk =on */
1092 rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04);
1096 /* X'tal cap 0pF for PLL */
1097 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03);
1101 rc = r820t_set_pll(priv, type, filt_cal_lo * 1000);
1102 if (rc < 0 || !priv->has_lock)
1106 rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10);
1110 usleep_range(1000, 2000);
1113 rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10);
1117 /* set cali clk =off */
1118 rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04);
1122 /* Check if calibration worked */
1123 rc = r820t_read(priv, 0x00, data, sizeof(data));
1127 priv->fil_cal_code = data[4] & 0x0f;
1128 if (priv->fil_cal_code && priv->fil_cal_code != 0x0f)
1132 if (priv->fil_cal_code == 0x0f)
1133 priv->fil_cal_code = 0;
1136 rc = r820t_write_reg_mask(priv, 0x0a,
1137 filt_q | priv->fil_cal_code, 0x1f);
1141 /* Set BW, Filter_gain, & HP corner */
1142 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0xef);
1148 rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80);
1152 /* Set filt_3dB, V6MHz */
1153 rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30);
1157 /* channel filter extension */
1158 rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60);
1163 rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80);
1167 /* Loop through attenuation */
1168 rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80);
1172 /* filter extension widest */
1173 rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80);
1177 /* RF poly filter current */
1178 rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60);
1182 /* Store current standard. If it changes, re-calibrate the tuner */
1183 priv->delsys = delsys;
1191 static int r820t_read_gain(struct r820t_priv *priv)
1196 rc = r820t_read(priv, 0x00, data, sizeof(data));
1200 return ((data[3] & 0x08) << 1) + ((data[3] & 0xf0) >> 4);
1204 /* FIXME: This routine requires more testing */
1207 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
1208 * input power, for raw results see:
1209 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
1212 static const int r820t_lna_gain_steps[] = {
1213 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
1216 static const int r820t_mixer_gain_steps[] = {
1217 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
1220 static int r820t_set_gain_mode(struct r820t_priv *priv,
1221 bool set_manual_gain,
1226 if (set_manual_gain) {
1227 int i, total_gain = 0;
1228 uint8_t mix_index = 0, lna_index = 0;
1232 rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10);
1236 /* Mixer auto off */
1237 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
1241 rc = r820t_read(priv, 0x00, data, sizeof(data));
1245 /* set fixed VGA gain for now (16.3 dB) */
1246 rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f);
1250 for (i = 0; i < 15; i++) {
1251 if (total_gain >= gain)
1254 total_gain += r820t_lna_gain_steps[++lna_index];
1256 if (total_gain >= gain)
1259 total_gain += r820t_mixer_gain_steps[++mix_index];
1263 rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f);
1267 /* set Mixer gain */
1268 rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f);
1273 rc = r820t_write_reg_mask(priv, 0x05, 0, 0x10);
1278 rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0x10);
1282 /* set fixed VGA gain for now (26.5 dB) */
1283 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
1292 static int generic_set_freq(struct dvb_frontend *fe,
1293 u32 freq /* in HZ */,
1295 enum v4l2_tuner_type type,
1296 v4l2_std_id std, u32 delsys)
1298 struct r820t_priv *priv = fe->tuner_priv;
1302 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1305 rc = r820t_set_tv_standard(priv, bw, type, std, delsys);
1309 if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC))
1310 lo_freq = freq - priv->int_freq;
1312 lo_freq = freq + priv->int_freq;
1314 rc = r820t_set_mux(priv, lo_freq);
1318 rc = r820t_set_pll(priv, type, lo_freq);
1319 if (rc < 0 || !priv->has_lock)
1322 rc = r820t_sysfreq_sel(priv, freq, type, std, delsys);
1326 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1327 __func__, freq, r820t_read_gain(priv));
1332 tuner_dbg("%s: failed=%d\n", __func__, rc);
1337 * r820t standby logic
1340 static int r820t_standby(struct r820t_priv *priv)
1344 /* If device was not initialized yet, don't need to standby */
1345 if (!priv->init_done)
1348 rc = r820t_write_reg(priv, 0x06, 0xb1);
1351 rc = r820t_write_reg(priv, 0x05, 0x03);
1354 rc = r820t_write_reg(priv, 0x07, 0x3a);
1357 rc = r820t_write_reg(priv, 0x08, 0x40);
1360 rc = r820t_write_reg(priv, 0x09, 0xc0);
1363 rc = r820t_write_reg(priv, 0x0a, 0x36);
1366 rc = r820t_write_reg(priv, 0x0c, 0x35);
1369 rc = r820t_write_reg(priv, 0x0f, 0x68);
1372 rc = r820t_write_reg(priv, 0x11, 0x03);
1375 rc = r820t_write_reg(priv, 0x17, 0xf4);
1378 rc = r820t_write_reg(priv, 0x19, 0x0c);
1380 /* Force initial calibration */
1387 * r820t device init logic
1390 static int r820t_xtal_check(struct r820t_priv *priv)
1395 /* Initialize the shadow registers */
1396 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1398 /* cap 30pF & Drive Low */
1399 rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b);
1403 /* set pll autotune = 128kHz */
1404 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
1408 /* set manual initial reg = 111111; */
1409 rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f);
1414 rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40);
1418 /* Try several xtal capacitor alternatives */
1419 for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) {
1420 rc = r820t_write_reg_mask(priv, 0x10,
1421 r820t_xtal_capacitor[i][0], 0x1b);
1425 usleep_range(5000, 6000);
1427 rc = r820t_read(priv, 0x00, data, sizeof(data));
1430 if (!(data[2] & 0x40))
1433 val = data[2] & 0x3f;
1435 if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23))
1442 if (i == ARRAY_SIZE(r820t_xtal_capacitor))
1445 return r820t_xtal_capacitor[i][1];
1448 static int r820t_imr_prepare(struct r820t_priv *priv)
1452 /* Initialize the shadow registers */
1453 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1455 /* lna off (air-in off) */
1456 rc = r820t_write_reg_mask(priv, 0x05, 0x20, 0x20);
1460 /* mixer gain mode = manual */
1461 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
1465 /* filter corner = lowest */
1466 rc = r820t_write_reg_mask(priv, 0x0a, 0x0f, 0x0f);
1470 /* filter bw=+2cap, hp=5M */
1471 rc = r820t_write_reg_mask(priv, 0x0b, 0x60, 0x6f);
1475 /* adc=on, vga code mode, gain = 26.5dB */
1476 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
1481 rc = r820t_write_reg_mask(priv, 0x0f, 0, 0x08);
1485 /* ring power = on */
1486 rc = r820t_write_reg_mask(priv, 0x18, 0x10, 0x10);
1490 /* from ring = ring pll in */
1491 rc = r820t_write_reg_mask(priv, 0x1c, 0x02, 0x02);
1495 /* sw_pdect = det3 */
1496 rc = r820t_write_reg_mask(priv, 0x1e, 0x80, 0x80);
1501 rc = r820t_write_reg_mask(priv, 0x06, 0x20, 0x20);
1506 static int r820t_multi_read(struct r820t_priv *priv)
1510 u8 data[2], min = 255, max = 0;
1512 usleep_range(5000, 6000);
1514 for (i = 0; i < 6; i++) {
1515 rc = r820t_read(priv, 0x00, data, sizeof(data));
1527 rc = sum - max - min;
1532 static int r820t_imr_cross(struct r820t_priv *priv,
1533 struct r820t_sect_type iq_point[3],
1536 struct r820t_sect_type cross[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
1537 struct r820t_sect_type tmp;
1541 reg08 = r820t_read_cache_reg(priv, 8) & 0xc0;
1542 reg09 = r820t_read_cache_reg(priv, 9) & 0xc0;
1548 for (i = 0; i < 5; i++) {
1551 cross[i].gain_x = reg08;
1552 cross[i].phase_y = reg09;
1555 cross[i].gain_x = reg08; /* 0 */
1556 cross[i].phase_y = reg09 + 1; /* Q-1 */
1559 cross[i].gain_x = reg08; /* 0 */
1560 cross[i].phase_y = (reg09 | 0x20) + 1; /* I-1 */
1563 cross[i].gain_x = reg08 + 1; /* Q-1 */
1564 cross[i].phase_y = reg09;
1567 cross[i].gain_x = (reg08 | 0x20) + 1; /* I-1 */
1568 cross[i].phase_y = reg09;
1571 rc = r820t_write_reg(priv, 0x08, cross[i].gain_x);
1575 rc = r820t_write_reg(priv, 0x09, cross[i].phase_y);
1579 rc = r820t_multi_read(priv);
1583 cross[i].value = rc;
1585 if (cross[i].value < tmp.value)
1589 if ((tmp.phase_y & 0x1f) == 1) { /* y-direction */
1592 iq_point[0] = cross[0];
1593 iq_point[1] = cross[1];
1594 iq_point[2] = cross[2];
1595 } else { /* (0,0) or x-direction */
1598 iq_point[0] = cross[0];
1599 iq_point[1] = cross[3];
1600 iq_point[2] = cross[4];
1605 static void r820t_compre_cor(struct r820t_sect_type iq[3])
1609 for (i = 3; i > 0; i--) {
1610 if (iq[0].value > iq[i - 1].value)
1611 swap(iq[0], iq[i - 1]);
1615 static int r820t_compre_step(struct r820t_priv *priv,
1616 struct r820t_sect_type iq[3], u8 reg)
1619 struct r820t_sect_type tmp;
1622 * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
1624 * new < min => update to min and continue
1628 /* min value already saved in iq[0] */
1629 tmp.phase_y = iq[0].phase_y;
1630 tmp.gain_x = iq[0].gain_x;
1632 while (((tmp.gain_x & 0x1f) < IMR_TRIAL) &&
1633 ((tmp.phase_y & 0x1f) < IMR_TRIAL)) {
1639 rc = r820t_write_reg(priv, 0x08, tmp.gain_x);
1643 rc = r820t_write_reg(priv, 0x09, tmp.phase_y);
1647 rc = r820t_multi_read(priv);
1652 if (tmp.value <= iq[0].value) {
1653 iq[0].gain_x = tmp.gain_x;
1654 iq[0].phase_y = tmp.phase_y;
1655 iq[0].value = tmp.value;
1665 static int r820t_iq_tree(struct r820t_priv *priv,
1666 struct r820t_sect_type iq[3],
1667 u8 fix_val, u8 var_val, u8 fix_reg)
1673 * record IMC results by input gain/phase location then adjust
1674 * gain or phase positive 1 step and negtive 1 step,
1675 * both record results
1678 if (fix_reg == 0x08)
1683 for (i = 0; i < 3; i++) {
1684 rc = r820t_write_reg(priv, fix_reg, fix_val);
1688 rc = r820t_write_reg(priv, var_reg, var_val);
1692 rc = r820t_multi_read(priv);
1697 if (fix_reg == 0x08) {
1698 iq[i].gain_x = fix_val;
1699 iq[i].phase_y = var_val;
1701 iq[i].phase_y = fix_val;
1702 iq[i].gain_x = var_val;
1705 if (i == 0) { /* try right-side point */
1707 } else if (i == 1) { /* try left-side point */
1708 /* if absolute location is 1, change I/Q direction */
1709 if ((var_val & 0x1f) < 0x02) {
1710 tmp = 2 - (var_val & 0x1f);
1712 /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
1713 if (var_val & 0x20) {
1717 var_val |= 0x20 | tmp;
1728 static int r820t_section(struct r820t_priv *priv,
1729 struct r820t_sect_type *iq_point)
1732 struct r820t_sect_type compare_iq[3], compare_bet[3];
1734 /* Try X-1 column and save min result to compare_bet[0] */
1735 if (!(iq_point->gain_x & 0x1f))
1736 compare_iq[0].gain_x = ((iq_point->gain_x) & 0xdf) + 1; /* Q-path, Gain=1 */
1738 compare_iq[0].gain_x = iq_point->gain_x - 1; /* left point */
1739 compare_iq[0].phase_y = iq_point->phase_y;
1742 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1743 compare_iq[0].phase_y, 0x08);
1747 r820t_compre_cor(compare_iq);
1749 compare_bet[0] = compare_iq[0];
1751 /* Try X column and save min result to compare_bet[1] */
1752 compare_iq[0].gain_x = iq_point->gain_x;
1753 compare_iq[0].phase_y = iq_point->phase_y;
1755 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1756 compare_iq[0].phase_y, 0x08);
1760 r820t_compre_cor(compare_iq);
1762 compare_bet[1] = compare_iq[0];
1764 /* Try X+1 column and save min result to compare_bet[2] */
1765 if ((iq_point->gain_x & 0x1f) == 0x00)
1766 compare_iq[0].gain_x = ((iq_point->gain_x) | 0x20) + 1; /* I-path, Gain=1 */
1768 compare_iq[0].gain_x = iq_point->gain_x + 1;
1769 compare_iq[0].phase_y = iq_point->phase_y;
1771 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1772 compare_iq[0].phase_y, 0x08);
1776 r820t_compre_cor(compare_iq);
1778 compare_bet[2] = compare_iq[0];
1780 r820t_compre_cor(compare_bet);
1782 *iq_point = compare_bet[0];
1787 static int r820t_vga_adjust(struct r820t_priv *priv)
1792 /* increase vga power to let image significant */
1793 for (vga_count = 12; vga_count < 16; vga_count++) {
1794 rc = r820t_write_reg_mask(priv, 0x0c, vga_count, 0x0f);
1798 usleep_range(10000, 11000);
1800 rc = r820t_multi_read(priv);
1811 static int r820t_iq(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
1813 struct r820t_sect_type compare_iq[3];
1815 u8 x_direction = 0; /* 1:x, 0:y */
1816 u8 dir_reg, other_reg;
1818 r820t_vga_adjust(priv);
1820 rc = r820t_imr_cross(priv, compare_iq, &x_direction);
1824 if (x_direction == 1) {
1832 /* compare and find min of 3 points. determine i/q direction */
1833 r820t_compre_cor(compare_iq);
1835 /* increase step to find min value of this direction */
1836 rc = r820t_compre_step(priv, compare_iq, dir_reg);
1840 /* the other direction */
1841 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1842 compare_iq[0].phase_y, dir_reg);
1846 /* compare and find min of 3 points. determine i/q direction */
1847 r820t_compre_cor(compare_iq);
1849 /* increase step to find min value on this direction */
1850 rc = r820t_compre_step(priv, compare_iq, other_reg);
1854 /* check 3 points again */
1855 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1856 compare_iq[0].phase_y, other_reg);
1860 r820t_compre_cor(compare_iq);
1862 /* section-9 check */
1863 rc = r820t_section(priv, compare_iq);
1865 *iq_pont = compare_iq[0];
1867 /* reset gain/phase control setting */
1868 rc = r820t_write_reg_mask(priv, 0x08, 0, 0x3f);
1872 rc = r820t_write_reg_mask(priv, 0x09, 0, 0x3f);
1877 static int r820t_f_imr(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
1881 r820t_vga_adjust(priv);
1884 * search surrounding points from previous point
1885 * try (x-1), (x), (x+1) columns, and find min IMR result point
1887 rc = r820t_section(priv, iq_pont);
1894 static int r820t_imr(struct r820t_priv *priv, unsigned imr_mem, bool im_flag)
1896 struct r820t_sect_type imr_point;
1898 u32 ring_vco, ring_freq, ring_ref;
1900 int reg18, reg19, reg1f;
1902 if (priv->cfg->xtal > 24000000)
1903 ring_ref = priv->cfg->xtal / 2000;
1905 ring_ref = priv->cfg->xtal / 1000;
1908 for (n = 0; n < 16; n++) {
1909 if ((16 + n) * 8 * ring_ref >= 3100000) {
1915 reg18 = r820t_read_cache_reg(priv, 0x18);
1916 reg19 = r820t_read_cache_reg(priv, 0x19);
1917 reg1f = r820t_read_cache_reg(priv, 0x1f);
1919 reg18 &= 0xf0; /* set ring[3:0] */
1922 ring_vco = (16 + n_ring) * 8 * ring_ref;
1924 reg18 &= 0xdf; /* clear ring_se23 */
1925 reg19 &= 0xfc; /* clear ring_seldiv */
1926 reg1f &= 0xfc; /* clear ring_att */
1930 ring_freq = ring_vco / 48;
1931 reg18 |= 0x20; /* ring_se23 = 1 */
1932 reg19 |= 0x03; /* ring_seldiv = 3 */
1933 reg1f |= 0x02; /* ring_att 10 */
1936 ring_freq = ring_vco / 16;
1937 reg18 |= 0x00; /* ring_se23 = 0 */
1938 reg19 |= 0x02; /* ring_seldiv = 2 */
1939 reg1f |= 0x00; /* pw_ring 00 */
1942 ring_freq = ring_vco / 8;
1943 reg18 |= 0x00; /* ring_se23 = 0 */
1944 reg19 |= 0x01; /* ring_seldiv = 1 */
1945 reg1f |= 0x03; /* pw_ring 11 */
1948 ring_freq = ring_vco / 6;
1949 reg18 |= 0x20; /* ring_se23 = 1 */
1950 reg19 |= 0x00; /* ring_seldiv = 0 */
1951 reg1f |= 0x03; /* pw_ring 11 */
1954 ring_freq = ring_vco / 4;
1955 reg18 |= 0x00; /* ring_se23 = 0 */
1956 reg19 |= 0x00; /* ring_seldiv = 0 */
1957 reg1f |= 0x01; /* pw_ring 01 */
1960 ring_freq = ring_vco / 4;
1961 reg18 |= 0x00; /* ring_se23 = 0 */
1962 reg19 |= 0x00; /* ring_seldiv = 0 */
1963 reg1f |= 0x01; /* pw_ring 01 */
1968 /* write pw_ring, n_ring, ringdiv2 registers */
1970 /* n_ring, ring_se23 */
1971 rc = r820t_write_reg(priv, 0x18, reg18);
1976 rc = r820t_write_reg(priv, 0x19, reg19);
1981 rc = r820t_write_reg(priv, 0x1f, reg1f);
1985 /* mux input freq ~ rf_in freq */
1986 rc = r820t_set_mux(priv, (ring_freq - 5300) * 1000);
1990 rc = r820t_set_pll(priv, V4L2_TUNER_DIGITAL_TV,
1991 (ring_freq - 5300) * 1000);
1992 if (!priv->has_lock)
1998 rc = r820t_iq(priv, &imr_point);
2000 imr_point.gain_x = priv->imr_data[3].gain_x;
2001 imr_point.phase_y = priv->imr_data[3].phase_y;
2002 imr_point.value = priv->imr_data[3].value;
2004 rc = r820t_f_imr(priv, &imr_point);
2009 /* save IMR value */
2012 priv->imr_data[0].gain_x = imr_point.gain_x;
2013 priv->imr_data[0].phase_y = imr_point.phase_y;
2014 priv->imr_data[0].value = imr_point.value;
2017 priv->imr_data[1].gain_x = imr_point.gain_x;
2018 priv->imr_data[1].phase_y = imr_point.phase_y;
2019 priv->imr_data[1].value = imr_point.value;
2022 priv->imr_data[2].gain_x = imr_point.gain_x;
2023 priv->imr_data[2].phase_y = imr_point.phase_y;
2024 priv->imr_data[2].value = imr_point.value;
2027 priv->imr_data[3].gain_x = imr_point.gain_x;
2028 priv->imr_data[3].phase_y = imr_point.phase_y;
2029 priv->imr_data[3].value = imr_point.value;
2032 priv->imr_data[4].gain_x = imr_point.gain_x;
2033 priv->imr_data[4].phase_y = imr_point.phase_y;
2034 priv->imr_data[4].value = imr_point.value;
2037 priv->imr_data[4].gain_x = imr_point.gain_x;
2038 priv->imr_data[4].phase_y = imr_point.phase_y;
2039 priv->imr_data[4].value = imr_point.value;
2046 static int r820t_imr_callibrate(struct r820t_priv *priv)
2051 if (priv->init_done)
2054 /* Detect Xtal capacitance */
2055 if ((priv->cfg->rafael_chip == CHIP_R820T) ||
2056 (priv->cfg->rafael_chip == CHIP_R828S) ||
2057 (priv->cfg->rafael_chip == CHIP_R820C)) {
2058 priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;
2060 /* Initialize registers */
2061 rc = r820t_write(priv, 0x05,
2062 r820t_init_array, sizeof(r820t_init_array));
2065 for (i = 0; i < 3; i++) {
2066 rc = r820t_xtal_check(priv);
2069 if (!i || rc > xtal_cap)
2072 priv->xtal_cap_sel = xtal_cap;
2076 * Disables IMR callibration. That emulates the same behaviour
2077 * as what is done by rtl-sdr userspace library. Useful for testing
2080 priv->init_done = true;
2085 /* Initialize registers */
2086 rc = r820t_write(priv, 0x05,
2087 r820t_init_array, sizeof(r820t_init_array));
2091 rc = r820t_imr_prepare(priv);
2095 rc = r820t_imr(priv, 3, true);
2098 rc = r820t_imr(priv, 1, false);
2101 rc = r820t_imr(priv, 0, false);
2104 rc = r820t_imr(priv, 2, false);
2107 rc = r820t_imr(priv, 4, false);
2111 priv->init_done = true;
2112 priv->imr_done = true;
2118 /* Not used, for now */
2119 static int r820t_gpio(struct r820t_priv *priv, bool enable)
2121 return r820t_write_reg_mask(priv, 0x0f, enable ? 1 : 0, 0x01);
2126 * r820t frontend operations and tuner attach code
2128 * All driver locks and i2c control are only in this part of the code
2131 static int r820t_init(struct dvb_frontend *fe)
2133 struct r820t_priv *priv = fe->tuner_priv;
2136 tuner_dbg("%s:\n", __func__);
2138 mutex_lock(&priv->lock);
2139 if (fe->ops.i2c_gate_ctrl)
2140 fe->ops.i2c_gate_ctrl(fe, 1);
2142 rc = r820t_imr_callibrate(priv);
2146 /* Initialize registers */
2147 rc = r820t_write(priv, 0x05,
2148 r820t_init_array, sizeof(r820t_init_array));
2151 if (fe->ops.i2c_gate_ctrl)
2152 fe->ops.i2c_gate_ctrl(fe, 0);
2153 mutex_unlock(&priv->lock);
2156 tuner_dbg("%s: failed=%d\n", __func__, rc);
2160 static int r820t_sleep(struct dvb_frontend *fe)
2162 struct r820t_priv *priv = fe->tuner_priv;
2165 tuner_dbg("%s:\n", __func__);
2167 mutex_lock(&priv->lock);
2168 if (fe->ops.i2c_gate_ctrl)
2169 fe->ops.i2c_gate_ctrl(fe, 1);
2171 rc = r820t_standby(priv);
2173 if (fe->ops.i2c_gate_ctrl)
2174 fe->ops.i2c_gate_ctrl(fe, 0);
2175 mutex_unlock(&priv->lock);
2177 tuner_dbg("%s: failed=%d\n", __func__, rc);
2181 static int r820t_set_analog_freq(struct dvb_frontend *fe,
2182 struct analog_parameters *p)
2184 struct r820t_priv *priv = fe->tuner_priv;
2188 tuner_dbg("%s called\n", __func__);
2190 /* if std is not defined, choose one */
2192 p->std = V4L2_STD_MN;
2194 if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC))
2199 mutex_lock(&priv->lock);
2200 if (fe->ops.i2c_gate_ctrl)
2201 fe->ops.i2c_gate_ctrl(fe, 1);
2203 rc = generic_set_freq(fe, 62500l * p->frequency, bw,
2204 V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED);
2206 if (fe->ops.i2c_gate_ctrl)
2207 fe->ops.i2c_gate_ctrl(fe, 0);
2208 mutex_unlock(&priv->lock);
2213 static int r820t_set_params(struct dvb_frontend *fe)
2215 struct r820t_priv *priv = fe->tuner_priv;
2216 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2220 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2221 __func__, c->delivery_system, c->frequency, c->bandwidth_hz);
2223 mutex_lock(&priv->lock);
2224 if (fe->ops.i2c_gate_ctrl)
2225 fe->ops.i2c_gate_ctrl(fe, 1);
2227 bw = (c->bandwidth_hz + 500000) / 1000000;
2231 rc = generic_set_freq(fe, c->frequency, bw,
2232 V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system);
2234 if (fe->ops.i2c_gate_ctrl)
2235 fe->ops.i2c_gate_ctrl(fe, 0);
2236 mutex_unlock(&priv->lock);
2239 tuner_dbg("%s: failed=%d\n", __func__, rc);
2243 static int r820t_signal(struct dvb_frontend *fe, u16 *strength)
2245 struct r820t_priv *priv = fe->tuner_priv;
2248 mutex_lock(&priv->lock);
2249 if (fe->ops.i2c_gate_ctrl)
2250 fe->ops.i2c_gate_ctrl(fe, 1);
2252 if (priv->has_lock) {
2253 rc = r820t_read_gain(priv);
2257 /* A higher gain at LNA means a lower signal strength */
2258 *strength = (45 - rc) << 4 | 0xff;
2259 if (*strength == 0xff)
2266 if (fe->ops.i2c_gate_ctrl)
2267 fe->ops.i2c_gate_ctrl(fe, 0);
2268 mutex_unlock(&priv->lock);
2270 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2272 priv->has_lock ? "PLL locked" : "no signal",
2278 static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
2280 struct r820t_priv *priv = fe->tuner_priv;
2282 tuner_dbg("%s:\n", __func__);
2284 *frequency = priv->int_freq;
2289 static int r820t_release(struct dvb_frontend *fe)
2291 struct r820t_priv *priv = fe->tuner_priv;
2293 tuner_dbg("%s:\n", __func__);
2295 mutex_lock(&r820t_list_mutex);
2298 hybrid_tuner_release_state(priv);
2300 mutex_unlock(&r820t_list_mutex);
2302 fe->tuner_priv = NULL;
2307 static const struct dvb_tuner_ops r820t_tuner_ops = {
2309 .name = "Rafael Micro R820T",
2310 .frequency_min = 42000000,
2311 .frequency_max = 1002000000,
2314 .release = r820t_release,
2315 .sleep = r820t_sleep,
2316 .set_params = r820t_set_params,
2317 .set_analog_params = r820t_set_analog_freq,
2318 .get_if_frequency = r820t_get_if_frequency,
2319 .get_rf_strength = r820t_signal,
2322 struct dvb_frontend *r820t_attach(struct dvb_frontend *fe,
2323 struct i2c_adapter *i2c,
2324 const struct r820t_config *cfg)
2326 struct r820t_priv *priv;
2331 mutex_lock(&r820t_list_mutex);
2333 instance = hybrid_tuner_request_state(struct r820t_priv, priv,
2334 hybrid_tuner_instance_list,
2339 /* memory allocation failure */
2342 /* new tuner instance */
2345 mutex_init(&priv->lock);
2347 fe->tuner_priv = priv;
2350 /* existing tuner instance */
2351 fe->tuner_priv = priv;
2355 if (fe->ops.i2c_gate_ctrl)
2356 fe->ops.i2c_gate_ctrl(fe, 1);
2358 /* check if the tuner is there */
2359 rc = r820t_read(priv, 0x00, data, sizeof(data));
2363 rc = r820t_sleep(fe);
2367 tuner_info("Rafael Micro r820t successfully identified\n");
2369 if (fe->ops.i2c_gate_ctrl)
2370 fe->ops.i2c_gate_ctrl(fe, 0);
2372 mutex_unlock(&r820t_list_mutex);
2374 memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops,
2375 sizeof(struct dvb_tuner_ops));
2379 if (fe->ops.i2c_gate_ctrl)
2380 fe->ops.i2c_gate_ctrl(fe, 0);
2383 mutex_unlock(&r820t_list_mutex);
2385 tuner_info("%s: failed=%d\n", __func__, rc);
2389 EXPORT_SYMBOL_GPL(r820t_attach);
2391 MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2392 MODULE_AUTHOR("Mauro Carvalho Chehab");
2393 MODULE_LICENSE("GPL");