dvb-pll.c 21.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * descriptions + helper functions for simple dvb plls.
 *
 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
 *
 *  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/module.h>
#include <linux/dvb/frontend.h>
#include <asm/types.h>

#include "dvb-pll.h"

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
struct dvb_pll_priv {
	/* pll number */
	int nr;

	/* i2c details */
	int pll_i2c_address;
	struct i2c_adapter *i2c;

	/* the PLL descriptor */
	struct dvb_pll_desc *pll_desc;

	/* cached frequency/bandwidth */
	u32 frequency;
	u32 bandwidth;
};

43
#define DVB_PLL_MAX 64
44 45 46 47 48 49 50 51 52 53 54 55 56

static unsigned int dvb_pll_devcount;

static int debug = 0;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable verbose debug messages");

static unsigned int input[DVB_PLL_MAX] = { [ 0 ... (DVB_PLL_MAX-1) ] = 0 };
module_param_array(input, int, NULL, 0644);
MODULE_PARM_DESC(input,"specify rf input choice, 0 for autoselect (default)");

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

57 58 59 60 61
struct dvb_pll_desc {
	char *name;
	u32  min;
	u32  max;
	u32  iffreq;
62 63
	void (*set)(struct dvb_frontend *fe, u8 *buf,
		    const struct dvb_frontend_parameters *params);
64 65 66 67 68 69 70 71 72 73 74
	u8   *initdata;
	u8   *sleepdata;
	int  count;
	struct {
		u32 limit;
		u32 stepsize;
		u8  config;
		u8  cb;
	} entries[12];
};

L
Linus Torvalds 已提交
75 76 77
/* ----------------------------------------------------------- */
/* descriptions                                                */

78 79 80 81 82 83 84 85 86 87 88
/* Set AGC TOP value to 103 dBuV:
	0x80 = Control Byte
	0x40 = 250 uA charge pump (irrelevant)
	0x18 = Aux Byte to follow
	0x06 = 64.5 kHz divider (irrelevant)
	0x01 = Disable Vt (aka sleep)

	0x00 = AGC Time constant 2s Iagc = 300 nA (vs 0x80 = 9 nA)
	0x50 = AGC Take over point = 103 dBuV */
static u8 tua603x_agc103[] = { 2, 0x80|0x40|0x18|0x06|0x01, 0x00|0x50 };

89 90 91 92 93 94
/*	0x04 = 166.67 kHz divider

	0x80 = AGC Time constant 50ms Iagc = 9 uA
	0x20 = AGC Take over point = 112 dBuV */
static u8 tua603x_agc112[] = { 2, 0x80|0x40|0x18|0x04|0x01, 0x80|0x20 };

95
static struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
L
Linus Torvalds 已提交
96 97 98
	.name  = "Thomson dtt7579",
	.min   = 177000000,
	.max   = 858000000,
99
	.iffreq= 36166667,
100 101
	.sleepdata = (u8[]){ 2, 0xb4, 0x03 },
	.count = 4,
L
Linus Torvalds 已提交
102
	.entries = {
103 104 105 106
		{  443250000, 166667, 0xb4, 0x02 },
		{  542000000, 166667, 0xb4, 0x08 },
		{  771000000, 166667, 0xbc, 0x08 },
		{  999999999, 166667, 0xf4, 0x08 },
L
Linus Torvalds 已提交
107 108 109
	},
};

110
static struct dvb_pll_desc dvb_pll_thomson_dtt7610 = {
L
Linus Torvalds 已提交
111 112 113
	.name  = "Thomson dtt7610",
	.min   =  44000000,
	.max   = 958000000,
114
	.iffreq= 44000000,
L
Linus Torvalds 已提交
115 116
	.count = 3,
	.entries = {
117 118 119
		{ 157250000, 62500, 0x8e, 0x39 },
		{ 454000000, 62500, 0x8e, 0x3a },
		{ 999999999, 62500, 0x8e, 0x3c },
L
Linus Torvalds 已提交
120 121 122
	},
};

123
static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf,
124
			       const struct dvb_frontend_parameters *params)
L
Linus Torvalds 已提交
125
{
126
	if (BANDWIDTH_7_MHZ == params->u.ofdm.bandwidth)
L
Linus Torvalds 已提交
127 128 129
		buf[3] |= 0x10;
}

130
static struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
L
Linus Torvalds 已提交
131 132 133
	.name  = "Thomson dtt759x",
	.min   = 177000000,
	.max   = 896000000,
134
	.set   = thomson_dtt759x_bw,
135
	.iffreq= 36166667,
136 137
	.sleepdata = (u8[]){ 2, 0x84, 0x03 },
	.count = 5,
L
Linus Torvalds 已提交
138
	.entries = {
139 140 141 142 143
		{  264000000, 166667, 0xb4, 0x02 },
		{  470000000, 166667, 0xbc, 0x02 },
		{  735000000, 166667, 0xbc, 0x08 },
		{  835000000, 166667, 0xf4, 0x08 },
		{  999999999, 166667, 0xfc, 0x08 },
L
Linus Torvalds 已提交
144 145 146
	},
};

147
static struct dvb_pll_desc dvb_pll_lg_z201 = {
L
Linus Torvalds 已提交
148 149 150
	.name  = "LG z201",
	.min   = 174000000,
	.max   = 862000000,
151
	.iffreq= 36166667,
152 153
	.sleepdata = (u8[]){ 2, 0xbc, 0x03 },
	.count = 5,
L
Linus Torvalds 已提交
154
	.entries = {
155 156 157 158 159
		{  157500000, 166667, 0xbc, 0x01 },
		{  443250000, 166667, 0xbc, 0x02 },
		{  542000000, 166667, 0xbc, 0x04 },
		{  830000000, 166667, 0xf4, 0x04 },
		{  999999999, 166667, 0xfc, 0x04 },
L
Linus Torvalds 已提交
160 161 162
	},
};

163
static struct dvb_pll_desc dvb_pll_microtune_4042 = {
164 165 166
	.name  = "Microtune 4042 FI5",
	.min   =  57000000,
	.max   = 858000000,
167
	.iffreq= 44000000,
168 169
	.count = 3,
	.entries = {
170 171 172
		{ 162000000, 62500, 0x8e, 0xa1 },
		{ 457000000, 62500, 0x8e, 0x91 },
		{ 999999999, 62500, 0x8e, 0x31 },
173 174 175
	},
};

176
static struct dvb_pll_desc dvb_pll_thomson_dtt761x = {
177 178 179 180
	/* DTT 7611 7611A 7612 7613 7613A 7614 7615 7615A */
	.name  = "Thomson dtt761x",
	.min   =  57000000,
	.max   = 863000000,
181
	.iffreq= 44000000,
182
	.count = 3,
183
	.initdata = tua603x_agc103,
184
	.entries = {
185 186 187
		{ 147000000, 62500, 0x8e, 0x39 },
		{ 417000000, 62500, 0x8e, 0x3a },
		{ 999999999, 62500, 0x8e, 0x3c },
188 189 190
	},
};

191
static struct dvb_pll_desc dvb_pll_unknown_1 = {
L
Linus Torvalds 已提交
192 193 194
	.name  = "unknown 1", /* used by dntv live dvb-t */
	.min   = 174000000,
	.max   = 862000000,
195
	.iffreq= 36166667,
L
Linus Torvalds 已提交
196 197
	.count = 9,
	.entries = {
198 199 200 201 202 203 204 205 206
		{  150000000, 166667, 0xb4, 0x01 },
		{  173000000, 166667, 0xbc, 0x01 },
		{  250000000, 166667, 0xb4, 0x02 },
		{  400000000, 166667, 0xbc, 0x02 },
		{  420000000, 166667, 0xf4, 0x02 },
		{  470000000, 166667, 0xfc, 0x02 },
		{  600000000, 166667, 0xbc, 0x08 },
		{  730000000, 166667, 0xf4, 0x08 },
		{  999999999, 166667, 0xfc, 0x08 },
L
Linus Torvalds 已提交
207 208 209
	},
};

210 211 212
/* Infineon TUA6010XS
 * used in Thomson Cable Tuner
 */
213
static struct dvb_pll_desc dvb_pll_tua6010xs = {
214 215 216
	.name  = "Infineon TUA6010XS",
	.min   =  44250000,
	.max   = 858000000,
217
	.iffreq= 36125000,
218 219
	.count = 3,
	.entries = {
220 221 222
		{  115750000, 62500, 0x8e, 0x03 },
		{  403250000, 62500, 0x8e, 0x06 },
		{  999999999, 62500, 0x8e, 0x85 },
223 224 225 226
	},
};

/* Panasonic env57h1xd5 (some Philips PLL ?) */
227
static struct dvb_pll_desc dvb_pll_env57h1xd5 = {
228 229 230
	.name  = "Panasonic ENV57H1XD5",
	.min   =  44250000,
	.max   = 858000000,
231
	.iffreq= 36125000,
232 233
	.count = 4,
	.entries = {
234 235 236 237
		{  153000000, 166667, 0xc2, 0x41 },
		{  470000000, 166667, 0xc2, 0x42 },
		{  526000000, 166667, 0xc2, 0x84 },
		{  999999999, 166667, 0xc2, 0xa4 },
238 239 240 241 242 243
	},
};

/* Philips TDA6650/TDA6651
 * used in Panasonic ENV77H11D5
 */
244 245
static void tda665x_bw(struct dvb_frontend *fe, u8 *buf,
		       const struct dvb_frontend_parameters *params)
246
{
247
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
248 249 250
		buf[3] |= 0x08;
}

