af9033.c 25.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Afatech AF9033 demodulator driver
 *
 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
 *
 *    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.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "af9033_priv.h"

24 25 26
/* Max transfer size done by I2C transfer functions */
#define MAX_XFER_SIZE  64

27 28 29 30 31 32 33 34
struct af9033_state {
	struct i2c_adapter *i2c;
	struct dvb_frontend fe;
	struct af9033_config cfg;

	u32 bandwidth_hz;
	bool ts_mode_parallel;
	bool ts_mode_serial;
35 36 37 38

	u32 ber;
	u32 ucb;
	unsigned long last_stat_check;
39 40 41 42 43 44 45
};

/* write multiple registers */
static int af9033_wr_regs(struct af9033_state *state, u32 reg, const u8 *val,
		int len)
{
	int ret;
46
	u8 buf[MAX_XFER_SIZE];
47 48 49 50
	struct i2c_msg msg[1] = {
		{
			.addr = state->cfg.i2c_addr,
			.flags = 0,
51
			.len = 3 + len,
52 53 54 55
			.buf = buf,
		}
	};

56 57 58 59 60 61 62
	if (3 + len > sizeof(buf)) {
		dev_warn(&state->i2c->dev,
			 "%s: i2c wr reg=%04x: len=%d is too big!\n",
			 KBUILD_MODNAME, reg, len);
		return -EINVAL;
	}

63 64 65 66 67 68 69 70 71
	buf[0] = (reg >> 16) & 0xff;
	buf[1] = (reg >>  8) & 0xff;
	buf[2] = (reg >>  0) & 0xff;
	memcpy(&buf[3], val, len);

	ret = i2c_transfer(state->i2c, msg, 1);
	if (ret == 1) {
		ret = 0;
	} else {
72 73 74
		dev_warn(&state->i2c->dev,
				"%s: i2c wr failed=%d reg=%06x len=%d\n",
				KBUILD_MODNAME, ret, reg, len);
75 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
		ret = -EREMOTEIO;
	}

	return ret;
}

/* read multiple registers */
static int af9033_rd_regs(struct af9033_state *state, u32 reg, u8 *val, int len)
{
	int ret;
	u8 buf[3] = { (reg >> 16) & 0xff, (reg >> 8) & 0xff,
			(reg >> 0) & 0xff };
	struct i2c_msg msg[2] = {
		{
			.addr = state->cfg.i2c_addr,
			.flags = 0,
			.len = sizeof(buf),
			.buf = buf
		}, {
			.addr = state->cfg.i2c_addr,
			.flags = I2C_M_RD,
			.len = len,
			.buf = val
		}
	};

	ret = i2c_transfer(state->i2c, msg, 2);
	if (ret == 2) {
		ret = 0;
	} else {
105 106 107
		dev_warn(&state->i2c->dev,
				"%s: i2c rd failed=%d reg=%06x len=%d\n",
				KBUILD_MODNAME, ret, reg, len);
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 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
		ret = -EREMOTEIO;
	}

	return ret;
}


/* write single register */
static int af9033_wr_reg(struct af9033_state *state, u32 reg, u8 val)
{
	return af9033_wr_regs(state, reg, &val, 1);
}

/* read single register */
static int af9033_rd_reg(struct af9033_state *state, u32 reg, u8 *val)
{
	return af9033_rd_regs(state, reg, val, 1);
}

/* write single register with mask */
static int af9033_wr_reg_mask(struct af9033_state *state, u32 reg, u8 val,
		u8 mask)
{
	int ret;
	u8 tmp;

	/* no need for read if whole reg is written */
	if (mask != 0xff) {
		ret = af9033_rd_regs(state, reg, &tmp, 1);
		if (ret)
			return ret;

		val &= mask;
		tmp &= ~mask;
		val |= tmp;
	}

	return af9033_wr_regs(state, reg, &val, 1);
}

/* read single register with mask */
static int af9033_rd_reg_mask(struct af9033_state *state, u32 reg, u8 *val,
		u8 mask)
{
	int ret, i;
	u8 tmp;

	ret = af9033_rd_regs(state, reg, &tmp, 1);
	if (ret)
		return ret;

	tmp &= mask;

	/* find position of the first bit */
	for (i = 0; i < 8; i++) {
		if ((mask >> i) & 0x01)
			break;
	}
	*val = tmp >> i;

	return 0;
}

