V4L/DVB (6963): tda18271: store IF frequency in a u16 instead of u32
[linux-2.6] / drivers / media / dvb / frontends / tda18271-fe.c
1 /*
2     tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
3
4     Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
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/delay.h>
22 #include <linux/videodev2.h>
23 #include "tda18271-priv.h"
24
25 int tda18271_debug;
26 module_param_named(debug, tda18271_debug, int, 0644);
27 MODULE_PARM_DESC(debug, "set debug level (info=1, map=2, reg=4 (or-able))");
28
29 /*---------------------------------------------------------------------*/
30
31 static int tda18271_init(struct dvb_frontend *fe)
32 {
33         struct tda18271_priv *priv = fe->tuner_priv;
34         unsigned char *regs = priv->tda18271_regs;
35
36         tda18271_read_regs(fe);
37
38         /* test IR_CAL_OK to see if we need init */
39         if ((regs[R_EP1] & 0x08) == 0)
40                 tda18271_init_regs(fe);
41
42         return 0;
43 }
44
45 /* ------------------------------------------------------------------ */
46
47 static int tda18271_channel_configuration(struct dvb_frontend *fe,
48                                           u32 ifc, u32 freq, u32 bw, u8 std)
49 {
50         struct tda18271_priv *priv = fe->tuner_priv;
51         unsigned char *regs = priv->tda18271_regs;
52         u32 N;
53
54         /* update TV broadcast parameters */
55
56         /* set standard */
57         regs[R_EP3]  &= ~0x1f; /* clear std bits */
58         regs[R_EP3]  |= std;
59
60         /* set cal mode to normal */
61         regs[R_EP4]  &= ~0x03;
62
63         /* update IF output level & IF notch frequency */
64         regs[R_EP4]  &= ~0x1c; /* clear if level bits */
65
66         switch (priv->mode) {
67         case TDA18271_ANALOG:
68                 regs[R_MPD]  &= ~0x80; /* IF notch = 0 */
69                 break;
70         case TDA18271_DIGITAL:
71                 regs[R_EP4]  |= 0x04; /* IF level = 1 */
72                 regs[R_MPD]  |= 0x80; /* IF notch = 1 */
73                 break;
74         }
75         regs[R_EP4]  &= ~0x80; /* FM_RFn: turn this bit on only for fm radio */
76
77         /* update RF_TOP / IF_TOP */
78         switch (priv->mode) {
79         case TDA18271_ANALOG:
80                 regs[R_EB22]  = 0x2c;
81                 break;
82         case TDA18271_DIGITAL:
83                 regs[R_EB22]  = 0x37;
84                 break;
85         }
86         tda18271_write_regs(fe, R_EB22, 1);
87
88         /* --------------------------------------------------------------- */
89
90         /* disable Power Level Indicator */
91         regs[R_EP1]  |= 0x40;
92
93         /* frequency dependent parameters */
94
95         tda18271_calc_ir_measure(fe, &freq);
96
97         tda18271_calc_bp_filter(fe, &freq);
98
99         tda18271_calc_rf_band(fe, &freq);
100
101         tda18271_calc_gain_taper(fe, &freq);
102
103         /* --------------------------------------------------------------- */
104
105         /* dual tuner and agc1 extra configuration */
106
107         /* main vco when Master, cal vco when slave */
108         regs[R_EB1]  |= 0x04; /* FIXME: assumes master */
109
110         /* agc1 always active */
111         regs[R_EB1]  &= ~0x02;
112
113         /* agc1 has priority on agc2 */
114         regs[R_EB1]  &= ~0x01;
115
116         tda18271_write_regs(fe, R_EB1, 1);
117
118         /* --------------------------------------------------------------- */
119
120         N = freq + ifc;
121
122         /* FIXME: assumes master */
123         tda18271_calc_main_pll(fe, N);
124         tda18271_write_regs(fe, R_MPD, 4);
125
126         tda18271_write_regs(fe, R_TM, 7);
127
128         /* main pll charge pump source */
129         regs[R_EB4] |= 0x20;
130         tda18271_write_regs(fe, R_EB4, 1);
131
132         msleep(1);
133
134         /* normal operation for the main pll */
135         regs[R_EB4] &= ~0x20;
136         tda18271_write_regs(fe, R_EB4, 1);
137
138         msleep(5);
139
140         return 0;
141 }
142
143 static int tda18271_read_thermometer(struct dvb_frontend *fe)
144 {
145         struct tda18271_priv *priv = fe->tuner_priv;
146         unsigned char *regs = priv->tda18271_regs;
147         int tm;
148
149         /* switch thermometer on */
150         regs[R_TM]   |= 0x10;
151         tda18271_write_regs(fe, R_TM, 1);
152
153         /* read thermometer info */
154         tda18271_read_regs(fe);
155
156         if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
157             (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
158
159                 if ((regs[R_TM] & 0x20) == 0x20)
160                         regs[R_TM] &= ~0x20;
161                 else
162                         regs[R_TM] |= 0x20;
163
164                 tda18271_write_regs(fe, R_TM, 1);
165
166                 msleep(10); /* temperature sensing */
167
168                 /* read thermometer info */
169                 tda18271_read_regs(fe);
170         }
171
172         tm = tda18271_lookup_thermometer(fe);
173
174         /* switch thermometer off */
175         regs[R_TM]   &= ~0x10;
176         tda18271_write_regs(fe, R_TM, 1);
177
178         /* set CAL mode to normal */
179         regs[R_EP4]  &= ~0x03;
180         tda18271_write_regs(fe, R_EP4, 1);
181
182         return tm;
183 }
184
185 static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe,
186                                                    u32 freq, int tm_rfcal)
187 {
188         struct tda18271_priv *priv = fe->tuner_priv;
189         struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
190         unsigned char *regs = priv->tda18271_regs;
191         int tm_current, rfcal_comp, approx, i;
192         u8 dc_over_dt, rf_tab;
193
194         /* power up */
195         regs[R_EP3]  &= ~0xe0; /* sm = 0, sm_lt = 0, sm_xt = 0 */
196         tda18271_write_regs(fe, R_EP3, 1);
197
198         /* read die current temperature */
199         tm_current = tda18271_read_thermometer(fe);
200
201         /* frequency dependent parameters */
202
203         tda18271_calc_rf_cal(fe, &freq);
204         rf_tab = regs[R_EB14];
205
206         i = tda18271_lookup_rf_band(fe, &freq, NULL);
207         if (i < 0)
208                 return -EINVAL;
209
210         if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
211                 approx = map[i].rf_a1 *
212                         (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
213         } else {
214                 approx = map[i].rf_a2 *
215                         (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
216         }
217
218         if (approx < 0)
219                 approx = 0;
220         if (approx > 255)
221                 approx = 255;
222
223         tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
224
225         /* calculate temperature compensation */
226         rfcal_comp = dc_over_dt * (tm_current - tm_rfcal);
227
228         regs[R_EB14] = approx + rfcal_comp;
229         tda18271_write_regs(fe, R_EB14, 1);
230
231         return 0;
232 }
233
234 static int tda18271_por(struct dvb_frontend *fe)
235 {
236         struct tda18271_priv *priv = fe->tuner_priv;
237         unsigned char *regs = priv->tda18271_regs;
238
239         /* power up detector 1 */
240         regs[R_EB12] &= ~0x20;
241         tda18271_write_regs(fe, R_EB12, 1);
242
243         regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
244         regs[R_EB18] &= ~0x03; /* set agc1_gain to  6 dB */
245         tda18271_write_regs(fe, R_EB18, 1);
246
247         regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
248
249         /* POR mode */
250         regs[R_EP3]  &= ~0xe0; /* clear sm, sm_lt, sm_xt */
251         regs[R_EP3]  |= 0x80; /* sm = 1, sm_lt = 0, sm_xt = 0 */
252         tda18271_write_regs(fe, R_EP3, 1);
253
254         /* disable 1.5 MHz low pass filter */
255         regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
256         regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
257         tda18271_write_regs(fe, R_EB21, 3);
258
259         return 0;
260 }
261
262 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
263 {
264         struct tda18271_priv *priv = fe->tuner_priv;
265         unsigned char *regs = priv->tda18271_regs;
266         u32 N;
267
268         /* set CAL mode to normal */
269         regs[R_EP4]  &= ~0x03;
270         tda18271_write_regs(fe, R_EP4, 1);
271
272         /* switch off agc1 */
273         regs[R_EP3]  |= 0x40; /* sm_lt = 1 */
274
275         regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
276         tda18271_write_regs(fe, R_EB18, 1);
277
278         /* frequency dependent parameters */
279
280         tda18271_calc_bp_filter(fe, &freq);
281         tda18271_calc_gain_taper(fe, &freq);
282         tda18271_calc_rf_band(fe, &freq);
283         tda18271_calc_km(fe, &freq);
284
285         tda18271_write_regs(fe, R_EP1, 3);
286         tda18271_write_regs(fe, R_EB13, 1);
287
288         /* main pll charge pump source */
289         regs[R_EB4]  |= 0x20;
290         tda18271_write_regs(fe, R_EB4, 1);
291
292         /* cal pll charge pump source */
293         regs[R_EB7]  |= 0x20;
294         tda18271_write_regs(fe, R_EB7, 1);
295
296         /* force dcdc converter to 0 V */
297         regs[R_EB14] = 0x00;
298         tda18271_write_regs(fe, R_EB14, 1);
299
300         /* disable plls lock */
301         regs[R_EB20] &= ~0x20;
302         tda18271_write_regs(fe, R_EB20, 1);
303
304         /* set CAL mode to RF tracking filter calibration */
305         regs[R_EP4]  |= 0x03;
306         tda18271_write_regs(fe, R_EP4, 2);
307
308         /* --------------------------------------------------------------- */
309
310         /* set the internal calibration signal */
311         N = freq;
312
313         tda18271_calc_main_pll(fe, N);
314         tda18271_write_regs(fe, R_MPD, 4);
315
316         /* downconvert internal calibration */
317         N += 1000000;
318
319         tda18271_calc_main_pll(fe, N);
320         tda18271_write_regs(fe, R_MPD, 4);
321
322         msleep(5);
323
324         tda18271_write_regs(fe, R_EP2, 1);
325         tda18271_write_regs(fe, R_EP1, 1);
326         tda18271_write_regs(fe, R_EP2, 1);
327         tda18271_write_regs(fe, R_EP1, 1);
328
329         /* --------------------------------------------------------------- */
330
331         /* normal operation for the main pll */
332         regs[R_EB4] &= ~0x20;
333         tda18271_write_regs(fe, R_EB4, 1);
334
335         /* normal operation for the cal pll  */
336         regs[R_EB7] &= ~0x20;
337         tda18271_write_regs(fe, R_EB7, 1);
338
339         msleep(5); /* plls locking */
340
341         /* launch the rf tracking filters calibration */
342         regs[R_EB20]  |= 0x20;
343         tda18271_write_regs(fe, R_EB20, 1);
344
345         msleep(60); /* calibration */
346
347         /* --------------------------------------------------------------- */
348
349         /* set CAL mode to normal */
350         regs[R_EP4]  &= ~0x03;
351
352         /* switch on agc1 */
353         regs[R_EP3]  &= ~0x40; /* sm_lt = 0 */
354
355         regs[R_EB18] &= ~0x03; /* set agc1_gain to  6 dB */
356         tda18271_write_regs(fe, R_EB18, 1);
357
358         tda18271_write_regs(fe, R_EP3, 2);
359
360         /* synchronization */
361         tda18271_write_regs(fe, R_EP1, 1);
362
363         /* get calibration result */
364         tda18271_read_extended(fe);
365
366         return regs[R_EB14];
367 }
368
369 static int tda18271_powerscan(struct dvb_frontend *fe,
370                               u32 *freq_in, u32 *freq_out)
371 {
372         struct tda18271_priv *priv = fe->tuner_priv;
373         unsigned char *regs = priv->tda18271_regs;
374         int sgn, bcal, count, wait;
375         u8 cid_target;
376         u16 count_limit;
377         u32 freq;
378
379         freq = *freq_in;
380
381         tda18271_calc_rf_band(fe, &freq);
382         tda18271_calc_rf_cal(fe, &freq);
383         tda18271_calc_gain_taper(fe, &freq);
384         tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
385
386         tda18271_write_regs(fe, R_EP2, 1);
387         tda18271_write_regs(fe, R_EB14, 1);
388
389         /* downconvert frequency */
390         freq += 1000000;
391
392         tda18271_calc_main_pll(fe, freq);
393         tda18271_write_regs(fe, R_MPD, 4);
394
395         msleep(5); /* pll locking */
396
397         /* detection mode */
398         regs[R_EP4]  &= ~0x03;
399         regs[R_EP4]  |= 0x01;
400         tda18271_write_regs(fe, R_EP4, 1);
401
402         /* launch power detection measurement */
403         tda18271_write_regs(fe, R_EP2, 1);
404
405         /* read power detection info, stored in EB10 */
406         tda18271_read_extended(fe);
407
408         /* algorithm initialization */
409         sgn = 1;
410         *freq_out = *freq_in;
411         bcal = 0;
412         count = 0;
413         wait = false;
414
415         while ((regs[R_EB10] & 0x3f) < cid_target) {
416                 /* downconvert updated freq to 1 MHz */
417                 freq = *freq_in + (sgn * count) + 1000000;
418
419                 tda18271_calc_main_pll(fe, freq);
420                 tda18271_write_regs(fe, R_MPD, 4);
421
422                 if (wait) {
423                         msleep(5); /* pll locking */
424                         wait = false;
425                 } else
426                         udelay(100); /* pll locking */
427
428                 /* launch power detection measurement */
429                 tda18271_write_regs(fe, R_EP2, 1);
430
431                 /* read power detection info, stored in EB10 */
432                 tda18271_read_extended(fe);
433
434                 count += 200;
435
436                 if (count < count_limit)
437                         continue;
438
439                 if (sgn <= 0)
440                         break;
441
442                 sgn = -1 * sgn;
443                 count = 200;
444                 wait = true;
445         }
446
447         if ((regs[R_EB10] & 0x3f) >= cid_target) {
448                 bcal = 1;
449                 *freq_out = freq - 1000000;
450         } else
451                 bcal = 0;
452
453         tda_dbg("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
454                 bcal, *freq_in, *freq_out, freq);
455
456         return bcal;
457 }
458
459 static int tda18271_powerscan_init(struct dvb_frontend *fe)
460 {
461         struct tda18271_priv *priv = fe->tuner_priv;
462         unsigned char *regs = priv->tda18271_regs;
463
464         /* set standard to digital */
465         regs[R_EP3]  &= ~0x1f; /* clear std bits */
466         regs[R_EP3]  |= 0x12;
467
468         /* set cal mode to normal */
469         regs[R_EP4]  &= ~0x03;
470
471         /* update IF output level & IF notch frequency */
472         regs[R_EP4]  &= ~0x1c; /* clear if level bits */
473
474         tda18271_write_regs(fe, R_EP3, 2);
475
476         regs[R_EB18] &= ~0x03; /* set agc1_gain to   6 dB */
477         tda18271_write_regs(fe, R_EB18, 1);
478
479         regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
480
481         /* 1.5 MHz low pass filter */
482         regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
483         regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
484
485         tda18271_write_regs(fe, R_EB21, 3);
486
487         return 0;
488 }
489
490 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
491 {
492         struct tda18271_priv *priv = fe->tuner_priv;
493         struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
494         unsigned char *regs = priv->tda18271_regs;
495         int bcal, rf, i;
496 #define RF1 0
497 #define RF2 1
498 #define RF3 2
499         u32 rf_default[3];
500         u32 rf_freq[3];
501         u8 prog_cal[3];
502         u8 prog_tab[3];
503
504         i = tda18271_lookup_rf_band(fe, &freq, NULL);
505
506         if (i < 0)
507                 return i;
508
509         rf_default[RF1] = 1000 * map[i].rf1_def;
510         rf_default[RF2] = 1000 * map[i].rf2_def;
511         rf_default[RF3] = 1000 * map[i].rf3_def;
512
513         for (rf = RF1; rf <= RF3; rf++) {
514                 if (0 == rf_default[rf])
515                         return 0;
516                 tda_dbg("freq = %d, rf = %d\n", freq, rf);
517
518                 /* look for optimized calibration frequency */
519                 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
520
521                 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
522                 prog_tab[rf] = regs[R_EB14];
523
524                 if (1 == bcal)
525                         prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
526                 else
527                         prog_cal[rf] = prog_tab[rf];
528
529                 switch (rf) {
530                 case RF1:
531                         map[i].rf_a1 = 0;
532                         map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
533                         map[i].rf1   = rf_freq[RF1] / 1000;
534                         break;
535                 case RF2:
536                         map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
537                                         prog_cal[RF1] + prog_tab[RF1]) /
538                                 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
539                         map[i].rf2   = rf_freq[RF2] / 1000;
540                         break;
541                 case RF3:
542                         map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
543                                         prog_cal[RF2] + prog_tab[RF2]) /
544                                 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
545                         map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
546                         map[i].rf3   = rf_freq[RF3] / 1000;
547                         break;
548                 default:
549                         BUG();
550                 }
551         }
552
553         return 0;
554 }
555
556 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe,
557                                          int *tm_rfcal)
558 {
559         struct tda18271_priv *priv = fe->tuner_priv;
560         unsigned int i;
561
562         tda_info("tda18271: performing RF tracking filter calibration\n");
563
564         /* wait for die temperature stabilization */
565         msleep(200);
566
567         tda18271_powerscan_init(fe);
568
569         /* rf band calibration */
570         for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
571                 tda18271_rf_tracking_filters_init(fe, 1000 *
572                                                   priv->rf_cal_state[i].rfmax);
573
574         *tm_rfcal = tda18271_read_thermometer(fe);
575
576         return 0;
577 }
578
579 /* ------------------------------------------------------------------ */
580
581 static int tda18271_init_cal(struct dvb_frontend *fe, int *tm)
582 {
583         struct tda18271_priv *priv = fe->tuner_priv;
584
585         if (priv->cal_initialized)
586                 return 0;
587
588         /* initialization */
589         tda18271_init(fe);
590
591         tda18271_calc_rf_filter_curve(fe, tm);
592
593         tda18271_por(fe);
594
595         priv->cal_initialized = true;
596
597         return 0;
598 }
599
600 static int tda18271c2_tune(struct dvb_frontend *fe,
601                            u32 ifc, u32 freq, u32 bw, u8 std)
602 {
603         int tm = 0;
604
605         tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
606
607         tda18271_init_cal(fe, &tm);
608
609         tda18271_rf_tracking_filters_correction(fe, freq, tm);
610
611         tda18271_channel_configuration(fe, ifc, freq, bw, std);
612
613         return 0;
614 }
615
616 /* ------------------------------------------------------------------ */
617
618 static int tda18271c1_tune(struct dvb_frontend *fe,
619                            u32 ifc, u32 freq, u32 bw, u8 std)
620 {
621         struct tda18271_priv *priv = fe->tuner_priv;
622         unsigned char *regs = priv->tda18271_regs;
623         u32 N = 0;
624
625         tda18271_init(fe);
626
627         tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
628
629         /* RF tracking filter calibration */
630
631         /* calculate bp filter */
632         tda18271_calc_bp_filter(fe, &freq);
633         tda18271_write_regs(fe, R_EP1, 1);
634
635         regs[R_EB4]  &= 0x07;
636         regs[R_EB4]  |= 0x60;
637         tda18271_write_regs(fe, R_EB4, 1);
638
639         regs[R_EB7]   = 0x60;
640         tda18271_write_regs(fe, R_EB7, 1);
641
642         regs[R_EB14]  = 0x00;
643         tda18271_write_regs(fe, R_EB14, 1);
644
645         regs[R_EB20]  = 0xcc;
646         tda18271_write_regs(fe, R_EB20, 1);
647
648         /* set cal mode to RF tracking filter calibration */
649         regs[R_EP4]  |= 0x03;
650
651         /* calculate cal pll */
652
653         switch (priv->mode) {
654         case TDA18271_ANALOG:
655                 N = freq - 1250000;
656                 break;
657         case TDA18271_DIGITAL:
658                 N = freq + bw / 2;
659                 break;
660         }
661
662         tda18271_calc_cal_pll(fe, N);
663
664         /* calculate main pll */
665
666         switch (priv->mode) {
667         case TDA18271_ANALOG:
668                 N = freq - 250000;
669                 break;
670         case TDA18271_DIGITAL:
671                 N = freq + bw / 2 + 1000000;
672                 break;
673         }
674
675         tda18271_calc_main_pll(fe, N);
676
677         tda18271_write_regs(fe, R_EP3, 11);
678         msleep(5); /* RF tracking filter calibration initialization */
679
680         /* search for K,M,CO for RF calibration */
681         tda18271_calc_km(fe, &freq);
682         tda18271_write_regs(fe, R_EB13, 1);
683
684         /* search for rf band */
685         tda18271_calc_rf_band(fe, &freq);
686
687         /* search for gain taper */
688         tda18271_calc_gain_taper(fe, &freq);
689
690         tda18271_write_regs(fe, R_EP2, 1);
691         tda18271_write_regs(fe, R_EP1, 1);
692         tda18271_write_regs(fe, R_EP2, 1);
693         tda18271_write_regs(fe, R_EP1, 1);
694
695         regs[R_EB4]  &= 0x07;
696         regs[R_EB4]  |= 0x40;
697         tda18271_write_regs(fe, R_EB4, 1);
698
699         regs[R_EB7]   = 0x40;
700         tda18271_write_regs(fe, R_EB7, 1);
701         msleep(10);
702
703         regs[R_EB20]  = 0xec;
704         tda18271_write_regs(fe, R_EB20, 1);
705         msleep(60); /* RF tracking filter calibration completion */
706
707         regs[R_EP4]  &= ~0x03; /* set cal mode to normal */
708         tda18271_write_regs(fe, R_EP4, 1);
709
710         tda18271_write_regs(fe, R_EP1, 1);
711
712         /* RF tracking filter correction for VHF_Low band */
713         if (0 == tda18271_calc_rf_cal(fe, &freq))
714                 tda18271_write_regs(fe, R_EB14, 1);
715
716         /* Channel Configuration */
717
718         switch (priv->mode) {
719         case TDA18271_ANALOG:
720                 regs[R_EB22]  = 0x2c;
721                 break;
722         case TDA18271_DIGITAL:
723                 regs[R_EB22]  = 0x37;
724                 break;
725         }
726         tda18271_write_regs(fe, R_EB22, 1);
727
728         regs[R_EP1]  |= 0x40; /* set dis power level on */
729
730         /* set standard */
731         regs[R_EP3]  &= ~0x1f; /* clear std bits */
732
733         /* see table 22 */
734         regs[R_EP3]  |= std;
735
736         regs[R_EP4]  &= ~0x03; /* set cal mode to normal */
737
738         regs[R_EP4]  &= ~0x1c; /* clear if level bits */
739         switch (priv->mode) {
740         case TDA18271_ANALOG:
741                 regs[R_MPD]  &= ~0x80; /* IF notch = 0 */
742                 break;
743         case TDA18271_DIGITAL:
744                 regs[R_EP4]  |= 0x04;
745                 regs[R_MPD]  |= 0x80;
746                 break;
747         }
748
749         regs[R_EP4]  &= ~0x80; /* turn this bit on only for fm */
750
751         /* image rejection validity */
752         tda18271_calc_ir_measure(fe, &freq);
753
754         /* calculate MAIN PLL */
755         N = freq + ifc;
756
757         tda18271_calc_main_pll(fe, N);
758
759         tda18271_write_regs(fe, R_TM, 15);
760         msleep(5);
761
762         return 0;
763 }
764
765 /* ------------------------------------------------------------------ */
766
767 static int tda18271_set_params(struct dvb_frontend *fe,
768                                struct dvb_frontend_parameters *params)
769 {
770         struct tda18271_priv *priv = fe->tuner_priv;
771         struct tda18271_std_map *std_map = &priv->std;
772         u8 std;
773         u16 sgIF;
774         u32 bw, freq = params->frequency;
775
776         BUG_ON(!priv->tune);
777
778         priv->mode = TDA18271_DIGITAL;
779
780         /* see table 22 */
781         if (fe->ops.info.type == FE_ATSC) {
782                 switch (params->u.vsb.modulation) {
783                 case VSB_8:
784                 case VSB_16:
785                         std  = std_map->atsc_6.std_bits;
786                         sgIF = std_map->atsc_6.if_freq;
787                         break;
788                 case QAM_64:
789                 case QAM_256:
790                         std  = std_map->qam_6.std_bits;
791                         sgIF = std_map->qam_6.if_freq;
792                         break;
793                 default:
794                         tda_warn("modulation not set!\n");
795                         return -EINVAL;
796                 }
797 #if 0
798                 /* userspace request is already center adjusted */
799                 freq += 1750000; /* Adjust to center (+1.75MHZ) */
800 #endif
801                 bw = 6000000;
802         } else if (fe->ops.info.type == FE_OFDM) {
803                 switch (params->u.ofdm.bandwidth) {
804                 case BANDWIDTH_6_MHZ:
805                         bw = 6000000;
806                         std  = std_map->dvbt_6.std_bits;
807                         sgIF = std_map->dvbt_6.if_freq;
808                         break;
809                 case BANDWIDTH_7_MHZ:
810                         bw = 7000000;
811                         std  = std_map->dvbt_7.std_bits;
812                         sgIF = std_map->dvbt_7.if_freq;
813                         break;
814                 case BANDWIDTH_8_MHZ:
815                         bw = 8000000;
816                         std  = std_map->dvbt_8.std_bits;
817                         sgIF = std_map->dvbt_8.if_freq;
818                         break;
819                 default:
820                         tda_warn("bandwidth not set!\n");
821                         return -EINVAL;
822                 }
823         } else {
824                 tda_warn("modulation type not supported!\n");
825                 return -EINVAL;
826         }
827
828         return priv->tune(fe, sgIF * 1000, freq, bw, std);
829 }
830
831 static int tda18271_set_analog_params(struct dvb_frontend *fe,
832                                       struct analog_parameters *params)
833 {
834         struct tda18271_priv *priv = fe->tuner_priv;
835         struct tda18271_std_map *std_map = &priv->std;
836         char *mode;
837         u8 std;
838         u16 sgIF;
839         u32 freq = params->frequency * 62500;
840
841         BUG_ON(!priv->tune);
842
843         priv->mode = TDA18271_ANALOG;
844
845         if (params->std & V4L2_STD_MN) {
846                 std  = std_map->atv_mn.std_bits;
847                 sgIF = std_map->atv_mn.if_freq;
848                 mode = "MN";
849         } else if (params->std & V4L2_STD_B) {
850                 std  = std_map->atv_b.std_bits;
851                 sgIF = std_map->atv_b.if_freq;
852                 mode = "B";
853         } else if (params->std & V4L2_STD_GH) {
854                 std  = std_map->atv_gh.std_bits;
855                 sgIF = std_map->atv_gh.if_freq;
856                 mode = "GH";
857         } else if (params->std & V4L2_STD_PAL_I) {
858                 std  = std_map->atv_i.std_bits;
859                 sgIF = std_map->atv_i.if_freq;
860                 mode = "I";
861         } else if (params->std & V4L2_STD_DK) {
862                 std  = std_map->atv_dk.std_bits;
863                 sgIF = std_map->atv_dk.if_freq;
864                 mode = "DK";
865         } else if (params->std & V4L2_STD_SECAM_L) {
866                 std  = std_map->atv_l.std_bits;
867                 sgIF = std_map->atv_l.if_freq;
868                 mode = "L";
869         } else if (params->std & V4L2_STD_SECAM_LC) {
870                 std  = std_map->atv_lc.std_bits;
871                 sgIF = std_map->atv_lc.if_freq;
872                 mode = "L'";
873         } else {
874                 std  = std_map->atv_i.std_bits;
875                 sgIF = std_map->atv_i.if_freq;
876                 mode = "xx";
877         }
878
879         tda_dbg("setting tda18271 to system %s\n", mode);
880
881         return priv->tune(fe, sgIF * 1000, freq, 0, std);
882 }
883
884 static int tda18271_release(struct dvb_frontend *fe)
885 {
886         kfree(fe->tuner_priv);
887         fe->tuner_priv = NULL;
888         return 0;
889 }
890
891 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
892 {
893         struct tda18271_priv *priv = fe->tuner_priv;
894         *frequency = priv->frequency;
895         return 0;
896 }
897
898 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
899 {
900         struct tda18271_priv *priv = fe->tuner_priv;
901         *bandwidth = priv->bandwidth;
902         return 0;
903 }
904
905 /* ------------------------------------------------------------------ */
906
907 #define tda18271_update_std(std_cfg, name) do {                         \
908         if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) {         \
909                 tda_dbg("Using custom std config for %s\n", name);      \
910                 memcpy(&std->std_cfg, &map->std_cfg,                    \
911                         sizeof(struct tda18271_std_map_item));          \
912         } } while (0)
913
914 #define tda18271_dump_std_item(std_cfg, name) do {                      \
915         tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n",               \
916                 name, std->std_cfg.if_freq, std->std_cfg.std_bits);     \
917         } while (0)
918
919 static int tda18271_dump_std_map(struct dvb_frontend *fe)
920 {
921         struct tda18271_priv *priv = fe->tuner_priv;
922         struct tda18271_std_map *std = &priv->std;
923
924         tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
925         tda18271_dump_std_item(atv_b,  "pal b");
926         tda18271_dump_std_item(atv_dk, "pal dk");
927         tda18271_dump_std_item(atv_gh, "pal gh");
928         tda18271_dump_std_item(atv_i,  "pal i");
929         tda18271_dump_std_item(atv_l,  "pal l");
930         tda18271_dump_std_item(atv_lc, "pal l'");
931         tda18271_dump_std_item(atv_mn, "atv mn");
932         tda18271_dump_std_item(atsc_6, "atsc 6");
933         tda18271_dump_std_item(dvbt_6, "dvbt 6");
934         tda18271_dump_std_item(dvbt_7, "dvbt 7");
935         tda18271_dump_std_item(dvbt_8, "dvbt 8");
936         tda18271_dump_std_item(qam_6,  "qam 6");
937         tda18271_dump_std_item(qam_8,  "qam 8");
938
939         return 0;
940 }
941
942 static int tda18271_update_std_map(struct dvb_frontend *fe,
943                                    struct tda18271_std_map *map)
944 {
945         struct tda18271_priv *priv = fe->tuner_priv;
946         struct tda18271_std_map *std = &priv->std;
947
948         if (!map)
949                 return -EINVAL;
950
951         tda18271_update_std(atv_b,  "atv b");
952         tda18271_update_std(atv_dk, "atv dk");
953         tda18271_update_std(atv_gh, "atv gh");
954         tda18271_update_std(atv_i,  "atv i");
955         tda18271_update_std(atv_l,  "atv l");
956         tda18271_update_std(atv_lc, "atv l'");
957         tda18271_update_std(atv_mn, "atv mn");
958         tda18271_update_std(atsc_6, "atsc 6");
959         tda18271_update_std(dvbt_6, "dvbt 6");
960         tda18271_update_std(dvbt_7, "dvbt 7");
961         tda18271_update_std(dvbt_8, "dvbt 8");
962         tda18271_update_std(qam_6,  "qam 6");
963         tda18271_update_std(qam_8,  "qam 8");
964
965         return 0;
966 }
967
968 static int tda18271_get_id(struct dvb_frontend *fe)
969 {
970         struct tda18271_priv *priv = fe->tuner_priv;
971         unsigned char *regs = priv->tda18271_regs;
972         char *name;
973         int ret = 0;
974
975         tda18271_read_regs(fe);
976
977         switch (regs[R_ID] & 0x7f) {
978         case 3:
979                 name = "TDA18271HD/C1";
980                 priv->id = TDA18271HDC1;
981                 priv->tune = tda18271c1_tune;
982                 break;
983         case 4:
984                 name = "TDA18271HD/C2";
985                 priv->id = TDA18271HDC2;
986                 priv->tune = tda18271c2_tune;
987                 break;
988         default:
989                 name = "Unknown device";
990                 ret = -EINVAL;
991                 break;
992         }
993
994         tda_info("%s detected @ %d-%04x%s\n", name,
995                  i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
996                  (0 == ret) ? "" : ", device not supported.");
997
998         return ret;
999 }
1000
1001 static struct dvb_tuner_ops tda18271_tuner_ops = {
1002         .info = {
1003                 .name = "NXP TDA18271HD",
1004                 .frequency_min  =  45000000,
1005                 .frequency_max  = 864000000,
1006                 .frequency_step =     62500
1007         },
1008         .init              = tda18271_init,
1009         .set_params        = tda18271_set_params,
1010         .set_analog_params = tda18271_set_analog_params,
1011         .release           = tda18271_release,
1012         .get_frequency     = tda18271_get_frequency,
1013         .get_bandwidth     = tda18271_get_bandwidth,
1014 };
1015
1016 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1017                                      struct i2c_adapter *i2c,
1018                                      struct tda18271_config *cfg)
1019 {
1020         struct tda18271_priv *priv = NULL;
1021
1022         priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
1023         if (priv == NULL)
1024                 return NULL;
1025
1026         priv->i2c_addr = addr;
1027         priv->i2c_adap = i2c;
1028         priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1029         priv->cal_initialized = false;
1030
1031         fe->tuner_priv = priv;
1032
1033         if (tda18271_get_id(fe) < 0)
1034                 goto fail;
1035
1036         if (tda18271_assign_map_layout(fe) < 0)
1037                 goto fail;
1038
1039         memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1040                sizeof(struct dvb_tuner_ops));
1041
1042         /* override default std map with values in config struct */
1043         if ((cfg) && (cfg->std_map))
1044                 tda18271_update_std_map(fe, cfg->std_map);
1045
1046         if (tda18271_debug & DBG_MAP)
1047                 tda18271_dump_std_map(fe);
1048
1049         tda18271_init_regs(fe);
1050
1051         return fe;
1052 fail:
1053         tda18271_release(fe);
1054         return NULL;
1055 }
1056 EXPORT_SYMBOL_GPL(tda18271_attach);
1057 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1058 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1059 MODULE_LICENSE("GPL");
1060 MODULE_VERSION("0.2");
1061
1062 /*
1063  * Overrides for Emacs so that we follow Linus's tabbing style.
1064  * ---------------------------------------------------------------------------
1065  * Local variables:
1066  * c-basic-offset: 8
1067  * End:
1068  */