it913x.c 10.8 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 24
/*
 * ITE Tech IT9137 silicon tuner driver
 *
 *  Copyright (C) 2011 Malcolm Priestley (tvboxspy@gmail.com)
 *  IT9137 Copyright (C) ITE Tech Inc.
 *
 *  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 "it913x_priv.h"

25
struct it913x_state {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	struct dvb_frontend frontend;
	struct i2c_adapter *i2c_adap;
	struct ite_config *config;
	u8 i2c_addr;
	u32 frequency;
	fe_modulation_t constellation;
	fe_transmit_mode_t transmission_mode;
	u8 priority;
	u32 crystalFrequency;
	u32 adcFrequency;
	u8 tuner_type;
	struct adctable *table;
	fe_status_t it913x_status;
	u16 tun_xtal;
	u8 tun_fdiv;
	u8 tun_clk_mode;
	u32 tun_fn_min;
	u32 ucblocks;
};

46 47
/* read multiple registers */
static int it913x_rd_regs(struct it913x_state *state,
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
		u32 reg, u8 *data, u8 count)
{
	int ret;
	u8 b[3];
	struct i2c_msg msg[2] = {
		{ .addr = state->i2c_addr, .flags = 0,
			.buf = b, .len = sizeof(b) },
		{ .addr = state->i2c_addr, .flags = I2C_M_RD,
			.buf = data, .len = count }
	};
	b[0] = (u8)(reg >> 16) & 0xff;
	b[1] = (u8)(reg >> 8) & 0xff;
	b[2] = (u8) reg & 0xff;
	b[0] |= 0x80; /* All reads from demodulator */

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

	return ret;
}

68 69
/* read single register */
static int it913x_rd_reg(struct it913x_state *state, u32 reg)
70 71 72
{
	int ret;
	u8 b[1];
73
	ret = it913x_rd_regs(state, reg, &b[0], sizeof(b));
74 75 76
	return (ret < 0) ? -ENODEV : b[0];
}

77 78
/* write multiple registers */
static int it913x_wr_regs(struct it913x_state *state,
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		u8 pro, u32 reg, u8 buf[], u8 count)
{
	u8 b[256];
	struct i2c_msg msg[1] = {
		{ .addr = state->i2c_addr, .flags = 0,
		  .buf = b, .len = 3 + count }
	};
	int ret;
	b[0] = (u8)(reg >> 16) & 0xff;
	b[1] = (u8)(reg >> 8) & 0xff;
	b[2] = (u8) reg & 0xff;
	memcpy(&b[3], buf, count);

	if (pro == PRO_DMOD)
		b[0] |= 0x80;

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

	if (ret < 0)
		return -EIO;

	return 0;
}

103 104
/* write single register */
static int it913x_wr_reg(struct it913x_state *state,
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
		u8 pro, u32 reg, u32 data)
{
	int ret;
	u8 b[4];
	u8 s;

	b[0] = data >> 24;
	b[1] = (data >> 16) & 0xff;
	b[2] = (data >> 8) & 0xff;
	b[3] = data & 0xff;
	/* expand write as needed */
	if (data < 0x100)
		s = 3;
	else if (data < 0x1000)
		s = 2;
	else if (data < 0x100000)
		s = 1;
	else
		s = 0;

125
	ret = it913x_wr_regs(state, pro, reg, &b[s], sizeof(b) - s);
126 127 128 129

	return ret;
}

130
static int it913x_script_loader(struct it913x_state *state,
131 132 133 134 135 136 137 138 139
		struct it913xset *loadscript)
{
	int ret, i;
	if (loadscript == NULL)
		return -EINVAL;

	for (i = 0; i < 1000; ++i) {
		if (loadscript[i].pro == 0xff)
			break;
140
		ret = it913x_wr_regs(state, loadscript[i].pro,
141 142 143 144 145 146 147 148
			loadscript[i].address,
			loadscript[i].reg, loadscript[i].count);
		if (ret < 0)
			return -ENODEV;
	}
	return 0;
}

149
static int it913x_init(struct dvb_frontend *fe)
150
{
151
	struct it913x_state *state = fe->tuner_priv;
152
	int ret, i, reg;
153
	struct it913xset *set_lna;
154 155 156 157
	u8 val, nv_val;
	u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
	u8 b[2];

158 159
	/* v1 or v2 tuner script */
	if (state->config->chip_ver > 1)
160
		ret = it913x_script_loader(state, it9135_v2);
161
	else
162
		ret = it913x_script_loader(state, it9135_v1);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	if (ret < 0)
		return ret;

	/* LNA Scripts */
	switch (state->tuner_type) {
	case IT9135_51:
		set_lna = it9135_51;
		break;
	case IT9135_52:
		set_lna = it9135_52;
		break;
	case IT9135_60:
		set_lna = it9135_60;
		break;
	case IT9135_61:
		set_lna = it9135_61;
		break;
	case IT9135_62:
		set_lna = it9135_62;
		break;
	case IT9135_38:
	default:
		set_lna = it9135_38;
	}
	pr_info("Tuner LNA type :%02x\n", state->tuner_type);

189
	ret = it913x_script_loader(state, set_lna);
190 191 192 193
	if (ret < 0)
		return ret;

	if (state->config->chip_ver == 2) {
194
		ret = it913x_wr_reg(state, PRO_DMOD, TRIGGER_OFSM, 0x1);
195 196
		if (ret < 0)
			return -ENODEV;
197 198
	}

199
	reg = it913x_rd_reg(state, 0xec86);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	switch (reg) {
	case 0:
		state->tun_clk_mode = reg;
		state->tun_xtal = 2000;
		state->tun_fdiv = 3;
		val = 16;
		break;
	case -ENODEV:
		return -ENODEV;
	case 1:
	default:
		state->tun_clk_mode = reg;
		state->tun_xtal = 640;
		state->tun_fdiv = 1;
		val = 6;
		break;
	}

218
	reg = it913x_rd_reg(state, 0xed03);
219 220 221 222 223 224 225 226 227

	if (reg < 0)
		return -ENODEV;
	else if (reg < ARRAY_SIZE(nv))
		nv_val = nv[reg];
	else
		nv_val = 2;

	for (i = 0; i < 50; i++) {
228
		ret = it913x_rd_regs(state, 0xed23, &b[0], sizeof(b));
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
		reg = (b[1] << 8) + b[0];
		if (reg > 0)
			break;
		if (ret < 0)
			return -ENODEV;
		udelay(2000);
	}
	state->tun_fn_min = state->tun_xtal * reg;
	state->tun_fn_min /= (state->tun_fdiv * nv_val);
	pr_debug("Tuner fn_min %d\n", state->tun_fn_min);

	if (state->config->chip_ver > 1)
		msleep(50);
	else {
		for (i = 0; i < 50; i++) {
244
			reg = it913x_rd_reg(state, 0xec82);
245 246 247 248 249 250 251 252
			if (reg > 0)
				break;
			if (reg < 0)
				return -ENODEV;
			udelay(2000);
		}
	}

253 254 255 256 257 258
	/* Power Up Tuner - common all versions */
	ret = it913x_wr_reg(state, PRO_DMOD, 0xec40, 0x1);
	ret |= it913x_wr_reg(state, PRO_DMOD, 0xfba8, 0x0);
	ret |= it913x_wr_reg(state, PRO_DMOD, 0xec57, 0x0);
	ret |= it913x_wr_reg(state, PRO_DMOD, 0xec58, 0x0);

259
	return it913x_wr_reg(state, PRO_DMOD, 0xed81, val);
260 261
}

262
static int it9137_set_params(struct dvb_frontend *fe)
263
{
264
	struct it913x_state *state = fe->tuner_priv;
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 302 303 304 305 306 307 308 309 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 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
	struct it913xset *set_tuner = set_it9137_template;
	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
	u32 bandwidth = p->bandwidth_hz;
	u32 frequency_m = p->frequency;
	int ret, reg;
	u32 frequency = frequency_m / 1000;
	u32 freq, temp_f, tmp;
	u16 iqik_m_cal;
	u16 n_div;
	u8 n;
	u8 l_band;
	u8 lna_band;
	u8 bw;

	if (state->config->firmware_ver == 1)
		set_tuner = set_it9135_template;
	else
		set_tuner = set_it9137_template;

	pr_debug("Tuner Frequency %d Bandwidth %d\n", frequency, bandwidth);

	if (frequency >= 51000 && frequency <= 440000) {
		l_band = 0;
		lna_band = 0;
	} else if (frequency > 440000 && frequency <= 484000) {
		l_band = 1;
		lna_band = 1;
	} else if (frequency > 484000 && frequency <= 533000) {
		l_band = 1;
		lna_band = 2;
	} else if (frequency > 533000 && frequency <= 587000) {
		l_band = 1;
		lna_band = 3;
	} else if (frequency > 587000 && frequency <= 645000) {
		l_band = 1;
		lna_band = 4;
	} else if (frequency > 645000 && frequency <= 710000) {
		l_band = 1;
		lna_band = 5;
	} else if (frequency > 710000 && frequency <= 782000) {
		l_band = 1;
		lna_band = 6;
	} else if (frequency > 782000 && frequency <= 860000) {
		l_band = 1;
		lna_band = 7;
	} else if (frequency > 1450000 && frequency <= 1492000) {
		l_band = 1;
		lna_band = 0;
	} else if (frequency > 1660000 && frequency <= 1685000) {
		l_band = 1;
		lna_band = 1;
	} else
		return -EINVAL;
	set_tuner[0].reg[0] = lna_band;

	switch (bandwidth) {
	case 5000000:
		bw = 0;
		break;
	case 6000000:
		bw = 2;
		break;
	case 7000000:
		bw = 4;
		break;
	default:
	case 8000000:
		bw = 6;
		break;
	}

	set_tuner[1].reg[0] = bw;
	set_tuner[2].reg[0] = 0xa0 | (l_band << 3);

	if (frequency > 53000 && frequency <= 74000) {
		n_div = 48;
		n = 0;
	} else if (frequency > 74000 && frequency <= 111000) {
		n_div = 32;
		n = 1;
	} else if (frequency > 111000 && frequency <= 148000) {
		n_div = 24;
		n = 2;
	} else if (frequency > 148000 && frequency <= 222000) {
		n_div = 16;
		n = 3;
	} else if (frequency > 222000 && frequency <= 296000) {
		n_div = 12;
		n = 4;
	} else if (frequency > 296000 && frequency <= 445000) {
		n_div = 8;
		n = 5;
	} else if (frequency > 445000 && frequency <= state->tun_fn_min) {
		n_div = 6;
		n = 6;
	} else if (frequency > state->tun_fn_min && frequency <= 950000) {
		n_div = 4;
		n = 7;
	} else if (frequency > 1450000 && frequency <= 1680000) {
		n_div = 2;
		n = 0;
	} else
		return -EINVAL;

369
	reg = it913x_rd_reg(state, 0xed81);
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
	iqik_m_cal = (u16)reg * n_div;

	if (reg < 0x20) {
		if (state->tun_clk_mode == 0)
			iqik_m_cal = (iqik_m_cal * 9) >> 5;
		else
			iqik_m_cal >>= 1;
	} else {
		iqik_m_cal = 0x40 - iqik_m_cal;
		if (state->tun_clk_mode == 0)
			iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
		else
			iqik_m_cal = ~(iqik_m_cal >> 1);
	}

	temp_f = frequency * (u32)n_div * (u32)state->tun_fdiv;
	freq = temp_f / state->tun_xtal;
	tmp = freq * state->tun_xtal;

	if ((temp_f - tmp) >= (state->tun_xtal >> 1))
		freq++;

	freq += (u32) n << 13;
	/* Frequency OMEGA_IQIK_M_CAL_MID*/
	temp_f = freq + (u32)iqik_m_cal;

	set_tuner[3].reg[0] =  temp_f & 0xff;
	set_tuner[4].reg[0] =  (temp_f >> 8) & 0xff;

	pr_debug("High Frequency = %04x\n", temp_f);

	/* Lower frequency */
	set_tuner[5].reg[0] =  freq & 0xff;
	set_tuner[6].reg[0] =  (freq >> 8) & 0xff;

	pr_debug("low Frequency = %04x\n", freq);

407
	ret = it913x_script_loader(state, set_tuner);
408 409 410 411 412 413 414 415

	return (ret < 0) ? -ENODEV : 0;
}

/* Power sequence */
/* Power Up	Tuner on -> Frontend suspend off -> Tuner clk on */
/* Power Down	Frontend suspend on -> Tuner clk off -> Tuner off */

416
static int it913x_sleep(struct dvb_frontend *fe)
417
{
418 419
	struct it913x_state *state = fe->tuner_priv;
	return it913x_script_loader(state, it9137_tuner_off);
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
}

static int it913x_release(struct dvb_frontend *fe)
{
	kfree(fe->tuner_priv);
	return 0;
}

static const struct dvb_tuner_ops it913x_tuner_ops = {
	.info = {
		.name           = "ITE Tech IT913X",
		.frequency_min  = 174000000,
		.frequency_max  = 862000000,
	},

	.release = it913x_release,

437 438 439
	.init = it913x_init,
	.sleep = it913x_sleep,
	.set_params = it9137_set_params,
440 441 442 443 444
};

struct dvb_frontend *it913x_attach(struct dvb_frontend *fe,
	struct i2c_adapter *i2c_adap, u8 i2c_addr, struct ite_config *config)
{
445
	struct it913x_state *state = NULL;
446 447 448
	int ret;

	/* allocate memory for the internal state */
449
	state = kzalloc(sizeof(struct it913x_state), GFP_KERNEL);
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
	if (state == NULL)
		return NULL;
	if (config == NULL)
		goto error;

	state->i2c_adap = i2c_adap;
	state->i2c_addr = i2c_addr;
	state->config = config;

	switch (state->config->tuner_id_0) {
	case IT9135_51:
	case IT9135_52:
	case IT9135_60:
	case IT9135_61:
	case IT9135_62:
		state->tuner_type = state->config->tuner_id_0;
		break;
	default:
	case IT9135_38:
		state->tuner_type = IT9135_38;
	}

	fe->tuner_priv = state;
	memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops,
			sizeof(struct dvb_tuner_ops));

	pr_info("%s: ITE Tech IT913X attached\n", KBUILD_MODNAME);

	return fe;
error:
	kfree(state);
	return NULL;
}
EXPORT_SYMBOL(it913x_attach);

MODULE_DESCRIPTION("ITE Tech IT913X silicon tuner driver");
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_LICENSE("GPL");