2 * Support for LGDT3306A - 8VSB/QAM-B
4 * Copyright (C) 2013 Fred Richter <frichter@hauppauge.com>
5 * - driver structure based on lgdt3305.[ch] by Michael Krufky
6 * - code based on LG3306_V0.35 API by LG Electronics Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <asm/div64.h>
22 #include <linux/dvb/frontend.h>
24 #include "lgdt3306a.h"
28 module_param(debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "set debug level (info=1, reg=2 (or-able))");
33 #define DBG_DUMP 4 /* FGR - comment out to remove dump code */
35 #define lg_debug(fmt, arg...) \
36 printk(KERN_DEBUG pr_fmt(fmt), ## arg)
38 #define dbg_info(fmt, arg...) \
40 if (debug & DBG_INFO) \
41 lg_debug(fmt, ## arg); \
44 #define dbg_reg(fmt, arg...) \
46 if (debug & DBG_REG) \
47 lg_debug(fmt, ## arg); \
50 #define lg_chkerr(ret) \
55 pr_err("error %d on line %d\n", ret, __LINE__); \
59 struct lgdt3306a_state
{
60 struct i2c_adapter
*i2c_adap
;
61 const struct lgdt3306a_config
*cfg
;
63 struct dvb_frontend frontend
;
65 enum fe_modulation current_modulation
;
66 u32 current_frequency
;
71 * LG3306A Register Usage
72 * (LG does not really name the registers, so this code does not either)
74 * 0000 -> 00FF Common control and status
75 * 1000 -> 10FF Synchronizer control and status
76 * 1F00 -> 1FFF Smart Antenna control and status
77 * 2100 -> 21FF VSB Equalizer control and status
78 * 2800 -> 28FF QAM Equalizer control and status
79 * 3000 -> 30FF FEC control and status
82 enum lgdt3306a_lock_status
{
85 LG3306_UNKNOWN_LOCK
= 0xff
88 enum lgdt3306a_neverlock_status
{
89 LG3306_NL_INIT
= 0x00,
90 LG3306_NL_PROCESS
= 0x01,
91 LG3306_NL_LOCK
= 0x02,
92 LG3306_NL_FAIL
= 0x03,
93 LG3306_NL_UNKNOWN
= 0xff
96 enum lgdt3306a_modulation
{
100 LG3306_UNKNOWN_MODE
= 0xff
103 enum lgdt3306a_lock_check
{
112 static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state
*state
);
113 static void lgdt3306a_DumpRegs(struct lgdt3306a_state
*state
);
117 static int lgdt3306a_write_reg(struct lgdt3306a_state
*state
, u16 reg
, u8 val
)
120 u8 buf
[] = { reg
>> 8, reg
& 0xff, val
};
121 struct i2c_msg msg
= {
122 .addr
= state
->cfg
->i2c_addr
, .flags
= 0,
123 .buf
= buf
, .len
= 3,
126 dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg
, val
);
128 ret
= i2c_transfer(state
->i2c_adap
, &msg
, 1);
131 pr_err("error (addr %02x %02x <- %02x, err = %i)\n",
132 msg
.buf
[0], msg
.buf
[1], msg
.buf
[2], ret
);
141 static int lgdt3306a_read_reg(struct lgdt3306a_state
*state
, u16 reg
, u8
*val
)
144 u8 reg_buf
[] = { reg
>> 8, reg
& 0xff };
145 struct i2c_msg msg
[] = {
146 { .addr
= state
->cfg
->i2c_addr
,
147 .flags
= 0, .buf
= reg_buf
, .len
= 2 },
148 { .addr
= state
->cfg
->i2c_addr
,
149 .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
152 ret
= i2c_transfer(state
->i2c_adap
, msg
, 2);
155 pr_err("error (addr %02x reg %04x error (ret == %i)\n",
156 state
->cfg
->i2c_addr
, reg
, ret
);
162 dbg_reg("reg: 0x%04x, val: 0x%02x\n", reg
, *val
);
167 #define read_reg(state, reg) \
170 int ret = lgdt3306a_read_reg(state, reg, &__val); \
171 if (lg_chkerr(ret)) \
176 static int lgdt3306a_set_reg_bit(struct lgdt3306a_state
*state
,
177 u16 reg
, int bit
, int onoff
)
182 dbg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg
, bit
, onoff
);
184 ret
= lgdt3306a_read_reg(state
, reg
, &val
);
189 val
|= (onoff
& 1) << bit
;
191 ret
= lgdt3306a_write_reg(state
, reg
, val
);
197 /* ------------------------------------------------------------------------ */
199 static int lgdt3306a_soft_reset(struct lgdt3306a_state
*state
)
205 ret
= lgdt3306a_set_reg_bit(state
, 0x0000, 7, 0);
210 ret
= lgdt3306a_set_reg_bit(state
, 0x0000, 7, 1);
217 static int lgdt3306a_mpeg_mode(struct lgdt3306a_state
*state
,
218 enum lgdt3306a_mpeg_mode mode
)
223 dbg_info("(%d)\n", mode
);
224 /* transport packet format - TPSENB=0x80 */
225 ret
= lgdt3306a_set_reg_bit(state
, 0x0071, 7,
226 mode
== LGDT3306A_MPEG_PARALLEL
? 1 : 0);
231 * start of packet signal duration
232 * TPSSOPBITEN=0x40; 0=byte duration, 1=bit duration
234 ret
= lgdt3306a_set_reg_bit(state
, 0x0071, 6, 0);
238 ret
= lgdt3306a_read_reg(state
, 0x0070, &val
);
242 val
|= 0x10; /* TPCLKSUPB=0x10 */
244 if (mode
== LGDT3306A_MPEG_PARALLEL
)
247 ret
= lgdt3306a_write_reg(state
, 0x0070, val
);
254 static int lgdt3306a_mpeg_mode_polarity(struct lgdt3306a_state
*state
,
255 enum lgdt3306a_tp_clock_edge edge
,
256 enum lgdt3306a_tp_valid_polarity valid
)
261 dbg_info("edge=%d, valid=%d\n", edge
, valid
);
263 ret
= lgdt3306a_read_reg(state
, 0x0070, &val
);
267 val
&= ~0x06; /* TPCLKPOL=0x04, TPVALPOL=0x02 */
269 if (edge
== LGDT3306A_TPCLK_RISING_EDGE
)
271 if (valid
== LGDT3306A_TP_VALID_HIGH
)
274 ret
= lgdt3306a_write_reg(state
, 0x0070, val
);
281 static int lgdt3306a_mpeg_tristate(struct lgdt3306a_state
*state
,
287 dbg_info("(%d)\n", mode
);
290 ret
= lgdt3306a_read_reg(state
, 0x0070, &val
);
294 * Tristate bus; TPOUTEN=0x80, TPCLKOUTEN=0x20,
298 ret
= lgdt3306a_write_reg(state
, 0x0070, val
);
302 /* AGCIFOUTENB=0x40; 1=Disable IFAGC pin */
303 ret
= lgdt3306a_set_reg_bit(state
, 0x0003, 6, 1);
308 /* enable IFAGC pin */
309 ret
= lgdt3306a_set_reg_bit(state
, 0x0003, 6, 0);
313 ret
= lgdt3306a_read_reg(state
, 0x0070, &val
);
317 val
|= 0xa8; /* enable bus */
318 ret
= lgdt3306a_write_reg(state
, 0x0070, val
);
327 static int lgdt3306a_ts_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
329 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
331 dbg_info("acquire=%d\n", acquire
);
333 return lgdt3306a_mpeg_tristate(state
, acquire
? 0 : 1);
337 static int lgdt3306a_power(struct lgdt3306a_state
*state
,
342 dbg_info("(%d)\n", mode
);
346 ret
= lgdt3306a_set_reg_bit(state
, 0x0000, 7, 0);
351 ret
= lgdt3306a_set_reg_bit(state
, 0x0000, 0, 0);
357 ret
= lgdt3306a_set_reg_bit(state
, 0x0000, 7, 1);
362 ret
= lgdt3306a_set_reg_bit(state
, 0x0000, 0, 1);
368 lgdt3306a_DumpAllRegs(state
);
375 static int lgdt3306a_set_vsb(struct lgdt3306a_state
*state
)
382 /* 0. Spectrum inversion detection manual; spectrum inverted */
383 ret
= lgdt3306a_read_reg(state
, 0x0002, &val
);
384 val
&= 0xf7; /* SPECINVAUTO Off */
385 val
|= 0x04; /* SPECINV On */
386 ret
= lgdt3306a_write_reg(state
, 0x0002, val
);
390 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
391 ret
= lgdt3306a_write_reg(state
, 0x0008, 0x80);
395 /* 2. Bandwidth mode for VSB(6MHz) */
396 ret
= lgdt3306a_read_reg(state
, 0x0009, &val
);
398 val
|= 0x0c; /* STDOPDETTMODE[2:0]=3 */
399 ret
= lgdt3306a_write_reg(state
, 0x0009, val
);
403 /* 3. QAM mode detection mode(None) */
404 ret
= lgdt3306a_read_reg(state
, 0x0009, &val
);
405 val
&= 0xfc; /* STDOPDETCMODE[1:0]=0 */
406 ret
= lgdt3306a_write_reg(state
, 0x0009, val
);
410 /* 4. ADC sampling frequency rate(2x sampling) */
411 ret
= lgdt3306a_read_reg(state
, 0x000d, &val
);
412 val
&= 0xbf; /* SAMPLING4XFEN=0 */
413 ret
= lgdt3306a_write_reg(state
, 0x000d, val
);
418 /* FGR - disable any AICC filtering, testing only */
420 ret
= lgdt3306a_write_reg(state
, 0x0024, 0x00);
424 /* AICCFIXFREQ0 NT N-1(Video rejection) */
425 ret
= lgdt3306a_write_reg(state
, 0x002e, 0x00);
426 ret
= lgdt3306a_write_reg(state
, 0x002f, 0x00);
427 ret
= lgdt3306a_write_reg(state
, 0x0030, 0x00);
429 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
430 ret
= lgdt3306a_write_reg(state
, 0x002b, 0x00);
431 ret
= lgdt3306a_write_reg(state
, 0x002c, 0x00);
432 ret
= lgdt3306a_write_reg(state
, 0x002d, 0x00);
434 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
435 ret
= lgdt3306a_write_reg(state
, 0x0028, 0x00);
436 ret
= lgdt3306a_write_reg(state
, 0x0029, 0x00);
437 ret
= lgdt3306a_write_reg(state
, 0x002a, 0x00);
439 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
440 ret
= lgdt3306a_write_reg(state
, 0x0025, 0x00);
441 ret
= lgdt3306a_write_reg(state
, 0x0026, 0x00);
442 ret
= lgdt3306a_write_reg(state
, 0x0027, 0x00);
445 /* FGR - this works well for HVR-1955,1975 */
447 /* 5. AICCOPMODE NT N-1 Adj. */
448 ret
= lgdt3306a_write_reg(state
, 0x0024, 0x5A);
452 /* AICCFIXFREQ0 NT N-1(Video rejection) */
453 ret
= lgdt3306a_write_reg(state
, 0x002e, 0x5A);
454 ret
= lgdt3306a_write_reg(state
, 0x002f, 0x00);
455 ret
= lgdt3306a_write_reg(state
, 0x0030, 0x00);
457 /* AICCFIXFREQ1 NT N-1(Audio rejection) */
458 ret
= lgdt3306a_write_reg(state
, 0x002b, 0x36);
459 ret
= lgdt3306a_write_reg(state
, 0x002c, 0x00);
460 ret
= lgdt3306a_write_reg(state
, 0x002d, 0x00);
462 /* AICCFIXFREQ2 NT Co-Channel(Video rejection) */
463 ret
= lgdt3306a_write_reg(state
, 0x0028, 0x2A);
464 ret
= lgdt3306a_write_reg(state
, 0x0029, 0x00);
465 ret
= lgdt3306a_write_reg(state
, 0x002a, 0x00);
467 /* AICCFIXFREQ3 NT Co-Channel(Audio rejection) */
468 ret
= lgdt3306a_write_reg(state
, 0x0025, 0x06);
469 ret
= lgdt3306a_write_reg(state
, 0x0026, 0x00);
470 ret
= lgdt3306a_write_reg(state
, 0x0027, 0x00);
473 ret
= lgdt3306a_read_reg(state
, 0x001e, &val
);
476 ret
= lgdt3306a_write_reg(state
, 0x001e, val
);
478 ret
= lgdt3306a_write_reg(state
, 0x0022, 0x08);
480 ret
= lgdt3306a_write_reg(state
, 0x0023, 0xFF);
482 ret
= lgdt3306a_read_reg(state
, 0x211f, &val
);
484 ret
= lgdt3306a_write_reg(state
, 0x211f, val
);
486 ret
= lgdt3306a_write_reg(state
, 0x2173, 0x01);
488 ret
= lgdt3306a_read_reg(state
, 0x1061, &val
);
491 ret
= lgdt3306a_write_reg(state
, 0x1061, val
);
493 ret
= lgdt3306a_read_reg(state
, 0x103d, &val
);
495 ret
= lgdt3306a_write_reg(state
, 0x103d, val
);
497 ret
= lgdt3306a_write_reg(state
, 0x2122, 0x40);
499 ret
= lgdt3306a_read_reg(state
, 0x2141, &val
);
501 ret
= lgdt3306a_write_reg(state
, 0x2141, val
);
503 ret
= lgdt3306a_read_reg(state
, 0x2135, &val
);
506 ret
= lgdt3306a_write_reg(state
, 0x2135, val
);
508 ret
= lgdt3306a_read_reg(state
, 0x0003, &val
);
510 ret
= lgdt3306a_write_reg(state
, 0x0003, val
);
512 ret
= lgdt3306a_read_reg(state
, 0x001c, &val
);
514 ret
= lgdt3306a_write_reg(state
, 0x001c, val
);
516 /* 6. EQ step size */
517 ret
= lgdt3306a_read_reg(state
, 0x2179, &val
);
519 ret
= lgdt3306a_write_reg(state
, 0x2179, val
);
521 ret
= lgdt3306a_read_reg(state
, 0x217a, &val
);
523 ret
= lgdt3306a_write_reg(state
, 0x217a, val
);
526 ret
= lgdt3306a_soft_reset(state
);
530 dbg_info("complete\n");
535 static int lgdt3306a_set_qam(struct lgdt3306a_state
*state
, int modulation
)
540 dbg_info("modulation=%d\n", modulation
);
542 /* 1. Selection of standard mode(0x08=QAM, 0x80=VSB) */
543 ret
= lgdt3306a_write_reg(state
, 0x0008, 0x08);
547 /* 1a. Spectrum inversion detection to Auto */
548 ret
= lgdt3306a_read_reg(state
, 0x0002, &val
);
549 val
&= 0xfb; /* SPECINV Off */
550 val
|= 0x08; /* SPECINVAUTO On */
551 ret
= lgdt3306a_write_reg(state
, 0x0002, val
);
555 /* 2. Bandwidth mode for QAM */
556 ret
= lgdt3306a_read_reg(state
, 0x0009, &val
);
557 val
&= 0xe3; /* STDOPDETTMODE[2:0]=0 VSB Off */
558 ret
= lgdt3306a_write_reg(state
, 0x0009, val
);
562 /* 3. : 64QAM/256QAM detection(manual, auto) */
563 ret
= lgdt3306a_read_reg(state
, 0x0009, &val
);
565 val
|= 0x02; /* STDOPDETCMODE[1:0]=1=Manual 2=Auto */
566 ret
= lgdt3306a_write_reg(state
, 0x0009, val
);
570 /* 3a. : 64QAM/256QAM selection for manual */
571 ret
= lgdt3306a_read_reg(state
, 0x101a, &val
);
573 if (modulation
== QAM_64
)
574 val
|= 0x02; /* QMDQMODE[2:0]=2=QAM64 */
576 val
|= 0x04; /* QMDQMODE[2:0]=4=QAM256 */
578 ret
= lgdt3306a_write_reg(state
, 0x101a, val
);
582 /* 4. ADC sampling frequency rate(4x sampling) */
583 ret
= lgdt3306a_read_reg(state
, 0x000d, &val
);
585 val
|= 0x40; /* SAMPLING4XFEN=1 */
586 ret
= lgdt3306a_write_reg(state
, 0x000d, val
);
590 /* 5. No AICC operation in QAM mode */
591 ret
= lgdt3306a_read_reg(state
, 0x0024, &val
);
593 ret
= lgdt3306a_write_reg(state
, 0x0024, val
);
598 ret
= lgdt3306a_soft_reset(state
);
602 dbg_info("complete\n");
607 static int lgdt3306a_set_modulation(struct lgdt3306a_state
*state
,
608 struct dtv_frontend_properties
*p
)
614 switch (p
->modulation
) {
616 ret
= lgdt3306a_set_vsb(state
);
619 ret
= lgdt3306a_set_qam(state
, QAM_64
);
622 ret
= lgdt3306a_set_qam(state
, QAM_256
);
630 state
->current_modulation
= p
->modulation
;
636 /* ------------------------------------------------------------------------ */
638 static int lgdt3306a_agc_setup(struct lgdt3306a_state
*state
,
639 struct dtv_frontend_properties
*p
)
641 /* TODO: anything we want to do here??? */
644 switch (p
->modulation
) {
656 /* ------------------------------------------------------------------------ */
658 static int lgdt3306a_set_inversion(struct lgdt3306a_state
*state
,
663 dbg_info("(%d)\n", inversion
);
665 ret
= lgdt3306a_set_reg_bit(state
, 0x0002, 2, inversion
? 1 : 0);
669 static int lgdt3306a_set_inversion_auto(struct lgdt3306a_state
*state
,
674 dbg_info("(%d)\n", enabled
);
676 /* 0=Manual 1=Auto(QAM only) - SPECINVAUTO=0x04 */
677 ret
= lgdt3306a_set_reg_bit(state
, 0x0002, 3, enabled
);
681 static int lgdt3306a_spectral_inversion(struct lgdt3306a_state
*state
,
682 struct dtv_frontend_properties
*p
,
687 dbg_info("(%d)\n", inversion
);
690 * FGR - spectral_inversion defaults already set for VSB and QAM;
691 * can enable later if desired
694 ret
= lgdt3306a_set_inversion(state
, inversion
);
696 switch (p
->modulation
) {
698 /* Manual only for VSB */
699 ret
= lgdt3306a_set_inversion_auto(state
, 0);
703 /* Auto ok for QAM */
704 ret
= lgdt3306a_set_inversion_auto(state
, 1);
713 static int lgdt3306a_set_if(struct lgdt3306a_state
*state
,
714 struct dtv_frontend_properties
*p
)
720 switch (p
->modulation
) {
722 if_freq_khz
= state
->cfg
->vsb_if_khz
;
726 if_freq_khz
= state
->cfg
->qam_if_khz
;
732 switch (if_freq_khz
) {
734 pr_warn("IF=%d KHz is not supported, 3250 assumed\n",
737 case 3250: /* 3.25Mhz */
741 case 3500: /* 3.50Mhz */
745 case 4000: /* 4.00Mhz */
749 case 5000: /* 5.00Mhz */
753 case 5380: /* 5.38Mhz */
758 ret
= lgdt3306a_write_reg(state
, 0x0010, nco1
);
761 ret
= lgdt3306a_write_reg(state
, 0x0011, nco2
);
765 dbg_info("if_freq=%d KHz->[%04x]\n", if_freq_khz
, nco1
<<8 | nco2
);
770 /* ------------------------------------------------------------------------ */
772 static int lgdt3306a_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
774 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
776 if (state
->cfg
->deny_i2c_rptr
) {
777 dbg_info("deny_i2c_rptr=%d\n", state
->cfg
->deny_i2c_rptr
);
780 dbg_info("(%d)\n", enable
);
783 return lgdt3306a_set_reg_bit(state
, 0x0002, 7, enable
? 0 : 1);
786 static int lgdt3306a_sleep(struct lgdt3306a_state
*state
)
791 state
->current_frequency
= -1; /* force re-tune, when we wake */
793 ret
= lgdt3306a_mpeg_tristate(state
, 1); /* disable data bus */
797 ret
= lgdt3306a_power(state
, 0); /* power down */
804 static int lgdt3306a_fe_sleep(struct dvb_frontend
*fe
)
806 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
808 return lgdt3306a_sleep(state
);
811 static int lgdt3306a_init(struct dvb_frontend
*fe
)
813 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
819 /* 1. Normal operation mode */
820 ret
= lgdt3306a_set_reg_bit(state
, 0x0001, 0, 1); /* SIMFASTENB=0x01 */
824 /* 2. Spectrum inversion auto detection (Not valid for VSB) */
825 ret
= lgdt3306a_set_inversion_auto(state
, 0);
829 /* 3. Spectrum inversion(According to the tuner configuration) */
830 ret
= lgdt3306a_set_inversion(state
, 1);
834 /* 4. Peak-to-peak voltage of ADC input signal */
836 /* ADCSEL1V=0x80=1Vpp; 0x00=2Vpp */
837 ret
= lgdt3306a_set_reg_bit(state
, 0x0004, 7, 1);
841 /* 5. ADC output data capture clock phase */
843 /* 0=same phase as ADC clock */
844 ret
= lgdt3306a_set_reg_bit(state
, 0x0004, 2, 0);
848 /* 5a. ADC sampling clock source */
850 /* ADCCLKPLLSEL=0x08; 0=use ext clock, not PLL */
851 ret
= lgdt3306a_set_reg_bit(state
, 0x0004, 3, 0);
855 /* 6. Automatic PLL set */
857 /* PLLSETAUTO=0x40; 0=off */
858 ret
= lgdt3306a_set_reg_bit(state
, 0x0005, 6, 0);
862 if (state
->cfg
->xtalMHz
== 24) { /* 24MHz */
863 /* 7. Frequency for PLL output(0x2564 for 192MHz for 24MHz) */
864 ret
= lgdt3306a_read_reg(state
, 0x0005, &val
);
869 ret
= lgdt3306a_write_reg(state
, 0x0005, val
);
872 ret
= lgdt3306a_write_reg(state
, 0x0006, 0x64);
876 /* 8. ADC sampling frequency(0x180000 for 24MHz sampling) */
877 ret
= lgdt3306a_read_reg(state
, 0x000d, &val
);
882 ret
= lgdt3306a_write_reg(state
, 0x000d, val
);
886 } else if (state
->cfg
->xtalMHz
== 25) { /* 25MHz */
887 /* 7. Frequency for PLL output */
888 ret
= lgdt3306a_read_reg(state
, 0x0005, &val
);
893 ret
= lgdt3306a_write_reg(state
, 0x0005, val
);
896 ret
= lgdt3306a_write_reg(state
, 0x0006, 0x64);
900 /* 8. ADC sampling frequency(0x190000 for 25MHz sampling) */
901 ret
= lgdt3306a_read_reg(state
, 0x000d, &val
);
906 ret
= lgdt3306a_write_reg(state
, 0x000d, val
);
910 pr_err("Bad xtalMHz=%d\n", state
->cfg
->xtalMHz
);
913 ret
= lgdt3306a_write_reg(state
, 0x000e, 0x00);
914 ret
= lgdt3306a_write_reg(state
, 0x000f, 0x00);
917 /* 9. Center frequency of input signal of ADC */
918 ret
= lgdt3306a_write_reg(state
, 0x0010, 0x34); /* 3.25MHz */
919 ret
= lgdt3306a_write_reg(state
, 0x0011, 0x00);
921 /* 10. Fixed gain error value */
922 ret
= lgdt3306a_write_reg(state
, 0x0014, 0); /* gain error=0 */
924 /* 10a. VSB TR BW gear shift initial step */
925 ret
= lgdt3306a_read_reg(state
, 0x103c, &val
);
927 val
|= 0x20; /* SAMGSAUTOSTL_V[3:0] = 2 */
928 ret
= lgdt3306a_write_reg(state
, 0x103c, val
);
930 /* 10b. Timing offset calibration in low temperature for VSB */
931 ret
= lgdt3306a_read_reg(state
, 0x103d, &val
);
934 ret
= lgdt3306a_write_reg(state
, 0x103d, val
);
936 /* 10c. Timing offset calibration in low temperature for QAM */
937 ret
= lgdt3306a_read_reg(state
, 0x1036, &val
);
940 ret
= lgdt3306a_write_reg(state
, 0x1036, val
);
942 /* 11. Using the imaginary part of CIR in CIR loading */
943 ret
= lgdt3306a_read_reg(state
, 0x211f, &val
);
944 val
&= 0xef; /* do not use imaginary of CIR */
945 ret
= lgdt3306a_write_reg(state
, 0x211f, val
);
947 /* 12. Control of no signal detector function */
948 ret
= lgdt3306a_read_reg(state
, 0x2849, &val
);
949 val
&= 0xef; /* NOUSENOSIGDET=0, enable no signal detector */
950 ret
= lgdt3306a_write_reg(state
, 0x2849, val
);
952 /* FGR - put demod in some known mode */
953 ret
= lgdt3306a_set_vsb(state
);
955 /* 13. TP stream format */
956 ret
= lgdt3306a_mpeg_mode(state
, state
->cfg
->mpeg_mode
);
958 /* 14. disable output buses */
959 ret
= lgdt3306a_mpeg_tristate(state
, 1);
961 /* 15. Sleep (in reset) */
962 ret
= lgdt3306a_sleep(state
);
969 static int lgdt3306a_set_parameters(struct dvb_frontend
*fe
)
971 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
972 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
975 dbg_info("(%d, %d)\n", p
->frequency
, p
->modulation
);
977 if (state
->current_frequency
== p
->frequency
&&
978 state
->current_modulation
== p
->modulation
) {
979 dbg_info(" (already set, skipping ...)\n");
982 state
->current_frequency
= -1;
983 state
->current_modulation
= -1;
985 ret
= lgdt3306a_power(state
, 1); /* power up */
989 if (fe
->ops
.tuner_ops
.set_params
) {
990 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
991 if (fe
->ops
.i2c_gate_ctrl
)
992 fe
->ops
.i2c_gate_ctrl(fe
, 0);
996 state
->current_frequency
= p
->frequency
;
1000 ret
= lgdt3306a_set_modulation(state
, p
);
1004 ret
= lgdt3306a_agc_setup(state
, p
);
1008 ret
= lgdt3306a_set_if(state
, p
);
1012 ret
= lgdt3306a_spectral_inversion(state
, p
,
1013 state
->cfg
->spectral_inversion
? 1 : 0);
1017 ret
= lgdt3306a_mpeg_mode(state
, state
->cfg
->mpeg_mode
);
1021 ret
= lgdt3306a_mpeg_mode_polarity(state
,
1022 state
->cfg
->tpclk_edge
,
1023 state
->cfg
->tpvalid_polarity
);
1027 ret
= lgdt3306a_mpeg_tristate(state
, 0); /* enable data bus */
1031 ret
= lgdt3306a_soft_reset(state
);
1036 lgdt3306a_DumpAllRegs(state
);
1038 state
->current_frequency
= p
->frequency
;
1043 static int lgdt3306a_get_frontend(struct dvb_frontend
*fe
,
1044 struct dtv_frontend_properties
*p
)
1046 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1048 dbg_info("(%u, %d)\n",
1049 state
->current_frequency
, state
->current_modulation
);
1051 p
->modulation
= state
->current_modulation
;
1052 p
->frequency
= state
->current_frequency
;
1056 static enum dvbfe_algo
lgdt3306a_get_frontend_algo(struct dvb_frontend
*fe
)
1059 return DVBFE_ALGO_CUSTOM
;
1061 return DVBFE_ALGO_HW
;
1065 /* ------------------------------------------------------------------------ */
1066 static int lgdt3306a_monitor_vsb(struct lgdt3306a_state
*state
)
1070 u8 snrRef
, maxPowerMan
, nCombDet
;
1073 ret
= lgdt3306a_read_reg(state
, 0x21a1, &val
);
1076 snrRef
= val
& 0x3f;
1078 ret
= lgdt3306a_read_reg(state
, 0x2185, &maxPowerMan
);
1082 ret
= lgdt3306a_read_reg(state
, 0x2191, &val
);
1085 nCombDet
= (val
& 0x80) >> 7;
1087 ret
= lgdt3306a_read_reg(state
, 0x2180, &val
);
1090 fbDlyCir
= (val
& 0x03) << 8;
1092 ret
= lgdt3306a_read_reg(state
, 0x2181, &val
);
1097 dbg_info("snrRef=%d maxPowerMan=0x%x nCombDet=%d fbDlyCir=0x%x\n",
1098 snrRef
, maxPowerMan
, nCombDet
, fbDlyCir
);
1100 /* Carrier offset sub loop bandwidth */
1101 ret
= lgdt3306a_read_reg(state
, 0x1061, &val
);
1105 if ((snrRef
> 18) && (maxPowerMan
> 0x68)
1106 && (nCombDet
== 0x01)
1107 && ((fbDlyCir
== 0x03FF) || (fbDlyCir
< 0x6C))) {
1108 /* SNR is over 18dB and no ghosting */
1109 val
|= 0x00; /* final bandwidth = 0 */
1111 val
|= 0x04; /* final bandwidth = 4 */
1113 ret
= lgdt3306a_write_reg(state
, 0x1061, val
);
1117 /* Adjust Notch Filter */
1118 ret
= lgdt3306a_read_reg(state
, 0x0024, &val
);
1122 if (nCombDet
== 0) { /* Turn on the Notch Filter */
1125 ret
= lgdt3306a_write_reg(state
, 0x0024, val
);
1129 /* VSB Timing Recovery output normalization */
1130 ret
= lgdt3306a_read_reg(state
, 0x103d, &val
);
1135 ret
= lgdt3306a_write_reg(state
, 0x103d, val
);
1140 static enum lgdt3306a_modulation
1141 lgdt3306a_check_oper_mode(struct lgdt3306a_state
*state
)
1146 ret
= lgdt3306a_read_reg(state
, 0x0081, &val
);
1155 ret
= lgdt3306a_read_reg(state
, 0x00a6, &val
);
1160 dbg_info("QAM256\n");
1161 return LG3306_QAM256
;
1163 dbg_info("QAM64\n");
1164 return LG3306_QAM64
;
1167 pr_warn("UNKNOWN\n");
1168 return LG3306_UNKNOWN_MODE
;
1171 static enum lgdt3306a_lock_status
1172 lgdt3306a_check_lock_status(struct lgdt3306a_state
*state
,
1173 enum lgdt3306a_lock_check whatLock
)
1177 enum lgdt3306a_modulation modeOper
;
1178 enum lgdt3306a_lock_status lockStatus
;
1180 modeOper
= LG3306_UNKNOWN_MODE
;
1183 case LG3306_SYNC_LOCK
:
1185 ret
= lgdt3306a_read_reg(state
, 0x00a6, &val
);
1189 if ((val
& 0x80) == 0x80)
1190 lockStatus
= LG3306_LOCK
;
1192 lockStatus
= LG3306_UNLOCK
;
1194 dbg_info("SYNC_LOCK=%x\n", lockStatus
);
1197 case LG3306_AGC_LOCK
:
1199 ret
= lgdt3306a_read_reg(state
, 0x0080, &val
);
1203 if ((val
& 0x40) == 0x40)
1204 lockStatus
= LG3306_LOCK
;
1206 lockStatus
= LG3306_UNLOCK
;
1208 dbg_info("AGC_LOCK=%x\n", lockStatus
);
1211 case LG3306_TR_LOCK
:
1213 modeOper
= lgdt3306a_check_oper_mode(state
);
1214 if ((modeOper
== LG3306_QAM64
) || (modeOper
== LG3306_QAM256
)) {
1215 ret
= lgdt3306a_read_reg(state
, 0x1094, &val
);
1219 if ((val
& 0x80) == 0x80)
1220 lockStatus
= LG3306_LOCK
;
1222 lockStatus
= LG3306_UNLOCK
;
1224 lockStatus
= LG3306_UNKNOWN_LOCK
;
1226 dbg_info("TR_LOCK=%x\n", lockStatus
);
1229 case LG3306_FEC_LOCK
:
1231 modeOper
= lgdt3306a_check_oper_mode(state
);
1232 if ((modeOper
== LG3306_QAM64
) || (modeOper
== LG3306_QAM256
)) {
1233 ret
= lgdt3306a_read_reg(state
, 0x0080, &val
);
1237 if ((val
& 0x10) == 0x10)
1238 lockStatus
= LG3306_LOCK
;
1240 lockStatus
= LG3306_UNLOCK
;
1242 lockStatus
= LG3306_UNKNOWN_LOCK
;
1244 dbg_info("FEC_LOCK=%x\n", lockStatus
);
1249 lockStatus
= LG3306_UNKNOWN_LOCK
;
1250 pr_warn("UNKNOWN whatLock=%d\n", whatLock
);
1257 static enum lgdt3306a_neverlock_status
1258 lgdt3306a_check_neverlock_status(struct lgdt3306a_state
*state
)
1262 enum lgdt3306a_neverlock_status lockStatus
;
1264 ret
= lgdt3306a_read_reg(state
, 0x0080, &val
);
1267 lockStatus
= (enum lgdt3306a_neverlock_status
)(val
& 0x03);
1269 dbg_info("NeverLock=%d", lockStatus
);
1274 static int lgdt3306a_pre_monitoring(struct lgdt3306a_state
*state
)
1278 u8 currChDiffACQ
, snrRef
, mainStrong
, aiccrejStatus
;
1280 /* Channel variation */
1281 ret
= lgdt3306a_read_reg(state
, 0x21bc, &currChDiffACQ
);
1285 /* SNR of Frame sync */
1286 ret
= lgdt3306a_read_reg(state
, 0x21a1, &val
);
1289 snrRef
= val
& 0x3f;
1291 /* Strong Main CIR */
1292 ret
= lgdt3306a_read_reg(state
, 0x2199, &val
);
1295 mainStrong
= (val
& 0x40) >> 6;
1297 ret
= lgdt3306a_read_reg(state
, 0x0090, &val
);
1300 aiccrejStatus
= (val
& 0xf0) >> 4;
1302 dbg_info("snrRef=%d mainStrong=%d aiccrejStatus=%d currChDiffACQ=0x%x\n",
1303 snrRef
, mainStrong
, aiccrejStatus
, currChDiffACQ
);
1306 /* Dynamic ghost exists */
1307 if ((mainStrong
== 0) && (currChDiffACQ
> 0x70))
1309 if (mainStrong
== 0) {
1310 ret
= lgdt3306a_read_reg(state
, 0x2135, &val
);
1315 ret
= lgdt3306a_write_reg(state
, 0x2135, val
);
1319 ret
= lgdt3306a_read_reg(state
, 0x2141, &val
);
1324 ret
= lgdt3306a_write_reg(state
, 0x2141, val
);
1328 ret
= lgdt3306a_write_reg(state
, 0x2122, 0x70);
1331 } else { /* Weak ghost or static channel */
1332 ret
= lgdt3306a_read_reg(state
, 0x2135, &val
);
1337 ret
= lgdt3306a_write_reg(state
, 0x2135, val
);
1341 ret
= lgdt3306a_read_reg(state
, 0x2141, &val
);
1346 ret
= lgdt3306a_write_reg(state
, 0x2141, val
);
1350 ret
= lgdt3306a_write_reg(state
, 0x2122, 0x40);
1357 static enum lgdt3306a_lock_status
1358 lgdt3306a_sync_lock_poll(struct lgdt3306a_state
*state
)
1360 enum lgdt3306a_lock_status syncLockStatus
= LG3306_UNLOCK
;
1363 for (i
= 0; i
< 2; i
++) {
1366 syncLockStatus
= lgdt3306a_check_lock_status(state
,
1369 if (syncLockStatus
== LG3306_LOCK
) {
1370 dbg_info("locked(%d)\n", i
);
1374 dbg_info("not locked\n");
1375 return LG3306_UNLOCK
;
1378 static enum lgdt3306a_lock_status
1379 lgdt3306a_fec_lock_poll(struct lgdt3306a_state
*state
)
1381 enum lgdt3306a_lock_status FECLockStatus
= LG3306_UNLOCK
;
1384 for (i
= 0; i
< 2; i
++) {
1387 FECLockStatus
= lgdt3306a_check_lock_status(state
,
1390 if (FECLockStatus
== LG3306_LOCK
) {
1391 dbg_info("locked(%d)\n", i
);
1392 return FECLockStatus
;
1395 dbg_info("not locked\n");
1396 return FECLockStatus
;
1399 static enum lgdt3306a_neverlock_status
1400 lgdt3306a_neverlock_poll(struct lgdt3306a_state
*state
)
1402 enum lgdt3306a_neverlock_status NLLockStatus
= LG3306_NL_FAIL
;
1405 for (i
= 0; i
< 5; i
++) {
1408 NLLockStatus
= lgdt3306a_check_neverlock_status(state
);
1410 if (NLLockStatus
== LG3306_NL_LOCK
) {
1411 dbg_info("NL_LOCK(%d)\n", i
);
1412 return NLLockStatus
;
1415 dbg_info("NLLockStatus=%d\n", NLLockStatus
);
1416 return NLLockStatus
;
1419 static u8
lgdt3306a_get_packet_error(struct lgdt3306a_state
*state
)
1424 ret
= lgdt3306a_read_reg(state
, 0x00fa, &val
);
1431 static const u32 valx_x10
[] = {
1432 10, 11, 13, 15, 17, 20, 25, 33, 41, 50, 59, 73, 87, 100
1434 static const u32 log10x_x1000
[] = {
1435 0, 41, 114, 176, 230, 301, 398, 518, 613, 699, 771, 863, 939, 1000
1438 static u32
log10_x1000(u32 x
)
1440 u32 diff_val
, step_val
, step_log10
;
1445 return -1000000; /* signal error */
1448 return 0; /* log(1)=0 */
1455 } else { /* x > 10 */
1463 if (x
== 10) /* was our input an exact multiple of 10 */
1464 return log_val
; /* don't need to interpolate */
1466 /* find our place on the log curve */
1467 for (i
= 1; i
< ARRAY_SIZE(valx_x10
); i
++) {
1468 if (valx_x10
[i
] >= x
)
1471 if (i
== ARRAY_SIZE(valx_x10
))
1472 return log_val
+ log10x_x1000
[i
- 1];
1474 diff_val
= x
- valx_x10
[i
-1];
1475 step_val
= valx_x10
[i
] - valx_x10
[i
- 1];
1476 step_log10
= log10x_x1000
[i
] - log10x_x1000
[i
- 1];
1478 /* do a linear interpolation to get in-between values */
1479 return log_val
+ log10x_x1000
[i
- 1] +
1480 ((diff_val
*step_log10
) / step_val
);
1483 static u32
lgdt3306a_calculate_snr_x100(struct lgdt3306a_state
*state
)
1485 u32 mse
; /* Mean-Square Error */
1486 u32 pwr
; /* Constelation power */
1489 mse
= (read_reg(state
, 0x00ec) << 8) |
1490 (read_reg(state
, 0x00ed));
1491 pwr
= (read_reg(state
, 0x00e8) << 8) |
1492 (read_reg(state
, 0x00e9));
1494 if (mse
== 0) /* no signal */
1497 snr_x100
= log10_x1000((pwr
* 10000) / mse
) - 3000;
1498 dbg_info("mse=%u, pwr=%u, snr_x100=%d\n", mse
, pwr
, snr_x100
);
1503 static enum lgdt3306a_lock_status
1504 lgdt3306a_vsb_lock_poll(struct lgdt3306a_state
*state
)
1511 for (cnt
= 0; cnt
< 10; cnt
++) {
1512 if (lgdt3306a_sync_lock_poll(state
) == LG3306_UNLOCK
) {
1513 dbg_info("no sync lock!\n");
1514 return LG3306_UNLOCK
;
1518 ret
= lgdt3306a_pre_monitoring(state
);
1522 packet_error
= lgdt3306a_get_packet_error(state
);
1523 snr
= lgdt3306a_calculate_snr_x100(state
);
1524 dbg_info("cnt=%d errors=%d snr=%d\n", cnt
, packet_error
, snr
);
1526 if ((snr
>= 1500) && (packet_error
< 0xff))
1530 dbg_info("not locked!\n");
1531 return LG3306_UNLOCK
;
1534 static enum lgdt3306a_lock_status
1535 lgdt3306a_qam_lock_poll(struct lgdt3306a_state
*state
)
1541 for (cnt
= 0; cnt
< 10; cnt
++) {
1542 if (lgdt3306a_fec_lock_poll(state
) == LG3306_UNLOCK
) {
1543 dbg_info("no fec lock!\n");
1544 return LG3306_UNLOCK
;
1549 packet_error
= lgdt3306a_get_packet_error(state
);
1550 snr
= lgdt3306a_calculate_snr_x100(state
);
1551 dbg_info("cnt=%d errors=%d snr=%d\n", cnt
, packet_error
, snr
);
1553 if ((snr
>= 1500) && (packet_error
< 0xff))
1557 dbg_info("not locked!\n");
1558 return LG3306_UNLOCK
;
1561 static int lgdt3306a_read_status(struct dvb_frontend
*fe
,
1562 enum fe_status
*status
)
1564 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1568 if (fe
->ops
.tuner_ops
.get_rf_strength
) {
1569 ret
= fe
->ops
.tuner_ops
.get_rf_strength(fe
, &strength
);
1571 dbg_info("strength=%d\n", strength
);
1573 dbg_info("fe->ops.tuner_ops.get_rf_strength() failed\n");
1577 if (lgdt3306a_neverlock_poll(state
) == LG3306_NL_LOCK
) {
1578 *status
|= FE_HAS_SIGNAL
;
1579 *status
|= FE_HAS_CARRIER
;
1581 switch (state
->current_modulation
) {
1584 if (lgdt3306a_qam_lock_poll(state
) == LG3306_LOCK
) {
1585 *status
|= FE_HAS_VITERBI
;
1586 *status
|= FE_HAS_SYNC
;
1588 *status
|= FE_HAS_LOCK
;
1592 if (lgdt3306a_vsb_lock_poll(state
) == LG3306_LOCK
) {
1593 *status
|= FE_HAS_VITERBI
;
1594 *status
|= FE_HAS_SYNC
;
1596 *status
|= FE_HAS_LOCK
;
1598 ret
= lgdt3306a_monitor_vsb(state
);
1609 static int lgdt3306a_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
1611 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1613 state
->snr
= lgdt3306a_calculate_snr_x100(state
);
1614 /* report SNR in dB * 10 */
1615 *snr
= state
->snr
/10;
1620 static int lgdt3306a_read_signal_strength(struct dvb_frontend
*fe
,
1624 * Calculate some sort of "strength" from SNR
1626 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1627 u16 snr
; /* snr_x10 */
1629 u32 ref_snr
; /* snr*100 */
1634 switch (state
->current_modulation
) {
1636 ref_snr
= 1600; /* 16dB */
1639 ref_snr
= 2200; /* 22dB */
1642 ref_snr
= 2800; /* 28dB */
1648 ret
= fe
->ops
.read_snr(fe
, &snr
);
1652 if (state
->snr
<= (ref_snr
- 100))
1654 else if (state
->snr
<= ref_snr
)
1655 str
= (0xffff * 65) / 100; /* 65% */
1657 str
= state
->snr
- ref_snr
;
1659 str
+= 78; /* 78%-100% */
1662 str
= (0xffff * str
) / 100;
1664 *strength
= (u16
)str
;
1665 dbg_info("strength=%u\n", *strength
);
1671 /* ------------------------------------------------------------------------ */
1673 static int lgdt3306a_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
1675 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1680 /* FGR - FIXME - I don't know what value is expected by dvb_core
1681 * what is the scale of the value?? */
1682 tmp
= read_reg(state
, 0x00fc); /* NBERVALUE[24-31] */
1683 tmp
= (tmp
<< 8) | read_reg(state
, 0x00fd); /* NBERVALUE[16-23] */
1684 tmp
= (tmp
<< 8) | read_reg(state
, 0x00fe); /* NBERVALUE[8-15] */
1685 tmp
= (tmp
<< 8) | read_reg(state
, 0x00ff); /* NBERVALUE[0-7] */
1687 dbg_info("ber=%u\n", tmp
);
1692 static int lgdt3306a_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
1694 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1698 /* FGR - FIXME - I don't know what value is expected by dvb_core
1699 * what happens when value wraps? */
1700 *ucblocks
= read_reg(state
, 0x00f4); /* TPIFTPERRCNT[0-7] */
1701 dbg_info("ucblocks=%u\n", *ucblocks
);
1707 static int lgdt3306a_tune(struct dvb_frontend
*fe
, bool re_tune
,
1708 unsigned int mode_flags
, unsigned int *delay
,
1709 enum fe_status
*status
)
1712 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1714 dbg_info("re_tune=%u\n", re_tune
);
1717 state
->current_frequency
= -1; /* force re-tune */
1718 ret
= lgdt3306a_set_parameters(fe
);
1723 ret
= lgdt3306a_read_status(fe
, status
);
1728 static int lgdt3306a_get_tune_settings(struct dvb_frontend
*fe
,
1729 struct dvb_frontend_tune_settings
1732 fe_tune_settings
->min_delay_ms
= 100;
1737 static int lgdt3306a_search(struct dvb_frontend
*fe
)
1739 enum fe_status status
= 0;
1743 ret
= lgdt3306a_set_parameters(fe
);
1747 ret
= lgdt3306a_read_status(fe
, &status
);
1751 /* check if we have a valid signal */
1752 if (status
& FE_HAS_LOCK
)
1753 return DVBFE_ALGO_SEARCH_SUCCESS
;
1755 return DVBFE_ALGO_SEARCH_AGAIN
;
1758 dbg_info("failed (%d)\n", ret
);
1759 return DVBFE_ALGO_SEARCH_ERROR
;
1762 static void lgdt3306a_release(struct dvb_frontend
*fe
)
1764 struct lgdt3306a_state
*state
= fe
->demodulator_priv
;
1770 static const struct dvb_frontend_ops lgdt3306a_ops
;
1772 struct dvb_frontend
*lgdt3306a_attach(const struct lgdt3306a_config
*config
,
1773 struct i2c_adapter
*i2c_adap
)
1775 struct lgdt3306a_state
*state
= NULL
;
1779 dbg_info("(%d-%04x)\n",
1780 i2c_adap
? i2c_adapter_id(i2c_adap
) : 0,
1781 config
? config
->i2c_addr
: 0);
1783 state
= kzalloc(sizeof(struct lgdt3306a_state
), GFP_KERNEL
);
1787 state
->cfg
= config
;
1788 state
->i2c_adap
= i2c_adap
;
1790 memcpy(&state
->frontend
.ops
, &lgdt3306a_ops
,
1791 sizeof(struct dvb_frontend_ops
));
1792 state
->frontend
.demodulator_priv
= state
;
1794 /* verify that we're talking to a lg3306a */
1795 /* FGR - NOTE - there is no obvious ChipId to check; we check
1796 * some "known" bits after reset, but it's still just a guess */
1797 ret
= lgdt3306a_read_reg(state
, 0x0000, &val
);
1800 if ((val
& 0x74) != 0x74) {
1801 pr_warn("expected 0x74, got 0x%x\n", (val
& 0x74));
1803 /* FIXME - re-enable when we know this is right */
1807 ret
= lgdt3306a_read_reg(state
, 0x0001, &val
);
1810 if ((val
& 0xf6) != 0xc6) {
1811 pr_warn("expected 0xc6, got 0x%x\n", (val
& 0xf6));
1813 /* FIXME - re-enable when we know this is right */
1817 ret
= lgdt3306a_read_reg(state
, 0x0002, &val
);
1820 if ((val
& 0x73) != 0x03) {
1821 pr_warn("expected 0x03, got 0x%x\n", (val
& 0x73));
1823 /* FIXME - re-enable when we know this is right */
1828 state
->current_frequency
= -1;
1829 state
->current_modulation
= -1;
1831 lgdt3306a_sleep(state
);
1833 return &state
->frontend
;
1836 pr_warn("unable to detect LGDT3306A hardware\n");
1840 EXPORT_SYMBOL(lgdt3306a_attach
);
1844 static const short regtab
[] = {
1845 0x0000, /* SOFTRSTB 1'b1 1'b1 1'b1 ADCPDB 1'b1 PLLPDB GBBPDB 11111111 */
1846 0x0001, /* 1'b1 1'b1 1'b0 1'b0 AUTORPTRS */
1847 0x0002, /* NI2CRPTEN 1'b0 1'b0 1'b0 SPECINVAUT */
1848 0x0003, /* AGCRFOUT */
1849 0x0004, /* ADCSEL1V ADCCNT ADCCNF ADCCNS ADCCLKPLL */
1850 0x0005, /* PLLINDIVSE */
1851 0x0006, /* PLLCTRL[7:0] 11100001 */
1852 0x0007, /* SYSINITWAITTIME[7:0] (msec) 00001000 */
1853 0x0008, /* STDOPMODE[7:0] 10000000 */
1854 0x0009, /* 1'b0 1'b0 1'b0 STDOPDETTMODE[2:0] STDOPDETCMODE[1:0] 00011110 */
1855 0x000a, /* DAFTEN 1'b1 x x SCSYSLOCK */
1856 0x000b, /* SCSYSLOCKCHKTIME[7:0] (10msec) 01100100 */
1857 0x000d, /* x SAMPLING4 */
1858 0x000e, /* SAMFREQ[15:8] 00000000 */
1859 0x000f, /* SAMFREQ[7:0] 00000000 */
1860 0x0010, /* IFFREQ[15:8] 01100000 */
1861 0x0011, /* IFFREQ[7:0] 00000000 */
1862 0x0012, /* AGCEN AGCREFMO */
1863 0x0013, /* AGCRFFIXB AGCIFFIXB AGCLOCKDETRNGSEL[1:0] 1'b1 1'b0 1'b0 1'b0 11101000 */
1864 0x0014, /* AGCFIXVALUE[7:0] 01111111 */
1865 0x0015, /* AGCREF[15:8] 00001010 */
1866 0x0016, /* AGCREF[7:0] 11100100 */
1867 0x0017, /* AGCDELAY[7:0] 00100000 */
1868 0x0018, /* AGCRFBW[3:0] AGCIFBW[3:0] 10001000 */
1869 0x0019, /* AGCUDOUTMODE[1:0] AGCUDCTRLLEN[1:0] AGCUDCTRL */
1870 0x001c, /* 1'b1 PFEN MFEN AICCVSYNC */
1871 0x001d, /* 1'b0 1'b1 1'b0 1'b1 AICCVSYNC */
1872 0x001e, /* AICCALPHA[3:0] 1'b1 1'b0 1'b1 1'b0 01111010 */
1873 0x001f, /* AICCDETTH[19:16] AICCOFFTH[19:16] 00000000 */
1874 0x0020, /* AICCDETTH[15:8] 01111100 */
1875 0x0021, /* AICCDETTH[7:0] 00000000 */
1876 0x0022, /* AICCOFFTH[15:8] 00000101 */
1877 0x0023, /* AICCOFFTH[7:0] 11100000 */
1878 0x0024, /* AICCOPMODE3[1:0] AICCOPMODE2[1:0] AICCOPMODE1[1:0] AICCOPMODE0[1:0] 00000000 */
1879 0x0025, /* AICCFIXFREQ3[23:16] 00000000 */
1880 0x0026, /* AICCFIXFREQ3[15:8] 00000000 */
1881 0x0027, /* AICCFIXFREQ3[7:0] 00000000 */
1882 0x0028, /* AICCFIXFREQ2[23:16] 00000000 */
1883 0x0029, /* AICCFIXFREQ2[15:8] 00000000 */
1884 0x002a, /* AICCFIXFREQ2[7:0] 00000000 */
1885 0x002b, /* AICCFIXFREQ1[23:16] 00000000 */
1886 0x002c, /* AICCFIXFREQ1[15:8] 00000000 */
1887 0x002d, /* AICCFIXFREQ1[7:0] 00000000 */
1888 0x002e, /* AICCFIXFREQ0[23:16] 00000000 */
1889 0x002f, /* AICCFIXFREQ0[15:8] 00000000 */
1890 0x0030, /* AICCFIXFREQ0[7:0] 00000000 */
1891 0x0031, /* 1'b0 1'b1 1'b0 1'b0 x DAGC1STER */
1892 0x0032, /* DAGC1STEN DAGC1STER */
1893 0x0033, /* DAGC1STREF[15:8] 00001010 */
1894 0x0034, /* DAGC1STREF[7:0] 11100100 */
1895 0x0035, /* DAGC2NDE */
1896 0x0036, /* DAGC2NDREF[15:8] 00001010 */
1897 0x0037, /* DAGC2NDREF[7:0] 10000000 */
1898 0x0038, /* DAGC2NDLOCKDETRNGSEL[1:0] */
1899 0x003d, /* 1'b1 SAMGEARS */
1900 0x0040, /* SAMLFGMA */
1901 0x0041, /* SAMLFBWM */
1902 0x0044, /* 1'b1 CRGEARSHE */
1903 0x0045, /* CRLFGMAN */
1904 0x0046, /* CFLFBWMA */
1905 0x0047, /* CRLFGMAN */
1906 0x0048, /* x x x x CRLFGSTEP_VS[3:0] xxxx1001 */
1907 0x0049, /* CRLFBWMA */
1908 0x004a, /* CRLFBWMA */
1909 0x0050, /* 1'b0 1'b1 1'b1 1'b0 MSECALCDA */
1910 0x0070, /* TPOUTEN TPIFEN TPCLKOUTE */
1911 0x0071, /* TPSENB TPSSOPBITE */
1912 0x0073, /* TP47HINS x x CHBERINT PERMODE[1:0] PERINT[1:0] 1xx11100 */
1913 0x0075, /* x x x x x IQSWAPCTRL[2:0] xxxxx000 */
1914 0x0076, /* NBERCON NBERST NBERPOL NBERWSYN */
1915 0x0077, /* x NBERLOSTTH[2:0] NBERACQTH[3:0] x0000000 */
1916 0x0078, /* NBERPOLY[31:24] 00000000 */
1917 0x0079, /* NBERPOLY[23:16] 00000000 */
1918 0x007a, /* NBERPOLY[15:8] 00000000 */
1919 0x007b, /* NBERPOLY[7:0] 00000000 */
1920 0x007c, /* NBERPED[31:24] 00000000 */
1921 0x007d, /* NBERPED[23:16] 00000000 */
1922 0x007e, /* NBERPED[15:8] 00000000 */
1923 0x007f, /* NBERPED[7:0] 00000000 */
1924 0x0080, /* x AGCLOCK DAGCLOCK SYSLOCK x x NEVERLOCK[1:0] */
1925 0x0085, /* SPECINVST */
1926 0x0088, /* SYSLOCKTIME[15:8] */
1927 0x0089, /* SYSLOCKTIME[7:0] */
1928 0x008c, /* FECLOCKTIME[15:8] */
1929 0x008d, /* FECLOCKTIME[7:0] */
1930 0x008e, /* AGCACCOUT[15:8] */
1931 0x008f, /* AGCACCOUT[7:0] */
1932 0x0090, /* AICCREJSTATUS[3:0] AICCREJBUSY[3:0] */
1933 0x0091, /* AICCVSYNC */
1934 0x009c, /* CARRFREQOFFSET[15:8] */
1935 0x009d, /* CARRFREQOFFSET[7:0] */
1936 0x00a1, /* SAMFREQOFFSET[23:16] */
1937 0x00a2, /* SAMFREQOFFSET[15:8] */
1938 0x00a3, /* SAMFREQOFFSET[7:0] */
1939 0x00a6, /* SYNCLOCK SYNCLOCKH */
1940 #if 0 /* covered elsewhere */
1941 0x00e8, /* CONSTPWR[15:8] */
1942 0x00e9, /* CONSTPWR[7:0] */
1943 0x00ea, /* BMSE[15:8] */
1944 0x00eb, /* BMSE[7:0] */
1945 0x00ec, /* MSE[15:8] */
1946 0x00ed, /* MSE[7:0] */
1947 0x00ee, /* CONSTI[7:0] */
1948 0x00ef, /* CONSTQ[7:0] */
1950 0x00f4, /* TPIFTPERRCNT[7:0] */
1951 0x00f5, /* TPCORREC */
1952 0x00f6, /* VBBER[15:8] */
1953 0x00f7, /* VBBER[7:0] */
1954 0x00f8, /* VABER[15:8] */
1955 0x00f9, /* VABER[7:0] */
1956 0x00fa, /* TPERRCNT[7:0] */
1957 0x00fb, /* NBERLOCK x x x x x x x */
1958 0x00fc, /* NBERVALUE[31:24] */
1959 0x00fd, /* NBERVALUE[23:16] */
1960 0x00fe, /* NBERVALUE[15:8] */
1961 0x00ff, /* NBERVALUE[7:0] */
1962 0x1000, /* 1'b0 WODAGCOU */
1963 0x1005, /* x x 1'b1 1'b1 x SRD_Q_QM */
1964 0x1009, /* SRDWAITTIME[7:0] (10msec) 00100011 */
1965 0x100a, /* SRDWAITTIME_CQS[7:0] (msec) 01100100 */
1966 0x101a, /* x 1'b1 1'b0 1'b0 x QMDQAMMODE[2:0] x100x010 */
1967 0x1036, /* 1'b0 1'b1 1'b0 1'b0 SAMGSEND_CQS[3:0] 01001110 */
1968 0x103c, /* SAMGSAUTOSTL_V[3:0] SAMGSAUTOEDL_V[3:0] 01000110 */
1969 0x103d, /* 1'b1 1'b1 SAMCNORMBP_V[1:0] 1'b0 1'b0 SAMMODESEL_V[1:0] 11100001 */
1970 0x103f, /* SAMZTEDSE */
1971 0x105d, /* EQSTATUSE */
1972 0x105f, /* x PMAPG2_V[2:0] x DMAPG2_V[2:0] x001x011 */
1973 0x1060, /* 1'b1 EQSTATUSE */
1974 0x1061, /* CRMAPBWSTL_V[3:0] CRMAPBWEDL_V[3:0] 00000100 */
1975 0x1065, /* 1'b0 x CRMODE_V[1:0] 1'b1 x 1'b1 x 0x111x1x */
1976 0x1066, /* 1'b0 1'b0 1'b1 1'b0 1'b1 PNBOOSTSE */
1977 0x1068, /* CREPHNGAIN2_V[3:0] CREPHNPBW_V[3:0] 10010001 */
1978 0x106e, /* x x x x x CREPHNEN_ */
1979 0x106f, /* CREPHNTH_V[7:0] 00010101 */
1980 0x1072, /* CRSWEEPN */
1981 0x1073, /* CRPGAIN_V[3:0] x x 1'b1 1'b1 1001xx11 */
1982 0x1074, /* CRPBW_V[3:0] x x 1'b1 1'b1 0001xx11 */
1983 0x1080, /* DAFTSTATUS[1:0] x x x x x x */
1984 0x1081, /* SRDSTATUS[1:0] x x x x x SRDLOCK */
1985 0x10a9, /* EQSTATUS_CQS[1:0] x x x x x x */
1986 0x10b7, /* EQSTATUS_V[1:0] x x x x x x */
1987 #if 0 /* SMART_ANT */
1988 0x1f00, /* MODEDETE */
1989 0x1f01, /* x x x x x x x SFNRST xxxxxxx0 */
1990 0x1f03, /* NUMOFANT[7:0] 10000000 */
1991 0x1f04, /* x SELMASK[6:0] x0000000 */
1992 0x1f05, /* x SETMASK[6:0] x0000000 */
1993 0x1f06, /* x TXDATA[6:0] x0000000 */
1994 0x1f07, /* x CHNUMBER[6:0] x0000000 */
1995 0x1f09, /* AGCTIME[23:16] 10011000 */
1996 0x1f0a, /* AGCTIME[15:8] 10010110 */
1997 0x1f0b, /* AGCTIME[7:0] 10000000 */
1998 0x1f0c, /* ANTTIME[31:24] 00000000 */
1999 0x1f0d, /* ANTTIME[23:16] 00000011 */
2000 0x1f0e, /* ANTTIME[15:8] 10010000 */
2001 0x1f0f, /* ANTTIME[7:0] 10010000 */
2002 0x1f11, /* SYNCTIME[23:16] 10011000 */
2003 0x1f12, /* SYNCTIME[15:8] 10010110 */
2004 0x1f13, /* SYNCTIME[7:0] 10000000 */
2005 0x1f14, /* SNRTIME[31:24] 00000001 */
2006 0x1f15, /* SNRTIME[23:16] 01111101 */
2007 0x1f16, /* SNRTIME[15:8] 01111000 */
2008 0x1f17, /* SNRTIME[7:0] 01000000 */
2009 0x1f19, /* FECTIME[23:16] 00000000 */
2010 0x1f1a, /* FECTIME[15:8] 01110010 */
2011 0x1f1b, /* FECTIME[7:0] 01110000 */
2012 0x1f1d, /* FECTHD[7:0] 00000011 */
2013 0x1f1f, /* SNRTHD[23:16] 00001000 */
2014 0x1f20, /* SNRTHD[15:8] 01111111 */
2015 0x1f21, /* SNRTHD[7:0] 10000101 */
2016 0x1f80, /* IRQFLG x x SFSDRFLG MODEBFLG SAVEFLG SCANFLG TRACKFLG */
2017 0x1f81, /* x SYNCCON SNRCON FECCON x STDBUSY SYNCRST AGCFZCO */
2018 0x1f82, /* x x x SCANOPCD[4:0] */
2019 0x1f83, /* x x x x MAINOPCD[3:0] */
2020 0x1f84, /* x x RXDATA[13:8] */
2021 0x1f85, /* RXDATA[7:0] */
2022 0x1f86, /* x x SDTDATA[13:8] */
2023 0x1f87, /* SDTDATA[7:0] */
2024 0x1f89, /* ANTSNR[23:16] */
2025 0x1f8a, /* ANTSNR[15:8] */
2026 0x1f8b, /* ANTSNR[7:0] */
2027 0x1f8c, /* x x x x ANTFEC[13:8] */
2028 0x1f8d, /* ANTFEC[7:0] */
2029 0x1f8e, /* MAXCNT[7:0] */
2030 0x1f8f, /* SCANCNT[7:0] */
2031 0x1f91, /* MAXPW[23:16] */
2032 0x1f92, /* MAXPW[15:8] */
2033 0x1f93, /* MAXPW[7:0] */
2034 0x1f95, /* CURPWMSE[23:16] */
2035 0x1f96, /* CURPWMSE[15:8] */
2036 0x1f97, /* CURPWMSE[7:0] */
2037 #endif /* SMART_ANT */
2038 0x211f, /* 1'b1 1'b1 1'b1 CIRQEN x x 1'b0 1'b0 1111xx00 */
2039 0x212a, /* EQAUTOST */
2040 0x2122, /* CHFAST[7:0] 01100000 */
2041 0x212b, /* FFFSTEP_V[3:0] x FBFSTEP_V[2:0] 0001x001 */
2042 0x212c, /* PHDEROTBWSEL[3:0] 1'b1 1'b1 1'b1 1'b0 10001110 */
2043 0x212d, /* 1'b1 1'b1 1'b1 1'b1 x x TPIFLOCKS */
2044 0x2135, /* DYNTRACKFDEQ[3:0] x 1'b0 1'b0 1'b0 1010x000 */
2045 0x2141, /* TRMODE[1:0] 1'b1 1'b1 1'b0 1'b1 1'b1 1'b1 01110111 */
2046 0x2162, /* AICCCTRLE */
2047 0x2173, /* PHNCNFCNT[7:0] 00000100 */
2048 0x2179, /* 1'b0 1'b0 1'b0 1'b1 x BADSINGLEDYNTRACKFBF[2:0] 0001x001 */
2049 0x217a, /* 1'b0 1'b0 1'b0 1'b1 x BADSLOWSINGLEDYNTRACKFBF[2:0] 0001x001 */
2050 0x217e, /* CNFCNTTPIF[7:0] 00001000 */
2051 0x217f, /* TPERRCNTTPIF[7:0] 00000001 */
2052 0x2180, /* x x x x x x FBDLYCIR[9:8] */
2053 0x2181, /* FBDLYCIR[7:0] */
2054 0x2185, /* MAXPWRMAIN[7:0] */
2055 0x2191, /* NCOMBDET x x x x x x x */
2056 0x2199, /* x MAINSTRON */
2057 0x219a, /* FFFEQSTEPOUT_V[3:0] FBFSTEPOUT_V[2:0] */
2058 0x21a1, /* x x SNRREF[5:0] */
2059 0x2845, /* 1'b0 1'b1 x x FFFSTEP_CQS[1:0] FFFCENTERTAP[1:0] 01xx1110 */
2060 0x2846, /* 1'b0 x 1'b0 1'b1 FBFSTEP_CQS[1:0] 1'b1 1'b0 0x011110 */
2061 0x2847, /* ENNOSIGDE */
2062 0x2849, /* 1'b1 1'b1 NOUSENOSI */
2063 0x284a, /* EQINITWAITTIME[7:0] 01100100 */
2064 0x3000, /* 1'b1 1'b1 1'b1 x x x 1'b0 RPTRSTM */
2065 0x3001, /* RPTRSTWAITTIME[7:0] (100msec) 00110010 */
2066 0x3031, /* FRAMELOC */
2067 0x3032, /* 1'b1 1'b0 1'b0 1'b0 x x FRAMELOCKMODE_CQS[1:0] 1000xx11 */
2068 0x30a9, /* VDLOCK_Q FRAMELOCK */
2069 0x30aa, /* MPEGLOCK */
2072 #define numDumpRegs (sizeof(regtab)/sizeof(regtab[0]))
2073 static u8 regval1
[numDumpRegs
] = {0, };
2074 static u8 regval2
[numDumpRegs
] = {0, };
2076 static void lgdt3306a_DumpAllRegs(struct lgdt3306a_state
*state
)
2078 memset(regval2
, 0xff, sizeof(regval2
));
2079 lgdt3306a_DumpRegs(state
);
2082 static void lgdt3306a_DumpRegs(struct lgdt3306a_state
*state
)
2085 int sav_debug
= debug
;
2087 if ((debug
& DBG_DUMP
) == 0)
2089 debug
&= ~DBG_REG
; /* suppress DBG_REG during reg dump */
2093 for (i
= 0; i
< numDumpRegs
; i
++) {
2094 lgdt3306a_read_reg(state
, regtab
[i
], ®val1
[i
]);
2095 if (regval1
[i
] != regval2
[i
]) {
2096 lg_debug(" %04X = %02X\n", regtab
[i
], regval1
[i
]);
2097 regval2
[i
] = regval1
[i
];
2102 #endif /* DBG_DUMP */
2106 static const struct dvb_frontend_ops lgdt3306a_ops
= {
2107 .delsys
= { SYS_ATSC
, SYS_DVBC_ANNEX_B
},
2109 .name
= "LG Electronics LGDT3306A VSB/QAM Frontend",
2110 .frequency_min
= 54000000,
2111 .frequency_max
= 858000000,
2112 .frequency_stepsize
= 62500,
2113 .caps
= FE_CAN_QAM_64
| FE_CAN_QAM_256
| FE_CAN_8VSB
2115 .i2c_gate_ctrl
= lgdt3306a_i2c_gate_ctrl
,
2116 .init
= lgdt3306a_init
,
2117 .sleep
= lgdt3306a_fe_sleep
,
2118 /* if this is set, it overrides the default swzigzag */
2119 .tune
= lgdt3306a_tune
,
2120 .set_frontend
= lgdt3306a_set_parameters
,
2121 .get_frontend
= lgdt3306a_get_frontend
,
2122 .get_frontend_algo
= lgdt3306a_get_frontend_algo
,
2123 .get_tune_settings
= lgdt3306a_get_tune_settings
,
2124 .read_status
= lgdt3306a_read_status
,
2125 .read_ber
= lgdt3306a_read_ber
,
2126 .read_signal_strength
= lgdt3306a_read_signal_strength
,
2127 .read_snr
= lgdt3306a_read_snr
,
2128 .read_ucblocks
= lgdt3306a_read_ucblocks
,
2129 .release
= lgdt3306a_release
,
2130 .ts_bus_ctrl
= lgdt3306a_ts_bus_ctrl
,
2131 .search
= lgdt3306a_search
,
2134 MODULE_DESCRIPTION("LG Electronics LGDT3306A ATSC/QAM-B Demodulator Driver");
2135 MODULE_AUTHOR("Fred Richter <frichter@hauppauge.com>");
2136 MODULE_LICENSE("GPL");
2137 MODULE_VERSION("0.2");