Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / dvb-frontends / si21xx.c
blobe31eb2c5cc4c91b0af7f86b9dc83f4642abdc5a8
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* DVB compliant Linux driver for the DVB-S si2109/2110 demodulator
4 * Copyright (C) 2008 Igor M. Liplianin (liplianin@me.by)
5 */
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <linux/slab.h>
11 #include <linux/jiffies.h>
12 #include <asm/div64.h>
14 #include <media/dvb_frontend.h>
15 #include "si21xx.h"
17 #define REVISION_REG 0x00
18 #define SYSTEM_MODE_REG 0x01
19 #define TS_CTRL_REG_1 0x02
20 #define TS_CTRL_REG_2 0x03
21 #define PIN_CTRL_REG_1 0x04
22 #define PIN_CTRL_REG_2 0x05
23 #define LOCK_STATUS_REG_1 0x0f
24 #define LOCK_STATUS_REG_2 0x10
25 #define ACQ_STATUS_REG 0x11
26 #define ACQ_CTRL_REG_1 0x13
27 #define ACQ_CTRL_REG_2 0x14
28 #define PLL_DIVISOR_REG 0x15
29 #define COARSE_TUNE_REG 0x16
30 #define FINE_TUNE_REG_L 0x17
31 #define FINE_TUNE_REG_H 0x18
33 #define ANALOG_AGC_POWER_LEVEL_REG 0x28
34 #define CFO_ESTIMATOR_CTRL_REG_1 0x29
35 #define CFO_ESTIMATOR_CTRL_REG_2 0x2a
36 #define CFO_ESTIMATOR_CTRL_REG_3 0x2b
38 #define SYM_RATE_ESTIMATE_REG_L 0x31
39 #define SYM_RATE_ESTIMATE_REG_M 0x32
40 #define SYM_RATE_ESTIMATE_REG_H 0x33
42 #define CFO_ESTIMATOR_OFFSET_REG_L 0x36
43 #define CFO_ESTIMATOR_OFFSET_REG_H 0x37
44 #define CFO_ERROR_REG_L 0x38
45 #define CFO_ERROR_REG_H 0x39
46 #define SYM_RATE_ESTIMATOR_CTRL_REG 0x3a
48 #define SYM_RATE_REG_L 0x3f
49 #define SYM_RATE_REG_M 0x40
50 #define SYM_RATE_REG_H 0x41
51 #define SYM_RATE_ESTIMATOR_MAXIMUM_REG 0x42
52 #define SYM_RATE_ESTIMATOR_MINIMUM_REG 0x43
54 #define C_N_ESTIMATOR_CTRL_REG 0x7c
55 #define C_N_ESTIMATOR_THRSHLD_REG 0x7d
56 #define C_N_ESTIMATOR_LEVEL_REG_L 0x7e
57 #define C_N_ESTIMATOR_LEVEL_REG_H 0x7f
59 #define BLIND_SCAN_CTRL_REG 0x80
61 #define LSA_CTRL_REG_1 0x8D
62 #define SPCTRM_TILT_CORR_THRSHLD_REG 0x8f
63 #define ONE_DB_BNDWDTH_THRSHLD_REG 0x90
64 #define TWO_DB_BNDWDTH_THRSHLD_REG 0x91
65 #define THREE_DB_BNDWDTH_THRSHLD_REG 0x92
66 #define INBAND_POWER_THRSHLD_REG 0x93
67 #define REF_NOISE_LVL_MRGN_THRSHLD_REG 0x94
69 #define VIT_SRCH_CTRL_REG_1 0xa0
70 #define VIT_SRCH_CTRL_REG_2 0xa1
71 #define VIT_SRCH_CTRL_REG_3 0xa2
72 #define VIT_SRCH_STATUS_REG 0xa3
73 #define VITERBI_BER_COUNT_REG_L 0xab
74 #define REED_SOLOMON_CTRL_REG 0xb0
75 #define REED_SOLOMON_ERROR_COUNT_REG_L 0xb1
76 #define PRBS_CTRL_REG 0xb5
78 #define LNB_CTRL_REG_1 0xc0
79 #define LNB_CTRL_REG_2 0xc1
80 #define LNB_CTRL_REG_3 0xc2
81 #define LNB_CTRL_REG_4 0xc3
82 #define LNB_CTRL_STATUS_REG 0xc4
83 #define LNB_FIFO_REGS_0 0xc5
84 #define LNB_FIFO_REGS_1 0xc6
85 #define LNB_FIFO_REGS_2 0xc7
86 #define LNB_FIFO_REGS_3 0xc8
87 #define LNB_FIFO_REGS_4 0xc9
88 #define LNB_FIFO_REGS_5 0xca
89 #define LNB_SUPPLY_CTRL_REG_1 0xcb
90 #define LNB_SUPPLY_CTRL_REG_2 0xcc
91 #define LNB_SUPPLY_CTRL_REG_3 0xcd
92 #define LNB_SUPPLY_CTRL_REG_4 0xce
93 #define LNB_SUPPLY_STATUS_REG 0xcf
95 #define FAIL -1
96 #define PASS 0
98 #define ALLOWABLE_FS_COUNT 10
99 #define STATUS_BER 0
100 #define STATUS_UCBLOCKS 1
102 static int debug;
103 #define dprintk(args...) \
104 do { \
105 if (debug) \
106 printk(KERN_DEBUG "si21xx: " args); \
107 } while (0)
109 enum {
110 ACTIVE_HIGH,
111 ACTIVE_LOW
113 enum {
114 BYTE_WIDE,
115 BIT_WIDE
117 enum {
118 CLK_GAPPED_MODE,
119 CLK_CONTINUOUS_MODE
121 enum {
122 RISING_EDGE,
123 FALLING_EDGE
125 enum {
126 MSB_FIRST,
127 LSB_FIRST
129 enum {
130 SERIAL,
131 PARALLEL
134 struct si21xx_state {
135 struct i2c_adapter *i2c;
136 const struct si21xx_config *config;
137 struct dvb_frontend frontend;
138 u8 initialised:1;
139 int errmode;
140 int fs; /*Sampling rate of the ADC in MHz*/
143 /* register default initialization */
144 static u8 serit_sp1511lhb_inittab[] = {
145 0x01, 0x28, /* set i2c_inc_disable */
146 0x20, 0x03,
147 0x27, 0x20,
148 0xe0, 0x45,
149 0xe1, 0x08,
150 0xfe, 0x01,
151 0x01, 0x28,
152 0x89, 0x09,
153 0x04, 0x80,
154 0x05, 0x01,
155 0x06, 0x00,
156 0x20, 0x03,
157 0x24, 0x88,
158 0x29, 0x09,
159 0x2a, 0x0f,
160 0x2c, 0x10,
161 0x2d, 0x19,
162 0x2e, 0x08,
163 0x2f, 0x10,
164 0x30, 0x19,
165 0x34, 0x20,
166 0x35, 0x03,
167 0x45, 0x02,
168 0x46, 0x45,
169 0x47, 0xd0,
170 0x48, 0x00,
171 0x49, 0x40,
172 0x4a, 0x03,
173 0x4c, 0xfd,
174 0x4f, 0x2e,
175 0x50, 0x2e,
176 0x51, 0x10,
177 0x52, 0x10,
178 0x56, 0x92,
179 0x59, 0x00,
180 0x5a, 0x2d,
181 0x5b, 0x33,
182 0x5c, 0x1f,
183 0x5f, 0x76,
184 0x62, 0xc0,
185 0x63, 0xc0,
186 0x64, 0xf3,
187 0x65, 0xf3,
188 0x79, 0x40,
189 0x6a, 0x40,
190 0x6b, 0x0a,
191 0x6c, 0x80,
192 0x6d, 0x27,
193 0x71, 0x06,
194 0x75, 0x60,
195 0x78, 0x00,
196 0x79, 0xb5,
197 0x7c, 0x05,
198 0x7d, 0x1a,
199 0x87, 0x55,
200 0x88, 0x72,
201 0x8f, 0x08,
202 0x90, 0xe0,
203 0x94, 0x40,
204 0xa0, 0x3f,
205 0xa1, 0xc0,
206 0xa4, 0xcc,
207 0xa5, 0x66,
208 0xa6, 0x66,
209 0xa7, 0x7b,
210 0xa8, 0x7b,
211 0xa9, 0x7b,
212 0xaa, 0x9a,
213 0xed, 0x04,
214 0xad, 0x00,
215 0xae, 0x03,
216 0xcc, 0xab,
217 0x01, 0x08,
218 0xff, 0xff
221 /* low level read/writes */
222 static int si21_writeregs(struct si21xx_state *state, u8 reg1,
223 u8 *data, int len)
225 int ret;
226 u8 buf[60];/* = { reg1, data };*/
227 struct i2c_msg msg = {
228 .addr = state->config->demod_address,
229 .flags = 0,
230 .buf = buf,
231 .len = len + 1
234 if (len > sizeof(buf) - 1)
235 return -EINVAL;
237 msg.buf[0] = reg1;
238 memcpy(msg.buf + 1, data, len);
240 ret = i2c_transfer(state->i2c, &msg, 1);
242 if (ret != 1)
243 dprintk("%s: writereg error (reg1 == 0x%02x, data == 0x%02x, ret == %i)\n",
244 __func__, reg1, data[0], ret);
246 return (ret != 1) ? -EREMOTEIO : 0;
249 static int si21_writereg(struct si21xx_state *state, u8 reg, u8 data)
251 int ret;
252 u8 buf[] = { reg, data };
253 struct i2c_msg msg = {
254 .addr = state->config->demod_address,
255 .flags = 0,
256 .buf = buf,
257 .len = 2
260 ret = i2c_transfer(state->i2c, &msg, 1);
262 if (ret != 1)
263 dprintk("%s: writereg error (reg == 0x%02x, data == 0x%02x, ret == %i)\n",
264 __func__, reg, data, ret);
266 return (ret != 1) ? -EREMOTEIO : 0;
269 static int si21_write(struct dvb_frontend *fe, const u8 buf[], int len)
271 struct si21xx_state *state = fe->demodulator_priv;
273 if (len != 2)
274 return -EINVAL;
276 return si21_writereg(state, buf[0], buf[1]);
279 static u8 si21_readreg(struct si21xx_state *state, u8 reg)
281 int ret;
282 u8 b0[] = { reg };
283 u8 b1[] = { 0 };
284 struct i2c_msg msg[] = {
286 .addr = state->config->demod_address,
287 .flags = 0,
288 .buf = b0,
289 .len = 1
290 }, {
291 .addr = state->config->demod_address,
292 .flags = I2C_M_RD,
293 .buf = b1,
294 .len = 1
298 ret = i2c_transfer(state->i2c, msg, 2);
300 if (ret != 2)
301 dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
302 __func__, reg, ret);
304 return b1[0];
307 static int si21_readregs(struct si21xx_state *state, u8 reg1, u8 *b, u8 len)
309 int ret;
310 struct i2c_msg msg[] = {
312 .addr = state->config->demod_address,
313 .flags = 0,
314 .buf = &reg1,
315 .len = 1
316 }, {
317 .addr = state->config->demod_address,
318 .flags = I2C_M_RD,
319 .buf = b,
320 .len = len
324 ret = i2c_transfer(state->i2c, msg, 2);
326 if (ret != 2)
327 dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
329 return ret == 2 ? 0 : -1;
332 static int si21xx_wait_diseqc_idle(struct si21xx_state *state, int timeout)
334 unsigned long start = jiffies;
336 dprintk("%s\n", __func__);
338 while ((si21_readreg(state, LNB_CTRL_REG_1) & 0x8) == 8) {
339 if (jiffies - start > timeout) {
340 dprintk("%s: timeout!!\n", __func__);
341 return -ETIMEDOUT;
343 msleep(10);
346 return 0;
349 static int si21xx_set_symbolrate(struct dvb_frontend *fe, u32 srate)
351 struct si21xx_state *state = fe->demodulator_priv;
352 u32 sym_rate, data_rate;
353 int i;
354 u8 sym_rate_bytes[3];
356 dprintk("%s : srate = %i\n", __func__ , srate);
358 if ((srate < 1000000) || (srate > 45000000))
359 return -EINVAL;
361 data_rate = srate;
362 sym_rate = 0;
364 for (i = 0; i < 4; ++i) {
365 sym_rate /= 100;
366 sym_rate = sym_rate + ((data_rate % 100) * 0x800000) /
367 state->fs;
368 data_rate /= 100;
370 for (i = 0; i < 3; ++i)
371 sym_rate_bytes[i] = (u8)((sym_rate >> (i * 8)) & 0xff);
373 si21_writeregs(state, SYM_RATE_REG_L, sym_rate_bytes, 0x03);
375 return 0;
378 static int si21xx_send_diseqc_msg(struct dvb_frontend *fe,
379 struct dvb_diseqc_master_cmd *m)
381 struct si21xx_state *state = fe->demodulator_priv;
382 u8 lnb_status;
383 u8 LNB_CTRL_1;
384 int status;
386 dprintk("%s\n", __func__);
388 status = PASS;
389 LNB_CTRL_1 = 0;
391 status |= si21_readregs(state, LNB_CTRL_STATUS_REG, &lnb_status, 0x01);
392 status |= si21_readregs(state, LNB_CTRL_REG_1, &lnb_status, 0x01);
394 /*fill the FIFO*/
395 status |= si21_writeregs(state, LNB_FIFO_REGS_0, m->msg, m->msg_len);
397 LNB_CTRL_1 = (lnb_status & 0x70);
398 LNB_CTRL_1 |= m->msg_len;
400 LNB_CTRL_1 |= 0x80; /* begin LNB signaling */
402 status |= si21_writeregs(state, LNB_CTRL_REG_1, &LNB_CTRL_1, 0x01);
404 return status;
407 static int si21xx_send_diseqc_burst(struct dvb_frontend *fe,
408 enum fe_sec_mini_cmd burst)
410 struct si21xx_state *state = fe->demodulator_priv;
411 u8 val;
413 dprintk("%s\n", __func__);
415 if (si21xx_wait_diseqc_idle(state, 100) < 0)
416 return -ETIMEDOUT;
418 val = (0x80 | si21_readreg(state, 0xc1));
419 if (si21_writereg(state, LNB_CTRL_REG_1,
420 burst == SEC_MINI_A ? (val & ~0x10) : (val | 0x10)))
421 return -EREMOTEIO;
423 if (si21xx_wait_diseqc_idle(state, 100) < 0)
424 return -ETIMEDOUT;
426 if (si21_writereg(state, LNB_CTRL_REG_1, val))
427 return -EREMOTEIO;
429 return 0;
431 /* 30.06.2008 */
432 static int si21xx_set_tone(struct dvb_frontend *fe, enum fe_sec_tone_mode tone)
434 struct si21xx_state *state = fe->demodulator_priv;
435 u8 val;
437 dprintk("%s\n", __func__);
438 val = (0x80 | si21_readreg(state, LNB_CTRL_REG_1));
440 switch (tone) {
441 case SEC_TONE_ON:
442 return si21_writereg(state, LNB_CTRL_REG_1, val | 0x20);
444 case SEC_TONE_OFF:
445 return si21_writereg(state, LNB_CTRL_REG_1, (val & ~0x20));
447 default:
448 return -EINVAL;
452 static int si21xx_set_voltage(struct dvb_frontend *fe, enum fe_sec_voltage volt)
454 struct si21xx_state *state = fe->demodulator_priv;
456 u8 val;
457 dprintk("%s: %s\n", __func__,
458 volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
459 volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
462 val = (0x80 | si21_readreg(state, LNB_CTRL_REG_1));
464 switch (volt) {
465 case SEC_VOLTAGE_18:
466 return si21_writereg(state, LNB_CTRL_REG_1, val | 0x40);
467 case SEC_VOLTAGE_13:
468 return si21_writereg(state, LNB_CTRL_REG_1, (val & ~0x40));
469 default:
470 return -EINVAL;
474 static int si21xx_init(struct dvb_frontend *fe)
476 struct si21xx_state *state = fe->demodulator_priv;
477 int i;
478 int status = 0;
479 u8 reg1;
480 u8 val;
481 u8 reg2[2];
483 dprintk("%s\n", __func__);
485 for (i = 0; ; i += 2) {
486 reg1 = serit_sp1511lhb_inittab[i];
487 val = serit_sp1511lhb_inittab[i+1];
488 if (reg1 == 0xff && val == 0xff)
489 break;
490 si21_writeregs(state, reg1, &val, 1);
493 /*DVB QPSK SYSTEM MODE REG*/
494 reg1 = 0x08;
495 si21_writeregs(state, SYSTEM_MODE_REG, &reg1, 0x01);
497 /*transport stream config*/
499 mode = PARALLEL;
500 sdata_form = LSB_FIRST;
501 clk_edge = FALLING_EDGE;
502 clk_mode = CLK_GAPPED_MODE;
503 strt_len = BYTE_WIDE;
504 sync_pol = ACTIVE_HIGH;
505 val_pol = ACTIVE_HIGH;
506 err_pol = ACTIVE_HIGH;
507 sclk_rate = 0x00;
508 parity = 0x00 ;
509 data_delay = 0x00;
510 clk_delay = 0x00;
511 pclk_smooth = 0x00;
513 reg2[0] =
514 PARALLEL + (LSB_FIRST << 1)
515 + (FALLING_EDGE << 2) + (CLK_GAPPED_MODE << 3)
516 + (BYTE_WIDE << 4) + (ACTIVE_HIGH << 5)
517 + (ACTIVE_HIGH << 6) + (ACTIVE_HIGH << 7);
519 reg2[1] = 0;
520 /* sclk_rate + (parity << 2)
521 + (data_delay << 3) + (clk_delay << 4)
522 + (pclk_smooth << 5);
524 status |= si21_writeregs(state, TS_CTRL_REG_1, reg2, 0x02);
525 if (status != 0)
526 dprintk(" %s : TS Set Error\n", __func__);
528 return 0;
532 static int si21_read_status(struct dvb_frontend *fe, enum fe_status *status)
534 struct si21xx_state *state = fe->demodulator_priv;
535 u8 regs_read[2];
536 u8 reg_read;
537 u8 i;
538 u8 lock;
539 u8 signal = si21_readreg(state, ANALOG_AGC_POWER_LEVEL_REG);
541 si21_readregs(state, LOCK_STATUS_REG_1, regs_read, 0x02);
542 reg_read = 0;
544 for (i = 0; i < 7; ++i)
545 reg_read |= ((regs_read[0] >> i) & 0x01) << (6 - i);
547 lock = ((reg_read & 0x7f) | (regs_read[1] & 0x80));
549 dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, lock);
550 *status = 0;
552 if (signal > 10)
553 *status |= FE_HAS_SIGNAL;
555 if (lock & 0x2)
556 *status |= FE_HAS_CARRIER;
558 if (lock & 0x20)
559 *status |= FE_HAS_VITERBI;
561 if (lock & 0x40)
562 *status |= FE_HAS_SYNC;
564 if ((lock & 0x7b) == 0x7b)
565 *status |= FE_HAS_LOCK;
567 return 0;
570 static int si21_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
572 struct si21xx_state *state = fe->demodulator_priv;
574 /*status = si21_readreg(state, ANALOG_AGC_POWER_LEVEL_REG,
575 (u8*)agclevel, 0x01);*/
577 u16 signal = (3 * si21_readreg(state, 0x27) *
578 si21_readreg(state, 0x28));
580 dprintk("%s : AGCPWR: 0x%02x%02x, signal=0x%04x\n", __func__,
581 si21_readreg(state, 0x27),
582 si21_readreg(state, 0x28), (int) signal);
584 signal <<= 4;
585 *strength = signal;
587 return 0;
590 static int si21_read_ber(struct dvb_frontend *fe, u32 *ber)
592 struct si21xx_state *state = fe->demodulator_priv;
594 dprintk("%s\n", __func__);
596 if (state->errmode != STATUS_BER)
597 return 0;
599 *ber = (si21_readreg(state, 0x1d) << 8) |
600 si21_readreg(state, 0x1e);
602 return 0;
605 static int si21_read_snr(struct dvb_frontend *fe, u16 *snr)
607 struct si21xx_state *state = fe->demodulator_priv;
609 s32 xsnr = 0xffff - ((si21_readreg(state, 0x24) << 8) |
610 si21_readreg(state, 0x25));
611 xsnr = 3 * (xsnr - 0xa100);
612 *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
614 dprintk("%s\n", __func__);
616 return 0;
619 static int si21_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
621 struct si21xx_state *state = fe->demodulator_priv;
623 dprintk("%s\n", __func__);
625 if (state->errmode != STATUS_UCBLOCKS)
626 *ucblocks = 0;
627 else
628 *ucblocks = (si21_readreg(state, 0x1d) << 8) |
629 si21_readreg(state, 0x1e);
631 return 0;
634 /* initiates a channel acquisition sequence
635 using the specified symbol rate and code rate */
636 static int si21xx_setacquire(struct dvb_frontend *fe, int symbrate,
637 enum fe_code_rate crate)
640 struct si21xx_state *state = fe->demodulator_priv;
641 u8 coderates[] = {
642 0x0, 0x01, 0x02, 0x04, 0x00,
643 0x8, 0x10, 0x20, 0x00, 0x3f
646 u8 coderate_ptr;
647 int status;
648 u8 start_acq = 0x80;
649 u8 reg, regs[3];
651 dprintk("%s\n", __func__);
653 status = PASS;
654 coderate_ptr = coderates[crate];
656 si21xx_set_symbolrate(fe, symbrate);
658 /* write code rates to use in the Viterbi search */
659 status |= si21_writeregs(state,
660 VIT_SRCH_CTRL_REG_1,
661 &coderate_ptr, 0x01);
663 /* clear acq_start bit */
664 status |= si21_readregs(state, ACQ_CTRL_REG_2, &reg, 0x01);
665 reg &= ~start_acq;
666 status |= si21_writeregs(state, ACQ_CTRL_REG_2, &reg, 0x01);
668 /* use new Carrier Frequency Offset Estimator (QuickLock) */
669 regs[0] = 0xCB;
670 regs[1] = 0x40;
671 regs[2] = 0xCB;
673 status |= si21_writeregs(state,
674 TWO_DB_BNDWDTH_THRSHLD_REG,
675 &regs[0], 0x03);
676 reg = 0x56;
677 status |= si21_writeregs(state,
678 LSA_CTRL_REG_1, &reg, 1);
679 reg = 0x05;
680 status |= si21_writeregs(state,
681 BLIND_SCAN_CTRL_REG, &reg, 1);
682 /* start automatic acq */
683 status |= si21_writeregs(state,
684 ACQ_CTRL_REG_2, &start_acq, 0x01);
686 return status;
689 static int si21xx_set_frontend(struct dvb_frontend *fe)
691 struct si21xx_state *state = fe->demodulator_priv;
692 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
694 /* freq Channel carrier frequency in KHz (i.e. 1550000 KHz)
695 datarate Channel symbol rate in Sps (i.e. 22500000 Sps)*/
697 /* in MHz */
698 unsigned char coarse_tune_freq;
699 int fine_tune_freq;
700 unsigned char sample_rate = 0;
701 /* boolean */
702 bool inband_interferer_ind;
704 /* INTERMEDIATE VALUES */
705 int icoarse_tune_freq; /* MHz */
706 int ifine_tune_freq; /* MHz */
707 unsigned int band_high;
708 unsigned int band_low;
709 unsigned int x1;
710 unsigned int x2;
711 int i;
712 bool inband_interferer_div2[ALLOWABLE_FS_COUNT];
713 bool inband_interferer_div4[ALLOWABLE_FS_COUNT];
714 int status;
716 /* allowable sample rates for ADC in MHz */
717 int afs[ALLOWABLE_FS_COUNT] = { 200, 192, 193, 194, 195,
718 196, 204, 205, 206, 207
720 /* in MHz */
721 int if_limit_high;
722 int if_limit_low;
723 int lnb_lo;
724 int lnb_uncertanity;
726 int rf_freq;
727 int data_rate;
728 unsigned char regs[4];
730 dprintk("%s : FE_SET_FRONTEND\n", __func__);
732 if (c->delivery_system != SYS_DVBS) {
733 dprintk("%s: unsupported delivery system selected (%d)\n",
734 __func__, c->delivery_system);
735 return -EOPNOTSUPP;
738 for (i = 0; i < ALLOWABLE_FS_COUNT; ++i)
739 inband_interferer_div2[i] = inband_interferer_div4[i] = false;
741 if_limit_high = -700000;
742 if_limit_low = -100000;
743 /* in MHz */
744 lnb_lo = 0;
745 lnb_uncertanity = 0;
747 rf_freq = 10 * c->frequency ;
748 data_rate = c->symbol_rate / 100;
750 status = PASS;
752 band_low = (rf_freq - lnb_lo) - ((lnb_uncertanity * 200)
753 + (data_rate * 135)) / 200;
755 band_high = (rf_freq - lnb_lo) + ((lnb_uncertanity * 200)
756 + (data_rate * 135)) / 200;
759 icoarse_tune_freq = 100000 *
760 (((rf_freq - lnb_lo) -
761 (if_limit_low + if_limit_high) / 2)
762 / 100000);
764 ifine_tune_freq = (rf_freq - lnb_lo) - icoarse_tune_freq ;
766 for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
767 x1 = ((rf_freq - lnb_lo) / (afs[i] * 2500)) *
768 (afs[i] * 2500) + afs[i] * 2500;
770 x2 = ((rf_freq - lnb_lo) / (afs[i] * 2500)) *
771 (afs[i] * 2500);
773 if (((band_low < x1) && (x1 < band_high)) ||
774 ((band_low < x2) && (x2 < band_high)))
775 inband_interferer_div4[i] = true;
779 for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
780 x1 = ((rf_freq - lnb_lo) / (afs[i] * 5000)) *
781 (afs[i] * 5000) + afs[i] * 5000;
783 x2 = ((rf_freq - lnb_lo) / (afs[i] * 5000)) *
784 (afs[i] * 5000);
786 if (((band_low < x1) && (x1 < band_high)) ||
787 ((band_low < x2) && (x2 < band_high)))
788 inband_interferer_div2[i] = true;
791 inband_interferer_ind = true;
792 for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
793 if (inband_interferer_div2[i] || inband_interferer_div4[i]) {
794 inband_interferer_ind = false;
795 break;
799 if (inband_interferer_ind) {
800 for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
801 if (!inband_interferer_div2[i]) {
802 sample_rate = (u8) afs[i];
803 break;
806 } else {
807 for (i = 0; i < ALLOWABLE_FS_COUNT; ++i) {
808 if ((inband_interferer_div2[i] ||
809 !inband_interferer_div4[i])) {
810 sample_rate = (u8) afs[i];
811 break;
817 if (sample_rate > 207 || sample_rate < 192)
818 sample_rate = 200;
820 fine_tune_freq = ((0x4000 * (ifine_tune_freq / 10)) /
821 ((sample_rate) * 1000));
823 coarse_tune_freq = (u8)(icoarse_tune_freq / 100000);
825 regs[0] = sample_rate;
826 regs[1] = coarse_tune_freq;
827 regs[2] = fine_tune_freq & 0xFF;
828 regs[3] = fine_tune_freq >> 8 & 0xFF;
830 status |= si21_writeregs(state, PLL_DIVISOR_REG, &regs[0], 0x04);
832 state->fs = sample_rate;/*ADC MHz*/
833 si21xx_setacquire(fe, c->symbol_rate, c->fec_inner);
835 return 0;
838 static int si21xx_sleep(struct dvb_frontend *fe)
840 struct si21xx_state *state = fe->demodulator_priv;
841 u8 regdata;
843 dprintk("%s\n", __func__);
845 si21_readregs(state, SYSTEM_MODE_REG, &regdata, 0x01);
846 regdata |= 1 << 6;
847 si21_writeregs(state, SYSTEM_MODE_REG, &regdata, 0x01);
848 state->initialised = 0;
850 return 0;
853 static void si21xx_release(struct dvb_frontend *fe)
855 struct si21xx_state *state = fe->demodulator_priv;
857 dprintk("%s\n", __func__);
859 kfree(state);
862 static const struct dvb_frontend_ops si21xx_ops = {
863 .delsys = { SYS_DVBS },
864 .info = {
865 .name = "SL SI21XX DVB-S",
866 .frequency_min_hz = 950 * MHz,
867 .frequency_max_hz = 2150 * MHz,
868 .frequency_stepsize_hz = 125 * kHz,
869 .symbol_rate_min = 1000000,
870 .symbol_rate_max = 45000000,
871 .symbol_rate_tolerance = 500, /* ppm */
872 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
873 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
874 FE_CAN_QPSK |
875 FE_CAN_FEC_AUTO
878 .release = si21xx_release,
879 .init = si21xx_init,
880 .sleep = si21xx_sleep,
881 .write = si21_write,
882 .read_status = si21_read_status,
883 .read_ber = si21_read_ber,
884 .read_signal_strength = si21_read_signal_strength,
885 .read_snr = si21_read_snr,
886 .read_ucblocks = si21_read_ucblocks,
887 .diseqc_send_master_cmd = si21xx_send_diseqc_msg,
888 .diseqc_send_burst = si21xx_send_diseqc_burst,
889 .set_tone = si21xx_set_tone,
890 .set_voltage = si21xx_set_voltage,
892 .set_frontend = si21xx_set_frontend,
895 struct dvb_frontend *si21xx_attach(const struct si21xx_config *config,
896 struct i2c_adapter *i2c)
898 struct si21xx_state *state = NULL;
899 int id;
901 dprintk("%s\n", __func__);
903 /* allocate memory for the internal state */
904 state = kzalloc(sizeof(struct si21xx_state), GFP_KERNEL);
905 if (state == NULL)
906 goto error;
908 /* setup the state */
909 state->config = config;
910 state->i2c = i2c;
911 state->initialised = 0;
912 state->errmode = STATUS_BER;
914 /* check if the demod is there */
915 id = si21_readreg(state, SYSTEM_MODE_REG);
916 si21_writereg(state, SYSTEM_MODE_REG, id | 0x40); /* standby off */
917 msleep(200);
918 id = si21_readreg(state, 0x00);
920 /* register 0x00 contains:
921 0x34 for SI2107
922 0x24 for SI2108
923 0x14 for SI2109
924 0x04 for SI2110
926 if (id != 0x04 && id != 0x14)
927 goto error;
929 /* create dvb_frontend */
930 memcpy(&state->frontend.ops, &si21xx_ops,
931 sizeof(struct dvb_frontend_ops));
932 state->frontend.demodulator_priv = state;
933 return &state->frontend;
935 error:
936 kfree(state);
937 return NULL;
939 EXPORT_SYMBOL(si21xx_attach);
941 module_param(debug, int, 0644);
942 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
944 MODULE_DESCRIPTION("SL SI21XX DVB Demodulator driver");
945 MODULE_AUTHOR("Igor M. Liplianin");
946 MODULE_LICENSE("GPL");