V4L/DVB: ngene: Initial check-in
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / common / tuners / tda18271-fe.c
blobb2e15456d5f36600c32c78542c42d7188634071a
1 /*
2 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
4 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
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.
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.
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.
21 #include <linux/delay.h>
22 #include <linux/videodev2.h>
23 #include "tda18271-priv.h"
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))");
30 static int tda18271_cal_on_startup = -1;
31 module_param_named(cal, tda18271_cal_on_startup, int, 0644);
32 MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");
34 static DEFINE_MUTEX(tda18271_list_mutex);
35 static LIST_HEAD(hybrid_tuner_instance_list);
37 /*---------------------------------------------------------------------*/
39 static int tda18271_toggle_output(struct dvb_frontend *fe, int standby)
41 struct tda18271_priv *priv = fe->tuner_priv;
43 int ret = tda18271_set_standby_mode(fe, standby ? 1 : 0,
44 priv->output_opt & TDA18271_OUTPUT_LT_OFF ? 1 : 0,
45 priv->output_opt & TDA18271_OUTPUT_XT_OFF ? 1 : 0);
47 if (tda_fail(ret))
48 goto fail;
50 tda_dbg("%s mode: xtal oscillator %s, slave tuner loop thru %s\n",
51 standby ? "standby" : "active",
52 priv->output_opt & TDA18271_OUTPUT_XT_OFF ? "off" : "on",
53 priv->output_opt & TDA18271_OUTPUT_LT_OFF ? "off" : "on");
54 fail:
55 return ret;
58 /*---------------------------------------------------------------------*/
60 static inline int charge_pump_source(struct dvb_frontend *fe, int force)
62 struct tda18271_priv *priv = fe->tuner_priv;
63 return tda18271_charge_pump_source(fe,
64 (priv->role == TDA18271_SLAVE) ?
65 TDA18271_CAL_PLL :
66 TDA18271_MAIN_PLL, force);
69 static inline void tda18271_set_if_notch(struct dvb_frontend *fe)
71 struct tda18271_priv *priv = fe->tuner_priv;
72 unsigned char *regs = priv->tda18271_regs;
74 switch (priv->mode) {
75 case TDA18271_ANALOG:
76 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
77 break;
78 case TDA18271_DIGITAL:
79 regs[R_MPD] |= 0x80; /* IF notch = 1 */
80 break;
84 static int tda18271_channel_configuration(struct dvb_frontend *fe,
85 struct tda18271_std_map_item *map,
86 u32 freq, u32 bw)
88 struct tda18271_priv *priv = fe->tuner_priv;
89 unsigned char *regs = priv->tda18271_regs;
90 int ret;
91 u32 N;
93 /* update TV broadcast parameters */
95 /* set standard */
96 regs[R_EP3] &= ~0x1f; /* clear std bits */
97 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
99 if (priv->id == TDA18271HDC2) {
100 /* set rfagc to high speed mode */
101 regs[R_EP3] &= ~0x04;
104 /* set cal mode to normal */
105 regs[R_EP4] &= ~0x03;
107 /* update IF output level */
108 regs[R_EP4] &= ~0x1c; /* clear if level bits */
109 regs[R_EP4] |= (map->if_lvl << 2);
111 /* update FM_RFn */
112 regs[R_EP4] &= ~0x80;
113 regs[R_EP4] |= map->fm_rfn << 7;
115 /* update rf top / if top */
116 regs[R_EB22] = 0x00;
117 regs[R_EB22] |= map->rfagc_top;
118 ret = tda18271_write_regs(fe, R_EB22, 1);
119 if (tda_fail(ret))
120 goto fail;
122 /* --------------------------------------------------------------- */
124 /* disable Power Level Indicator */
125 regs[R_EP1] |= 0x40;
127 /* make sure thermometer is off */
128 regs[R_TM] &= ~0x10;
130 /* frequency dependent parameters */
132 tda18271_calc_ir_measure(fe, &freq);
134 tda18271_calc_bp_filter(fe, &freq);
136 tda18271_calc_rf_band(fe, &freq);
138 tda18271_calc_gain_taper(fe, &freq);
140 /* --------------------------------------------------------------- */
142 /* dual tuner and agc1 extra configuration */
144 switch (priv->role) {
145 case TDA18271_MASTER:
146 regs[R_EB1] |= 0x04; /* main vco */
147 break;
148 case TDA18271_SLAVE:
149 regs[R_EB1] &= ~0x04; /* cal vco */
150 break;
153 /* agc1 always active */
154 regs[R_EB1] &= ~0x02;
156 /* agc1 has priority on agc2 */
157 regs[R_EB1] &= ~0x01;
159 ret = tda18271_write_regs(fe, R_EB1, 1);
160 if (tda_fail(ret))
161 goto fail;
163 /* --------------------------------------------------------------- */
165 N = map->if_freq * 1000 + freq;
167 switch (priv->role) {
168 case TDA18271_MASTER:
169 tda18271_calc_main_pll(fe, N);
170 tda18271_set_if_notch(fe);
171 tda18271_write_regs(fe, R_MPD, 4);
172 break;
173 case TDA18271_SLAVE:
174 tda18271_calc_cal_pll(fe, N);
175 tda18271_write_regs(fe, R_CPD, 4);
177 regs[R_MPD] = regs[R_CPD] & 0x7f;
178 tda18271_set_if_notch(fe);
179 tda18271_write_regs(fe, R_MPD, 1);
180 break;
183 ret = tda18271_write_regs(fe, R_TM, 7);
184 if (tda_fail(ret))
185 goto fail;
187 /* force charge pump source */
188 charge_pump_source(fe, 1);
190 msleep(1);
192 /* return pll to normal operation */
193 charge_pump_source(fe, 0);
195 msleep(20);
197 if (priv->id == TDA18271HDC2) {
198 /* set rfagc to normal speed mode */
199 if (map->fm_rfn)
200 regs[R_EP3] &= ~0x04;
201 else
202 regs[R_EP3] |= 0x04;
203 ret = tda18271_write_regs(fe, R_EP3, 1);
205 fail:
206 return ret;
209 static int tda18271_read_thermometer(struct dvb_frontend *fe)
211 struct tda18271_priv *priv = fe->tuner_priv;
212 unsigned char *regs = priv->tda18271_regs;
213 int tm;
215 /* switch thermometer on */
216 regs[R_TM] |= 0x10;
217 tda18271_write_regs(fe, R_TM, 1);
219 /* read thermometer info */
220 tda18271_read_regs(fe);
222 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
223 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
225 if ((regs[R_TM] & 0x20) == 0x20)
226 regs[R_TM] &= ~0x20;
227 else
228 regs[R_TM] |= 0x20;
230 tda18271_write_regs(fe, R_TM, 1);
232 msleep(10); /* temperature sensing */
234 /* read thermometer info */
235 tda18271_read_regs(fe);
238 tm = tda18271_lookup_thermometer(fe);
240 /* switch thermometer off */
241 regs[R_TM] &= ~0x10;
242 tda18271_write_regs(fe, R_TM, 1);
244 /* set CAL mode to normal */
245 regs[R_EP4] &= ~0x03;
246 tda18271_write_regs(fe, R_EP4, 1);
248 return tm;
251 /* ------------------------------------------------------------------ */
253 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
254 u32 freq)
256 struct tda18271_priv *priv = fe->tuner_priv;
257 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
258 unsigned char *regs = priv->tda18271_regs;
259 int i, ret;
260 u8 tm_current, dc_over_dt, rf_tab;
261 s32 rfcal_comp, approx;
263 /* power up */
264 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
265 if (tda_fail(ret))
266 goto fail;
268 /* read die current temperature */
269 tm_current = tda18271_read_thermometer(fe);
271 /* frequency dependent parameters */
273 tda18271_calc_rf_cal(fe, &freq);
274 rf_tab = regs[R_EB14];
276 i = tda18271_lookup_rf_band(fe, &freq, NULL);
277 if (tda_fail(i))
278 return i;
280 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
281 approx = map[i].rf_a1 * (s32)(freq / 1000 - map[i].rf1) +
282 map[i].rf_b1 + rf_tab;
283 } else {
284 approx = map[i].rf_a2 * (s32)(freq / 1000 - map[i].rf2) +
285 map[i].rf_b2 + rf_tab;
288 if (approx < 0)
289 approx = 0;
290 if (approx > 255)
291 approx = 255;
293 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
295 /* calculate temperature compensation */
296 rfcal_comp = dc_over_dt * (s32)(tm_current - priv->tm_rfcal) / 1000;
298 regs[R_EB14] = (unsigned char)(approx + rfcal_comp);
299 ret = tda18271_write_regs(fe, R_EB14, 1);
300 fail:
301 return ret;
304 static int tda18271_por(struct dvb_frontend *fe)
306 struct tda18271_priv *priv = fe->tuner_priv;
307 unsigned char *regs = priv->tda18271_regs;
308 int ret;
310 /* power up detector 1 */
311 regs[R_EB12] &= ~0x20;
312 ret = tda18271_write_regs(fe, R_EB12, 1);
313 if (tda_fail(ret))
314 goto fail;
316 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
317 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
318 ret = tda18271_write_regs(fe, R_EB18, 1);
319 if (tda_fail(ret))
320 goto fail;
322 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
324 /* POR mode */
325 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
326 if (tda_fail(ret))
327 goto fail;
329 /* disable 1.5 MHz low pass filter */
330 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
331 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
332 ret = tda18271_write_regs(fe, R_EB21, 3);
333 fail:
334 return ret;
337 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
339 struct tda18271_priv *priv = fe->tuner_priv;
340 unsigned char *regs = priv->tda18271_regs;
341 u32 N;
343 /* set CAL mode to normal */
344 regs[R_EP4] &= ~0x03;
345 tda18271_write_regs(fe, R_EP4, 1);
347 /* switch off agc1 */
348 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
350 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
351 tda18271_write_regs(fe, R_EB18, 1);
353 /* frequency dependent parameters */
355 tda18271_calc_bp_filter(fe, &freq);
356 tda18271_calc_gain_taper(fe, &freq);
357 tda18271_calc_rf_band(fe, &freq);
358 tda18271_calc_km(fe, &freq);
360 tda18271_write_regs(fe, R_EP1, 3);
361 tda18271_write_regs(fe, R_EB13, 1);
363 /* main pll charge pump source */
364 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
366 /* cal pll charge pump source */
367 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
369 /* force dcdc converter to 0 V */
370 regs[R_EB14] = 0x00;
371 tda18271_write_regs(fe, R_EB14, 1);
373 /* disable plls lock */
374 regs[R_EB20] &= ~0x20;
375 tda18271_write_regs(fe, R_EB20, 1);
377 /* set CAL mode to RF tracking filter calibration */
378 regs[R_EP4] |= 0x03;
379 tda18271_write_regs(fe, R_EP4, 2);
381 /* --------------------------------------------------------------- */
383 /* set the internal calibration signal */
384 N = freq;
386 tda18271_calc_cal_pll(fe, N);
387 tda18271_write_regs(fe, R_CPD, 4);
389 /* downconvert internal calibration */
390 N += 1000000;
392 tda18271_calc_main_pll(fe, N);
393 tda18271_write_regs(fe, R_MPD, 4);
395 msleep(5);
397 tda18271_write_regs(fe, R_EP2, 1);
398 tda18271_write_regs(fe, R_EP1, 1);
399 tda18271_write_regs(fe, R_EP2, 1);
400 tda18271_write_regs(fe, R_EP1, 1);
402 /* --------------------------------------------------------------- */
404 /* normal operation for the main pll */
405 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
407 /* normal operation for the cal pll */
408 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
410 msleep(10); /* plls locking */
412 /* launch the rf tracking filters calibration */
413 regs[R_EB20] |= 0x20;
414 tda18271_write_regs(fe, R_EB20, 1);
416 msleep(60); /* calibration */
418 /* --------------------------------------------------------------- */
420 /* set CAL mode to normal */
421 regs[R_EP4] &= ~0x03;
423 /* switch on agc1 */
424 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
426 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
427 tda18271_write_regs(fe, R_EB18, 1);
429 tda18271_write_regs(fe, R_EP3, 2);
431 /* synchronization */
432 tda18271_write_regs(fe, R_EP1, 1);
434 /* get calibration result */
435 tda18271_read_extended(fe);
437 return regs[R_EB14];
440 static int tda18271_powerscan(struct dvb_frontend *fe,
441 u32 *freq_in, u32 *freq_out)
443 struct tda18271_priv *priv = fe->tuner_priv;
444 unsigned char *regs = priv->tda18271_regs;
445 int sgn, bcal, count, wait, ret;
446 u8 cid_target;
447 u16 count_limit;
448 u32 freq;
450 freq = *freq_in;
452 tda18271_calc_rf_band(fe, &freq);
453 tda18271_calc_rf_cal(fe, &freq);
454 tda18271_calc_gain_taper(fe, &freq);
455 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
457 tda18271_write_regs(fe, R_EP2, 1);
458 tda18271_write_regs(fe, R_EB14, 1);
460 /* downconvert frequency */
461 freq += 1000000;
463 tda18271_calc_main_pll(fe, freq);
464 tda18271_write_regs(fe, R_MPD, 4);
466 msleep(5); /* pll locking */
468 /* detection mode */
469 regs[R_EP4] &= ~0x03;
470 regs[R_EP4] |= 0x01;
471 tda18271_write_regs(fe, R_EP4, 1);
473 /* launch power detection measurement */
474 tda18271_write_regs(fe, R_EP2, 1);
476 /* read power detection info, stored in EB10 */
477 ret = tda18271_read_extended(fe);
478 if (tda_fail(ret))
479 return ret;
481 /* algorithm initialization */
482 sgn = 1;
483 *freq_out = *freq_in;
484 bcal = 0;
485 count = 0;
486 wait = false;
488 while ((regs[R_EB10] & 0x3f) < cid_target) {
489 /* downconvert updated freq to 1 MHz */
490 freq = *freq_in + (sgn * count) + 1000000;
492 tda18271_calc_main_pll(fe, freq);
493 tda18271_write_regs(fe, R_MPD, 4);
495 if (wait) {
496 msleep(5); /* pll locking */
497 wait = false;
498 } else
499 udelay(100); /* pll locking */
501 /* launch power detection measurement */
502 tda18271_write_regs(fe, R_EP2, 1);
504 /* read power detection info, stored in EB10 */
505 ret = tda18271_read_extended(fe);
506 if (tda_fail(ret))
507 return ret;
509 count += 200;
511 if (count <= count_limit)
512 continue;
514 if (sgn <= 0)
515 break;
517 sgn = -1 * sgn;
518 count = 200;
519 wait = true;
522 if ((regs[R_EB10] & 0x3f) >= cid_target) {
523 bcal = 1;
524 *freq_out = freq - 1000000;
525 } else
526 bcal = 0;
528 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
529 bcal, *freq_in, *freq_out, freq);
531 return bcal;
534 static int tda18271_powerscan_init(struct dvb_frontend *fe)
536 struct tda18271_priv *priv = fe->tuner_priv;
537 unsigned char *regs = priv->tda18271_regs;
538 int ret;
540 /* set standard to digital */
541 regs[R_EP3] &= ~0x1f; /* clear std bits */
542 regs[R_EP3] |= 0x12;
544 /* set cal mode to normal */
545 regs[R_EP4] &= ~0x03;
547 /* update IF output level */
548 regs[R_EP4] &= ~0x1c; /* clear if level bits */
550 ret = tda18271_write_regs(fe, R_EP3, 2);
551 if (tda_fail(ret))
552 goto fail;
554 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
555 ret = tda18271_write_regs(fe, R_EB18, 1);
556 if (tda_fail(ret))
557 goto fail;
559 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
561 /* 1.5 MHz low pass filter */
562 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
563 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
565 ret = tda18271_write_regs(fe, R_EB21, 3);
566 fail:
567 return ret;
570 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
572 struct tda18271_priv *priv = fe->tuner_priv;
573 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
574 unsigned char *regs = priv->tda18271_regs;
575 int bcal, rf, i;
576 s32 divisor, dividend;
577 #define RF1 0
578 #define RF2 1
579 #define RF3 2
580 u32 rf_default[3];
581 u32 rf_freq[3];
582 u8 prog_cal[3];
583 u8 prog_tab[3];
585 i = tda18271_lookup_rf_band(fe, &freq, NULL);
587 if (tda_fail(i))
588 return i;
590 rf_default[RF1] = 1000 * map[i].rf1_def;
591 rf_default[RF2] = 1000 * map[i].rf2_def;
592 rf_default[RF3] = 1000 * map[i].rf3_def;
594 for (rf = RF1; rf <= RF3; rf++) {
595 if (0 == rf_default[rf])
596 return 0;
597 tda_cal("freq = %d, rf = %d\n", freq, rf);
599 /* look for optimized calibration frequency */
600 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
601 if (tda_fail(bcal))
602 return bcal;
604 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
605 prog_tab[rf] = regs[R_EB14];
607 if (1 == bcal)
608 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
609 else
610 prog_cal[rf] = prog_tab[rf];
612 switch (rf) {
613 case RF1:
614 map[i].rf_a1 = 0;
615 map[i].rf_b1 = (s32)(prog_cal[RF1] - prog_tab[RF1]);
616 map[i].rf1 = rf_freq[RF1] / 1000;
617 break;
618 case RF2:
619 dividend = (s32)(prog_cal[RF2] - prog_tab[RF2]) -
620 (s32)(prog_cal[RF1] + prog_tab[RF1]);
621 divisor = (s32)(rf_freq[RF2] - rf_freq[RF1]) / 1000;
622 map[i].rf_a1 = (dividend / divisor);
623 map[i].rf2 = rf_freq[RF2] / 1000;
624 break;
625 case RF3:
626 dividend = (s32)(prog_cal[RF3] - prog_tab[RF3]) -
627 (s32)(prog_cal[RF2] + prog_tab[RF2]);
628 divisor = (s32)(rf_freq[RF3] - rf_freq[RF2]) / 1000;
629 map[i].rf_a2 = (dividend / divisor);
630 map[i].rf_b2 = (s32)(prog_cal[RF2] - prog_tab[RF2]);
631 map[i].rf3 = rf_freq[RF3] / 1000;
632 break;
633 default:
634 BUG();
638 return 0;
641 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
643 struct tda18271_priv *priv = fe->tuner_priv;
644 unsigned int i;
645 int ret;
647 tda_info("tda18271: performing RF tracking filter calibration\n");
649 /* wait for die temperature stabilization */
650 msleep(200);
652 ret = tda18271_powerscan_init(fe);
653 if (tda_fail(ret))
654 goto fail;
656 /* rf band calibration */
657 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
658 ret =
659 tda18271_rf_tracking_filters_init(fe, 1000 *
660 priv->rf_cal_state[i].rfmax);
661 if (tda_fail(ret))
662 goto fail;
665 priv->tm_rfcal = tda18271_read_thermometer(fe);
666 fail:
667 return ret;
670 /* ------------------------------------------------------------------ */
672 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
674 struct tda18271_priv *priv = fe->tuner_priv;
675 unsigned char *regs = priv->tda18271_regs;
676 int ret;
678 /* test RF_CAL_OK to see if we need init */
679 if ((regs[R_EP1] & 0x10) == 0)
680 priv->cal_initialized = false;
682 if (priv->cal_initialized)
683 return 0;
685 ret = tda18271_calc_rf_filter_curve(fe);
686 if (tda_fail(ret))
687 goto fail;
689 ret = tda18271_por(fe);
690 if (tda_fail(ret))
691 goto fail;
693 tda_info("tda18271: RF tracking filter calibration complete\n");
695 priv->cal_initialized = true;
696 goto end;
697 fail:
698 tda_info("tda18271: RF tracking filter calibration failed!\n");
699 end:
700 return ret;
703 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
704 u32 freq, u32 bw)
706 struct tda18271_priv *priv = fe->tuner_priv;
707 unsigned char *regs = priv->tda18271_regs;
708 int ret;
709 u32 N = 0;
711 /* calculate bp filter */
712 tda18271_calc_bp_filter(fe, &freq);
713 tda18271_write_regs(fe, R_EP1, 1);
715 regs[R_EB4] &= 0x07;
716 regs[R_EB4] |= 0x60;
717 tda18271_write_regs(fe, R_EB4, 1);
719 regs[R_EB7] = 0x60;
720 tda18271_write_regs(fe, R_EB7, 1);
722 regs[R_EB14] = 0x00;
723 tda18271_write_regs(fe, R_EB14, 1);
725 regs[R_EB20] = 0xcc;
726 tda18271_write_regs(fe, R_EB20, 1);
728 /* set cal mode to RF tracking filter calibration */
729 regs[R_EP4] |= 0x03;
731 /* calculate cal pll */
733 switch (priv->mode) {
734 case TDA18271_ANALOG:
735 N = freq - 1250000;
736 break;
737 case TDA18271_DIGITAL:
738 N = freq + bw / 2;
739 break;
742 tda18271_calc_cal_pll(fe, N);
744 /* calculate main pll */
746 switch (priv->mode) {
747 case TDA18271_ANALOG:
748 N = freq - 250000;
749 break;
750 case TDA18271_DIGITAL:
751 N = freq + bw / 2 + 1000000;
752 break;
755 tda18271_calc_main_pll(fe, N);
757 ret = tda18271_write_regs(fe, R_EP3, 11);
758 if (tda_fail(ret))
759 return ret;
761 msleep(5); /* RF tracking filter calibration initialization */
763 /* search for K,M,CO for RF calibration */
764 tda18271_calc_km(fe, &freq);
765 tda18271_write_regs(fe, R_EB13, 1);
767 /* search for rf band */
768 tda18271_calc_rf_band(fe, &freq);
770 /* search for gain taper */
771 tda18271_calc_gain_taper(fe, &freq);
773 tda18271_write_regs(fe, R_EP2, 1);
774 tda18271_write_regs(fe, R_EP1, 1);
775 tda18271_write_regs(fe, R_EP2, 1);
776 tda18271_write_regs(fe, R_EP1, 1);
778 regs[R_EB4] &= 0x07;
779 regs[R_EB4] |= 0x40;
780 tda18271_write_regs(fe, R_EB4, 1);
782 regs[R_EB7] = 0x40;
783 tda18271_write_regs(fe, R_EB7, 1);
784 msleep(10); /* pll locking */
786 regs[R_EB20] = 0xec;
787 tda18271_write_regs(fe, R_EB20, 1);
788 msleep(60); /* RF tracking filter calibration completion */
790 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
791 tda18271_write_regs(fe, R_EP4, 1);
793 tda18271_write_regs(fe, R_EP1, 1);
795 /* RF tracking filter correction for VHF_Low band */
796 if (0 == tda18271_calc_rf_cal(fe, &freq))
797 tda18271_write_regs(fe, R_EB14, 1);
799 return 0;
802 /* ------------------------------------------------------------------ */
804 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
806 struct tda18271_priv *priv = fe->tuner_priv;
807 unsigned char *regs = priv->tda18271_regs;
808 int ret;
810 ret = tda18271_read_regs(fe);
811 if (tda_fail(ret))
812 goto fail;
814 /* test IR_CAL_OK to see if we need init */
815 if ((regs[R_EP1] & 0x08) == 0)
816 ret = tda18271_init_regs(fe);
817 fail:
818 return ret;
821 static int tda18271_init(struct dvb_frontend *fe)
823 struct tda18271_priv *priv = fe->tuner_priv;
824 int ret;
826 mutex_lock(&priv->lock);
828 /* full power up */
829 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
830 if (tda_fail(ret))
831 goto fail;
833 /* initialization */
834 ret = tda18271_ir_cal_init(fe);
835 if (tda_fail(ret))
836 goto fail;
838 if (priv->id == TDA18271HDC2)
839 tda18271c2_rf_cal_init(fe);
840 fail:
841 mutex_unlock(&priv->lock);
843 return ret;
846 static int tda18271_sleep(struct dvb_frontend *fe)
848 struct tda18271_priv *priv = fe->tuner_priv;
849 int ret;
851 mutex_lock(&priv->lock);
853 /* enter standby mode, with required output features enabled */
854 ret = tda18271_toggle_output(fe, 1);
856 mutex_unlock(&priv->lock);
858 return ret;
861 /* ------------------------------------------------------------------ */
863 static int tda18271_agc(struct dvb_frontend *fe)
865 struct tda18271_priv *priv = fe->tuner_priv;
866 int ret = 0;
868 switch (priv->config) {
869 case 0:
870 /* no external agc configuration required */
871 if (tda18271_debug & DBG_ADV)
872 tda_dbg("no agc configuration provided\n");
873 break;
874 case 3:
875 /* switch with GPIO of saa713x */
876 tda_dbg("invoking callback\n");
877 if (fe->callback)
878 ret = fe->callback(priv->i2c_props.adap->algo_data,
879 DVB_FRONTEND_COMPONENT_TUNER,
880 TDA18271_CALLBACK_CMD_AGC_ENABLE,
881 priv->mode);
882 break;
883 case 1:
884 case 2:
885 default:
886 /* n/a - currently not supported */
887 tda_err("unsupported configuration: %d\n", priv->config);
888 ret = -EINVAL;
889 break;
891 return ret;
894 static int tda18271_tune(struct dvb_frontend *fe,
895 struct tda18271_std_map_item *map, u32 freq, u32 bw)
897 struct tda18271_priv *priv = fe->tuner_priv;
898 int ret;
900 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
901 freq, map->if_freq, bw, map->agc_mode, map->std);
903 ret = tda18271_agc(fe);
904 if (tda_fail(ret))
905 tda_warn("failed to configure agc\n");
907 ret = tda18271_init(fe);
908 if (tda_fail(ret))
909 goto fail;
911 mutex_lock(&priv->lock);
913 switch (priv->id) {
914 case TDA18271HDC1:
915 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
916 break;
917 case TDA18271HDC2:
918 tda18271c2_rf_tracking_filters_correction(fe, freq);
919 break;
921 ret = tda18271_channel_configuration(fe, map, freq, bw);
923 mutex_unlock(&priv->lock);
924 fail:
925 return ret;
928 /* ------------------------------------------------------------------ */
930 static int tda18271_set_params(struct dvb_frontend *fe,
931 struct dvb_frontend_parameters *params)
933 struct tda18271_priv *priv = fe->tuner_priv;
934 struct tda18271_std_map *std_map = &priv->std;
935 struct tda18271_std_map_item *map;
936 int ret;
937 u32 bw, freq = params->frequency;
939 priv->mode = TDA18271_DIGITAL;
941 if (fe->ops.info.type == FE_ATSC) {
942 switch (params->u.vsb.modulation) {
943 case VSB_8:
944 case VSB_16:
945 map = &std_map->atsc_6;
946 break;
947 case QAM_64:
948 case QAM_256:
949 map = &std_map->qam_6;
950 break;
951 default:
952 tda_warn("modulation not set!\n");
953 return -EINVAL;
955 #if 0
956 /* userspace request is already center adjusted */
957 freq += 1750000; /* Adjust to center (+1.75MHZ) */
958 #endif
959 bw = 6000000;
960 } else if (fe->ops.info.type == FE_OFDM) {
961 switch (params->u.ofdm.bandwidth) {
962 case BANDWIDTH_6_MHZ:
963 bw = 6000000;
964 map = &std_map->dvbt_6;
965 break;
966 case BANDWIDTH_7_MHZ:
967 bw = 7000000;
968 map = &std_map->dvbt_7;
969 break;
970 case BANDWIDTH_8_MHZ:
971 bw = 8000000;
972 map = &std_map->dvbt_8;
973 break;
974 default:
975 tda_warn("bandwidth not set!\n");
976 return -EINVAL;
978 } else {
979 tda_warn("modulation type not supported!\n");
980 return -EINVAL;
983 /* When tuning digital, the analog demod must be tri-stated */
984 if (fe->ops.analog_ops.standby)
985 fe->ops.analog_ops.standby(fe);
987 ret = tda18271_tune(fe, map, freq, bw);
989 if (tda_fail(ret))
990 goto fail;
992 priv->frequency = freq;
993 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
994 params->u.ofdm.bandwidth : 0;
995 fail:
996 return ret;
999 static int tda18271_set_analog_params(struct dvb_frontend *fe,
1000 struct analog_parameters *params)
1002 struct tda18271_priv *priv = fe->tuner_priv;
1003 struct tda18271_std_map *std_map = &priv->std;
1004 struct tda18271_std_map_item *map;
1005 char *mode;
1006 int ret;
1007 u32 freq = params->frequency * 125 *
1008 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
1010 priv->mode = TDA18271_ANALOG;
1012 if (params->mode == V4L2_TUNER_RADIO) {
1013 map = &std_map->fm_radio;
1014 mode = "fm";
1015 } else if (params->std & V4L2_STD_MN) {
1016 map = &std_map->atv_mn;
1017 mode = "MN";
1018 } else if (params->std & V4L2_STD_B) {
1019 map = &std_map->atv_b;
1020 mode = "B";
1021 } else if (params->std & V4L2_STD_GH) {
1022 map = &std_map->atv_gh;
1023 mode = "GH";
1024 } else if (params->std & V4L2_STD_PAL_I) {
1025 map = &std_map->atv_i;
1026 mode = "I";
1027 } else if (params->std & V4L2_STD_DK) {
1028 map = &std_map->atv_dk;
1029 mode = "DK";
1030 } else if (params->std & V4L2_STD_SECAM_L) {
1031 map = &std_map->atv_l;
1032 mode = "L";
1033 } else if (params->std & V4L2_STD_SECAM_LC) {
1034 map = &std_map->atv_lc;
1035 mode = "L'";
1036 } else {
1037 map = &std_map->atv_i;
1038 mode = "xx";
1041 tda_dbg("setting tda18271 to system %s\n", mode);
1043 ret = tda18271_tune(fe, map, freq, 0);
1045 if (tda_fail(ret))
1046 goto fail;
1048 priv->frequency = freq;
1049 priv->bandwidth = 0;
1050 fail:
1051 return ret;
1054 static int tda18271_release(struct dvb_frontend *fe)
1056 struct tda18271_priv *priv = fe->tuner_priv;
1058 mutex_lock(&tda18271_list_mutex);
1060 if (priv)
1061 hybrid_tuner_release_state(priv);
1063 mutex_unlock(&tda18271_list_mutex);
1065 fe->tuner_priv = NULL;
1067 return 0;
1070 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1072 struct tda18271_priv *priv = fe->tuner_priv;
1073 *frequency = priv->frequency;
1074 return 0;
1077 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1079 struct tda18271_priv *priv = fe->tuner_priv;
1080 *bandwidth = priv->bandwidth;
1081 return 0;
1084 /* ------------------------------------------------------------------ */
1086 #define tda18271_update_std(std_cfg, name) do { \
1087 if (map->std_cfg.if_freq + \
1088 map->std_cfg.agc_mode + map->std_cfg.std + \
1089 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1090 tda_dbg("Using custom std config for %s\n", name); \
1091 memcpy(&std->std_cfg, &map->std_cfg, \
1092 sizeof(struct tda18271_std_map_item)); \
1093 } } while (0)
1095 #define tda18271_dump_std_item(std_cfg, name) do { \
1096 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1097 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1098 name, std->std_cfg.if_freq, \
1099 std->std_cfg.agc_mode, std->std_cfg.std, \
1100 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1101 } while (0)
1103 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1105 struct tda18271_priv *priv = fe->tuner_priv;
1106 struct tda18271_std_map *std = &priv->std;
1108 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1109 tda18271_dump_std_item(fm_radio, " fm ");
1110 tda18271_dump_std_item(atv_b, "atv b ");
1111 tda18271_dump_std_item(atv_dk, "atv dk");
1112 tda18271_dump_std_item(atv_gh, "atv gh");
1113 tda18271_dump_std_item(atv_i, "atv i ");
1114 tda18271_dump_std_item(atv_l, "atv l ");
1115 tda18271_dump_std_item(atv_lc, "atv l'");
1116 tda18271_dump_std_item(atv_mn, "atv mn");
1117 tda18271_dump_std_item(atsc_6, "atsc 6");
1118 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1119 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1120 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1121 tda18271_dump_std_item(qam_6, "qam 6 ");
1122 tda18271_dump_std_item(qam_8, "qam 8 ");
1124 return 0;
1127 static int tda18271_update_std_map(struct dvb_frontend *fe,
1128 struct tda18271_std_map *map)
1130 struct tda18271_priv *priv = fe->tuner_priv;
1131 struct tda18271_std_map *std = &priv->std;
1133 if (!map)
1134 return -EINVAL;
1136 tda18271_update_std(fm_radio, "fm");
1137 tda18271_update_std(atv_b, "atv b");
1138 tda18271_update_std(atv_dk, "atv dk");
1139 tda18271_update_std(atv_gh, "atv gh");
1140 tda18271_update_std(atv_i, "atv i");
1141 tda18271_update_std(atv_l, "atv l");
1142 tda18271_update_std(atv_lc, "atv l'");
1143 tda18271_update_std(atv_mn, "atv mn");
1144 tda18271_update_std(atsc_6, "atsc 6");
1145 tda18271_update_std(dvbt_6, "dvbt 6");
1146 tda18271_update_std(dvbt_7, "dvbt 7");
1147 tda18271_update_std(dvbt_8, "dvbt 8");
1148 tda18271_update_std(qam_6, "qam 6");
1149 tda18271_update_std(qam_8, "qam 8");
1151 return 0;
1154 static int tda18271_get_id(struct dvb_frontend *fe)
1156 struct tda18271_priv *priv = fe->tuner_priv;
1157 unsigned char *regs = priv->tda18271_regs;
1158 char *name;
1159 int ret = 0;
1161 mutex_lock(&priv->lock);
1162 tda18271_read_regs(fe);
1163 mutex_unlock(&priv->lock);
1165 switch (regs[R_ID] & 0x7f) {
1166 case 3:
1167 name = "TDA18271HD/C1";
1168 priv->id = TDA18271HDC1;
1169 break;
1170 case 4:
1171 name = "TDA18271HD/C2";
1172 priv->id = TDA18271HDC2;
1173 break;
1174 default:
1175 name = "Unknown device";
1176 ret = -EINVAL;
1177 break;
1180 tda_info("%s detected @ %d-%04x%s\n", name,
1181 i2c_adapter_id(priv->i2c_props.adap),
1182 priv->i2c_props.addr,
1183 (0 == ret) ? "" : ", device not supported.");
1185 return ret;
1188 static int tda18271_setup_configuration(struct dvb_frontend *fe,
1189 struct tda18271_config *cfg)
1191 struct tda18271_priv *priv = fe->tuner_priv;
1193 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1194 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1195 priv->config = (cfg) ? cfg->config : 0;
1196 priv->small_i2c = (cfg) ?
1197 cfg->small_i2c : TDA18271_39_BYTE_CHUNK_INIT;
1198 priv->output_opt = (cfg) ?
1199 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
1201 return 0;
1204 static inline int tda18271_need_cal_on_startup(struct tda18271_config *cfg)
1206 /* tda18271_cal_on_startup == -1 when cal module option is unset */
1207 return ((tda18271_cal_on_startup == -1) ?
1208 /* honor configuration setting */
1209 ((cfg) && (cfg->rf_cal_on_startup)) :
1210 /* module option overrides configuration setting */
1211 (tda18271_cal_on_startup)) ? 1 : 0;
1214 static int tda18271_set_config(struct dvb_frontend *fe, void *priv_cfg)
1216 struct tda18271_config *cfg = (struct tda18271_config *) priv_cfg;
1218 tda18271_setup_configuration(fe, cfg);
1220 if (tda18271_need_cal_on_startup(cfg))
1221 tda18271_init(fe);
1223 /* override default std map with values in config struct */
1224 if ((cfg) && (cfg->std_map))
1225 tda18271_update_std_map(fe, cfg->std_map);
1227 return 0;
1230 static struct dvb_tuner_ops tda18271_tuner_ops = {
1231 .info = {
1232 .name = "NXP TDA18271HD",
1233 .frequency_min = 45000000,
1234 .frequency_max = 864000000,
1235 .frequency_step = 62500
1237 .init = tda18271_init,
1238 .sleep = tda18271_sleep,
1239 .set_params = tda18271_set_params,
1240 .set_analog_params = tda18271_set_analog_params,
1241 .release = tda18271_release,
1242 .set_config = tda18271_set_config,
1243 .get_frequency = tda18271_get_frequency,
1244 .get_bandwidth = tda18271_get_bandwidth,
1247 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1248 struct i2c_adapter *i2c,
1249 struct tda18271_config *cfg)
1251 struct tda18271_priv *priv = NULL;
1252 int instance;
1254 mutex_lock(&tda18271_list_mutex);
1256 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1257 hybrid_tuner_instance_list,
1258 i2c, addr, "tda18271");
1259 switch (instance) {
1260 case 0:
1261 goto fail;
1262 case 1:
1263 /* new tuner instance */
1264 fe->tuner_priv = priv;
1266 tda18271_setup_configuration(fe, cfg);
1268 priv->cal_initialized = false;
1269 mutex_init(&priv->lock);
1271 if (tda_fail(tda18271_get_id(fe)))
1272 goto fail;
1274 if (tda_fail(tda18271_assign_map_layout(fe)))
1275 goto fail;
1277 mutex_lock(&priv->lock);
1278 tda18271_init_regs(fe);
1280 if ((tda18271_need_cal_on_startup(cfg)) &&
1281 (priv->id == TDA18271HDC2))
1282 tda18271c2_rf_cal_init(fe);
1284 mutex_unlock(&priv->lock);
1285 break;
1286 default:
1287 /* existing tuner instance */
1288 fe->tuner_priv = priv;
1290 /* allow dvb driver to override configuration settings */
1291 if (cfg) {
1292 if (cfg->gate != TDA18271_GATE_ANALOG)
1293 priv->gate = cfg->gate;
1294 if (cfg->role)
1295 priv->role = cfg->role;
1296 if (cfg->config)
1297 priv->config = cfg->config;
1298 if (cfg->small_i2c)
1299 priv->small_i2c = cfg->small_i2c;
1300 if (cfg->output_opt)
1301 priv->output_opt = cfg->output_opt;
1302 if (cfg->std_map)
1303 tda18271_update_std_map(fe, cfg->std_map);
1305 if (tda18271_need_cal_on_startup(cfg))
1306 tda18271_init(fe);
1307 break;
1310 /* override default std map with values in config struct */
1311 if ((cfg) && (cfg->std_map))
1312 tda18271_update_std_map(fe, cfg->std_map);
1314 mutex_unlock(&tda18271_list_mutex);
1316 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1317 sizeof(struct dvb_tuner_ops));
1319 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1320 tda18271_dump_std_map(fe);
1322 return fe;
1323 fail:
1324 mutex_unlock(&tda18271_list_mutex);
1326 tda18271_release(fe);
1327 return NULL;
1329 EXPORT_SYMBOL_GPL(tda18271_attach);
1330 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1331 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1332 MODULE_LICENSE("GPL");
1333 MODULE_VERSION("0.4");
1336 * Overrides for Emacs so that we follow Linus's tabbing style.
1337 * ---------------------------------------------------------------------------
1338 * Local variables:
1339 * c-basic-offset: 8
1340 * End: