2 * Support for LG2160 - ATSC/MH
4 * Copyright (C) 2010 Michael Krufky <mkrufky@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/jiffies.h>
23 #include <linux/dvb/frontend.h>
27 module_param(debug
, int, 0644);
28 MODULE_PARM_DESC(debug
, "set debug level (info=1, reg=2 (or-able))");
33 #define lg_printk(kern, fmt, arg...) \
34 printk(kern "%s: " fmt, __func__, ##arg)
36 #define lg_info(fmt, arg...) printk(KERN_INFO "lg2160: " fmt, ##arg)
37 #define lg_warn(fmt, arg...) lg_printk(KERN_WARNING, fmt, ##arg)
38 #define lg_err(fmt, arg...) lg_printk(KERN_ERR, fmt, ##arg)
39 #define lg_dbg(fmt, arg...) if (debug & DBG_INFO) \
40 lg_printk(KERN_DEBUG, fmt, ##arg)
41 #define lg_reg(fmt, arg...) if (debug & DBG_REG) \
42 lg_printk(KERN_DEBUG, fmt, ##arg)
44 #define lg_fail(ret) \
49 lg_err("error %d on line %d\n", ret, __LINE__); \
54 struct i2c_adapter
*i2c_adap
;
55 const struct lg2160_config
*cfg
;
57 struct dvb_frontend frontend
;
59 u32 current_frequency
;
62 unsigned int last_reset
;
65 /* ------------------------------------------------------------------------ */
67 static int lg216x_write_reg(struct lg216x_state
*state
, u16 reg
, u8 val
)
70 u8 buf
[] = { reg
>> 8, reg
& 0xff, val
};
71 struct i2c_msg msg
= {
72 .addr
= state
->cfg
->i2c_addr
, .flags
= 0,
76 lg_reg("reg: 0x%04x, val: 0x%02x\n", reg
, val
);
78 ret
= i2c_transfer(state
->i2c_adap
, &msg
, 1);
81 lg_err("error (addr %02x %02x <- %02x, err = %i)\n",
82 msg
.buf
[0], msg
.buf
[1], msg
.buf
[2], ret
);
91 static int lg216x_read_reg(struct lg216x_state
*state
, u16 reg
, u8
*val
)
94 u8 reg_buf
[] = { reg
>> 8, reg
& 0xff };
95 struct i2c_msg msg
[] = {
96 { .addr
= state
->cfg
->i2c_addr
,
97 .flags
= 0, .buf
= reg_buf
, .len
= 2 },
98 { .addr
= state
->cfg
->i2c_addr
,
99 .flags
= I2C_M_RD
, .buf
= val
, .len
= 1 },
102 lg_reg("reg: 0x%04x\n", reg
);
104 ret
= i2c_transfer(state
->i2c_adap
, msg
, 2);
107 lg_err("error (addr %02x reg %04x error (ret == %i)\n",
108 state
->cfg
->i2c_addr
, reg
, ret
);
122 static int lg216x_write_regs(struct lg216x_state
*state
,
123 struct lg216x_reg
*regs
, int len
)
127 lg_reg("writing %d registers...\n", len
);
129 for (i
= 0; i
< len
; i
++) {
130 ret
= lg216x_write_reg(state
, regs
[i
].reg
, regs
[i
].val
);
137 static int lg216x_set_reg_bit(struct lg216x_state
*state
,
138 u16 reg
, int bit
, int onoff
)
143 lg_reg("reg: 0x%04x, bit: %d, level: %d\n", reg
, bit
, onoff
);
145 ret
= lg216x_read_reg(state
, reg
, &val
);
150 val
|= (onoff
& 1) << bit
;
152 ret
= lg216x_write_reg(state
, reg
, val
);
158 /* ------------------------------------------------------------------------ */
160 static int lg216x_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
162 struct lg216x_state
*state
= fe
->demodulator_priv
;
165 if (state
->cfg
->deny_i2c_rptr
)
168 lg_dbg("(%d)\n", enable
);
170 ret
= lg216x_set_reg_bit(state
, 0x0000, 0, enable
? 0 : 1);
177 static int lg216x_soft_reset(struct lg216x_state
*state
)
183 ret
= lg216x_write_reg(state
, 0x0002, 0x00);
188 ret
= lg216x_write_reg(state
, 0x0002, 0x01);
192 state
->last_reset
= jiffies_to_msecs(jiffies
);
197 static int lg216x_initialize(struct lg216x_state
*state
)
201 static struct lg216x_reg lg2160_init
[] = {
203 { .reg
= 0x0015, .val
= 0xe6 },
205 { .reg
= 0x0015, .val
= 0xf7 },
206 { .reg
= 0x001b, .val
= 0x52 },
207 { .reg
= 0x0208, .val
= 0x00 },
208 { .reg
= 0x0209, .val
= 0x82 },
209 { .reg
= 0x0210, .val
= 0xf9 },
210 { .reg
= 0x020a, .val
= 0x00 },
211 { .reg
= 0x020b, .val
= 0x82 },
212 { .reg
= 0x020d, .val
= 0x28 },
213 { .reg
= 0x020f, .val
= 0x14 },
217 static struct lg216x_reg lg2161_init
[] = {
218 { .reg
= 0x0000, .val
= 0x41 },
219 { .reg
= 0x0001, .val
= 0xfb },
220 { .reg
= 0x0216, .val
= 0x00 },
221 { .reg
= 0x0219, .val
= 0x00 },
222 { .reg
= 0x021b, .val
= 0x55 },
223 { .reg
= 0x0606, .val
= 0x0a },
226 switch (state
->cfg
->lg_chip
) {
228 ret
= lg216x_write_regs(state
,
229 lg2160_init
, ARRAY_SIZE(lg2160_init
));
232 ret
= lg216x_write_regs(state
,
233 lg2161_init
, ARRAY_SIZE(lg2161_init
));
242 ret
= lg216x_soft_reset(state
);
248 /* ------------------------------------------------------------------------ */
250 static int lg216x_set_if(struct lg216x_state
*state
)
255 lg_dbg("%d KHz\n", state
->cfg
->if_khz
);
257 ret
= lg216x_read_reg(state
, 0x0132, &val
);
262 val
|= (0 == state
->cfg
->if_khz
) ? 0x04 : 0x00;
264 ret
= lg216x_write_reg(state
, 0x0132, val
);
267 /* if NOT zero IF, 6 MHz is the default */
272 /* ------------------------------------------------------------------------ */
274 static int lg2160_agc_fix(struct lg216x_state
*state
,
275 int if_agc_fix
, int rf_agc_fix
)
280 ret
= lg216x_read_reg(state
, 0x0100, &val
);
285 val
|= (if_agc_fix
) ? 0x08 : 0x00;
286 val
|= (rf_agc_fix
) ? 0x04 : 0x00;
288 ret
= lg216x_write_reg(state
, 0x0100, val
);
295 static int lg2160_agc_freeze(struct lg216x_state
*state
,
296 int if_agc_freeze
, int rf_agc_freeze
)
301 ret
= lg216x_read_reg(state
, 0x0100, &val
);
306 val
|= (if_agc_freeze
) ? 0x20 : 0x00;
307 val
|= (rf_agc_freeze
) ? 0x10 : 0x00;
309 ret
= lg216x_write_reg(state
, 0x0100, val
);
316 static int lg2160_agc_polarity(struct lg216x_state
*state
,
317 int if_agc_polarity
, int rf_agc_polarity
)
322 ret
= lg216x_read_reg(state
, 0x0100, &val
);
327 val
|= (if_agc_polarity
) ? 0x02 : 0x00;
328 val
|= (rf_agc_polarity
) ? 0x01 : 0x00;
330 ret
= lg216x_write_reg(state
, 0x0100, val
);
336 static int lg2160_tuner_pwr_save_polarity(struct lg216x_state
*state
,
342 ret
= lg216x_read_reg(state
, 0x0008, &val
);
347 val
|= (polarity
) ? 0x01 : 0x00;
349 ret
= lg216x_write_reg(state
, 0x0008, val
);
355 static int lg2160_spectrum_polarity(struct lg216x_state
*state
,
361 ret
= lg216x_read_reg(state
, 0x0132, &val
);
366 val
|= (inverted
) ? 0x02 : 0x00;
368 ret
= lg216x_write_reg(state
, 0x0132, val
);
371 return lg216x_soft_reset(state
);
374 static int lg2160_tuner_pwr_save(struct lg216x_state
*state
, int onoff
)
379 ret
= lg216x_read_reg(state
, 0x0007, &val
);
384 val
|= (onoff
) ? 0x40 : 0x00;
386 ret
= lg216x_write_reg(state
, 0x0007, val
);
392 static int lg216x_set_parade(struct lg216x_state
*state
, int id
)
396 ret
= lg216x_write_reg(state
, 0x013e, id
& 0x7f);
400 state
->parade_id
= id
& 0x7f;
405 static int lg216x_set_ensemble(struct lg216x_state
*state
, int id
)
411 switch (state
->cfg
->lg_chip
) {
421 ret
= lg216x_read_reg(state
, reg
, &val
);
426 val
|= (id
) ? 0x01 : 0x00;
428 ret
= lg216x_write_reg(state
, reg
, val
);
434 static int lg2160_set_spi_clock(struct lg216x_state
*state
)
439 ret
= lg216x_read_reg(state
, 0x0014, &val
);
444 val
|= (state
->cfg
->spi_clock
<< 2);
446 ret
= lg216x_write_reg(state
, 0x0014, val
);
452 static int lg2161_set_output_interface(struct lg216x_state
*state
)
457 ret
= lg216x_read_reg(state
, 0x0014, &val
);
462 val
|= state
->cfg
->output_if
; /* FIXME: needs sanity check */
464 ret
= lg216x_write_reg(state
, 0x0014, val
);
470 static int lg216x_enable_fic(struct lg216x_state
*state
, int onoff
)
474 ret
= lg216x_write_reg(state
, 0x0017, 0x23);
478 ret
= lg216x_write_reg(state
, 0x0016, 0xfc);
482 switch (state
->cfg
->lg_chip
) {
484 ret
= lg216x_write_reg(state
, 0x0016,
485 0xfc | ((onoff
) ? 0x02 : 0x00));
488 ret
= lg216x_write_reg(state
, 0x0016, (onoff
) ? 0x10 : 0x00);
494 ret
= lg216x_initialize(state
);
499 ret
= lg216x_write_reg(state
, 0x0017, 0x03);
506 /* ------------------------------------------------------------------------ */
508 static int lg216x_get_fic_version(struct lg216x_state
*state
, u8
*ficver
)
513 *ficver
= 0xff; /* invalid value */
515 ret
= lg216x_read_reg(state
, 0x0128, &val
);
519 *ficver
= (val
>> 3) & 0x1f;
525 static int lg2160_get_parade_id(struct lg216x_state
*state
, u8
*id
)
530 *id
= 0xff; /* invalid value */
532 ret
= lg216x_read_reg(state
, 0x0123, &val
);
542 static int lg216x_get_nog(struct lg216x_state
*state
, u8
*nog
)
547 *nog
= 0xff; /* invalid value */
549 ret
= lg216x_read_reg(state
, 0x0124, &val
);
553 *nog
= ((val
>> 4) & 0x07) + 1;
558 static int lg216x_get_tnog(struct lg216x_state
*state
, u8
*tnog
)
563 *tnog
= 0xff; /* invalid value */
565 ret
= lg216x_read_reg(state
, 0x0125, &val
);
574 static int lg216x_get_sgn(struct lg216x_state
*state
, u8
*sgn
)
579 *sgn
= 0xff; /* invalid value */
581 ret
= lg216x_read_reg(state
, 0x0124, &val
);
590 static int lg216x_get_prc(struct lg216x_state
*state
, u8
*prc
)
595 *prc
= 0xff; /* invalid value */
597 ret
= lg216x_read_reg(state
, 0x0125, &val
);
601 *prc
= ((val
>> 5) & 0x07) + 1;
606 /* ------------------------------------------------------------------------ */
608 static int lg216x_get_rs_frame_mode(struct lg216x_state
*state
,
609 enum atscmh_rs_frame_mode
*rs_framemode
)
614 switch (state
->cfg
->lg_chip
) {
616 ret
= lg216x_read_reg(state
, 0x0410, &val
);
619 ret
= lg216x_read_reg(state
, 0x0513, &val
);
627 switch ((val
>> 4) & 0x03) {
632 *rs_framemode
= ATSCMH_RSFRAME_PRI_ONLY
;
635 *rs_framemode
= ATSCMH_RSFRAME_PRI_SEC
;
639 *rs_framemode
= ATSCMH_RSFRAME_RES
;
648 int lg216x_get_rs_frame_ensemble(struct lg216x_state
*state
,
649 enum atscmh_rs_frame_ensemble
*rs_frame_ens
)
654 switch (state
->cfg
->lg_chip
) {
656 ret
= lg216x_read_reg(state
, 0x0400, &val
);
659 ret
= lg216x_read_reg(state
, 0x0500, &val
);
668 *rs_frame_ens
= (enum atscmh_rs_frame_ensemble
) val
;
673 static int lg216x_get_rs_code_mode(struct lg216x_state
*state
,
674 enum atscmh_rs_code_mode
*rs_code_pri
,
675 enum atscmh_rs_code_mode
*rs_code_sec
)
680 switch (state
->cfg
->lg_chip
) {
682 ret
= lg216x_read_reg(state
, 0x0410, &val
);
685 ret
= lg216x_read_reg(state
, 0x0513, &val
);
693 *rs_code_pri
= (enum atscmh_rs_code_mode
) ((val
>> 2) & 0x03);
694 *rs_code_sec
= (enum atscmh_rs_code_mode
) (val
& 0x03);
699 static int lg216x_get_sccc_block_mode(struct lg216x_state
*state
,
700 enum atscmh_sccc_block_mode
*sccc_block
)
705 switch (state
->cfg
->lg_chip
) {
707 ret
= lg216x_read_reg(state
, 0x0315, &val
);
710 ret
= lg216x_read_reg(state
, 0x0511, &val
);
718 switch (val
& 0x03) {
720 *sccc_block
= ATSCMH_SCCC_BLK_SEP
;
723 *sccc_block
= ATSCMH_SCCC_BLK_COMB
;
726 *sccc_block
= ATSCMH_SCCC_BLK_RES
;
733 static int lg216x_get_sccc_code_mode(struct lg216x_state
*state
,
734 enum atscmh_sccc_code_mode
*mode_a
,
735 enum atscmh_sccc_code_mode
*mode_b
,
736 enum atscmh_sccc_code_mode
*mode_c
,
737 enum atscmh_sccc_code_mode
*mode_d
)
742 switch (state
->cfg
->lg_chip
) {
744 ret
= lg216x_read_reg(state
, 0x0316, &val
);
747 ret
= lg216x_read_reg(state
, 0x0512, &val
);
755 switch ((val
>> 6) & 0x03) {
757 *mode_a
= ATSCMH_SCCC_CODE_HLF
;
760 *mode_a
= ATSCMH_SCCC_CODE_QTR
;
763 *mode_a
= ATSCMH_SCCC_CODE_RES
;
767 switch ((val
>> 4) & 0x03) {
769 *mode_b
= ATSCMH_SCCC_CODE_HLF
;
772 *mode_b
= ATSCMH_SCCC_CODE_QTR
;
775 *mode_b
= ATSCMH_SCCC_CODE_RES
;
779 switch ((val
>> 2) & 0x03) {
781 *mode_c
= ATSCMH_SCCC_CODE_HLF
;
784 *mode_c
= ATSCMH_SCCC_CODE_QTR
;
787 *mode_c
= ATSCMH_SCCC_CODE_RES
;
791 switch (val
& 0x03) {
793 *mode_d
= ATSCMH_SCCC_CODE_HLF
;
796 *mode_d
= ATSCMH_SCCC_CODE_QTR
;
799 *mode_d
= ATSCMH_SCCC_CODE_RES
;
806 /* ------------------------------------------------------------------------ */
809 static int lg216x_read_fic_err_count(struct lg216x_state
*state
, u8
*err
)
816 switch (state
->cfg
->lg_chip
) {
818 ret
= lg216x_read_reg(state
, 0x0012, &fic_err
);
821 ret
= lg216x_read_reg(state
, 0x001e, &fic_err
);
832 static int lg2160_read_crc_err_count(struct lg216x_state
*state
, u16
*err
)
834 u8 crc_err1
, crc_err2
;
839 ret
= lg216x_read_reg(state
, 0x0411, &crc_err1
);
843 ret
= lg216x_read_reg(state
, 0x0412, &crc_err2
);
847 *err
= (u16
)(((crc_err2
& 0x0f) << 8) | crc_err1
);
852 static int lg2161_read_crc_err_count(struct lg216x_state
*state
, u16
*err
)
859 ret
= lg216x_read_reg(state
, 0x0612, &crc_err
);
868 static int lg216x_read_crc_err_count(struct lg216x_state
*state
, u16
*err
)
871 switch (state
->cfg
->lg_chip
) {
873 ret
= lg2160_read_crc_err_count(state
, err
);
876 ret
= lg2161_read_crc_err_count(state
, err
);
885 static int lg2160_read_rs_err_count(struct lg216x_state
*state
, u16
*err
)
892 ret
= lg216x_read_reg(state
, 0x0413, &rs_err1
);
896 ret
= lg216x_read_reg(state
, 0x0414, &rs_err2
);
900 *err
= (u16
)(((rs_err2
& 0x0f) << 8) | rs_err1
);
905 static int lg2161_read_rs_err_count(struct lg216x_state
*state
, u16
*err
)
912 ret
= lg216x_read_reg(state
, 0x0613, &rs_err1
);
916 ret
= lg216x_read_reg(state
, 0x0614, &rs_err2
);
920 *err
= (u16
)((rs_err1
<< 8) | rs_err2
);
925 static int lg216x_read_rs_err_count(struct lg216x_state
*state
, u16
*err
)
928 switch (state
->cfg
->lg_chip
) {
930 ret
= lg2160_read_rs_err_count(state
, err
);
933 ret
= lg2161_read_rs_err_count(state
, err
);
943 /* ------------------------------------------------------------------------ */
945 static int lg216x_get_frontend(struct dvb_frontend
*fe
,
946 struct dtv_frontend_properties
*c
)
948 struct lg216x_state
*state
= fe
->demodulator_priv
;
953 c
->modulation
= VSB_8
;
954 c
->frequency
= state
->current_frequency
;
955 c
->delivery_system
= SYS_ATSCMH
;
957 ret
= lg216x_get_fic_version(state
,
961 if (state
->fic_ver
!= c
->atscmh_fic_ver
) {
962 state
->fic_ver
= c
->atscmh_fic_ver
;
965 ret
= lg2160_get_parade_id(state
,
966 &c
->atscmh_parade_id
);
970 c
->atscmh_parade_id
= state
->parade_id
;
972 ret
= lg216x_get_nog(state
,
976 ret
= lg216x_get_tnog(state
,
980 ret
= lg216x_get_sgn(state
,
984 ret
= lg216x_get_prc(state
,
989 ret
= lg216x_get_rs_frame_mode(state
,
990 (enum atscmh_rs_frame_mode
*)
991 &c
->atscmh_rs_frame_mode
);
994 ret
= lg216x_get_rs_frame_ensemble(state
,
995 (enum atscmh_rs_frame_ensemble
*)
996 &c
->atscmh_rs_frame_ensemble
);
999 ret
= lg216x_get_rs_code_mode(state
,
1000 (enum atscmh_rs_code_mode
*)
1001 &c
->atscmh_rs_code_mode_pri
,
1002 (enum atscmh_rs_code_mode
*)
1003 &c
->atscmh_rs_code_mode_sec
);
1006 ret
= lg216x_get_sccc_block_mode(state
,
1007 (enum atscmh_sccc_block_mode
*)
1008 &c
->atscmh_sccc_block_mode
);
1011 ret
= lg216x_get_sccc_code_mode(state
,
1012 (enum atscmh_sccc_code_mode
*)
1013 &c
->atscmh_sccc_code_mode_a
,
1014 (enum atscmh_sccc_code_mode
*)
1015 &c
->atscmh_sccc_code_mode_b
,
1016 (enum atscmh_sccc_code_mode
*)
1017 &c
->atscmh_sccc_code_mode_c
,
1018 (enum atscmh_sccc_code_mode
*)
1019 &c
->atscmh_sccc_code_mode_d
);
1024 ret
= lg216x_read_fic_err_count(state
,
1025 (u8
*)&c
->atscmh_fic_err
);
1028 ret
= lg216x_read_crc_err_count(state
,
1029 &c
->atscmh_crc_err
);
1032 ret
= lg216x_read_rs_err_count(state
,
1037 switch (state
->cfg
->lg_chip
) {
1039 if (((c
->atscmh_rs_err
>= 240) &&
1040 (c
->atscmh_crc_err
>= 240)) &&
1041 ((jiffies_to_msecs(jiffies
) - state
->last_reset
) > 6000))
1042 ret
= lg216x_soft_reset(state
);
1045 /* no fix needed here (as far as we know) */
1055 static int lg216x_get_property(struct dvb_frontend
*fe
,
1056 struct dtv_property
*tvp
)
1058 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1060 return (DTV_ATSCMH_FIC_VER
== tvp
->cmd
) ?
1061 lg216x_get_frontend(fe
, c
) : 0;
1065 static int lg2160_set_frontend(struct dvb_frontend
*fe
)
1067 struct lg216x_state
*state
= fe
->demodulator_priv
;
1068 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1071 lg_dbg("(%d)\n", fe
->dtv_property_cache
.frequency
);
1073 if (fe
->ops
.tuner_ops
.set_params
) {
1074 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
1075 if (fe
->ops
.i2c_gate_ctrl
)
1076 fe
->ops
.i2c_gate_ctrl(fe
, 0);
1079 state
->current_frequency
= fe
->dtv_property_cache
.frequency
;
1082 ret
= lg2160_agc_fix(state
, 0, 0);
1085 ret
= lg2160_agc_polarity(state
, 0, 0);
1088 ret
= lg2160_tuner_pwr_save_polarity(state
, 1);
1091 ret
= lg216x_set_if(state
);
1094 ret
= lg2160_spectrum_polarity(state
, state
->cfg
->spectral_inversion
);
1098 /* be tuned before this point */
1099 ret
= lg216x_soft_reset(state
);
1103 ret
= lg2160_tuner_pwr_save(state
, 0);
1107 switch (state
->cfg
->lg_chip
) {
1109 ret
= lg2160_set_spi_clock(state
);
1114 ret
= lg2161_set_output_interface(state
);
1120 ret
= lg216x_set_parade(state
, fe
->dtv_property_cache
.atscmh_parade_id
);
1124 ret
= lg216x_set_ensemble(state
,
1125 fe
->dtv_property_cache
.atscmh_rs_frame_ensemble
);
1129 ret
= lg216x_initialize(state
);
1133 ret
= lg216x_enable_fic(state
, 1);
1136 lg216x_get_frontend(fe
, c
);
1141 /* ------------------------------------------------------------------------ */
1143 static int lg2160_read_lock_status(struct lg216x_state
*state
,
1144 int *acq_lock
, int *sync_lock
)
1152 ret
= lg216x_read_reg(state
, 0x011b, &val
);
1156 *sync_lock
= (val
& 0x20) ? 0 : 1;
1157 *acq_lock
= (val
& 0x40) ? 0 : 1;
1162 #ifdef USE_LG2161_LOCK_BITS
1163 static int lg2161_read_lock_status(struct lg216x_state
*state
,
1164 int *acq_lock
, int *sync_lock
)
1172 ret
= lg216x_read_reg(state
, 0x0304, &val
);
1176 *sync_lock
= (val
& 0x80) ? 0 : 1;
1178 ret
= lg216x_read_reg(state
, 0x011b, &val
);
1182 *acq_lock
= (val
& 0x40) ? 0 : 1;
1188 static int lg216x_read_lock_status(struct lg216x_state
*state
,
1189 int *acq_lock
, int *sync_lock
)
1191 #ifdef USE_LG2161_LOCK_BITS
1193 switch (state
->cfg
->lg_chip
) {
1195 ret
= lg2160_read_lock_status(state
, acq_lock
, sync_lock
);
1198 ret
= lg2161_read_lock_status(state
, acq_lock
, sync_lock
);
1206 return lg2160_read_lock_status(state
, acq_lock
, sync_lock
);
1210 static int lg216x_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
1212 struct lg216x_state
*state
= fe
->demodulator_priv
;
1213 int ret
, acq_lock
, sync_lock
;
1217 ret
= lg216x_read_lock_status(state
, &acq_lock
, &sync_lock
);
1222 acq_lock
? "SIGNALEXIST " : "",
1223 sync_lock
? "SYNCLOCK" : "");
1226 *status
|= FE_HAS_SIGNAL
;
1228 *status
|= FE_HAS_SYNC
;
1231 *status
|= FE_HAS_CARRIER
| FE_HAS_VITERBI
| FE_HAS_LOCK
;
1237 /* ------------------------------------------------------------------------ */
1239 static int lg2160_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
1241 struct lg216x_state
*state
= fe
->demodulator_priv
;
1247 ret
= lg216x_read_reg(state
, 0x0202, &snr1
);
1251 ret
= lg216x_read_reg(state
, 0x0203, &snr2
);
1255 if ((snr1
== 0xba) || (snr2
== 0xdf))
1259 *snr
= ((snr1
>> 4) * 100) + ((snr1
& 0x0f) * 10) + (snr2
>> 4);
1261 *snr
= (snr2
| (snr1
<< 8));
1267 static int lg2161_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
1269 struct lg216x_state
*state
= fe
->demodulator_priv
;
1275 ret
= lg216x_read_reg(state
, 0x0302, &snr1
);
1279 ret
= lg216x_read_reg(state
, 0x0303, &snr2
);
1283 if ((snr1
== 0xba) || (snr2
== 0xfd))
1287 *snr
= ((snr1
>> 4) * 100) + ((snr1
& 0x0f) * 10) + (snr2
& 0x0f);
1292 static int lg216x_read_signal_strength(struct dvb_frontend
*fe
,
1296 /* borrowed from lgdt330x.c
1298 * Calculate strength from SNR up to 35dB
1299 * Even though the SNR can go higher than 35dB,
1300 * there is some comfort factor in having a range of
1301 * strong signals that can show at 100%
1303 struct lg216x_state
*state
= fe
->demodulator_priv
;
1309 ret
= fe
->ops
.read_snr(fe
, &snr
);
1312 /* Rather than use the 8.8 value snr, use state->snr which is 8.24 */
1313 /* scale the range 0 - 35*2^24 into 0 - 65535 */
1314 if (state
->snr
>= 8960 * 0x10000)
1317 *strength
= state
->snr
/ 8960;
1325 /* ------------------------------------------------------------------------ */
1327 static int lg216x_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
1330 struct lg216x_state
*state
= fe
->demodulator_priv
;
1333 ret
= lg216x_read_rs_err_count(state
,
1334 &fe
->dtv_property_cache
.atscmh_rs_err
);
1338 *ucblocks
= fe
->dtv_property_cache
.atscmh_rs_err
;
1346 static int lg216x_get_tune_settings(struct dvb_frontend
*fe
,
1347 struct dvb_frontend_tune_settings
1350 fe_tune_settings
->min_delay_ms
= 500;
1355 static void lg216x_release(struct dvb_frontend
*fe
)
1357 struct lg216x_state
*state
= fe
->demodulator_priv
;
1362 static struct dvb_frontend_ops lg2160_ops
= {
1363 .delsys
= { SYS_ATSCMH
},
1365 .name
= "LG Electronics LG2160 ATSC/MH Frontend",
1366 .frequency_min
= 54000000,
1367 .frequency_max
= 858000000,
1368 .frequency_stepsize
= 62500,
1370 .i2c_gate_ctrl
= lg216x_i2c_gate_ctrl
,
1372 .init
= lg216x_init
,
1373 .sleep
= lg216x_sleep
,
1375 .get_property
= lg216x_get_property
,
1377 .set_frontend
= lg2160_set_frontend
,
1378 .get_frontend
= lg216x_get_frontend
,
1379 .get_tune_settings
= lg216x_get_tune_settings
,
1380 .read_status
= lg216x_read_status
,
1382 .read_ber
= lg216x_read_ber
,
1384 .read_signal_strength
= lg216x_read_signal_strength
,
1385 .read_snr
= lg2160_read_snr
,
1386 .read_ucblocks
= lg216x_read_ucblocks
,
1387 .release
= lg216x_release
,
1390 static struct dvb_frontend_ops lg2161_ops
= {
1391 .delsys
= { SYS_ATSCMH
},
1393 .name
= "LG Electronics LG2161 ATSC/MH Frontend",
1394 .frequency_min
= 54000000,
1395 .frequency_max
= 858000000,
1396 .frequency_stepsize
= 62500,
1398 .i2c_gate_ctrl
= lg216x_i2c_gate_ctrl
,
1400 .init
= lg216x_init
,
1401 .sleep
= lg216x_sleep
,
1403 .get_property
= lg216x_get_property
,
1405 .set_frontend
= lg2160_set_frontend
,
1406 .get_frontend
= lg216x_get_frontend
,
1407 .get_tune_settings
= lg216x_get_tune_settings
,
1408 .read_status
= lg216x_read_status
,
1410 .read_ber
= lg216x_read_ber
,
1412 .read_signal_strength
= lg216x_read_signal_strength
,
1413 .read_snr
= lg2161_read_snr
,
1414 .read_ucblocks
= lg216x_read_ucblocks
,
1415 .release
= lg216x_release
,
1418 struct dvb_frontend
*lg2160_attach(const struct lg2160_config
*config
,
1419 struct i2c_adapter
*i2c_adap
)
1421 struct lg216x_state
*state
= NULL
;
1423 lg_dbg("(%d-%04x)\n",
1424 i2c_adap
? i2c_adapter_id(i2c_adap
) : 0,
1425 config
? config
->i2c_addr
: 0);
1427 state
= kzalloc(sizeof(struct lg216x_state
), GFP_KERNEL
);
1431 state
->cfg
= config
;
1432 state
->i2c_adap
= i2c_adap
;
1433 state
->fic_ver
= 0xff;
1434 state
->parade_id
= 0xff;
1436 switch (config
->lg_chip
) {
1438 lg_warn("invalid chip requested, defaulting to LG2160");
1441 memcpy(&state
->frontend
.ops
, &lg2160_ops
,
1442 sizeof(struct dvb_frontend_ops
));
1445 memcpy(&state
->frontend
.ops
, &lg2161_ops
,
1446 sizeof(struct dvb_frontend_ops
));
1450 state
->frontend
.demodulator_priv
= state
;
1451 state
->current_frequency
= -1;
1452 /* parade 1 by default */
1453 state
->frontend
.dtv_property_cache
.atscmh_parade_id
= 1;
1455 return &state
->frontend
;
1457 EXPORT_SYMBOL(lg2160_attach
);
1459 MODULE_DESCRIPTION("LG Electronics LG216x ATSC/MH Demodulator Driver");
1460 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1461 MODULE_LICENSE("GPL");
1462 MODULE_VERSION("0.3");