x86: cpa self-test, WARN_ON()
[wrt350n-kernel.git] / drivers / media / dvb / frontends / tda18271-fe.c
blobdfe72aaec3802188f3f385d3171d80617c7d903e
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 LIST_HEAD(tda18271_list);
35 static DEFINE_MUTEX(tda18271_list_mutex);
37 /*---------------------------------------------------------------------*/
39 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
41 struct tda18271_priv *priv = fe->tuner_priv;
42 unsigned char *regs = priv->tda18271_regs;
44 tda18271_read_regs(fe);
46 /* test IR_CAL_OK to see if we need init */
47 if ((regs[R_EP1] & 0x08) == 0)
48 tda18271_init_regs(fe);
50 return 0;
53 /* ------------------------------------------------------------------ */
55 static int tda18271_channel_configuration(struct dvb_frontend *fe,
56 u32 ifc, u32 freq, u32 bw, u8 std,
57 int radio)
59 struct tda18271_priv *priv = fe->tuner_priv;
60 unsigned char *regs = priv->tda18271_regs;
61 u32 N;
63 /* update TV broadcast parameters */
65 /* set standard */
66 regs[R_EP3] &= ~0x1f; /* clear std bits */
67 regs[R_EP3] |= std;
69 /* set cal mode to normal */
70 regs[R_EP4] &= ~0x03;
72 /* update IF output level & IF notch frequency */
73 regs[R_EP4] &= ~0x1c; /* clear if level bits */
75 switch (priv->mode) {
76 case TDA18271_ANALOG:
77 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
78 break;
79 case TDA18271_DIGITAL:
80 regs[R_EP4] |= 0x04; /* IF level = 1 */
81 regs[R_MPD] |= 0x80; /* IF notch = 1 */
82 break;
85 if (radio)
86 regs[R_EP4] |= 0x80;
87 else
88 regs[R_EP4] &= ~0x80;
90 /* update RF_TOP / IF_TOP */
91 switch (priv->mode) {
92 case TDA18271_ANALOG:
93 regs[R_EB22] = 0x2c;
94 break;
95 case TDA18271_DIGITAL:
96 regs[R_EB22] = 0x37;
97 break;
99 tda18271_write_regs(fe, R_EB22, 1);
101 /* --------------------------------------------------------------- */
103 /* disable Power Level Indicator */
104 regs[R_EP1] |= 0x40;
106 /* frequency dependent parameters */
108 tda18271_calc_ir_measure(fe, &freq);
110 tda18271_calc_bp_filter(fe, &freq);
112 tda18271_calc_rf_band(fe, &freq);
114 tda18271_calc_gain_taper(fe, &freq);
116 /* --------------------------------------------------------------- */
118 /* dual tuner and agc1 extra configuration */
120 /* main vco when Master, cal vco when slave */
121 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
123 /* agc1 always active */
124 regs[R_EB1] &= ~0x02;
126 /* agc1 has priority on agc2 */
127 regs[R_EB1] &= ~0x01;
129 tda18271_write_regs(fe, R_EB1, 1);
131 /* --------------------------------------------------------------- */
133 N = freq + ifc;
135 /* FIXME: assumes master */
136 tda18271_calc_main_pll(fe, N);
137 tda18271_write_regs(fe, R_MPD, 4);
139 tda18271_write_regs(fe, R_TM, 7);
141 /* main pll charge pump source */
142 regs[R_EB4] |= 0x20;
143 tda18271_write_regs(fe, R_EB4, 1);
145 msleep(1);
147 /* normal operation for the main pll */
148 regs[R_EB4] &= ~0x20;
149 tda18271_write_regs(fe, R_EB4, 1);
151 msleep(5);
153 return 0;
156 static int tda18271_read_thermometer(struct dvb_frontend *fe)
158 struct tda18271_priv *priv = fe->tuner_priv;
159 unsigned char *regs = priv->tda18271_regs;
160 int tm;
162 /* switch thermometer on */
163 regs[R_TM] |= 0x10;
164 tda18271_write_regs(fe, R_TM, 1);
166 /* read thermometer info */
167 tda18271_read_regs(fe);
169 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
170 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
172 if ((regs[R_TM] & 0x20) == 0x20)
173 regs[R_TM] &= ~0x20;
174 else
175 regs[R_TM] |= 0x20;
177 tda18271_write_regs(fe, R_TM, 1);
179 msleep(10); /* temperature sensing */
181 /* read thermometer info */
182 tda18271_read_regs(fe);
185 tm = tda18271_lookup_thermometer(fe);
187 /* switch thermometer off */
188 regs[R_TM] &= ~0x10;
189 tda18271_write_regs(fe, R_TM, 1);
191 /* set CAL mode to normal */
192 regs[R_EP4] &= ~0x03;
193 tda18271_write_regs(fe, R_EP4, 1);
195 return tm;
198 static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe,
199 u32 freq)
201 struct tda18271_priv *priv = fe->tuner_priv;
202 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
203 unsigned char *regs = priv->tda18271_regs;
204 int tm_current, rfcal_comp, approx, i;
205 u8 dc_over_dt, rf_tab;
207 /* power up */
208 tda18271_set_standby_mode(fe, 0, 0, 0);
210 /* read die current temperature */
211 tm_current = tda18271_read_thermometer(fe);
213 /* frequency dependent parameters */
215 tda18271_calc_rf_cal(fe, &freq);
216 rf_tab = regs[R_EB14];
218 i = tda18271_lookup_rf_band(fe, &freq, NULL);
219 if (i < 0)
220 return -EINVAL;
222 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
223 approx = map[i].rf_a1 *
224 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
225 } else {
226 approx = map[i].rf_a2 *
227 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
230 if (approx < 0)
231 approx = 0;
232 if (approx > 255)
233 approx = 255;
235 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
237 /* calculate temperature compensation */
238 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
240 regs[R_EB14] = approx + rfcal_comp;
241 tda18271_write_regs(fe, R_EB14, 1);
243 return 0;
246 static int tda18271_por(struct dvb_frontend *fe)
248 struct tda18271_priv *priv = fe->tuner_priv;
249 unsigned char *regs = priv->tda18271_regs;
251 /* power up detector 1 */
252 regs[R_EB12] &= ~0x20;
253 tda18271_write_regs(fe, R_EB12, 1);
255 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
256 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
257 tda18271_write_regs(fe, R_EB18, 1);
259 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
261 /* POR mode */
262 tda18271_set_standby_mode(fe, 1, 0, 0);
264 /* disable 1.5 MHz low pass filter */
265 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
266 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
267 tda18271_write_regs(fe, R_EB21, 3);
269 return 0;
272 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
274 struct tda18271_priv *priv = fe->tuner_priv;
275 unsigned char *regs = priv->tda18271_regs;
276 u32 N;
278 /* set CAL mode to normal */
279 regs[R_EP4] &= ~0x03;
280 tda18271_write_regs(fe, R_EP4, 1);
282 /* switch off agc1 */
283 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
285 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
286 tda18271_write_regs(fe, R_EB18, 1);
288 /* frequency dependent parameters */
290 tda18271_calc_bp_filter(fe, &freq);
291 tda18271_calc_gain_taper(fe, &freq);
292 tda18271_calc_rf_band(fe, &freq);
293 tda18271_calc_km(fe, &freq);
295 tda18271_write_regs(fe, R_EP1, 3);
296 tda18271_write_regs(fe, R_EB13, 1);
298 /* main pll charge pump source */
299 regs[R_EB4] |= 0x20;
300 tda18271_write_regs(fe, R_EB4, 1);
302 /* cal pll charge pump source */
303 regs[R_EB7] |= 0x20;
304 tda18271_write_regs(fe, R_EB7, 1);
306 /* force dcdc converter to 0 V */
307 regs[R_EB14] = 0x00;
308 tda18271_write_regs(fe, R_EB14, 1);
310 /* disable plls lock */
311 regs[R_EB20] &= ~0x20;
312 tda18271_write_regs(fe, R_EB20, 1);
314 /* set CAL mode to RF tracking filter calibration */
315 regs[R_EP4] |= 0x03;
316 tda18271_write_regs(fe, R_EP4, 2);
318 /* --------------------------------------------------------------- */
320 /* set the internal calibration signal */
321 N = freq;
323 tda18271_calc_main_pll(fe, N);
324 tda18271_write_regs(fe, R_MPD, 4);
326 /* downconvert internal calibration */
327 N += 1000000;
329 tda18271_calc_main_pll(fe, N);
330 tda18271_write_regs(fe, R_MPD, 4);
332 msleep(5);
334 tda18271_write_regs(fe, R_EP2, 1);
335 tda18271_write_regs(fe, R_EP1, 1);
336 tda18271_write_regs(fe, R_EP2, 1);
337 tda18271_write_regs(fe, R_EP1, 1);
339 /* --------------------------------------------------------------- */
341 /* normal operation for the main pll */
342 regs[R_EB4] &= ~0x20;
343 tda18271_write_regs(fe, R_EB4, 1);
345 /* normal operation for the cal pll */
346 regs[R_EB7] &= ~0x20;
347 tda18271_write_regs(fe, R_EB7, 1);
349 msleep(5); /* plls locking */
351 /* launch the rf tracking filters calibration */
352 regs[R_EB20] |= 0x20;
353 tda18271_write_regs(fe, R_EB20, 1);
355 msleep(60); /* calibration */
357 /* --------------------------------------------------------------- */
359 /* set CAL mode to normal */
360 regs[R_EP4] &= ~0x03;
362 /* switch on agc1 */
363 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
365 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
366 tda18271_write_regs(fe, R_EB18, 1);
368 tda18271_write_regs(fe, R_EP3, 2);
370 /* synchronization */
371 tda18271_write_regs(fe, R_EP1, 1);
373 /* get calibration result */
374 tda18271_read_extended(fe);
376 return regs[R_EB14];
379 static int tda18271_powerscan(struct dvb_frontend *fe,
380 u32 *freq_in, u32 *freq_out)
382 struct tda18271_priv *priv = fe->tuner_priv;
383 unsigned char *regs = priv->tda18271_regs;
384 int sgn, bcal, count, wait;
385 u8 cid_target;
386 u16 count_limit;
387 u32 freq;
389 freq = *freq_in;
391 tda18271_calc_rf_band(fe, &freq);
392 tda18271_calc_rf_cal(fe, &freq);
393 tda18271_calc_gain_taper(fe, &freq);
394 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
396 tda18271_write_regs(fe, R_EP2, 1);
397 tda18271_write_regs(fe, R_EB14, 1);
399 /* downconvert frequency */
400 freq += 1000000;
402 tda18271_calc_main_pll(fe, freq);
403 tda18271_write_regs(fe, R_MPD, 4);
405 msleep(5); /* pll locking */
407 /* detection mode */
408 regs[R_EP4] &= ~0x03;
409 regs[R_EP4] |= 0x01;
410 tda18271_write_regs(fe, R_EP4, 1);
412 /* launch power detection measurement */
413 tda18271_write_regs(fe, R_EP2, 1);
415 /* read power detection info, stored in EB10 */
416 tda18271_read_extended(fe);
418 /* algorithm initialization */
419 sgn = 1;
420 *freq_out = *freq_in;
421 bcal = 0;
422 count = 0;
423 wait = false;
425 while ((regs[R_EB10] & 0x3f) < cid_target) {
426 /* downconvert updated freq to 1 MHz */
427 freq = *freq_in + (sgn * count) + 1000000;
429 tda18271_calc_main_pll(fe, freq);
430 tda18271_write_regs(fe, R_MPD, 4);
432 if (wait) {
433 msleep(5); /* pll locking */
434 wait = false;
435 } else
436 udelay(100); /* pll locking */
438 /* launch power detection measurement */
439 tda18271_write_regs(fe, R_EP2, 1);
441 /* read power detection info, stored in EB10 */
442 tda18271_read_extended(fe);
444 count += 200;
446 if (count < count_limit)
447 continue;
449 if (sgn <= 0)
450 break;
452 sgn = -1 * sgn;
453 count = 200;
454 wait = true;
457 if ((regs[R_EB10] & 0x3f) >= cid_target) {
458 bcal = 1;
459 *freq_out = freq - 1000000;
460 } else
461 bcal = 0;
463 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
464 bcal, *freq_in, *freq_out, freq);
466 return bcal;
469 static int tda18271_powerscan_init(struct dvb_frontend *fe)
471 struct tda18271_priv *priv = fe->tuner_priv;
472 unsigned char *regs = priv->tda18271_regs;
474 /* set standard to digital */
475 regs[R_EP3] &= ~0x1f; /* clear std bits */
476 regs[R_EP3] |= 0x12;
478 /* set cal mode to normal */
479 regs[R_EP4] &= ~0x03;
481 /* update IF output level & IF notch frequency */
482 regs[R_EP4] &= ~0x1c; /* clear if level bits */
484 tda18271_write_regs(fe, R_EP3, 2);
486 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
487 tda18271_write_regs(fe, R_EB18, 1);
489 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
491 /* 1.5 MHz low pass filter */
492 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
493 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
495 tda18271_write_regs(fe, R_EB21, 3);
497 return 0;
500 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
502 struct tda18271_priv *priv = fe->tuner_priv;
503 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
504 unsigned char *regs = priv->tda18271_regs;
505 int bcal, rf, i;
506 #define RF1 0
507 #define RF2 1
508 #define RF3 2
509 u32 rf_default[3];
510 u32 rf_freq[3];
511 u8 prog_cal[3];
512 u8 prog_tab[3];
514 i = tda18271_lookup_rf_band(fe, &freq, NULL);
516 if (i < 0)
517 return i;
519 rf_default[RF1] = 1000 * map[i].rf1_def;
520 rf_default[RF2] = 1000 * map[i].rf2_def;
521 rf_default[RF3] = 1000 * map[i].rf3_def;
523 for (rf = RF1; rf <= RF3; rf++) {
524 if (0 == rf_default[rf])
525 return 0;
526 tda_cal("freq = %d, rf = %d\n", freq, rf);
528 /* look for optimized calibration frequency */
529 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
531 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
532 prog_tab[rf] = regs[R_EB14];
534 if (1 == bcal)
535 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
536 else
537 prog_cal[rf] = prog_tab[rf];
539 switch (rf) {
540 case RF1:
541 map[i].rf_a1 = 0;
542 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
543 map[i].rf1 = rf_freq[RF1] / 1000;
544 break;
545 case RF2:
546 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
547 prog_cal[RF1] + prog_tab[RF1]) /
548 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
549 map[i].rf2 = rf_freq[RF2] / 1000;
550 break;
551 case RF3:
552 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
553 prog_cal[RF2] + prog_tab[RF2]) /
554 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
555 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
556 map[i].rf3 = rf_freq[RF3] / 1000;
557 break;
558 default:
559 BUG();
563 return 0;
566 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
568 struct tda18271_priv *priv = fe->tuner_priv;
569 unsigned int i;
571 tda_info("tda18271: performing RF tracking filter calibration\n");
573 /* wait for die temperature stabilization */
574 msleep(200);
576 tda18271_powerscan_init(fe);
578 /* rf band calibration */
579 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
580 tda18271_rf_tracking_filters_init(fe, 1000 *
581 priv->rf_cal_state[i].rfmax);
583 priv->tm_rfcal = tda18271_read_thermometer(fe);
585 return 0;
588 /* ------------------------------------------------------------------ */
590 static int tda18271_rf_cal_init(struct dvb_frontend *fe)
592 struct tda18271_priv *priv = fe->tuner_priv;
593 unsigned char *regs = priv->tda18271_regs;
595 /* test RF_CAL_OK to see if we need init */
596 if ((regs[R_EP1] & 0x10) == 0)
597 priv->cal_initialized = false;
599 if (priv->cal_initialized)
600 return 0;
602 tda18271_calc_rf_filter_curve(fe);
604 tda18271_por(fe);
606 tda_info("tda18271: RF tracking filter calibration complete\n");
608 priv->cal_initialized = true;
610 return 0;
613 static int tda18271_init(struct dvb_frontend *fe)
615 struct tda18271_priv *priv = fe->tuner_priv;
617 mutex_lock(&priv->lock);
619 /* power up */
620 tda18271_set_standby_mode(fe, 0, 0, 0);
622 /* initialization */
623 tda18271_ir_cal_init(fe);
625 if (priv->id == TDA18271HDC2)
626 tda18271_rf_cal_init(fe);
628 mutex_unlock(&priv->lock);
630 return 0;
633 static int tda18271c2_tune(struct dvb_frontend *fe,
634 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
636 struct tda18271_priv *priv = fe->tuner_priv;
638 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
640 tda18271_init(fe);
642 mutex_lock(&priv->lock);
644 tda18271_rf_tracking_filters_correction(fe, freq);
646 tda18271_channel_configuration(fe, ifc, freq, bw, std, radio);
648 mutex_unlock(&priv->lock);
650 return 0;
653 /* ------------------------------------------------------------------ */
655 static int tda18271c1_tune(struct dvb_frontend *fe,
656 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
658 struct tda18271_priv *priv = fe->tuner_priv;
659 unsigned char *regs = priv->tda18271_regs;
660 u32 N = 0;
662 tda18271_init(fe);
664 mutex_lock(&priv->lock);
666 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
668 /* RF tracking filter calibration */
670 /* calculate bp filter */
671 tda18271_calc_bp_filter(fe, &freq);
672 tda18271_write_regs(fe, R_EP1, 1);
674 regs[R_EB4] &= 0x07;
675 regs[R_EB4] |= 0x60;
676 tda18271_write_regs(fe, R_EB4, 1);
678 regs[R_EB7] = 0x60;
679 tda18271_write_regs(fe, R_EB7, 1);
681 regs[R_EB14] = 0x00;
682 tda18271_write_regs(fe, R_EB14, 1);
684 regs[R_EB20] = 0xcc;
685 tda18271_write_regs(fe, R_EB20, 1);
687 /* set cal mode to RF tracking filter calibration */
688 regs[R_EP4] |= 0x03;
690 /* calculate cal pll */
692 switch (priv->mode) {
693 case TDA18271_ANALOG:
694 N = freq - 1250000;
695 break;
696 case TDA18271_DIGITAL:
697 N = freq + bw / 2;
698 break;
701 tda18271_calc_cal_pll(fe, N);
703 /* calculate main pll */
705 switch (priv->mode) {
706 case TDA18271_ANALOG:
707 N = freq - 250000;
708 break;
709 case TDA18271_DIGITAL:
710 N = freq + bw / 2 + 1000000;
711 break;
714 tda18271_calc_main_pll(fe, N);
716 tda18271_write_regs(fe, R_EP3, 11);
717 msleep(5); /* RF tracking filter calibration initialization */
719 /* search for K,M,CO for RF calibration */
720 tda18271_calc_km(fe, &freq);
721 tda18271_write_regs(fe, R_EB13, 1);
723 /* search for rf band */
724 tda18271_calc_rf_band(fe, &freq);
726 /* search for gain taper */
727 tda18271_calc_gain_taper(fe, &freq);
729 tda18271_write_regs(fe, R_EP2, 1);
730 tda18271_write_regs(fe, R_EP1, 1);
731 tda18271_write_regs(fe, R_EP2, 1);
732 tda18271_write_regs(fe, R_EP1, 1);
734 regs[R_EB4] &= 0x07;
735 regs[R_EB4] |= 0x40;
736 tda18271_write_regs(fe, R_EB4, 1);
738 regs[R_EB7] = 0x40;
739 tda18271_write_regs(fe, R_EB7, 1);
740 msleep(10);
742 regs[R_EB20] = 0xec;
743 tda18271_write_regs(fe, R_EB20, 1);
744 msleep(60); /* RF tracking filter calibration completion */
746 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
747 tda18271_write_regs(fe, R_EP4, 1);
749 tda18271_write_regs(fe, R_EP1, 1);
751 /* RF tracking filter correction for VHF_Low band */
752 if (0 == tda18271_calc_rf_cal(fe, &freq))
753 tda18271_write_regs(fe, R_EB14, 1);
755 /* Channel Configuration */
757 switch (priv->mode) {
758 case TDA18271_ANALOG:
759 regs[R_EB22] = 0x2c;
760 break;
761 case TDA18271_DIGITAL:
762 regs[R_EB22] = 0x37;
763 break;
765 tda18271_write_regs(fe, R_EB22, 1);
767 regs[R_EP1] |= 0x40; /* set dis power level on */
769 /* set standard */
770 regs[R_EP3] &= ~0x1f; /* clear std bits */
772 /* see table 22 */
773 regs[R_EP3] |= std;
775 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
777 regs[R_EP4] &= ~0x1c; /* clear if level bits */
778 switch (priv->mode) {
779 case TDA18271_ANALOG:
780 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
781 break;
782 case TDA18271_DIGITAL:
783 regs[R_EP4] |= 0x04;
784 regs[R_MPD] |= 0x80;
785 break;
788 if (radio)
789 regs[R_EP4] |= 0x80;
790 else
791 regs[R_EP4] &= ~0x80;
793 /* image rejection validity */
794 tda18271_calc_ir_measure(fe, &freq);
796 /* calculate MAIN PLL */
797 N = freq + ifc;
799 tda18271_calc_main_pll(fe, N);
801 tda18271_write_regs(fe, R_TM, 15);
802 msleep(5);
803 mutex_unlock(&priv->lock);
805 return 0;
808 static inline int tda18271_tune(struct dvb_frontend *fe,
809 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
811 struct tda18271_priv *priv = fe->tuner_priv;
812 int ret = -EINVAL;
814 switch (priv->id) {
815 case TDA18271HDC1:
816 ret = tda18271c1_tune(fe, ifc, freq, bw, std, radio);
817 break;
818 case TDA18271HDC2:
819 ret = tda18271c2_tune(fe, ifc, freq, bw, std, radio);
820 break;
822 return ret;
825 /* ------------------------------------------------------------------ */
827 static int tda18271_set_params(struct dvb_frontend *fe,
828 struct dvb_frontend_parameters *params)
830 struct tda18271_priv *priv = fe->tuner_priv;
831 struct tda18271_std_map *std_map = &priv->std;
832 int ret;
833 u8 std;
834 u16 sgIF;
835 u32 bw, freq = params->frequency;
837 priv->mode = TDA18271_DIGITAL;
839 if (fe->ops.info.type == FE_ATSC) {
840 switch (params->u.vsb.modulation) {
841 case VSB_8:
842 case VSB_16:
843 std = std_map->atsc_6.std_bits;
844 sgIF = std_map->atsc_6.if_freq;
845 break;
846 case QAM_64:
847 case QAM_256:
848 std = std_map->qam_6.std_bits;
849 sgIF = std_map->qam_6.if_freq;
850 break;
851 default:
852 tda_warn("modulation not set!\n");
853 return -EINVAL;
855 #if 0
856 /* userspace request is already center adjusted */
857 freq += 1750000; /* Adjust to center (+1.75MHZ) */
858 #endif
859 bw = 6000000;
860 } else if (fe->ops.info.type == FE_OFDM) {
861 switch (params->u.ofdm.bandwidth) {
862 case BANDWIDTH_6_MHZ:
863 bw = 6000000;
864 std = std_map->dvbt_6.std_bits;
865 sgIF = std_map->dvbt_6.if_freq;
866 break;
867 case BANDWIDTH_7_MHZ:
868 bw = 7000000;
869 std = std_map->dvbt_7.std_bits;
870 sgIF = std_map->dvbt_7.if_freq;
871 break;
872 case BANDWIDTH_8_MHZ:
873 bw = 8000000;
874 std = std_map->dvbt_8.std_bits;
875 sgIF = std_map->dvbt_8.if_freq;
876 break;
877 default:
878 tda_warn("bandwidth not set!\n");
879 return -EINVAL;
881 } else {
882 tda_warn("modulation type not supported!\n");
883 return -EINVAL;
886 /* When tuning digital, the analog demod must be tri-stated */
887 if (fe->ops.analog_ops.standby)
888 fe->ops.analog_ops.standby(fe);
890 ret = tda18271_tune(fe, sgIF * 1000, freq, bw, std, 0);
892 if (ret < 0)
893 goto fail;
895 priv->frequency = freq;
896 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
897 params->u.ofdm.bandwidth : 0;
898 fail:
899 return ret;
902 static int tda18271_set_analog_params(struct dvb_frontend *fe,
903 struct analog_parameters *params)
905 struct tda18271_priv *priv = fe->tuner_priv;
906 struct tda18271_std_map *std_map = &priv->std;
907 char *mode;
908 int ret, radio = 0;
909 u8 std;
910 u16 sgIF;
911 u32 freq = params->frequency * 62500;
913 priv->mode = TDA18271_ANALOG;
915 if (params->mode == V4L2_TUNER_RADIO) {
916 radio = 1;
917 freq = freq / 1000;
918 std = std_map->fm_radio.std_bits;
919 sgIF = std_map->fm_radio.if_freq;
920 mode = "fm";
921 } else if (params->std & V4L2_STD_MN) {
922 std = std_map->atv_mn.std_bits;
923 sgIF = std_map->atv_mn.if_freq;
924 mode = "MN";
925 } else if (params->std & V4L2_STD_B) {
926 std = std_map->atv_b.std_bits;
927 sgIF = std_map->atv_b.if_freq;
928 mode = "B";
929 } else if (params->std & V4L2_STD_GH) {
930 std = std_map->atv_gh.std_bits;
931 sgIF = std_map->atv_gh.if_freq;
932 mode = "GH";
933 } else if (params->std & V4L2_STD_PAL_I) {
934 std = std_map->atv_i.std_bits;
935 sgIF = std_map->atv_i.if_freq;
936 mode = "I";
937 } else if (params->std & V4L2_STD_DK) {
938 std = std_map->atv_dk.std_bits;
939 sgIF = std_map->atv_dk.if_freq;
940 mode = "DK";
941 } else if (params->std & V4L2_STD_SECAM_L) {
942 std = std_map->atv_l.std_bits;
943 sgIF = std_map->atv_l.if_freq;
944 mode = "L";
945 } else if (params->std & V4L2_STD_SECAM_LC) {
946 std = std_map->atv_lc.std_bits;
947 sgIF = std_map->atv_lc.if_freq;
948 mode = "L'";
949 } else {
950 std = std_map->atv_i.std_bits;
951 sgIF = std_map->atv_i.if_freq;
952 mode = "xx";
955 tda_dbg("setting tda18271 to system %s\n", mode);
957 ret = tda18271_tune(fe, sgIF * 1000, freq, 0, std, radio);
959 if (ret < 0)
960 goto fail;
962 priv->frequency = freq;
963 priv->bandwidth = 0;
964 fail:
965 return ret;
968 static int tda18271_sleep(struct dvb_frontend *fe)
970 struct tda18271_priv *priv = fe->tuner_priv;
972 mutex_lock(&priv->lock);
974 /* standby mode w/ slave tuner output
975 * & loop thru & xtal oscillator on */
976 tda18271_set_standby_mode(fe, 1, 0, 0);
978 mutex_unlock(&priv->lock);
980 return 0;
983 static int tda18271_release(struct dvb_frontend *fe)
985 struct tda18271_priv *priv = fe->tuner_priv;
987 mutex_lock(&tda18271_list_mutex);
989 priv->count--;
991 if (!priv->count) {
992 tda_dbg("destroying instance @ %d-%04x\n",
993 i2c_adapter_id(priv->i2c_adap),
994 priv->i2c_addr);
995 list_del(&priv->tda18271_list);
997 kfree(priv);
999 mutex_unlock(&tda18271_list_mutex);
1001 fe->tuner_priv = NULL;
1003 return 0;
1006 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1008 struct tda18271_priv *priv = fe->tuner_priv;
1009 *frequency = priv->frequency;
1010 return 0;
1013 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1015 struct tda18271_priv *priv = fe->tuner_priv;
1016 *bandwidth = priv->bandwidth;
1017 return 0;
1020 /* ------------------------------------------------------------------ */
1022 #define tda18271_update_std(std_cfg, name) do { \
1023 if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) { \
1024 tda_dbg("Using custom std config for %s\n", name); \
1025 memcpy(&std->std_cfg, &map->std_cfg, \
1026 sizeof(struct tda18271_std_map_item)); \
1027 } } while (0)
1029 #define tda18271_dump_std_item(std_cfg, name) do { \
1030 tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \
1031 name, std->std_cfg.if_freq, std->std_cfg.std_bits); \
1032 } while (0)
1034 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1036 struct tda18271_priv *priv = fe->tuner_priv;
1037 struct tda18271_std_map *std = &priv->std;
1039 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1040 tda18271_dump_std_item(fm_radio, "fm");
1041 tda18271_dump_std_item(atv_b, "pal b");
1042 tda18271_dump_std_item(atv_dk, "pal dk");
1043 tda18271_dump_std_item(atv_gh, "pal gh");
1044 tda18271_dump_std_item(atv_i, "pal i");
1045 tda18271_dump_std_item(atv_l, "pal l");
1046 tda18271_dump_std_item(atv_lc, "pal l'");
1047 tda18271_dump_std_item(atv_mn, "atv mn");
1048 tda18271_dump_std_item(atsc_6, "atsc 6");
1049 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1050 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1051 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1052 tda18271_dump_std_item(qam_6, "qam 6");
1053 tda18271_dump_std_item(qam_8, "qam 8");
1055 return 0;
1058 static int tda18271_update_std_map(struct dvb_frontend *fe,
1059 struct tda18271_std_map *map)
1061 struct tda18271_priv *priv = fe->tuner_priv;
1062 struct tda18271_std_map *std = &priv->std;
1064 if (!map)
1065 return -EINVAL;
1067 tda18271_update_std(fm_radio, "fm");
1068 tda18271_update_std(atv_b, "atv b");
1069 tda18271_update_std(atv_dk, "atv dk");
1070 tda18271_update_std(atv_gh, "atv gh");
1071 tda18271_update_std(atv_i, "atv i");
1072 tda18271_update_std(atv_l, "atv l");
1073 tda18271_update_std(atv_lc, "atv l'");
1074 tda18271_update_std(atv_mn, "atv mn");
1075 tda18271_update_std(atsc_6, "atsc 6");
1076 tda18271_update_std(dvbt_6, "dvbt 6");
1077 tda18271_update_std(dvbt_7, "dvbt 7");
1078 tda18271_update_std(dvbt_8, "dvbt 8");
1079 tda18271_update_std(qam_6, "qam 6");
1080 tda18271_update_std(qam_8, "qam 8");
1082 return 0;
1085 static int tda18271_get_id(struct dvb_frontend *fe)
1087 struct tda18271_priv *priv = fe->tuner_priv;
1088 unsigned char *regs = priv->tda18271_regs;
1089 char *name;
1090 int ret = 0;
1092 mutex_lock(&priv->lock);
1093 tda18271_read_regs(fe);
1094 mutex_unlock(&priv->lock);
1096 switch (regs[R_ID] & 0x7f) {
1097 case 3:
1098 name = "TDA18271HD/C1";
1099 priv->id = TDA18271HDC1;
1100 break;
1101 case 4:
1102 name = "TDA18271HD/C2";
1103 priv->id = TDA18271HDC2;
1104 break;
1105 default:
1106 name = "Unknown device";
1107 ret = -EINVAL;
1108 break;
1111 tda_info("%s detected @ %d-%04x%s\n", name,
1112 i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
1113 (0 == ret) ? "" : ", device not supported.");
1115 return ret;
1118 static struct dvb_tuner_ops tda18271_tuner_ops = {
1119 .info = {
1120 .name = "NXP TDA18271HD",
1121 .frequency_min = 45000000,
1122 .frequency_max = 864000000,
1123 .frequency_step = 62500
1125 .init = tda18271_init,
1126 .sleep = tda18271_sleep,
1127 .set_params = tda18271_set_params,
1128 .set_analog_params = tda18271_set_analog_params,
1129 .release = tda18271_release,
1130 .get_frequency = tda18271_get_frequency,
1131 .get_bandwidth = tda18271_get_bandwidth,
1134 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1135 struct i2c_adapter *i2c,
1136 struct tda18271_config *cfg)
1138 struct tda18271_priv *priv = NULL;
1139 int state_found = 0;
1141 mutex_lock(&tda18271_list_mutex);
1143 list_for_each_entry(priv, &tda18271_list, tda18271_list) {
1144 if ((i2c_adapter_id(priv->i2c_adap) == i2c_adapter_id(i2c)) &&
1145 (priv->i2c_addr == addr)) {
1146 tda_dbg("attaching existing tuner @ %d-%04x\n",
1147 i2c_adapter_id(priv->i2c_adap),
1148 priv->i2c_addr);
1149 priv->count++;
1150 fe->tuner_priv = priv;
1151 state_found = 1;
1152 /* allow dvb driver to override i2c gate setting */
1153 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1154 priv->gate = cfg->gate;
1155 break;
1158 if (state_found == 0) {
1159 tda_dbg("creating new tuner instance @ %d-%04x\n",
1160 i2c_adapter_id(i2c), addr);
1162 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
1163 if (priv == NULL) {
1164 mutex_unlock(&tda18271_list_mutex);
1165 return NULL;
1168 priv->i2c_addr = addr;
1169 priv->i2c_adap = i2c;
1170 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1171 priv->cal_initialized = false;
1172 mutex_init(&priv->lock);
1173 priv->count++;
1175 fe->tuner_priv = priv;
1177 list_add_tail(&priv->tda18271_list, &tda18271_list);
1179 if (tda18271_get_id(fe) < 0)
1180 goto fail;
1182 if (tda18271_assign_map_layout(fe) < 0)
1183 goto fail;
1185 mutex_lock(&priv->lock);
1186 tda18271_init_regs(fe);
1188 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1189 tda18271_rf_cal_init(fe);
1191 mutex_unlock(&priv->lock);
1194 /* override default std map with values in config struct */
1195 if ((cfg) && (cfg->std_map))
1196 tda18271_update_std_map(fe, cfg->std_map);
1198 mutex_unlock(&tda18271_list_mutex);
1200 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1201 sizeof(struct dvb_tuner_ops));
1203 if (tda18271_debug & DBG_MAP)
1204 tda18271_dump_std_map(fe);
1206 return fe;
1207 fail:
1208 mutex_unlock(&tda18271_list_mutex);
1210 tda18271_release(fe);
1211 return NULL;
1213 EXPORT_SYMBOL_GPL(tda18271_attach);
1214 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1215 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1216 MODULE_LICENSE("GPL");
1217 MODULE_VERSION("0.2");
1220 * Overrides for Emacs so that we follow Linus's tabbing style.
1221 * ---------------------------------------------------------------------------
1222 * Local variables:
1223 * c-basic-offset: 8
1224 * End: