x86/amd-iommu: Add function to complete a tlb flush
[linux/fpc-iii.git] / drivers / media / common / tuners / tda18271-fe.c
blob3a50ce96fcb9a6b0224254c2db369a3a7e53d28b
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 tm_current, rfcal_comp, approx, i, ret;
260 u8 dc_over_dt, rf_tab;
262 /* power up */
263 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
264 if (tda_fail(ret))
265 goto fail;
267 /* read die current temperature */
268 tm_current = tda18271_read_thermometer(fe);
270 /* frequency dependent parameters */
272 tda18271_calc_rf_cal(fe, &freq);
273 rf_tab = regs[R_EB14];
275 i = tda18271_lookup_rf_band(fe, &freq, NULL);
276 if (tda_fail(i))
277 return i;
279 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
280 approx = map[i].rf_a1 *
281 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
282 } else {
283 approx = map[i].rf_a2 *
284 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
287 if (approx < 0)
288 approx = 0;
289 if (approx > 255)
290 approx = 255;
292 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
294 /* calculate temperature compensation */
295 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal) / 1000;
297 regs[R_EB14] = approx + rfcal_comp;
298 ret = tda18271_write_regs(fe, R_EB14, 1);
299 fail:
300 return ret;
303 static int tda18271_por(struct dvb_frontend *fe)
305 struct tda18271_priv *priv = fe->tuner_priv;
306 unsigned char *regs = priv->tda18271_regs;
307 int ret;
309 /* power up detector 1 */
310 regs[R_EB12] &= ~0x20;
311 ret = tda18271_write_regs(fe, R_EB12, 1);
312 if (tda_fail(ret))
313 goto fail;
315 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
316 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
317 ret = tda18271_write_regs(fe, R_EB18, 1);
318 if (tda_fail(ret))
319 goto fail;
321 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
323 /* POR mode */
324 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
325 if (tda_fail(ret))
326 goto fail;
328 /* disable 1.5 MHz low pass filter */
329 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
330 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
331 ret = tda18271_write_regs(fe, R_EB21, 3);
332 fail:
333 return ret;
336 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
338 struct tda18271_priv *priv = fe->tuner_priv;
339 unsigned char *regs = priv->tda18271_regs;
340 u32 N;
342 /* set CAL mode to normal */
343 regs[R_EP4] &= ~0x03;
344 tda18271_write_regs(fe, R_EP4, 1);
346 /* switch off agc1 */
347 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
349 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
350 tda18271_write_regs(fe, R_EB18, 1);
352 /* frequency dependent parameters */
354 tda18271_calc_bp_filter(fe, &freq);
355 tda18271_calc_gain_taper(fe, &freq);
356 tda18271_calc_rf_band(fe, &freq);
357 tda18271_calc_km(fe, &freq);
359 tda18271_write_regs(fe, R_EP1, 3);
360 tda18271_write_regs(fe, R_EB13, 1);
362 /* main pll charge pump source */
363 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
365 /* cal pll charge pump source */
366 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
368 /* force dcdc converter to 0 V */
369 regs[R_EB14] = 0x00;
370 tda18271_write_regs(fe, R_EB14, 1);
372 /* disable plls lock */
373 regs[R_EB20] &= ~0x20;
374 tda18271_write_regs(fe, R_EB20, 1);
376 /* set CAL mode to RF tracking filter calibration */
377 regs[R_EP4] |= 0x03;
378 tda18271_write_regs(fe, R_EP4, 2);
380 /* --------------------------------------------------------------- */
382 /* set the internal calibration signal */
383 N = freq;
385 tda18271_calc_cal_pll(fe, N);
386 tda18271_write_regs(fe, R_CPD, 4);
388 /* downconvert internal calibration */
389 N += 1000000;
391 tda18271_calc_main_pll(fe, N);
392 tda18271_write_regs(fe, R_MPD, 4);
394 msleep(5);
396 tda18271_write_regs(fe, R_EP2, 1);
397 tda18271_write_regs(fe, R_EP1, 1);
398 tda18271_write_regs(fe, R_EP2, 1);
399 tda18271_write_regs(fe, R_EP1, 1);
401 /* --------------------------------------------------------------- */
403 /* normal operation for the main pll */
404 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
406 /* normal operation for the cal pll */
407 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
409 msleep(10); /* plls locking */
411 /* launch the rf tracking filters calibration */
412 regs[R_EB20] |= 0x20;
413 tda18271_write_regs(fe, R_EB20, 1);
415 msleep(60); /* calibration */
417 /* --------------------------------------------------------------- */
419 /* set CAL mode to normal */
420 regs[R_EP4] &= ~0x03;
422 /* switch on agc1 */
423 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
425 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
426 tda18271_write_regs(fe, R_EB18, 1);
428 tda18271_write_regs(fe, R_EP3, 2);
430 /* synchronization */
431 tda18271_write_regs(fe, R_EP1, 1);
433 /* get calibration result */
434 tda18271_read_extended(fe);
436 return regs[R_EB14];
439 static int tda18271_powerscan(struct dvb_frontend *fe,
440 u32 *freq_in, u32 *freq_out)
442 struct tda18271_priv *priv = fe->tuner_priv;
443 unsigned char *regs = priv->tda18271_regs;
444 int sgn, bcal, count, wait, ret;
445 u8 cid_target;
446 u16 count_limit;
447 u32 freq;
449 freq = *freq_in;
451 tda18271_calc_rf_band(fe, &freq);
452 tda18271_calc_rf_cal(fe, &freq);
453 tda18271_calc_gain_taper(fe, &freq);
454 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
456 tda18271_write_regs(fe, R_EP2, 1);
457 tda18271_write_regs(fe, R_EB14, 1);
459 /* downconvert frequency */
460 freq += 1000000;
462 tda18271_calc_main_pll(fe, freq);
463 tda18271_write_regs(fe, R_MPD, 4);
465 msleep(5); /* pll locking */
467 /* detection mode */
468 regs[R_EP4] &= ~0x03;
469 regs[R_EP4] |= 0x01;
470 tda18271_write_regs(fe, R_EP4, 1);
472 /* launch power detection measurement */
473 tda18271_write_regs(fe, R_EP2, 1);
475 /* read power detection info, stored in EB10 */
476 ret = tda18271_read_extended(fe);
477 if (tda_fail(ret))
478 return ret;
480 /* algorithm initialization */
481 sgn = 1;
482 *freq_out = *freq_in;
483 bcal = 0;
484 count = 0;
485 wait = false;
487 while ((regs[R_EB10] & 0x3f) < cid_target) {
488 /* downconvert updated freq to 1 MHz */
489 freq = *freq_in + (sgn * count) + 1000000;
491 tda18271_calc_main_pll(fe, freq);
492 tda18271_write_regs(fe, R_MPD, 4);
494 if (wait) {
495 msleep(5); /* pll locking */
496 wait = false;
497 } else
498 udelay(100); /* pll locking */
500 /* launch power detection measurement */
501 tda18271_write_regs(fe, R_EP2, 1);
503 /* read power detection info, stored in EB10 */
504 ret = tda18271_read_extended(fe);
505 if (tda_fail(ret))
506 return ret;
508 count += 200;
510 if (count <= count_limit)
511 continue;
513 if (sgn <= 0)
514 break;
516 sgn = -1 * sgn;
517 count = 200;
518 wait = true;
521 if ((regs[R_EB10] & 0x3f) >= cid_target) {
522 bcal = 1;
523 *freq_out = freq - 1000000;
524 } else
525 bcal = 0;
527 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
528 bcal, *freq_in, *freq_out, freq);
530 return bcal;
533 static int tda18271_powerscan_init(struct dvb_frontend *fe)
535 struct tda18271_priv *priv = fe->tuner_priv;
536 unsigned char *regs = priv->tda18271_regs;
537 int ret;
539 /* set standard to digital */
540 regs[R_EP3] &= ~0x1f; /* clear std bits */
541 regs[R_EP3] |= 0x12;
543 /* set cal mode to normal */
544 regs[R_EP4] &= ~0x03;
546 /* update IF output level */
547 regs[R_EP4] &= ~0x1c; /* clear if level bits */
549 ret = tda18271_write_regs(fe, R_EP3, 2);
550 if (tda_fail(ret))
551 goto fail;
553 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
554 ret = tda18271_write_regs(fe, R_EB18, 1);
555 if (tda_fail(ret))
556 goto fail;
558 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
560 /* 1.5 MHz low pass filter */
561 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
562 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
564 ret = tda18271_write_regs(fe, R_EB21, 3);
565 fail:
566 return ret;
569 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
571 struct tda18271_priv *priv = fe->tuner_priv;
572 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
573 unsigned char *regs = priv->tda18271_regs;
574 int bcal, rf, i;
575 #define RF1 0
576 #define RF2 1
577 #define RF3 2
578 u32 rf_default[3];
579 u32 rf_freq[3];
580 u8 prog_cal[3];
581 u8 prog_tab[3];
583 i = tda18271_lookup_rf_band(fe, &freq, NULL);
585 if (tda_fail(i))
586 return i;
588 rf_default[RF1] = 1000 * map[i].rf1_def;
589 rf_default[RF2] = 1000 * map[i].rf2_def;
590 rf_default[RF3] = 1000 * map[i].rf3_def;
592 for (rf = RF1; rf <= RF3; rf++) {
593 if (0 == rf_default[rf])
594 return 0;
595 tda_cal("freq = %d, rf = %d\n", freq, rf);
597 /* look for optimized calibration frequency */
598 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
599 if (tda_fail(bcal))
600 return bcal;
602 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
603 prog_tab[rf] = regs[R_EB14];
605 if (1 == bcal)
606 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
607 else
608 prog_cal[rf] = prog_tab[rf];
610 switch (rf) {
611 case RF1:
612 map[i].rf_a1 = 0;
613 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
614 map[i].rf1 = rf_freq[RF1] / 1000;
615 break;
616 case RF2:
617 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
618 prog_cal[RF1] + prog_tab[RF1]) /
619 (s32)((rf_freq[RF2] - rf_freq[RF1]) / 1000);
620 map[i].rf2 = rf_freq[RF2] / 1000;
621 break;
622 case RF3:
623 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
624 prog_cal[RF2] + prog_tab[RF2]) /
625 (s32)((rf_freq[RF3] - rf_freq[RF2]) / 1000);
626 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
627 map[i].rf3 = rf_freq[RF3] / 1000;
628 break;
629 default:
630 BUG();
634 return 0;
637 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
639 struct tda18271_priv *priv = fe->tuner_priv;
640 unsigned int i;
641 int ret;
643 tda_info("tda18271: performing RF tracking filter calibration\n");
645 /* wait for die temperature stabilization */
646 msleep(200);
648 ret = tda18271_powerscan_init(fe);
649 if (tda_fail(ret))
650 goto fail;
652 /* rf band calibration */
653 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
654 ret =
655 tda18271_rf_tracking_filters_init(fe, 1000 *
656 priv->rf_cal_state[i].rfmax);
657 if (tda_fail(ret))
658 goto fail;
661 priv->tm_rfcal = tda18271_read_thermometer(fe);
662 fail:
663 return ret;
666 /* ------------------------------------------------------------------ */
668 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
670 struct tda18271_priv *priv = fe->tuner_priv;
671 unsigned char *regs = priv->tda18271_regs;
672 int ret;
674 /* test RF_CAL_OK to see if we need init */
675 if ((regs[R_EP1] & 0x10) == 0)
676 priv->cal_initialized = false;
678 if (priv->cal_initialized)
679 return 0;
681 ret = tda18271_calc_rf_filter_curve(fe);
682 if (tda_fail(ret))
683 goto fail;
685 ret = tda18271_por(fe);
686 if (tda_fail(ret))
687 goto fail;
689 tda_info("tda18271: RF tracking filter calibration complete\n");
691 priv->cal_initialized = true;
692 goto end;
693 fail:
694 tda_info("tda18271: RF tracking filter calibration failed!\n");
695 end:
696 return ret;
699 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
700 u32 freq, u32 bw)
702 struct tda18271_priv *priv = fe->tuner_priv;
703 unsigned char *regs = priv->tda18271_regs;
704 int ret;
705 u32 N = 0;
707 /* calculate bp filter */
708 tda18271_calc_bp_filter(fe, &freq);
709 tda18271_write_regs(fe, R_EP1, 1);
711 regs[R_EB4] &= 0x07;
712 regs[R_EB4] |= 0x60;
713 tda18271_write_regs(fe, R_EB4, 1);
715 regs[R_EB7] = 0x60;
716 tda18271_write_regs(fe, R_EB7, 1);
718 regs[R_EB14] = 0x00;
719 tda18271_write_regs(fe, R_EB14, 1);
721 regs[R_EB20] = 0xcc;
722 tda18271_write_regs(fe, R_EB20, 1);
724 /* set cal mode to RF tracking filter calibration */
725 regs[R_EP4] |= 0x03;
727 /* calculate cal pll */
729 switch (priv->mode) {
730 case TDA18271_ANALOG:
731 N = freq - 1250000;
732 break;
733 case TDA18271_DIGITAL:
734 N = freq + bw / 2;
735 break;
738 tda18271_calc_cal_pll(fe, N);
740 /* calculate main pll */
742 switch (priv->mode) {
743 case TDA18271_ANALOG:
744 N = freq - 250000;
745 break;
746 case TDA18271_DIGITAL:
747 N = freq + bw / 2 + 1000000;
748 break;
751 tda18271_calc_main_pll(fe, N);
753 ret = tda18271_write_regs(fe, R_EP3, 11);
754 if (tda_fail(ret))
755 return ret;
757 msleep(5); /* RF tracking filter calibration initialization */
759 /* search for K,M,CO for RF calibration */
760 tda18271_calc_km(fe, &freq);
761 tda18271_write_regs(fe, R_EB13, 1);
763 /* search for rf band */
764 tda18271_calc_rf_band(fe, &freq);
766 /* search for gain taper */
767 tda18271_calc_gain_taper(fe, &freq);
769 tda18271_write_regs(fe, R_EP2, 1);
770 tda18271_write_regs(fe, R_EP1, 1);
771 tda18271_write_regs(fe, R_EP2, 1);
772 tda18271_write_regs(fe, R_EP1, 1);
774 regs[R_EB4] &= 0x07;
775 regs[R_EB4] |= 0x40;
776 tda18271_write_regs(fe, R_EB4, 1);
778 regs[R_EB7] = 0x40;
779 tda18271_write_regs(fe, R_EB7, 1);
780 msleep(10); /* pll locking */
782 regs[R_EB20] = 0xec;
783 tda18271_write_regs(fe, R_EB20, 1);
784 msleep(60); /* RF tracking filter calibration completion */
786 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
787 tda18271_write_regs(fe, R_EP4, 1);
789 tda18271_write_regs(fe, R_EP1, 1);
791 /* RF tracking filter correction for VHF_Low band */
792 if (0 == tda18271_calc_rf_cal(fe, &freq))
793 tda18271_write_regs(fe, R_EB14, 1);
795 return 0;
798 /* ------------------------------------------------------------------ */
800 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
802 struct tda18271_priv *priv = fe->tuner_priv;
803 unsigned char *regs = priv->tda18271_regs;
804 int ret;
806 ret = tda18271_read_regs(fe);
807 if (tda_fail(ret))
808 goto fail;
810 /* test IR_CAL_OK to see if we need init */
811 if ((regs[R_EP1] & 0x08) == 0)
812 ret = tda18271_init_regs(fe);
813 fail:
814 return ret;
817 static int tda18271_init(struct dvb_frontend *fe)
819 struct tda18271_priv *priv = fe->tuner_priv;
820 int ret;
822 mutex_lock(&priv->lock);
824 /* full power up */
825 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
826 if (tda_fail(ret))
827 goto fail;
829 /* initialization */
830 ret = tda18271_ir_cal_init(fe);
831 if (tda_fail(ret))
832 goto fail;
834 if (priv->id == TDA18271HDC2)
835 tda18271c2_rf_cal_init(fe);
836 fail:
837 mutex_unlock(&priv->lock);
839 return ret;
842 static int tda18271_sleep(struct dvb_frontend *fe)
844 struct tda18271_priv *priv = fe->tuner_priv;
845 int ret;
847 mutex_lock(&priv->lock);
849 /* enter standby mode, with required output features enabled */
850 ret = tda18271_toggle_output(fe, 1);
852 mutex_unlock(&priv->lock);
854 return ret;
857 /* ------------------------------------------------------------------ */
859 static int tda18271_agc(struct dvb_frontend *fe)
861 struct tda18271_priv *priv = fe->tuner_priv;
862 int ret = 0;
864 switch (priv->config) {
865 case 0:
866 /* no external agc configuration required */
867 if (tda18271_debug & DBG_ADV)
868 tda_dbg("no agc configuration provided\n");
869 break;
870 case 3:
871 /* switch with GPIO of saa713x */
872 tda_dbg("invoking callback\n");
873 if (fe->callback)
874 ret = fe->callback(priv->i2c_props.adap->algo_data,
875 DVB_FRONTEND_COMPONENT_TUNER,
876 TDA18271_CALLBACK_CMD_AGC_ENABLE,
877 priv->mode);
878 break;
879 case 1:
880 case 2:
881 default:
882 /* n/a - currently not supported */
883 tda_err("unsupported configuration: %d\n", priv->config);
884 ret = -EINVAL;
885 break;
887 return ret;
890 static int tda18271_tune(struct dvb_frontend *fe,
891 struct tda18271_std_map_item *map, u32 freq, u32 bw)
893 struct tda18271_priv *priv = fe->tuner_priv;
894 int ret;
896 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
897 freq, map->if_freq, bw, map->agc_mode, map->std);
899 ret = tda18271_agc(fe);
900 if (tda_fail(ret))
901 tda_warn("failed to configure agc\n");
903 ret = tda18271_init(fe);
904 if (tda_fail(ret))
905 goto fail;
907 mutex_lock(&priv->lock);
909 switch (priv->id) {
910 case TDA18271HDC1:
911 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
912 break;
913 case TDA18271HDC2:
914 tda18271c2_rf_tracking_filters_correction(fe, freq);
915 break;
917 ret = tda18271_channel_configuration(fe, map, freq, bw);
919 mutex_unlock(&priv->lock);
920 fail:
921 return ret;
924 /* ------------------------------------------------------------------ */
926 static int tda18271_set_params(struct dvb_frontend *fe,
927 struct dvb_frontend_parameters *params)
929 struct tda18271_priv *priv = fe->tuner_priv;
930 struct tda18271_std_map *std_map = &priv->std;
931 struct tda18271_std_map_item *map;
932 int ret;
933 u32 bw, freq = params->frequency;
935 priv->mode = TDA18271_DIGITAL;
937 if (fe->ops.info.type == FE_ATSC) {
938 switch (params->u.vsb.modulation) {
939 case VSB_8:
940 case VSB_16:
941 map = &std_map->atsc_6;
942 break;
943 case QAM_64:
944 case QAM_256:
945 map = &std_map->qam_6;
946 break;
947 default:
948 tda_warn("modulation not set!\n");
949 return -EINVAL;
951 #if 0
952 /* userspace request is already center adjusted */
953 freq += 1750000; /* Adjust to center (+1.75MHZ) */
954 #endif
955 bw = 6000000;
956 } else if (fe->ops.info.type == FE_OFDM) {
957 switch (params->u.ofdm.bandwidth) {
958 case BANDWIDTH_6_MHZ:
959 bw = 6000000;
960 map = &std_map->dvbt_6;
961 break;
962 case BANDWIDTH_7_MHZ:
963 bw = 7000000;
964 map = &std_map->dvbt_7;
965 break;
966 case BANDWIDTH_8_MHZ:
967 bw = 8000000;
968 map = &std_map->dvbt_8;
969 break;
970 default:
971 tda_warn("bandwidth not set!\n");
972 return -EINVAL;
974 } else {
975 tda_warn("modulation type not supported!\n");
976 return -EINVAL;
979 /* When tuning digital, the analog demod must be tri-stated */
980 if (fe->ops.analog_ops.standby)
981 fe->ops.analog_ops.standby(fe);
983 ret = tda18271_tune(fe, map, freq, bw);
985 if (tda_fail(ret))
986 goto fail;
988 priv->frequency = freq;
989 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
990 params->u.ofdm.bandwidth : 0;
991 fail:
992 return ret;
995 static int tda18271_set_analog_params(struct dvb_frontend *fe,
996 struct analog_parameters *params)
998 struct tda18271_priv *priv = fe->tuner_priv;
999 struct tda18271_std_map *std_map = &priv->std;
1000 struct tda18271_std_map_item *map;
1001 char *mode;
1002 int ret;
1003 u32 freq = params->frequency * 125 *
1004 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
1006 priv->mode = TDA18271_ANALOG;
1008 if (params->mode == V4L2_TUNER_RADIO) {
1009 map = &std_map->fm_radio;
1010 mode = "fm";
1011 } else if (params->std & V4L2_STD_MN) {
1012 map = &std_map->atv_mn;
1013 mode = "MN";
1014 } else if (params->std & V4L2_STD_B) {
1015 map = &std_map->atv_b;
1016 mode = "B";
1017 } else if (params->std & V4L2_STD_GH) {
1018 map = &std_map->atv_gh;
1019 mode = "GH";
1020 } else if (params->std & V4L2_STD_PAL_I) {
1021 map = &std_map->atv_i;
1022 mode = "I";
1023 } else if (params->std & V4L2_STD_DK) {
1024 map = &std_map->atv_dk;
1025 mode = "DK";
1026 } else if (params->std & V4L2_STD_SECAM_L) {
1027 map = &std_map->atv_l;
1028 mode = "L";
1029 } else if (params->std & V4L2_STD_SECAM_LC) {
1030 map = &std_map->atv_lc;
1031 mode = "L'";
1032 } else {
1033 map = &std_map->atv_i;
1034 mode = "xx";
1037 tda_dbg("setting tda18271 to system %s\n", mode);
1039 ret = tda18271_tune(fe, map, freq, 0);
1041 if (tda_fail(ret))
1042 goto fail;
1044 priv->frequency = freq;
1045 priv->bandwidth = 0;
1046 fail:
1047 return ret;
1050 static int tda18271_release(struct dvb_frontend *fe)
1052 struct tda18271_priv *priv = fe->tuner_priv;
1054 mutex_lock(&tda18271_list_mutex);
1056 if (priv)
1057 hybrid_tuner_release_state(priv);
1059 mutex_unlock(&tda18271_list_mutex);
1061 fe->tuner_priv = NULL;
1063 return 0;
1066 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1068 struct tda18271_priv *priv = fe->tuner_priv;
1069 *frequency = priv->frequency;
1070 return 0;
1073 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1075 struct tda18271_priv *priv = fe->tuner_priv;
1076 *bandwidth = priv->bandwidth;
1077 return 0;
1080 /* ------------------------------------------------------------------ */
1082 #define tda18271_update_std(std_cfg, name) do { \
1083 if (map->std_cfg.if_freq + \
1084 map->std_cfg.agc_mode + map->std_cfg.std + \
1085 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1086 tda_dbg("Using custom std config for %s\n", name); \
1087 memcpy(&std->std_cfg, &map->std_cfg, \
1088 sizeof(struct tda18271_std_map_item)); \
1089 } } while (0)
1091 #define tda18271_dump_std_item(std_cfg, name) do { \
1092 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1093 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1094 name, std->std_cfg.if_freq, \
1095 std->std_cfg.agc_mode, std->std_cfg.std, \
1096 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1097 } while (0)
1099 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1101 struct tda18271_priv *priv = fe->tuner_priv;
1102 struct tda18271_std_map *std = &priv->std;
1104 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1105 tda18271_dump_std_item(fm_radio, " fm ");
1106 tda18271_dump_std_item(atv_b, "atv b ");
1107 tda18271_dump_std_item(atv_dk, "atv dk");
1108 tda18271_dump_std_item(atv_gh, "atv gh");
1109 tda18271_dump_std_item(atv_i, "atv i ");
1110 tda18271_dump_std_item(atv_l, "atv l ");
1111 tda18271_dump_std_item(atv_lc, "atv l'");
1112 tda18271_dump_std_item(atv_mn, "atv mn");
1113 tda18271_dump_std_item(atsc_6, "atsc 6");
1114 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1115 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1116 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1117 tda18271_dump_std_item(qam_6, "qam 6 ");
1118 tda18271_dump_std_item(qam_8, "qam 8 ");
1120 return 0;
1123 static int tda18271_update_std_map(struct dvb_frontend *fe,
1124 struct tda18271_std_map *map)
1126 struct tda18271_priv *priv = fe->tuner_priv;
1127 struct tda18271_std_map *std = &priv->std;
1129 if (!map)
1130 return -EINVAL;
1132 tda18271_update_std(fm_radio, "fm");
1133 tda18271_update_std(atv_b, "atv b");
1134 tda18271_update_std(atv_dk, "atv dk");
1135 tda18271_update_std(atv_gh, "atv gh");
1136 tda18271_update_std(atv_i, "atv i");
1137 tda18271_update_std(atv_l, "atv l");
1138 tda18271_update_std(atv_lc, "atv l'");
1139 tda18271_update_std(atv_mn, "atv mn");
1140 tda18271_update_std(atsc_6, "atsc 6");
1141 tda18271_update_std(dvbt_6, "dvbt 6");
1142 tda18271_update_std(dvbt_7, "dvbt 7");
1143 tda18271_update_std(dvbt_8, "dvbt 8");
1144 tda18271_update_std(qam_6, "qam 6");
1145 tda18271_update_std(qam_8, "qam 8");
1147 return 0;
1150 static int tda18271_get_id(struct dvb_frontend *fe)
1152 struct tda18271_priv *priv = fe->tuner_priv;
1153 unsigned char *regs = priv->tda18271_regs;
1154 char *name;
1155 int ret = 0;
1157 mutex_lock(&priv->lock);
1158 tda18271_read_regs(fe);
1159 mutex_unlock(&priv->lock);
1161 switch (regs[R_ID] & 0x7f) {
1162 case 3:
1163 name = "TDA18271HD/C1";
1164 priv->id = TDA18271HDC1;
1165 break;
1166 case 4:
1167 name = "TDA18271HD/C2";
1168 priv->id = TDA18271HDC2;
1169 break;
1170 default:
1171 name = "Unknown device";
1172 ret = -EINVAL;
1173 break;
1176 tda_info("%s detected @ %d-%04x%s\n", name,
1177 i2c_adapter_id(priv->i2c_props.adap),
1178 priv->i2c_props.addr,
1179 (0 == ret) ? "" : ", device not supported.");
1181 return ret;
1184 static struct dvb_tuner_ops tda18271_tuner_ops = {
1185 .info = {
1186 .name = "NXP TDA18271HD",
1187 .frequency_min = 45000000,
1188 .frequency_max = 864000000,
1189 .frequency_step = 62500
1191 .init = tda18271_init,
1192 .sleep = tda18271_sleep,
1193 .set_params = tda18271_set_params,
1194 .set_analog_params = tda18271_set_analog_params,
1195 .release = tda18271_release,
1196 .get_frequency = tda18271_get_frequency,
1197 .get_bandwidth = tda18271_get_bandwidth,
1200 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1201 struct i2c_adapter *i2c,
1202 struct tda18271_config *cfg)
1204 struct tda18271_priv *priv = NULL;
1205 int instance;
1207 mutex_lock(&tda18271_list_mutex);
1209 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1210 hybrid_tuner_instance_list,
1211 i2c, addr, "tda18271");
1212 switch (instance) {
1213 case 0:
1214 goto fail;
1215 case 1:
1217 /* new tuner instance */
1218 int rf_cal_on_startup;
1220 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1221 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1222 priv->config = (cfg) ? cfg->config : 0;
1223 priv->small_i2c = (cfg) ? cfg->small_i2c : 0;
1224 priv->output_opt = (cfg) ?
1225 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
1227 /* tda18271_cal_on_startup == -1 when cal
1228 * module option is unset */
1229 if (tda18271_cal_on_startup == -1) {
1230 /* honor attach-time configuration */
1231 rf_cal_on_startup =
1232 ((cfg) && (cfg->rf_cal_on_startup)) ? 1 : 0;
1233 } else {
1234 /* module option overrides attach configuration */
1235 rf_cal_on_startup = tda18271_cal_on_startup;
1238 priv->cal_initialized = false;
1239 mutex_init(&priv->lock);
1241 fe->tuner_priv = priv;
1243 if (tda_fail(tda18271_get_id(fe)))
1244 goto fail;
1246 if (tda_fail(tda18271_assign_map_layout(fe)))
1247 goto fail;
1249 mutex_lock(&priv->lock);
1250 tda18271_init_regs(fe);
1252 if ((rf_cal_on_startup) && (priv->id == TDA18271HDC2))
1253 tda18271c2_rf_cal_init(fe);
1255 mutex_unlock(&priv->lock);
1256 break;
1258 default:
1259 /* existing tuner instance */
1260 fe->tuner_priv = priv;
1262 /* allow dvb driver to override configuration settings */
1263 if (cfg) {
1264 if (cfg->gate != TDA18271_GATE_ANALOG)
1265 priv->gate = cfg->gate;
1266 if (cfg->role)
1267 priv->role = cfg->role;
1268 if (cfg->config)
1269 priv->config = cfg->config;
1270 if (cfg->small_i2c)
1271 priv->small_i2c = cfg->small_i2c;
1272 if (cfg->output_opt)
1273 priv->output_opt = cfg->output_opt;
1275 break;
1278 /* override default std map with values in config struct */
1279 if ((cfg) && (cfg->std_map))
1280 tda18271_update_std_map(fe, cfg->std_map);
1282 mutex_unlock(&tda18271_list_mutex);
1284 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1285 sizeof(struct dvb_tuner_ops));
1287 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1288 tda18271_dump_std_map(fe);
1290 return fe;
1291 fail:
1292 mutex_unlock(&tda18271_list_mutex);
1294 tda18271_release(fe);
1295 return NULL;
1297 EXPORT_SYMBOL_GPL(tda18271_attach);
1298 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1299 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1300 MODULE_LICENSE("GPL");
1301 MODULE_VERSION("0.3");
1304 * Overrides for Emacs so that we follow Linus's tabbing style.
1305 * ---------------------------------------------------------------------------
1306 * Local variables:
1307 * c-basic-offset: 8
1308 * End: