2 Driver for ST STV0288 demodulator
3 Copyright (C) 2006 Georg Acher, BayCom GmbH, acher (at) baycom (dot) de
5 Copyright (C) 2008 TurboSight.com, Bob Liu <bob@turbosight.com>
6 Copyright (C) 2008 Igor M. Liplianin <liplianin@me.by>
7 Removed stb6000 specific tuner code and revised some
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
30 #include <linux/slab.h>
31 #include <linux/jiffies.h>
32 #include <asm/div64.h>
34 #include "dvb_frontend.h"
37 struct stv0288_state {
38 struct i2c_adapter *i2c;
39 const struct stv0288_config *config;
40 struct dvb_frontend frontend;
45 fe_code_rate_t fec_inner;
50 #define STATUS_UCBLOCKS 1
53 static int debug_legacy_dish_switch;
54 #define dprintk(args...) \
57 printk(KERN_DEBUG "stv0288: " args); \
61 static int stv0288_writeregI(struct stv0288_state *state, u8 reg, u8 data)
64 u8 buf[] = { reg, data };
65 struct i2c_msg msg = {
66 .addr = state->config->demod_address,
72 ret = i2c_transfer(state->i2c, &msg, 1);
75 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
76 "ret == %i)\n", __func__, reg, data, ret);
78 return (ret != 1) ? -EREMOTEIO : 0;
81 static int stv0288_write(struct dvb_frontend *fe, u8 *buf, int len)
83 struct stv0288_state *state = fe->demodulator_priv;
88 return stv0288_writeregI(state, buf[0], buf[1]);
91 static u8 stv0288_readreg(struct stv0288_state *state, u8 reg)
96 struct i2c_msg msg[] = {
98 .addr = state->config->demod_address,
103 .addr = state->config->demod_address,
110 ret = i2c_transfer(state->i2c, msg, 2);
113 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
119 static int stv0288_set_symbolrate(struct dvb_frontend *fe, u32 srate)
121 struct stv0288_state *state = fe->demodulator_priv;
125 if ((srate < 1000000) || (srate > 45000000))
128 temp = (unsigned int)srate / 1000;
133 b[0] = (unsigned char)((temp >> 12) & 0xff);
134 b[1] = (unsigned char)((temp >> 4) & 0xff);
135 b[2] = (unsigned char)((temp << 4) & 0xf0);
136 stv0288_writeregI(state, 0x28, 0x80); /* SFRH */
137 stv0288_writeregI(state, 0x29, 0); /* SFRM */
138 stv0288_writeregI(state, 0x2a, 0); /* SFRL */
140 stv0288_writeregI(state, 0x28, b[0]);
141 stv0288_writeregI(state, 0x29, b[1]);
142 stv0288_writeregI(state, 0x2a, b[2]);
143 dprintk("stv0288: stv0288_set_symbolrate\n");
148 static int stv0288_send_diseqc_msg(struct dvb_frontend *fe,
149 struct dvb_diseqc_master_cmd *m)
151 struct stv0288_state *state = fe->demodulator_priv;
155 dprintk("%s\n", __func__);
157 stv0288_writeregI(state, 0x09, 0);
159 stv0288_writeregI(state, 0x05, 0x16);
161 for (i = 0; i < m->msg_len; i++) {
162 if (stv0288_writeregI(state, 0x06, m->msg[i]))
170 static int stv0288_send_diseqc_burst(struct dvb_frontend *fe,
171 fe_sec_mini_cmd_t burst)
173 struct stv0288_state *state = fe->demodulator_priv;
175 dprintk("%s\n", __func__);
177 if (stv0288_writeregI(state, 0x05, 0x16))/* burst mode */
180 if (stv0288_writeregI(state, 0x06, burst == SEC_MINI_A ? 0x00 : 0xff))
183 if (stv0288_writeregI(state, 0x06, 0x12))
189 static int stv0288_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
191 struct stv0288_state *state = fe->demodulator_priv;
195 if (stv0288_writeregI(state, 0x05, 0x10))/* burst mode */
197 return stv0288_writeregI(state, 0x06, 0xff);
200 if (stv0288_writeregI(state, 0x05, 0x13))/* burst mode */
202 return stv0288_writeregI(state, 0x06, 0x00);
209 static u8 stv0288_inittab[] = {
318 static int stv0288_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t volt)
320 dprintk("%s: %s\n", __func__,
321 volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
322 volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
327 static int stv0288_init(struct dvb_frontend *fe)
329 struct stv0288_state *state = fe->demodulator_priv;
332 dprintk("stv0288: init chip\n");
333 stv0288_writeregI(state, 0x41, 0x04);
336 for (i = 0; !(stv0288_inittab[i] == 0xff &&
337 stv0288_inittab[i + 1] == 0xff); i += 2)
338 stv0288_writeregI(state, stv0288_inittab[i],
339 stv0288_inittab[i + 1]);
344 static int stv0288_read_status(struct dvb_frontend *fe, fe_status_t *status)
346 struct stv0288_state *state = fe->demodulator_priv;
348 u8 sync = stv0288_readreg(state, 0x24);
352 dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
356 if ((sync & 0x08) == 0x08) {
357 *status |= FE_HAS_LOCK;
358 dprintk("stv0288 has locked\n");
364 static int stv0288_read_ber(struct dvb_frontend *fe, u32 *ber)
366 struct stv0288_state *state = fe->demodulator_priv;
368 if (state->errmode != STATUS_BER)
370 *ber = (stv0288_readreg(state, 0x26) << 8) |
371 stv0288_readreg(state, 0x27);
372 dprintk("stv0288_read_ber %d\n", *ber);
378 static int stv0288_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
380 struct stv0288_state *state = fe->demodulator_priv;
382 s32 signal = 0xffff - ((stv0288_readreg(state, 0x10) << 8));
385 signal = signal * 5 / 4;
386 *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
387 dprintk("stv0288_read_signal_strength %d\n", *strength);
391 static int stv0288_sleep(struct dvb_frontend *fe)
393 struct stv0288_state *state = fe->demodulator_priv;
395 stv0288_writeregI(state, 0x41, 0x84);
396 state->initialised = 0;
400 static int stv0288_read_snr(struct dvb_frontend *fe, u16 *snr)
402 struct stv0288_state *state = fe->demodulator_priv;
404 s32 xsnr = 0xffff - ((stv0288_readreg(state, 0x2d) << 8)
405 | stv0288_readreg(state, 0x2e));
406 xsnr = 3 * (xsnr - 0xa100);
407 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
408 dprintk("stv0288_read_snr %d\n", *snr);
413 static int stv0288_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
415 struct stv0288_state *state = fe->demodulator_priv;
417 if (state->errmode != STATUS_BER)
419 *ucblocks = (stv0288_readreg(state, 0x26) << 8) |
420 stv0288_readreg(state, 0x27);
421 dprintk("stv0288_read_ber %d\n", *ucblocks);
426 static int stv0288_set_property(struct dvb_frontend *fe, struct dtv_property *p)
428 dprintk("%s(..)\n", __func__);
432 static int stv0288_get_property(struct dvb_frontend *fe, struct dtv_property *p)
434 dprintk("%s(..)\n", __func__);
438 static int stv0288_set_frontend(struct dvb_frontend *fe,
439 struct dvb_frontend_parameters *dfp)
441 struct stv0288_state *state = fe->demodulator_priv;
442 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
445 unsigned char tda[3];
447 dprintk("%s : FE_SET_FRONTEND\n", __func__);
449 if (c->delivery_system != SYS_DVBS) {
450 dprintk("%s: unsupported delivery "
451 "system selected (%d)\n",
452 __func__, c->delivery_system);
456 if (state->config->set_ts_params)
457 state->config->set_ts_params(fe, 0);
459 /* only frequency & symbol_rate are used for tuner*/
460 dfp->frequency = c->frequency;
461 dfp->u.qpsk.symbol_rate = c->symbol_rate;
462 if (fe->ops.tuner_ops.set_params) {
463 fe->ops.tuner_ops.set_params(fe, dfp);
464 if (fe->ops.i2c_gate_ctrl)
465 fe->ops.i2c_gate_ctrl(fe, 0);
469 stv0288_set_symbolrate(fe, c->symbol_rate);
470 /* Carrier lock control register */
471 stv0288_writeregI(state, 0x15, 0xc5);
473 tda[0] = 0x2b; /* CFRM */
474 tda[2] = 0x0; /* CFRL */
475 for (tm = -6; tm < 7;) {
477 if (stv0288_readreg(state, 0x24) & 0x80)
483 tda[1] = (unsigned char)tm;
484 stv0288_writeregI(state, 0x2b, tda[1]);
485 stv0288_writeregI(state, 0x2c, tda[2]);
489 state->tuner_frequency = c->frequency;
490 state->fec_inner = FEC_AUTO;
491 state->symbol_rate = c->symbol_rate;
496 static int stv0288_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
498 struct stv0288_state *state = fe->demodulator_priv;
501 stv0288_writeregI(state, 0x01, 0xb5);
503 stv0288_writeregI(state, 0x01, 0x35);
510 static void stv0288_release(struct dvb_frontend *fe)
512 struct stv0288_state *state = fe->demodulator_priv;
516 static struct dvb_frontend_ops stv0288_ops = {
519 .name = "ST STV0288 DVB-S",
521 .frequency_min = 950000,
522 .frequency_max = 2150000,
523 .frequency_stepsize = 1000, /* kHz for QPSK frontends */
524 .frequency_tolerance = 0,
525 .symbol_rate_min = 1000000,
526 .symbol_rate_max = 45000000,
527 .symbol_rate_tolerance = 500, /* ppm */
528 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
529 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
534 .release = stv0288_release,
535 .init = stv0288_init,
536 .sleep = stv0288_sleep,
537 .write = stv0288_write,
538 .i2c_gate_ctrl = stv0288_i2c_gate_ctrl,
539 .read_status = stv0288_read_status,
540 .read_ber = stv0288_read_ber,
541 .read_signal_strength = stv0288_read_signal_strength,
542 .read_snr = stv0288_read_snr,
543 .read_ucblocks = stv0288_read_ucblocks,
544 .diseqc_send_master_cmd = stv0288_send_diseqc_msg,
545 .diseqc_send_burst = stv0288_send_diseqc_burst,
546 .set_tone = stv0288_set_tone,
547 .set_voltage = stv0288_set_voltage,
549 .set_property = stv0288_set_property,
550 .get_property = stv0288_get_property,
551 .set_frontend = stv0288_set_frontend,
554 struct dvb_frontend *stv0288_attach(const struct stv0288_config *config,
555 struct i2c_adapter *i2c)
557 struct stv0288_state *state = NULL;
560 /* allocate memory for the internal state */
561 state = kmalloc(sizeof(struct stv0288_state), GFP_KERNEL);
565 /* setup the state */
566 state->config = config;
568 state->initialised = 0;
569 state->tuner_frequency = 0;
570 state->symbol_rate = 0;
571 state->fec_inner = 0;
572 state->errmode = STATUS_BER;
574 stv0288_writeregI(state, 0x41, 0x04);
576 id = stv0288_readreg(state, 0x00);
577 dprintk("stv0288 id %x\n", id);
579 /* register 0x00 contains 0x11 for STV0288 */
583 /* create dvb_frontend */
584 memcpy(&state->frontend.ops, &stv0288_ops,
585 sizeof(struct dvb_frontend_ops));
586 state->frontend.demodulator_priv = state;
587 return &state->frontend;
594 EXPORT_SYMBOL(stv0288_attach);
596 module_param(debug_legacy_dish_switch, int, 0444);
597 MODULE_PARM_DESC(debug_legacy_dish_switch,
598 "Enable timing analysis for Dish Network legacy switches");
600 module_param(debug, int, 0644);
601 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
603 MODULE_DESCRIPTION("ST STV0288 DVB Demodulator driver");
604 MODULE_AUTHOR("Georg Acher, Bob Liu, Igor liplianin");
605 MODULE_LICENSE("GPL");