1 // SPDX-License-Identifier: GPL-2.0
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/videodev2.h>
26 #include <linux/mutex.h>
27 #include <linux/slab.h>
28 #include <linux/bitrev.h>
30 #include "tuner-i2c.h"
34 * FIXME: I think that there are only 32 registers, but better safe than
35 * sorry. After finishing the driver, we may review it.
37 #define REG_SHADOW_START 5
45 module_param(debug, int, 0644);
46 MODULE_PARM_DESC(debug, "enable verbose debug messages");
48 static int no_imr_cal;
49 module_param(no_imr_cal, int, 0444);
50 MODULE_PARM_DESC(no_imr_cal, "Disable IMR calibration at module init");
54 * enums and structures
65 struct r820t_sect_type {
72 struct list_head hybrid_tuner_instance_list;
73 const struct r820t_config *cfg;
74 struct tuner_i2c_props i2c_props;
79 enum xtal_cap_value xtal_cap_sel;
86 struct r820t_sect_type imr_data[NUM_IMR];
88 /* Store current mode */
90 enum v4l2_tuner_type type;
95 struct r820t_freq_range {
103 u8 imr_mem; /* Not used, currently */
106 #define VCO_POWER_REF 0x02
107 #define DIP_FREQ 32000000
113 static LIST_HEAD(hybrid_tuner_instance_list);
114 static DEFINE_MUTEX(r820t_list_mutex);
116 /* Those initial values start from REG_SHADOW_START */
117 static const u8 r820t_init_array[NUM_REGS] = {
118 0x83, 0x32, 0x75, /* 05 to 07 */
119 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
120 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
121 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
122 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
123 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
124 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
127 /* Tuner frequency ranges */
128 static const struct r820t_freq_range freq_ranges[] = {
131 .open_d = 0x08, /* low */
132 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
133 .tf_c = 0xdf, /* R27[7:0] band2,band0 */
134 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
139 .freq = 50, /* Start freq, in MHz */
140 .open_d = 0x08, /* low */
141 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
142 .tf_c = 0xbe, /* R27[7:0] band4,band1 */
143 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
148 .freq = 55, /* Start freq, in MHz */
149 .open_d = 0x08, /* low */
150 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
151 .tf_c = 0x8b, /* R27[7:0] band7,band4 */
152 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
157 .freq = 60, /* Start freq, in MHz */
158 .open_d = 0x08, /* low */
159 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
160 .tf_c = 0x7b, /* R27[7:0] band8,band4 */
161 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
166 .freq = 65, /* Start freq, in MHz */
167 .open_d = 0x08, /* low */
168 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
169 .tf_c = 0x69, /* R27[7:0] band9,band6 */
170 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
175 .freq = 70, /* Start freq, in MHz */
176 .open_d = 0x08, /* low */
177 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
178 .tf_c = 0x58, /* R27[7:0] band10,band7 */
179 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
184 .freq = 75, /* Start freq, in MHz */
185 .open_d = 0x00, /* high */
186 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
187 .tf_c = 0x44, /* R27[7:0] band11,band11 */
188 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
193 .freq = 80, /* Start freq, in MHz */
194 .open_d = 0x00, /* high */
195 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
196 .tf_c = 0x44, /* R27[7:0] band11,band11 */
197 .xtal_cap20p = 0x02, /* R16[1:0] 20pF (10) */
202 .freq = 90, /* Start freq, in MHz */
203 .open_d = 0x00, /* high */
204 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
205 .tf_c = 0x34, /* R27[7:0] band12,band11 */
206 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
211 .freq = 100, /* Start freq, in MHz */
212 .open_d = 0x00, /* high */
213 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
214 .tf_c = 0x34, /* R27[7:0] band12,band11 */
215 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
220 .freq = 110, /* Start freq, in MHz */
221 .open_d = 0x00, /* high */
222 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
223 .tf_c = 0x24, /* R27[7:0] band13,band11 */
224 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
229 .freq = 120, /* Start freq, in MHz */
230 .open_d = 0x00, /* high */
231 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
232 .tf_c = 0x24, /* R27[7:0] band13,band11 */
233 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
238 .freq = 140, /* Start freq, in MHz */
239 .open_d = 0x00, /* high */
240 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
241 .tf_c = 0x14, /* R27[7:0] band14,band11 */
242 .xtal_cap20p = 0x01, /* R16[1:0] 10pF (01) */
247 .freq = 180, /* Start freq, in MHz */
248 .open_d = 0x00, /* high */
249 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
250 .tf_c = 0x13, /* R27[7:0] band14,band12 */
251 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
256 .freq = 220, /* Start freq, in MHz */
257 .open_d = 0x00, /* high */
258 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
259 .tf_c = 0x13, /* R27[7:0] band14,band12 */
260 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
265 .freq = 250, /* Start freq, in MHz */
266 .open_d = 0x00, /* high */
267 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
268 .tf_c = 0x11, /* R27[7:0] highest,highest */
269 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
274 .freq = 280, /* Start freq, in MHz */
275 .open_d = 0x00, /* high */
276 .rf_mux_ploy = 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
277 .tf_c = 0x00, /* R27[7:0] highest,highest */
278 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
283 .freq = 310, /* Start freq, in MHz */
284 .open_d = 0x00, /* high */
285 .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
286 .tf_c = 0x00, /* R27[7:0] highest,highest */
287 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
292 .freq = 450, /* Start freq, in MHz */
293 .open_d = 0x00, /* high */
294 .rf_mux_ploy = 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
295 .tf_c = 0x00, /* R27[7:0] highest,highest */
296 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
301 .freq = 588, /* Start freq, in MHz */
302 .open_d = 0x00, /* high */
303 .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
304 .tf_c = 0x00, /* R27[7:0] highest,highest */
305 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
310 .freq = 650, /* Start freq, in MHz */
311 .open_d = 0x00, /* high */
312 .rf_mux_ploy = 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
313 .tf_c = 0x00, /* R27[7:0] highest,highest */
314 .xtal_cap20p = 0x00, /* R16[1:0] 0pF (00) */
321 static int r820t_xtal_capacitor[][2] = {
322 { 0x0b, XTAL_LOW_CAP_30P },
323 { 0x02, XTAL_LOW_CAP_20P },
324 { 0x01, XTAL_LOW_CAP_10P },
325 { 0x00, XTAL_LOW_CAP_0P },
326 { 0x10, XTAL_HIGH_CAP_0P },
329 static const char *r820t_chip_enum_to_str(enum r820t_chip chip)
350 * I2C read/write code and shadow registers logic
352 static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
355 int r = reg - REG_SHADOW_START;
363 if (len > NUM_REGS - r)
366 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
367 __func__, r + REG_SHADOW_START, len, len, val);
369 memcpy(&priv->regs[r], val, len);
372 static int r820t_write(struct r820t_priv *priv, u8 reg, const u8 *val,
375 int rc, size, pos = 0;
377 /* Store the shadow registers */
378 shadow_store(priv, reg, val, len);
381 if (len > priv->cfg->max_i2c_msg_len - 1)
382 size = priv->cfg->max_i2c_msg_len - 1;
386 /* Fill I2C buffer */
388 memcpy(&priv->buf[1], &val[pos], size);
390 rc = tuner_i2c_xfer_send(&priv->i2c_props, priv->buf, size + 1);
391 if (rc != size + 1) {
392 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
393 __func__, rc, reg, size, size, &priv->buf[1]);
398 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
399 __func__, reg, size, size, &priv->buf[1]);
409 static inline int r820t_write_reg(struct r820t_priv *priv, u8 reg, u8 val)
411 u8 tmp = val; /* work around GCC PR81715 with asan-stack=1 */
413 return r820t_write(priv, reg, &tmp, 1);
416 static int r820t_read_cache_reg(struct r820t_priv *priv, int reg)
418 reg -= REG_SHADOW_START;
420 if (reg >= 0 && reg < NUM_REGS)
421 return priv->regs[reg];
426 static inline int r820t_write_reg_mask(struct r820t_priv *priv, u8 reg, u8 val,
430 int rc = r820t_read_cache_reg(priv, reg);
435 tmp = (rc & ~bit_mask) | (tmp & bit_mask);
437 return r820t_write(priv, reg, &tmp, 1);
440 static int r820t_read(struct r820t_priv *priv, u8 reg, u8 *val, int len)
443 u8 *p = &priv->buf[1];
447 rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, priv->buf, 1, p, len);
449 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
450 __func__, rc, reg, len, len, p);
456 /* Copy data to the output buffer */
457 for (i = 0; i < len; i++)
458 val[i] = bitrev8(p[i]);
460 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
461 __func__, reg, len, len, val);
470 static int r820t_set_mux(struct r820t_priv *priv, u32 freq)
472 const struct r820t_freq_range *range;
474 u8 val, reg08, reg09;
476 /* Get the proper frequency range */
477 freq = freq / 1000000;
478 for (i = 0; i < ARRAY_SIZE(freq_ranges) - 1; i++) {
479 if (freq < freq_ranges[i + 1].freq)
482 range = &freq_ranges[i];
484 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i, freq);
487 rc = r820t_write_reg_mask(priv, 0x17, range->open_d, 0x08);
492 rc = r820t_write_reg_mask(priv, 0x1a, range->rf_mux_ploy, 0xc3);
497 rc = r820t_write_reg(priv, 0x1b, range->tf_c);
501 /* XTAL CAP & Drive */
502 switch (priv->xtal_cap_sel) {
503 case XTAL_LOW_CAP_30P:
504 case XTAL_LOW_CAP_20P:
505 val = range->xtal_cap20p | 0x08;
507 case XTAL_LOW_CAP_10P:
508 val = range->xtal_cap10p | 0x08;
510 case XTAL_HIGH_CAP_0P:
511 val = range->xtal_cap0p | 0x00;
514 case XTAL_LOW_CAP_0P:
515 val = range->xtal_cap0p | 0x08;
518 rc = r820t_write_reg_mask(priv, 0x10, val, 0x0b);
522 if (priv->imr_done) {
523 reg08 = priv->imr_data[range->imr_mem].gain_x;
524 reg09 = priv->imr_data[range->imr_mem].phase_y;
529 rc = r820t_write_reg_mask(priv, 0x08, reg08, 0x3f);
533 rc = r820t_write_reg_mask(priv, 0x09, reg09, 0x3f);
538 static int r820t_set_pll(struct r820t_priv *priv, enum v4l2_tuner_type type,
543 unsigned sleep_time = 10000;
544 u32 vco_fra; /* VCO contribution by SDM (kHz) */
545 u32 vco_min = 1770000;
546 u32 vco_max = vco_min * 2;
554 u8 ni, si, nint, vco_fine_tune, val;
557 /* Frequency in kHz */
559 pll_ref = priv->cfg->xtal / 1000;
562 /* Doesn't exist on rtl-sdk, and on field tests, caused troubles */
563 if ((priv->cfg->rafael_chip == CHIP_R620D) ||
564 (priv->cfg->rafael_chip == CHIP_R828D) ||
565 (priv->cfg->rafael_chip == CHIP_R828)) {
566 /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
567 if (type != V4L2_TUNER_DIGITAL_TV) {
573 if (priv->cfg->xtal > 24000000) {
580 rc = r820t_write_reg_mask(priv, 0x10, refdiv2, 0x10);
584 /* set pll autotune = 128kHz */
585 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
589 /* set VCO current = 100 */
590 rc = r820t_write_reg_mask(priv, 0x12, 0x80, 0xe0);
594 /* Calculate divider */
595 while (mix_div <= 64) {
596 if (((freq * mix_div) >= vco_min) &&
597 ((freq * mix_div) < vco_max)) {
599 while (div_buf > 2) {
600 div_buf = div_buf >> 1;
605 mix_div = mix_div << 1;
608 rc = r820t_read(priv, 0x00, data, sizeof(data));
612 vco_fine_tune = (data[4] & 0x30) >> 4;
614 tuner_dbg("mix_div=%d div_num=%d vco_fine_tune=%d\n",
615 mix_div, div_num, vco_fine_tune);
618 * XXX: R828D/16MHz seems to have always vco_fine_tune=1.
619 * Due to that, this calculation goes wrong.
621 if (priv->cfg->rafael_chip != CHIP_R828D) {
622 if (vco_fine_tune > VCO_POWER_REF)
623 div_num = div_num - 1;
624 else if (vco_fine_tune < VCO_POWER_REF)
625 div_num = div_num + 1;
628 rc = r820t_write_reg_mask(priv, 0x10, div_num << 5, 0xe0);
632 vco_freq = freq * mix_div;
633 nint = vco_freq / (2 * pll_ref);
634 vco_fra = vco_freq - 2 * pll_ref * nint;
636 /* boundary spur prevention */
637 if (vco_fra < pll_ref / 64) {
639 } else if (vco_fra > pll_ref * 127 / 64) {
642 } else if ((vco_fra > pll_ref * 127 / 128) && (vco_fra < pll_ref)) {
643 vco_fra = pll_ref * 127 / 128;
644 } else if ((vco_fra > pll_ref) && (vco_fra < pll_ref * 129 / 128)) {
645 vco_fra = pll_ref * 129 / 128;
648 ni = (nint - 13) / 4;
649 si = nint - 4 * ni - 13;
651 rc = r820t_write_reg(priv, 0x14, ni + (si << 6));
661 rc = r820t_write_reg_mask(priv, 0x12, val, 0x08);
666 while (vco_fra > 1) {
667 if (vco_fra > (2 * pll_ref / n_sdm)) {
668 sdm = sdm + 32768 / (n_sdm / 2);
669 vco_fra = vco_fra - 2 * pll_ref / n_sdm;
676 tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
677 freq, pll_ref, refdiv2 ? " / 2" : "", sdm);
679 rc = r820t_write_reg(priv, 0x16, sdm >> 8);
682 rc = r820t_write_reg(priv, 0x15, sdm & 0xff);
686 for (i = 0; i < 2; i++) {
687 usleep_range(sleep_time, sleep_time + 1000);
689 /* Check if PLL has locked */
690 rc = r820t_read(priv, 0x00, data, 3);
697 /* Didn't lock. Increase VCO current */
698 rc = r820t_write_reg_mask(priv, 0x12, 0x60, 0xe0);
704 if (!(data[2] & 0x40)) {
705 priv->has_lock = false;
709 priv->has_lock = true;
710 tuner_dbg("tuner has lock at frequency %d kHz\n", freq);
712 /* set pll autotune = 8kHz */
713 rc = r820t_write_reg_mask(priv, 0x1a, 0x08, 0x08);
718 static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq,
719 enum v4l2_tuner_type type,
724 u8 mixer_top, lna_top, cp_cur, div_buf_cur, lna_vth_l, mixer_vth_l;
725 u8 air_cable1_in, cable2_in, pre_dect, lna_discharge, filter_cur;
727 tuner_dbg("adjusting tuner parameters for the standard\n");
731 if ((freq == 506000000) || (freq == 666000000) ||
732 (freq == 818000000)) {
733 mixer_top = 0x14; /* mixer top:14 , top-1, low-discharge */
734 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
735 cp_cur = 0x28; /* 101, 0.2 */
736 div_buf_cur = 0x20; /* 10, 200u */
738 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
739 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
740 cp_cur = 0x38; /* 111, auto */
741 div_buf_cur = 0x30; /* 11, 150u */
743 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
744 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
745 air_cable1_in = 0x00;
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 = 0x53; /* lna vth 0.84 , vtl 0.64 */
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 */
765 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
766 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
767 lna_vth_l = 0x75; /* lna vth 1.04 , vtl 0.84 */
768 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
769 air_cable1_in = 0x00;
773 cp_cur = 0x38; /* 111, auto */
774 div_buf_cur = 0x30; /* 11, 150u */
775 filter_cur = 0x40; /* 10, low */
777 case SYS_DVBC_ANNEX_A:
778 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
782 air_cable1_in = 0x60;
786 cp_cur = 0x38; /* 111, auto */
787 div_buf_cur = 0x30; /* 11, 150u */
788 filter_cur = 0x40; /* 10, low */
790 default: /* DVB-T 8M */
791 mixer_top = 0x24; /* mixer top:13 , top-1, low-discharge */
792 lna_top = 0xe5; /* detect bw 3, lna top:4, predet top:2 */
793 lna_vth_l = 0x53; /* lna vth 0.84 , vtl 0.64 */
794 mixer_vth_l = 0x75; /* mixer vth 1.04, vtl 0.84 */
795 air_cable1_in = 0x00;
799 cp_cur = 0x38; /* 111, auto */
800 div_buf_cur = 0x30; /* 11, 150u */
801 filter_cur = 0x40; /* 10, low */
805 if (priv->cfg->use_diplexer &&
806 ((priv->cfg->rafael_chip == CHIP_R820T) ||
807 (priv->cfg->rafael_chip == CHIP_R828S) ||
808 (priv->cfg->rafael_chip == CHIP_R820C))) {
810 air_cable1_in = 0x00;
812 air_cable1_in = 0x60;
817 if (priv->cfg->use_predetect) {
818 rc = r820t_write_reg_mask(priv, 0x06, pre_dect, 0x40);
823 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7);
826 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0xf8);
829 rc = r820t_write_reg(priv, 0x0d, lna_vth_l);
832 rc = r820t_write_reg(priv, 0x0e, mixer_vth_l);
836 /* Air-IN only for Astrometa */
837 rc = r820t_write_reg_mask(priv, 0x05, air_cable1_in, 0x60);
840 rc = r820t_write_reg_mask(priv, 0x06, cable2_in, 0x08);
844 rc = r820t_write_reg_mask(priv, 0x11, cp_cur, 0x38);
847 rc = r820t_write_reg_mask(priv, 0x17, div_buf_cur, 0x30);
850 rc = r820t_write_reg_mask(priv, 0x0a, filter_cur, 0x60);
854 * Original driver initializes regs 0x05 and 0x06 with the
855 * same value again on this point. Probably, it is just an
863 tuner_dbg("adjusting LNA parameters\n");
864 if (type != V4L2_TUNER_ANALOG_TV) {
865 /* LNA TOP: lowest */
866 rc = r820t_write_reg_mask(priv, 0x1d, 0, 0x38);
871 rc = r820t_write_reg_mask(priv, 0x1c, 0, 0x04);
875 /* 0: PRE_DECT off */
876 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
881 rc = r820t_write_reg_mask(priv, 0x1a, 0x30, 0x30);
887 /* write LNA TOP = 3 */
888 rc = r820t_write_reg_mask(priv, 0x1d, 0x18, 0x38);
893 * write discharge mode
894 * FIXME: IMHO, the mask here is wrong, but it matches
895 * what's there at the original driver
897 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
901 /* LNA discharge current */
902 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
907 rc = r820t_write_reg_mask(priv, 0x1a, 0x20, 0x30);
912 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
917 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0x38);
922 * write discharge mode
923 * FIXME: IMHO, the mask here is wrong, but it matches
924 * what's there at the original driver
926 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
930 /* LNA discharge current */
931 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
935 /* agc clk 1Khz, external det1 cap 1u */
936 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x30);
940 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x04);
947 static int r820t_set_tv_standard(struct r820t_priv *priv,
949 enum v4l2_tuner_type type,
950 v4l2_std_id std, u32 delsys)
954 u32 if_khz, filt_cal_lo;
956 u8 filt_gain, img_r, filt_q, hp_cor, ext_enable, loop_through;
957 u8 lt_att, flt_ext_widest, polyfil_cur;
958 bool need_calibration;
960 tuner_dbg("selecting the delivery system\n");
962 if (delsys == SYS_ISDBT) {
965 filt_gain = 0x10; /* +3db, 6mhz on */
966 img_r = 0x00; /* image negative */
967 filt_q = 0x10; /* r10[4]:low q(1'b1) */
968 hp_cor = 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
969 ext_enable = 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
970 loop_through = 0x00; /* r5[7], lt on */
971 lt_att = 0x00; /* r31[7], lt att enable */
972 flt_ext_widest = 0x80; /* r15[7]: flt_ext_wide on */
973 polyfil_cur = 0x60; /* r25[6:5]:min */
974 } else if (delsys == SYS_DVBC_ANNEX_A) {
977 filt_gain = 0x10; /* +3db, 6mhz on */
978 img_r = 0x00; /* image negative */
979 filt_q = 0x10; /* r10[4]:low q(1'b1) */
980 hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
981 ext_enable = 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
982 loop_through = 0x00; /* r5[7], lt on */
983 lt_att = 0x00; /* r31[7], lt att enable */
984 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
985 polyfil_cur = 0x60; /* r25[6:5]:min */
986 } else if (delsys == SYS_DVBC_ANNEX_C) {
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 = 0x6a; /* 1.7m disable, +0cap, 1.0mhz */
993 ext_enable = 0x40; /* 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 = 0x80; /* r15[7]: flt_ext_wide on */
997 polyfil_cur = 0x60; /* r25[6:5]:min */
1001 filt_cal_lo = 56000; /* 52000->56000 */
1002 filt_gain = 0x10; /* +3db, 6mhz on */
1003 img_r = 0x00; /* image negative */
1004 filt_q = 0x10; /* r10[4]:low q(1'b1) */
1005 hp_cor = 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
1006 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1007 loop_through = 0x00; /* r5[7], lt on */
1008 lt_att = 0x00; /* r31[7], lt att enable */
1009 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
1010 polyfil_cur = 0x60; /* r25[6:5]:min */
1011 } else if (bw == 7) {
1014 * There are two 7 MHz tables defined on the original
1015 * driver, but just the second one seems to be visible
1016 * by rtl2832. Keep this one here commented, as it
1017 * might be needed in the future
1021 filt_cal_lo = 60000;
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 = 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
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 */
1032 /* 7 MHz, second table */
1034 filt_cal_lo = 63000;
1035 filt_gain = 0x10; /* +3db, 6mhz on */
1036 img_r = 0x00; /* image negative */
1037 filt_q = 0x10; /* r10[4]:low q(1'b1) */
1038 hp_cor = 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
1039 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1040 loop_through = 0x00; /* r5[7], lt on */
1041 lt_att = 0x00; /* r31[7], lt att enable */
1042 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
1043 polyfil_cur = 0x60; /* r25[6:5]:min */
1046 filt_cal_lo = 68500;
1047 filt_gain = 0x10; /* +3db, 6mhz on */
1048 img_r = 0x00; /* image negative */
1049 filt_q = 0x10; /* r10[4]:low q(1'b1) */
1050 hp_cor = 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
1051 ext_enable = 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1052 loop_through = 0x00; /* r5[7], lt on */
1053 lt_att = 0x00; /* r31[7], lt att enable */
1054 flt_ext_widest = 0x00; /* r15[7]: flt_ext_wide off */
1055 polyfil_cur = 0x60; /* r25[6:5]:min */
1059 /* Initialize the shadow registers */
1060 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1062 /* Init Flag & Xtal_check Result */
1064 val = 1 | priv->xtal_cap_sel << 1;
1067 rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f);
1072 rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f);
1076 /* for LT Gain test */
1077 if (type != V4L2_TUNER_ANALOG_TV) {
1078 rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38);
1081 usleep_range(1000, 2000);
1083 priv->int_freq = if_khz * 1000;
1085 /* Check if standard changed. If so, filter calibration is needed */
1086 if (type != priv->type)
1087 need_calibration = true;
1088 else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std))
1089 need_calibration = true;
1090 else if ((type == V4L2_TUNER_DIGITAL_TV) &&
1091 ((delsys != priv->delsys) || bw != priv->bw))
1092 need_calibration = true;
1094 need_calibration = false;
1096 if (need_calibration) {
1097 tuner_dbg("calibrating the tuner\n");
1098 for (i = 0; i < 2; i++) {
1100 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60);
1104 /* set cali clk =on */
1105 rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04);
1109 /* X'tal cap 0pF for PLL */
1110 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03);
1114 rc = r820t_set_pll(priv, type, filt_cal_lo * 1000);
1115 if (rc < 0 || !priv->has_lock)
1119 rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10);
1123 usleep_range(1000, 2000);
1126 rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10);
1130 /* set cali clk =off */
1131 rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04);
1135 /* Check if calibration worked */
1136 rc = r820t_read(priv, 0x00, data, sizeof(data));
1140 priv->fil_cal_code = data[4] & 0x0f;
1141 if (priv->fil_cal_code && priv->fil_cal_code != 0x0f)
1145 if (priv->fil_cal_code == 0x0f)
1146 priv->fil_cal_code = 0;
1149 rc = r820t_write_reg_mask(priv, 0x0a,
1150 filt_q | priv->fil_cal_code, 0x1f);
1154 /* Set BW, Filter_gain, & HP corner */
1155 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0xef);
1161 rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80);
1165 /* Set filt_3dB, V6MHz */
1166 rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30);
1170 /* channel filter extension */
1171 rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60);
1176 rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80);
1180 /* Loop through attenuation */
1181 rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80);
1185 /* filter extension widest */
1186 rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80);
1190 /* RF poly filter current */
1191 rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60);
1195 /* Store current standard. If it changes, re-calibrate the tuner */
1196 priv->delsys = delsys;
1204 static int r820t_read_gain(struct r820t_priv *priv)
1209 rc = r820t_read(priv, 0x00, data, sizeof(data));
1213 return ((data[3] & 0x08) << 1) + ((data[3] & 0xf0) >> 4);
1217 /* FIXME: This routine requires more testing */
1220 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
1221 * input power, for raw results see:
1222 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
1225 static const int r820t_lna_gain_steps[] = {
1226 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
1229 static const int r820t_mixer_gain_steps[] = {
1230 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
1233 static int r820t_set_gain_mode(struct r820t_priv *priv,
1234 bool set_manual_gain,
1239 if (set_manual_gain) {
1240 int i, total_gain = 0;
1241 uint8_t mix_index = 0, lna_index = 0;
1245 rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10);
1249 /* Mixer auto off */
1250 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
1254 rc = r820t_read(priv, 0x00, data, sizeof(data));
1258 /* set fixed VGA gain for now (16.3 dB) */
1259 rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f);
1263 for (i = 0; i < 15; i++) {
1264 if (total_gain >= gain)
1267 total_gain += r820t_lna_gain_steps[++lna_index];
1269 if (total_gain >= gain)
1272 total_gain += r820t_mixer_gain_steps[++mix_index];
1276 rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f);
1280 /* set Mixer gain */
1281 rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f);
1286 rc = r820t_write_reg_mask(priv, 0x05, 0, 0x10);
1291 rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0x10);
1295 /* set fixed VGA gain for now (26.5 dB) */
1296 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
1305 static int generic_set_freq(struct dvb_frontend *fe,
1306 u32 freq /* in HZ */,
1308 enum v4l2_tuner_type type,
1309 v4l2_std_id std, u32 delsys)
1311 struct r820t_priv *priv = fe->tuner_priv;
1315 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1318 rc = r820t_set_tv_standard(priv, bw, type, std, delsys);
1322 if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC))
1323 lo_freq = freq - priv->int_freq;
1325 lo_freq = freq + priv->int_freq;
1327 rc = r820t_set_mux(priv, lo_freq);
1331 rc = r820t_set_pll(priv, type, lo_freq);
1332 if (rc < 0 || !priv->has_lock)
1335 rc = r820t_sysfreq_sel(priv, freq, type, std, delsys);
1339 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1340 __func__, freq, r820t_read_gain(priv));
1345 tuner_dbg("%s: failed=%d\n", __func__, rc);
1350 * r820t standby logic
1353 static int r820t_standby(struct r820t_priv *priv)
1357 /* If device was not initialized yet, don't need to standby */
1358 if (!priv->init_done)
1361 rc = r820t_write_reg(priv, 0x06, 0xb1);
1364 rc = r820t_write_reg(priv, 0x05, 0x03);
1367 rc = r820t_write_reg(priv, 0x07, 0x3a);
1370 rc = r820t_write_reg(priv, 0x08, 0x40);
1373 rc = r820t_write_reg(priv, 0x09, 0xc0);
1376 rc = r820t_write_reg(priv, 0x0a, 0x36);
1379 rc = r820t_write_reg(priv, 0x0c, 0x35);
1382 rc = r820t_write_reg(priv, 0x0f, 0x68);
1385 rc = r820t_write_reg(priv, 0x11, 0x03);
1388 rc = r820t_write_reg(priv, 0x17, 0xf4);
1391 rc = r820t_write_reg(priv, 0x19, 0x0c);
1393 /* Force initial calibration */
1400 * r820t device init logic
1403 static int r820t_xtal_check(struct r820t_priv *priv)
1408 /* Initialize the shadow registers */
1409 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1411 /* cap 30pF & Drive Low */
1412 rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b);
1416 /* set pll autotune = 128kHz */
1417 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
1421 /* set manual initial reg = 111111; */
1422 rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f);
1427 rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40);
1431 /* Try several xtal capacitor alternatives */
1432 for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) {
1433 rc = r820t_write_reg_mask(priv, 0x10,
1434 r820t_xtal_capacitor[i][0], 0x1b);
1438 usleep_range(5000, 6000);
1440 rc = r820t_read(priv, 0x00, data, sizeof(data));
1443 if (!(data[2] & 0x40))
1446 val = data[2] & 0x3f;
1448 if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23))
1455 if (i == ARRAY_SIZE(r820t_xtal_capacitor))
1458 return r820t_xtal_capacitor[i][1];
1461 static int r820t_imr_prepare(struct r820t_priv *priv)
1465 /* Initialize the shadow registers */
1466 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1468 /* lna off (air-in off) */
1469 rc = r820t_write_reg_mask(priv, 0x05, 0x20, 0x20);
1473 /* mixer gain mode = manual */
1474 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
1478 /* filter corner = lowest */
1479 rc = r820t_write_reg_mask(priv, 0x0a, 0x0f, 0x0f);
1483 /* filter bw=+2cap, hp=5M */
1484 rc = r820t_write_reg_mask(priv, 0x0b, 0x60, 0x6f);
1488 /* adc=on, vga code mode, gain = 26.5dB */
1489 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
1494 rc = r820t_write_reg_mask(priv, 0x0f, 0, 0x08);
1498 /* ring power = on */
1499 rc = r820t_write_reg_mask(priv, 0x18, 0x10, 0x10);
1503 /* from ring = ring pll in */
1504 rc = r820t_write_reg_mask(priv, 0x1c, 0x02, 0x02);
1508 /* sw_pdect = det3 */
1509 rc = r820t_write_reg_mask(priv, 0x1e, 0x80, 0x80);
1514 rc = r820t_write_reg_mask(priv, 0x06, 0x20, 0x20);
1519 static int r820t_multi_read(struct r820t_priv *priv)
1523 u8 data[2], min = 255, max = 0;
1525 usleep_range(5000, 6000);
1527 for (i = 0; i < 6; i++) {
1528 rc = r820t_read(priv, 0x00, data, sizeof(data));
1540 rc = sum - max - min;
1545 static int r820t_imr_cross(struct r820t_priv *priv,
1546 struct r820t_sect_type iq_point[3],
1549 struct r820t_sect_type cross[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
1550 struct r820t_sect_type tmp;
1554 reg08 = r820t_read_cache_reg(priv, 8) & 0xc0;
1555 reg09 = r820t_read_cache_reg(priv, 9) & 0xc0;
1561 for (i = 0; i < 5; i++) {
1564 cross[i].gain_x = reg08;
1565 cross[i].phase_y = reg09;
1568 cross[i].gain_x = reg08; /* 0 */
1569 cross[i].phase_y = reg09 + 1; /* Q-1 */
1572 cross[i].gain_x = reg08; /* 0 */
1573 cross[i].phase_y = (reg09 | 0x20) + 1; /* I-1 */
1576 cross[i].gain_x = reg08 + 1; /* Q-1 */
1577 cross[i].phase_y = reg09;
1580 cross[i].gain_x = (reg08 | 0x20) + 1; /* I-1 */
1581 cross[i].phase_y = reg09;
1584 rc = r820t_write_reg(priv, 0x08, cross[i].gain_x);
1588 rc = r820t_write_reg(priv, 0x09, cross[i].phase_y);
1592 rc = r820t_multi_read(priv);
1596 cross[i].value = rc;
1598 if (cross[i].value < tmp.value)
1602 if ((tmp.phase_y & 0x1f) == 1) { /* y-direction */
1605 iq_point[0] = cross[0];
1606 iq_point[1] = cross[1];
1607 iq_point[2] = cross[2];
1608 } else { /* (0,0) or x-direction */
1611 iq_point[0] = cross[0];
1612 iq_point[1] = cross[3];
1613 iq_point[2] = cross[4];
1618 static void r820t_compre_cor(struct r820t_sect_type iq[3])
1622 for (i = 3; i > 0; i--) {
1623 if (iq[0].value > iq[i - 1].value)
1624 swap(iq[0], iq[i - 1]);
1628 static int r820t_compre_step(struct r820t_priv *priv,
1629 struct r820t_sect_type iq[3], u8 reg)
1632 struct r820t_sect_type tmp;
1635 * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
1637 * new < min => update to min and continue
1641 /* min value already saved in iq[0] */
1642 tmp.phase_y = iq[0].phase_y;
1643 tmp.gain_x = iq[0].gain_x;
1645 while (((tmp.gain_x & 0x1f) < IMR_TRIAL) &&
1646 ((tmp.phase_y & 0x1f) < IMR_TRIAL)) {
1652 rc = r820t_write_reg(priv, 0x08, tmp.gain_x);
1656 rc = r820t_write_reg(priv, 0x09, tmp.phase_y);
1660 rc = r820t_multi_read(priv);
1665 if (tmp.value <= iq[0].value) {
1666 iq[0].gain_x = tmp.gain_x;
1667 iq[0].phase_y = tmp.phase_y;
1668 iq[0].value = tmp.value;
1678 static int r820t_iq_tree(struct r820t_priv *priv,
1679 struct r820t_sect_type iq[3],
1680 u8 fix_val, u8 var_val, u8 fix_reg)
1686 * record IMC results by input gain/phase location then adjust
1687 * gain or phase positive 1 step and negative 1 step,
1688 * both record results
1691 if (fix_reg == 0x08)
1696 for (i = 0; i < 3; i++) {
1697 rc = r820t_write_reg(priv, fix_reg, fix_val);
1701 rc = r820t_write_reg(priv, var_reg, var_val);
1705 rc = r820t_multi_read(priv);
1710 if (fix_reg == 0x08) {
1711 iq[i].gain_x = fix_val;
1712 iq[i].phase_y = var_val;
1714 iq[i].phase_y = fix_val;
1715 iq[i].gain_x = var_val;
1718 if (i == 0) { /* try right-side point */
1720 } else if (i == 1) { /* try left-side point */
1721 /* if absolute location is 1, change I/Q direction */
1722 if ((var_val & 0x1f) < 0x02) {
1723 tmp = 2 - (var_val & 0x1f);
1725 /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
1726 if (var_val & 0x20) {
1730 var_val |= 0x20 | tmp;
1741 static int r820t_section(struct r820t_priv *priv,
1742 struct r820t_sect_type *iq_point)
1745 struct r820t_sect_type compare_iq[3], compare_bet[3];
1747 /* Try X-1 column and save min result to compare_bet[0] */
1748 if (!(iq_point->gain_x & 0x1f))
1749 compare_iq[0].gain_x = ((iq_point->gain_x) & 0xdf) + 1; /* Q-path, Gain=1 */
1751 compare_iq[0].gain_x = iq_point->gain_x - 1; /* left point */
1752 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[0] = compare_iq[0];
1764 /* Try X column and save min result to compare_bet[1] */
1765 compare_iq[0].gain_x = iq_point->gain_x;
1766 compare_iq[0].phase_y = iq_point->phase_y;
1768 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1769 compare_iq[0].phase_y, 0x08);
1773 r820t_compre_cor(compare_iq);
1775 compare_bet[1] = compare_iq[0];
1777 /* Try X+1 column and save min result to compare_bet[2] */
1778 if ((iq_point->gain_x & 0x1f) == 0x00)
1779 compare_iq[0].gain_x = ((iq_point->gain_x) | 0x20) + 1; /* I-path, Gain=1 */
1781 compare_iq[0].gain_x = iq_point->gain_x + 1;
1782 compare_iq[0].phase_y = iq_point->phase_y;
1784 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1785 compare_iq[0].phase_y, 0x08);
1789 r820t_compre_cor(compare_iq);
1791 compare_bet[2] = compare_iq[0];
1793 r820t_compre_cor(compare_bet);
1795 *iq_point = compare_bet[0];
1800 static int r820t_vga_adjust(struct r820t_priv *priv)
1805 /* increase vga power to let image significant */
1806 for (vga_count = 12; vga_count < 16; vga_count++) {
1807 rc = r820t_write_reg_mask(priv, 0x0c, vga_count, 0x0f);
1811 usleep_range(10000, 11000);
1813 rc = r820t_multi_read(priv);
1824 static int r820t_iq(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
1826 struct r820t_sect_type compare_iq[3];
1828 u8 x_direction = 0; /* 1:x, 0:y */
1829 u8 dir_reg, other_reg;
1831 r820t_vga_adjust(priv);
1833 rc = r820t_imr_cross(priv, compare_iq, &x_direction);
1837 if (x_direction == 1) {
1845 /* compare and find min of 3 points. determine i/q direction */
1846 r820t_compre_cor(compare_iq);
1848 /* increase step to find min value of this direction */
1849 rc = r820t_compre_step(priv, compare_iq, dir_reg);
1853 /* the other direction */
1854 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1855 compare_iq[0].phase_y, dir_reg);
1859 /* compare and find min of 3 points. determine i/q direction */
1860 r820t_compre_cor(compare_iq);
1862 /* increase step to find min value on this direction */
1863 rc = r820t_compre_step(priv, compare_iq, other_reg);
1867 /* check 3 points again */
1868 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1869 compare_iq[0].phase_y, other_reg);
1873 r820t_compre_cor(compare_iq);
1875 /* section-9 check */
1876 rc = r820t_section(priv, compare_iq);
1878 *iq_pont = compare_iq[0];
1880 /* reset gain/phase control setting */
1881 rc = r820t_write_reg_mask(priv, 0x08, 0, 0x3f);
1885 rc = r820t_write_reg_mask(priv, 0x09, 0, 0x3f);
1890 static int r820t_f_imr(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
1894 r820t_vga_adjust(priv);
1897 * search surrounding points from previous point
1898 * try (x-1), (x), (x+1) columns, and find min IMR result point
1900 rc = r820t_section(priv, iq_pont);
1907 static int r820t_imr(struct r820t_priv *priv, unsigned imr_mem, bool im_flag)
1909 struct r820t_sect_type imr_point;
1911 u32 ring_vco, ring_freq, ring_ref;
1913 int reg18, reg19, reg1f;
1915 if (priv->cfg->xtal > 24000000)
1916 ring_ref = priv->cfg->xtal / 2000;
1918 ring_ref = priv->cfg->xtal / 1000;
1921 for (n = 0; n < 16; n++) {
1922 if ((16 + n) * 8 * ring_ref >= 3100000) {
1928 reg18 = r820t_read_cache_reg(priv, 0x18);
1929 reg19 = r820t_read_cache_reg(priv, 0x19);
1930 reg1f = r820t_read_cache_reg(priv, 0x1f);
1932 reg18 &= 0xf0; /* set ring[3:0] */
1935 ring_vco = (16 + n_ring) * 8 * ring_ref;
1937 reg18 &= 0xdf; /* clear ring_se23 */
1938 reg19 &= 0xfc; /* clear ring_seldiv */
1939 reg1f &= 0xfc; /* clear ring_att */
1943 ring_freq = ring_vco / 48;
1944 reg18 |= 0x20; /* ring_se23 = 1 */
1945 reg19 |= 0x03; /* ring_seldiv = 3 */
1946 reg1f |= 0x02; /* ring_att 10 */
1949 ring_freq = ring_vco / 16;
1950 reg18 |= 0x00; /* ring_se23 = 0 */
1951 reg19 |= 0x02; /* ring_seldiv = 2 */
1952 reg1f |= 0x00; /* pw_ring 00 */
1955 ring_freq = ring_vco / 8;
1956 reg18 |= 0x00; /* ring_se23 = 0 */
1957 reg19 |= 0x01; /* ring_seldiv = 1 */
1958 reg1f |= 0x03; /* pw_ring 11 */
1961 ring_freq = ring_vco / 6;
1962 reg18 |= 0x20; /* ring_se23 = 1 */
1963 reg19 |= 0x00; /* ring_seldiv = 0 */
1964 reg1f |= 0x03; /* pw_ring 11 */
1967 ring_freq = ring_vco / 4;
1968 reg18 |= 0x00; /* ring_se23 = 0 */
1969 reg19 |= 0x00; /* ring_seldiv = 0 */
1970 reg1f |= 0x01; /* pw_ring 01 */
1973 ring_freq = ring_vco / 4;
1974 reg18 |= 0x00; /* ring_se23 = 0 */
1975 reg19 |= 0x00; /* ring_seldiv = 0 */
1976 reg1f |= 0x01; /* pw_ring 01 */
1981 /* write pw_ring, n_ring, ringdiv2 registers */
1983 /* n_ring, ring_se23 */
1984 rc = r820t_write_reg(priv, 0x18, reg18);
1989 rc = r820t_write_reg(priv, 0x19, reg19);
1994 rc = r820t_write_reg(priv, 0x1f, reg1f);
1998 /* mux input freq ~ rf_in freq */
1999 rc = r820t_set_mux(priv, (ring_freq - 5300) * 1000);
2003 rc = r820t_set_pll(priv, V4L2_TUNER_DIGITAL_TV,
2004 (ring_freq - 5300) * 1000);
2005 if (!priv->has_lock)
2011 rc = r820t_iq(priv, &imr_point);
2013 imr_point.gain_x = priv->imr_data[3].gain_x;
2014 imr_point.phase_y = priv->imr_data[3].phase_y;
2015 imr_point.value = priv->imr_data[3].value;
2017 rc = r820t_f_imr(priv, &imr_point);
2022 /* save IMR value */
2025 priv->imr_data[0].gain_x = imr_point.gain_x;
2026 priv->imr_data[0].phase_y = imr_point.phase_y;
2027 priv->imr_data[0].value = imr_point.value;
2030 priv->imr_data[1].gain_x = imr_point.gain_x;
2031 priv->imr_data[1].phase_y = imr_point.phase_y;
2032 priv->imr_data[1].value = imr_point.value;
2035 priv->imr_data[2].gain_x = imr_point.gain_x;
2036 priv->imr_data[2].phase_y = imr_point.phase_y;
2037 priv->imr_data[2].value = imr_point.value;
2040 priv->imr_data[3].gain_x = imr_point.gain_x;
2041 priv->imr_data[3].phase_y = imr_point.phase_y;
2042 priv->imr_data[3].value = imr_point.value;
2045 priv->imr_data[4].gain_x = imr_point.gain_x;
2046 priv->imr_data[4].phase_y = imr_point.phase_y;
2047 priv->imr_data[4].value = imr_point.value;
2050 priv->imr_data[4].gain_x = imr_point.gain_x;
2051 priv->imr_data[4].phase_y = imr_point.phase_y;
2052 priv->imr_data[4].value = imr_point.value;
2059 static int r820t_imr_callibrate(struct r820t_priv *priv)
2064 if (priv->init_done)
2067 /* Detect Xtal capacitance */
2068 if ((priv->cfg->rafael_chip == CHIP_R820T) ||
2069 (priv->cfg->rafael_chip == CHIP_R828S) ||
2070 (priv->cfg->rafael_chip == CHIP_R820C)) {
2071 priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;
2073 /* Initialize registers */
2074 rc = r820t_write(priv, 0x05,
2075 r820t_init_array, sizeof(r820t_init_array));
2078 for (i = 0; i < 3; i++) {
2079 rc = r820t_xtal_check(priv);
2082 if (!i || rc > xtal_cap)
2085 priv->xtal_cap_sel = xtal_cap;
2089 * Disables IMR calibration. That emulates the same behaviour
2090 * as what is done by rtl-sdr userspace library. Useful for testing
2093 priv->init_done = true;
2098 /* Initialize registers */
2099 rc = r820t_write(priv, 0x05,
2100 r820t_init_array, sizeof(r820t_init_array));
2104 rc = r820t_imr_prepare(priv);
2108 rc = r820t_imr(priv, 3, true);
2111 rc = r820t_imr(priv, 1, false);
2114 rc = r820t_imr(priv, 0, false);
2117 rc = r820t_imr(priv, 2, false);
2120 rc = r820t_imr(priv, 4, false);
2124 priv->init_done = true;
2125 priv->imr_done = true;
2131 /* Not used, for now */
2132 static int r820t_gpio(struct r820t_priv *priv, bool enable)
2134 return r820t_write_reg_mask(priv, 0x0f, enable ? 1 : 0, 0x01);
2139 * r820t frontend operations and tuner attach code
2141 * All driver locks and i2c control are only in this part of the code
2144 static int r820t_init(struct dvb_frontend *fe)
2146 struct r820t_priv *priv = fe->tuner_priv;
2149 tuner_dbg("%s:\n", __func__);
2151 mutex_lock(&priv->lock);
2152 if (fe->ops.i2c_gate_ctrl)
2153 fe->ops.i2c_gate_ctrl(fe, 1);
2155 rc = r820t_imr_callibrate(priv);
2159 /* Initialize registers */
2160 rc = r820t_write(priv, 0x05,
2161 r820t_init_array, sizeof(r820t_init_array));
2164 if (fe->ops.i2c_gate_ctrl)
2165 fe->ops.i2c_gate_ctrl(fe, 0);
2166 mutex_unlock(&priv->lock);
2169 tuner_dbg("%s: failed=%d\n", __func__, rc);
2173 static int r820t_sleep(struct dvb_frontend *fe)
2175 struct r820t_priv *priv = fe->tuner_priv;
2178 tuner_dbg("%s:\n", __func__);
2180 mutex_lock(&priv->lock);
2181 if (fe->ops.i2c_gate_ctrl)
2182 fe->ops.i2c_gate_ctrl(fe, 1);
2184 rc = r820t_standby(priv);
2186 if (fe->ops.i2c_gate_ctrl)
2187 fe->ops.i2c_gate_ctrl(fe, 0);
2188 mutex_unlock(&priv->lock);
2190 tuner_dbg("%s: failed=%d\n", __func__, rc);
2194 static int r820t_set_analog_freq(struct dvb_frontend *fe,
2195 struct analog_parameters *p)
2197 struct r820t_priv *priv = fe->tuner_priv;
2201 tuner_dbg("%s called\n", __func__);
2203 /* if std is not defined, choose one */
2205 p->std = V4L2_STD_MN;
2207 if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC))
2212 mutex_lock(&priv->lock);
2213 if (fe->ops.i2c_gate_ctrl)
2214 fe->ops.i2c_gate_ctrl(fe, 1);
2216 rc = generic_set_freq(fe, 62500l * p->frequency, bw,
2217 V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED);
2219 if (fe->ops.i2c_gate_ctrl)
2220 fe->ops.i2c_gate_ctrl(fe, 0);
2221 mutex_unlock(&priv->lock);
2226 static int r820t_set_params(struct dvb_frontend *fe)
2228 struct r820t_priv *priv = fe->tuner_priv;
2229 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2233 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2234 __func__, c->delivery_system, c->frequency, c->bandwidth_hz);
2236 mutex_lock(&priv->lock);
2237 if (fe->ops.i2c_gate_ctrl)
2238 fe->ops.i2c_gate_ctrl(fe, 1);
2240 bw = (c->bandwidth_hz + 500000) / 1000000;
2244 rc = generic_set_freq(fe, c->frequency, bw,
2245 V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system);
2247 if (fe->ops.i2c_gate_ctrl)
2248 fe->ops.i2c_gate_ctrl(fe, 0);
2249 mutex_unlock(&priv->lock);
2252 tuner_dbg("%s: failed=%d\n", __func__, rc);
2256 static int r820t_signal(struct dvb_frontend *fe, u16 *strength)
2258 struct r820t_priv *priv = fe->tuner_priv;
2261 mutex_lock(&priv->lock);
2262 if (fe->ops.i2c_gate_ctrl)
2263 fe->ops.i2c_gate_ctrl(fe, 1);
2265 if (priv->has_lock) {
2266 rc = r820t_read_gain(priv);
2270 /* A higher gain at LNA means a lower signal strength */
2271 *strength = (45 - rc) << 4 | 0xff;
2272 if (*strength == 0xff)
2279 if (fe->ops.i2c_gate_ctrl)
2280 fe->ops.i2c_gate_ctrl(fe, 0);
2281 mutex_unlock(&priv->lock);
2283 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2285 priv->has_lock ? "PLL locked" : "no signal",
2291 static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
2293 struct r820t_priv *priv = fe->tuner_priv;
2295 tuner_dbg("%s:\n", __func__);
2297 *frequency = priv->int_freq;
2302 static void r820t_release(struct dvb_frontend *fe)
2304 struct r820t_priv *priv = fe->tuner_priv;
2306 tuner_dbg("%s:\n", __func__);
2308 mutex_lock(&r820t_list_mutex);
2311 hybrid_tuner_release_state(priv);
2313 mutex_unlock(&r820t_list_mutex);
2315 fe->tuner_priv = NULL;
2318 static const struct dvb_tuner_ops r820t_tuner_ops = {
2320 .name = "Rafael Micro R820T",
2321 .frequency_min_hz = 42 * MHz,
2322 .frequency_max_hz = 1002 * MHz,
2325 .release = r820t_release,
2326 .sleep = r820t_sleep,
2327 .set_params = r820t_set_params,
2328 .set_analog_params = r820t_set_analog_freq,
2329 .get_if_frequency = r820t_get_if_frequency,
2330 .get_rf_strength = r820t_signal,
2333 struct dvb_frontend *r820t_attach(struct dvb_frontend *fe,
2334 struct i2c_adapter *i2c,
2335 const struct r820t_config *cfg)
2337 struct r820t_priv *priv;
2342 mutex_lock(&r820t_list_mutex);
2344 instance = hybrid_tuner_request_state(struct r820t_priv, priv,
2345 hybrid_tuner_instance_list,
2350 /* memory allocation failure */
2353 /* new tuner instance */
2356 mutex_init(&priv->lock);
2358 fe->tuner_priv = priv;
2361 /* existing tuner instance */
2362 fe->tuner_priv = priv;
2366 if (fe->ops.i2c_gate_ctrl)
2367 fe->ops.i2c_gate_ctrl(fe, 1);
2369 /* check if the tuner is there */
2370 rc = r820t_read(priv, 0x00, data, sizeof(data));
2374 rc = r820t_sleep(fe);
2379 "Rafael Micro r820t successfully identified, chip type: %s\n",
2380 r820t_chip_enum_to_str(cfg->rafael_chip));
2382 if (fe->ops.i2c_gate_ctrl)
2383 fe->ops.i2c_gate_ctrl(fe, 0);
2385 mutex_unlock(&r820t_list_mutex);
2387 memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops,
2388 sizeof(struct dvb_tuner_ops));
2392 if (fe->ops.i2c_gate_ctrl)
2393 fe->ops.i2c_gate_ctrl(fe, 0);
2396 mutex_unlock(&r820t_list_mutex);
2398 pr_info("%s: failed=%d\n", __func__, rc);
2402 EXPORT_SYMBOL_GPL(r820t_attach);
2404 MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2405 MODULE_AUTHOR("Mauro Carvalho Chehab");
2406 MODULE_LICENSE("GPL v2");