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>
27 #include <linux/firmware.h>
29 #include "dvb_frontend.h"
32 #include "lgs8gxx_priv.h"
34 #define dprintk(args...) \
37 printk(KERN_DEBUG "lgs8gxx: " args); \
41 static int fake_signal_str
= 1;
43 #define LGS8GXX_FIRMWARE "lgs8g75.fw"
45 module_param(debug
, int, 0644);
46 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
48 module_param(fake_signal_str
, int, 0644);
49 MODULE_PARM_DESC(fake_signal_str
, "fake signal strength for LGS8913."
50 "Signal strength calculation is slow.(default:on).");
52 /* LGS8GXX internal helper functions */
54 static int lgs8gxx_write_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8 data
)
57 u8 buf
[] = { reg
, data
};
58 struct i2c_msg msg
= { .flags
= 0, .buf
= buf
, .len
= 2 };
60 msg
.addr
= priv
->config
->demod_address
;
61 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
65 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, data
);
67 ret
= i2c_transfer(priv
->i2c
, &msg
, 1);
70 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
71 __func__
, reg
, data
, ret
);
73 return (ret
!= 1) ? -1 : 0;
76 static int lgs8gxx_read_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8
*p_data
)
83 struct i2c_msg msg
[] = {
84 { .flags
= 0, .buf
= b0
, .len
= 1 },
85 { .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 },
88 dev_addr
= priv
->config
->demod_address
;
89 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
91 msg
[1].addr
= msg
[0].addr
= dev_addr
;
93 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
95 dprintk("%s: error reg=0x%x, ret=%i\n", __func__
, reg
, ret
);
101 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, b1
[0]);
105 static int lgs8gxx_soft_reset(struct lgs8gxx_state
*priv
)
107 lgs8gxx_write_reg(priv
, 0x02, 0x00);
109 lgs8gxx_write_reg(priv
, 0x02, 0x01);
115 static int wait_reg_mask(struct lgs8gxx_state
*priv
, u8 reg
, u8 mask
,
116 u8 val
, u8 delay
, u8 tries
)
121 for (i
= 0; i
< tries
; i
++) {
122 lgs8gxx_read_reg(priv
, reg
, &t
);
124 if ((t
& mask
) == val
)
132 static int lgs8gxx_set_ad_mode(struct lgs8gxx_state
*priv
)
134 const struct lgs8gxx_config
*config
= priv
->config
;
137 if_conf
= 0x10; /* AGC output on, RF_AGC output off; */
140 ((config
->ext_adc
) ? 0x80 : 0x00) |
141 ((config
->if_neg_center
) ? 0x04 : 0x00) |
142 ((config
->if_freq
== 0) ? 0x08 : 0x00) | /* Baseband */
143 ((config
->adc_signed
) ? 0x02 : 0x00) |
144 ((config
->if_neg_edge
) ? 0x01 : 0x00);
146 if (config
->ext_adc
&&
147 (config
->prod
== LGS8GXX_PROD_LGS8G52
)) {
148 lgs8gxx_write_reg(priv
, 0xBA, 0x40);
151 lgs8gxx_write_reg(priv
, 0x07, if_conf
);
156 static int lgs8gxx_set_if_freq(struct lgs8gxx_state
*priv
, u32 freq
/*in kHz*/)
162 if_clk
= priv
->config
->if_clk_freq
;
169 v32
= val
& 0xFFFFFFFF;
170 dprintk("Set IF Freq to %dkHz\n", freq
);
173 dprintk("Set IF Freq to baseband\n");
175 dprintk("AFC_INIT_FREQ = 0x%08X\n", v32
);
177 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
178 lgs8gxx_write_reg(priv
, 0x08, 0xFF & (v32
));
179 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
>> 8));
180 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 16));
181 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 24));
183 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
));
184 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 8));
185 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 16));
186 lgs8gxx_write_reg(priv
, 0x0C, 0xFF & (v32
>> 24));
192 static int lgs8gxx_get_afc_phase(struct lgs8gxx_state
*priv
)
199 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
204 for (i
= 0; i
< 4; i
++) {
205 lgs8gxx_read_reg(priv
, reg_addr
, &t
);
212 val
*= priv
->config
->if_clk_freq
;
214 dprintk("AFC = %u kHz\n", (u32
)val
);
218 static int lgs8gxx_set_mode_auto(struct lgs8gxx_state
*priv
)
221 u8 prod
= priv
->config
->prod
;
223 if (prod
== LGS8GXX_PROD_LGS8913
)
224 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
226 if (prod
== LGS8GXX_PROD_LGS8G75
) {
227 lgs8gxx_read_reg(priv
, 0x0C, &t
);
229 lgs8gxx_write_reg(priv
, 0x0C, t
| 0x80);
230 lgs8gxx_write_reg(priv
, 0x39, 0x00);
231 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
232 } else if (prod
== LGS8GXX_PROD_LGS8913
||
233 prod
== LGS8GXX_PROD_LGS8GL5
||
234 prod
== LGS8GXX_PROD_LGS8G42
||
235 prod
== LGS8GXX_PROD_LGS8G52
||
236 prod
== LGS8GXX_PROD_LGS8G54
) {
237 lgs8gxx_read_reg(priv
, 0x7E, &t
);
238 lgs8gxx_write_reg(priv
, 0x7E, t
| 0x01);
240 /* clear FEC self reset */
241 lgs8gxx_read_reg(priv
, 0xC5, &t
);
242 lgs8gxx_write_reg(priv
, 0xC5, t
& 0xE0);
245 if (prod
== LGS8GXX_PROD_LGS8913
) {
246 /* FEC auto detect */
247 lgs8gxx_write_reg(priv
, 0xC1, 0x03);
249 lgs8gxx_read_reg(priv
, 0x7C, &t
);
250 t
= (t
& 0x8C) | 0x03;
251 lgs8gxx_write_reg(priv
, 0x7C, t
);
254 lgs8gxx_read_reg(priv
, 0xC3, &t
);
255 t
= (t
& 0xEF) | 0x10;
256 lgs8gxx_write_reg(priv
, 0xC3, t
);
259 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G52
)
260 lgs8gxx_write_reg(priv
, 0xD9, 0x40);
265 static int lgs8gxx_set_mode_manual(struct lgs8gxx_state
*priv
)
269 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
271 lgs8gxx_read_reg(priv
, 0x0C, &t
);
273 lgs8gxx_write_reg(priv
, 0x0C, t
);
275 lgs8gxx_read_reg(priv
, 0x0C, &t
);
276 lgs8gxx_read_reg(priv
, 0x19, &t2
);
278 if (((t
&0x03) == 0x01) && (t2
&0x01)) {
279 lgs8gxx_write_reg(priv
, 0x6E, 0x05);
280 lgs8gxx_write_reg(priv
, 0x39, 0x02);
281 lgs8gxx_write_reg(priv
, 0x39, 0x03);
282 lgs8gxx_write_reg(priv
, 0x3D, 0x05);
283 lgs8gxx_write_reg(priv
, 0x3E, 0x28);
284 lgs8gxx_write_reg(priv
, 0x53, 0x80);
286 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
287 lgs8gxx_write_reg(priv
, 0x39, 0x00);
288 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
291 lgs8gxx_soft_reset(priv
);
295 /* turn off auto-detect; manual settings */
296 lgs8gxx_write_reg(priv
, 0x7E, 0);
297 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
298 lgs8gxx_write_reg(priv
, 0xC1, 0);
300 lgs8gxx_read_reg(priv
, 0xC5, &t
);
301 t
= (t
& 0xE0) | 0x06;
302 lgs8gxx_write_reg(priv
, 0xC5, t
);
304 lgs8gxx_soft_reset(priv
);
309 static int lgs8gxx_is_locked(struct lgs8gxx_state
*priv
, u8
*locked
)
314 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
315 ret
= lgs8gxx_read_reg(priv
, 0x13, &t
);
317 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
321 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
322 *locked
= ((t
& 0x80) == 0x80) ? 1 : 0;
324 *locked
= ((t
& 0xC0) == 0xC0) ? 1 : 0;
328 /* Wait for Code Acquisition Lock */
329 static int lgs8gxx_wait_ca_lock(struct lgs8gxx_state
*priv
, u8
*locked
)
334 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
344 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 50, 40);
345 *locked
= (ret
== 0) ? 1 : 0;
350 static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state
*priv
,
356 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
366 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 10, 20);
367 *finished
= (ret
== 0) ? 1 : 0;
372 static int lgs8gxx_autolock_gi(struct lgs8gxx_state
*priv
, u8 gi
, u8 cpn
,
380 dprintk("try GI 945\n");
381 else if (gi
== GI_595
)
382 dprintk("try GI 595\n");
383 else if (gi
== GI_420
)
384 dprintk("try GI 420\n");
385 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
386 lgs8gxx_read_reg(priv
, 0x0C, &t1
);
387 lgs8gxx_read_reg(priv
, 0x18, &t2
);
391 t2
|= cpn
? 0x01 : 0x00;
392 lgs8gxx_write_reg(priv
, 0x0C, t1
);
393 lgs8gxx_write_reg(priv
, 0x18, t2
);
395 lgs8gxx_write_reg(priv
, 0x04, gi
);
397 lgs8gxx_soft_reset(priv
);
398 err
= lgs8gxx_wait_ca_lock(priv
, locked
);
399 if (err
|| !(*locked
))
401 err
= lgs8gxx_is_autodetect_finished(priv
, &ad_fini
);
405 dprintk("auto detect finished\n");
412 static int lgs8gxx_auto_detect(struct lgs8gxx_state
*priv
,
413 u8
*detected_param
, u8
*gi
)
417 u8 locked
= 0, tmp_gi
;
419 dprintk("%s\n", __func__
);
421 lgs8gxx_set_mode_auto(priv
);
422 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
423 lgs8gxx_write_reg(priv
, 0x67, 0xAA);
424 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
427 lgs8gxx_write_reg(priv
, 0x03, 00);
430 for (i
= 0; i
< 2; i
++) {
431 for (j
= 0; j
< 2; j
++) {
433 err
= lgs8gxx_autolock_gi(priv
, GI_945
, j
, &locked
);
439 for (j
= 0; j
< 2; j
++) {
441 err
= lgs8gxx_autolock_gi(priv
, GI_420
, j
, &locked
);
448 err
= lgs8gxx_autolock_gi(priv
, GI_595
, 1, &locked
);
456 if ((err
== 0) && (locked
== 1)) {
459 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
) {
460 lgs8gxx_read_reg(priv
, 0xA2, &t
);
463 lgs8gxx_read_reg(priv
, 0x1F, &t
);
464 *detected_param
= t
& 0x3F;
467 if (tmp_gi
== GI_945
)
468 dprintk("GI 945 locked\n");
469 else if (tmp_gi
== GI_595
)
470 dprintk("GI 595 locked\n");
471 else if (tmp_gi
== GI_420
)
472 dprintk("GI 420 locked\n");
482 static void lgs8gxx_auto_lock(struct lgs8gxx_state
*priv
)
486 u8 detected_param
= 0;
488 err
= lgs8gxx_auto_detect(priv
, &detected_param
, &gi
);
491 dprintk("lgs8gxx_auto_detect failed\n");
493 dprintk("detected param = 0x%02X\n", detected_param
);
495 /* Apply detected parameters */
496 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
497 u8 inter_leave_len
= detected_param
& TIM_MASK
;
498 /* Fix 8913 time interleaver detection bug */
499 inter_leave_len
= (inter_leave_len
== TIM_MIDDLE
) ? 0x60 : 0x40;
500 detected_param
&= CF_MASK
| SC_MASK
| LGS_FEC_MASK
;
501 detected_param
|= inter_leave_len
;
503 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
505 lgs8gxx_read_reg(priv
, 0x19, &t
);
507 t
|= detected_param
<< 1;
508 lgs8gxx_write_reg(priv
, 0x19, t
);
510 lgs8gxx_write_reg(priv
, 0x7D, detected_param
);
511 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
512 lgs8gxx_write_reg(priv
, 0xC0, detected_param
);
514 /* lgs8gxx_soft_reset(priv); */
516 /* Enter manual mode */
517 lgs8gxx_set_mode_manual(priv
);
521 priv
->curr_gi
= 945; break;
523 priv
->curr_gi
= 595; break;
525 priv
->curr_gi
= 420; break;
527 priv
->curr_gi
= 945; break;
531 static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state
*priv
,
532 u8 serial
, u8 clk_pol
, u8 clk_gated
)
537 reg_addr
= (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) ? 0x30 : 0xC2;
538 ret
= lgs8gxx_read_reg(priv
, reg_addr
, &t
);
543 t
|= serial
? TS_SERIAL
: TS_PARALLEL
;
544 t
|= clk_pol
? TS_CLK_INVERTED
: TS_CLK_NORMAL
;
545 t
|= clk_gated
? TS_CLK_GATED
: TS_CLK_FREERUN
;
547 ret
= lgs8gxx_write_reg(priv
, reg_addr
, t
);
554 /* A/D input peak-to-peak voltage range */
555 static int lgs8g75_set_adc_vpp(struct lgs8gxx_state
*priv
,
558 u8 r26
= 0x73, r27
= 0x90;
560 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
)
563 r26
|= (sel
& 0x01) << 7;
564 r27
|= (sel
& 0x02) >> 1;
565 lgs8gxx_write_reg(priv
, 0x26, r26
);
566 lgs8gxx_write_reg(priv
, 0x27, r27
);
571 /* LGS8913 demod frontend functions */
573 static int lgs8913_init(struct lgs8gxx_state
*priv
)
577 /* LGS8913 specific */
578 lgs8gxx_write_reg(priv
, 0xc1, 0x3);
580 lgs8gxx_read_reg(priv
, 0x7c, &t
);
581 lgs8gxx_write_reg(priv
, 0x7c, (t
&0x8c) | 0x3);
583 /* LGS8913 specific */
584 lgs8gxx_read_reg(priv
, 0xc3, &t
);
585 lgs8gxx_write_reg(priv
, 0xc3, t
&0x10);
591 static int lgs8g75_init_data(struct lgs8gxx_state
*priv
)
593 const struct firmware
*fw
;
597 rc
= request_firmware(&fw
, LGS8GXX_FIRMWARE
, &priv
->i2c
->dev
);
601 lgs8gxx_write_reg(priv
, 0xC6, 0x40);
603 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
604 lgs8gxx_write_reg(priv
, 0x39, 0x00);
606 lgs8gxx_write_reg(priv
, 0x3A, 0x00);
607 lgs8gxx_write_reg(priv
, 0x38, 0x00);
608 lgs8gxx_write_reg(priv
, 0x3B, 0x00);
609 lgs8gxx_write_reg(priv
, 0x38, 0x00);
611 for (i
= 0; i
< fw
->size
; i
++) {
612 lgs8gxx_write_reg(priv
, 0x38, 0x00);
613 lgs8gxx_write_reg(priv
, 0x3A, (u8
)(i
&0xff));
614 lgs8gxx_write_reg(priv
, 0x3B, (u8
)(i
>>8));
615 lgs8gxx_write_reg(priv
, 0x3C, fw
->data
[i
]);
618 lgs8gxx_write_reg(priv
, 0x38, 0x00);
620 release_firmware(fw
);
624 static int lgs8gxx_init(struct dvb_frontend
*fe
)
626 struct lgs8gxx_state
*priv
=
627 (struct lgs8gxx_state
*)fe
->demodulator_priv
;
628 const struct lgs8gxx_config
*config
= priv
->config
;
631 dprintk("%s\n", __func__
);
633 lgs8gxx_read_reg(priv
, 0, &data
);
634 dprintk("reg 0 = 0x%02X\n", data
);
636 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
637 lgs8g75_set_adc_vpp(priv
, config
->adc_vpp
);
639 /* Setup MPEG output format */
640 err
= lgs8gxx_set_mpeg_mode(priv
, config
->serial_ts
,
642 config
->ts_clk_gated
);
646 if (config
->prod
== LGS8GXX_PROD_LGS8913
)
648 lgs8gxx_set_if_freq(priv
, priv
->config
->if_freq
);
649 lgs8gxx_set_ad_mode(priv
);
654 static void lgs8gxx_release(struct dvb_frontend
*fe
)
656 struct lgs8gxx_state
*state
= fe
->demodulator_priv
;
657 dprintk("%s\n", __func__
);
663 static int lgs8gxx_write(struct dvb_frontend
*fe
, const u8 buf
[], int len
)
665 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
670 return lgs8gxx_write_reg(priv
, buf
[0], buf
[1]);
673 static int lgs8gxx_set_fe(struct dvb_frontend
*fe
)
675 struct dtv_frontend_properties
*fe_params
= &fe
->dtv_property_cache
;
676 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
678 dprintk("%s\n", __func__
);
681 if (fe
->ops
.tuner_ops
.set_params
) {
682 fe
->ops
.tuner_ops
.set_params(fe
);
683 if (fe
->ops
.i2c_gate_ctrl
)
684 fe
->ops
.i2c_gate_ctrl(fe
, 0);
687 /* start auto lock */
688 lgs8gxx_auto_lock(priv
);
692 /* TODO: get real readings from device */
695 fe_params
->bandwidth_hz
= 8000000;
697 fe_params
->code_rate_HP
= FEC_AUTO
;
698 fe_params
->code_rate_LP
= FEC_AUTO
;
700 fe_params
->modulation
= QAM_AUTO
;
702 /* transmission mode */
703 fe_params
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
706 fe_params
->guard_interval
= GUARD_INTERVAL_AUTO
;
709 fe_params
->hierarchy
= HIERARCHY_NONE
;
715 int lgs8gxx_get_tune_settings(struct dvb_frontend
*fe
,
716 struct dvb_frontend_tune_settings
*fesettings
)
718 /* FIXME: copy from tda1004x.c */
719 fesettings
->min_delay_ms
= 800;
720 fesettings
->step_size
= 0;
721 fesettings
->max_drift
= 0;
725 static int lgs8gxx_read_status(struct dvb_frontend
*fe
,
726 enum fe_status
*fe_status
)
728 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
732 dprintk("%s\n", __func__
);
735 lgs8gxx_get_afc_phase(priv
);
736 lgs8gxx_is_locked(priv
, &locked
);
737 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
739 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
740 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
744 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
748 dprintk("Reg 0x4B: 0x%02X\n", t
);
751 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
752 if ((t
& 0x40) == 0x40)
753 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
754 if ((t
& 0x80) == 0x80)
755 *fe_status
|= FE_HAS_VITERBI
| FE_HAS_SYNC
|
758 if ((t
& 0x80) == 0x80)
759 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
760 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
764 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
768 static int lgs8gxx_read_signal_agc(struct lgs8gxx_state
*priv
, u16
*signal
)
773 dprintk("%s()\n", __func__
);
774 lgs8gxx_read_reg(priv
, 0x3F, &agc_lvl
[0]);
775 lgs8gxx_read_reg(priv
, 0x3E, &agc_lvl
[1]);
781 dprintk("agc_lvl: 0x%04X\n", v
);
798 *signal
= cat
* 65535 / 5;
803 static int lgs8913_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
806 s16 max_strength
= 0;
808 u16 i
, gi
= priv
->curr_gi
;
810 dprintk("%s\n", __func__
);
812 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
816 if (fake_signal_str
) {
817 if ((t
& 0xC0) == 0xC0) {
818 dprintk("Fake signal strength\n");
825 dprintk("gi = %d\n", gi
);
826 for (i
= 0; i
< gi
; i
++) {
829 lgs8gxx_write_reg(priv
, 0x84, 0x03 & (i
>> 8));
830 lgs8gxx_write_reg(priv
, 0x83, i
& 0xFF);
832 lgs8gxx_read_reg(priv
, 0x94, &str
);
833 if (max_strength
< str
)
837 *signal
= max_strength
;
838 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
840 lgs8gxx_read_reg(priv
, 0x95, &t
);
841 dprintk("%s: AVG Noise=0x%02X\n", __func__
, t
);
846 static int lgs8g75_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
851 dprintk("%s\n", __func__
);
853 lgs8gxx_read_reg(priv
, 0xB1, &t
);
856 lgs8gxx_read_reg(priv
, 0xB0, &t
);
860 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
865 static int lgs8gxx_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
867 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
869 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
870 return lgs8913_read_signal_strength(priv
, signal
);
871 else if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
872 return lgs8g75_read_signal_strength(priv
, signal
);
874 return lgs8gxx_read_signal_agc(priv
, signal
);
877 static int lgs8gxx_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
879 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
883 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
884 lgs8gxx_read_reg(priv
, 0x34, &t
);
886 lgs8gxx_read_reg(priv
, 0x95, &t
);
887 dprintk("AVG Noise=0x%02X\n", t
);
890 dprintk("snr=0x%x\n", *snr
);
895 static int lgs8gxx_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
898 dprintk("%s: ucblocks=0x%x\n", __func__
, *ucblocks
);
902 static void packet_counter_start(struct lgs8gxx_state
*priv
)
906 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
907 lgs8gxx_read_reg(priv
, 0x30, &orig
);
910 lgs8gxx_write_reg(priv
, 0x30, t
);
912 lgs8gxx_write_reg(priv
, 0x30, t
);
914 lgs8gxx_write_reg(priv
, 0x30, t
);
916 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
917 lgs8gxx_write_reg(priv
, 0xC6, 0x41);
918 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
922 static void packet_counter_stop(struct lgs8gxx_state
*priv
)
926 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
927 lgs8gxx_read_reg(priv
, 0x30, &t
);
929 lgs8gxx_write_reg(priv
, 0x30, t
);
931 lgs8gxx_write_reg(priv
, 0xC6, 0x81);
935 static int lgs8gxx_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
937 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
938 u8 reg_err
, reg_total
, t
;
939 u32 total_cnt
= 0, err_cnt
= 0;
942 dprintk("%s\n", __func__
);
944 packet_counter_start(priv
);
946 packet_counter_stop(priv
);
948 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
949 reg_total
= 0x28; reg_err
= 0x2C;
951 reg_total
= 0xD0; reg_err
= 0xD4;
954 for (i
= 0; i
< 4; i
++) {
956 lgs8gxx_read_reg(priv
, reg_total
+3-i
, &t
);
959 for (i
= 0; i
< 4; i
++) {
961 lgs8gxx_read_reg(priv
, reg_err
+3-i
, &t
);
964 dprintk("error=%d total=%d\n", err_cnt
, total_cnt
);
969 *ber
= err_cnt
* 100 / total_cnt
;
971 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
975 static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
977 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
979 if (priv
->config
->tuner_address
== 0)
982 u8 v
= 0x80 | priv
->config
->tuner_address
;
983 return lgs8gxx_write_reg(priv
, 0x01, v
);
985 return lgs8gxx_write_reg(priv
, 0x01, 0);
988 static const struct dvb_frontend_ops lgs8gxx_ops
= {
989 .delsys
= { SYS_DTMB
},
991 .name
= "Legend Silicon LGS8913/LGS8GXX DMB-TH",
992 .frequency_min
= 474000000,
993 .frequency_max
= 858000000,
994 .frequency_stepsize
= 10000,
998 FE_CAN_TRANSMISSION_MODE_AUTO
|
999 FE_CAN_GUARD_INTERVAL_AUTO
1002 .release
= lgs8gxx_release
,
1004 .init
= lgs8gxx_init
,
1005 .write
= lgs8gxx_write
,
1006 .i2c_gate_ctrl
= lgs8gxx_i2c_gate_ctrl
,
1008 .set_frontend
= lgs8gxx_set_fe
,
1009 .get_tune_settings
= lgs8gxx_get_tune_settings
,
1011 .read_status
= lgs8gxx_read_status
,
1012 .read_ber
= lgs8gxx_read_ber
,
1013 .read_signal_strength
= lgs8gxx_read_signal_strength
,
1014 .read_snr
= lgs8gxx_read_snr
,
1015 .read_ucblocks
= lgs8gxx_read_ucblocks
,
1018 struct dvb_frontend
*lgs8gxx_attach(const struct lgs8gxx_config
*config
,
1019 struct i2c_adapter
*i2c
)
1021 struct lgs8gxx_state
*priv
= NULL
;
1024 dprintk("%s()\n", __func__
);
1026 if (config
== NULL
|| i2c
== NULL
)
1029 priv
= kzalloc(sizeof(struct lgs8gxx_state
), GFP_KERNEL
);
1033 priv
->config
= config
;
1036 /* check if the demod is there */
1037 if (lgs8gxx_read_reg(priv
, 0, &data
) != 0) {
1038 dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
1039 __func__
, priv
->config
->demod_address
);
1043 lgs8gxx_read_reg(priv
, 1, &data
);
1045 memcpy(&priv
->frontend
.ops
, &lgs8gxx_ops
,
1046 sizeof(struct dvb_frontend_ops
));
1047 priv
->frontend
.demodulator_priv
= priv
;
1049 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
1050 lgs8g75_init_data(priv
);
1052 return &priv
->frontend
;
1055 dprintk("%s() error_out\n", __func__
);
1060 EXPORT_SYMBOL(lgs8gxx_attach
);
1062 MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
1063 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
1064 MODULE_LICENSE("GPL");
1065 MODULE_FIRMWARE(LGS8GXX_FIRMWARE
);