dvb-pll.c 18.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 27 28 29
/*
 * 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"

/* ----------------------------------------------------------- */
/* descriptions                                                */

30 31 32 33 34 35 36 37 38 39 40
/* 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 };

L
Linus Torvalds 已提交
41 42 43 44
struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
	.name  = "Thomson dtt7579",
	.min   = 177000000,
	.max   = 858000000,
45
	.iffreq= 36166667,
46 47
	.sleepdata = (u8[]){ 2, 0xb4, 0x03 },
	.count = 4,
L
Linus Torvalds 已提交
48
	.entries = {
49 50 51 52
		{  443250000, 166667, 0xb4, 0x02 },
		{  542000000, 166667, 0xb4, 0x08 },
		{  771000000, 166667, 0xbc, 0x08 },
		{  999999999, 166667, 0xf4, 0x08 },
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60
	},
};
EXPORT_SYMBOL(dvb_pll_thomson_dtt7579);

struct dvb_pll_desc dvb_pll_thomson_dtt7610 = {
	.name  = "Thomson dtt7610",
	.min   =  44000000,
	.max   = 958000000,
61
	.iffreq= 44000000,
L
Linus Torvalds 已提交
62 63
	.count = 3,
	.entries = {
64 65 66
		{ 157250000, 62500, 0x8e, 0x39 },
		{ 454000000, 62500, 0x8e, 0x3a },
		{ 999999999, 62500, 0x8e, 0x3c },
L
Linus Torvalds 已提交
67 68 69 70
	},
};
EXPORT_SYMBOL(dvb_pll_thomson_dtt7610);

71 72
static void thomson_dtt759x_bw(u8 *buf,
			       const struct dvb_frontend_parameters *params)
L
Linus Torvalds 已提交
73
{
74
	if (BANDWIDTH_7_MHZ == params->u.ofdm.bandwidth)
L
Linus Torvalds 已提交
75 76 77 78 79 80 81
		buf[3] |= 0x10;
}

struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
	.name  = "Thomson dtt759x",
	.min   = 177000000,
	.max   = 896000000,
82
	.set   = thomson_dtt759x_bw,
83
	.iffreq= 36166667,
84 85
	.sleepdata = (u8[]){ 2, 0x84, 0x03 },
	.count = 5,
L
Linus Torvalds 已提交
86
	.entries = {
87 88 89 90 91
		{  264000000, 166667, 0xb4, 0x02 },
		{  470000000, 166667, 0xbc, 0x02 },
		{  735000000, 166667, 0xbc, 0x08 },
		{  835000000, 166667, 0xf4, 0x08 },
		{  999999999, 166667, 0xfc, 0x08 },
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99
	},
};
EXPORT_SYMBOL(dvb_pll_thomson_dtt759x);

struct dvb_pll_desc dvb_pll_lg_z201 = {
	.name  = "LG z201",
	.min   = 174000000,
	.max   = 862000000,
100
	.iffreq= 36166667,
101 102
	.sleepdata = (u8[]){ 2, 0xbc, 0x03 },
	.count = 5,
L
Linus Torvalds 已提交
103
	.entries = {
104 105 106 107 108
		{  157500000, 166667, 0xbc, 0x01 },
		{  443250000, 166667, 0xbc, 0x02 },
		{  542000000, 166667, 0xbc, 0x04 },
		{  830000000, 166667, 0xf4, 0x04 },
		{  999999999, 166667, 0xfc, 0x04 },
L
Linus Torvalds 已提交
109 110 111 112
	},
};
EXPORT_SYMBOL(dvb_pll_lg_z201);

113 114 115 116
struct dvb_pll_desc dvb_pll_microtune_4042 = {
	.name  = "Microtune 4042 FI5",
	.min   =  57000000,
	.max   = 858000000,
117
	.iffreq= 44000000,
118 119
	.count = 3,
	.entries = {
120 121 122
		{ 162000000, 62500, 0x8e, 0xa1 },
		{ 457000000, 62500, 0x8e, 0x91 },
		{ 999999999, 62500, 0x8e, 0x31 },
123 124 125 126
	},
};
EXPORT_SYMBOL(dvb_pll_microtune_4042);

127 128 129 130 131
struct dvb_pll_desc dvb_pll_thomson_dtt761x = {
	/* DTT 7611 7611A 7612 7613 7613A 7614 7615 7615A */
	.name  = "Thomson dtt761x",
	.min   =  57000000,
	.max   = 863000000,
132
	.iffreq= 44000000,
133
	.count = 3,
134
	.initdata = tua603x_agc103,
135
	.entries = {
136 137 138
		{ 147000000, 62500, 0x8e, 0x39 },
		{ 417000000, 62500, 0x8e, 0x3a },
		{ 999999999, 62500, 0x8e, 0x3c },
139 140
	},
};
141
EXPORT_SYMBOL(dvb_pll_thomson_dtt761x);
142

L
Linus Torvalds 已提交
143 144 145 146
struct dvb_pll_desc dvb_pll_unknown_1 = {
	.name  = "unknown 1", /* used by dntv live dvb-t */
	.min   = 174000000,
	.max   = 862000000,
147
	.iffreq= 36166667,
L
Linus Torvalds 已提交
148 149
	.count = 9,
	.entries = {
150 151 152 153 154 155 156 157 158
		{  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 已提交
159 160 161 162
	},
};
EXPORT_SYMBOL(dvb_pll_unknown_1);

163 164 165 166 167 168 169
/* Infineon TUA6010XS
 * used in Thomson Cable Tuner
 */
struct dvb_pll_desc dvb_pll_tua6010xs = {
	.name  = "Infineon TUA6010XS",
	.min   =  44250000,
	.max   = 858000000,
170
	.iffreq= 36125000,
171 172
	.count = 3,
	.entries = {
173 174 175
		{  115750000, 62500, 0x8e, 0x03 },
		{  403250000, 62500, 0x8e, 0x06 },
		{  999999999, 62500, 0x8e, 0x85 },
176 177 178 179 180 181 182 183 184
	},
};
EXPORT_SYMBOL(dvb_pll_tua6010xs);

/* Panasonic env57h1xd5 (some Philips PLL ?) */
struct dvb_pll_desc dvb_pll_env57h1xd5 = {
	.name  = "Panasonic ENV57H1XD5",
	.min   =  44250000,
	.max   = 858000000,
185
	.iffreq= 36125000,
186 187
	.count = 4,
	.entries = {
188 189 190 191
		{  153000000, 166667, 0xc2, 0x41 },
		{  470000000, 166667, 0xc2, 0x42 },
		{  526000000, 166667, 0xc2, 0x84 },
		{  999999999, 166667, 0xc2, 0xa4 },
192 193 194 195 196 197 198
	},
};
EXPORT_SYMBOL(dvb_pll_env57h1xd5);

/* Philips TDA6650/TDA6651
 * used in Panasonic ENV77H11D5
 */
199
static void tda665x_bw(u8 *buf, const struct dvb_frontend_parameters *params)
200
{
201
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
202 203 204 205 206 207 208
		buf[3] |= 0x08;
}

struct dvb_pll_desc dvb_pll_tda665x = {
	.name  = "Philips TDA6650/TDA6651",
	.min   =  44250000,
	.max   = 858000000,
209
	.set   = tda665x_bw,
210
	.iffreq= 36166667,
211
	.initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
212 213
	.count = 12,
	.entries = {
214 215 216 217 218 219 220 221 222 223 224 225
		{   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 */ },
226 227 228 229 230 231 232
	}
};
EXPORT_SYMBOL(dvb_pll_tda665x);

/* Infineon TUA6034
 * used in LG TDTP E102P
 */
233
static void tua6034_bw(u8 *buf, const struct dvb_frontend_parameters *params)
234
{
235
	if (BANDWIDTH_7_MHZ != params->u.ofdm.bandwidth)
236 237 238 239 240 241 242
		buf[3] |= 0x08;
}

struct dvb_pll_desc dvb_pll_tua6034 = {
	.name  = "Infineon TUA6034",
	.min   =  44250000,
	.max   = 858000000,
243
	.iffreq= 36166667,
244
	.count = 3,
245
	.set   = tua6034_bw,
246
	.entries = {
247 248 249
		{  174500000, 62500, 0xce, 0x01 },
		{  230000000, 62500, 0xce, 0x02 },
		{  999999999, 62500, 0xce, 0x04 },
250 251 252 253
	},
};
EXPORT_SYMBOL(dvb_pll_tua6034);

254
/* Infineon TUA6034
255
 * used in LG TDVS-H061F, LG TDVS-H062F and LG TDVS-H064F
256
 */
257
struct dvb_pll_desc dvb_pll_lg_tdvs_h06xf = {
258
	.name  = "LG TDVS-H06xF",
259 260
	.min   =  54000000,
	.max   = 863000000,
261
	.iffreq= 44000000,
262
	.initdata = tua603x_agc103,
263 264
	.count = 3,
	.entries = {
265 266 267
		{  165000000, 62500, 0xce, 0x01 },
		{  450000000, 62500, 0xce, 0x02 },
		{  999999999, 62500, 0xce, 0x04 },
268 269
	},
};
270
EXPORT_SYMBOL(dvb_pll_lg_tdvs_h06xf);
271

272 273 274
/* Philips FMD1216ME
 * used in Medion Hybrid PCMCIA card and USB Box
 */
275
static void fmd1216me_bw(u8 *buf, const struct dvb_frontend_parameters *params)
276
{
277 278
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
	    params->frequency >= 158870000)
279 280 281 282
		buf[3] |= 0x08;
}

