4 * PnpNetwork PN1010 QPSK Demodulator
6 * Copyright (C) 2005 Andrew de Quincey <adq_dvb@lidskialf.net>
7 * Copyright (C) 2005-8 Patrick Boettcher <pb@linuxtv.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU 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/kernel.h>
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/jiffies.h>
32 #include <asm/div64.h>
34 #include <linux/i2c.h>
37 #include "dvb_frontend.h"
39 #include "s5h1420_priv.h"
41 #define TONE_FREQ 22000
43 struct s5h1420_state {
44 struct i2c_adapter* i2c;
45 const struct s5h1420_config* config;
47 struct dvb_frontend frontend;
48 struct i2c_adapter tuner_i2c_adapter;
55 fe_code_rate_t fec_inner;
58 /* FIXME: ugly workaround for flexcop's incapable i2c-controller
59 * it does not support repeated-start, workaround: write addr-1
65 static u32 s5h1420_getsymbolrate(struct s5h1420_state* state);
66 static int s5h1420_get_tune_settings(struct dvb_frontend* fe,
67 struct dvb_frontend_tune_settings* fesettings);
71 module_param(debug, int, 0644);
72 MODULE_PARM_DESC(debug, "enable debugging");
74 #define dprintk(x...) do { \
76 printk(KERN_DEBUG "S5H1420: " x); \
79 static u8 s5h1420_readreg(struct s5h1420_state *state, u8 reg)
83 struct i2c_msg msg[] = {
84 { .addr = state->config->demod_address, .flags = 0, .buf = b, .len = 2 },
85 { .addr = state->config->demod_address, .flags = 0, .buf = ®, .len = 1 },
86 { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = 1 },
89 b[0] = (reg - 1) & 0xff;
90 b[1] = state->shadow[(reg - 1) & 0xff];
92 if (state->config->repeated_start_workaround) {
93 ret = i2c_transfer(state->i2c, msg, 3);
97 ret = i2c_transfer(state->i2c, &msg[1], 1);
100 ret = i2c_transfer(state->i2c, &msg[2], 1);
105 /* dprintk("rd(%02x): %02x %02x\n", state->config->demod_address, reg, b[0]); */
110 static int s5h1420_writereg (struct s5h1420_state* state, u8 reg, u8 data)
112 u8 buf[] = { reg, data };
113 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
116 /* dprintk("wr(%02x): %02x %02x\n", state->config->demod_address, reg, data); */
117 err = i2c_transfer(state->i2c, &msg, 1);
119 dprintk("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __func__, err, reg, data);
122 state->shadow[reg] = data;
127 static int s5h1420_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
129 struct s5h1420_state* state = fe->demodulator_priv;
131 dprintk("enter %s\n", __func__);
135 s5h1420_writereg(state, 0x3c,
136 (s5h1420_readreg(state, 0x3c) & 0xfe) | 0x02);
140 s5h1420_writereg(state, 0x3c, s5h1420_readreg(state, 0x3c) | 0x03);
143 case SEC_VOLTAGE_OFF:
144 s5h1420_writereg(state, 0x3c, s5h1420_readreg(state, 0x3c) & 0xfd);
148 dprintk("leave %s\n", __func__);
152 static int s5h1420_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
154 struct s5h1420_state* state = fe->demodulator_priv;
156 dprintk("enter %s\n", __func__);
159 s5h1420_writereg(state, 0x3b,
160 (s5h1420_readreg(state, 0x3b) & 0x74) | 0x08);
164 s5h1420_writereg(state, 0x3b,
165 (s5h1420_readreg(state, 0x3b) & 0x74) | 0x01);
168 dprintk("leave %s\n", __func__);
173 static int s5h1420_send_master_cmd (struct dvb_frontend* fe,
174 struct dvb_diseqc_master_cmd* cmd)
176 struct s5h1420_state* state = fe->demodulator_priv;
179 unsigned long timeout;
182 dprintk("enter %s\n", __func__);
183 if (cmd->msg_len > 8)
186 /* setup for DISEQC */
187 val = s5h1420_readreg(state, 0x3b);
188 s5h1420_writereg(state, 0x3b, 0x02);
191 /* write the DISEQC command bytes */
192 for(i=0; i< cmd->msg_len; i++) {
193 s5h1420_writereg(state, 0x3d + i, cmd->msg[i]);
196 /* kick off transmission */
197 s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) |
198 ((cmd->msg_len-1) << 4) | 0x08);
200 /* wait for transmission to complete */
201 timeout = jiffies + ((100*HZ) / 1000);
202 while(time_before(jiffies, timeout)) {
203 if (!(s5h1420_readreg(state, 0x3b) & 0x08))
208 if (time_after(jiffies, timeout))
211 /* restore original settings */
212 s5h1420_writereg(state, 0x3b, val);
214 dprintk("leave %s\n", __func__);
218 static int s5h1420_recv_slave_reply (struct dvb_frontend* fe,
219 struct dvb_diseqc_slave_reply* reply)
221 struct s5h1420_state* state = fe->demodulator_priv;
225 unsigned long timeout;
228 /* setup for DISEQC recieve */
229 val = s5h1420_readreg(state, 0x3b);
230 s5h1420_writereg(state, 0x3b, 0x82); /* FIXME: guess - do we need to set DIS_RDY(0x08) in receive mode? */
233 /* wait for reception to complete */
234 timeout = jiffies + ((reply->timeout*HZ) / 1000);
235 while(time_before(jiffies, timeout)) {
236 if (!(s5h1420_readreg(state, 0x3b) & 0x80)) /* FIXME: do we test DIS_RDY(0x08) or RCV_EN(0x80)? */
241 if (time_after(jiffies, timeout)) {
246 /* check error flag - FIXME: not sure what this does - docs do not describe
247 * beyond "error flag for diseqc receive data :( */
248 if (s5h1420_readreg(state, 0x49)) {
254 length = (s5h1420_readreg(state, 0x3b) & 0x70) >> 4;
255 if (length > sizeof(reply->msg)) {
259 reply->msg_len = length;
262 for(i=0; i< length; i++) {
263 reply->msg[i] = s5h1420_readreg(state, 0x3d + i);
267 /* restore original settings */
268 s5h1420_writereg(state, 0x3b, val);
273 static int s5h1420_send_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
275 struct s5h1420_state* state = fe->demodulator_priv;
278 unsigned long timeout;
280 /* setup for tone burst */
281 val = s5h1420_readreg(state, 0x3b);
282 s5h1420_writereg(state, 0x3b, (s5h1420_readreg(state, 0x3b) & 0x70) | 0x01);
284 /* set value for B position if requested */
285 if (minicmd == SEC_MINI_B) {
286 s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) | 0x04);
290 /* start transmission */
291 s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) | 0x08);
293 /* wait for transmission to complete */
294 timeout = jiffies + ((100*HZ) / 1000);
295 while(time_before(jiffies, timeout)) {
296 if (!(s5h1420_readreg(state, 0x3b) & 0x08))
301 if (time_after(jiffies, timeout))
304 /* restore original settings */
305 s5h1420_writereg(state, 0x3b, val);
310 static fe_status_t s5h1420_get_status_bits(struct s5h1420_state* state)
313 fe_status_t status = 0;
315 val = s5h1420_readreg(state, 0x14);
317 status |= FE_HAS_SIGNAL;
319 status |= FE_HAS_CARRIER;
320 val = s5h1420_readreg(state, 0x36);
322 status |= FE_HAS_VITERBI;
324 status |= FE_HAS_SYNC;
325 if (status == (FE_HAS_SIGNAL|FE_HAS_CARRIER|FE_HAS_VITERBI|FE_HAS_SYNC))
326 status |= FE_HAS_LOCK;
331 static int s5h1420_read_status(struct dvb_frontend* fe, fe_status_t* status)
333 struct s5h1420_state* state = fe->demodulator_priv;
336 dprintk("enter %s\n", __func__);
341 /* determine lock state */
342 *status = s5h1420_get_status_bits(state);
344 /* fix for FEC 5/6 inversion issue - if it doesn't quite lock, invert
345 the inversion, wait a bit and check again */
346 if (*status == (FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI)) {
347 val = s5h1420_readreg(state, Vit10);
348 if ((val & 0x07) == 0x03) {
350 s5h1420_writereg(state, Vit09, 0x13);
352 s5h1420_writereg(state, Vit09, 0x1b);
354 /* wait a bit then update lock status */
356 *status = s5h1420_get_status_bits(state);
360 /* perform post lock setup */
361 if ((*status & FE_HAS_LOCK) && !state->postlocked) {
363 /* calculate the data rate */
364 u32 tmp = s5h1420_getsymbolrate(state);
365 switch (s5h1420_readreg(state, Vit10) & 0x07) {
366 case 0: tmp = (tmp * 2 * 1) / 2; break;
367 case 1: tmp = (tmp * 2 * 2) / 3; break;
368 case 2: tmp = (tmp * 2 * 3) / 4; break;
369 case 3: tmp = (tmp * 2 * 5) / 6; break;
370 case 4: tmp = (tmp * 2 * 6) / 7; break;
371 case 5: tmp = (tmp * 2 * 7) / 8; break;
375 printk(KERN_ERR "s5h1420: avoided division by 0\n");
378 tmp = state->fclk / tmp;
381 /* set the MPEG_CLK_INTL for the calculated data rate */
398 dprintk("for MPEG_CLK_INTL %d %x\n", tmp, val);
400 s5h1420_writereg(state, FEC01, 0x18);
401 s5h1420_writereg(state, FEC01, 0x10);
402 s5h1420_writereg(state, FEC01, val);
404 /* Enable "MPEG_Out" */
405 val = s5h1420_readreg(state, Mpeg02);
406 s5h1420_writereg(state, Mpeg02, val | (1 << 6));
409 val = s5h1420_readreg(state, QPSK01) & 0x7f;
410 s5h1420_writereg(state, QPSK01, val);
412 /* DC freeze TODO it was never activated by default or it can stay activated */
414 if (s5h1420_getsymbolrate(state) >= 20000000) {
415 s5h1420_writereg(state, Loop04, 0x8a);
416 s5h1420_writereg(state, Loop05, 0x6a);
418 s5h1420_writereg(state, Loop04, 0x58);
419 s5h1420_writereg(state, Loop05, 0x27);
422 /* post-lock processing has been done! */
423 state->postlocked = 1;
426 dprintk("leave %s\n", __func__);
431 static int s5h1420_read_ber(struct dvb_frontend* fe, u32* ber)
433 struct s5h1420_state* state = fe->demodulator_priv;
435 s5h1420_writereg(state, 0x46, 0x1d);
438 *ber = (s5h1420_readreg(state, 0x48) << 8) | s5h1420_readreg(state, 0x47);
443 static int s5h1420_read_signal_strength(struct dvb_frontend* fe, u16* strength)
445 struct s5h1420_state* state = fe->demodulator_priv;
447 u8 val = s5h1420_readreg(state, 0x15);
449 *strength = (u16) ((val << 8) | val);
454 static int s5h1420_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
456 struct s5h1420_state* state = fe->demodulator_priv;
458 s5h1420_writereg(state, 0x46, 0x1f);
461 *ucblocks = (s5h1420_readreg(state, 0x48) << 8) | s5h1420_readreg(state, 0x47);
466 static void s5h1420_reset(struct s5h1420_state* state)
468 dprintk("%s\n", __func__);
469 s5h1420_writereg (state, 0x01, 0x08);
470 s5h1420_writereg (state, 0x01, 0x00);
474 static void s5h1420_setsymbolrate(struct s5h1420_state* state,
475 struct dvb_frontend_parameters *p)
480 dprintk("enter %s\n", __func__);
482 val = ((u64) p->u.qpsk.symbol_rate / 1000ULL) * (1ULL<<24);
483 if (p->u.qpsk.symbol_rate < 29000000)
485 do_div(val, (state->fclk / 1000));
487 dprintk("symbol rate register: %06llx\n", (unsigned long long)val);
489 v = s5h1420_readreg(state, Loop01);
490 s5h1420_writereg(state, Loop01, v & 0x7f);
491 s5h1420_writereg(state, Tnco01, val >> 16);
492 s5h1420_writereg(state, Tnco02, val >> 8);
493 s5h1420_writereg(state, Tnco03, val & 0xff);
494 s5h1420_writereg(state, Loop01, v | 0x80);
495 dprintk("leave %s\n", __func__);
498 static u32 s5h1420_getsymbolrate(struct s5h1420_state* state)
500 return state->symbol_rate;
503 static void s5h1420_setfreqoffset(struct s5h1420_state* state, int freqoffset)
508 dprintk("enter %s\n", __func__);
510 /* remember freqoffset is in kHz, but the chip wants the offset in Hz, so
511 * divide fclk by 1000000 to get the correct value. */
512 val = -(int) ((freqoffset * (1<<24)) / (state->fclk / 1000000));
514 dprintk("phase rotator/freqoffset: %d %06x\n", freqoffset, val);
516 v = s5h1420_readreg(state, Loop01);
517 s5h1420_writereg(state, Loop01, v & 0xbf);
518 s5h1420_writereg(state, Pnco01, val >> 16);
519 s5h1420_writereg(state, Pnco02, val >> 8);
520 s5h1420_writereg(state, Pnco03, val & 0xff);
521 s5h1420_writereg(state, Loop01, v | 0x40);
522 dprintk("leave %s\n", __func__);
525 static int s5h1420_getfreqoffset(struct s5h1420_state* state)
529 s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) | 0x08);
530 val = s5h1420_readreg(state, 0x0e) << 16;
531 val |= s5h1420_readreg(state, 0x0f) << 8;
532 val |= s5h1420_readreg(state, 0x10);
533 s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) & 0xf7);
538 /* remember freqoffset is in kHz, but the chip wants the offset in Hz, so
539 * divide fclk by 1000000 to get the correct value. */
540 val = (((-val) * (state->fclk/1000000)) / (1<<24));
545 static void s5h1420_setfec_inversion(struct s5h1420_state* state,
546 struct dvb_frontend_parameters *p)
551 dprintk("enter %s\n", __func__);
553 if (p->inversion == INVERSION_OFF)
554 inversion = state->config->invert ? 0x08 : 0;
555 else if (p->inversion == INVERSION_ON)
556 inversion = state->config->invert ? 0 : 0x08;
558 if ((p->u.qpsk.fec_inner == FEC_AUTO) || (p->inversion == INVERSION_AUTO)) {
562 switch(p->u.qpsk.fec_inner) {
564 vit08 = 0x01; vit09 = 0x10;
568 vit08 = 0x02; vit09 = 0x11;
572 vit08 = 0x04; vit09 = 0x12;
576 vit08 = 0x08; vit09 = 0x13;
580 vit08 = 0x10; vit09 = 0x14;
584 vit08 = 0x20; vit09 = 0x15;
592 dprintk("fec: %02x %02x\n", vit08, vit09);
593 s5h1420_writereg(state, Vit08, vit08);
594 s5h1420_writereg(state, Vit09, vit09);
595 dprintk("leave %s\n", __func__);
598 static fe_code_rate_t s5h1420_getfec(struct s5h1420_state* state)
600 switch(s5h1420_readreg(state, 0x32) & 0x07) {
623 static fe_spectral_inversion_t s5h1420_getinversion(struct s5h1420_state* state)
625 if (s5h1420_readreg(state, 0x32) & 0x08)
628 return INVERSION_OFF;
631 static int s5h1420_set_frontend(struct dvb_frontend* fe,
632 struct dvb_frontend_parameters *p)
634 struct s5h1420_state* state = fe->demodulator_priv;
636 struct dvb_frontend_tune_settings fesettings;
637 uint8_t clock_settting;
639 dprintk("enter %s\n", __func__);
641 /* check if we should do a fast-tune */
642 memcpy(&fesettings.parameters, p, sizeof(struct dvb_frontend_parameters));
643 s5h1420_get_tune_settings(fe, &fesettings);
644 frequency_delta = p->frequency - state->tunedfreq;
645 if ((frequency_delta > -fesettings.max_drift) &&
646 (frequency_delta < fesettings.max_drift) &&
647 (frequency_delta != 0) &&
648 (state->fec_inner == p->u.qpsk.fec_inner) &&
649 (state->symbol_rate == p->u.qpsk.symbol_rate)) {
651 if (fe->ops.tuner_ops.set_params) {
652 fe->ops.tuner_ops.set_params(fe, p);
653 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
655 if (fe->ops.tuner_ops.get_frequency) {
657 fe->ops.tuner_ops.get_frequency(fe, &tmp);
658 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
659 s5h1420_setfreqoffset(state, p->frequency - tmp);
661 s5h1420_setfreqoffset(state, 0);
663 dprintk("simple tune\n");
666 dprintk("tuning demod\n");
668 /* first of all, software reset */
669 s5h1420_reset(state);
671 /* set s5h1420 fclk PLL according to desired symbol rate */
672 if (p->u.qpsk.symbol_rate > 33000000)
673 state->fclk = 80000000;
674 else if (p->u.qpsk.symbol_rate > 28500000)
675 state->fclk = 59000000;
676 else if (p->u.qpsk.symbol_rate > 25000000)
677 state->fclk = 86000000;
678 else if (p->u.qpsk.symbol_rate > 1900000)
679 state->fclk = 88000000;
681 state->fclk = 44000000;
684 switch (state->fclk) {
702 dprintk("pll01: %d, ToneFreq: %d\n", state->fclk/1000000 - 8, (state->fclk + (TONE_FREQ * 32) - 1) / (TONE_FREQ * 32));
703 s5h1420_writereg(state, PLL01, state->fclk/1000000 - 8);
704 s5h1420_writereg(state, PLL02, 0x40);
705 s5h1420_writereg(state, DiS01, (state->fclk + (TONE_FREQ * 32) - 1) / (TONE_FREQ * 32));
707 /* TODO DC offset removal, config parameter ? */
708 if (p->u.qpsk.symbol_rate > 29000000)
709 s5h1420_writereg(state, QPSK01, 0xae | 0x10);
711 s5h1420_writereg(state, QPSK01, 0xac | 0x10);
713 /* set misc registers */
714 s5h1420_writereg(state, CON_1, 0x00);
715 s5h1420_writereg(state, QPSK02, 0x00);
716 s5h1420_writereg(state, Pre01, 0xb0);
718 s5h1420_writereg(state, Loop01, 0xF0);
719 s5h1420_writereg(state, Loop02, 0x2a); /* e7 for s5h1420 */
720 s5h1420_writereg(state, Loop03, 0x79); /* 78 for s5h1420 */
721 if (p->u.qpsk.symbol_rate > 20000000)
722 s5h1420_writereg(state, Loop04, 0x79);
724 s5h1420_writereg(state, Loop04, 0x58);
725 s5h1420_writereg(state, Loop05, 0x6b);
727 if (p->u.qpsk.symbol_rate >= 8000000)
728 s5h1420_writereg(state, Post01, (0 << 6) | 0x10);
729 else if (p->u.qpsk.symbol_rate >= 4000000)
730 s5h1420_writereg(state, Post01, (1 << 6) | 0x10);
732 s5h1420_writereg(state, Post01, (3 << 6) | 0x10);
734 s5h1420_writereg(state, Monitor12, 0x00); /* unfreeze DC compensation */
736 s5h1420_writereg(state, Sync01, 0x33);
737 s5h1420_writereg(state, Mpeg01, state->config->cdclk_polarity);
738 s5h1420_writereg(state, Mpeg02, 0x3d); /* Parallel output more, disabled -> enabled later */
739 s5h1420_writereg(state, Err01, 0x03); /* 0x1d for s5h1420 */
741 s5h1420_writereg(state, Vit06, 0x6e); /* 0x8e for s5h1420 */
742 s5h1420_writereg(state, DiS03, 0x00);
743 s5h1420_writereg(state, Rf01, 0x61); /* Tuner i2c address - for the gate controller */
746 if (fe->ops.tuner_ops.set_params) {
747 fe->ops.tuner_ops.set_params(fe, p);
748 if (fe->ops.i2c_gate_ctrl)
749 fe->ops.i2c_gate_ctrl(fe, 0);
750 s5h1420_setfreqoffset(state, 0);
753 /* set the reset of the parameters */
754 s5h1420_setsymbolrate(state, p);
755 s5h1420_setfec_inversion(state, p);
758 s5h1420_writereg(state, QPSK01, s5h1420_readreg(state, QPSK01) | 1);
760 state->fec_inner = p->u.qpsk.fec_inner;
761 state->symbol_rate = p->u.qpsk.symbol_rate;
762 state->postlocked = 0;
763 state->tunedfreq = p->frequency;
765 dprintk("leave %s\n", __func__);
769 static int s5h1420_get_frontend(struct dvb_frontend* fe,
770 struct dvb_frontend_parameters *p)
772 struct s5h1420_state* state = fe->demodulator_priv;
774 p->frequency = state->tunedfreq + s5h1420_getfreqoffset(state);
775 p->inversion = s5h1420_getinversion(state);
776 p->u.qpsk.symbol_rate = s5h1420_getsymbolrate(state);
777 p->u.qpsk.fec_inner = s5h1420_getfec(state);
782 static int s5h1420_get_tune_settings(struct dvb_frontend* fe,
783 struct dvb_frontend_tune_settings* fesettings)
785 if (fesettings->parameters.u.qpsk.symbol_rate > 20000000) {
786 fesettings->min_delay_ms = 50;
787 fesettings->step_size = 2000;
788 fesettings->max_drift = 8000;
789 } else if (fesettings->parameters.u.qpsk.symbol_rate > 12000000) {
790 fesettings->min_delay_ms = 100;
791 fesettings->step_size = 1500;
792 fesettings->max_drift = 9000;
793 } else if (fesettings->parameters.u.qpsk.symbol_rate > 8000000) {
794 fesettings->min_delay_ms = 100;
795 fesettings->step_size = 1000;
796 fesettings->max_drift = 8000;
797 } else if (fesettings->parameters.u.qpsk.symbol_rate > 4000000) {
798 fesettings->min_delay_ms = 100;
799 fesettings->step_size = 500;
800 fesettings->max_drift = 7000;
801 } else if (fesettings->parameters.u.qpsk.symbol_rate > 2000000) {
802 fesettings->min_delay_ms = 200;
803 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
804 fesettings->max_drift = 14 * fesettings->step_size;
806 fesettings->min_delay_ms = 200;
807 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
808 fesettings->max_drift = 18 * fesettings->step_size;
814 static int s5h1420_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
816 struct s5h1420_state* state = fe->demodulator_priv;
819 return s5h1420_writereg(state, 0x02, state->CON_1_val | 1);
821 return s5h1420_writereg(state, 0x02, state->CON_1_val & 0xfe);
824 static int s5h1420_init (struct dvb_frontend* fe)
826 struct s5h1420_state* state = fe->demodulator_priv;
828 /* disable power down and do reset */
829 state->CON_1_val = state->config->serial_mpeg << 4;
830 s5h1420_writereg(state, 0x02, state->CON_1_val);
832 s5h1420_reset(state);
837 static int s5h1420_sleep(struct dvb_frontend* fe)
839 struct s5h1420_state* state = fe->demodulator_priv;
840 state->CON_1_val = 0x12;
841 return s5h1420_writereg(state, 0x02, state->CON_1_val);
844 static void s5h1420_release(struct dvb_frontend* fe)
846 struct s5h1420_state* state = fe->demodulator_priv;
847 i2c_del_adapter(&state->tuner_i2c_adapter);
851 static u32 s5h1420_tuner_i2c_func(struct i2c_adapter *adapter)
856 static int s5h1420_tuner_i2c_tuner_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg msg[], int num)
858 struct s5h1420_state *state = i2c_get_adapdata(i2c_adap);
859 struct i2c_msg m[1 + num];
860 u8 tx_open[2] = { CON_1, state->CON_1_val | 1 }; /* repeater stops once there was a stop condition */
862 memset(m, 0, sizeof(struct i2c_msg) * (1 + num));
864 m[0].addr = state->config->demod_address;
868 memcpy(&m[1], msg, sizeof(struct i2c_msg) * num);
870 return i2c_transfer(state->i2c, m, 1+num) == 1 + num ? num : -EIO;
873 static struct i2c_algorithm s5h1420_tuner_i2c_algo = {
874 .master_xfer = s5h1420_tuner_i2c_tuner_xfer,
875 .functionality = s5h1420_tuner_i2c_func,
878 struct i2c_adapter *s5h1420_get_tuner_i2c_adapter(struct dvb_frontend *fe)
880 struct s5h1420_state *state = fe->demodulator_priv;
881 return &state->tuner_i2c_adapter;
883 EXPORT_SYMBOL(s5h1420_get_tuner_i2c_adapter);
885 static struct dvb_frontend_ops s5h1420_ops;
887 struct dvb_frontend *s5h1420_attach(const struct s5h1420_config *config,
888 struct i2c_adapter *i2c)
890 /* allocate memory for the internal state */
891 struct s5h1420_state *state = kzalloc(sizeof(struct s5h1420_state), GFP_KERNEL);
897 /* setup the state */
898 state->config = config;
900 state->postlocked = 0;
901 state->fclk = 88000000;
902 state->tunedfreq = 0;
903 state->fec_inner = FEC_NONE;
904 state->symbol_rate = 0;
906 /* check if the demod is there + identify it */
907 i = s5h1420_readreg(state, ID01);
911 memset(state->shadow, 0xff, sizeof(state->shadow));
913 for (i = 0; i < 0x50; i++)
914 state->shadow[i] = s5h1420_readreg(state, i);
916 /* create dvb_frontend */
917 memcpy(&state->frontend.ops, &s5h1420_ops, sizeof(struct dvb_frontend_ops));
918 state->frontend.demodulator_priv = state;
920 /* create tuner i2c adapter */
921 strlcpy(state->tuner_i2c_adapter.name, "S5H1420-PN1010 tuner I2C bus",
922 sizeof(state->tuner_i2c_adapter.name));
923 state->tuner_i2c_adapter.class = I2C_CLASS_TV_DIGITAL,
924 state->tuner_i2c_adapter.algo = &s5h1420_tuner_i2c_algo;
925 state->tuner_i2c_adapter.algo_data = NULL;
926 i2c_set_adapdata(&state->tuner_i2c_adapter, state);
927 if (i2c_add_adapter(&state->tuner_i2c_adapter) < 0) {
928 printk(KERN_ERR "S5H1420/PN1010: tuner i2c bus could not be initialized\n");
932 return &state->frontend;
938 EXPORT_SYMBOL(s5h1420_attach);
940 static struct dvb_frontend_ops s5h1420_ops = {
943 .name = "Samsung S5H1420/PnpNetwork PN1010 DVB-S",
945 .frequency_min = 950000,
946 .frequency_max = 2150000,
947 .frequency_stepsize = 125, /* kHz for QPSK frontends */
948 .frequency_tolerance = 29500,
949 .symbol_rate_min = 1000000,
950 .symbol_rate_max = 45000000,
951 /* .symbol_rate_tolerance = ???,*/
952 .caps = FE_CAN_INVERSION_AUTO |
953 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
954 FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
958 .release = s5h1420_release,
960 .init = s5h1420_init,
961 .sleep = s5h1420_sleep,
962 .i2c_gate_ctrl = s5h1420_i2c_gate_ctrl,
964 .set_frontend = s5h1420_set_frontend,
965 .get_frontend = s5h1420_get_frontend,
966 .get_tune_settings = s5h1420_get_tune_settings,
968 .read_status = s5h1420_read_status,
969 .read_ber = s5h1420_read_ber,
970 .read_signal_strength = s5h1420_read_signal_strength,
971 .read_ucblocks = s5h1420_read_ucblocks,
973 .diseqc_send_master_cmd = s5h1420_send_master_cmd,
974 .diseqc_recv_slave_reply = s5h1420_recv_slave_reply,
975 .diseqc_send_burst = s5h1420_send_burst,
976 .set_tone = s5h1420_set_tone,
977 .set_voltage = s5h1420_set_voltage,
980 MODULE_DESCRIPTION("Samsung S5H1420/PnpNetwork PN1010 DVB-S Demodulator driver");
981 MODULE_AUTHOR("Andrew de Quincey, Patrick Boettcher");
982 MODULE_LICENSE("GPL");