2 Driver for ST STV0299 demodulator
4 Copyright (C) 2001-2002 Convergence Integrated Media GmbH
5 <ralph@convergence.de>,
6 <holger@convergence.de>,
12 Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>
17 Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
18 & Andreas Oberritter <obi@linuxtv.org>
21 Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B
23 Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:
25 Support for Philips SU1278 on Technotrend hardware
27 Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>
29 This program is free software; you can redistribute it and/or modify
30 it under the terms of the GNU General Public License as published by
31 the Free Software Foundation; either version 2 of the License, or
32 (at your option) any later version.
34 This program is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 #include <linux/init.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/string.h>
49 #include <linux/slab.h>
50 #include <linux/jiffies.h>
51 #include <asm/div64.h>
53 #include "dvb_frontend.h"
56 struct stv0299_state {
57 struct i2c_adapter* i2c;
58 const struct stv0299_config* config;
59 struct dvb_frontend frontend;
64 enum fe_code_rate fec_inner;
71 #define STATUS_UCBLOCKS 1
74 static int debug_legacy_dish_switch;
75 #define dprintk(args...) \
77 if (debug) printk(KERN_DEBUG "stv0299: " args); \
81 static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
84 u8 buf [] = { reg, data };
85 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
87 ret = i2c_transfer (state->i2c, &msg, 1);
90 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
91 "ret == %i)\n", __func__, reg, data, ret);
93 return (ret != 1) ? -EREMOTEIO : 0;
96 static int stv0299_write(struct dvb_frontend* fe, const u8 buf[], int len)
98 struct stv0299_state* state = fe->demodulator_priv;
103 return stv0299_writeregI(state, buf[0], buf[1]);
106 static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
111 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
112 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
114 ret = i2c_transfer (state->i2c, msg, 2);
117 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
123 static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
126 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 },
127 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
129 ret = i2c_transfer (state->i2c, msg, 2);
132 dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
134 return ret == 2 ? 0 : ret;
137 static int stv0299_set_FEC(struct stv0299_state *state, enum fe_code_rate fec)
139 dprintk ("%s\n", __func__);
144 return stv0299_writeregI (state, 0x31, 0x1f);
148 return stv0299_writeregI (state, 0x31, 0x01);
152 return stv0299_writeregI (state, 0x31, 0x02);
156 return stv0299_writeregI (state, 0x31, 0x04);
160 return stv0299_writeregI (state, 0x31, 0x08);
164 return stv0299_writeregI (state, 0x31, 0x10);
173 static enum fe_code_rate stv0299_get_fec(struct stv0299_state *state)
175 static enum fe_code_rate fec_tab[] = { FEC_2_3, FEC_3_4, FEC_5_6,
179 dprintk ("%s\n", __func__);
181 index = stv0299_readreg (state, 0x1b);
187 return fec_tab [index];
190 static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
192 unsigned long start = jiffies;
194 dprintk ("%s\n", __func__);
196 while (stv0299_readreg(state, 0x0a) & 1) {
197 if (jiffies - start > timeout) {
198 dprintk ("%s: timeout!!\n", __func__);
207 static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
209 unsigned long start = jiffies;
211 dprintk ("%s\n", __func__);
213 while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
214 if (jiffies - start > timeout) {
215 dprintk ("%s: timeout!!\n", __func__);
224 static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
226 struct stv0299_state* state = fe->demodulator_priv;
230 // check rate is within limits
231 if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
233 // calculate value to program
235 big += (state->config->mclk-1); // round correctly
236 do_div(big, state->config->mclk);
239 return state->config->set_symbol_rate(fe, srate, ratio);
242 static int stv0299_get_symbolrate (struct stv0299_state* state)
244 u32 Mclk = state->config->mclk / 4096L;
250 dprintk ("%s\n", __func__);
252 stv0299_readregs (state, 0x1f, sfr, 3);
253 stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1);
255 srate = (sfr[0] << 8) | sfr[1];
258 srate += (sfr[2] >> 4) * Mclk / 256;
259 offset = (s32) rtf * (srate / 4096L);
262 dprintk ("%s : srate = %i\n", __func__, srate);
263 dprintk ("%s : ofset = %i\n", __func__, offset);
274 static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
275 struct dvb_diseqc_master_cmd *m)
277 struct stv0299_state* state = fe->demodulator_priv;
281 dprintk ("%s\n", __func__);
283 if (stv0299_wait_diseqc_idle (state, 100) < 0)
286 val = stv0299_readreg (state, 0x08);
288 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
291 for (i=0; i<m->msg_len; i++) {
292 if (stv0299_wait_diseqc_fifo (state, 100) < 0)
295 if (stv0299_writeregI (state, 0x09, m->msg[i]))
299 if (stv0299_wait_diseqc_idle (state, 100) < 0)
305 static int stv0299_send_diseqc_burst(struct dvb_frontend *fe,
306 enum fe_sec_mini_cmd burst)
308 struct stv0299_state* state = fe->demodulator_priv;
311 dprintk ("%s\n", __func__);
313 if (stv0299_wait_diseqc_idle (state, 100) < 0)
316 val = stv0299_readreg (state, 0x08);
318 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
321 if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
324 if (stv0299_wait_diseqc_idle (state, 100) < 0)
327 if (stv0299_writeregI (state, 0x08, val))
333 static int stv0299_set_tone(struct dvb_frontend *fe,
334 enum fe_sec_tone_mode tone)
336 struct stv0299_state* state = fe->demodulator_priv;
339 if (stv0299_wait_diseqc_idle (state, 100) < 0)
342 val = stv0299_readreg (state, 0x08);
346 return stv0299_writeregI (state, 0x08, val | 0x3);
349 return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
356 static int stv0299_set_voltage(struct dvb_frontend *fe,
357 enum fe_sec_voltage voltage)
359 struct stv0299_state* state = fe->demodulator_priv;
363 dprintk("%s: %s\n", __func__,
364 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
365 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
367 reg0x08 = stv0299_readreg (state, 0x08);
368 reg0x0c = stv0299_readreg (state, 0x0c);
371 * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
374 reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6);
378 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
379 reg0x0c |= 0x10; /* OP1 off, OP0 on */
381 reg0x0c |= 0x40; /* OP1 on, OP0 off */
384 reg0x0c |= 0x50; /* OP1 on, OP0 on */
386 case SEC_VOLTAGE_OFF:
395 if (state->config->op0_off)
398 stv0299_writeregI(state, 0x08, reg0x08);
399 return stv0299_writeregI(state, 0x0c, reg0x0c);
402 static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
404 struct stv0299_state* state = fe->demodulator_priv;
410 struct timeval nexttime;
411 struct timeval tv[10];
413 reg0x08 = stv0299_readreg (state, 0x08);
414 reg0x0c = stv0299_readreg (state, 0x0c);
416 stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
417 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
421 if (debug_legacy_dish_switch)
422 printk ("%s switch command: 0x%04lx\n",__func__, cmd);
424 do_gettimeofday (&nexttime);
425 if (debug_legacy_dish_switch)
427 stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
429 dvb_frontend_sleep_until(&nexttime, 32000);
431 for (i=0; i<9; i++) {
432 if (debug_legacy_dish_switch)
433 do_gettimeofday (&tv[i+1]);
434 if((cmd & 0x01) != last) {
435 /* set voltage to (last ? 13V : 18V) */
436 stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
437 last = (last) ? 0 : 1;
443 dvb_frontend_sleep_until(&nexttime, 8000);
445 if (debug_legacy_dish_switch) {
446 printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
447 __func__, fe->dvb->num);
448 for (i = 1; i < 10; i++)
449 printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
455 static int stv0299_init (struct dvb_frontend* fe)
457 struct stv0299_state* state = fe->demodulator_priv;
462 dprintk("stv0299: init chip\n");
464 stv0299_writeregI(state, 0x02, 0x30 | state->mcr_reg);
467 for (i = 0; ; i += 2) {
468 reg = state->config->inittab[i];
469 val = state->config->inittab[i+1];
470 if (reg == 0xff && val == 0xff)
472 if (reg == 0x0c && state->config->op0_off)
475 state->mcr_reg = val & 0xf;
476 stv0299_writeregI(state, reg, val);
482 static int stv0299_read_status(struct dvb_frontend *fe,
483 enum fe_status *status)
485 struct stv0299_state* state = fe->demodulator_priv;
487 u8 signal = 0xff - stv0299_readreg (state, 0x18);
488 u8 sync = stv0299_readreg (state, 0x1b);
490 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
494 *status |= FE_HAS_SIGNAL;
497 *status |= FE_HAS_CARRIER;
500 *status |= FE_HAS_VITERBI;
503 *status |= FE_HAS_SYNC;
505 if ((sync & 0x98) == 0x98)
506 *status |= FE_HAS_LOCK;
511 static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
513 struct stv0299_state* state = fe->demodulator_priv;
515 if (state->errmode != STATUS_BER)
518 *ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8);
523 static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
525 struct stv0299_state* state = fe->demodulator_priv;
527 s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
528 | stv0299_readreg (state, 0x19));
530 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__,
531 stv0299_readreg (state, 0x18),
532 stv0299_readreg (state, 0x19), (int) signal);
534 signal = signal * 5 / 4;
535 *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
540 static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
542 struct stv0299_state* state = fe->demodulator_priv;
544 s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
545 | stv0299_readreg (state, 0x25));
546 xsnr = 3 * (xsnr - 0xa100);
547 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
552 static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
554 struct stv0299_state* state = fe->demodulator_priv;
556 if (state->errmode != STATUS_UCBLOCKS)
559 state->ucblocks += stv0299_readreg(state, 0x1e);
560 state->ucblocks += (stv0299_readreg(state, 0x1d) << 8);
561 *ucblocks = state->ucblocks;
566 static int stv0299_set_frontend(struct dvb_frontend *fe)
568 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
569 struct stv0299_state* state = fe->demodulator_priv;
572 dprintk ("%s : FE_SET_FRONTEND\n", __func__);
573 if (state->config->set_ts_params)
574 state->config->set_ts_params(fe, 0);
577 if (p->inversion == INVERSION_OFF) invval = 0;
578 else if (p->inversion == INVERSION_ON) invval = 1;
580 printk("stv0299 does not support auto-inversion\n");
583 if (state->config->invert) invval = (~invval) & 1;
584 stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
586 if (fe->ops.tuner_ops.set_params) {
587 fe->ops.tuner_ops.set_params(fe);
588 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
591 stv0299_set_FEC(state, p->fec_inner);
592 stv0299_set_symbolrate(fe, p->symbol_rate);
593 stv0299_writeregI(state, 0x22, 0x00);
594 stv0299_writeregI(state, 0x23, 0x00);
596 state->tuner_frequency = p->frequency;
597 state->fec_inner = p->fec_inner;
598 state->symbol_rate = p->symbol_rate;
603 static int stv0299_get_frontend(struct dvb_frontend *fe)
605 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
606 struct stv0299_state* state = fe->demodulator_priv;
610 derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
611 | stv0299_readreg (state, 0x23));
613 derot_freq *= (state->config->mclk >> 16);
617 p->frequency += derot_freq;
619 invval = stv0299_readreg (state, 0x0c) & 1;
620 if (state->config->invert) invval = (~invval) & 1;
621 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
623 p->fec_inner = stv0299_get_fec(state);
624 p->symbol_rate = stv0299_get_symbolrate(state);
629 static int stv0299_sleep(struct dvb_frontend* fe)
631 struct stv0299_state* state = fe->demodulator_priv;
633 stv0299_writeregI(state, 0x02, 0xb0 | state->mcr_reg);
634 state->initialised = 0;
639 static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
641 struct stv0299_state* state = fe->demodulator_priv;
644 stv0299_writeregI(state, 0x05, 0xb5);
646 stv0299_writeregI(state, 0x05, 0x35);
652 static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
654 struct stv0299_state* state = fe->demodulator_priv;
655 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
657 fesettings->min_delay_ms = state->config->min_delay_ms;
658 if (p->symbol_rate < 10000000) {
659 fesettings->step_size = p->symbol_rate / 32000;
660 fesettings->max_drift = 5000;
662 fesettings->step_size = p->symbol_rate / 16000;
663 fesettings->max_drift = p->symbol_rate / 2000;
668 static void stv0299_release(struct dvb_frontend* fe)
670 struct stv0299_state* state = fe->demodulator_priv;
674 static struct dvb_frontend_ops stv0299_ops;
676 struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
677 struct i2c_adapter* i2c)
679 struct stv0299_state* state = NULL;
682 /* allocate memory for the internal state */
683 state = kzalloc(sizeof(struct stv0299_state), GFP_KERNEL);
684 if (state == NULL) goto error;
686 /* setup the state */
687 state->config = config;
689 state->initialised = 0;
690 state->tuner_frequency = 0;
691 state->symbol_rate = 0;
692 state->fec_inner = 0;
693 state->errmode = STATUS_BER;
695 /* check if the demod is there */
696 stv0299_writeregI(state, 0x02, 0x30); /* standby off */
698 id = stv0299_readreg(state, 0x00);
700 /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
701 /* register 0x00 might contain 0x80 when returning from standby */
702 if (id != 0xa1 && id != 0x80) goto error;
704 /* create dvb_frontend */
705 memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
706 state->frontend.demodulator_priv = state;
707 return &state->frontend;
714 static struct dvb_frontend_ops stv0299_ops = {
715 .delsys = { SYS_DVBS },
717 .name = "ST STV0299 DVB-S",
718 .frequency_min = 950000,
719 .frequency_max = 2150000,
720 .frequency_stepsize = 125, /* kHz for QPSK frontends */
721 .frequency_tolerance = 0,
722 .symbol_rate_min = 1000000,
723 .symbol_rate_max = 45000000,
724 .symbol_rate_tolerance = 500, /* ppm */
725 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
726 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
731 .release = stv0299_release,
733 .init = stv0299_init,
734 .sleep = stv0299_sleep,
735 .write = stv0299_write,
736 .i2c_gate_ctrl = stv0299_i2c_gate_ctrl,
738 .set_frontend = stv0299_set_frontend,
739 .get_frontend = stv0299_get_frontend,
740 .get_tune_settings = stv0299_get_tune_settings,
742 .read_status = stv0299_read_status,
743 .read_ber = stv0299_read_ber,
744 .read_signal_strength = stv0299_read_signal_strength,
745 .read_snr = stv0299_read_snr,
746 .read_ucblocks = stv0299_read_ucblocks,
748 .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
749 .diseqc_send_burst = stv0299_send_diseqc_burst,
750 .set_tone = stv0299_set_tone,
751 .set_voltage = stv0299_set_voltage,
752 .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
755 module_param(debug_legacy_dish_switch, int, 0444);
756 MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
758 module_param(debug, int, 0644);
759 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
761 MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
762 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
763 "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
764 MODULE_LICENSE("GPL");
766 EXPORT_SYMBOL(stv0299_attach);