tda18271-fe.c 26.2 KB
Newer Older
1
/*
2
    tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
3

4
    Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

    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/delay.h>
#include <linux/videodev2.h>
23
#include "tda18271-priv.h"
24

25
int tda18271_debug;
26
module_param_named(debug, tda18271_debug, int, 0644);
27
MODULE_PARM_DESC(debug, "set debug level "
28
		 "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
29

30
static int tda18271_cal_on_startup;
31 32 33
module_param_named(cal, tda18271_cal_on_startup, int, 0644);
MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");

34
static DEFINE_MUTEX(tda18271_list_mutex);
35
static LIST_HEAD(hybrid_tuner_instance_list);
36

37 38
/*---------------------------------------------------------------------*/

39
static int tda18271_channel_configuration(struct dvb_frontend *fe,
40 41
					  struct tda18271_std_map_item *map,
					  u32 freq, u32 bw)
42 43 44 45 46 47 48 49 50
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
	u32 N;

	/* update TV broadcast parameters */

	/* set standard */
	regs[R_EP3]  &= ~0x1f; /* clear std bits */
51
	regs[R_EP3]  |= (map->agc_mode << 3) | map->std;
52 53 54 55 56 57

	/* set cal mode to normal */
	regs[R_EP4]  &= ~0x03;

	/* update IF output level & IF notch frequency */
	regs[R_EP4]  &= ~0x1c; /* clear if level bits */
58
	regs[R_EP4]  |= (map->if_lvl << 2);
59 60 61 62 63 64 65 66 67

	switch (priv->mode) {
	case TDA18271_ANALOG:
		regs[R_MPD]  &= ~0x80; /* IF notch = 0 */
		break;
	case TDA18271_DIGITAL:
		regs[R_MPD]  |= 0x80; /* IF notch = 1 */
		break;
	}
68

69 70 71
	/* update FM_RFn */
	regs[R_EP4]  &= ~0x80;
	regs[R_EP4]  |= map->fm_rfn << 7;
72

73 74 75
	/* update rf top / if top */
	regs[R_EB22]  = 0x00;
	regs[R_EB22] |= map->rfagc_top;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	tda18271_write_regs(fe, R_EB22, 1);

	/* --------------------------------------------------------------- */

	/* disable Power Level Indicator */
	regs[R_EP1]  |= 0x40;

	/* frequency dependent parameters */

	tda18271_calc_ir_measure(fe, &freq);

	tda18271_calc_bp_filter(fe, &freq);

	tda18271_calc_rf_band(fe, &freq);

	tda18271_calc_gain_taper(fe, &freq);

	/* --------------------------------------------------------------- */

	/* dual tuner and agc1 extra configuration */

	/* main vco when Master, cal vco when slave */
	regs[R_EB1]  |= 0x04; /* FIXME: assumes master */

	/* agc1 always active */
	regs[R_EB1]  &= ~0x02;

	/* agc1 has priority on agc2 */
	regs[R_EB1]  &= ~0x01;

	tda18271_write_regs(fe, R_EB1, 1);

	/* --------------------------------------------------------------- */

110
	N = map->if_freq * 1000 + freq;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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

	/* FIXME: assumes master */
	tda18271_calc_main_pll(fe, N);
	tda18271_write_regs(fe, R_MPD, 4);

	tda18271_write_regs(fe, R_TM, 7);

	/* main pll charge pump source */
	regs[R_EB4] |= 0x20;
	tda18271_write_regs(fe, R_EB4, 1);

	msleep(1);

	/* normal operation for the main pll */
	regs[R_EB4] &= ~0x20;
	tda18271_write_regs(fe, R_EB4, 1);

	msleep(5);

	return 0;
}

