2 * Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
4 * Copyright (C) 2005 Steven Toth <stoth@linuxtv.org>
6 * Support for KWorld DVB-S 100 by Vadim Catana <skystar@moldova.cc>
8 * Support for CX24123/CX24113-NIM by Patrick Boettcher <pb@linuxtv.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (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 GNU
18 * 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.
25 #include <linux/slab.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
30 #include "dvb_frontend.h"
35 static int force_band
;
36 module_param(force_band
, int, 0644);
37 MODULE_PARM_DESC(force_band
, "Force a specific band select "\
38 "(1-9, default:off).");
41 module_param(debug
, int, 0644);
42 MODULE_PARM_DESC(debug
, "Activates frontend debugging (default:0)");
44 #define info(args...) do { printk(KERN_INFO "CX24123: " args); } while (0)
45 #define err(args...) do { printk(KERN_ERR "CX24123: " args); } while (0)
47 #define dprintk(args...) \
50 printk(KERN_DEBUG "CX24123: %s: ", __func__); \
55 struct cx24123_state
{
56 struct i2c_adapter
*i2c
;
57 const struct cx24123_config
*config
;
59 struct dvb_frontend frontend
;
61 /* Some PLL specifics for tuning */
68 struct i2c_adapter tuner_i2c_adapter
;
72 /* The Demod/Tuner can't easily provide these, we cache them */
74 u32 currentsymbolrate
;
77 /* Various tuner defaults need to be established for a given symbol rate Sps */
78 static struct cx24123_AGC_val
{
84 } cx24123_AGC_vals
[] =
87 .symbolrate_low
= 1000000,
88 .symbolrate_high
= 4999999,
89 /* the specs recommend other values for VGA offsets,
90 but tests show they are wrong */
91 .VGAprogdata
= (1 << 19) | (0x180 << 9) | 0x1e0,
92 .VCAprogdata
= (2 << 19) | (0x07 << 9) | 0x07,
93 .FILTune
= 0x27f /* 0.41 V */
96 .symbolrate_low
= 5000000,
97 .symbolrate_high
= 14999999,
98 .VGAprogdata
= (1 << 19) | (0x180 << 9) | 0x1e0,
99 .VCAprogdata
= (2 << 19) | (0x07 << 9) | 0x1f,
100 .FILTune
= 0x317 /* 0.90 V */
103 .symbolrate_low
= 15000000,
104 .symbolrate_high
= 45000000,
105 .VGAprogdata
= (1 << 19) | (0x100 << 9) | 0x180,
106 .VCAprogdata
= (2 << 19) | (0x07 << 9) | 0x3f,
107 .FILTune
= 0x145 /* 2.70 V */
111 static struct cx24123_bandselect_val
{
116 } cx24123_bandselect_vals
[] =
121 .freq_high
= 1074999,
123 .progdata
= (0 << 19) | (0 << 9) | 0x40,
129 .freq_high
= 1177999,
131 .progdata
= (0 << 19) | (0 << 9) | 0x80,
137 .freq_high
= 1295999,
139 .progdata
= (0 << 19) | (1 << 9) | 0x01,
145 .freq_high
= 1431999,
147 .progdata
= (0 << 19) | (1 << 9) | 0x02,
153 .freq_high
= 1575999,
155 .progdata
= (0 << 19) | (1 << 9) | 0x04,
161 .freq_high
= 1717999,
163 .progdata
= (0 << 19) | (1 << 9) | 0x08,
169 .freq_high
= 1855999,
171 .progdata
= (0 << 19) | (1 << 9) | 0x10,
177 .freq_high
= 2035999,
179 .progdata
= (0 << 19) | (1 << 9) | 0x20,
185 .freq_high
= 2150000,
187 .progdata
= (0 << 19) | (1 << 9) | 0x40,
194 } cx24123_regdata
[] =
196 {0x00, 0x03}, /* Reset system */
197 {0x00, 0x00}, /* Clear reset */
198 {0x03, 0x07}, /* QPSK, DVB, Auto Acquisition (default) */
199 {0x04, 0x10}, /* MPEG */
200 {0x05, 0x04}, /* MPEG */
201 {0x06, 0x31}, /* MPEG (default) */
202 {0x0b, 0x00}, /* Freq search start point (default) */
203 {0x0c, 0x00}, /* Demodulator sample gain (default) */
204 {0x0d, 0x7f}, /* Force driver to shift until the maximum (+-10 MHz) */
205 {0x0e, 0x03}, /* Default non-inverted, FEC 3/4 (default) */
206 {0x0f, 0xfe}, /* FEC search mask (all supported codes) */
207 {0x10, 0x01}, /* Default search inversion, no repeat (default) */
208 {0x16, 0x00}, /* Enable reading of frequency */
209 {0x17, 0x01}, /* Enable EsNO Ready Counter */
210 {0x1c, 0x80}, /* Enable error counter */
211 {0x20, 0x00}, /* Tuner burst clock rate = 500KHz */
212 {0x21, 0x15}, /* Tuner burst mode, word length = 0x15 */
213 {0x28, 0x00}, /* Enable FILTERV with positive pol., DiSEqC 2.x off */
214 {0x29, 0x00}, /* DiSEqC LNB_DC off */
215 {0x2a, 0xb0}, /* DiSEqC Parameters (default) */
216 {0x2b, 0x73}, /* DiSEqC Tone Frequency (default) */
217 {0x2c, 0x00}, /* DiSEqC Message (0x2c - 0x31) */
223 {0x32, 0x8c}, /* DiSEqC Parameters (default) */
224 {0x33, 0x00}, /* Interrupts off (0x33 - 0x34) */
226 {0x35, 0x03}, /* DiSEqC Tone Amplitude (default) */
227 {0x36, 0x02}, /* DiSEqC Parameters (default) */
228 {0x37, 0x3a}, /* DiSEqC Parameters (default) */
229 {0x3a, 0x00}, /* Enable AGC accumulator (for signal strength) */
230 {0x44, 0x00}, /* Constellation (default) */
231 {0x45, 0x00}, /* Symbol count (default) */
232 {0x46, 0x0d}, /* Symbol rate estimator on (default) */
233 {0x56, 0xc1}, /* Error Counter = Viterbi BER */
234 {0x57, 0xff}, /* Error Counter Window (default) */
235 {0x5c, 0x20}, /* Acquisition AFC Expiration window (default is 0x10) */
236 {0x67, 0x83}, /* Non-DCII symbol clock */
239 static int cx24123_i2c_writereg(struct cx24123_state
*state
,
240 u8 i2c_addr
, int reg
, int data
)
242 u8 buf
[] = { reg
, data
};
243 struct i2c_msg msg
= {
244 .addr
= i2c_addr
, .flags
= 0, .buf
= buf
, .len
= 2
248 /* printk(KERN_DEBUG "wr(%02x): %02x %02x\n", i2c_addr, reg, data); */
250 err
= i2c_transfer(state
->i2c
, &msg
, 1);
252 printk("%s: writereg error(err == %i, reg == 0x%02x,"
253 " data == 0x%02x)\n", __func__
, err
, reg
, data
);
260 static int cx24123_i2c_readreg(struct cx24123_state
*state
, u8 i2c_addr
, u8 reg
)
264 struct i2c_msg msg
[] = {
265 { .addr
= i2c_addr
, .flags
= 0, .buf
= ®
, .len
= 1 },
266 { .addr
= i2c_addr
, .flags
= I2C_M_RD
, .buf
= &b
, .len
= 1 }
269 ret
= i2c_transfer(state
->i2c
, msg
, 2);
272 err("%s: reg=0x%x (error=%d)\n", __func__
, reg
, ret
);
276 /* printk(KERN_DEBUG "rd(%02x): %02x %02x\n", i2c_addr, reg, b); */
281 #define cx24123_readreg(state, reg) \
282 cx24123_i2c_readreg(state, state->config->demod_address, reg)
283 #define cx24123_writereg(state, reg, val) \
284 cx24123_i2c_writereg(state, state->config->demod_address, reg, val)
286 static int cx24123_set_inversion(struct cx24123_state
*state
,
287 fe_spectral_inversion_t inversion
)
289 u8 nom_reg
= cx24123_readreg(state
, 0x0e);
290 u8 auto_reg
= cx24123_readreg(state
, 0x10);
294 dprintk("inversion off\n");
295 cx24123_writereg(state
, 0x0e, nom_reg
& ~0x80);
296 cx24123_writereg(state
, 0x10, auto_reg
| 0x80);
299 dprintk("inversion on\n");
300 cx24123_writereg(state
, 0x0e, nom_reg
| 0x80);
301 cx24123_writereg(state
, 0x10, auto_reg
| 0x80);
304 dprintk("inversion auto\n");
305 cx24123_writereg(state
, 0x10, auto_reg
& ~0x80);
314 static int cx24123_get_inversion(struct cx24123_state
*state
,
315 fe_spectral_inversion_t
*inversion
)
319 val
= cx24123_readreg(state
, 0x1b) >> 7;
322 dprintk("read inversion off\n");
323 *inversion
= INVERSION_OFF
;
325 dprintk("read inversion on\n");
326 *inversion
= INVERSION_ON
;
332 static int cx24123_set_fec(struct cx24123_state
*state
, fe_code_rate_t fec
)
334 u8 nom_reg
= cx24123_readreg(state
, 0x0e) & ~0x07;
336 if ((fec
< FEC_NONE
) || (fec
> FEC_AUTO
))
339 /* Set the soft decision threshold */
341 cx24123_writereg(state
, 0x43,
342 cx24123_readreg(state
, 0x43) | 0x01);
344 cx24123_writereg(state
, 0x43,
345 cx24123_readreg(state
, 0x43) & ~0x01);
349 dprintk("set FEC to 1/2\n");
350 cx24123_writereg(state
, 0x0e, nom_reg
| 0x01);
351 cx24123_writereg(state
, 0x0f, 0x02);
354 dprintk("set FEC to 2/3\n");
355 cx24123_writereg(state
, 0x0e, nom_reg
| 0x02);
356 cx24123_writereg(state
, 0x0f, 0x04);
359 dprintk("set FEC to 3/4\n");
360 cx24123_writereg(state
, 0x0e, nom_reg
| 0x03);
361 cx24123_writereg(state
, 0x0f, 0x08);
364 dprintk("set FEC to 4/5\n");
365 cx24123_writereg(state
, 0x0e, nom_reg
| 0x04);
366 cx24123_writereg(state
, 0x0f, 0x10);
369 dprintk("set FEC to 5/6\n");
370 cx24123_writereg(state
, 0x0e, nom_reg
| 0x05);
371 cx24123_writereg(state
, 0x0f, 0x20);
374 dprintk("set FEC to 6/7\n");
375 cx24123_writereg(state
, 0x0e, nom_reg
| 0x06);
376 cx24123_writereg(state
, 0x0f, 0x40);
379 dprintk("set FEC to 7/8\n");
380 cx24123_writereg(state
, 0x0e, nom_reg
| 0x07);
381 cx24123_writereg(state
, 0x0f, 0x80);
384 dprintk("set FEC to auto\n");
385 cx24123_writereg(state
, 0x0f, 0xfe);
394 static int cx24123_get_fec(struct cx24123_state
*state
, fe_code_rate_t
*fec
)
398 ret
= cx24123_readreg(state
, 0x1b);
426 /* this can happen when there's no lock */
433 /* Approximation of closest integer of log2(a/b). It actually gives the
434 lowest integer i such that 2^i >= round(a/b) */
435 static u32
cx24123_int_log2(u32 a
, u32 b
)
437 u32 exp
, nearest
= 0;
441 if (div
< (1 << 31)) {
442 for (exp
= 1; div
> exp
; nearest
++)
448 static int cx24123_set_symbolrate(struct cx24123_state
*state
, u32 srate
)
450 u32 tmp
, sample_rate
, ratio
, sample_gain
;
453 /* check if symbol rate is within limits */
454 if ((srate
> state
->frontend
.ops
.info
.symbol_rate_max
) ||
455 (srate
< state
->frontend
.ops
.info
.symbol_rate_min
))
458 /* choose the sampling rate high enough for the required operation,
459 while optimizing the power consumed by the demodulator */
460 if (srate
< (XTAL
*2)/2)
462 else if (srate
< (XTAL
*3)/2)
464 else if (srate
< (XTAL
*4)/2)
466 else if (srate
< (XTAL
*5)/2)
468 else if (srate
< (XTAL
*6)/2)
470 else if (srate
< (XTAL
*7)/2)
472 else if (srate
< (XTAL
*8)/2)
478 sample_rate
= pll_mult
* XTAL
;
481 SYSSymbolRate[21:0] = (srate << 23) / sample_rate
483 We have to use 32 bit unsigned arithmetic without precision loss.
484 The maximum srate is 45000000 or 0x02AEA540. This number has
485 only 6 clear bits on top, hence we can shift it left only 6 bits
486 at a time. Borrowed from cx24110.c
490 ratio
= tmp
/ sample_rate
;
492 tmp
= (tmp
% sample_rate
) << 6;
493 ratio
= (ratio
<< 6) + (tmp
/ sample_rate
);
495 tmp
= (tmp
% sample_rate
) << 6;
496 ratio
= (ratio
<< 6) + (tmp
/ sample_rate
);
498 tmp
= (tmp
% sample_rate
) << 5;
499 ratio
= (ratio
<< 5) + (tmp
/ sample_rate
);
502 cx24123_writereg(state
, 0x01, pll_mult
* 6);
504 cx24123_writereg(state
, 0x08, (ratio
>> 16) & 0x3f);
505 cx24123_writereg(state
, 0x09, (ratio
>> 8) & 0xff);
506 cx24123_writereg(state
, 0x0a, ratio
& 0xff);
508 /* also set the demodulator sample gain */
509 sample_gain
= cx24123_int_log2(sample_rate
, srate
);
510 tmp
= cx24123_readreg(state
, 0x0c) & ~0xe0;
511 cx24123_writereg(state
, 0x0c, tmp
| sample_gain
<< 5);
513 dprintk("srate=%d, ratio=0x%08x, sample_rate=%i sample_gain=%d\n",
514 srate
, ratio
, sample_rate
, sample_gain
);
520 * Based on the required frequency and symbolrate, the tuner AGC has
521 * to be configured and the correct band selected.
522 * Calculate those values.
524 static int cx24123_pll_calculate(struct dvb_frontend
*fe
,
525 struct dvb_frontend_parameters
*p
)
527 struct cx24123_state
*state
= fe
->demodulator_priv
;
528 u32 ndiv
= 0, adiv
= 0, vco_div
= 0;
532 int num_bands
= ARRAY_SIZE(cx24123_bandselect_vals
);
533 struct cx24123_bandselect_val
*bsv
= NULL
;
534 struct cx24123_AGC_val
*agcv
= NULL
;
536 /* Defaults for low freq, low rate */
537 state
->VCAarg
= cx24123_AGC_vals
[0].VCAprogdata
;
538 state
->VGAarg
= cx24123_AGC_vals
[0].VGAprogdata
;
539 state
->bandselectarg
= cx24123_bandselect_vals
[0].progdata
;
540 vco_div
= cx24123_bandselect_vals
[0].VCOdivider
;
542 /* For the given symbol rate, determine the VCA, VGA and
543 * FILTUNE programming bits */
544 for (i
= 0; i
< ARRAY_SIZE(cx24123_AGC_vals
); i
++) {
545 agcv
= &cx24123_AGC_vals
[i
];
546 if ((agcv
->symbolrate_low
<= p
->u
.qpsk
.symbol_rate
) &&
547 (agcv
->symbolrate_high
>= p
->u
.qpsk
.symbol_rate
)) {
548 state
->VCAarg
= agcv
->VCAprogdata
;
549 state
->VGAarg
= agcv
->VGAprogdata
;
550 state
->FILTune
= agcv
->FILTune
;
554 /* determine the band to use */
555 if (force_band
< 1 || force_band
> num_bands
) {
556 for (i
= 0; i
< num_bands
; i
++) {
557 bsv
= &cx24123_bandselect_vals
[i
];
558 if ((bsv
->freq_low
<= p
->frequency
) &&
559 (bsv
->freq_high
>= p
->frequency
))
563 band
= force_band
- 1;
565 state
->bandselectarg
= cx24123_bandselect_vals
[band
].progdata
;
566 vco_div
= cx24123_bandselect_vals
[band
].VCOdivider
;
568 /* determine the charge pump current */
569 if (p
->frequency
< (cx24123_bandselect_vals
[band
].freq_low
+
570 cx24123_bandselect_vals
[band
].freq_high
) / 2)
575 /* Determine the N/A dividers for the requested lband freq (in kHz). */
576 /* Note: the reference divider R=10, frequency is in KHz,
578 ndiv
= (((p
->frequency
* vco_div
* 10) /
579 (2 * XTAL
/ 1000)) / 32) & 0x1ff;
580 adiv
= (((p
->frequency
* vco_div
* 10) /
581 (2 * XTAL
/ 1000)) % 32) & 0x1f;
583 if (adiv
== 0 && ndiv
> 0)
586 /* control bits 11, refdiv 11, charge pump polarity 1,
587 * charge pump current, ndiv, adiv */
588 state
->pllarg
= (3 << 19) | (3 << 17) | (1 << 16) |
589 (pump
<< 14) | (ndiv
<< 5) | adiv
;
595 * Tuner data is 21 bits long, must be left-aligned in data.
596 * Tuner cx24109 is written through a dedicated 3wire interface
599 static int cx24123_pll_writereg(struct dvb_frontend
*fe
,
600 struct dvb_frontend_parameters
*p
, u32 data
)
602 struct cx24123_state
*state
= fe
->demodulator_priv
;
603 unsigned long timeout
;
605 dprintk("pll writereg called, data=0x%08x\n", data
);
607 /* align the 21 bytes into to bit23 boundary */
610 /* Reset the demod pll word length to 0x15 bits */
611 cx24123_writereg(state
, 0x21, 0x15);
613 /* write the msb 8 bits, wait for the send to be completed */
614 timeout
= jiffies
+ msecs_to_jiffies(40);
615 cx24123_writereg(state
, 0x22, (data
>> 16) & 0xff);
616 while ((cx24123_readreg(state
, 0x20) & 0x40) == 0) {
617 if (time_after(jiffies
, timeout
)) {
618 err("%s: demodulator is not responding, "\
619 "possibly hung, aborting.\n", __func__
);
625 /* send another 8 bytes, wait for the send to be completed */
626 timeout
= jiffies
+ msecs_to_jiffies(40);
627 cx24123_writereg(state
, 0x22, (data
>> 8) & 0xff);
628 while ((cx24123_readreg(state
, 0x20) & 0x40) == 0) {
629 if (time_after(jiffies
, timeout
)) {
630 err("%s: demodulator is not responding, "\
631 "possibly hung, aborting.\n", __func__
);
637 /* send the lower 5 bits of this byte, padded with 3 LBB,
638 * wait for the send to be completed */
639 timeout
= jiffies
+ msecs_to_jiffies(40);
640 cx24123_writereg(state
, 0x22, (data
) & 0xff);
641 while ((cx24123_readreg(state
, 0x20) & 0x80)) {
642 if (time_after(jiffies
, timeout
)) {
643 err("%s: demodulator is not responding," \
644 "possibly hung, aborting.\n", __func__
);
650 /* Trigger the demod to configure the tuner */
651 cx24123_writereg(state
, 0x20, cx24123_readreg(state
, 0x20) | 2);
652 cx24123_writereg(state
, 0x20, cx24123_readreg(state
, 0x20) & 0xfd);
657 static int cx24123_pll_tune(struct dvb_frontend
*fe
,
658 struct dvb_frontend_parameters
*p
)
660 struct cx24123_state
*state
= fe
->demodulator_priv
;
663 dprintk("frequency=%i\n", p
->frequency
);
665 if (cx24123_pll_calculate(fe
, p
) != 0) {
666 err("%s: cx24123_pll_calcutate failed\n", __func__
);
670 /* Write the new VCO/VGA */
671 cx24123_pll_writereg(fe
, p
, state
->VCAarg
);
672 cx24123_pll_writereg(fe
, p
, state
->VGAarg
);
674 /* Write the new bandselect and pll args */
675 cx24123_pll_writereg(fe
, p
, state
->bandselectarg
);
676 cx24123_pll_writereg(fe
, p
, state
->pllarg
);
678 /* set the FILTUNE voltage */
679 val
= cx24123_readreg(state
, 0x28) & ~0x3;
680 cx24123_writereg(state
, 0x27, state
->FILTune
>> 2);
681 cx24123_writereg(state
, 0x28, val
| (state
->FILTune
& 0x3));
683 dprintk("pll tune VCA=%d, band=%d, pll=%d\n", state
->VCAarg
,
684 state
->bandselectarg
, state
->pllarg
);
692 * [7:7] = BTI enabled
693 * [6:6] = I2C repeater enabled
694 * [5:5] = I2C repeater start
698 /* mode == 1 -> i2c-repeater, 0 -> bti */
699 static int cx24123_repeater_mode(struct cx24123_state
*state
, u8 mode
, u8 start
)
701 u8 r
= cx24123_readreg(state
, 0x23) & 0x1e;
703 r
|= (1 << 6) | (start
<< 5);
705 r
|= (1 << 7) | (start
);
706 return cx24123_writereg(state
, 0x23, r
);
709 static int cx24123_initfe(struct dvb_frontend
*fe
)
711 struct cx24123_state
*state
= fe
->demodulator_priv
;
714 dprintk("init frontend\n");
716 /* Configure the demod to a good set of defaults */
717 for (i
= 0; i
< ARRAY_SIZE(cx24123_regdata
); i
++)
718 cx24123_writereg(state
, cx24123_regdata
[i
].reg
,
719 cx24123_regdata
[i
].data
);
721 /* Set the LNB polarity */
722 if (state
->config
->lnb_polarity
)
723 cx24123_writereg(state
, 0x32,
724 cx24123_readreg(state
, 0x32) | 0x02);
726 if (state
->config
->dont_use_pll
)
727 cx24123_repeater_mode(state
, 1, 0);
732 static int cx24123_set_voltage(struct dvb_frontend
*fe
,
733 fe_sec_voltage_t voltage
)
735 struct cx24123_state
*state
= fe
->demodulator_priv
;
738 val
= cx24123_readreg(state
, 0x29) & ~0x40;
742 dprintk("setting voltage 13V\n");
743 return cx24123_writereg(state
, 0x29, val
& 0x7f);
745 dprintk("setting voltage 18V\n");
746 return cx24123_writereg(state
, 0x29, val
| 0x80);
747 case SEC_VOLTAGE_OFF
:
748 /* already handled in cx88-dvb */
757 /* wait for diseqc queue to become ready (or timeout) */
758 static void cx24123_wait_for_diseqc(struct cx24123_state
*state
)
760 unsigned long timeout
= jiffies
+ msecs_to_jiffies(200);
761 while (!(cx24123_readreg(state
, 0x29) & 0x40)) {
762 if (time_after(jiffies
, timeout
)) {
763 err("%s: diseqc queue not ready, " \
764 "command may be lost.\n", __func__
);
771 static int cx24123_send_diseqc_msg(struct dvb_frontend
*fe
,
772 struct dvb_diseqc_master_cmd
*cmd
)
774 struct cx24123_state
*state
= fe
->demodulator_priv
;
779 /* stop continuous tone if enabled */
780 tone
= cx24123_readreg(state
, 0x29);
782 cx24123_writereg(state
, 0x29, tone
& ~0x50);
784 /* wait for diseqc queue ready */
785 cx24123_wait_for_diseqc(state
);
787 /* select tone mode */
788 cx24123_writereg(state
, 0x2a, cx24123_readreg(state
, 0x2a) & 0xfb);
790 for (i
= 0; i
< cmd
->msg_len
; i
++)
791 cx24123_writereg(state
, 0x2C + i
, cmd
->msg
[i
]);
793 val
= cx24123_readreg(state
, 0x29);
794 cx24123_writereg(state
, 0x29, ((val
& 0x90) | 0x40) |
795 ((cmd
->msg_len
-3) & 3));
797 /* wait for diseqc message to finish sending */
798 cx24123_wait_for_diseqc(state
);
800 /* restart continuous tone if enabled */
802 cx24123_writereg(state
, 0x29, tone
& ~0x40);
807 static int cx24123_diseqc_send_burst(struct dvb_frontend
*fe
,
808 fe_sec_mini_cmd_t burst
)
810 struct cx24123_state
*state
= fe
->demodulator_priv
;
815 /* stop continuous tone if enabled */
816 tone
= cx24123_readreg(state
, 0x29);
818 cx24123_writereg(state
, 0x29, tone
& ~0x50);
820 /* wait for diseqc queue ready */
821 cx24123_wait_for_diseqc(state
);
823 /* select tone mode */
824 cx24123_writereg(state
, 0x2a, cx24123_readreg(state
, 0x2a) | 0x4);
826 val
= cx24123_readreg(state
, 0x29);
827 if (burst
== SEC_MINI_A
)
828 cx24123_writereg(state
, 0x29, ((val
& 0x90) | 0x40 | 0x00));
829 else if (burst
== SEC_MINI_B
)
830 cx24123_writereg(state
, 0x29, ((val
& 0x90) | 0x40 | 0x08));
834 cx24123_wait_for_diseqc(state
);
835 cx24123_writereg(state
, 0x2a, cx24123_readreg(state
, 0x2a) & 0xfb);
837 /* restart continuous tone if enabled */
839 cx24123_writereg(state
, 0x29, tone
& ~0x40);
844 static int cx24123_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
846 struct cx24123_state
*state
= fe
->demodulator_priv
;
847 int sync
= cx24123_readreg(state
, 0x14);
850 if (state
->config
->dont_use_pll
) {
852 if (fe
->ops
.tuner_ops
.get_status
)
853 fe
->ops
.tuner_ops
.get_status(fe
, &tun_status
);
854 if (tun_status
& TUNER_STATUS_LOCKED
)
855 *status
|= FE_HAS_SIGNAL
;
857 int lock
= cx24123_readreg(state
, 0x20);
859 *status
|= FE_HAS_SIGNAL
;
863 *status
|= FE_HAS_CARRIER
; /* Phase locked */
865 *status
|= FE_HAS_VITERBI
;
867 /* Reed-Solomon Status */
869 *status
|= FE_HAS_SYNC
;
871 *status
|= FE_HAS_LOCK
; /*Full Sync */
877 * Configured to return the measurement of errors in blocks,
878 * because no UCBLOCKS value is available, so this value doubles up
879 * to satisfy both measurements.
881 static int cx24123_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
883 struct cx24123_state
*state
= fe
->demodulator_priv
;
885 /* The true bit error rate is this value divided by
886 the window size (set as 256 * 255) */
887 *ber
= ((cx24123_readreg(state
, 0x1c) & 0x3f) << 16) |
888 (cx24123_readreg(state
, 0x1d) << 8 |
889 cx24123_readreg(state
, 0x1e));
891 dprintk("BER = %d\n", *ber
);
896 static int cx24123_read_signal_strength(struct dvb_frontend
*fe
,
897 u16
*signal_strength
)
899 struct cx24123_state
*state
= fe
->demodulator_priv
;
901 /* larger = better */
902 *signal_strength
= cx24123_readreg(state
, 0x3b) << 8;
904 dprintk("Signal strength = %d\n", *signal_strength
);
909 static int cx24123_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
911 struct cx24123_state
*state
= fe
->demodulator_priv
;
913 /* Inverted raw Es/N0 count, totally bogus but better than the
915 *snr
= 65535 - (((u16
)cx24123_readreg(state
, 0x18) << 8) |
916 (u16
)cx24123_readreg(state
, 0x19));
918 dprintk("read S/N index = %d\n", *snr
);
923 static int cx24123_set_frontend(struct dvb_frontend
*fe
,
924 struct dvb_frontend_parameters
*p
)
926 struct cx24123_state
*state
= fe
->demodulator_priv
;
930 if (state
->config
->set_ts_params
)
931 state
->config
->set_ts_params(fe
, 0);
933 state
->currentfreq
= p
->frequency
;
934 state
->currentsymbolrate
= p
->u
.qpsk
.symbol_rate
;
936 cx24123_set_inversion(state
, p
->inversion
);
937 cx24123_set_fec(state
, p
->u
.qpsk
.fec_inner
);
938 cx24123_set_symbolrate(state
, p
->u
.qpsk
.symbol_rate
);
940 if (!state
->config
->dont_use_pll
)
941 cx24123_pll_tune(fe
, p
);
942 else if (fe
->ops
.tuner_ops
.set_params
)
943 fe
->ops
.tuner_ops
.set_params(fe
, p
);
945 err("it seems I don't have a tuner...");
947 /* Enable automatic aquisition and reset cycle */
948 cx24123_writereg(state
, 0x03, (cx24123_readreg(state
, 0x03) | 0x07));
949 cx24123_writereg(state
, 0x00, 0x10);
950 cx24123_writereg(state
, 0x00, 0);
952 if (state
->config
->agc_callback
)
953 state
->config
->agc_callback(fe
);
958 static int cx24123_get_frontend(struct dvb_frontend
*fe
,
959 struct dvb_frontend_parameters
*p
)
961 struct cx24123_state
*state
= fe
->demodulator_priv
;
965 if (cx24123_get_inversion(state
, &p
->inversion
) != 0) {
966 err("%s: Failed to get inversion status\n", __func__
);
969 if (cx24123_get_fec(state
, &p
->u
.qpsk
.fec_inner
) != 0) {
970 err("%s: Failed to get fec status\n", __func__
);
973 p
->frequency
= state
->currentfreq
;
974 p
->u
.qpsk
.symbol_rate
= state
->currentsymbolrate
;
979 static int cx24123_set_tone(struct dvb_frontend
*fe
, fe_sec_tone_mode_t tone
)
981 struct cx24123_state
*state
= fe
->demodulator_priv
;
984 /* wait for diseqc queue ready */
985 cx24123_wait_for_diseqc(state
);
987 val
= cx24123_readreg(state
, 0x29) & ~0x40;
991 dprintk("setting tone on\n");
992 return cx24123_writereg(state
, 0x29, val
| 0x10);
994 dprintk("setting tone off\n");
995 return cx24123_writereg(state
, 0x29, val
& 0xef);
997 err("CASE reached default with tone=%d\n", tone
);
1004 static int cx24123_tune(struct dvb_frontend
*fe
,
1005 struct dvb_frontend_parameters
*params
,
1006 unsigned int mode_flags
,
1007 unsigned int *delay
,
1008 fe_status_t
*status
)
1013 retval
= cx24123_set_frontend(fe
, params
);
1015 if (!(mode_flags
& FE_TUNE_MODE_ONESHOT
))
1016 cx24123_read_status(fe
, status
);
1022 static int cx24123_get_algo(struct dvb_frontend
*fe
)
1024 return 1; /* FE_ALGO_HW */
1027 static void cx24123_release(struct dvb_frontend
*fe
)
1029 struct cx24123_state
*state
= fe
->demodulator_priv
;
1031 i2c_del_adapter(&state
->tuner_i2c_adapter
);
1035 static int cx24123_tuner_i2c_tuner_xfer(struct i2c_adapter
*i2c_adap
,
1036 struct i2c_msg msg
[], int num
)
1038 struct cx24123_state
*state
= i2c_get_adapdata(i2c_adap
);
1039 /* this repeater closes after the first stop */
1040 cx24123_repeater_mode(state
, 1, 1);
1041 return i2c_transfer(state
->i2c
, msg
, num
);
1044 static u32
cx24123_tuner_i2c_func(struct i2c_adapter
*adapter
)
1046 return I2C_FUNC_I2C
;
1049 static struct i2c_algorithm cx24123_tuner_i2c_algo
= {
1050 .master_xfer
= cx24123_tuner_i2c_tuner_xfer
,
1051 .functionality
= cx24123_tuner_i2c_func
,
1054 struct i2c_adapter
*
1055 cx24123_get_tuner_i2c_adapter(struct dvb_frontend
*fe
)
1057 struct cx24123_state
*state
= fe
->demodulator_priv
;
1058 return &state
->tuner_i2c_adapter
;
1060 EXPORT_SYMBOL(cx24123_get_tuner_i2c_adapter
);
1062 static struct dvb_frontend_ops cx24123_ops
;
1064 struct dvb_frontend
*cx24123_attach(const struct cx24123_config
*config
,
1065 struct i2c_adapter
*i2c
)
1067 /* allocate memory for the internal state */
1068 struct cx24123_state
*state
=
1069 kzalloc(sizeof(struct cx24123_state
), GFP_KERNEL
);
1072 if (state
== NULL
) {
1073 err("Unable to kzalloc\n");
1077 /* setup the state */
1078 state
->config
= config
;
1081 /* check if the demod is there */
1082 state
->demod_rev
= cx24123_readreg(state
, 0x00);
1083 switch (state
->demod_rev
) {
1085 info("detected CX24123C\n");
1088 info("detected CX24123\n");
1091 err("wrong demod revision: %x\n", state
->demod_rev
);
1095 /* create dvb_frontend */
1096 memcpy(&state
->frontend
.ops
, &cx24123_ops
,
1097 sizeof(struct dvb_frontend_ops
));
1098 state
->frontend
.demodulator_priv
= state
;
1100 /* create tuner i2c adapter */
1101 if (config
->dont_use_pll
)
1102 cx24123_repeater_mode(state
, 1, 0);
1104 strlcpy(state
->tuner_i2c_adapter
.name
, "CX24123 tuner I2C bus",
1105 sizeof(state
->tuner_i2c_adapter
.name
));
1106 state
->tuner_i2c_adapter
.class = I2C_CLASS_TV_DIGITAL
,
1107 state
->tuner_i2c_adapter
.algo
= &cx24123_tuner_i2c_algo
;
1108 state
->tuner_i2c_adapter
.algo_data
= NULL
;
1109 i2c_set_adapdata(&state
->tuner_i2c_adapter
, state
);
1110 if (i2c_add_adapter(&state
->tuner_i2c_adapter
) < 0) {
1111 err("tuner i2c bus could not be initialized\n");
1115 return &state
->frontend
;
1122 EXPORT_SYMBOL(cx24123_attach
);
1124 static struct dvb_frontend_ops cx24123_ops
= {
1127 .name
= "Conexant CX24123/CX24109",
1129 .frequency_min
= 950000,
1130 .frequency_max
= 2150000,
1131 .frequency_stepsize
= 1011, /* kHz for QPSK frontends */
1132 .frequency_tolerance
= 5000,
1133 .symbol_rate_min
= 1000000,
1134 .symbol_rate_max
= 45000000,
1135 .caps
= FE_CAN_INVERSION_AUTO
|
1136 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
1137 FE_CAN_FEC_4_5
| FE_CAN_FEC_5_6
| FE_CAN_FEC_6_7
|
1138 FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
1139 FE_CAN_QPSK
| FE_CAN_RECOVER
1142 .release
= cx24123_release
,
1144 .init
= cx24123_initfe
,
1145 .set_frontend
= cx24123_set_frontend
,
1146 .get_frontend
= cx24123_get_frontend
,
1147 .read_status
= cx24123_read_status
,
1148 .read_ber
= cx24123_read_ber
,
1149 .read_signal_strength
= cx24123_read_signal_strength
,
1150 .read_snr
= cx24123_read_snr
,
1151 .diseqc_send_master_cmd
= cx24123_send_diseqc_msg
,
1152 .diseqc_send_burst
= cx24123_diseqc_send_burst
,
1153 .set_tone
= cx24123_set_tone
,
1154 .set_voltage
= cx24123_set_voltage
,
1155 .tune
= cx24123_tune
,
1156 .get_frontend_algo
= cx24123_get_algo
,
1159 MODULE_DESCRIPTION("DVB Frontend module for Conexant " \
1160 "CX24123/CX24109/CX24113 hardware");
1161 MODULE_AUTHOR("Steven Toth");
1162 MODULE_LICENSE("GPL");