171 172 173 174
/* write reg val table using reg addr auto increment */
static int af9033_wr_reg_val_tab(struct af9033_state *state,
		const struct reg_val *tab, int tab_len)
{
A
Antti Palosaari 已提交
175
#define MAX_TAB_LEN 212
176
	int ret, i, j;
A
Antti Palosaari 已提交
177 178 179
	u8 buf[1 + MAX_TAB_LEN];

	dev_dbg(&state->i2c->dev, "%s: tab_len=%d\n", __func__, tab_len);
180 181

	if (tab_len > sizeof(buf)) {
A
Antti Palosaari 已提交
182 183
		dev_warn(&state->i2c->dev, "%s: tab len %d is too big\n",
				KBUILD_MODNAME, tab_len);
184 185
		return -EINVAL;
	}
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

	for (i = 0, j = 0; i < tab_len; i++) {
		buf[j] = tab[i].val;

		if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
			ret = af9033_wr_regs(state, tab[i].reg - j, buf, j + 1);
			if (ret < 0)
				goto err;

			j = 0;
		} else {
			j++;
		}
	}

	return 0;

err:
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}

209
static u32 af9033_div(struct af9033_state *state, u32 a, u32 b, u32 x)
210 211 212
{
	u32 r = 0, c = 0, i;

213
	dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d\n", __func__, a, b, x);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	if (a > b) {
		c = a / b;
		a = a - c * b;
	}

	for (i = 0; i < x; i++) {
		if (a >= b) {
			r += 1;
			a -= b;
		}
		a <<= 1;
		r <<= 1;
	}
	r = (c << (u32)x) + r;

230 231
	dev_dbg(&state->i2c->dev, "%s: a=%d b=%d x=%d r=%d r=%x\n",
			__func__, a, b, x, r, r);
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

	return r;
}

static void af9033_release(struct dvb_frontend *fe)
{
	struct af9033_state *state = fe->demodulator_priv;

	kfree(state);
}

static int af9033_init(struct dvb_frontend *fe)
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret, i, len;
	const struct reg_val *init;
	u8 buf[4];
	u32 adc_cw, clock_cw;
	struct reg_val_mask tab[] = {
		{ 0x80fb24, 0x00, 0x08 },
		{ 0x80004c, 0x00, 0xff },
		{ 0x00f641, state->cfg.tuner, 0xff },
		{ 0x80f5ca, 0x01, 0x01 },
		{ 0x80f715, 0x01, 0x01 },
		{ 0x00f41f, 0x04, 0x04 },
		{ 0x00f41a, 0x01, 0x01 },
		{ 0x80f731, 0x00, 0x01 },
		{ 0x00d91e, 0x00, 0x01 },
		{ 0x00d919, 0x00, 0x01 },
		{ 0x80f732, 0x00, 0x01 },
		{ 0x00d91f, 0x00, 0x01 },
		{ 0x00d91a, 0x00, 0x01 },
		{ 0x80f730, 0x00, 0x01 },
		{ 0x80f778, 0x00, 0xff },
		{ 0x80f73c, 0x01, 0x01 },
		{ 0x80f776, 0x00, 0x01 },
		{ 0x00d8fd, 0x01, 0xff },
		{ 0x00d830, 0x01, 0xff },
		{ 0x00d831, 0x00, 0xff },
		{ 0x00d832, 0x00, 0xff },
		{ 0x80f985, state->ts_mode_serial, 0x01 },
		{ 0x80f986, state->ts_mode_parallel, 0x01 },
		{ 0x00d827, 0x00, 0xff },
		{ 0x00d829, 0x00, 0xff },
276
		{ 0x800045, state->cfg.adc_multiplier, 0xff },
277 278 279
	};

	/* program clock control */
280
	clock_cw = af9033_div(state, state->cfg.clock, 1000000ul, 19ul);
281 282 283 284 285
	buf[0] = (clock_cw >>  0) & 0xff;
	buf[1] = (clock_cw >>  8) & 0xff;
	buf[2] = (clock_cw >> 16) & 0xff;
	buf[3] = (clock_cw >> 24) & 0xff;

286 287
	dev_dbg(&state->i2c->dev, "%s: clock=%d clock_cw=%08x\n",
			__func__, state->cfg.clock, clock_cw);
288 289 290 291 292 293 294 295 296 297 298

	ret = af9033_wr_regs(state, 0x800025, buf, 4);
	if (ret < 0)
		goto err;

	/* program ADC control */
	for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
		if (clock_adc_lut[i].clock == state->cfg.clock)
			break;
	}

299
	adc_cw = af9033_div(state, clock_adc_lut[i].adc, 1000000ul, 19ul);
300 301 302 303
	buf[0] = (adc_cw >>  0) & 0xff;
	buf[1] = (adc_cw >>  8) & 0xff;
	buf[2] = (adc_cw >> 16) & 0xff;

304 305
	dev_dbg(&state->i2c->dev, "%s: adc=%d adc_cw=%06x\n",
			__func__, clock_adc_lut[i].adc, adc_cw);
306 307 308 309 310 311 312 313 314 315 316 317 318

	ret = af9033_wr_regs(state, 0x80f1cd, buf, 3);
	if (ret < 0)
		goto err;

	/* program register table */
	for (i = 0; i < ARRAY_SIZE(tab); i++) {
		ret = af9033_wr_reg_mask(state, tab[i].reg, tab[i].val,
				tab[i].mask);
		if (ret < 0)
			goto err;
	}

319 320
	/* clock output */
	if (state->cfg.dyn0_clk) {
321 322 323 324 325
		ret = af9033_wr_reg(state, 0x80fba8, 0x00);
		if (ret < 0)
			goto err;
	}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	/* settings for TS interface */
	if (state->cfg.ts_mode == AF9033_TS_MODE_USB) {
		ret = af9033_wr_reg_mask(state, 0x80f9a5, 0x00, 0x01);
		if (ret < 0)
			goto err;

		ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x01, 0x01);
		if (ret < 0)
			goto err;
	} else {
		ret = af9033_wr_reg_mask(state, 0x80f990, 0x00, 0x01);
		if (ret < 0)
			goto err;

		ret = af9033_wr_reg_mask(state, 0x80f9b5, 0x00, 0x01);
		if (ret < 0)
			goto err;
	}

	/* load OFSM settings */
346
	dev_dbg(&state->i2c->dev, "%s: load ofsm settings\n", __func__);
347 348 349 350
	switch (state->cfg.tuner) {
	case AF9033_TUNER_IT9135_38:
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
351 352 353
		len = ARRAY_SIZE(ofsm_init_it9135_v1);
		init = ofsm_init_it9135_v1;
		break;
354 355 356
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
357 358
		len = ARRAY_SIZE(ofsm_init_it9135_v2);
		init = ofsm_init_it9135_v2;
359 360 361 362 363 364 365
		break;
	default:
		len = ARRAY_SIZE(ofsm_init);
		init = ofsm_init;
		break;
	}

366 367 368
	ret = af9033_wr_reg_val_tab(state, init, len);
	if (ret < 0)
		goto err;
369 370

	/* load tuner specific settings */
371
	dev_dbg(&state->i2c->dev, "%s: load tuner specific settings\n",
372 373 374 375 376 377
			__func__);
	switch (state->cfg.tuner) {
	case AF9033_TUNER_TUA9001:
		len = ARRAY_SIZE(tuner_init_tua9001);
		init = tuner_init_tua9001;
		break;
378 379 380 381
	case AF9033_TUNER_FC0011:
		len = ARRAY_SIZE(tuner_init_fc0011);
		init = tuner_init_fc0011;
		break;
382 383 384 385
	case AF9033_TUNER_MXL5007T:
		len = ARRAY_SIZE(tuner_init_mxl5007t);
		init = tuner_init_mxl5007t;
		break;
386 387 388 389
	case AF9033_TUNER_TDA18218:
		len = ARRAY_SIZE(tuner_init_tda18218);
		init = tuner_init_tda18218;
		break;
390 391 392 393
	case AF9033_TUNER_FC2580:
		len = ARRAY_SIZE(tuner_init_fc2580);
		init = tuner_init_fc2580;
		break;
394 395 396 397
	case AF9033_TUNER_FC0012:
		len = ARRAY_SIZE(tuner_init_fc0012);
		init = tuner_init_fc0012;
		break;
398
	case AF9033_TUNER_IT9135_38:
399 400 401
		len = ARRAY_SIZE(tuner_init_it9135_38);
		init = tuner_init_it9135_38;
		break;
402
	case AF9033_TUNER_IT9135_51:
403 404 405
		len = ARRAY_SIZE(tuner_init_it9135_51);
		init = tuner_init_it9135_51;
		break;
406
	case AF9033_TUNER_IT9135_52:
407 408 409
		len = ARRAY_SIZE(tuner_init_it9135_52);
		init = tuner_init_it9135_52;
		break;
410
	case AF9033_TUNER_IT9135_60:
411 412 413
		len = ARRAY_SIZE(tuner_init_it9135_60);
		init = tuner_init_it9135_60;
		break;
414
	case AF9033_TUNER_IT9135_61:
415 416 417
		len = ARRAY_SIZE(tuner_init_it9135_61);
		init = tuner_init_it9135_61;
		break;
418
	case AF9033_TUNER_IT9135_62:
419 420
		len = ARRAY_SIZE(tuner_init_it9135_62);
		init = tuner_init_it9135_62;
421
		break;
422
	default:
423 424
		dev_dbg(&state->i2c->dev, "%s: unsupported tuner ID=%d\n",
				__func__, state->cfg.tuner);
425 426 427 428
		ret = -ENODEV;
		goto err;
	}

429 430 431
	ret = af9033_wr_reg_val_tab(state, init, len);
	if (ret < 0)
		goto err;
432

433 434 435 436
	if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
		ret = af9033_wr_reg_mask(state, 0x00d91c, 0x01, 0x01);
		if (ret < 0)
			goto err;
437

438 439 440
		ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
		if (ret < 0)
			goto err;
441

442 443 444 445 446
		ret = af9033_wr_reg_mask(state, 0x00d916, 0x00, 0x01);
		if (ret < 0)
			goto err;
	}

447 448 449 450 451 452 453 454 455
	switch (state->cfg.tuner) {
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
		ret = af9033_wr_reg(state, 0x800000, 0x01);
		if (ret < 0)
			goto err;
	}

456 457 458 459 460
	state->bandwidth_hz = 0; /* force to program all parameters */

	return 0;

err:
461
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
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

	return ret;
}

static int af9033_sleep(struct dvb_frontend *fe)
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret, i;
	u8 tmp;

	ret = af9033_wr_reg(state, 0x80004c, 1);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg(state, 0x800000, 0);
	if (ret < 0)
		goto err;

	for (i = 100, tmp = 1; i && tmp; i--) {
		ret = af9033_rd_reg(state, 0x80004c, &tmp);
		if (ret < 0)
			goto err;

		usleep_range(200, 10000);
	}

488
	dev_dbg(&state->i2c->dev, "%s: loop=%d\n", __func__, i);
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513

	if (i == 0) {
		ret = -ETIMEDOUT;
		goto err;
	}

	ret = af9033_wr_reg_mask(state, 0x80fb24, 0x08, 0x08);
	if (ret < 0)
		goto err;

	/* prevent current leak (?) */
	if (state->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
		/* enable parallel TS */
		ret = af9033_wr_reg_mask(state, 0x00d917, 0x00, 0x01);
		if (ret < 0)
			goto err;

		ret = af9033_wr_reg_mask(state, 0x00d916, 0x01, 0x01);
		if (ret < 0)
			goto err;
	}

	return 0;

err:
514
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
515 516 517 518 519 520 521

	return ret;
}

static int af9033_get_tune_settings(struct dvb_frontend *fe,
		struct dvb_frontend_tune_settings *fesettings)
{
522 523
	/* 800 => 2000 because IT9135 v2 is slow to gain lock */
	fesettings->min_delay_ms = 2000;
524 525 526 527 528 529 530 531 532 533
	fesettings->step_size = 0;
	fesettings->max_drift = 0;

	return 0;
}

static int af9033_set_frontend(struct dvb_frontend *fe)
{
	struct af9033_state *state = fe->demodulator_priv;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
534
	int ret, i, spec_inv, sampling_freq;
535
	u8 tmp, buf[3], bandwidth_reg_val;
536
	u32 if_frequency, freq_cw, adc_freq;
537

538 539
	dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
			__func__, c->frequency, c->bandwidth_hz);
