stv0299.c 18.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
    Driver for ST STV0299 demodulator

    Copyright (C) 2001-2002 Convergence Integrated Media GmbH
	<ralph@convergence.de>,
	<holger@convergence.de>,
	<js@convergence.de>


    Philips SU1278/SH

    Copyright (C) 2002 by Peter Schildmann <peter.schildmann@web.de>


    LG TDQF-S001F

    Copyright (C) 2002 Felix Domke <tmbinc@elitedvb.net>
		     & Andreas Oberritter <obi@linuxtv.org>


    Support for Samsung TBMU24112IMB used on Technisat SkyStar2 rev. 2.6B

    Copyright (C) 2003 Vadim Catana <skystar@moldova.cc>:

    Support for Philips SU1278 on Technotrend hardware

    Copyright (C) 2004 Andrew de Quincey <adq_dvb@lidskialf.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/string.h>
#include <linux/slab.h>
T
Tim Schmielau 已提交
51
#include <linux/jiffies.h>
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include <asm/div64.h>

#include "dvb_frontend.h"
#include "stv0299.h"

struct stv0299_state {
	struct i2c_adapter* i2c;
	struct dvb_frontend_ops ops;
	const struct stv0299_config* config;
	struct dvb_frontend frontend;

	u8 initialised:1;
	u32 tuner_frequency;
	u32 symbol_rate;
	fe_code_rate_t fec_inner;
67
	int errmode;
L
Linus Torvalds 已提交
68 69
};

70 71 72
#define STATUS_BER 0
#define STATUS_UCBLOCKS 1

L
Linus Torvalds 已提交
73
static int debug;
74
static int debug_legacy_dish_switch;
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#define dprintk(args...) \
	do { \
		if (debug) printk(KERN_DEBUG "stv0299: " args); \
	} while (0)


static int stv0299_writeregI (struct stv0299_state* state, u8 reg, u8 data)
{
	int ret;
	u8 buf [] = { reg, data };
	struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 2 };

	ret = i2c_transfer (state->i2c, &msg, 1);

	if (ret != 1)
		dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
			"ret == %i)\n", __FUNCTION__, reg, data, ret);

	return (ret != 1) ? -EREMOTEIO : 0;
}

int stv0299_writereg (struct dvb_frontend* fe, u8 reg, u8 data)
{
98
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	return stv0299_writeregI(state, reg, data);
}

static u8 stv0299_readreg (struct stv0299_state* state, u8 reg)
{
	int ret;
	u8 b0 [] = { reg };
	u8 b1 [] = { 0 };
	struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 1 },
			   { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 1 } };

	ret = i2c_transfer (state->i2c, msg, 2);

	if (ret != 2)
		dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
				__FUNCTION__, reg, ret);

	return b1[0];
}

static int stv0299_readregs (struct stv0299_state* state, u8 reg1, u8 *b, u8 len)
{
	int ret;
	struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = &reg1, .len = 1 },
			   { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b, .len = len } };

	ret = i2c_transfer (state->i2c, msg, 2);

	if (ret != 2)
		dprintk("%s: readreg error (ret == %i)\n", __FUNCTION__, ret);

	return ret == 2 ? 0 : ret;
}

134 135 136 137 138 139 140
int stv0299_enable_plli2c (struct dvb_frontend* fe)
{
	struct stv0299_state* state = fe->demodulator_priv;

	return stv0299_writeregI(state, 0x05, 0xb5);	/*  enable i2c repeater on stv0299  */
}

L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static int stv0299_set_FEC (struct stv0299_state* state, fe_code_rate_t fec)
{
	dprintk ("%s\n", __FUNCTION__);

	switch (fec) {
	case FEC_AUTO:
	{
		return stv0299_writeregI (state, 0x31, 0x1f);
	}
	case FEC_1_2:
	{
		return stv0299_writeregI (state, 0x31, 0x01);
	}
	case FEC_2_3:
	{
		return stv0299_writeregI (state, 0x31, 0x02);
	}
	case FEC_3_4:
	{
		return stv0299_writeregI (state, 0x31, 0x04);
	}
	case FEC_5_6:
	{
		return stv0299_writeregI (state, 0x31, 0x08);
	}
	case FEC_7_8:
	{
		return stv0299_writeregI (state, 0x31, 0x10);
	}
	default:
	{
		return -EINVAL;
	}
    }
}

static fe_code_rate_t stv0299_get_fec (struct stv0299_state* state)
{
	static fe_code_rate_t fec_tab [] = { FEC_2_3, FEC_3_4, FEC_5_6,
					     FEC_7_8, FEC_1_2 };
	u8 index;

	dprintk ("%s\n", __FUNCTION__);

	index = stv0299_readreg (state, 0x1b);
	index &= 0x7;

	if (index > 4)
		return FEC_AUTO;

	return fec_tab [index];
}

static int stv0299_wait_diseqc_fifo (struct stv0299_state* state, int timeout)
{
	unsigned long start = jiffies;

	dprintk ("%s\n", __FUNCTION__);

	while (stv0299_readreg(state, 0x0a) & 1) {
		if (jiffies - start > timeout) {
			dprintk ("%s: timeout!!\n", __FUNCTION__);
			return -ETIMEDOUT;
		}
		msleep(10);
	};

	return 0;
}

static int stv0299_wait_diseqc_idle (struct stv0299_state* state, int timeout)
{
	unsigned long start = jiffies;

	dprintk ("%s\n", __FUNCTION__);

	while ((stv0299_readreg(state, 0x0a) & 3) != 2 ) {
		if (jiffies - start > timeout) {
			dprintk ("%s: timeout!!\n", __FUNCTION__);
			return -ETIMEDOUT;
		}
		msleep(10);
	};

	return 0;
}

static int stv0299_set_symbolrate (struct dvb_frontend* fe, u32 srate)
{
230
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	u64 big = srate;
	u32 ratio;

	// check rate is within limits
	if ((srate < 1000000) || (srate > 45000000)) return -EINVAL;

	// calculate value to program
	big = big << 20;
	big += (state->config->mclk-1); // round correctly
	do_div(big, state->config->mclk);
	ratio = big << 4;

	return state->config->set_symbol_rate(fe, srate, ratio);
}

static int stv0299_get_symbolrate (struct stv0299_state* state)
{
	u32 Mclk = state->config->mclk / 4096L;
	u32 srate;
	s32 offset;
	u8 sfr[3];
	s8 rtf;

	dprintk ("%s\n", __FUNCTION__);

	stv0299_readregs (state, 0x1f, sfr, 3);
	stv0299_readregs (state, 0x1a, &rtf, 1);

	srate = (sfr[0] << 8) | sfr[1];
	srate *= Mclk;
	srate /= 16;
	srate += (sfr[2] >> 4) * Mclk / 256;
	offset = (s32) rtf * (srate / 4096L);
	offset /= 128;

	dprintk ("%s : srate = %i\n", __FUNCTION__, srate);
	dprintk ("%s : ofset = %i\n", __FUNCTION__, offset);

	srate += offset;

	srate += 1000;
	srate /= 2000;
	srate *= 2000;

	return srate;
}

static int stv0299_send_diseqc_msg (struct dvb_frontend* fe,
				    struct dvb_diseqc_master_cmd *m)
{
281
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
	u8 val;
	int i;

	dprintk ("%s\n", __FUNCTION__);

	if (stv0299_wait_diseqc_idle (state, 100) < 0)
		return -ETIMEDOUT;

	val = stv0299_readreg (state, 0x08);

	if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x6))  /* DiSEqC mode */
		return -EREMOTEIO;

	for (i=0; i<m->msg_len; i++) {
		if (stv0299_wait_diseqc_fifo (state, 100) < 0)
			return -ETIMEDOUT;

		if (stv0299_writeregI (state, 0x09, m->msg[i]))
			return -EREMOTEIO;
	}

	if (stv0299_wait_diseqc_idle (state, 100) < 0)
		return -ETIMEDOUT;

	return 0;
}

static int stv0299_send_diseqc_burst (struct dvb_frontend* fe, fe_sec_mini_cmd_t burst)
{
311
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
	u8 val;

	dprintk ("%s\n", __FUNCTION__);

	if (stv0299_wait_diseqc_idle (state, 100) < 0)
		return -ETIMEDOUT;

	val = stv0299_readreg (state, 0x08);

	if (stv0299_writeregI (state, 0x08, (val & ~0x7) | 0x2))	/* burst mode */
		return -EREMOTEIO;

	if (stv0299_writeregI (state, 0x09, burst == SEC_MINI_A ? 0x00 : 0xff))
		return -EREMOTEIO;

	if (stv0299_wait_diseqc_idle (state, 100) < 0)
		return -ETIMEDOUT;

	if (stv0299_writeregI (state, 0x08, val))
		return -EREMOTEIO;

	return 0;
}

static int stv0299_set_tone (struct dvb_frontend* fe, fe_sec_tone_mode_t tone)
{
338
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	u8 val;

	if (stv0299_wait_diseqc_idle (state, 100) < 0)
		return -ETIMEDOUT;

	val = stv0299_readreg (state, 0x08);

	switch (tone) {
	case SEC_TONE_ON:
		return stv0299_writeregI (state, 0x08, val | 0x3);

	case SEC_TONE_OFF:
		return stv0299_writeregI (state, 0x08, (val & ~0x3) | 0x02);

	default:
		return -EINVAL;
	}
}

static int stv0299_set_voltage (struct dvb_frontend* fe, fe_sec_voltage_t voltage)
{
360
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	u8 reg0x08;
	u8 reg0x0c;

	dprintk("%s: %s\n", __FUNCTION__,
		voltage == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
		voltage == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");

	reg0x08 = stv0299_readreg (state, 0x08);
	reg0x0c = stv0299_readreg (state, 0x0c);

	/**
	 *  H/V switching over OP0, OP1 and OP2 are LNB power enable bits
	 */
	reg0x0c &= 0x0f;

	if (voltage == SEC_VOLTAGE_OFF) {
		stv0299_writeregI (state, 0x0c, 0x00); /*	LNB power off! */
		return stv0299_writeregI (state, 0x08, 0x00); /*	LNB power off! */
	}

	stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));

	switch (voltage) {
	case SEC_VOLTAGE_13:
		if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0) reg0x0c |= 0x10;
		else reg0x0c |= 0x40;

		return stv0299_writeregI(state, 0x0c, reg0x0c);

	case SEC_VOLTAGE_18:
		return stv0299_writeregI(state, 0x0c, reg0x0c | 0x50);
	default:
		return -EINVAL;
	};
}

397
static int stv0299_send_legacy_dish_cmd (struct dvb_frontend* fe, unsigned long cmd)
398 399 400 401 402
{
	struct stv0299_state* state = fe->demodulator_priv;
	u8 reg0x08;
	u8 reg0x0c;
	u8 lv_mask = 0x40;
L
Linus Torvalds 已提交
403 404
	u8 last = 1;
	int i;
405 406
	struct timeval nexttime;
	struct timeval tv[10];
L
Linus Torvalds 已提交
407

408 409 410 411 412 413
	reg0x08 = stv0299_readreg (state, 0x08);
	reg0x0c = stv0299_readreg (state, 0x0c);
	reg0x0c &= 0x0f;
	stv0299_writeregI (state, 0x08, (reg0x08 & 0x3f) | (state->config->lock_output << 6));
	if (state->config->volt13_op0_op1 == STV0299_VOLT13_OP0)
		lv_mask = 0x10;
L
Linus Torvalds 已提交
414 415

	cmd = cmd << 1;
416
	if (debug_legacy_dish_switch)
417
		printk ("%s switch command: 0x%04lx\n",__FUNCTION__, cmd);
418 419 420 421 422

	do_gettimeofday (&nexttime);
	if (debug_legacy_dish_switch)
		memcpy (&tv[0], &nexttime, sizeof (struct timeval));
	stv0299_writeregI (state, 0x0c, reg0x0c | 0x50); /* set LNB to 18V */
L
Linus Torvalds 已提交
423

424
	dvb_frontend_sleep_until(&nexttime, 32000);
L
Linus Torvalds 已提交
425 426

	for (i=0; i<9; i++) {
427 428
		if (debug_legacy_dish_switch)
			do_gettimeofday (&tv[i+1]);
L
Linus Torvalds 已提交
429
		if((cmd & 0x01) != last) {
430 431
			/* set voltage to (last ? 13V : 18V) */
			stv0299_writeregI (state, 0x0c, reg0x0c | (last ? lv_mask : 0x50));
L
Linus Torvalds 已提交
432 433 434 435 436 437
			last = (last) ? 0 : 1;
		}

		cmd = cmd >> 1;

		if (i != 8)
438
			dvb_frontend_sleep_until(&nexttime, 8000);
439 440 441 442
	}
	if (debug_legacy_dish_switch) {
		printk ("%s(%d): switch delay (should be 32k followed by all 8k\n",
			__FUNCTION__, fe->dvb->num);
443 444
		for (i = 1; i < 10; i++)
			printk ("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
L
Linus Torvalds 已提交
445 446 447 448 449 450 451
	}

	return 0;
}

static int stv0299_init (struct dvb_frontend* fe)
{
452
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461
	int i;

	dprintk("stv0299: init chip\n");

	for (i=0; !(state->config->inittab[i] == 0xff && state->config->inittab[i+1] == 0xff); i+=2)
		stv0299_writeregI(state, state->config->inittab[i], state->config->inittab[i+1]);

	if (state->config->pll_init) {
		stv0299_writeregI(state, 0x05, 0xb5);	/*  enable i2c repeater on stv0299  */
462
		state->config->pll_init(fe, state->i2c);
L
Linus Torvalds 已提交
463 464 465 466 467 468 469 470
		stv0299_writeregI(state, 0x05, 0x35);	/*  disable i2c repeater on stv0299  */
	}

	return 0;
}

static int stv0299_read_status(struct dvb_frontend* fe, fe_status_t* status)
{
471
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498

	u8 signal = 0xff - stv0299_readreg (state, 0x18);
	u8 sync = stv0299_readreg (state, 0x1b);

	dprintk ("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __FUNCTION__, sync);
	*status = 0;

	if (signal > 10)
		*status |= FE_HAS_SIGNAL;

	if (sync & 0x80)
		*status |= FE_HAS_CARRIER;

	if (sync & 0x10)
		*status |= FE_HAS_VITERBI;

	if (sync & 0x08)
		*status |= FE_HAS_SYNC;

	if ((sync & 0x98) == 0x98)
		*status |= FE_HAS_LOCK;

	return 0;
}

static int stv0299_read_ber(struct dvb_frontend* fe, u32* ber)
{
499
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
500

501
	if (state->errmode != STATUS_BER) return 0;
L
Linus Torvalds 已提交
502 503 504 505 506 507 508
	*ber = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);

	return 0;
}

static int stv0299_read_signal_strength(struct dvb_frontend* fe, u16* strength)
{
509
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

	s32 signal =  0xffff - ((stv0299_readreg (state, 0x18) << 8)
			       | stv0299_readreg (state, 0x19));

	dprintk ("%s : FE_READ_SIGNAL_STRENGTH : AGC2I: 0x%02x%02x, signal=0x%04x\n", __FUNCTION__,
		 stv0299_readreg (state, 0x18),
		 stv0299_readreg (state, 0x19), (int) signal);

	signal = signal * 5 / 4;
	*strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;

	return 0;
}

static int stv0299_read_snr(struct dvb_frontend* fe, u16* snr)
{
526
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534 535 536 537

	s32 xsnr = 0xffff - ((stv0299_readreg (state, 0x24) << 8)
			   | stv0299_readreg (state, 0x25));
	xsnr = 3 * (xsnr - 0xa100);
	*snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;

	return 0;
}

static int stv0299_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
{
538
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
539

540 541
	if (state->errmode != STATUS_UCBLOCKS) *ucblocks = 0;
	else *ucblocks = (stv0299_readreg (state, 0x1d) << 8) | stv0299_readreg (state, 0x1e);
L
Linus Torvalds 已提交
542 543 544 545 546 547

	return 0;
}

static int stv0299_set_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
{
548
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562
	int invval = 0;

	dprintk ("%s : FE_SET_FRONTEND\n", __FUNCTION__);

	// set the inversion
	if (p->inversion == INVERSION_OFF) invval = 0;
	else if (p->inversion == INVERSION_ON) invval = 1;
	else {
		printk("stv0299 does not support auto-inversion\n");
		return -EINVAL;
	}
	if (state->config->invert) invval = (~invval) & 1;
	stv0299_writeregI(state, 0x0c, (stv0299_readreg(state, 0x0c) & 0xfe) | invval);

563 564 565 566 567 568 569 570
	stv0299_writeregI(state, 0x05, 0xb5);	/*  enable i2c repeater on stv0299  */
	state->config->pll_set(fe, state->i2c, p);
	stv0299_writeregI(state, 0x05, 0x35);	/*  disable i2c repeater on stv0299  */

	stv0299_set_FEC (state, p->u.qpsk.fec_inner);
	stv0299_set_symbolrate (fe, p->u.qpsk.symbol_rate);
	stv0299_writeregI(state, 0x22, 0x00);
	stv0299_writeregI(state, 0x23, 0x00);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578 579 580

	state->tuner_frequency = p->frequency;
	state->fec_inner = p->u.qpsk.fec_inner;
	state->symbol_rate = p->u.qpsk.symbol_rate;

	return 0;
}

static int stv0299_get_frontend(struct dvb_frontend* fe, struct dvb_frontend_parameters * p)
{
581
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	s32 derot_freq;
	int invval;

	derot_freq = (s32)(s16) ((stv0299_readreg (state, 0x22) << 8)
				| stv0299_readreg (state, 0x23));

	derot_freq *= (state->config->mclk >> 16);
	derot_freq += 500;
	derot_freq /= 1000;

	p->frequency += derot_freq;

	invval = stv0299_readreg (state, 0x0c) & 1;
	if (state->config->invert) invval = (~invval) & 1;
	p->inversion = invval ? INVERSION_ON : INVERSION_OFF;

	p->u.qpsk.fec_inner = stv0299_get_fec (state);
	p->u.qpsk.symbol_rate = stv0299_get_symbolrate (state);

	return 0;
}

static int stv0299_sleep(struct dvb_frontend* fe)
{
606
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
607 608 609 610 611 612 613 614 615

	stv0299_writeregI(state, 0x02, 0x80);
	state->initialised = 0;

	return 0;
}

static int stv0299_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
{
616
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625 626 627 628 629 630

	fesettings->min_delay_ms = state->config->min_delay_ms;
	if (fesettings->parameters.u.qpsk.symbol_rate < 10000000) {
		fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 32000;
		fesettings->max_drift = 5000;
	} else {
		fesettings->step_size = fesettings->parameters.u.qpsk.symbol_rate / 16000;
		fesettings->max_drift = fesettings->parameters.u.qpsk.symbol_rate / 2000;
	}
	return 0;
}

static void stv0299_release(struct dvb_frontend* fe)
{
631
	struct stv0299_state* state = fe->demodulator_priv;
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640 641 642 643
	kfree(state);
}

static struct dvb_frontend_ops stv0299_ops;

struct dvb_frontend* stv0299_attach(const struct stv0299_config* config,
				    struct i2c_adapter* i2c)
{
	struct stv0299_state* state = NULL;
	int id;

	/* allocate memory for the internal state */
644
	state = kmalloc(sizeof(struct stv0299_state), GFP_KERNEL);
L
Linus Torvalds 已提交
645 646 647 648 649 650 651 652 653 654
	if (state == NULL) goto error;

	/* setup the state */
	state->config = config;
	state->i2c = i2c;
	memcpy(&state->ops, &stv0299_ops, sizeof(struct dvb_frontend_ops));
	state->initialised = 0;
	state->tuner_frequency = 0;
	state->symbol_rate = 0;
	state->fec_inner = 0;
655
	state->errmode = STATUS_BER;
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667

	/* check if the demod is there */
	stv0299_writeregI(state, 0x02, 0x34); /* standby off */
	msleep(200);
	id = stv0299_readreg(state, 0x00);

	/* register 0x00 contains 0xa1 for STV0299 and STV0299B */
	/* register 0x00 might contain 0x80 when returning from standby */
	if (id != 0xa1 && id != 0x80) goto error;

	/* create dvb_frontend */
	state->frontend.ops = &state->ops;
668
	state->frontend.demodulator_priv = state;
L
Linus Torvalds 已提交
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
	return &state->frontend;

error:
	kfree(state);
	return NULL;
}

static struct dvb_frontend_ops stv0299_ops = {

	.info = {
		.name			= "ST STV0299 DVB-S",
		.type			= FE_QPSK,
		.frequency_min		= 950000,
		.frequency_max		= 2150000,
		.frequency_stepsize	= 125,	 /* kHz for QPSK frontends */
		.frequency_tolerance	= 0,
		.symbol_rate_min	= 1000000,
		.symbol_rate_max	= 45000000,
		.symbol_rate_tolerance	= 500,	/* ppm */
		.caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
		      FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
		      FE_CAN_QPSK |
		      FE_CAN_FEC_AUTO
	},

	.release = stv0299_release,

	.init = stv0299_init,
	.sleep = stv0299_sleep,

	.set_frontend = stv0299_set_frontend,
	.get_frontend = stv0299_get_frontend,
	.get_tune_settings = stv0299_get_tune_settings,

	.read_status = stv0299_read_status,
	.read_ber = stv0299_read_ber,
	.read_signal_strength = stv0299_read_signal_strength,
	.read_snr = stv0299_read_snr,
	.read_ucblocks = stv0299_read_ucblocks,

	.diseqc_send_master_cmd = stv0299_send_diseqc_msg,
	.diseqc_send_burst = stv0299_send_diseqc_burst,
	.set_tone = stv0299_set_tone,
	.set_voltage = stv0299_set_voltage,
	.dishnetwork_send_legacy_command = stv0299_send_legacy_dish_cmd,
};

716 717 718
module_param(debug_legacy_dish_switch, int, 0444);
MODULE_PARM_DESC(debug_legacy_dish_switch, "Enable timing analysis for Dish Network legacy switches");

L
Linus Torvalds 已提交
719 720 721 722 723
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");

MODULE_DESCRIPTION("ST STV0299 DVB Demodulator driver");
MODULE_AUTHOR("Ralph Metzler, Holger Waechtler, Peter Schildmann, Felix Domke, "
724
	      "Andreas Oberritter, Andrew de Quincey, Kenneth Aafly");
L
Linus Torvalds 已提交
725 726
MODULE_LICENSE("GPL");

727
EXPORT_SYMBOL(stv0299_enable_plli2c);
L
Linus Torvalds 已提交
728 729
EXPORT_SYMBOL(stv0299_writereg);
EXPORT_SYMBOL(stv0299_attach);