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.
22 #include <asm/div64.h>
23 #include <linux/firmware.h>
25 #include <media/dvb_frontend.h>
28 #include "lgs8gxx_priv.h"
30 #define dprintk(args...) \
33 printk(KERN_DEBUG "lgs8gxx: " args); \
37 static int fake_signal_str
= 1;
39 #define LGS8GXX_FIRMWARE "lgs8g75.fw"
41 module_param(debug
, int, 0644);
42 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
44 module_param(fake_signal_str
, int, 0644);
45 MODULE_PARM_DESC(fake_signal_str
, "fake signal strength for LGS8913."
46 "Signal strength calculation is slow.(default:on).");
48 /* LGS8GXX internal helper functions */
50 static int lgs8gxx_write_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8 data
)
53 u8 buf
[] = { reg
, data
};
54 struct i2c_msg msg
= { .flags
= 0, .buf
= buf
, .len
= 2 };
56 msg
.addr
= priv
->config
->demod_address
;
57 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
61 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, data
);
63 ret
= i2c_transfer(priv
->i2c
, &msg
, 1);
66 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
67 __func__
, reg
, data
, ret
);
69 return (ret
!= 1) ? -1 : 0;
72 static int lgs8gxx_read_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8
*p_data
)
79 struct i2c_msg msg
[] = {
80 { .flags
= 0, .buf
= b0
, .len
= 1 },
81 { .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 },
84 dev_addr
= priv
->config
->demod_address
;
85 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
87 msg
[1].addr
= msg
[0].addr
= dev_addr
;
89 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
91 dprintk("%s: error reg=0x%x, ret=%i\n", __func__
, reg
, ret
);
97 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, b1
[0]);
101 static int lgs8gxx_soft_reset(struct lgs8gxx_state
*priv
)
103 lgs8gxx_write_reg(priv
, 0x02, 0x00);
105 lgs8gxx_write_reg(priv
, 0x02, 0x01);
111 static int wait_reg_mask(struct lgs8gxx_state
*priv
, u8 reg
, u8 mask
,
112 u8 val
, u8 delay
, u8 tries
)
117 for (i
= 0; i
< tries
; i
++) {
118 lgs8gxx_read_reg(priv
, reg
, &t
);
120 if ((t
& mask
) == val
)
128 static int lgs8gxx_set_ad_mode(struct lgs8gxx_state
*priv
)
130 const struct lgs8gxx_config
*config
= priv
->config
;
133 if_conf
= 0x10; /* AGC output on, RF_AGC output off; */
136 ((config
->ext_adc
) ? 0x80 : 0x00) |
137 ((config
->if_neg_center
) ? 0x04 : 0x00) |
138 ((config
->if_freq
== 0) ? 0x08 : 0x00) | /* Baseband */
139 ((config
->adc_signed
) ? 0x02 : 0x00) |
140 ((config
->if_neg_edge
) ? 0x01 : 0x00);
142 if (config
->ext_adc
&&
143 (config
->prod
== LGS8GXX_PROD_LGS8G52
)) {
144 lgs8gxx_write_reg(priv
, 0xBA, 0x40);
147 lgs8gxx_write_reg(priv
, 0x07, if_conf
);
152 static int lgs8gxx_set_if_freq(struct lgs8gxx_state
*priv
, u32 freq
/*in kHz*/)
158 if_clk
= priv
->config
->if_clk_freq
;
165 v32
= val
& 0xFFFFFFFF;
166 dprintk("Set IF Freq to %dkHz\n", freq
);
169 dprintk("Set IF Freq to baseband\n");
171 dprintk("AFC_INIT_FREQ = 0x%08X\n", v32
);
173 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
174 lgs8gxx_write_reg(priv
, 0x08, 0xFF & (v32
));
175 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
>> 8));
176 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 16));
177 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 24));
179 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
));
180 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 8));
181 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 16));
182 lgs8gxx_write_reg(priv
, 0x0C, 0xFF & (v32
>> 24));
188 static int lgs8gxx_get_afc_phase(struct lgs8gxx_state
*priv
)
195 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
200 for (i
= 0; i
< 4; i
++) {
201 lgs8gxx_read_reg(priv
, reg_addr
, &t
);
208 val
*= priv
->config
->if_clk_freq
;
210 dprintk("AFC = %u kHz\n", (u32
)val
);
214 static int lgs8gxx_set_mode_auto(struct lgs8gxx_state
*priv
)
217 u8 prod
= priv
->config
->prod
;
219 if (prod
== LGS8GXX_PROD_LGS8913
)
220 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
222 if (prod
== LGS8GXX_PROD_LGS8G75
) {
223 lgs8gxx_read_reg(priv
, 0x0C, &t
);
225 lgs8gxx_write_reg(priv
, 0x0C, t
| 0x80);
226 lgs8gxx_write_reg(priv
, 0x39, 0x00);
227 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
228 } else if (prod
== LGS8GXX_PROD_LGS8913
||
229 prod
== LGS8GXX_PROD_LGS8GL5
||
230 prod
== LGS8GXX_PROD_LGS8G42
||
231 prod
== LGS8GXX_PROD_LGS8G52
||
232 prod
== LGS8GXX_PROD_LGS8G54
) {
233 lgs8gxx_read_reg(priv
, 0x7E, &t
);
234 lgs8gxx_write_reg(priv
, 0x7E, t
| 0x01);
236 /* clear FEC self reset */
237 lgs8gxx_read_reg(priv
, 0xC5, &t
);
238 lgs8gxx_write_reg(priv
, 0xC5, t
& 0xE0);
241 if (prod
== LGS8GXX_PROD_LGS8913
) {
242 /* FEC auto detect */
243 lgs8gxx_write_reg(priv
, 0xC1, 0x03);
245 lgs8gxx_read_reg(priv
, 0x7C, &t
);
246 t
= (t
& 0x8C) | 0x03;
247 lgs8gxx_write_reg(priv
, 0x7C, t
);
250 lgs8gxx_read_reg(priv
, 0xC3, &t
);
251 t
= (t
& 0xEF) | 0x10;
252 lgs8gxx_write_reg(priv
, 0xC3, t
);
255 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G52
)
256 lgs8gxx_write_reg(priv
, 0xD9, 0x40);
261 static int lgs8gxx_set_mode_manual(struct lgs8gxx_state
*priv
)
265 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
267 lgs8gxx_read_reg(priv
, 0x0C, &t
);
269 lgs8gxx_write_reg(priv
, 0x0C, t
);
271 lgs8gxx_read_reg(priv
, 0x0C, &t
);
272 lgs8gxx_read_reg(priv
, 0x19, &t2
);
274 if (((t
&0x03) == 0x01) && (t2
&0x01)) {
275 lgs8gxx_write_reg(priv
, 0x6E, 0x05);
276 lgs8gxx_write_reg(priv
, 0x39, 0x02);
277 lgs8gxx_write_reg(priv
, 0x39, 0x03);
278 lgs8gxx_write_reg(priv
, 0x3D, 0x05);
279 lgs8gxx_write_reg(priv
, 0x3E, 0x28);
280 lgs8gxx_write_reg(priv
, 0x53, 0x80);
282 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
283 lgs8gxx_write_reg(priv
, 0x39, 0x00);
284 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
287 lgs8gxx_soft_reset(priv
);
291 /* turn off auto-detect; manual settings */
292 lgs8gxx_write_reg(priv
, 0x7E, 0);
293 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
294 lgs8gxx_write_reg(priv
, 0xC1, 0);
296 lgs8gxx_read_reg(priv
, 0xC5, &t
);
297 t
= (t
& 0xE0) | 0x06;
298 lgs8gxx_write_reg(priv
, 0xC5, t
);
300 lgs8gxx_soft_reset(priv
);
305 static int lgs8gxx_is_locked(struct lgs8gxx_state
*priv
, u8
*locked
)
310 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
311 ret
= lgs8gxx_read_reg(priv
, 0x13, &t
);
313 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
317 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
318 *locked
= ((t
& 0x80) == 0x80) ? 1 : 0;
320 *locked
= ((t
& 0xC0) == 0xC0) ? 1 : 0;
324 /* Wait for Code Acquisition Lock */
325 static int lgs8gxx_wait_ca_lock(struct lgs8gxx_state
*priv
, u8
*locked
)
330 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
340 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 50, 40);
341 *locked
= (ret
== 0) ? 1 : 0;
346 static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state
*priv
,
352 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
362 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 10, 20);
363 *finished
= (ret
== 0) ? 1 : 0;
368 static int lgs8gxx_autolock_gi(struct lgs8gxx_state
*priv
, u8 gi
, u8 cpn
,
376 dprintk("try GI 945\n");
377 else if (gi
== GI_595
)
378 dprintk("try GI 595\n");
379 else if (gi
== GI_420
)
380 dprintk("try GI 420\n");
381 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
382 lgs8gxx_read_reg(priv
, 0x0C, &t1
);
383 lgs8gxx_read_reg(priv
, 0x18, &t2
);
387 t2
|= cpn
? 0x01 : 0x00;
388 lgs8gxx_write_reg(priv
, 0x0C, t1
);
389 lgs8gxx_write_reg(priv
, 0x18, t2
);
391 lgs8gxx_write_reg(priv
, 0x04, gi
);
393 lgs8gxx_soft_reset(priv
);
394 err
= lgs8gxx_wait_ca_lock(priv
, locked
);
395 if (err
|| !(*locked
))
397 err
= lgs8gxx_is_autodetect_finished(priv
, &ad_fini
);
401 dprintk("auto detect finished\n");
408 static int lgs8gxx_auto_detect(struct lgs8gxx_state
*priv
,
409 u8
*detected_param
, u8
*gi
)
413 u8 locked
= 0, tmp_gi
;
415 dprintk("%s\n", __func__
);
417 lgs8gxx_set_mode_auto(priv
);
418 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
419 lgs8gxx_write_reg(priv
, 0x67, 0xAA);
420 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
423 lgs8gxx_write_reg(priv
, 0x03, 00);
426 for (i
= 0; i
< 2; i
++) {
427 for (j
= 0; j
< 2; j
++) {
429 err
= lgs8gxx_autolock_gi(priv
, GI_945
, j
, &locked
);
435 for (j
= 0; j
< 2; j
++) {
437 err
= lgs8gxx_autolock_gi(priv
, GI_420
, j
, &locked
);
444 err
= lgs8gxx_autolock_gi(priv
, GI_595
, 1, &locked
);
452 if ((err
== 0) && (locked
== 1)) {
455 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
) {
456 lgs8gxx_read_reg(priv
, 0xA2, &t
);
459 lgs8gxx_read_reg(priv
, 0x1F, &t
);
460 *detected_param
= t
& 0x3F;
463 if (tmp_gi
== GI_945
)
464 dprintk("GI 945 locked\n");
465 else if (tmp_gi
== GI_595
)
466 dprintk("GI 595 locked\n");
467 else if (tmp_gi
== GI_420
)
468 dprintk("GI 420 locked\n");
478 static void lgs8gxx_auto_lock(struct lgs8gxx_state
*priv
)
482 u8 detected_param
= 0;
484 err
= lgs8gxx_auto_detect(priv
, &detected_param
, &gi
);
487 dprintk("lgs8gxx_auto_detect failed\n");
489 dprintk("detected param = 0x%02X\n", detected_param
);
491 /* Apply detected parameters */
492 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
493 u8 inter_leave_len
= detected_param
& TIM_MASK
;
494 /* Fix 8913 time interleaver detection bug */
495 inter_leave_len
= (inter_leave_len
== TIM_MIDDLE
) ? 0x60 : 0x40;
496 detected_param
&= CF_MASK
| SC_MASK
| LGS_FEC_MASK
;
497 detected_param
|= inter_leave_len
;
499 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
501 lgs8gxx_read_reg(priv
, 0x19, &t
);
503 t
|= detected_param
<< 1;
504 lgs8gxx_write_reg(priv
, 0x19, t
);
506 lgs8gxx_write_reg(priv
, 0x7D, detected_param
);
507 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
508 lgs8gxx_write_reg(priv
, 0xC0, detected_param
);
510 /* lgs8gxx_soft_reset(priv); */
512 /* Enter manual mode */
513 lgs8gxx_set_mode_manual(priv
);
517 priv
->curr_gi
= 945; break;
519 priv
->curr_gi
= 595; break;
521 priv
->curr_gi
= 420; break;
523 priv
->curr_gi
= 945; break;
527 static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state
*priv
,
528 u8 serial
, u8 clk_pol
, u8 clk_gated
)
533 reg_addr
= (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) ? 0x30 : 0xC2;
534 ret
= lgs8gxx_read_reg(priv
, reg_addr
, &t
);
539 t
|= serial
? TS_SERIAL
: TS_PARALLEL
;
540 t
|= clk_pol
? TS_CLK_INVERTED
: TS_CLK_NORMAL
;
541 t
|= clk_gated
? TS_CLK_GATED
: TS_CLK_FREERUN
;
543 ret
= lgs8gxx_write_reg(priv
, reg_addr
, t
);
550 /* A/D input peak-to-peak voltage range */
551 static int lgs8g75_set_adc_vpp(struct lgs8gxx_state
*priv
,
554 u8 r26
= 0x73, r27
= 0x90;
556 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
)
559 r26
|= (sel
& 0x01) << 7;
560 r27
|= (sel
& 0x02) >> 1;
561 lgs8gxx_write_reg(priv
, 0x26, r26
);
562 lgs8gxx_write_reg(priv
, 0x27, r27
);
567 /* LGS8913 demod frontend functions */
569 static int lgs8913_init(struct lgs8gxx_state
*priv
)
573 /* LGS8913 specific */
574 lgs8gxx_write_reg(priv
, 0xc1, 0x3);
576 lgs8gxx_read_reg(priv
, 0x7c, &t
);
577 lgs8gxx_write_reg(priv
, 0x7c, (t
&0x8c) | 0x3);
579 /* LGS8913 specific */
580 lgs8gxx_read_reg(priv
, 0xc3, &t
);
581 lgs8gxx_write_reg(priv
, 0xc3, t
&0x10);
587 static int lgs8g75_init_data(struct lgs8gxx_state
*priv
)
589 const struct firmware
*fw
;
593 rc
= request_firmware(&fw
, LGS8GXX_FIRMWARE
, &priv
->i2c
->dev
);
597 lgs8gxx_write_reg(priv
, 0xC6, 0x40);
599 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
600 lgs8gxx_write_reg(priv
, 0x39, 0x00);
602 lgs8gxx_write_reg(priv
, 0x3A, 0x00);
603 lgs8gxx_write_reg(priv
, 0x38, 0x00);
604 lgs8gxx_write_reg(priv
, 0x3B, 0x00);
605 lgs8gxx_write_reg(priv
, 0x38, 0x00);
607 for (i
= 0; i
< fw
->size
; i
++) {
608 lgs8gxx_write_reg(priv
, 0x38, 0x00);
609 lgs8gxx_write_reg(priv
, 0x3A, (u8
)(i
&0xff));
610 lgs8gxx_write_reg(priv
, 0x3B, (u8
)(i
>>8));
611 lgs8gxx_write_reg(priv
, 0x3C, fw
->data
[i
]);
614 lgs8gxx_write_reg(priv
, 0x38, 0x00);
616 release_firmware(fw
);
620 static int lgs8gxx_init(struct dvb_frontend
*fe
)
622 struct lgs8gxx_state
*priv
=
623 (struct lgs8gxx_state
*)fe
->demodulator_priv
;
624 const struct lgs8gxx_config
*config
= priv
->config
;
627 dprintk("%s\n", __func__
);
629 lgs8gxx_read_reg(priv
, 0, &data
);
630 dprintk("reg 0 = 0x%02X\n", data
);
632 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
633 lgs8g75_set_adc_vpp(priv
, config
->adc_vpp
);
635 /* Setup MPEG output format */
636 err
= lgs8gxx_set_mpeg_mode(priv
, config
->serial_ts
,
638 config
->ts_clk_gated
);
642 if (config
->prod
== LGS8GXX_PROD_LGS8913
)
644 lgs8gxx_set_if_freq(priv
, priv
->config
->if_freq
);
645 lgs8gxx_set_ad_mode(priv
);
650 static void lgs8gxx_release(struct dvb_frontend
*fe
)
652 struct lgs8gxx_state
*state
= fe
->demodulator_priv
;
653 dprintk("%s\n", __func__
);
659 static int lgs8gxx_write(struct dvb_frontend
*fe
, const u8 buf
[], int len
)
661 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
666 return lgs8gxx_write_reg(priv
, buf
[0], buf
[1]);
669 static int lgs8gxx_set_fe(struct dvb_frontend
*fe
)
671 struct dtv_frontend_properties
*fe_params
= &fe
->dtv_property_cache
;
672 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
674 dprintk("%s\n", __func__
);
677 if (fe
->ops
.tuner_ops
.set_params
) {
678 fe
->ops
.tuner_ops
.set_params(fe
);
679 if (fe
->ops
.i2c_gate_ctrl
)
680 fe
->ops
.i2c_gate_ctrl(fe
, 0);
683 /* start auto lock */
684 lgs8gxx_auto_lock(priv
);
688 /* TODO: get real readings from device */
691 fe_params
->bandwidth_hz
= 8000000;
693 fe_params
->code_rate_HP
= FEC_AUTO
;
694 fe_params
->code_rate_LP
= FEC_AUTO
;
696 fe_params
->modulation
= QAM_AUTO
;
698 /* transmission mode */
699 fe_params
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
702 fe_params
->guard_interval
= GUARD_INTERVAL_AUTO
;
705 fe_params
->hierarchy
= HIERARCHY_NONE
;
711 int lgs8gxx_get_tune_settings(struct dvb_frontend
*fe
,
712 struct dvb_frontend_tune_settings
*fesettings
)
714 /* FIXME: copy from tda1004x.c */
715 fesettings
->min_delay_ms
= 800;
716 fesettings
->step_size
= 0;
717 fesettings
->max_drift
= 0;
721 static int lgs8gxx_read_status(struct dvb_frontend
*fe
,
722 enum fe_status
*fe_status
)
724 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
728 dprintk("%s\n", __func__
);
731 lgs8gxx_get_afc_phase(priv
);
732 lgs8gxx_is_locked(priv
, &locked
);
733 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
735 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
736 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
740 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
744 dprintk("Reg 0x4B: 0x%02X\n", t
);
747 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
748 if ((t
& 0x40) == 0x40)
749 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
750 if ((t
& 0x80) == 0x80)
751 *fe_status
|= FE_HAS_VITERBI
| FE_HAS_SYNC
|
754 if ((t
& 0x80) == 0x80)
755 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
756 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
760 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
764 static int lgs8gxx_read_signal_agc(struct lgs8gxx_state
*priv
, u16
*signal
)
769 dprintk("%s()\n", __func__
);
770 lgs8gxx_read_reg(priv
, 0x3F, &agc_lvl
[0]);
771 lgs8gxx_read_reg(priv
, 0x3E, &agc_lvl
[1]);
777 dprintk("agc_lvl: 0x%04X\n", v
);
794 *signal
= cat
* 65535 / 5;
799 static int lgs8913_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
802 s16 max_strength
= 0;
804 u16 i
, gi
= priv
->curr_gi
;
806 dprintk("%s\n", __func__
);
808 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
812 if (fake_signal_str
) {
813 if ((t
& 0xC0) == 0xC0) {
814 dprintk("Fake signal strength\n");
821 dprintk("gi = %d\n", gi
);
822 for (i
= 0; i
< gi
; i
++) {
825 lgs8gxx_write_reg(priv
, 0x84, 0x03 & (i
>> 8));
826 lgs8gxx_write_reg(priv
, 0x83, i
& 0xFF);
828 lgs8gxx_read_reg(priv
, 0x94, &str
);
829 if (max_strength
< str
)
833 *signal
= max_strength
;
834 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
836 lgs8gxx_read_reg(priv
, 0x95, &t
);
837 dprintk("%s: AVG Noise=0x%02X\n", __func__
, t
);
842 static int lgs8g75_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
847 dprintk("%s\n", __func__
);
849 lgs8gxx_read_reg(priv
, 0xB1, &t
);
852 lgs8gxx_read_reg(priv
, 0xB0, &t
);
856 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
861 static int lgs8gxx_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
863 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
865 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
866 return lgs8913_read_signal_strength(priv
, signal
);
867 else if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
868 return lgs8g75_read_signal_strength(priv
, signal
);
870 return lgs8gxx_read_signal_agc(priv
, signal
);
873 static int lgs8gxx_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
875 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
879 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
880 lgs8gxx_read_reg(priv
, 0x34, &t
);
882 lgs8gxx_read_reg(priv
, 0x95, &t
);
883 dprintk("AVG Noise=0x%02X\n", t
);
886 dprintk("snr=0x%x\n", *snr
);
891 static int lgs8gxx_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
894 dprintk("%s: ucblocks=0x%x\n", __func__
, *ucblocks
);
898 static void packet_counter_start(struct lgs8gxx_state
*priv
)
902 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
903 lgs8gxx_read_reg(priv
, 0x30, &orig
);
906 lgs8gxx_write_reg(priv
, 0x30, t
);
908 lgs8gxx_write_reg(priv
, 0x30, t
);
910 lgs8gxx_write_reg(priv
, 0x30, t
);
912 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
913 lgs8gxx_write_reg(priv
, 0xC6, 0x41);
914 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
918 static void packet_counter_stop(struct lgs8gxx_state
*priv
)
922 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
923 lgs8gxx_read_reg(priv
, 0x30, &t
);
925 lgs8gxx_write_reg(priv
, 0x30, t
);
927 lgs8gxx_write_reg(priv
, 0xC6, 0x81);
931 static int lgs8gxx_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
933 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
934 u8 reg_err
, reg_total
, t
;
935 u32 total_cnt
= 0, err_cnt
= 0;
938 dprintk("%s\n", __func__
);
940 packet_counter_start(priv
);
942 packet_counter_stop(priv
);
944 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
945 reg_total
= 0x28; reg_err
= 0x2C;
947 reg_total
= 0xD0; reg_err
= 0xD4;
950 for (i
= 0; i
< 4; i
++) {
952 lgs8gxx_read_reg(priv
, reg_total
+3-i
, &t
);
955 for (i
= 0; i
< 4; i
++) {
957 lgs8gxx_read_reg(priv
, reg_err
+3-i
, &t
);
960 dprintk("error=%d total=%d\n", err_cnt
, total_cnt
);
965 *ber
= err_cnt
* 100 / total_cnt
;
967 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
971 static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
973 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
975 if (priv
->config
->tuner_address
== 0)
978 u8 v
= 0x80 | priv
->config
->tuner_address
;
979 return lgs8gxx_write_reg(priv
, 0x01, v
);
981 return lgs8gxx_write_reg(priv
, 0x01, 0);
984 static const struct dvb_frontend_ops lgs8gxx_ops
= {
985 .delsys
= { SYS_DTMB
},
987 .name
= "Legend Silicon LGS8913/LGS8GXX DMB-TH",
988 .frequency_min
= 474000000,
989 .frequency_max
= 858000000,
990 .frequency_stepsize
= 10000,
994 FE_CAN_TRANSMISSION_MODE_AUTO
|
995 FE_CAN_GUARD_INTERVAL_AUTO
998 .release
= lgs8gxx_release
,
1000 .init
= lgs8gxx_init
,
1001 .write
= lgs8gxx_write
,
1002 .i2c_gate_ctrl
= lgs8gxx_i2c_gate_ctrl
,
1004 .set_frontend
= lgs8gxx_set_fe
,
1005 .get_tune_settings
= lgs8gxx_get_tune_settings
,
1007 .read_status
= lgs8gxx_read_status
,
1008 .read_ber
= lgs8gxx_read_ber
,
1009 .read_signal_strength
= lgs8gxx_read_signal_strength
,
1010 .read_snr
= lgs8gxx_read_snr
,
1011 .read_ucblocks
= lgs8gxx_read_ucblocks
,
1014 struct dvb_frontend
*lgs8gxx_attach(const struct lgs8gxx_config
*config
,
1015 struct i2c_adapter
*i2c
)
1017 struct lgs8gxx_state
*priv
= NULL
;
1020 dprintk("%s()\n", __func__
);
1022 if (config
== NULL
|| i2c
== NULL
)
1025 priv
= kzalloc(sizeof(struct lgs8gxx_state
), GFP_KERNEL
);
1029 priv
->config
= config
;
1032 /* check if the demod is there */
1033 if (lgs8gxx_read_reg(priv
, 0, &data
) != 0) {
1034 dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
1035 __func__
, priv
->config
->demod_address
);
1039 lgs8gxx_read_reg(priv
, 1, &data
);
1041 memcpy(&priv
->frontend
.ops
, &lgs8gxx_ops
,
1042 sizeof(struct dvb_frontend_ops
));
1043 priv
->frontend
.demodulator_priv
= priv
;
1045 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
1046 lgs8g75_init_data(priv
);
1048 return &priv
->frontend
;
1051 dprintk("%s() error_out\n", __func__
);
1056 EXPORT_SYMBOL(lgs8gxx_attach
);
1058 MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
1059 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
1060 MODULE_LICENSE("GPL");
1061 MODULE_FIRMWARE(LGS8GXX_FIRMWARE
);