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 fe_code_rate_t fec_inner;
70 #define STATUS_UCBLOCKS 1
73 static int debug_legacy_dish_switch;
74 #define dprintk(args...) \
76 if (debug) printk(KERN_DEBUG "stv0299: " args); \
80 static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
83 u8 buf [] = { reg, data };
84 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
86 ret = i2c_transfer (state->i2c, &msg, 1);
89 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
90 "ret == %i)\n", __func__, reg, data, ret);
92 return (ret != 1) ? -EREMOTEIO : 0;
95 static int stv0299_write(struct dvb_frontend* fe, u8 *buf, int len)
97 struct stv0299_state* state = fe->demodulator_priv;
102 return stv0299_writeregI(state, buf[0], buf[1]);
105 static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
110 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
111 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
113 ret = i2c_transfer (state->i2c, msg, 2);
116 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
122 static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
125 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 },
126 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
128 ret = i2c_transfer (state->i2c, msg, 2);
131 dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
133 return ret == 2 ? 0 : ret;
136 static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec)
138 dprintk ("%s\n", __func__);
143 return stv0299_writeregI (state, 0x31, 0x1f);
147 return stv0299_writeregI (state, 0x31, 0x01);
151 return stv0299_writeregI (state, 0x31, 0x02);
155 return stv0299_writeregI (state, 0x31, 0x04);
159 return stv0299_writeregI (state, 0x31, 0x08);
163 return stv0299_writeregI (state, 0x31, 0x10);
172 static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state)
174 static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
178 dprintk ("%s\n", __func__);
180 index = stv0299_readreg (state, 0x1b);
186 return fec_tab [index];
189 static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
191 unsigned long start = jiffies;
193 dprintk ("%s\n", __func__);
195 while (stv0299_readreg(state, 0x0a) & 1) {
196 if (jiffies - start > timeout) {
197 dprintk ("%s: timeout!!\n", __func__);
206 static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
208 unsigned long start = jiffies;
210 dprintk ("%s\n", __func__);
212 while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
213 if (jiffies - start > timeout) {
214 dprintk ("%s: timeout!!\n", __func__);
223 static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
225 struct stv0299_state* state = fe->demodulator_priv;
229 // check rate is within limits
230 if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
232 // calculate value to program
234 big += (state->config->mclk-1); // round correctly
235 do_div(big, state->config->mclk);
238 return state->config->set_symbol_rate(fe, srate, ratio);
241 static int stv0299_get_symbolrate (struct stv0299_state* state)
243 u32 Mclk = state->config->mclk / 4096L;
249 dprintk ("%s\n", __func__);
251 stv0299_readregs (state, 0x1f, sfr, 3);
252 stv0299_readregs (state, 0x1a, (u8 *)&rtf, 1);
254 srate = (sfr[0] << 8) | sfr[1];
257 srate += (sfr[2] >> 4) * Mclk / 256;
258 offset = (s32) rtf * (srate / 4096L);
261 dprintk ("%s : srate = %i\n", __func__, srate);
262 dprintk ("%s : ofset = %i\n", __func__, offset);
273 static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
274 struct dvb_diseqc_master_cmd *m)
276 struct stv0299_state* state = fe->demodulator_priv;
280 dprintk ("%s\n", __func__);
282 if (stv0299_wait_diseqc_idle (state, 100) < 0)
285 val = stv0299_readreg (state, 0x08);
287 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
290 for (i=0; i<m->msg_len; i++) {
291 if (stv0299_wait_diseqc_fifo (state, 100) < 0)
294 if (stv0299_writeregI (state, 0x09, m->msg[i]))
298 if (stv0299_wait_diseqc_idle (state, 100) < 0)
304 static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
306 struct stv0299_state* state = fe->demodulator_priv;
309 dprintk ("%s\n", __func__);
311 if (stv0299_wait_diseqc_idle (state, 100) < 0)
314 val = stv0299_readreg (state, 0x08);
316 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
319 if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
322 if (stv0299_wait_diseqc_idle (state, 100) < 0)
325 if (stv0299_writeregI (state, 0x08, val))
331 static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
333 struct stv0299_state* state = fe->demodulator_priv;
336 if (stv0299_wait_diseqc_idle (state, 100) < 0)
339 val = stv0299_readreg (state, 0x08);
343 return stv0299_writeregI (state, 0x08, val | 0x3);
346 return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
353 static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
355 struct stv0299_state* state = fe->demodulator_priv;
359 dprintk("%s: %s\n", __func__,
360 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
361 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
363 reg0x08 = stv0299_readreg (state, 0x08);
364 reg0x0c = stv0299_readreg (state, 0x0c);
367 * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
370 reg0x08 = (reg0x08 & 0x3f) | (state->config->lock_output << 6);
374 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
375 reg0x0c |= 0x10; /* OP1 off, OP0 on */
377 reg0x0c |= 0x40; /* OP1 on, OP0 off */
380 reg0x0c |= 0x50; /* OP1 on, OP0 on */
382 case SEC_VOLTAGE_OFF:
391 if (state->config->op0_off)
394 stv0299_writeregI(state, 0x08, reg0x08);
395 return stv0299_writeregI(state, 0x0c, reg0x0c);
398 static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
400 struct stv0299_state* state = fe->demodulator_priv;
406 struct timeval nexttime;
407 struct timeval tv[10];
409 reg0x08 = stv0299_readreg (state, 0x08);
410 reg0x0c = stv0299_readreg (state, 0x0c);
412 stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
413 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
417 if (debug_legacy_dish_switch)
418 printk ("%s switch command: 0x%04lx\n",__func__, cmd);
420 do_gettimeofday (&nexttime);
421 if (debug_legacy_dish_switch)
422 memcpy (&tv[0], &nexttime, sizeof (struct timeval));
423 stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
425 dvb_frontend_sleep_until(&nexttime, 32000);
427 for (i=0; i<9; i++) {
428 if (debug_legacy_dish_switch)
429 do_gettimeofday (&tv[i+1]);
430 if((cmd & 0x01) != last) {
431 /* set voltage to (last ? 13V : 18V) */
432 stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
433 last = (last) ? 0 : 1;
439 dvb_frontend_sleep_until(&nexttime, 8000);
441 if (debug_legacy_dish_switch) {
442 printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
443 __func__, fe->dvb->num);
444 for (i = 1; i < 10; i++)
445 printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
451 static int stv0299_init (struct dvb_frontend* fe)
453 struct stv0299_state* state = fe->demodulator_priv;
458 dprintk("stv0299: init chip\n");
460 for (i = 0; ; i += 2) {
461 reg = state->config->inittab[i];
462 val = state->config->inittab[i+1];
463 if (reg == 0xff && val == 0xff)
465 if (reg == 0x0c && state->config->op0_off)
467 stv0299_writeregI(state, reg, val);
473 static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status)
475 struct stv0299_state* state = fe->demodulator_priv;
477 u8 signal = 0xff - stv0299_readreg (state, 0x18);
478 u8 sync = stv0299_readreg (state, 0x1b);
480 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
484 *status |= FE_HAS_SIGNAL;
487 *status |= FE_HAS_CARRIER;
490 *status |= FE_HAS_VITERBI;
493 *status |= FE_HAS_SYNC;
495 if ((sync & 0x98) == 0x98)
496 *status |= FE_HAS_LOCK;
501 static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
503 struct stv0299_state* state = fe->demodulator_priv;
505 if (state->errmode != STATUS_BER)
508 *ber = stv0299_readreg(state, 0x1e) | (stv0299_readreg(state, 0x1d) << 8);
513 static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
515 struct stv0299_state* state = fe->demodulator_priv;
517 s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
518 | stv0299_readreg (state, 0x19));
520 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __func__,
521 stv0299_readreg (state, 0x18),
522 stv0299_readreg (state, 0x19), (int) signal);
524 signal = signal * 5 / 4;
525 *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
530 static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
532 struct stv0299_state* state = fe->demodulator_priv;
534 s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
535 | stv0299_readreg (state, 0x25));
536 xsnr = 3 * (xsnr - 0xa100);
537 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
542 static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
544 struct stv0299_state* state = fe->demodulator_priv;
546 if (state->errmode != STATUS_UCBLOCKS)
549 state->ucblocks += stv0299_readreg(state, 0x1e);
550 state->ucblocks += (stv0299_readreg(state, 0x1d) << 8);
551 *ucblocks = state->ucblocks;
556 static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
558 struct stv0299_state* state = fe->demodulator_priv;
561 dprintk ("%s : FE_SET_FRONTEND\n", __func__);
564 if (p->inversion == INVERSION_OFF) invval = 0;
565 else if (p->inversion == INVERSION_ON) invval = 1;
567 printk("stv0299 does not support auto-inversion\n");
570 if (state->config->invert) invval = (~invval) & 1;
571 stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);
573 if (fe->ops.tuner_ops.set_params) {
574 fe->ops.tuner_ops.set_params(fe, p);
575 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
578 stv0299_set_FEC (state, p->u.qpsk.fec_inner);
579 stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
580 stv0299_writeregI(state, 0x22, 0x00);
581 stv0299_writeregI(state, 0x23, 0x00);
583 state->tuner_frequency = p->frequency;
584 state->fec_inner = p->u.qpsk.fec_inner;
585 state->symbol_rate = p->u.qpsk.symbol_rate;
590 static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
592 struct stv0299_state* state = fe->demodulator_priv;
596 derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
597 | stv0299_readreg (state, 0x23));
599 derot_freq *= (state->config->mclk >> 16);
603 p->frequency += derot_freq;
605 invval = stv0299_readreg (state, 0x0c) & 1;
606 if (state->config->invert) invval = (~invval) & 1;
607 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
609 p->u.qpsk.fec_inner = stv0299_get_fec (state);
610 p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state);
615 static int stv0299_sleep(struct dvb_frontend* fe)
617 struct stv0299_state* state = fe->demodulator_priv;
619 stv0299_writeregI(state, 0x02, 0x80);
620 state->initialised = 0;
625 static int stv0299_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
627 struct stv0299_state* state = fe->demodulator_priv;
630 stv0299_writeregI(state, 0x05, 0xb5);
632 stv0299_writeregI(state, 0x05, 0x35);
638 static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
640 struct stv0299_state* state = fe->demodulator_priv;
642 fesettings->min_delay_ms = state->config->min_delay_ms;
643 if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
644 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
645 fesettings->max_drift = 5000;
647 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
648 fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
653 static void stv0299_release(struct dvb_frontend* fe)
655 struct stv0299_state* state = fe->demodulator_priv;
659 static struct dvb_frontend_ops stv0299_ops;
661 struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
662 struct i2c_adapter* i2c)
664 struct stv0299_state* state = NULL;
667 /* allocate memory for the internal state */
668 state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL);
669 if (state == NULL) goto error;
671 /* setup the state */
672 state->config = config;
674 state->initialised = 0;
675 state->tuner_frequency = 0;
676 state->symbol_rate = 0;
677 state->fec_inner = 0;
678 state->errmode = STATUS_BER;
680 /* check if the demod is there */
681 stv0299_writeregI(state, 0x02, 0x34); /* standby off */
683 id = stv0299_readreg(state, 0x00);
685 /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
686 /* register 0x00 might contain 0x80 when returning from standby */
687 if (id != 0xa1 && id != 0x80) goto error;
689 /* create dvb_frontend */
690 memcpy(&state->frontend.ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
691 state->frontend.demodulator_priv = state;
692 return &state->frontend;
699 static struct dvb_frontend_ops stv0299_ops = {
702 .name = "ST STV0299 DVB-S",
704 .frequency_min = 950000,
705 .frequency_max = 2150000,
706 .frequency_stepsize = 125, /* kHz for QPSK frontends */
707 .frequency_tolerance = 0,
708 .symbol_rate_min = 1000000,
709 .symbol_rate_max = 45000000,
710 .symbol_rate_tolerance = 500, /* ppm */
711 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
712 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
717 .release = stv0299_release,
719 .init = stv0299_init,
720 .sleep = stv0299_sleep,
721 .write = stv0299_write,
722 .i2c_gate_ctrl = stv0299_i2c_gate_ctrl,
724 .set_frontend = stv0299_set_frontend,
725 .get_frontend = stv0299_get_frontend,
726 .get_tune_settings = stv0299_get_tune_settings,
728 .read_status = stv0299_read_status,
729 .read_ber = stv0299_read_ber,
730 .read_signal_strength = stv0299_read_signal_strength,
731 .read_snr = stv0299_read_snr,
732 .read_ucblocks = stv0299_read_ucblocks,
734 .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
735 .diseqc_send_burst = stv0299_send_diseqc_burst,
736 .set_tone = stv0299_set_tone,
737 .set_voltage = stv0299_set_voltage,
738 .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
741 module_param(debug_legacy_dish_switch, int, 0444);
742 MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
744 module_param(debug, int, 0644);
745 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
747 MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
748 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
749 "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
750 MODULE_LICENSE("GPL");
752 EXPORT_SYMBOL(stv0299_attach);