1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Driver for VES1893 and VES1993 QPSK Demodulators
5 Copyright (C) 1999 Convergence Integrated Media GmbH <ralph@convergence.de>
6 Copyright (C) 2001 Ronny Strutz <3des@elitedvb.de>
7 Copyright (C) 2002 Dennis Noermann <dennis.noermann@noernet.de>
8 Copyright (C) 2002-2003 Andreas Oberritter <obi@linuxtv.org>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
20 #include <media/dvb_frontend.h>
24 struct ves1x93_state {
25 struct i2c_adapter* i2c;
26 /* configuration settings */
27 const struct ves1x93_config* config;
28 struct dvb_frontend frontend;
30 /* previous uncorrected block counter */
31 enum fe_spectral_inversion inversion;
40 #define dprintk if (debug) printk
42 #define DEMOD_VES1893 0
43 #define DEMOD_VES1993 1
45 static u8 init_1893_tab [] = {
46 0x01, 0xa4, 0x35, 0x80, 0x2a, 0x0b, 0x55, 0xc4,
47 0x09, 0x69, 0x00, 0x86, 0x4c, 0x28, 0x7f, 0x00,
48 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x80, 0x00, 0x21, 0xb0, 0x14, 0x00, 0xdc, 0x00,
50 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x55, 0x00, 0x00, 0x7f, 0x00
55 static u8 init_1993_tab [] = {
56 0x00, 0x9c, 0x35, 0x80, 0x6a, 0x09, 0x72, 0x8c,
57 0x09, 0x6b, 0x00, 0x00, 0x4c, 0x08, 0x00, 0x00,
58 0x00, 0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x80, 0x40, 0x21, 0xb0, 0x00, 0x00, 0x00, 0x10,
60 0x81, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x55, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03,
63 0x00, 0x00, 0x0e, 0x80, 0x00
66 static u8 init_1893_wtab[] =
68 1,1,1,1,1,1,1,1, 1,1,0,0,1,1,0,0,
69 0,1,0,0,0,0,0,0, 1,0,1,1,0,0,0,1,
70 1,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,
74 static u8 init_1993_wtab[] =
76 1,1,1,1,1,1,1,1, 1,1,0,0,1,1,0,0,
77 0,1,0,0,0,0,0,0, 1,1,1,1,0,0,0,1,
78 1,1,1,0,0,0,0,0, 0,0,1,1,0,0,0,0,
79 1,1,1,0,1,1,1,1, 1,1,1,1,1
82 static int ves1x93_writereg (struct ves1x93_state* state, u8 reg, u8 data)
84 u8 buf [] = { 0x00, reg, data };
85 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 3 };
88 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
89 dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __func__, err, reg, data);
96 static u8 ves1x93_readreg (struct ves1x93_state* state, u8 reg)
99 u8 b0 [] = { 0x00, reg };
101 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
102 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
104 ret = i2c_transfer (state->i2c, msg, 2);
106 if (ret != 2) return ret;
111 static int ves1x93_clr_bit (struct ves1x93_state* state)
114 ves1x93_writereg (state, 0, state->init_1x93_tab[0] & 0xfe);
115 ves1x93_writereg (state, 0, state->init_1x93_tab[0]);
120 static int ves1x93_set_inversion(struct ves1x93_state *state,
121 enum fe_spectral_inversion inversion)
126 * inversion on/off are interchanged because i and q seem to
127 * be swapped on the hardware
144 return ves1x93_writereg (state, 0x0c, (state->init_1x93_tab[0x0c] & 0x3f) | val);
147 static int ves1x93_set_fec(struct ves1x93_state *state, enum fe_code_rate fec)
150 return ves1x93_writereg (state, 0x0d, 0x08);
151 else if (fec < FEC_1_2 || fec > FEC_8_9)
154 return ves1x93_writereg (state, 0x0d, fec - FEC_1_2);
157 static enum fe_code_rate ves1x93_get_fec(struct ves1x93_state *state)
159 return FEC_1_2 + ((ves1x93_readreg (state, 0x0d) >> 4) & 0x7);
162 static int ves1x93_set_symbolrate (struct ves1x93_state* state, u32 srate)
166 u8 ADCONF, FCONF, FNR, AGCR;
171 dprintk("%s: srate == %d\n", __func__, (unsigned int) srate);
173 if (srate > state->config->xin/2)
174 srate = state->config->xin/2;
179 #define MUL (1UL<<26)
181 FIN = (state->config->xin + 6000) >> 4;
186 tmp = (tmp % FIN) << 8;
187 ratio = (ratio << 8) + tmp / FIN;
189 tmp = (tmp % FIN) << 8;
190 ratio = (ratio << 8) + tmp / FIN;
194 if (ratio < MUL/3) FNR = 0;
195 if (ratio < (MUL*11)/50) FNR = 1;
196 if (ratio < MUL/6) FNR = 2;
197 if (ratio < MUL/9) FNR = 3;
198 if (ratio < MUL/12) FNR = 4;
199 if (ratio < (MUL*11)/200) FNR = 5;
200 if (ratio < MUL/24) FNR = 6;
201 if (ratio < (MUL*27)/1000) FNR = 7;
202 if (ratio < MUL/48) FNR = 8;
203 if (ratio < (MUL*137)/10000) FNR = 9;
211 FCONF = 0x88 | (FNR >> 1) | ((FNR & 0x01) << 5);
212 /*FCONF = 0x80 | ((FNR & 0x01) << 5) | (((FNR > 1) & 0x03) << 3) | ((FNR >> 1) & 0x07);*/
215 BDR = (( (ratio << (FNR >> 1)) >> 4) + 1) >> 1;
216 BDRI = ( ((FIN << 8) / ((srate << (FNR >> 1)) >> 2)) + 1) >> 1;
218 dprintk("FNR= %d\n", FNR);
219 dprintk("ratio= %08x\n", (unsigned int) ratio);
220 dprintk("BDR= %08x\n", (unsigned int) BDR);
221 dprintk("BDRI= %02x\n", (unsigned int) BDRI);
226 ves1x93_writereg (state, 0x06, 0xff & BDR);
227 ves1x93_writereg (state, 0x07, 0xff & (BDR >> 8));
228 ves1x93_writereg (state, 0x08, 0x0f & (BDR >> 16));
230 ves1x93_writereg (state, 0x09, BDRI);
231 ves1x93_writereg (state, 0x20, ADCONF);
232 ves1x93_writereg (state, 0x21, FCONF);
234 AGCR = state->init_1x93_tab[0x05];
235 if (state->config->invert_pwm)
243 ves1x93_writereg (state, 0x05, AGCR);
245 /* ves1993 hates this, will lose lock */
246 if (state->demod_type != DEMOD_VES1993)
247 ves1x93_clr_bit (state);
252 static int ves1x93_init (struct dvb_frontend* fe)
254 struct ves1x93_state* state = fe->demodulator_priv;
258 dprintk("%s: init chip\n", __func__);
260 for (i = 0; i < state->tab_size; i++) {
261 if (state->init_1x93_wtab[i]) {
262 val = state->init_1x93_tab[i];
264 if (state->config->invert_pwm && (i == 0x05)) val |= 0x20; /* invert PWM */
265 ves1x93_writereg (state, i, val);
272 static int ves1x93_set_voltage(struct dvb_frontend *fe,
273 enum fe_sec_voltage voltage)
275 struct ves1x93_state* state = fe->demodulator_priv;
279 return ves1x93_writereg (state, 0x1f, 0x20);
281 return ves1x93_writereg (state, 0x1f, 0x30);
282 case SEC_VOLTAGE_OFF:
283 return ves1x93_writereg (state, 0x1f, 0x00);
289 static int ves1x93_read_status(struct dvb_frontend *fe,
290 enum fe_status *status)
292 struct ves1x93_state* state = fe->demodulator_priv;
294 u8 sync = ves1x93_readreg (state, 0x0e);
297 * The ves1893 sometimes returns sync values that make no sense,
298 * because, e.g., the SIGNAL bit is 0, while some of the higher
299 * bits are 1 (and how can there be a CARRIER w/o a SIGNAL?).
300 * Tests showed that the VITERBI and SYNC bits are returned
301 * reliably, while the SIGNAL and CARRIER bits ar sometimes wrong.
302 * If such a case occurs, we read the value again, until we get a
305 int maxtry = 10; /* just for safety - let's not get stuck here */
306 while ((sync & 0x03) != 0x03 && (sync & 0x0c) && maxtry--) {
308 sync = ves1x93_readreg (state, 0x0e);
314 *status |= FE_HAS_SIGNAL;
317 *status |= FE_HAS_CARRIER;
320 *status |= FE_HAS_VITERBI;
323 *status |= FE_HAS_SYNC;
325 if ((sync & 0x1f) == 0x1f)
326 *status |= FE_HAS_LOCK;
331 static int ves1x93_read_ber(struct dvb_frontend* fe, u32* ber)
333 struct ves1x93_state* state = fe->demodulator_priv;
335 *ber = ves1x93_readreg (state, 0x15);
336 *ber |= (ves1x93_readreg (state, 0x16) << 8);
337 *ber |= ((ves1x93_readreg (state, 0x17) & 0x0F) << 16);
343 static int ves1x93_read_signal_strength(struct dvb_frontend* fe, u16* strength)
345 struct ves1x93_state* state = fe->demodulator_priv;
347 u8 signal = ~ves1x93_readreg (state, 0x0b);
348 *strength = (signal << 8) | signal;
353 static int ves1x93_read_snr(struct dvb_frontend* fe, u16* snr)
355 struct ves1x93_state* state = fe->demodulator_priv;
357 u8 _snr = ~ves1x93_readreg (state, 0x1c);
358 *snr = (_snr << 8) | _snr;
363 static int ves1x93_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
365 struct ves1x93_state* state = fe->demodulator_priv;
367 *ucblocks = ves1x93_readreg (state, 0x18) & 0x7f;
369 if (*ucblocks == 0x7f)
370 *ucblocks = 0xffffffff; /* counter overflow... */
372 ves1x93_writereg (state, 0x18, 0x00); /* reset the counter */
373 ves1x93_writereg (state, 0x18, 0x80); /* dto. */
378 static int ves1x93_set_frontend(struct dvb_frontend *fe)
380 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
381 struct ves1x93_state* state = fe->demodulator_priv;
383 if (fe->ops.tuner_ops.set_params) {
384 fe->ops.tuner_ops.set_params(fe);
385 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
387 ves1x93_set_inversion (state, p->inversion);
388 ves1x93_set_fec(state, p->fec_inner);
389 ves1x93_set_symbolrate(state, p->symbol_rate);
390 state->inversion = p->inversion;
391 state->frequency = p->frequency;
396 static int ves1x93_get_frontend(struct dvb_frontend *fe,
397 struct dtv_frontend_properties *p)
399 struct ves1x93_state* state = fe->demodulator_priv;
402 afc = ((int)((char)(ves1x93_readreg (state, 0x0a) << 1)))/2;
403 afc = (afc * (int)(p->symbol_rate/1000/8))/16;
405 p->frequency = state->frequency - afc;
408 * inversion indicator is only valid
409 * if auto inversion was used
411 if (state->inversion == INVERSION_AUTO)
412 p->inversion = (ves1x93_readreg (state, 0x0f) & 2) ?
413 INVERSION_OFF : INVERSION_ON;
414 p->fec_inner = ves1x93_get_fec(state);
415 /* XXX FIXME: timing offset !! */
420 static int ves1x93_sleep(struct dvb_frontend* fe)
422 struct ves1x93_state* state = fe->demodulator_priv;
424 return ves1x93_writereg (state, 0x00, 0x08);
427 static void ves1x93_release(struct dvb_frontend* fe)
429 struct ves1x93_state* state = fe->demodulator_priv;
433 static int ves1x93_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
435 struct ves1x93_state* state = fe->demodulator_priv;
438 return ves1x93_writereg(state, 0x00, 0x11);
440 return ves1x93_writereg(state, 0x00, 0x01);
444 static const struct dvb_frontend_ops ves1x93_ops;
446 struct dvb_frontend* ves1x93_attach(const struct ves1x93_config* config,
447 struct i2c_adapter* i2c)
449 struct ves1x93_state* state = NULL;
452 /* allocate memory for the internal state */
453 state = kzalloc(sizeof(struct ves1x93_state), GFP_KERNEL);
454 if (state == NULL) goto error;
456 /* setup the state */
457 state->config = config;
459 state->inversion = INVERSION_OFF;
461 /* check if the demod is there + identify it */
462 identity = ves1x93_readreg(state, 0x1e);
464 case 0xdc: /* VES1893A rev1 */
465 printk("ves1x93: Detected ves1893a rev1\n");
466 state->demod_type = DEMOD_VES1893;
467 state->init_1x93_tab = init_1893_tab;
468 state->init_1x93_wtab = init_1893_wtab;
469 state->tab_size = sizeof(init_1893_tab);
472 case 0xdd: /* VES1893A rev2 */
473 printk("ves1x93: Detected ves1893a rev2\n");
474 state->demod_type = DEMOD_VES1893;
475 state->init_1x93_tab = init_1893_tab;
476 state->init_1x93_wtab = init_1893_wtab;
477 state->tab_size = sizeof(init_1893_tab);
480 case 0xde: /* VES1993 */
481 printk("ves1x93: Detected ves1993\n");
482 state->demod_type = DEMOD_VES1993;
483 state->init_1x93_tab = init_1993_tab;
484 state->init_1x93_wtab = init_1993_wtab;
485 state->tab_size = sizeof(init_1993_tab);
492 /* create dvb_frontend */
493 memcpy(&state->frontend.ops, &ves1x93_ops, sizeof(struct dvb_frontend_ops));
494 state->frontend.demodulator_priv = state;
495 return &state->frontend;
502 static const struct dvb_frontend_ops ves1x93_ops = {
503 .delsys = { SYS_DVBS },
505 .name = "VLSI VES1x93 DVB-S",
506 .frequency_min_hz = 950 * MHz,
507 .frequency_max_hz = 2150 * MHz,
508 .frequency_stepsize_hz = 125 * kHz,
509 .frequency_tolerance_hz = 29500 * kHz,
510 .symbol_rate_min = 1000000,
511 .symbol_rate_max = 45000000,
512 /* .symbol_rate_tolerance = ???,*/
513 .caps = FE_CAN_INVERSION_AUTO |
514 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
515 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
519 .release = ves1x93_release,
521 .init = ves1x93_init,
522 .sleep = ves1x93_sleep,
523 .i2c_gate_ctrl = ves1x93_i2c_gate_ctrl,
525 .set_frontend = ves1x93_set_frontend,
526 .get_frontend = ves1x93_get_frontend,
528 .read_status = ves1x93_read_status,
529 .read_ber = ves1x93_read_ber,
530 .read_signal_strength = ves1x93_read_signal_strength,
531 .read_snr = ves1x93_read_snr,
532 .read_ucblocks = ves1x93_read_ucblocks,
534 .set_voltage = ves1x93_set_voltage,
537 module_param(debug, int, 0644);
539 MODULE_DESCRIPTION("VLSI VES1x93 DVB-S Demodulator driver");
540 MODULE_AUTHOR("Ralph Metzler");
541 MODULE_LICENSE("GPL");
543 EXPORT_SYMBOL(ves1x93_attach);