static int tda18271_read_thermometer(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
	int tm;

	/* switch thermometer on */
	regs[R_TM]   |= 0x10;
	tda18271_write_regs(fe, R_TM, 1);

	/* read thermometer info */
	tda18271_read_regs(fe);

	if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
	    (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {

		if ((regs[R_TM] & 0x20) == 0x20)
			regs[R_TM] &= ~0x20;
		else
			regs[R_TM] |= 0x20;

		tda18271_write_regs(fe, R_TM, 1);

		msleep(10); /* temperature sensing */

		/* read thermometer info */
		tda18271_read_regs(fe);
	}

	tm = tda18271_lookup_thermometer(fe);

	/* switch thermometer off */
	regs[R_TM]   &= ~0x10;
	tda18271_write_regs(fe, R_TM, 1);

	/* set CAL mode to normal */
	regs[R_EP4]  &= ~0x03;
	tda18271_write_regs(fe, R_EP4, 1);

	return tm;
}

175 176
/* ------------------------------------------------------------------ */

177 178
static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
						     u32 freq)
179 180 181 182 183 184 185 186
{
	struct tda18271_priv *priv = fe->tuner_priv;
	struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
	unsigned char *regs = priv->tda18271_regs;
	int tm_current, rfcal_comp, approx, i;
	u8 dc_over_dt, rf_tab;

	/* power up */
187
	tda18271_set_standby_mode(fe, 0, 0, 0);
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

	/* read die current temperature */
	tm_current = tda18271_read_thermometer(fe);

	/* frequency dependent parameters */

	tda18271_calc_rf_cal(fe, &freq);
	rf_tab = regs[R_EB14];

	i = tda18271_lookup_rf_band(fe, &freq, NULL);
	if (i < 0)
		return -EINVAL;

	if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
		approx = map[i].rf_a1 *
			(freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
	} else {
		approx = map[i].rf_a2 *
			(freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
	}

	if (approx < 0)
		approx = 0;
	if (approx > 255)
		approx = 255;

	tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);

	/* calculate temperature compensation */
217
	rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	regs[R_EB14] = approx + rfcal_comp;
	tda18271_write_regs(fe, R_EB14, 1);

	return 0;
}

static int tda18271_por(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;

	/* power up detector 1 */
	regs[R_EB12] &= ~0x20;
	tda18271_write_regs(fe, R_EB12, 1);

	regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
	regs[R_EB18] &= ~0x03; /* set agc1_gain to  6 dB */
	tda18271_write_regs(fe, R_EB18, 1);

	regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */

	/* POR mode */
241
	tda18271_set_standby_mode(fe, 1, 0, 0);
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301

	/* disable 1.5 MHz low pass filter */
	regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
	regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
	tda18271_write_regs(fe, R_EB21, 3);

	return 0;
}

static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
	u32 N;

	/* set CAL mode to normal */
	regs[R_EP4]  &= ~0x03;
	tda18271_write_regs(fe, R_EP4, 1);

	/* switch off agc1 */
	regs[R_EP3]  |= 0x40; /* sm_lt = 1 */

	regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
	tda18271_write_regs(fe, R_EB18, 1);

	/* frequency dependent parameters */

	tda18271_calc_bp_filter(fe, &freq);
	tda18271_calc_gain_taper(fe, &freq);
	tda18271_calc_rf_band(fe, &freq);
	tda18271_calc_km(fe, &freq);

	tda18271_write_regs(fe, R_EP1, 3);
	tda18271_write_regs(fe, R_EB13, 1);

	/* main pll charge pump source */
	regs[R_EB4]  |= 0x20;
	tda18271_write_regs(fe, R_EB4, 1);

	/* cal pll charge pump source */
	regs[R_EB7]  |= 0x20;
	tda18271_write_regs(fe, R_EB7, 1);

	/* force dcdc converter to 0 V */
	regs[R_EB14] = 0x00;
	tda18271_write_regs(fe, R_EB14, 1);

	/* disable plls lock */
	regs[R_EB20] &= ~0x20;
	tda18271_write_regs(fe, R_EB20, 1);

	/* set CAL mode to RF tracking filter calibration */
	regs[R_EP4]  |= 0x03;
	tda18271_write_regs(fe, R_EP4, 2);

	/* --------------------------------------------------------------- */

	/* set the internal calibration signal */
	N = freq;

302 303
	tda18271_calc_cal_pll(fe, N);
	tda18271_write_regs(fe, R_CPD, 4);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

	/* downconvert internal calibration */
	N += 1000000;

	tda18271_calc_main_pll(fe, N);
	tda18271_write_regs(fe, R_MPD, 4);

	msleep(5);

	tda18271_write_regs(fe, R_EP2, 1);
	tda18271_write_regs(fe, R_EP1, 1);
	tda18271_write_regs(fe, R_EP2, 1);
	tda18271_write_regs(fe, R_EP1, 1);

	/* --------------------------------------------------------------- */

	/* normal operation for the main pll */
	regs[R_EB4] &= ~0x20;
	tda18271_write_regs(fe, R_EB4, 1);

	/* normal operation for the cal pll  */
	regs[R_EB7] &= ~0x20;
	tda18271_write_regs(fe, R_EB7, 1);

328
	msleep(10); /* plls locking */
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441

	/* launch the rf tracking filters calibration */
	regs[R_EB20]  |= 0x20;
	tda18271_write_regs(fe, R_EB20, 1);

	msleep(60); /* calibration */

	/* --------------------------------------------------------------- */

	/* set CAL mode to normal */
	regs[R_EP4]  &= ~0x03;

	/* switch on agc1 */
	regs[R_EP3]  &= ~0x40; /* sm_lt = 0 */

	regs[R_EB18] &= ~0x03; /* set agc1_gain to  6 dB */
	tda18271_write_regs(fe, R_EB18, 1);

	tda18271_write_regs(fe, R_EP3, 2);

	/* synchronization */
	tda18271_write_regs(fe, R_EP1, 1);

	/* get calibration result */
	tda18271_read_extended(fe);

	return regs[R_EB14];
}

static int tda18271_powerscan(struct dvb_frontend *fe,
			      u32 *freq_in, u32 *freq_out)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
	int sgn, bcal, count, wait;
	u8 cid_target;
	u16 count_limit;
	u32 freq;

	freq = *freq_in;

	tda18271_calc_rf_band(fe, &freq);
	tda18271_calc_rf_cal(fe, &freq);
	tda18271_calc_gain_taper(fe, &freq);
	tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);

	tda18271_write_regs(fe, R_EP2, 1);
	tda18271_write_regs(fe, R_EB14, 1);

	/* downconvert frequency */
	freq += 1000000;

	tda18271_calc_main_pll(fe, freq);
	tda18271_write_regs(fe, R_MPD, 4);

	msleep(5); /* pll locking */

	/* detection mode */
	regs[R_EP4]  &= ~0x03;
	regs[R_EP4]  |= 0x01;
	tda18271_write_regs(fe, R_EP4, 1);

	/* launch power detection measurement */
	tda18271_write_regs(fe, R_EP2, 1);

	/* read power detection info, stored in EB10 */
	tda18271_read_extended(fe);

	/* algorithm initialization */
	sgn = 1;
	*freq_out = *freq_in;
	bcal = 0;
	count = 0;
	wait = false;

	while ((regs[R_EB10] & 0x3f) < cid_target) {
		/* downconvert updated freq to 1 MHz */
		freq = *freq_in + (sgn * count) + 1000000;

		tda18271_calc_main_pll(fe, freq);
		tda18271_write_regs(fe, R_MPD, 4);

		if (wait) {
			msleep(5); /* pll locking */
			wait = false;
		} else
			udelay(100); /* pll locking */

		/* launch power detection measurement */
		tda18271_write_regs(fe, R_EP2, 1);

		/* read power detection info, stored in EB10 */
		tda18271_read_extended(fe);

		count += 200;

		if (count < count_limit)
			continue;

		if (sgn <= 0)
			break;

		sgn = -1 * sgn;
		count = 200;
		wait = true;
	}

	if ((regs[R_EB10] & 0x3f) >= cid_target) {
		bcal = 1;
		*freq_out = freq - 1000000;
	} else
		bcal = 0;

442
	tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 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 499 500 501 502 503 504
		bcal, *freq_in, *freq_out, freq);

	return bcal;
}

