it913x.c 9.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
/*
 * 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.=
 */

23
#include "it913x_priv.h"
24

25
struct it913x_dev {
26
	struct i2c_client *client;
27
	struct regmap *regmap;
28
	struct dvb_frontend *fe;
29 30
	u8 chip_ver;
	u8 firmware_ver;
31 32 33 34 35 36
	u16 tun_xtal;
	u8 tun_fdiv;
	u8 tun_clk_mode;
	u32 tun_fn_min;
};

37
static int it913x_script_loader(struct it913x_dev *dev,
38 39 40
		struct it913xset *loadscript)
{
	int ret, i;
41

42 43 44 45
	if (loadscript == NULL)
		return -EINVAL;

	for (i = 0; i < 1000; ++i) {
46
		if (loadscript[i].address == 0x000000)
47
			break;
48
		ret = regmap_bulk_write(dev->regmap, loadscript[i].address,
49 50 51 52 53 54 55
			loadscript[i].reg, loadscript[i].count);
		if (ret < 0)
			return -ENODEV;
	}
	return 0;
}

56
static int it913x_init(struct dvb_frontend *fe)
57
{
58
	struct it913x_dev *dev = fe->tuner_priv;
59
	int ret, i;
60
	unsigned int reg;
61 62 63 64
	u8 val, nv_val;
	u8 nv[] = {48, 32, 24, 16, 12, 8, 6, 4, 2};
	u8 b[2];

65
	ret = regmap_read(dev->regmap, 0x80ec86, &reg);
66 67
	switch (reg) {
	case 0:
68 69 70
		dev->tun_clk_mode = reg;
		dev->tun_xtal = 2000;
		dev->tun_fdiv = 3;
71 72 73
		val = 16;
		break;
	case 1:
74
	default: /* I/O error too */
75 76 77
		dev->tun_clk_mode = reg;
		dev->tun_xtal = 640;
		dev->tun_fdiv = 1;
78 79 80 81
		val = 6;
		break;
	}

82
	ret = regmap_read(dev->regmap, 0x80ed03,  &reg);
83 84 85 86 87 88 89 90 91

	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++) {
92
		ret = regmap_bulk_read(dev->regmap, 0x80ed23, &b[0], sizeof(b));
93 94 95
		reg = (b[1] << 8) + b[0];
		if (reg > 0)
			break;
96
		if (ret)
97 98 99
			return -ENODEV;
		udelay(2000);
	}
100 101 102
	dev->tun_fn_min = dev->tun_xtal * reg;
	dev->tun_fn_min /= (dev->tun_fdiv * nv_val);
	dev_dbg(&dev->client->dev, "Tuner fn_min %d\n", dev->tun_fn_min);
103

104
	if (dev->chip_ver > 1)
105 106 107
		msleep(50);
	else {
		for (i = 0; i < 50; i++) {
108
			ret = regmap_read(dev->regmap, 0x80ec82, &reg);
109 110
			if (ret < 0)
				return -ENODEV;
111 112 113 114 115 116
			if (reg > 0)
				break;
			udelay(2000);
		}
	}

117
	/* Power Up Tuner - common all versions */
118 119 120
	ret = regmap_write(dev->regmap, 0x80ec40, 0x1);
	ret |= regmap_write(dev->regmap, 0x80ec57, 0x0);
	ret |= regmap_write(dev->regmap, 0x80ec58, 0x0);
121

122
	return regmap_write(dev->regmap, 0x80ed81, val);
123 124
}

125
static int it9137_set_params(struct dvb_frontend *fe)
126
{
127
	struct it913x_dev *dev = fe->tuner_priv;
128 129 130 131
	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;
132
	int ret;
133
	unsigned int reg;
134 135 136 137 138 139 140 141 142
	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;

143
	if (dev->firmware_ver == 1)
144 145 146 147
		set_tuner = set_it9135_template;
	else
		set_tuner = set_it9137_template;

148
	dev_dbg(&dev->client->dev, "Tuner Frequency %d Bandwidth %d\n",
149
			frequency, bandwidth);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

	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;
222
	} else if (frequency > 445000 && frequency <= dev->tun_fn_min) {
223 224
		n_div = 6;
		n = 6;
225
	} else if (frequency > dev->tun_fn_min && frequency <= 950000) {
226 227 228 229 230 231 232 233
		n_div = 4;
		n = 7;
	} else if (frequency > 1450000 && frequency <= 1680000) {
		n_div = 2;
		n = 0;
	} else
		return -EINVAL;

234
	ret = regmap_read(dev->regmap, 0x80ed81, &reg);
235 236 237
	iqik_m_cal = (u16)reg * n_div;

	if (reg < 0x20) {
238
		if (dev->tun_clk_mode == 0)
239 240 241 242 243
			iqik_m_cal = (iqik_m_cal * 9) >> 5;
		else
			iqik_m_cal >>= 1;
	} else {
		iqik_m_cal = 0x40 - iqik_m_cal;
244
		if (dev->tun_clk_mode == 0)
245 246 247 248 249
			iqik_m_cal = ~((iqik_m_cal * 9) >> 5);
		else
			iqik_m_cal = ~(iqik_m_cal >> 1);
	}

250 251 252
	temp_f = frequency * (u32)n_div * (u32)dev->tun_fdiv;
	freq = temp_f / dev->tun_xtal;
	tmp = freq * dev->tun_xtal;
253

254
	if ((temp_f - tmp) >= (dev->tun_xtal >> 1))
255 256 257 258 259 260 261 262 263
		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;

264
	dev_dbg(&dev->client->dev, "High Frequency = %04x\n", temp_f);
265 266 267 268 269

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

270
	dev_dbg(&dev->client->dev, "low Frequency = %04x\n", freq);
271

272
	ret = it913x_script_loader(dev, set_tuner);
273 274 275 276 277 278 279 280

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

281
static int it913x_sleep(struct dvb_frontend *fe)
282
{
283
	struct it913x_dev *dev = fe->tuner_priv;
284

285 286
	if (dev->chip_ver == 0x01)
		return it913x_script_loader(dev, it9135ax_tuner_off);
287
	else
288
		return it913x_script_loader(dev, it9137_tuner_off);
289 290 291 292 293 294 295 296 297
}

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

298 299 300
	.init = it913x_init,
	.sleep = it913x_sleep,
	.set_params = it9137_set_params,
301 302
};

303 304
static int it913x_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
305
{
306 307
	struct it913x_config *cfg = client->dev.platform_data;
	struct dvb_frontend *fe = cfg->fe;
308
	struct it913x_dev *dev;
309
	int ret;
310
	char *chip_ver_str;
311 312 313 314
	static const struct regmap_config regmap_config = {
		.reg_bits = 24,
		.val_bits = 8,
	};
315

316 317
	dev = kzalloc(sizeof(struct it913x_dev), GFP_KERNEL);
	if (dev == NULL) {
318 319 320
		ret = -ENOMEM;
		dev_err(&client->dev, "kzalloc() failed\n");
		goto err;
321 322
	}

323 324 325 326
	dev->client = client;
	dev->fe = cfg->fe;
	dev->chip_ver = cfg->chip_ver;
	dev->firmware_ver = 1;
327 328 329 330 331
	dev->regmap = regmap_init_i2c(client, &regmap_config);
	if (IS_ERR(dev->regmap)) {
		ret = PTR_ERR(dev->regmap);
		goto err_kfree;
	}
332

333
	/* tuner RF initial */
334 335 336
	ret = regmap_write(dev->regmap, 0x80ec4c, 0x68);
	if (ret)
		goto err_regmap_exit;
337

338
	fe->tuner_priv = dev;
339 340
	memcpy(&fe->ops.tuner_ops, &it913x_tuner_ops,
			sizeof(struct dvb_tuner_ops));
341
	i2c_set_clientdata(client, dev);
342

343
	if (dev->chip_ver == 1)
344
		chip_ver_str = "AX";
345
	else if (dev->chip_ver == 2)
346 347 348 349
		chip_ver_str = "BX";
	else
		chip_ver_str = "??";

350
	dev_info(&dev->client->dev, "ITE IT913X %s successfully attached\n",
351
			chip_ver_str);
352
	dev_dbg(&dev->client->dev, "chip_ver=%02x\n", dev->chip_ver);
353
	return 0;
354 355 356 357 358

err_regmap_exit:
	regmap_exit(dev->regmap);
err_kfree:
	kfree(dev);
359 360 361 362
err:
	dev_dbg(&client->dev, "failed %d\n", ret);
	return ret;
}
363

364 365
static int it913x_remove(struct i2c_client *client)
{
366 367
	struct it913x_dev *dev = i2c_get_clientdata(client);
	struct dvb_frontend *fe = dev->fe;
368 369 370 371 372

	dev_dbg(&client->dev, "\n");

	memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
	fe->tuner_priv = NULL;
373
	regmap_exit(dev->regmap);
374
	kfree(dev);
375 376

	return 0;
377
}
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

static const struct i2c_device_id it913x_id_table[] = {
	{"it913x", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, it913x_id_table);

static struct i2c_driver it913x_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "it913x",
	},
	.probe		= it913x_probe,
	.remove		= it913x_remove,
	.id_table	= it913x_id_table,
};

module_i2c_driver(it913x_driver);
396 397 398 399

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