V4L/DVB (6227): dvb-pll: store instance ID in dvb_pll_priv structure
[safe/jmp/linux-2.6] / drivers / media / dvb / frontends / dvb-pll.c
1 /*
2  * descriptions + helper functions for simple dvb plls.
3  *
4  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include <linux/module.h>
22 #include <linux/dvb/frontend.h>
23 #include <asm/types.h>
24
25 #include "dvb-pll.h"
26
27 struct dvb_pll_desc {
28         char *name;
29         u32  min;
30         u32  max;
31         u32  iffreq;
32         void (*set)(struct dvb_frontend *fe, u8 *buf,
33                     const struct dvb_frontend_parameters *params);
34         u8   *initdata;
35         u8   *sleepdata;
36         int  count;
37         struct {
38                 u32 limit;
39                 u32 stepsize;
40                 u8  config;
41                 u8  cb;
42         } entries[12];
43 };
44
45 /* ----------------------------------------------------------- */
46 /* descriptions                                                */
47
48 /* Set AGC TOP value to 103 dBuV:
49         0x80 = Control Byte
50         0x40 = 250 uA charge pump (irrelevant)
51         0x18 = Aux Byte to follow
52         0x06 = 64.5 kHz divider (irrelevant)
53         0x01 = Disable Vt (aka sleep)
54
55         0x00 = AGC Time constant 2s Iagc = 300 nA (vs 0x80 = 9 nA)
56         0x50 = AGC Take over point = 103 dBuV */
57 static u8 tua603x_agc103[] = { 2, 0x80|0x40|0x18|0x06|0x01, 0x00|0x50 };
58
59 /*      0x04 = 166.67 kHz divider
60
61         0x80 = AGC Time constant 50ms Iagc = 9 uA
62         0x20 = AGC Take over point = 112 dBuV */
63 static u8 tua603x_agc112[] = { 2, 0x80|0x40|0x18|0x04|0x01, 0x80|0x20 };
64
65 static struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
66         .name  = "Thomson dtt7579",
67         .min   = 177000000,
68         .max   = 858000000,
69         .iffreq= 36166667,
70         .sleepdata = (u8[]){ 2, 0xb4, 0x03 },
71         .count = 4,
72         .entries = {
73                 {  443250000, 166667, 0xb4, 0x02 },
74                 {  542000000, 166667, 0xb4, 0x08 },
75                 {  771000000, 166667, 0xbc, 0x08 },
76                 {  999999999, 166667, 0xf4, 0x08 },
77         },
78 };
79
80 static struct dvb_pll_desc dvb_pll_thomson_dtt7610 = {
81         .name  = "Thomson dtt7610",
82         .min   =  44000000,
83         .max   = 958000000,
84         .iffreq= 44000000,
85         .count = 3,
86         .entries = {
87                 { 157250000, 62500, 0x8e, 0x39 },
88                 { 454000000, 62500, 0x8e, 0x3a },
89                 { 999999999, 62500, 0x8e, 0x3c },
90         },
91 };
92
93 static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf,
94                                const struct dvb_frontend_parameters *params)
95 {
96         if (BANDWIDTH_7_MHZ == params->u.ofdm.bandwidth)
97                 buf[3] |= 0x10;
98 }
99
100 static struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
101         .name  = "Thomson dtt759x",
102         .min   = 177000000,
103         .max   = 896000000,
104         .set   = thomson_dtt759x_bw,
105         .iffreq= 36166667,
106         .sleepdata = (u8[]){ 2, 0x84, 0x03 },
107         .count = 5,
108         .entries = {
109                 {  264000000, 166667, 0xb4, 0x02 },
110                 {  470000000, 166667, 0xbc, 0x02 },
111                 {  735000000, 166667, 0xbc, 0x08 },
112                 {  835000000, 166667, 0xf4, 0x08 },
113                 {  999999999, 166667, 0xfc, 0x08 },
114         },
115 };
116
117 static struct dvb_pll_desc dvb_pll_lg_z201 = {
118         .name  = "LG z201",
119         .min   = 174000000,
120         .max   = 862000000,
121         .iffreq= 36166667,
122         .sleepdata = (u8[]){ 2, 0xbc, 0x03 },
123         .count = 5,
124         .entries = {
125                 {  157500000, 166667, 0xbc, 0x01 },
126                 {  443250000, 166667, 0xbc, 0x02 },
127                 {  542000000, 166667, 0xbc, 0x04 },
128                 {  830000000, 166667, 0xf4, 0x04 },
129                 {  999999999, 166667, 0xfc, 0x04 },
130         },
131 };
132
133 static struct dvb_pll_desc dvb_pll_microtune_4042 = {
134         .name  = "Microtune 4042 FI5",
135         .min   =  57000000,
136         .max   = 858000000,
137         .iffreq= 44000000,
138         .count = 3,
139         .entries = {
140                 { 162000000, 62500, 0x8e, 0xa1 },
141                 { 457000000, 62500, 0x8e, 0x91 },
142                 { 999999999, 62500, 0x8e, 0x31 },
143         },
144 };
145
146 static struct dvb_pll_desc dvb_pll_thomson_dtt761x = {
147         /* DTT 7611 7611A 7612 7613 7613A 7614 7615 7615A */
148         .name  = "Thomson dtt761x",
149         .min   =  57000000,
150         .max   = 863000000,
151         .iffreq= 44000000,
152         .count = 3,
153         .initdata = tua603x_agc103,
154         .entries = {
155                 { 147000000, 62500, 0x8e, 0x39 },
156                 { 417000000, 62500, 0x8e, 0x3a },
157                 { 999999999, 62500, 0x8e, 0x3c },
158         },
159 };
160
161 static struct dvb_pll_desc dvb_pll_unknown_1 = {
162         .name  = "unknown 1", /* used by dntv live dvb-t */
163         .min   = 174000000,
164         .max   = 862000000,
165         .iffreq= 36166667,
166         .count = 9,
167         .entries = {
168                 {  150000000, 166667, 0xb4, 0x01 },
169                 {  173000000, 166667, 0xbc, 0x01 },
170                 {  250000000, 166667, 0xb4, 0x02 },
171                 {  400000000, 166667, 0xbc, 0x02 },
172                 {  420000000, 166667, 0xf4, 0x02 },
173                 {  470000000, 166667, 0xfc, 0x02 },
174                 {  600000000, 166667, 0xbc, 0x08 },
175                 {  730000000, 166667, 0xf4, 0x08 },
176                 {  999999999, 166667, 0xfc, 0x08 },
177         },
178 };
179
180 /* Infineon TUA6010XS
181  * used in Thomson Cable Tuner
182  */
183 static struct dvb_pll_desc dvb_pll_tua6010xs = {
184         .name  = "Infineon TUA6010XS",
185         .min   =  44250000,
186         .max   = 858000000,
187         .iffreq= 36125000,
188         .count = 3,
189         .entries = {
190                 {  115750000, 62500, 0x8e, 0x03 },
191                 {  403250000, 62500, 0x8e, 0x06 },
192                 {  999999999, 62500, 0x8e, 0x85 },
193         },
194 };
195
196 /* Panasonic env57h1xd5 (some Philips PLL ?) */
197 static struct dvb_pll_desc dvb_pll_env57h1xd5 = {
198         .name  = "Panasonic ENV57H1XD5",
199         .min   =  44250000,
200         .max   = 858000000,
201         .iffreq= 36125000,
202         .count = 4,
203         .entries = {
204                 {  153000000, 166667, 0xc2, 0x41 },
205                 {  470000000, 166667, 0xc2, 0x42 },
206                 {  526000000, 166667, 0xc2, 0x84 },
207                 {  999999999, 166667, 0xc2, 0xa4 },
208         },
209 };
210
211 /* Philips TDA6650/TDA6651
212  * used in Panasonic ENV77H11D5
213  */
214 static void tda665x_bw(struct dvb_frontend *fe, u8 *buf,
215                        const struct dvb_frontend_parameters *params)
216 {
217         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
218                 buf[3] |= 0x08;
219 }
220
221 static struct dvb_pll_desc dvb_pll_tda665x = {
222         .name  = "Philips TDA6650/TDA6651",
223         .min   =  44250000,
224         .max   = 858000000,
225         .set   = tda665x_bw,
226         .iffreq= 36166667,
227         .initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
228         .count = 12,
229         .entries = {
230                 {   93834000, 166667, 0xca, 0x61 /* 011 0 0 0  01 */ },
231                 {  123834000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
232                 {  161000000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
233                 {  163834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
234                 {  253834000, 166667, 0xca, 0x62 /* 011 0 0 0  10 */ },
235                 {  383834000, 166667, 0xca, 0xa2 /* 101 0 0 0  10 */ },
236                 {  443834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
237                 {  444000000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
238                 {  583834000, 166667, 0xca, 0x64 /* 011 0 0 1  00 */ },
239                 {  793834000, 166667, 0xca, 0xa4 /* 101 0 0 1  00 */ },
240                 {  444834000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
241                 {  861000000, 166667, 0xca, 0xe4 /* 111 0 0 1  00 */ },
242         }
243 };
244
245 /* Infineon TUA6034
246  * used in LG TDTP E102P
247  */
248 static void tua6034_bw(struct dvb_frontend *fe, u8 *buf,
249                        const struct dvb_frontend_parameters *params)
250 {
251         if (BANDWIDTH_7_MHZ != params->u.ofdm.bandwidth)
252                 buf[3] |= 0x08;
253 }
254
255 static struct dvb_pll_desc dvb_pll_tua6034 = {
256         .name  = "Infineon TUA6034",
257         .min   =  44250000,
258         .max   = 858000000,
259         .iffreq= 36166667,
260         .count = 3,
261         .set   = tua6034_bw,
262         .entries = {
263                 {  174500000, 62500, 0xce, 0x01 },
264                 {  230000000, 62500, 0xce, 0x02 },
265                 {  999999999, 62500, 0xce, 0x04 },
266         },
267 };
268
269 /* Infineon TUA6034
270  * used in LG TDVS-H061F, LG TDVS-H062F and LG TDVS-H064F
271  */
272 static struct dvb_pll_desc dvb_pll_lg_tdvs_h06xf = {
273         .name  = "LG TDVS-H06xF",
274         .min   =  54000000,
275         .max   = 863000000,
276         .iffreq= 44000000,
277         .initdata = tua603x_agc103,
278         .count = 3,
279         .entries = {
280                 {  165000000, 62500, 0xce, 0x01 },
281                 {  450000000, 62500, 0xce, 0x02 },
282                 {  999999999, 62500, 0xce, 0x04 },
283         },
284 };
285
286 /* Philips FMD1216ME
287  * used in Medion Hybrid PCMCIA card and USB Box
288  */
289 static void fmd1216me_bw(struct dvb_frontend *fe, u8 *buf,
290                          const struct dvb_frontend_parameters *params)
291 {
292         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ &&
293             params->frequency >= 158870000)
294                 buf[3] |= 0x08;
295 }
296
297 static struct dvb_pll_desc dvb_pll_fmd1216me = {
298         .name = "Philips FMD1216ME",
299         .min = 50870000,
300         .max = 858000000,
301         .iffreq= 36125000,
302         .set   = fmd1216me_bw,
303         .initdata = tua603x_agc112,
304         .sleepdata = (u8[]){ 4, 0x9c, 0x60, 0x85, 0x54 },
305         .count = 7,
306         .entries = {
307                 { 143870000, 166667, 0xbc, 0x41 },
308                 { 158870000, 166667, 0xf4, 0x41 },
309                 { 329870000, 166667, 0xbc, 0x42 },
310                 { 441870000, 166667, 0xf4, 0x42 },
311                 { 625870000, 166667, 0xbc, 0x44 },
312                 { 803870000, 166667, 0xf4, 0x44 },
313                 { 999999999, 166667, 0xfc, 0x44 },
314         }
315 };
316
317 /* ALPS TDED4
318  * used in Nebula-Cards and USB boxes
319  */
320 static void tded4_bw(struct dvb_frontend *fe, u8 *buf,
321                      const struct dvb_frontend_parameters *params)
322 {
323         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
324                 buf[3] |= 0x04;
325 }
326
327 static struct dvb_pll_desc dvb_pll_tded4 = {
328         .name = "ALPS TDED4",
329         .min = 47000000,
330         .max = 863000000,
331         .iffreq= 36166667,
332         .set   = tded4_bw,
333         .count = 4,
334         .entries = {
335                 { 153000000, 166667, 0x85, 0x01 },
336                 { 470000000, 166667, 0x85, 0x02 },
337                 { 823000000, 166667, 0x85, 0x08 },
338                 { 999999999, 166667, 0x85, 0x88 },
339         }
340 };
341
342 /* ALPS TDHU2
343  * used in AverTVHD MCE A180
344  */
345 static struct dvb_pll_desc dvb_pll_tdhu2 = {
346         .name = "ALPS TDHU2",
347         .min = 54000000,
348         .max = 864000000,
349         .iffreq= 44000000,
350         .count = 4,
351         .entries = {
352                 { 162000000, 62500, 0x85, 0x01 },
353                 { 426000000, 62500, 0x85, 0x02 },
354                 { 782000000, 62500, 0x85, 0x08 },
355                 { 999999999, 62500, 0x85, 0x88 },
356         }
357 };
358
359 /* Philips TUV1236D
360  * used in ATI HDTV Wonder
361  */
362 static void tuv1236d_rf(struct dvb_frontend *fe, u8 *buf,
363                         const struct dvb_frontend_parameters *params)
364 {
365         switch (params->u.vsb.modulation) {
366                 case QAM_64:
367                 case QAM_256:
368                         buf[3] |= 0x08;
369                         break;
370                 case VSB_8:
371                 default:
372                         buf[3] &= ~0x08;
373         }
374 }
375
376 static struct dvb_pll_desc dvb_pll_tuv1236d = {
377         .name  = "Philips TUV1236D",
378         .min   =  54000000,
379         .max   = 864000000,
380         .iffreq= 44000000,
381         .set   = tuv1236d_rf,
382         .count = 3,
383         .entries = {
384                 { 157250000, 62500, 0xc6, 0x41 },
385                 { 454000000, 62500, 0xc6, 0x42 },
386                 { 999999999, 62500, 0xc6, 0x44 },
387         },
388 };
389
390 /* Samsung TBMV30111IN / TBMV30712IN1
391  * used in Air2PC ATSC - 2nd generation (nxt2002)
392  */
393 static struct dvb_pll_desc dvb_pll_samsung_tbmv = {
394         .name = "Samsung TBMV30111IN / TBMV30712IN1",
395         .min = 54000000,
396         .max = 860000000,
397         .iffreq= 44000000,
398         .count = 6,
399         .entries = {
400                 { 172000000, 166667, 0xb4, 0x01 },
401                 { 214000000, 166667, 0xb4, 0x02 },
402                 { 467000000, 166667, 0xbc, 0x02 },
403                 { 721000000, 166667, 0xbc, 0x08 },
404                 { 841000000, 166667, 0xf4, 0x08 },
405                 { 999999999, 166667, 0xfc, 0x02 },
406         }
407 };
408
409 /*
410  * Philips SD1878 Tuner.
411  */
412 static struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
413         .name  = "Philips SD1878",
414         .min   =  950000,
415         .max   = 2150000,
416         .iffreq= 249, /* zero-IF, offset 249 is to round up */
417         .count = 4,
418         .entries = {
419                 { 1250000, 500, 0xc4, 0x00},
420                 { 1550000, 500, 0xc4, 0x40},
421                 { 2050000, 500, 0xc4, 0x80},
422                 { 2150000, 500, 0xc4, 0xc0},
423         },
424 };
425
426 /*
427  * Philips TD1316 Tuner.
428  */
429 static void td1316_bw(struct dvb_frontend *fe, u8 *buf,
430                       const struct dvb_frontend_parameters *params)
431 {
432         u8 band;
433
434         /* determine band */
435         if (params->frequency < 161000000)
436                 band = 1;
437         else if (params->frequency < 444000000)
438                 band = 2;
439         else
440                 band = 4;
441
442         buf[3] |= band;
443
444         /* setup PLL filter */
445         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
446                 buf[3] |= 1 << 3;
447 }
448
449 static struct dvb_pll_desc dvb_pll_philips_td1316 = {
450         .name  = "Philips TD1316",
451         .min   =  87000000,
452         .max   = 895000000,
453         .iffreq= 36166667,
454         .set   = td1316_bw,
455         .count = 9,
456         .entries = {
457                 {  93834000, 166667, 0xca, 0x60},
458                 { 123834000, 166667, 0xca, 0xa0},
459                 { 163834000, 166667, 0xca, 0xc0},
460                 { 253834000, 166667, 0xca, 0x60},
461                 { 383834000, 166667, 0xca, 0xa0},
462                 { 443834000, 166667, 0xca, 0xc0},
463                 { 583834000, 166667, 0xca, 0x60},
464                 { 793834000, 166667, 0xca, 0xa0},
465                 { 858834000, 166667, 0xca, 0xe0},
466         },
467 };
468
469 /* FE6600 used on DViCO Hybrid */
470 static struct dvb_pll_desc dvb_pll_thomson_fe6600 = {
471         .name = "Thomson FE6600",
472         .min =  44250000,
473         .max = 858000000,
474         .iffreq= 36125000,
475         .count = 4,
476         .entries = {
477                 { 250000000, 166667, 0xb4, 0x12 },
478                 { 455000000, 166667, 0xfe, 0x11 },
479                 { 775500000, 166667, 0xbc, 0x18 },
480                 { 999999999, 166667, 0xf4, 0x18 },
481         }
482 };
483
484 static void opera1_bw(struct dvb_frontend *fe, u8 *buf,
485                       const struct dvb_frontend_parameters *params)
486 {
487         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
488                 buf[2] |= 0x08;
489 }
490
491 static struct dvb_pll_desc dvb_pll_opera1 = {
492         .name  = "Opera Tuner",
493         .min   =  900000,
494         .max   = 2250000,
495         .iffreq= 0,
496         .set   = opera1_bw,
497         .count = 8,
498         .entries = {
499                 { 1064000, 500, 0xe5, 0xc6 },
500                 { 1169000, 500, 0xe5, 0xe6 },
501                 { 1299000, 500, 0xe5, 0x24 },
502                 { 1444000, 500, 0xe5, 0x44 },
503                 { 1606000, 500, 0xe5, 0x64 },
504                 { 1777000, 500, 0xe5, 0x84 },
505                 { 1941000, 500, 0xe5, 0xa4 },
506                 { 2250000, 500, 0xe5, 0xc4 },
507         }
508 };
509
510 /* Philips FCV1236D
511  */
512 static struct dvb_pll_desc dvb_pll_fcv1236d = {
513 /* Bit_0: RF Input select
514  * Bit_1: 0=digital, 1=analog
515  */
516         .name  = "Philips FCV1236D",
517         .min   =  53000000,
518         .max   = 803000000,
519         .iffreq= 44000000,
520         .count = 3,
521         .entries = {
522                 { 159000000, 62500, 0x8e, 0xa0 },
523                 { 453000000, 62500, 0x8e, 0x90 },
524                 { 999999999, 62500, 0x8e, 0x30 },
525         },
526 };
527
528 /* ----------------------------------------------------------- */
529
530 static struct dvb_pll_desc *pll_list[] = {
531         [DVB_PLL_UNDEFINED]              = NULL,
532         [DVB_PLL_THOMSON_DTT7579]        = &dvb_pll_thomson_dtt7579,
533         [DVB_PLL_THOMSON_DTT759X]        = &dvb_pll_thomson_dtt759x,
534         [DVB_PLL_THOMSON_DTT7610]        = &dvb_pll_thomson_dtt7610,
535         [DVB_PLL_LG_Z201]                = &dvb_pll_lg_z201,
536         [DVB_PLL_MICROTUNE_4042]         = &dvb_pll_microtune_4042,
537         [DVB_PLL_THOMSON_DTT761X]        = &dvb_pll_thomson_dtt761x,
538         [DVB_PLL_UNKNOWN_1]              = &dvb_pll_unknown_1,
539         [DVB_PLL_TUA6010XS]              = &dvb_pll_tua6010xs,
540         [DVB_PLL_ENV57H1XD5]             = &dvb_pll_env57h1xd5,
541         [DVB_PLL_TUA6034]                = &dvb_pll_tua6034,
542         [DVB_PLL_LG_TDVS_H06XF]          = &dvb_pll_lg_tdvs_h06xf,
543         [DVB_PLL_TDA665X]                = &dvb_pll_tda665x,
544         [DVB_PLL_FMD1216ME]              = &dvb_pll_fmd1216me,
545         [DVB_PLL_TDED4]                  = &dvb_pll_tded4,
546         [DVB_PLL_TUV1236D]               = &dvb_pll_tuv1236d,
547         [DVB_PLL_TDHU2]                  = &dvb_pll_tdhu2,
548         [DVB_PLL_SAMSUNG_TBMV]           = &dvb_pll_samsung_tbmv,
549         [DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
550         [DVB_PLL_PHILIPS_TD1316]         = &dvb_pll_philips_td1316,
551         [DVB_PLL_THOMSON_FE6600]         = &dvb_pll_thomson_fe6600,
552         [DVB_PLL_OPERA1]                 = &dvb_pll_opera1,
553         [DVB_PLL_FCV1236D]               = &dvb_pll_fcv1236d,
554 };
555
556 /* ----------------------------------------------------------- */
557
558 struct dvb_pll_priv {
559         /* pll number */
560         int nr;
561
562         /* i2c details */
563         int pll_i2c_address;
564         struct i2c_adapter *i2c;
565
566         /* the PLL descriptor */
567         struct dvb_pll_desc *pll_desc;
568
569         /* cached frequency/bandwidth */
570         u32 frequency;
571         u32 bandwidth;
572 };
573
574 /* ----------------------------------------------------------- */
575 /* code                                                        */
576
577 static int debug = 0;
578 module_param(debug, int, 0644);
579 MODULE_PARM_DESC(debug, "enable verbose debug messages");
580
581 static unsigned int dvb_pll_devcount;
582
583 static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
584                              const struct dvb_frontend_parameters *params)
585 {
586         struct dvb_pll_priv *priv = fe->tuner_priv;
587         struct dvb_pll_desc *desc = priv->pll_desc;
588         u32 div;
589         int i;
590
591         if (params->frequency != 0 && (params->frequency < desc->min ||
592                                        params->frequency > desc->max))
593                 return -EINVAL;
594
595         for (i = 0; i < desc->count; i++) {
596                 if (params->frequency > desc->entries[i].limit)
597                         continue;
598                 break;
599         }
600
601         if (debug)
602                 printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
603                        params->frequency, i, desc->count);
604         if (i == desc->count)
605                 return -EINVAL;
606
607         div = (params->frequency + desc->iffreq +
608                desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
609         buf[0] = div >> 8;
610         buf[1] = div & 0xff;
611         buf[2] = desc->entries[i].config;
612         buf[3] = desc->entries[i].cb;
613
614         if (desc->set)
615                 desc->set(fe, buf, params);
616
617         if (debug)
618                 printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
619                        desc->name, div, buf[0], buf[1], buf[2], buf[3]);
620
621         // calculate the frequency we set it to
622         return (div * desc->entries[i].stepsize) - desc->iffreq;
623 }
624
625 static int dvb_pll_release(struct dvb_frontend *fe)
626 {
627         kfree(fe->tuner_priv);
628         fe->tuner_priv = NULL;
629         return 0;
630 }
631
632 static int dvb_pll_sleep(struct dvb_frontend *fe)
633 {
634         struct dvb_pll_priv *priv = fe->tuner_priv;
635
636         if (priv->i2c == NULL)
637                 return -EINVAL;
638
639         if (priv->pll_desc->sleepdata) {
640                 struct i2c_msg msg = { .flags = 0,
641                         .addr = priv->pll_i2c_address,
642                         .buf = priv->pll_desc->sleepdata + 1,
643                         .len = priv->pll_desc->sleepdata[0] };
644
645                 int result;
646
647                 if (fe->ops.i2c_gate_ctrl)
648                         fe->ops.i2c_gate_ctrl(fe, 1);
649                 if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
650                         return result;
651                 }
652                 return 0;
653         }
654         /* Shouldn't be called when initdata is NULL, maybe BUG()? */
655         return -EINVAL;
656 }
657
658 static int dvb_pll_set_params(struct dvb_frontend *fe,
659                               struct dvb_frontend_parameters *params)
660 {
661         struct dvb_pll_priv *priv = fe->tuner_priv;
662         u8 buf[4];
663         struct i2c_msg msg =
664                 { .addr = priv->pll_i2c_address, .flags = 0,
665                   .buf = buf, .len = sizeof(buf) };
666         int result;
667         u32 frequency = 0;
668
669         if (priv->i2c == NULL)
670                 return -EINVAL;
671
672         if ((result = dvb_pll_configure(fe, buf, params)) < 0)
673                 return result;
674         else
675                 frequency = result;
676
677         if (fe->ops.i2c_gate_ctrl)
678                 fe->ops.i2c_gate_ctrl(fe, 1);
679         if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
680                 return result;
681         }
682
683         priv->frequency = frequency;
684         priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
685
686         return 0;
687 }
688
689 static int dvb_pll_calc_regs(struct dvb_frontend *fe,
690                              struct dvb_frontend_parameters *params,
691                              u8 *buf, int buf_len)
692 {
693         struct dvb_pll_priv *priv = fe->tuner_priv;
694         int result;
695         u32 frequency = 0;
696
697         if (buf_len < 5)
698                 return -EINVAL;
699
700         if ((result = dvb_pll_configure(fe, buf+1, params)) < 0)
701                 return result;
702         else
703                 frequency = result;
704
705         buf[0] = priv->pll_i2c_address;
706
707         priv->frequency = frequency;
708         priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
709
710         return 5;
711 }
712
713 static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
714 {
715         struct dvb_pll_priv *priv = fe->tuner_priv;
716         *frequency = priv->frequency;
717         return 0;
718 }
719
720 static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
721 {
722         struct dvb_pll_priv *priv = fe->tuner_priv;
723         *bandwidth = priv->bandwidth;
724         return 0;
725 }
726
727 static int dvb_pll_init(struct dvb_frontend *fe)
728 {
729         struct dvb_pll_priv *priv = fe->tuner_priv;
730
731         if (priv->i2c == NULL)
732                 return -EINVAL;
733
734         if (priv->pll_desc->initdata) {
735                 struct i2c_msg msg = { .flags = 0,
736                         .addr = priv->pll_i2c_address,
737                         .buf = priv->pll_desc->initdata + 1,
738                         .len = priv->pll_desc->initdata[0] };
739
740                 int result;
741                 if (fe->ops.i2c_gate_ctrl)
742                         fe->ops.i2c_gate_ctrl(fe, 1);
743                 if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
744                         return result;
745                 }
746                 return 0;
747         }
748         /* Shouldn't be called when initdata is NULL, maybe BUG()? */
749         return -EINVAL;
750 }
751
752 static struct dvb_tuner_ops dvb_pll_tuner_ops = {
753         .release = dvb_pll_release,
754         .sleep = dvb_pll_sleep,
755         .init = dvb_pll_init,
756         .set_params = dvb_pll_set_params,
757         .calc_regs = dvb_pll_calc_regs,
758         .get_frequency = dvb_pll_get_frequency,
759         .get_bandwidth = dvb_pll_get_bandwidth,
760 };
761
762 struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
763                                     struct i2c_adapter *i2c,
764                                     unsigned int pll_desc_id)
765 {
766         u8 b1 [] = { 0 };
767         struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
768                                .buf = b1, .len = 1 };
769         struct dvb_pll_priv *priv = NULL;
770         int ret;
771         struct dvb_pll_desc *desc;
772
773         BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));
774
775         desc = pll_list[pll_desc_id];
776
777         if (i2c != NULL) {
778                 if (fe->ops.i2c_gate_ctrl)
779                         fe->ops.i2c_gate_ctrl(fe, 1);
780
781                 ret = i2c_transfer (i2c, &msg, 1);
782                 if (ret != 1)
783                         return NULL;
784                 if (fe->ops.i2c_gate_ctrl)
785                              fe->ops.i2c_gate_ctrl(fe, 0);
786         }
787
788         priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
789         if (priv == NULL)
790                 return NULL;
791
792         priv->pll_i2c_address = pll_addr;
793         priv->i2c = i2c;
794         priv->pll_desc = desc;
795         priv->nr = dvb_pll_devcount++;
796
797         memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
798                sizeof(struct dvb_tuner_ops));
799
800         strncpy(fe->ops.tuner_ops.info.name, desc->name,
801                 sizeof(fe->ops.tuner_ops.info.name));
802         fe->ops.tuner_ops.info.frequency_min = desc->min;
803         fe->ops.tuner_ops.info.frequency_max = desc->max;
804         if (!desc->initdata)
805                 fe->ops.tuner_ops.init = NULL;
806         if (!desc->sleepdata)
807                 fe->ops.tuner_ops.sleep = NULL;
808
809         fe->tuner_priv = priv;
810
811         if (debug) {
812                 printk("dvb-pll[%d]", priv->nr);
813                 if (i2c != NULL)
814                         printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
815                 printk(": id# %d (%s) attached\n", pll_desc_id, desc->name);
816         }
817
818         return fe;
819 }
820 EXPORT_SYMBOL(dvb_pll_attach);
821
822 MODULE_DESCRIPTION("dvb pll library");
823 MODULE_AUTHOR("Gerd Knorr");
824 MODULE_LICENSE("GPL");