struct dvb_pll_desc dvb_pll_fmd1216me = {
283
	.name = "Philips FMD1216ME",
284 285
	.min = 50870000,
	.max = 858000000,
286
	.iffreq= 36125000,
287
	.set   = fmd1216me_bw,
288 289
	.count = 7,
	.entries = {
290 291 292 293 294 295 296
		{ 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 },
297 298 299 300
	}
};
EXPORT_SYMBOL(dvb_pll_fmd1216me);

301 302 303
/* ALPS TDED4
 * used in Nebula-Cards and USB boxes
 */
304
static void tded4_bw(u8 *buf, const struct dvb_frontend_parameters *params)
305
{
306
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
307 308 309 310 311 312 313
		buf[3] |= 0x04;
}

struct dvb_pll_desc dvb_pll_tded4 = {
	.name = "ALPS TDED4",
	.min = 47000000,
	.max = 863000000,
314
	.iffreq= 36166667,
315
	.set   = tded4_bw,
316 317
	.count = 4,
	.entries = {
318 319 320 321
		{ 153000000, 166667, 0x85, 0x01 },
		{ 470000000, 166667, 0x85, 0x02 },
		{ 823000000, 166667, 0x85, 0x08 },
		{ 999999999, 166667, 0x85, 0x88 },
322 323 324 325
	}
};
EXPORT_SYMBOL(dvb_pll_tded4);

326 327 328 329 330 331 332
/* ALPS TDHU2
 * used in AverTVHD MCE A180
 */
struct dvb_pll_desc dvb_pll_tdhu2 = {
	.name = "ALPS TDHU2",
	.min = 54000000,
	.max = 864000000,
333
	.iffreq= 44000000,
334 335
	.count = 4,
	.entries = {
336 337 338 339
		{ 162000000, 62500, 0x85, 0x01 },
		{ 426000000, 62500, 0x85, 0x02 },
		{ 782000000, 62500, 0x85, 0x08 },
		{ 999999999, 62500, 0x85, 0x88 },
340 341 342 343 344 345 346
	}
};
EXPORT_SYMBOL(dvb_pll_tdhu2);

/* Philips TUV1236D
 * used in ATI HDTV Wonder
 */
347 348 349 350 351 352 353 354 355 356 357 358 359
static void tuv1236d_rf(u8 *buf, const struct dvb_frontend_parameters *params)
{
	switch (params->u.vsb.modulation) {
		case QAM_64:
		case QAM_256:
			buf[3] |= 0x08;
			break;
		case VSB_8:
		default:
			buf[3] &= ~0x08;
	}
}

360 361
struct dvb_pll_desc dvb_pll_tuv1236d = {
	.name  = "Philips TUV1236D",
362
	.min   =  54000000,
363
	.max   = 864000000,
364
	.iffreq= 44000000,
365
	.set   = tuv1236d_rf,
366 367
	.count = 3,
	.entries = {
368 369 370
		{ 157250000, 62500, 0xc6, 0x41 },
		{ 454000000, 62500, 0xc6, 0x42 },
		{ 999999999, 62500, 0xc6, 0x44 },
371 372 373 374
	},
};
EXPORT_SYMBOL(dvb_pll_tuv1236d);

375
/* Samsung TBMV30111IN / TBMV30712IN1
376 377
 * used in Air2PC ATSC - 2nd generation (nxt2002)
 */
378 379
struct dvb_pll_desc dvb_pll_samsung_tbmv = {
	.name = "Samsung TBMV30111IN / TBMV30712IN1",
380 381
	.min = 54000000,
	.max = 860000000,
382
	.iffreq= 44000000,
383
	.count = 6,
384
	.entries = {
385 386 387 388 389 390
		{ 172000000, 166667, 0xb4, 0x01 },
		{ 214000000, 166667, 0xb4, 0x02 },
		{ 467000000, 166667, 0xbc, 0x02 },
		{ 721000000, 166667, 0xbc, 0x08 },
		{ 841000000, 166667, 0xf4, 0x08 },
		{ 999999999, 166667, 0xfc, 0x02 },
391 392
	}
};
393
EXPORT_SYMBOL(dvb_pll_samsung_tbmv);
394