251
static struct dvb_pll_desc dvb_pll_tda665x = {
252 253 254
	.name  = "Philips TDA6650/TDA6651",
	.min   =  44250000,
	.max   = 858000000,
255
	.set   = tda665x_bw,
256
	.iffreq= 36166667,
257
	.initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
258 259
	.count = 12,
	.entries = {
260 261 262 263 264 265 266 267 268 269 270 271
		{   93834000, 166667, 0xca, 0x61 /* 011 0 0 0  01 */ },
		{  123834000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
		{  161000000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
		{  163834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
		{  253834000, 166667, 0xca, 0x62 /* 011 0 0 0  10 */ },
		{  383834000, 166667, 0xca, 0xa2 /* 101 0 0 0  10 */ },
		{  443834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
		{  444000000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
		{  583834000, 166667, 0xca, 0x64 /* 011 0 0 1  00 */ },
		{  793834000, 166667, 0xca, 0xa4 /* 101 0 0 1  00 */ },
		{  444834000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
		{  861000000, 166667, 0xca, 0xe4 /* 111 0 0 1  00 */ },
272 273 274 275 276 277
	}
};

/* Infineon TUA6034
 * used in LG TDTP E102P
 */
278 279
static void tua6034_bw(struct dvb_frontend *fe, u8 *buf,
		       const struct dvb_frontend_parameters *params)
280
{
281
	if (BANDWIDTH_7_MHZ != params->u.ofdm.bandwidth)
282 283 284
		buf[3] |= 0x08;
}

285
static struct dvb_pll_desc dvb_pll_tua6034 = {
286 287 288
	.name  = "Infineon TUA6034",
	.min   =  44250000,
	.max   = 858000000,
289
	.iffreq= 36166667,
290
	.count = 3,
291
	.set   = tua6034_bw,
292
	.entries = {
293 294 295
		{  174500000, 62500, 0xce, 0x01 },
		{  230000000, 62500, 0xce, 0x02 },
		{  999999999, 62500, 0xce, 0x04 },
296 297 298
	},
};

299
/* Infineon TUA6034
300
 * used in LG TDVS-H061F, LG TDVS-H062F and LG TDVS-H064F
301
 */
302
static struct dvb_pll_desc dvb_pll_lg_tdvs_h06xf = {
303
	.name  = "LG TDVS-H06xF",
304 305
	.min   =  54000000,
	.max   = 863000000,
306
	.iffreq= 44000000,
307
	.initdata = tua603x_agc103,
308 309
	.count = 3,
	.entries = {
310 311 312
		{  165000000, 62500, 0xce, 0x01 },
		{  450000000, 62500, 0xce, 0x02 },
		{  999999999, 62500, 0xce, 0x04 },
313 314 315
	},
};

316 317 318
/* Philips FMD1216ME
 * used in Medion Hybrid PCMCIA card and USB Box
 */
319 320
static void fmd1216me_bw(struct dvb_frontend *fe, u8 *buf,
			 const struct dvb_frontend_parameters *params)
321
{
322 323
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
	    params->frequency >= 158870000)
324 325 326
		buf[3] |= 0x08;
}

327
static struct dvb_pll_desc dvb_pll_fmd1216me = {
328
	.name = "Philips FMD1216ME",
329 330
	.min = 50870000,
	.max = 858000000,
331
	.iffreq= 36125000,
332
	.set   = fmd1216me_bw,
333 334
	.initdata = tua603x_agc112,
	.sleepdata = (u8[]){ 4, 0x9c, 0x60, 0x85, 0x54 },
335 336
	.count = 7,
	.entries = {
337 338 339 340 341 342 343
		{ 143870000, 166667, 0xbc, 0x41 },
		{ 158870000, 166667, 0xf4, 0x41 },
		{ 329870000, 166667, 0xbc, 0x42 },
		{ 441870000, 166667, 0xf4, 0x42 },
		{ 625870000, 166667, 0xbc, 0x44 },
		{ 803870000, 166667, 0xf4, 0x44 },
		{ 999999999, 166667, 0xfc, 0x44 },
344 345 346
	}
};

347 348 349
/* ALPS TDED4
 * used in Nebula-Cards and USB boxes
 */
350 351
static void tded4_bw(struct dvb_frontend *fe, u8 *buf,
		     const struct dvb_frontend_parameters *params)
352
{
353
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
354 355 356
		buf[3] |= 0x04;
}

357
static struct dvb_pll_desc dvb_pll_tded4 = {
358 359 360
	.name = "ALPS TDED4",
	.min = 47000000,
	.max = 863000000,
361
	.iffreq= 36166667,
362
	.set   = tded4_bw,
363 364
	.count = 4,
	.entries = {
365 366 367 368
		{ 153000000, 166667, 0x85, 0x01 },
		{ 470000000, 166667, 0x85, 0x02 },
		{ 823000000, 166667, 0x85, 0x08 },
		{ 999999999, 166667, 0x85, 0x88 },
369 370 371
	}
};

372 373 374
/* ALPS TDHU2
 * used in AverTVHD MCE A180
 */
375
static struct dvb_pll_desc dvb_pll_tdhu2 = {
376 377 378
	.name = "ALPS TDHU2",
	.min = 54000000,
	.max = 864000000,
379
	.iffreq= 44000000,
380 381
	.count = 4,
	.entries = {
382 383 384 385
		{ 162000000, 62500, 0x85, 0x01 },
		{ 426000000, 62500, 0x85, 0x02 },
		{ 782000000, 62500, 0x85, 0x08 },
		{ 999999999, 62500, 0x85, 0x88 },
386 387 388 389 390 391
	}
};

/* Philips TUV1236D
 * used in ATI HDTV Wonder
 */
392 393
static void tuv1236d_rf(struct dvb_frontend *fe, u8 *buf,
			const struct dvb_frontend_parameters *params)
394
{
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
	struct dvb_pll_priv *priv = fe->tuner_priv;
	unsigned int new_rf = input[priv->nr];

	if ((new_rf == 0) || (new_rf > 2)) {
		switch (params->u.vsb.modulation) {
			case QAM_64:
			case QAM_256:
				new_rf = 1;
				break;
			case VSB_8:
			default:
				new_rf = 2;
		}
	}

	switch (new_rf) {
		case 1:
412 413
			buf[3] |= 0x08;
			break;
414
		case 2:
415
			buf[3] &= ~0x08;
416 417 418 419 420
			break;
		default:
			printk(KERN_WARNING
			       "%s: unhandled rf input selection: %d",
			       __FUNCTION__, new_rf);
421 422 423
	}
}

424
static struct dvb_pll_desc dvb_pll_tuv1236d = {
425
	.name  = "Philips TUV1236D",
426
	.min   =  54000000,
427
	.max   = 864000000,
428
	.iffreq= 44000000,
429
	.set   = tuv1236d_rf,
430 431
	.count = 3,
	.entries = {
432 433 434
		{ 157250000, 62500, 0xc6, 0x41 },
		{ 454000000, 62500, 0xc6, 0x42 },
		{ 999999999, 62500, 0xc6, 0x44 },
435 436 437
	},
};

438
/* Samsung TBMV30111IN / TBMV30712IN1
439 440
 * used in Air2PC ATSC - 2nd generation (nxt2002)
 */
441
static struct dvb_pll_desc dvb_pll_samsung_tbmv = {
442
	.name = "Samsung TBMV30111IN / TBMV30712IN1",
443 444
	.min = 54000000,
	.max = 860000000,
445
	.iffreq= 44000000,
446
	.count = 6,
447
	.entries = {
448 449 450 451 452 453
		{ 172000000, 166667, 0xb4, 0x01 },
		{ 214000000, 166667, 0xb4, 0x02 },
		{ 467000000, 166667, 0xbc, 0x02 },
		{ 721000000, 166667, 0xbc, 0x08 },
		{ 841000000, 166667, 0xf4, 0x08 },
		{ 999999999, 166667, 0xfc, 0x02 },
454 455 456
	}
};

457 458 459
/*
 * Philips SD1878 Tuner.
 */
460
static struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
461 462 463
	.name  = "Philips SD1878",
	.min   =  950000,
	.max   = 2150000,
464
	.iffreq= 249, /* zero-IF, offset 249 is to round up */
465 466
	.count = 4,
	.entries = {
467 468 469 470
		{ 1250000, 500, 0xc4, 0x00},
		{ 1550000, 500, 0xc4, 0x40},
		{ 2050000, 500, 0xc4, 0x80},
		{ 2150000, 500, 0xc4, 0xc0},
471 472 473
	},
};

474 475 476
/*
 * Philips TD1316 Tuner.
 */
477 478
static void td1316_bw(struct dvb_frontend *fe, u8 *buf,
		      const struct dvb_frontend_parameters *params)
479 480 481 482
{
	u8 band;

	/* determine band */
483
	if (params->frequency < 161000000)
484
		band = 1;
485
	else if (params->frequency < 444000000)
486 487 488 489 490 491 492
		band = 2;
	else
		band = 4;

	buf[3] |= band;

	/* setup PLL filter */
493
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
494 495 496
		buf[3] |= 1 << 3;
}

497
static struct dvb_pll_desc dvb_pll_philips_td1316 = {
498 499 500
	.name  = "Philips TD1316",
	.min   =  87000000,
	.max   = 895000000,
501
	.iffreq= 36166667,
502
	.set   = td1316_bw,
503 504
	.count = 9,
	.entries = {
505 506 507 508 509 510 511 512 513
		{  93834000, 166667, 0xca, 0x60},
		{ 123834000, 166667, 0xca, 0xa0},
		{ 163834000, 166667, 0xca, 0xc0},
		{ 253834000, 166667, 0xca, 0x60},
		{ 383834000, 166667, 0xca, 0xa0},
		{ 443834000, 166667, 0xca, 0xc0},
		{ 583834000, 166667, 0xca, 0x60},
		{ 793834000, 166667, 0xca, 0xa0},
		{ 858834000, 166667, 0xca, 0xe0},
514 515 516
	},
};

517
/* FE6600 used on DViCO Hybrid */
518
static struct dvb_pll_desc dvb_pll_thomson_fe6600 = {
519
	.name = "Thomson FE6600",
520 521
	.min =  44250000,
	.max = 858000000,
522
	.iffreq= 36125000,
523 524
	.count = 4,
	.entries = {
525 526 527 528
		{ 250000000, 166667, 0xb4, 0x12 },
		{ 455000000, 166667, 0xfe, 0x11 },
		{ 775500000, 166667, 0xbc, 0x18 },
		{ 999999999, 166667, 0xf4, 0x18 },
529 530
	}
};
531

532 533
static void opera1_bw(struct dvb_frontend *fe, u8 *buf,
		      const struct dvb_frontend_parameters *params)
534
{
535
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
536 537 538
		buf[2] |= 0x08;
}

539
static struct dvb_pll_desc dvb_pll_opera1 = {
540 541 542 543
	.name  = "Opera Tuner",
	.min   =  900000,
	.max   = 2250000,
	.iffreq= 0,
544
	.set   = opera1_bw,
545 546 547 548 549 550 551 552 553 554 555 556
	.count = 8,
	.entries = {
		{ 1064000, 500, 0xe5, 0xc6 },
		{ 1169000, 500, 0xe5, 0xe6 },
		{ 1299000, 500, 0xe5, 0x24 },
		{ 1444000, 500, 0xe5, 0x44 },
		{ 1606000, 500, 0xe5, 0x64 },
		{ 1777000, 500, 0xe5, 0x84 },
		{ 1941000, 500, 0xe5, 0xa4 },
		{ 2250000, 500, 0xe5, 0xc4 },
	}
};
557

558 559
/* Philips FCV1236D
 */
560
static struct dvb_pll_desc dvb_pll_fcv1236d = {
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
/* Bit_0: RF Input select
 * Bit_1: 0=digital, 1=analog
 */
	.name  = "Philips FCV1236D",
	.min   =  53000000,
	.max   = 803000000,
	.iffreq= 44000000,
	.count = 3,
	.entries = {
		{ 159000000, 62500, 0x8e, 0xa0 },
		{ 453000000, 62500, 0x8e, 0x90 },
		{ 999999999, 62500, 0x8e, 0x30 },
	},
};

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
/* ----------------------------------------------------------- */

static struct dvb_pll_desc *pll_list[] = {
	[DVB_PLL_UNDEFINED]              = NULL,
	[DVB_PLL_THOMSON_DTT7579]        = &dvb_pll_thomson_dtt7579,
	[DVB_PLL_THOMSON_DTT759X]        = &dvb_pll_thomson_dtt759x,
	[DVB_PLL_THOMSON_DTT7610]        = &dvb_pll_thomson_dtt7610,
	[DVB_PLL_LG_Z201]                = &dvb_pll_lg_z201,
	[DVB_PLL_MICROTUNE_4042]         = &dvb_pll_microtune_4042,
	[DVB_PLL_THOMSON_DTT761X]        = &dvb_pll_thomson_dtt761x,
	[DVB_PLL_UNKNOWN_1]              = &dvb_pll_unknown_1,
	[DVB_PLL_TUA6010XS]              = &dvb_pll_tua6010xs,
	[DVB_PLL_ENV57H1XD5]             = &dvb_pll_env57h1xd5,
	[DVB_PLL_TUA6034]                = &dvb_pll_tua6034,
	[DVB_PLL_LG_TDVS_H06XF]          = &dvb_pll_lg_tdvs_h06xf,
	[DVB_PLL_TDA665X]                = &dvb_pll_tda665x,
	[DVB_PLL_FMD1216ME]              = &dvb_pll_fmd1216me,
	[DVB_PLL_TDED4]                  = &dvb_pll_tded4,
	[DVB_PLL_TUV1236D]               = &dvb_pll_tuv1236d,
	[DVB_PLL_TDHU2]                  = &dvb_pll_tdhu2,
	[DVB_PLL_SAMSUNG_TBMV]           = &dvb_pll_samsung_tbmv,
	[DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
	[DVB_PLL_PHILIPS_TD1316]         = &dvb_pll_philips_td1316,
	[DVB_PLL_THOMSON_FE6600]         = &dvb_pll_thomson_fe6600,
	[DVB_PLL_OPERA1]                 = &dvb_pll_opera1,
601
	[DVB_PLL_FCV1236D]               = &dvb_pll_fcv1236d,
602 603
};

L
Linus Torvalds 已提交
604 605 606
/* ----------------------------------------------------------- */
/* code                                                        */

607
static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
608
			     const struct dvb_frontend_parameters *params)
L
Linus Torvalds 已提交
609
{
610 611
	struct dvb_pll_priv *priv = fe->tuner_priv;
	struct dvb_pll_desc *desc = priv->pll_desc;
L
Linus Torvalds 已提交
612 613 614
	u32 div;
	int i;

615 616 617
	if (params->frequency != 0 && (params->frequency < desc->min ||
				       params->frequency > desc->max))
		return -EINVAL;
L
Linus Torvalds 已提交
618 619

	for (i = 0; i < desc->count; i++) {
620
		if (params->frequency > desc->entries[i].limit)
L
Linus Torvalds 已提交
621 622 623
			continue;
		break;
	}
624

L
Linus Torvalds 已提交
625
	if (debug)
626 627
		printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
		       params->frequency, i, desc->count);
628 629
	if (i == desc->count)
		return -EINVAL;
L
Linus Torvalds 已提交
630

631 632
	div = (params->frequency + desc->iffreq +
	       desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
L
Linus Torvalds 已提交
633 634
	buf[0] = div >> 8;
	buf[1] = div & 0xff;
635 636
	buf[2] = desc->entries[i].config;
	buf[3] = desc->entries[i].cb;
L
Linus Torvalds 已提交
637

638
	if (desc->set)
639
		desc->set(fe, buf, params);
L
Linus Torvalds 已提交
640 641 642 643 644

	if (debug)
		printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
		       desc->name, div, buf[0], buf[1], buf[2], buf[3]);

645
	// calculate the frequency we set it to
646
	return (div * desc->entries[i].stepsize) - desc->iffreq;
L
Linus Torvalds 已提交
647 648
}

649 650
static int dvb_pll_release(struct dvb_frontend *fe)
{
651
	kfree(fe->tuner_priv);
652 653 654 655 656 657 658 659
	fe->tuner_priv = NULL;
	return 0;
}

static int dvb_pll_sleep(struct dvb_frontend *fe)
{
	struct dvb_pll_priv *priv = fe->tuner_priv;

660 661 662
	if (priv->i2c == NULL)
		return -EINVAL;

663 664 665 666 667
	if (priv->pll_desc->sleepdata) {
		struct i2c_msg msg = { .flags = 0,
			.addr = priv->pll_i2c_address,
			.buf = priv->pll_desc->sleepdata + 1,
			.len = priv->pll_desc->sleepdata[0] };
668

669
		int result;
670

671 672 673 674 675 676
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);
		if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
			return result;
		}
		return 0;
677
	}
678 679
	/* Shouldn't be called when initdata is NULL, maybe BUG()? */
	return -EINVAL;
680 681
}

682 683
static int dvb_pll_set_params(struct dvb_frontend *fe,
			      struct dvb_frontend_parameters *params)
684 685 686 687
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	u8 buf[4];
	struct i2c_msg msg =
688 689
		{ .addr = priv->pll_i2c_address, .flags = 0,
		  .buf = buf, .len = sizeof(buf) };
690
	int result;
691
	u32 frequency = 0;
692 693 694 695

	if (priv->i2c == NULL)
		return -EINVAL;

696
	if ((result = dvb_pll_configure(fe, buf, params)) < 0)
697
		return result;
698 699
	else
		frequency = result;
700

701 702
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
703 704 705 706
	if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
		return result;
	}

707
	priv->frequency = frequency;
708
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
709 710 711 712

	return 0;
}

713 714 715
static int dvb_pll_calc_regs(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *params,
			     u8 *buf, int buf_len)
716 717 718
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	int result;
719
	u32 frequency = 0;
720 721 722 723

	if (buf_len < 5)
		return -EINVAL;

724
	if ((result = dvb_pll_configure(fe, buf+1, params)) < 0)
725
		return result;
726 727 728
	else
		frequency = result;

729 730
	buf[0] = priv->pll_i2c_address;

731
	priv->frequency = frequency;
732
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

	return 5;
}

static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	*frequency = priv->frequency;
	return 0;
}

static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	*bandwidth = priv->bandwidth;
	return 0;
}

751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
static int dvb_pll_init(struct dvb_frontend *fe)
{
	struct dvb_pll_priv *priv = fe->tuner_priv;

	if (priv->i2c == NULL)
		return -EINVAL;

	if (priv->pll_desc->initdata) {
		struct i2c_msg msg = { .flags = 0,
			.addr = priv->pll_i2c_address,
			.buf = priv->pll_desc->initdata + 1,
			.len = priv->pll_desc->initdata[0] };

		int result;
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);
		if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
			return result;
		}
		return 0;
	}
	/* Shouldn't be called when initdata is NULL, maybe BUG()? */
	return -EINVAL;
}

