1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Driver for Zarlink VP310/MT312/ZL10313 Satellite Channel Decoder
5 Copyright (C) 2003 Andreas Oberritter <obi@linuxtv.org>
6 Copyright (C) 2008 Matthias Schwarzott <zzam@gentoo.org>
10 http://products.zarlink.com/product_profiles/MT312.htm
11 http://products.zarlink.com/product_profiles/SL1935.htm
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/string.h>
20 #include <linux/slab.h>
22 #include <media/dvb_frontend.h>
23 #include "mt312_priv.h"
26 /* Max transfer size done by I2C transfer functions */
27 #define MAX_XFER_SIZE 64
30 struct i2c_adapter *i2c;
31 /* configuration settings */
32 const struct mt312_config *config;
33 struct dvb_frontend frontend;
41 #define dprintk(args...) \
44 printk(KERN_DEBUG "mt312: " args); \
47 #define MT312_PLL_CLK 10000000UL /* 10 MHz */
48 #define MT312_PLL_CLK_10_111 10111000UL /* 10.111 MHz */
50 static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg,
51 u8 *buf, const size_t count)
54 struct i2c_msg msg[2];
55 u8 regbuf[1] = { reg };
57 msg[0].addr = state->config->demod_address;
61 msg[1].addr = state->config->demod_address;
62 msg[1].flags = I2C_M_RD;
66 ret = i2c_transfer(state->i2c, msg, 2);
69 printk(KERN_DEBUG "%s: ret == %d\n", __func__, ret);
75 dprintk("R(%d):", reg & 0x7f);
76 for (i = 0; i < count; i++)
77 printk(KERN_CONT " %02x", buf[i]);
84 static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg,
85 const u8 *src, const size_t count)
88 u8 buf[MAX_XFER_SIZE];
91 if (1 + count > sizeof(buf)) {
93 "mt312: write: len=%zu is too big!\n", count);
99 dprintk("W(%d):", reg & 0x7f);
100 for (i = 0; i < count; i++)
101 printk(KERN_CONT " %02x", src[i]);
106 memcpy(&buf[1], src, count);
108 msg.addr = state->config->demod_address;
113 ret = i2c_transfer(state->i2c, &msg, 1);
116 dprintk("%s: ret == %d\n", __func__, ret);
123 static inline int mt312_readreg(struct mt312_state *state,
124 const enum mt312_reg_addr reg, u8 *val)
126 return mt312_read(state, reg, val, 1);
129 static inline int mt312_writereg(struct mt312_state *state,
130 const enum mt312_reg_addr reg, const u8 val)
132 u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
135 return mt312_write(state, reg, &tmp, 1);
138 static int mt312_reset(struct mt312_state *state, const u8 full)
140 return mt312_writereg(state, RESET, full ? 0x80 : 0x40);
143 static int mt312_get_inversion(struct mt312_state *state,
144 enum fe_spectral_inversion *i)
149 ret = mt312_readreg(state, VIT_MODE, &vit_mode);
153 if (vit_mode & 0x80) /* auto inversion was used */
154 *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF;
159 static int mt312_get_symbol_rate(struct mt312_state *state, u32 *sr)
168 ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h);
172 if (sym_rate_h & 0x80) {
173 /* symbol rate search was used */
174 ret = mt312_writereg(state, MON_CTRL, 0x03);
178 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
182 monitor = (buf[0] << 8) | buf[1];
184 dprintk("sr(auto) = %u\n",
185 DIV_ROUND_CLOSEST(monitor * 15625, 4));
187 ret = mt312_writereg(state, MON_CTRL, 0x05);
191 ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
195 dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
197 ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf));
201 sym_rat_op = (buf[0] << 8) | buf[1];
203 dprintk("sym_rat_op=%d dec_ratio=%d\n",
204 sym_rat_op, dec_ratio);
205 dprintk("*sr(manual) = %lu\n",
206 (((state->xtal * 8192) / (sym_rat_op + 8192)) *
213 static int mt312_get_code_rate(struct mt312_state *state, enum fe_code_rate *cr)
215 const enum fe_code_rate fec_tab[8] =
216 { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
217 FEC_AUTO, FEC_AUTO };
222 ret = mt312_readreg(state, FEC_STATUS, &fec_status);
226 *cr = fec_tab[(fec_status >> 4) & 0x07];
231 static int mt312_initfe(struct dvb_frontend *fe)
233 struct mt312_state *state = fe->demodulator_priv;
238 ret = mt312_writereg(state, CONFIG,
239 (state->freq_mult == 6 ? 0x88 : 0x8c));
243 /* wait at least 150 usec */
247 ret = mt312_reset(state, 1);
251 /* Per datasheet, write correct values. 09/28/03 ACCJr.
252 * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */
254 u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02,
255 0x01, 0x00, 0x00, 0x00 };
257 ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def));
265 ret = mt312_writereg(state, GPP_CTRL, 0x80);
269 /* configure ZL10313 for optimal ADC performance */
272 ret = mt312_write(state, HW_CTRL, buf, 2);
276 /* enable MPEG output and ADCs */
277 ret = mt312_writereg(state, HW_CTRL, 0x00);
281 ret = mt312_writereg(state, MPEG_CTRL, 0x00);
289 buf[0] = DIV_ROUND_CLOSEST(state->xtal * state->freq_mult * 2, 1000000);
292 buf[1] = DIV_ROUND_CLOSEST(state->xtal, 22000 * 4);
294 ret = mt312_write(state, SYS_CLK, buf, sizeof(buf));
298 ret = mt312_writereg(state, SNR_THS_HIGH, 0x32);
302 /* different MOCLK polarity */
312 ret = mt312_writereg(state, OP_CTRL, buf[0]);
320 ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf));
324 ret = mt312_writereg(state, CS_SW_LIM, 0x69);
331 static int mt312_send_master_cmd(struct dvb_frontend *fe,
332 struct dvb_diseqc_master_cmd *c)
334 struct mt312_state *state = fe->demodulator_priv;
338 if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
341 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
345 ret = mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len);
349 ret = mt312_writereg(state, DISEQC_MODE,
350 (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
355 /* is there a better way to wait for message to be transmitted */
358 /* set DISEQC_MODE[2:0] to zero if a return message is expected */
359 if (c->msg[0] & 0x02) {
360 ret = mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40));
368 static int mt312_send_burst(struct dvb_frontend *fe,
369 const enum fe_sec_mini_cmd c)
371 struct mt312_state *state = fe->demodulator_priv;
372 const u8 mini_tab[2] = { 0x02, 0x03 };
380 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
384 ret = mt312_writereg(state, DISEQC_MODE,
385 (diseqc_mode & 0x40) | mini_tab[c]);
392 static int mt312_set_tone(struct dvb_frontend *fe,
393 const enum fe_sec_tone_mode t)
395 struct mt312_state *state = fe->demodulator_priv;
396 const u8 tone_tab[2] = { 0x01, 0x00 };
401 if (t > SEC_TONE_OFF)
404 ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
408 ret = mt312_writereg(state, DISEQC_MODE,
409 (diseqc_mode & 0x40) | tone_tab[t]);
416 static int mt312_set_voltage(struct dvb_frontend *fe,
417 const enum fe_sec_voltage v)
419 struct mt312_state *state = fe->demodulator_priv;
420 const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
423 if (v > SEC_VOLTAGE_OFF)
427 if (state->config->voltage_inverted)
430 return mt312_writereg(state, DISEQC_MODE, val);
433 static int mt312_read_status(struct dvb_frontend *fe, enum fe_status *s)
435 struct mt312_state *state = fe->demodulator_priv;
441 ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status));
445 dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x, FEC_STATUS: 0x%02x\n",
446 status[0], status[1], status[2]);
448 if (status[0] & 0xc0)
449 *s |= FE_HAS_SIGNAL; /* signal noise ratio */
450 if (status[0] & 0x04)
451 *s |= FE_HAS_CARRIER; /* qpsk carrier lock */
452 if (status[2] & 0x02)
453 *s |= FE_HAS_VITERBI; /* viterbi lock */
454 if (status[2] & 0x04)
455 *s |= FE_HAS_SYNC; /* byte align lock */
456 if (status[0] & 0x01)
457 *s |= FE_HAS_LOCK; /* qpsk lock */
462 static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber)
464 struct mt312_state *state = fe->demodulator_priv;
468 ret = mt312_read(state, RS_BERCNT_H, buf, 3);
472 *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
477 static int mt312_read_signal_strength(struct dvb_frontend *fe,
478 u16 *signal_strength)
480 struct mt312_state *state = fe->demodulator_priv;
486 ret = mt312_read(state, AGC_H, buf, sizeof(buf));
490 agc = (buf[0] << 6) | (buf[1] >> 2);
491 err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
493 *signal_strength = agc;
495 dprintk("agc=%08x err_db=%hd\n", agc, err_db);
500 static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr)
502 struct mt312_state *state = fe->demodulator_priv;
506 ret = mt312_read(state, M_SNR_H, buf, sizeof(buf));
510 *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
515 static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc)
517 struct mt312_state *state = fe->demodulator_priv;
521 ret = mt312_read(state, RS_UBC_H, buf, sizeof(buf));
525 *ubc = (buf[0] << 8) | buf[1];
530 static int mt312_set_frontend(struct dvb_frontend *fe)
532 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
533 struct mt312_state *state = fe->demodulator_priv;
535 u8 buf[5], config_val;
538 const u8 fec_tab[10] =
539 { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
540 const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
542 dprintk("%s: Freq %d\n", __func__, p->frequency);
544 if ((p->frequency < fe->ops.info.frequency_min_hz / kHz)
545 || (p->frequency > fe->ops.info.frequency_max_hz / kHz))
548 if (((int)p->inversion < INVERSION_OFF)
549 || (p->inversion > INVERSION_ON))
552 if ((p->symbol_rate < fe->ops.info.symbol_rate_min)
553 || (p->symbol_rate > fe->ops.info.symbol_rate_max))
556 if (((int)p->fec_inner < FEC_NONE)
557 || (p->fec_inner > FEC_AUTO))
560 if ((p->fec_inner == FEC_4_5)
561 || (p->fec_inner == FEC_8_9))
566 /* For now we will do this only for the VP310.
567 * It should be better for the mt312 as well,
568 * but tuning will be slower. ACCJr 09/29/03
570 ret = mt312_readreg(state, CONFIG, &config_val);
573 if (p->symbol_rate >= 30000000) {
574 /* Note that 30MS/s should use 90MHz */
575 if (state->freq_mult == 6) {
576 /* We are running 60MHz */
577 state->freq_mult = 9;
578 ret = mt312_initfe(fe);
583 if (state->freq_mult == 9) {
584 /* We are running 90MHz */
585 state->freq_mult = 6;
586 ret = mt312_initfe(fe);
601 if (fe->ops.tuner_ops.set_params) {
602 fe->ops.tuner_ops.set_params(fe);
603 if (fe->ops.i2c_gate_ctrl)
604 fe->ops.i2c_gate_ctrl(fe, 0);
607 /* sr = (u16)(sr * 256.0 / 1000000.0) */
608 sr = DIV_ROUND_CLOSEST(p->symbol_rate * 4, 15625);
611 buf[0] = (sr >> 8) & 0x3f;
612 buf[1] = (sr >> 0) & 0xff;
615 buf[2] = inv_tab[p->inversion] | fec_tab[p->fec_inner];
618 buf[3] = 0x40; /* swap I and Q before QPSK demodulation */
620 if (p->symbol_rate < 10000000)
621 buf[3] |= 0x04; /* use afc mode */
626 ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf));
630 ret = mt312_reset(state, 0);
637 static int mt312_get_frontend(struct dvb_frontend *fe,
638 struct dtv_frontend_properties *p)
640 struct mt312_state *state = fe->demodulator_priv;
643 ret = mt312_get_inversion(state, &p->inversion);
647 ret = mt312_get_symbol_rate(state, &p->symbol_rate);
651 ret = mt312_get_code_rate(state, &p->fec_inner);
658 static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
660 struct mt312_state *state = fe->demodulator_priv;
667 ret = mt312_readreg(state, GPP_CTRL, &val);
671 /* preserve this bit to not accidentally shutdown ADC */
681 ret = mt312_writereg(state, GPP_CTRL, val);
687 static int mt312_sleep(struct dvb_frontend *fe)
689 struct mt312_state *state = fe->demodulator_priv;
693 /* reset all registers to defaults */
694 ret = mt312_reset(state, 1);
698 if (state->id == ID_ZL10313) {
700 ret = mt312_writereg(state, GPP_CTRL, 0x00);
704 /* full shutdown of ADCs, mpeg bus tristated */
705 ret = mt312_writereg(state, HW_CTRL, 0x0d);
710 ret = mt312_readreg(state, CONFIG, &config);
715 ret = mt312_writereg(state, CONFIG, config & 0x7f);
722 static int mt312_get_tune_settings(struct dvb_frontend *fe,
723 struct dvb_frontend_tune_settings *fesettings)
725 fesettings->min_delay_ms = 50;
726 fesettings->step_size = 0;
727 fesettings->max_drift = 0;
731 static void mt312_release(struct dvb_frontend *fe)
733 struct mt312_state *state = fe->demodulator_priv;
737 #define MT312_SYS_CLK 90000000UL /* 90 MHz */
738 static const struct dvb_frontend_ops mt312_ops = {
739 .delsys = { SYS_DVBS },
741 .name = "Zarlink ???? DVB-S",
742 .frequency_min_hz = 950 * MHz,
743 .frequency_max_hz = 2150 * MHz,
744 /* FIXME: adjust freq to real used xtal */
745 .frequency_stepsize_hz = MT312_PLL_CLK / 128,
746 .symbol_rate_min = MT312_SYS_CLK / 128, /* FIXME as above */
747 .symbol_rate_max = MT312_SYS_CLK / 2,
749 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
750 FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
751 FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS |
755 .release = mt312_release,
757 .init = mt312_initfe,
758 .sleep = mt312_sleep,
759 .i2c_gate_ctrl = mt312_i2c_gate_ctrl,
761 .set_frontend = mt312_set_frontend,
762 .get_frontend = mt312_get_frontend,
763 .get_tune_settings = mt312_get_tune_settings,
765 .read_status = mt312_read_status,
766 .read_ber = mt312_read_ber,
767 .read_signal_strength = mt312_read_signal_strength,
768 .read_snr = mt312_read_snr,
769 .read_ucblocks = mt312_read_ucblocks,
771 .diseqc_send_master_cmd = mt312_send_master_cmd,
772 .diseqc_send_burst = mt312_send_burst,
773 .set_tone = mt312_set_tone,
774 .set_voltage = mt312_set_voltage,
777 struct dvb_frontend *mt312_attach(const struct mt312_config *config,
778 struct i2c_adapter *i2c)
780 struct mt312_state *state = NULL;
782 /* allocate memory for the internal state */
783 state = kzalloc(sizeof(struct mt312_state), GFP_KERNEL);
787 /* setup the state */
788 state->config = config;
791 /* check if the demod is there */
792 if (mt312_readreg(state, ID, &state->id) < 0)
795 /* create dvb_frontend */
796 memcpy(&state->frontend.ops, &mt312_ops,
797 sizeof(struct dvb_frontend_ops));
798 state->frontend.demodulator_priv = state;
802 strscpy(state->frontend.ops.info.name, "Zarlink VP310 DVB-S",
803 sizeof(state->frontend.ops.info.name));
804 state->xtal = MT312_PLL_CLK;
805 state->freq_mult = 9;
808 strscpy(state->frontend.ops.info.name, "Zarlink MT312 DVB-S",
809 sizeof(state->frontend.ops.info.name));
810 state->xtal = MT312_PLL_CLK;
811 state->freq_mult = 6;
814 strscpy(state->frontend.ops.info.name, "Zarlink ZL10313 DVB-S",
815 sizeof(state->frontend.ops.info.name));
816 state->xtal = MT312_PLL_CLK_10_111;
817 state->freq_mult = 9;
820 printk(KERN_WARNING "Only Zarlink VP310/MT312/ZL10313 are supported chips.\n");
824 return &state->frontend;
830 EXPORT_SYMBOL_GPL(mt312_attach);
832 module_param(debug, int, 0644);
833 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
835 MODULE_DESCRIPTION("Zarlink VP310/MT312/ZL10313 DVB-S Demodulator driver");
836 MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>");
837 MODULE_AUTHOR("Matthias Schwarzott <zzam@gentoo.org>");
838 MODULE_LICENSE("GPL");