static int tda18271_powerscan_init(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;

	/* set standard to digital */
	regs[R_EP3]  &= ~0x1f; /* clear std bits */
	regs[R_EP3]  |= 0x12;

	/* set cal mode to normal */
	regs[R_EP4]  &= ~0x03;

	/* update IF output level & IF notch frequency */
	regs[R_EP4]  &= ~0x1c; /* clear if level bits */

	tda18271_write_regs(fe, R_EP3, 2);

	regs[R_EB18] &= ~0x03; /* set agc1_gain to   6 dB */
	tda18271_write_regs(fe, R_EB18, 1);

	regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */

	/* 1.5 MHz low pass filter */
	regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
	regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */

	tda18271_write_regs(fe, R_EB21, 3);

	return 0;
}

static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
	unsigned char *regs = priv->tda18271_regs;
	int bcal, rf, i;
#define RF1 0
#define RF2 1
#define RF3 2
	u32 rf_default[3];
	u32 rf_freq[3];
	u8 prog_cal[3];
	u8 prog_tab[3];

	i = tda18271_lookup_rf_band(fe, &freq, NULL);

	if (i < 0)
		return i;

	rf_default[RF1] = 1000 * map[i].rf1_def;
	rf_default[RF2] = 1000 * map[i].rf2_def;
	rf_default[RF3] = 1000 * map[i].rf3_def;

	for (rf = RF1; rf <= RF3; rf++) {
		if (0 == rf_default[rf])
			return 0;
505
		tda_cal("freq = %d, rf = %d\n", freq, rf);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

		/* look for optimized calibration frequency */
		bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);

		tda18271_calc_rf_cal(fe, &rf_freq[rf]);
		prog_tab[rf] = regs[R_EB14];

		if (1 == bcal)
			prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
		else
			prog_cal[rf] = prog_tab[rf];

		switch (rf) {
		case RF1:
			map[i].rf_a1 = 0;
			map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
			map[i].rf1   = rf_freq[RF1] / 1000;
			break;
		case RF2:
			map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
					prog_cal[RF1] + prog_tab[RF1]) /
				((rf_freq[RF2] - rf_freq[RF1]) / 1000);
			map[i].rf2   = rf_freq[RF2] / 1000;
			break;
		case RF3:
			map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
					prog_cal[RF2] + prog_tab[RF2]) /
				((rf_freq[RF3] - rf_freq[RF2]) / 1000);
			map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
			map[i].rf3   = rf_freq[RF3] / 1000;
			break;
		default:
			BUG();
		}
	}

	return 0;
}

545
static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned int i;

	tda_info("tda18271: performing RF tracking filter calibration\n");

	/* wait for die temperature stabilization */
	msleep(200);

	tda18271_powerscan_init(fe);

	/* rf band calibration */
	for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
		tda18271_rf_tracking_filters_init(fe, 1000 *
						  priv->rf_cal_state[i].rfmax);

562
	priv->tm_rfcal = tda18271_read_thermometer(fe);
563 564 565 566 567 568

	return 0;
}

/* ------------------------------------------------------------------ */

569
static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
570 571
{
	struct tda18271_priv *priv = fe->tuner_priv;
572 573 574 575 576
	unsigned char *regs = priv->tda18271_regs;

	/* test RF_CAL_OK to see if we need init */
	if ((regs[R_EP1] & 0x10) == 0)
		priv->cal_initialized = false;
577 578 579 580

	if (priv->cal_initialized)
		return 0;

581
	tda18271_calc_rf_filter_curve(fe);
582 583 584

	tda18271_por(fe);

585 586
	tda_info("tda18271: RF tracking filter calibration complete\n");

587 588 589 590 591
	priv->cal_initialized = true;

	return 0;
}

592 593
static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
						     u32 freq, u32 bw)
594 595 596
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
597
	u32 N = 0;
598

599
	/* calculate bp filter */
600
	tda18271_calc_bp_filter(fe, &freq);
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
	tda18271_write_regs(fe, R_EP1, 1);

	regs[R_EB4]  &= 0x07;
	regs[R_EB4]  |= 0x60;
	tda18271_write_regs(fe, R_EB4, 1);

	regs[R_EB7]   = 0x60;
	tda18271_write_regs(fe, R_EB7, 1);

	regs[R_EB14]  = 0x00;
	tda18271_write_regs(fe, R_EB14, 1);

	regs[R_EB20]  = 0xcc;
	tda18271_write_regs(fe, R_EB20, 1);

616
	/* set cal mode to RF tracking filter calibration */
617
	regs[R_EP4]  |= 0x03;
618

619
	/* calculate cal pll */
620 621 622 623 624 625 626 627 628 629

	switch (priv->mode) {
	case TDA18271_ANALOG:
		N = freq - 1250000;
		break;
	case TDA18271_DIGITAL:
		N = freq + bw / 2;
		break;
	}

630
	tda18271_calc_cal_pll(fe, N);
631

632
	/* calculate main pll */
633 634 635 636 637 638 639 640 641 642

	switch (priv->mode) {
	case TDA18271_ANALOG:
		N = freq - 250000;
		break;
	case TDA18271_DIGITAL:
		N = freq + bw / 2 + 1000000;
		break;
	}

643
	tda18271_calc_main_pll(fe, N);
644 645 646 647

	tda18271_write_regs(fe, R_EP3, 11);
	msleep(5); /* RF tracking filter calibration initialization */

648
	/* search for K,M,CO for RF calibration */
649
	tda18271_calc_km(fe, &freq);
650 651
	tda18271_write_regs(fe, R_EB13, 1);

652
	/* search for rf band */
653
	tda18271_calc_rf_band(fe, &freq);
654

655
	/* search for gain taper */
656
	tda18271_calc_gain_taper(fe, &freq);
657 658 659 660 661 662 663 664 665 666 667 668

	tda18271_write_regs(fe, R_EP2, 1);
	tda18271_write_regs(fe, R_EP1, 1);
	tda18271_write_regs(fe, R_EP2, 1);
	tda18271_write_regs(fe, R_EP1, 1);

	regs[R_EB4]  &= 0x07;
	regs[R_EB4]  |= 0x40;
	tda18271_write_regs(fe, R_EB4, 1);

	regs[R_EB7]   = 0x40;
	tda18271_write_regs(fe, R_EB7, 1);
669
	msleep(10); /* pll locking */
670 671 672 673 674 675 676 677 678 679

	regs[R_EB20]  = 0xec;
	tda18271_write_regs(fe, R_EB20, 1);
	msleep(60); /* RF tracking filter calibration completion */

	regs[R_EP4]  &= ~0x03; /* set cal mode to normal */
	tda18271_write_regs(fe, R_EP4, 1);

	tda18271_write_regs(fe, R_EP1, 1);

680 681
	/* RF tracking filter correction for VHF_Low band */
	if (0 == tda18271_calc_rf_cal(fe, &freq))
682 683
		tda18271_write_regs(fe, R_EB14, 1);

684 685 686
	return 0;
}

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 716 717 718 719 720 721 722
static int tda18271_ir_cal_init(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;

	tda18271_read_regs(fe);

	/* test IR_CAL_OK to see if we need init */
	if ((regs[R_EP1] & 0x08) == 0)
		tda18271_init_regs(fe);

	return 0;
}

static int tda18271_init(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;

	mutex_lock(&priv->lock);

	/* power up */
	tda18271_set_standby_mode(fe, 0, 0, 0);

	/* initialization */
	tda18271_ir_cal_init(fe);

	if (priv->id == TDA18271HDC2)
		tda18271c2_rf_cal_init(fe);

	mutex_unlock(&priv->lock);

	return 0;
}

723
static int tda18271_tune(struct dvb_frontend *fe,
724
			 struct tda18271_std_map_item *map, u32 freq, u32 bw)
725 726 727
{
	struct tda18271_priv *priv = fe->tuner_priv;

728 729
	tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
		freq, map->if_freq, bw, map->agc_mode, map->std);
730

731 732 733 734
	tda18271_init(fe);

	mutex_lock(&priv->lock);

735 736
	switch (priv->id) {
	case TDA18271HDC1:
737
		tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
738 739
		break;
	case TDA18271HDC2:
740
		tda18271c2_rf_tracking_filters_correction(fe, freq);
741 742
		break;
	}
743
	tda18271_channel_configuration(fe, map, freq, bw);
