2 Driver for Zarlink VP310/MT312/ZL10313 Satellite Channel Decoder
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 http://products.zarlink.com/product_profiles/MT312.htm
24 http://products.zarlink.com/product_profiles/SL1935.htm
27 #include <linux/delay.h>
28 #include <linux/errno.h>
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
35 #include "dvb_frontend.h"
36 #include "mt312_priv.h"
39 /* Max transfer size done by I2C transfer functions */
40 #define MAX_XFER_SIZE 64
43 struct i2c_adapter *i2c;
44 /* configuration settings */
45 const struct mt312_config *config;
46 struct dvb_frontend frontend;
54 #define dprintk(args...) \
57 printk(KERN_DEBUG "mt312: " args); \
60 #define MT312_PLL_CLK 10000000UL /* 10 MHz */
61 #define MT312_PLL_CLK_10_111 10111000UL /* 10.111 MHz */
63 static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg,
64 u8 *buf, const size_t count)
67 struct i2c_msg msg[2];
68 u8 regbuf[1] = { reg };
70 msg[0].addr = state->config->demod_address;
74 msg[1].addr = state->config->demod_address;
75 msg[1].flags = I2C_M_RD;
79 ret = i2c_transfer(state->i2c, msg, 2);
82 printk(KERN_DEBUG "%s: ret == %d\n", __func__, ret);
88 dprintk("R(%d):", reg & 0x7f);
89 for (i = 0; i < count; i++)
90 printk(KERN_CONT " %02x", buf[i]);
97 static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg,
98 const u8 *src, const size_t count)
101 u8 buf[MAX_XFER_SIZE];
104 if (1 + count > sizeof(buf)) {
106 "mt312: write: len=%zd is too big!\n", count);
112 dprintk("W(%d):", reg & 0x7f);
113 for (i = 0; i < count; i++)
114 printk(KERN_CONT " %02x", src[i]);
119 memcpy(&buf[1], src, count);
121 msg.addr = state->config->demod_address;
126 ret = i2c_transfer(state->i2c, &msg, 1);
129 dprintk("%s: ret == %d\n", __func__, ret);
136 static inline int mt312_readreg(struct mt312_state *state,
137 const enum mt312_reg_addr reg, u8 *val)
139 return mt312_read(state, reg, val, 1);
142 static inline int mt312_writereg(struct mt312_state *state,
143 const enum mt312_reg_addr reg, const u8 val)
145 return mt312_write(state, reg, &val, 1);
148 static inline u32 mt312_div(u32 a, u32 b)
150 return (a + (b / 2)) / b;
153 static int mt312_reset(struct mt312_state *state, const u8 full)
155 return mt312_writereg(state, RESET, full ? 0x80 : 0x40);
158 static int mt312_get_inversion(struct mt312_state *state,
159 fe_spectral_inversion_t *i)
164 ret = mt312_readreg(state, VIT_MODE, &vit_mode);
168 if (vit_mode & 0x80) /* auto inversion was used */
169 *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF;
174 static int mt312_get_symbol_rate(struct mt312_state *state, u32 *sr)
183 ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h);
187 if (sym_rate_h & 0x80) {
188 /* symbol rate search was used */
189 ret = mt312_writereg(state, MON_CTRL, 0x03);
193 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
197 monitor = (buf[0] << 8) | buf[1];
199 dprintk("sr(auto) = %u\n",
200 mt312_div(monitor * 15625, 4));
202 ret = mt312_writereg(state, MON_CTRL, 0x05);
206 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
210 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
212 ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf));
216 sym_rat_op = (buf[0] << 8) | buf[1];
218 dprintk("sym_rat_op=%d dec_ratio=%d\n",
219 sym_rat_op, dec_ratio);
220 dprintk("*sr(manual) = %lu\n",
221 (((state->xtal * 8192) / (sym_rat_op + 8192)) *
228 static int mt312_get_code_rate(struct mt312_state *state, fe_code_rate_t *cr)
230 const fe_code_rate_t fec_tab[8] =
231 { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
232 FEC_AUTO, FEC_AUTO };
237 ret = mt312_readreg(state, FEC_STATUS, &fec_status);
241 *cr = fec_tab[(fec_status >> 4) & 0x07];
246 static int mt312_initfe(struct dvb_frontend *fe)
248 struct mt312_state *state = fe->demodulator_priv;
253 ret = mt312_writereg(state, CONFIG,
254 (state->freq_mult == 6 ? 0x88 : 0x8c));
258 /* wait at least 150 usec */
262 ret = mt312_reset(state, 1);
266 /* Per datasheet, write correct values. 09/28/03 ACCJr.
267 * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */
269 u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02,
270 0x01, 0x00, 0x00, 0x00 };
272 ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def));
280 ret = mt312_writereg(state, GPP_CTRL, 0x80);
284 /* configure ZL10313 for optimal ADC performance */
287 ret = mt312_write(state, HW_CTRL, buf, 2);
291 /* enable MPEG output and ADCs */
292 ret = mt312_writereg(state, HW_CTRL, 0x00);
296 ret = mt312_writereg(state, MPEG_CTRL, 0x00);
304 buf[0] = mt312_div(state->xtal * state->freq_mult * 2, 1000000);
307 buf[1] = mt312_div(state->xtal, 22000 * 4);
309 ret = mt312_write(state, SYS_CLK, buf, sizeof(buf));
313 ret = mt312_writereg(state, SNR_THS_HIGH, 0x32);
317 /* different MOCLK polarity */
327 ret = mt312_writereg(state, OP_CTRL, buf[0]);
335 ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf));
339 ret = mt312_writereg(state, CS_SW_LIM, 0x69);
346 static int mt312_send_master_cmd(struct dvb_frontend *fe,
347 struct dvb_diseqc_master_cmd *c)
349 struct mt312_state *state = fe->demodulator_priv;
353 if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
356 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
360 ret = mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len);
364 ret = mt312_writereg(state, DISEQC_MODE,
365 (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
370 /* is there a better way to wait for message to be transmitted */
373 /* set DISEQC_MODE[2:0] to zero if a return message is expected */
374 if (c->msg[0] & 0x02) {
375 ret = mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40));
383 static int mt312_send_burst(struct dvb_frontend *fe, const fe_sec_mini_cmd_t c)
385 struct mt312_state *state = fe->demodulator_priv;
386 const u8 mini_tab[2] = { 0x02, 0x03 };
394 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
398 ret = mt312_writereg(state, DISEQC_MODE,
399 (diseqc_mode & 0x40) | mini_tab[c]);
406 static int mt312_set_tone(struct dvb_frontend *fe, const fe_sec_tone_mode_t t)
408 struct mt312_state *state = fe->demodulator_priv;
409 const u8 tone_tab[2] = { 0x01, 0x00 };
414 if (t > SEC_TONE_OFF)
417 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
421 ret = mt312_writereg(state, DISEQC_MODE,
422 (diseqc_mode & 0x40) | tone_tab[t]);
429 static int mt312_set_voltage(struct dvb_frontend *fe, const fe_sec_voltage_t v)
431 struct mt312_state *state = fe->demodulator_priv;
432 const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
435 if (v > SEC_VOLTAGE_OFF)
439 if (state->config->voltage_inverted)
442 return mt312_writereg(state, DISEQC_MODE, val);
445 static int mt312_read_status(struct dvb_frontend *fe, fe_status_t *s)
447 struct mt312_state *state = fe->demodulator_priv;
453 ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status));
457 dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x,"
458 " FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]);
460 if (status[0] & 0xc0)
461 *s |= FE_HAS_SIGNAL; /* signal noise ratio */
462 if (status[0] & 0x04)
463 *s |= FE_HAS_CARRIER; /* qpsk carrier lock */
464 if (status[2] & 0x02)
465 *s |= FE_HAS_VITERBI; /* viterbi lock */
466 if (status[2] & 0x04)
467 *s |= FE_HAS_SYNC; /* byte align lock */
468 if (status[0] & 0x01)
469 *s |= FE_HAS_LOCK; /* qpsk lock */
474 static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber)
476 struct mt312_state *state = fe->demodulator_priv;
480 ret = mt312_read(state, RS_BERCNT_H, buf, 3);
484 *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
489 static int mt312_read_signal_strength(struct dvb_frontend *fe,
490 u16 *signal_strength)
492 struct mt312_state *state = fe->demodulator_priv;
498 ret = mt312_read(state, AGC_H, buf, sizeof(buf));
502 agc = (buf[0] << 6) | (buf[1] >> 2);
503 err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
505 *signal_strength = agc;
507 dprintk("agc=%08x err_db=%hd\n", agc, err_db);
512 static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr)
514 struct mt312_state *state = fe->demodulator_priv;
518 ret = mt312_read(state, M_SNR_H, buf, sizeof(buf));
522 *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
527 static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc)
529 struct mt312_state *state = fe->demodulator_priv;
533 ret = mt312_read(state, RS_UBC_H, buf, sizeof(buf));
537 *ubc = (buf[0] << 8) | buf[1];
542 static int mt312_set_frontend(struct dvb_frontend *fe)
544 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
545 struct mt312_state *state = fe->demodulator_priv;
547 u8 buf[5], config_val;
550 const u8 fec_tab[10] =
551 { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
552 const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
554 dprintk("%s: Freq %d\n", __func__, p->frequency);
556 if ((p->frequency < fe->ops.info.frequency_min)
557 || (p->frequency > fe->ops.info.frequency_max))
560 if (((int)p->inversion < INVERSION_OFF)
561 || (p->inversion > INVERSION_ON))
564 if ((p->symbol_rate < fe->ops.info.symbol_rate_min)
565 || (p->symbol_rate > fe->ops.info.symbol_rate_max))
568 if (((int)p->fec_inner < FEC_NONE)
569 || (p->fec_inner > FEC_AUTO))
572 if ((p->fec_inner == FEC_4_5)
573 || (p->fec_inner == FEC_8_9))
578 /* For now we will do this only for the VP310.
579 * It should be better for the mt312 as well,
580 * but tuning will be slower. ACCJr 09/29/03
582 ret = mt312_readreg(state, CONFIG, &config_val);
585 if (p->symbol_rate >= 30000000) {
586 /* Note that 30MS/s should use 90MHz */
587 if (state->freq_mult == 6) {
588 /* We are running 60MHz */
589 state->freq_mult = 9;
590 ret = mt312_initfe(fe);
595 if (state->freq_mult == 9) {
596 /* We are running 90MHz */
597 state->freq_mult = 6;
598 ret = mt312_initfe(fe);
613 if (fe->ops.tuner_ops.set_params) {
614 fe->ops.tuner_ops.set_params(fe);
615 if (fe->ops.i2c_gate_ctrl)
616 fe->ops.i2c_gate_ctrl(fe, 0);
619 /* sr = (u16)(sr * 256.0 / 1000000.0) */
620 sr = mt312_div(p->symbol_rate * 4, 15625);
623 buf[0] = (sr >> 8) & 0x3f;
624 buf[1] = (sr >> 0) & 0xff;
627 buf[2] = inv_tab[p->inversion] | fec_tab[p->fec_inner];
630 buf[3] = 0x40; /* swap I and Q before QPSK demodulation */
632 if (p->symbol_rate < 10000000)
633 buf[3] |= 0x04; /* use afc mode */
638 ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf));
642 mt312_reset(state, 0);
647 static int mt312_get_frontend(struct dvb_frontend *fe)
649 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
650 struct mt312_state *state = fe->demodulator_priv;
653 ret = mt312_get_inversion(state, &p->inversion);
657 ret = mt312_get_symbol_rate(state, &p->symbol_rate);
661 ret = mt312_get_code_rate(state, &p->fec_inner);
668 static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
670 struct mt312_state *state = fe->demodulator_priv;
677 ret = mt312_readreg(state, GPP_CTRL, &val);
681 /* preserve this bit to not accidentally shutdown ADC */
691 ret = mt312_writereg(state, GPP_CTRL, val);
697 static int mt312_sleep(struct dvb_frontend *fe)
699 struct mt312_state *state = fe->demodulator_priv;
703 /* reset all registers to defaults */
704 ret = mt312_reset(state, 1);
708 if (state->id == ID_ZL10313) {
710 ret = mt312_writereg(state, GPP_CTRL, 0x00);
714 /* full shutdown of ADCs, mpeg bus tristated */
715 ret = mt312_writereg(state, HW_CTRL, 0x0d);
720 ret = mt312_readreg(state, CONFIG, &config);
725 ret = mt312_writereg(state, CONFIG, config & 0x7f);
732 static int mt312_get_tune_settings(struct dvb_frontend *fe,
733 struct dvb_frontend_tune_settings *fesettings)
735 fesettings->min_delay_ms = 50;
736 fesettings->step_size = 0;
737 fesettings->max_drift = 0;
741 static void mt312_release(struct dvb_frontend *fe)
743 struct mt312_state *state = fe->demodulator_priv;
747 #define MT312_SYS_CLK 90000000UL /* 90 MHz */
748 static struct dvb_frontend_ops mt312_ops = {
749 .delsys = { SYS_DVBS },
751 .name = "Zarlink ???? DVB-S",
752 .frequency_min = 950000,
753 .frequency_max = 2150000,
754 /* FIXME: adjust freq to real used xtal */
755 .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128,
756 .symbol_rate_min = MT312_SYS_CLK / 128, /* FIXME as above */
757 .symbol_rate_max = MT312_SYS_CLK / 2,
759 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
760 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
761 FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS |
765 .release = mt312_release,
767 .init = mt312_initfe,
768 .sleep = mt312_sleep,
769 .i2c_gate_ctrl = mt312_i2c_gate_ctrl,
771 .set_frontend = mt312_set_frontend,
772 .get_frontend = mt312_get_frontend,
773 .get_tune_settings = mt312_get_tune_settings,
775 .read_status = mt312_read_status,
776 .read_ber = mt312_read_ber,
777 .read_signal_strength = mt312_read_signal_strength,
778 .read_snr = mt312_read_snr,
779 .read_ucblocks = mt312_read_ucblocks,
781 .diseqc_send_master_cmd = mt312_send_master_cmd,
782 .diseqc_send_burst = mt312_send_burst,
783 .set_tone = mt312_set_tone,
784 .set_voltage = mt312_set_voltage,
787 struct dvb_frontend *mt312_attach(const struct mt312_config *config,
788 struct i2c_adapter *i2c)
790 struct mt312_state *state = NULL;
792 /* allocate memory for the internal state */
793 state = kzalloc(sizeof(struct mt312_state), GFP_KERNEL);
797 /* setup the state */
798 state->config = config;
801 /* check if the demod is there */
802 if (mt312_readreg(state, ID, &state->id) < 0)
805 /* create dvb_frontend */
806 memcpy(&state->frontend.ops, &mt312_ops,
807 sizeof(struct dvb_frontend_ops));
808 state->frontend.demodulator_priv = state;
812 strcpy(state->frontend.ops.info.name, "Zarlink VP310 DVB-S");
813 state->xtal = MT312_PLL_CLK;
814 state->freq_mult = 9;
817 strcpy(state->frontend.ops.info.name, "Zarlink MT312 DVB-S");
818 state->xtal = MT312_PLL_CLK;
819 state->freq_mult = 6;
822 strcpy(state->frontend.ops.info.name, "Zarlink ZL10313 DVB-S");
823 state->xtal = MT312_PLL_CLK_10_111;
824 state->freq_mult = 9;
827 printk(KERN_WARNING "Only Zarlink VP310/MT312/ZL10313"
828 " are supported chips.\n");
832 return &state->frontend;
838 EXPORT_SYMBOL(mt312_attach);
840 module_param(debug, int, 0644);
841 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
843 MODULE_DESCRIPTION("Zarlink VP310/MT312/ZL10313 DVB-S Demodulator driver");
846 MODULE_LICENSE("GPL");