395 396 397 398 399 400 401
/*
 * Philips SD1878 Tuner.
 */
struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
	.name  = "Philips SD1878",
	.min   =  950000,
	.max   = 2150000,
402
	.iffreq= 249, /* zero-IF, offset 249 is to round up */
403 404
	.count = 4,
	.entries = {
405 406 407 408
		{ 1250000, 500, 0xc4, 0x00},
		{ 1550000, 500, 0xc4, 0x40},
		{ 2050000, 500, 0xc4, 0x80},
		{ 2150000, 500, 0xc4, 0xc0},
409 410 411 412
	},
};
EXPORT_SYMBOL(dvb_pll_philips_sd1878_tda8261);

413 414 415
/*
 * Philips TD1316 Tuner.
 */
416
static void td1316_bw(u8 *buf, const struct dvb_frontend_parameters *params)
417 418 419 420
{
	u8 band;

	/* determine band */
421
	if (params->frequency < 161000000)
422
		band = 1;
423
	else if (params->frequency < 444000000)
424 425 426 427 428 429 430
		band = 2;
	else
		band = 4;

	buf[3] |= band;

	/* setup PLL filter */
431
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
432 433 434 435 436 437 438
		buf[3] |= 1 << 3;
}

struct dvb_pll_desc dvb_pll_philips_td1316 = {
	.name  = "Philips TD1316",
	.min   =  87000000,
	.max   = 895000000,
439
	.iffreq= 36166667,
440
	.set   = td1316_bw,
441 442
	.count = 9,
	.entries = {
443 444 445 446 447 448 449 450 451
		{  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},
452 453 454 455
	},
};
EXPORT_SYMBOL(dvb_pll_philips_td1316);

456
/* FE6600 used on DViCO Hybrid */
457 458
struct dvb_pll_desc dvb_pll_thomson_fe6600 = {
	.name = "Thomson FE6600",
459 460
	.min =  44250000,
	.max = 858000000,
461
	.iffreq= 36125000,
462 463
	.count = 4,
	.entries = {
464 465 466 467
		{ 250000000, 166667, 0xb4, 0x12 },
		{ 455000000, 166667, 0xfe, 0x11 },
		{ 775500000, 166667, 0xbc, 0x18 },
		{ 999999999, 166667, 0xf4, 0x18 },
468 469
	}
};
470
EXPORT_SYMBOL(dvb_pll_thomson_fe6600);
471
static void opera1_bw(u8 *buf, const struct dvb_frontend_parameters *params)
472
{
473
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
474 475 476 477 478 479 480 481
		buf[2] |= 0x08;
}

struct dvb_pll_desc dvb_pll_opera1 = {
	.name  = "Opera Tuner",
	.min   =  900000,
	.max   = 2250000,
	.iffreq= 0,
482
	.set   = opera1_bw,
483 484 485 486 487 488 489 490 491 492 493 494 495
	.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 },
	}
};
EXPORT_SYMBOL(dvb_pll_opera1);
496

497 498 499 500 501 502 503 504 505 506 507 508 509
struct dvb_pll_priv {
	/* 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;
};

L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517
/* ----------------------------------------------------------- */
/* code                                                        */

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

int dvb_pll_configure(struct dvb_pll_desc *desc, u8 *buf,
518
		      const struct dvb_frontend_parameters *params)
L
Linus Torvalds 已提交
519 520 521 522
{
	u32 div;
	int i;

523 524 525
	if (params->frequency != 0 && (params->frequency < desc->min ||
				       params->frequency > desc->max))
		return -EINVAL;
L
Linus Torvalds 已提交
526 527

	for (i = 0; i < desc->count; i++) {
528
		if (params->frequency > desc->entries[i].limit)
L
Linus Torvalds 已提交
529 530 531
			continue;
		break;
	}
532

L
Linus Torvalds 已提交
533
	if (debug)
534 535
		printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
		       params->frequency, i, desc->count);
536 537
	if (i == desc->count)
		return -EINVAL;
L
Linus Torvalds 已提交
538

539 540
	div = (params->frequency + desc->iffreq +
	       desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
L
Linus Torvalds 已提交
541 542
	buf[0] = div >> 8;
	buf[1] = div & 0xff;
543 544
	buf[2] = desc->entries[i].config;
	buf[3] = desc->entries[i].cb;
L
Linus Torvalds 已提交
545

546 547
	if (desc->set)
		desc->set(buf, params);
L
Linus Torvalds 已提交
548 549 550 551 552

	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]);

