2 * Rafael Micro R820T driver
4 * Copyright (C) 2013 Mauro Carvalho Chehab
6 * This driver was written from scratch, based on an existing driver
7 * that it is part of rtl-sdr git tree, released under GPLv2:
8 * https://groups.google.com/forum/#!topic/ultra-cheap-sdr/Y3rBEOFtHug
9 * https://github.com/n1gp/gr-baz
11 * From what I understood from the threads, the original driver was converted
12 * to userspace from a Realtek tree. I couldn't find the original tree.
13 * However, the original driver look awkward on my eyes. So, I decided to
14 * write a new version from it from the scratch, while trying to reproduce
15 * everything found there.
18 * After locking, the original driver seems to have some routines to
19 * improve reception. This was not implemented here yet.
21 * RF Gain set/get is not implemented.
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
35 #include <linux/videodev2.h>
36 #include <linux/mutex.h>
37 #include <linux/slab.h>
38 #include <linux/bitrev.h>
40 #include "tuner-i2c.h"
44 * FIXME: I think that there are only 32 registers, but better safe than
45 * sorry. After finishing the driver, we may review it.
47 #define REG_SHADOW_START 5
55 module_param(debug
, int, 0644);
56 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
58 static int no_imr_cal
;
59 module_param(no_imr_cal
, int, 0444);
60 MODULE_PARM_DESC(no_imr_cal
, "Disable IMR calibration at module init");
64 * enums and structures
75 struct r820t_sect_type
{
82 struct list_head hybrid_tuner_instance_list
;
83 const struct r820t_config
*cfg
;
84 struct tuner_i2c_props i2c_props
;
89 enum xtal_cap_value xtal_cap_sel
;
96 struct r820t_sect_type imr_data
[NUM_IMR
];
98 /* Store current mode */
100 enum v4l2_tuner_type type
;
105 struct r820t_freq_range
{
113 u8 imr_mem
; /* Not used, currently */
116 #define VCO_POWER_REF 0x02
117 #define DIP_FREQ 32000000
123 static LIST_HEAD(hybrid_tuner_instance_list
);
124 static DEFINE_MUTEX(r820t_list_mutex
);
126 /* Those initial values start from REG_SHADOW_START */
127 static const u8 r820t_init_array
[NUM_REGS
] = {
128 0x83, 0x32, 0x75, /* 05 to 07 */
129 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
130 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
131 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
132 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
133 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
134 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
137 /* Tuner frequency ranges */
138 static const struct r820t_freq_range freq_ranges
[] = {
141 .open_d
= 0x08, /* low */
142 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
143 .tf_c
= 0xdf, /* R27[7:0] band2,band0 */
144 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
149 .freq
= 50, /* Start freq, in MHz */
150 .open_d
= 0x08, /* low */
151 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
152 .tf_c
= 0xbe, /* R27[7:0] band4,band1 */
153 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
158 .freq
= 55, /* Start freq, in MHz */
159 .open_d
= 0x08, /* low */
160 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
161 .tf_c
= 0x8b, /* R27[7:0] band7,band4 */
162 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
167 .freq
= 60, /* Start freq, in MHz */
168 .open_d
= 0x08, /* low */
169 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
170 .tf_c
= 0x7b, /* R27[7:0] band8,band4 */
171 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
176 .freq
= 65, /* Start freq, in MHz */
177 .open_d
= 0x08, /* low */
178 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
179 .tf_c
= 0x69, /* R27[7:0] band9,band6 */
180 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
185 .freq
= 70, /* Start freq, in MHz */
186 .open_d
= 0x08, /* low */
187 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
188 .tf_c
= 0x58, /* R27[7:0] band10,band7 */
189 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
194 .freq
= 75, /* Start freq, in MHz */
195 .open_d
= 0x00, /* high */
196 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
197 .tf_c
= 0x44, /* R27[7:0] band11,band11 */
198 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
203 .freq
= 80, /* Start freq, in MHz */
204 .open_d
= 0x00, /* high */
205 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
206 .tf_c
= 0x44, /* R27[7:0] band11,band11 */
207 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
212 .freq
= 90, /* Start freq, in MHz */
213 .open_d
= 0x00, /* high */
214 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
215 .tf_c
= 0x34, /* R27[7:0] band12,band11 */
216 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
221 .freq
= 100, /* Start freq, in MHz */
222 .open_d
= 0x00, /* high */
223 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
224 .tf_c
= 0x34, /* R27[7:0] band12,band11 */
225 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
230 .freq
= 110, /* Start freq, in MHz */
231 .open_d
= 0x00, /* high */
232 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
233 .tf_c
= 0x24, /* R27[7:0] band13,band11 */
234 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
239 .freq
= 120, /* Start freq, in MHz */
240 .open_d
= 0x00, /* high */
241 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
242 .tf_c
= 0x24, /* R27[7:0] band13,band11 */
243 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
248 .freq
= 140, /* Start freq, in MHz */
249 .open_d
= 0x00, /* high */
250 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
251 .tf_c
= 0x14, /* R27[7:0] band14,band11 */
252 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
257 .freq
= 180, /* Start freq, in MHz */
258 .open_d
= 0x00, /* high */
259 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
260 .tf_c
= 0x13, /* R27[7:0] band14,band12 */
261 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
266 .freq
= 220, /* Start freq, in MHz */
267 .open_d
= 0x00, /* high */
268 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
269 .tf_c
= 0x13, /* R27[7:0] band14,band12 */
270 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
275 .freq
= 250, /* Start freq, in MHz */
276 .open_d
= 0x00, /* high */
277 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
278 .tf_c
= 0x11, /* R27[7:0] highest,highest */
279 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
284 .freq
= 280, /* Start freq, in MHz */
285 .open_d
= 0x00, /* high */
286 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
287 .tf_c
= 0x00, /* R27[7:0] highest,highest */
288 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
293 .freq
= 310, /* Start freq, in MHz */
294 .open_d
= 0x00, /* high */
295 .rf_mux_ploy
= 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
296 .tf_c
= 0x00, /* R27[7:0] highest,highest */
297 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
302 .freq
= 450, /* Start freq, in MHz */
303 .open_d
= 0x00, /* high */
304 .rf_mux_ploy
= 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
305 .tf_c
= 0x00, /* R27[7:0] highest,highest */
306 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
311 .freq
= 588, /* Start freq, in MHz */
312 .open_d
= 0x00, /* high */
313 .rf_mux_ploy
= 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
314 .tf_c
= 0x00, /* R27[7:0] highest,highest */
315 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
320 .freq
= 650, /* Start freq, in MHz */
321 .open_d
= 0x00, /* high */
322 .rf_mux_ploy
= 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
323 .tf_c
= 0x00, /* R27[7:0] highest,highest */
324 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
331 static int r820t_xtal_capacitor
[][2] = {
332 { 0x0b, XTAL_LOW_CAP_30P
},
333 { 0x02, XTAL_LOW_CAP_20P
},
334 { 0x01, XTAL_LOW_CAP_10P
},
335 { 0x00, XTAL_LOW_CAP_0P
},
336 { 0x10, XTAL_HIGH_CAP_0P
},
340 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
341 * input power, for raw results see:
342 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
345 static const int r820t_lna_gain_steps
[] = {
346 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
349 static const int r820t_mixer_gain_steps
[] = {
350 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
354 * I2C read/write code and shadow registers logic
356 static void shadow_store(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
359 int r
= reg
- REG_SHADOW_START
;
367 if (len
> NUM_REGS
- r
)
370 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
371 __func__
, r
+ REG_SHADOW_START
, len
, len
, val
);
373 memcpy(&priv
->regs
[r
], val
, len
);
376 static int r820t_write(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
379 int rc
, size
, pos
= 0;
381 /* Store the shadow registers */
382 shadow_store(priv
, reg
, val
, len
);
385 if (len
> priv
->cfg
->max_i2c_msg_len
- 1)
386 size
= priv
->cfg
->max_i2c_msg_len
- 1;
390 /* Fill I2C buffer */
392 memcpy(&priv
->buf
[1], &val
[pos
], size
);
394 rc
= tuner_i2c_xfer_send(&priv
->i2c_props
, priv
->buf
, size
+ 1);
395 if (rc
!= size
+ 1) {
396 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
397 __func__
, rc
, reg
, size
, size
, &priv
->buf
[1]);
402 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
403 __func__
, reg
, size
, size
, &priv
->buf
[1]);
413 static int r820t_write_reg(struct r820t_priv
*priv
, u8 reg
, u8 val
)
415 return r820t_write(priv
, reg
, &val
, 1);
418 static int r820t_read_cache_reg(struct r820t_priv
*priv
, int reg
)
420 reg
-= REG_SHADOW_START
;
422 if (reg
>= 0 && reg
< NUM_REGS
)
423 return priv
->regs
[reg
];
428 static int r820t_write_reg_mask(struct r820t_priv
*priv
, u8 reg
, u8 val
,
431 int rc
= r820t_read_cache_reg(priv
, reg
);
436 val
= (rc
& ~bit_mask
) | (val
& bit_mask
);
438 return r820t_write(priv
, reg
, &val
, 1);
441 static int r820t_read(struct r820t_priv
*priv
, u8 reg
, u8
*val
, int len
)
444 u8
*p
= &priv
->buf
[1];
448 rc
= tuner_i2c_xfer_send_recv(&priv
->i2c_props
, priv
->buf
, 1, p
, len
);
450 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
451 __func__
, rc
, reg
, len
, len
, p
);
457 /* Copy data to the output buffer */
458 for (i
= 0; i
< len
; i
++)
459 val
[i
] = bitrev8(p
[i
]);
461 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
462 __func__
, reg
, len
, len
, val
);
471 static int r820t_set_mux(struct r820t_priv
*priv
, u32 freq
)
473 const struct r820t_freq_range
*range
;
475 u8 val
, reg08
, reg09
;
477 /* Get the proper frequency range */
478 freq
= freq
/ 1000000;
479 for (i
= 0; i
< ARRAY_SIZE(freq_ranges
) - 1; i
++) {
480 if (freq
< freq_ranges
[i
+ 1].freq
)
483 range
= &freq_ranges
[i
];
485 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i
, freq
);
488 rc
= r820t_write_reg_mask(priv
, 0x17, range
->open_d
, 0x08);
493 rc
= r820t_write_reg_mask(priv
, 0x1a, range
->rf_mux_ploy
, 0xc3);
498 rc
= r820t_write_reg(priv
, 0x1b, range
->tf_c
);
502 /* XTAL CAP & Drive */
503 switch (priv
->xtal_cap_sel
) {
504 case XTAL_LOW_CAP_30P
:
505 case XTAL_LOW_CAP_20P
:
506 val
= range
->xtal_cap20p
| 0x08;
508 case XTAL_LOW_CAP_10P
:
509 val
= range
->xtal_cap10p
| 0x08;
511 case XTAL_HIGH_CAP_0P
:
512 val
= range
->xtal_cap0p
| 0x00;
515 case XTAL_LOW_CAP_0P
:
516 val
= range
->xtal_cap0p
| 0x08;
519 rc
= r820t_write_reg_mask(priv
, 0x10, val
, 0x0b);
523 if (priv
->imr_done
) {
524 reg08
= priv
->imr_data
[range
->imr_mem
].gain_x
;
525 reg09
= priv
->imr_data
[range
->imr_mem
].phase_y
;
530 rc
= r820t_write_reg_mask(priv
, 0x08, reg08
, 0x3f);
534 rc
= r820t_write_reg_mask(priv
, 0x09, reg09
, 0x3f);
539 static int r820t_set_pll(struct r820t_priv
*priv
, enum v4l2_tuner_type type
,
544 unsigned sleep_time
= 10000;
545 u32 vco_fra
; /* VCO contribution by SDM (kHz) */
546 u32 vco_min
= 1770000;
547 u32 vco_max
= vco_min
* 2;
555 u8 ni
, si
, nint
, vco_fine_tune
, val
;
558 /* Frequency in kHz */
560 pll_ref
= priv
->cfg
->xtal
/ 1000;
563 /* Doesn't exist on rtl-sdk, and on field tests, caused troubles */
564 if ((priv
->cfg
->rafael_chip
== CHIP_R620D
) ||
565 (priv
->cfg
->rafael_chip
== CHIP_R828D
) ||
566 (priv
->cfg
->rafael_chip
== CHIP_R828
)) {
567 /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
568 if (type
!= V4L2_TUNER_DIGITAL_TV
) {
574 if (priv
->cfg
->xtal
> 24000000) {
581 rc
= r820t_write_reg_mask(priv
, 0x10, refdiv2
, 0x10);
585 /* set pll autotune = 128kHz */
586 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
590 /* set VCO current = 100 */
591 rc
= r820t_write_reg_mask(priv
, 0x12, 0x80, 0xe0);
595 /* Calculate divider */
596 while (mix_div
<= 64) {
597 if (((freq
* mix_div
) >= vco_min
) &&
598 ((freq
* mix_div
) < vco_max
)) {
600 while (div_buf
> 2) {
601 div_buf
= div_buf
>> 1;
606 mix_div
= mix_div
<< 1;
609 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
613 vco_fine_tune
= (data
[4] & 0x30) >> 4;
615 tuner_dbg("mix_div=%d div_num=%d vco_fine_tune=%d\n",
616 mix_div
, div_num
, vco_fine_tune
);
619 * XXX: R828D/16MHz seems to have always vco_fine_tune=1.
620 * Due to that, this calculation goes wrong.
622 if (priv
->cfg
->rafael_chip
!= CHIP_R828D
) {
623 if (vco_fine_tune
> VCO_POWER_REF
)
624 div_num
= div_num
- 1;
625 else if (vco_fine_tune
< VCO_POWER_REF
)
626 div_num
= div_num
+ 1;
629 rc
= r820t_write_reg_mask(priv
, 0x10, div_num
<< 5, 0xe0);
633 vco_freq
= freq
* mix_div
;
634 nint
= vco_freq
/ (2 * pll_ref
);
635 vco_fra
= vco_freq
- 2 * pll_ref
* nint
;
637 /* boundary spur prevention */
638 if (vco_fra
< pll_ref
/ 64) {
640 } else if (vco_fra
> pll_ref
* 127 / 64) {
643 } else if ((vco_fra
> pll_ref
* 127 / 128) && (vco_fra
< pll_ref
)) {
644 vco_fra
= pll_ref
* 127 / 128;
645 } else if ((vco_fra
> pll_ref
) && (vco_fra
< pll_ref
* 129 / 128)) {
646 vco_fra
= pll_ref
* 129 / 128;
649 ni
= (nint
- 13) / 4;
650 si
= nint
- 4 * ni
- 13;
652 rc
= r820t_write_reg(priv
, 0x14, ni
+ (si
<< 6));
662 rc
= r820t_write_reg_mask(priv
, 0x12, val
, 0x08);
667 while (vco_fra
> 1) {
668 if (vco_fra
> (2 * pll_ref
/ n_sdm
)) {
669 sdm
= sdm
+ 32768 / (n_sdm
/ 2);
670 vco_fra
= vco_fra
- 2 * pll_ref
/ n_sdm
;
677 tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
678 freq
, pll_ref
, refdiv2
? " / 2" : "", sdm
);
680 rc
= r820t_write_reg(priv
, 0x16, sdm
>> 8);
683 rc
= r820t_write_reg(priv
, 0x15, sdm
& 0xff);
687 for (i
= 0; i
< 2; i
++) {
688 usleep_range(sleep_time
, sleep_time
+ 1000);
690 /* Check if PLL has locked */
691 rc
= r820t_read(priv
, 0x00, data
, 3);
698 /* Didn't lock. Increase VCO current */
699 rc
= r820t_write_reg_mask(priv
, 0x12, 0x60, 0xe0);
705 if (!(data
[2] & 0x40)) {
706 priv
->has_lock
= false;
710 priv
->has_lock
= true;
711 tuner_dbg("tuner has lock at frequency %d kHz\n", freq
);
713 /* set pll autotune = 8kHz */
714 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x08, 0x08);
719 static int r820t_sysfreq_sel(struct r820t_priv
*priv
, u32 freq
,
720 enum v4l2_tuner_type type
,
725 u8 mixer_top
, lna_top
, cp_cur
, div_buf_cur
, lna_vth_l
, mixer_vth_l
;
726 u8 air_cable1_in
, cable2_in
, pre_dect
, lna_discharge
, filter_cur
;
728 tuner_dbg("adjusting tuner parameters for the standard\n");
732 if ((freq
== 506000000) || (freq
== 666000000) ||
733 (freq
== 818000000)) {
734 mixer_top
= 0x14; /* mixer top:14 , top-1, low-discharge */
735 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
736 cp_cur
= 0x28; /* 101, 0.2 */
737 div_buf_cur
= 0x20; /* 10, 200u */
739 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
740 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
741 cp_cur
= 0x38; /* 111, auto */
742 div_buf_cur
= 0x30; /* 11, 150u */
744 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
745 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
746 air_cable1_in
= 0x00;
750 filter_cur
= 0x40; /* 10, low */
753 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
754 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
755 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
756 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
757 air_cable1_in
= 0x00;
761 cp_cur
= 0x38; /* 111, auto */
762 div_buf_cur
= 0x30; /* 11, 150u */
763 filter_cur
= 0x40; /* 10, low */
766 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
767 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
768 lna_vth_l
= 0x75; /* lna vth 1.04 , vtl 0.84 */
769 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
770 air_cable1_in
= 0x00;
774 cp_cur
= 0x38; /* 111, auto */
775 div_buf_cur
= 0x30; /* 11, 150u */
776 filter_cur
= 0x40; /* 10, low */
778 default: /* DVB-T 8M */
779 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
780 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
781 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
782 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
783 air_cable1_in
= 0x00;
787 cp_cur
= 0x38; /* 111, auto */
788 div_buf_cur
= 0x30; /* 11, 150u */
789 filter_cur
= 0x40; /* 10, low */
793 if (priv
->cfg
->use_diplexer
&&
794 ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
795 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
796 (priv
->cfg
->rafael_chip
== CHIP_R820C
))) {
798 air_cable1_in
= 0x00;
800 air_cable1_in
= 0x60;
805 if (priv
->cfg
->use_predetect
) {
806 rc
= r820t_write_reg_mask(priv
, 0x06, pre_dect
, 0x40);
811 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0xc7);
814 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0xf8);
817 rc
= r820t_write_reg(priv
, 0x0d, lna_vth_l
);
820 rc
= r820t_write_reg(priv
, 0x0e, mixer_vth_l
);
824 /* Air-IN only for Astrometa */
825 rc
= r820t_write_reg_mask(priv
, 0x05, air_cable1_in
, 0x60);
828 rc
= r820t_write_reg_mask(priv
, 0x06, cable2_in
, 0x08);
832 rc
= r820t_write_reg_mask(priv
, 0x11, cp_cur
, 0x38);
835 rc
= r820t_write_reg_mask(priv
, 0x17, div_buf_cur
, 0x30);
838 rc
= r820t_write_reg_mask(priv
, 0x0a, filter_cur
, 0x60);
842 * Original driver initializes regs 0x05 and 0x06 with the
843 * same value again on this point. Probably, it is just an
851 tuner_dbg("adjusting LNA parameters\n");
852 if (type
!= V4L2_TUNER_ANALOG_TV
) {
853 /* LNA TOP: lowest */
854 rc
= r820t_write_reg_mask(priv
, 0x1d, 0, 0x38);
859 rc
= r820t_write_reg_mask(priv
, 0x1c, 0, 0x04);
863 /* 0: PRE_DECT off */
864 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
869 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x30, 0x30);
875 /* write LNA TOP = 3 */
876 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x18, 0x38);
881 * write discharge mode
882 * FIXME: IMHO, the mask here is wrong, but it matches
883 * what's there at the original driver
885 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
889 /* LNA discharge current */
890 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
895 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x20, 0x30);
900 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
905 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0x38);
910 * write discharge mode
911 * FIXME: IMHO, the mask here is wrong, but it matches
912 * what's there at the original driver
914 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
918 /* LNA discharge current */
919 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
923 /* agc clk 1Khz, external det1 cap 1u */
924 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x30);
928 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x04);
935 static int r820t_set_tv_standard(struct r820t_priv
*priv
,
937 enum v4l2_tuner_type type
,
938 v4l2_std_id std
, u32 delsys
)
942 u32 if_khz
, filt_cal_lo
;
944 u8 filt_gain
, img_r
, filt_q
, hp_cor
, ext_enable
, loop_through
;
945 u8 lt_att
, flt_ext_widest
, polyfil_cur
;
946 bool need_calibration
;
948 tuner_dbg("selecting the delivery system\n");
950 if (delsys
== SYS_ISDBT
) {
953 filt_gain
= 0x10; /* +3db, 6mhz on */
954 img_r
= 0x00; /* image negative */
955 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
956 hp_cor
= 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
957 ext_enable
= 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
958 loop_through
= 0x00; /* r5[7], lt on */
959 lt_att
= 0x00; /* r31[7], lt att enable */
960 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
961 polyfil_cur
= 0x60; /* r25[6:5]:min */
965 filt_cal_lo
= 56000; /* 52000->56000 */
966 filt_gain
= 0x10; /* +3db, 6mhz on */
967 img_r
= 0x00; /* image negative */
968 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
969 hp_cor
= 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
970 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
971 loop_through
= 0x00; /* r5[7], lt on */
972 lt_att
= 0x00; /* r31[7], lt att enable */
973 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
974 polyfil_cur
= 0x60; /* r25[6:5]:min */
975 } else if (bw
== 7) {
978 * There are two 7 MHz tables defined on the original
979 * driver, but just the second one seems to be visible
980 * by rtl2832. Keep this one here commented, as it
981 * might be needed in the future
986 filt_gain
= 0x10; /* +3db, 6mhz on */
987 img_r
= 0x00; /* image negative */
988 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
989 hp_cor
= 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
990 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
991 loop_through
= 0x00; /* r5[7], lt on */
992 lt_att
= 0x00; /* r31[7], lt att enable */
993 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
994 polyfil_cur
= 0x60; /* r25[6:5]:min */
996 /* 7 MHz, second table */
999 filt_gain
= 0x10; /* +3db, 6mhz on */
1000 img_r
= 0x00; /* image negative */
1001 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1002 hp_cor
= 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
1003 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1004 loop_through
= 0x00; /* r5[7], lt on */
1005 lt_att
= 0x00; /* r31[7], lt att enable */
1006 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1007 polyfil_cur
= 0x60; /* r25[6:5]:min */
1010 filt_cal_lo
= 68500;
1011 filt_gain
= 0x10; /* +3db, 6mhz on */
1012 img_r
= 0x00; /* image negative */
1013 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
1014 hp_cor
= 0x0b; /* 1.7m disable, +0cap, 1.0mhz */
1015 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
1016 loop_through
= 0x00; /* r5[7], lt on */
1017 lt_att
= 0x00; /* r31[7], lt att enable */
1018 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
1019 polyfil_cur
= 0x60; /* r25[6:5]:min */
1023 /* Initialize the shadow registers */
1024 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1026 /* Init Flag & Xtal_check Result */
1028 val
= 1 | priv
->xtal_cap_sel
<< 1;
1031 rc
= r820t_write_reg_mask(priv
, 0x0c, val
, 0x0f);
1036 rc
= r820t_write_reg_mask(priv
, 0x13, VER_NUM
, 0x3f);
1040 /* for LT Gain test */
1041 if (type
!= V4L2_TUNER_ANALOG_TV
) {
1042 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x00, 0x38);
1045 usleep_range(1000, 2000);
1047 priv
->int_freq
= if_khz
* 1000;
1049 /* Check if standard changed. If so, filter calibration is needed */
1050 if (type
!= priv
->type
)
1051 need_calibration
= true;
1052 else if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
!= priv
->std
))
1053 need_calibration
= true;
1054 else if ((type
== V4L2_TUNER_DIGITAL_TV
) &&
1055 ((delsys
!= priv
->delsys
) || bw
!= priv
->bw
))
1056 need_calibration
= true;
1058 need_calibration
= false;
1060 if (need_calibration
) {
1061 tuner_dbg("calibrating the tuner\n");
1062 for (i
= 0; i
< 2; i
++) {
1064 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0x60);
1068 /* set cali clk =on */
1069 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x04, 0x04);
1073 /* X'tal cap 0pF for PLL */
1074 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x03);
1078 rc
= r820t_set_pll(priv
, type
, filt_cal_lo
* 1000);
1079 if (rc
< 0 || !priv
->has_lock
)
1083 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x10, 0x10);
1087 usleep_range(1000, 2000);
1090 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x00, 0x10);
1094 /* set cali clk =off */
1095 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x00, 0x04);
1099 /* Check if calibration worked */
1100 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1104 priv
->fil_cal_code
= data
[4] & 0x0f;
1105 if (priv
->fil_cal_code
&& priv
->fil_cal_code
!= 0x0f)
1109 if (priv
->fil_cal_code
== 0x0f)
1110 priv
->fil_cal_code
= 0;
1113 rc
= r820t_write_reg_mask(priv
, 0x0a,
1114 filt_q
| priv
->fil_cal_code
, 0x1f);
1118 /* Set BW, Filter_gain, & HP corner */
1119 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0xef);
1125 rc
= r820t_write_reg_mask(priv
, 0x07, img_r
, 0x80);
1129 /* Set filt_3dB, V6MHz */
1130 rc
= r820t_write_reg_mask(priv
, 0x06, filt_gain
, 0x30);
1134 /* channel filter extension */
1135 rc
= r820t_write_reg_mask(priv
, 0x1e, ext_enable
, 0x60);
1140 rc
= r820t_write_reg_mask(priv
, 0x05, loop_through
, 0x80);
1144 /* Loop through attenuation */
1145 rc
= r820t_write_reg_mask(priv
, 0x1f, lt_att
, 0x80);
1149 /* filter extension widest */
1150 rc
= r820t_write_reg_mask(priv
, 0x0f, flt_ext_widest
, 0x80);
1154 /* RF poly filter current */
1155 rc
= r820t_write_reg_mask(priv
, 0x19, polyfil_cur
, 0x60);
1159 /* Store current standard. If it changes, re-calibrate the tuner */
1160 priv
->delsys
= delsys
;
1168 static int r820t_read_gain(struct r820t_priv
*priv
)
1173 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1177 return ((data
[3] & 0x0f) << 1) + ((data
[3] & 0xf0) >> 4);
1181 /* FIXME: This routine requires more testing */
1182 static int r820t_set_gain_mode(struct r820t_priv
*priv
,
1183 bool set_manual_gain
,
1188 if (set_manual_gain
) {
1189 int i
, total_gain
= 0;
1190 uint8_t mix_index
= 0, lna_index
= 0;
1194 rc
= r820t_write_reg_mask(priv
, 0x05, 0x10, 0x10);
1198 /* Mixer auto off */
1199 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1203 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1207 /* set fixed VGA gain for now (16.3 dB) */
1208 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x08, 0x9f);
1212 for (i
= 0; i
< 15; i
++) {
1213 if (total_gain
>= gain
)
1216 total_gain
+= r820t_lna_gain_steps
[++lna_index
];
1218 if (total_gain
>= gain
)
1221 total_gain
+= r820t_mixer_gain_steps
[++mix_index
];
1225 rc
= r820t_write_reg_mask(priv
, 0x05, lna_index
, 0x0f);
1229 /* set Mixer gain */
1230 rc
= r820t_write_reg_mask(priv
, 0x07, mix_index
, 0x0f);
1235 rc
= r820t_write_reg_mask(priv
, 0x05, 0, 0x10);
1240 rc
= r820t_write_reg_mask(priv
, 0x07, 0x10, 0x10);
1244 /* set fixed VGA gain for now (26.5 dB) */
1245 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1254 static int generic_set_freq(struct dvb_frontend
*fe
,
1255 u32 freq
/* in HZ */,
1257 enum v4l2_tuner_type type
,
1258 v4l2_std_id std
, u32 delsys
)
1260 struct r820t_priv
*priv
= fe
->tuner_priv
;
1264 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1267 rc
= r820t_set_tv_standard(priv
, bw
, type
, std
, delsys
);
1271 if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
== V4L2_STD_SECAM_LC
))
1272 lo_freq
= freq
- priv
->int_freq
;
1274 lo_freq
= freq
+ priv
->int_freq
;
1276 rc
= r820t_set_mux(priv
, lo_freq
);
1280 rc
= r820t_set_pll(priv
, type
, lo_freq
);
1281 if (rc
< 0 || !priv
->has_lock
)
1284 rc
= r820t_sysfreq_sel(priv
, freq
, type
, std
, delsys
);
1288 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1289 __func__
, freq
, r820t_read_gain(priv
));
1294 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
1299 * r820t standby logic
1302 static int r820t_standby(struct r820t_priv
*priv
)
1306 /* If device was not initialized yet, don't need to standby */
1307 if (!priv
->init_done
)
1310 rc
= r820t_write_reg(priv
, 0x06, 0xb1);
1313 rc
= r820t_write_reg(priv
, 0x05, 0x03);
1316 rc
= r820t_write_reg(priv
, 0x07, 0x3a);
1319 rc
= r820t_write_reg(priv
, 0x08, 0x40);
1322 rc
= r820t_write_reg(priv
, 0x09, 0xc0);
1325 rc
= r820t_write_reg(priv
, 0x0a, 0x36);
1328 rc
= r820t_write_reg(priv
, 0x0c, 0x35);
1331 rc
= r820t_write_reg(priv
, 0x0f, 0x68);
1334 rc
= r820t_write_reg(priv
, 0x11, 0x03);
1337 rc
= r820t_write_reg(priv
, 0x17, 0xf4);
1340 rc
= r820t_write_reg(priv
, 0x19, 0x0c);
1342 /* Force initial calibration */
1349 * r820t device init logic
1352 static int r820t_xtal_check(struct r820t_priv
*priv
)
1357 /* Initialize the shadow registers */
1358 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1360 /* cap 30pF & Drive Low */
1361 rc
= r820t_write_reg_mask(priv
, 0x10, 0x0b, 0x0b);
1365 /* set pll autotune = 128kHz */
1366 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
1370 /* set manual initial reg = 111111; */
1371 rc
= r820t_write_reg_mask(priv
, 0x13, 0x7f, 0x7f);
1376 rc
= r820t_write_reg_mask(priv
, 0x13, 0x00, 0x40);
1380 /* Try several xtal capacitor alternatives */
1381 for (i
= 0; i
< ARRAY_SIZE(r820t_xtal_capacitor
); i
++) {
1382 rc
= r820t_write_reg_mask(priv
, 0x10,
1383 r820t_xtal_capacitor
[i
][0], 0x1b);
1387 usleep_range(5000, 6000);
1389 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1392 if (!(data
[2] & 0x40))
1395 val
= data
[2] & 0x3f;
1397 if (priv
->cfg
->xtal
== 16000000 && (val
> 29 || val
< 23))
1404 if (i
== ARRAY_SIZE(r820t_xtal_capacitor
))
1407 return r820t_xtal_capacitor
[i
][1];
1410 static int r820t_imr_prepare(struct r820t_priv
*priv
)
1414 /* Initialize the shadow registers */
1415 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1417 /* lna off (air-in off) */
1418 rc
= r820t_write_reg_mask(priv
, 0x05, 0x20, 0x20);
1422 /* mixer gain mode = manual */
1423 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1427 /* filter corner = lowest */
1428 rc
= r820t_write_reg_mask(priv
, 0x0a, 0x0f, 0x0f);
1432 /* filter bw=+2cap, hp=5M */
1433 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x60, 0x6f);
1437 /* adc=on, vga code mode, gain = 26.5dB */
1438 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1443 rc
= r820t_write_reg_mask(priv
, 0x0f, 0, 0x08);
1447 /* ring power = on */
1448 rc
= r820t_write_reg_mask(priv
, 0x18, 0x10, 0x10);
1452 /* from ring = ring pll in */
1453 rc
= r820t_write_reg_mask(priv
, 0x1c, 0x02, 0x02);
1457 /* sw_pdect = det3 */
1458 rc
= r820t_write_reg_mask(priv
, 0x1e, 0x80, 0x80);
1463 rc
= r820t_write_reg_mask(priv
, 0x06, 0x20, 0x20);
1468 static int r820t_multi_read(struct r820t_priv
*priv
)
1472 u8 data
[2], min
= 255, max
= 0;
1474 usleep_range(5000, 6000);
1476 for (i
= 0; i
< 6; i
++) {
1477 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1489 rc
= sum
- max
- min
;
1494 static int r820t_imr_cross(struct r820t_priv
*priv
,
1495 struct r820t_sect_type iq_point
[3],
1498 struct r820t_sect_type cross
[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
1499 struct r820t_sect_type tmp
;
1503 reg08
= r820t_read_cache_reg(priv
, 8) & 0xc0;
1504 reg09
= r820t_read_cache_reg(priv
, 9) & 0xc0;
1510 for (i
= 0; i
< 5; i
++) {
1513 cross
[i
].gain_x
= reg08
;
1514 cross
[i
].phase_y
= reg09
;
1517 cross
[i
].gain_x
= reg08
; /* 0 */
1518 cross
[i
].phase_y
= reg09
+ 1; /* Q-1 */
1521 cross
[i
].gain_x
= reg08
; /* 0 */
1522 cross
[i
].phase_y
= (reg09
| 0x20) + 1; /* I-1 */
1525 cross
[i
].gain_x
= reg08
+ 1; /* Q-1 */
1526 cross
[i
].phase_y
= reg09
;
1529 cross
[i
].gain_x
= (reg08
| 0x20) + 1; /* I-1 */
1530 cross
[i
].phase_y
= reg09
;
1533 rc
= r820t_write_reg(priv
, 0x08, cross
[i
].gain_x
);
1537 rc
= r820t_write_reg(priv
, 0x09, cross
[i
].phase_y
);
1541 rc
= r820t_multi_read(priv
);
1545 cross
[i
].value
= rc
;
1547 if (cross
[i
].value
< tmp
.value
)
1548 memcpy(&tmp
, &cross
[i
], sizeof(tmp
));
1551 if ((tmp
.phase_y
& 0x1f) == 1) { /* y-direction */
1554 iq_point
[0] = cross
[0];
1555 iq_point
[1] = cross
[1];
1556 iq_point
[2] = cross
[2];
1557 } else { /* (0,0) or x-direction */
1560 iq_point
[0] = cross
[0];
1561 iq_point
[1] = cross
[3];
1562 iq_point
[2] = cross
[4];
1567 static void r820t_compre_cor(struct r820t_sect_type iq
[3])
1571 for (i
= 3; i
> 0; i
--) {
1572 if (iq
[0].value
> iq
[i
- 1].value
)
1573 swap(iq
[0], iq
[i
- 1]);
1577 static int r820t_compre_step(struct r820t_priv
*priv
,
1578 struct r820t_sect_type iq
[3], u8 reg
)
1581 struct r820t_sect_type tmp
;
1584 * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
1586 * new < min => update to min and continue
1590 /* min value already saved in iq[0] */
1591 tmp
.phase_y
= iq
[0].phase_y
;
1592 tmp
.gain_x
= iq
[0].gain_x
;
1594 while (((tmp
.gain_x
& 0x1f) < IMR_TRIAL
) &&
1595 ((tmp
.phase_y
& 0x1f) < IMR_TRIAL
)) {
1601 rc
= r820t_write_reg(priv
, 0x08, tmp
.gain_x
);
1605 rc
= r820t_write_reg(priv
, 0x09, tmp
.phase_y
);
1609 rc
= r820t_multi_read(priv
);
1614 if (tmp
.value
<= iq
[0].value
) {
1615 iq
[0].gain_x
= tmp
.gain_x
;
1616 iq
[0].phase_y
= tmp
.phase_y
;
1617 iq
[0].value
= tmp
.value
;
1627 static int r820t_iq_tree(struct r820t_priv
*priv
,
1628 struct r820t_sect_type iq
[3],
1629 u8 fix_val
, u8 var_val
, u8 fix_reg
)
1635 * record IMC results by input gain/phase location then adjust
1636 * gain or phase positive 1 step and negtive 1 step,
1637 * both record results
1640 if (fix_reg
== 0x08)
1645 for (i
= 0; i
< 3; i
++) {
1646 rc
= r820t_write_reg(priv
, fix_reg
, fix_val
);
1650 rc
= r820t_write_reg(priv
, var_reg
, var_val
);
1654 rc
= r820t_multi_read(priv
);
1659 if (fix_reg
== 0x08) {
1660 iq
[i
].gain_x
= fix_val
;
1661 iq
[i
].phase_y
= var_val
;
1663 iq
[i
].phase_y
= fix_val
;
1664 iq
[i
].gain_x
= var_val
;
1667 if (i
== 0) { /* try right-side point */
1669 } else if (i
== 1) { /* try left-side point */
1670 /* if absolute location is 1, change I/Q direction */
1671 if ((var_val
& 0x1f) < 0x02) {
1672 tmp
= 2 - (var_val
& 0x1f);
1674 /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
1675 if (var_val
& 0x20) {
1679 var_val
|= 0x20 | tmp
;
1690 static int r820t_section(struct r820t_priv
*priv
,
1691 struct r820t_sect_type
*iq_point
)
1694 struct r820t_sect_type compare_iq
[3], compare_bet
[3];
1696 /* Try X-1 column and save min result to compare_bet[0] */
1697 if (!(iq_point
->gain_x
& 0x1f))
1698 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) & 0xdf) + 1; /* Q-path, Gain=1 */
1700 compare_iq
[0].gain_x
= iq_point
->gain_x
- 1; /* left point */
1701 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1704 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1705 compare_iq
[0].phase_y
, 0x08);
1709 r820t_compre_cor(compare_iq
);
1711 compare_bet
[0] = compare_iq
[0];
1713 /* Try X column and save min result to compare_bet[1] */
1714 compare_iq
[0].gain_x
= iq_point
->gain_x
;
1715 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1717 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1718 compare_iq
[0].phase_y
, 0x08);
1722 r820t_compre_cor(compare_iq
);
1724 compare_bet
[1] = compare_iq
[0];
1726 /* Try X+1 column and save min result to compare_bet[2] */
1727 if ((iq_point
->gain_x
& 0x1f) == 0x00)
1728 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) | 0x20) + 1; /* I-path, Gain=1 */
1730 compare_iq
[0].gain_x
= iq_point
->gain_x
+ 1;
1731 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
[2] = compare_iq
[0];
1742 r820t_compre_cor(compare_bet
);
1744 *iq_point
= compare_bet
[0];
1749 static int r820t_vga_adjust(struct r820t_priv
*priv
)
1754 /* increase vga power to let image significant */
1755 for (vga_count
= 12; vga_count
< 16; vga_count
++) {
1756 rc
= r820t_write_reg_mask(priv
, 0x0c, vga_count
, 0x0f);
1760 usleep_range(10000, 11000);
1762 rc
= r820t_multi_read(priv
);
1773 static int r820t_iq(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1775 struct r820t_sect_type compare_iq
[3];
1777 u8 x_direction
= 0; /* 1:x, 0:y */
1778 u8 dir_reg
, other_reg
;
1780 r820t_vga_adjust(priv
);
1782 rc
= r820t_imr_cross(priv
, compare_iq
, &x_direction
);
1786 if (x_direction
== 1) {
1794 /* compare and find min of 3 points. determine i/q direction */
1795 r820t_compre_cor(compare_iq
);
1797 /* increase step to find min value of this direction */
1798 rc
= r820t_compre_step(priv
, compare_iq
, dir_reg
);
1802 /* the other direction */
1803 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1804 compare_iq
[0].phase_y
, dir_reg
);
1808 /* compare and find min of 3 points. determine i/q direction */
1809 r820t_compre_cor(compare_iq
);
1811 /* increase step to find min value on this direction */
1812 rc
= r820t_compre_step(priv
, compare_iq
, other_reg
);
1816 /* check 3 points again */
1817 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1818 compare_iq
[0].phase_y
, other_reg
);
1822 r820t_compre_cor(compare_iq
);
1824 /* section-9 check */
1825 rc
= r820t_section(priv
, compare_iq
);
1827 *iq_pont
= compare_iq
[0];
1829 /* reset gain/phase control setting */
1830 rc
= r820t_write_reg_mask(priv
, 0x08, 0, 0x3f);
1834 rc
= r820t_write_reg_mask(priv
, 0x09, 0, 0x3f);
1839 static int r820t_f_imr(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1843 r820t_vga_adjust(priv
);
1846 * search surrounding points from previous point
1847 * try (x-1), (x), (x+1) columns, and find min IMR result point
1849 rc
= r820t_section(priv
, iq_pont
);
1856 static int r820t_imr(struct r820t_priv
*priv
, unsigned imr_mem
, bool im_flag
)
1858 struct r820t_sect_type imr_point
;
1860 u32 ring_vco
, ring_freq
, ring_ref
;
1862 int reg18
, reg19
, reg1f
;
1864 if (priv
->cfg
->xtal
> 24000000)
1865 ring_ref
= priv
->cfg
->xtal
/ 2000;
1867 ring_ref
= priv
->cfg
->xtal
/ 1000;
1870 for (n
= 0; n
< 16; n
++) {
1871 if ((16 + n
) * 8 * ring_ref
>= 3100000) {
1877 reg18
= r820t_read_cache_reg(priv
, 0x18);
1878 reg19
= r820t_read_cache_reg(priv
, 0x19);
1879 reg1f
= r820t_read_cache_reg(priv
, 0x1f);
1881 reg18
&= 0xf0; /* set ring[3:0] */
1884 ring_vco
= (16 + n_ring
) * 8 * ring_ref
;
1886 reg18
&= 0xdf; /* clear ring_se23 */
1887 reg19
&= 0xfc; /* clear ring_seldiv */
1888 reg1f
&= 0xfc; /* clear ring_att */
1892 ring_freq
= ring_vco
/ 48;
1893 reg18
|= 0x20; /* ring_se23 = 1 */
1894 reg19
|= 0x03; /* ring_seldiv = 3 */
1895 reg1f
|= 0x02; /* ring_att 10 */
1898 ring_freq
= ring_vco
/ 16;
1899 reg18
|= 0x00; /* ring_se23 = 0 */
1900 reg19
|= 0x02; /* ring_seldiv = 2 */
1901 reg1f
|= 0x00; /* pw_ring 00 */
1904 ring_freq
= ring_vco
/ 8;
1905 reg18
|= 0x00; /* ring_se23 = 0 */
1906 reg19
|= 0x01; /* ring_seldiv = 1 */
1907 reg1f
|= 0x03; /* pw_ring 11 */
1910 ring_freq
= ring_vco
/ 6;
1911 reg18
|= 0x20; /* ring_se23 = 1 */
1912 reg19
|= 0x00; /* ring_seldiv = 0 */
1913 reg1f
|= 0x03; /* pw_ring 11 */
1916 ring_freq
= ring_vco
/ 4;
1917 reg18
|= 0x00; /* ring_se23 = 0 */
1918 reg19
|= 0x00; /* ring_seldiv = 0 */
1919 reg1f
|= 0x01; /* pw_ring 01 */
1922 ring_freq
= ring_vco
/ 4;
1923 reg18
|= 0x00; /* ring_se23 = 0 */
1924 reg19
|= 0x00; /* ring_seldiv = 0 */
1925 reg1f
|= 0x01; /* pw_ring 01 */
1930 /* write pw_ring, n_ring, ringdiv2 registers */
1932 /* n_ring, ring_se23 */
1933 rc
= r820t_write_reg(priv
, 0x18, reg18
);
1938 rc
= r820t_write_reg(priv
, 0x19, reg19
);
1943 rc
= r820t_write_reg(priv
, 0x1f, reg1f
);
1947 /* mux input freq ~ rf_in freq */
1948 rc
= r820t_set_mux(priv
, (ring_freq
- 5300) * 1000);
1952 rc
= r820t_set_pll(priv
, V4L2_TUNER_DIGITAL_TV
,
1953 (ring_freq
- 5300) * 1000);
1954 if (!priv
->has_lock
)
1960 rc
= r820t_iq(priv
, &imr_point
);
1962 imr_point
.gain_x
= priv
->imr_data
[3].gain_x
;
1963 imr_point
.phase_y
= priv
->imr_data
[3].phase_y
;
1964 imr_point
.value
= priv
->imr_data
[3].value
;
1966 rc
= r820t_f_imr(priv
, &imr_point
);
1971 /* save IMR value */
1974 priv
->imr_data
[0].gain_x
= imr_point
.gain_x
;
1975 priv
->imr_data
[0].phase_y
= imr_point
.phase_y
;
1976 priv
->imr_data
[0].value
= imr_point
.value
;
1979 priv
->imr_data
[1].gain_x
= imr_point
.gain_x
;
1980 priv
->imr_data
[1].phase_y
= imr_point
.phase_y
;
1981 priv
->imr_data
[1].value
= imr_point
.value
;
1984 priv
->imr_data
[2].gain_x
= imr_point
.gain_x
;
1985 priv
->imr_data
[2].phase_y
= imr_point
.phase_y
;
1986 priv
->imr_data
[2].value
= imr_point
.value
;
1989 priv
->imr_data
[3].gain_x
= imr_point
.gain_x
;
1990 priv
->imr_data
[3].phase_y
= imr_point
.phase_y
;
1991 priv
->imr_data
[3].value
= imr_point
.value
;
1994 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
1995 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
1996 priv
->imr_data
[4].value
= imr_point
.value
;
1999 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
2000 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
2001 priv
->imr_data
[4].value
= imr_point
.value
;
2008 static int r820t_imr_callibrate(struct r820t_priv
*priv
)
2013 if (priv
->init_done
)
2016 /* Detect Xtal capacitance */
2017 if ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
2018 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
2019 (priv
->cfg
->rafael_chip
== CHIP_R820C
)) {
2020 priv
->xtal_cap_sel
= XTAL_HIGH_CAP_0P
;
2022 /* Initialize registers */
2023 rc
= r820t_write(priv
, 0x05,
2024 r820t_init_array
, sizeof(r820t_init_array
));
2027 for (i
= 0; i
< 3; i
++) {
2028 rc
= r820t_xtal_check(priv
);
2031 if (!i
|| rc
> xtal_cap
)
2034 priv
->xtal_cap_sel
= xtal_cap
;
2038 * Disables IMR callibration. That emulates the same behaviour
2039 * as what is done by rtl-sdr userspace library. Useful for testing
2042 priv
->init_done
= true;
2047 /* Initialize registers */
2048 rc
= r820t_write(priv
, 0x05,
2049 r820t_init_array
, sizeof(r820t_init_array
));
2053 rc
= r820t_imr_prepare(priv
);
2057 rc
= r820t_imr(priv
, 3, true);
2060 rc
= r820t_imr(priv
, 1, false);
2063 rc
= r820t_imr(priv
, 0, false);
2066 rc
= r820t_imr(priv
, 2, false);
2069 rc
= r820t_imr(priv
, 4, false);
2073 priv
->init_done
= true;
2074 priv
->imr_done
= true;
2080 /* Not used, for now */
2081 static int r820t_gpio(struct r820t_priv
*priv
, bool enable
)
2083 return r820t_write_reg_mask(priv
, 0x0f, enable
? 1 : 0, 0x01);
2088 * r820t frontend operations and tuner attach code
2090 * All driver locks and i2c control are only in this part of the code
2093 static int r820t_init(struct dvb_frontend
*fe
)
2095 struct r820t_priv
*priv
= fe
->tuner_priv
;
2098 tuner_dbg("%s:\n", __func__
);
2100 mutex_lock(&priv
->lock
);
2101 if (fe
->ops
.i2c_gate_ctrl
)
2102 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2104 rc
= r820t_imr_callibrate(priv
);
2108 /* Initialize registers */
2109 rc
= r820t_write(priv
, 0x05,
2110 r820t_init_array
, sizeof(r820t_init_array
));
2113 if (fe
->ops
.i2c_gate_ctrl
)
2114 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2115 mutex_unlock(&priv
->lock
);
2118 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2122 static int r820t_sleep(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_standby(priv
);
2135 if (fe
->ops
.i2c_gate_ctrl
)
2136 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2137 mutex_unlock(&priv
->lock
);
2139 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2143 static int r820t_set_analog_freq(struct dvb_frontend
*fe
,
2144 struct analog_parameters
*p
)
2146 struct r820t_priv
*priv
= fe
->tuner_priv
;
2150 tuner_dbg("%s called\n", __func__
);
2152 /* if std is not defined, choose one */
2154 p
->std
= V4L2_STD_MN
;
2156 if ((p
->std
== V4L2_STD_PAL_M
) || (p
->std
== V4L2_STD_NTSC
))
2161 mutex_lock(&priv
->lock
);
2162 if (fe
->ops
.i2c_gate_ctrl
)
2163 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2165 rc
= generic_set_freq(fe
, 62500l * p
->frequency
, bw
,
2166 V4L2_TUNER_ANALOG_TV
, p
->std
, SYS_UNDEFINED
);
2168 if (fe
->ops
.i2c_gate_ctrl
)
2169 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2170 mutex_unlock(&priv
->lock
);
2175 static int r820t_set_params(struct dvb_frontend
*fe
)
2177 struct r820t_priv
*priv
= fe
->tuner_priv
;
2178 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
2182 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2183 __func__
, c
->delivery_system
, c
->frequency
, c
->bandwidth_hz
);
2185 mutex_lock(&priv
->lock
);
2186 if (fe
->ops
.i2c_gate_ctrl
)
2187 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2189 bw
= (c
->bandwidth_hz
+ 500000) / 1000000;
2193 rc
= generic_set_freq(fe
, c
->frequency
, bw
,
2194 V4L2_TUNER_DIGITAL_TV
, 0, c
->delivery_system
);
2196 if (fe
->ops
.i2c_gate_ctrl
)
2197 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2198 mutex_unlock(&priv
->lock
);
2201 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2205 static int r820t_signal(struct dvb_frontend
*fe
, u16
*strength
)
2207 struct r820t_priv
*priv
= fe
->tuner_priv
;
2210 mutex_lock(&priv
->lock
);
2211 if (fe
->ops
.i2c_gate_ctrl
)
2212 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2214 if (priv
->has_lock
) {
2215 rc
= r820t_read_gain(priv
);
2219 /* A higher gain at LNA means a lower signal strength */
2220 *strength
= (45 - rc
) << 4 | 0xff;
2221 if (*strength
== 0xff)
2228 if (fe
->ops
.i2c_gate_ctrl
)
2229 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2230 mutex_unlock(&priv
->lock
);
2232 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2234 priv
->has_lock
? "PLL locked" : "no signal",
2240 static int r820t_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
2242 struct r820t_priv
*priv
= fe
->tuner_priv
;
2244 tuner_dbg("%s:\n", __func__
);
2246 *frequency
= priv
->int_freq
;
2251 static int r820t_release(struct dvb_frontend
*fe
)
2253 struct r820t_priv
*priv
= fe
->tuner_priv
;
2255 tuner_dbg("%s:\n", __func__
);
2257 mutex_lock(&r820t_list_mutex
);
2260 hybrid_tuner_release_state(priv
);
2262 mutex_unlock(&r820t_list_mutex
);
2264 fe
->tuner_priv
= NULL
;
2269 static const struct dvb_tuner_ops r820t_tuner_ops
= {
2271 .name
= "Rafael Micro R820T",
2272 .frequency_min
= 42000000,
2273 .frequency_max
= 1002000000,
2276 .release
= r820t_release
,
2277 .sleep
= r820t_sleep
,
2278 .set_params
= r820t_set_params
,
2279 .set_analog_params
= r820t_set_analog_freq
,
2280 .get_if_frequency
= r820t_get_if_frequency
,
2281 .get_rf_strength
= r820t_signal
,
2284 struct dvb_frontend
*r820t_attach(struct dvb_frontend
*fe
,
2285 struct i2c_adapter
*i2c
,
2286 const struct r820t_config
*cfg
)
2288 struct r820t_priv
*priv
;
2293 mutex_lock(&r820t_list_mutex
);
2295 instance
= hybrid_tuner_request_state(struct r820t_priv
, priv
,
2296 hybrid_tuner_instance_list
,
2301 /* memory allocation failure */
2305 /* new tuner instance */
2308 mutex_init(&priv
->lock
);
2310 fe
->tuner_priv
= priv
;
2313 /* existing tuner instance */
2314 fe
->tuner_priv
= priv
;
2318 if (fe
->ops
.i2c_gate_ctrl
)
2319 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2321 /* check if the tuner is there */
2322 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
2326 rc
= r820t_sleep(fe
);
2330 tuner_info("Rafael Micro r820t successfully identified\n");
2332 if (fe
->ops
.i2c_gate_ctrl
)
2333 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2335 mutex_unlock(&r820t_list_mutex
);
2337 memcpy(&fe
->ops
.tuner_ops
, &r820t_tuner_ops
,
2338 sizeof(struct dvb_tuner_ops
));
2342 if (fe
->ops
.i2c_gate_ctrl
)
2343 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2346 mutex_unlock(&r820t_list_mutex
);
2348 tuner_info("%s: failed=%d\n", __func__
, rc
);
2352 EXPORT_SYMBOL_GPL(r820t_attach
);
2354 MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2355 MODULE_AUTHOR("Mauro Carvalho Chehab");
2356 MODULE_LICENSE("GPL");