[CPUFREQ] Misc cleanups in ondemand.
[linux-2.6/next.git] / drivers / media / dvb / frontends / s5h1420.c
blob2c2c344c4c648a3c85c92f3c7e6f8f4a98e85594
1 /*
2 Driver for Samsung S5H1420 QPSK Demodulator
4 Copyright (C) 2005 Andrew de Quincey <adq_dvb@lidskialf.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
29 #include <linux/jiffies.h>
30 #include <asm/div64.h>
32 #include "dvb_frontend.h"
33 #include "s5h1420.h"
37 #define TONE_FREQ 22000
39 struct s5h1420_state {
40 struct i2c_adapter* i2c;
41 const struct s5h1420_config* config;
42 struct dvb_frontend frontend;
44 u8 postlocked:1;
45 u32 fclk;
46 u32 tunedfreq;
47 fe_code_rate_t fec_inner;
48 u32 symbol_rate;
51 static u32 s5h1420_getsymbolrate(struct s5h1420_state* state);
52 static int s5h1420_get_tune_settings(struct dvb_frontend* fe,
53 struct dvb_frontend_tune_settings* fesettings);
56 static int debug = 0;
57 #define dprintk if (debug) printk
59 static int s5h1420_writereg (struct s5h1420_state* state, u8 reg, u8 data)
61 u8 buf [] = { reg, data };
62 struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };
63 int err;
65 if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
66 dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __FUNCTION__, err, reg, data);
67 return -EREMOTEIO;
70 return 0;
73 static u8 s5h1420_readreg (struct s5h1420_state* state, u8 reg)
75 int ret;
76 u8 b0 [] = { reg };
77 u8 b1 [] = { 0 };
78 struct i2c_msg msg1 = { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 };
79 struct i2c_msg msg2 = { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 };
81 if ((ret = i2c_transfer (state->i2c, &msg1, 1)) != 1)
82 return ret;
84 if ((ret = i2c_transfer (state->i2c, &msg2, 1)) != 1)
85 return ret;
87 return b1[0];
90 static int s5h1420_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
92 struct s5h1420_state* state = fe->demodulator_priv;
94 switch(voltage) {
95 case SEC_VOLTAGE_13:
96 s5h1420_writereg(state, 0x3c,
97 (s5h1420_readreg(state, 0x3c) & 0xfe) | 0x02);
98 break;
100 case SEC_VOLTAGE_18:
101 s5h1420_writereg(state, 0x3c, s5h1420_readreg(state, 0x3c) | 0x03);
102 break;
104 case SEC_VOLTAGE_OFF:
105 s5h1420_writereg(state, 0x3c, s5h1420_readreg(state, 0x3c) & 0xfd);
106 break;
109 return 0;
112 static int s5h1420_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
114 struct s5h1420_state* state = fe->demodulator_priv;
116 switch(tone) {
117 case SEC_TONE_ON:
118 s5h1420_writereg(state, 0x3b,
119 (s5h1420_readreg(state, 0x3b) & 0x74) | 0x08);
120 break;
122 case SEC_TONE_OFF:
123 s5h1420_writereg(state, 0x3b,
124 (s5h1420_readreg(state, 0x3b) & 0x74) | 0x01);
125 break;
128 return 0;
131 static int s5h1420_send_master_cmd (struct dvb_frontend* fe,
132 struct dvb_diseqc_master_cmd* cmd)
134 struct s5h1420_state* state = fe->demodulator_priv;
135 u8 val;
136 int i;
137 unsigned long timeout;
138 int result = 0;
140 if (cmd->msg_len > 8)
141 return -EINVAL;
143 /* setup for DISEQC */
144 val = s5h1420_readreg(state, 0x3b);
145 s5h1420_writereg(state, 0x3b, 0x02);
146 msleep(15);
148 /* write the DISEQC command bytes */
149 for(i=0; i< cmd->msg_len; i++) {
150 s5h1420_writereg(state, 0x3d + i, cmd->msg[i]);
153 /* kick off transmission */
154 s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) |
155 ((cmd->msg_len-1) << 4) | 0x08);
157 /* wait for transmission to complete */
158 timeout = jiffies + ((100*HZ) / 1000);
159 while(time_before(jiffies, timeout)) {
160 if (!(s5h1420_readreg(state, 0x3b) & 0x08))
161 break;
163 msleep(5);
165 if (time_after(jiffies, timeout))
166 result = -ETIMEDOUT;
168 /* restore original settings */
169 s5h1420_writereg(state, 0x3b, val);
170 msleep(15);
171 return result;
174 static int s5h1420_recv_slave_reply (struct dvb_frontend* fe,
175 struct dvb_diseqc_slave_reply* reply)
177 struct s5h1420_state* state = fe->demodulator_priv;
178 u8 val;
179 int i;
180 int length;
181 unsigned long timeout;
182 int result = 0;
184 /* setup for DISEQC recieve */
185 val = s5h1420_readreg(state, 0x3b);
186 s5h1420_writereg(state, 0x3b, 0x82); /* FIXME: guess - do we need to set DIS_RDY(0x08) in receive mode? */
187 msleep(15);
189 /* wait for reception to complete */
190 timeout = jiffies + ((reply->timeout*HZ) / 1000);
191 while(time_before(jiffies, timeout)) {
192 if (!(s5h1420_readreg(state, 0x3b) & 0x80)) /* FIXME: do we test DIS_RDY(0x08) or RCV_EN(0x80)? */
193 break;
195 msleep(5);
197 if (time_after(jiffies, timeout)) {
198 result = -ETIMEDOUT;
199 goto exit;
202 /* check error flag - FIXME: not sure what this does - docs do not describe
203 * beyond "error flag for diseqc receive data :( */
204 if (s5h1420_readreg(state, 0x49)) {
205 result = -EIO;
206 goto exit;
209 /* check length */
210 length = (s5h1420_readreg(state, 0x3b) & 0x70) >> 4;
211 if (length > sizeof(reply->msg)) {
212 result = -EOVERFLOW;
213 goto exit;
215 reply->msg_len = length;
217 /* extract data */
218 for(i=0; i< length; i++) {
219 reply->msg[i] = s5h1420_readreg(state, 0x3d + i);
222 exit:
223 /* restore original settings */
224 s5h1420_writereg(state, 0x3b, val);
225 msleep(15);
226 return result;
229 static int s5h1420_send_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t minicmd)
231 struct s5h1420_state* state = fe->demodulator_priv;
232 u8 val;
233 int result = 0;
234 unsigned long timeout;
236 /* setup for tone burst */
237 val = s5h1420_readreg(state, 0x3b);
238 s5h1420_writereg(state, 0x3b, (s5h1420_readreg(state, 0x3b) & 0x70) | 0x01);
240 /* set value for B position if requested */
241 if (minicmd == SEC_MINI_B) {
242 s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) | 0x04);
244 msleep(15);
246 /* start transmission */
247 s5h1420_writereg(state, 0x3b, s5h1420_readreg(state, 0x3b) | 0x08);
249 /* wait for transmission to complete */
250 timeout = jiffies + ((100*HZ) / 1000);
251 while(time_before(jiffies, timeout)) {
252 if (!(s5h1420_readreg(state, 0x3b) & 0x08))
253 break;
255 msleep(5);
257 if (time_after(jiffies, timeout))
258 result = -ETIMEDOUT;
260 /* restore original settings */
261 s5h1420_writereg(state, 0x3b, val);
262 msleep(15);
263 return result;
266 static fe_status_t s5h1420_get_status_bits(struct s5h1420_state* state)
268 u8 val;
269 fe_status_t status = 0;
271 val = s5h1420_readreg(state, 0x14);
272 if (val & 0x02)
273 status |= FE_HAS_SIGNAL;
274 if (val & 0x01)
275 status |= FE_HAS_CARRIER;
276 val = s5h1420_readreg(state, 0x36);
277 if (val & 0x01)
278 status |= FE_HAS_VITERBI;
279 if (val & 0x20)
280 status |= FE_HAS_SYNC;
281 if (status == (FE_HAS_SIGNAL|FE_HAS_CARRIER|FE_HAS_VITERBI|FE_HAS_SYNC))
282 status |= FE_HAS_LOCK;
284 return status;
287 static int s5h1420_read_status(struct dvb_frontend* fe, fe_status_t* status)
289 struct s5h1420_state* state = fe->demodulator_priv;
290 u8 val;
292 if (status == NULL)
293 return -EINVAL;
295 /* determine lock state */
296 *status = s5h1420_get_status_bits(state);
298 /* fix for FEC 5/6 inversion issue - if it doesn't quite lock, invert
299 the inversion, wait a bit and check again */
300 if (*status == (FE_HAS_SIGNAL|FE_HAS_CARRIER|FE_HAS_VITERBI)) {
301 val = s5h1420_readreg(state, 0x32);
302 if ((val & 0x07) == 0x03) {
303 if (val & 0x08)
304 s5h1420_writereg(state, 0x31, 0x13);
305 else
306 s5h1420_writereg(state, 0x31, 0x1b);
308 /* wait a bit then update lock status */
309 mdelay(200);
310 *status = s5h1420_get_status_bits(state);
314 /* perform post lock setup */
315 if ((*status & FE_HAS_LOCK) && (!state->postlocked)) {
317 /* calculate the data rate */
318 u32 tmp = s5h1420_getsymbolrate(state);
319 switch(s5h1420_readreg(state, 0x32) & 0x07) {
320 case 0:
321 tmp = (tmp * 2 * 1) / 2;
322 break;
324 case 1:
325 tmp = (tmp * 2 * 2) / 3;
326 break;
328 case 2:
329 tmp = (tmp * 2 * 3) / 4;
330 break;
332 case 3:
333 tmp = (tmp * 2 * 5) / 6;
334 break;
336 case 4:
337 tmp = (tmp * 2 * 6) / 7;
338 break;
340 case 5:
341 tmp = (tmp * 2 * 7) / 8;
342 break;
344 if (tmp == 0) {
345 printk("s5h1420: avoided division by 0\n");
346 tmp = 1;
348 tmp = state->fclk / tmp;
350 /* set the MPEG_CLK_INTL for the calculated data rate */
351 if (tmp < 4)
352 val = 0x00;
353 else if (tmp < 8)
354 val = 0x01;
355 else if (tmp < 12)
356 val = 0x02;
357 else if (tmp < 16)
358 val = 0x03;
359 else if (tmp < 24)
360 val = 0x04;
361 else if (tmp < 32)
362 val = 0x05;
363 else
364 val = 0x06;
365 s5h1420_writereg(state, 0x22, val);
367 /* DC freeze */
368 s5h1420_writereg(state, 0x1f, s5h1420_readreg(state, 0x1f) | 0x01);
370 /* kicker disable + remove DC offset */
371 s5h1420_writereg(state, 0x05, s5h1420_readreg(state, 0x05) & 0x6f);
373 /* post-lock processing has been done! */
374 state->postlocked = 1;
377 return 0;
380 static int s5h1420_read_ber(struct dvb_frontend* fe, u32* ber)
382 struct s5h1420_state* state = fe->demodulator_priv;
384 s5h1420_writereg(state, 0x46, 0x1d);
385 mdelay(25);
387 *ber = (s5h1420_readreg(state, 0x48) << 8) | s5h1420_readreg(state, 0x47);
389 return 0;
392 static int s5h1420_read_signal_strength(struct dvb_frontend* fe, u16* strength)
394 struct s5h1420_state* state = fe->demodulator_priv;
396 u8 val = s5h1420_readreg(state, 0x15);
398 *strength = (u16) ((val << 8) | val);
400 return 0;
403 static int s5h1420_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
405 struct s5h1420_state* state = fe->demodulator_priv;
407 s5h1420_writereg(state, 0x46, 0x1f);
408 mdelay(25);
410 *ucblocks = (s5h1420_readreg(state, 0x48) << 8) | s5h1420_readreg(state, 0x47);
412 return 0;
415 static void s5h1420_reset(struct s5h1420_state* state)
417 s5h1420_writereg (state, 0x01, 0x08);
418 s5h1420_writereg (state, 0x01, 0x00);
419 udelay(10);
422 static void s5h1420_setsymbolrate(struct s5h1420_state* state,
423 struct dvb_frontend_parameters *p)
425 u64 val;
427 val = ((u64) p->u.qpsk.symbol_rate / 1000ULL) * (1ULL<<24);
428 if (p->u.qpsk.symbol_rate <= 21000000) {
429 val *= 2;
431 do_div(val, (state->fclk / 1000));
433 s5h1420_writereg(state, 0x09, s5h1420_readreg(state, 0x09) & 0x7f);
434 s5h1420_writereg(state, 0x11, val >> 16);
435 s5h1420_writereg(state, 0x12, val >> 8);
436 s5h1420_writereg(state, 0x13, val & 0xff);
437 s5h1420_writereg(state, 0x09, s5h1420_readreg(state, 0x09) | 0x80);
440 static u32 s5h1420_getsymbolrate(struct s5h1420_state* state)
442 u64 val = 0;
443 int sampling = 2;
445 if (s5h1420_readreg(state, 0x05) & 0x2)
446 sampling = 1;
448 s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) | 0x08);
449 val = s5h1420_readreg(state, 0x11) << 16;
450 val |= s5h1420_readreg(state, 0x12) << 8;
451 val |= s5h1420_readreg(state, 0x13);
452 s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) & 0xf7);
454 val *= (state->fclk / 1000ULL);
455 do_div(val, ((1<<24) * sampling));
457 return (u32) (val * 1000ULL);
460 static void s5h1420_setfreqoffset(struct s5h1420_state* state, int freqoffset)
462 int val;
464 /* remember freqoffset is in kHz, but the chip wants the offset in Hz, so
465 * divide fclk by 1000000 to get the correct value. */
466 val = -(int) ((freqoffset * (1<<24)) / (state->fclk / 1000000));
468 s5h1420_writereg(state, 0x09, s5h1420_readreg(state, 0x09) & 0xbf);
469 s5h1420_writereg(state, 0x0e, val >> 16);
470 s5h1420_writereg(state, 0x0f, val >> 8);
471 s5h1420_writereg(state, 0x10, val & 0xff);
472 s5h1420_writereg(state, 0x09, s5h1420_readreg(state, 0x09) | 0x40);
475 static int s5h1420_getfreqoffset(struct s5h1420_state* state)
477 int val;
479 s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) | 0x08);
480 val = s5h1420_readreg(state, 0x0e) << 16;
481 val |= s5h1420_readreg(state, 0x0f) << 8;
482 val |= s5h1420_readreg(state, 0x10);
483 s5h1420_writereg(state, 0x06, s5h1420_readreg(state, 0x06) & 0xf7);
485 if (val & 0x800000)
486 val |= 0xff000000;
488 /* remember freqoffset is in kHz, but the chip wants the offset in Hz, so
489 * divide fclk by 1000000 to get the correct value. */
490 val = (((-val) * (state->fclk/1000000)) / (1<<24));
492 return val;
495 static void s5h1420_setfec_inversion(struct s5h1420_state* state,
496 struct dvb_frontend_parameters *p)
498 u8 inversion = 0;
500 if (p->inversion == INVERSION_OFF) {
501 inversion = state->config->invert ? 0x08 : 0;
502 } else if (p->inversion == INVERSION_ON) {
503 inversion = state->config->invert ? 0 : 0x08;
506 if ((p->u.qpsk.fec_inner == FEC_AUTO) || (p->inversion == INVERSION_AUTO)) {
507 s5h1420_writereg(state, 0x30, 0x3f);
508 s5h1420_writereg(state, 0x31, 0x00 | inversion);
509 } else {
510 switch(p->u.qpsk.fec_inner) {
511 case FEC_1_2:
512 s5h1420_writereg(state, 0x30, 0x01);
513 s5h1420_writereg(state, 0x31, 0x10 | inversion);
514 break;
516 case FEC_2_3:
517 s5h1420_writereg(state, 0x30, 0x02);
518 s5h1420_writereg(state, 0x31, 0x11 | inversion);
519 break;
521 case FEC_3_4:
522 s5h1420_writereg(state, 0x30, 0x04);
523 s5h1420_writereg(state, 0x31, 0x12 | inversion);
524 break;
526 case FEC_5_6:
527 s5h1420_writereg(state, 0x30, 0x08);
528 s5h1420_writereg(state, 0x31, 0x13 | inversion);
529 break;
531 case FEC_6_7:
532 s5h1420_writereg(state, 0x30, 0x10);
533 s5h1420_writereg(state, 0x31, 0x14 | inversion);
534 break;
536 case FEC_7_8:
537 s5h1420_writereg(state, 0x30, 0x20);
538 s5h1420_writereg(state, 0x31, 0x15 | inversion);
539 break;
541 default:
542 return;
547 static fe_code_rate_t s5h1420_getfec(struct s5h1420_state* state)
549 switch(s5h1420_readreg(state, 0x32) & 0x07) {
550 case 0:
551 return FEC_1_2;
553 case 1:
554 return FEC_2_3;
556 case 2:
557 return FEC_3_4;
559 case 3:
560 return FEC_5_6;
562 case 4:
563 return FEC_6_7;
565 case 5:
566 return FEC_7_8;
569 return FEC_NONE;
572 static fe_spectral_inversion_t s5h1420_getinversion(struct s5h1420_state* state)
574 if (s5h1420_readreg(state, 0x32) & 0x08)
575 return INVERSION_ON;
577 return INVERSION_OFF;
580 static int s5h1420_set_frontend(struct dvb_frontend* fe,
581 struct dvb_frontend_parameters *p)
583 struct s5h1420_state* state = fe->demodulator_priv;
584 int frequency_delta;
585 struct dvb_frontend_tune_settings fesettings;
587 /* check if we should do a fast-tune */
588 memcpy(&fesettings.parameters, p, sizeof(struct dvb_frontend_parameters));
589 s5h1420_get_tune_settings(fe, &fesettings);
590 frequency_delta = p->frequency - state->tunedfreq;
591 if ((frequency_delta > -fesettings.max_drift) &&
592 (frequency_delta < fesettings.max_drift) &&
593 (frequency_delta != 0) &&
594 (state->fec_inner == p->u.qpsk.fec_inner) &&
595 (state->symbol_rate == p->u.qpsk.symbol_rate)) {
597 if (fe->ops.tuner_ops.set_params) {
598 fe->ops.tuner_ops.set_params(fe, p);
599 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
601 if (fe->ops.tuner_ops.get_frequency) {
602 u32 tmp;
603 fe->ops.tuner_ops.get_frequency(fe, &tmp);
604 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
605 s5h1420_setfreqoffset(state, p->frequency - tmp);
606 } else {
607 s5h1420_setfreqoffset(state, 0);
609 return 0;
612 /* first of all, software reset */
613 s5h1420_reset(state);
615 /* set s5h1420 fclk PLL according to desired symbol rate */
616 if (p->u.qpsk.symbol_rate > 28000000) {
617 state->fclk = 88000000;
618 s5h1420_writereg(state, 0x03, 0x50);
619 s5h1420_writereg(state, 0x04, 0x40);
620 s5h1420_writereg(state, 0x05, 0xae);
621 } else if (p->u.qpsk.symbol_rate > 21000000) {
622 state->fclk = 59000000;
623 s5h1420_writereg(state, 0x03, 0x33);
624 s5h1420_writereg(state, 0x04, 0x40);
625 s5h1420_writereg(state, 0x05, 0xae);
626 } else {
627 state->fclk = 88000000;
628 s5h1420_writereg(state, 0x03, 0x50);
629 s5h1420_writereg(state, 0x04, 0x40);
630 s5h1420_writereg(state, 0x05, 0xac);
633 /* set misc registers */
634 s5h1420_writereg(state, 0x02, 0x00);
635 s5h1420_writereg(state, 0x06, 0x00);
636 s5h1420_writereg(state, 0x07, 0xb0);
637 s5h1420_writereg(state, 0x0a, 0xe7);
638 s5h1420_writereg(state, 0x0b, 0x78);
639 s5h1420_writereg(state, 0x0c, 0x48);
640 s5h1420_writereg(state, 0x0d, 0x6b);
641 s5h1420_writereg(state, 0x2e, 0x8e);
642 s5h1420_writereg(state, 0x35, 0x33);
643 s5h1420_writereg(state, 0x38, 0x01);
644 s5h1420_writereg(state, 0x39, 0x7d);
645 s5h1420_writereg(state, 0x3a, (state->fclk + (TONE_FREQ * 32) - 1) / (TONE_FREQ * 32));
646 s5h1420_writereg(state, 0x3c, 0x00);
647 s5h1420_writereg(state, 0x45, 0x61);
648 s5h1420_writereg(state, 0x46, 0x1d);
650 /* start QPSK */
651 s5h1420_writereg(state, 0x05, s5h1420_readreg(state, 0x05) | 1);
653 /* set tuner PLL */
654 if (fe->ops.tuner_ops.set_params) {
655 fe->ops.tuner_ops.set_params(fe, p);
656 if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
657 s5h1420_setfreqoffset(state, 0);
660 /* set the reset of the parameters */
661 s5h1420_setsymbolrate(state, p);
662 s5h1420_setfec_inversion(state, p);
664 state->fec_inner = p->u.qpsk.fec_inner;
665 state->symbol_rate = p->u.qpsk.symbol_rate;
666 state->postlocked = 0;
667 state->tunedfreq = p->frequency;
668 return 0;
671 static int s5h1420_get_frontend(struct dvb_frontend* fe,
672 struct dvb_frontend_parameters *p)
674 struct s5h1420_state* state = fe->demodulator_priv;
676 p->frequency = state->tunedfreq + s5h1420_getfreqoffset(state);
677 p->inversion = s5h1420_getinversion(state);
678 p->u.qpsk.symbol_rate = s5h1420_getsymbolrate(state);
679 p->u.qpsk.fec_inner = s5h1420_getfec(state);
681 return 0;
684 static int s5h1420_get_tune_settings(struct dvb_frontend* fe,
685 struct dvb_frontend_tune_settings* fesettings)
687 if (fesettings->parameters.u.qpsk.symbol_rate > 20000000) {
688 fesettings->min_delay_ms = 50;
689 fesettings->step_size = 2000;
690 fesettings->max_drift = 8000;
691 } else if (fesettings->parameters.u.qpsk.symbol_rate > 12000000) {
692 fesettings->min_delay_ms = 100;
693 fesettings->step_size = 1500;
694 fesettings->max_drift = 9000;
695 } else if (fesettings->parameters.u.qpsk.symbol_rate > 8000000) {
696 fesettings->min_delay_ms = 100;
697 fesettings->step_size = 1000;
698 fesettings->max_drift = 8000;
699 } else if (fesettings->parameters.u.qpsk.symbol_rate > 4000000) {
700 fesettings->min_delay_ms = 100;
701 fesettings->step_size = 500;
702 fesettings->max_drift = 7000;
703 } else if (fesettings->parameters.u.qpsk.symbol_rate > 2000000) {
704 fesettings->min_delay_ms = 200;
705 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
706 fesettings->max_drift = 14 * fesettings->step_size;
707 } else {
708 fesettings->min_delay_ms = 200;
709 fesettings->step_size = (fesettings->parameters.u.qpsk.symbol_rate / 8000);
710 fesettings->max_drift = 18 * fesettings->step_size;
713 return 0;
716 static int s5h1420_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
718 struct s5h1420_state* state = fe->demodulator_priv;
720 if (enable) {
721 return s5h1420_writereg (state, 0x02, s5h1420_readreg(state,0x02) | 1);
722 } else {
723 return s5h1420_writereg (state, 0x02, s5h1420_readreg(state,0x02) & 0xfe);
727 static int s5h1420_init (struct dvb_frontend* fe)
729 struct s5h1420_state* state = fe->demodulator_priv;
731 /* disable power down and do reset */
732 s5h1420_writereg(state, 0x02, 0x10);
733 msleep(10);
734 s5h1420_reset(state);
736 return 0;
739 static int s5h1420_sleep(struct dvb_frontend* fe)
741 struct s5h1420_state* state = fe->demodulator_priv;
743 return s5h1420_writereg(state, 0x02, 0x12);
746 static void s5h1420_release(struct dvb_frontend* fe)
748 struct s5h1420_state* state = fe->demodulator_priv;
749 kfree(state);
752 static struct dvb_frontend_ops s5h1420_ops;
754 struct dvb_frontend* s5h1420_attach(const struct s5h1420_config* config,
755 struct i2c_adapter* i2c)
757 struct s5h1420_state* state = NULL;
758 u8 identity;
760 /* allocate memory for the internal state */
761 state = kmalloc(sizeof(struct s5h1420_state), GFP_KERNEL);
762 if (state == NULL)
763 goto error;
765 /* setup the state */
766 state->config = config;
767 state->i2c = i2c;
768 state->postlocked = 0;
769 state->fclk = 88000000;
770 state->tunedfreq = 0;
771 state->fec_inner = FEC_NONE;
772 state->symbol_rate = 0;
774 /* check if the demod is there + identify it */
775 identity = s5h1420_readreg(state, 0x00);
776 if (identity != 0x03)
777 goto error;
779 /* create dvb_frontend */
780 memcpy(&state->frontend.ops, &s5h1420_ops, sizeof(struct dvb_frontend_ops));
781 state->frontend.demodulator_priv = state;
782 return &state->frontend;
784 error:
785 kfree(state);
786 return NULL;
789 static struct dvb_frontend_ops s5h1420_ops = {
791 .info = {
792 .name = "Samsung S5H1420 DVB-S",
793 .type = FE_QPSK,
794 .frequency_min = 950000,
795 .frequency_max = 2150000,
796 .frequency_stepsize = 125, /* kHz for QPSK frontends */
797 .frequency_tolerance = 29500,
798 .symbol_rate_min = 1000000,
799 .symbol_rate_max = 45000000,
800 /* .symbol_rate_tolerance = ???,*/
801 .caps = FE_CAN_INVERSION_AUTO |
802 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
803 FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
804 FE_CAN_QPSK
807 .release = s5h1420_release,
809 .init = s5h1420_init,
810 .sleep = s5h1420_sleep,
811 .i2c_gate_ctrl = s5h1420_i2c_gate_ctrl,
813 .set_frontend = s5h1420_set_frontend,
814 .get_frontend = s5h1420_get_frontend,
815 .get_tune_settings = s5h1420_get_tune_settings,
817 .read_status = s5h1420_read_status,
818 .read_ber = s5h1420_read_ber,
819 .read_signal_strength = s5h1420_read_signal_strength,
820 .read_ucblocks = s5h1420_read_ucblocks,
822 .diseqc_send_master_cmd = s5h1420_send_master_cmd,
823 .diseqc_recv_slave_reply = s5h1420_recv_slave_reply,
824 .diseqc_send_burst = s5h1420_send_burst,
825 .set_tone = s5h1420_set_tone,
826 .set_voltage = s5h1420_set_voltage,
829 module_param(debug, int, 0644);
831 MODULE_DESCRIPTION("Samsung S5H1420 DVB-S Demodulator driver");
832 MODULE_AUTHOR("Andrew de Quincey");
833 MODULE_LICENSE("GPL");
835 EXPORT_SYMBOL(s5h1420_attach);