tda18271-fe.c 28.5 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 40 41 42 43 44 45 46 47
static inline int charge_pump_source(struct dvb_frontend *fe, int force)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	return tda18271_charge_pump_source(fe,
					   (priv->role == TDA18271_SLAVE) ?
					   TDA18271_CAL_PLL :
					   TDA18271_MAIN_PLL, force);
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
static inline void tda18271_set_if_notch(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;

	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;
	}
}

63
static int tda18271_channel_configuration(struct dvb_frontend *fe,
64 65
					  struct tda18271_std_map_item *map,
					  u32 freq, u32 bw)
66 67 68
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
69
	int ret;
70 71 72 73 74 75
	u32 N;

	/* update TV broadcast parameters */

	/* set standard */
	regs[R_EP3]  &= ~0x1f; /* clear std bits */
76
	regs[R_EP3]  |= (map->agc_mode << 3) | map->std;
77

78 79 80 81
	if (priv->id == TDA18271HDC2) {
		/* set rfagc to high speed mode */
		regs[R_EP3] &= ~0x04;
	}
82

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

86
	/* update IF output level */
87
	regs[R_EP4]  &= ~0x1c; /* clear if level bits */
88
	regs[R_EP4]  |= (map->if_lvl << 2);
89

90 91 92
	/* update FM_RFn */
	regs[R_EP4]  &= ~0x80;
	regs[R_EP4]  |= map->fm_rfn << 7;
93

94 95 96
	/* update rf top / if top */
	regs[R_EB22]  = 0x00;
	regs[R_EB22] |= map->rfagc_top;
97
	ret = tda18271_write_regs(fe, R_EB22, 1);
98
	if (tda_fail(ret))
99
		goto fail;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

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

	/* 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 */

120 121 122 123 124 125 126 127
	switch (priv->role) {
	case TDA18271_MASTER:
		regs[R_EB1]  |= 0x04; /* main vco */
		break;
	case TDA18271_SLAVE:
		regs[R_EB1]  &= ~0x04; /* cal vco */
		break;
	}
128 129 130 131 132 133 134

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

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

135
	ret = tda18271_write_regs(fe, R_EB1, 1);
136
	if (tda_fail(ret))
137
		goto fail;
138 139 140

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

141
	N = map->if_freq * 1000 + freq;
142

143 144 145
	switch (priv->role) {
	case TDA18271_MASTER:
		tda18271_calc_main_pll(fe, N);
146
		tda18271_set_if_notch(fe);
147 148 149 150 151 152 153
		tda18271_write_regs(fe, R_MPD, 4);
		break;
	case TDA18271_SLAVE:
		tda18271_calc_cal_pll(fe, N);
		tda18271_write_regs(fe, R_CPD, 4);

		regs[R_MPD] = regs[R_CPD] & 0x7f;
154
		tda18271_set_if_notch(fe);
155 156 157
		tda18271_write_regs(fe, R_MPD, 1);
		break;
	}
158

159
	ret = tda18271_write_regs(fe, R_TM, 7);
160
	if (tda_fail(ret))
161
		goto fail;
162

163 164
	/* force charge pump source */
	charge_pump_source(fe, 1);
165 166 167

	msleep(1);

168 169
	/* return pll to normal operation */
	charge_pump_source(fe, 0);
170

171 172
	msleep(20);

173 174 175 176 177 178 179 180
	if (priv->id == TDA18271HDC2) {
		/* set rfagc to normal speed mode */
		if (map->fm_rfn)
			regs[R_EP3] &= ~0x04;
		else
			regs[R_EP3] |= 0x04;
		ret = tda18271_write_regs(fe, R_EP3, 1);
	}
181 182
fail:
	return ret;
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
}

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;
}

227 228
/* ------------------------------------------------------------------ */

229 230
static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
						     u32 freq)
231 232 233 234
{
	struct tda18271_priv *priv = fe->tuner_priv;
	struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
	unsigned char *regs = priv->tda18271_regs;
235
	int tm_current, rfcal_comp, approx, i, ret;
236 237 238
	u8 dc_over_dt, rf_tab;

	/* power up */
239
	ret = tda18271_set_standby_mode(fe, 0, 0, 0);
240
	if (tda_fail(ret))
241
		goto fail;
242 243 244 245 246 247 248 249 250 251

	/* 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);
252 253
	if (tda_fail(i))
		return i;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

	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 */
271
	rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
272 273

	regs[R_EB14] = approx + rfcal_comp;
274 275 276
	ret = tda18271_write_regs(fe, R_EB14, 1);
fail:
	return ret;
277 278 279 280 281 282
}

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

	/* power up detector 1 */
	regs[R_EB12] &= ~0x20;
287
	ret = tda18271_write_regs(fe, R_EB12, 1);
288
	if (tda_fail(ret))
289
		goto fail;
290 291 292

	regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
	regs[R_EB18] &= ~0x03; /* set agc1_gain to  6 dB */
293
	ret = tda18271_write_regs(fe, R_EB18, 1);
294
	if (tda_fail(ret))
295
		goto fail;
296 297 298 299

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

	/* POR mode */
300
	ret = tda18271_set_standby_mode(fe, 1, 0, 0);
301
	if (tda_fail(ret))
302
		goto fail;
303 304 305 306

	/* disable 1.5 MHz low pass filter */
	regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
	regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
307 308 309
	ret = tda18271_write_regs(fe, R_EB21, 3);
fail:
	return ret;
310 311 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 338
}

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 */
339
	tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
340 341

	/* cal pll charge pump source */
342
	tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

	/* 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;

361 362
	tda18271_calc_cal_pll(fe, N);
	tda18271_write_regs(fe, R_CPD, 4);
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

	/* 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 */
380
	tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
381 382

	/* normal operation for the cal pll  */
383
	tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
384

385
	msleep(10); /* plls locking */
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

	/* 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;
420
	int sgn, bcal, count, wait, ret;
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	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 */
452
	ret = tda18271_read_extended(fe);
453
	if (tda_fail(ret))
454
		return ret;
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

	/* 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 */
480
		ret = tda18271_read_extended(fe);
481
		if (tda_fail(ret))
482
			return ret;
483 484 485

		count += 200;

486
		if (count <= count_limit)
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
			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;

503
	tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
504 505 506 507 508 509 510 511 512
		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;
513
	int ret;
514 515 516 517 518 519 520 521

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

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

522
	/* update IF output level */
523 524
	regs[R_EP4]  &= ~0x1c; /* clear if level bits */

525
	ret = tda18271_write_regs(fe, R_EP3, 2);
526
	if (tda_fail(ret))
527
		goto fail;
528 529

	regs[R_EB18] &= ~0x03; /* set agc1_gain to   6 dB */
530
	ret = tda18271_write_regs(fe, R_EB18, 1);
531
	if (tda_fail(ret))
532
		goto fail;
533 534 535 536 537 538 539

	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 */

540 541 542
	ret = tda18271_write_regs(fe, R_EB21, 3);
fail:
	return ret;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
}

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);

561
	if (tda_fail(i))
562 563 564 565 566 567 568 569 570
		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;
571
		tda_cal("freq = %d, rf = %d\n", freq, rf);
572 573 574

		/* look for optimized calibration frequency */
		bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
575
		if (tda_fail(bcal))
576
			return bcal;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

		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;
}

613
static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
614 615 616
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned int i;
617
	int ret;
618 619 620 621 622 623

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

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

624
	ret = tda18271_powerscan_init(fe);
625
	if (tda_fail(ret))
626
		goto fail;
627 628

	/* rf band calibration */
629 630
	for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
		ret =
631 632
		tda18271_rf_tracking_filters_init(fe, 1000 *
						  priv->rf_cal_state[i].rfmax);
633
		if (tda_fail(ret))
634 635
			goto fail;
	}
636

637
	priv->tm_rfcal = tda18271_read_thermometer(fe);
638 639
fail:
	return ret;
640 641 642 643
}

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

644
static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
645 646
{
	struct tda18271_priv *priv = fe->tuner_priv;
647
	unsigned char *regs = priv->tda18271_regs;
648
	int ret;
649 650 651 652

	/* test RF_CAL_OK to see if we need init */
	if ((regs[R_EP1] & 0x10) == 0)
		priv->cal_initialized = false;
653 654 655 656

	if (priv->cal_initialized)
		return 0;

657
	ret = tda18271_calc_rf_filter_curve(fe);
658
	if (tda_fail(ret))
659
		goto fail;
660

661
	ret = tda18271_por(fe);
662
	if (tda_fail(ret))
663
		goto fail;
664

665 666
	tda_info("tda18271: RF tracking filter calibration complete\n");

667
	priv->cal_initialized = true;
668
	goto end;
669
fail:
670 671
	tda_info("tda18271: RF tracking filter calibration failed!\n");
end:
672
	return ret;
673 674
}

675 676
static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
						     u32 freq, u32 bw)
677 678 679
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
680
	int ret;
681
	u32 N = 0;
682

683
	/* calculate bp filter */
684
	tda18271_calc_bp_filter(fe, &freq);
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	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);

700
	/* set cal mode to RF tracking filter calibration */