540 541 542 543 544 545 546 547 548 549 550 551 552

	/* check bandwidth */
	switch (c->bandwidth_hz) {
	case 6000000:
		bandwidth_reg_val = 0x00;
		break;
	case 7000000:
		bandwidth_reg_val = 0x01;
		break;
	case 8000000:
		bandwidth_reg_val = 0x02;
		break;
	default:
553 554
		dev_dbg(&state->i2c->dev, "%s: invalid bandwidth_hz\n",
				__func__);
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
		ret = -EINVAL;
		goto err;
	}

	/* program tuner */
	if (fe->ops.tuner_ops.set_params)
		fe->ops.tuner_ops.set_params(fe);

	/* program CFOE coefficients */
	if (c->bandwidth_hz != state->bandwidth_hz) {
		for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
			if (coeff_lut[i].clock == state->cfg.clock &&
				coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
				break;
			}
		}
		ret =  af9033_wr_regs(state, 0x800001,
				coeff_lut[i].val, sizeof(coeff_lut[i].val));
	}

	/* program frequency control */
	if (c->bandwidth_hz != state->bandwidth_hz) {
577 578 579 580 581 582 583 584
		spec_inv = state->cfg.spec_inv ? -1 : 1;

		for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
			if (clock_adc_lut[i].clock == state->cfg.clock)
				break;
		}
		adc_freq = clock_adc_lut[i].adc;

585 586 587 588 589 590
		/* get used IF frequency */
		if (fe->ops.tuner_ops.get_if_frequency)
			fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
		else
			if_frequency = 0;

591
		sampling_freq = if_frequency;
592

593 594 595 596
		while (sampling_freq > (adc_freq / 2))
			sampling_freq -= adc_freq;

		if (sampling_freq >= 0)
597 598
			spec_inv *= -1;
		else
599
			sampling_freq *= -1;
600

601
		freq_cw = af9033_div(state, sampling_freq, adc_freq, 23ul);
602 603

		if (spec_inv == -1)
604
			freq_cw = 0x800000 - freq_cw;
605

606
		if (state->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
607 608
			freq_cw /= 2;

609 610 611
		buf[0] = (freq_cw >>  0) & 0xff;
		buf[1] = (freq_cw >>  8) & 0xff;
		buf[2] = (freq_cw >> 16) & 0x7f;
612 613 614 615 616

		/* FIXME: there seems to be calculation error here... */
		if (if_frequency == 0)
			buf[2] = 0;

617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
		ret = af9033_wr_regs(state, 0x800029, buf, 3);
		if (ret < 0)
			goto err;

		state->bandwidth_hz = c->bandwidth_hz;
	}

	ret = af9033_wr_reg_mask(state, 0x80f904, bandwidth_reg_val, 0x03);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg(state, 0x800040, 0x00);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg(state, 0x800047, 0x00);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg_mask(state, 0x80f999, 0x00, 0x01);
	if (ret < 0)
		goto err;

	if (c->frequency <= 230000000)
		tmp = 0x00; /* VHF */
	else
		tmp = 0x01; /* UHF */

	ret = af9033_wr_reg(state, 0x80004b, tmp);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg(state, 0x800000, 0x00);
	if (ret < 0)
		goto err;

	return 0;

err:
656
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
657 658 659 660

	return ret;
}

661 662 663
static int af9033_get_frontend(struct dvb_frontend *fe)
{
	struct af9033_state *state = fe->demodulator_priv;
664
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
665 666 667
	int ret;
	u8 buf[8];

668
	dev_dbg(&state->i2c->dev, "%s:\n", __func__);
669 670 671

	/* read all needed registers */
	ret = af9033_rd_regs(state, 0x80f900, buf, sizeof(buf));
672 673
	if (ret < 0)
		goto err;
674 675 676

	switch ((buf[0] >> 0) & 3) {
	case 0:
677
		c->transmission_mode = TRANSMISSION_MODE_2K;
678 679
		break;
	case 1:
680
		c->transmission_mode = TRANSMISSION_MODE_8K;
681 682 683 684 685
		break;
	}

	switch ((buf[1] >> 0) & 3) {
	case 0:
686
		c->guard_interval = GUARD_INTERVAL_1_32;
687 688
		break;
	case 1:
689
		c->guard_interval = GUARD_INTERVAL_1_16;
690 691
		break;
	case 2:
692
		c->guard_interval = GUARD_INTERVAL_1_8;
693 694
		break;
	case 3:
695
		c->guard_interval = GUARD_INTERVAL_1_4;
696 697 698 699 700
		break;
	}

	switch ((buf[2] >> 0) & 7) {
	case 0:
701
		c->hierarchy = HIERARCHY_NONE;
702 703
		break;
	case 1:
704
		c->hierarchy = HIERARCHY_1;
705 706
		break;
	case 2:
707
		c->hierarchy = HIERARCHY_2;
708 709
		break;
	case 3:
710
		c->hierarchy = HIERARCHY_4;
711 712 713 714 715
		break;
	}

	switch ((buf[3] >> 0) & 3) {
	case 0:
716
		c->modulation = QPSK;
717 718
		break;
	case 1:
719
		c->modulation = QAM_16;
720 721
		break;
	case 2:
722
		c->modulation = QAM_64;
723 724 725 726 727
		break;
	}

	switch ((buf[4] >> 0) & 3) {
	case 0:
728
		c->bandwidth_hz = 6000000;
729 730
		break;
	case 1:
731
		c->bandwidth_hz = 7000000;
732 733
		break;
	case 2:
734
		c->bandwidth_hz = 8000000;
735 736 737 738 739
		break;
	}

	switch ((buf[6] >> 0) & 7) {
	case 0:
740
		c->code_rate_HP = FEC_1_2;
741 742
		break;
	case 1:
743
		c->code_rate_HP = FEC_2_3;
744 745
		break;
	case 2:
746
		c->code_rate_HP = FEC_3_4;
747 748
		break;
	case 3:
749
		c->code_rate_HP = FEC_5_6;
750 751
		break;
	case 4:
752
		c->code_rate_HP = FEC_7_8;
753 754
		break;
	case 5:
755
		c->code_rate_HP = FEC_NONE;
756 757 758 759 760
		break;
	}

	switch ((buf[7] >> 0) & 7) {
	case 0:
761
		c->code_rate_LP = FEC_1_2;
762 763
		break;
	case 1:
764
		c->code_rate_LP = FEC_2_3;
765 766
		break;
	case 2:
767
		c->code_rate_LP = FEC_3_4;
768 769
		break;
	case 3:
770
		c->code_rate_LP = FEC_5_6;
771 772
		break;
	case 4:
773
		c->code_rate_LP = FEC_7_8;
774 775
		break;
	case 5:
776
		c->code_rate_LP = FEC_NONE;
777 778 779
		break;
	}

780
	return 0;
781

782
err:
783
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
784 785 786 787

	return ret;
}

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
static int af9033_read_status(struct dvb_frontend *fe, fe_status_t *status)
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret;
	u8 tmp;

	*status = 0;

	/* radio channel status, 0=no result, 1=has signal, 2=no signal */
	ret = af9033_rd_reg(state, 0x800047, &tmp);
	if (ret < 0)
		goto err;

	/* has signal */
	if (tmp == 0x01)
		*status |= FE_HAS_SIGNAL;

	if (tmp != 0x02) {
		/* TPS lock */
		ret = af9033_rd_reg_mask(state, 0x80f5a9, &tmp, 0x01);
		if (ret < 0)
			goto err;

		if (tmp)
			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
					FE_HAS_VITERBI;

		/* full lock */
		ret = af9033_rd_reg_mask(state, 0x80f999, &tmp, 0x01);
		if (ret < 0)
			goto err;

		if (tmp)
			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
					FE_HAS_VITERBI | FE_HAS_SYNC |
					FE_HAS_LOCK;
	}

	return 0;

err:
829
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
830 831 832 833 834 835

	return ret;
}

static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
{
836 837 838 839
	struct af9033_state *state = fe->demodulator_priv;
	int ret, i, len;
	u8 buf[3], tmp;
	u32 snr_val;
840
	const struct val_snr *snr_lut;
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878

	/* read value */
	ret = af9033_rd_regs(state, 0x80002c, buf, 3);
	if (ret < 0)
		goto err;

	snr_val = (buf[2] << 16) | (buf[1] << 8) | buf[0];

	/* read current modulation */
	ret = af9033_rd_reg(state, 0x80f903, &tmp);
	if (ret < 0)
		goto err;

	switch ((tmp >> 0) & 3) {
	case 0:
		len = ARRAY_SIZE(qpsk_snr_lut);
		snr_lut = qpsk_snr_lut;
		break;
	case 1:
		len = ARRAY_SIZE(qam16_snr_lut);
		snr_lut = qam16_snr_lut;
		break;
	case 2:
		len = ARRAY_SIZE(qam64_snr_lut);
		snr_lut = qam64_snr_lut;
		break;
	default:
		goto err;
	}

	for (i = 0; i < len; i++) {
		tmp = snr_lut[i].snr;

		if (snr_val < snr_lut[i].val)
			break;
	}

	*snr = tmp * 10; /* dB/10 */
879 880

	return 0;
881 882

err:
883
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
884 885

	return ret;
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
}

static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret;
	u8 strength2;

	/* read signal strength of 0-100 scale */
	ret = af9033_rd_reg(state, 0x800048, &strength2);
	if (ret < 0)
		goto err;

	/* scale value to 0x0000-0xffff */
	*strength = strength2 * 0xffff / 100;

	return 0;

err:
905
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
906 907 908 909

	return ret;
}

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
static int af9033_update_ch_stat(struct af9033_state *state)
{
	int ret = 0;
	u32 err_cnt, bit_cnt;
	u16 abort_cnt;
	u8 buf[7];

	/* only update data every half second */
	if (time_after(jiffies, state->last_stat_check + msecs_to_jiffies(500))) {
		ret = af9033_rd_regs(state, 0x800032, buf, sizeof(buf));
		if (ret < 0)
			goto err;
		/* in 8 byte packets? */
		abort_cnt = (buf[1] << 8) + buf[0];
		/* in bits */
		err_cnt = (buf[4] << 16) + (buf[3] << 8) + buf[2];
		/* in 8 byte packets? always(?) 0x2710 = 10000 */
		bit_cnt = (buf[6] << 8) + buf[5];

		if (bit_cnt < abort_cnt) {
			abort_cnt = 1000;
			state->ber = 0xffffffff;
		} else {
933 934 935
			/*
			 * 8 byte packets, that have not been rejected already
			 */
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
			bit_cnt -= (u32)abort_cnt;
			if (bit_cnt == 0) {
				state->ber = 0xffffffff;
			} else {
				err_cnt -= (u32)abort_cnt * 8 * 8;
				bit_cnt *= 8 * 8;
				state->ber = err_cnt * (0xffffffff / bit_cnt);
			}
		}
		state->ucb += abort_cnt;
		state->last_stat_check = jiffies;
	}

	return 0;
err:
951 952
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);

953 954 955
	return ret;
}

956 957
static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
{
958 959 960 961 962 963 964 965
	struct af9033_state *state = fe->demodulator_priv;
	int ret;

	ret = af9033_update_ch_stat(state);
	if (ret < 0)
		return ret;

	*ber = state->ber;
966 967 968 969 970 971

	return 0;
}

static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{
972 973 974 975 976 977 978 979
	struct af9033_state *state = fe->demodulator_priv;
	int ret;

	ret = af9033_update_ch_stat(state);
	if (ret < 0)
		return ret;

	*ucblocks = state->ucb;
980 981 982 983 984 985 986 987 988

	return 0;
}

static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret;

989
	dev_dbg(&state->i2c->dev, "%s: enable=%d\n", __func__, enable);
990 991 992 993 994 995 996 997

	ret = af9033_wr_reg_mask(state, 0x00fa04, enable, 0x01);
	if (ret < 0)
		goto err;

	return 0;

err:
998
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
999 1000 1001 1002

	return ret;
}

1003
static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret;

	dev_dbg(&state->i2c->dev, "%s: onoff=%d\n", __func__, onoff);

	ret = af9033_wr_reg_mask(state, 0x80f993, onoff, 0x01);
	if (ret < 0)
		goto err;

	return 0;

err:
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}

1022 1023
static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
		int onoff)
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
{
	struct af9033_state *state = fe->demodulator_priv;
	int ret;
	u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};

	dev_dbg(&state->i2c->dev, "%s: index=%d pid=%04x onoff=%d\n",
			__func__, index, pid, onoff);

	if (pid > 0x1fff)
		return 0;

	ret = af9033_wr_regs(state, 0x80f996, wbuf, 2);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg(state, 0x80f994, onoff);
	if (ret < 0)
		goto err;

	ret = af9033_wr_reg(state, 0x80f995, index);
	if (ret < 0)
		goto err;

	return 0;

err:
	dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);

	return ret;
}

1055 1056 1057
static struct dvb_frontend_ops af9033_ops;

struct dvb_frontend *af9033_attach(const struct af9033_config *config,
1058 1059
				   struct i2c_adapter *i2c,
				   struct af9033_ops *ops)
1060 1061 1062 1063
{
	int ret;
	struct af9033_state *state;
	u8 buf[8];
1064
	u32 reg;
1065

1066
	dev_dbg(&i2c->dev, "%s:\n", __func__);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076

	/* allocate memory for the internal state */
	state = kzalloc(sizeof(struct af9033_state), GFP_KERNEL);
	if (state == NULL)
		goto err;

	/* setup the state */
	state->i2c = i2c;
	memcpy(&state->cfg, config, sizeof(struct af9033_config));

1077
	if (state->cfg.clock != 12000000) {
1078 1079
		dev_err(&state->i2c->dev,
				"%s: af9033: unsupported clock=%d, only 12000000 Hz is supported currently\n",
1080
				KBUILD_MODNAME, state->cfg.clock);
1081 1082 1083
		goto err;
	}

1084
	/* firmware version */
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	switch (state->cfg.tuner) {
	case AF9033_TUNER_IT9135_38:
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
		reg = 0x004bfc;
		break;
	default:
		reg = 0x0083e9;
		break;
	}

	ret = af9033_rd_regs(state, reg, &buf[0], 4);
1100 1101 1102 1103 1104 1105 1106
	if (ret < 0)
		goto err;

	ret = af9033_rd_regs(state, 0x804191, &buf[4], 4);
	if (ret < 0)
		goto err;

1107 1108 1109 1110
	dev_info(&state->i2c->dev,
			"%s: firmware version: LINK=%d.%d.%d.%d OFDM=%d.%d.%d.%d\n",
			KBUILD_MODNAME, buf[0], buf[1], buf[2], buf[3], buf[4],
			buf[5], buf[6], buf[7]);
1111

1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	/* sleep */
	switch (state->cfg.tuner) {
	case AF9033_TUNER_IT9135_38:
	case AF9033_TUNER_IT9135_51:
	case AF9033_TUNER_IT9135_52:
	case AF9033_TUNER_IT9135_60:
	case AF9033_TUNER_IT9135_61:
	case AF9033_TUNER_IT9135_62:
		/* IT9135 did not like to sleep at that early */
		break;
	default:
1123 1124 1125 1126 1127 1128 1129 1130
		ret = af9033_wr_reg(state, 0x80004c, 1);
		if (ret < 0)
			goto err;

		ret = af9033_wr_reg(state, 0x800000, 0);
		if (ret < 0)
			goto err;
	}
1131

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	/* configure internal TS mode */
	switch (state->cfg.ts_mode) {
	case AF9033_TS_MODE_PARALLEL:
		state->ts_mode_parallel = true;
		break;
	case AF9033_TS_MODE_SERIAL:
		state->ts_mode_serial = true;
		break;
	case AF9033_TS_MODE_USB:
		/* usb mode for AF9035 */
	default:
		break;
	}

	/* create dvb_frontend */
	memcpy(&state->fe.ops, &af9033_ops, sizeof(struct dvb_frontend_ops));
	state->fe.demodulator_priv = state;

1150 1151 1152 1153 1154
	if (ops) {
		ops->pid_filter = af9033_pid_filter;
		ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
	}

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	return &state->fe;

err:
	kfree(state);
	return NULL;
}
EXPORT_SYMBOL(af9033_attach);

static struct dvb_frontend_ops af9033_ops = {
	.delsys = { SYS_DVBT },
	.info = {
		.name = "Afatech AF9033 (DVB-T)",
		.frequency_min = 174000000,
		.frequency_max = 862000000,
		.frequency_stepsize = 250000,
		.frequency_tolerance = 0,
		.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_FEC_AUTO |
			FE_CAN_QPSK |
			FE_CAN_QAM_16 |
			FE_CAN_QAM_64 |
			FE_CAN_QAM_AUTO |
			FE_CAN_TRANSMISSION_MODE_AUTO |
			FE_CAN_GUARD_INTERVAL_AUTO |
			FE_CAN_HIERARCHY_AUTO |
			FE_CAN_RECOVER |
			FE_CAN_MUTE_TS
	},

	.release = af9033_release,

	.init = af9033_init,
	.sleep = af9033_sleep,

	.get_tune_settings = af9033_get_tune_settings,
	.set_frontend = af9033_set_frontend,
1195
	.get_frontend = af9033_get_frontend,
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

	.read_status = af9033_read_status,
	.read_snr = af9033_read_snr,
	.read_signal_strength = af9033_read_signal_strength,
	.read_ber = af9033_read_ber,
	.read_ucblocks = af9033_read_ucblocks,

	.i2c_gate_ctrl = af9033_i2c_gate_ctrl,
};

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
MODULE_LICENSE("GPL");