744 745 746 747

	mutex_unlock(&priv->lock);

	return 0;
748 749
}

750 751 752 753 754 755
/* ------------------------------------------------------------------ */

static int tda18271_set_params(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *params)
{
	struct tda18271_priv *priv = fe->tuner_priv;
756
	struct tda18271_std_map *std_map = &priv->std;
757
	struct tda18271_std_map_item *map;
758
	int ret;
759
	u32 bw, freq = params->frequency;
760 761 762 763 764 765 766

	priv->mode = TDA18271_DIGITAL;

	if (fe->ops.info.type == FE_ATSC) {
		switch (params->u.vsb.modulation) {
		case VSB_8:
		case VSB_16:
767
			map = &std_map->atsc_6;
768 769 770
			break;
		case QAM_64:
		case QAM_256:
771
			map = &std_map->qam_6;
772 773
			break;
		default:
774
			tda_warn("modulation not set!\n");
775 776
			return -EINVAL;
		}
777 778
#if 0
		/* userspace request is already center adjusted */
779
		freq += 1750000; /* Adjust to center (+1.75MHZ) */
780
#endif
781 782 783 784 785
		bw = 6000000;
	} else if (fe->ops.info.type == FE_OFDM) {
		switch (params->u.ofdm.bandwidth) {
		case BANDWIDTH_6_MHZ:
			bw = 6000000;
786
			map = &std_map->dvbt_6;
787 788 789
			break;
		case BANDWIDTH_7_MHZ:
			bw = 7000000;
790
			map = &std_map->dvbt_7;
791 792 793
			break;
		case BANDWIDTH_8_MHZ:
			bw = 8000000;
794
			map = &std_map->dvbt_8;
795 796
			break;
		default:
797
			tda_warn("bandwidth not set!\n");
798 799 800
			return -EINVAL;
		}
	} else {
801
		tda_warn("modulation type not supported!\n");
802 803 804
		return -EINVAL;
	}

805 806 807 808
	/* When tuning digital, the analog demod must be tri-stated */
	if (fe->ops.analog_ops.standby)
		fe->ops.analog_ops.standby(fe);

809
	ret = tda18271_tune(fe, map, freq, bw);
810 811 812 813 814 815 816 817 818

	if (ret < 0)
		goto fail;

	priv->frequency = freq;
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
		params->u.ofdm.bandwidth : 0;
fail:
	return ret;
819 820 821 822 823 824
}

static int tda18271_set_analog_params(struct dvb_frontend *fe,
				      struct analog_parameters *params)
{
	struct tda18271_priv *priv = fe->tuner_priv;
825
	struct tda18271_std_map *std_map = &priv->std;
826
	struct tda18271_std_map_item *map;
827
	char *mode;
828
	int ret;
829
	u32 freq = params->frequency * 62500;
830 831 832

	priv->mode = TDA18271_ANALOG;

833 834
	if (params->mode == V4L2_TUNER_RADIO) {
		freq = freq / 1000;
835
		map = &std_map->fm_radio;
836 837
		mode = "fm";
	} else if (params->std & V4L2_STD_MN) {
838
		map = &std_map->atv_mn;
839 840
		mode = "MN";
	} else if (params->std & V4L2_STD_B) {
841
		map = &std_map->atv_b;
842 843
		mode = "B";
	} else if (params->std & V4L2_STD_GH) {
844
		map = &std_map->atv_gh;
845 846
		mode = "GH";
	} else if (params->std & V4L2_STD_PAL_I) {
847
		map = &std_map->atv_i;
848 849
		mode = "I";
	} else if (params->std & V4L2_STD_DK) {
850
		map = &std_map->atv_dk;
851 852
		mode = "DK";
	} else if (params->std & V4L2_STD_SECAM_L) {
853
		map = &std_map->atv_l;
854 855
		mode = "L";
	} else if (params->std & V4L2_STD_SECAM_LC) {
856
		map = &std_map->atv_lc;
857
		mode = "L'";
858
	} else {
859
		map = &std_map->atv_i;
860 861 862
		mode = "xx";
	}

863
	tda_dbg("setting tda18271 to system %s\n", mode);
864

865
	ret = tda18271_tune(fe, map, freq, 0);
866 867 868 869 870 871 872 873

	if (ret < 0)
		goto fail;

	priv->frequency = freq;
	priv->bandwidth = 0;
fail:
	return ret;
874 875
}

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
static int tda18271_sleep(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;

	mutex_lock(&priv->lock);

	/* standby mode w/ slave tuner output
	 * & loop thru & xtal oscillator on */
	tda18271_set_standby_mode(fe, 1, 0, 0);

	mutex_unlock(&priv->lock);

	return 0;
}

891 892
static int tda18271_release(struct dvb_frontend *fe)
{
893 894 895 896
	struct tda18271_priv *priv = fe->tuner_priv;

	mutex_lock(&tda18271_list_mutex);

897 898
	if (priv)
		hybrid_tuner_release_state(priv);
899 900 901

	mutex_unlock(&tda18271_list_mutex);

902
	fe->tuner_priv = NULL;
903

904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	return 0;
}

static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	*frequency = priv->frequency;
	return 0;
}

static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	*bandwidth = priv->bandwidth;
	return 0;
}

921 922 923
/* ------------------------------------------------------------------ */

#define tda18271_update_std(std_cfg, name) do {				\
924 925
	if (map->std_cfg.if_freq +					\
		map->std_cfg.agc_mode + map->std_cfg.std > 0) {		\
926 927 928 929 930 931
		tda_dbg("Using custom std config for %s\n", name);	\
		memcpy(&std->std_cfg, &map->std_cfg,			\
			sizeof(struct tda18271_std_map_item));		\
	} } while (0)

#define tda18271_dump_std_item(std_cfg, name) do {			\
932 933 934
	tda_dbg("(%s) if freq = %d, agc_mode = %d, std = %d\n",		\
		name, std->std_cfg.if_freq,				\
		std->std_cfg.agc_mode, std->std_cfg.std);		\
935 936 937 938 939 940 941 942
	} while (0)

static int tda18271_dump_std_map(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	struct tda18271_std_map *std = &priv->std;

	tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
943
	tda18271_dump_std_item(fm_radio, "fm");
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	tda18271_dump_std_item(atv_b,  "pal b");
	tda18271_dump_std_item(atv_dk, "pal dk");
	tda18271_dump_std_item(atv_gh, "pal gh");
	tda18271_dump_std_item(atv_i,  "pal i");
	tda18271_dump_std_item(atv_l,  "pal l");
	tda18271_dump_std_item(atv_lc, "pal l'");
	tda18271_dump_std_item(atv_mn, "atv mn");
	tda18271_dump_std_item(atsc_6, "atsc 6");
	tda18271_dump_std_item(dvbt_6, "dvbt 6");
	tda18271_dump_std_item(dvbt_7, "dvbt 7");
	tda18271_dump_std_item(dvbt_8, "dvbt 8");
	tda18271_dump_std_item(qam_6,  "qam 6");
	tda18271_dump_std_item(qam_8,  "qam 8");

	return 0;
}

static int tda18271_update_std_map(struct dvb_frontend *fe,
				   struct tda18271_std_map *map)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	struct tda18271_std_map *std = &priv->std;

	if (!map)
		return -EINVAL;

970
	tda18271_update_std(fm_radio, "fm");
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	tda18271_update_std(atv_b,  "atv b");
	tda18271_update_std(atv_dk, "atv dk");
	tda18271_update_std(atv_gh, "atv gh");
	tda18271_update_std(atv_i,  "atv i");
	tda18271_update_std(atv_l,  "atv l");
	tda18271_update_std(atv_lc, "atv l'");
	tda18271_update_std(atv_mn, "atv mn");
	tda18271_update_std(atsc_6, "atsc 6");
	tda18271_update_std(dvbt_6, "dvbt 6");
	tda18271_update_std(dvbt_7, "dvbt 7");
	tda18271_update_std(dvbt_8, "dvbt 8");
	tda18271_update_std(qam_6,  "qam 6");
	tda18271_update_std(qam_8,  "qam 8");

	return 0;
}

988 989 990 991 992 993 994
static int tda18271_get_id(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
	char *name;
	int ret = 0;

995
	mutex_lock(&priv->lock);
996
	tda18271_read_regs(fe);
997
	mutex_unlock(&priv->lock);
998 999 1000 1001

	switch (regs[R_ID] & 0x7f) {
	case 3:
		name = "TDA18271HD/C1";
1002
		priv->id = TDA18271HDC1;
1003 1004 1005
		break;
	case 4:
		name = "TDA18271HD/C2";
1006
		priv->id = TDA18271HDC2;
1007 1008 1009 1010 1011 1012 1013
		break;
	default:
		name = "Unknown device";
		ret = -EINVAL;
		break;
	}

1014
	tda_info("%s detected @ %d-%04x%s\n", name,
1015 1016
		 i2c_adapter_id(priv->i2c_props.adap),
		 priv->i2c_props.addr,
1017 1018 1019 1020 1021
		 (0 == ret) ? "" : ", device not supported.");

	return ret;
}

1022 1023 1024 1025 1026 1027 1028
static struct dvb_tuner_ops tda18271_tuner_ops = {
	.info = {
		.name = "NXP TDA18271HD",
		.frequency_min  =  45000000,
		.frequency_max  = 864000000,
		.frequency_step =     62500
	},
1029
	.init              = tda18271_init,
1030
	.sleep             = tda18271_sleep,
1031 1032 1033 1034 1035 1036 1037 1038
	.set_params        = tda18271_set_params,
	.set_analog_params = tda18271_set_analog_params,
	.release           = tda18271_release,
	.get_frequency     = tda18271_get_frequency,
	.get_bandwidth     = tda18271_get_bandwidth,
};

struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1039
				     struct i2c_adapter *i2c,
1040
				     struct tda18271_config *cfg)
1041 1042
{
	struct tda18271_priv *priv = NULL;
1043
	int instance;
1044 1045 1046

	mutex_lock(&tda18271_list_mutex);

1047 1048 1049 1050 1051 1052 1053 1054 1055
	instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
					      hybrid_tuner_instance_list,
					      i2c, addr, "tda18271");
	switch (instance) {
	case 0:
		goto fail;
		break;
	case 1:
		/* new tuner instance */
1056 1057 1058
		priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
		priv->cal_initialized = false;
		mutex_init(&priv->lock);
1059

1060
		fe->tuner_priv = priv;
1061

1062 1063 1064
		if (cfg)
			priv->small_i2c = cfg->small_i2c;

1065 1066
		if (tda18271_get_id(fe) < 0)
			goto fail;
1067

1068 1069
		if (tda18271_assign_map_layout(fe) < 0)
			goto fail;
1070

1071 1072
		mutex_lock(&priv->lock);
		tda18271_init_regs(fe);
1073 1074

		if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1075
			tda18271c2_rf_cal_init(fe);
1076

1077
		mutex_unlock(&priv->lock);
1078 1079 1080 1081 1082 1083 1084 1085 1086
		break;
	default:
		/* existing tuner instance */
		fe->tuner_priv = priv;

		/* allow dvb driver to override i2c gate setting */
		if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
			priv->gate = cfg->gate;
		break;
1087
	}
1088

1089 1090 1091 1092
	/* override default std map with values in config struct */
	if ((cfg) && (cfg->std_map))
		tda18271_update_std_map(fe, cfg->std_map);

1093
	mutex_unlock(&tda18271_list_mutex);
1094

1095 1096
	memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
	       sizeof(struct dvb_tuner_ops));
1097

1098 1099
	if (tda18271_debug & DBG_MAP)
		tda18271_dump_std_map(fe);
1100

1101
	return fe;
1102
fail:
1103 1104
	mutex_unlock(&tda18271_list_mutex);

1105 1106
	tda18271_release(fe);
	return NULL;
1107 1108 1109 1110 1111
}
EXPORT_SYMBOL_GPL(tda18271_attach);
MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
MODULE_LICENSE("GPL");
1112
MODULE_VERSION("0.2");
1113 1114 1115 1116 1117 1118 1119 1120

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-basic-offset: 8
 * End:
 */