553
	// calculate the frequency we set it to
554
	return (div * desc->entries[i].stepsize) - desc->iffreq;
L
Linus Torvalds 已提交
555 556 557
}
EXPORT_SYMBOL(dvb_pll_configure);

558 559
static int dvb_pll_release(struct dvb_frontend *fe)
{
560
	kfree(fe->tuner_priv);
561 562 563 564 565 566 567 568
	fe->tuner_priv = NULL;
	return 0;
}

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

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

572 573 574 575 576
	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] };
577

578
		int result;
579

580 581 582 583 584 585
		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;
586
	}
587 588
	/* Shouldn't be called when initdata is NULL, maybe BUG()? */
	return -EINVAL;
589 590
}

591 592
static int dvb_pll_set_params(struct dvb_frontend *fe,
			      struct dvb_frontend_parameters *params)
593 594 595 596
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	u8 buf[4];
	struct i2c_msg msg =
597 598
		{ .addr = priv->pll_i2c_address, .flags = 0,
		  .buf = buf, .len = sizeof(buf) };
599
	int result;
600
	u32 frequency = 0;
601 602 603 604

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

605
	if ((result = dvb_pll_configure(priv->pll_desc, buf, params)) < 0)
606
		return result;
607 608
	else
		frequency = result;
609

610 611
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
612 613 614 615
	if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
		return result;
	}

616
	priv->frequency = frequency;
617
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
618 619 620 621

	return 0;
}

622 623 624
static int dvb_pll_calc_regs(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *params,
			     u8 *buf, int buf_len)
625 626 627
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	int result;
628
	u32 frequency = 0;
629 630 631 632

	if (buf_len < 5)
		return -EINVAL;

633
	if ((result = dvb_pll_configure(priv->pll_desc, buf+1, params)) < 0)
634
		return result;
635 636 637
	else
		frequency = result;

638 639
	buf[0] = priv->pll_i2c_address;

640
	priv->frequency = frequency;
641
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

	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;
}

660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
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;
}

685 686 687
static struct dvb_tuner_ops dvb_pll_tuner_ops = {
	.release = dvb_pll_release,
	.sleep = dvb_pll_sleep,
688
	.init = dvb_pll_init,
689
	.set_params = dvb_pll_set_params,
690
	.calc_regs = dvb_pll_calc_regs,
691 692 693 694
	.get_frequency = dvb_pll_get_frequency,
	.get_bandwidth = dvb_pll_get_bandwidth,
};

695 696 697
struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
				    struct i2c_adapter *i2c,
				    struct dvb_pll_desc *desc)
698
{
699
	u8 b1 [] = { 0 };
700 701
	struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
			       .buf = b1, .len = 1 };
702
	struct dvb_pll_priv *priv = NULL;
703 704
	int ret;

705 706 707 708
	if (i2c != NULL) {
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);

709 710
		ret = i2c_transfer (i2c, &msg, 1);
		if (ret != 1)
711
			return NULL;
712 713 714
		if (fe->ops.i2c_gate_ctrl)
			     fe->ops.i2c_gate_ctrl(fe, 0);
	}
715 716 717

	priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
	if (priv == NULL)
718
		return NULL;
719 720 721 722 723

	priv->pll_i2c_address = pll_addr;
	priv->i2c = i2c;
	priv->pll_desc = desc;

724 725 726
	memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
	       sizeof(struct dvb_tuner_ops));

727 728
	strncpy(fe->ops.tuner_ops.info.name, desc->name,
		sizeof(fe->ops.tuner_ops.info.name));
729 730
	fe->ops.tuner_ops.info.frequency_min = desc->min;
	fe->ops.tuner_ops.info.frequency_min = desc->max;
731 732 733 734
	if (!desc->initdata)
		fe->ops.tuner_ops.init = NULL;
	if (!desc->sleepdata)
		fe->ops.tuner_ops.sleep = NULL;
735 736

	fe->tuner_priv = priv;
737
	return fe;
738 739 740
}
EXPORT_SYMBOL(dvb_pll_attach);

L
Linus Torvalds 已提交
741 742 743
MODULE_DESCRIPTION("dvb pll library");
MODULE_AUTHOR("Gerd Knorr");
MODULE_LICENSE("GPL");