776 777 778
static struct dvb_tuner_ops dvb_pll_tuner_ops = {
	.release = dvb_pll_release,
	.sleep = dvb_pll_sleep,
779
	.init = dvb_pll_init,
780
	.set_params = dvb_pll_set_params,
781
	.calc_regs = dvb_pll_calc_regs,
782 783 784 785
	.get_frequency = dvb_pll_get_frequency,
	.get_bandwidth = dvb_pll_get_bandwidth,
};

786 787
struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
				    struct i2c_adapter *i2c,
788
				    unsigned int pll_desc_id)
789
{
790
	u8 b1 [] = { 0 };
791 792
	struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
			       .buf = b1, .len = 1 };
793
	struct dvb_pll_priv *priv = NULL;
794
	int ret;
795 796 797 798 799
	struct dvb_pll_desc *desc;

	BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));

	desc = pll_list[pll_desc_id];
800

801 802 803 804
	if (i2c != NULL) {
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);

805 806
		ret = i2c_transfer (i2c, &msg, 1);
		if (ret != 1)
807
			return NULL;
808 809 810
		if (fe->ops.i2c_gate_ctrl)
			     fe->ops.i2c_gate_ctrl(fe, 0);
	}
811 812 813

	priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
	if (priv == NULL)
814
		return NULL;
