tuner-xc2028.c 11.9 KB
Newer Older
1 2 3
/* tuner-xc2028
 *
 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4 5
 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
 *       - frontend interface
6 7 8 9 10 11 12 13
 * This code is placed under the terms of the GNU General Public License v2
 */

#include <linux/i2c.h>
#include <asm/div64.h>
#include <linux/firmware.h>
#include <linux/videodev.h>
#include <linux/delay.h>
14
#include <media/tuner.h>
15 16 17
#include "tuner-driver.h"
#include "tuner-xc2028.h"

18 19 20 21 22 23 24 25
#include <linux/dvb/frontend.h>
#include "dvb_frontend.h"

/* digital TV standards */
#define V4L2_STD_DTV_6MHZ       ((v4l2_std_id)0x04000000)
#define V4L2_STD_DTV_7MHZ       ((v4l2_std_id)0x08000000)
#define V4L2_STD_DTV_8MHZ       ((v4l2_std_id)0x10000000)

26
/* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
27 28 29 30 31 32 33 34 35 36 37 38 39

/* Generic firmwares */
static const char *firmware_INIT0      = "tm_xc3028_MTS_init0.fw";
static const char *firmware_8MHZ_INIT0 = "tm_xc3028_8M_MTS_init0.fw";
static const char *firmware_INIT1      = "tm_xc3028_68M_MTS_init1.fw";

/* Standard-specific firmwares */
static const char *firmware_6M         = "tm_xc3028_DTV_6M.fw";
static const char *firmware_7M         = "tm_xc3028_7M.fw";
static const char *firmware_8M         = "tm_xc3028_8M.fw";
static const char *firmware_B          = "tm_xc3028_B_PAL.fw";
static const char *firmware_DK         = "tm_xc3028_DK_PAL_MTS.fw";
static const char *firmware_MN         = "tm_xc3028_MN_BTSC.fw";
40 41

struct xc2028_data {
42 43 44 45 46 47 48 49
	v4l2_std_id		firm_type;	   /* video stds supported
							by current firmware */
	fe_bandwidth_t		bandwidth;	   /* Firmware bandwidth:
							      6M, 7M or 8M */
	int			need_load_generic; /* The generic firmware
							      were loaded? */
	enum tuner_mode	mode;
	struct i2c_client	*i2c_client;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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
};

#define i2c_send(rc,c,buf,size)						\
if (size != (rc = i2c_master_send(c, buf, size)))			\
	tuner_warn("i2c output error: rc = %d (should be %d)\n",	\
	rc, (int)size);

#define i2c_rcv(rc,c,buf,size)						\
if (size != (rc = i2c_master_recv(c, buf, size)))			\
	tuner_warn("i2c input error: rc = %d (should be %d)\n",		\
	rc, (int)size);

#define send_seq(c, data...)						\
{	int rc;								\
	const static u8 _val[] = data;					\
	if (sizeof(_val) !=						\
				(rc = i2c_master_send 			\
				(c, _val, sizeof(_val)))) {		\
		printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);	\
		return;							\
	}								\
	msleep (10);							\
}

static int xc2028_get_reg(struct i2c_client *c, u16 reg)
{
	int rc;
	unsigned char buf[1];
	struct tuner *t = i2c_get_clientdata(c);

	buf[0]= reg;

	i2c_send(rc, c, buf, sizeof(buf));
	if (rc<0)
		return rc;

	i2c_rcv(rc, c, buf, 2);
	if (rc<0)
		return rc;

	return (buf[1])|(buf[0]<<8);
}

static int load_firmware (struct i2c_client *c, const char *name)
{
	const struct firmware *fw=NULL;
	struct tuner          *t = i2c_get_clientdata(c);
	unsigned char         *p, *endp;
	int                   len=0, rc=0;
	static const char     firmware_ver[] = "tm6000/xcv v1";

101
	tuner_info("xc2028: Loading firmware %s\n", name);
102 103
	rc = request_firmware(&fw, name, &c->dev);
	if (rc < 0) {
104 105 106 107 108
		if (rc==-ENOENT)
			tuner_info("Error: firmware %s not found.\n", name);
		else
			tuner_info("Error %d while requesting firmware %s \n", rc, name);

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 171 172 173 174 175 176 177 178
		return rc;
	}
	p=fw->data;
	endp=p+fw->size;

	if(fw->size==0) {
		tuner_info("Error: firmware size is zero!\n");
		rc=-EINVAL;
		goto err;
	}
	if (fw->size<sizeof(firmware_ver)-1) {
		/* Firmware is incorrect */
		tuner_info("Error: firmware size is less than header (%d<%d)!\n",
			   (int)fw->size,(int)sizeof(firmware_ver)-1);
		rc=-EINVAL;
		goto err;
	}

	if (memcmp(p,firmware_ver,sizeof(firmware_ver)-1)) {
		/* Firmware is incorrect */
		tuner_info("Error: firmware is not for tm5600/6000 + Xcv2028/3028!\n");
		rc=-EINVAL;
		goto err;
	}
	p+=sizeof(firmware_ver)-1;

	while(p<endp) {
		if ((*p) & 0x80) {
			/* Special callback command received */
			rc = t->tuner_callback(c->adapter->algo_data,
					     XC2028_TUNER_RESET, (*p)&0x7f);
			if (rc<0) {
				tuner_info("Error at RESET code %d\n",
								(*p)&0x7f);
				goto err;
			}
			p++;
			continue;
		}
		len=*p;
		p++;
		if (p+len+1>endp) {
			/* Firmware is incorrect */
			tuner_info("Error: firmware is truncated!\n");
			rc=-EINVAL;
			goto err;
		}
		if (len<=0) {
			tuner_info("Error: firmware file is corrupted!\n");
			rc=-EINVAL;
			goto err;
		}

		i2c_send(rc, c, p, len);
		if (rc<0)
			goto err;
		p+=len;

		if (*p)
			msleep(*p);
		p++;
	}


err:
	release_firmware(fw);

	return rc;
}

179 180
static int check_firmware(struct i2c_client *c, enum tuner_mode new_mode,
						fe_bandwidth_t bandwidth)
181 182 183 184 185
{
	int			rc, version;
	struct tuner		*t = i2c_get_clientdata(c);
	struct xc2028_data	*xc2028 = t->priv;
	const char		*name;
186
	int change_digital_bandwidth;
187 188 189 190 191 192

	if (!t->tuner_callback) {
		printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
		return -EINVAL;
	}

193 194 195 196 197 198 199 200 201 202 203 204 205 206
	printk(KERN_INFO "xc2028: I am in mode %u and I should switch to mode %i\n",
							    xc2028->mode, new_mode);

	/* first of all, determine whether we have switched the mode */
	if(new_mode != xc2028->mode) {
		xc2028->mode = new_mode;
		xc2028->need_load_generic = 1;
	}

	change_digital_bandwidth = (xc2028->mode == T_DIGITAL_TV
				 && bandwidth != xc2028->bandwidth) ? 1 : 0;
	tuner_info("xc2028: old bandwidth %u, new bandwidth %u\n", xc2028->bandwidth,
								   bandwidth);

207
	if (xc2028->need_load_generic) {
208
		if (xc2028->bandwidth==8)
209
			name = firmware_8MHZ_INIT0;
210 211 212
		else
			name = firmware_INIT0;

213 214 215 216 217 218 219 220 221 222 223 224
		/* Reset is needed before loading firmware */
		rc = t->tuner_callback(c->adapter->algo_data,
				     XC2028_TUNER_RESET, 0);
		if (rc<0)
			return rc;

		rc = load_firmware(c,name);
		if (rc<0)
			return rc;

		xc2028->need_load_generic=0;
		xc2028->firm_type=0;
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		if(xc2028->mode == T_DIGITAL_TV) {
			change_digital_bandwidth=1;
		}
	}

	tuner_info("xc2028: I should change bandwidth %u\n",
						   change_digital_bandwidth);

	if (change_digital_bandwidth) {
		switch(bandwidth) {
			case BANDWIDTH_8_MHZ:
				t->std = V4L2_STD_DTV_8MHZ;
			break;

			case BANDWIDTH_7_MHZ:
				t->std = V4L2_STD_DTV_7MHZ;
			break;

			case BANDWIDTH_6_MHZ:
				t->std = V4L2_STD_DTV_6MHZ;
			break;

			default:
				tuner_info("error: bandwidth not supported.\n");
		};
		xc2028->bandwidth = bandwidth;
251 252
	}

253 254
	if (xc2028->firm_type & t->std) {
		tuner_info("xc3028: no need to load a std-specific firmware.\n");
255
		return 0;
256
	}
257

258
	rc = load_firmware(c,firmware_INIT1);
259

260 261
	if (t->std & V4L2_STD_MN)
		name=firmware_MN;
262 263 264 265 266 267 268 269
	else if (t->std & V4L2_STD_DTV_6MHZ)
		name=firmware_6M;
	else if (t->std & V4L2_STD_DTV_7MHZ)
		name=firmware_7M;
	else if (t->std & V4L2_STD_DTV_8MHZ)
		name=firmware_8M;
	else if (t->std & V4L2_STD_PAL_B)
		name=firmware_B;
270 271 272
	else
		name=firmware_DK;

273 274
	tuner_info("xc2028: loading firmware named %s.\n", name);
	rc = load_firmware(c, name);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	if (rc<0)
		return rc;

	version = xc2028_get_reg(c, 0x4);
	tuner_info("Firmware version is %d.%d\n",
					(version>>4)&0x0f,(version)&0x0f);

	xc2028->firm_type=t->std;

	return 0;
}

static int xc2028_signal(struct i2c_client *c)
{
	int lock, signal;

291
	printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308

	lock = xc2028_get_reg(c, 0x2);
	if (lock<=0)
		return lock;

	/* Frequency is locked. Return signal quality */

	signal = xc2028_get_reg(c, 0x40);

	if(signal<=0)
		return lock;

	return signal;
}

#define DIV 15625

309 310
static void generic_set_tv_freq(struct i2c_client *c, u32 freq /* in Hz */,
				enum tuner_mode new_mode, fe_bandwidth_t bandwidth)
311 312 313 314
{
	int           rc;
	unsigned char buf[5];
	struct tuner  *t  = i2c_get_clientdata(c);
315
	u32 div, offset = 0;
316

317 318 319
	printk("xc3028: should set frequency %d kHz)\n", freq / 1000);

	if (check_firmware(c, new_mode, bandwidth)<0)
320 321
		return;

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	if(new_mode == T_DIGITAL_TV) {
		switch(bandwidth) {
			case BANDWIDTH_8_MHZ:
				offset = 2750000;
			break;

			case BANDWIDTH_7_MHZ:
				offset = 2750000;
			break;

			case BANDWIDTH_6_MHZ:
			default:
				printk(KERN_ERR "xc2028: bandwidth not implemented!\n");
		}
	}

	div = (freq - offset + DIV/2)/DIV;

340 341 342 343 344 345 346 347 348
	/* Reset GPIO 1 */
	if (t->tuner_callback) {
		rc = t->tuner_callback( c->adapter->algo_data,
					XC2028_TUNER_RESET, 0);
		if (rc<0)
			return;
	}
	msleep(10);

349 350 351 352 353 354 355 356 357 358
	char *name;

	rc = load_firmware(c,firmware_INIT1);

	if (t->std & V4L2_STD_MN)
		name=firmware_MN;
	else
		name=firmware_DK;

	rc = load_firmware(c,name);
359 360 361 362 363 364 365 366 367 368 369 370
	/* CMD= Set frequency */
	send_seq(c, {0x00, 0x02, 0x00, 0x00});
	if (t->tuner_callback) {
		rc = t->tuner_callback( c->adapter->algo_data,
					XC2028_RESET_CLK, 1);
		if (rc<0)
			return;
	}

	msleep(10);
//	send_seq(c, {0x00, 0x00, 0x10, 0xd0, 0x00});
//	msleep(100);
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	buf[0]= 0xff & (div>>24);
	buf[1]= 0xff & (div>>16);
	buf[2]= 0xff & (div>>8);
	buf[3]= 0xff & (div);
	buf[4]= 0;

	i2c_send(rc, c, buf, sizeof(buf));
	if (rc<0)
		return;
	msleep(100);

	printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
		 buf[1],buf[2],buf[3],buf[4],
		 freq / 16, freq % 16 * 100 / 16);
