1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for Legend Silicon GB20600 (a.k.a DMB-TH) demodulator
4 * LGS8913, LGS8GL5, LGS8G75
5 * experimental support LGS8G42, LGS8G52
7 * Copyright (C) 2007-2009 David T.L. Wong <davidtlwong@gmail.com>
8 * Copyright (C) 2008 Sirius International (Hong Kong) Limited
9 * Timothy Lee <timothy.lee@siriushk.com> (for initial work on LGS8GL5)
12 #include <asm/div64.h>
13 #include <linux/firmware.h>
15 #include <media/dvb_frontend.h>
18 #include "lgs8gxx_priv.h"
20 #define dprintk(args...) \
23 printk(KERN_DEBUG "lgs8gxx: " args); \
27 static int fake_signal_str
= 1;
29 #define LGS8GXX_FIRMWARE "lgs8g75.fw"
31 module_param(debug
, int, 0644);
32 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
34 module_param(fake_signal_str
, int, 0644);
35 MODULE_PARM_DESC(fake_signal_str
, "fake signal strength for LGS8913."
36 "Signal strength calculation is slow.(default:on).");
38 /* LGS8GXX internal helper functions */
40 static int lgs8gxx_write_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8 data
)
43 u8 buf
[] = { reg
, data
};
44 struct i2c_msg msg
= { .flags
= 0, .buf
= buf
, .len
= 2 };
46 msg
.addr
= priv
->config
->demod_address
;
47 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
51 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, data
);
53 ret
= i2c_transfer(priv
->i2c
, &msg
, 1);
56 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
57 __func__
, reg
, data
, ret
);
59 return (ret
!= 1) ? -1 : 0;
62 static int lgs8gxx_read_reg(struct lgs8gxx_state
*priv
, u8 reg
, u8
*p_data
)
69 struct i2c_msg msg
[] = {
70 { .flags
= 0, .buf
= b0
, .len
= 1 },
71 { .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 },
74 dev_addr
= priv
->config
->demod_address
;
75 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
&& reg
>= 0xC0)
77 msg
[1].addr
= msg
[0].addr
= dev_addr
;
79 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
81 dprintk("%s: error reg=0x%x, ret=%i\n", __func__
, reg
, ret
);
87 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, b1
[0]);
91 static int lgs8gxx_soft_reset(struct lgs8gxx_state
*priv
)
93 lgs8gxx_write_reg(priv
, 0x02, 0x00);
95 lgs8gxx_write_reg(priv
, 0x02, 0x01);
101 static int wait_reg_mask(struct lgs8gxx_state
*priv
, u8 reg
, u8 mask
,
102 u8 val
, u8 delay
, u8 tries
)
107 for (i
= 0; i
< tries
; i
++) {
108 lgs8gxx_read_reg(priv
, reg
, &t
);
110 if ((t
& mask
) == val
)
118 static int lgs8gxx_set_ad_mode(struct lgs8gxx_state
*priv
)
120 const struct lgs8gxx_config
*config
= priv
->config
;
123 if_conf
= 0x10; /* AGC output on, RF_AGC output off; */
126 ((config
->ext_adc
) ? 0x80 : 0x00) |
127 ((config
->if_neg_center
) ? 0x04 : 0x00) |
128 ((config
->if_freq
== 0) ? 0x08 : 0x00) | /* Baseband */
129 ((config
->adc_signed
) ? 0x02 : 0x00) |
130 ((config
->if_neg_edge
) ? 0x01 : 0x00);
132 if (config
->ext_adc
&&
133 (config
->prod
== LGS8GXX_PROD_LGS8G52
)) {
134 lgs8gxx_write_reg(priv
, 0xBA, 0x40);
137 lgs8gxx_write_reg(priv
, 0x07, if_conf
);
142 static int lgs8gxx_set_if_freq(struct lgs8gxx_state
*priv
, u32 freq
/*in kHz*/)
148 if_clk
= priv
->config
->if_clk_freq
;
155 v32
= val
& 0xFFFFFFFF;
156 dprintk("Set IF Freq to %dkHz\n", freq
);
159 dprintk("Set IF Freq to baseband\n");
161 dprintk("AFC_INIT_FREQ = 0x%08X\n", v32
);
163 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
164 lgs8gxx_write_reg(priv
, 0x08, 0xFF & (v32
));
165 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
>> 8));
166 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 16));
167 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 24));
169 lgs8gxx_write_reg(priv
, 0x09, 0xFF & (v32
));
170 lgs8gxx_write_reg(priv
, 0x0A, 0xFF & (v32
>> 8));
171 lgs8gxx_write_reg(priv
, 0x0B, 0xFF & (v32
>> 16));
172 lgs8gxx_write_reg(priv
, 0x0C, 0xFF & (v32
>> 24));
178 static int lgs8gxx_get_afc_phase(struct lgs8gxx_state
*priv
)
185 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
190 for (i
= 0; i
< 4; i
++) {
191 lgs8gxx_read_reg(priv
, reg_addr
, &t
);
198 val
*= priv
->config
->if_clk_freq
;
200 dprintk("AFC = %u kHz\n", (u32
)val
);
204 static int lgs8gxx_set_mode_auto(struct lgs8gxx_state
*priv
)
207 u8 prod
= priv
->config
->prod
;
209 if (prod
== LGS8GXX_PROD_LGS8913
)
210 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
212 if (prod
== LGS8GXX_PROD_LGS8G75
) {
213 lgs8gxx_read_reg(priv
, 0x0C, &t
);
215 lgs8gxx_write_reg(priv
, 0x0C, t
| 0x80);
216 lgs8gxx_write_reg(priv
, 0x39, 0x00);
217 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
218 } else if (prod
== LGS8GXX_PROD_LGS8913
||
219 prod
== LGS8GXX_PROD_LGS8GL5
||
220 prod
== LGS8GXX_PROD_LGS8G42
||
221 prod
== LGS8GXX_PROD_LGS8G52
||
222 prod
== LGS8GXX_PROD_LGS8G54
) {
223 lgs8gxx_read_reg(priv
, 0x7E, &t
);
224 lgs8gxx_write_reg(priv
, 0x7E, t
| 0x01);
226 /* clear FEC self reset */
227 lgs8gxx_read_reg(priv
, 0xC5, &t
);
228 lgs8gxx_write_reg(priv
, 0xC5, t
& 0xE0);
231 if (prod
== LGS8GXX_PROD_LGS8913
) {
232 /* FEC auto detect */
233 lgs8gxx_write_reg(priv
, 0xC1, 0x03);
235 lgs8gxx_read_reg(priv
, 0x7C, &t
);
236 t
= (t
& 0x8C) | 0x03;
237 lgs8gxx_write_reg(priv
, 0x7C, t
);
240 lgs8gxx_read_reg(priv
, 0xC3, &t
);
241 t
= (t
& 0xEF) | 0x10;
242 lgs8gxx_write_reg(priv
, 0xC3, t
);
245 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G52
)
246 lgs8gxx_write_reg(priv
, 0xD9, 0x40);
251 static int lgs8gxx_set_mode_manual(struct lgs8gxx_state
*priv
)
255 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
257 lgs8gxx_read_reg(priv
, 0x0C, &t
);
259 lgs8gxx_write_reg(priv
, 0x0C, t
);
261 lgs8gxx_read_reg(priv
, 0x0C, &t
);
262 lgs8gxx_read_reg(priv
, 0x19, &t2
);
264 if (((t
&0x03) == 0x01) && (t2
&0x01)) {
265 lgs8gxx_write_reg(priv
, 0x6E, 0x05);
266 lgs8gxx_write_reg(priv
, 0x39, 0x02);
267 lgs8gxx_write_reg(priv
, 0x39, 0x03);
268 lgs8gxx_write_reg(priv
, 0x3D, 0x05);
269 lgs8gxx_write_reg(priv
, 0x3E, 0x28);
270 lgs8gxx_write_reg(priv
, 0x53, 0x80);
272 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
273 lgs8gxx_write_reg(priv
, 0x39, 0x00);
274 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
277 lgs8gxx_soft_reset(priv
);
281 /* turn off auto-detect; manual settings */
282 lgs8gxx_write_reg(priv
, 0x7E, 0);
283 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
284 lgs8gxx_write_reg(priv
, 0xC1, 0);
286 lgs8gxx_read_reg(priv
, 0xC5, &t
);
287 t
= (t
& 0xE0) | 0x06;
288 lgs8gxx_write_reg(priv
, 0xC5, t
);
290 lgs8gxx_soft_reset(priv
);
295 static int lgs8gxx_is_locked(struct lgs8gxx_state
*priv
, u8
*locked
)
300 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
301 ret
= lgs8gxx_read_reg(priv
, 0x13, &t
);
303 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
307 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
308 *locked
= ((t
& 0x80) == 0x80) ? 1 : 0;
310 *locked
= ((t
& 0xC0) == 0xC0) ? 1 : 0;
314 /* Wait for Code Acquisition Lock */
315 static int lgs8gxx_wait_ca_lock(struct lgs8gxx_state
*priv
, u8
*locked
)
320 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
330 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 50, 40);
331 *locked
= (ret
== 0) ? 1 : 0;
336 static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state
*priv
,
342 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
352 ret
= wait_reg_mask(priv
, reg
, mask
, val
, 10, 20);
353 *finished
= (ret
== 0) ? 1 : 0;
358 static int lgs8gxx_autolock_gi(struct lgs8gxx_state
*priv
, u8 gi
, u8 cpn
,
366 dprintk("try GI 945\n");
367 else if (gi
== GI_595
)
368 dprintk("try GI 595\n");
369 else if (gi
== GI_420
)
370 dprintk("try GI 420\n");
371 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
372 lgs8gxx_read_reg(priv
, 0x0C, &t1
);
373 lgs8gxx_read_reg(priv
, 0x18, &t2
);
377 t2
|= cpn
? 0x01 : 0x00;
378 lgs8gxx_write_reg(priv
, 0x0C, t1
);
379 lgs8gxx_write_reg(priv
, 0x18, t2
);
381 lgs8gxx_write_reg(priv
, 0x04, gi
);
383 lgs8gxx_soft_reset(priv
);
384 err
= lgs8gxx_wait_ca_lock(priv
, locked
);
385 if (err
|| !(*locked
))
387 err
= lgs8gxx_is_autodetect_finished(priv
, &ad_fini
);
391 dprintk("auto detect finished\n");
398 static int lgs8gxx_auto_detect(struct lgs8gxx_state
*priv
,
399 u8
*detected_param
, u8
*gi
)
403 u8 locked
= 0, tmp_gi
;
405 dprintk("%s\n", __func__
);
407 lgs8gxx_set_mode_auto(priv
);
408 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
409 lgs8gxx_write_reg(priv
, 0x67, 0xAA);
410 lgs8gxx_write_reg(priv
, 0x6E, 0x3F);
413 lgs8gxx_write_reg(priv
, 0x03, 00);
416 for (i
= 0; i
< 2; i
++) {
417 for (j
= 0; j
< 2; j
++) {
419 err
= lgs8gxx_autolock_gi(priv
, GI_945
, j
, &locked
);
425 for (j
= 0; j
< 2; j
++) {
427 err
= lgs8gxx_autolock_gi(priv
, GI_420
, j
, &locked
);
434 err
= lgs8gxx_autolock_gi(priv
, GI_595
, 1, &locked
);
442 if ((err
== 0) && (locked
== 1)) {
445 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
) {
446 lgs8gxx_read_reg(priv
, 0xA2, &t
);
449 lgs8gxx_read_reg(priv
, 0x1F, &t
);
450 *detected_param
= t
& 0x3F;
453 if (tmp_gi
== GI_945
)
454 dprintk("GI 945 locked\n");
455 else if (tmp_gi
== GI_595
)
456 dprintk("GI 595 locked\n");
457 else if (tmp_gi
== GI_420
)
458 dprintk("GI 420 locked\n");
468 static void lgs8gxx_auto_lock(struct lgs8gxx_state
*priv
)
472 u8 detected_param
= 0;
474 err
= lgs8gxx_auto_detect(priv
, &detected_param
, &gi
);
477 dprintk("lgs8gxx_auto_detect failed\n");
479 dprintk("detected param = 0x%02X\n", detected_param
);
481 /* Apply detected parameters */
482 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
483 u8 inter_leave_len
= detected_param
& TIM_MASK
;
484 /* Fix 8913 time interleaver detection bug */
485 inter_leave_len
= (inter_leave_len
== TIM_MIDDLE
) ? 0x60 : 0x40;
486 detected_param
&= CF_MASK
| SC_MASK
| LGS_FEC_MASK
;
487 detected_param
|= inter_leave_len
;
489 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
491 lgs8gxx_read_reg(priv
, 0x19, &t
);
493 t
|= detected_param
<< 1;
494 lgs8gxx_write_reg(priv
, 0x19, t
);
496 lgs8gxx_write_reg(priv
, 0x7D, detected_param
);
497 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
498 lgs8gxx_write_reg(priv
, 0xC0, detected_param
);
500 /* lgs8gxx_soft_reset(priv); */
502 /* Enter manual mode */
503 lgs8gxx_set_mode_manual(priv
);
507 priv
->curr_gi
= 945; break;
509 priv
->curr_gi
= 595; break;
511 priv
->curr_gi
= 420; break;
513 priv
->curr_gi
= 945; break;
517 static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state
*priv
,
518 u8 serial
, u8 clk_pol
, u8 clk_gated
)
523 reg_addr
= (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) ? 0x30 : 0xC2;
524 ret
= lgs8gxx_read_reg(priv
, reg_addr
, &t
);
529 t
|= serial
? TS_SERIAL
: TS_PARALLEL
;
530 t
|= clk_pol
? TS_CLK_INVERTED
: TS_CLK_NORMAL
;
531 t
|= clk_gated
? TS_CLK_GATED
: TS_CLK_FREERUN
;
533 ret
= lgs8gxx_write_reg(priv
, reg_addr
, t
);
540 /* A/D input peak-to-peak voltage range */
541 static int lgs8g75_set_adc_vpp(struct lgs8gxx_state
*priv
,
544 u8 r26
= 0x73, r27
= 0x90;
546 if (priv
->config
->prod
!= LGS8GXX_PROD_LGS8G75
)
549 r26
|= (sel
& 0x01) << 7;
550 r27
|= (sel
& 0x02) >> 1;
551 lgs8gxx_write_reg(priv
, 0x26, r26
);
552 lgs8gxx_write_reg(priv
, 0x27, r27
);
557 /* LGS8913 demod frontend functions */
559 static int lgs8913_init(struct lgs8gxx_state
*priv
)
563 /* LGS8913 specific */
564 lgs8gxx_write_reg(priv
, 0xc1, 0x3);
566 lgs8gxx_read_reg(priv
, 0x7c, &t
);
567 lgs8gxx_write_reg(priv
, 0x7c, (t
&0x8c) | 0x3);
569 /* LGS8913 specific */
570 lgs8gxx_read_reg(priv
, 0xc3, &t
);
571 lgs8gxx_write_reg(priv
, 0xc3, t
&0x10);
577 static int lgs8g75_init_data(struct lgs8gxx_state
*priv
)
579 const struct firmware
*fw
;
583 rc
= request_firmware(&fw
, LGS8GXX_FIRMWARE
, &priv
->i2c
->dev
);
587 lgs8gxx_write_reg(priv
, 0xC6, 0x40);
589 lgs8gxx_write_reg(priv
, 0x3D, 0x04);
590 lgs8gxx_write_reg(priv
, 0x39, 0x00);
592 lgs8gxx_write_reg(priv
, 0x3A, 0x00);
593 lgs8gxx_write_reg(priv
, 0x38, 0x00);
594 lgs8gxx_write_reg(priv
, 0x3B, 0x00);
595 lgs8gxx_write_reg(priv
, 0x38, 0x00);
597 for (i
= 0; i
< fw
->size
; i
++) {
598 lgs8gxx_write_reg(priv
, 0x38, 0x00);
599 lgs8gxx_write_reg(priv
, 0x3A, (u8
)(i
&0xff));
600 lgs8gxx_write_reg(priv
, 0x3B, (u8
)(i
>>8));
601 lgs8gxx_write_reg(priv
, 0x3C, fw
->data
[i
]);
604 lgs8gxx_write_reg(priv
, 0x38, 0x00);
606 release_firmware(fw
);
610 static int lgs8gxx_init(struct dvb_frontend
*fe
)
612 struct lgs8gxx_state
*priv
=
613 (struct lgs8gxx_state
*)fe
->demodulator_priv
;
614 const struct lgs8gxx_config
*config
= priv
->config
;
617 dprintk("%s\n", __func__
);
619 lgs8gxx_read_reg(priv
, 0, &data
);
620 dprintk("reg 0 = 0x%02X\n", data
);
622 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
623 lgs8g75_set_adc_vpp(priv
, config
->adc_vpp
);
625 /* Setup MPEG output format */
626 err
= lgs8gxx_set_mpeg_mode(priv
, config
->serial_ts
,
628 config
->ts_clk_gated
);
632 if (config
->prod
== LGS8GXX_PROD_LGS8913
)
634 lgs8gxx_set_if_freq(priv
, priv
->config
->if_freq
);
635 lgs8gxx_set_ad_mode(priv
);
640 static void lgs8gxx_release(struct dvb_frontend
*fe
)
642 struct lgs8gxx_state
*state
= fe
->demodulator_priv
;
643 dprintk("%s\n", __func__
);
649 static int lgs8gxx_write(struct dvb_frontend
*fe
, const u8 buf
[], int len
)
651 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
656 return lgs8gxx_write_reg(priv
, buf
[0], buf
[1]);
659 static int lgs8gxx_set_fe(struct dvb_frontend
*fe
)
661 struct dtv_frontend_properties
*fe_params
= &fe
->dtv_property_cache
;
662 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
664 dprintk("%s\n", __func__
);
667 if (fe
->ops
.tuner_ops
.set_params
) {
668 fe
->ops
.tuner_ops
.set_params(fe
);
669 if (fe
->ops
.i2c_gate_ctrl
)
670 fe
->ops
.i2c_gate_ctrl(fe
, 0);
673 /* start auto lock */
674 lgs8gxx_auto_lock(priv
);
678 /* TODO: get real readings from device */
681 fe_params
->bandwidth_hz
= 8000000;
683 fe_params
->code_rate_HP
= FEC_AUTO
;
684 fe_params
->code_rate_LP
= FEC_AUTO
;
686 fe_params
->modulation
= QAM_AUTO
;
688 /* transmission mode */
689 fe_params
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
692 fe_params
->guard_interval
= GUARD_INTERVAL_AUTO
;
695 fe_params
->hierarchy
= HIERARCHY_NONE
;
701 int lgs8gxx_get_tune_settings(struct dvb_frontend
*fe
,
702 struct dvb_frontend_tune_settings
*fesettings
)
704 /* FIXME: copy from tda1004x.c */
705 fesettings
->min_delay_ms
= 800;
706 fesettings
->step_size
= 0;
707 fesettings
->max_drift
= 0;
711 static int lgs8gxx_read_status(struct dvb_frontend
*fe
,
712 enum fe_status
*fe_status
)
714 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
718 dprintk("%s\n", __func__
);
721 lgs8gxx_get_afc_phase(priv
);
722 lgs8gxx_is_locked(priv
, &locked
);
723 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
725 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
726 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
730 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
734 dprintk("Reg 0x4B: 0x%02X\n", t
);
737 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
) {
738 if ((t
& 0x40) == 0x40)
739 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
740 if ((t
& 0x80) == 0x80)
741 *fe_status
|= FE_HAS_VITERBI
| FE_HAS_SYNC
|
744 if ((t
& 0x80) == 0x80)
745 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
746 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
750 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
754 static int lgs8gxx_read_signal_agc(struct lgs8gxx_state
*priv
, u16
*signal
)
759 dprintk("%s()\n", __func__
);
760 lgs8gxx_read_reg(priv
, 0x3F, &agc_lvl
[0]);
761 lgs8gxx_read_reg(priv
, 0x3E, &agc_lvl
[1]);
767 dprintk("agc_lvl: 0x%04X\n", v
);
784 *signal
= cat
* 65535 / 5;
789 static int lgs8913_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
792 s16 max_strength
= 0;
794 u16 i
, gi
= priv
->curr_gi
;
796 dprintk("%s\n", __func__
);
798 ret
= lgs8gxx_read_reg(priv
, 0x4B, &t
);
802 if (fake_signal_str
) {
803 if ((t
& 0xC0) == 0xC0) {
804 dprintk("Fake signal strength\n");
811 dprintk("gi = %d\n", gi
);
812 for (i
= 0; i
< gi
; i
++) {
815 lgs8gxx_write_reg(priv
, 0x84, 0x03 & (i
>> 8));
816 lgs8gxx_write_reg(priv
, 0x83, i
& 0xFF);
818 lgs8gxx_read_reg(priv
, 0x94, &str
);
819 if (max_strength
< str
)
823 *signal
= max_strength
;
824 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
826 lgs8gxx_read_reg(priv
, 0x95, &t
);
827 dprintk("%s: AVG Noise=0x%02X\n", __func__
, t
);
832 static int lgs8g75_read_signal_strength(struct lgs8gxx_state
*priv
, u16
*signal
)
837 dprintk("%s\n", __func__
);
839 lgs8gxx_read_reg(priv
, 0xB1, &t
);
842 lgs8gxx_read_reg(priv
, 0xB0, &t
);
846 dprintk("%s: signal=0x%02X\n", __func__
, *signal
);
851 static int lgs8gxx_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
853 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
855 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8913
)
856 return lgs8913_read_signal_strength(priv
, signal
);
857 else if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
858 return lgs8g75_read_signal_strength(priv
, signal
);
860 return lgs8gxx_read_signal_agc(priv
, signal
);
863 static int lgs8gxx_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
865 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
869 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
)
870 lgs8gxx_read_reg(priv
, 0x34, &t
);
872 lgs8gxx_read_reg(priv
, 0x95, &t
);
873 dprintk("AVG Noise=0x%02X\n", t
);
876 dprintk("snr=0x%x\n", *snr
);
881 static int lgs8gxx_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
884 dprintk("%s: ucblocks=0x%x\n", __func__
, *ucblocks
);
888 static void packet_counter_start(struct lgs8gxx_state
*priv
)
892 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
893 lgs8gxx_read_reg(priv
, 0x30, &orig
);
896 lgs8gxx_write_reg(priv
, 0x30, t
);
898 lgs8gxx_write_reg(priv
, 0x30, t
);
900 lgs8gxx_write_reg(priv
, 0x30, t
);
902 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
903 lgs8gxx_write_reg(priv
, 0xC6, 0x41);
904 lgs8gxx_write_reg(priv
, 0xC6, 0x01);
908 static void packet_counter_stop(struct lgs8gxx_state
*priv
)
912 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
913 lgs8gxx_read_reg(priv
, 0x30, &t
);
915 lgs8gxx_write_reg(priv
, 0x30, t
);
917 lgs8gxx_write_reg(priv
, 0xC6, 0x81);
921 static int lgs8gxx_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
923 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
924 u8 reg_err
, reg_total
, t
;
925 u32 total_cnt
= 0, err_cnt
= 0;
928 dprintk("%s\n", __func__
);
930 packet_counter_start(priv
);
932 packet_counter_stop(priv
);
934 if (priv
->config
->prod
== LGS8GXX_PROD_LGS8G75
) {
935 reg_total
= 0x28; reg_err
= 0x2C;
937 reg_total
= 0xD0; reg_err
= 0xD4;
940 for (i
= 0; i
< 4; i
++) {
942 lgs8gxx_read_reg(priv
, reg_total
+3-i
, &t
);
945 for (i
= 0; i
< 4; i
++) {
947 lgs8gxx_read_reg(priv
, reg_err
+3-i
, &t
);
950 dprintk("error=%d total=%d\n", err_cnt
, total_cnt
);
955 *ber
= err_cnt
* 100 / total_cnt
;
957 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
961 static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
963 struct lgs8gxx_state
*priv
= fe
->demodulator_priv
;
965 if (priv
->config
->tuner_address
== 0)
968 u8 v
= 0x80 | priv
->config
->tuner_address
;
969 return lgs8gxx_write_reg(priv
, 0x01, v
);
971 return lgs8gxx_write_reg(priv
, 0x01, 0);
974 static const struct dvb_frontend_ops lgs8gxx_ops
= {
975 .delsys
= { SYS_DTMB
},
977 .name
= "Legend Silicon LGS8913/LGS8GXX DMB-TH",
978 .frequency_min_hz
= 474 * MHz
,
979 .frequency_max_hz
= 858 * MHz
,
980 .frequency_stepsize_hz
= 10 * kHz
,
984 FE_CAN_TRANSMISSION_MODE_AUTO
|
985 FE_CAN_GUARD_INTERVAL_AUTO
988 .release
= lgs8gxx_release
,
990 .init
= lgs8gxx_init
,
991 .write
= lgs8gxx_write
,
992 .i2c_gate_ctrl
= lgs8gxx_i2c_gate_ctrl
,
994 .set_frontend
= lgs8gxx_set_fe
,
995 .get_tune_settings
= lgs8gxx_get_tune_settings
,
997 .read_status
= lgs8gxx_read_status
,
998 .read_ber
= lgs8gxx_read_ber
,
999 .read_signal_strength
= lgs8gxx_read_signal_strength
,
1000 .read_snr
= lgs8gxx_read_snr
,
1001 .read_ucblocks
= lgs8gxx_read_ucblocks
,
1004 struct dvb_frontend
*lgs8gxx_attach(const struct lgs8gxx_config
*config
,
1005 struct i2c_adapter
*i2c
)
1007 struct lgs8gxx_state
*priv
= NULL
;
1010 dprintk("%s()\n", __func__
);
1012 if (config
== NULL
|| i2c
== NULL
)
1015 priv
= kzalloc(sizeof(struct lgs8gxx_state
), GFP_KERNEL
);
1019 priv
->config
= config
;
1022 /* check if the demod is there */
1023 if (lgs8gxx_read_reg(priv
, 0, &data
) != 0) {
1024 dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
1025 __func__
, priv
->config
->demod_address
);
1029 lgs8gxx_read_reg(priv
, 1, &data
);
1031 memcpy(&priv
->frontend
.ops
, &lgs8gxx_ops
,
1032 sizeof(struct dvb_frontend_ops
));
1033 priv
->frontend
.demodulator_priv
= priv
;
1035 if (config
->prod
== LGS8GXX_PROD_LGS8G75
)
1036 lgs8g75_init_data(priv
);
1038 return &priv
->frontend
;
1041 dprintk("%s() error_out\n", __func__
);
1046 EXPORT_SYMBOL(lgs8gxx_attach
);
1048 MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
1049 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
1050 MODULE_LICENSE("GPL");
1051 MODULE_FIRMWARE(LGS8GXX_FIRMWARE
);