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
9 2010-09-01 Josef Pavlik <josef@pavlik.it>
10 Fixed diseqc_msg, diseqc_burst and set_tone problems
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/jiffies.h>
34 #include <asm/div64.h>
36 #include "dvb_frontend.h"
39 struct stv0288_state
{
40 struct i2c_adapter
*i2c
;
41 const struct stv0288_config
*config
;
42 struct dvb_frontend frontend
;
47 enum fe_code_rate fec_inner
;
52 #define STATUS_UCBLOCKS 1
55 static int debug_legacy_dish_switch
;
56 #define dprintk(args...) \
59 printk(KERN_DEBUG "stv0288: " args); \
63 static int stv0288_writeregI(struct stv0288_state
*state
, u8 reg
, u8 data
)
66 u8 buf
[] = { reg
, data
};
67 struct i2c_msg msg
= {
68 .addr
= state
->config
->demod_address
,
74 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
77 dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
78 __func__
, reg
, data
, ret
);
80 return (ret
!= 1) ? -EREMOTEIO
: 0;
83 static int stv0288_write(struct dvb_frontend
*fe
, const u8 buf
[], int len
)
85 struct stv0288_state
*state
= fe
->demodulator_priv
;
90 return stv0288_writeregI(state
, buf
[0], buf
[1]);
93 static u8
stv0288_readreg(struct stv0288_state
*state
, u8 reg
)
98 struct i2c_msg msg
[] = {
100 .addr
= state
->config
->demod_address
,
105 .addr
= state
->config
->demod_address
,
112 ret
= i2c_transfer(state
->i2c
, msg
, 2);
115 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
121 static int stv0288_set_symbolrate(struct dvb_frontend
*fe
, u32 srate
)
123 struct stv0288_state
*state
= fe
->demodulator_priv
;
127 if ((srate
< 1000000) || (srate
> 45000000))
130 stv0288_writeregI(state
, 0x22, 0);
131 stv0288_writeregI(state
, 0x23, 0);
132 stv0288_writeregI(state
, 0x2b, 0xff);
133 stv0288_writeregI(state
, 0x2c, 0xf7);
135 temp
= (unsigned int)srate
/ 1000;
140 b
[0] = (unsigned char)((temp
>> 12) & 0xff);
141 b
[1] = (unsigned char)((temp
>> 4) & 0xff);
142 b
[2] = (unsigned char)((temp
<< 4) & 0xf0);
143 stv0288_writeregI(state
, 0x28, 0x80); /* SFRH */
144 stv0288_writeregI(state
, 0x29, 0); /* SFRM */
145 stv0288_writeregI(state
, 0x2a, 0); /* SFRL */
147 stv0288_writeregI(state
, 0x28, b
[0]);
148 stv0288_writeregI(state
, 0x29, b
[1]);
149 stv0288_writeregI(state
, 0x2a, b
[2]);
150 dprintk("stv0288: stv0288_set_symbolrate\n");
155 static int stv0288_send_diseqc_msg(struct dvb_frontend
*fe
,
156 struct dvb_diseqc_master_cmd
*m
)
158 struct stv0288_state
*state
= fe
->demodulator_priv
;
162 dprintk("%s\n", __func__
);
164 stv0288_writeregI(state
, 0x09, 0);
166 stv0288_writeregI(state
, 0x05, 0x12);/* modulated mode, single shot */
168 for (i
= 0; i
< m
->msg_len
; i
++) {
169 if (stv0288_writeregI(state
, 0x06, m
->msg
[i
]))
172 msleep(m
->msg_len
*12);
176 static int stv0288_send_diseqc_burst(struct dvb_frontend
*fe
,
177 enum fe_sec_mini_cmd burst
)
179 struct stv0288_state
*state
= fe
->demodulator_priv
;
181 dprintk("%s\n", __func__
);
183 if (stv0288_writeregI(state
, 0x05, 0x03))/* burst mode, single shot */
186 if (stv0288_writeregI(state
, 0x06, burst
== SEC_MINI_A
? 0x00 : 0xff))
190 if (stv0288_writeregI(state
, 0x05, 0x12))
196 static int stv0288_set_tone(struct dvb_frontend
*fe
, enum fe_sec_tone_mode tone
)
198 struct stv0288_state
*state
= fe
->demodulator_priv
;
202 if (stv0288_writeregI(state
, 0x05, 0x10))/* cont carrier */
207 if (stv0288_writeregI(state
, 0x05, 0x12))/* burst mode off*/
217 static u8 stv0288_inittab
[] = {
326 static int stv0288_set_voltage(struct dvb_frontend
*fe
,
327 enum fe_sec_voltage volt
)
329 dprintk("%s: %s\n", __func__
,
330 volt
== SEC_VOLTAGE_13
? "SEC_VOLTAGE_13" :
331 volt
== SEC_VOLTAGE_18
? "SEC_VOLTAGE_18" : "??");
336 static int stv0288_init(struct dvb_frontend
*fe
)
338 struct stv0288_state
*state
= fe
->demodulator_priv
;
343 dprintk("stv0288: init chip\n");
344 stv0288_writeregI(state
, 0x41, 0x04);
347 /* we have default inittab */
348 if (state
->config
->inittab
== NULL
) {
349 for (i
= 0; !(stv0288_inittab
[i
] == 0xff &&
350 stv0288_inittab
[i
+ 1] == 0xff); i
+= 2)
351 stv0288_writeregI(state
, stv0288_inittab
[i
],
352 stv0288_inittab
[i
+ 1]);
354 for (i
= 0; ; i
+= 2) {
355 reg
= state
->config
->inittab
[i
];
356 val
= state
->config
->inittab
[i
+1];
357 if (reg
== 0xff && val
== 0xff)
359 stv0288_writeregI(state
, reg
, val
);
365 static int stv0288_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
367 struct stv0288_state
*state
= fe
->demodulator_priv
;
369 u8 sync
= stv0288_readreg(state
, 0x24);
373 dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__
, sync
);
377 *status
|= FE_HAS_CARRIER
| FE_HAS_SIGNAL
;
379 *status
|= FE_HAS_VITERBI
;
381 *status
|= FE_HAS_LOCK
;
382 dprintk("stv0288 has locked\n");
388 static int stv0288_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
390 struct stv0288_state
*state
= fe
->demodulator_priv
;
392 if (state
->errmode
!= STATUS_BER
)
394 *ber
= (stv0288_readreg(state
, 0x26) << 8) |
395 stv0288_readreg(state
, 0x27);
396 dprintk("stv0288_read_ber %d\n", *ber
);
402 static int stv0288_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
404 struct stv0288_state
*state
= fe
->demodulator_priv
;
406 s32 signal
= 0xffff - ((stv0288_readreg(state
, 0x10) << 8));
409 signal
= signal
* 5 / 4;
410 *strength
= (signal
> 0xffff) ? 0xffff : (signal
< 0) ? 0 : signal
;
411 dprintk("stv0288_read_signal_strength %d\n", *strength
);
415 static int stv0288_sleep(struct dvb_frontend
*fe
)
417 struct stv0288_state
*state
= fe
->demodulator_priv
;
419 stv0288_writeregI(state
, 0x41, 0x84);
420 state
->initialised
= 0;
424 static int stv0288_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
426 struct stv0288_state
*state
= fe
->demodulator_priv
;
428 s32 xsnr
= 0xffff - ((stv0288_readreg(state
, 0x2d) << 8)
429 | stv0288_readreg(state
, 0x2e));
430 xsnr
= 3 * (xsnr
- 0xa100);
431 *snr
= (xsnr
> 0xffff) ? 0xffff : (xsnr
< 0) ? 0 : xsnr
;
432 dprintk("stv0288_read_snr %d\n", *snr
);
437 static int stv0288_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
439 struct stv0288_state
*state
= fe
->demodulator_priv
;
441 if (state
->errmode
!= STATUS_BER
)
443 *ucblocks
= (stv0288_readreg(state
, 0x26) << 8) |
444 stv0288_readreg(state
, 0x27);
445 dprintk("stv0288_read_ber %d\n", *ucblocks
);
450 static int stv0288_set_property(struct dvb_frontend
*fe
, struct dtv_property
*p
)
452 dprintk("%s(..)\n", __func__
);
456 static int stv0288_set_frontend(struct dvb_frontend
*fe
)
458 struct stv0288_state
*state
= fe
->demodulator_priv
;
459 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
462 unsigned char tda
[3];
463 u8 reg
, time_out
= 0;
465 dprintk("%s : FE_SET_FRONTEND\n", __func__
);
467 if (c
->delivery_system
!= SYS_DVBS
) {
468 dprintk("%s: unsupported delivery system selected (%d)\n",
469 __func__
, c
->delivery_system
);
473 if (state
->config
->set_ts_params
)
474 state
->config
->set_ts_params(fe
, 0);
476 /* only frequency & symbol_rate are used for tuner*/
477 if (fe
->ops
.tuner_ops
.set_params
) {
478 fe
->ops
.tuner_ops
.set_params(fe
);
479 if (fe
->ops
.i2c_gate_ctrl
)
480 fe
->ops
.i2c_gate_ctrl(fe
, 0);
484 stv0288_set_symbolrate(fe
, c
->symbol_rate
);
485 /* Carrier lock control register */
486 stv0288_writeregI(state
, 0x15, 0xc5);
488 tda
[2] = 0x0; /* CFRL */
489 for (tm
= -9; tm
< 7;) {
491 reg
= stv0288_readreg(state
, 0x24);
506 tda
[1] = (unsigned char)tm
;
507 stv0288_writeregI(state
, 0x2b, tda
[1]);
508 stv0288_writeregI(state
, 0x2c, tda
[2]);
511 state
->tuner_frequency
= c
->frequency
;
512 state
->fec_inner
= FEC_AUTO
;
513 state
->symbol_rate
= c
->symbol_rate
;
518 static int stv0288_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
520 struct stv0288_state
*state
= fe
->demodulator_priv
;
523 stv0288_writeregI(state
, 0x01, 0xb5);
525 stv0288_writeregI(state
, 0x01, 0x35);
532 static void stv0288_release(struct dvb_frontend
*fe
)
534 struct stv0288_state
*state
= fe
->demodulator_priv
;
538 static const struct dvb_frontend_ops stv0288_ops
= {
539 .delsys
= { SYS_DVBS
},
541 .name
= "ST STV0288 DVB-S",
542 .frequency_min
= 950000,
543 .frequency_max
= 2150000,
544 .frequency_stepsize
= 1000, /* kHz for QPSK frontends */
545 .frequency_tolerance
= 0,
546 .symbol_rate_min
= 1000000,
547 .symbol_rate_max
= 45000000,
548 .symbol_rate_tolerance
= 500, /* ppm */
549 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
550 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
|
555 .release
= stv0288_release
,
556 .init
= stv0288_init
,
557 .sleep
= stv0288_sleep
,
558 .write
= stv0288_write
,
559 .i2c_gate_ctrl
= stv0288_i2c_gate_ctrl
,
560 .read_status
= stv0288_read_status
,
561 .read_ber
= stv0288_read_ber
,
562 .read_signal_strength
= stv0288_read_signal_strength
,
563 .read_snr
= stv0288_read_snr
,
564 .read_ucblocks
= stv0288_read_ucblocks
,
565 .diseqc_send_master_cmd
= stv0288_send_diseqc_msg
,
566 .diseqc_send_burst
= stv0288_send_diseqc_burst
,
567 .set_tone
= stv0288_set_tone
,
568 .set_voltage
= stv0288_set_voltage
,
570 .set_property
= stv0288_set_property
,
571 .set_frontend
= stv0288_set_frontend
,
574 struct dvb_frontend
*stv0288_attach(const struct stv0288_config
*config
,
575 struct i2c_adapter
*i2c
)
577 struct stv0288_state
*state
= NULL
;
580 /* allocate memory for the internal state */
581 state
= kzalloc(sizeof(struct stv0288_state
), GFP_KERNEL
);
585 /* setup the state */
586 state
->config
= config
;
588 state
->initialised
= 0;
589 state
->tuner_frequency
= 0;
590 state
->symbol_rate
= 0;
591 state
->fec_inner
= 0;
592 state
->errmode
= STATUS_BER
;
594 stv0288_writeregI(state
, 0x41, 0x04);
596 id
= stv0288_readreg(state
, 0x00);
597 dprintk("stv0288 id %x\n", id
);
599 /* register 0x00 contains 0x11 for STV0288 */
603 /* create dvb_frontend */
604 memcpy(&state
->frontend
.ops
, &stv0288_ops
,
605 sizeof(struct dvb_frontend_ops
));
606 state
->frontend
.demodulator_priv
= state
;
607 return &state
->frontend
;
614 EXPORT_SYMBOL(stv0288_attach
);
616 module_param(debug_legacy_dish_switch
, int, 0444);
617 MODULE_PARM_DESC(debug_legacy_dish_switch
,
618 "Enable timing analysis for Dish Network legacy switches");
620 module_param(debug
, int, 0644);
621 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
623 MODULE_DESCRIPTION("ST STV0288 DVB Demodulator driver");
624 MODULE_AUTHOR("Georg Acher, Bob Liu, Igor liplianin");
625 MODULE_LICENSE("GPL");