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