701
	regs[R_EP4]  |= 0x03;
702

703
	/* calculate cal pll */
704 705 706 707 708 709 710 711 712 713

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

714
	tda18271_calc_cal_pll(fe, N);
715

716
	/* calculate main pll */
717 718 719 720 721 722 723 724 725 726

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

727
	tda18271_calc_main_pll(fe, N);
728

729
	ret = tda18271_write_regs(fe, R_EP3, 11);
730
	if (tda_fail(ret))
731 732
		return ret;

733 734
	msleep(5); /* RF tracking filter calibration initialization */

735
	/* search for K,M,CO for RF calibration */
736
	tda18271_calc_km(fe, &freq);
737 738
	tda18271_write_regs(fe, R_EB13, 1);

739
	/* search for rf band */
740
	tda18271_calc_rf_band(fe, &freq);
741

742
	/* search for gain taper */
743
	tda18271_calc_gain_taper(fe, &freq);
744 745 746 747 748 749 750 751 752 753 754 755

	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);
756
	msleep(10); /* pll locking */
757 758 759 760 761 762 763 764 765 766

	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);

767 768
	/* RF tracking filter correction for VHF_Low band */
	if (0 == tda18271_calc_rf_cal(fe, &freq))
769 770
		tda18271_write_regs(fe, R_EB14, 1);

771 772 773
	return 0;
}

774 775
/* ------------------------------------------------------------------ */

776 777 778 779
static int tda18271_ir_cal_init(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
	unsigned char *regs = priv->tda18271_regs;
780
	int ret;
781

782
	ret = tda18271_read_regs(fe);
783
	if (tda_fail(ret))
784
		goto fail;
785 786 787

	/* test IR_CAL_OK to see if we need init */
	if ((regs[R_EP1] & 0x08) == 0)
788 789 790
		ret = tda18271_init_regs(fe);
fail:
	return ret;
791 792 793 794 795
}

static int tda18271_init(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
796
	int ret;
797 798 799 800

	mutex_lock(&priv->lock);

	/* power up */
801
	ret = tda18271_set_standby_mode(fe, 0, 0, 0);
802
	if (tda_fail(ret))
803
		goto fail;
804 805

	/* initialization */
806
	ret = tda18271_ir_cal_init(fe);
807
	if (tda_fail(ret))
808
		goto fail;
809 810 811

	if (priv->id == TDA18271HDC2)
		tda18271c2_rf_cal_init(fe);
812
fail:
813 814
	mutex_unlock(&priv->lock);

815
	return ret;
816 817
}

818
static int tda18271_tune(struct dvb_frontend *fe,
819
			 struct tda18271_std_map_item *map, u32 freq, u32 bw)
820 821
{
	struct tda18271_priv *priv = fe->tuner_priv;
822
	int ret;
823

824 825
	tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
		freq, map->if_freq, bw, map->agc_mode, map->std);
826

827
	ret = tda18271_init(fe);
828
	if (tda_fail(ret))
829
		goto fail;
830 831 832

	mutex_lock(&priv->lock);

833 834
	switch (priv->id) {
	case TDA18271HDC1:
835
		tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
836 837
		break;
	case TDA18271HDC2:
838
		tda18271c2_rf_tracking_filters_correction(fe, freq);
839 840
		break;
	}
841
	ret = tda18271_channel_configuration(fe, map, freq, bw);
842 843

	mutex_unlock(&priv->lock);
844 845
fail:
	return ret;
846 847
}

848 849 850 851 852 853
/* ------------------------------------------------------------------ */

static int tda18271_set_params(struct dvb_frontend *fe,
			       struct dvb_frontend_parameters *params)
{
	struct tda18271_priv *priv = fe->tuner_priv;
854
	struct tda18271_std_map *std_map = &priv->std;
855
	struct tda18271_std_map_item *map;
856
	int ret;
857
	u32 bw, freq = params->frequency;
858 859 860 861 862 863 864

	priv->mode = TDA18271_DIGITAL;

	if (fe->ops.info.type == FE_ATSC) {
		switch (params->u.vsb.modulation) {
		case VSB_8:
		case VSB_16:
865
			map = &std_map->atsc_6;
866 867 868
			break;
		case QAM_64:
		case QAM_256:
869
			map = &std_map->qam_6;
870 871
			break;
		default:
872
			tda_warn("modulation not set!\n");
873 874
			return -EINVAL;
		}
875 876
#if 0
		/* userspace request is already center adjusted */
877
		freq += 1750000; /* Adjust to center (+1.75MHZ) */
878
#endif
879 880 881 882 883
		bw = 6000000;
	} else if (fe->ops.info.type == FE_OFDM) {
		switch (params->u.ofdm.bandwidth) {
		case BANDWIDTH_6_MHZ:
			bw = 6000000;
884
			map = &std_map->dvbt_6;
885 886 887
			break;
		case BANDWIDTH_7_MHZ:
			bw = 7000000;
888
			map = &std_map->dvbt_7;
889 890 891
			break;
		case BANDWIDTH_8_MHZ:
			bw = 8000000;
892
			map = &std_map->dvbt_8;
893 894
			break;
		default:
895
			tda_warn("bandwidth not set!\n");
896 897 898
			return -EINVAL;
		}
	} else {
899
		tda_warn("modulation type not supported!\n");
900 901 902
		return -EINVAL;
	}

903 904 905 906
	/* When tuning digital, the analog demod must be tri-stated */
	if (fe->ops.analog_ops.standby)
		fe->ops.analog_ops.standby(fe);

907
	ret = tda18271_tune(fe, map, freq, bw);
908

909
	if (tda_fail(ret))
910 911 912 913 914 915 916
		goto fail;

	priv->frequency = freq;
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
		params->u.ofdm.bandwidth : 0;
fail:
	return ret;
917 918 919 920 921 922
}

static int tda18271_set_analog_params(struct dvb_frontend *fe,
				      struct analog_parameters *params)
{
	struct tda18271_priv *priv = fe->tuner_priv;
923
	struct tda18271_std_map *std_map = &priv->std;
924
	struct tda18271_std_map_item *map;
925
	char *mode;
926
	int ret;
927
	u32 freq = params->frequency * 62500;
928 929 930

	priv->mode = TDA18271_ANALOG;

931 932
	if (params->mode == V4L2_TUNER_RADIO) {
		freq = freq / 1000;
933
		map = &std_map->fm_radio;
934 935
		mode = "fm";
	} else if (params->std & V4L2_STD_MN) {
936
		map = &std_map->atv_mn;
937 938
		mode = "MN";
	} else if (params->std & V4L2_STD_B) {
939
		map = &std_map->atv_b;
940 941
		mode = "B";
	} else if (params->std & V4L2_STD_GH) {
942
		map = &std_map->atv_gh;
943 944
		mode = "GH";
	} else if (params->std & V4L2_STD_PAL_I) {
945
		map = &std_map->atv_i;
946 947
		mode = "I";
	} else if (params->std & V4L2_STD_DK) {
948
		map = &std_map->atv_dk;
949 950
		mode = "DK";
	} else if (params->std & V4L2_STD_SECAM_L) {
951
		map = &std_map->atv_l;
952 953
		mode = "L";
	} else if (params->std & V4L2_STD_SECAM_LC) {
954
		map = &std_map->atv_lc;
955
		mode = "L'";
956
	} else {
957
		map = &std_map->atv_i;
958 959 960
		mode = "xx";
	}

961
	tda_dbg("setting tda18271 to system %s\n", mode);
962

963
	ret = tda18271_tune(fe, map, freq, 0);
964

965
	if (tda_fail(ret))
966 967 968 969 970 971
		goto fail;

	priv->frequency = freq;
	priv->bandwidth = 0;
fail:
	return ret;
972 973
}

974 975 976
static int tda18271_sleep(struct dvb_frontend *fe)
{
	struct tda18271_priv *priv = fe->tuner_priv;
977
	int ret;
978 979 980 981 982

	mutex_lock(&priv->lock);

	/* standby mode w/ slave tuner output
	 * & loop thru & xtal oscillator on */
983
	ret = tda18271_set_standby_mode(fe, 1, 0, 0);
984 985 986

	mutex_unlock(&priv->lock);

987
	return ret;
988 989
}

990 991
static int tda18271_release(struct dvb_frontend *fe)
{
992 993 994 995
	struct tda18271_priv *priv = fe->tuner_priv;

	mutex_lock(&tda18271_list_mutex);

996 997
	if (priv)
		hybrid_tuner_release_state(priv);
998 999 1000

	mutex_unlock(&tda18271_list_mutex);

1001
	fe->tuner_priv = NULL;
1002

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	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;
}

1020 1021 1022
/* ------------------------------------------------------------------ */

#define tda18271_update_std(std_cfg, name) do {				\
1023
	if (map->std_cfg.if_freq +					\
1024 1025
		map->std_cfg.agc_mode + map->std_cfg.std +		\
		map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) {	\
1026 1027 1028 1029 1030 1031
		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 {			\
1032 1033
	tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, "		\
		"if_lvl = %d, rfagc_top = 0x%02x\n",			\
1034
		name, std->std_cfg.if_freq,				\
1035 1036
		std->std_cfg.agc_mode, std->std_cfg.std,		\
		std->std_cfg.if_lvl, std->std_cfg.rfagc_top);		\
1037 1038 1039 1040 1041 1042 1043 1044
	} 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");
