1 // SPDX-License-Identifier: GPL-2.0-or-later
3 Driver for M88RS2000 demodulator and tuner
5 Copyright (C) 2012 Malcolm Priestley (tvboxspy@gmail.com)
8 Include various calculation code from DS3000 driver.
9 Copyright (C) 2009 Konstantin Dimitrov.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/jiffies.h>
17 #include <linux/string.h>
18 #include <linux/slab.h>
19 #include <linux/types.h>
22 #include <media/dvb_frontend.h>
23 #include "m88rs2000.h"
25 struct m88rs2000_state
{
26 struct i2c_adapter
*i2c
;
27 const struct m88rs2000_config
*config
;
28 struct dvb_frontend frontend
;
32 enum fe_code_rate fec_inner
;
37 static int m88rs2000_debug
;
39 module_param_named(debug
, m88rs2000_debug
, int, 0644);
40 MODULE_PARM_DESC(debug
, "set debugging level (1=info (or-able)).");
42 #define dprintk(level, args...) do { \
43 if (level & m88rs2000_debug) \
44 printk(KERN_DEBUG "m88rs2000-fe: " args); \
47 #define deb_info(args...) dprintk(0x01, args)
48 #define info(format, arg...) \
49 printk(KERN_INFO "m88rs2000-fe: " format "\n" , ## arg)
51 static int m88rs2000_writereg(struct m88rs2000_state
*state
,
55 u8 buf
[] = { reg
, data
};
56 struct i2c_msg msg
= {
57 .addr
= state
->config
->demod_addr
,
63 ret
= i2c_transfer(state
->i2c
, &msg
, 1);
66 deb_info("%s: writereg error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
67 __func__
, reg
, data
, ret
);
69 return (ret
!= 1) ? -EREMOTEIO
: 0;
72 static u8
m88rs2000_readreg(struct m88rs2000_state
*state
, u8 reg
)
78 struct i2c_msg msg
[] = {
80 .addr
= state
->config
->demod_addr
,
85 .addr
= state
->config
->demod_addr
,
92 ret
= i2c_transfer(state
->i2c
, msg
, 2);
95 deb_info("%s: readreg error (reg == 0x%02x, ret == %i)\n",
101 static u32
m88rs2000_get_mclk(struct dvb_frontend
*fe
)
103 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
106 /* Must not be 0x00 or 0xff */
107 reg
= m88rs2000_readreg(state
, 0x86);
108 if (!reg
|| reg
== 0xff)
114 mclk
= (u32
)(reg
* RS2000_FE_CRYSTAL_KHZ
+ 28 / 2) / 28;
119 static int m88rs2000_set_carrieroffset(struct dvb_frontend
*fe
, s16 offset
)
121 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
127 mclk
= m88rs2000_get_mclk(fe
);
131 tmp
= (offset
* 4096 + (s32
)mclk
/ 2) / (s32
)mclk
;
136 ret
= m88rs2000_writereg(state
, 0x9c, (u8
)(tmp
>> 4));
138 reg
= m88rs2000_readreg(state
, 0x9d);
140 reg
|= (u8
)(tmp
& 0xf) << 4;
142 ret
|= m88rs2000_writereg(state
, 0x9d, reg
);
147 static int m88rs2000_set_symbolrate(struct dvb_frontend
*fe
, u32 srate
)
149 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
155 if ((srate
< 1000000) || (srate
> 45000000))
158 mclk
= m88rs2000_get_mclk(fe
);
167 b
[0] = (u8
) (temp
>> 16) & 0xff;
168 b
[1] = (u8
) (temp
>> 8) & 0xff;
169 b
[2] = (u8
) temp
& 0xff;
171 ret
= m88rs2000_writereg(state
, 0x93, b
[2]);
172 ret
|= m88rs2000_writereg(state
, 0x94, b
[1]);
173 ret
|= m88rs2000_writereg(state
, 0x95, b
[0]);
175 if (srate
> 10000000)
176 ret
|= m88rs2000_writereg(state
, 0xa0, 0x20);
178 ret
|= m88rs2000_writereg(state
, 0xa0, 0x60);
180 ret
|= m88rs2000_writereg(state
, 0xa1, 0xe0);
182 if (srate
> 12000000)
183 ret
|= m88rs2000_writereg(state
, 0xa3, 0x20);
184 else if (srate
> 2800000)
185 ret
|= m88rs2000_writereg(state
, 0xa3, 0x98);
187 ret
|= m88rs2000_writereg(state
, 0xa3, 0x90);
189 deb_info("m88rs2000: m88rs2000_set_symbolrate\n");
193 static int m88rs2000_send_diseqc_msg(struct dvb_frontend
*fe
,
194 struct dvb_diseqc_master_cmd
*m
)
196 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
200 deb_info("%s\n", __func__
);
201 m88rs2000_writereg(state
, 0x9a, 0x30);
202 reg
= m88rs2000_readreg(state
, 0xb2);
204 m88rs2000_writereg(state
, 0xb2, reg
);
205 for (i
= 0; i
< m
->msg_len
; i
++)
206 m88rs2000_writereg(state
, 0xb3 + i
, m
->msg
[i
]);
208 reg
= m88rs2000_readreg(state
, 0xb1);
210 reg
|= ((m
->msg_len
- 1) << 3) | 0x07;
212 m88rs2000_writereg(state
, 0xb1, reg
);
214 for (i
= 0; i
< 15; i
++) {
215 if ((m88rs2000_readreg(state
, 0xb1) & 0x40) == 0x0)
220 reg
= m88rs2000_readreg(state
, 0xb1);
221 if ((reg
& 0x40) > 0x0) {
224 m88rs2000_writereg(state
, 0xb1, reg
);
227 reg
= m88rs2000_readreg(state
, 0xb2);
230 m88rs2000_writereg(state
, 0xb2, reg
);
231 m88rs2000_writereg(state
, 0x9a, 0xb0);
237 static int m88rs2000_send_diseqc_burst(struct dvb_frontend
*fe
,
238 enum fe_sec_mini_cmd burst
)
240 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
242 deb_info("%s\n", __func__
);
243 m88rs2000_writereg(state
, 0x9a, 0x30);
245 reg0
= m88rs2000_readreg(state
, 0xb1);
246 reg1
= m88rs2000_readreg(state
, 0xb2);
247 /* TODO complete this section */
248 m88rs2000_writereg(state
, 0xb2, reg1
);
249 m88rs2000_writereg(state
, 0xb1, reg0
);
250 m88rs2000_writereg(state
, 0x9a, 0xb0);
255 static int m88rs2000_set_tone(struct dvb_frontend
*fe
,
256 enum fe_sec_tone_mode tone
)
258 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
260 m88rs2000_writereg(state
, 0x9a, 0x30);
261 reg0
= m88rs2000_readreg(state
, 0xb1);
262 reg1
= m88rs2000_readreg(state
, 0xb2);
277 m88rs2000_writereg(state
, 0xb2, reg1
);
278 m88rs2000_writereg(state
, 0xb1, reg0
);
279 m88rs2000_writereg(state
, 0x9a, 0xb0);
289 static struct inittab m88rs2000_setup
[] = {
290 {DEMOD_WRITE
, 0x9a, 0x30},
291 {DEMOD_WRITE
, 0x00, 0x01},
292 {WRITE_DELAY
, 0x19, 0x00},
293 {DEMOD_WRITE
, 0x00, 0x00},
294 {DEMOD_WRITE
, 0x9a, 0xb0},
295 {DEMOD_WRITE
, 0x81, 0xc1},
296 {DEMOD_WRITE
, 0x81, 0x81},
297 {DEMOD_WRITE
, 0x86, 0xc6},
298 {DEMOD_WRITE
, 0x9a, 0x30},
299 {DEMOD_WRITE
, 0xf0, 0x22},
300 {DEMOD_WRITE
, 0xf1, 0xbf},
301 {DEMOD_WRITE
, 0xb0, 0x45},
302 {DEMOD_WRITE
, 0xb2, 0x01}, /* set voltage pin always set 1*/
303 {DEMOD_WRITE
, 0x9a, 0xb0},
307 static struct inittab m88rs2000_shutdown
[] = {
308 {DEMOD_WRITE
, 0x9a, 0x30},
309 {DEMOD_WRITE
, 0xb0, 0x00},
310 {DEMOD_WRITE
, 0xf1, 0x89},
311 {DEMOD_WRITE
, 0x00, 0x01},
312 {DEMOD_WRITE
, 0x9a, 0xb0},
313 {DEMOD_WRITE
, 0x81, 0x81},
317 static struct inittab fe_reset
[] = {
318 {DEMOD_WRITE
, 0x00, 0x01},
319 {DEMOD_WRITE
, 0x20, 0x81},
320 {DEMOD_WRITE
, 0x21, 0x80},
321 {DEMOD_WRITE
, 0x10, 0x33},
322 {DEMOD_WRITE
, 0x11, 0x44},
323 {DEMOD_WRITE
, 0x12, 0x07},
324 {DEMOD_WRITE
, 0x18, 0x20},
325 {DEMOD_WRITE
, 0x28, 0x04},
326 {DEMOD_WRITE
, 0x29, 0x8e},
327 {DEMOD_WRITE
, 0x3b, 0xff},
328 {DEMOD_WRITE
, 0x32, 0x10},
329 {DEMOD_WRITE
, 0x33, 0x02},
330 {DEMOD_WRITE
, 0x34, 0x30},
331 {DEMOD_WRITE
, 0x35, 0xff},
332 {DEMOD_WRITE
, 0x38, 0x50},
333 {DEMOD_WRITE
, 0x39, 0x68},
334 {DEMOD_WRITE
, 0x3c, 0x7f},
335 {DEMOD_WRITE
, 0x3d, 0x0f},
336 {DEMOD_WRITE
, 0x45, 0x20},
337 {DEMOD_WRITE
, 0x46, 0x24},
338 {DEMOD_WRITE
, 0x47, 0x7c},
339 {DEMOD_WRITE
, 0x48, 0x16},
340 {DEMOD_WRITE
, 0x49, 0x04},
341 {DEMOD_WRITE
, 0x4a, 0x01},
342 {DEMOD_WRITE
, 0x4b, 0x78},
343 {DEMOD_WRITE
, 0X4d, 0xd2},
344 {DEMOD_WRITE
, 0x4e, 0x6d},
345 {DEMOD_WRITE
, 0x50, 0x30},
346 {DEMOD_WRITE
, 0x51, 0x30},
347 {DEMOD_WRITE
, 0x54, 0x7b},
348 {DEMOD_WRITE
, 0x56, 0x09},
349 {DEMOD_WRITE
, 0x58, 0x59},
350 {DEMOD_WRITE
, 0x59, 0x37},
351 {DEMOD_WRITE
, 0x63, 0xfa},
355 static struct inittab fe_trigger
[] = {
356 {DEMOD_WRITE
, 0x97, 0x04},
357 {DEMOD_WRITE
, 0x99, 0x77},
358 {DEMOD_WRITE
, 0x9b, 0x64},
359 {DEMOD_WRITE
, 0x9e, 0x00},
360 {DEMOD_WRITE
, 0x9f, 0xf8},
361 {DEMOD_WRITE
, 0x98, 0xff},
362 {DEMOD_WRITE
, 0xc0, 0x0f},
363 {DEMOD_WRITE
, 0x89, 0x01},
364 {DEMOD_WRITE
, 0x00, 0x00},
365 {WRITE_DELAY
, 0x0a, 0x00},
366 {DEMOD_WRITE
, 0x00, 0x01},
367 {DEMOD_WRITE
, 0x00, 0x00},
368 {DEMOD_WRITE
, 0x9a, 0xb0},
372 static int m88rs2000_tab_set(struct m88rs2000_state
*state
,
380 for (i
= 0; i
< 255; i
++) {
381 switch (tab
[i
].cmd
) {
383 ret
= m88rs2000_writereg(state
, tab
[i
].reg
,
391 if (tab
[i
].reg
== 0xaa && tab
[i
].val
== 0xff)
404 static int m88rs2000_set_voltage(struct dvb_frontend
*fe
,
405 enum fe_sec_voltage volt
)
407 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
410 data
= m88rs2000_readreg(state
, 0xb2);
411 data
|= 0x03; /* bit0 V/H, bit1 off/on */
421 case SEC_VOLTAGE_OFF
:
425 m88rs2000_writereg(state
, 0xb2, data
);
430 static int m88rs2000_init(struct dvb_frontend
*fe
)
432 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
435 deb_info("m88rs2000: init chip\n");
436 /* Setup frontend from shutdown/cold */
437 if (state
->config
->inittab
)
438 ret
= m88rs2000_tab_set(state
,
439 (struct inittab
*)state
->config
->inittab
);
441 ret
= m88rs2000_tab_set(state
, m88rs2000_setup
);
446 static int m88rs2000_sleep(struct dvb_frontend
*fe
)
448 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
450 /* Shutdown the frondend */
451 ret
= m88rs2000_tab_set(state
, m88rs2000_shutdown
);
455 static int m88rs2000_read_status(struct dvb_frontend
*fe
,
456 enum fe_status
*status
)
458 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
459 u8 reg
= m88rs2000_readreg(state
, 0x8c);
463 if ((reg
& 0xee) == 0xee) {
464 *status
= FE_HAS_CARRIER
| FE_HAS_SIGNAL
| FE_HAS_VITERBI
465 | FE_HAS_SYNC
| FE_HAS_LOCK
;
466 if (state
->config
->set_ts_params
)
467 state
->config
->set_ts_params(fe
, CALL_IS_READ
);
472 static int m88rs2000_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
474 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
477 m88rs2000_writereg(state
, 0x9a, 0x30);
478 tmp0
= m88rs2000_readreg(state
, 0xd8);
479 if ((tmp0
& 0x10) != 0) {
480 m88rs2000_writereg(state
, 0x9a, 0xb0);
485 *ber
= (m88rs2000_readreg(state
, 0xd7) << 8) |
486 m88rs2000_readreg(state
, 0xd6);
488 tmp1
= m88rs2000_readreg(state
, 0xd9);
489 m88rs2000_writereg(state
, 0xd9, (tmp1
& ~7) | 4);
491 m88rs2000_writereg(state
, 0xd8, (tmp0
& ~8) | 0x30);
492 m88rs2000_writereg(state
, 0xd8, (tmp0
& ~8) | 0x30);
493 m88rs2000_writereg(state
, 0x9a, 0xb0);
498 static int m88rs2000_read_signal_strength(struct dvb_frontend
*fe
,
501 if (fe
->ops
.tuner_ops
.get_rf_strength
)
502 fe
->ops
.tuner_ops
.get_rf_strength(fe
, strength
);
507 static int m88rs2000_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
509 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
511 *snr
= 512 * m88rs2000_readreg(state
, 0x65);
516 static int m88rs2000_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
518 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
521 *ucblocks
= (m88rs2000_readreg(state
, 0xd5) << 8) |
522 m88rs2000_readreg(state
, 0xd4);
523 tmp
= m88rs2000_readreg(state
, 0xd8);
524 m88rs2000_writereg(state
, 0xd8, tmp
& ~0x20);
525 /* needs two times */
526 m88rs2000_writereg(state
, 0xd8, tmp
| 0x20);
527 m88rs2000_writereg(state
, 0xd8, tmp
| 0x20);
532 static int m88rs2000_set_fec(struct m88rs2000_state
*state
,
533 enum fe_code_rate fec
)
559 reg
= m88rs2000_readreg(state
, 0x70);
561 ret
= m88rs2000_writereg(state
, 0x70, reg
| fec_set
);
563 ret
|= m88rs2000_writereg(state
, 0x76, 0x8);
568 static enum fe_code_rate
m88rs2000_get_fec(struct m88rs2000_state
*state
)
571 m88rs2000_writereg(state
, 0x9a, 0x30);
572 reg
= m88rs2000_readreg(state
, 0x76);
573 m88rs2000_writereg(state
, 0x9a, 0xb0);
596 static int m88rs2000_set_frontend(struct dvb_frontend
*fe
)
598 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
599 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
600 enum fe_status status
= 0;
606 state
->no_lock_count
= 0;
608 if (c
->delivery_system
!= SYS_DVBS
) {
609 deb_info("%s: unsupported delivery system selected (%d)\n",
610 __func__
, c
->delivery_system
);
615 if (fe
->ops
.tuner_ops
.set_params
)
616 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
621 if (fe
->ops
.tuner_ops
.get_frequency
) {
622 ret
= fe
->ops
.tuner_ops
.get_frequency(fe
, &tuner_freq
);
627 offset
= (s16
)((s32
)tuner_freq
- c
->frequency
);
632 /* default mclk value 96.4285 * 2 * 1000 = 192857 */
633 if (((c
->frequency
% 192857) >= (192857 - 3000)) ||
634 (c
->frequency
% 192857) <= 3000)
635 ret
= m88rs2000_writereg(state
, 0x86, 0xc2);
637 ret
= m88rs2000_writereg(state
, 0x86, 0xc6);
639 ret
|= m88rs2000_set_carrieroffset(fe
, offset
);
643 /* Reset demod by symbol rate */
644 if (c
->symbol_rate
> 27500000)
645 ret
= m88rs2000_writereg(state
, 0xf1, 0xa4);
647 ret
= m88rs2000_writereg(state
, 0xf1, 0xbf);
649 ret
|= m88rs2000_tab_set(state
, fe_reset
);
654 ret
= m88rs2000_set_fec(state
, c
->fec_inner
);
655 ret
|= m88rs2000_writereg(state
, 0x85, 0x1);
656 ret
|= m88rs2000_writereg(state
, 0x8a, 0xbf);
657 ret
|= m88rs2000_writereg(state
, 0x8d, 0x1e);
658 ret
|= m88rs2000_writereg(state
, 0x90, 0xf1);
659 ret
|= m88rs2000_writereg(state
, 0x91, 0x08);
664 /* Set Symbol Rate */
665 ret
= m88rs2000_set_symbolrate(fe
, c
->symbol_rate
);
670 ret
= m88rs2000_tab_set(state
, fe_trigger
);
674 for (i
= 0; i
< 25; i
++) {
675 reg
= m88rs2000_readreg(state
, 0x8c);
676 if ((reg
& 0xee) == 0xee) {
677 status
= FE_HAS_LOCK
;
680 state
->no_lock_count
++;
681 if (state
->no_lock_count
== 15) {
682 reg
= m88rs2000_readreg(state
, 0x70);
684 m88rs2000_writereg(state
, 0x70, reg
);
685 state
->no_lock_count
= 0;
690 if (status
& FE_HAS_LOCK
) {
691 state
->fec_inner
= m88rs2000_get_fec(state
);
692 /* Unknown suspect SNR level */
693 reg
= m88rs2000_readreg(state
, 0x65);
696 state
->tuner_frequency
= c
->frequency
;
697 state
->symbol_rate
= c
->symbol_rate
;
701 static int m88rs2000_get_frontend(struct dvb_frontend
*fe
,
702 struct dtv_frontend_properties
*c
)
704 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
706 c
->fec_inner
= state
->fec_inner
;
707 c
->frequency
= state
->tuner_frequency
;
708 c
->symbol_rate
= state
->symbol_rate
;
712 static int m88rs2000_get_tune_settings(struct dvb_frontend
*fe
,
713 struct dvb_frontend_tune_settings
*tune
)
715 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
717 if (c
->symbol_rate
> 3000000)
718 tune
->min_delay_ms
= 2000;
720 tune
->min_delay_ms
= 3000;
722 tune
->step_size
= c
->symbol_rate
/ 16000;
723 tune
->max_drift
= c
->symbol_rate
/ 2000;
728 static int m88rs2000_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
730 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
733 m88rs2000_writereg(state
, 0x81, 0x84);
735 m88rs2000_writereg(state
, 0x81, 0x81);
740 static void m88rs2000_release(struct dvb_frontend
*fe
)
742 struct m88rs2000_state
*state
= fe
->demodulator_priv
;
746 static const struct dvb_frontend_ops m88rs2000_ops
= {
747 .delsys
= { SYS_DVBS
},
749 .name
= "M88RS2000 DVB-S",
750 .frequency_min_hz
= 950 * MHz
,
751 .frequency_max_hz
= 2150 * MHz
,
752 .frequency_stepsize_hz
= 1 * MHz
,
753 .frequency_tolerance_hz
= 5 * MHz
,
754 .symbol_rate_min
= 1000000,
755 .symbol_rate_max
= 45000000,
756 .symbol_rate_tolerance
= 500, /* ppm */
757 .caps
= FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
758 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
|
759 FE_CAN_QPSK
| FE_CAN_INVERSION_AUTO
|
763 .release
= m88rs2000_release
,
764 .init
= m88rs2000_init
,
765 .sleep
= m88rs2000_sleep
,
766 .i2c_gate_ctrl
= m88rs2000_i2c_gate_ctrl
,
767 .read_status
= m88rs2000_read_status
,
768 .read_ber
= m88rs2000_read_ber
,
769 .read_signal_strength
= m88rs2000_read_signal_strength
,
770 .read_snr
= m88rs2000_read_snr
,
771 .read_ucblocks
= m88rs2000_read_ucblocks
,
772 .diseqc_send_master_cmd
= m88rs2000_send_diseqc_msg
,
773 .diseqc_send_burst
= m88rs2000_send_diseqc_burst
,
774 .set_tone
= m88rs2000_set_tone
,
775 .set_voltage
= m88rs2000_set_voltage
,
777 .set_frontend
= m88rs2000_set_frontend
,
778 .get_frontend
= m88rs2000_get_frontend
,
779 .get_tune_settings
= m88rs2000_get_tune_settings
,
782 struct dvb_frontend
*m88rs2000_attach(const struct m88rs2000_config
*config
,
783 struct i2c_adapter
*i2c
)
785 struct m88rs2000_state
*state
= NULL
;
787 /* allocate memory for the internal state */
788 state
= kzalloc(sizeof(struct m88rs2000_state
), GFP_KERNEL
);
792 /* setup the state */
793 state
->config
= config
;
795 state
->tuner_frequency
= 0;
796 state
->symbol_rate
= 0;
797 state
->fec_inner
= 0;
799 /* create dvb_frontend */
800 memcpy(&state
->frontend
.ops
, &m88rs2000_ops
,
801 sizeof(struct dvb_frontend_ops
));
802 state
->frontend
.demodulator_priv
= state
;
803 return &state
->frontend
;
810 EXPORT_SYMBOL(m88rs2000_attach
);
812 MODULE_DESCRIPTION("M88RS2000 DVB-S Demodulator driver");
813 MODULE_AUTHOR("Malcolm Priestley tvboxspy@gmail.com");
814 MODULE_LICENSE("GPL");
815 MODULE_VERSION("1.13");