2 * Support for Legend Silicon DMB-TH demodulator
4 * experimental support LGS8G42, LGS8G52
6 * Copyright (C) 2007,2008 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;
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:off).");
49 /* LGS8GXX internal helper functions */
51 static int lgs8gxx_write_reg(struct lgs8gxx_state *priv, u8 reg, u8 data)
54 u8 buf[] = { reg, data };
55 struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
57 msg.addr = priv->config->demod_address;
62 printk(KERN_DEBUG "%s: reg=0x%02X, data=0x%02X\n",
65 ret = i2c_transfer(priv->i2c, &msg, 1);
68 dprintk(KERN_DEBUG "%s: error reg=0x%x, data=0x%x, ret=%i\n",
69 __func__, reg, data, ret);
71 return (ret != 1) ? -1 : 0;
74 static int lgs8gxx_read_reg(struct lgs8gxx_state *priv, u8 reg, u8 *p_data)
81 struct i2c_msg msg[] = {
82 { .flags = 0, .buf = b0, .len = 1 },
83 { .flags = I2C_M_RD, .buf = b1, .len = 1 },
86 dev_addr = priv->config->demod_address;
89 msg[1].addr = msg[0].addr = dev_addr;
91 ret = i2c_transfer(priv->i2c, msg, 2);
93 dprintk(KERN_DEBUG "%s: error reg=0x%x, ret=%i\n",
100 printk(KERN_DEBUG "%s: reg=0x%02X, data=0x%02X\n",
101 __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 lgs8gxx_set_ad_mode(struct lgs8gxx_state *priv)
117 const struct lgs8gxx_config *config = priv->config;
120 if_conf = 0x10; /* AGC output on; */
123 ((config->ext_adc) ? 0x80 : 0x00) |
124 ((config->if_neg_center) ? 0x04 : 0x00) |
125 ((config->if_freq == 0) ? 0x08 : 0x00) | /* Baseband */
126 ((config->ext_adc && config->adc_signed) ? 0x02 : 0x00) |
127 ((config->ext_adc && config->if_neg_edge) ? 0x01 : 0x00);
129 if (config->ext_adc &&
130 (config->prod == LGS8GXX_PROD_LGS8G52)) {
131 lgs8gxx_write_reg(priv, 0xBA, 0x40);
134 lgs8gxx_write_reg(priv, 0x07, if_conf);
139 static int lgs8gxx_set_if_freq(struct lgs8gxx_state *priv, u32 freq /*in kHz*/)
145 if_clk = priv->config->if_clk_freq;
152 v32 = val & 0xFFFFFFFF;
153 dprintk("Set IF Freq to %dkHz\n", freq);
156 dprintk("Set IF Freq to baseband\n");
158 dprintk("AFC_INIT_FREQ = 0x%08X\n", v32);
160 lgs8gxx_write_reg(priv, 0x09, 0xFF & (v32));
161 lgs8gxx_write_reg(priv, 0x0A, 0xFF & (v32 >> 8));
162 lgs8gxx_write_reg(priv, 0x0B, 0xFF & (v32 >> 16));
163 lgs8gxx_write_reg(priv, 0x0C, 0xFF & (v32 >> 24));
168 static int lgs8gxx_set_mode_auto(struct lgs8gxx_state *priv)
172 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
173 lgs8gxx_write_reg(priv, 0xC6, 0x01);
175 lgs8gxx_read_reg(priv, 0x7E, &t);
176 lgs8gxx_write_reg(priv, 0x7E, t | 0x01);
178 /* clear FEC self reset */
179 lgs8gxx_read_reg(priv, 0xC5, &t);
180 lgs8gxx_write_reg(priv, 0xC5, t & 0xE0);
182 if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
183 /* FEC auto detect */
184 lgs8gxx_write_reg(priv, 0xC1, 0x03);
186 lgs8gxx_read_reg(priv, 0x7C, &t);
187 t = (t & 0x8C) | 0x03;
188 lgs8gxx_write_reg(priv, 0x7C, t);
192 if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
194 lgs8gxx_read_reg(priv, 0xC3, &t);
195 t = (t & 0xEF) | 0x10;
196 lgs8gxx_write_reg(priv, 0xC3, t);
199 if (priv->config->prod == LGS8GXX_PROD_LGS8G52)
200 lgs8gxx_write_reg(priv, 0xD9, 0x40);
205 static int lgs8gxx_set_mode_manual(struct lgs8gxx_state *priv)
210 /* turn off auto-detect; manual settings */
211 lgs8gxx_write_reg(priv, 0x7E, 0);
212 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
213 lgs8gxx_write_reg(priv, 0xC1, 0);
215 ret = lgs8gxx_read_reg(priv, 0xC5, &t);
216 t = (t & 0xE0) | 0x06;
217 lgs8gxx_write_reg(priv, 0xC5, t);
219 lgs8gxx_soft_reset(priv);
224 static int lgs8gxx_is_locked(struct lgs8gxx_state *priv, u8 *locked)
229 ret = lgs8gxx_read_reg(priv, 0x4B, &t);
233 *locked = ((t & 0xC0) == 0xC0) ? 1 : 0;
237 static int lgs8gxx_is_autodetect_finished(struct lgs8gxx_state *priv,
243 ret = lgs8gxx_read_reg(priv, 0xA4, &t);
247 *finished = ((t & 0x3) == 0x1) ? 1 : 0;
252 static int lgs8gxx_autolock_gi(struct lgs8gxx_state *priv, u8 gi, u8 *locked)
258 dprintk("try GI 945\n");
259 else if (gi == GI_595)
260 dprintk("try GI 595\n");
261 else if (gi == GI_420)
262 dprintk("try GI 420\n");
263 lgs8gxx_write_reg(priv, 0x04, gi);
264 lgs8gxx_soft_reset(priv);
266 err = lgs8gxx_is_autodetect_finished(priv, &ad_fini);
270 err = lgs8gxx_is_locked(priv, locked);
278 static int lgs8gxx_auto_detect(struct lgs8gxx_state *priv,
279 u8 *detected_param, u8 *gi)
283 u8 locked = 0, tmp_gi;
285 dprintk("%s\n", __func__);
287 lgs8gxx_set_mode_auto(priv);
289 lgs8gxx_write_reg(priv, 0x03, 00);
291 for (i = 0; i < 2; i++) {
292 for (j = 0; j < 2; j++) {
294 err = lgs8gxx_autolock_gi(priv, GI_945, &locked);
300 for (j = 0; j < 2; j++) {
302 err = lgs8gxx_autolock_gi(priv, GI_420, &locked);
309 err = lgs8gxx_autolock_gi(priv, GI_595, &locked);
317 if ((err == 0) && (locked == 1)) {
320 lgs8gxx_read_reg(priv, 0xA2, &t);
323 if (tmp_gi == GI_945)
324 dprintk("GI 945 locked\n");
325 else if (tmp_gi == GI_595)
326 dprintk("GI 595 locked\n");
327 else if (tmp_gi == GI_420)
328 dprintk("GI 420 locked\n");
338 static void lgs8gxx_auto_lock(struct lgs8gxx_state *priv)
342 u8 detected_param = 0;
344 err = lgs8gxx_auto_detect(priv, &detected_param, &gi);
347 dprintk("lgs8gxx_auto_detect failed\n");
350 /* Apply detected parameters */
351 if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
352 u8 inter_leave_len = detected_param & TIM_MASK ;
353 inter_leave_len = (inter_leave_len == TIM_LONG) ? 0x60 : 0x40;
354 detected_param &= CF_MASK | SC_MASK | LGS_FEC_MASK;
355 detected_param |= inter_leave_len;
357 lgs8gxx_write_reg(priv, 0x7D, detected_param);
358 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
359 lgs8gxx_write_reg(priv, 0xC0, detected_param);
360 /* lgs8gxx_soft_reset(priv); */
362 /* Enter manual mode */
363 lgs8gxx_set_mode_manual(priv);
367 priv->curr_gi = 945; break;
369 priv->curr_gi = 595; break;
371 priv->curr_gi = 420; break;
373 priv->curr_gi = 945; break;
377 static int lgs8gxx_set_mpeg_mode(struct lgs8gxx_state *priv,
378 u8 serial, u8 clk_pol, u8 clk_gated)
383 ret = lgs8gxx_read_reg(priv, 0xC2, &t);
388 t |= serial ? TS_SERIAL : TS_PARALLEL;
389 t |= clk_pol ? TS_CLK_INVERTED : TS_CLK_NORMAL;
390 t |= clk_gated ? TS_CLK_GATED : TS_CLK_FREERUN;
392 ret = lgs8gxx_write_reg(priv, 0xC2, t);
400 /* LGS8913 demod frontend functions */
402 static int lgs8913_init(struct lgs8gxx_state *priv)
406 /* LGS8913 specific */
407 lgs8gxx_write_reg(priv, 0xc1, 0x3);
409 lgs8gxx_read_reg(priv, 0x7c, &t);
410 lgs8gxx_write_reg(priv, 0x7c, (t&0x8c) | 0x3);
412 /* LGS8913 specific */
413 lgs8gxx_read_reg(priv, 0xc3, &t);
414 lgs8gxx_write_reg(priv, 0xc3, t&0x10);
420 static int lgs8gxx_init(struct dvb_frontend *fe)
422 struct lgs8gxx_state *priv =
423 (struct lgs8gxx_state *)fe->demodulator_priv;
424 const struct lgs8gxx_config *config = priv->config;
427 dprintk("%s\n", __func__);
429 lgs8gxx_read_reg(priv, 0, &data);
430 dprintk("reg 0 = 0x%02X\n", data);
432 /* Setup MPEG output format */
433 err = lgs8gxx_set_mpeg_mode(priv, config->serial_ts,
435 config->ts_clk_gated);
439 if (config->prod == LGS8GXX_PROD_LGS8913)
441 lgs8gxx_set_if_freq(priv, priv->config->if_freq);
442 if (config->prod != LGS8GXX_PROD_LGS8913)
443 lgs8gxx_set_ad_mode(priv);
448 static void lgs8gxx_release(struct dvb_frontend *fe)
450 struct lgs8gxx_state *state = fe->demodulator_priv;
451 dprintk("%s\n", __func__);
457 static int lgs8gxx_write(struct dvb_frontend *fe, u8 *buf, int len)
459 struct lgs8gxx_state *priv = fe->demodulator_priv;
464 return lgs8gxx_write_reg(priv, buf[0], buf[1]);
467 static int lgs8gxx_set_fe(struct dvb_frontend *fe,
468 struct dvb_frontend_parameters *fe_params)
470 struct lgs8gxx_state *priv = fe->demodulator_priv;
472 dprintk("%s\n", __func__);
475 if (fe->ops.tuner_ops.set_params) {
476 fe->ops.tuner_ops.set_params(fe, fe_params);
477 if (fe->ops.i2c_gate_ctrl)
478 fe->ops.i2c_gate_ctrl(fe, 0);
481 /* start auto lock */
482 lgs8gxx_auto_lock(priv);
489 static int lgs8gxx_get_fe(struct dvb_frontend *fe,
490 struct dvb_frontend_parameters *fe_params)
492 struct lgs8gxx_state *priv = fe->demodulator_priv;
495 dprintk("%s\n", __func__);
497 /* TODO: get real readings from device */
498 /* inversion status */
499 fe_params->inversion = INVERSION_OFF;
502 fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
505 lgs8gxx_read_reg(priv, 0x7D, &t);
506 fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
507 fe_params->u.ofdm.code_rate_LP = FEC_AUTO;
510 switch (t & SC_MASK) {
512 fe_params->u.ofdm.constellation = QAM_64;
515 fe_params->u.ofdm.constellation = QAM_32;
518 fe_params->u.ofdm.constellation = QAM_16;
522 fe_params->u.ofdm.constellation = QPSK;
525 fe_params->u.ofdm.constellation = QAM_64;
528 /* transmission mode */
529 fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
532 fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
535 fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
541 int lgs8gxx_get_tune_settings(struct dvb_frontend *fe,
542 struct dvb_frontend_tune_settings *fesettings)
544 /* FIXME: copy from tda1004x.c */
545 fesettings->min_delay_ms = 800;
546 fesettings->step_size = 0;
547 fesettings->max_drift = 0;
551 static int lgs8gxx_read_status(struct dvb_frontend *fe, fe_status_t *fe_status)
553 struct lgs8gxx_state *priv = fe->demodulator_priv;
557 dprintk("%s\n", __func__);
559 ret = lgs8gxx_read_reg(priv, 0x4B, &t);
563 dprintk("Reg 0x4B: 0x%02X\n", t);
566 if (priv->config->prod == LGS8GXX_PROD_LGS8913) {
567 if ((t & 0x40) == 0x40)
568 *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER;
569 if ((t & 0x80) == 0x80)
570 *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC |
573 if ((t & 0x80) == 0x80)
574 *fe_status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
575 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
579 dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
583 static int lgs8gxx_read_signal_agc(struct lgs8gxx_state *priv, u16 *signal)
588 dprintk("%s()\n", __func__);
589 lgs8gxx_read_reg(priv, 0x3F, &agc_lvl[0]);
590 lgs8gxx_read_reg(priv, 0x3E, &agc_lvl[1]);
596 dprintk("agc_lvl: 0x%04X\n", v);
618 static int lgs8913_read_signal_strength(struct lgs8gxx_state *priv, u16 *signal)
621 s16 max_strength = 0;
623 u16 i, gi = priv->curr_gi;
625 dprintk("%s\n", __func__);
627 ret = lgs8gxx_read_reg(priv, 0x4B, &t);
631 if (fake_signal_str) {
632 if ((t & 0xC0) == 0xC0) {
633 dprintk("Fake signal strength as 50\n");
640 dprintk("gi = %d\n", gi);
641 for (i = 0; i < gi; i++) {
644 lgs8gxx_write_reg(priv, 0x84, 0x03 & (i >> 8));
645 lgs8gxx_write_reg(priv, 0x83, i & 0xFF);
647 lgs8gxx_read_reg(priv, 0x94, &str);
648 if (max_strength < str)
652 *signal = max_strength;
653 dprintk("%s: signal=0x%02X\n", __func__, *signal);
655 lgs8gxx_read_reg(priv, 0x95, &t);
656 dprintk("%s: AVG Noise=0x%02X\n", __func__, t);
661 static int lgs8gxx_read_signal_strength(struct dvb_frontend *fe, u16 *signal)
663 struct lgs8gxx_state *priv = fe->demodulator_priv;
665 if (priv->config->prod == LGS8GXX_PROD_LGS8913)
666 return lgs8913_read_signal_strength(priv, signal);
668 return lgs8gxx_read_signal_agc(priv, signal);
671 static int lgs8gxx_read_snr(struct dvb_frontend *fe, u16 *snr)
673 struct lgs8gxx_state *priv = fe->demodulator_priv;
677 lgs8gxx_read_reg(priv, 0x95, &t);
678 dprintk("AVG Noise=0x%02X\n", t);
681 dprintk("snr=0x%x\n", *snr);
686 static int lgs8gxx_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
689 dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
693 static int lgs8gxx_read_ber(struct dvb_frontend *fe, u32 *ber)
695 struct lgs8gxx_state *priv = fe->demodulator_priv;
697 u32 total_cnt, err_cnt;
699 dprintk("%s\n", __func__);
701 lgs8gxx_write_reg(priv, 0xc6, 0x01);
702 lgs8gxx_write_reg(priv, 0xc6, 0x41);
703 lgs8gxx_write_reg(priv, 0xc6, 0x01);
707 lgs8gxx_write_reg(priv, 0xc6, 0x81);
708 lgs8gxx_read_reg(priv, 0xd0, &r0);
709 lgs8gxx_read_reg(priv, 0xd1, &r1);
710 lgs8gxx_read_reg(priv, 0xd2, &r2);
711 lgs8gxx_read_reg(priv, 0xd3, &r3);
712 total_cnt = (r3 << 24) | (r2 << 16) | (r1 << 8) | (r0);
713 lgs8gxx_read_reg(priv, 0xd4, &r0);
714 lgs8gxx_read_reg(priv, 0xd5, &r1);
715 lgs8gxx_read_reg(priv, 0xd6, &r2);
716 lgs8gxx_read_reg(priv, 0xd7, &r3);
717 err_cnt = (r3 << 24) | (r2 << 16) | (r1 << 8) | (r0);
718 dprintk("error=%d total=%d\n", err_cnt, total_cnt);
723 *ber = err_cnt * 100 / total_cnt;
725 dprintk("%s: ber=0x%x\n", __func__, *ber);
729 static int lgs8gxx_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
731 struct lgs8gxx_state *priv = fe->demodulator_priv;
733 if (priv->config->tuner_address == 0)
736 u8 v = 0x80 | priv->config->tuner_address;
737 return lgs8gxx_write_reg(priv, 0x01, v);
739 return lgs8gxx_write_reg(priv, 0x01, 0);
742 static struct dvb_frontend_ops lgs8gxx_ops = {
744 .name = "Legend Silicon LGS8913/LGS8GXX DMB-TH",
746 .frequency_min = 474000000,
747 .frequency_max = 858000000,
748 .frequency_stepsize = 10000,
752 FE_CAN_TRANSMISSION_MODE_AUTO |
753 FE_CAN_GUARD_INTERVAL_AUTO
756 .release = lgs8gxx_release,
758 .init = lgs8gxx_init,
759 .write = lgs8gxx_write,
760 .i2c_gate_ctrl = lgs8gxx_i2c_gate_ctrl,
762 .set_frontend = lgs8gxx_set_fe,
763 .get_frontend = lgs8gxx_get_fe,
764 .get_tune_settings = lgs8gxx_get_tune_settings,
766 .read_status = lgs8gxx_read_status,
767 .read_ber = lgs8gxx_read_ber,
768 .read_signal_strength = lgs8gxx_read_signal_strength,
769 .read_snr = lgs8gxx_read_snr,
770 .read_ucblocks = lgs8gxx_read_ucblocks,
773 struct dvb_frontend *lgs8gxx_attach(const struct lgs8gxx_config *config,
774 struct i2c_adapter *i2c)
776 struct lgs8gxx_state *priv = NULL;
779 dprintk("%s()\n", __func__);
781 if (config == NULL || i2c == NULL)
784 priv = kzalloc(sizeof(struct lgs8gxx_state), GFP_KERNEL);
788 priv->config = config;
791 /* check if the demod is there */
792 if (lgs8gxx_read_reg(priv, 0, &data) != 0) {
793 dprintk("%s lgs8gxx not found at i2c addr 0x%02X\n",
794 __func__, priv->config->demod_address);
798 lgs8gxx_read_reg(priv, 1, &data);
800 memcpy(&priv->frontend.ops, &lgs8gxx_ops,
801 sizeof(struct dvb_frontend_ops));
802 priv->frontend.demodulator_priv = priv;
804 return &priv->frontend;
807 dprintk("%s() error_out\n", __func__);
812 EXPORT_SYMBOL(lgs8gxx_attach);
814 MODULE_DESCRIPTION("Legend Silicon LGS8913/LGS8GXX DMB-TH demodulator driver");
815 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
816 MODULE_LICENSE("GPL");