2 * Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
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>
29 #include <asm/div64.h>
31 #include "dvb_frontend.h"
36 static int force_band;
37 module_param(force_band, int, 0644);
38 MODULE_PARM_DESC(force_band, "Force a specific band select "\
39 "(1-9, default:off).");
42 module_param(debug, int, 0644);
43 MODULE_PARM_DESC(debug, "Activates frontend debugging (default:0)");
45 #define info(args...) do { printk(KERN_INFO "CX24123: " args); } while (0)
46 #define err(args...) do { printk(KERN_ERR "CX24123: " args); } while (0)
48 #define dprintk(args...) \
51 printk(KERN_DEBUG "CX24123: %s: ", __func__); \
56 struct cx24123_state {
57 struct i2c_adapter *i2c;
58 const struct cx24123_config *config;
60 struct dvb_frontend frontend;
62 /* Some PLL specifics for tuning */
69 struct i2c_adapter tuner_i2c_adapter;
73 /* The Demod/Tuner can't easily provide these, we cache them */
75 u32 currentsymbolrate;
78 /* Various tuner defaults need to be established for a given symbol rate Sps */
79 static struct cx24123_AGC_val {
85 } cx24123_AGC_vals[] =
88 .symbolrate_low = 1000000,
89 .symbolrate_high = 4999999,
90 /* the specs recommend other values for VGA offsets,
91 but tests show they are wrong */
92 .VGAprogdata = (1 << 19) | (0x180 << 9) | 0x1e0,
93 .VCAprogdata = (2 << 19) | (0x07 << 9) | 0x07,
94 .FILTune = 0x27f /* 0.41 V */
97 .symbolrate_low = 5000000,
98 .symbolrate_high = 14999999,
99 .VGAprogdata = (1 << 19) | (0x180 << 9) | 0x1e0,
100 .VCAprogdata = (2 << 19) | (0x07 << 9) | 0x1f,
101 .FILTune = 0x317 /* 0.90 V */
104 .symbolrate_low = 15000000,
105 .symbolrate_high = 45000000,
106 .VGAprogdata = (1 << 19) | (0x100 << 9) | 0x180,
107 .VCAprogdata = (2 << 19) | (0x07 << 9) | 0x3f,
108 .FILTune = 0x145 /* 2.70 V */
113 * Various tuner defaults need to be established for a given frequency kHz.
114 * fixme: The bounds on the bands do not match the doc in real life.
115 * fixme: Some of them have been moved, other might need adjustment.
117 static struct cx24123_bandselect_val {
122 } cx24123_bandselect_vals[] =
127 .freq_high = 1074999,
129 .progdata = (0 << 19) | (0 << 9) | 0x40,
135 .freq_high = 1177999,
137 .progdata = (0 << 19) | (0 << 9) | 0x80,
143 .freq_high = 1295999,
145 .progdata = (0 << 19) | (1 << 9) | 0x01,
151 .freq_high = 1431999,
153 .progdata = (0 << 19) | (1 << 9) | 0x02,
159 .freq_high = 1575999,
161 .progdata = (0 << 19) | (1 << 9) | 0x04,
167 .freq_high = 1717999,
169 .progdata = (0 << 19) | (1 << 9) | 0x08,
175 .freq_high = 1855999,
177 .progdata = (0 << 19) | (1 << 9) | 0x10,
183 .freq_high = 2035999,
185 .progdata = (0 << 19) | (1 << 9) | 0x20,
191 .freq_high = 2150000,
193 .progdata = (0 << 19) | (1 << 9) | 0x40,
200 } cx24123_regdata[] =
202 {0x00, 0x03}, /* Reset system */
203 {0x00, 0x00}, /* Clear reset */
204 {0x03, 0x07}, /* QPSK, DVB, Auto Acquisition (default) */
205 {0x04, 0x10}, /* MPEG */
206 {0x05, 0x04}, /* MPEG */
207 {0x06, 0x31}, /* MPEG (default) */
208 {0x0b, 0x00}, /* Freq search start point (default) */
209 {0x0c, 0x00}, /* Demodulator sample gain (default) */
210 {0x0d, 0x7f}, /* Force driver to shift until the maximum (+-10 MHz) */
211 {0x0e, 0x03}, /* Default non-inverted, FEC 3/4 (default) */
212 {0x0f, 0xfe}, /* FEC search mask (all supported codes) */
213 {0x10, 0x01}, /* Default search inversion, no repeat (default) */
214 {0x16, 0x00}, /* Enable reading of frequency */
215 {0x17, 0x01}, /* Enable EsNO Ready Counter */
216 {0x1c, 0x80}, /* Enable error counter */
217 {0x20, 0x00}, /* Tuner burst clock rate = 500KHz */
218 {0x21, 0x15}, /* Tuner burst mode, word length = 0x15 */
219 {0x28, 0x00}, /* Enable FILTERV with positive pol., DiSEqC 2.x off */
220 {0x29, 0x00}, /* DiSEqC LNB_DC off */
221 {0x2a, 0xb0}, /* DiSEqC Parameters (default) */
222 {0x2b, 0x73}, /* DiSEqC Tone Frequency (default) */
223 {0x2c, 0x00}, /* DiSEqC Message (0x2c - 0x31) */
229 {0x32, 0x8c}, /* DiSEqC Parameters (default) */
230 {0x33, 0x00}, /* Interrupts off (0x33 - 0x34) */
232 {0x35, 0x03}, /* DiSEqC Tone Amplitude (default) */
233 {0x36, 0x02}, /* DiSEqC Parameters (default) */
234 {0x37, 0x3a}, /* DiSEqC Parameters (default) */
235 {0x3a, 0x00}, /* Enable AGC accumulator (for signal strength) */
236 {0x44, 0x00}, /* Constellation (default) */
237 {0x45, 0x00}, /* Symbol count (default) */
238 {0x46, 0x0d}, /* Symbol rate estimator on (default) */
239 {0x56, 0xc1}, /* Error Counter = Viterbi BER */
240 {0x57, 0xff}, /* Error Counter Window (default) */
241 {0x5c, 0x20}, /* Acquisition AFC Expiration window (default is 0x10) */
242 {0x67, 0x83}, /* Non-DCII symbol clock */
245 static int cx24123_i2c_writereg(struct cx24123_state *state,
246 u8 i2c_addr, int reg, int data)
248 u8 buf[] = { reg, data };
249 struct i2c_msg msg = {
250 .addr = i2c_addr, .flags = 0, .buf = buf, .len = 2
254 /* printk(KERN_DEBUG "wr(%02x): %02x %02x\n", i2c_addr, reg, data); */
256 err = i2c_transfer(state->i2c, &msg, 1);
258 printk("%s: writereg error(err == %i, reg == 0x%02x,"
259 " data == 0x%02x)\n", __func__, err, reg, data);
266 static int cx24123_i2c_readreg(struct cx24123_state *state, u8 i2c_addr, u8 reg)
270 struct i2c_msg msg[] = {
271 { .addr = i2c_addr, .flags = 0, .buf = ®, .len = 1 },
272 { .addr = i2c_addr, .flags = I2C_M_RD, .buf = &b, .len = 1 }
275 ret = i2c_transfer(state->i2c, msg, 2);
278 err("%s: reg=0x%x (error=%d)\n", __func__, reg, ret);
282 /* printk(KERN_DEBUG "rd(%02x): %02x %02x\n", i2c_addr, reg, b); */
287 #define cx24123_readreg(state, reg) \
288 cx24123_i2c_readreg(state, state->config->demod_address, reg)
289 #define cx24123_writereg(state, reg, val) \
290 cx24123_i2c_writereg(state, state->config->demod_address, reg, val)
292 static int cx24123_set_inversion(struct cx24123_state *state,
293 fe_spectral_inversion_t inversion)
295 u8 nom_reg = cx24123_readreg(state, 0x0e);
296 u8 auto_reg = cx24123_readreg(state, 0x10);
300 dprintk("inversion off\n");
301 cx24123_writereg(state, 0x0e, nom_reg & ~0x80);
302 cx24123_writereg(state, 0x10, auto_reg | 0x80);
305 dprintk("inversion on\n");
306 cx24123_writereg(state, 0x0e, nom_reg | 0x80);
307 cx24123_writereg(state, 0x10, auto_reg | 0x80);
310 dprintk("inversion auto\n");
311 cx24123_writereg(state, 0x10, auto_reg & ~0x80);
320 static int cx24123_get_inversion(struct cx24123_state *state,
321 fe_spectral_inversion_t *inversion)
325 val = cx24123_readreg(state, 0x1b) >> 7;
328 dprintk("read inversion off\n");
329 *inversion = INVERSION_OFF;
331 dprintk("read inversion on\n");
332 *inversion = INVERSION_ON;
338 static int cx24123_set_fec(struct cx24123_state *state, fe_code_rate_t fec)
340 u8 nom_reg = cx24123_readreg(state, 0x0e) & ~0x07;
342 if (((int)fec < FEC_NONE) || (fec > FEC_AUTO))
345 /* Set the soft decision threshold */
347 cx24123_writereg(state, 0x43,
348 cx24123_readreg(state, 0x43) | 0x01);
350 cx24123_writereg(state, 0x43,
351 cx24123_readreg(state, 0x43) & ~0x01);
355 dprintk("set FEC to 1/2\n");
356 cx24123_writereg(state, 0x0e, nom_reg | 0x01);
357 cx24123_writereg(state, 0x0f, 0x02);
360 dprintk("set FEC to 2/3\n");
361 cx24123_writereg(state, 0x0e, nom_reg | 0x02);
362 cx24123_writereg(state, 0x0f, 0x04);
365 dprintk("set FEC to 3/4\n");
366 cx24123_writereg(state, 0x0e, nom_reg | 0x03);
367 cx24123_writereg(state, 0x0f, 0x08);
370 dprintk("set FEC to 4/5\n");
371 cx24123_writereg(state, 0x0e, nom_reg | 0x04);
372 cx24123_writereg(state, 0x0f, 0x10);
375 dprintk("set FEC to 5/6\n");
376 cx24123_writereg(state, 0x0e, nom_reg | 0x05);
377 cx24123_writereg(state, 0x0f, 0x20);
380 dprintk("set FEC to 6/7\n");
381 cx24123_writereg(state, 0x0e, nom_reg | 0x06);
382 cx24123_writereg(state, 0x0f, 0x40);
385 dprintk("set FEC to 7/8\n");
386 cx24123_writereg(state, 0x0e, nom_reg | 0x07);
387 cx24123_writereg(state, 0x0f, 0x80);
390 dprintk("set FEC to auto\n");
391 cx24123_writereg(state, 0x0f, 0xfe);
400 static int cx24123_get_fec(struct cx24123_state *state, fe_code_rate_t *fec)
404 ret = cx24123_readreg(state, 0x1b);
432 /* this can happen when there's no lock */
439 /* Approximation of closest integer of log2(a/b). It actually gives the
440 lowest integer i such that 2^i >= round(a/b) */
441 static u32 cx24123_int_log2(u32 a, u32 b)
443 u32 exp, nearest = 0;
447 if (div < (1 << 31)) {
448 for (exp = 1; div > exp; nearest++)
454 static int cx24123_set_symbolrate(struct cx24123_state *state, u32 srate)
457 u32 sample_rate, ratio, sample_gain;
460 /* check if symbol rate is within limits */
461 if ((srate > state->frontend.ops.info.symbol_rate_max) ||
462 (srate < state->frontend.ops.info.symbol_rate_min))
465 /* choose the sampling rate high enough for the required operation,
466 while optimizing the power consumed by the demodulator */
467 if (srate < (XTAL*2)/2)
469 else if (srate < (XTAL*3)/2)
471 else if (srate < (XTAL*4)/2)
473 else if (srate < (XTAL*5)/2)
475 else if (srate < (XTAL*6)/2)
477 else if (srate < (XTAL*7)/2)
479 else if (srate < (XTAL*8)/2)
485 sample_rate = pll_mult * XTAL;
487 /* SYSSymbolRate[21:0] = (srate << 23) / sample_rate */
489 tmp = ((u64)srate) << 23;
490 do_div(tmp, sample_rate);
493 cx24123_writereg(state, 0x01, pll_mult * 6);
495 cx24123_writereg(state, 0x08, (ratio >> 16) & 0x3f);
496 cx24123_writereg(state, 0x09, (ratio >> 8) & 0xff);
497 cx24123_writereg(state, 0x0a, ratio & 0xff);
499 /* also set the demodulator sample gain */
500 sample_gain = cx24123_int_log2(sample_rate, srate);
501 tmp = cx24123_readreg(state, 0x0c) & ~0xe0;
502 cx24123_writereg(state, 0x0c, tmp | sample_gain << 5);
504 dprintk("srate=%d, ratio=0x%08x, sample_rate=%i sample_gain=%d\n",
505 srate, ratio, sample_rate, sample_gain);
511 * Based on the required frequency and symbolrate, the tuner AGC has
512 * to be configured and the correct band selected.
513 * Calculate those values.
515 static int cx24123_pll_calculate(struct dvb_frontend *fe)
517 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
518 struct cx24123_state *state = fe->demodulator_priv;
519 u32 ndiv = 0, adiv = 0, vco_div = 0;
523 int num_bands = ARRAY_SIZE(cx24123_bandselect_vals);
524 struct cx24123_bandselect_val *bsv = NULL;
525 struct cx24123_AGC_val *agcv = NULL;
527 /* Defaults for low freq, low rate */
528 state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
529 state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
530 state->bandselectarg = cx24123_bandselect_vals[0].progdata;
531 vco_div = cx24123_bandselect_vals[0].VCOdivider;
533 /* For the given symbol rate, determine the VCA, VGA and
534 * FILTUNE programming bits */
535 for (i = 0; i < ARRAY_SIZE(cx24123_AGC_vals); i++) {
536 agcv = &cx24123_AGC_vals[i];
537 if ((agcv->symbolrate_low <= p->symbol_rate) &&
538 (agcv->symbolrate_high >= p->symbol_rate)) {
539 state->VCAarg = agcv->VCAprogdata;
540 state->VGAarg = agcv->VGAprogdata;
541 state->FILTune = agcv->FILTune;
545 /* determine the band to use */
546 if (force_band < 1 || force_band > num_bands) {
547 for (i = 0; i < num_bands; i++) {
548 bsv = &cx24123_bandselect_vals[i];
549 if ((bsv->freq_low <= p->frequency) &&
550 (bsv->freq_high >= p->frequency))
554 band = force_band - 1;
556 state->bandselectarg = cx24123_bandselect_vals[band].progdata;
557 vco_div = cx24123_bandselect_vals[band].VCOdivider;
559 /* determine the charge pump current */
560 if (p->frequency < (cx24123_bandselect_vals[band].freq_low +
561 cx24123_bandselect_vals[band].freq_high) / 2)
566 /* Determine the N/A dividers for the requested lband freq (in kHz). */
567 /* Note: the reference divider R=10, frequency is in KHz,
569 ndiv = (((p->frequency * vco_div * 10) /
570 (2 * XTAL / 1000)) / 32) & 0x1ff;
571 adiv = (((p->frequency * vco_div * 10) /
572 (2 * XTAL / 1000)) % 32) & 0x1f;
574 if (adiv == 0 && ndiv > 0)
577 /* control bits 11, refdiv 11, charge pump polarity 1,
578 * charge pump current, ndiv, adiv */
579 state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) |
580 (pump << 14) | (ndiv << 5) | adiv;
586 * Tuner data is 21 bits long, must be left-aligned in data.
587 * Tuner cx24109 is written through a dedicated 3wire interface
590 static int cx24123_pll_writereg(struct dvb_frontend *fe, u32 data)
592 struct cx24123_state *state = fe->demodulator_priv;
593 unsigned long timeout;
595 dprintk("pll writereg called, data=0x%08x\n", data);
597 /* align the 21 bytes into to bit23 boundary */
600 /* Reset the demod pll word length to 0x15 bits */
601 cx24123_writereg(state, 0x21, 0x15);
603 /* write the msb 8 bits, wait for the send to be completed */
604 timeout = jiffies + msecs_to_jiffies(40);
605 cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
606 while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
607 if (time_after(jiffies, timeout)) {
608 err("%s: demodulator is not responding, "\
609 "possibly hung, aborting.\n", __func__);
615 /* send another 8 bytes, wait for the send to be completed */
616 timeout = jiffies + msecs_to_jiffies(40);
617 cx24123_writereg(state, 0x22, (data >> 8) & 0xff);
618 while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
619 if (time_after(jiffies, timeout)) {
620 err("%s: demodulator is not responding, "\
621 "possibly hung, aborting.\n", __func__);
627 /* send the lower 5 bits of this byte, padded with 3 LBB,
628 * wait for the send to be completed */
629 timeout = jiffies + msecs_to_jiffies(40);
630 cx24123_writereg(state, 0x22, (data) & 0xff);
631 while ((cx24123_readreg(state, 0x20) & 0x80)) {
632 if (time_after(jiffies, timeout)) {
633 err("%s: demodulator is not responding," \
634 "possibly hung, aborting.\n", __func__);
640 /* Trigger the demod to configure the tuner */
641 cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
642 cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
647 static int cx24123_pll_tune(struct dvb_frontend *fe)
649 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
650 struct cx24123_state *state = fe->demodulator_priv;
653 dprintk("frequency=%i\n", p->frequency);
655 if (cx24123_pll_calculate(fe) != 0) {
656 err("%s: cx24123_pll_calcutate failed\n", __func__);
660 /* Write the new VCO/VGA */
661 cx24123_pll_writereg(fe, state->VCAarg);
662 cx24123_pll_writereg(fe, state->VGAarg);
664 /* Write the new bandselect and pll args */
665 cx24123_pll_writereg(fe, state->bandselectarg);
666 cx24123_pll_writereg(fe, state->pllarg);
668 /* set the FILTUNE voltage */
669 val = cx24123_readreg(state, 0x28) & ~0x3;
670 cx24123_writereg(state, 0x27, state->FILTune >> 2);
671 cx24123_writereg(state, 0x28, val | (state->FILTune & 0x3));
673 dprintk("pll tune VCA=%d, band=%d, pll=%d\n", state->VCAarg,
674 state->bandselectarg, state->pllarg);
682 * [7:7] = BTI enabled
683 * [6:6] = I2C repeater enabled
684 * [5:5] = I2C repeater start
688 /* mode == 1 -> i2c-repeater, 0 -> bti */
689 static int cx24123_repeater_mode(struct cx24123_state *state, u8 mode, u8 start)
691 u8 r = cx24123_readreg(state, 0x23) & 0x1e;
693 r |= (1 << 6) | (start << 5);
695 r |= (1 << 7) | (start);
696 return cx24123_writereg(state, 0x23, r);
699 static int cx24123_initfe(struct dvb_frontend *fe)
701 struct cx24123_state *state = fe->demodulator_priv;
704 dprintk("init frontend\n");
706 /* Configure the demod to a good set of defaults */
707 for (i = 0; i < ARRAY_SIZE(cx24123_regdata); i++)
708 cx24123_writereg(state, cx24123_regdata[i].reg,
709 cx24123_regdata[i].data);
711 /* Set the LNB polarity */
712 if (state->config->lnb_polarity)
713 cx24123_writereg(state, 0x32,
714 cx24123_readreg(state, 0x32) | 0x02);
716 if (state->config->dont_use_pll)
717 cx24123_repeater_mode(state, 1, 0);
722 static int cx24123_set_voltage(struct dvb_frontend *fe,
723 fe_sec_voltage_t voltage)
725 struct cx24123_state *state = fe->demodulator_priv;
728 val = cx24123_readreg(state, 0x29) & ~0x40;
732 dprintk("setting voltage 13V\n");
733 return cx24123_writereg(state, 0x29, val & 0x7f);
735 dprintk("setting voltage 18V\n");
736 return cx24123_writereg(state, 0x29, val | 0x80);
737 case SEC_VOLTAGE_OFF:
738 /* already handled in cx88-dvb */
747 /* wait for diseqc queue to become ready (or timeout) */
748 static void cx24123_wait_for_diseqc(struct cx24123_state *state)
750 unsigned long timeout = jiffies + msecs_to_jiffies(200);
751 while (!(cx24123_readreg(state, 0x29) & 0x40)) {
752 if (time_after(jiffies, timeout)) {
753 err("%s: diseqc queue not ready, " \
754 "command may be lost.\n", __func__);
761 static int cx24123_send_diseqc_msg(struct dvb_frontend *fe,
762 struct dvb_diseqc_master_cmd *cmd)
764 struct cx24123_state *state = fe->demodulator_priv;
769 /* stop continuous tone if enabled */
770 tone = cx24123_readreg(state, 0x29);
772 cx24123_writereg(state, 0x29, tone & ~0x50);
774 /* wait for diseqc queue ready */
775 cx24123_wait_for_diseqc(state);
777 /* select tone mode */
778 cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
780 for (i = 0; i < cmd->msg_len; i++)
781 cx24123_writereg(state, 0x2C + i, cmd->msg[i]);
783 val = cx24123_readreg(state, 0x29);
784 cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40) |
785 ((cmd->msg_len-3) & 3));
787 /* wait for diseqc message to finish sending */
788 cx24123_wait_for_diseqc(state);
790 /* restart continuous tone if enabled */
792 cx24123_writereg(state, 0x29, tone & ~0x40);
797 static int cx24123_diseqc_send_burst(struct dvb_frontend *fe,
798 fe_sec_mini_cmd_t burst)
800 struct cx24123_state *state = fe->demodulator_priv;
805 /* stop continuous tone if enabled */
806 tone = cx24123_readreg(state, 0x29);
808 cx24123_writereg(state, 0x29, tone & ~0x50);
810 /* wait for diseqc queue ready */
811 cx24123_wait_for_diseqc(state);
813 /* select tone mode */
814 cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) | 0x4);
816 val = cx24123_readreg(state, 0x29);
817 if (burst == SEC_MINI_A)
818 cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x00));
819 else if (burst == SEC_MINI_B)
820 cx24123_writereg(state, 0x29, ((val & 0x90) | 0x40 | 0x08));
824 cx24123_wait_for_diseqc(state);
825 cx24123_writereg(state, 0x2a, cx24123_readreg(state, 0x2a) & 0xfb);
827 /* restart continuous tone if enabled */
829 cx24123_writereg(state, 0x29, tone & ~0x40);
834 static int cx24123_read_status(struct dvb_frontend *fe, fe_status_t *status)
836 struct cx24123_state *state = fe->demodulator_priv;
837 int sync = cx24123_readreg(state, 0x14);
840 if (state->config->dont_use_pll) {
842 if (fe->ops.tuner_ops.get_status)
843 fe->ops.tuner_ops.get_status(fe, &tun_status);
844 if (tun_status & TUNER_STATUS_LOCKED)
845 *status |= FE_HAS_SIGNAL;
847 int lock = cx24123_readreg(state, 0x20);
849 *status |= FE_HAS_SIGNAL;
853 *status |= FE_HAS_CARRIER; /* Phase locked */
855 *status |= FE_HAS_VITERBI;
857 /* Reed-Solomon Status */
859 *status |= FE_HAS_SYNC;
861 *status |= FE_HAS_LOCK; /*Full Sync */
867 * Configured to return the measurement of errors in blocks,
868 * because no UCBLOCKS value is available, so this value doubles up
869 * to satisfy both measurements.
871 static int cx24123_read_ber(struct dvb_frontend *fe, u32 *ber)
873 struct cx24123_state *state = fe->demodulator_priv;
875 /* The true bit error rate is this value divided by
876 the window size (set as 256 * 255) */
877 *ber = ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
878 (cx24123_readreg(state, 0x1d) << 8 |
879 cx24123_readreg(state, 0x1e));
881 dprintk("BER = %d\n", *ber);
886 static int cx24123_read_signal_strength(struct dvb_frontend *fe,
887 u16 *signal_strength)
889 struct cx24123_state *state = fe->demodulator_priv;
891 /* larger = better */
892 *signal_strength = cx24123_readreg(state, 0x3b) << 8;
894 dprintk("Signal strength = %d\n", *signal_strength);
899 static int cx24123_read_snr(struct dvb_frontend *fe, u16 *snr)
901 struct cx24123_state *state = fe->demodulator_priv;
903 /* Inverted raw Es/N0 count, totally bogus but better than the
905 *snr = 65535 - (((u16)cx24123_readreg(state, 0x18) << 8) |
906 (u16)cx24123_readreg(state, 0x19));
908 dprintk("read S/N index = %d\n", *snr);
913 static int cx24123_set_frontend(struct dvb_frontend *fe)
915 struct cx24123_state *state = fe->demodulator_priv;
916 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
920 if (state->config->set_ts_params)
921 state->config->set_ts_params(fe, 0);
923 state->currentfreq = p->frequency;
924 state->currentsymbolrate = p->symbol_rate;
926 cx24123_set_inversion(state, p->inversion);
927 cx24123_set_fec(state, p->fec_inner);
928 cx24123_set_symbolrate(state, p->symbol_rate);
930 if (!state->config->dont_use_pll)
931 cx24123_pll_tune(fe);
932 else if (fe->ops.tuner_ops.set_params)
933 fe->ops.tuner_ops.set_params(fe);
935 err("it seems I don't have a tuner...");
937 /* Enable automatic acquisition and reset cycle */
938 cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
939 cx24123_writereg(state, 0x00, 0x10);
940 cx24123_writereg(state, 0x00, 0);
942 if (state->config->agc_callback)
943 state->config->agc_callback(fe);
948 static int cx24123_get_frontend(struct dvb_frontend *fe)
950 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
951 struct cx24123_state *state = fe->demodulator_priv;
955 if (cx24123_get_inversion(state, &p->inversion) != 0) {
956 err("%s: Failed to get inversion status\n", __func__);
959 if (cx24123_get_fec(state, &p->fec_inner) != 0) {
960 err("%s: Failed to get fec status\n", __func__);
963 p->frequency = state->currentfreq;
964 p->symbol_rate = state->currentsymbolrate;
969 static int cx24123_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
971 struct cx24123_state *state = fe->demodulator_priv;
974 /* wait for diseqc queue ready */
975 cx24123_wait_for_diseqc(state);
977 val = cx24123_readreg(state, 0x29) & ~0x40;
981 dprintk("setting tone on\n");
982 return cx24123_writereg(state, 0x29, val | 0x10);
984 dprintk("setting tone off\n");
985 return cx24123_writereg(state, 0x29, val & 0xef);
987 err("CASE reached default with tone=%d\n", tone);
994 static int cx24123_tune(struct dvb_frontend *fe,
996 unsigned int mode_flags,
1003 retval = cx24123_set_frontend(fe);
1005 if (!(mode_flags & FE_TUNE_MODE_ONESHOT))
1006 cx24123_read_status(fe, status);
1012 static int cx24123_get_algo(struct dvb_frontend *fe)
1014 return 1; /* FE_ALGO_HW */
1017 static void cx24123_release(struct dvb_frontend *fe)
1019 struct cx24123_state *state = fe->demodulator_priv;
1021 i2c_del_adapter(&state->tuner_i2c_adapter);
1025 static int cx24123_tuner_i2c_tuner_xfer(struct i2c_adapter *i2c_adap,
1026 struct i2c_msg msg[], int num)
1028 struct cx24123_state *state = i2c_get_adapdata(i2c_adap);
1029 /* this repeater closes after the first stop */
1030 cx24123_repeater_mode(state, 1, 1);
1031 return i2c_transfer(state->i2c, msg, num);
1034 static u32 cx24123_tuner_i2c_func(struct i2c_adapter *adapter)
1036 return I2C_FUNC_I2C;
1039 static struct i2c_algorithm cx24123_tuner_i2c_algo = {
1040 .master_xfer = cx24123_tuner_i2c_tuner_xfer,
1041 .functionality = cx24123_tuner_i2c_func,
1044 struct i2c_adapter *
1045 cx24123_get_tuner_i2c_adapter(struct dvb_frontend *fe)
1047 struct cx24123_state *state = fe->demodulator_priv;
1048 return &state->tuner_i2c_adapter;
1050 EXPORT_SYMBOL(cx24123_get_tuner_i2c_adapter);
1052 static struct dvb_frontend_ops cx24123_ops;
1054 struct dvb_frontend *cx24123_attach(const struct cx24123_config *config,
1055 struct i2c_adapter *i2c)
1057 /* allocate memory for the internal state */
1058 struct cx24123_state *state =
1059 kzalloc(sizeof(struct cx24123_state), GFP_KERNEL);
1062 if (state == NULL) {
1063 err("Unable to kzalloc\n");
1067 /* setup the state */
1068 state->config = config;
1071 /* check if the demod is there */
1072 state->demod_rev = cx24123_readreg(state, 0x00);
1073 switch (state->demod_rev) {
1075 info("detected CX24123C\n");
1078 info("detected CX24123\n");
1081 err("wrong demod revision: %x\n", state->demod_rev);
1085 /* create dvb_frontend */
1086 memcpy(&state->frontend.ops, &cx24123_ops,
1087 sizeof(struct dvb_frontend_ops));
1088 state->frontend.demodulator_priv = state;
1090 /* create tuner i2c adapter */
1091 if (config->dont_use_pll)
1092 cx24123_repeater_mode(state, 1, 0);
1094 strlcpy(state->tuner_i2c_adapter.name, "CX24123 tuner I2C bus",
1095 sizeof(state->tuner_i2c_adapter.name));
1096 state->tuner_i2c_adapter.algo = &cx24123_tuner_i2c_algo;
1097 state->tuner_i2c_adapter.algo_data = NULL;
1098 i2c_set_adapdata(&state->tuner_i2c_adapter, state);
1099 if (i2c_add_adapter(&state->tuner_i2c_adapter) < 0) {
1100 err("tuner i2c bus could not be initialized\n");
1104 return &state->frontend;
1111 EXPORT_SYMBOL(cx24123_attach);
1113 static struct dvb_frontend_ops cx24123_ops = {
1114 .delsys = { SYS_DVBS },
1116 .name = "Conexant CX24123/CX24109",
1117 .frequency_min = 950000,
1118 .frequency_max = 2150000,
1119 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
1120 .frequency_tolerance = 5000,
1121 .symbol_rate_min = 1000000,
1122 .symbol_rate_max = 45000000,
1123 .caps = FE_CAN_INVERSION_AUTO |
1124 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1125 FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
1126 FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1127 FE_CAN_QPSK | FE_CAN_RECOVER
1130 .release = cx24123_release,
1132 .init = cx24123_initfe,
1133 .set_frontend = cx24123_set_frontend,
1134 .get_frontend = cx24123_get_frontend,
1135 .read_status = cx24123_read_status,
1136 .read_ber = cx24123_read_ber,
1137 .read_signal_strength = cx24123_read_signal_strength,
1138 .read_snr = cx24123_read_snr,
1139 .diseqc_send_master_cmd = cx24123_send_diseqc_msg,
1140 .diseqc_send_burst = cx24123_diseqc_send_burst,
1141 .set_tone = cx24123_set_tone,
1142 .set_voltage = cx24123_set_voltage,
1143 .tune = cx24123_tune,
1144 .get_frontend_algo = cx24123_get_algo,
1147 MODULE_DESCRIPTION("DVB Frontend module for Conexant " \
1148 "CX24123/CX24109/CX24113 hardware");
1149 MODULE_AUTHOR("Steven Toth");
1150 MODULE_LICENSE("GPL");