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