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
},
330 * I2C read/write code and shadow registers logic
332 static void shadow_store(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
335 int r
= reg
- REG_SHADOW_START
;
343 if (len
> NUM_REGS
- r
)
346 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
347 __func__
, r
+ REG_SHADOW_START
, len
, len
, val
);
349 memcpy(&priv
->regs
[r
], val
, len
);
352 static int r820t_write(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
355 int rc
, size
, pos
= 0;
357 /* Store the shadow registers */
358 shadow_store(priv
, reg
, val
, len
);
361 if (len
> priv
->cfg
->max_i2c_msg_len
- 1)
362 size
= priv
->cfg
->max_i2c_msg_len
- 1;
366 /* Fill I2C buffer */
368 memcpy(&priv
->buf
[1], &val
[pos
], size
);
370 rc
= tuner_i2c_xfer_send(&priv
->i2c_props
, priv
->buf
, size
+ 1);
371 if (rc
!= size
+ 1) {
372 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
373 __func__
, rc
, reg
, size
, size
, &priv
->buf
[1]);
378 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
379 __func__
, reg
, size
, size
, &priv
->buf
[1]);
389 static inline int r820t_write_reg(struct r820t_priv
*priv
, u8 reg
, u8 val
)
391 u8 tmp
= val
; /* work around GCC PR81715 with asan-stack=1 */
393 return r820t_write(priv
, reg
, &tmp
, 1);
396 static int r820t_read_cache_reg(struct r820t_priv
*priv
, int reg
)
398 reg
-= REG_SHADOW_START
;
400 if (reg
>= 0 && reg
< NUM_REGS
)
401 return priv
->regs
[reg
];
406 static inline int r820t_write_reg_mask(struct r820t_priv
*priv
, u8 reg
, u8 val
,
410 int rc
= r820t_read_cache_reg(priv
, reg
);
415 tmp
= (rc
& ~bit_mask
) | (tmp
& bit_mask
);
417 return r820t_write(priv
, reg
, &tmp
, 1);
420 static int r820t_read(struct r820t_priv
*priv
, u8 reg
, u8
*val
, int len
)
423 u8
*p
= &priv
->buf
[1];
427 rc
= tuner_i2c_xfer_send_recv(&priv
->i2c_props
, priv
->buf
, 1, p
, len
);
429 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
430 __func__
, rc
, reg
, len
, len
, p
);
436 /* Copy data to the output buffer */
437 for (i
= 0; i
< len
; i
++)
438 val
[i
] = bitrev8(p
[i
]);
440 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
441 __func__
, reg
, len
, len
, val
);
450 static int r820t_set_mux(struct r820t_priv
*priv
, u32 freq
)
452 const struct r820t_freq_range
*range
;
454 u8 val
, reg08
, reg09
;
456 /* Get the proper frequency range */
457 freq
= freq
/ 1000000;
458 for (i
= 0; i
< ARRAY_SIZE(freq_ranges
) - 1; i
++) {
459 if (freq
< freq_ranges
[i
+ 1].freq
)
462 range
= &freq_ranges
[i
];
464 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i
, freq
);
467 rc
= r820t_write_reg_mask(priv
, 0x17, range
->open_d
, 0x08);
472 rc
= r820t_write_reg_mask(priv
, 0x1a, range
->rf_mux_ploy
, 0xc3);
477 rc
= r820t_write_reg(priv
, 0x1b, range
->tf_c
);
481 /* XTAL CAP & Drive */
482 switch (priv
->xtal_cap_sel
) {
483 case XTAL_LOW_CAP_30P
:
484 case XTAL_LOW_CAP_20P
:
485 val
= range
->xtal_cap20p
| 0x08;
487 case XTAL_LOW_CAP_10P
:
488 val
= range
->xtal_cap10p
| 0x08;
490 case XTAL_HIGH_CAP_0P
:
491 val
= range
->xtal_cap0p
| 0x00;
494 case XTAL_LOW_CAP_0P
:
495 val
= range
->xtal_cap0p
| 0x08;
498 rc
= r820t_write_reg_mask(priv
, 0x10, val
, 0x0b);
502 if (priv
->imr_done
) {
503 reg08
= priv
->imr_data
[range
->imr_mem
].gain_x
;
504 reg09
= priv
->imr_data
[range
->imr_mem
].phase_y
;
509 rc
= r820t_write_reg_mask(priv
, 0x08, reg08
, 0x3f);
513 rc
= r820t_write_reg_mask(priv
, 0x09, reg09
, 0x3f);
518 static int r820t_set_pll(struct r820t_priv
*priv
, enum v4l2_tuner_type type
,
523 unsigned sleep_time
= 10000;
524 u32 vco_fra
; /* VCO contribution by SDM (kHz) */
525 u32 vco_min
= 1770000;
526 u32 vco_max
= vco_min
* 2;
534 u8 ni
, si
, nint
, vco_fine_tune
, val
;
537 /* Frequency in kHz */
539 pll_ref
= priv
->cfg
->xtal
/ 1000;
542 /* Doesn't exist on rtl-sdk, and on field tests, caused troubles */
543 if ((priv
->cfg
->rafael_chip
== CHIP_R620D
) ||
544 (priv
->cfg
->rafael_chip
== CHIP_R828D
) ||
545 (priv
->cfg
->rafael_chip
== CHIP_R828
)) {
546 /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
547 if (type
!= V4L2_TUNER_DIGITAL_TV
) {
553 if (priv
->cfg
->xtal
> 24000000) {
560 rc
= r820t_write_reg_mask(priv
, 0x10, refdiv2
, 0x10);
564 /* set pll autotune = 128kHz */
565 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
569 /* set VCO current = 100 */
570 rc
= r820t_write_reg_mask(priv
, 0x12, 0x80, 0xe0);
574 /* Calculate divider */
575 while (mix_div
<= 64) {
576 if (((freq
* mix_div
) >= vco_min
) &&
577 ((freq
* mix_div
) < vco_max
)) {
579 while (div_buf
> 2) {
580 div_buf
= div_buf
>> 1;
585 mix_div
= mix_div
<< 1;
588 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
592 vco_fine_tune
= (data
[4] & 0x30) >> 4;
594 tuner_dbg("mix_div=%d div_num=%d vco_fine_tune=%d\n",
595 mix_div
, div_num
, vco_fine_tune
);
598 * XXX: R828D/16MHz seems to have always vco_fine_tune=1.
599 * Due to that, this calculation goes wrong.
601 if (priv
->cfg
->rafael_chip
!= CHIP_R828D
) {
602 if (vco_fine_tune
> VCO_POWER_REF
)
603 div_num
= div_num
- 1;
604 else if (vco_fine_tune
< VCO_POWER_REF
)
605 div_num
= div_num
+ 1;
608 rc
= r820t_write_reg_mask(priv
, 0x10, div_num
<< 5, 0xe0);
612 vco_freq
= freq
* mix_div
;
613 nint
= vco_freq
/ (2 * pll_ref
);
614 vco_fra
= vco_freq
- 2 * pll_ref
* nint
;
616 /* boundary spur prevention */
617 if (vco_fra
< pll_ref
/ 64) {
619 } else if (vco_fra
> pll_ref
* 127 / 64) {
622 } else if ((vco_fra
> pll_ref
* 127 / 128) && (vco_fra
< pll_ref
)) {
623 vco_fra
= pll_ref
* 127 / 128;
624 } else if ((vco_fra
> pll_ref
) && (vco_fra
< pll_ref
* 129 / 128)) {
625 vco_fra
= pll_ref
* 129 / 128;
628 ni
= (nint
- 13) / 4;
629 si
= nint
- 4 * ni
- 13;
631 rc
= r820t_write_reg(priv
, 0x14, ni
+ (si
<< 6));
641 rc
= r820t_write_reg_mask(priv
, 0x12, val
, 0x08);
646 while (vco_fra
> 1) {
647 if (vco_fra
> (2 * pll_ref
/ n_sdm
)) {
648 sdm
= sdm
+ 32768 / (n_sdm
/ 2);
649 vco_fra
= vco_fra
- 2 * pll_ref
/ n_sdm
;
656 tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
657 freq
, pll_ref
, refdiv2
? " / 2" : "", sdm
);
659 rc
= r820t_write_reg(priv
, 0x16, sdm
>> 8);
662 rc
= r820t_write_reg(priv
, 0x15, sdm
& 0xff);
666 for (i
= 0; i
< 2; i
++) {
667 usleep_range(sleep_time
, sleep_time
+ 1000);
669 /* Check if PLL has locked */
670 rc
= r820t_read(priv
, 0x00, data
, 3);
677 /* Didn't lock. Increase VCO current */
678 rc
= r820t_write_reg_mask(priv
, 0x12, 0x60, 0xe0);
684 if (!(data
[2] & 0x40)) {
685 priv
->has_lock
= false;
689 priv
->has_lock
= true;
690 tuner_dbg("tuner has lock at frequency %d kHz\n", freq
);
692 /* set pll autotune = 8kHz */
693 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x08, 0x08);
698 static int r820t_sysfreq_sel(struct r820t_priv
*priv
, u32 freq
,
699 enum v4l2_tuner_type type
,
704 u8 mixer_top
, lna_top
, cp_cur
, div_buf_cur
, lna_vth_l
, mixer_vth_l
;
705 u8 air_cable1_in
, cable2_in
, pre_dect
, lna_discharge
, filter_cur
;
707 tuner_dbg("adjusting tuner parameters for the standard\n");
711 if ((freq
== 506000000) || (freq
== 666000000) ||
712 (freq
== 818000000)) {
713 mixer_top
= 0x14; /* mixer top:14 , top-1, low-discharge */
714 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
715 cp_cur
= 0x28; /* 101, 0.2 */
716 div_buf_cur
= 0x20; /* 10, 200u */
718 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
719 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
720 cp_cur
= 0x38; /* 111, auto */
721 div_buf_cur
= 0x30; /* 11, 150u */
723 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
724 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
725 air_cable1_in
= 0x00;
729 filter_cur
= 0x40; /* 10, low */
732 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
733 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
734 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
735 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
736 air_cable1_in
= 0x00;
740 cp_cur
= 0x38; /* 111, auto */
741 div_buf_cur
= 0x30; /* 11, 150u */
742 filter_cur
= 0x40; /* 10, low */
745 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
746 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
747 lna_vth_l
= 0x75; /* lna vth 1.04 , vtl 0.84 */
748 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
749 air_cable1_in
= 0x00;
753 cp_cur
= 0x38; /* 111, auto */
754 div_buf_cur
= 0x30; /* 11, 150u */
755 filter_cur
= 0x40; /* 10, low */
757 case SYS_DVBC_ANNEX_A
:
758 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
762 air_cable1_in
= 0x60;
766 cp_cur
= 0x38; /* 111, auto */
767 div_buf_cur
= 0x30; /* 11, 150u */
768 filter_cur
= 0x40; /* 10, low */
770 default: /* DVB-T 8M */
771 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
772 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
773 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
774 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
775 air_cable1_in
= 0x00;
779 cp_cur
= 0x38; /* 111, auto */
780 div_buf_cur
= 0x30; /* 11, 150u */
781 filter_cur
= 0x40; /* 10, low */
785 if (priv
->cfg
->use_diplexer
&&
786 ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
787 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
788 (priv
->cfg
->rafael_chip
== CHIP_R820C
))) {
790 air_cable1_in
= 0x00;
792 air_cable1_in
= 0x60;
797 if (priv
->cfg
->use_predetect
) {
798 rc
= r820t_write_reg_mask(priv
, 0x06, pre_dect
, 0x40);
803 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0xc7);
806 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0xf8);
809 rc
= r820t_write_reg(priv
, 0x0d, lna_vth_l
);
812 rc
= r820t_write_reg(priv
, 0x0e, mixer_vth_l
);
816 /* Air-IN only for Astrometa */
817 rc
= r820t_write_reg_mask(priv
, 0x05, air_cable1_in
, 0x60);
820 rc
= r820t_write_reg_mask(priv
, 0x06, cable2_in
, 0x08);
824 rc
= r820t_write_reg_mask(priv
, 0x11, cp_cur
, 0x38);
827 rc
= r820t_write_reg_mask(priv
, 0x17, div_buf_cur
, 0x30);
830 rc
= r820t_write_reg_mask(priv
, 0x0a, filter_cur
, 0x60);
834 * Original driver initializes regs 0x05 and 0x06 with the
835 * same value again on this point. Probably, it is just an
843 tuner_dbg("adjusting LNA parameters\n");
844 if (type
!= V4L2_TUNER_ANALOG_TV
) {
845 /* LNA TOP: lowest */
846 rc
= r820t_write_reg_mask(priv
, 0x1d, 0, 0x38);
851 rc
= r820t_write_reg_mask(priv
, 0x1c, 0, 0x04);
855 /* 0: PRE_DECT off */
856 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
861 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x30, 0x30);
867 /* write LNA TOP = 3 */
868 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x18, 0x38);
873 * write discharge mode
874 * FIXME: IMHO, the mask here is wrong, but it matches
875 * what's there at the original driver
877 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
881 /* LNA discharge current */
882 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
887 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x20, 0x30);
892 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
897 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0x38);
902 * write discharge mode
903 * FIXME: IMHO, the mask here is wrong, but it matches
904 * what's there at the original driver
906 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
910 /* LNA discharge current */
911 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
915 /* agc clk 1Khz, external det1 cap 1u */
916 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x30);
920 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x04);
927 static int r820t_set_tv_standard(struct r820t_priv
*priv
,
929 enum v4l2_tuner_type type
,
930 v4l2_std_id std
, u32 delsys
)
934 u32 if_khz
, filt_cal_lo
;
936 u8 filt_gain
, img_r
, filt_q
, hp_cor
, ext_enable
, loop_through
;
937 u8 lt_att
, flt_ext_widest
, polyfil_cur
;
938 bool need_calibration
;
940 tuner_dbg("selecting the delivery system\n");
942 if (delsys
== SYS_ISDBT
) {
945 filt_gain
= 0x10; /* +3db, 6mhz on */
946 img_r
= 0x00; /* image negative */
947 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
948 hp_cor
= 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
949 ext_enable
= 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
950 loop_through
= 0x00; /* r5[7], lt on */
951 lt_att
= 0x00; /* r31[7], lt att enable */
952 flt_ext_widest
= 0x80; /* r15[7]: flt_ext_wide on */
953 polyfil_cur
= 0x60; /* r25[6:5]:min */
954 } else if (delsys
== SYS_DVBC_ANNEX_A
) {
957 filt_gain
= 0x10; /* +3db, 6mhz on */
958 img_r
= 0x00; /* image negative */
959 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
960 hp_cor
= 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
961 ext_enable
= 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
962 loop_through
= 0x00; /* r5[7], lt on */
963 lt_att
= 0x00; /* r31[7], lt att enable */
964 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
965 polyfil_cur
= 0x60; /* r25[6:5]:min */
966 } else if (delsys
== SYS_DVBC_ANNEX_C
) {
969 filt_gain
= 0x10; /* +3db, 6mhz on */
970 img_r
= 0x00; /* image negative */
971 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
972 hp_cor
= 0x6a; /* 1.7m disable, +0cap, 1.0mhz */
973 ext_enable
= 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
974 loop_through
= 0x00; /* r5[7], lt on */
975 lt_att
= 0x00; /* r31[7], lt att enable */
976 flt_ext_widest
= 0x80; /* r15[7]: flt_ext_wide on */
977 polyfil_cur
= 0x60; /* r25[6:5]:min */
981 filt_cal_lo
= 56000; /* 52000->56000 */
982 filt_gain
= 0x10; /* +3db, 6mhz on */
983 img_r
= 0x00; /* image negative */
984 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
985 hp_cor
= 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
986 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
987 loop_through
= 0x00; /* r5[7], lt on */
988 lt_att
= 0x00; /* r31[7], lt att enable */
989 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
990 polyfil_cur
= 0x60; /* r25[6:5]:min */
991 } else if (bw
== 7) {
994 * There are two 7 MHz tables defined on the original
995 * driver, but just the second one seems to be visible
996 * by rtl2832. Keep this one here commented, as it
997 * might be needed in the future
1001 filt_cal_lo
= 60000;
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
= 0x2b; /* 1.7m disable, +1cap, 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 */
1012 /* 7 MHz, second table */
1014 filt_cal_lo
= 63000;
1015 filt_gain
= 0x10; /* +3db, 6mhz on */
1016 img_r
= 0x00; /* image negative */
1017 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1018 hp_cor
= 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
1019 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1020 loop_through
= 0x00; /* r5[7], lt on */
1021 lt_att
= 0x00; /* r31[7], lt att enable */
1022 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1023 polyfil_cur
= 0x60; /* r25[6:5]:min */
1026 filt_cal_lo
= 68500;
1027 filt_gain
= 0x10; /* +3db, 6mhz on */
1028 img_r
= 0x00; /* image negative */
1029 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1030 hp_cor
= 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
1031 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1032 loop_through
= 0x00; /* r5[7], lt on */
1033 lt_att
= 0x00; /* r31[7], lt att enable */
1034 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1035 polyfil_cur
= 0x60; /* r25[6:5]:min */
1039 /* Initialize the shadow registers */
1040 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1042 /* Init Flag & Xtal_check Result */
1044 val
= 1 | priv
->xtal_cap_sel
<< 1;
1047 rc
= r820t_write_reg_mask(priv
, 0x0c, val
, 0x0f);
1052 rc
= r820t_write_reg_mask(priv
, 0x13, VER_NUM
, 0x3f);
1056 /* for LT Gain test */
1057 if (type
!= V4L2_TUNER_ANALOG_TV
) {
1058 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x00, 0x38);
1061 usleep_range(1000, 2000);
1063 priv
->int_freq
= if_khz
* 1000;
1065 /* Check if standard changed. If so, filter calibration is needed */
1066 if (type
!= priv
->type
)
1067 need_calibration
= true;
1068 else if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
!= priv
->std
))
1069 need_calibration
= true;
1070 else if ((type
== V4L2_TUNER_DIGITAL_TV
) &&
1071 ((delsys
!= priv
->delsys
) || bw
!= priv
->bw
))
1072 need_calibration
= true;
1074 need_calibration
= false;
1076 if (need_calibration
) {
1077 tuner_dbg("calibrating the tuner\n");
1078 for (i
= 0; i
< 2; i
++) {
1080 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0x60);
1084 /* set cali clk =on */
1085 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x04, 0x04);
1089 /* X'tal cap 0pF for PLL */
1090 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x03);
1094 rc
= r820t_set_pll(priv
, type
, filt_cal_lo
* 1000);
1095 if (rc
< 0 || !priv
->has_lock
)
1099 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x10, 0x10);
1103 usleep_range(1000, 2000);
1106 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x00, 0x10);
1110 /* set cali clk =off */
1111 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x00, 0x04);
1115 /* Check if calibration worked */
1116 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1120 priv
->fil_cal_code
= data
[4] & 0x0f;
1121 if (priv
->fil_cal_code
&& priv
->fil_cal_code
!= 0x0f)
1125 if (priv
->fil_cal_code
== 0x0f)
1126 priv
->fil_cal_code
= 0;
1129 rc
= r820t_write_reg_mask(priv
, 0x0a,
1130 filt_q
| priv
->fil_cal_code
, 0x1f);
1134 /* Set BW, Filter_gain, & HP corner */
1135 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0xef);
1141 rc
= r820t_write_reg_mask(priv
, 0x07, img_r
, 0x80);
1145 /* Set filt_3dB, V6MHz */
1146 rc
= r820t_write_reg_mask(priv
, 0x06, filt_gain
, 0x30);
1150 /* channel filter extension */
1151 rc
= r820t_write_reg_mask(priv
, 0x1e, ext_enable
, 0x60);
1156 rc
= r820t_write_reg_mask(priv
, 0x05, loop_through
, 0x80);
1160 /* Loop through attenuation */
1161 rc
= r820t_write_reg_mask(priv
, 0x1f, lt_att
, 0x80);
1165 /* filter extension widest */
1166 rc
= r820t_write_reg_mask(priv
, 0x0f, flt_ext_widest
, 0x80);
1170 /* RF poly filter current */
1171 rc
= r820t_write_reg_mask(priv
, 0x19, polyfil_cur
, 0x60);
1175 /* Store current standard. If it changes, re-calibrate the tuner */
1176 priv
->delsys
= delsys
;
1184 static int r820t_read_gain(struct r820t_priv
*priv
)
1189 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1193 return ((data
[3] & 0x08) << 1) + ((data
[3] & 0xf0) >> 4);
1197 /* FIXME: This routine requires more testing */
1200 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
1201 * input power, for raw results see:
1202 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
1205 static const int r820t_lna_gain_steps
[] = {
1206 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
1209 static const int r820t_mixer_gain_steps
[] = {
1210 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
1213 static int r820t_set_gain_mode(struct r820t_priv
*priv
,
1214 bool set_manual_gain
,
1219 if (set_manual_gain
) {
1220 int i
, total_gain
= 0;
1221 uint8_t mix_index
= 0, lna_index
= 0;
1225 rc
= r820t_write_reg_mask(priv
, 0x05, 0x10, 0x10);
1229 /* Mixer auto off */
1230 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1234 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1238 /* set fixed VGA gain for now (16.3 dB) */
1239 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x08, 0x9f);
1243 for (i
= 0; i
< 15; i
++) {
1244 if (total_gain
>= gain
)
1247 total_gain
+= r820t_lna_gain_steps
[++lna_index
];
1249 if (total_gain
>= gain
)
1252 total_gain
+= r820t_mixer_gain_steps
[++mix_index
];
1256 rc
= r820t_write_reg_mask(priv
, 0x05, lna_index
, 0x0f);
1260 /* set Mixer gain */
1261 rc
= r820t_write_reg_mask(priv
, 0x07, mix_index
, 0x0f);
1266 rc
= r820t_write_reg_mask(priv
, 0x05, 0, 0x10);
1271 rc
= r820t_write_reg_mask(priv
, 0x07, 0x10, 0x10);
1275 /* set fixed VGA gain for now (26.5 dB) */
1276 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1285 static int generic_set_freq(struct dvb_frontend
*fe
,
1286 u32 freq
/* in HZ */,
1288 enum v4l2_tuner_type type
,
1289 v4l2_std_id std
, u32 delsys
)
1291 struct r820t_priv
*priv
= fe
->tuner_priv
;
1295 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1298 rc
= r820t_set_tv_standard(priv
, bw
, type
, std
, delsys
);
1302 if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
== V4L2_STD_SECAM_LC
))
1303 lo_freq
= freq
- priv
->int_freq
;
1305 lo_freq
= freq
+ priv
->int_freq
;
1307 rc
= r820t_set_mux(priv
, lo_freq
);
1311 rc
= r820t_set_pll(priv
, type
, lo_freq
);
1312 if (rc
< 0 || !priv
->has_lock
)
1315 rc
= r820t_sysfreq_sel(priv
, freq
, type
, std
, delsys
);
1319 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1320 __func__
, freq
, r820t_read_gain(priv
));
1325 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
1330 * r820t standby logic
1333 static int r820t_standby(struct r820t_priv
*priv
)
1337 /* If device was not initialized yet, don't need to standby */
1338 if (!priv
->init_done
)
1341 rc
= r820t_write_reg(priv
, 0x06, 0xb1);
1344 rc
= r820t_write_reg(priv
, 0x05, 0x03);
1347 rc
= r820t_write_reg(priv
, 0x07, 0x3a);
1350 rc
= r820t_write_reg(priv
, 0x08, 0x40);
1353 rc
= r820t_write_reg(priv
, 0x09, 0xc0);
1356 rc
= r820t_write_reg(priv
, 0x0a, 0x36);
1359 rc
= r820t_write_reg(priv
, 0x0c, 0x35);
1362 rc
= r820t_write_reg(priv
, 0x0f, 0x68);
1365 rc
= r820t_write_reg(priv
, 0x11, 0x03);
1368 rc
= r820t_write_reg(priv
, 0x17, 0xf4);
1371 rc
= r820t_write_reg(priv
, 0x19, 0x0c);
1373 /* Force initial calibration */
1380 * r820t device init logic
1383 static int r820t_xtal_check(struct r820t_priv
*priv
)
1388 /* Initialize the shadow registers */
1389 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1391 /* cap 30pF & Drive Low */
1392 rc
= r820t_write_reg_mask(priv
, 0x10, 0x0b, 0x0b);
1396 /* set pll autotune = 128kHz */
1397 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
1401 /* set manual initial reg = 111111; */
1402 rc
= r820t_write_reg_mask(priv
, 0x13, 0x7f, 0x7f);
1407 rc
= r820t_write_reg_mask(priv
, 0x13, 0x00, 0x40);
1411 /* Try several xtal capacitor alternatives */
1412 for (i
= 0; i
< ARRAY_SIZE(r820t_xtal_capacitor
); i
++) {
1413 rc
= r820t_write_reg_mask(priv
, 0x10,
1414 r820t_xtal_capacitor
[i
][0], 0x1b);
1418 usleep_range(5000, 6000);
1420 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1423 if (!(data
[2] & 0x40))
1426 val
= data
[2] & 0x3f;
1428 if (priv
->cfg
->xtal
== 16000000 && (val
> 29 || val
< 23))
1435 if (i
== ARRAY_SIZE(r820t_xtal_capacitor
))
1438 return r820t_xtal_capacitor
[i
][1];
1441 static int r820t_imr_prepare(struct r820t_priv
*priv
)
1445 /* Initialize the shadow registers */
1446 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1448 /* lna off (air-in off) */
1449 rc
= r820t_write_reg_mask(priv
, 0x05, 0x20, 0x20);
1453 /* mixer gain mode = manual */
1454 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1458 /* filter corner = lowest */
1459 rc
= r820t_write_reg_mask(priv
, 0x0a, 0x0f, 0x0f);
1463 /* filter bw=+2cap, hp=5M */
1464 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x60, 0x6f);
1468 /* adc=on, vga code mode, gain = 26.5dB */
1469 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1474 rc
= r820t_write_reg_mask(priv
, 0x0f, 0, 0x08);
1478 /* ring power = on */
1479 rc
= r820t_write_reg_mask(priv
, 0x18, 0x10, 0x10);
1483 /* from ring = ring pll in */
1484 rc
= r820t_write_reg_mask(priv
, 0x1c, 0x02, 0x02);
1488 /* sw_pdect = det3 */
1489 rc
= r820t_write_reg_mask(priv
, 0x1e, 0x80, 0x80);
1494 rc
= r820t_write_reg_mask(priv
, 0x06, 0x20, 0x20);
1499 static int r820t_multi_read(struct r820t_priv
*priv
)
1503 u8 data
[2], min
= 255, max
= 0;
1505 usleep_range(5000, 6000);
1507 for (i
= 0; i
< 6; i
++) {
1508 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1520 rc
= sum
- max
- min
;
1525 static int r820t_imr_cross(struct r820t_priv
*priv
,
1526 struct r820t_sect_type iq_point
[3],
1529 struct r820t_sect_type cross
[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
1530 struct r820t_sect_type tmp
;
1534 reg08
= r820t_read_cache_reg(priv
, 8) & 0xc0;
1535 reg09
= r820t_read_cache_reg(priv
, 9) & 0xc0;
1541 for (i
= 0; i
< 5; i
++) {
1544 cross
[i
].gain_x
= reg08
;
1545 cross
[i
].phase_y
= reg09
;
1548 cross
[i
].gain_x
= reg08
; /* 0 */
1549 cross
[i
].phase_y
= reg09
+ 1; /* Q-1 */
1552 cross
[i
].gain_x
= reg08
; /* 0 */
1553 cross
[i
].phase_y
= (reg09
| 0x20) + 1; /* I-1 */
1556 cross
[i
].gain_x
= reg08
+ 1; /* Q-1 */
1557 cross
[i
].phase_y
= reg09
;
1560 cross
[i
].gain_x
= (reg08
| 0x20) + 1; /* I-1 */
1561 cross
[i
].phase_y
= reg09
;
1564 rc
= r820t_write_reg(priv
, 0x08, cross
[i
].gain_x
);
1568 rc
= r820t_write_reg(priv
, 0x09, cross
[i
].phase_y
);
1572 rc
= r820t_multi_read(priv
);
1576 cross
[i
].value
= rc
;
1578 if (cross
[i
].value
< tmp
.value
)
1582 if ((tmp
.phase_y
& 0x1f) == 1) { /* y-direction */
1585 iq_point
[0] = cross
[0];
1586 iq_point
[1] = cross
[1];
1587 iq_point
[2] = cross
[2];
1588 } else { /* (0,0) or x-direction */
1591 iq_point
[0] = cross
[0];
1592 iq_point
[1] = cross
[3];
1593 iq_point
[2] = cross
[4];
1598 static void r820t_compre_cor(struct r820t_sect_type iq
[3])
1602 for (i
= 3; i
> 0; i
--) {
1603 if (iq
[0].value
> iq
[i
- 1].value
)
1604 swap(iq
[0], iq
[i
- 1]);
1608 static int r820t_compre_step(struct r820t_priv
*priv
,
1609 struct r820t_sect_type iq
[3], u8 reg
)
1612 struct r820t_sect_type tmp
;
1615 * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
1617 * new < min => update to min and continue
1621 /* min value already saved in iq[0] */
1622 tmp
.phase_y
= iq
[0].phase_y
;
1623 tmp
.gain_x
= iq
[0].gain_x
;
1625 while (((tmp
.gain_x
& 0x1f) < IMR_TRIAL
) &&
1626 ((tmp
.phase_y
& 0x1f) < IMR_TRIAL
)) {
1632 rc
= r820t_write_reg(priv
, 0x08, tmp
.gain_x
);
1636 rc
= r820t_write_reg(priv
, 0x09, tmp
.phase_y
);
1640 rc
= r820t_multi_read(priv
);
1645 if (tmp
.value
<= iq
[0].value
) {
1646 iq
[0].gain_x
= tmp
.gain_x
;
1647 iq
[0].phase_y
= tmp
.phase_y
;
1648 iq
[0].value
= tmp
.value
;
1658 static int r820t_iq_tree(struct r820t_priv
*priv
,
1659 struct r820t_sect_type iq
[3],
1660 u8 fix_val
, u8 var_val
, u8 fix_reg
)
1666 * record IMC results by input gain/phase location then adjust
1667 * gain or phase positive 1 step and negative 1 step,
1668 * both record results
1671 if (fix_reg
== 0x08)
1676 for (i
= 0; i
< 3; i
++) {
1677 rc
= r820t_write_reg(priv
, fix_reg
, fix_val
);
1681 rc
= r820t_write_reg(priv
, var_reg
, var_val
);
1685 rc
= r820t_multi_read(priv
);
1690 if (fix_reg
== 0x08) {
1691 iq
[i
].gain_x
= fix_val
;
1692 iq
[i
].phase_y
= var_val
;
1694 iq
[i
].phase_y
= fix_val
;
1695 iq
[i
].gain_x
= var_val
;
1698 if (i
== 0) { /* try right-side point */
1700 } else if (i
== 1) { /* try left-side point */
1701 /* if absolute location is 1, change I/Q direction */
1702 if ((var_val
& 0x1f) < 0x02) {
1703 tmp
= 2 - (var_val
& 0x1f);
1705 /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
1706 if (var_val
& 0x20) {
1710 var_val
|= 0x20 | tmp
;
1721 static int r820t_section(struct r820t_priv
*priv
,
1722 struct r820t_sect_type
*iq_point
)
1725 struct r820t_sect_type compare_iq
[3], compare_bet
[3];
1727 /* Try X-1 column and save min result to compare_bet[0] */
1728 if (!(iq_point
->gain_x
& 0x1f))
1729 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) & 0xdf) + 1; /* Q-path, Gain=1 */
1731 compare_iq
[0].gain_x
= iq_point
->gain_x
- 1; /* left point */
1732 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1735 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1736 compare_iq
[0].phase_y
, 0x08);
1740 r820t_compre_cor(compare_iq
);
1742 compare_bet
[0] = compare_iq
[0];
1744 /* Try X column and save min result to compare_bet[1] */
1745 compare_iq
[0].gain_x
= iq_point
->gain_x
;
1746 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1748 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1749 compare_iq
[0].phase_y
, 0x08);
1753 r820t_compre_cor(compare_iq
);
1755 compare_bet
[1] = compare_iq
[0];
1757 /* Try X+1 column and save min result to compare_bet[2] */
1758 if ((iq_point
->gain_x
& 0x1f) == 0x00)
1759 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) | 0x20) + 1; /* I-path, Gain=1 */
1761 compare_iq
[0].gain_x
= iq_point
->gain_x
+ 1;
1762 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1764 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1765 compare_iq
[0].phase_y
, 0x08);
1769 r820t_compre_cor(compare_iq
);
1771 compare_bet
[2] = compare_iq
[0];
1773 r820t_compre_cor(compare_bet
);
1775 *iq_point
= compare_bet
[0];
1780 static int r820t_vga_adjust(struct r820t_priv
*priv
)
1785 /* increase vga power to let image significant */
1786 for (vga_count
= 12; vga_count
< 16; vga_count
++) {
1787 rc
= r820t_write_reg_mask(priv
, 0x0c, vga_count
, 0x0f);
1791 usleep_range(10000, 11000);
1793 rc
= r820t_multi_read(priv
);
1804 static int r820t_iq(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1806 struct r820t_sect_type compare_iq
[3];
1808 u8 x_direction
= 0; /* 1:x, 0:y */
1809 u8 dir_reg
, other_reg
;
1811 r820t_vga_adjust(priv
);
1813 rc
= r820t_imr_cross(priv
, compare_iq
, &x_direction
);
1817 if (x_direction
== 1) {
1825 /* compare and find min of 3 points. determine i/q direction */
1826 r820t_compre_cor(compare_iq
);
1828 /* increase step to find min value of this direction */
1829 rc
= r820t_compre_step(priv
, compare_iq
, dir_reg
);
1833 /* the other direction */
1834 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1835 compare_iq
[0].phase_y
, dir_reg
);
1839 /* compare and find min of 3 points. determine i/q direction */
1840 r820t_compre_cor(compare_iq
);
1842 /* increase step to find min value on this direction */
1843 rc
= r820t_compre_step(priv
, compare_iq
, other_reg
);
1847 /* check 3 points again */
1848 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1849 compare_iq
[0].phase_y
, other_reg
);
1853 r820t_compre_cor(compare_iq
);
1855 /* section-9 check */
1856 rc
= r820t_section(priv
, compare_iq
);
1858 *iq_pont
= compare_iq
[0];
1860 /* reset gain/phase control setting */
1861 rc
= r820t_write_reg_mask(priv
, 0x08, 0, 0x3f);
1865 rc
= r820t_write_reg_mask(priv
, 0x09, 0, 0x3f);
1870 static int r820t_f_imr(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1874 r820t_vga_adjust(priv
);
1877 * search surrounding points from previous point
1878 * try (x-1), (x), (x+1) columns, and find min IMR result point
1880 rc
= r820t_section(priv
, iq_pont
);
1887 static int r820t_imr(struct r820t_priv
*priv
, unsigned imr_mem
, bool im_flag
)
1889 struct r820t_sect_type imr_point
;
1891 u32 ring_vco
, ring_freq
, ring_ref
;
1893 int reg18
, reg19
, reg1f
;
1895 if (priv
->cfg
->xtal
> 24000000)
1896 ring_ref
= priv
->cfg
->xtal
/ 2000;
1898 ring_ref
= priv
->cfg
->xtal
/ 1000;
1901 for (n
= 0; n
< 16; n
++) {
1902 if ((16 + n
) * 8 * ring_ref
>= 3100000) {
1908 reg18
= r820t_read_cache_reg(priv
, 0x18);
1909 reg19
= r820t_read_cache_reg(priv
, 0x19);
1910 reg1f
= r820t_read_cache_reg(priv
, 0x1f);
1912 reg18
&= 0xf0; /* set ring[3:0] */
1915 ring_vco
= (16 + n_ring
) * 8 * ring_ref
;
1917 reg18
&= 0xdf; /* clear ring_se23 */
1918 reg19
&= 0xfc; /* clear ring_seldiv */
1919 reg1f
&= 0xfc; /* clear ring_att */
1923 ring_freq
= ring_vco
/ 48;
1924 reg18
|= 0x20; /* ring_se23 = 1 */
1925 reg19
|= 0x03; /* ring_seldiv = 3 */
1926 reg1f
|= 0x02; /* ring_att 10 */
1929 ring_freq
= ring_vco
/ 16;
1930 reg18
|= 0x00; /* ring_se23 = 0 */
1931 reg19
|= 0x02; /* ring_seldiv = 2 */
1932 reg1f
|= 0x00; /* pw_ring 00 */
1935 ring_freq
= ring_vco
/ 8;
1936 reg18
|= 0x00; /* ring_se23 = 0 */
1937 reg19
|= 0x01; /* ring_seldiv = 1 */
1938 reg1f
|= 0x03; /* pw_ring 11 */
1941 ring_freq
= ring_vco
/ 6;
1942 reg18
|= 0x20; /* ring_se23 = 1 */
1943 reg19
|= 0x00; /* ring_seldiv = 0 */
1944 reg1f
|= 0x03; /* pw_ring 11 */
1947 ring_freq
= ring_vco
/ 4;
1948 reg18
|= 0x00; /* ring_se23 = 0 */
1949 reg19
|= 0x00; /* ring_seldiv = 0 */
1950 reg1f
|= 0x01; /* pw_ring 01 */
1953 ring_freq
= ring_vco
/ 4;
1954 reg18
|= 0x00; /* ring_se23 = 0 */
1955 reg19
|= 0x00; /* ring_seldiv = 0 */
1956 reg1f
|= 0x01; /* pw_ring 01 */
1961 /* write pw_ring, n_ring, ringdiv2 registers */
1963 /* n_ring, ring_se23 */
1964 rc
= r820t_write_reg(priv
, 0x18, reg18
);
1969 rc
= r820t_write_reg(priv
, 0x19, reg19
);
1974 rc
= r820t_write_reg(priv
, 0x1f, reg1f
);
1978 /* mux input freq ~ rf_in freq */
1979 rc
= r820t_set_mux(priv
, (ring_freq
- 5300) * 1000);
1983 rc
= r820t_set_pll(priv
, V4L2_TUNER_DIGITAL_TV
,
1984 (ring_freq
- 5300) * 1000);
1985 if (!priv
->has_lock
)
1991 rc
= r820t_iq(priv
, &imr_point
);
1993 imr_point
.gain_x
= priv
->imr_data
[3].gain_x
;
1994 imr_point
.phase_y
= priv
->imr_data
[3].phase_y
;
1995 imr_point
.value
= priv
->imr_data
[3].value
;
1997 rc
= r820t_f_imr(priv
, &imr_point
);
2002 /* save IMR value */
2005 priv
->imr_data
[0].gain_x
= imr_point
.gain_x
;
2006 priv
->imr_data
[0].phase_y
= imr_point
.phase_y
;
2007 priv
->imr_data
[0].value
= imr_point
.value
;
2010 priv
->imr_data
[1].gain_x
= imr_point
.gain_x
;
2011 priv
->imr_data
[1].phase_y
= imr_point
.phase_y
;
2012 priv
->imr_data
[1].value
= imr_point
.value
;
2015 priv
->imr_data
[2].gain_x
= imr_point
.gain_x
;
2016 priv
->imr_data
[2].phase_y
= imr_point
.phase_y
;
2017 priv
->imr_data
[2].value
= imr_point
.value
;
2020 priv
->imr_data
[3].gain_x
= imr_point
.gain_x
;
2021 priv
->imr_data
[3].phase_y
= imr_point
.phase_y
;
2022 priv
->imr_data
[3].value
= imr_point
.value
;
2025 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
2026 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
2027 priv
->imr_data
[4].value
= imr_point
.value
;
2030 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
2031 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
2032 priv
->imr_data
[4].value
= imr_point
.value
;
2039 static int r820t_imr_callibrate(struct r820t_priv
*priv
)
2044 if (priv
->init_done
)
2047 /* Detect Xtal capacitance */
2048 if ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
2049 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
2050 (priv
->cfg
->rafael_chip
== CHIP_R820C
)) {
2051 priv
->xtal_cap_sel
= XTAL_HIGH_CAP_0P
;
2053 /* Initialize registers */
2054 rc
= r820t_write(priv
, 0x05,
2055 r820t_init_array
, sizeof(r820t_init_array
));
2058 for (i
= 0; i
< 3; i
++) {
2059 rc
= r820t_xtal_check(priv
);
2062 if (!i
|| rc
> xtal_cap
)
2065 priv
->xtal_cap_sel
= xtal_cap
;
2069 * Disables IMR calibration. That emulates the same behaviour
2070 * as what is done by rtl-sdr userspace library. Useful for testing
2073 priv
->init_done
= true;
2078 /* Initialize registers */
2079 rc
= r820t_write(priv
, 0x05,
2080 r820t_init_array
, sizeof(r820t_init_array
));
2084 rc
= r820t_imr_prepare(priv
);
2088 rc
= r820t_imr(priv
, 3, true);
2091 rc
= r820t_imr(priv
, 1, false);
2094 rc
= r820t_imr(priv
, 0, false);
2097 rc
= r820t_imr(priv
, 2, false);
2100 rc
= r820t_imr(priv
, 4, false);
2104 priv
->init_done
= true;
2105 priv
->imr_done
= true;
2111 /* Not used, for now */
2112 static int r820t_gpio(struct r820t_priv
*priv
, bool enable
)
2114 return r820t_write_reg_mask(priv
, 0x0f, enable
? 1 : 0, 0x01);
2119 * r820t frontend operations and tuner attach code
2121 * All driver locks and i2c control are only in this part of the code
2124 static int r820t_init(struct dvb_frontend
*fe
)
2126 struct r820t_priv
*priv
= fe
->tuner_priv
;
2129 tuner_dbg("%s:\n", __func__
);
2131 mutex_lock(&priv
->lock
);
2132 if (fe
->ops
.i2c_gate_ctrl
)
2133 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2135 rc
= r820t_imr_callibrate(priv
);
2139 /* Initialize registers */
2140 rc
= r820t_write(priv
, 0x05,
2141 r820t_init_array
, sizeof(r820t_init_array
));
2144 if (fe
->ops
.i2c_gate_ctrl
)
2145 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2146 mutex_unlock(&priv
->lock
);
2149 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2153 static int r820t_sleep(struct dvb_frontend
*fe
)
2155 struct r820t_priv
*priv
= fe
->tuner_priv
;
2158 tuner_dbg("%s:\n", __func__
);
2160 mutex_lock(&priv
->lock
);
2161 if (fe
->ops
.i2c_gate_ctrl
)
2162 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2164 rc
= r820t_standby(priv
);
2166 if (fe
->ops
.i2c_gate_ctrl
)
2167 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2168 mutex_unlock(&priv
->lock
);
2170 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2174 static int r820t_set_analog_freq(struct dvb_frontend
*fe
,
2175 struct analog_parameters
*p
)
2177 struct r820t_priv
*priv
= fe
->tuner_priv
;
2181 tuner_dbg("%s called\n", __func__
);
2183 /* if std is not defined, choose one */
2185 p
->std
= V4L2_STD_MN
;
2187 if ((p
->std
== V4L2_STD_PAL_M
) || (p
->std
== V4L2_STD_NTSC
))
2192 mutex_lock(&priv
->lock
);
2193 if (fe
->ops
.i2c_gate_ctrl
)
2194 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2196 rc
= generic_set_freq(fe
, 62500l * p
->frequency
, bw
,
2197 V4L2_TUNER_ANALOG_TV
, p
->std
, SYS_UNDEFINED
);
2199 if (fe
->ops
.i2c_gate_ctrl
)
2200 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2201 mutex_unlock(&priv
->lock
);
2206 static int r820t_set_params(struct dvb_frontend
*fe
)
2208 struct r820t_priv
*priv
= fe
->tuner_priv
;
2209 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
2213 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2214 __func__
, c
->delivery_system
, c
->frequency
, c
->bandwidth_hz
);
2216 mutex_lock(&priv
->lock
);
2217 if (fe
->ops
.i2c_gate_ctrl
)
2218 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2220 bw
= (c
->bandwidth_hz
+ 500000) / 1000000;
2224 rc
= generic_set_freq(fe
, c
->frequency
, bw
,
2225 V4L2_TUNER_DIGITAL_TV
, 0, c
->delivery_system
);
2227 if (fe
->ops
.i2c_gate_ctrl
)
2228 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2229 mutex_unlock(&priv
->lock
);
2232 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2236 static int r820t_signal(struct dvb_frontend
*fe
, u16
*strength
)
2238 struct r820t_priv
*priv
= fe
->tuner_priv
;
2241 mutex_lock(&priv
->lock
);
2242 if (fe
->ops
.i2c_gate_ctrl
)
2243 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2245 if (priv
->has_lock
) {
2246 rc
= r820t_read_gain(priv
);
2250 /* A higher gain at LNA means a lower signal strength */
2251 *strength
= (45 - rc
) << 4 | 0xff;
2252 if (*strength
== 0xff)
2259 if (fe
->ops
.i2c_gate_ctrl
)
2260 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2261 mutex_unlock(&priv
->lock
);
2263 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2265 priv
->has_lock
? "PLL locked" : "no signal",
2271 static int r820t_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
2273 struct r820t_priv
*priv
= fe
->tuner_priv
;
2275 tuner_dbg("%s:\n", __func__
);
2277 *frequency
= priv
->int_freq
;
2282 static void r820t_release(struct dvb_frontend
*fe
)
2284 struct r820t_priv
*priv
= fe
->tuner_priv
;
2286 tuner_dbg("%s:\n", __func__
);
2288 mutex_lock(&r820t_list_mutex
);
2291 hybrid_tuner_release_state(priv
);
2293 mutex_unlock(&r820t_list_mutex
);
2295 fe
->tuner_priv
= NULL
;
2298 static const struct dvb_tuner_ops r820t_tuner_ops
= {
2300 .name
= "Rafael Micro R820T",
2301 .frequency_min_hz
= 42 * MHz
,
2302 .frequency_max_hz
= 1002 * MHz
,
2305 .release
= r820t_release
,
2306 .sleep
= r820t_sleep
,
2307 .set_params
= r820t_set_params
,
2308 .set_analog_params
= r820t_set_analog_freq
,
2309 .get_if_frequency
= r820t_get_if_frequency
,
2310 .get_rf_strength
= r820t_signal
,
2313 struct dvb_frontend
*r820t_attach(struct dvb_frontend
*fe
,
2314 struct i2c_adapter
*i2c
,
2315 const struct r820t_config
*cfg
)
2317 struct r820t_priv
*priv
;
2322 mutex_lock(&r820t_list_mutex
);
2324 instance
= hybrid_tuner_request_state(struct r820t_priv
, priv
,
2325 hybrid_tuner_instance_list
,
2330 /* memory allocation failure */
2333 /* new tuner instance */
2336 mutex_init(&priv
->lock
);
2338 fe
->tuner_priv
= priv
;
2341 /* existing tuner instance */
2342 fe
->tuner_priv
= priv
;
2346 if (fe
->ops
.i2c_gate_ctrl
)
2347 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2349 /* check if the tuner is there */
2350 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
2354 rc
= r820t_sleep(fe
);
2358 tuner_info("Rafael Micro r820t successfully identified\n");
2360 if (fe
->ops
.i2c_gate_ctrl
)
2361 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2363 mutex_unlock(&r820t_list_mutex
);
2365 memcpy(&fe
->ops
.tuner_ops
, &r820t_tuner_ops
,
2366 sizeof(struct dvb_tuner_ops
));
2370 if (fe
->ops
.i2c_gate_ctrl
)
2371 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2374 mutex_unlock(&r820t_list_mutex
);
2376 pr_info("%s: failed=%d\n", __func__
, rc
);
2380 EXPORT_SYMBOL_GPL(r820t_attach
);
2382 MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2383 MODULE_AUTHOR("Mauro Carvalho Chehab");
2384 MODULE_LICENSE("GPL v2");