1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/videodev2.h>
36#include <linux/mutex.h>
37#include <linux/slab.h>
38#include <linux/bitrev.h>
39
40#include "tuner-i2c.h"
41#include "r820t.h"
42
43
44
45
46
47#define REG_SHADOW_START 5
48#define NUM_REGS 27
49#define NUM_IMR 5
50#define IMR_TRIAL 9
51
52#define VER_NUM 49
53
54static int debug;
55module_param(debug, int, 0644);
56MODULE_PARM_DESC(debug, "enable verbose debug messages");
57
58static int no_imr_cal;
59module_param(no_imr_cal, int, 0444);
60MODULE_PARM_DESC(no_imr_cal, "Disable IMR calibration at module init");
61
62
63
64
65
66
67enum xtal_cap_value {
68 XTAL_LOW_CAP_30P = 0,
69 XTAL_LOW_CAP_20P,
70 XTAL_LOW_CAP_10P,
71 XTAL_LOW_CAP_0P,
72 XTAL_HIGH_CAP_0P
73};
74
75struct r820t_sect_type {
76 u8 phase_y;
77 u8 gain_x;
78 u16 value;
79};
80
81struct r820t_priv {
82 struct list_head hybrid_tuner_instance_list;
83 const struct r820t_config *cfg;
84 struct tuner_i2c_props i2c_props;
85 struct mutex lock;
86
87 u8 regs[NUM_REGS];
88 u8 buf[NUM_REGS + 1];
89 enum xtal_cap_value xtal_cap_sel;
90 u16 pll;
91 u32 int_freq;
92 u8 fil_cal_code;
93 bool imr_done;
94 bool has_lock;
95 bool init_done;
96 struct r820t_sect_type imr_data[NUM_IMR];
97
98
99 u32 delsys;
100 enum v4l2_tuner_type type;
101 v4l2_std_id std;
102 u32 bw;
103};
104
105struct r820t_freq_range {
106 u32 freq;
107 u8 open_d;
108 u8 rf_mux_ploy;
109 u8 tf_c;
110 u8 xtal_cap20p;
111 u8 xtal_cap10p;
112 u8 xtal_cap0p;
113 u8 imr_mem;
114};
115
116#define VCO_POWER_REF 0x02
117#define DIP_FREQ 32000000
118
119
120
121
122
123static LIST_HEAD(hybrid_tuner_instance_list);
124static DEFINE_MUTEX(r820t_list_mutex);
125
126
127static const u8 r820t_init_array[NUM_REGS] = {
128 0x83, 0x32, 0x75,
129 0xc0, 0x40, 0xd6, 0x6c,
130 0xf5, 0x63, 0x75, 0x68,
131 0x6c, 0x83, 0x80, 0x00,
132 0x0f, 0x00, 0xc0, 0x30,
133 0x48, 0xcc, 0x60, 0x00,
134 0x54, 0xae, 0x4a, 0xc0
135};
136
137
138static const struct r820t_freq_range freq_ranges[] = {
139 {
140 .freq = 0,
141 .open_d = 0x08,
142 .rf_mux_ploy = 0x02,
143 .tf_c = 0xdf,
144 .xtal_cap20p = 0x02,
145 .xtal_cap10p = 0x01,
146 .xtal_cap0p = 0x00,
147 .imr_mem = 0,
148 }, {
149 .freq = 50,
150 .open_d = 0x08,
151 .rf_mux_ploy = 0x02,
152 .tf_c = 0xbe,
153 .xtal_cap20p = 0x02,
154 .xtal_cap10p = 0x01,
155 .xtal_cap0p = 0x00,
156 .imr_mem = 0,
157 }, {
158 .freq = 55,
159 .open_d = 0x08,
160 .rf_mux_ploy = 0x02,
161 .tf_c = 0x8b,
162 .xtal_cap20p = 0x02,
163 .xtal_cap10p = 0x01,
164 .xtal_cap0p = 0x00,
165 .imr_mem = 0,
166 }, {
167 .freq = 60,
168 .open_d = 0x08,
169 .rf_mux_ploy = 0x02,
170 .tf_c = 0x7b,
171 .xtal_cap20p = 0x02,
172 .xtal_cap10p = 0x01,
173 .xtal_cap0p = 0x00,
174 .imr_mem = 0,
175 }, {
176 .freq = 65,
177 .open_d = 0x08,
178 .rf_mux_ploy = 0x02,
179 .tf_c = 0x69,
180 .xtal_cap20p = 0x02,
181 .xtal_cap10p = 0x01,
182 .xtal_cap0p = 0x00,
183 .imr_mem = 0,
184 }, {
185 .freq = 70,
186 .open_d = 0x08,
187 .rf_mux_ploy = 0x02,
188 .tf_c = 0x58,
189 .xtal_cap20p = 0x02,
190 .xtal_cap10p = 0x01,
191 .xtal_cap0p = 0x00,
192 .imr_mem = 0,
193 }, {
194 .freq = 75,
195 .open_d = 0x00,
196 .rf_mux_ploy = 0x02,
197 .tf_c = 0x44,
198 .xtal_cap20p = 0x02,
199 .xtal_cap10p = 0x01,
200 .xtal_cap0p = 0x00,
201 .imr_mem = 0,
202 }, {
203 .freq = 80,
204 .open_d = 0x00,
205 .rf_mux_ploy = 0x02,
206 .tf_c = 0x44,
207 .xtal_cap20p = 0x02,
208 .xtal_cap10p = 0x01,
209 .xtal_cap0p = 0x00,
210 .imr_mem = 0,
211 }, {
212 .freq = 90,
213 .open_d = 0x00,
214 .rf_mux_ploy = 0x02,
215 .tf_c = 0x34,
216 .xtal_cap20p = 0x01,
217 .xtal_cap10p = 0x01,
218 .xtal_cap0p = 0x00,
219 .imr_mem = 0,
220 }, {
221 .freq = 100,
222 .open_d = 0x00,
223 .rf_mux_ploy = 0x02,
224 .tf_c = 0x34,
225 .xtal_cap20p = 0x01,
226 .xtal_cap10p = 0x01,
227 .xtal_cap0p = 0x00,
228 .imr_mem = 0,
229 }, {
230 .freq = 110,
231 .open_d = 0x00,
232 .rf_mux_ploy = 0x02,
233 .tf_c = 0x24,
234 .xtal_cap20p = 0x01,
235 .xtal_cap10p = 0x01,
236 .xtal_cap0p = 0x00,
237 .imr_mem = 1,
238 }, {
239 .freq = 120,
240 .open_d = 0x00,
241 .rf_mux_ploy = 0x02,
242 .tf_c = 0x24,
243 .xtal_cap20p = 0x01,
244 .xtal_cap10p = 0x01,
245 .xtal_cap0p = 0x00,
246 .imr_mem = 1,
247 }, {
248 .freq = 140,
249 .open_d = 0x00,
250 .rf_mux_ploy = 0x02,
251 .tf_c = 0x14,
252 .xtal_cap20p = 0x01,
253 .xtal_cap10p = 0x01,
254 .xtal_cap0p = 0x00,
255 .imr_mem = 1,
256 }, {
257 .freq = 180,
258 .open_d = 0x00,
259 .rf_mux_ploy = 0x02,
260 .tf_c = 0x13,
261 .xtal_cap20p = 0x00,
262 .xtal_cap10p = 0x00,
263 .xtal_cap0p = 0x00,
264 .imr_mem = 1,
265 }, {
266 .freq = 220,
267 .open_d = 0x00,
268 .rf_mux_ploy = 0x02,
269 .tf_c = 0x13,
270 .xtal_cap20p = 0x00,
271 .xtal_cap10p = 0x00,
272 .xtal_cap0p = 0x00,
273 .imr_mem = 2,
274 }, {
275 .freq = 250,
276 .open_d = 0x00,
277 .rf_mux_ploy = 0x02,
278 .tf_c = 0x11,
279 .xtal_cap20p = 0x00,
280 .xtal_cap10p = 0x00,
281 .xtal_cap0p = 0x00,
282 .imr_mem = 2,
283 }, {
284 .freq = 280,
285 .open_d = 0x00,
286 .rf_mux_ploy = 0x02,
287 .tf_c = 0x00,
288 .xtal_cap20p = 0x00,
289 .xtal_cap10p = 0x00,
290 .xtal_cap0p = 0x00,
291 .imr_mem = 2,
292 }, {
293 .freq = 310,
294 .open_d = 0x00,
295 .rf_mux_ploy = 0x41,
296 .tf_c = 0x00,
297 .xtal_cap20p = 0x00,
298 .xtal_cap10p = 0x00,
299 .xtal_cap0p = 0x00,
300 .imr_mem = 2,
301 }, {
302 .freq = 450,
303 .open_d = 0x00,
304 .rf_mux_ploy = 0x41,
305 .tf_c = 0x00,
306 .xtal_cap20p = 0x00,
307 .xtal_cap10p = 0x00,
308 .xtal_cap0p = 0x00,
309 .imr_mem = 3,
310 }, {
311 .freq = 588,
312 .open_d = 0x00,
313 .rf_mux_ploy = 0x40,
314 .tf_c = 0x00,
315 .xtal_cap20p = 0x00,
316 .xtal_cap10p = 0x00,
317 .xtal_cap0p = 0x00,
318 .imr_mem = 3,
319 }, {
320 .freq = 650,
321 .open_d = 0x00,
322 .rf_mux_ploy = 0x40,
323 .tf_c = 0x00,
324 .xtal_cap20p = 0x00,
325 .xtal_cap10p = 0x00,
326 .xtal_cap0p = 0x00,
327 .imr_mem = 4,
328 }
329};
330
331static 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 },
337};
338
339
340
341
342
343
344
345static const int r820t_lna_gain_steps[] = {
346 0, 9, 13, 40, 38, 13, 31, 22, 26, 31, 26, 14, 19, 5, 35, 13
347};
348
349static const int r820t_mixer_gain_steps[] = {
350 0, 5, 10, 10, 19, 9, 10, 25, 17, 10, 8, 16, 13, 6, 3, -8
351};
352
353
354
355
356static void shadow_store(struct r820t_priv *priv, u8 reg, const u8 *val,
357 int len)
358{
359 int r = reg - REG_SHADOW_START;
360
361 if (r < 0) {
362 len += r;
363 r = 0;
364 }
365 if (len <= 0)
366 return;
367 if (len > NUM_REGS)
368 len = NUM_REGS;
369
370 tuner_dbg("%s: prev reg=%02x len=%d: %*ph\n",
371 __func__, r + REG_SHADOW_START, len, len, val);
372
373 memcpy(&priv->regs[r], val, len);
374}
375
376static int r820t_write(struct r820t_priv *priv, u8 reg, const u8 *val,
377 int len)
378{
379 int rc, size, pos = 0;
380
381
382 shadow_store(priv, reg, val, len);
383
384 do {
385 if (len > priv->cfg->max_i2c_msg_len - 1)
386 size = priv->cfg->max_i2c_msg_len - 1;
387 else
388 size = len;
389
390
391 priv->buf[0] = reg;
392 memcpy(&priv->buf[1], &val[pos], size);
393
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]);
398 if (rc < 0)
399 return rc;
400 return -EREMOTEIO;
401 }
402 tuner_dbg("%s: i2c wr reg=%02x len=%d: %*ph\n",
403 __func__, reg, size, size, &priv->buf[1]);
404
405 reg += size;
406 len -= size;
407 pos += size;
408 } while (len > 0);
409
410 return 0;
411}
412
413static int r820t_write_reg(struct r820t_priv *priv, u8 reg, u8 val)
414{
415 return r820t_write(priv, reg, &val, 1);
416}
417
418static int r820t_read_cache_reg(struct r820t_priv *priv, int reg)
419{
420 reg -= REG_SHADOW_START;
421
422 if (reg >= 0 && reg < NUM_REGS)
423 return priv->regs[reg];
424 else
425 return -EINVAL;
426}
427
428static int r820t_write_reg_mask(struct r820t_priv *priv, u8 reg, u8 val,
429 u8 bit_mask)
430{
431 int rc = r820t_read_cache_reg(priv, reg);
432
433 if (rc < 0)
434 return rc;
435
436 val = (rc & ~bit_mask) | (val & bit_mask);
437
438 return r820t_write(priv, reg, &val, 1);
439}
440
441static int r820t_read(struct r820t_priv *priv, u8 reg, u8 *val, int len)
442{
443 int rc, i;
444 u8 *p = &priv->buf[1];
445
446 priv->buf[0] = reg;
447
448 rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, priv->buf, 1, p, len);
449 if (rc != len) {
450 tuner_info("%s: i2c rd failed=%d reg=%02x len=%d: %*ph\n",
451 __func__, rc, reg, len, len, p);
452 if (rc < 0)
453 return rc;
454 return -EREMOTEIO;
455 }
456
457
458 for (i = 0; i < len; i++)
459 val[i] = bitrev8(p[i]);
460
461 tuner_dbg("%s: i2c rd reg=%02x len=%d: %*ph\n",
462 __func__, reg, len, len, val);
463
464 return 0;
465}
466
467
468
469
470
471static int r820t_set_mux(struct r820t_priv *priv, u32 freq)
472{
473 const struct r820t_freq_range *range;
474 int i, rc;
475 u8 val, reg08, reg09;
476
477
478 freq = freq / 1000000;
479 for (i = 0; i < ARRAY_SIZE(freq_ranges) - 1; i++) {
480 if (freq < freq_ranges[i + 1].freq)
481 break;
482 }
483 range = &freq_ranges[i];
484
485 tuner_dbg("set r820t range#%d for frequency %d MHz\n", i, freq);
486
487
488 rc = r820t_write_reg_mask(priv, 0x17, range->open_d, 0x08);
489 if (rc < 0)
490 return rc;
491
492
493 rc = r820t_write_reg_mask(priv, 0x1a, range->rf_mux_ploy, 0xc3);
494 if (rc < 0)
495 return rc;
496
497
498 rc = r820t_write_reg(priv, 0x1b, range->tf_c);
499 if (rc < 0)
500 return rc;
501
502
503 switch (priv->xtal_cap_sel) {
504 case XTAL_LOW_CAP_30P:
505 case XTAL_LOW_CAP_20P:
506 val = range->xtal_cap20p | 0x08;
507 break;
508 case XTAL_LOW_CAP_10P:
509 val = range->xtal_cap10p | 0x08;
510 break;
511 case XTAL_HIGH_CAP_0P:
512 val = range->xtal_cap0p | 0x00;
513 break;
514 default:
515 case XTAL_LOW_CAP_0P:
516 val = range->xtal_cap0p | 0x08;
517 break;
518 }
519 rc = r820t_write_reg_mask(priv, 0x10, val, 0x0b);
520 if (rc < 0)
521 return rc;
522
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;
526 } else {
527 reg08 = 0;
528 reg09 = 0;
529 }
530 rc = r820t_write_reg_mask(priv, 0x08, reg08, 0x3f);
531 if (rc < 0)
532 return rc;
533
534 rc = r820t_write_reg_mask(priv, 0x09, reg09, 0x3f);
535
536 return rc;
537}
538
539static int r820t_set_pll(struct r820t_priv *priv, enum v4l2_tuner_type type,
540 u32 freq)
541{
542 u32 vco_freq;
543 int rc, i;
544 unsigned sleep_time = 10000;
545 u32 vco_fra;
546 u32 vco_min = 1770000;
547 u32 vco_max = vco_min * 2;
548 u32 pll_ref;
549 u16 n_sdm = 2;
550 u16 sdm = 0;
551 u8 mix_div = 2;
552 u8 div_buf = 0;
553 u8 div_num = 0;
554 u8 refdiv2 = 0;
555 u8 ni, si, nint, vco_fine_tune, val;
556 u8 data[5];
557
558
559 freq = freq / 1000;
560 pll_ref = priv->cfg->xtal / 1000;
561
562#if 0
563
564 if ((priv->cfg->rafael_chip == CHIP_R620D) ||
565 (priv->cfg->rafael_chip == CHIP_R828D) ||
566 (priv->cfg->rafael_chip == CHIP_R828)) {
567
568 if (type != V4L2_TUNER_DIGITAL_TV) {
569 pll_ref /= 2;
570 refdiv2 = 0x10;
571 sleep_time = 20000;
572 }
573 } else {
574 if (priv->cfg->xtal > 24000000) {
575 pll_ref /= 2;
576 refdiv2 = 0x10;
577 }
578 }
579#endif
580
581 rc = r820t_write_reg_mask(priv, 0x10, refdiv2, 0x10);
582 if (rc < 0)
583 return rc;
584
585
586 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
587 if (rc < 0)
588 return rc;
589
590
591 rc = r820t_write_reg_mask(priv, 0x12, 0x80, 0xe0);
592 if (rc < 0)
593 return rc;
594
595
596 while (mix_div <= 64) {
597 if (((freq * mix_div) >= vco_min) &&
598 ((freq * mix_div) < vco_max)) {
599 div_buf = mix_div;
600 while (div_buf > 2) {
601 div_buf = div_buf >> 1;
602 div_num++;
603 }
604 break;
605 }
606 mix_div = mix_div << 1;
607 }
608
609 rc = r820t_read(priv, 0x00, data, sizeof(data));
610 if (rc < 0)
611 return rc;
612
613 vco_fine_tune = (data[4] & 0x30) >> 4;
614
615 if (vco_fine_tune > VCO_POWER_REF)
616 div_num = div_num - 1;
617 else if (vco_fine_tune < VCO_POWER_REF)
618 div_num = div_num + 1;
619
620 rc = r820t_write_reg_mask(priv, 0x10, div_num << 5, 0xe0);
621 if (rc < 0)
622 return rc;
623
624 vco_freq = freq * mix_div;
625 nint = vco_freq / (2 * pll_ref);
626 vco_fra = vco_freq - 2 * pll_ref * nint;
627
628
629 if (vco_fra < pll_ref / 64) {
630 vco_fra = 0;
631 } else if (vco_fra > pll_ref * 127 / 64) {
632 vco_fra = 0;
633 nint++;
634 } else if ((vco_fra > pll_ref * 127 / 128) && (vco_fra < pll_ref)) {
635 vco_fra = pll_ref * 127 / 128;
636 } else if ((vco_fra > pll_ref) && (vco_fra < pll_ref * 129 / 128)) {
637 vco_fra = pll_ref * 129 / 128;
638 }
639
640 if (nint > 63) {
641 tuner_info("No valid PLL values for %u kHz!\n", freq);
642 return -EINVAL;
643 }
644
645 ni = (nint - 13) / 4;
646 si = nint - 4 * ni - 13;
647
648 rc = r820t_write_reg(priv, 0x14, ni + (si << 6));
649 if (rc < 0)
650 return rc;
651
652
653 if (!vco_fra)
654 val = 0x08;
655 else
656 val = 0x00;
657
658 rc = r820t_write_reg_mask(priv, 0x12, val, 0x08);
659 if (rc < 0)
660 return rc;
661
662
663 while (vco_fra > 1) {
664 if (vco_fra > (2 * pll_ref / n_sdm)) {
665 sdm = sdm + 32768 / (n_sdm / 2);
666 vco_fra = vco_fra - 2 * pll_ref / n_sdm;
667 if (n_sdm >= 0x8000)
668 break;
669 }
670 n_sdm = n_sdm << 1;
671 }
672
673 tuner_dbg("freq %d kHz, pll ref %d%s, sdm=0x%04x\n",
674 freq, pll_ref, refdiv2 ? " / 2" : "", sdm);
675
676 rc = r820t_write_reg(priv, 0x16, sdm >> 8);
677 if (rc < 0)
678 return rc;
679 rc = r820t_write_reg(priv, 0x15, sdm & 0xff);
680 if (rc < 0)
681 return rc;
682
683 for (i = 0; i < 2; i++) {
684 usleep_range(sleep_time, sleep_time + 1000);
685
686
687 rc = r820t_read(priv, 0x00, data, 3);
688 if (rc < 0)
689 return rc;
690 if (data[2] & 0x40)
691 break;
692
693 if (!i) {
694
695 rc = r820t_write_reg_mask(priv, 0x12, 0x60, 0xe0);
696 if (rc < 0)
697 return rc;
698 }
699 }
700
701 if (!(data[2] & 0x40)) {
702 priv->has_lock = false;
703 return 0;
704 }
705
706 priv->has_lock = true;
707 tuner_dbg("tuner has lock at frequency %d kHz\n", freq);
708
709
710 rc = r820t_write_reg_mask(priv, 0x1a, 0x08, 0x08);
711
712 return rc;
713}
714
715static int r820t_sysfreq_sel(struct r820t_priv *priv, u32 freq,
716 enum v4l2_tuner_type type,
717 v4l2_std_id std,
718 u32 delsys)
719{
720 int rc;
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;
723
724 tuner_dbg("adjusting tuner parameters for the standard\n");
725
726 switch (delsys) {
727 case SYS_DVBT:
728 if ((freq == 506000000) || (freq == 666000000) ||
729 (freq == 818000000)) {
730 mixer_top = 0x14;
731 lna_top = 0xe5;
732 cp_cur = 0x28;
733 div_buf_cur = 0x20;
734 } else {
735 mixer_top = 0x24;
736 lna_top = 0xe5;
737 cp_cur = 0x38;
738 div_buf_cur = 0x30;
739 }
740 lna_vth_l = 0x53;
741 mixer_vth_l = 0x75;
742 air_cable1_in = 0x00;
743 cable2_in = 0x00;
744 pre_dect = 0x40;
745 lna_discharge = 14;
746 filter_cur = 0x40;
747 break;
748 case SYS_DVBT2:
749 mixer_top = 0x24;
750 lna_top = 0xe5;
751 lna_vth_l = 0x53;
752 mixer_vth_l = 0x75;
753 air_cable1_in = 0x00;
754 cable2_in = 0x00;
755 pre_dect = 0x40;
756 lna_discharge = 14;
757 cp_cur = 0x38;
758 div_buf_cur = 0x30;
759 filter_cur = 0x40;
760 break;
761 case SYS_ISDBT:
762 mixer_top = 0x24;
763 lna_top = 0xe5;
764 lna_vth_l = 0x75;
765 mixer_vth_l = 0x75;
766 air_cable1_in = 0x00;
767 cable2_in = 0x00;
768 pre_dect = 0x40;
769 lna_discharge = 14;
770 cp_cur = 0x38;
771 div_buf_cur = 0x30;
772 filter_cur = 0x40;
773 break;
774 default:
775 mixer_top = 0x24;
776 lna_top = 0xe5;
777 lna_vth_l = 0x53;
778 mixer_vth_l = 0x75;
779 air_cable1_in = 0x00;
780 cable2_in = 0x00;
781 pre_dect = 0x40;
782 lna_discharge = 14;
783 cp_cur = 0x38;
784 div_buf_cur = 0x30;
785 filter_cur = 0x40;
786 break;
787 }
788
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))) {
793 if (freq > DIP_FREQ)
794 air_cable1_in = 0x00;
795 else
796 air_cable1_in = 0x60;
797 cable2_in = 0x00;
798 }
799
800
801 if (priv->cfg->use_predetect) {
802 rc = r820t_write_reg_mask(priv, 0x06, pre_dect, 0x40);
803 if (rc < 0)
804 return rc;
805 }
806
807 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0xc7);
808 if (rc < 0)
809 return rc;
810 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0xf8);
811 if (rc < 0)
812 return rc;
813 rc = r820t_write_reg(priv, 0x0d, lna_vth_l);
814 if (rc < 0)
815 return rc;
816 rc = r820t_write_reg(priv, 0x0e, mixer_vth_l);
817 if (rc < 0)
818 return rc;
819
820
821 rc = r820t_write_reg_mask(priv, 0x05, air_cable1_in, 0x60);
822 if (rc < 0)
823 return rc;
824 rc = r820t_write_reg_mask(priv, 0x06, cable2_in, 0x08);
825 if (rc < 0)
826 return rc;
827
828 rc = r820t_write_reg_mask(priv, 0x11, cp_cur, 0x38);
829 if (rc < 0)
830 return rc;
831 rc = r820t_write_reg_mask(priv, 0x17, div_buf_cur, 0x30);
832 if (rc < 0)
833 return rc;
834 rc = r820t_write_reg_mask(priv, 0x0a, filter_cur, 0x60);
835 if (rc < 0)
836 return rc;
837
838
839
840
841
842
843
844
845
846
847 tuner_dbg("adjusting LNA parameters\n");
848 if (type != V4L2_TUNER_ANALOG_TV) {
849
850 rc = r820t_write_reg_mask(priv, 0x1d, 0, 0x38);
851 if (rc < 0)
852 return rc;
853
854
855 rc = r820t_write_reg_mask(priv, 0x1c, 0, 0x04);
856 if (rc < 0)
857 return rc;
858
859
860 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
861 if (rc < 0)
862 return rc;
863
864
865 rc = r820t_write_reg_mask(priv, 0x1a, 0x30, 0x30);
866 if (rc < 0)
867 return rc;
868
869 msleep(250);
870
871
872 rc = r820t_write_reg_mask(priv, 0x1d, 0x18, 0x38);
873 if (rc < 0)
874 return rc;
875
876
877
878
879
880
881 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
882 if (rc < 0)
883 return rc;
884
885
886 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
887 if (rc < 0)
888 return rc;
889
890
891 rc = r820t_write_reg_mask(priv, 0x1a, 0x20, 0x30);
892 if (rc < 0)
893 return rc;
894 } else {
895
896 rc = r820t_write_reg_mask(priv, 0x06, 0, 0x40);
897 if (rc < 0)
898 return rc;
899
900
901 rc = r820t_write_reg_mask(priv, 0x1d, lna_top, 0x38);
902 if (rc < 0)
903 return rc;
904
905
906
907
908
909
910 rc = r820t_write_reg_mask(priv, 0x1c, mixer_top, 0x04);
911 if (rc < 0)
912 return rc;
913
914
915 rc = r820t_write_reg_mask(priv, 0x1e, lna_discharge, 0x1f);
916 if (rc < 0)
917 return rc;
918
919
920 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x30);
921 if (rc < 0)
922 return rc;
923
924 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x04);
925 if (rc < 0)
926 return rc;
927 }
928 return 0;
929}
930
931static int r820t_set_tv_standard(struct r820t_priv *priv,
932 unsigned bw,
933 enum v4l2_tuner_type type,
934 v4l2_std_id std, u32 delsys)
935
936{
937 int rc, i;
938 u32 if_khz, filt_cal_lo;
939 u8 data[5], val;
940 u8 filt_gain, img_r, filt_q, hp_cor, ext_enable, loop_through;
941 u8 lt_att, flt_ext_widest, polyfil_cur;
942 bool need_calibration;
943
944 tuner_dbg("selecting the delivery system\n");
945
946 if (delsys == SYS_ISDBT) {
947 if_khz = 4063;
948 filt_cal_lo = 59000;
949 filt_gain = 0x10;
950 img_r = 0x00;
951 filt_q = 0x10;
952 hp_cor = 0x6a;
953 ext_enable = 0x40;
954 loop_through = 0x00;
955 lt_att = 0x00;
956 flt_ext_widest = 0x00;
957 polyfil_cur = 0x60;
958 } else {
959 if (bw <= 6) {
960 if_khz = 3570;
961 filt_cal_lo = 56000;
962 filt_gain = 0x10;
963 img_r = 0x00;
964 filt_q = 0x10;
965 hp_cor = 0x6b;
966 ext_enable = 0x60;
967 loop_through = 0x00;
968 lt_att = 0x00;
969 flt_ext_widest = 0x00;
970 polyfil_cur = 0x60;
971 } else if (bw == 7) {
972#if 0
973
974
975
976
977
978
979
980 if_khz = 4070;
981 filt_cal_lo = 60000;
982 filt_gain = 0x10;
983 img_r = 0x00;
984 filt_q = 0x10;
985 hp_cor = 0x2b;
986 ext_enable = 0x60;
987 loop_through = 0x00;
988 lt_att = 0x00;
989 flt_ext_widest = 0x00;
990 polyfil_cur = 0x60;
991#endif
992
993 if_khz = 4570;
994 filt_cal_lo = 63000;
995 filt_gain = 0x10;
996 img_r = 0x00;
997 filt_q = 0x10;
998 hp_cor = 0x2a;
999 ext_enable = 0x60;
1000 loop_through = 0x00;
1001 lt_att = 0x00;
1002 flt_ext_widest = 0x00;
1003 polyfil_cur = 0x60;
1004 } else {
1005 if_khz = 4570;
1006 filt_cal_lo = 68500;
1007 filt_gain = 0x10;
1008 img_r = 0x00;
1009 filt_q = 0x10;
1010 hp_cor = 0x0b;
1011 ext_enable = 0x60;
1012 loop_through = 0x00;
1013 lt_att = 0x00;
1014 flt_ext_widest = 0x00;
1015 polyfil_cur = 0x60;
1016 }
1017 }
1018
1019
1020 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1021
1022
1023 if (priv->imr_done)
1024 val = 1 | priv->xtal_cap_sel << 1;
1025 else
1026 val = 0;
1027 rc = r820t_write_reg_mask(priv, 0x0c, val, 0x0f);
1028 if (rc < 0)
1029 return rc;
1030
1031
1032 rc = r820t_write_reg_mask(priv, 0x13, VER_NUM, 0x3f);
1033 if (rc < 0)
1034 return rc;
1035
1036
1037 if (type != V4L2_TUNER_ANALOG_TV) {
1038 rc = r820t_write_reg_mask(priv, 0x1d, 0x00, 0x38);
1039 if (rc < 0)
1040 return rc;
1041 usleep_range(1000, 2000);
1042 }
1043 priv->int_freq = if_khz * 1000;
1044
1045
1046 if (type != priv->type)
1047 need_calibration = true;
1048 else if ((type == V4L2_TUNER_ANALOG_TV) && (std != priv->std))
1049 need_calibration = true;
1050 else if ((type == V4L2_TUNER_DIGITAL_TV) &&
1051 ((delsys != priv->delsys) || bw != priv->bw))
1052 need_calibration = true;
1053 else
1054 need_calibration = false;
1055
1056 if (need_calibration) {
1057 tuner_dbg("calibrating the tuner\n");
1058 for (i = 0; i < 2; i++) {
1059
1060 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0x60);
1061 if (rc < 0)
1062 return rc;
1063
1064
1065 rc = r820t_write_reg_mask(priv, 0x0f, 0x04, 0x04);
1066 if (rc < 0)
1067 return rc;
1068
1069
1070 rc = r820t_write_reg_mask(priv, 0x10, 0x00, 0x03);
1071 if (rc < 0)
1072 return rc;
1073
1074 rc = r820t_set_pll(priv, type, filt_cal_lo * 1000);
1075 if (rc < 0 || !priv->has_lock)
1076 return rc;
1077
1078
1079 rc = r820t_write_reg_mask(priv, 0x0b, 0x10, 0x10);
1080 if (rc < 0)
1081 return rc;
1082
1083 usleep_range(1000, 2000);
1084
1085
1086 rc = r820t_write_reg_mask(priv, 0x0b, 0x00, 0x10);
1087 if (rc < 0)
1088 return rc;
1089
1090
1091 rc = r820t_write_reg_mask(priv, 0x0f, 0x00, 0x04);
1092 if (rc < 0)
1093 return rc;
1094
1095
1096 rc = r820t_read(priv, 0x00, data, sizeof(data));
1097 if (rc < 0)
1098 return rc;
1099
1100 priv->fil_cal_code = data[4] & 0x0f;
1101 if (priv->fil_cal_code && priv->fil_cal_code != 0x0f)
1102 break;
1103 }
1104
1105 if (priv->fil_cal_code == 0x0f)
1106 priv->fil_cal_code = 0;
1107 }
1108
1109 rc = r820t_write_reg_mask(priv, 0x0a,
1110 filt_q | priv->fil_cal_code, 0x1f);
1111 if (rc < 0)
1112 return rc;
1113
1114
1115 rc = r820t_write_reg_mask(priv, 0x0b, hp_cor, 0xef);
1116 if (rc < 0)
1117 return rc;
1118
1119
1120
1121 rc = r820t_write_reg_mask(priv, 0x07, img_r, 0x80);
1122 if (rc < 0)
1123 return rc;
1124
1125
1126 rc = r820t_write_reg_mask(priv, 0x06, filt_gain, 0x30);
1127 if (rc < 0)
1128 return rc;
1129
1130
1131 rc = r820t_write_reg_mask(priv, 0x1e, ext_enable, 0x60);
1132 if (rc < 0)
1133 return rc;
1134
1135
1136 rc = r820t_write_reg_mask(priv, 0x05, loop_through, 0x80);
1137 if (rc < 0)
1138 return rc;
1139
1140
1141 rc = r820t_write_reg_mask(priv, 0x1f, lt_att, 0x80);
1142 if (rc < 0)
1143 return rc;
1144
1145
1146 rc = r820t_write_reg_mask(priv, 0x0f, flt_ext_widest, 0x80);
1147 if (rc < 0)
1148 return rc;
1149
1150
1151 rc = r820t_write_reg_mask(priv, 0x19, polyfil_cur, 0x60);
1152 if (rc < 0)
1153 return rc;
1154
1155
1156 priv->delsys = delsys;
1157 priv->type = type;
1158 priv->std = std;
1159 priv->bw = bw;
1160
1161 return 0;
1162}
1163
1164static int r820t_read_gain(struct r820t_priv *priv)
1165{
1166 u8 data[4];
1167 int rc;
1168
1169 rc = r820t_read(priv, 0x00, data, sizeof(data));
1170 if (rc < 0)
1171 return rc;
1172
1173 return ((data[3] & 0x0f) << 1) + ((data[3] & 0xf0) >> 4);
1174}
1175
1176#if 0
1177
1178static int r820t_set_gain_mode(struct r820t_priv *priv,
1179 bool set_manual_gain,
1180 int gain)
1181{
1182 int rc;
1183
1184 if (set_manual_gain) {
1185 int i, total_gain = 0;
1186 uint8_t mix_index = 0, lna_index = 0;
1187 u8 data[4];
1188
1189
1190 rc = r820t_write_reg_mask(priv, 0x05, 0x10, 0x10);
1191 if (rc < 0)
1192 return rc;
1193
1194
1195 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
1196 if (rc < 0)
1197 return rc;
1198
1199 rc = r820t_read(priv, 0x00, data, sizeof(data));
1200 if (rc < 0)
1201 return rc;
1202
1203
1204 rc = r820t_write_reg_mask(priv, 0x0c, 0x08, 0x9f);
1205 if (rc < 0)
1206 return rc;
1207
1208 for (i = 0; i < 15; i++) {
1209 if (total_gain >= gain)
1210 break;
1211
1212 total_gain += r820t_lna_gain_steps[++lna_index];
1213
1214 if (total_gain >= gain)
1215 break;
1216
1217 total_gain += r820t_mixer_gain_steps[++mix_index];
1218 }
1219
1220
1221 rc = r820t_write_reg_mask(priv, 0x05, lna_index, 0x0f);
1222 if (rc < 0)
1223 return rc;
1224
1225
1226 rc = r820t_write_reg_mask(priv, 0x07, mix_index, 0x0f);
1227 if (rc < 0)
1228 return rc;
1229 } else {
1230
1231 rc = r820t_write_reg_mask(priv, 0x05, 0, 0x10);
1232 if (rc < 0)
1233 return rc;
1234
1235
1236 rc = r820t_write_reg_mask(priv, 0x07, 0x10, 0x10);
1237 if (rc < 0)
1238 return rc;
1239
1240
1241 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
1242 if (rc < 0)
1243 return rc;
1244 }
1245
1246 return 0;
1247}
1248#endif
1249
1250static int generic_set_freq(struct dvb_frontend *fe,
1251 u32 freq ,
1252 unsigned bw,
1253 enum v4l2_tuner_type type,
1254 v4l2_std_id std, u32 delsys)
1255{
1256 struct r820t_priv *priv = fe->tuner_priv;
1257 int rc = -EINVAL;
1258 u32 lo_freq;
1259
1260 tuner_dbg("should set frequency to %d kHz, bw %d MHz\n",
1261 freq / 1000, bw);
1262
1263 rc = r820t_set_tv_standard(priv, bw, type, std, delsys);
1264 if (rc < 0)
1265 goto err;
1266
1267 if ((type == V4L2_TUNER_ANALOG_TV) && (std == V4L2_STD_SECAM_LC))
1268 lo_freq = freq - priv->int_freq;
1269 else
1270 lo_freq = freq + priv->int_freq;
1271
1272 rc = r820t_set_mux(priv, lo_freq);
1273 if (rc < 0)
1274 goto err;
1275
1276 rc = r820t_set_pll(priv, type, lo_freq);
1277 if (rc < 0 || !priv->has_lock)
1278 goto err;
1279
1280 rc = r820t_sysfreq_sel(priv, freq, type, std, delsys);
1281 if (rc < 0)
1282 goto err;
1283
1284 tuner_dbg("%s: PLL locked on frequency %d Hz, gain=%d\n",
1285 __func__, freq, r820t_read_gain(priv));
1286
1287err:
1288
1289 if (rc < 0)
1290 tuner_dbg("%s: failed=%d\n", __func__, rc);
1291 return rc;
1292}
1293
1294
1295
1296
1297
1298static int r820t_standby(struct r820t_priv *priv)
1299{
1300 int rc;
1301
1302
1303 if (!priv->init_done)
1304 return 0;
1305
1306 rc = r820t_write_reg(priv, 0x06, 0xb1);
1307 if (rc < 0)
1308 return rc;
1309 rc = r820t_write_reg(priv, 0x05, 0x03);
1310 if (rc < 0)
1311 return rc;
1312 rc = r820t_write_reg(priv, 0x07, 0x3a);
1313 if (rc < 0)
1314 return rc;
1315 rc = r820t_write_reg(priv, 0x08, 0x40);
1316 if (rc < 0)
1317 return rc;
1318 rc = r820t_write_reg(priv, 0x09, 0xc0);
1319 if (rc < 0)
1320 return rc;
1321 rc = r820t_write_reg(priv, 0x0a, 0x36);
1322 if (rc < 0)
1323 return rc;
1324 rc = r820t_write_reg(priv, 0x0c, 0x35);
1325 if (rc < 0)
1326 return rc;
1327 rc = r820t_write_reg(priv, 0x0f, 0x68);
1328 if (rc < 0)
1329 return rc;
1330 rc = r820t_write_reg(priv, 0x11, 0x03);
1331 if (rc < 0)
1332 return rc;
1333 rc = r820t_write_reg(priv, 0x17, 0xf4);
1334 if (rc < 0)
1335 return rc;
1336 rc = r820t_write_reg(priv, 0x19, 0x0c);
1337
1338
1339 priv->type = -1;
1340
1341 return rc;
1342}
1343
1344
1345
1346
1347
1348static int r820t_xtal_check(struct r820t_priv *priv)
1349{
1350 int rc, i;
1351 u8 data[3], val;
1352
1353
1354 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1355
1356
1357 rc = r820t_write_reg_mask(priv, 0x10, 0x0b, 0x0b);
1358 if (rc < 0)
1359 return rc;
1360
1361
1362 rc = r820t_write_reg_mask(priv, 0x1a, 0x00, 0x0c);
1363 if (rc < 0)
1364 return rc;
1365
1366
1367 rc = r820t_write_reg_mask(priv, 0x13, 0x7f, 0x7f);
1368 if (rc < 0)
1369 return rc;
1370
1371
1372 rc = r820t_write_reg_mask(priv, 0x13, 0x00, 0x40);
1373 if (rc < 0)
1374 return rc;
1375
1376
1377 for (i = 0; i < ARRAY_SIZE(r820t_xtal_capacitor); i++) {
1378 rc = r820t_write_reg_mask(priv, 0x10,
1379 r820t_xtal_capacitor[i][0], 0x1b);
1380 if (rc < 0)
1381 return rc;
1382
1383 usleep_range(5000, 6000);
1384
1385 rc = r820t_read(priv, 0x00, data, sizeof(data));
1386 if (rc < 0)
1387 return rc;
1388 if (!(data[2] & 0x40))
1389 continue;
1390
1391 val = data[2] & 0x3f;
1392
1393 if (priv->cfg->xtal == 16000000 && (val > 29 || val < 23))
1394 break;
1395
1396 if (val != 0x3f)
1397 break;
1398 }
1399
1400 if (i == ARRAY_SIZE(r820t_xtal_capacitor))
1401 return -EINVAL;
1402
1403 return r820t_xtal_capacitor[i][1];
1404}
1405
1406static int r820t_imr_prepare(struct r820t_priv *priv)
1407{
1408 int rc;
1409
1410
1411 memcpy(priv->regs, r820t_init_array, sizeof(r820t_init_array));
1412
1413
1414 rc = r820t_write_reg_mask(priv, 0x05, 0x20, 0x20);
1415 if (rc < 0)
1416 return rc;
1417
1418
1419 rc = r820t_write_reg_mask(priv, 0x07, 0, 0x10);
1420 if (rc < 0)
1421 return rc;
1422
1423
1424 rc = r820t_write_reg_mask(priv, 0x0a, 0x0f, 0x0f);
1425 if (rc < 0)
1426 return rc;
1427
1428
1429 rc = r820t_write_reg_mask(priv, 0x0b, 0x60, 0x6f);
1430 if (rc < 0)
1431 return rc;
1432
1433
1434 rc = r820t_write_reg_mask(priv, 0x0c, 0x0b, 0x9f);
1435 if (rc < 0)
1436 return rc;
1437
1438
1439 rc = r820t_write_reg_mask(priv, 0x0f, 0, 0x08);
1440 if (rc < 0)
1441 return rc;
1442
1443
1444 rc = r820t_write_reg_mask(priv, 0x18, 0x10, 0x10);
1445 if (rc < 0)
1446 return rc;
1447
1448
1449 rc = r820t_write_reg_mask(priv, 0x1c, 0x02, 0x02);
1450 if (rc < 0)
1451 return rc;
1452
1453
1454 rc = r820t_write_reg_mask(priv, 0x1e, 0x80, 0x80);
1455 if (rc < 0)
1456 return rc;
1457
1458
1459 rc = r820t_write_reg_mask(priv, 0x06, 0x20, 0x20);
1460
1461 return rc;
1462}
1463
1464static int r820t_multi_read(struct r820t_priv *priv)
1465{
1466 int rc, i;
1467 u8 data[2], min = 0, max = 255, sum = 0;
1468
1469 usleep_range(5000, 6000);
1470
1471 for (i = 0; i < 6; i++) {
1472 rc = r820t_read(priv, 0x00, data, sizeof(data));
1473 if (rc < 0)
1474 return rc;
1475
1476 sum += data[1];
1477
1478 if (data[1] < min)
1479 min = data[1];
1480
1481 if (data[1] > max)
1482 max = data[1];
1483 }
1484 rc = sum - max - min;
1485
1486 return rc;
1487}
1488
1489static int r820t_imr_cross(struct r820t_priv *priv,
1490 struct r820t_sect_type iq_point[3],
1491 u8 *x_direct)
1492{
1493 struct r820t_sect_type cross[5];
1494 struct r820t_sect_type tmp;
1495 int i, rc;
1496 u8 reg08, reg09;
1497
1498 reg08 = r820t_read_cache_reg(priv, 8) & 0xc0;
1499 reg09 = r820t_read_cache_reg(priv, 9) & 0xc0;
1500
1501 tmp.gain_x = 0;
1502 tmp.phase_y = 0;
1503 tmp.value = 255;
1504
1505 for (i = 0; i < 5; i++) {
1506 switch (i) {
1507 case 0:
1508 cross[i].gain_x = reg08;
1509 cross[i].phase_y = reg09;
1510 break;
1511 case 1:
1512 cross[i].gain_x = reg08;
1513 cross[i].phase_y = reg09 + 1;
1514 break;
1515 case 2:
1516 cross[i].gain_x = reg08;
1517 cross[i].phase_y = (reg09 | 0x20) + 1;
1518 break;
1519 case 3:
1520 cross[i].gain_x = reg08 + 1;
1521 cross[i].phase_y = reg09;
1522 break;
1523 default:
1524 cross[i].gain_x = (reg08 | 0x20) + 1;
1525 cross[i].phase_y = reg09;
1526 }
1527
1528 rc = r820t_write_reg(priv, 0x08, cross[i].gain_x);
1529 if (rc < 0)
1530 return rc;
1531
1532 rc = r820t_write_reg(priv, 0x09, cross[i].phase_y);
1533 if (rc < 0)
1534 return rc;
1535
1536 rc = r820t_multi_read(priv);
1537 if (rc < 0)
1538 return rc;
1539
1540 cross[i].value = rc;
1541
1542 if (cross[i].value < tmp.value)
1543 memcpy(&tmp, &cross[i], sizeof(tmp));
1544 }
1545
1546 if ((tmp.phase_y & 0x1f) == 1) {
1547 *x_direct = 0;
1548
1549 iq_point[0] = cross[0];
1550 iq_point[1] = cross[1];
1551 iq_point[2] = cross[2];
1552 } else {
1553 *x_direct = 1;
1554
1555 iq_point[0] = cross[0];
1556 iq_point[1] = cross[3];
1557 iq_point[2] = cross[4];
1558 }
1559 return 0;
1560}
1561
1562static void r820t_compre_cor(struct r820t_sect_type iq[3])
1563{
1564 int i;
1565
1566 for (i = 3; i > 0; i--) {
1567 if (iq[0].value > iq[i - 1].value) {
1568 gmb();
1569 swap(iq[0], iq[i - 1]);
1570 }
1571 }
1572}
1573
1574static int r820t_compre_step(struct r820t_priv *priv,
1575 struct r820t_sect_type iq[3], u8 reg)
1576{
1577 int rc;
1578 struct r820t_sect_type tmp;
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588 tmp.phase_y = iq[0].phase_y;
1589 tmp.gain_x = iq[0].gain_x;
1590
1591 while (((tmp.gain_x & 0x1f) < IMR_TRIAL) &&
1592 ((tmp.phase_y & 0x1f) < IMR_TRIAL)) {
1593 if (reg == 0x08)
1594 tmp.gain_x++;
1595 else
1596 tmp.phase_y++;
1597
1598 rc = r820t_write_reg(priv, 0x08, tmp.gain_x);
1599 if (rc < 0)
1600 return rc;
1601
1602 rc = r820t_write_reg(priv, 0x09, tmp.phase_y);
1603 if (rc < 0)
1604 return rc;
1605
1606 rc = r820t_multi_read(priv);
1607 if (rc < 0)
1608 return rc;
1609 tmp.value = rc;
1610
1611 if (tmp.value <= iq[0].value) {
1612 iq[0].gain_x = tmp.gain_x;
1613 iq[0].phase_y = tmp.phase_y;
1614 iq[0].value = tmp.value;
1615 } else {
1616 return 0;
1617 }
1618
1619 }
1620
1621 return 0;
1622}
1623
1624static int r820t_iq_tree(struct r820t_priv *priv,
1625 struct r820t_sect_type iq[3],
1626 u8 fix_val, u8 var_val, u8 fix_reg)
1627{
1628 int rc, i;
1629 u8 tmp, var_reg;
1630
1631
1632
1633
1634
1635
1636
1637 if (fix_reg == 0x08)
1638 var_reg = 0x09;
1639 else
1640 var_reg = 0x08;
1641
1642 for (i = 0; i < 3; i++) {
1643 rc = r820t_write_reg(priv, fix_reg, fix_val);
1644 if (rc < 0)
1645 return rc;
1646
1647 rc = r820t_write_reg(priv, var_reg, var_val);
1648 if (rc < 0)
1649 return rc;
1650
1651 rc = r820t_multi_read(priv);
1652 if (rc < 0)
1653 return rc;
1654 iq[i].value = rc;
1655
1656 if (fix_reg == 0x08) {
1657 iq[i].gain_x = fix_val;
1658 iq[i].phase_y = var_val;
1659 } else {
1660 iq[i].phase_y = fix_val;
1661 iq[i].gain_x = var_val;
1662 }
1663
1664 if (i == 0) {
1665 var_val++;
1666 } else if (i == 1) {
1667
1668 if ((var_val & 0x1f) < 0x02) {
1669 tmp = 2 - (var_val & 0x1f);
1670
1671
1672 if (var_val & 0x20) {
1673 var_val &= 0xc0;
1674 var_val |= tmp;
1675 } else {
1676 var_val |= 0x20 | tmp;
1677 }
1678 } else {
1679 var_val -= 2;
1680 }
1681 }
1682 }
1683
1684 return 0;
1685}
1686
1687static int r820t_section(struct r820t_priv *priv,
1688 struct r820t_sect_type *iq_point)
1689{
1690 int rc;
1691 struct r820t_sect_type compare_iq[3], compare_bet[3];
1692
1693
1694 if (!(iq_point->gain_x & 0x1f))
1695 compare_iq[0].gain_x = ((iq_point->gain_x) & 0xdf) + 1;
1696 else
1697 compare_iq[0].gain_x = iq_point->gain_x - 1;
1698 compare_iq[0].phase_y = iq_point->phase_y;
1699
1700
1701 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1702 compare_iq[0].phase_y, 0x08);
1703 if (rc < 0)
1704 return rc;
1705
1706 r820t_compre_cor(compare_iq);
1707
1708 compare_bet[0] = compare_iq[0];
1709
1710
1711 compare_iq[0].gain_x = iq_point->gain_x;
1712 compare_iq[0].phase_y = iq_point->phase_y;
1713
1714 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1715 compare_iq[0].phase_y, 0x08);
1716 if (rc < 0)
1717 return rc;
1718
1719 r820t_compre_cor(compare_iq);
1720
1721 compare_bet[1] = compare_iq[0];
1722
1723
1724 if ((iq_point->gain_x & 0x1f) == 0x00)
1725 compare_iq[0].gain_x = ((iq_point->gain_x) | 0x20) + 1;
1726 else
1727 compare_iq[0].gain_x = iq_point->gain_x + 1;
1728 compare_iq[0].phase_y = iq_point->phase_y;
1729
1730 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1731 compare_iq[0].phase_y, 0x08);
1732 if (rc < 0)
1733 return rc;
1734
1735 r820t_compre_cor(compare_iq);
1736
1737 compare_bet[2] = compare_iq[0];
1738
1739 r820t_compre_cor(compare_bet);
1740
1741 *iq_point = compare_bet[0];
1742
1743 return 0;
1744}
1745
1746static int r820t_vga_adjust(struct r820t_priv *priv)
1747{
1748 int rc;
1749 u8 vga_count;
1750
1751
1752 for (vga_count = 12; vga_count < 16; vga_count++) {
1753 rc = r820t_write_reg_mask(priv, 0x0c, vga_count, 0x0f);
1754 if (rc < 0)
1755 return rc;
1756
1757 usleep_range(10000, 11000);
1758
1759 rc = r820t_multi_read(priv);
1760 if (rc < 0)
1761 return rc;
1762
1763 if (rc > 40 * 4)
1764 break;
1765 }
1766
1767 return 0;
1768}
1769
1770static int r820t_iq(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
1771{
1772 struct r820t_sect_type compare_iq[3];
1773 int rc;
1774 u8 x_direction = 0;
1775 u8 dir_reg, other_reg;
1776
1777 r820t_vga_adjust(priv);
1778
1779 rc = r820t_imr_cross(priv, compare_iq, &x_direction);
1780 if (rc < 0)
1781 return rc;
1782
1783 if (x_direction == 1) {
1784 dir_reg = 0x08;
1785 other_reg = 0x09;
1786 } else {
1787 dir_reg = 0x09;
1788 other_reg = 0x08;
1789 }
1790
1791
1792 r820t_compre_cor(compare_iq);
1793
1794
1795 rc = r820t_compre_step(priv, compare_iq, dir_reg);
1796 if (rc < 0)
1797 return rc;
1798
1799
1800 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1801 compare_iq[0].phase_y, dir_reg);
1802 if (rc < 0)
1803 return rc;
1804
1805
1806 r820t_compre_cor(compare_iq);
1807
1808
1809 rc = r820t_compre_step(priv, compare_iq, other_reg);
1810 if (rc < 0)
1811 return rc;
1812
1813
1814 rc = r820t_iq_tree(priv, compare_iq, compare_iq[0].gain_x,
1815 compare_iq[0].phase_y, other_reg);
1816 if (rc < 0)
1817 return rc;
1818
1819 r820t_compre_cor(compare_iq);
1820
1821
1822 rc = r820t_section(priv, compare_iq);
1823
1824 *iq_pont = compare_iq[0];
1825
1826
1827 rc = r820t_write_reg_mask(priv, 0x08, 0, 0x3f);
1828 if (rc < 0)
1829 return rc;
1830
1831 rc = r820t_write_reg_mask(priv, 0x09, 0, 0x3f);
1832
1833 return rc;
1834}
1835
1836static int r820t_f_imr(struct r820t_priv *priv, struct r820t_sect_type *iq_pont)
1837{
1838 int rc;
1839
1840 r820t_vga_adjust(priv);
1841
1842
1843
1844
1845
1846 rc = r820t_section(priv, iq_pont);
1847 if (rc < 0)
1848 return rc;
1849
1850 return 0;
1851}
1852
1853static int r820t_imr(struct r820t_priv *priv, unsigned imr_mem, bool im_flag)
1854{
1855 struct r820t_sect_type imr_point;
1856 int rc;
1857 u32 ring_vco, ring_freq, ring_ref;
1858 u8 n_ring, n;
1859 int reg18, reg19, reg1f;
1860
1861 if (priv->cfg->xtal > 24000000)
1862 ring_ref = priv->cfg->xtal / 2;
1863 else
1864 ring_ref = priv->cfg->xtal;
1865
1866 n_ring = 15;
1867 for (n = 0; n < 16; n++) {
1868 if ((16 + n) * 8 * ring_ref >= 3100000) {
1869 n_ring = n;
1870 break;
1871 }
1872 }
1873
1874 reg18 = r820t_read_cache_reg(priv, 0x18);
1875 reg19 = r820t_read_cache_reg(priv, 0x19);
1876 reg1f = r820t_read_cache_reg(priv, 0x1f);
1877
1878 reg18 &= 0xf0;
1879 reg18 |= n_ring;
1880
1881 ring_vco = (16 + n_ring) * 8 * ring_ref;
1882
1883 reg18 &= 0xdf;
1884 reg19 &= 0xfc;
1885 reg1f &= 0xfc;
1886
1887 switch (imr_mem) {
1888 case 0:
1889 ring_freq = ring_vco / 48;
1890 reg18 |= 0x20;
1891 reg19 |= 0x03;
1892 reg1f |= 0x02;
1893 break;
1894 case 1:
1895 ring_freq = ring_vco / 16;
1896 reg18 |= 0x00;
1897 reg19 |= 0x02;
1898 reg1f |= 0x00;
1899 break;
1900 case 2:
1901 ring_freq = ring_vco / 8;
1902 reg18 |= 0x00;
1903 reg19 |= 0x01;
1904 reg1f |= 0x03;
1905 break;
1906 case 3:
1907 ring_freq = ring_vco / 6;
1908 reg18 |= 0x20;
1909 reg19 |= 0x00;
1910 reg1f |= 0x03;
1911 break;
1912 case 4:
1913 ring_freq = ring_vco / 4;
1914 reg18 |= 0x00;
1915 reg19 |= 0x00;
1916 reg1f |= 0x01;
1917 break;
1918 default:
1919 ring_freq = ring_vco / 4;
1920 reg18 |= 0x00;
1921 reg19 |= 0x00;
1922 reg1f |= 0x01;
1923 break;
1924 }
1925
1926
1927
1928
1929
1930 rc = r820t_write_reg(priv, 0x18, reg18);
1931 if (rc < 0)
1932 return rc;
1933
1934
1935 rc = r820t_write_reg(priv, 0x19, reg19);
1936 if (rc < 0)
1937 return rc;
1938
1939
1940 rc = r820t_write_reg(priv, 0x1f, reg1f);
1941 if (rc < 0)
1942 return rc;
1943
1944
1945 rc = r820t_set_mux(priv, (ring_freq - 5300) * 1000);
1946 if (rc < 0)
1947 return rc;
1948
1949 rc = r820t_set_pll(priv, V4L2_TUNER_DIGITAL_TV,
1950 (ring_freq - 5300) * 1000);
1951 if (!priv->has_lock)
1952 rc = -EINVAL;
1953 if (rc < 0)
1954 return rc;
1955
1956 if (im_flag) {
1957 rc = r820t_iq(priv, &imr_point);
1958 } else {
1959 imr_point.gain_x = priv->imr_data[3].gain_x;
1960 imr_point.phase_y = priv->imr_data[3].phase_y;
1961 imr_point.value = priv->imr_data[3].value;
1962
1963 rc = r820t_f_imr(priv, &imr_point);
1964 }
1965 if (rc < 0)
1966 return rc;
1967
1968
1969 switch (imr_mem) {
1970 case 0:
1971 priv->imr_data[0].gain_x = imr_point.gain_x;
1972 priv->imr_data[0].phase_y = imr_point.phase_y;
1973 priv->imr_data[0].value = imr_point.value;
1974 break;
1975 case 1:
1976 priv->imr_data[1].gain_x = imr_point.gain_x;
1977 priv->imr_data[1].phase_y = imr_point.phase_y;
1978 priv->imr_data[1].value = imr_point.value;
1979 break;
1980 case 2:
1981 priv->imr_data[2].gain_x = imr_point.gain_x;
1982 priv->imr_data[2].phase_y = imr_point.phase_y;
1983 priv->imr_data[2].value = imr_point.value;
1984 break;
1985 case 3:
1986 priv->imr_data[3].gain_x = imr_point.gain_x;
1987 priv->imr_data[3].phase_y = imr_point.phase_y;
1988 priv->imr_data[3].value = imr_point.value;
1989 break;
1990 case 4:
1991 priv->imr_data[4].gain_x = imr_point.gain_x;
1992 priv->imr_data[4].phase_y = imr_point.phase_y;
1993 priv->imr_data[4].value = imr_point.value;
1994 break;
1995 default:
1996 priv->imr_data[4].gain_x = imr_point.gain_x;
1997 priv->imr_data[4].phase_y = imr_point.phase_y;
1998 priv->imr_data[4].value = imr_point.value;
1999 break;
2000 }
2001
2002 return 0;
2003}
2004
2005static int r820t_imr_callibrate(struct r820t_priv *priv)
2006{
2007 int rc, i;
2008 int xtal_cap = 0;
2009
2010 if (priv->init_done)
2011 return 0;
2012
2013
2014 if ((priv->cfg->rafael_chip == CHIP_R820T) ||
2015 (priv->cfg->rafael_chip == CHIP_R828S) ||
2016 (priv->cfg->rafael_chip == CHIP_R820C)) {
2017 priv->xtal_cap_sel = XTAL_HIGH_CAP_0P;
2018 } else {
2019
2020 rc = r820t_write(priv, 0x05,
2021 r820t_init_array, sizeof(r820t_init_array));
2022 if (rc < 0)
2023 return rc;
2024 for (i = 0; i < 3; i++) {
2025 rc = r820t_xtal_check(priv);
2026 if (rc < 0)
2027 return rc;
2028 if (!i || rc > xtal_cap)
2029 xtal_cap = rc;
2030 }
2031 priv->xtal_cap_sel = xtal_cap;
2032 }
2033
2034
2035
2036
2037
2038 if (no_imr_cal) {
2039 priv->init_done = true;
2040
2041 return 0;
2042 }
2043
2044
2045 rc = r820t_write(priv, 0x05,
2046 r820t_init_array, sizeof(r820t_init_array));
2047 if (rc < 0)
2048 return rc;
2049
2050 rc = r820t_imr_prepare(priv);
2051 if (rc < 0)
2052 return rc;
2053
2054 rc = r820t_imr(priv, 3, true);
2055 if (rc < 0)
2056 return rc;
2057 rc = r820t_imr(priv, 1, false);
2058 if (rc < 0)
2059 return rc;
2060 rc = r820t_imr(priv, 0, false);
2061 if (rc < 0)
2062 return rc;
2063 rc = r820t_imr(priv, 2, false);
2064 if (rc < 0)
2065 return rc;
2066 rc = r820t_imr(priv, 4, false);
2067 if (rc < 0)
2068 return rc;
2069
2070 priv->init_done = true;
2071 priv->imr_done = true;
2072
2073 return 0;
2074}
2075
2076#if 0
2077
2078static int r820t_gpio(struct r820t_priv *priv, bool enable)
2079{
2080 return r820t_write_reg_mask(priv, 0x0f, enable ? 1 : 0, 0x01);
2081}
2082#endif
2083
2084
2085
2086
2087
2088
2089
2090static int r820t_init(struct dvb_frontend *fe)
2091{
2092 struct r820t_priv *priv = fe->tuner_priv;
2093 int rc;
2094
2095 tuner_dbg("%s:\n", __func__);
2096
2097 mutex_lock(&priv->lock);
2098 if (fe->ops.i2c_gate_ctrl)
2099 fe->ops.i2c_gate_ctrl(fe, 1);
2100
2101 rc = r820t_imr_callibrate(priv);
2102 if (rc < 0)
2103 goto err;
2104
2105
2106 rc = r820t_write(priv, 0x05,
2107 r820t_init_array, sizeof(r820t_init_array));
2108
2109err:
2110 if (fe->ops.i2c_gate_ctrl)
2111 fe->ops.i2c_gate_ctrl(fe, 0);
2112 mutex_unlock(&priv->lock);
2113
2114 if (rc < 0)
2115 tuner_dbg("%s: failed=%d\n", __func__, rc);
2116 return rc;
2117}
2118
2119static int r820t_sleep(struct dvb_frontend *fe)
2120{
2121 struct r820t_priv *priv = fe->tuner_priv;
2122 int rc;
2123
2124 tuner_dbg("%s:\n", __func__);
2125
2126 mutex_lock(&priv->lock);
2127 if (fe->ops.i2c_gate_ctrl)
2128 fe->ops.i2c_gate_ctrl(fe, 1);
2129
2130 rc = r820t_standby(priv);
2131
2132 if (fe->ops.i2c_gate_ctrl)
2133 fe->ops.i2c_gate_ctrl(fe, 0);
2134 mutex_unlock(&priv->lock);
2135
2136 tuner_dbg("%s: failed=%d\n", __func__, rc);
2137 return rc;
2138}
2139
2140static int r820t_set_analog_freq(struct dvb_frontend *fe,
2141 struct analog_parameters *p)
2142{
2143 struct r820t_priv *priv = fe->tuner_priv;
2144 unsigned bw;
2145 int rc;
2146
2147 tuner_dbg("%s called\n", __func__);
2148
2149
2150 if (!p->std)
2151 p->std = V4L2_STD_MN;
2152
2153 if ((p->std == V4L2_STD_PAL_M) || (p->std == V4L2_STD_NTSC))
2154 bw = 6;
2155 else
2156 bw = 8;
2157
2158 mutex_lock(&priv->lock);
2159 if (fe->ops.i2c_gate_ctrl)
2160 fe->ops.i2c_gate_ctrl(fe, 1);
2161
2162 rc = generic_set_freq(fe, 62500l * p->frequency, bw,
2163 V4L2_TUNER_ANALOG_TV, p->std, SYS_UNDEFINED);
2164
2165 if (fe->ops.i2c_gate_ctrl)
2166 fe->ops.i2c_gate_ctrl(fe, 0);
2167 mutex_unlock(&priv->lock);
2168
2169 return rc;
2170}
2171
2172static int r820t_set_params(struct dvb_frontend *fe)
2173{
2174 struct r820t_priv *priv = fe->tuner_priv;
2175 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2176 int rc;
2177 unsigned bw;
2178
2179 tuner_dbg("%s: delivery_system=%d frequency=%d bandwidth_hz=%d\n",
2180 __func__, c->delivery_system, c->frequency, c->bandwidth_hz);
2181
2182 mutex_lock(&priv->lock);
2183 if (fe->ops.i2c_gate_ctrl)
2184 fe->ops.i2c_gate_ctrl(fe, 1);
2185
2186 bw = (c->bandwidth_hz + 500000) / 1000000;
2187 if (!bw)
2188 bw = 8;
2189
2190 rc = generic_set_freq(fe, c->frequency, bw,
2191 V4L2_TUNER_DIGITAL_TV, 0, c->delivery_system);
2192
2193 if (fe->ops.i2c_gate_ctrl)
2194 fe->ops.i2c_gate_ctrl(fe, 0);
2195 mutex_unlock(&priv->lock);
2196
2197 if (rc)
2198 tuner_dbg("%s: failed=%d\n", __func__, rc);
2199 return rc;
2200}
2201
2202static int r820t_signal(struct dvb_frontend *fe, u16 *strength)
2203{
2204 struct r820t_priv *priv = fe->tuner_priv;
2205 int rc = 0;
2206
2207 mutex_lock(&priv->lock);
2208 if (fe->ops.i2c_gate_ctrl)
2209 fe->ops.i2c_gate_ctrl(fe, 1);
2210
2211 if (priv->has_lock) {
2212 rc = r820t_read_gain(priv);
2213 if (rc < 0)
2214 goto err;
2215
2216
2217 *strength = (45 - rc) << 4 | 0xff;
2218 if (*strength == 0xff)
2219 *strength = 0;
2220 } else {
2221 *strength = 0;
2222 }
2223
2224err:
2225 if (fe->ops.i2c_gate_ctrl)
2226 fe->ops.i2c_gate_ctrl(fe, 0);
2227 mutex_unlock(&priv->lock);
2228
2229 tuner_dbg("%s: %s, gain=%d strength=%d\n",
2230 __func__,
2231 priv->has_lock ? "PLL locked" : "no signal",
2232 rc, *strength);
2233
2234 return 0;
2235}
2236
2237static int r820t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
2238{
2239 struct r820t_priv *priv = fe->tuner_priv;
2240
2241 tuner_dbg("%s:\n", __func__);
2242
2243 *frequency = priv->int_freq;
2244
2245 return 0;
2246}
2247
2248static int r820t_release(struct dvb_frontend *fe)
2249{
2250 struct r820t_priv *priv = fe->tuner_priv;
2251
2252 tuner_dbg("%s:\n", __func__);
2253
2254 mutex_lock(&r820t_list_mutex);
2255
2256 if (priv)
2257 hybrid_tuner_release_state(priv);
2258
2259 mutex_unlock(&r820t_list_mutex);
2260
2261 kfree(fe->tuner_priv);
2262 fe->tuner_priv = NULL;
2263
2264 return 0;
2265}
2266
2267static const struct dvb_tuner_ops r820t_tuner_ops = {
2268 .info = {
2269 .name = "Rafael Micro R820T",
2270 .frequency_min = 42000000,
2271 .frequency_max = 1002000000,
2272 },
2273 .init = r820t_init,
2274 .release = r820t_release,
2275 .sleep = r820t_sleep,
2276 .set_params = r820t_set_params,
2277 .set_analog_params = r820t_set_analog_freq,
2278 .get_if_frequency = r820t_get_if_frequency,
2279 .get_rf_strength = r820t_signal,
2280};
2281
2282struct dvb_frontend *r820t_attach(struct dvb_frontend *fe,
2283 struct i2c_adapter *i2c,
2284 const struct r820t_config *cfg)
2285{
2286 struct r820t_priv *priv;
2287 int rc = -ENODEV;
2288 u8 data[5];
2289 int instance;
2290
2291 mutex_lock(&r820t_list_mutex);
2292
2293 instance = hybrid_tuner_request_state(struct r820t_priv, priv,
2294 hybrid_tuner_instance_list,
2295 i2c, cfg->i2c_addr,
2296 "r820t");
2297 switch (instance) {
2298 case 0:
2299
2300 goto err_no_gate;
2301 break;
2302 case 1:
2303
2304 priv->cfg = cfg;
2305
2306 mutex_init(&priv->lock);
2307
2308 fe->tuner_priv = priv;
2309 break;
2310 case 2:
2311
2312 fe->tuner_priv = priv;
2313 break;
2314 }
2315
2316 memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops, sizeof(r820t_tuner_ops));
2317
2318 if (fe->ops.i2c_gate_ctrl)
2319 fe->ops.i2c_gate_ctrl(fe, 1);
2320
2321
2322 rc = r820t_read(priv, 0x00, data, sizeof(data));
2323 if (rc < 0)
2324 goto err;
2325
2326 rc = r820t_sleep(fe);
2327 if (rc < 0)
2328 goto err;
2329
2330 tuner_info("Rafael Micro r820t successfully identified\n");
2331
2332 fe->tuner_priv = priv;
2333 memcpy(&fe->ops.tuner_ops, &r820t_tuner_ops,
2334 sizeof(struct dvb_tuner_ops));
2335
2336 if (fe->ops.i2c_gate_ctrl)
2337 fe->ops.i2c_gate_ctrl(fe, 0);
2338
2339 mutex_unlock(&r820t_list_mutex);
2340
2341 return fe;
2342err:
2343 if (fe->ops.i2c_gate_ctrl)
2344 fe->ops.i2c_gate_ctrl(fe, 0);
2345
2346err_no_gate:
2347 mutex_unlock(&r820t_list_mutex);
2348
2349 tuner_info("%s: failed=%d\n", __func__, rc);
2350 r820t_release(fe);
2351 return NULL;
2352}
2353EXPORT_SYMBOL_GPL(r820t_attach);
2354
2355MODULE_DESCRIPTION("Rafael Micro r820t silicon tuner driver");
2356MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
2357MODULE_LICENSE("GPL");
2358