1045 1046 1047 1048 1049 1050 1051
	tda18271_dump_std_item(fm_radio, "  fm  ");
	tda18271_dump_std_item(atv_b,  "atv b ");
	tda18271_dump_std_item(atv_dk, "atv dk");
	tda18271_dump_std_item(atv_gh, "atv gh");
	tda18271_dump_std_item(atv_i,  "atv i ");
	tda18271_dump_std_item(atv_l,  "atv l ");
	tda18271_dump_std_item(atv_lc, "atv l'");
1052 1053 1054 1055 1056
	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");
1057 1058
	tda18271_dump_std_item(qam_6,  "qam 6 ");
	tda18271_dump_std_item(qam_8,  "qam 8 ");
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

	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;

1072
	tda18271_update_std(fm_radio, "fm");
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	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;
}

1090 1091 1092 1093 1094 1095 1096
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;

1097
	mutex_lock(&priv->lock);
1098
	tda18271_read_regs(fe);
1099
	mutex_unlock(&priv->lock);
1100 1101 1102 1103

	switch (regs[R_ID] & 0x7f) {
	case 3:
		name = "TDA18271HD/C1";
1104
		priv->id = TDA18271HDC1;
1105 1106 1107
		break;
	case 4:
		name = "TDA18271HD/C2";
1108
		priv->id = TDA18271HDC2;
1109 1110 1111 1112 1113 1114 1115
		break;
	default:
		name = "Unknown device";
		ret = -EINVAL;
		break;
	}

1116
	tda_info("%s detected @ %d-%04x%s\n", name,
1117 1118
		 i2c_adapter_id(priv->i2c_props.adap),
		 priv->i2c_props.addr,
1119 1120 1121 1122 1123
		 (0 == ret) ? "" : ", device not supported.");

	return ret;
}

1124 1125 1126 1127 1128 1129 1130
static struct dvb_tuner_ops tda18271_tuner_ops = {
	.info = {
		.name = "NXP TDA18271HD",
		.frequency_min  =  45000000,
		.frequency_max  = 864000000,
		.frequency_step =     62500
	},
1131
	.init              = tda18271_init,
1132
	.sleep             = tda18271_sleep,
1133 1134 1135 1136 1137 1138 1139 1140
	.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,
1141
				     struct i2c_adapter *i2c,
1142
				     struct tda18271_config *cfg)
1143 1144
{
	struct tda18271_priv *priv = NULL;
1145
	int instance;
1146 1147 1148

	mutex_lock(&tda18271_list_mutex);

1149 1150 1151 1152 1153 1154 1155 1156 1157
	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 */
1158
		priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1159
		priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1160 1161
		priv->cal_initialized = false;
		mutex_init(&priv->lock);
1162

1163
		fe->tuner_priv = priv;
1164

1165 1166 1167
		if (cfg)
			priv->small_i2c = cfg->small_i2c;

1168
		if (tda_fail(tda18271_get_id(fe)))
1169
			goto fail;
1170

1171
		if (tda_fail(tda18271_assign_map_layout(fe)))
1172
			goto fail;
1173

1174 1175
		mutex_lock(&priv->lock);
		tda18271_init_regs(fe);
1176 1177

		if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1178
			tda18271c2_rf_cal_init(fe);
1179

1180
		mutex_unlock(&priv->lock);
1181 1182 1183 1184 1185 1186 1187 1188 1189
		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;
1190
	}
1191

1192 1193 1194 1195
	/* override default std map with values in config struct */
	if ((cfg) && (cfg->std_map))
		tda18271_update_std_map(fe, cfg->std_map);

1196
	mutex_unlock(&tda18271_list_mutex);
1197

1198 1199
	memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
	       sizeof(struct dvb_tuner_ops));
1200

1201
	if (tda18271_debug & (DBG_MAP | DBG_ADV))
1202
		tda18271_dump_std_map(fe);
1203

1204
	return fe;
1205
fail:
1206 1207
	mutex_unlock(&tda18271_list_mutex);

1208 1209
	tda18271_release(fe);
	return NULL;
1210 1211 1212 1213 1214
}
EXPORT_SYMBOL_GPL(tda18271_attach);
MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
MODULE_LICENSE("GPL");
1215
MODULE_VERSION("0.3");
1216 1217 1218 1219 1220 1221 1222 1223

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