2 * Rafael Micro R820T driver
4 * Copyright (C) 2013 Mauro Carvalho Chehab <mchehab@redhat.com>
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>
39 #include <asm/div64.h>
41 #include "tuner-i2c.h"
45 * FIXME: I think that there are only 32 registers, but better safe than
46 * sorry. After finishing the driver, we may review it.
48 #define REG_SHADOW_START 5
56 module_param(debug
, int, 0644);
57 MODULE_PARM_DESC(debug
, "enable verbose debug messages");
60 * enums and structures
71 struct r820t_sect_type
{
78 struct list_head hybrid_tuner_instance_list
;
79 const struct r820t_config
*cfg
;
80 struct tuner_i2c_props i2c_props
;
85 enum xtal_cap_value xtal_cap_sel
;
91 struct r820t_sect_type imr_data
[NUM_IMR
];
93 /* Store current mode */
95 enum v4l2_tuner_type type
;
102 struct r820t_freq_range
{
110 u8 imr_mem
; /* Not used, currently */
113 #define VCO_POWER_REF 0x02
114 #define DIP_FREQ 32000000
120 static LIST_HEAD(hybrid_tuner_instance_list
);
121 static DEFINE_MUTEX(r820t_list_mutex
);
123 /* Those initial values start from REG_SHADOW_START */
124 static const u8 r820t_init_array
[NUM_REGS
] = {
125 0x83, 0x32, 0x75, /* 05 to 07 */
126 0xc0, 0x40, 0xd6, 0x6c, /* 08 to 0b */
127 0xf5, 0x63, 0x75, 0x68, /* 0c to 0f */
128 0x6c, 0x83, 0x80, 0x00, /* 10 to 13 */
129 0x0f, 0x00, 0xc0, 0x30, /* 14 to 17 */
130 0x48, 0xcc, 0x60, 0x00, /* 18 to 1b */
131 0x54, 0xae, 0x4a, 0xc0 /* 1c to 1f */
134 /* Tuner frequency ranges */
135 static const struct r820t_freq_range freq_ranges
[] = {
138 .open_d
= 0x08, /* low */
139 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
140 .tf_c
= 0xdf, /* R27[7:0] band2,band0 */
141 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
146 .freq
= 50, /* 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
= 0xbe, /* R27[7:0] band4,band1 */
150 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
155 .freq
= 55, /* 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
= 0x8b, /* R27[7:0] band7,band4 */
159 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
164 .freq
= 60, /* 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
= 0x7b, /* R27[7:0] band8,band4 */
168 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
173 .freq
= 65, /* 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
= 0x69, /* R27[7:0] band9,band6 */
177 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
182 .freq
= 70, /* Start freq, in MHz */
183 .open_d
= 0x08, /* low */
184 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
185 .tf_c
= 0x58, /* R27[7:0] band10,band7 */
186 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
191 .freq
= 75, /* 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
= 80, /* 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
= 0x44, /* R27[7:0] band11,band11 */
204 .xtal_cap20p
= 0x02, /* R16[1:0] 20pF (10) */
209 .freq
= 90, /* 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
= 100, /* 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
= 0x34, /* R27[7:0] band12,band11 */
222 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
227 .freq
= 110, /* 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
= 120, /* 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
= 0x24, /* R27[7:0] band13,band11 */
240 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
245 .freq
= 140, /* 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
= 0x14, /* R27[7:0] band14,band11 */
249 .xtal_cap20p
= 0x01, /* R16[1:0] 10pF (01) */
254 .freq
= 180, /* 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
= 220, /* 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
= 0x13, /* R27[7:0] band14,band12 */
267 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
272 .freq
= 250, /* 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
= 0x11, /* R27[7:0] highest,highest */
276 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
281 .freq
= 280, /* Start freq, in MHz */
282 .open_d
= 0x00, /* high */
283 .rf_mux_ploy
= 0x02, /* R26[7:6]=0 (LPF) R26[1:0]=2 (low) */
284 .tf_c
= 0x00, /* R27[7:0] highest,highest */
285 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
290 .freq
= 310, /* 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
= 450, /* Start freq, in MHz */
300 .open_d
= 0x00, /* high */
301 .rf_mux_ploy
= 0x41, /* R26[7:6]=1 (bypass) R26[1:0]=1 (middle) */
302 .tf_c
= 0x00, /* R27[7:0] highest,highest */
303 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
308 .freq
= 588, /* 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) */
317 .freq
= 650, /* Start freq, in MHz */
318 .open_d
= 0x00, /* high */
319 .rf_mux_ploy
= 0x40, /* R26[7:6]=1 (bypass) R26[1:0]=0 (highest) */
320 .tf_c
= 0x00, /* R27[7:0] highest,highest */
321 .xtal_cap20p
= 0x00, /* R16[1:0] 0pF (00) */
328 static int r820t_xtal_capacitor
[][2] = {
329 { 0x0b, XTAL_LOW_CAP_30P
},
330 { 0x02, XTAL_LOW_CAP_20P
},
331 { 0x01, XTAL_LOW_CAP_10P
},
332 { 0x00, XTAL_LOW_CAP_0P
},
333 { 0x10, XTAL_HIGH_CAP_0P
},
337 * measured with a Racal 6103E GSM test set at 928 MHz with -60 dBm
338 * input power, for raw results see:
339 * http://steve-m.de/projects/rtl-sdr/gain_measurement/r820t/
342 static const int r820t_lna_gain_steps
[] = {
343 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
346 static const int r820t_mixer_gain_steps
[] = {
347 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
351 * I2C read/write code and shadow registers logic
353 static void shadow_store(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
356 int r
= reg
- REG_SHADOW_START
;
367 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
368 __func__
, r
+ REG_SHADOW_START
, len
, len
, val
);
370 memcpy(&priv
->regs
[r
], val
, len
);
373 static int r820t_write(struct r820t_priv
*priv
, u8 reg
, const u8
*val
,
376 int rc
, size
, pos
= 0;
378 /* Store the shadow registers */
379 shadow_store(priv
, reg
, val
, len
);
382 if (len
> priv
->cfg
->max_i2c_msg_len
- 1)
383 size
= priv
->cfg
->max_i2c_msg_len
- 1;
387 /* Fill I2C buffer */
389 memcpy(&priv
->buf
[1], &val
[pos
], size
);
391 rc
= tuner_i2c_xfer_send(&priv
->i2c_props
, priv
->buf
, size
+ 1);
392 if (rc
!= size
+ 1) {
393 tuner_info("%s: i2c wr failed=%d reg=%02x len=%d: %*ph\n",
394 __func__
, rc
, reg
, size
, size
, &priv
->buf
[1]);
399 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
400 __func__
, reg
, size
, size
, &priv
->buf
[1]);
410 static int r820t_write_reg(struct r820t_priv
*priv
, u8 reg
, u8 val
)
412 return r820t_write(priv
, reg
, &val
, 1);
415 static int r820t_read_cache_reg(struct r820t_priv
*priv
, int reg
)
417 reg
-= REG_SHADOW_START
;
419 if (reg
>= 0 && reg
< NUM_REGS
)
420 return priv
->regs
[reg
];
425 static int r820t_write_reg_mask(struct r820t_priv
*priv
, u8 reg
, u8 val
,
428 int rc
= r820t_read_cache_reg(priv
, reg
);
433 val
= (rc
& ~bit_mask
) | (val
& bit_mask
);
435 return r820t_write(priv
, reg
, &val
, 1);
438 static int r820t_read(struct r820t_priv
*priv
, u8 reg
, u8
*val
, int len
)
441 u8
*p
= &priv
->buf
[1];
445 rc
= tuner_i2c_xfer_send_recv(&priv
->i2c_props
, priv
->buf
, 1, p
, len
);
447 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
448 __func__
, rc
, reg
, len
, len
, p
);
454 /* Copy data to the output buffer */
455 for (i
= 0; i
< len
; i
++)
456 val
[i
] = bitrev8(p
[i
]);
458 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
459 __func__
, reg
, len
, len
, val
);
468 static int r820t_set_mux(struct r820t_priv
*priv
, u32 freq
)
470 const struct r820t_freq_range
*range
;
472 u8 val
, reg08
, reg09
;
474 /* Get the proper frequency range */
475 freq
= freq
/ 1000000;
476 for (i
= 0; i
< ARRAY_SIZE(freq_ranges
) - 1; i
++) {
477 if (freq
< freq_ranges
[i
+ 1].freq
)
480 range
= &freq_ranges
[i
];
482 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i
, freq
);
485 rc
= r820t_write_reg_mask(priv
, 0x17, range
->open_d
, 0x08);
490 rc
= r820t_write_reg_mask(priv
, 0x1a, range
->rf_mux_ploy
, 0xc3);
495 rc
= r820t_write_reg(priv
, 0x1b, range
->tf_c
);
499 /* XTAL CAP & Drive */
500 switch (priv
->xtal_cap_sel
) {
501 case XTAL_LOW_CAP_30P
:
502 case XTAL_LOW_CAP_20P
:
503 val
= range
->xtal_cap20p
| 0x08;
505 case XTAL_LOW_CAP_10P
:
506 val
= range
->xtal_cap10p
| 0x08;
508 case XTAL_HIGH_CAP_0P
:
509 val
= range
->xtal_cap0p
| 0x00;
512 case XTAL_LOW_CAP_0P
:
513 val
= range
->xtal_cap0p
| 0x08;
516 rc
= r820t_write_reg_mask(priv
, 0x10, val
, 0x0b);
520 if (priv
->imr_done
) {
521 reg08
= priv
->imr_data
[range
->imr_mem
].gain_x
;
522 reg09
= priv
->imr_data
[range
->imr_mem
].phase_y
;
527 rc
= r820t_write_reg_mask(priv
, 0x08, reg08
, 0x3f);
531 rc
= r820t_write_reg_mask(priv
, 0x09, reg09
, 0x3f);
536 static int r820t_set_pll(struct r820t_priv
*priv
, enum v4l2_tuner_type type
,
541 unsigned sleep_time
= 10000;
542 u32 vco_fra
; /* VCO contribution by SDM (kHz) */
543 u32 vco_min
= 1770000;
544 u32 vco_max
= vco_min
* 2;
552 u8 ni
, si
, nint
, vco_fine_tune
, val
;
555 /* Frequency in kHz */
557 pll_ref
= priv
->cfg
->xtal
/ 1000;
559 if ((priv
->cfg
->rafael_chip
== CHIP_R620D
) ||
560 (priv
->cfg
->rafael_chip
== CHIP_R828D
) ||
561 (priv
->cfg
->rafael_chip
== CHIP_R828
)) {
562 /* ref set refdiv2, reffreq = Xtal/2 on ATV application */
563 if (type
!= V4L2_TUNER_DIGITAL_TV
) {
569 if (priv
->cfg
->xtal
> 24000000) {
575 tuner_dbg("set r820t pll for frequency %d kHz = %d%s\n",
576 freq
, pll_ref
, refdiv2
? " / 2" : "");
578 rc
= r820t_write_reg_mask(priv
, 0x10, refdiv2
, 0x10);
582 /* set pll autotune = 128kHz */
583 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
587 /* set VCO current = 100 */
588 rc
= r820t_write_reg_mask(priv
, 0x12, 0x80, 0xe0);
592 /* Calculate divider */
593 while (mix_div
<= 64) {
594 if (((freq
* mix_div
) >= vco_min
) &&
595 ((freq
* mix_div
) < vco_max
)) {
597 while (div_buf
> 2) {
598 div_buf
= div_buf
>> 1;
603 mix_div
= mix_div
<< 1;
606 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
610 vco_fine_tune
= (data
[4] & 0x30) >> 4;
612 if (vco_fine_tune
> VCO_POWER_REF
)
613 div_num
= div_num
- 1;
614 else if (vco_fine_tune
< VCO_POWER_REF
)
615 div_num
= div_num
+ 1;
617 rc
= r820t_write_reg_mask(priv
, 0x10, div_num
<< 5, 0xe0);
621 vco_freq
= (u64
)(freq
* (u64
)mix_div
);
624 do_div(tmp64
, 2 * pll_ref
);
627 tmp64
= vco_freq
- ((u64
)2) * pll_ref
* nint
;
629 vco_fra
= (u16
)(tmp64
);
631 /* boundary spur prevention */
632 if (vco_fra
< pll_ref
/ 64) {
634 } else if (vco_fra
> pll_ref
* 127 / 64) {
637 } else if ((vco_fra
> pll_ref
* 127 / 128) && (vco_fra
< pll_ref
)) {
638 vco_fra
= pll_ref
* 127 / 128;
639 } else if ((vco_fra
> pll_ref
) && (vco_fra
< pll_ref
* 129 / 128)) {
640 vco_fra
= pll_ref
* 129 / 128;
644 tuner_info("No valid PLL values for %u kHz!\n", freq
);
648 ni
= (nint
- 13) / 4;
649 si
= nint
- 4 * ni
- 13;
651 rc
= r820t_write_reg(priv
, 0x14, ni
+ (si
<< 6));
661 rc
= r820t_write_reg_mask(priv
, 0x12, val
, 0x08);
666 while (vco_fra
> 1) {
667 if (vco_fra
> (2 * pll_ref
/ n_sdm
)) {
668 sdm
= sdm
+ 32768 / (n_sdm
/ 2);
669 vco_fra
= vco_fra
- 2 * pll_ref
/ n_sdm
;
676 rc
= r820t_write_reg_mask(priv
, 0x16, sdm
>> 8, 0x08);
679 rc
= r820t_write_reg_mask(priv
, 0x15, sdm
& 0xff, 0x08);
683 for (i
= 0; i
< 2; i
++) {
684 usleep_range(sleep_time
, sleep_time
+ 1000);
686 /* Check if PLL has locked */
687 rc
= r820t_read(priv
, 0x00, data
, 3);
694 /* Didn't lock. Increase VCO current */
695 rc
= r820t_write_reg_mask(priv
, 0x12, 0x60, 0xe0);
701 if (!(data
[2] & 0x40)) {
702 priv
->has_lock
= false;
706 priv
->has_lock
= true;
707 tuner_dbg("tuner has lock at frequency %d kHz\n", freq
);
709 /* set pll autotune = 8kHz */
710 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x08, 0x08);
715 static int r820t_sysfreq_sel(struct r820t_priv
*priv
, u32 freq
,
716 enum v4l2_tuner_type type
,
721 u8 mixer_top
, lna_top
, cp_cur
, div_buf_cur
, lna_vth_l
, mixer_vth_l
;
722 u8 air_cable1_in
, cable2_in
, pre_dect
, lna_discharge
, filter_cur
;
724 tuner_dbg("adjusting tuner parameters for the standard\n");
728 if ((freq
== 506000000) || (freq
== 666000000) ||
729 (freq
== 818000000)) {
730 mixer_top
= 0x14; /* mixer top:14 , top-1, low-discharge */
731 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
732 cp_cur
= 0x28; /* 101, 0.2 */
733 div_buf_cur
= 0x20; /* 10, 200u */
735 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
736 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
737 cp_cur
= 0x38; /* 111, auto */
738 div_buf_cur
= 0x30; /* 11, 150u */
740 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
741 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
742 air_cable1_in
= 0x00;
746 filter_cur
= 0x40; /* 10, low */
749 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
750 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
751 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
752 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
753 air_cable1_in
= 0x00;
757 cp_cur
= 0x38; /* 111, auto */
758 div_buf_cur
= 0x30; /* 11, 150u */
759 filter_cur
= 0x40; /* 10, low */
762 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
763 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
764 lna_vth_l
= 0x75; /* lna vth 1.04 , vtl 0.84 */
765 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
766 air_cable1_in
= 0x00;
770 cp_cur
= 0x38; /* 111, auto */
771 div_buf_cur
= 0x30; /* 11, 150u */
772 filter_cur
= 0x40; /* 10, low */
774 default: /* DVB-T 8M */
775 mixer_top
= 0x24; /* mixer top:13 , top-1, low-discharge */
776 lna_top
= 0xe5; /* detect bw 3, lna top:4, predet top:2 */
777 lna_vth_l
= 0x53; /* lna vth 0.84 , vtl 0.64 */
778 mixer_vth_l
= 0x75; /* mixer vth 1.04, vtl 0.84 */
779 air_cable1_in
= 0x00;
783 cp_cur
= 0x38; /* 111, auto */
784 div_buf_cur
= 0x30; /* 11, 150u */
785 filter_cur
= 0x40; /* 10, low */
789 if (priv
->cfg
->use_diplexer
&&
790 ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
791 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
792 (priv
->cfg
->rafael_chip
== CHIP_R820C
))) {
794 air_cable1_in
= 0x00;
796 air_cable1_in
= 0x60;
800 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0xc7);
803 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0xf8);
806 rc
= r820t_write_reg(priv
, 0x0d, lna_vth_l
);
809 rc
= r820t_write_reg(priv
, 0x0e, mixer_vth_l
);
813 /* Air-IN only for Astrometa */
814 rc
= r820t_write_reg_mask(priv
, 0x05, air_cable1_in
, 0x60);
817 rc
= r820t_write_reg_mask(priv
, 0x06, cable2_in
, 0x08);
821 rc
= r820t_write_reg_mask(priv
, 0x11, cp_cur
, 0x38);
824 rc
= r820t_write_reg_mask(priv
, 0x17, div_buf_cur
, 0x30);
827 rc
= r820t_write_reg_mask(priv
, 0x0a, filter_cur
, 0x60);
831 * Original driver initializes regs 0x05 and 0x06 with the
832 * same value again on this point. Probably, it is just an
840 tuner_dbg("adjusting LNA parameters\n");
841 if (type
!= V4L2_TUNER_ANALOG_TV
) {
842 /* LNA TOP: lowest */
843 rc
= r820t_write_reg_mask(priv
, 0x1d, 0, 0x38);
848 rc
= r820t_write_reg_mask(priv
, 0x1c, 0, 0x04);
852 /* 0: PRE_DECT off */
853 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
858 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x30, 0x30);
864 /* write LNA TOP = 3 */
865 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x18, 0x38);
870 * write discharge mode
871 * FIXME: IMHO, the mask here is wrong, but it matches
872 * what's there at the original driver
874 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
878 /* LNA discharge current */
879 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
884 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x20, 0x30);
889 rc
= r820t_write_reg_mask(priv
, 0x06, 0, 0x40);
894 rc
= r820t_write_reg_mask(priv
, 0x1d, lna_top
, 0x38);
899 * write discharge mode
900 * FIXME: IMHO, the mask here is wrong, but it matches
901 * what's there at the original driver
903 rc
= r820t_write_reg_mask(priv
, 0x1c, mixer_top
, 0x04);
907 /* LNA discharge current */
908 rc
= r820t_write_reg_mask(priv
, 0x1e, lna_discharge
, 0x1f);
912 /* agc clk 1Khz, external det1 cap 1u */
913 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x30);
917 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x04);
924 static int r820t_set_tv_standard(struct r820t_priv
*priv
,
926 enum v4l2_tuner_type type
,
927 v4l2_std_id std
, u32 delsys
)
931 u32 if_khz
, filt_cal_lo
;
933 u8 filt_gain
, img_r
, filt_q
, hp_cor
, ext_enable
, loop_through
;
934 u8 lt_att
, flt_ext_widest
, polyfil_cur
;
935 bool need_calibration
;
937 tuner_dbg("selecting the delivery system\n");
939 if (delsys
== SYS_ISDBT
) {
942 filt_gain
= 0x10; /* +3db, 6mhz on */
943 img_r
= 0x00; /* image negative */
944 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
945 hp_cor
= 0x6a; /* 1.7m disable, +2cap, 1.25mhz */
946 ext_enable
= 0x40; /* r30[6], ext enable; r30[5]:0 ext at lna max */
947 loop_through
= 0x00; /* r5[7], lt on */
948 lt_att
= 0x00; /* r31[7], lt att enable */
949 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
950 polyfil_cur
= 0x60; /* r25[6:5]:min */
954 filt_cal_lo
= 56000; /* 52000->56000 */
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
= 0x6b; /* 1.7m disable, +2cap, 1.0mhz */
959 ext_enable
= 0x60; /* 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 (bw
== 7) {
967 * There are two 7 MHz tables defined on the original
968 * driver, but just the second one seems to be visible
969 * by rtl2832. Keep this one here commented, as it
970 * might be needed in the future
975 filt_gain
= 0x10; /* +3db, 6mhz on */
976 img_r
= 0x00; /* image negative */
977 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
978 hp_cor
= 0x2b; /* 1.7m disable, +1cap, 1.0mhz */
979 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
980 loop_through
= 0x00; /* r5[7], lt on */
981 lt_att
= 0x00; /* r31[7], lt att enable */
982 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
983 polyfil_cur
= 0x60; /* r25[6:5]:min */
985 /* 7 MHz, second table */
988 filt_gain
= 0x10; /* +3db, 6mhz on */
989 img_r
= 0x00; /* image negative */
990 filt_q
= 0x10; /* r10[4]:low q(1'b1) */
991 hp_cor
= 0x2a; /* 1.7m disable, +1cap, 1.25mhz */
992 ext_enable
= 0x60; /* r30[6]=1 ext enable; r30[5]:1 ext at lna max-1 */
993 loop_through
= 0x00; /* r5[7], lt on */
994 lt_att
= 0x00; /* r31[7], lt att enable */
995 flt_ext_widest
= 0x00; /* r15[7]: flt_ext_wide off */
996 polyfil_cur
= 0x60; /* r25[6:5]:min */
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
= 0x0b; /* 1.7m disable, +0cap, 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 */
1012 /* Initialize the shadow registers */
1013 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1015 /* Init Flag & Xtal_check Result */
1017 val
= 1 | priv
->xtal_cap_sel
<< 1;
1020 rc
= r820t_write_reg_mask(priv
, 0x0c, val
, 0x0f);
1025 rc
= r820t_write_reg_mask(priv
, 0x13, VER_NUM
, 0x3f);
1029 /* for LT Gain test */
1030 if (type
!= V4L2_TUNER_ANALOG_TV
) {
1031 rc
= r820t_write_reg_mask(priv
, 0x1d, 0x00, 0x38);
1034 usleep_range(1000, 2000);
1036 priv
->int_freq
= if_khz
* 1000;
1038 /* Check if standard changed. If so, filter calibration is needed */
1039 if (type
!= priv
->type
)
1040 need_calibration
= true;
1041 else if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
!= priv
->std
))
1042 need_calibration
= true;
1043 else if ((type
== V4L2_TUNER_DIGITAL_TV
) &&
1044 ((delsys
!= priv
->delsys
) || bw
!= priv
->bw
))
1045 need_calibration
= true;
1047 need_calibration
= false;
1049 if (need_calibration
) {
1050 tuner_dbg("calibrating the tuner\n");
1051 for (i
= 0; i
< 2; i
++) {
1053 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0x60);
1057 /* set cali clk =on */
1058 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x04, 0x04);
1062 /* X'tal cap 0pF for PLL */
1063 rc
= r820t_write_reg_mask(priv
, 0x10, 0x00, 0x03);
1067 rc
= r820t_set_pll(priv
, type
, filt_cal_lo
);
1068 if (rc
< 0 || !priv
->has_lock
)
1072 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x10, 0x10);
1076 usleep_range(1000, 2000);
1079 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x00, 0x10);
1083 /* set cali clk =off */
1084 rc
= r820t_write_reg_mask(priv
, 0x0f, 0x00, 0x04);
1088 /* Check if calibration worked */
1089 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1093 priv
->fil_cal_code
= data
[4] & 0x0f;
1094 if (priv
->fil_cal_code
&& priv
->fil_cal_code
!= 0x0f)
1098 if (priv
->fil_cal_code
== 0x0f)
1099 priv
->fil_cal_code
= 0;
1102 rc
= r820t_write_reg_mask(priv
, 0x0a,
1103 filt_q
| priv
->fil_cal_code
, 0x1f);
1107 /* Set BW, Filter_gain, & HP corner */
1108 rc
= r820t_write_reg_mask(priv
, 0x0b, hp_cor
, 0x10);
1114 rc
= r820t_write_reg_mask(priv
, 0x07, img_r
, 0x80);
1118 /* Set filt_3dB, V6MHz */
1119 rc
= r820t_write_reg_mask(priv
, 0x06, filt_gain
, 0x30);
1123 /* channel filter extension */
1124 rc
= r820t_write_reg_mask(priv
, 0x1e, ext_enable
, 0x60);
1129 rc
= r820t_write_reg_mask(priv
, 0x05, loop_through
, 0x80);
1133 /* Loop through attenuation */
1134 rc
= r820t_write_reg_mask(priv
, 0x1f, lt_att
, 0x80);
1138 /* filter extension widest */
1139 rc
= r820t_write_reg_mask(priv
, 0x0f, flt_ext_widest
, 0x80);
1143 /* RF poly filter current */
1144 rc
= r820t_write_reg_mask(priv
, 0x19, polyfil_cur
, 0x60);
1148 /* Store current standard. If it changes, re-calibrate the tuner */
1149 priv
->delsys
= delsys
;
1157 static int r820t_read_gain(struct r820t_priv
*priv
)
1162 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1166 return ((data
[3] & 0x0f) << 1) + ((data
[3] & 0xf0) >> 4);
1169 static int r820t_set_gain_mode(struct r820t_priv
*priv
,
1170 bool set_manual_gain
,
1175 if (set_manual_gain
) {
1176 int i
, total_gain
= 0;
1177 uint8_t mix_index
= 0, lna_index
= 0;
1181 rc
= r820t_write_reg_mask(priv
, 0x05, 0x10, 0x10);
1185 /* Mixer auto off */
1186 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1190 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1194 /* set fixed VGA gain for now (16.3 dB) */
1195 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x08, 0x9f);
1199 for (i
= 0; i
< 15; i
++) {
1200 if (total_gain
>= gain
)
1203 total_gain
+= r820t_lna_gain_steps
[++lna_index
];
1205 if (total_gain
>= gain
)
1208 total_gain
+= r820t_mixer_gain_steps
[++mix_index
];
1212 rc
= r820t_write_reg_mask(priv
, 0x05, lna_index
, 0x0f);
1216 /* set Mixer gain */
1217 rc
= r820t_write_reg_mask(priv
, 0x07, mix_index
, 0x0f);
1222 rc
= r820t_write_reg_mask(priv
, 0x05, 0, 0xef);
1227 rc
= r820t_write_reg_mask(priv
, 0x07, 0x10, 0xef);
1231 /* set fixed VGA gain for now (26.5 dB) */
1232 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1241 static int generic_set_freq(struct dvb_frontend
*fe
,
1242 u32 freq
/* in HZ */,
1244 enum v4l2_tuner_type type
,
1245 v4l2_std_id std
, u32 delsys
)
1247 struct r820t_priv
*priv
= fe
->tuner_priv
;
1251 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1254 rc
= r820t_set_tv_standard(priv
, bw
, type
, std
, delsys
);
1258 if ((type
== V4L2_TUNER_ANALOG_TV
) && (std
== V4L2_STD_SECAM_LC
))
1259 lo_freq
= freq
- priv
->int_freq
;
1261 lo_freq
= freq
+ priv
->int_freq
;
1263 rc
= r820t_set_mux(priv
, lo_freq
);
1267 rc
= r820t_set_gain_mode(priv
, true, 0);
1271 rc
= r820t_set_pll(priv
, type
, lo_freq
);
1272 if (rc
< 0 || !priv
->has_lock
)
1275 rc
= r820t_sysfreq_sel(priv
, freq
, type
, std
, delsys
);
1279 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1280 __func__
, freq
, r820t_read_gain(priv
));
1285 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
1290 * r820t standby logic
1293 static int r820t_standby(struct r820t_priv
*priv
)
1297 rc
= r820t_write_reg(priv
, 0x06, 0xb1);
1300 rc
= r820t_write_reg(priv
, 0x05, 0x03);
1303 rc
= r820t_write_reg(priv
, 0x07, 0x3a);
1306 rc
= r820t_write_reg(priv
, 0x08, 0x40);
1309 rc
= r820t_write_reg(priv
, 0x09, 0xc0);
1312 rc
= r820t_write_reg(priv
, 0x0a, 0x36);
1315 rc
= r820t_write_reg(priv
, 0x0c, 0x35);
1318 rc
= r820t_write_reg(priv
, 0x0f, 0x68);
1321 rc
= r820t_write_reg(priv
, 0x11, 0x03);
1324 rc
= r820t_write_reg(priv
, 0x17, 0xf4);
1327 rc
= r820t_write_reg(priv
, 0x19, 0x0c);
1329 /* Force initial calibration */
1336 * r820t device init logic
1339 static int r820t_xtal_check(struct r820t_priv
*priv
)
1344 /* Initialize the shadow registers */
1345 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1347 /* cap 30pF & Drive Low */
1348 rc
= r820t_write_reg_mask(priv
, 0x10, 0x0b, 0x0b);
1352 /* set pll autotune = 128kHz */
1353 rc
= r820t_write_reg_mask(priv
, 0x1a, 0x00, 0x0c);
1357 /* set manual initial reg = 111111; */
1358 rc
= r820t_write_reg_mask(priv
, 0x13, 0x7f, 0x7f);
1363 rc
= r820t_write_reg_mask(priv
, 0x13, 0x00, 0x40);
1367 /* Try several xtal capacitor alternatives */
1368 for (i
= 0; i
< ARRAY_SIZE(r820t_xtal_capacitor
); i
++) {
1369 rc
= r820t_write_reg_mask(priv
, 0x10,
1370 r820t_xtal_capacitor
[i
][0], 0x1b);
1374 usleep_range(5000, 6000);
1376 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1379 if ((!data
[2]) & 0x40)
1382 val
= data
[2] & 0x3f;
1384 if (priv
->cfg
->xtal
== 16000000 && (val
> 29 || val
< 23))
1391 if (i
== ARRAY_SIZE(r820t_xtal_capacitor
))
1394 return r820t_xtal_capacitor
[i
][1];
1397 static int r820t_imr_prepare(struct r820t_priv
*priv
)
1401 /* Initialize the shadow registers */
1402 memcpy(priv
->regs
, r820t_init_array
, sizeof(r820t_init_array
));
1404 /* lna off (air-in off) */
1405 rc
= r820t_write_reg_mask(priv
, 0x05, 0x20, 0x20);
1409 /* mixer gain mode = manual */
1410 rc
= r820t_write_reg_mask(priv
, 0x07, 0, 0x10);
1414 /* filter corner = lowest */
1415 rc
= r820t_write_reg_mask(priv
, 0x0a, 0x0f, 0x0f);
1419 /* filter bw=+2cap, hp=5M */
1420 rc
= r820t_write_reg_mask(priv
, 0x0b, 0x60, 0x6f);
1424 /* adc=on, vga code mode, gain = 26.5dB */
1425 rc
= r820t_write_reg_mask(priv
, 0x0c, 0x0b, 0x9f);
1430 rc
= r820t_write_reg_mask(priv
, 0x0f, 0, 0x08);
1434 /* ring power = on */
1435 rc
= r820t_write_reg_mask(priv
, 0x18, 0x10, 0x10);
1439 /* from ring = ring pll in */
1440 rc
= r820t_write_reg_mask(priv
, 0x1c, 0x02, 0x02);
1444 /* sw_pdect = det3 */
1445 rc
= r820t_write_reg_mask(priv
, 0x1e, 0x80, 0x80);
1450 rc
= r820t_write_reg_mask(priv
, 0x06, 0x20, 0x20);
1455 static int r820t_multi_read(struct r820t_priv
*priv
)
1458 u8 data
[2], min
= 0, max
= 255, sum
= 0;
1460 usleep_range(5000, 6000);
1462 for (i
= 0; i
< 6; i
++) {
1463 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
1475 rc
= sum
- max
- min
;
1480 static int r820t_imr_cross(struct r820t_priv
*priv
,
1481 struct r820t_sect_type iq_point
[3],
1484 struct r820t_sect_type cross
[5]; /* (0,0)(0,Q-1)(0,I-1)(Q-1,0)(I-1,0) */
1485 struct r820t_sect_type tmp
;
1489 reg08
= r820t_read_cache_reg(priv
, 8) & 0xc0;
1490 reg09
= r820t_read_cache_reg(priv
, 9) & 0xc0;
1496 for (i
= 0; i
< 5; i
++) {
1499 cross
[i
].gain_x
= reg08
;
1500 cross
[i
].phase_y
= reg09
;
1503 cross
[i
].gain_x
= reg08
; /* 0 */
1504 cross
[i
].phase_y
= reg09
+ 1; /* Q-1 */
1507 cross
[i
].gain_x
= reg08
; /* 0 */
1508 cross
[i
].phase_y
= (reg09
| 0x20) + 1; /* I-1 */
1511 cross
[i
].gain_x
= reg08
+ 1; /* Q-1 */
1512 cross
[i
].phase_y
= reg09
;
1515 cross
[i
].gain_x
= (reg08
| 0x20) + 1; /* I-1 */
1516 cross
[i
].phase_y
= reg09
;
1519 rc
= r820t_write_reg(priv
, 0x08, cross
[i
].gain_x
);
1523 rc
= r820t_write_reg(priv
, 0x09, cross
[i
].phase_y
);
1527 rc
= r820t_multi_read(priv
);
1531 cross
[i
].value
= rc
;
1533 if (cross
[i
].value
< tmp
.value
)
1534 memcpy(&tmp
, &cross
[i
], sizeof(tmp
));
1537 if ((tmp
.phase_y
& 0x1f) == 1) { /* y-direction */
1540 iq_point
[0] = cross
[0];
1541 iq_point
[1] = cross
[1];
1542 iq_point
[2] = cross
[2];
1543 } else { /* (0,0) or x-direction */
1546 iq_point
[0] = cross
[0];
1547 iq_point
[1] = cross
[3];
1548 iq_point
[2] = cross
[4];
1553 static void r820t_compre_cor(struct r820t_sect_type iq
[3])
1557 for (i
= 3; i
> 0; i
--) {
1558 if (iq
[0].value
> iq
[i
- 1].value
)
1559 swap(iq
[0], iq
[i
- 1]);
1563 static int r820t_compre_step(struct r820t_priv
*priv
,
1564 struct r820t_sect_type iq
[3], u8 reg
)
1567 struct r820t_sect_type tmp
;
1570 * Purpose: if (Gain<9 or Phase<9), Gain+1 or Phase+1 and compare
1572 * new < min => update to min and continue
1576 /* min value already saved in iq[0] */
1577 tmp
.phase_y
= iq
[0].phase_y
;
1578 tmp
.gain_x
= iq
[0].gain_x
;
1580 while (((tmp
.gain_x
& 0x1f) < IMR_TRIAL
) &&
1581 ((tmp
.phase_y
& 0x1f) < IMR_TRIAL
)) {
1587 rc
= r820t_write_reg(priv
, 0x08, tmp
.gain_x
);
1591 rc
= r820t_write_reg(priv
, 0x09, tmp
.phase_y
);
1595 rc
= r820t_multi_read(priv
);
1600 if (tmp
.value
<= iq
[0].value
) {
1601 iq
[0].gain_x
= tmp
.gain_x
;
1602 iq
[0].phase_y
= tmp
.phase_y
;
1603 iq
[0].value
= tmp
.value
;
1613 static int r820t_iq_tree(struct r820t_priv
*priv
,
1614 struct r820t_sect_type iq
[3],
1615 u8 fix_val
, u8 var_val
, u8 fix_reg
)
1621 * record IMC results by input gain/phase location then adjust
1622 * gain or phase positive 1 step and negtive 1 step,
1623 * both record results
1626 if (fix_reg
== 0x08)
1631 for (i
= 0; i
< 3; i
++) {
1632 rc
= r820t_write_reg(priv
, fix_reg
, fix_val
);
1636 rc
= r820t_write_reg(priv
, var_reg
, var_val
);
1640 rc
= r820t_multi_read(priv
);
1645 if (fix_reg
== 0x08) {
1646 iq
[i
].gain_x
= fix_val
;
1647 iq
[i
].phase_y
= var_val
;
1649 iq
[i
].phase_y
= fix_val
;
1650 iq
[i
].gain_x
= var_val
;
1653 if (i
== 0) { /* try right-side point */
1655 } else if (i
== 1) { /* try left-side point */
1656 /* if absolute location is 1, change I/Q direction */
1657 if ((var_val
& 0x1f) < 0x02) {
1658 tmp
= 2 - (var_val
& 0x1f);
1660 /* b[5]:I/Q selection. 0:Q-path, 1:I-path */
1661 if (var_val
& 0x20) {
1665 var_val
|= 0x20 | tmp
;
1676 static int r820t_section(struct r820t_priv
*priv
,
1677 struct r820t_sect_type
*iq_point
)
1680 struct r820t_sect_type compare_iq
[3], compare_bet
[3];
1682 /* Try X-1 column and save min result to compare_bet[0] */
1683 if (!(iq_point
->gain_x
& 0x1f))
1684 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) & 0xdf) + 1; /* Q-path, Gain=1 */
1686 compare_iq
[0].gain_x
= iq_point
->gain_x
- 1; /* left point */
1687 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1690 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1691 compare_iq
[0].phase_y
, 0x08);
1695 r820t_compre_cor(compare_iq
);
1697 compare_bet
[0] = compare_iq
[0];
1699 /* Try X column and save min result to compare_bet[1] */
1700 compare_iq
[0].gain_x
= iq_point
->gain_x
;
1701 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1703 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1704 compare_iq
[0].phase_y
, 0x08);
1708 r820t_compre_cor(compare_iq
);
1710 compare_bet
[1] = compare_iq
[0];
1712 /* Try X+1 column and save min result to compare_bet[2] */
1713 if ((iq_point
->gain_x
& 0x1f) == 0x00)
1714 compare_iq
[0].gain_x
= ((iq_point
->gain_x
) | 0x20) + 1; /* I-path, Gain=1 */
1716 compare_iq
[0].gain_x
= iq_point
->gain_x
+ 1;
1717 compare_iq
[0].phase_y
= iq_point
->phase_y
;
1719 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1720 compare_iq
[0].phase_y
, 0x08);
1724 r820t_compre_cor(compare_iq
);
1726 compare_bet
[2] = compare_iq
[0];
1728 r820t_compre_cor(compare_bet
);
1730 *iq_point
= compare_bet
[0];
1735 static int r820t_vga_adjust(struct r820t_priv
*priv
)
1740 /* increase vga power to let image significant */
1741 for (vga_count
= 12; vga_count
< 16; vga_count
++) {
1742 rc
= r820t_write_reg_mask(priv
, 0x0c, vga_count
, 0x0f);
1746 usleep_range(10000, 11000);
1748 rc
= r820t_multi_read(priv
);
1759 static int r820t_iq(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1761 struct r820t_sect_type compare_iq
[3];
1763 u8 x_direction
= 0; /* 1:x, 0:y */
1764 u8 dir_reg
, other_reg
;
1766 r820t_vga_adjust(priv
);
1768 rc
= r820t_imr_cross(priv
, compare_iq
, &x_direction
);
1772 if (x_direction
== 1) {
1780 /* compare and find min of 3 points. determine i/q direction */
1781 r820t_compre_cor(compare_iq
);
1783 /* increase step to find min value of this direction */
1784 rc
= r820t_compre_step(priv
, compare_iq
, dir_reg
);
1788 /* the other direction */
1789 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1790 compare_iq
[0].phase_y
, dir_reg
);
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 on this direction */
1798 rc
= r820t_compre_step(priv
, compare_iq
, other_reg
);
1802 /* check 3 points again */
1803 rc
= r820t_iq_tree(priv
, compare_iq
, compare_iq
[0].gain_x
,
1804 compare_iq
[0].phase_y
, other_reg
);
1808 r820t_compre_cor(compare_iq
);
1810 /* section-9 check */
1811 rc
= r820t_section(priv
, compare_iq
);
1813 *iq_pont
= compare_iq
[0];
1815 /* reset gain/phase control setting */
1816 rc
= r820t_write_reg_mask(priv
, 0x08, 0, 0x3f);
1820 rc
= r820t_write_reg_mask(priv
, 0x09, 0, 0x3f);
1825 static int r820t_f_imr(struct r820t_priv
*priv
, struct r820t_sect_type
*iq_pont
)
1829 r820t_vga_adjust(priv
);
1832 * search surrounding points from previous point
1833 * try (x-1), (x), (x+1) columns, and find min IMR result point
1835 rc
= r820t_section(priv
, iq_pont
);
1842 static int r820t_imr(struct r820t_priv
*priv
, unsigned imr_mem
, bool im_flag
)
1844 struct r820t_sect_type imr_point
;
1846 u32 ring_vco
, ring_freq
, ring_ref
;
1848 int reg18
, reg19
, reg1f
;
1850 if (priv
->cfg
->xtal
> 24000000)
1851 ring_ref
= priv
->cfg
->xtal
/ 2;
1853 ring_ref
= priv
->cfg
->xtal
;
1855 for (n
= 0; n
< 16; n
++) {
1856 if ((16 + n
) * 8 * ring_ref
>= 3100000) {
1861 /* n_ring not found */
1866 reg18
= r820t_read_cache_reg(priv
, 0x18);
1867 reg19
= r820t_read_cache_reg(priv
, 0x19);
1868 reg1f
= r820t_read_cache_reg(priv
, 0x1f);
1870 reg18
&= 0xf0; /* set ring[3:0] */
1873 ring_vco
= (16 + n_ring
) * 8 * ring_ref
;
1875 reg18
&= 0xdf; /* clear ring_se23 */
1876 reg19
&= 0xfc; /* clear ring_seldiv */
1877 reg1f
&= 0xfc; /* clear ring_att */
1881 ring_freq
= ring_vco
/ 48;
1882 reg18
|= 0x20; /* ring_se23 = 1 */
1883 reg19
|= 0x03; /* ring_seldiv = 3 */
1884 reg1f
|= 0x02; /* ring_att 10 */
1887 ring_freq
= ring_vco
/ 16;
1888 reg18
|= 0x00; /* ring_se23 = 0 */
1889 reg19
|= 0x02; /* ring_seldiv = 2 */
1890 reg1f
|= 0x00; /* pw_ring 00 */
1893 ring_freq
= ring_vco
/ 8;
1894 reg18
|= 0x00; /* ring_se23 = 0 */
1895 reg19
|= 0x01; /* ring_seldiv = 1 */
1896 reg1f
|= 0x03; /* pw_ring 11 */
1899 ring_freq
= ring_vco
/ 6;
1900 reg18
|= 0x20; /* ring_se23 = 1 */
1901 reg19
|= 0x00; /* ring_seldiv = 0 */
1902 reg1f
|= 0x03; /* pw_ring 11 */
1905 ring_freq
= ring_vco
/ 4;
1906 reg18
|= 0x00; /* ring_se23 = 0 */
1907 reg19
|= 0x00; /* ring_seldiv = 0 */
1908 reg1f
|= 0x01; /* pw_ring 01 */
1911 ring_freq
= ring_vco
/ 4;
1912 reg18
|= 0x00; /* ring_se23 = 0 */
1913 reg19
|= 0x00; /* ring_seldiv = 0 */
1914 reg1f
|= 0x01; /* pw_ring 01 */
1919 /* write pw_ring, n_ring, ringdiv2 registers */
1921 /* n_ring, ring_se23 */
1922 rc
= r820t_write_reg(priv
, 0x18, reg18
);
1927 rc
= r820t_write_reg(priv
, 0x19, reg19
);
1932 rc
= r820t_write_reg(priv
, 0x1f, reg1f
);
1936 /* mux input freq ~ rf_in freq */
1937 rc
= r820t_set_mux(priv
, (ring_freq
- 5300) * 1000);
1941 rc
= r820t_set_pll(priv
, V4L2_TUNER_DIGITAL_TV
,
1942 (ring_freq
- 5300) * 1000);
1943 if (!priv
->has_lock
)
1949 rc
= r820t_iq(priv
, &imr_point
);
1951 imr_point
.gain_x
= priv
->imr_data
[3].gain_x
;
1952 imr_point
.phase_y
= priv
->imr_data
[3].phase_y
;
1953 imr_point
.value
= priv
->imr_data
[3].value
;
1955 rc
= r820t_f_imr(priv
, &imr_point
);
1960 /* save IMR value */
1963 priv
->imr_data
[0].gain_x
= imr_point
.gain_x
;
1964 priv
->imr_data
[0].phase_y
= imr_point
.phase_y
;
1965 priv
->imr_data
[0].value
= imr_point
.value
;
1968 priv
->imr_data
[1].gain_x
= imr_point
.gain_x
;
1969 priv
->imr_data
[1].phase_y
= imr_point
.phase_y
;
1970 priv
->imr_data
[1].value
= imr_point
.value
;
1973 priv
->imr_data
[2].gain_x
= imr_point
.gain_x
;
1974 priv
->imr_data
[2].phase_y
= imr_point
.phase_y
;
1975 priv
->imr_data
[2].value
= imr_point
.value
;
1978 priv
->imr_data
[3].gain_x
= imr_point
.gain_x
;
1979 priv
->imr_data
[3].phase_y
= imr_point
.phase_y
;
1980 priv
->imr_data
[3].value
= imr_point
.value
;
1983 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
1984 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
1985 priv
->imr_data
[4].value
= imr_point
.value
;
1988 priv
->imr_data
[4].gain_x
= imr_point
.gain_x
;
1989 priv
->imr_data
[4].phase_y
= imr_point
.phase_y
;
1990 priv
->imr_data
[4].value
= imr_point
.value
;
1997 static int r820t_imr_callibrate(struct r820t_priv
*priv
)
2005 /* Initialize registers */
2006 rc
= r820t_write(priv
, 0x05,
2007 r820t_init_array
, sizeof(r820t_init_array
));
2011 /* Detect Xtal capacitance */
2012 if ((priv
->cfg
->rafael_chip
== CHIP_R820T
) ||
2013 (priv
->cfg
->rafael_chip
== CHIP_R828S
) ||
2014 (priv
->cfg
->rafael_chip
== CHIP_R820C
)) {
2015 priv
->xtal_cap_sel
= XTAL_HIGH_CAP_0P
;
2017 for (i
= 0; i
< 3; i
++) {
2018 rc
= r820t_xtal_check(priv
);
2021 if (!i
|| rc
> xtal_cap
)
2024 priv
->xtal_cap_sel
= xtal_cap
;
2027 /* Initialize registers */
2028 rc
= r820t_write(priv
, 0x05,
2029 r820t_init_array
, sizeof(r820t_init_array
));
2033 rc
= r820t_imr_prepare(priv
);
2037 rc
= r820t_imr(priv
, 3, true);
2040 rc
= r820t_imr(priv
, 1, false);
2043 rc
= r820t_imr(priv
, 0, false);
2046 rc
= r820t_imr(priv
, 2, false);
2049 rc
= r820t_imr(priv
, 4, false);
2053 priv
->imr_done
= true;
2059 * r820t frontend operations and tuner attach code
2061 * All driver locks and i2c control are only in this part of the code
2064 static int r820t_init(struct dvb_frontend
*fe
)
2066 struct r820t_priv
*priv
= fe
->tuner_priv
;
2069 tuner_dbg("%s:\n", __func__
);
2071 mutex_lock(&priv
->lock
);
2072 if (fe
->ops
.i2c_gate_ctrl
)
2073 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2075 rc
= r820t_imr_callibrate(priv
);
2079 /* Initialize registers */
2080 rc
= r820t_write(priv
, 0x05,
2081 r820t_init_array
, sizeof(r820t_init_array
));
2084 if (fe
->ops
.i2c_gate_ctrl
)
2085 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2086 mutex_unlock(&priv
->lock
);
2089 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2093 static int r820t_sleep(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_standby(priv
);
2106 if (fe
->ops
.i2c_gate_ctrl
)
2107 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2108 mutex_unlock(&priv
->lock
);
2110 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2114 static int r820t_set_analog_freq(struct dvb_frontend
*fe
,
2115 struct analog_parameters
*p
)
2117 struct r820t_priv
*priv
= fe
->tuner_priv
;
2121 tuner_dbg("%s called\n", __func__
);
2123 /* if std is not defined, choose one */
2125 p
->std
= V4L2_STD_MN
;
2127 if ((p
->std
== V4L2_STD_PAL_M
) || (p
->std
== V4L2_STD_NTSC
))
2132 mutex_lock(&priv
->lock
);
2133 if (fe
->ops
.i2c_gate_ctrl
)
2134 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2136 rc
= generic_set_freq(fe
, 62500l * p
->frequency
, bw
,
2137 V4L2_TUNER_ANALOG_TV
, p
->std
, SYS_UNDEFINED
);
2139 if (fe
->ops
.i2c_gate_ctrl
)
2140 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2141 mutex_unlock(&priv
->lock
);
2146 static int r820t_set_params(struct dvb_frontend
*fe
)
2148 struct r820t_priv
*priv
= fe
->tuner_priv
;
2149 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
2153 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2154 __func__
, c
->delivery_system
, c
->frequency
, c
->bandwidth_hz
);
2156 mutex_lock(&priv
->lock
);
2157 if (fe
->ops
.i2c_gate_ctrl
)
2158 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2160 bw
= (c
->bandwidth_hz
+ 500000) / 1000000;
2164 rc
= generic_set_freq(fe
, c
->frequency
, bw
,
2165 V4L2_TUNER_DIGITAL_TV
, 0, c
->delivery_system
);
2167 if (fe
->ops
.i2c_gate_ctrl
)
2168 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2169 mutex_unlock(&priv
->lock
);
2172 tuner_dbg("%s: failed=%d\n", __func__
, rc
);
2176 static int r820t_signal(struct dvb_frontend
*fe
, u16
*strength
)
2178 struct r820t_priv
*priv
= fe
->tuner_priv
;
2181 mutex_lock(&priv
->lock
);
2182 if (fe
->ops
.i2c_gate_ctrl
)
2183 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2185 if (priv
->has_lock
) {
2186 rc
= r820t_read_gain(priv
);
2190 /* A higher gain at LNA means a lower signal strength */
2191 *strength
= (45 - rc
) << 4 | 0xff;
2192 if (*strength
== 0xff)
2199 if (fe
->ops
.i2c_gate_ctrl
)
2200 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2201 mutex_unlock(&priv
->lock
);
2203 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2205 priv
->has_lock
? "PLL locked" : "no signal",
2211 static int r820t_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
2213 struct r820t_priv
*priv
= fe
->tuner_priv
;
2215 tuner_dbg("%s:\n", __func__
);
2217 *frequency
= priv
->int_freq
;
2222 static int r820t_release(struct dvb_frontend
*fe
)
2224 struct r820t_priv
*priv
= fe
->tuner_priv
;
2226 tuner_dbg("%s:\n", __func__
);
2228 mutex_lock(&r820t_list_mutex
);
2231 hybrid_tuner_release_state(priv
);
2233 mutex_unlock(&r820t_list_mutex
);
2235 fe
->tuner_priv
= NULL
;
2237 kfree(fe
->tuner_priv
);
2242 static const struct dvb_tuner_ops r820t_tuner_ops
= {
2244 .name
= "Rafael Micro R820T",
2245 .frequency_min
= 42000000,
2246 .frequency_max
= 1002000000,
2249 .release
= r820t_release
,
2250 .sleep
= r820t_sleep
,
2251 .set_params
= r820t_set_params
,
2252 .set_analog_params
= r820t_set_analog_freq
,
2253 .get_if_frequency
= r820t_get_if_frequency
,
2254 .get_rf_strength
= r820t_signal
,
2257 struct dvb_frontend
*r820t_attach(struct dvb_frontend
*fe
,
2258 struct i2c_adapter
*i2c
,
2259 const struct r820t_config
*cfg
)
2261 struct r820t_priv
*priv
;
2266 mutex_lock(&r820t_list_mutex
);
2268 instance
= hybrid_tuner_request_state(struct r820t_priv
, priv
,
2269 hybrid_tuner_instance_list
,
2274 /* memory allocation failure */
2278 /* new tuner instance */
2281 mutex_init(&priv
->lock
);
2283 fe
->tuner_priv
= priv
;
2286 /* existing tuner instance */
2287 fe
->tuner_priv
= priv
;
2291 memcpy(&fe
->ops
.tuner_ops
, &r820t_tuner_ops
, sizeof(r820t_tuner_ops
));
2293 if (fe
->ops
.i2c_gate_ctrl
)
2294 fe
->ops
.i2c_gate_ctrl(fe
, 1);
2296 /* check if the tuner is there */
2297 rc
= r820t_read(priv
, 0x00, data
, sizeof(data
));
2301 rc
= r820t_sleep(fe
);
2305 tuner_info("Rafael Micro r820t successfully identified\n");
2307 fe
->tuner_priv
= priv
;
2308 memcpy(&fe
->ops
.tuner_ops
, &r820t_tuner_ops
,
2309 sizeof(struct dvb_tuner_ops
));
2311 if (fe
->ops
.i2c_gate_ctrl
)
2312 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2314 mutex_unlock(&r820t_list_mutex
);
2318 if (fe
->ops
.i2c_gate_ctrl
)
2319 fe
->ops
.i2c_gate_ctrl(fe
, 0);
2322 mutex_unlock(&r820t_list_mutex
);
2324 tuner_info("%s: failed=%d\n", __func__
, rc
);
2328 EXPORT_SYMBOL_GPL(r820t_attach
);
2330 MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2331 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2332 MODULE_LICENSE("GPL");