//	printk("signal=%d\n",xc2028_signal(c));
}


390 391 392 393 394 395 396 397
static void set_tv_freq(struct i2c_client *c, unsigned int freq)
{
	printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);

	generic_set_tv_freq(c, freq * 62500l, T_ANALOG_TV,
					      BANDWIDTH_8_MHZ /* unimportant */);
}

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
static void xc2028_release(struct i2c_client *c)
{
	struct tuner *t = i2c_get_clientdata(c);

	kfree(t->priv);
	t->priv = NULL;
}

static struct tuner_operations tea5767_tuner_ops = {
	.set_tv_freq    = set_tv_freq,
	.has_signal     = xc2028_signal,
	.release        = xc2028_release,
//	.is_stereo      = xc2028_stereo,
};


static int init=0;

int xc2028_tuner_init(struct i2c_client *c)
{
	struct tuner *t = i2c_get_clientdata(c);
	int version = xc2028_get_reg(c, 0x4);
	int prd_id = xc2028_get_reg(c, 0x8);
	struct xc2028_data *xc2028;

423 424
	tuner_info("Xcv2028/3028 init called!\n");

425 426 427 428 429 430 431 432 433 434 435
	if (init) {
		printk (KERN_ERR "Module already initialized!\n");
		return 0;
	}
	init++;

	xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
	if (!xc2028)
		return -ENOMEM;
	t->priv = xc2028;

436
	xc2028->bandwidth=BANDWIDTH_6_MHZ;
437
	xc2028->need_load_generic=1;
438
	xc2028->mode = T_UNINITIALIZED;
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457

	/* FIXME: Check where t->priv will be freed */

	if (version<0)
		version=0;

	if (prd_id<0)
		prd_id=0;

	strlcpy(c->name, "xc2028", sizeof(c->name));
	tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
		   t->type, c->name,
		   (version>>12)&0x0f,(version>>8)&0x0f,
		   (version>>4)&0x0f,(version)&0x0f, prd_id);

	memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));

	return 0;
}
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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

static int xc3028_set_params(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *p)
{
	struct i2c_client *c = fe->tuner_priv;

	printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);

	generic_set_tv_freq(c, p->frequency, T_DIGITAL_TV,
					     p->u.ofdm.bandwidth);

	return 0;
}

static int xc3028_dvb_release(struct dvb_frontend *fe)
{
	printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);

	fe->tuner_priv = NULL;

	return 0;
}

static int xc3028_dvb_init(struct dvb_frontend *fe)
{
	printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);

	return 0;
}

static const struct dvb_tuner_ops xc3028_dvb_tuner_ops = {
	.info = {
			.name           = "Xceive XC3028",
			.frequency_min  =  42000000,
			.frequency_max  = 864000000,
			.frequency_step =     50000,
		},

	.release = xc3028_dvb_release,
	.init = xc3028_dvb_init,

// 	int (*sleep)(struct dvb_frontend *fe);

	/** This is for simple PLLs - set all parameters in one go. */
	.set_params = xc3028_set_params,

	/** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */
// 	int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len);

// 	int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency);
// 	int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);

// 	int (*get_status)(struct dvb_frontend *fe, u32 *status);
};

int xc2028_attach(struct i2c_client *c, struct dvb_frontend *fe)
{
	fe->tuner_priv = c;

	memcpy(&fe->ops.tuner_ops, &xc3028_dvb_tuner_ops, sizeof(fe->ops.tuner_ops));

	return 0;
}

EXPORT_SYMBOL(xc2028_attach);