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/moduleparam.h>
49 #include <linux/string.h>
50 #include <linux/slab.h>
51 #include <linux/jiffies.h>
52 #include <asm/div64.h>
54 #include "dvb_frontend.h"
57 struct stv0299_state {
58 struct i2c_adapter* i2c;
59 struct dvb_frontend_ops ops;
60 const struct stv0299_config* config;
61 struct dvb_frontend frontend;
66 fe_code_rate_t 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", __FUNCTION__, reg, data, ret);
93 return (ret != 1) ? -EREMOTEIO : 0;
96 int stv0299_writereg (struct dvb_frontend* fe, u8 reg, u8 data)
98 struct stv0299_state* state = fe->demodulator_priv;
100 return stv0299_writeregI(state, reg, data);
103 static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
108 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
109 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };
111 ret = i2c_transfer (state->i2c, msg, 2);
114 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
115 __FUNCTION__, reg, ret);
120 static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
123 struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = ®1, .len = 1 },
124 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };
126 ret = i2c_transfer (state->i2c, msg, 2);
129 dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);
131 return ret == 2 ? 0 : ret;
134 static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec)
136 dprintk ("%s\n", __FUNCTION__);
141 return stv0299_writeregI (state, 0x31, 0x1f);
145 return stv0299_writeregI (state, 0x31, 0x01);
149 return stv0299_writeregI (state, 0x31, 0x02);
153 return stv0299_writeregI (state, 0x31, 0x04);
157 return stv0299_writeregI (state, 0x31, 0x08);
161 return stv0299_writeregI (state, 0x31, 0x10);
170 static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state)
172 static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
176 dprintk ("%s\n", __FUNCTION__);
178 index = stv0299_readreg (state, 0x1b);
184 return fec_tab [index];
187 static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
189 unsigned long start = jiffies;
191 dprintk ("%s\n", __FUNCTION__);
193 while (stv0299_readreg(state, 0x0a) & 1) {
194 if (jiffies - start > timeout) {
195 dprintk ("%s: timeout!!\n", __FUNCTION__);
204 static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
206 unsigned long start = jiffies;
208 dprintk ("%s\n", __FUNCTION__);
210 while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
211 if (jiffies - start > timeout) {
212 dprintk ("%s: timeout!!\n", __FUNCTION__);
221 static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
223 struct stv0299_state* state = fe->demodulator_priv;
227 // check rate is within limits
228 if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;
230 // calculate value to program
232 big += (state->config->mclk-1); // round correctly
233 do_div(big, state->config->mclk);
236 return state->config->set_symbol_rate(fe, srate, ratio);
239 static int stv0299_get_symbolrate (struct stv0299_state* state)
241 u32 Mclk = state->config->mclk / 4096L;
247 dprintk ("%s\n", __FUNCTION__);
249 stv0299_readregs (state, 0x1f, sfr, 3);
250 stv0299_readregs (state, 0x1a, &rtf, 1);
252 srate = (sfr[0] << 8) | sfr[1];
255 srate += (sfr[2] >> 4) * Mclk / 256;
256 offset = (s32) rtf * (srate / 4096L);
259 dprintk ("%s : srate = %i\n", __FUNCTION__, srate);
260 dprintk ("%s : ofset = %i\n", __FUNCTION__, offset);
271 static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
272 struct dvb_diseqc_master_cmd *m)
274 struct stv0299_state* state = fe->demodulator_priv;
278 dprintk ("%s\n", __FUNCTION__);
280 if (stv0299_wait_diseqc_idle (state, 100) < 0)
283 val = stv0299_readreg (state, 0x08);
285 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6)) /* DiSEqC mode */
288 for (i=0; i<m->msg_len; i++) {
289 if (stv0299_wait_diseqc_fifo (state, 100) < 0)
292 if (stv0299_writeregI (state, 0x09, m->msg[i]))
296 if (stv0299_wait_diseqc_idle (state, 100) < 0)
302 static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
304 struct stv0299_state* state = fe->demodulator_priv;
307 dprintk ("%s\n", __FUNCTION__);
309 if (stv0299_wait_diseqc_idle (state, 100) < 0)
312 val = stv0299_readreg (state, 0x08);
314 if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2)) /* burst mode */
317 if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
320 if (stv0299_wait_diseqc_idle (state, 100) < 0)
323 if (stv0299_writeregI (state, 0x08, val))
329 static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
331 struct stv0299_state* state = fe->demodulator_priv;
334 if (stv0299_wait_diseqc_idle (state, 100) < 0)
337 val = stv0299_readreg (state, 0x08);
341 return stv0299_writeregI (state, 0x08, val | 0x3);
344 return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);
351 static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
353 struct stv0299_state* state = fe->demodulator_priv;
357 dprintk("%s: %s\n", __FUNCTION__,
358 voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
359 voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
361 reg0x08 = stv0299_readreg (state, 0x08);
362 reg0x0c = stv0299_readreg (state, 0x0c);
365 * H/V switching over OP0, OP1 and OP2 are LNB power enable bits
369 if (voltage == SEC_VOLTAGE_OFF) {
370 stv0299_writeregI (state, 0x0c, 0x00); /* LNB power off! */
371 return stv0299_writeregI (state, 0x08, 0x00); /* LNB power off! */
374 stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
378 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) reg0x0c |= 0x10;
379 else reg0x0c |= 0x40;
381 return stv0299_writeregI(state, 0x0c, reg0x0c);
384 return stv0299_writeregI(state, 0x0c, reg0x0c | 0x50);
390 static inline s32 stv0299_calc_usec_delay (struct timeval lasttime, struct timeval curtime)
392 return ((curtime.tv_usec < lasttime.tv_usec) ?
393 1000000 - lasttime.tv_usec + curtime.tv_usec :
394 curtime.tv_usec - lasttime.tv_usec);
397 static void stv0299_sleep_until (struct timeval *waketime, u32 add_usec)
399 struct timeval lasttime;
402 waketime->tv_usec += add_usec;
403 if (waketime->tv_usec >= 1000000) {
404 waketime->tv_usec -= 1000000;
408 do_gettimeofday (&lasttime);
409 delta = stv0299_calc_usec_delay (lasttime, *waketime);
411 msleep ((delta - 1500) / 1000);
412 do_gettimeofday (&lasttime);
413 newdelta = stv0299_calc_usec_delay (lasttime, *waketime);
414 delta = (newdelta > delta) ? 0 : newdelta;
420 static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, u32 cmd)
422 struct stv0299_state* state = fe->demodulator_priv;
428 struct timeval nexttime;
429 struct timeval tv[10];
431 reg0x08 = stv0299_readreg (state, 0x08);
432 reg0x0c = stv0299_readreg (state, 0x0c);
434 stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
435 if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
439 if (debug_legacy_dish_switch)
440 printk ("%s switch command: 0x%04x\n",__FUNCTION__, cmd);
442 do_gettimeofday (&nexttime);
443 if (debug_legacy_dish_switch)
444 memcpy (&tv[0], &nexttime, sizeof (struct timeval));
445 stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
447 stv0299_sleep_until (&nexttime, 32000);
449 for (i=0; i<9; i++) {
450 if (debug_legacy_dish_switch)
451 do_gettimeofday (&tv[i+1]);
452 if((cmd & 0x01) != last) {
453 /* set voltage to (last ? 13V : 18V) */
454 stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
455 last = (last) ? 0 : 1;
461 stv0299_sleep_until (&nexttime, 8000);
463 if (debug_legacy_dish_switch) {
464 printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
465 __FUNCTION__, fe->dvb->num);
466 for (i=1; i < 10; i++)
467 printk ("%d: %d\n", i, stv0299_calc_usec_delay (tv[i-1] , tv[i]));
473 static int stv0299_init (struct dvb_frontend* fe)
475 struct stv0299_state* state = fe->demodulator_priv;
478 dprintk("stv0299: init chip\n");
480 for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
481 stv0299_writeregI(state, state->config->inittab[i], state->config->inittab[i+1]);
483 if (state->config->pll_init) {
484 stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */
485 state->config->pll_init(fe, state->i2c);
486 stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */
492 static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status)
494 struct stv0299_state* state = fe->demodulator_priv;
496 u8 signal = 0xff - stv0299_readreg (state, 0x18);
497 u8 sync = stv0299_readreg (state, 0x1b);
499 dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync);
503 *status |= FE_HAS_SIGNAL;
506 *status |= FE_HAS_CARRIER;
509 *status |= FE_HAS_VITERBI;
512 *status |= FE_HAS_SYNC;
514 if ((sync & 0x98) == 0x98)
515 *status |= FE_HAS_LOCK;
520 static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
522 struct stv0299_state* state = fe->demodulator_priv;
524 if (state->errmode != STATUS_BER) return 0;
525 *ber = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
530 static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
532 struct stv0299_state* state = fe->demodulator_priv;
534 s32 signal = 0xffff - ((stv0299_readreg (state, 0x18) << 8)
535 | stv0299_readreg (state, 0x19));
537 dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__,
538 stv0299_readreg (state, 0x18),
539 stv0299_readreg (state, 0x19), (int) signal);
541 signal = signal * 5 / 4;
542 *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
547 static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
549 struct stv0299_state* state = fe->demodulator_priv;
551 s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
552 | stv0299_readreg (state, 0x25));
553 xsnr = 3 * (xsnr - 0xa100);
554 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
559 static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
561 struct stv0299_state* state = fe->demodulator_priv;
563 if (state->errmode != STATUS_UCBLOCKS) *ucblocks = 0;
564 else *ucblocks = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
569 static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
571 struct stv0299_state* state = fe->demodulator_priv;
574 dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__);
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 (state->config->enhanced_tuning) {
587 /* check if we should do a finetune */
588 int frequency_delta = p->frequency - state->tuner_frequency;
589 int minmax = p->u.qpsk.symbol_rate / 2000;
590 if (minmax < 5000) minmax = 5000;
592 if ((frequency_delta > -minmax) && (frequency_delta < minmax) && (frequency_delta != 0) &&
593 (state->fec_inner == p->u.qpsk.fec_inner) &&
594 (state->symbol_rate == p->u.qpsk.symbol_rate)) {
595 int Drot_freq = (frequency_delta << 16) / (state->config->mclk / 1000);
597 // zap the derotator registers first
598 stv0299_writeregI(state, 0x22, 0x00);
599 stv0299_writeregI(state, 0x23, 0x00);
601 // now set them as we want
602 stv0299_writeregI(state, 0x22, Drot_freq >> 8);
603 stv0299_writeregI(state, 0x23, Drot_freq);
605 /* A "normal" tune is requested */
606 stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */
607 state->config->pll_set(fe, state->i2c, p);
608 stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */
610 stv0299_writeregI(state, 0x32, 0x80);
611 stv0299_writeregI(state, 0x22, 0x00);
612 stv0299_writeregI(state, 0x23, 0x00);
613 stv0299_writeregI(state, 0x32, 0x19);
614 stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
615 stv0299_set_FEC (state, p->u.qpsk.fec_inner);
618 stv0299_writeregI(state, 0x05, 0xb5); /* enable i2c repeater on stv0299 */
619 state->config->pll_set(fe, state->i2c, p);
620 stv0299_writeregI(state, 0x05, 0x35); /* disable i2c repeater on stv0299 */
622 stv0299_set_FEC (state, p->u.qpsk.fec_inner);
623 stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
624 stv0299_writeregI(state, 0x22, 0x00);
625 stv0299_writeregI(state, 0x23, 0x00);
626 stv0299_readreg (state, 0x23);
627 stv0299_writeregI(state, 0x12, 0xb9);
630 state->tuner_frequency = p->frequency;
631 state->fec_inner = p->u.qpsk.fec_inner;
632 state->symbol_rate = p->u.qpsk.symbol_rate;
637 static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
639 struct stv0299_state* state = fe->demodulator_priv;
643 derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
644 | stv0299_readreg (state, 0x23));
646 derot_freq *= (state->config->mclk >> 16);
650 p->frequency += derot_freq;
652 invval = stv0299_readreg (state, 0x0c) & 1;
653 if (state->config->invert) invval = (~invval) & 1;
654 p->inversion = invval ? INVERSION_ON : INVERSION_OFF;
656 p->u.qpsk.fec_inner = stv0299_get_fec (state);
657 p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state);
662 static int stv0299_sleep(struct dvb_frontend* fe)
664 struct stv0299_state* state = fe->demodulator_priv;
666 stv0299_writeregI(state, 0x02, 0x80);
667 state->initialised = 0;
672 static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
674 struct stv0299_state* state = fe->demodulator_priv;
676 fesettings->min_delay_ms = state->config->min_delay_ms;
677 if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
678 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
679 fesettings->max_drift = 5000;
681 fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
682 fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
687 static void stv0299_release(struct dvb_frontend* fe)
689 struct stv0299_state* state = fe->demodulator_priv;
693 static struct dvb_frontend_ops stv0299_ops;
695 struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
696 struct i2c_adapter* i2c)
698 struct stv0299_state* state = NULL;
701 /* allocate memory for the internal state */
702 state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL);
703 if (state == NULL) goto error;
705 /* setup the state */
706 state->config = config;
708 memcpy(&state->ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
709 state->initialised = 0;
710 state->tuner_frequency = 0;
711 state->symbol_rate = 0;
712 state->fec_inner = 0;
713 state->errmode = STATUS_BER;
715 /* check if the demod is there */
716 stv0299_writeregI(state, 0x02, 0x34); /* standby off */
718 id = stv0299_readreg(state, 0x00);
720 /* register 0x00 contains 0xa1 for STV0299 and STV0299B */
721 /* register 0x00 might contain 0x80 when returning from standby */
722 if (id != 0xa1 && id != 0x80) goto error;
724 /* create dvb_frontend */
725 state->frontend.ops = &state->ops;
726 state->frontend.demodulator_priv = state;
727 return &state->frontend;
734 static struct dvb_frontend_ops stv0299_ops = {
737 .name = "ST STV0299 DVB-S",
739 .frequency_min = 950000,
740 .frequency_max = 2150000,
741 .frequency_stepsize = 125, /* kHz for QPSK frontends */
742 .frequency_tolerance = 0,
743 .symbol_rate_min = 1000000,
744 .symbol_rate_max = 45000000,
745 .symbol_rate_tolerance = 500, /* ppm */
746 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
747 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
752 .release = stv0299_release,
754 .init = stv0299_init,
755 .sleep = stv0299_sleep,
757 .set_frontend = stv0299_set_frontend,
758 .get_frontend = stv0299_get_frontend,
759 .get_tune_settings = stv0299_get_tune_settings,
761 .read_status = stv0299_read_status,
762 .read_ber = stv0299_read_ber,
763 .read_signal_strength = stv0299_read_signal_strength,
764 .read_snr = stv0299_read_snr,
765 .read_ucblocks = stv0299_read_ucblocks,
767 .diseqc_send_master_cmd = stv0299_send_diseqc_msg,
768 .diseqc_send_burst = stv0299_send_diseqc_burst,
769 .set_tone = stv0299_set_tone,
770 .set_voltage = stv0299_set_voltage,
771 .dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
774 module_param(debug_legacy_dish_switch, int, 0444);
775 MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");
777 module_param(debug, int, 0644);
778 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
780 MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
781 MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
782 "Andreas Oberritter, Andrew de Quincey, Kenneth Aafløy");
783 MODULE_LICENSE("GPL");
785 EXPORT_SYMBOL(stv0299_writereg);
786 EXPORT_SYMBOL(stv0299_attach);