dvb-pll.c 18.4 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

static unsigned int dvb_pll_devcount;

47
static int debug;
48 49 50
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "enable verbose debug messages");

51 52 53 54 55
static unsigned int id[DVB_PLL_MAX] =
	{ [ 0 ... (DVB_PLL_MAX-1) ] = DVB_PLL_UNDEFINED };
module_param_array(id, int, NULL, 0644);
MODULE_PARM_DESC(id, "force pll id to use (DEBUG ONLY)");

56 57
/* ----------------------------------------------------------- */

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

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

79
static struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
L
Linus Torvalds 已提交
80 81 82
	.name  = "Thomson dtt7579",
	.min   = 177000000,
	.max   = 858000000,
83
	.iffreq= 36166667,
84 85
	.sleepdata = (u8[]){ 2, 0xb4, 0x03 },
	.count = 4,
L
Linus Torvalds 已提交
86
	.entries = {
87 88 89 90
		{  443250000, 166667, 0xb4, 0x02 },
		{  542000000, 166667, 0xb4, 0x08 },
		{  771000000, 166667, 0xbc, 0x08 },
		{  999999999, 166667, 0xf4, 0x08 },
L
Linus Torvalds 已提交
91 92 93
	},
};

94
static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf,
95
			       const struct dvb_frontend_parameters *params)
L
Linus Torvalds 已提交
96
{
97
	if (BANDWIDTH_7_MHZ == params->u.ofdm.bandwidth)
L
Linus Torvalds 已提交
98 99 100
		buf[3] |= 0x10;
}

101
static struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
L
Linus Torvalds 已提交
102 103 104
	.name  = "Thomson dtt759x",
	.min   = 177000000,
	.max   = 896000000,
105
	.set   = thomson_dtt759x_bw,
106
	.iffreq= 36166667,
107 108
	.sleepdata = (u8[]){ 2, 0x84, 0x03 },
	.count = 5,
L
Linus Torvalds 已提交
109
	.entries = {
110 111 112 113 114
		{  264000000, 166667, 0xb4, 0x02 },
		{  470000000, 166667, 0xbc, 0x02 },
		{  735000000, 166667, 0xbc, 0x08 },
		{  835000000, 166667, 0xf4, 0x08 },
		{  999999999, 166667, 0xfc, 0x08 },
L
Linus Torvalds 已提交
115 116 117
	},
};

118
static struct dvb_pll_desc dvb_pll_lg_z201 = {
L
Linus Torvalds 已提交
119 120 121
	.name  = "LG z201",
	.min   = 174000000,
	.max   = 862000000,
122
	.iffreq= 36166667,
123 124
	.sleepdata = (u8[]){ 2, 0xbc, 0x03 },
	.count = 5,
L
Linus Torvalds 已提交
125
	.entries = {
126 127 128 129 130
		{  157500000, 166667, 0xbc, 0x01 },
		{  443250000, 166667, 0xbc, 0x02 },
		{  542000000, 166667, 0xbc, 0x04 },
		{  830000000, 166667, 0xf4, 0x04 },
		{  999999999, 166667, 0xfc, 0x04 },
L
Linus Torvalds 已提交
131 132 133
	},
};

134
static struct dvb_pll_desc dvb_pll_unknown_1 = {
L
Linus Torvalds 已提交
135 136 137
	.name  = "unknown 1", /* used by dntv live dvb-t */
	.min   = 174000000,
	.max   = 862000000,
138
	.iffreq= 36166667,
L
Linus Torvalds 已提交
139 140
	.count = 9,
	.entries = {
141 142 143 144 145 146 147 148 149
		{  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 已提交
150 151 152
	},
};

153 154 155
/* Infineon TUA6010XS
 * used in Thomson Cable Tuner
 */
156
static struct dvb_pll_desc dvb_pll_tua6010xs = {
157 158 159
	.name  = "Infineon TUA6010XS",
	.min   =  44250000,
	.max   = 858000000,
160
	.iffreq= 36125000,
161 162
	.count = 3,
	.entries = {
163 164 165
		{  115750000, 62500, 0x8e, 0x03 },
		{  403250000, 62500, 0x8e, 0x06 },
		{  999999999, 62500, 0x8e, 0x85 },
166 167 168 169
	},
};

/* Panasonic env57h1xd5 (some Philips PLL ?) */
170
static struct dvb_pll_desc dvb_pll_env57h1xd5 = {
171 172 173
	.name  = "Panasonic ENV57H1XD5",
	.min   =  44250000,
	.max   = 858000000,
174
	.iffreq= 36125000,
175 176
	.count = 4,
	.entries = {
177 178 179 180
		{  153000000, 166667, 0xc2, 0x41 },
		{  470000000, 166667, 0xc2, 0x42 },
		{  526000000, 166667, 0xc2, 0x84 },
		{  999999999, 166667, 0xc2, 0xa4 },
181 182 183 184 185 186
	},
};

/* Philips TDA6650/TDA6651
 * used in Panasonic ENV77H11D5
 */
187 188
static void tda665x_bw(struct dvb_frontend *fe, u8 *buf,
		       const struct dvb_frontend_parameters *params)
189
{
190
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
191 192 193
		buf[3] |= 0x08;
}

194
static struct dvb_pll_desc dvb_pll_tda665x = {
195 196 197
	.name  = "Philips TDA6650/TDA6651",
	.min   =  44250000,
	.max   = 858000000,
198
	.set   = tda665x_bw,
199
	.iffreq= 36166667,
200
	.initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
201 202
	.count = 12,
	.entries = {
203 204 205 206 207 208 209 210 211 212 213 214
		{   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 */ },
215 216 217 218 219 220
	}
};

/* Infineon TUA6034
 * used in LG TDTP E102P
 */
221 222
static void tua6034_bw(struct dvb_frontend *fe, u8 *buf,
		       const struct dvb_frontend_parameters *params)
223
{
224
	if (BANDWIDTH_7_MHZ != params->u.ofdm.bandwidth)
225 226 227
		buf[3] |= 0x08;
}

228
static struct dvb_pll_desc dvb_pll_tua6034 = {
229 230 231
	.name  = "Infineon TUA6034",
	.min   =  44250000,
	.max   = 858000000,
232
	.iffreq= 36166667,
233
	.count = 3,
234
	.set   = tua6034_bw,
235
	.entries = {
236 237 238
		{  174500000, 62500, 0xce, 0x01 },
		{  230000000, 62500, 0xce, 0x02 },
		{  999999999, 62500, 0xce, 0x04 },
239 240 241
	},
};

242 243 244
/* ALPS TDED4
 * used in Nebula-Cards and USB boxes
 */
245 246
static void tded4_bw(struct dvb_frontend *fe, u8 *buf,
		     const struct dvb_frontend_parameters *params)
247
{
248
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
249 250 251
		buf[3] |= 0x04;
}

252
static struct dvb_pll_desc dvb_pll_tded4 = {
253 254 255
	.name = "ALPS TDED4",
	.min = 47000000,
	.max = 863000000,
256
	.iffreq= 36166667,
257
	.set   = tded4_bw,
258 259
	.count = 4,
	.entries = {
260 261 262 263
		{ 153000000, 166667, 0x85, 0x01 },
		{ 470000000, 166667, 0x85, 0x02 },
		{ 823000000, 166667, 0x85, 0x08 },
		{ 999999999, 166667, 0x85, 0x88 },
264 265 266
	}
};

267 268 269
/* ALPS TDHU2
 * used in AverTVHD MCE A180
 */
270
static struct dvb_pll_desc dvb_pll_tdhu2 = {
271 272 273
	.name = "ALPS TDHU2",
	.min = 54000000,
	.max = 864000000,
274
	.iffreq= 44000000,
275 276
	.count = 4,
	.entries = {
277 278 279 280
		{ 162000000, 62500, 0x85, 0x01 },
		{ 426000000, 62500, 0x85, 0x02 },
		{ 782000000, 62500, 0x85, 0x08 },
		{ 999999999, 62500, 0x85, 0x88 },
281 282 283
	}
};

284
/* Samsung TBMV30111IN / TBMV30712IN1
285 286
 * used in Air2PC ATSC - 2nd generation (nxt2002)
 */
287
static struct dvb_pll_desc dvb_pll_samsung_tbmv = {
288
	.name = "Samsung TBMV30111IN / TBMV30712IN1",
289 290
	.min = 54000000,
	.max = 860000000,
291
	.iffreq= 44000000,
292
	.count = 6,
293
	.entries = {
294 295 296 297 298 299
		{ 172000000, 166667, 0xb4, 0x01 },
		{ 214000000, 166667, 0xb4, 0x02 },
		{ 467000000, 166667, 0xbc, 0x02 },
		{ 721000000, 166667, 0xbc, 0x08 },
		{ 841000000, 166667, 0xf4, 0x08 },
		{ 999999999, 166667, 0xfc, 0x02 },
300 301 302
	}
};

303 304 305
/*
 * Philips SD1878 Tuner.
 */
306
static struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
307 308 309
	.name  = "Philips SD1878",
	.min   =  950000,
	.max   = 2150000,
310
	.iffreq= 249, /* zero-IF, offset 249 is to round up */
311 312
	.count = 4,
	.entries = {
313
		{ 1250000, 500, 0xc4, 0x00},
314
		{ 1450000, 500, 0xc4, 0x40},
315 316
		{ 2050000, 500, 0xc4, 0x80},
		{ 2150000, 500, 0xc4, 0xc0},
317 318 319
	},
};

320 321
static void opera1_bw(struct dvb_frontend *fe, u8 *buf,
		      const struct dvb_frontend_parameters *params)
322
{
323
	if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
324 325 326
		buf[2] |= 0x08;
}

327
static struct dvb_pll_desc dvb_pll_opera1 = {
328 329 330 331
	.name  = "Opera Tuner",
	.min   =  900000,
	.max   = 2250000,
	.iffreq= 0,
332
	.set   = opera1_bw,
333 334 335 336 337 338 339 340 341 342 343 344
	.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 },
	}
};
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
static void samsung_dtos403ih102a_set(struct dvb_frontend *fe, u8 *buf,
		       const struct dvb_frontend_parameters *params)
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	struct i2c_msg msg = {
		.addr = priv->pll_i2c_address,
		.flags = 0,
		.buf = buf,
		.len = 4
	};
	int result;

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);

	result = i2c_transfer(priv->i2c, &msg, 1);
	if (result != 1)
		printk(KERN_ERR "%s: i2c_transfer failed:%d",
			__func__, result);

	buf[2] = 0x9e;
	buf[3] = 0x90;

	return;
}

/* unknown pll used in Samsung DTOS403IH102A DVB-C tuner */
static struct dvb_pll_desc dvb_pll_samsung_dtos403ih102a = {
	.name   = "Samsung DTOS403IH102A",
	.min    =  44250000,
	.max    = 858000000,
	.iffreq =  36125000,
	.count  = 8,
	.set    = samsung_dtos403ih102a_set,
	.entries = {
		{ 135000000, 62500, 0xbe, 0x01 },
		{ 177000000, 62500, 0xf6, 0x01 },
		{ 370000000, 62500, 0xbe, 0x02 },
		{ 450000000, 62500, 0xf6, 0x02 },
		{ 466000000, 62500, 0xfe, 0x02 },
		{ 538000000, 62500, 0xbe, 0x08 },
		{ 826000000, 62500, 0xf6, 0x08 },
		{ 999999999, 62500, 0xfe, 0x08 },
	}
};

392 393 394 395 396 397 398 399 400 401 402 403 404 405
/* Samsung TDTC9251DH0 DVB-T NIM, as used on AirStar 2 */
static struct dvb_pll_desc dvb_pll_samsung_tdtc9251dh0 = {
	.name	= "Samsung TDTC9251DH0",
	.min	=  48000000,
	.max	= 863000000,
	.iffreq	=  36166667,
	.count	= 3,
	.entries = {
		{ 157500000, 166667, 0xcc, 0x09 },
		{ 443000000, 166667, 0xcc, 0x0a },
		{ 863000000, 166667, 0xcc, 0x08 },
	}
};

406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
/* Samsung TBDU18132 DVB-S NIM with TSA5059 PLL, used in SkyStar2 DVB-S 2.3 */
static struct dvb_pll_desc dvb_pll_samsung_tbdu18132 = {
	.name = "Samsung TBDU18132",
	.min	=  950000,
	.max	= 2150000, /* guesses */
	.iffreq = 0,
	.count = 2,
	.entries = {
		{ 1550000, 125, 0x84, 0x82 },
		{ 4095937, 125, 0x84, 0x80 },
	}
	/* TSA5059 PLL has a 17 bit divisor rather than the 15 bits supported
	 * by this driver.  The two extra bits are 0x60 in the third byte.  15
	 * bits is enough for over 4 GHz, which is enough to cover the range
	 * of this tuner.  We could use the additional divisor bits by adding
	 * more entries, e.g.
	 { 0x0ffff * 125 + 125/2, 125, 0x84 | 0x20, },
	 { 0x17fff * 125 + 125/2, 125, 0x84 | 0x40, },
	 { 0x1ffff * 125 + 125/2, 125, 0x84 | 0x60, }, */
};

427 428 429 430 431 432 433 434 435 436 437 438 439
/* Samsung TBMU24112 DVB-S NIM with SL1935 zero-IF tuner */
static struct dvb_pll_desc dvb_pll_samsung_tbmu24112 = {
	.name = "Samsung TBMU24112",
	.min	=  950000,
	.max	= 2150000, /* guesses */
	.iffreq = 0,
	.count = 2,
	.entries = {
		{ 1500000, 125, 0x84, 0x18 },
		{ 9999999, 125, 0x84, 0x08 },
	}
};

440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
/* ----------------------------------------------------------- */

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_LG_Z201]                = &dvb_pll_lg_z201,
	[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_TDA665X]                = &dvb_pll_tda665x,
	[DVB_PLL_TDED4]                  = &dvb_pll_tded4,
	[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_OPERA1]                 = &dvb_pll_opera1,
457
	[DVB_PLL_SAMSUNG_DTOS403IH102A]  = &dvb_pll_samsung_dtos403ih102a,
458
	[DVB_PLL_SAMSUNG_TDTC9251DH0]    = &dvb_pll_samsung_tdtc9251dh0,
459
	[DVB_PLL_SAMSUNG_TBDU18132]	 = &dvb_pll_samsung_tbdu18132,
460
	[DVB_PLL_SAMSUNG_TBMU24112]      = &dvb_pll_samsung_tbmu24112,
461 462
};

L
Linus Torvalds 已提交
463 464 465
/* ----------------------------------------------------------- */
/* code                                                        */

466
static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
467
			     const struct dvb_frontend_parameters *params)
L
Linus Torvalds 已提交
468
{
469 470
	struct dvb_pll_priv *priv = fe->tuner_priv;
	struct dvb_pll_desc *desc = priv->pll_desc;
L
Linus Torvalds 已提交
471 472 473
	u32 div;
	int i;

474 475 476
	if (params->frequency != 0 && (params->frequency < desc->min ||
				       params->frequency > desc->max))
		return -EINVAL;
L
Linus Torvalds 已提交
477 478

	for (i = 0; i < desc->count; i++) {
479
		if (params->frequency > desc->entries[i].limit)
L
Linus Torvalds 已提交
480 481 482
			continue;
		break;
	}
483

L
Linus Torvalds 已提交
484
	if (debug)
485 486
		printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
		       params->frequency, i, desc->count);
487 488
	if (i == desc->count)
		return -EINVAL;
L
Linus Torvalds 已提交
489

490 491
	div = (params->frequency + desc->iffreq +
	       desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
L
Linus Torvalds 已提交
492 493
	buf[0] = div >> 8;
	buf[1] = div & 0xff;
494 495
	buf[2] = desc->entries[i].config;
	buf[3] = desc->entries[i].cb;
L
Linus Torvalds 已提交
496

497
	if (desc->set)
498
		desc->set(fe, buf, params);
L
Linus Torvalds 已提交
499 500 501 502 503

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

504
	// calculate the frequency we set it to
505
	return (div * desc->entries[i].stepsize) - desc->iffreq;
L
Linus Torvalds 已提交
506 507
}

508 509
static int dvb_pll_release(struct dvb_frontend *fe)
{
510
	kfree(fe->tuner_priv);
511 512 513 514 515 516 517 518
	fe->tuner_priv = NULL;
	return 0;
}

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

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

522 523 524 525 526
	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] };
527

528
		int result;
529

530 531 532 533 534 535
		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;
536
	}
537 538
	/* Shouldn't be called when initdata is NULL, maybe BUG()? */
	return -EINVAL;
539 540
}

541 542
static int dvb_pll_set_params(struct dvb_frontend *fe,
			      struct dvb_frontend_parameters *params)
543 544 545 546
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	u8 buf[4];
	struct i2c_msg msg =
547 548
		{ .addr = priv->pll_i2c_address, .flags = 0,
		  .buf = buf, .len = sizeof(buf) };
549
	int result;
550
	u32 frequency = 0;
551 552 553 554

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

555
	if ((result = dvb_pll_configure(fe, buf, params)) < 0)
556
		return result;
557 558
	else
		frequency = result;
559

560 561
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1);
562 563 564 565
	if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
		return result;
	}

566
	priv->frequency = frequency;
567
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
568 569 570 571

	return 0;
}

572 573 574
static int dvb_pll_calc_regs(struct dvb_frontend *fe,
			     struct dvb_frontend_parameters *params,
			     u8 *buf, int buf_len)
575 576 577
{
	struct dvb_pll_priv *priv = fe->tuner_priv;
	int result;
578
	u32 frequency = 0;
579 580 581 582

	if (buf_len < 5)
		return -EINVAL;

583
	if ((result = dvb_pll_configure(fe, buf+1, params)) < 0)
584
		return result;
585 586 587
	else
		frequency = result;

588 589
	buf[0] = priv->pll_i2c_address;

590
	priv->frequency = frequency;
591
	priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

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

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
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;
}

635 636 637
static struct dvb_tuner_ops dvb_pll_tuner_ops = {
	.release = dvb_pll_release,
	.sleep = dvb_pll_sleep,
638
	.init = dvb_pll_init,
639
	.set_params = dvb_pll_set_params,
640
	.calc_regs = dvb_pll_calc_regs,
641 642 643 644
	.get_frequency = dvb_pll_get_frequency,
	.get_bandwidth = dvb_pll_get_bandwidth,
};

645 646
struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
				    struct i2c_adapter *i2c,
647
				    unsigned int pll_desc_id)
648
{
649
	u8 b1 [] = { 0 };
650 651
	struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
			       .buf = b1, .len = 1 };
652
	struct dvb_pll_priv *priv = NULL;
653
	int ret;
654 655
	struct dvb_pll_desc *desc;

656 657 658 659
	if ((id[dvb_pll_devcount] > DVB_PLL_UNDEFINED) &&
	    (id[dvb_pll_devcount] < ARRAY_SIZE(pll_list)))
		pll_desc_id = id[dvb_pll_devcount];

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

	desc = pll_list[pll_desc_id];
663

664 665 666 667
	if (i2c != NULL) {
		if (fe->ops.i2c_gate_ctrl)
			fe->ops.i2c_gate_ctrl(fe, 1);

668 669
		ret = i2c_transfer (i2c, &msg, 1);
		if (ret != 1)
670
			return NULL;
671 672 673
		if (fe->ops.i2c_gate_ctrl)
			     fe->ops.i2c_gate_ctrl(fe, 0);
	}
674 675 676

	priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
	if (priv == NULL)
677
		return NULL;
678 679 680 681

	priv->pll_i2c_address = pll_addr;
	priv->i2c = i2c;
	priv->pll_desc = desc;
682
	priv->nr = dvb_pll_devcount++;
683

684 685 686
	memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
	       sizeof(struct dvb_tuner_ops));

687 688
	strncpy(fe->ops.tuner_ops.info.name, desc->name,
		sizeof(fe->ops.tuner_ops.info.name));
689
	fe->ops.tuner_ops.info.frequency_min = desc->min;
690
	fe->ops.tuner_ops.info.frequency_max = desc->max;
691 692 693 694
	if (!desc->initdata)
		fe->ops.tuner_ops.init = NULL;
	if (!desc->sleepdata)
		fe->ops.tuner_ops.sleep = NULL;
695 696

	fe->tuner_priv = priv;
697

698
	if ((debug) || (id[priv->nr] == pll_desc_id)) {
699 700 701
		printk("dvb-pll[%d]", priv->nr);
		if (i2c != NULL)
			printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
702 703 704
		printk(": id# %d (%s) attached, %s\n", pll_desc_id, desc->name,
		       id[priv->nr] == pll_desc_id ?
				"insmod option" : "autodetected");
705
	}
706

707
	return fe;
708 709 710
}
EXPORT_SYMBOL(dvb_pll_attach);

L
Linus Torvalds 已提交
711 712 713
MODULE_DESCRIPTION("dvb pll library");
MODULE_AUTHOR("Gerd Knorr");
MODULE_LICENSE("GPL");