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