Merge branch 'for-linus' of git://git390.marist.edu/pub/scm/linux-2.6
[pandora-kernel.git] / 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/slab.h>
22 #include <linux/module.h>
23 #include <linux/dvb/frontend.h>
24 #include <asm/types.h>
25
26 #include "dvb-pll.h"
27
28 struct dvb_pll_priv {
29         /* pll number */
30         int nr;
31
32         /* i2c details */
33         int pll_i2c_address;
34         struct i2c_adapter *i2c;
35
36         /* the PLL descriptor */
37         struct dvb_pll_desc *pll_desc;
38
39         /* cached frequency/bandwidth */
40         u32 frequency;
41         u32 bandwidth;
42 };
43
44 #define DVB_PLL_MAX 64
45
46 static unsigned int dvb_pll_devcount;
47
48 static int debug;
49 module_param(debug, int, 0644);
50 MODULE_PARM_DESC(debug, "enable verbose debug messages");
51
52 static unsigned int id[DVB_PLL_MAX] =
53         { [ 0 ... (DVB_PLL_MAX-1) ] = DVB_PLL_UNDEFINED };
54 module_param_array(id, int, NULL, 0644);
55 MODULE_PARM_DESC(id, "force pll id to use (DEBUG ONLY)");
56
57 /* ----------------------------------------------------------- */
58
59 struct dvb_pll_desc {
60         char *name;
61         u32  min;
62         u32  max;
63         u32  iffreq;
64         void (*set)(struct dvb_frontend *fe, u8 *buf,
65                     const struct dvb_frontend_parameters *params);
66         u8   *initdata;
67         u8   *initdata2;
68         u8   *sleepdata;
69         int  count;
70         struct {
71                 u32 limit;
72                 u32 stepsize;
73                 u8  config;
74                 u8  cb;
75         } entries[12];
76 };
77
78 /* ----------------------------------------------------------- */
79 /* descriptions                                                */
80
81 static struct dvb_pll_desc dvb_pll_thomson_dtt7579 = {
82         .name  = "Thomson dtt7579",
83         .min   = 177000000,
84         .max   = 858000000,
85         .iffreq= 36166667,
86         .sleepdata = (u8[]){ 2, 0xb4, 0x03 },
87         .count = 4,
88         .entries = {
89                 {  443250000, 166667, 0xb4, 0x02 },
90                 {  542000000, 166667, 0xb4, 0x08 },
91                 {  771000000, 166667, 0xbc, 0x08 },
92                 {  999999999, 166667, 0xf4, 0x08 },
93         },
94 };
95
96 static void thomson_dtt759x_bw(struct dvb_frontend *fe, u8 *buf,
97                                const struct dvb_frontend_parameters *params)
98 {
99         if (BANDWIDTH_7_MHZ == params->u.ofdm.bandwidth)
100                 buf[3] |= 0x10;
101 }
102
103 static struct dvb_pll_desc dvb_pll_thomson_dtt759x = {
104         .name  = "Thomson dtt759x",
105         .min   = 177000000,
106         .max   = 896000000,
107         .set   = thomson_dtt759x_bw,
108         .iffreq= 36166667,
109         .sleepdata = (u8[]){ 2, 0x84, 0x03 },
110         .count = 5,
111         .entries = {
112                 {  264000000, 166667, 0xb4, 0x02 },
113                 {  470000000, 166667, 0xbc, 0x02 },
114                 {  735000000, 166667, 0xbc, 0x08 },
115                 {  835000000, 166667, 0xf4, 0x08 },
116                 {  999999999, 166667, 0xfc, 0x08 },
117         },
118 };
119
120 static struct dvb_pll_desc dvb_pll_lg_z201 = {
121         .name  = "LG z201",
122         .min   = 174000000,
123         .max   = 862000000,
124         .iffreq= 36166667,
125         .sleepdata = (u8[]){ 2, 0xbc, 0x03 },
126         .count = 5,
127         .entries = {
128                 {  157500000, 166667, 0xbc, 0x01 },
129                 {  443250000, 166667, 0xbc, 0x02 },
130                 {  542000000, 166667, 0xbc, 0x04 },
131                 {  830000000, 166667, 0xf4, 0x04 },
132                 {  999999999, 166667, 0xfc, 0x04 },
133         },
134 };
135
136 static struct dvb_pll_desc dvb_pll_unknown_1 = {
137         .name  = "unknown 1", /* used by dntv live dvb-t */
138         .min   = 174000000,
139         .max   = 862000000,
140         .iffreq= 36166667,
141         .count = 9,
142         .entries = {
143                 {  150000000, 166667, 0xb4, 0x01 },
144                 {  173000000, 166667, 0xbc, 0x01 },
145                 {  250000000, 166667, 0xb4, 0x02 },
146                 {  400000000, 166667, 0xbc, 0x02 },
147                 {  420000000, 166667, 0xf4, 0x02 },
148                 {  470000000, 166667, 0xfc, 0x02 },
149                 {  600000000, 166667, 0xbc, 0x08 },
150                 {  730000000, 166667, 0xf4, 0x08 },
151                 {  999999999, 166667, 0xfc, 0x08 },
152         },
153 };
154
155 /* Infineon TUA6010XS
156  * used in Thomson Cable Tuner
157  */
158 static struct dvb_pll_desc dvb_pll_tua6010xs = {
159         .name  = "Infineon TUA6010XS",
160         .min   =  44250000,
161         .max   = 858000000,
162         .iffreq= 36125000,
163         .count = 3,
164         .entries = {
165                 {  115750000, 62500, 0x8e, 0x03 },
166                 {  403250000, 62500, 0x8e, 0x06 },
167                 {  999999999, 62500, 0x8e, 0x85 },
168         },
169 };
170
171 /* Panasonic env57h1xd5 (some Philips PLL ?) */
172 static struct dvb_pll_desc dvb_pll_env57h1xd5 = {
173         .name  = "Panasonic ENV57H1XD5",
174         .min   =  44250000,
175         .max   = 858000000,
176         .iffreq= 36125000,
177         .count = 4,
178         .entries = {
179                 {  153000000, 166667, 0xc2, 0x41 },
180                 {  470000000, 166667, 0xc2, 0x42 },
181                 {  526000000, 166667, 0xc2, 0x84 },
182                 {  999999999, 166667, 0xc2, 0xa4 },
183         },
184 };
185
186 /* Philips TDA6650/TDA6651
187  * used in Panasonic ENV77H11D5
188  */
189 static void tda665x_bw(struct dvb_frontend *fe, u8 *buf,
190                        const struct dvb_frontend_parameters *params)
191 {
192         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
193                 buf[3] |= 0x08;
194 }
195
196 static struct dvb_pll_desc dvb_pll_tda665x = {
197         .name  = "Philips TDA6650/TDA6651",
198         .min   =  44250000,
199         .max   = 858000000,
200         .set   = tda665x_bw,
201         .iffreq= 36166667,
202         .initdata = (u8[]){ 4, 0x0b, 0xf5, 0x85, 0xab },
203         .count = 12,
204         .entries = {
205                 {   93834000, 166667, 0xca, 0x61 /* 011 0 0 0  01 */ },
206                 {  123834000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
207                 {  161000000, 166667, 0xca, 0xa1 /* 101 0 0 0  01 */ },
208                 {  163834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
209                 {  253834000, 166667, 0xca, 0x62 /* 011 0 0 0  10 */ },
210                 {  383834000, 166667, 0xca, 0xa2 /* 101 0 0 0  10 */ },
211                 {  443834000, 166667, 0xca, 0xc2 /* 110 0 0 0  10 */ },
212                 {  444000000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
213                 {  583834000, 166667, 0xca, 0x64 /* 011 0 0 1  00 */ },
214                 {  793834000, 166667, 0xca, 0xa4 /* 101 0 0 1  00 */ },
215                 {  444834000, 166667, 0xca, 0xc4 /* 110 0 0 1  00 */ },
216                 {  861000000, 166667, 0xca, 0xe4 /* 111 0 0 1  00 */ },
217         }
218 };
219
220 /* Infineon TUA6034
221  * used in LG TDTP E102P
222  */
223 static void tua6034_bw(struct dvb_frontend *fe, u8 *buf,
224                        const struct dvb_frontend_parameters *params)
225 {
226         if (BANDWIDTH_7_MHZ != params->u.ofdm.bandwidth)
227                 buf[3] |= 0x08;
228 }
229
230 static struct dvb_pll_desc dvb_pll_tua6034 = {
231         .name  = "Infineon TUA6034",
232         .min   =  44250000,
233         .max   = 858000000,
234         .iffreq= 36166667,
235         .count = 3,
236         .set   = tua6034_bw,
237         .entries = {
238                 {  174500000, 62500, 0xce, 0x01 },
239                 {  230000000, 62500, 0xce, 0x02 },
240                 {  999999999, 62500, 0xce, 0x04 },
241         },
242 };
243
244 /* ALPS TDED4
245  * used in Nebula-Cards and USB boxes
246  */
247 static void tded4_bw(struct dvb_frontend *fe, u8 *buf,
248                      const struct dvb_frontend_parameters *params)
249 {
250         if (params->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
251                 buf[3] |= 0x04;
252 }
253
254 static struct dvb_pll_desc dvb_pll_tded4 = {
255         .name = "ALPS TDED4",
256         .min = 47000000,
257         .max = 863000000,
258         .iffreq= 36166667,
259         .set   = tded4_bw,
260         .count = 4,
261         .entries = {
262                 { 153000000, 166667, 0x85, 0x01 },
263                 { 470000000, 166667, 0x85, 0x02 },
264                 { 823000000, 166667, 0x85, 0x08 },
265                 { 999999999, 166667, 0x85, 0x88 },
266         }
267 };
268
269 /* ALPS TDHU2
270  * used in AverTVHD MCE A180
271  */
272 static struct dvb_pll_desc dvb_pll_tdhu2 = {
273         .name = "ALPS TDHU2",
274         .min = 54000000,
275         .max = 864000000,
276         .iffreq= 44000000,
277         .count = 4,
278         .entries = {
279                 { 162000000, 62500, 0x85, 0x01 },
280                 { 426000000, 62500, 0x85, 0x02 },
281                 { 782000000, 62500, 0x85, 0x08 },
282                 { 999999999, 62500, 0x85, 0x88 },
283         }
284 };
285
286 /* Samsung TBMV30111IN / TBMV30712IN1
287  * used in Air2PC ATSC - 2nd generation (nxt2002)
288  */
289 static struct dvb_pll_desc dvb_pll_samsung_tbmv = {
290         .name = "Samsung TBMV30111IN / TBMV30712IN1",
291         .min = 54000000,
292         .max = 860000000,
293         .iffreq= 44000000,
294         .count = 6,
295         .entries = {
296                 { 172000000, 166667, 0xb4, 0x01 },
297                 { 214000000, 166667, 0xb4, 0x02 },
298                 { 467000000, 166667, 0xbc, 0x02 },
299                 { 721000000, 166667, 0xbc, 0x08 },
300                 { 841000000, 166667, 0xf4, 0x08 },
301                 { 999999999, 166667, 0xfc, 0x02 },
302         }
303 };
304
305 /*
306  * Philips SD1878 Tuner.
307  */
308 static struct dvb_pll_desc dvb_pll_philips_sd1878_tda8261 = {
309         .name  = "Philips SD1878",
310         .min   =  950000,
311         .max   = 2150000,
312         .iffreq= 249, /* zero-IF, offset 249 is to round up */
313         .count = 4,
314         .entries = {
315                 { 1250000, 500, 0xc4, 0x00},
316                 { 1450000, 500, 0xc4, 0x40},
317                 { 2050000, 500, 0xc4, 0x80},
318                 { 2150000, 500, 0xc4, 0xc0},
319         },
320 };
321
322 static void opera1_bw(struct dvb_frontend *fe, u8 *buf,
323                       const struct dvb_frontend_parameters *params)
324 {
325         struct dvb_pll_priv *priv = fe->tuner_priv;
326         u32 b_w  = (params->u.qpsk.symbol_rate * 27) / 32000;
327         struct i2c_msg msg = {
328                 .addr = priv->pll_i2c_address,
329                 .flags = 0,
330                 .buf = buf,
331                 .len = 4
332         };
333         int result;
334         u8 lpf;
335
336         if (fe->ops.i2c_gate_ctrl)
337                 fe->ops.i2c_gate_ctrl(fe, 1);
338
339         result = i2c_transfer(priv->i2c, &msg, 1);
340         if (result != 1)
341                 printk(KERN_ERR "%s: i2c_transfer failed:%d",
342                         __func__, result);
343
344         if (b_w <= 10000)
345                 lpf = 0xc;
346         else if (b_w <= 12000)
347                 lpf = 0x2;
348         else if (b_w <= 14000)
349                 lpf = 0xa;
350         else if (b_w <= 16000)
351                 lpf = 0x6;
352         else if (b_w <= 18000)
353                 lpf = 0xe;
354         else if (b_w <= 20000)
355                 lpf = 0x1;
356         else if (b_w <= 22000)
357                 lpf = 0x9;
358         else if (b_w <= 24000)
359                 lpf = 0x5;
360         else if (b_w <= 26000)
361                 lpf = 0xd;
362         else if (b_w <= 28000)
363                 lpf = 0x3;
364                 else
365                 lpf = 0xb;
366         buf[2] ^= 0x1c; /* Flip bits 3-5 */
367         /* Set lpf */
368         buf[2] |= ((lpf >> 2) & 0x3) << 3;
369         buf[3] |= (lpf & 0x3) << 2;
370
371         return;
372 }
373
374 static struct dvb_pll_desc dvb_pll_opera1 = {
375         .name  = "Opera Tuner",
376         .min   =  900000,
377         .max   = 2250000,
378         .initdata = (u8[]){ 4, 0x08, 0xe5, 0xe1, 0x00 },
379         .initdata2 = (u8[]){ 4, 0x08, 0xe5, 0xe5, 0x00 },
380         .iffreq= 0,
381         .set   = opera1_bw,
382         .count = 8,
383         .entries = {
384                 { 1064000, 500, 0xf9, 0xc2 },
385                 { 1169000, 500, 0xf9, 0xe2 },
386                 { 1299000, 500, 0xf9, 0x20 },
387                 { 1444000, 500, 0xf9, 0x40 },
388                 { 1606000, 500, 0xf9, 0x60 },
389                 { 1777000, 500, 0xf9, 0x80 },
390                 { 1941000, 500, 0xf9, 0xa0 },
391                 { 2250000, 500, 0xf9, 0xc0 },
392         }
393 };
394
395 static void samsung_dtos403ih102a_set(struct dvb_frontend *fe, u8 *buf,
396                        const struct dvb_frontend_parameters *params)
397 {
398         struct dvb_pll_priv *priv = fe->tuner_priv;
399         struct i2c_msg msg = {
400                 .addr = priv->pll_i2c_address,
401                 .flags = 0,
402                 .buf = buf,
403                 .len = 4
404         };
405         int result;
406
407         if (fe->ops.i2c_gate_ctrl)
408                 fe->ops.i2c_gate_ctrl(fe, 1);
409
410         result = i2c_transfer(priv->i2c, &msg, 1);
411         if (result != 1)
412                 printk(KERN_ERR "%s: i2c_transfer failed:%d",
413                         __func__, result);
414
415         buf[2] = 0x9e;
416         buf[3] = 0x90;
417
418         return;
419 }
420
421 /* unknown pll used in Samsung DTOS403IH102A DVB-C tuner */
422 static struct dvb_pll_desc dvb_pll_samsung_dtos403ih102a = {
423         .name   = "Samsung DTOS403IH102A",
424         .min    =  44250000,
425         .max    = 858000000,
426         .iffreq =  36125000,
427         .count  = 8,
428         .set    = samsung_dtos403ih102a_set,
429         .entries = {
430                 { 135000000, 62500, 0xbe, 0x01 },
431                 { 177000000, 62500, 0xf6, 0x01 },
432                 { 370000000, 62500, 0xbe, 0x02 },
433                 { 450000000, 62500, 0xf6, 0x02 },
434                 { 466000000, 62500, 0xfe, 0x02 },
435                 { 538000000, 62500, 0xbe, 0x08 },
436                 { 826000000, 62500, 0xf6, 0x08 },
437                 { 999999999, 62500, 0xfe, 0x08 },
438         }
439 };
440
441 /* Samsung TDTC9251DH0 DVB-T NIM, as used on AirStar 2 */
442 static struct dvb_pll_desc dvb_pll_samsung_tdtc9251dh0 = {
443         .name   = "Samsung TDTC9251DH0",
444         .min    =  48000000,
445         .max    = 863000000,
446         .iffreq =  36166667,
447         .count  = 3,
448         .entries = {
449                 { 157500000, 166667, 0xcc, 0x09 },
450                 { 443000000, 166667, 0xcc, 0x0a },
451                 { 863000000, 166667, 0xcc, 0x08 },
452         }
453 };
454
455 /* Samsung TBDU18132 DVB-S NIM with TSA5059 PLL, used in SkyStar2 DVB-S 2.3 */
456 static struct dvb_pll_desc dvb_pll_samsung_tbdu18132 = {
457         .name = "Samsung TBDU18132",
458         .min    =  950000,
459         .max    = 2150000, /* guesses */
460         .iffreq = 0,
461         .count = 2,
462         .entries = {
463                 { 1550000, 125, 0x84, 0x82 },
464                 { 4095937, 125, 0x84, 0x80 },
465         }
466         /* TSA5059 PLL has a 17 bit divisor rather than the 15 bits supported
467          * by this driver.  The two extra bits are 0x60 in the third byte.  15
468          * bits is enough for over 4 GHz, which is enough to cover the range
469          * of this tuner.  We could use the additional divisor bits by adding
470          * more entries, e.g.
471          { 0x0ffff * 125 + 125/2, 125, 0x84 | 0x20, },
472          { 0x17fff * 125 + 125/2, 125, 0x84 | 0x40, },
473          { 0x1ffff * 125 + 125/2, 125, 0x84 | 0x60, }, */
474 };
475
476 /* Samsung TBMU24112 DVB-S NIM with SL1935 zero-IF tuner */
477 static struct dvb_pll_desc dvb_pll_samsung_tbmu24112 = {
478         .name = "Samsung TBMU24112",
479         .min    =  950000,
480         .max    = 2150000, /* guesses */
481         .iffreq = 0,
482         .count = 2,
483         .entries = {
484                 { 1500000, 125, 0x84, 0x18 },
485                 { 9999999, 125, 0x84, 0x08 },
486         }
487 };
488
489 /* Alps TDEE4 DVB-C NIM, used on Cablestar 2 */
490 /* byte 4 : 1  *   *   AGD R3  R2  R1  R0
491  * byte 5 : C1 *   RE  RTS BS4 BS3 BS2 BS1
492  * AGD = 1, R3 R2 R1 R0 = 0 1 0 1 => byte 4 = 1**10101 = 0x95
493  * Range(MHz)  C1 *  RE RTS BS4 BS3 BS2 BS1  Byte 5
494  *  47 - 153   0  *  0   0   0   0   0   1   0x01
495  * 153 - 430   0  *  0   0   0   0   1   0   0x02
496  * 430 - 822   0  *  0   0   1   0   0   0   0x08
497  * 822 - 862   1  *  0   0   1   0   0   0   0x88 */
498 static struct dvb_pll_desc dvb_pll_alps_tdee4 = {
499         .name = "ALPS TDEE4",
500         .min    =  47000000,
501         .max    = 862000000,
502         .iffreq =  36125000,
503         .count = 4,
504         .entries = {
505                 { 153000000, 62500, 0x95, 0x01 },
506                 { 430000000, 62500, 0x95, 0x02 },
507                 { 822000000, 62500, 0x95, 0x08 },
508                 { 999999999, 62500, 0x95, 0x88 },
509         }
510 };
511
512 /* ----------------------------------------------------------- */
513
514 static struct dvb_pll_desc *pll_list[] = {
515         [DVB_PLL_UNDEFINED]              = NULL,
516         [DVB_PLL_THOMSON_DTT7579]        = &dvb_pll_thomson_dtt7579,
517         [DVB_PLL_THOMSON_DTT759X]        = &dvb_pll_thomson_dtt759x,
518         [DVB_PLL_LG_Z201]                = &dvb_pll_lg_z201,
519         [DVB_PLL_UNKNOWN_1]              = &dvb_pll_unknown_1,
520         [DVB_PLL_TUA6010XS]              = &dvb_pll_tua6010xs,
521         [DVB_PLL_ENV57H1XD5]             = &dvb_pll_env57h1xd5,
522         [DVB_PLL_TUA6034]                = &dvb_pll_tua6034,
523         [DVB_PLL_TDA665X]                = &dvb_pll_tda665x,
524         [DVB_PLL_TDED4]                  = &dvb_pll_tded4,
525         [DVB_PLL_TDEE4]                  = &dvb_pll_alps_tdee4,
526         [DVB_PLL_TDHU2]                  = &dvb_pll_tdhu2,
527         [DVB_PLL_SAMSUNG_TBMV]           = &dvb_pll_samsung_tbmv,
528         [DVB_PLL_PHILIPS_SD1878_TDA8261] = &dvb_pll_philips_sd1878_tda8261,
529         [DVB_PLL_OPERA1]                 = &dvb_pll_opera1,
530         [DVB_PLL_SAMSUNG_DTOS403IH102A]  = &dvb_pll_samsung_dtos403ih102a,
531         [DVB_PLL_SAMSUNG_TDTC9251DH0]    = &dvb_pll_samsung_tdtc9251dh0,
532         [DVB_PLL_SAMSUNG_TBDU18132]      = &dvb_pll_samsung_tbdu18132,
533         [DVB_PLL_SAMSUNG_TBMU24112]      = &dvb_pll_samsung_tbmu24112,
534 };
535
536 /* ----------------------------------------------------------- */
537 /* code                                                        */
538
539 static int dvb_pll_configure(struct dvb_frontend *fe, u8 *buf,
540                              const struct dvb_frontend_parameters *params)
541 {
542         struct dvb_pll_priv *priv = fe->tuner_priv;
543         struct dvb_pll_desc *desc = priv->pll_desc;
544         u32 div;
545         int i;
546
547         if (params->frequency != 0 && (params->frequency < desc->min ||
548                                        params->frequency > desc->max))
549                 return -EINVAL;
550
551         for (i = 0; i < desc->count; i++) {
552                 if (params->frequency > desc->entries[i].limit)
553                         continue;
554                 break;
555         }
556
557         if (debug)
558                 printk("pll: %s: freq=%d | i=%d/%d\n", desc->name,
559                        params->frequency, i, desc->count);
560         if (i == desc->count)
561                 return -EINVAL;
562
563         div = (params->frequency + desc->iffreq +
564                desc->entries[i].stepsize/2) / desc->entries[i].stepsize;
565         buf[0] = div >> 8;
566         buf[1] = div & 0xff;
567         buf[2] = desc->entries[i].config;
568         buf[3] = desc->entries[i].cb;
569
570         if (desc->set)
571                 desc->set(fe, buf, params);
572
573         if (debug)
574                 printk("pll: %s: div=%d | buf=0x%02x,0x%02x,0x%02x,0x%02x\n",
575                        desc->name, div, buf[0], buf[1], buf[2], buf[3]);
576
577         // calculate the frequency we set it to
578         return (div * desc->entries[i].stepsize) - desc->iffreq;
579 }
580
581 static int dvb_pll_release(struct dvb_frontend *fe)
582 {
583         kfree(fe->tuner_priv);
584         fe->tuner_priv = NULL;
585         return 0;
586 }
587
588 static int dvb_pll_sleep(struct dvb_frontend *fe)
589 {
590         struct dvb_pll_priv *priv = fe->tuner_priv;
591
592         if (priv->i2c == NULL)
593                 return -EINVAL;
594
595         if (priv->pll_desc->sleepdata) {
596                 struct i2c_msg msg = { .flags = 0,
597                         .addr = priv->pll_i2c_address,
598                         .buf = priv->pll_desc->sleepdata + 1,
599                         .len = priv->pll_desc->sleepdata[0] };
600
601                 int result;
602
603                 if (fe->ops.i2c_gate_ctrl)
604                         fe->ops.i2c_gate_ctrl(fe, 1);
605                 if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
606                         return result;
607                 }
608                 return 0;
609         }
610         /* Shouldn't be called when initdata is NULL, maybe BUG()? */
611         return -EINVAL;
612 }
613
614 static int dvb_pll_set_params(struct dvb_frontend *fe,
615                               struct dvb_frontend_parameters *params)
616 {
617         struct dvb_pll_priv *priv = fe->tuner_priv;
618         u8 buf[4];
619         struct i2c_msg msg =
620                 { .addr = priv->pll_i2c_address, .flags = 0,
621                   .buf = buf, .len = sizeof(buf) };
622         int result;
623         u32 frequency = 0;
624
625         if (priv->i2c == NULL)
626                 return -EINVAL;
627
628         if ((result = dvb_pll_configure(fe, buf, params)) < 0)
629                 return result;
630         else
631                 frequency = result;
632
633         if (fe->ops.i2c_gate_ctrl)
634                 fe->ops.i2c_gate_ctrl(fe, 1);
635         if ((result = i2c_transfer(priv->i2c, &msg, 1)) != 1) {
636                 return result;
637         }
638
639         priv->frequency = frequency;
640         priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
641
642         return 0;
643 }
644
645 static int dvb_pll_calc_regs(struct dvb_frontend *fe,
646                              struct dvb_frontend_parameters *params,
647                              u8 *buf, int buf_len)
648 {
649         struct dvb_pll_priv *priv = fe->tuner_priv;
650         int result;
651         u32 frequency = 0;
652
653         if (buf_len < 5)
654                 return -EINVAL;
655
656         if ((result = dvb_pll_configure(fe, buf+1, params)) < 0)
657                 return result;
658         else
659                 frequency = result;
660
661         buf[0] = priv->pll_i2c_address;
662
663         priv->frequency = frequency;
664         priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? params->u.ofdm.bandwidth : 0;
665
666         return 5;
667 }
668
669 static int dvb_pll_get_frequency(struct dvb_frontend *fe, u32 *frequency)
670 {
671         struct dvb_pll_priv *priv = fe->tuner_priv;
672         *frequency = priv->frequency;
673         return 0;
674 }
675
676 static int dvb_pll_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
677 {
678         struct dvb_pll_priv *priv = fe->tuner_priv;
679         *bandwidth = priv->bandwidth;
680         return 0;
681 }
682
683 static int dvb_pll_init(struct dvb_frontend *fe)
684 {
685         struct dvb_pll_priv *priv = fe->tuner_priv;
686
687         if (priv->i2c == NULL)
688                 return -EINVAL;
689
690         if (priv->pll_desc->initdata) {
691                 struct i2c_msg msg = { .flags = 0,
692                         .addr = priv->pll_i2c_address,
693                         .buf = priv->pll_desc->initdata + 1,
694                         .len = priv->pll_desc->initdata[0] };
695
696                 int result;
697                 if (fe->ops.i2c_gate_ctrl)
698                         fe->ops.i2c_gate_ctrl(fe, 1);
699                 result = i2c_transfer(priv->i2c, &msg, 1);
700                 if (result != 1)
701                         return result;
702                 if (priv->pll_desc->initdata2) {
703                         msg.buf = priv->pll_desc->initdata2 + 1;
704                         msg.len = priv->pll_desc->initdata2[0];
705                         if (fe->ops.i2c_gate_ctrl)
706                                 fe->ops.i2c_gate_ctrl(fe, 1);
707                         result = i2c_transfer(priv->i2c, &msg, 1);
708                         if (result != 1)
709                                 return result;
710                 }
711                 return 0;
712         }
713         /* Shouldn't be called when initdata is NULL, maybe BUG()? */
714         return -EINVAL;
715 }
716
717 static struct dvb_tuner_ops dvb_pll_tuner_ops = {
718         .release = dvb_pll_release,
719         .sleep = dvb_pll_sleep,
720         .init = dvb_pll_init,
721         .set_params = dvb_pll_set_params,
722         .calc_regs = dvb_pll_calc_regs,
723         .get_frequency = dvb_pll_get_frequency,
724         .get_bandwidth = dvb_pll_get_bandwidth,
725 };
726
727 struct dvb_frontend *dvb_pll_attach(struct dvb_frontend *fe, int pll_addr,
728                                     struct i2c_adapter *i2c,
729                                     unsigned int pll_desc_id)
730 {
731         u8 b1 [] = { 0 };
732         struct i2c_msg msg = { .addr = pll_addr, .flags = I2C_M_RD,
733                                .buf = b1, .len = 1 };
734         struct dvb_pll_priv *priv = NULL;
735         int ret;
736         struct dvb_pll_desc *desc;
737
738         if ((id[dvb_pll_devcount] > DVB_PLL_UNDEFINED) &&
739             (id[dvb_pll_devcount] < ARRAY_SIZE(pll_list)))
740                 pll_desc_id = id[dvb_pll_devcount];
741
742         BUG_ON(pll_desc_id < 1 || pll_desc_id >= ARRAY_SIZE(pll_list));
743
744         desc = pll_list[pll_desc_id];
745
746         if (i2c != NULL) {
747                 if (fe->ops.i2c_gate_ctrl)
748                         fe->ops.i2c_gate_ctrl(fe, 1);
749
750                 ret = i2c_transfer (i2c, &msg, 1);
751                 if (ret != 1)
752                         return NULL;
753                 if (fe->ops.i2c_gate_ctrl)
754                              fe->ops.i2c_gate_ctrl(fe, 0);
755         }
756
757         priv = kzalloc(sizeof(struct dvb_pll_priv), GFP_KERNEL);
758         if (priv == NULL)
759                 return NULL;
760
761         priv->pll_i2c_address = pll_addr;
762         priv->i2c = i2c;
763         priv->pll_desc = desc;
764         priv->nr = dvb_pll_devcount++;
765
766         memcpy(&fe->ops.tuner_ops, &dvb_pll_tuner_ops,
767                sizeof(struct dvb_tuner_ops));
768
769         strncpy(fe->ops.tuner_ops.info.name, desc->name,
770                 sizeof(fe->ops.tuner_ops.info.name));
771         fe->ops.tuner_ops.info.frequency_min = desc->min;
772         fe->ops.tuner_ops.info.frequency_max = desc->max;
773         if (!desc->initdata)
774                 fe->ops.tuner_ops.init = NULL;
775         if (!desc->sleepdata)
776                 fe->ops.tuner_ops.sleep = NULL;
777
778         fe->tuner_priv = priv;
779
780         if ((debug) || (id[priv->nr] == pll_desc_id)) {
781                 printk("dvb-pll[%d]", priv->nr);
782                 if (i2c != NULL)
783                         printk(" %d-%04x", i2c_adapter_id(i2c), pll_addr);
784                 printk(": id# %d (%s) attached, %s\n", pll_desc_id, desc->name,
785                        id[priv->nr] == pll_desc_id ?
786                                 "insmod option" : "autodetected");
787         }
788
789         return fe;
790 }
791 EXPORT_SYMBOL(dvb_pll_attach);
792
793 MODULE_DESCRIPTION("dvb pll library");
794 MODULE_AUTHOR("Gerd Knorr");
795 MODULE_LICENSE("GPL");