Driver Core: devtmpfs - kernel-maintained tmpfs-based /dev
[linux/fpc-iii.git] / drivers / media / common / tuners / tda18271-fe.c
blobb10935630154f94b4845c15f4abf314cdf9eb4ee
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;
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 inline int charge_pump_source(struct dvb_frontend *fe, int force)
41 struct tda18271_priv *priv = fe->tuner_priv;
42 return tda18271_charge_pump_source(fe,
43 (priv->role == TDA18271_SLAVE) ?
44 TDA18271_CAL_PLL :
45 TDA18271_MAIN_PLL, force);
48 static inline void tda18271_set_if_notch(struct dvb_frontend *fe)
50 struct tda18271_priv *priv = fe->tuner_priv;
51 unsigned char *regs = priv->tda18271_regs;
53 switch (priv->mode) {
54 case TDA18271_ANALOG:
55 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
56 break;
57 case TDA18271_DIGITAL:
58 regs[R_MPD] |= 0x80; /* IF notch = 1 */
59 break;
63 static int tda18271_channel_configuration(struct dvb_frontend *fe,
64 struct tda18271_std_map_item *map,
65 u32 freq, u32 bw)
67 struct tda18271_priv *priv = fe->tuner_priv;
68 unsigned char *regs = priv->tda18271_regs;
69 int ret;
70 u32 N;
72 /* update TV broadcast parameters */
74 /* set standard */
75 regs[R_EP3] &= ~0x1f; /* clear std bits */
76 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
78 if (priv->id == TDA18271HDC2) {
79 /* set rfagc to high speed mode */
80 regs[R_EP3] &= ~0x04;
83 /* set cal mode to normal */
84 regs[R_EP4] &= ~0x03;
86 /* update IF output level */
87 regs[R_EP4] &= ~0x1c; /* clear if level bits */
88 regs[R_EP4] |= (map->if_lvl << 2);
90 /* update FM_RFn */
91 regs[R_EP4] &= ~0x80;
92 regs[R_EP4] |= map->fm_rfn << 7;
94 /* update rf top / if top */
95 regs[R_EB22] = 0x00;
96 regs[R_EB22] |= map->rfagc_top;
97 ret = tda18271_write_regs(fe, R_EB22, 1);
98 if (tda_fail(ret))
99 goto fail;
101 /* --------------------------------------------------------------- */
103 /* disable Power Level Indicator */
104 regs[R_EP1] |= 0x40;
106 /* make sure thermometer is off */
107 regs[R_TM] &= ~0x10;
109 /* frequency dependent parameters */
111 tda18271_calc_ir_measure(fe, &freq);
113 tda18271_calc_bp_filter(fe, &freq);
115 tda18271_calc_rf_band(fe, &freq);
117 tda18271_calc_gain_taper(fe, &freq);
119 /* --------------------------------------------------------------- */
121 /* dual tuner and agc1 extra configuration */
123 switch (priv->role) {
124 case TDA18271_MASTER:
125 regs[R_EB1] |= 0x04; /* main vco */
126 break;
127 case TDA18271_SLAVE:
128 regs[R_EB1] &= ~0x04; /* cal vco */
129 break;
132 /* agc1 always active */
133 regs[R_EB1] &= ~0x02;
135 /* agc1 has priority on agc2 */
136 regs[R_EB1] &= ~0x01;
138 ret = tda18271_write_regs(fe, R_EB1, 1);
139 if (tda_fail(ret))
140 goto fail;
142 /* --------------------------------------------------------------- */
144 N = map->if_freq * 1000 + freq;
146 switch (priv->role) {
147 case TDA18271_MASTER:
148 tda18271_calc_main_pll(fe, N);
149 tda18271_set_if_notch(fe);
150 tda18271_write_regs(fe, R_MPD, 4);
151 break;
152 case TDA18271_SLAVE:
153 tda18271_calc_cal_pll(fe, N);
154 tda18271_write_regs(fe, R_CPD, 4);
156 regs[R_MPD] = regs[R_CPD] & 0x7f;
157 tda18271_set_if_notch(fe);
158 tda18271_write_regs(fe, R_MPD, 1);
159 break;
162 ret = tda18271_write_regs(fe, R_TM, 7);
163 if (tda_fail(ret))
164 goto fail;
166 /* force charge pump source */
167 charge_pump_source(fe, 1);
169 msleep(1);
171 /* return pll to normal operation */
172 charge_pump_source(fe, 0);
174 msleep(20);
176 if (priv->id == TDA18271HDC2) {
177 /* set rfagc to normal speed mode */
178 if (map->fm_rfn)
179 regs[R_EP3] &= ~0x04;
180 else
181 regs[R_EP3] |= 0x04;
182 ret = tda18271_write_regs(fe, R_EP3, 1);
184 fail:
185 return ret;
188 static int tda18271_read_thermometer(struct dvb_frontend *fe)
190 struct tda18271_priv *priv = fe->tuner_priv;
191 unsigned char *regs = priv->tda18271_regs;
192 int tm;
194 /* switch thermometer on */
195 regs[R_TM] |= 0x10;
196 tda18271_write_regs(fe, R_TM, 1);
198 /* read thermometer info */
199 tda18271_read_regs(fe);
201 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
202 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
204 if ((regs[R_TM] & 0x20) == 0x20)
205 regs[R_TM] &= ~0x20;
206 else
207 regs[R_TM] |= 0x20;
209 tda18271_write_regs(fe, R_TM, 1);
211 msleep(10); /* temperature sensing */
213 /* read thermometer info */
214 tda18271_read_regs(fe);
217 tm = tda18271_lookup_thermometer(fe);
219 /* switch thermometer off */
220 regs[R_TM] &= ~0x10;
221 tda18271_write_regs(fe, R_TM, 1);
223 /* set CAL mode to normal */
224 regs[R_EP4] &= ~0x03;
225 tda18271_write_regs(fe, R_EP4, 1);
227 return tm;
230 /* ------------------------------------------------------------------ */
232 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
233 u32 freq)
235 struct tda18271_priv *priv = fe->tuner_priv;
236 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
237 unsigned char *regs = priv->tda18271_regs;
238 int tm_current, rfcal_comp, approx, i, ret;
239 u8 dc_over_dt, rf_tab;
241 /* power up */
242 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
243 if (tda_fail(ret))
244 goto fail;
246 /* read die current temperature */
247 tm_current = tda18271_read_thermometer(fe);
249 /* frequency dependent parameters */
251 tda18271_calc_rf_cal(fe, &freq);
252 rf_tab = regs[R_EB14];
254 i = tda18271_lookup_rf_band(fe, &freq, NULL);
255 if (tda_fail(i))
256 return i;
258 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
259 approx = map[i].rf_a1 *
260 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
261 } else {
262 approx = map[i].rf_a2 *
263 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
266 if (approx < 0)
267 approx = 0;
268 if (approx > 255)
269 approx = 255;
271 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
273 /* calculate temperature compensation */
274 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
276 regs[R_EB14] = approx + rfcal_comp;
277 ret = tda18271_write_regs(fe, R_EB14, 1);
278 fail:
279 return ret;
282 static int tda18271_por(struct dvb_frontend *fe)
284 struct tda18271_priv *priv = fe->tuner_priv;
285 unsigned char *regs = priv->tda18271_regs;
286 int ret;
288 /* power up detector 1 */
289 regs[R_EB12] &= ~0x20;
290 ret = tda18271_write_regs(fe, R_EB12, 1);
291 if (tda_fail(ret))
292 goto fail;
294 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
295 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
296 ret = tda18271_write_regs(fe, R_EB18, 1);
297 if (tda_fail(ret))
298 goto fail;
300 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
302 /* POR mode */
303 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
304 if (tda_fail(ret))
305 goto fail;
307 /* disable 1.5 MHz low pass filter */
308 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
309 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
310 ret = tda18271_write_regs(fe, R_EB21, 3);
311 fail:
312 return ret;
315 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
317 struct tda18271_priv *priv = fe->tuner_priv;
318 unsigned char *regs = priv->tda18271_regs;
319 u32 N;
321 /* set CAL mode to normal */
322 regs[R_EP4] &= ~0x03;
323 tda18271_write_regs(fe, R_EP4, 1);
325 /* switch off agc1 */
326 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
328 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
329 tda18271_write_regs(fe, R_EB18, 1);
331 /* frequency dependent parameters */
333 tda18271_calc_bp_filter(fe, &freq);
334 tda18271_calc_gain_taper(fe, &freq);
335 tda18271_calc_rf_band(fe, &freq);
336 tda18271_calc_km(fe, &freq);
338 tda18271_write_regs(fe, R_EP1, 3);
339 tda18271_write_regs(fe, R_EB13, 1);
341 /* main pll charge pump source */
342 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
344 /* cal pll charge pump source */
345 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
347 /* force dcdc converter to 0 V */
348 regs[R_EB14] = 0x00;
349 tda18271_write_regs(fe, R_EB14, 1);
351 /* disable plls lock */
352 regs[R_EB20] &= ~0x20;
353 tda18271_write_regs(fe, R_EB20, 1);
355 /* set CAL mode to RF tracking filter calibration */
356 regs[R_EP4] |= 0x03;
357 tda18271_write_regs(fe, R_EP4, 2);
359 /* --------------------------------------------------------------- */
361 /* set the internal calibration signal */
362 N = freq;
364 tda18271_calc_cal_pll(fe, N);
365 tda18271_write_regs(fe, R_CPD, 4);
367 /* downconvert internal calibration */
368 N += 1000000;
370 tda18271_calc_main_pll(fe, N);
371 tda18271_write_regs(fe, R_MPD, 4);
373 msleep(5);
375 tda18271_write_regs(fe, R_EP2, 1);
376 tda18271_write_regs(fe, R_EP1, 1);
377 tda18271_write_regs(fe, R_EP2, 1);
378 tda18271_write_regs(fe, R_EP1, 1);
380 /* --------------------------------------------------------------- */
382 /* normal operation for the main pll */
383 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
385 /* normal operation for the cal pll */
386 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
388 msleep(10); /* plls locking */
390 /* launch the rf tracking filters calibration */
391 regs[R_EB20] |= 0x20;
392 tda18271_write_regs(fe, R_EB20, 1);
394 msleep(60); /* calibration */
396 /* --------------------------------------------------------------- */
398 /* set CAL mode to normal */
399 regs[R_EP4] &= ~0x03;
401 /* switch on agc1 */
402 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
404 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
405 tda18271_write_regs(fe, R_EB18, 1);
407 tda18271_write_regs(fe, R_EP3, 2);
409 /* synchronization */
410 tda18271_write_regs(fe, R_EP1, 1);
412 /* get calibration result */
413 tda18271_read_extended(fe);
415 return regs[R_EB14];
418 static int tda18271_powerscan(struct dvb_frontend *fe,
419 u32 *freq_in, u32 *freq_out)
421 struct tda18271_priv *priv = fe->tuner_priv;
422 unsigned char *regs = priv->tda18271_regs;
423 int sgn, bcal, count, wait, ret;
424 u8 cid_target;
425 u16 count_limit;
426 u32 freq;
428 freq = *freq_in;
430 tda18271_calc_rf_band(fe, &freq);
431 tda18271_calc_rf_cal(fe, &freq);
432 tda18271_calc_gain_taper(fe, &freq);
433 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
435 tda18271_write_regs(fe, R_EP2, 1);
436 tda18271_write_regs(fe, R_EB14, 1);
438 /* downconvert frequency */
439 freq += 1000000;
441 tda18271_calc_main_pll(fe, freq);
442 tda18271_write_regs(fe, R_MPD, 4);
444 msleep(5); /* pll locking */
446 /* detection mode */
447 regs[R_EP4] &= ~0x03;
448 regs[R_EP4] |= 0x01;
449 tda18271_write_regs(fe, R_EP4, 1);
451 /* launch power detection measurement */
452 tda18271_write_regs(fe, R_EP2, 1);
454 /* read power detection info, stored in EB10 */
455 ret = tda18271_read_extended(fe);
456 if (tda_fail(ret))
457 return ret;
459 /* algorithm initialization */
460 sgn = 1;
461 *freq_out = *freq_in;
462 bcal = 0;
463 count = 0;
464 wait = false;
466 while ((regs[R_EB10] & 0x3f) < cid_target) {
467 /* downconvert updated freq to 1 MHz */
468 freq = *freq_in + (sgn * count) + 1000000;
470 tda18271_calc_main_pll(fe, freq);
471 tda18271_write_regs(fe, R_MPD, 4);
473 if (wait) {
474 msleep(5); /* pll locking */
475 wait = false;
476 } else
477 udelay(100); /* pll locking */
479 /* launch power detection measurement */
480 tda18271_write_regs(fe, R_EP2, 1);
482 /* read power detection info, stored in EB10 */
483 ret = tda18271_read_extended(fe);
484 if (tda_fail(ret))
485 return ret;
487 count += 200;
489 if (count <= count_limit)
490 continue;
492 if (sgn <= 0)
493 break;
495 sgn = -1 * sgn;
496 count = 200;
497 wait = true;
500 if ((regs[R_EB10] & 0x3f) >= cid_target) {
501 bcal = 1;
502 *freq_out = freq - 1000000;
503 } else
504 bcal = 0;
506 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
507 bcal, *freq_in, *freq_out, freq);
509 return bcal;
512 static int tda18271_powerscan_init(struct dvb_frontend *fe)
514 struct tda18271_priv *priv = fe->tuner_priv;
515 unsigned char *regs = priv->tda18271_regs;
516 int ret;
518 /* set standard to digital */
519 regs[R_EP3] &= ~0x1f; /* clear std bits */
520 regs[R_EP3] |= 0x12;
522 /* set cal mode to normal */
523 regs[R_EP4] &= ~0x03;
525 /* update IF output level */
526 regs[R_EP4] &= ~0x1c; /* clear if level bits */
528 ret = tda18271_write_regs(fe, R_EP3, 2);
529 if (tda_fail(ret))
530 goto fail;
532 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
533 ret = tda18271_write_regs(fe, R_EB18, 1);
534 if (tda_fail(ret))
535 goto fail;
537 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
539 /* 1.5 MHz low pass filter */
540 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
541 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
543 ret = tda18271_write_regs(fe, R_EB21, 3);
544 fail:
545 return ret;
548 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
550 struct tda18271_priv *priv = fe->tuner_priv;
551 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
552 unsigned char *regs = priv->tda18271_regs;
553 int bcal, rf, i;
554 #define RF1 0
555 #define RF2 1
556 #define RF3 2
557 u32 rf_default[3];
558 u32 rf_freq[3];
559 u8 prog_cal[3];
560 u8 prog_tab[3];
562 i = tda18271_lookup_rf_band(fe, &freq, NULL);
564 if (tda_fail(i))
565 return i;
567 rf_default[RF1] = 1000 * map[i].rf1_def;
568 rf_default[RF2] = 1000 * map[i].rf2_def;
569 rf_default[RF3] = 1000 * map[i].rf3_def;
571 for (rf = RF1; rf <= RF3; rf++) {
572 if (0 == rf_default[rf])
573 return 0;
574 tda_cal("freq = %d, rf = %d\n", freq, rf);
576 /* look for optimized calibration frequency */
577 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
578 if (tda_fail(bcal))
579 return bcal;
581 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
582 prog_tab[rf] = regs[R_EB14];
584 if (1 == bcal)
585 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
586 else
587 prog_cal[rf] = prog_tab[rf];
589 switch (rf) {
590 case RF1:
591 map[i].rf_a1 = 0;
592 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
593 map[i].rf1 = rf_freq[RF1] / 1000;
594 break;
595 case RF2:
596 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
597 prog_cal[RF1] + prog_tab[RF1]) /
598 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
599 map[i].rf2 = rf_freq[RF2] / 1000;
600 break;
601 case RF3:
602 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
603 prog_cal[RF2] + prog_tab[RF2]) /
604 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
605 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
606 map[i].rf3 = rf_freq[RF3] / 1000;
607 break;
608 default:
609 BUG();
613 return 0;
616 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
618 struct tda18271_priv *priv = fe->tuner_priv;
619 unsigned int i;
620 int ret;
622 tda_info("tda18271: performing RF tracking filter calibration\n");
624 /* wait for die temperature stabilization */
625 msleep(200);
627 ret = tda18271_powerscan_init(fe);
628 if (tda_fail(ret))
629 goto fail;
631 /* rf band calibration */
632 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
633 ret =
634 tda18271_rf_tracking_filters_init(fe, 1000 *
635 priv->rf_cal_state[i].rfmax);
636 if (tda_fail(ret))
637 goto fail;
640 priv->tm_rfcal = tda18271_read_thermometer(fe);
641 fail:
642 return ret;
645 /* ------------------------------------------------------------------ */
647 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
649 struct tda18271_priv *priv = fe->tuner_priv;
650 unsigned char *regs = priv->tda18271_regs;
651 int ret;
653 /* test RF_CAL_OK to see if we need init */
654 if ((regs[R_EP1] & 0x10) == 0)
655 priv->cal_initialized = false;
657 if (priv->cal_initialized)
658 return 0;
660 ret = tda18271_calc_rf_filter_curve(fe);
661 if (tda_fail(ret))
662 goto fail;
664 ret = tda18271_por(fe);
665 if (tda_fail(ret))
666 goto fail;
668 tda_info("tda18271: RF tracking filter calibration complete\n");
670 priv->cal_initialized = true;
671 goto end;
672 fail:
673 tda_info("tda18271: RF tracking filter calibration failed!\n");
674 end:
675 return ret;
678 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
679 u32 freq, u32 bw)
681 struct tda18271_priv *priv = fe->tuner_priv;
682 unsigned char *regs = priv->tda18271_regs;
683 int ret;
684 u32 N = 0;
686 /* calculate bp filter */
687 tda18271_calc_bp_filter(fe, &freq);
688 tda18271_write_regs(fe, R_EP1, 1);
690 regs[R_EB4] &= 0x07;
691 regs[R_EB4] |= 0x60;
692 tda18271_write_regs(fe, R_EB4, 1);
694 regs[R_EB7] = 0x60;
695 tda18271_write_regs(fe, R_EB7, 1);
697 regs[R_EB14] = 0x00;
698 tda18271_write_regs(fe, R_EB14, 1);
700 regs[R_EB20] = 0xcc;
701 tda18271_write_regs(fe, R_EB20, 1);
703 /* set cal mode to RF tracking filter calibration */
704 regs[R_EP4] |= 0x03;
706 /* calculate cal pll */
708 switch (priv->mode) {
709 case TDA18271_ANALOG:
710 N = freq - 1250000;
711 break;
712 case TDA18271_DIGITAL:
713 N = freq + bw / 2;
714 break;
717 tda18271_calc_cal_pll(fe, N);
719 /* calculate main pll */
721 switch (priv->mode) {
722 case TDA18271_ANALOG:
723 N = freq - 250000;
724 break;
725 case TDA18271_DIGITAL:
726 N = freq + bw / 2 + 1000000;
727 break;
730 tda18271_calc_main_pll(fe, N);
732 ret = tda18271_write_regs(fe, R_EP3, 11);
733 if (tda_fail(ret))
734 return ret;
736 msleep(5); /* RF tracking filter calibration initialization */
738 /* search for K,M,CO for RF calibration */
739 tda18271_calc_km(fe, &freq);
740 tda18271_write_regs(fe, R_EB13, 1);
742 /* search for rf band */
743 tda18271_calc_rf_band(fe, &freq);
745 /* search for gain taper */
746 tda18271_calc_gain_taper(fe, &freq);
748 tda18271_write_regs(fe, R_EP2, 1);
749 tda18271_write_regs(fe, R_EP1, 1);
750 tda18271_write_regs(fe, R_EP2, 1);
751 tda18271_write_regs(fe, R_EP1, 1);
753 regs[R_EB4] &= 0x07;
754 regs[R_EB4] |= 0x40;
755 tda18271_write_regs(fe, R_EB4, 1);
757 regs[R_EB7] = 0x40;
758 tda18271_write_regs(fe, R_EB7, 1);
759 msleep(10); /* pll locking */
761 regs[R_EB20] = 0xec;
762 tda18271_write_regs(fe, R_EB20, 1);
763 msleep(60); /* RF tracking filter calibration completion */
765 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
766 tda18271_write_regs(fe, R_EP4, 1);
768 tda18271_write_regs(fe, R_EP1, 1);
770 /* RF tracking filter correction for VHF_Low band */
771 if (0 == tda18271_calc_rf_cal(fe, &freq))
772 tda18271_write_regs(fe, R_EB14, 1);
774 return 0;
777 /* ------------------------------------------------------------------ */
779 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
781 struct tda18271_priv *priv = fe->tuner_priv;
782 unsigned char *regs = priv->tda18271_regs;
783 int ret;
785 ret = tda18271_read_regs(fe);
786 if (tda_fail(ret))
787 goto fail;
789 /* test IR_CAL_OK to see if we need init */
790 if ((regs[R_EP1] & 0x08) == 0)
791 ret = tda18271_init_regs(fe);
792 fail:
793 return ret;
796 static int tda18271_init(struct dvb_frontend *fe)
798 struct tda18271_priv *priv = fe->tuner_priv;
799 int ret;
801 mutex_lock(&priv->lock);
803 /* power up */
804 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
805 if (tda_fail(ret))
806 goto fail;
808 /* initialization */
809 ret = tda18271_ir_cal_init(fe);
810 if (tda_fail(ret))
811 goto fail;
813 if (priv->id == TDA18271HDC2)
814 tda18271c2_rf_cal_init(fe);
815 fail:
816 mutex_unlock(&priv->lock);
818 return ret;
821 /* ------------------------------------------------------------------ */
823 static int tda18271_agc(struct dvb_frontend *fe)
825 struct tda18271_priv *priv = fe->tuner_priv;
826 int ret = 0;
828 switch (priv->config) {
829 case 0:
830 /* no LNA */
831 tda_dbg("no agc configuration provided\n");
832 break;
833 case 3:
834 /* switch with GPIO of saa713x */
835 tda_dbg("invoking callback\n");
836 if (fe->callback)
837 ret = fe->callback(priv->i2c_props.adap->algo_data,
838 DVB_FRONTEND_COMPONENT_TUNER,
839 TDA18271_CALLBACK_CMD_AGC_ENABLE,
840 priv->mode);
841 break;
842 case 1:
843 case 2:
844 default:
845 /* n/a - currently not supported */
846 tda_err("unsupported configuration: %d\n", priv->config);
847 ret = -EINVAL;
848 break;
850 return ret;
853 static int tda18271_tune(struct dvb_frontend *fe,
854 struct tda18271_std_map_item *map, u32 freq, u32 bw)
856 struct tda18271_priv *priv = fe->tuner_priv;
857 int ret;
859 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
860 freq, map->if_freq, bw, map->agc_mode, map->std);
862 ret = tda18271_agc(fe);
863 if (tda_fail(ret))
864 tda_warn("failed to configure agc\n");
866 ret = tda18271_init(fe);
867 if (tda_fail(ret))
868 goto fail;
870 mutex_lock(&priv->lock);
872 switch (priv->id) {
873 case TDA18271HDC1:
874 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
875 break;
876 case TDA18271HDC2:
877 tda18271c2_rf_tracking_filters_correction(fe, freq);
878 break;
880 ret = tda18271_channel_configuration(fe, map, freq, bw);
882 mutex_unlock(&priv->lock);
883 fail:
884 return ret;
887 /* ------------------------------------------------------------------ */
889 static int tda18271_set_params(struct dvb_frontend *fe,
890 struct dvb_frontend_parameters *params)
892 struct tda18271_priv *priv = fe->tuner_priv;
893 struct tda18271_std_map *std_map = &priv->std;
894 struct tda18271_std_map_item *map;
895 int ret;
896 u32 bw, freq = params->frequency;
898 priv->mode = TDA18271_DIGITAL;
900 if (fe->ops.info.type == FE_ATSC) {
901 switch (params->u.vsb.modulation) {
902 case VSB_8:
903 case VSB_16:
904 map = &std_map->atsc_6;
905 break;
906 case QAM_64:
907 case QAM_256:
908 map = &std_map->qam_6;
909 break;
910 default:
911 tda_warn("modulation not set!\n");
912 return -EINVAL;
914 #if 0
915 /* userspace request is already center adjusted */
916 freq += 1750000; /* Adjust to center (+1.75MHZ) */
917 #endif
918 bw = 6000000;
919 } else if (fe->ops.info.type == FE_OFDM) {
920 switch (params->u.ofdm.bandwidth) {
921 case BANDWIDTH_6_MHZ:
922 bw = 6000000;
923 map = &std_map->dvbt_6;
924 break;
925 case BANDWIDTH_7_MHZ:
926 bw = 7000000;
927 map = &std_map->dvbt_7;
928 break;
929 case BANDWIDTH_8_MHZ:
930 bw = 8000000;
931 map = &std_map->dvbt_8;
932 break;
933 default:
934 tda_warn("bandwidth not set!\n");
935 return -EINVAL;
937 } else {
938 tda_warn("modulation type not supported!\n");
939 return -EINVAL;
942 /* When tuning digital, the analog demod must be tri-stated */
943 if (fe->ops.analog_ops.standby)
944 fe->ops.analog_ops.standby(fe);
946 ret = tda18271_tune(fe, map, freq, bw);
948 if (tda_fail(ret))
949 goto fail;
951 priv->frequency = freq;
952 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
953 params->u.ofdm.bandwidth : 0;
954 fail:
955 return ret;
958 static int tda18271_set_analog_params(struct dvb_frontend *fe,
959 struct analog_parameters *params)
961 struct tda18271_priv *priv = fe->tuner_priv;
962 struct tda18271_std_map *std_map = &priv->std;
963 struct tda18271_std_map_item *map;
964 char *mode;
965 int ret;
966 u32 freq = params->frequency * 62500;
968 priv->mode = TDA18271_ANALOG;
970 if (params->mode == V4L2_TUNER_RADIO) {
971 freq = freq / 1000;
972 map = &std_map->fm_radio;
973 mode = "fm";
974 } else if (params->std & V4L2_STD_MN) {
975 map = &std_map->atv_mn;
976 mode = "MN";
977 } else if (params->std & V4L2_STD_B) {
978 map = &std_map->atv_b;
979 mode = "B";
980 } else if (params->std & V4L2_STD_GH) {
981 map = &std_map->atv_gh;
982 mode = "GH";
983 } else if (params->std & V4L2_STD_PAL_I) {
984 map = &std_map->atv_i;
985 mode = "I";
986 } else if (params->std & V4L2_STD_DK) {
987 map = &std_map->atv_dk;
988 mode = "DK";
989 } else if (params->std & V4L2_STD_SECAM_L) {
990 map = &std_map->atv_l;
991 mode = "L";
992 } else if (params->std & V4L2_STD_SECAM_LC) {
993 map = &std_map->atv_lc;
994 mode = "L'";
995 } else {
996 map = &std_map->atv_i;
997 mode = "xx";
1000 tda_dbg("setting tda18271 to system %s\n", mode);
1002 ret = tda18271_tune(fe, map, freq, 0);
1004 if (tda_fail(ret))
1005 goto fail;
1007 priv->frequency = freq;
1008 priv->bandwidth = 0;
1009 fail:
1010 return ret;
1013 static int tda18271_sleep(struct dvb_frontend *fe)
1015 struct tda18271_priv *priv = fe->tuner_priv;
1016 int ret;
1018 mutex_lock(&priv->lock);
1020 /* standby mode w/ slave tuner output
1021 * & loop thru & xtal oscillator on */
1022 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
1024 mutex_unlock(&priv->lock);
1026 return ret;
1029 static int tda18271_release(struct dvb_frontend *fe)
1031 struct tda18271_priv *priv = fe->tuner_priv;
1033 mutex_lock(&tda18271_list_mutex);
1035 if (priv)
1036 hybrid_tuner_release_state(priv);
1038 mutex_unlock(&tda18271_list_mutex);
1040 fe->tuner_priv = NULL;
1042 return 0;
1045 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1047 struct tda18271_priv *priv = fe->tuner_priv;
1048 *frequency = priv->frequency;
1049 return 0;
1052 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1054 struct tda18271_priv *priv = fe->tuner_priv;
1055 *bandwidth = priv->bandwidth;
1056 return 0;
1059 /* ------------------------------------------------------------------ */
1061 #define tda18271_update_std(std_cfg, name) do { \
1062 if (map->std_cfg.if_freq + \
1063 map->std_cfg.agc_mode + map->std_cfg.std + \
1064 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1065 tda_dbg("Using custom std config for %s\n", name); \
1066 memcpy(&std->std_cfg, &map->std_cfg, \
1067 sizeof(struct tda18271_std_map_item)); \
1068 } } while (0)
1070 #define tda18271_dump_std_item(std_cfg, name) do { \
1071 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1072 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1073 name, std->std_cfg.if_freq, \
1074 std->std_cfg.agc_mode, std->std_cfg.std, \
1075 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1076 } while (0)
1078 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1080 struct tda18271_priv *priv = fe->tuner_priv;
1081 struct tda18271_std_map *std = &priv->std;
1083 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1084 tda18271_dump_std_item(fm_radio, " fm ");
1085 tda18271_dump_std_item(atv_b, "atv b ");
1086 tda18271_dump_std_item(atv_dk, "atv dk");
1087 tda18271_dump_std_item(atv_gh, "atv gh");
1088 tda18271_dump_std_item(atv_i, "atv i ");
1089 tda18271_dump_std_item(atv_l, "atv l ");
1090 tda18271_dump_std_item(atv_lc, "atv l'");
1091 tda18271_dump_std_item(atv_mn, "atv mn");
1092 tda18271_dump_std_item(atsc_6, "atsc 6");
1093 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1094 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1095 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1096 tda18271_dump_std_item(qam_6, "qam 6 ");
1097 tda18271_dump_std_item(qam_8, "qam 8 ");
1099 return 0;
1102 static int tda18271_update_std_map(struct dvb_frontend *fe,
1103 struct tda18271_std_map *map)
1105 struct tda18271_priv *priv = fe->tuner_priv;
1106 struct tda18271_std_map *std = &priv->std;
1108 if (!map)
1109 return -EINVAL;
1111 tda18271_update_std(fm_radio, "fm");
1112 tda18271_update_std(atv_b, "atv b");
1113 tda18271_update_std(atv_dk, "atv dk");
1114 tda18271_update_std(atv_gh, "atv gh");
1115 tda18271_update_std(atv_i, "atv i");
1116 tda18271_update_std(atv_l, "atv l");
1117 tda18271_update_std(atv_lc, "atv l'");
1118 tda18271_update_std(atv_mn, "atv mn");
1119 tda18271_update_std(atsc_6, "atsc 6");
1120 tda18271_update_std(dvbt_6, "dvbt 6");
1121 tda18271_update_std(dvbt_7, "dvbt 7");
1122 tda18271_update_std(dvbt_8, "dvbt 8");
1123 tda18271_update_std(qam_6, "qam 6");
1124 tda18271_update_std(qam_8, "qam 8");
1126 return 0;
1129 static int tda18271_get_id(struct dvb_frontend *fe)
1131 struct tda18271_priv *priv = fe->tuner_priv;
1132 unsigned char *regs = priv->tda18271_regs;
1133 char *name;
1134 int ret = 0;
1136 mutex_lock(&priv->lock);
1137 tda18271_read_regs(fe);
1138 mutex_unlock(&priv->lock);
1140 switch (regs[R_ID] & 0x7f) {
1141 case 3:
1142 name = "TDA18271HD/C1";
1143 priv->id = TDA18271HDC1;
1144 break;
1145 case 4:
1146 name = "TDA18271HD/C2";
1147 priv->id = TDA18271HDC2;
1148 break;
1149 default:
1150 name = "Unknown device";
1151 ret = -EINVAL;
1152 break;
1155 tda_info("%s detected @ %d-%04x%s\n", name,
1156 i2c_adapter_id(priv->i2c_props.adap),
1157 priv->i2c_props.addr,
1158 (0 == ret) ? "" : ", device not supported.");
1160 return ret;
1163 static struct dvb_tuner_ops tda18271_tuner_ops = {
1164 .info = {
1165 .name = "NXP TDA18271HD",
1166 .frequency_min = 45000000,
1167 .frequency_max = 864000000,
1168 .frequency_step = 62500
1170 .init = tda18271_init,
1171 .sleep = tda18271_sleep,
1172 .set_params = tda18271_set_params,
1173 .set_analog_params = tda18271_set_analog_params,
1174 .release = tda18271_release,
1175 .get_frequency = tda18271_get_frequency,
1176 .get_bandwidth = tda18271_get_bandwidth,
1179 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1180 struct i2c_adapter *i2c,
1181 struct tda18271_config *cfg)
1183 struct tda18271_priv *priv = NULL;
1184 int instance;
1186 mutex_lock(&tda18271_list_mutex);
1188 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1189 hybrid_tuner_instance_list,
1190 i2c, addr, "tda18271");
1191 switch (instance) {
1192 case 0:
1193 goto fail;
1194 case 1:
1195 /* new tuner instance */
1196 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1197 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1198 priv->config = (cfg) ? cfg->config : 0;
1199 priv->cal_initialized = false;
1200 mutex_init(&priv->lock);
1202 fe->tuner_priv = priv;
1204 if (cfg)
1205 priv->small_i2c = cfg->small_i2c;
1207 if (tda_fail(tda18271_get_id(fe)))
1208 goto fail;
1210 if (tda_fail(tda18271_assign_map_layout(fe)))
1211 goto fail;
1213 mutex_lock(&priv->lock);
1214 tda18271_init_regs(fe);
1216 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1217 tda18271c2_rf_cal_init(fe);
1219 mutex_unlock(&priv->lock);
1220 break;
1221 default:
1222 /* existing tuner instance */
1223 fe->tuner_priv = priv;
1225 /* allow dvb driver to override i2c gate setting */
1226 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1227 priv->gate = cfg->gate;
1228 break;
1231 /* override default std map with values in config struct */
1232 if ((cfg) && (cfg->std_map))
1233 tda18271_update_std_map(fe, cfg->std_map);
1235 mutex_unlock(&tda18271_list_mutex);
1237 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1238 sizeof(struct dvb_tuner_ops));
1240 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1241 tda18271_dump_std_map(fe);
1243 return fe;
1244 fail:
1245 mutex_unlock(&tda18271_list_mutex);
1247 tda18271_release(fe);
1248 return NULL;
1250 EXPORT_SYMBOL_GPL(tda18271_attach);
1251 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1252 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1253 MODULE_LICENSE("GPL");
1254 MODULE_VERSION("0.3");
1257 * Overrides for Emacs so that we follow Linus's tabbing style.
1258 * ---------------------------------------------------------------------------
1259 * Local variables:
1260 * c-basic-offset: 8
1261 * End: