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 #include <linux/videodev2.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/bitrev.h>
28 #include "tuner-i2c.h"
32 * FIXME: I think that there are only 32 registers, but better safe than
33 * sorry. After finishing the driver, we may review it.
35 #define REG_SHADOW_START 5
43 module_param(debug
, int, 0644);
44 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
46 static int no_imr_cal
;
47 module_param(no_imr_cal
, int, 0444);
48 MODULE_PARM_DESC(no_imr_cal
, "Disable IMR calibration at module init");
52 * enums and structures
63 struct r820t_sect_type
{
70 struct list_head hybrid_tuner_instance_list
;
71 const struct r820t_config
*cfg
;
72 struct tuner_i2c_props i2c_props
;
77 enum xtal_cap_value xtal_cap_sel
;
84 struct r820t_sect_type imr_data
[NUM_IMR
];
86 /* Store current mode */
88 enum v4l2_tuner_type type
;
93 struct r820t_freq_range
{
101 u8 imr_mem
; /* Not used, currently */
104 #define VCO_POWER_REF 0x02
105 #define DIP_FREQ 32000000
111 static LIST_HEAD(hybrid_tuner_instance_list
);
112 static DEFINE_MUTEX(r820t_list_mutex
);
114 /* Those initial values start from REG_SHADOW_START */
115 static const u8 r820t_init_array
[NUM_REGS
] = {
116 0x83, 0x32, 0x75, /* 05 to 07 */
117 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
118 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
119 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
120 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
121 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
122 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
125 /* Tuner frequency ranges */
126 static const struct r820t_freq_range freq_ranges
[] = {
129 .open_d
= 0x08, /* low */
130 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
131 .tf_c
= 0xdf, /* R27[7:0] band2,band0 */
132 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
137 .freq
= 50, /* Start freq, in MHz */
138 .open_d
= 0x08, /* low */
139 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
140 .tf_c
= 0xbe, /* R27[7:0] band4,band1 */
141 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
146 .freq
= 55, /* Start freq, in MHz */
147 .open_d
= 0x08, /* low */
148 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
149 .tf_c
= 0x8b, /* R27[7:0] band7,band4 */
150 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
155 .freq
= 60, /* Start freq, in MHz */
156 .open_d
= 0x08, /* low */
157 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
158 .tf_c
= 0x7b, /* R27[7:0] band8,band4 */
159 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
164 .freq
= 65, /* Start freq, in MHz */
165 .open_d
= 0x08, /* low */
166 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
167 .tf_c
= 0x69, /* R27[7:0] band9,band6 */
168 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
173 .freq
= 70, /* Start freq, in MHz */
174 .open_d
= 0x08, /* low */
175 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
176 .tf_c
= 0x58, /* R27[7:0] band10,band7 */
177 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
182 .freq
= 75, /* Start freq, in MHz */
183 .open_d
= 0x00, /* high */
184 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
185 .tf_c
= 0x44, /* R27[7:0] band11,band11 */
186 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
191 .freq
= 80, /* Start freq, in MHz */
192 .open_d
= 0x00, /* high */
193 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
194 .tf_c
= 0x44, /* R27[7:0] band11,band11 */
195 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
200 .freq
= 90, /* Start freq, in MHz */
201 .open_d
= 0x00, /* high */
202 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
203 .tf_c
= 0x34, /* R27[7:0] band12,band11 */
204 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
209 .freq
= 100, /* Start freq, in MHz */
210 .open_d
= 0x00, /* high */
211 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
212 .tf_c
= 0x34, /* R27[7:0] band12,band11 */
213 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
218 .freq
= 110, /* Start freq, in MHz */
219 .open_d
= 0x00, /* high */
220 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
221 .tf_c
= 0x24, /* R27[7:0] band13,band11 */
222 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
227 .freq
= 120, /* Start freq, in MHz */
228 .open_d
= 0x00, /* high */
229 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
230 .tf_c
= 0x24, /* R27[7:0] band13,band11 */
231 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
236 .freq
= 140, /* Start freq, in MHz */
237 .open_d
= 0x00, /* high */
238 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
239 .tf_c
= 0x14, /* R27[7:0] band14,band11 */
240 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
245 .freq
= 180, /* Start freq, in MHz */
246 .open_d
= 0x00, /* high */
247 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
248 .tf_c
= 0x13, /* R27[7:0] band14,band12 */
249 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
254 .freq
= 220, /* Start freq, in MHz */
255 .open_d
= 0x00, /* high */
256 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
257 .tf_c
= 0x13, /* R27[7:0] band14,band12 */
258 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
263 .freq
= 250, /* Start freq, in MHz */
264 .open_d
= 0x00, /* high */
265 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
266 .tf_c
= 0x11, /* R27[7:0] highest,highest */
267 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
272 .freq
= 280, /* Start freq, in MHz */
273 .open_d
= 0x00, /* high */
274 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
275 .tf_c
= 0x00, /* R27[7:0] highest,highest */
276 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
281 .freq
= 310, /* Start freq, in MHz */
282 .open_d
= 0x00, /* high */
283 .rf_mux_ploy
= 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
284 .tf_c
= 0x00, /* R27[7:0] highest,highest */
285 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
290 .freq
= 450, /* Start freq, in MHz */
291 .open_d
= 0x00, /* high */
292 .rf_mux_ploy
= 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
293 .tf_c
= 0x00, /* R27[7:0] highest,highest */
294 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
299 .freq
= 588, /* Start freq, in MHz */
300 .open_d
= 0x00, /* high */
301 .rf_mux_ploy
= 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
302 .tf_c
= 0x00, /* R27[7:0] highest,highest */
303 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
308 .freq
= 650, /* Start freq, in MHz */
309 .open_d
= 0x00, /* high */
310 .rf_mux_ploy
= 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
311 .tf_c
= 0x00, /* R27[7:0] highest,highest */
312 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
319 static int r820t_xtal_capacitor
[][2] = {
320 { 0x0b, XTAL_LOW_CAP_30P
},
321 { 0x02, XTAL_LOW_CAP_20P
},
322 { 0x01, XTAL_LOW_CAP_10P
},
323 { 0x00, XTAL_LOW_CAP_0P
},
324 { 0x10, XTAL_HIGH_CAP_0P
},
328 * I2C read/write code and shadow registers logic
330 static void shadow_store(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
333 int r
= reg
- REG_SHADOW_START
;
341 if (len
> NUM_REGS
- r
)
344 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
345 __func__
, r
+ REG_SHADOW_START
, len
, len
, val
);
347 memcpy(&priv
->regs
[r
], val
, len
);
350 static int r820t_write(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
353 int rc
, size
, pos
= 0;
355 /* Store the shadow registers */
356 shadow_store(priv
, reg
, val
, len
);
359 if (len
> priv
->cfg
->max_i2c_msg_len
- 1)
360 size
= priv
->cfg
->max_i2c_msg_len
- 1;
364 /* Fill I2C buffer */
366 memcpy(&priv
->buf
[1], &val
[pos
], size
);
368 rc
= tuner_i2c_xfer_send(&priv
->i2c_props
, priv
->buf
, size
+ 1);
369 if (rc
!= size
+ 1) {
370 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
371 __func__
, rc
, reg
, size
, size
, &priv
->buf
[1]);
376 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
377 __func__
, reg
, size
, size
, &priv
->buf
[1]);
387 static inline int r820t_write_reg(struct r820t_priv
*priv
, u8 reg
, u8 val
)
389 u8 tmp
= val
; /* work around GCC PR81715 with asan-stack=1 */
391 return r820t_write(priv
, reg
, &tmp
, 1);
394 static int r820t_read_cache_reg(struct r820t_priv
*priv
, int reg
)
396 reg
-= REG_SHADOW_START
;
398 if (reg
>= 0 && reg
< NUM_REGS
)
399 return priv
->regs
[reg
];
404 static inline int r820t_write_reg_mask(struct r820t_priv
*priv
, u8 reg
, u8 val
,
408 int rc
= r820t_read_cache_reg(priv
, reg
);
413 tmp
= (rc
& ~bit_mask
) | (tmp
& bit_mask
);
415 return r820t_write(priv
, reg
, &tmp
, 1);
418 static int r820t_read(struct r820t_priv
*priv
, u8 reg
, u8
*val
, int len
)
421 u8
*p
= &priv
->buf
[1];
425 rc
= tuner_i2c_xfer_send_recv(&priv
->i2c_props
, priv
->buf
, 1, p
, len
);
427 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
428 __func__
, rc
, reg
, len
, len
, p
);
434 /* Copy data to the output buffer */
435 for (i
= 0; i
< len
; i
++)
436 val
[i
] = bitrev8(p
[i
]);
438 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
439 __func__
, reg
, len
, len
, val
);
448 static int r820t_set_mux(struct r820t_priv
*priv
, u32 freq
)
450 const struct r820t_freq_range
*range
;
452 u8 val
, reg08
, reg09
;
454 /* Get the proper frequency range */
455 freq
= freq
/ 1000000;
456 for (i
= 0; i
< ARRAY_SIZE(freq_ranges
) - 1; i
++) {
457 if (freq
< freq_ranges
[i
+ 1].freq
)
460 range
= &freq_ranges
[i
];
462 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i
, freq
);
465 rc
= r820t_write_reg_mask(priv
, 0x17, range
->open_d
, 0x08);
470 rc
= r820t_write_reg_mask(priv
, 0x1a, range
->rf_mux_ploy
, 0xc3);
475 rc
= r820t_write_reg(priv
, 0x1b, range
->tf_c
);
479 /* XTAL CAP & Drive */
480 switch (priv
->xtal_cap_sel
) {
481 case XTAL_LOW_CAP_30P
:
482 case XTAL_LOW_CAP_20P
:
483 val
= range
->xtal_cap20p
| 0x08;
485 case XTAL_LOW_CAP_10P
:
486 val
= range
->xtal_cap10p
| 0x08;
488 case XTAL_HIGH_CAP_0P
:
489 val
= range
->xtal_cap0p
| 0x00;
492 case XTAL_LOW_CAP_0P
:
493 val
= range
->xtal_cap0p
| 0x08;
496 rc
= r820t_write_reg_mask(priv
, 0x10, val
, 0x0b);
500 if (priv
->imr_done
) {
501 reg08
= priv
->imr_data
[range
->imr_mem
].gain_x
;
502 reg09
= priv
->imr_data
[range
->imr_mem
].phase_y
;
507 rc
= r820t_write_reg_mask(priv
, 0x08, reg08
, 0x3f);
511 rc
= r820t_write_reg_mask(priv
, 0x09, reg09
, 0x3f);
516 static int r820t_set_pll(struct r820t_priv
*priv
, enum v4l2_tuner_type type
,
521 unsigned sleep_time
= 10000;
522 u32 vco_fra
; /* VCO contribution by SDM (kHz) */
523 u32 vco_min
= 1770000;
524 u32 vco_max
= vco_min
* 2;
532 u8 ni
, si
, nint
, vco_fine_tune
, val
;
535 /* Frequency in kHz */
537 pll_ref
= priv
->cfg
->xtal
/ 1000;
540 /* Doesn't exist on rtl-sdk, and on field tests, caused troubles */
541 if ((priv
->cfg
->rafael_chip
== CHIP_R620D
) ||
542 (priv
->cfg
->rafael_chip
== CHIP_R828D
) ||
543 (priv
->cfg
->rafael_chip
== CHIP_R828
)) {
544 /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
545 if (type
!= V4L2_TUNER_DIGITAL_TV
) {
551 if (priv
->cfg
->xtal
> 24000000) {
558 rc
= r820t_write_reg_mask(priv
, 0x10, refdiv2
, 0x10);
562 /* set pll autotune = 128kHz */
563 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
567 /* set VCO current = 100 */
568 rc
= r820t_write_reg_mask(priv
, 0x12, 0x80, 0xe0);
572 /* Calculate divider */
573 while (mix_div
<= 64) {
574 if (((freq
* mix_div
) >= vco_min
) &&
575 ((freq
* mix_div
) < vco_max
)) {
577 while (div_buf
> 2) {
578 div_buf
= div_buf
>> 1;
583 mix_div
= mix_div
<< 1;
586 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
590 vco_fine_tune
= (data
[4] & 0x30) >> 4;
592 tuner_dbg("mix_div=%d div_num=%d vco_fine_tune=%d\n",
593 mix_div
, div_num
, vco_fine_tune
);
596 * XXX: R828D/16MHz seems to have always vco_fine_tune=1.
597 * Due to that, this calculation goes wrong.
599 if (priv
->cfg
->rafael_chip
!= CHIP_R828D
) {
600 if (vco_fine_tune
> VCO_POWER_REF
)
601 div_num
= div_num
- 1;
602 else if (vco_fine_tune
< VCO_POWER_REF
)
603 div_num
= div_num
+ 1;
606 rc
= r820t_write_reg_mask(priv
, 0x10, div_num
<< 5, 0xe0);
610 vco_freq
= freq
* mix_div
;
611 nint
= vco_freq
/ (2 * pll_ref
);
612 vco_fra
= vco_freq
- 2 * pll_ref
* nint
;
614 /* boundary spur prevention */
615 if (vco_fra
< pll_ref
/ 64) {
617 } else if (vco_fra
> pll_ref
* 127 / 64) {
620 } else if ((vco_fra
> pll_ref
* 127 / 128) && (vco_fra
< pll_ref
)) {
621 vco_fra
= pll_ref
* 127 / 128;
622 } else if ((vco_fra
> pll_ref
) && (vco_fra
< pll_ref
* 129 / 128)) {
623 vco_fra
= pll_ref
* 129 / 128;
626 ni
= (nint
- 13) / 4;
627 si
= nint
- 4 * ni
- 13;
629 rc
= r820t_write_reg(priv
, 0x14, ni
+ (si
<< 6));
639 rc
= r820t_write_reg_mask(priv
, 0x12, val
, 0x08);
644 while (vco_fra
> 1) {
645 if (vco_fra
> (2 * pll_ref
/ n_sdm
)) {
646 sdm
= sdm
+ 32768 / (n_sdm
/ 2);
647 vco_fra
= vco_fra
- 2 * pll_ref
/ n_sdm
;
654 tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
655 freq
, pll_ref
, refdiv2
? " / 2" : "", sdm
);
657 rc
= r820t_write_reg(priv
, 0x16, sdm
>> 8);
660 rc
= r820t_write_reg(priv
, 0x15, sdm
& 0xff);
664 for (i
= 0; i
< 2; i
++) {
665 usleep_range(sleep_time
, sleep_time
+ 1000);
667 /* Check if PLL has locked */
668 rc
= r820t_read(priv
, 0x00, data
, 3);
675 /* Didn't lock. Increase VCO current */
676 rc
= r820t_write_reg_mask(priv
, 0x12, 0x60, 0xe0);
682 if (!(data
[2] & 0x40)) {
683 priv
->has_lock
= false;
687 priv
->has_lock
= true;
688 tuner_dbg("tuner has lock at frequency %d kHz\n", freq
);
690 /* set pll autotune = 8kHz */
691 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x08, 0x08);
696 static int r820t_sysfreq_sel(struct r820t_priv
*priv
, u32 freq
,
697 enum v4l2_tuner_type type
,
702 u8 mixer_top
, lna_top
, cp_cur
, div_buf_cur
, lna_vth_l
, mixer_vth_l
;
703 u8 air_cable1_in
, cable2_in
, pre_dect
, lna_discharge
, filter_cur
;
705 tuner_dbg("adjusting tuner parameters for the standard\n");
709 if ((freq
== 506000000) || (freq
== 666000000) ||
710 (freq
== 818000000)) {
711 mixer_top
= 0x14; /* mixer top:14 , top-1, low-discharge */
712 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
713 cp_cur
= 0x28; /* 101, 0.2 */
714 div_buf_cur
= 0x20; /* 10, 200u */
716 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
717 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
718 cp_cur
= 0x38; /* 111, auto */
719 div_buf_cur
= 0x30; /* 11, 150u */
721 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
722 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
723 air_cable1_in
= 0x00;
727 filter_cur
= 0x40; /* 10, low */
730 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
731 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
732 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
733 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
734 air_cable1_in
= 0x00;
738 cp_cur
= 0x38; /* 111, auto */
739 div_buf_cur
= 0x30; /* 11, 150u */
740 filter_cur
= 0x40; /* 10, low */
743 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
744 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
745 lna_vth_l
= 0x75; /* lna vth 1.04 , vtl 0.84 */
746 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
747 air_cable1_in
= 0x00;
751 cp_cur
= 0x38; /* 111, auto */
752 div_buf_cur
= 0x30; /* 11, 150u */
753 filter_cur
= 0x40; /* 10, low */
755 case SYS_DVBC_ANNEX_A
:
756 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
760 air_cable1_in
= 0x60;
764 cp_cur
= 0x38; /* 111, auto */
765 div_buf_cur
= 0x30; /* 11, 150u */
766 filter_cur
= 0x40; /* 10, low */
768 default: /* DVB-T 8M */
769 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
770 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
771 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
772 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
773 air_cable1_in
= 0x00;
777 cp_cur
= 0x38; /* 111, auto */
778 div_buf_cur
= 0x30; /* 11, 150u */
779 filter_cur
= 0x40; /* 10, low */
783 if (priv
->cfg
->use_diplexer
&&
784 ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
785 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
786 (priv
->cfg
->rafael_chip
== CHIP_R820C
))) {
788 air_cable1_in
= 0x00;
790 air_cable1_in
= 0x60;
795 if (priv
->cfg
->use_predetect
) {
796 rc
= r820t_write_reg_mask(priv
, 0x06, pre_dect
, 0x40);
801 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0xc7);
804 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0xf8);
807 rc
= r820t_write_reg(priv
, 0x0d, lna_vth_l
);
810 rc
= r820t_write_reg(priv
, 0x0e, mixer_vth_l
);
814 /* Air-IN only for Astrometa */
815 rc
= r820t_write_reg_mask(priv
, 0x05, air_cable1_in
, 0x60);
818 rc
= r820t_write_reg_mask(priv
, 0x06, cable2_in
, 0x08);
822 rc
= r820t_write_reg_mask(priv
, 0x11, cp_cur
, 0x38);
825 rc
= r820t_write_reg_mask(priv
, 0x17, div_buf_cur
, 0x30);
828 rc
= r820t_write_reg_mask(priv
, 0x0a, filter_cur
, 0x60);
832 * Original driver initializes regs 0x05 and 0x06 with the
833 * same value again on this point. Probably, it is just an
841 tuner_dbg("adjusting LNA parameters\n");
842 if (type
!= V4L2_TUNER_ANALOG_TV
) {
843 /* LNA TOP: lowest */
844 rc
= r820t_write_reg_mask(priv
, 0x1d, 0, 0x38);
849 rc
= r820t_write_reg_mask(priv
, 0x1c, 0, 0x04);
853 /* 0: PRE_DECT off */
854 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
859 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x30, 0x30);
865 /* write LNA TOP = 3 */
866 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x18, 0x38);
871 * write discharge mode
872 * FIXME: IMHO, the mask here is wrong, but it matches
873 * what's there at the original driver
875 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
879 /* LNA discharge current */
880 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
885 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x20, 0x30);
890 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
895 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0x38);
900 * write discharge mode
901 * FIXME: IMHO, the mask here is wrong, but it matches
902 * what's there at the original driver
904 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
908 /* LNA discharge current */
909 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
913 /* agc clk 1Khz, external det1 cap 1u */
914 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x30);
918 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x04);
925 static int r820t_set_tv_standard(struct r820t_priv
*priv
,
927 enum v4l2_tuner_type type
,
928 v4l2_std_id std
, u32 delsys
)
932 u32 if_khz
, filt_cal_lo
;
934 u8 filt_gain
, img_r
, filt_q
, hp_cor
, ext_enable
, loop_through
;
935 u8 lt_att
, flt_ext_widest
, polyfil_cur
;
936 bool need_calibration
;
938 tuner_dbg("selecting the delivery system\n");
940 if (delsys
== SYS_ISDBT
) {
943 filt_gain
= 0x10; /* +3db, 6mhz on */
944 img_r
= 0x00; /* image negative */
945 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
946 hp_cor
= 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
947 ext_enable
= 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
948 loop_through
= 0x00; /* r5[7], lt on */
949 lt_att
= 0x00; /* r31[7], lt att enable */
950 flt_ext_widest
= 0x80; /* r15[7]: flt_ext_wide on */
951 polyfil_cur
= 0x60; /* r25[6:5]:min */
952 } else if (delsys
== SYS_DVBC_ANNEX_A
) {
955 filt_gain
= 0x10; /* +3db, 6mhz on */
956 img_r
= 0x00; /* image negative */
957 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
958 hp_cor
= 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
959 ext_enable
= 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
960 loop_through
= 0x00; /* r5[7], lt on */
961 lt_att
= 0x00; /* r31[7], lt att enable */
962 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
963 polyfil_cur
= 0x60; /* r25[6:5]:min */
964 } else if (delsys
== SYS_DVBC_ANNEX_C
) {
967 filt_gain
= 0x10; /* +3db, 6mhz on */
968 img_r
= 0x00; /* image negative */
969 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
970 hp_cor
= 0x6a; /* 1.7m disable, +0cap, 1.0mhz */
971 ext_enable
= 0x40; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
972 loop_through
= 0x00; /* r5[7], lt on */
973 lt_att
= 0x00; /* r31[7], lt att enable */
974 flt_ext_widest
= 0x80; /* r15[7]: flt_ext_wide on */
975 polyfil_cur
= 0x60; /* r25[6:5]:min */
979 filt_cal_lo
= 56000; /* 52000->56000 */
980 filt_gain
= 0x10; /* +3db, 6mhz on */
981 img_r
= 0x00; /* image negative */
982 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
983 hp_cor
= 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
984 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
985 loop_through
= 0x00; /* r5[7], lt on */
986 lt_att
= 0x00; /* r31[7], lt att enable */
987 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
988 polyfil_cur
= 0x60; /* r25[6:5]:min */
989 } else if (bw
== 7) {
992 * There are two 7 MHz tables defined on the original
993 * driver, but just the second one seems to be visible
994 * by rtl2832. Keep this one here commented, as it
995 * might be needed in the future
1000 filt_gain
= 0x10; /* +3db, 6mhz on */
1001 img_r
= 0x00; /* image negative */
1002 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1003 hp_cor
= 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
1004 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1005 loop_through
= 0x00; /* r5[7], lt on */
1006 lt_att
= 0x00; /* r31[7], lt att enable */
1007 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1008 polyfil_cur
= 0x60; /* r25[6:5]:min */
1010 /* 7 MHz, second table */
1012 filt_cal_lo
= 63000;
1013 filt_gain
= 0x10; /* +3db, 6mhz on */
1014 img_r
= 0x00; /* image negative */
1015 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1016 hp_cor
= 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
1017 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1018 loop_through
= 0x00; /* r5[7], lt on */
1019 lt_att
= 0x00; /* r31[7], lt att enable */
1020 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1021 polyfil_cur
= 0x60; /* r25[6:5]:min */
1024 filt_cal_lo
= 68500;
1025 filt_gain
= 0x10; /* +3db, 6mhz on */
1026 img_r
= 0x00; /* image negative */
1027 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1028 hp_cor
= 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
1029 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1030 loop_through
= 0x00; /* r5[7], lt on */
1031 lt_att
= 0x00; /* r31[7], lt att enable */
1032 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1033 polyfil_cur
= 0x60; /* r25[6:5]:min */
1037 /* Initialize the shadow registers */
1038 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1040 /* Init Flag & Xtal_check Result */
1042 val
= 1 | priv
->xtal_cap_sel
<< 1;
1045 rc
= r820t_write_reg_mask(priv
, 0x0c, val
, 0x0f);
1050 rc
= r820t_write_reg_mask(priv
, 0x13, VER_NUM
, 0x3f);
1054 /* for LT Gain test */
1055 if (type
!= V4L2_TUNER_ANALOG_TV
) {
1056 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x00, 0x38);
1059 usleep_range(1000, 2000);
1061 priv
->int_freq
= if_khz
* 1000;
1063 /* Check if standard changed. If so, filter calibration is needed */
1064 if (type
!= priv
->type
)
1065 need_calibration
= true;
1066 else if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
!= priv
->std
))
1067 need_calibration
= true;
1068 else if ((type
== V4L2_TUNER_DIGITAL_TV
) &&
1069 ((delsys
!= priv
->delsys
) || bw
!= priv
->bw
))
1070 need_calibration
= true;
1072 need_calibration
= false;
1074 if (need_calibration
) {
1075 tuner_dbg("calibrating the tuner\n");
1076 for (i
= 0; i
< 2; i
++) {
1078 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0x60);
1082 /* set cali clk =on */
1083 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x04, 0x04);
1087 /* X'tal cap 0pF for PLL */
1088 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x03);
1092 rc
= r820t_set_pll(priv
, type
, filt_cal_lo
* 1000);
1093 if (rc
< 0 || !priv
->has_lock
)
1097 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x10, 0x10);
1101 usleep_range(1000, 2000);
1104 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x00, 0x10);
1108 /* set cali clk =off */
1109 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x00, 0x04);
1113 /* Check if calibration worked */
1114 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1118 priv
->fil_cal_code
= data
[4] & 0x0f;
1119 if (priv
->fil_cal_code
&& priv
->fil_cal_code
!= 0x0f)
1123 if (priv
->fil_cal_code
== 0x0f)
1124 priv
->fil_cal_code
= 0;
1127 rc
= r820t_write_reg_mask(priv
, 0x0a,
1128 filt_q
| priv
->fil_cal_code
, 0x1f);
1132 /* Set BW, Filter_gain, & HP corner */
1133 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0xef);
1139 rc
= r820t_write_reg_mask(priv
, 0x07, img_r
, 0x80);
1143 /* Set filt_3dB, V6MHz */
1144 rc
= r820t_write_reg_mask(priv
, 0x06, filt_gain
, 0x30);
1148 /* channel filter extension */
1149 rc
= r820t_write_reg_mask(priv
, 0x1e, ext_enable
, 0x60);
1154 rc
= r820t_write_reg_mask(priv
, 0x05, loop_through
, 0x80);
1158 /* Loop through attenuation */
1159 rc
= r820t_write_reg_mask(priv
, 0x1f, lt_att
, 0x80);
1163 /* filter extension widest */
1164 rc
= r820t_write_reg_mask(priv
, 0x0f, flt_ext_widest
, 0x80);
1168 /* RF poly filter current */
1169 rc
= r820t_write_reg_mask(priv
, 0x19, polyfil_cur
, 0x60);
1173 /* Store current standard. If it changes, re-calibrate the tuner */
1174 priv
->delsys
= delsys
;
1182 static int r820t_read_gain(struct r820t_priv
*priv
)
1187 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1191 return ((data
[3] & 0x08) << 1) + ((data
[3] & 0xf0) >> 4);
1195 /* FIXME: This routine requires more testing */
1198 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
1199 * input power, for raw results see:
1200 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
1203 static const int r820t_lna_gain_steps
[] = {
1204 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
1207 static const int r820t_mixer_gain_steps
[] = {
1208 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
1211 static int r820t_set_gain_mode(struct r820t_priv
*priv
,
1212 bool set_manual_gain
,
1217 if (set_manual_gain
) {
1218 int i
, total_gain
= 0;
1219 uint8_t mix_index
= 0, lna_index
= 0;
1223 rc
= r820t_write_reg_mask(priv
, 0x05, 0x10, 0x10);
1227 /* Mixer auto off */
1228 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1232 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1236 /* set fixed VGA gain for now (16.3 dB) */
1237 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x08, 0x9f);
1241 for (i
= 0; i
< 15; i
++) {
1242 if (total_gain
>= gain
)
1245 total_gain
+= r820t_lna_gain_steps
[++lna_index
];
1247 if (total_gain
>= gain
)
1250 total_gain
+= r820t_mixer_gain_steps
[++mix_index
];
1254 rc
= r820t_write_reg_mask(priv
, 0x05, lna_index
, 0x0f);
1258 /* set Mixer gain */
1259 rc
= r820t_write_reg_mask(priv
, 0x07, mix_index
, 0x0f);
1264 rc
= r820t_write_reg_mask(priv
, 0x05, 0, 0x10);
1269 rc
= r820t_write_reg_mask(priv
, 0x07, 0x10, 0x10);
1273 /* set fixed VGA gain for now (26.5 dB) */
1274 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1283 static int generic_set_freq(struct dvb_frontend
*fe
,
1284 u32 freq
/* in HZ */,
1286 enum v4l2_tuner_type type
,
1287 v4l2_std_id std
, u32 delsys
)
1289 struct r820t_priv
*priv
= fe
->tuner_priv
;
1293 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1296 rc
= r820t_set_tv_standard(priv
, bw
, type
, std
, delsys
);
1300 if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
== V4L2_STD_SECAM_LC
))
1301 lo_freq
= freq
- priv
->int_freq
;
1303 lo_freq
= freq
+ priv
->int_freq
;
1305 rc
= r820t_set_mux(priv
, lo_freq
);
1309 rc
= r820t_set_pll(priv
, type
, lo_freq
);
1310 if (rc
< 0 || !priv
->has_lock
)
1313 rc
= r820t_sysfreq_sel(priv
, freq
, type
, std
, delsys
);
1317 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1318 __func__
, freq
, r820t_read_gain(priv
));
1323 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
1328 * r820t standby logic
1331 static int r820t_standby(struct r820t_priv
*priv
)
1335 /* If device was not initialized yet, don't need to standby */
1336 if (!priv
->init_done
)
1339 rc
= r820t_write_reg(priv
, 0x06, 0xb1);
1342 rc
= r820t_write_reg(priv
, 0x05, 0x03);
1345 rc
= r820t_write_reg(priv
, 0x07, 0x3a);
1348 rc
= r820t_write_reg(priv
, 0x08, 0x40);
1351 rc
= r820t_write_reg(priv
, 0x09, 0xc0);
1354 rc
= r820t_write_reg(priv
, 0x0a, 0x36);
1357 rc
= r820t_write_reg(priv
, 0x0c, 0x35);
1360 rc
= r820t_write_reg(priv
, 0x0f, 0x68);
1363 rc
= r820t_write_reg(priv
, 0x11, 0x03);
1366 rc
= r820t_write_reg(priv
, 0x17, 0xf4);
1369 rc
= r820t_write_reg(priv
, 0x19, 0x0c);
1371 /* Force initial calibration */
1378 * r820t device init logic
1381 static int r820t_xtal_check(struct r820t_priv
*priv
)
1386 /* Initialize the shadow registers */
1387 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1389 /* cap 30pF & Drive Low */
1390 rc
= r820t_write_reg_mask(priv
, 0x10, 0x0b, 0x0b);
1394 /* set pll autotune = 128kHz */
1395 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
1399 /* set manual initial reg = 111111; */
1400 rc
= r820t_write_reg_mask(priv
, 0x13, 0x7f, 0x7f);
1405 rc
= r820t_write_reg_mask(priv
, 0x13, 0x00, 0x40);
1409 /* Try several xtal capacitor alternatives */
1410 for (i
= 0; i
< ARRAY_SIZE(r820t_xtal_capacitor
); i
++) {
1411 rc
= r820t_write_reg_mask(priv
, 0x10,
1412 r820t_xtal_capacitor
[i
][0], 0x1b);
1416 usleep_range(5000, 6000);
1418 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1421 if (!(data
[2] & 0x40))
1424 val
= data
[2] & 0x3f;
1426 if (priv
->cfg
->xtal
== 16000000 && (val
> 29 || val
< 23))
1433 if (i
== ARRAY_SIZE(r820t_xtal_capacitor
))
1436 return r820t_xtal_capacitor
[i
][1];
1439 static int r820t_imr_prepare(struct r820t_priv
*priv
)
1443 /* Initialize the shadow registers */
1444 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1446 /* lna off (air-in off) */
1447 rc
= r820t_write_reg_mask(priv
, 0x05, 0x20, 0x20);
1451 /* mixer gain mode = manual */
1452 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1456 /* filter corner = lowest */
1457 rc
= r820t_write_reg_mask(priv
, 0x0a, 0x0f, 0x0f);
1461 /* filter bw=+2cap, hp=5M */
1462 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x60, 0x6f);
1466 /* adc=on, vga code mode, gain = 26.5dB */
1467 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1472 rc
= r820t_write_reg_mask(priv
, 0x0f, 0, 0x08);
1476 /* ring power = on */
1477 rc
= r820t_write_reg_mask(priv
, 0x18, 0x10, 0x10);
1481 /* from ring = ring pll in */
1482 rc
= r820t_write_reg_mask(priv
, 0x1c, 0x02, 0x02);
1486 /* sw_pdect = det3 */
1487 rc
= r820t_write_reg_mask(priv
, 0x1e, 0x80, 0x80);
1492 rc
= r820t_write_reg_mask(priv
, 0x06, 0x20, 0x20);
1497 static int r820t_multi_read(struct r820t_priv
*priv
)
1501 u8 data
[2], min
= 255, max
= 0;
1503 usleep_range(5000, 6000);
1505 for (i
= 0; i
< 6; i
++) {
1506 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1518 rc
= sum
- max
- min
;
1523 static int r820t_imr_cross(struct r820t_priv
*priv
,
1524 struct r820t_sect_type iq_point
[3],
1527 struct r820t_sect_type cross
[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
1528 struct r820t_sect_type tmp
;
1532 reg08
= r820t_read_cache_reg(priv
, 8) & 0xc0;
1533 reg09
= r820t_read_cache_reg(priv
, 9) & 0xc0;
1539 for (i
= 0; i
< 5; i
++) {
1542 cross
[i
].gain_x
= reg08
;
1543 cross
[i
].phase_y
= reg09
;
1546 cross
[i
].gain_x
= reg08
; /* 0 */
1547 cross
[i
].phase_y
= reg09
+ 1; /* Q-1 */
1550 cross
[i
].gain_x
= reg08
; /* 0 */
1551 cross
[i
].phase_y
= (reg09
| 0x20) + 1; /* I-1 */
1554 cross
[i
].gain_x
= reg08
+ 1; /* Q-1 */
1555 cross
[i
].phase_y
= reg09
;
1558 cross
[i
].gain_x
= (reg08
| 0x20) + 1; /* I-1 */
1559 cross
[i
].phase_y
= reg09
;
1562 rc
= r820t_write_reg(priv
, 0x08, cross
[i
].gain_x
);
1566 rc
= r820t_write_reg(priv
, 0x09, cross
[i
].phase_y
);
1570 rc
= r820t_multi_read(priv
);
1574 cross
[i
].value
= rc
;
1576 if (cross
[i
].value
< tmp
.value
)
1580 if ((tmp
.phase_y
& 0x1f) == 1) { /* y-direction */
1583 iq_point
[0] = cross
[0];
1584 iq_point
[1] = cross
[1];
1585 iq_point
[2] = cross
[2];
1586 } else { /* (0,0) or x-direction */
1589 iq_point
[0] = cross
[0];
1590 iq_point
[1] = cross
[3];
1591 iq_point
[2] = cross
[4];
1596 static void r820t_compre_cor(struct r820t_sect_type iq
[3])
1600 for (i
= 3; i
> 0; i
--) {
1601 if (iq
[0].value
> iq
[i
- 1].value
)
1602 swap(iq
[0], iq
[i
- 1]);
1606 static int r820t_compre_step(struct r820t_priv
*priv
,
1607 struct r820t_sect_type iq
[3], u8 reg
)
1610 struct r820t_sect_type tmp
;
1613 * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
1615 * new < min => update to min and continue
1619 /* min value already saved in iq[0] */
1620 tmp
.phase_y
= iq
[0].phase_y
;
1621 tmp
.gain_x
= iq
[0].gain_x
;
1623 while (((tmp
.gain_x
& 0x1f) < IMR_TRIAL
) &&
1624 ((tmp
.phase_y
& 0x1f) < IMR_TRIAL
)) {
1630 rc
= r820t_write_reg(priv
, 0x08, tmp
.gain_x
);
1634 rc
= r820t_write_reg(priv
, 0x09, tmp
.phase_y
);
1638 rc
= r820t_multi_read(priv
);
1643 if (tmp
.value
<= iq
[0].value
) {
1644 iq
[0].gain_x
= tmp
.gain_x
;
1645 iq
[0].phase_y
= tmp
.phase_y
;
1646 iq
[0].value
= tmp
.value
;
1656 static int r820t_iq_tree(struct r820t_priv
*priv
,
1657 struct r820t_sect_type iq
[3],
1658 u8 fix_val
, u8 var_val
, u8 fix_reg
)
1664 * record IMC results by input gain/phase location then adjust
1665 * gain or phase positive 1 step and negtive 1 step,
1666 * both record results
1669 if (fix_reg
== 0x08)
1674 for (i
= 0; i
< 3; i
++) {
1675 rc
= r820t_write_reg(priv
, fix_reg
, fix_val
);
1679 rc
= r820t_write_reg(priv
, var_reg
, var_val
);
1683 rc
= r820t_multi_read(priv
);
1688 if (fix_reg
== 0x08) {
1689 iq
[i
].gain_x
= fix_val
;
1690 iq
[i
].phase_y
= var_val
;
1692 iq
[i
].phase_y
= fix_val
;
1693 iq
[i
].gain_x
= var_val
;
1696 if (i
== 0) { /* try right-side point */
1698 } else if (i
== 1) { /* try left-side point */
1699 /* if absolute location is 1, change I/Q direction */
1700 if ((var_val
& 0x1f) < 0x02) {
1701 tmp
= 2 - (var_val
& 0x1f);
1703 /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
1704 if (var_val
& 0x20) {
1708 var_val
|= 0x20 | tmp
;
1719 static int r820t_section(struct r820t_priv
*priv
,
1720 struct r820t_sect_type
*iq_point
)
1723 struct r820t_sect_type compare_iq
[3], compare_bet
[3];
1725 /* Try X-1 column and save min result to compare_bet[0] */
1726 if (!(iq_point
->gain_x
& 0x1f))
1727 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) & 0xdf) + 1; /* Q-path, Gain=1 */
1729 compare_iq
[0].gain_x
= iq_point
->gain_x
- 1; /* left point */
1730 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1733 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1734 compare_iq
[0].phase_y
, 0x08);
1738 r820t_compre_cor(compare_iq
);
1740 compare_bet
[0] = compare_iq
[0];
1742 /* Try X column and save min result to compare_bet[1] */
1743 compare_iq
[0].gain_x
= iq_point
->gain_x
;
1744 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1746 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1747 compare_iq
[0].phase_y
, 0x08);
1751 r820t_compre_cor(compare_iq
);
1753 compare_bet
[1] = compare_iq
[0];
1755 /* Try X+1 column and save min result to compare_bet[2] */
1756 if ((iq_point
->gain_x
& 0x1f) == 0x00)
1757 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) | 0x20) + 1; /* I-path, Gain=1 */
1759 compare_iq
[0].gain_x
= iq_point
->gain_x
+ 1;
1760 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1762 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1763 compare_iq
[0].phase_y
, 0x08);
1767 r820t_compre_cor(compare_iq
);
1769 compare_bet
[2] = compare_iq
[0];
1771 r820t_compre_cor(compare_bet
);
1773 *iq_point
= compare_bet
[0];
1778 static int r820t_vga_adjust(struct r820t_priv
*priv
)
1783 /* increase vga power to let image significant */
1784 for (vga_count
= 12; vga_count
< 16; vga_count
++) {
1785 rc
= r820t_write_reg_mask(priv
, 0x0c, vga_count
, 0x0f);
1789 usleep_range(10000, 11000);
1791 rc
= r820t_multi_read(priv
);
1802 static int r820t_iq(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1804 struct r820t_sect_type compare_iq
[3];
1806 u8 x_direction
= 0; /* 1:x, 0:y */
1807 u8 dir_reg
, other_reg
;
1809 r820t_vga_adjust(priv
);
1811 rc
= r820t_imr_cross(priv
, compare_iq
, &x_direction
);
1815 if (x_direction
== 1) {
1823 /* compare and find min of 3 points. determine i/q direction */
1824 r820t_compre_cor(compare_iq
);
1826 /* increase step to find min value of this direction */
1827 rc
= r820t_compre_step(priv
, compare_iq
, dir_reg
);
1831 /* the other direction */
1832 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1833 compare_iq
[0].phase_y
, dir_reg
);
1837 /* compare and find min of 3 points. determine i/q direction */
1838 r820t_compre_cor(compare_iq
);
1840 /* increase step to find min value on this direction */
1841 rc
= r820t_compre_step(priv
, compare_iq
, other_reg
);
1845 /* check 3 points again */
1846 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1847 compare_iq
[0].phase_y
, other_reg
);
1851 r820t_compre_cor(compare_iq
);
1853 /* section-9 check */
1854 rc
= r820t_section(priv
, compare_iq
);
1856 *iq_pont
= compare_iq
[0];
1858 /* reset gain/phase control setting */
1859 rc
= r820t_write_reg_mask(priv
, 0x08, 0, 0x3f);
1863 rc
= r820t_write_reg_mask(priv
, 0x09, 0, 0x3f);
1868 static int r820t_f_imr(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1872 r820t_vga_adjust(priv
);
1875 * search surrounding points from previous point
1876 * try (x-1), (x), (x+1) columns, and find min IMR result point
1878 rc
= r820t_section(priv
, iq_pont
);
1885 static int r820t_imr(struct r820t_priv
*priv
, unsigned imr_mem
, bool im_flag
)
1887 struct r820t_sect_type imr_point
;
1889 u32 ring_vco
, ring_freq
, ring_ref
;
1891 int reg18
, reg19
, reg1f
;
1893 if (priv
->cfg
->xtal
> 24000000)
1894 ring_ref
= priv
->cfg
->xtal
/ 2000;
1896 ring_ref
= priv
->cfg
->xtal
/ 1000;
1899 for (n
= 0; n
< 16; n
++) {
1900 if ((16 + n
) * 8 * ring_ref
>= 3100000) {
1906 reg18
= r820t_read_cache_reg(priv
, 0x18);
1907 reg19
= r820t_read_cache_reg(priv
, 0x19);
1908 reg1f
= r820t_read_cache_reg(priv
, 0x1f);
1910 reg18
&= 0xf0; /* set ring[3:0] */
1913 ring_vco
= (16 + n_ring
) * 8 * ring_ref
;
1915 reg18
&= 0xdf; /* clear ring_se23 */
1916 reg19
&= 0xfc; /* clear ring_seldiv */
1917 reg1f
&= 0xfc; /* clear ring_att */
1921 ring_freq
= ring_vco
/ 48;
1922 reg18
|= 0x20; /* ring_se23 = 1 */
1923 reg19
|= 0x03; /* ring_seldiv = 3 */
1924 reg1f
|= 0x02; /* ring_att 10 */
1927 ring_freq
= ring_vco
/ 16;
1928 reg18
|= 0x00; /* ring_se23 = 0 */
1929 reg19
|= 0x02; /* ring_seldiv = 2 */
1930 reg1f
|= 0x00; /* pw_ring 00 */
1933 ring_freq
= ring_vco
/ 8;
1934 reg18
|= 0x00; /* ring_se23 = 0 */
1935 reg19
|= 0x01; /* ring_seldiv = 1 */
1936 reg1f
|= 0x03; /* pw_ring 11 */
1939 ring_freq
= ring_vco
/ 6;
1940 reg18
|= 0x20; /* ring_se23 = 1 */
1941 reg19
|= 0x00; /* ring_seldiv = 0 */
1942 reg1f
|= 0x03; /* pw_ring 11 */
1945 ring_freq
= ring_vco
/ 4;
1946 reg18
|= 0x00; /* ring_se23 = 0 */
1947 reg19
|= 0x00; /* ring_seldiv = 0 */
1948 reg1f
|= 0x01; /* pw_ring 01 */
1951 ring_freq
= ring_vco
/ 4;
1952 reg18
|= 0x00; /* ring_se23 = 0 */
1953 reg19
|= 0x00; /* ring_seldiv = 0 */
1954 reg1f
|= 0x01; /* pw_ring 01 */
1959 /* write pw_ring, n_ring, ringdiv2 registers */
1961 /* n_ring, ring_se23 */
1962 rc
= r820t_write_reg(priv
, 0x18, reg18
);
1967 rc
= r820t_write_reg(priv
, 0x19, reg19
);
1972 rc
= r820t_write_reg(priv
, 0x1f, reg1f
);
1976 /* mux input freq ~ rf_in freq */
1977 rc
= r820t_set_mux(priv
, (ring_freq
- 5300) * 1000);
1981 rc
= r820t_set_pll(priv
, V4L2_TUNER_DIGITAL_TV
,
1982 (ring_freq
- 5300) * 1000);
1983 if (!priv
->has_lock
)
1989 rc
= r820t_iq(priv
, &imr_point
);
1991 imr_point
.gain_x
= priv
->imr_data
[3].gain_x
;
1992 imr_point
.phase_y
= priv
->imr_data
[3].phase_y
;
1993 imr_point
.value
= priv
->imr_data
[3].value
;
1995 rc
= r820t_f_imr(priv
, &imr_point
);
2000 /* save IMR value */
2003 priv
->imr_data
[0].gain_x
= imr_point
.gain_x
;
2004 priv
->imr_data
[0].phase_y
= imr_point
.phase_y
;
2005 priv
->imr_data
[0].value
= imr_point
.value
;
2008 priv
->imr_data
[1].gain_x
= imr_point
.gain_x
;
2009 priv
->imr_data
[1].phase_y
= imr_point
.phase_y
;
2010 priv
->imr_data
[1].value
= imr_point
.value
;
2013 priv
->imr_data
[2].gain_x
= imr_point
.gain_x
;
2014 priv
->imr_data
[2].phase_y
= imr_point
.phase_y
;
2015 priv
->imr_data
[2].value
= imr_point
.value
;
2018 priv
->imr_data
[3].gain_x
= imr_point
.gain_x
;
2019 priv
->imr_data
[3].phase_y
= imr_point
.phase_y
;
2020 priv
->imr_data
[3].value
= imr_point
.value
;
2023 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
2024 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
2025 priv
->imr_data
[4].value
= imr_point
.value
;
2028 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
2029 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
2030 priv
->imr_data
[4].value
= imr_point
.value
;
2037 static int r820t_imr_callibrate(struct r820t_priv
*priv
)
2042 if (priv
->init_done
)
2045 /* Detect Xtal capacitance */
2046 if ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
2047 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
2048 (priv
->cfg
->rafael_chip
== CHIP_R820C
)) {
2049 priv
->xtal_cap_sel
= XTAL_HIGH_CAP_0P
;
2051 /* Initialize registers */
2052 rc
= r820t_write(priv
, 0x05,
2053 r820t_init_array
, sizeof(r820t_init_array
));
2056 for (i
= 0; i
< 3; i
++) {
2057 rc
= r820t_xtal_check(priv
);
2060 if (!i
|| rc
> xtal_cap
)
2063 priv
->xtal_cap_sel
= xtal_cap
;
2067 * Disables IMR callibration. That emulates the same behaviour
2068 * as what is done by rtl-sdr userspace library. Useful for testing
2071 priv
->init_done
= true;
2076 /* Initialize registers */
2077 rc
= r820t_write(priv
, 0x05,
2078 r820t_init_array
, sizeof(r820t_init_array
));
2082 rc
= r820t_imr_prepare(priv
);
2086 rc
= r820t_imr(priv
, 3, true);
2089 rc
= r820t_imr(priv
, 1, false);
2092 rc
= r820t_imr(priv
, 0, false);
2095 rc
= r820t_imr(priv
, 2, false);
2098 rc
= r820t_imr(priv
, 4, false);
2102 priv
->init_done
= true;
2103 priv
->imr_done
= true;
2109 /* Not used, for now */
2110 static int r820t_gpio(struct r820t_priv
*priv
, bool enable
)
2112 return r820t_write_reg_mask(priv
, 0x0f, enable
? 1 : 0, 0x01);
2117 * r820t frontend operations and tuner attach code
2119 * All driver locks and i2c control are only in this part of the code
2122 static int r820t_init(struct dvb_frontend
*fe
)
2124 struct r820t_priv
*priv
= fe
->tuner_priv
;
2127 tuner_dbg("%s:\n", __func__
);
2129 mutex_lock(&priv
->lock
);
2130 if (fe
->ops
.i2c_gate_ctrl
)
2131 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2133 rc
= r820t_imr_callibrate(priv
);
2137 /* Initialize registers */
2138 rc
= r820t_write(priv
, 0x05,
2139 r820t_init_array
, sizeof(r820t_init_array
));
2142 if (fe
->ops
.i2c_gate_ctrl
)
2143 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2144 mutex_unlock(&priv
->lock
);
2147 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2151 static int r820t_sleep(struct dvb_frontend
*fe
)
2153 struct r820t_priv
*priv
= fe
->tuner_priv
;
2156 tuner_dbg("%s:\n", __func__
);
2158 mutex_lock(&priv
->lock
);
2159 if (fe
->ops
.i2c_gate_ctrl
)
2160 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2162 rc
= r820t_standby(priv
);
2164 if (fe
->ops
.i2c_gate_ctrl
)
2165 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2166 mutex_unlock(&priv
->lock
);
2168 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2172 static int r820t_set_analog_freq(struct dvb_frontend
*fe
,
2173 struct analog_parameters
*p
)
2175 struct r820t_priv
*priv
= fe
->tuner_priv
;
2179 tuner_dbg("%s called\n", __func__
);
2181 /* if std is not defined, choose one */
2183 p
->std
= V4L2_STD_MN
;
2185 if ((p
->std
== V4L2_STD_PAL_M
) || (p
->std
== V4L2_STD_NTSC
))
2190 mutex_lock(&priv
->lock
);
2191 if (fe
->ops
.i2c_gate_ctrl
)
2192 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2194 rc
= generic_set_freq(fe
, 62500l * p
->frequency
, bw
,
2195 V4L2_TUNER_ANALOG_TV
, p
->std
, SYS_UNDEFINED
);
2197 if (fe
->ops
.i2c_gate_ctrl
)
2198 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2199 mutex_unlock(&priv
->lock
);
2204 static int r820t_set_params(struct dvb_frontend
*fe
)
2206 struct r820t_priv
*priv
= fe
->tuner_priv
;
2207 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
2211 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2212 __func__
, c
->delivery_system
, c
->frequency
, c
->bandwidth_hz
);
2214 mutex_lock(&priv
->lock
);
2215 if (fe
->ops
.i2c_gate_ctrl
)
2216 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2218 bw
= (c
->bandwidth_hz
+ 500000) / 1000000;
2222 rc
= generic_set_freq(fe
, c
->frequency
, bw
,
2223 V4L2_TUNER_DIGITAL_TV
, 0, c
->delivery_system
);
2225 if (fe
->ops
.i2c_gate_ctrl
)
2226 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2227 mutex_unlock(&priv
->lock
);
2230 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2234 static int r820t_signal(struct dvb_frontend
*fe
, u16
*strength
)
2236 struct r820t_priv
*priv
= fe
->tuner_priv
;
2239 mutex_lock(&priv
->lock
);
2240 if (fe
->ops
.i2c_gate_ctrl
)
2241 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2243 if (priv
->has_lock
) {
2244 rc
= r820t_read_gain(priv
);
2248 /* A higher gain at LNA means a lower signal strength */
2249 *strength
= (45 - rc
) << 4 | 0xff;
2250 if (*strength
== 0xff)
2257 if (fe
->ops
.i2c_gate_ctrl
)
2258 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2259 mutex_unlock(&priv
->lock
);
2261 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2263 priv
->has_lock
? "PLL locked" : "no signal",
2269 static int r820t_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
2271 struct r820t_priv
*priv
= fe
->tuner_priv
;
2273 tuner_dbg("%s:\n", __func__
);
2275 *frequency
= priv
->int_freq
;
2280 static void r820t_release(struct dvb_frontend
*fe
)
2282 struct r820t_priv
*priv
= fe
->tuner_priv
;
2284 tuner_dbg("%s:\n", __func__
);
2286 mutex_lock(&r820t_list_mutex
);
2289 hybrid_tuner_release_state(priv
);
2291 mutex_unlock(&r820t_list_mutex
);
2293 fe
->tuner_priv
= NULL
;
2296 static const struct dvb_tuner_ops r820t_tuner_ops
= {
2298 .name
= "Rafael Micro R820T",
2299 .frequency_min
= 42000000,
2300 .frequency_max
= 1002000000,
2303 .release
= r820t_release
,
2304 .sleep
= r820t_sleep
,
2305 .set_params
= r820t_set_params
,
2306 .set_analog_params
= r820t_set_analog_freq
,
2307 .get_if_frequency
= r820t_get_if_frequency
,
2308 .get_rf_strength
= r820t_signal
,
2311 struct dvb_frontend
*r820t_attach(struct dvb_frontend
*fe
,
2312 struct i2c_adapter
*i2c
,
2313 const struct r820t_config
*cfg
)
2315 struct r820t_priv
*priv
;
2320 mutex_lock(&r820t_list_mutex
);
2322 instance
= hybrid_tuner_request_state(struct r820t_priv
, priv
,
2323 hybrid_tuner_instance_list
,
2328 /* memory allocation failure */
2331 /* new tuner instance */
2334 mutex_init(&priv
->lock
);
2336 fe
->tuner_priv
= priv
;
2339 /* existing tuner instance */
2340 fe
->tuner_priv
= priv
;
2344 if (fe
->ops
.i2c_gate_ctrl
)
2345 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2347 /* check if the tuner is there */
2348 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
2352 rc
= r820t_sleep(fe
);
2356 tuner_info("Rafael Micro r820t successfully identified\n");
2358 if (fe
->ops
.i2c_gate_ctrl
)
2359 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2361 mutex_unlock(&r820t_list_mutex
);
2363 memcpy(&fe
->ops
.tuner_ops
, &r820t_tuner_ops
,
2364 sizeof(struct dvb_tuner_ops
));
2368 if (fe
->ops
.i2c_gate_ctrl
)
2369 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2372 mutex_unlock(&r820t_list_mutex
);
2374 tuner_info("%s: failed=%d\n", __func__
, rc
);
2378 EXPORT_SYMBOL_GPL(r820t_attach
);
2380 MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2381 MODULE_AUTHOR("Mauro Carvalho Chehab");
2382 MODULE_LICENSE("GPL v2");