2 Conexant cx24123/cx24109 - DVB QPSK Satellite demod/tuner driver
4 Copyright (C) 2005 Steven Toth <stoth@hauppauge.com>
6 Support for KWorld DVB-S 100 by Vadim Catana <skystar@moldova.cc>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/slab.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
29 #include "dvb_frontend.h"
33 #define dprintk(args...) \
35 if (debug) printk (KERN_DEBUG "cx24123: " args); \
40 struct i2c_adapter* i2c;
41 struct dvb_frontend_ops ops;
42 const struct cx24123_config* config;
44 struct dvb_frontend frontend;
50 /* Some PLL specifics for tuning */
56 /* The Demod/Tuner can't easily provide these, we cache them */
58 u32 currentsymbolrate;
61 /* Various tuner defaults need to be established for a given symbol rate Sps */
72 } cx24123_AGC_vals[] =
75 .symbolrate_low = 1000000,
76 .symbolrate_high = 4999999,
81 .VGAprogdata = (2 << 18) | (0x1f8 << 9) | 0x1f8,
82 .VCAprogdata = (4 << 18) | (0x07 << 9) | 0x07,
85 .symbolrate_low = 5000000,
86 .symbolrate_high = 14999999,
91 .VGAprogdata = (2 << 18) | (0x180 << 9) | 0x1e0,
92 .VCAprogdata = (4 << 18) | (0x07 << 9) | 0x1f,
95 .symbolrate_low = 15000000,
96 .symbolrate_high = 45000000,
101 .VGAprogdata = (2 << 18) | (0x100 << 9) | 0x180,
102 .VCAprogdata = (4 << 18) | (0x07 << 9) | 0x3f,
107 * Various tuner defaults need to be established for a given frequency kHz.
108 * fixme: The bounds on the bands do not match the doc in real life.
109 * fixme: Some of them have been moved, other might need adjustment.
119 } cx24123_bandselect_vals[] =
123 .freq_high = 1018999,
127 .progdata = (0 << 18) | (0 << 9) | 0x40,
131 .freq_high = 1074999,
135 .progdata = (0 << 18) | (0 << 9) | 0x80,
139 .freq_high = 1227999,
143 .progdata = (0 << 18) | (1 << 9) | 0x01,
147 .freq_high = 1349999,
151 .progdata = (0 << 18) | (1 << 9) | 0x02,
155 .freq_high = 1481999,
159 .progdata = (0 << 18) | (1 << 9) | 0x04,
163 .freq_high = 1595999,
167 .progdata = (0 << 18) | (1 << 9) | 0x08,
171 .freq_high = 1717999,
175 .progdata = (0 << 18) | (1 << 9) | 0x10,
179 .freq_high = 1855999,
183 .progdata = (0 << 18) | (1 << 9) | 0x20,
187 .freq_high = 2035999,
191 .progdata = (0 << 18) | (1 << 9) | 0x40,
195 .freq_high = 2149999,
199 .progdata = (0 << 18) | (1 << 9) | 0x80,
206 } cx24123_regdata[] =
208 {0x00, 0x03}, /* Reset system */
209 {0x00, 0x00}, /* Clear reset */
210 {0x01, 0x3b}, /* Apply sensible defaults, from an i2c sniffer */
246 {0x3a, 0x00}, /* Enable AGC accumulator */
255 static int cx24123_writereg(struct cx24123_state* state, int reg, int data)
257 u8 buf[] = { reg, data };
258 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
261 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
262 printk("%s: writereg error(err == %i, reg == 0x%02x,"
263 " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
270 static int cx24123_writelnbreg(struct cx24123_state* state, int reg, int data)
272 u8 buf[] = { reg, data };
273 /* fixme: put the intersil addr int the config */
274 struct i2c_msg msg = { .addr = 0x08, .flags = 0, .buf = buf, .len = 2 };
277 if ((err = i2c_transfer(state->i2c, &msg, 1)) != 1) {
278 printk("%s: writelnbreg error (err == %i, reg == 0x%02x,"
279 " data == 0x%02x)\n", __FUNCTION__, err, reg, data);
283 /* cache the write, no way to read back */
284 state->lnbreg = data;
289 static int cx24123_readreg(struct cx24123_state* state, u8 reg)
294 struct i2c_msg msg[] = {
295 { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
296 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 }
299 ret = i2c_transfer(state->i2c, msg, 2);
302 printk("%s: reg=0x%x (error=%d)\n", __FUNCTION__, reg, ret);
309 static int cx24123_readlnbreg(struct cx24123_state* state, u8 reg)
311 return state->lnbreg;
314 static int cx24123_set_inversion(struct cx24123_state* state, fe_spectral_inversion_t inversion)
318 cx24123_writereg(state, 0x0e, cx24123_readreg(state, 0x0e) & 0x7f);
319 cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) | 0x80);
322 cx24123_writereg(state, 0x0e, cx24123_readreg(state, 0x0e) | 0x80);
323 cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) | 0x80);
326 cx24123_writereg(state, 0x10, cx24123_readreg(state, 0x10) & 0x7f);
335 static int cx24123_get_inversion(struct cx24123_state* state, fe_spectral_inversion_t *inversion)
339 val = cx24123_readreg(state, 0x1b) >> 7;
342 *inversion = INVERSION_OFF;
344 *inversion = INVERSION_ON;
349 static int cx24123_set_fec(struct cx24123_state* state, fe_code_rate_t fec)
351 if ( (fec < FEC_NONE) || (fec > FEC_AUTO) )
354 /* Hardware has 5/11 and 3/5 but are never unused */
357 return cx24123_writereg(state, 0x0f, 0x01);
359 return cx24123_writereg(state, 0x0f, 0x02);
361 return cx24123_writereg(state, 0x0f, 0x04);
363 return cx24123_writereg(state, 0x0f, 0x08);
365 return cx24123_writereg(state, 0x0f, 0x20);
367 return cx24123_writereg(state, 0x0f, 0x80);
369 return cx24123_writereg(state, 0x0f, 0xae);
375 static int cx24123_get_fec(struct cx24123_state* state, fe_code_rate_t *fec)
380 ret = cx24123_readreg (state, 0x1b);
403 case 2: /* *fec = FEC_3_5; break; */
404 case 0: /* *fec = FEC_5_11; break; */
408 *fec = FEC_NONE; // can't happen
414 /* fixme: Symbol rates < 3MSps may not work because of precision loss */
415 static int cx24123_set_symbolrate(struct cx24123_state* state, u32 srate)
419 val = (srate / 1185) * 100;
421 /* Compensate for scaling up, by removing 17 symbols per 1Msps */
422 val = val - (17 * (srate / 1000000));
424 cx24123_writereg(state, 0x08, (val >> 16) & 0xff );
425 cx24123_writereg(state, 0x09, (val >> 8) & 0xff );
426 cx24123_writereg(state, 0x0a, (val ) & 0xff );
432 * Based on the required frequency and symbolrate, the tuner AGC has to be configured
433 * and the correct band selected. Calculate those values
435 static int cx24123_pll_calculate(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
437 struct cx24123_state *state = fe->demodulator_priv;
438 u32 ndiv = 0, adiv = 0, vco_div = 0;
441 /* Defaults for low freq, low rate */
442 state->VCAarg = cx24123_AGC_vals[0].VCAprogdata;
443 state->VGAarg = cx24123_AGC_vals[0].VGAprogdata;
444 state->bandselectarg = cx24123_bandselect_vals[0].progdata;
445 vco_div = cx24123_bandselect_vals[0].VCOdivider;
447 /* For the given symbolerate, determine the VCA and VGA programming bits */
448 for (i = 0; i < sizeof(cx24123_AGC_vals) / sizeof(cx24123_AGC_vals[0]); i++)
450 if ((cx24123_AGC_vals[i].symbolrate_low <= p->u.qpsk.symbol_rate) &&
451 (cx24123_AGC_vals[i].symbolrate_high >= p->u.qpsk.symbol_rate) ) {
452 state->VCAarg = cx24123_AGC_vals[i].VCAprogdata;
453 state->VGAarg = cx24123_AGC_vals[i].VGAprogdata;
457 /* For the given frequency, determine the bandselect programming bits */
458 for (i = 0; i < sizeof(cx24123_bandselect_vals) / sizeof(cx24123_bandselect_vals[0]); i++)
460 if ((cx24123_bandselect_vals[i].freq_low <= p->frequency) &&
461 (cx24123_bandselect_vals[i].freq_high >= p->frequency) ) {
462 state->bandselectarg = cx24123_bandselect_vals[i].progdata;
463 vco_div = cx24123_bandselect_vals[i].VCOdivider;
467 /* Determine the N/A dividers for the requested lband freq (in kHz). */
468 /* Note: 10111 (kHz) is the Crystal Freq and divider of 10. */
469 ndiv = ( ((p->frequency * vco_div) / (10111 / 10) / 2) / 32) & 0x1ff;
470 adiv = ( ((p->frequency * vco_div) / (10111 / 10) / 2) % 32) & 0x1f;
475 /* determine the correct pll frequency values. */
476 /* Command 11, refdiv 11, cpump polarity 1, cpump current 3mA 10. */
477 state->pllarg = (3 << 19) | (3 << 17) | (1 << 16) | (2 << 14);
478 state->pllarg |= (ndiv << 5) | adiv;
484 * Tuner data is 21 bits long, must be left-aligned in data.
485 * Tuner cx24109 is written through a dedicated 3wire interface on the demod chip.
487 static int cx24123_pll_writereg(struct dvb_frontend* fe, struct dvb_frontend_parameters *p, u32 data)
489 struct cx24123_state *state = fe->demodulator_priv;
490 unsigned long timeout;
492 /* align the 21 bytes into to bit23 boundary */
495 /* Reset the demod pll word length to 0x15 bits */
496 cx24123_writereg(state, 0x21, 0x15);
498 /* write the msb 8 bits, wait for the send to be completed */
499 timeout = jiffies + msecs_to_jiffies(40);
500 cx24123_writereg(state, 0x22, (data >> 16) & 0xff);
501 while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
502 if (time_after(jiffies, timeout)) {
503 printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
509 /* send another 8 bytes, wait for the send to be completed */
510 timeout = jiffies + msecs_to_jiffies(40);
511 cx24123_writereg(state, 0x22, (data>>8) & 0xff );
512 while ((cx24123_readreg(state, 0x20) & 0x40) == 0) {
513 if (time_after(jiffies, timeout)) {
514 printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
520 /* send the lower 5 bits of this byte, padded with 3 LBB, wait for the send to be completed */
521 timeout = jiffies + msecs_to_jiffies(40);
522 cx24123_writereg(state, 0x22, (data) & 0xff );
523 while ((cx24123_readreg(state, 0x20) & 0x80)) {
524 if (time_after(jiffies, timeout)) {
525 printk("%s: demodulator is not responding, possibly hung, aborting.\n", __FUNCTION__);
531 /* Trigger the demod to configure the tuner */
532 cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) | 2);
533 cx24123_writereg(state, 0x20, cx24123_readreg(state, 0x20) & 0xfd);
538 static int cx24123_pll_tune(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
540 struct cx24123_state *state = fe->demodulator_priv;
542 if (cx24123_pll_calculate(fe, p) != 0) {
543 printk("%s: cx24123_pll_calcutate failed\n",__FUNCTION__);
547 /* Write the new VCO/VGA */
548 cx24123_pll_writereg(fe, p, state->VCAarg);
549 cx24123_pll_writereg(fe, p, state->VGAarg);
551 /* Write the new bandselect and pll args */
552 cx24123_pll_writereg(fe, p, state->bandselectarg);
553 cx24123_pll_writereg(fe, p, state->pllarg);
558 static int cx24123_initfe(struct dvb_frontend* fe)
560 struct cx24123_state *state = fe->demodulator_priv;
563 /* Configure the demod to a good set of defaults */
564 for (i = 0; i < sizeof(cx24123_regdata) / sizeof(cx24123_regdata[0]); i++)
565 cx24123_writereg(state, cx24123_regdata[i].reg, cx24123_regdata[i].data);
567 if (state->config->pll_init)
568 state->config->pll_init(fe);
570 /* Configure the LNB for 14V */
571 if (state->config->use_isl6421)
572 cx24123_writelnbreg(state, 0x0, 0x2a);
577 static int cx24123_set_voltage(struct dvb_frontend* fe, fe_sec_voltage_t voltage)
579 struct cx24123_state *state = fe->demodulator_priv;
582 switch (state->config->use_isl6421) {
586 val = cx24123_readlnbreg(state, 0x0);
590 return cx24123_writelnbreg(state, 0x0, val & 0x32); /* V 13v */
592 return cx24123_writelnbreg(state, 0x0, val | 0x04); /* H 18v */
593 case SEC_VOLTAGE_OFF:
594 return cx24123_writelnbreg(state, 0x0, val & 0x30);
601 val = cx24123_readreg(state, 0x29);
605 dprintk("%s: setting voltage 13V\n", __FUNCTION__);
606 if (state->config->enable_lnb_voltage)
607 state->config->enable_lnb_voltage(fe, 1);
608 return cx24123_writereg(state, 0x29, val | 0x80);
610 dprintk("%s: setting voltage 18V\n", __FUNCTION__);
611 if (state->config->enable_lnb_voltage)
612 state->config->enable_lnb_voltage(fe, 1);
613 return cx24123_writereg(state, 0x29, val & 0x7f);
614 case SEC_VOLTAGE_OFF:
615 dprintk("%s: setting voltage off\n", __FUNCTION__);
616 if (state->config->enable_lnb_voltage)
617 state->config->enable_lnb_voltage(fe, 0);
627 static int cx24123_send_diseqc_msg(struct dvb_frontend* fe,
628 struct dvb_diseqc_master_cmd *cmd)
630 /* fixme: Implement diseqc */
631 printk("%s: No support yet\n",__FUNCTION__);
636 static int cx24123_read_status(struct dvb_frontend* fe, fe_status_t* status)
638 struct cx24123_state *state = fe->demodulator_priv;
640 int sync = cx24123_readreg(state, 0x14);
641 int lock = cx24123_readreg(state, 0x20);
645 *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
647 *status |= FE_HAS_VITERBI;
649 *status |= FE_HAS_CARRIER;
651 *status |= FE_HAS_SYNC | FE_HAS_LOCK;
657 * Configured to return the measurement of errors in blocks, because no UCBLOCKS value
658 * is available, so this value doubles up to satisfy both measurements
660 static int cx24123_read_ber(struct dvb_frontend* fe, u32* ber)
662 struct cx24123_state *state = fe->demodulator_priv;
665 ((cx24123_readreg(state, 0x1c) & 0x3f) << 16) |
666 (cx24123_readreg(state, 0x1d) << 8 |
667 cx24123_readreg(state, 0x1e));
669 /* Do the signal quality processing here, it's derived from the BER. */
670 /* Scale the BER from a 24bit to a SNR 16 bit where higher = better */
671 if (state->lastber < 5000)
672 state->snr = 655*100;
673 else if ( (state->lastber >= 5000) && (state->lastber < 55000) )
675 else if ( (state->lastber >= 55000) && (state->lastber < 150000) )
677 else if ( (state->lastber >= 150000) && (state->lastber < 250000) )
679 else if ( (state->lastber >= 250000) && (state->lastber < 450000) )
684 *ber = state->lastber;
689 static int cx24123_read_signal_strength(struct dvb_frontend* fe, u16* signal_strength)
691 struct cx24123_state *state = fe->demodulator_priv;
692 *signal_strength = cx24123_readreg(state, 0x3b) << 8; /* larger = better */
697 static int cx24123_read_snr(struct dvb_frontend* fe, u16* snr)
699 struct cx24123_state *state = fe->demodulator_priv;
705 static int cx24123_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
707 struct cx24123_state *state = fe->demodulator_priv;
708 *ucblocks = state->lastber;
713 static int cx24123_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
715 struct cx24123_state *state = fe->demodulator_priv;
717 if (state->config->set_ts_params)
718 state->config->set_ts_params(fe, 0);
720 state->currentfreq=p->frequency;
721 state->currentsymbolrate = p->u.qpsk.symbol_rate;
723 cx24123_set_inversion(state, p->inversion);
724 cx24123_set_fec(state, p->u.qpsk.fec_inner);
725 cx24123_set_symbolrate(state, p->u.qpsk.symbol_rate);
726 cx24123_pll_tune(fe, p);
728 /* Enable automatic aquisition and reset cycle */
729 cx24123_writereg(state, 0x03, (cx24123_readreg(state, 0x03) | 0x07));
730 cx24123_writereg(state, 0x00, 0x10);
731 cx24123_writereg(state, 0x00, 0);
736 static int cx24123_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
738 struct cx24123_state *state = fe->demodulator_priv;
740 if (cx24123_get_inversion(state, &p->inversion) != 0) {
741 printk("%s: Failed to get inversion status\n",__FUNCTION__);
744 if (cx24123_get_fec(state, &p->u.qpsk.fec_inner) != 0) {
745 printk("%s: Failed to get fec status\n",__FUNCTION__);
748 p->frequency = state->currentfreq;
749 p->u.qpsk.symbol_rate = state->currentsymbolrate;
754 static int cx24123_set_tone(struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
756 struct cx24123_state *state = fe->demodulator_priv;
759 switch (state->config->use_isl6421) {
762 val = cx24123_readlnbreg(state, 0x0);
766 return cx24123_writelnbreg(state, 0x0, val | 0x10);
768 return cx24123_writelnbreg(state, 0x0, val & 0x2f);
770 printk("%s: CASE reached default with tone=%d\n", __FUNCTION__, tone);
776 val = cx24123_readreg(state, 0x29);
780 dprintk("%s: setting tone on\n", __FUNCTION__);
781 return cx24123_writereg(state, 0x29, val | 0x10);
783 dprintk("%s: setting tone off\n",__FUNCTION__);
784 return cx24123_writereg(state, 0x29, val & 0xef);
786 printk("%s: CASE reached default with tone=%d\n", __FUNCTION__, tone);
794 static void cx24123_release(struct dvb_frontend* fe)
796 struct cx24123_state* state = fe->demodulator_priv;
797 dprintk("%s\n",__FUNCTION__);
801 static struct dvb_frontend_ops cx24123_ops;
803 struct dvb_frontend* cx24123_attach(const struct cx24123_config* config,
804 struct i2c_adapter* i2c)
806 struct cx24123_state* state = NULL;
809 dprintk("%s\n",__FUNCTION__);
811 /* allocate memory for the internal state */
812 state = kmalloc(sizeof(struct cx24123_state), GFP_KERNEL);
814 printk("Unable to kmalloc\n");
818 /* setup the state */
819 state->config = config;
821 memcpy(&state->ops, &cx24123_ops, sizeof(struct dvb_frontend_ops));
827 state->bandselectarg = 0;
829 state->currentfreq = 0;
830 state->currentsymbolrate = 0;
832 /* check if the demod is there */
833 ret = cx24123_readreg(state, 0x00);
834 if ((ret != 0xd1) && (ret != 0xe1)) {
835 printk("Version != d1 or e1\n");
839 /* create dvb_frontend */
840 state->frontend.ops = &state->ops;
841 state->frontend.demodulator_priv = state;
842 return &state->frontend;
850 static struct dvb_frontend_ops cx24123_ops = {
853 .name = "Conexant CX24123/CX24109",
855 .frequency_min = 950000,
856 .frequency_max = 2150000,
857 .frequency_stepsize = 1011, /* kHz for QPSK frontends */
858 .frequency_tolerance = 29500,
859 .symbol_rate_min = 1000000,
860 .symbol_rate_max = 45000000,
861 .caps = FE_CAN_INVERSION_AUTO |
862 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
863 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
864 FE_CAN_QPSK | FE_CAN_RECOVER
867 .release = cx24123_release,
869 .init = cx24123_initfe,
870 .set_frontend = cx24123_set_frontend,
871 .get_frontend = cx24123_get_frontend,
872 .read_status = cx24123_read_status,
873 .read_ber = cx24123_read_ber,
874 .read_signal_strength = cx24123_read_signal_strength,
875 .read_snr = cx24123_read_snr,
876 .read_ucblocks = cx24123_read_ucblocks,
877 .diseqc_send_master_cmd = cx24123_send_diseqc_msg,
878 .set_tone = cx24123_set_tone,
879 .set_voltage = cx24123_set_voltage,
882 module_param(debug, int, 0644);
883 MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
885 MODULE_DESCRIPTION("DVB Frontend module for Conexant cx24123/cx24109 hardware");
886 MODULE_AUTHOR("Steven Toth");
887 MODULE_LICENSE("GPL");
889 EXPORT_SYMBOL(cx24123_attach);