2 * Support for Legend Silicon GB20600 (a.k.a DMB-TH) demodulator
3 * LGS8913, LGS8GL5, LGS8G75
4 * experimental support LGS8G42, LGS8G52
6 * Copyright (C) 2007-2009 David T.L. Wong <davidtlwong@gmail.com>
7 * Copyright (C) 2008 Sirius International (Hong Kong) Limited
8 * Timothy Lee <timothy.lee@siriushk.com> (for initial work on LGS8GL5)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <asm/div64.h>
28 #include "dvb_frontend.h"
31 #include "lgs8gxx_priv.h"
33 #define dprintk(args...) \
36 printk(KERN_DEBUG "lgs8gxx: " args); \
40 static int fake_signal_str
= 1;
42 module_param(debug
, int, 0644);
43 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
45 module_param(fake_signal_str
, int, 0644);
46 MODULE_PARM_DESC(fake_signal_str
, "fake signal strength for LGS8913."
47 "Signal strength calculation is slow.(default:on).");
49 static const u8 lgs8g75_initdat
[] = {
50 0x01, 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
51 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
52 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
53 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
54 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
55 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
56 0xE4, 0xF5, 0xA8, 0xF5, 0xB8, 0xF5, 0x88, 0xF5,
57 0x89, 0xF5, 0x87, 0x75, 0xD0, 0x00, 0x11, 0x50,
58 0x11, 0x50, 0xF4, 0xF5, 0x80, 0xF5, 0x90, 0xF5,
59 0xA0, 0xF5, 0xB0, 0x75, 0x81, 0x30, 0x80, 0x01,
60 0x32, 0x90, 0x80, 0x12, 0x74, 0xFF, 0xF0, 0x90,
61 0x80, 0x13, 0x74, 0x1F, 0xF0, 0x90, 0x80, 0x23,
62 0x74, 0x01, 0xF0, 0x90, 0x80, 0x22, 0xF0, 0x90,
63 0x00, 0x48, 0x74, 0x00, 0xF0, 0x90, 0x80, 0x4D,
64 0x74, 0x05, 0xF0, 0x90, 0x80, 0x09, 0xE0, 0x60,
65 0x21, 0x12, 0x00, 0xDD, 0x14, 0x60, 0x1B, 0x12,
66 0x00, 0xDD, 0x14, 0x60, 0x15, 0x12, 0x00, 0xDD,
67 0x14, 0x60, 0x0F, 0x12, 0x00, 0xDD, 0x14, 0x60,
68 0x09, 0x12, 0x00, 0xDD, 0x14, 0x60, 0x03, 0x12,
69 0x00, 0xDD, 0x90, 0x80, 0x42, 0xE0, 0x60, 0x0B,
70 0x14, 0x60, 0x0C, 0x14, 0x60, 0x0D, 0x14, 0x60,
71 0x0E, 0x01, 0xB3, 0x74, 0x04, 0x01, 0xB9, 0x74,
72 0x05, 0x01, 0xB9, 0x74, 0x07, 0x01, 0xB9, 0x74,
73 0x0A, 0xC0, 0xE0, 0x74, 0xC8, 0x12, 0x00, 0xE2,
74 0xD0, 0xE0, 0x14, 0x70, 0xF4, 0x90, 0x80, 0x09,
75 0xE0, 0x70, 0xAE, 0x12, 0x00, 0xF6, 0x12, 0x00,
76 0xFE, 0x90, 0x00, 0x48, 0xE0, 0x04, 0xF0, 0x90,
77 0x80, 0x4E, 0xF0, 0x01, 0x73, 0x90, 0x80, 0x08,
78 0xF0, 0x22, 0xF8, 0x7A, 0x0C, 0x79, 0xFD, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD9,
80 0xF6, 0xDA, 0xF2, 0xD8, 0xEE, 0x22, 0x90, 0x80,
81 0x65, 0xE0, 0x54, 0xFD, 0xF0, 0x22, 0x90, 0x80,
82 0x65, 0xE0, 0x44, 0xC2, 0xF0, 0x22
85 /* LGS8GXX internal helper functions */
87 static int lgs8gxx_write_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8 data
)
90 u8 buf
[] = { reg
, data
};
91 struct i2c_msg msg
= { .flags
= 0, .buf
= buf
, .len
= 2 };
93 msg
.addr
= priv
->config
->demod_address
;
94 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
98 printk(KERN_DEBUG
"%s: reg=0x%02X, data=0x%02X\n",
101 ret
= i2c_transfer(priv
->i2c
, &msg
, 1);
104 dprintk(KERN_DEBUG
"%s: error reg=0x%x, data=0x%x, ret=%i\n",
105 __func__
, reg
, data
, ret
);
107 return (ret
!= 1) ? -1 : 0;
110 static int lgs8gxx_read_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8
*p_data
)
117 struct i2c_msg msg
[] = {
118 { .flags
= 0, .buf
= b0
, .len
= 1 },
119 { .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 },
122 dev_addr
= priv
->config
->demod_address
;
123 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
125 msg
[1].addr
= msg
[0].addr
= dev_addr
;
127 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
129 dprintk(KERN_DEBUG
"%s: error reg=0x%x, ret=%i\n",
136 printk(KERN_DEBUG
"%s: reg=0x%02X, data=0x%02X\n",
137 __func__
, reg
, b1
[0]);
141 static int lgs8gxx_soft_reset(struct lgs8gxx_state
*priv
)
143 lgs8gxx_write_reg(priv
, 0x02, 0x00);
145 lgs8gxx_write_reg(priv
, 0x02, 0x01);
151 static int wait_reg_mask(struct lgs8gxx_state
*priv
, u8 reg
, u8 mask
,
152 u8 val
, u8 delay
, u8 tries
)
157 for (i
= 0; i
< tries
; i
++) {
158 lgs8gxx_read_reg(priv
, reg
, &t
);
160 if ((t
& mask
) == val
)
168 static int lgs8gxx_set_ad_mode(struct lgs8gxx_state
*priv
)
170 const struct lgs8gxx_config
*config
= priv
->config
;
173 if_conf
= 0x10; /* AGC output on, RF_AGC output off; */
176 ((config
->ext_adc
) ? 0x80 : 0x00) |
177 ((config
->if_neg_center
) ? 0x04 : 0x00) |
178 ((config
->if_freq
== 0) ? 0x08 : 0x00) | /* Baseband */
179 ((config
->adc_signed
) ? 0x02 : 0x00) |
180 ((config
->if_neg_edge
) ? 0x01 : 0x00);
182 if (config
->ext_adc
&&
183 (config
->prod
== LGS8GXX_PROD_LGS8G52
)) {
184 lgs8gxx_write_reg(priv
, 0xBA, 0x40);
187 lgs8gxx_write_reg(priv
, 0x07, if_conf
);
192 static int lgs8gxx_set_if_freq(struct lgs8gxx_state
*priv
, u32 freq
/*in kHz*/)
198 if_clk
= priv
->config
->if_clk_freq
;
205 v32
= val
& 0xFFFFFFFF;
206 dprintk("Set IF Freq to %dkHz\n", freq
);
209 dprintk("Set IF Freq to baseband\n");
211 dprintk("AFC_INIT_FREQ = 0x%08X\n", v32
);
213 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
214 lgs8gxx_write_reg(priv
, 0x08, 0xFF & (v32
));
215 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
>> 8));
216 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 16));
217 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 24));
219 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
));
220 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 8));
221 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 16));
222 lgs8gxx_write_reg(priv
, 0x0C, 0xFF & (v32
>> 24));
228 static int lgs8gxx_get_afc_phase(struct lgs8gxx_state
*priv
)
235 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
240 for (i
= 0; i
< 4; i
++) {
241 lgs8gxx_read_reg(priv
, reg_addr
, &t
);
248 val
*= priv
->config
->if_clk_freq
;
250 dprintk("AFC = %u kHz\n", (u32
)val
);
254 static int lgs8gxx_set_mode_auto(struct lgs8gxx_state
*priv
)
257 u8 prod
= priv
->config
->prod
;
259 if (prod
== LGS8GXX_PROD_LGS8913
)
260 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
262 if (prod
== LGS8GXX_PROD_LGS8G75
) {
263 lgs8gxx_read_reg(priv
, 0x0C, &t
);
265 lgs8gxx_write_reg(priv
, 0x0C, t
| 0x80);
266 lgs8gxx_write_reg(priv
, 0x39, 0x00);
267 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
268 } else if (prod
== LGS8GXX_PROD_LGS8913
||
269 prod
== LGS8GXX_PROD_LGS8GL5
||
270 prod
== LGS8GXX_PROD_LGS8G42
||
271 prod
== LGS8GXX_PROD_LGS8G52
||
272 prod
== LGS8GXX_PROD_LGS8G54
) {
273 lgs8gxx_read_reg(priv
, 0x7E, &t
);
274 lgs8gxx_write_reg(priv
, 0x7E, t
| 0x01);
276 /* clear FEC self reset */
277 lgs8gxx_read_reg(priv
, 0xC5, &t
);
278 lgs8gxx_write_reg(priv
, 0xC5, t
& 0xE0);
281 if (prod
== LGS8GXX_PROD_LGS8913
) {
282 /* FEC auto detect */
283 lgs8gxx_write_reg(priv
, 0xC1, 0x03);
285 lgs8gxx_read_reg(priv
, 0x7C, &t
);
286 t
= (t
& 0x8C) | 0x03;
287 lgs8gxx_write_reg(priv
, 0x7C, t
);
290 lgs8gxx_read_reg(priv
, 0xC3, &t
);
291 t
= (t
& 0xEF) | 0x10;
292 lgs8gxx_write_reg(priv
, 0xC3, t
);
295 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G52
)
296 lgs8gxx_write_reg(priv
, 0xD9, 0x40);
301 static int lgs8gxx_set_mode_manual(struct lgs8gxx_state
*priv
)
306 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
308 lgs8gxx_read_reg(priv
, 0x0C, &t
);
310 lgs8gxx_write_reg(priv
, 0x0C, t
);
312 lgs8gxx_read_reg(priv
, 0x0C, &t
);
313 lgs8gxx_read_reg(priv
, 0x19, &t2
);
315 if (((t
&0x03) == 0x01) && (t2
&0x01)) {
316 lgs8gxx_write_reg(priv
, 0x6E, 0x05);
317 lgs8gxx_write_reg(priv
, 0x39, 0x02);
318 lgs8gxx_write_reg(priv
, 0x39, 0x03);
319 lgs8gxx_write_reg(priv
, 0x3D, 0x05);
320 lgs8gxx_write_reg(priv
, 0x3E, 0x28);
321 lgs8gxx_write_reg(priv
, 0x53, 0x80);
323 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
324 lgs8gxx_write_reg(priv
, 0x39, 0x00);
325 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
328 lgs8gxx_soft_reset(priv
);
332 /* turn off auto-detect; manual settings */
333 lgs8gxx_write_reg(priv
, 0x7E, 0);
334 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
335 lgs8gxx_write_reg(priv
, 0xC1, 0);
337 ret
= lgs8gxx_read_reg(priv
, 0xC5, &t
);
338 t
= (t
& 0xE0) | 0x06;
339 lgs8gxx_write_reg(priv
, 0xC5, t
);
341 lgs8gxx_soft_reset(priv
);
346 static int lgs8gxx_is_locked(struct lgs8gxx_state
*priv
, u8
*locked
)
351 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
352 ret
= lgs8gxx_read_reg(priv
, 0x13, &t
);
354 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
358 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
359 *locked
= ((t
& 0x80) == 0x80) ? 1 : 0;
361 *locked
= ((t
& 0xC0) == 0xC0) ? 1 : 0;
365 /* Wait for Code Acquisition Lock */
366 static int lgs8gxx_wait_ca_lock(struct lgs8gxx_state
*priv
, u8
*locked
)
371 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
381 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 50, 40);
382 *locked
= (ret
== 0) ? 1 : 0;
387 static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state
*priv
,
393 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
403 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 10, 20);
404 *finished
= (ret
== 0) ? 1 : 0;
409 static int lgs8gxx_autolock_gi(struct lgs8gxx_state
*priv
, u8 gi
, u8 cpn
,
417 dprintk("try GI 945\n");
418 else if (gi
== GI_595
)
419 dprintk("try GI 595\n");
420 else if (gi
== GI_420
)
421 dprintk("try GI 420\n");
422 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
423 lgs8gxx_read_reg(priv
, 0x0C, &t1
);
424 lgs8gxx_read_reg(priv
, 0x18, &t2
);
428 t2
|= cpn
? 0x01 : 0x00;
429 lgs8gxx_write_reg(priv
, 0x0C, t1
);
430 lgs8gxx_write_reg(priv
, 0x18, t2
);
432 lgs8gxx_write_reg(priv
, 0x04, gi
);
434 lgs8gxx_soft_reset(priv
);
435 err
= lgs8gxx_wait_ca_lock(priv
, locked
);
436 if (err
|| !(*locked
))
438 err
= lgs8gxx_is_autodetect_finished(priv
, &ad_fini
);
442 dprintk("auto detect finished\n");
449 static int lgs8gxx_auto_detect(struct lgs8gxx_state
*priv
,
450 u8
*detected_param
, u8
*gi
)
454 u8 locked
= 0, tmp_gi
;
456 dprintk("%s\n", __func__
);
458 lgs8gxx_set_mode_auto(priv
);
459 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
460 lgs8gxx_write_reg(priv
, 0x67, 0xAA);
461 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
464 lgs8gxx_write_reg(priv
, 0x03, 00);
467 for (i
= 0; i
< 2; i
++) {
468 for (j
= 0; j
< 2; j
++) {
470 err
= lgs8gxx_autolock_gi(priv
, GI_945
, j
, &locked
);
476 for (j
= 0; j
< 2; j
++) {
478 err
= lgs8gxx_autolock_gi(priv
, GI_420
, j
, &locked
);
485 err
= lgs8gxx_autolock_gi(priv
, GI_595
, 1, &locked
);
493 if ((err
== 0) && (locked
== 1)) {
496 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
) {
497 lgs8gxx_read_reg(priv
, 0xA2, &t
);
500 lgs8gxx_read_reg(priv
, 0x1F, &t
);
501 *detected_param
= t
& 0x3F;
504 if (tmp_gi
== GI_945
)
505 dprintk("GI 945 locked\n");
506 else if (tmp_gi
== GI_595
)
507 dprintk("GI 595 locked\n");
508 else if (tmp_gi
== GI_420
)
509 dprintk("GI 420 locked\n");
519 static void lgs8gxx_auto_lock(struct lgs8gxx_state
*priv
)
523 u8 detected_param
= 0;
525 err
= lgs8gxx_auto_detect(priv
, &detected_param
, &gi
);
528 dprintk("lgs8gxx_auto_detect failed\n");
530 dprintk("detected param = 0x%02X\n", detected_param
);
532 /* Apply detected parameters */
533 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
534 u8 inter_leave_len
= detected_param
& TIM_MASK
;
535 /* Fix 8913 time interleaver detection bug */
536 inter_leave_len
= (inter_leave_len
== TIM_MIDDLE
) ? 0x60 : 0x40;
537 detected_param
&= CF_MASK
| SC_MASK
| LGS_FEC_MASK
;
538 detected_param
|= inter_leave_len
;
540 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
542 lgs8gxx_read_reg(priv
, 0x19, &t
);
544 t
|= detected_param
<< 1;
545 lgs8gxx_write_reg(priv
, 0x19, t
);
547 lgs8gxx_write_reg(priv
, 0x7D, detected_param
);
548 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
549 lgs8gxx_write_reg(priv
, 0xC0, detected_param
);
551 /* lgs8gxx_soft_reset(priv); */
553 /* Enter manual mode */
554 lgs8gxx_set_mode_manual(priv
);
558 priv
->curr_gi
= 945; break;
560 priv
->curr_gi
= 595; break;
562 priv
->curr_gi
= 420; break;
564 priv
->curr_gi
= 945; break;
568 static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state
*priv
,
569 u8 serial
, u8 clk_pol
, u8 clk_gated
)
574 reg_addr
= (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) ? 0x30 : 0xC2;
575 ret
= lgs8gxx_read_reg(priv
, reg_addr
, &t
);
580 t
|= serial
? TS_SERIAL
: TS_PARALLEL
;
581 t
|= clk_pol
? TS_CLK_INVERTED
: TS_CLK_NORMAL
;
582 t
|= clk_gated
? TS_CLK_GATED
: TS_CLK_FREERUN
;
584 ret
= lgs8gxx_write_reg(priv
, reg_addr
, t
);
591 /* A/D input peak-to-peak voltage range */
592 static int lgs8g75_set_adc_vpp(struct lgs8gxx_state
*priv
,
595 u8 r26
= 0x73, r27
= 0x90;
597 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
)
600 r26
|= (sel
& 0x01) << 7;
601 r27
|= (sel
& 0x02) >> 1;
602 lgs8gxx_write_reg(priv
, 0x26, r26
);
603 lgs8gxx_write_reg(priv
, 0x27, r27
);
608 /* LGS8913 demod frontend functions */
610 static int lgs8913_init(struct lgs8gxx_state
*priv
)
614 /* LGS8913 specific */
615 lgs8gxx_write_reg(priv
, 0xc1, 0x3);
617 lgs8gxx_read_reg(priv
, 0x7c, &t
);
618 lgs8gxx_write_reg(priv
, 0x7c, (t
&0x8c) | 0x3);
620 /* LGS8913 specific */
621 lgs8gxx_read_reg(priv
, 0xc3, &t
);
622 lgs8gxx_write_reg(priv
, 0xc3, t
&0x10);
628 static int lgs8g75_init_data(struct lgs8gxx_state
*priv
)
630 const u8
*p
= lgs8g75_initdat
;
633 lgs8gxx_write_reg(priv
, 0xC6, 0x40);
635 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
636 lgs8gxx_write_reg(priv
, 0x39, 0x00);
638 lgs8gxx_write_reg(priv
, 0x3A, 0x00);
639 lgs8gxx_write_reg(priv
, 0x38, 0x00);
640 lgs8gxx_write_reg(priv
, 0x3B, 0x00);
641 lgs8gxx_write_reg(priv
, 0x38, 0x00);
643 for (i
= 0; i
< sizeof(lgs8g75_initdat
); i
++) {
644 lgs8gxx_write_reg(priv
, 0x38, 0x00);
645 lgs8gxx_write_reg(priv
, 0x3A, (u8
)(i
&0xff));
646 lgs8gxx_write_reg(priv
, 0x3B, (u8
)(i
>>8));
647 lgs8gxx_write_reg(priv
, 0x3C, *p
);
651 lgs8gxx_write_reg(priv
, 0x38, 0x00);
656 static int lgs8gxx_init(struct dvb_frontend
*fe
)
658 struct lgs8gxx_state
*priv
=
659 (struct lgs8gxx_state
*)fe
->demodulator_priv
;
660 const struct lgs8gxx_config
*config
= priv
->config
;
663 dprintk("%s\n", __func__
);
665 lgs8gxx_read_reg(priv
, 0, &data
);
666 dprintk("reg 0 = 0x%02X\n", data
);
668 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
669 lgs8g75_set_adc_vpp(priv
, config
->adc_vpp
);
671 /* Setup MPEG output format */
672 err
= lgs8gxx_set_mpeg_mode(priv
, config
->serial_ts
,
674 config
->ts_clk_gated
);
678 if (config
->prod
== LGS8GXX_PROD_LGS8913
)
680 lgs8gxx_set_if_freq(priv
, priv
->config
->if_freq
);
681 lgs8gxx_set_ad_mode(priv
);
686 static void lgs8gxx_release(struct dvb_frontend
*fe
)
688 struct lgs8gxx_state
*state
= fe
->demodulator_priv
;
689 dprintk("%s\n", __func__
);
695 static int lgs8gxx_write(struct dvb_frontend
*fe
, u8
*buf
, int len
)
697 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
702 return lgs8gxx_write_reg(priv
, buf
[0], buf
[1]);
705 static int lgs8gxx_set_fe(struct dvb_frontend
*fe
,
706 struct dvb_frontend_parameters
*fe_params
)
708 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
710 dprintk("%s\n", __func__
);
713 if (fe
->ops
.tuner_ops
.set_params
) {
714 fe
->ops
.tuner_ops
.set_params(fe
, fe_params
);
715 if (fe
->ops
.i2c_gate_ctrl
)
716 fe
->ops
.i2c_gate_ctrl(fe
, 0);
719 /* start auto lock */
720 lgs8gxx_auto_lock(priv
);
727 static int lgs8gxx_get_fe(struct dvb_frontend
*fe
,
728 struct dvb_frontend_parameters
*fe_params
)
730 dprintk("%s\n", __func__
);
732 /* TODO: get real readings from device */
733 /* inversion status */
734 fe_params
->inversion
= INVERSION_OFF
;
737 fe_params
->u
.ofdm
.bandwidth
= BANDWIDTH_8_MHZ
;
739 fe_params
->u
.ofdm
.code_rate_HP
= FEC_AUTO
;
740 fe_params
->u
.ofdm
.code_rate_LP
= FEC_AUTO
;
742 fe_params
->u
.ofdm
.constellation
= QAM_AUTO
;
744 /* transmission mode */
745 fe_params
->u
.ofdm
.transmission_mode
= TRANSMISSION_MODE_AUTO
;
748 fe_params
->u
.ofdm
.guard_interval
= GUARD_INTERVAL_AUTO
;
751 fe_params
->u
.ofdm
.hierarchy_information
= HIERARCHY_NONE
;
757 int lgs8gxx_get_tune_settings(struct dvb_frontend
*fe
,
758 struct dvb_frontend_tune_settings
*fesettings
)
760 /* FIXME: copy from tda1004x.c */
761 fesettings
->min_delay_ms
= 800;
762 fesettings
->step_size
= 0;
763 fesettings
->max_drift
= 0;
767 static int lgs8gxx_read_status(struct dvb_frontend
*fe
, fe_status_t
*fe_status
)
769 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
773 dprintk("%s\n", __func__
);
776 lgs8gxx_get_afc_phase(priv
);
777 lgs8gxx_is_locked(priv
, &locked
);
778 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
780 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
781 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
785 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
789 dprintk("Reg 0x4B: 0x%02X\n", t
);
792 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
793 if ((t
& 0x40) == 0x40)
794 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
795 if ((t
& 0x80) == 0x80)
796 *fe_status
|= FE_HAS_VITERBI
| FE_HAS_SYNC
|
799 if ((t
& 0x80) == 0x80)
800 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
801 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
805 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
809 static int lgs8gxx_read_signal_agc(struct lgs8gxx_state
*priv
, u16
*signal
)
814 dprintk("%s()\n", __func__
);
815 lgs8gxx_read_reg(priv
, 0x3F, &agc_lvl
[0]);
816 lgs8gxx_read_reg(priv
, 0x3E, &agc_lvl
[1]);
822 dprintk("agc_lvl: 0x%04X\n", v
);
839 *signal
= cat
* 65535 / 5;
844 static int lgs8913_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
847 s16 max_strength
= 0;
849 u16 i
, gi
= priv
->curr_gi
;
851 dprintk("%s\n", __func__
);
853 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
857 if (fake_signal_str
) {
858 if ((t
& 0xC0) == 0xC0) {
859 dprintk("Fake signal strength\n");
866 dprintk("gi = %d\n", gi
);
867 for (i
= 0; i
< gi
; i
++) {
870 lgs8gxx_write_reg(priv
, 0x84, 0x03 & (i
>> 8));
871 lgs8gxx_write_reg(priv
, 0x83, i
& 0xFF);
873 lgs8gxx_read_reg(priv
, 0x94, &str
);
874 if (max_strength
< str
)
878 *signal
= max_strength
;
879 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
881 lgs8gxx_read_reg(priv
, 0x95, &t
);
882 dprintk("%s: AVG Noise=0x%02X\n", __func__
, t
);
887 static int lgs8g75_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
892 dprintk("%s\n", __func__
);
894 lgs8gxx_read_reg(priv
, 0xB1, &t
);
897 lgs8gxx_read_reg(priv
, 0xB0, &t
);
901 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
906 static int lgs8gxx_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
908 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
910 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
911 return lgs8913_read_signal_strength(priv
, signal
);
912 else if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
913 return lgs8g75_read_signal_strength(priv
, signal
);
915 return lgs8gxx_read_signal_agc(priv
, signal
);
918 static int lgs8gxx_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
920 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
924 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
925 lgs8gxx_read_reg(priv
, 0x34, &t
);
927 lgs8gxx_read_reg(priv
, 0x95, &t
);
928 dprintk("AVG Noise=0x%02X\n", t
);
931 dprintk("snr=0x%x\n", *snr
);
936 static int lgs8gxx_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
939 dprintk("%s: ucblocks=0x%x\n", __func__
, *ucblocks
);
943 static void packet_counter_start(struct lgs8gxx_state
*priv
)
947 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
948 lgs8gxx_read_reg(priv
, 0x30, &orig
);
951 lgs8gxx_write_reg(priv
, 0x30, t
);
953 lgs8gxx_write_reg(priv
, 0x30, t
);
955 lgs8gxx_write_reg(priv
, 0x30, t
);
957 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
958 lgs8gxx_write_reg(priv
, 0xC6, 0x41);
959 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
963 static void packet_counter_stop(struct lgs8gxx_state
*priv
)
967 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
968 lgs8gxx_read_reg(priv
, 0x30, &t
);
970 lgs8gxx_write_reg(priv
, 0x30, t
);
972 lgs8gxx_write_reg(priv
, 0xC6, 0x81);
976 static int lgs8gxx_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
978 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
979 u8 reg_err
, reg_total
, t
;
980 u32 total_cnt
= 0, err_cnt
= 0;
983 dprintk("%s\n", __func__
);
985 packet_counter_start(priv
);
987 packet_counter_stop(priv
);
989 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
990 reg_total
= 0x28; reg_err
= 0x2C;
992 reg_total
= 0xD0; reg_err
= 0xD4;
995 for (i
= 0; i
< 4; i
++) {
997 lgs8gxx_read_reg(priv
, reg_total
+3-i
, &t
);
1000 for (i
= 0; i
< 4; i
++) {
1002 lgs8gxx_read_reg(priv
, reg_err
+3-i
, &t
);
1005 dprintk("error=%d total=%d\n", err_cnt
, total_cnt
);
1010 *ber
= err_cnt
* 100 / total_cnt
;
1012 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
1016 static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
1018 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
1020 if (priv
->config
->tuner_address
== 0)
1023 u8 v
= 0x80 | priv
->config
->tuner_address
;
1024 return lgs8gxx_write_reg(priv
, 0x01, v
);
1026 return lgs8gxx_write_reg(priv
, 0x01, 0);
1029 static struct dvb_frontend_ops lgs8gxx_ops
= {
1031 .name
= "Legend Silicon LGS8913/LGS8GXX DMB-TH",
1033 .frequency_min
= 474000000,
1034 .frequency_max
= 858000000,
1035 .frequency_stepsize
= 10000,
1039 FE_CAN_TRANSMISSION_MODE_AUTO
|
1040 FE_CAN_GUARD_INTERVAL_AUTO
1043 .release
= lgs8gxx_release
,
1045 .init
= lgs8gxx_init
,
1046 .write
= lgs8gxx_write
,
1047 .i2c_gate_ctrl
= lgs8gxx_i2c_gate_ctrl
,
1049 .set_frontend
= lgs8gxx_set_fe
,
1050 .get_frontend
= lgs8gxx_get_fe
,
1051 .get_tune_settings
= lgs8gxx_get_tune_settings
,
1053 .read_status
= lgs8gxx_read_status
,
1054 .read_ber
= lgs8gxx_read_ber
,
1055 .read_signal_strength
= lgs8gxx_read_signal_strength
,
1056 .read_snr
= lgs8gxx_read_snr
,
1057 .read_ucblocks
= lgs8gxx_read_ucblocks
,
1060 struct dvb_frontend
*lgs8gxx_attach(const struct lgs8gxx_config
*config
,
1061 struct i2c_adapter
*i2c
)
1063 struct lgs8gxx_state
*priv
= NULL
;
1066 dprintk("%s()\n", __func__
);
1068 if (config
== NULL
|| i2c
== NULL
)
1071 priv
= kzalloc(sizeof(struct lgs8gxx_state
), GFP_KERNEL
);
1075 priv
->config
= config
;
1078 /* check if the demod is there */
1079 if (lgs8gxx_read_reg(priv
, 0, &data
) != 0) {
1080 dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
1081 __func__
, priv
->config
->demod_address
);
1085 lgs8gxx_read_reg(priv
, 1, &data
);
1087 memcpy(&priv
->frontend
.ops
, &lgs8gxx_ops
,
1088 sizeof(struct dvb_frontend_ops
));
1089 priv
->frontend
.demodulator_priv
= priv
;
1091 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
1092 lgs8g75_init_data(priv
);
1094 return &priv
->frontend
;
1097 dprintk("%s() error_out\n", __func__
);
1102 EXPORT_SYMBOL(lgs8gxx_attach
);
1104 MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
1105 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
1106 MODULE_LICENSE("GPL");