815 816 817 818

	priv->pll_i2c_address = pll_addr;
	priv->i2c = i2c;
	priv->pll_desc = desc;
819
	priv->nr = dvb_pll_devcount++;
820

821 822 823
	memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
	       sizeof(struct dvb_tuner_ops));

824 825
	strncpy(fe->ops.tuner_ops.info.name, desc->name,
		sizeof(fe->ops.tuner_ops.info.name));
826
	fe->ops.tuner_ops.info.frequency_min = desc->min;
827
	fe->ops.tuner_ops.info.frequency_max = desc->max;
828 829 830 831
	if (!desc->initdata)
		fe->ops.tuner_ops.init = NULL;
	if (!desc->sleepdata)
		fe->ops.tuner_ops.sleep = NULL;
832 833

	fe->tuner_priv = priv;
834 835 836 837 838 839 840 841

	if (debug) {
		printk("dvb-pll[%d]", priv->nr);
		if (i2c != NULL)
			printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
		printk(": id# %d (%s) attached\n", pll_desc_id, desc->name);
	}

842
	return fe;
843 844 845
}
EXPORT_SYMBOL(dvb_pll_attach);

L
Linus Torvalds 已提交
846 847 848
MODULE_DESCRIPTION("dvb pll library");
MODULE_AUTHOR("Gerd Knorr");
MODULE_LICENSE("GPL");