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"
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) ?
45 TDA18271_MAIN_PLL, force);
48 static int tda18271_channel_configuration(struct dvb_frontend *fe,
49 struct tda18271_std_map_item *map,
52 struct tda18271_priv *priv = fe->tuner_priv;
53 unsigned char *regs = priv->tda18271_regs;
57 /* update TV broadcast parameters */
60 regs[R_EP3] &= ~0x1f; /* clear std bits */
61 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
63 /* set rfagc to high speed mode */
66 /* set cal mode to normal */
69 /* update IF output level & IF notch frequency */
70 regs[R_EP4] &= ~0x1c; /* clear if level bits */
71 regs[R_EP4] |= (map->if_lvl << 2);
75 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
77 case TDA18271_DIGITAL:
78 regs[R_MPD] |= 0x80; /* IF notch = 1 */
84 regs[R_EP4] |= map->fm_rfn << 7;
86 /* update rf top / if top */
88 regs[R_EB22] |= map->rfagc_top;
89 ret = tda18271_write_regs(fe, R_EB22, 1);
93 /* --------------------------------------------------------------- */
95 /* disable Power Level Indicator */
98 /* frequency dependent parameters */
100 tda18271_calc_ir_measure(fe, &freq);
102 tda18271_calc_bp_filter(fe, &freq);
104 tda18271_calc_rf_band(fe, &freq);
106 tda18271_calc_gain_taper(fe, &freq);
108 /* --------------------------------------------------------------- */
110 /* dual tuner and agc1 extra configuration */
112 switch (priv->role) {
113 case TDA18271_MASTER:
114 regs[R_EB1] |= 0x04; /* main vco */
117 regs[R_EB1] &= ~0x04; /* cal vco */
121 /* agc1 always active */
122 regs[R_EB1] &= ~0x02;
124 /* agc1 has priority on agc2 */
125 regs[R_EB1] &= ~0x01;
127 ret = tda18271_write_regs(fe, R_EB1, 1);
131 /* --------------------------------------------------------------- */
133 N = map->if_freq * 1000 + freq;
135 switch (priv->role) {
136 case TDA18271_MASTER:
137 tda18271_calc_main_pll(fe, N);
138 tda18271_write_regs(fe, R_MPD, 4);
141 tda18271_calc_cal_pll(fe, N);
142 tda18271_write_regs(fe, R_CPD, 4);
144 regs[R_MPD] = regs[R_CPD] & 0x7f;
145 tda18271_write_regs(fe, R_MPD, 1);
149 ret = tda18271_write_regs(fe, R_TM, 7);
153 /* force charge pump source */
154 charge_pump_source(fe, 1);
158 /* return pll to normal operation */
159 charge_pump_source(fe, 0);
163 /* set rfagc to normal speed mode */
165 regs[R_EP3] &= ~0x04;
168 ret = tda18271_write_regs(fe, R_EP3, 1);
173 static int tda18271_read_thermometer(struct dvb_frontend *fe)
175 struct tda18271_priv *priv = fe->tuner_priv;
176 unsigned char *regs = priv->tda18271_regs;
179 /* switch thermometer on */
181 tda18271_write_regs(fe, R_TM, 1);
183 /* read thermometer info */
184 tda18271_read_regs(fe);
186 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
187 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
189 if ((regs[R_TM] & 0x20) == 0x20)
194 tda18271_write_regs(fe, R_TM, 1);
196 msleep(10); /* temperature sensing */
198 /* read thermometer info */
199 tda18271_read_regs(fe);
202 tm = tda18271_lookup_thermometer(fe);
204 /* switch thermometer off */
206 tda18271_write_regs(fe, R_TM, 1);
208 /* set CAL mode to normal */
209 regs[R_EP4] &= ~0x03;
210 tda18271_write_regs(fe, R_EP4, 1);
215 /* ------------------------------------------------------------------ */
217 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
220 struct tda18271_priv *priv = fe->tuner_priv;
221 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
222 unsigned char *regs = priv->tda18271_regs;
223 int tm_current, rfcal_comp, approx, i, ret;
224 u8 dc_over_dt, rf_tab;
227 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
231 /* read die current temperature */
232 tm_current = tda18271_read_thermometer(fe);
234 /* frequency dependent parameters */
236 tda18271_calc_rf_cal(fe, &freq);
237 rf_tab = regs[R_EB14];
239 i = tda18271_lookup_rf_band(fe, &freq, NULL);
243 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
244 approx = map[i].rf_a1 *
245 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
247 approx = map[i].rf_a2 *
248 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
256 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
258 /* calculate temperature compensation */
259 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
261 regs[R_EB14] = approx + rfcal_comp;
262 ret = tda18271_write_regs(fe, R_EB14, 1);
267 static int tda18271_por(struct dvb_frontend *fe)
269 struct tda18271_priv *priv = fe->tuner_priv;
270 unsigned char *regs = priv->tda18271_regs;
273 /* power up detector 1 */
274 regs[R_EB12] &= ~0x20;
275 ret = tda18271_write_regs(fe, R_EB12, 1);
279 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
280 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
281 ret = tda18271_write_regs(fe, R_EB18, 1);
285 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
288 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
292 /* disable 1.5 MHz low pass filter */
293 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
294 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
295 ret = tda18271_write_regs(fe, R_EB21, 3);
300 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
302 struct tda18271_priv *priv = fe->tuner_priv;
303 unsigned char *regs = priv->tda18271_regs;
306 /* set CAL mode to normal */
307 regs[R_EP4] &= ~0x03;
308 tda18271_write_regs(fe, R_EP4, 1);
310 /* switch off agc1 */
311 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
313 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
314 tda18271_write_regs(fe, R_EB18, 1);
316 /* frequency dependent parameters */
318 tda18271_calc_bp_filter(fe, &freq);
319 tda18271_calc_gain_taper(fe, &freq);
320 tda18271_calc_rf_band(fe, &freq);
321 tda18271_calc_km(fe, &freq);
323 tda18271_write_regs(fe, R_EP1, 3);
324 tda18271_write_regs(fe, R_EB13, 1);
326 /* main pll charge pump source */
327 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
329 /* cal pll charge pump source */
330 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
332 /* force dcdc converter to 0 V */
334 tda18271_write_regs(fe, R_EB14, 1);
336 /* disable plls lock */
337 regs[R_EB20] &= ~0x20;
338 tda18271_write_regs(fe, R_EB20, 1);
340 /* set CAL mode to RF tracking filter calibration */
342 tda18271_write_regs(fe, R_EP4, 2);
344 /* --------------------------------------------------------------- */
346 /* set the internal calibration signal */
349 tda18271_calc_cal_pll(fe, N);
350 tda18271_write_regs(fe, R_CPD, 4);
352 /* downconvert internal calibration */
355 tda18271_calc_main_pll(fe, N);
356 tda18271_write_regs(fe, R_MPD, 4);
360 tda18271_write_regs(fe, R_EP2, 1);
361 tda18271_write_regs(fe, R_EP1, 1);
362 tda18271_write_regs(fe, R_EP2, 1);
363 tda18271_write_regs(fe, R_EP1, 1);
365 /* --------------------------------------------------------------- */
367 /* normal operation for the main pll */
368 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
370 /* normal operation for the cal pll */
371 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
373 msleep(10); /* plls locking */
375 /* launch the rf tracking filters calibration */
376 regs[R_EB20] |= 0x20;
377 tda18271_write_regs(fe, R_EB20, 1);
379 msleep(60); /* calibration */
381 /* --------------------------------------------------------------- */
383 /* set CAL mode to normal */
384 regs[R_EP4] &= ~0x03;
387 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
389 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
390 tda18271_write_regs(fe, R_EB18, 1);
392 tda18271_write_regs(fe, R_EP3, 2);
394 /* synchronization */
395 tda18271_write_regs(fe, R_EP1, 1);
397 /* get calibration result */
398 tda18271_read_extended(fe);
403 static int tda18271_powerscan(struct dvb_frontend *fe,
404 u32 *freq_in, u32 *freq_out)
406 struct tda18271_priv *priv = fe->tuner_priv;
407 unsigned char *regs = priv->tda18271_regs;
408 int sgn, bcal, count, wait, ret;
415 tda18271_calc_rf_band(fe, &freq);
416 tda18271_calc_rf_cal(fe, &freq);
417 tda18271_calc_gain_taper(fe, &freq);
418 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
420 tda18271_write_regs(fe, R_EP2, 1);
421 tda18271_write_regs(fe, R_EB14, 1);
423 /* downconvert frequency */
426 tda18271_calc_main_pll(fe, freq);
427 tda18271_write_regs(fe, R_MPD, 4);
429 msleep(5); /* pll locking */
432 regs[R_EP4] &= ~0x03;
434 tda18271_write_regs(fe, R_EP4, 1);
436 /* launch power detection measurement */
437 tda18271_write_regs(fe, R_EP2, 1);
439 /* read power detection info, stored in EB10 */
440 ret = tda18271_read_extended(fe);
444 /* algorithm initialization */
446 *freq_out = *freq_in;
451 while ((regs[R_EB10] & 0x3f) < cid_target) {
452 /* downconvert updated freq to 1 MHz */
453 freq = *freq_in + (sgn * count) + 1000000;
455 tda18271_calc_main_pll(fe, freq);
456 tda18271_write_regs(fe, R_MPD, 4);
459 msleep(5); /* pll locking */
462 udelay(100); /* pll locking */
464 /* launch power detection measurement */
465 tda18271_write_regs(fe, R_EP2, 1);
467 /* read power detection info, stored in EB10 */
468 ret = tda18271_read_extended(fe);
474 if (count <= count_limit)
485 if ((regs[R_EB10] & 0x3f) >= cid_target) {
487 *freq_out = freq - 1000000;
491 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
492 bcal, *freq_in, *freq_out, freq);
497 static int tda18271_powerscan_init(struct dvb_frontend *fe)
499 struct tda18271_priv *priv = fe->tuner_priv;
500 unsigned char *regs = priv->tda18271_regs;
503 /* set standard to digital */
504 regs[R_EP3] &= ~0x1f; /* clear std bits */
507 /* set cal mode to normal */
508 regs[R_EP4] &= ~0x03;
510 /* update IF output level & IF notch frequency */
511 regs[R_EP4] &= ~0x1c; /* clear if level bits */
513 ret = tda18271_write_regs(fe, R_EP3, 2);
517 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
518 ret = tda18271_write_regs(fe, R_EB18, 1);
522 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
524 /* 1.5 MHz low pass filter */
525 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
526 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
528 ret = tda18271_write_regs(fe, R_EB21, 3);
533 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
535 struct tda18271_priv *priv = fe->tuner_priv;
536 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
537 unsigned char *regs = priv->tda18271_regs;
547 i = tda18271_lookup_rf_band(fe, &freq, NULL);
552 rf_default[RF1] = 1000 * map[i].rf1_def;
553 rf_default[RF2] = 1000 * map[i].rf2_def;
554 rf_default[RF3] = 1000 * map[i].rf3_def;
556 for (rf = RF1; rf <= RF3; rf++) {
557 if (0 == rf_default[rf])
559 tda_cal("freq = %d, rf = %d\n", freq, rf);
561 /* look for optimized calibration frequency */
562 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
566 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
567 prog_tab[rf] = regs[R_EB14];
570 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
572 prog_cal[rf] = prog_tab[rf];
577 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
578 map[i].rf1 = rf_freq[RF1] / 1000;
581 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
582 prog_cal[RF1] + prog_tab[RF1]) /
583 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
584 map[i].rf2 = rf_freq[RF2] / 1000;
587 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
588 prog_cal[RF2] + prog_tab[RF2]) /
589 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
590 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
591 map[i].rf3 = rf_freq[RF3] / 1000;
601 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
603 struct tda18271_priv *priv = fe->tuner_priv;
607 tda_info("tda18271: performing RF tracking filter calibration\n");
609 /* wait for die temperature stabilization */
612 ret = tda18271_powerscan_init(fe);
616 /* rf band calibration */
617 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
619 tda18271_rf_tracking_filters_init(fe, 1000 *
620 priv->rf_cal_state[i].rfmax);
625 priv->tm_rfcal = tda18271_read_thermometer(fe);
630 /* ------------------------------------------------------------------ */
632 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
634 struct tda18271_priv *priv = fe->tuner_priv;
635 unsigned char *regs = priv->tda18271_regs;
638 /* test RF_CAL_OK to see if we need init */
639 if ((regs[R_EP1] & 0x10) == 0)
640 priv->cal_initialized = false;
642 if (priv->cal_initialized)
645 ret = tda18271_calc_rf_filter_curve(fe);
649 ret = tda18271_por(fe);
653 tda_info("tda18271: RF tracking filter calibration complete\n");
655 priv->cal_initialized = true;
658 tda_info("tda18271: RF tracking filter calibration failed!\n");
663 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
666 struct tda18271_priv *priv = fe->tuner_priv;
667 unsigned char *regs = priv->tda18271_regs;
671 /* calculate bp filter */
672 tda18271_calc_bp_filter(fe, &freq);
673 tda18271_write_regs(fe, R_EP1, 1);
677 tda18271_write_regs(fe, R_EB4, 1);
680 tda18271_write_regs(fe, R_EB7, 1);
683 tda18271_write_regs(fe, R_EB14, 1);
686 tda18271_write_regs(fe, R_EB20, 1);
688 /* set cal mode to RF tracking filter calibration */
691 /* calculate cal pll */
693 switch (priv->mode) {
694 case TDA18271_ANALOG:
697 case TDA18271_DIGITAL:
702 tda18271_calc_cal_pll(fe, N);
704 /* calculate main pll */
706 switch (priv->mode) {
707 case TDA18271_ANALOG:
710 case TDA18271_DIGITAL:
711 N = freq + bw / 2 + 1000000;
715 tda18271_calc_main_pll(fe, N);
717 ret = tda18271_write_regs(fe, R_EP3, 11);
721 msleep(5); /* RF tracking filter calibration initialization */
723 /* search for K,M,CO for RF calibration */
724 tda18271_calc_km(fe, &freq);
725 tda18271_write_regs(fe, R_EB13, 1);
727 /* search for rf band */
728 tda18271_calc_rf_band(fe, &freq);
730 /* search for gain taper */
731 tda18271_calc_gain_taper(fe, &freq);
733 tda18271_write_regs(fe, R_EP2, 1);
734 tda18271_write_regs(fe, R_EP1, 1);
735 tda18271_write_regs(fe, R_EP2, 1);
736 tda18271_write_regs(fe, R_EP1, 1);
740 tda18271_write_regs(fe, R_EB4, 1);
743 tda18271_write_regs(fe, R_EB7, 1);
744 msleep(10); /* pll locking */
747 tda18271_write_regs(fe, R_EB20, 1);
748 msleep(60); /* RF tracking filter calibration completion */
750 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
751 tda18271_write_regs(fe, R_EP4, 1);
753 tda18271_write_regs(fe, R_EP1, 1);
755 /* RF tracking filter correction for VHF_Low band */
756 if (0 == tda18271_calc_rf_cal(fe, &freq))
757 tda18271_write_regs(fe, R_EB14, 1);
762 /* ------------------------------------------------------------------ */
764 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
766 struct tda18271_priv *priv = fe->tuner_priv;
767 unsigned char *regs = priv->tda18271_regs;
770 ret = tda18271_read_regs(fe);
774 /* test IR_CAL_OK to see if we need init */
775 if ((regs[R_EP1] & 0x08) == 0)
776 ret = tda18271_init_regs(fe);
781 static int tda18271_init(struct dvb_frontend *fe)
783 struct tda18271_priv *priv = fe->tuner_priv;
786 mutex_lock(&priv->lock);
789 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
794 ret = tda18271_ir_cal_init(fe);
798 if (priv->id == TDA18271HDC2)
799 tda18271c2_rf_cal_init(fe);
801 mutex_unlock(&priv->lock);
806 static int tda18271_tune(struct dvb_frontend *fe,
807 struct tda18271_std_map_item *map, u32 freq, u32 bw)
809 struct tda18271_priv *priv = fe->tuner_priv;
812 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
813 freq, map->if_freq, bw, map->agc_mode, map->std);
815 ret = tda18271_init(fe);
819 mutex_lock(&priv->lock);
823 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
826 tda18271c2_rf_tracking_filters_correction(fe, freq);
829 ret = tda18271_channel_configuration(fe, map, freq, bw);
831 mutex_unlock(&priv->lock);
836 /* ------------------------------------------------------------------ */
838 static int tda18271_set_params(struct dvb_frontend *fe,
839 struct dvb_frontend_parameters *params)
841 struct tda18271_priv *priv = fe->tuner_priv;
842 struct tda18271_std_map *std_map = &priv->std;
843 struct tda18271_std_map_item *map;
845 u32 bw, freq = params->frequency;
847 priv->mode = TDA18271_DIGITAL;
849 if (fe->ops.info.type == FE_ATSC) {
850 switch (params->u.vsb.modulation) {
853 map = &std_map->atsc_6;
857 map = &std_map->qam_6;
860 tda_warn("modulation not set!\n");
864 /* userspace request is already center adjusted */
865 freq += 1750000; /* Adjust to center (+1.75MHZ) */
868 } else if (fe->ops.info.type == FE_OFDM) {
869 switch (params->u.ofdm.bandwidth) {
870 case BANDWIDTH_6_MHZ:
872 map = &std_map->dvbt_6;
874 case BANDWIDTH_7_MHZ:
876 map = &std_map->dvbt_7;
878 case BANDWIDTH_8_MHZ:
880 map = &std_map->dvbt_8;
883 tda_warn("bandwidth not set!\n");
887 tda_warn("modulation type not supported!\n");
891 /* When tuning digital, the analog demod must be tri-stated */
892 if (fe->ops.analog_ops.standby)
893 fe->ops.analog_ops.standby(fe);
895 ret = tda18271_tune(fe, map, freq, bw);
900 priv->frequency = freq;
901 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
902 params->u.ofdm.bandwidth : 0;
907 static int tda18271_set_analog_params(struct dvb_frontend *fe,
908 struct analog_parameters *params)
910 struct tda18271_priv *priv = fe->tuner_priv;
911 struct tda18271_std_map *std_map = &priv->std;
912 struct tda18271_std_map_item *map;
915 u32 freq = params->frequency * 62500;
917 priv->mode = TDA18271_ANALOG;
919 if (params->mode == V4L2_TUNER_RADIO) {
921 map = &std_map->fm_radio;
923 } else if (params->std & V4L2_STD_MN) {
924 map = &std_map->atv_mn;
926 } else if (params->std & V4L2_STD_B) {
927 map = &std_map->atv_b;
929 } else if (params->std & V4L2_STD_GH) {
930 map = &std_map->atv_gh;
932 } else if (params->std & V4L2_STD_PAL_I) {
933 map = &std_map->atv_i;
935 } else if (params->std & V4L2_STD_DK) {
936 map = &std_map->atv_dk;
938 } else if (params->std & V4L2_STD_SECAM_L) {
939 map = &std_map->atv_l;
941 } else if (params->std & V4L2_STD_SECAM_LC) {
942 map = &std_map->atv_lc;
945 map = &std_map->atv_i;
949 tda_dbg("setting tda18271 to system %s\n", mode);
951 ret = tda18271_tune(fe, map, freq, 0);
956 priv->frequency = freq;
962 static int tda18271_sleep(struct dvb_frontend *fe)
964 struct tda18271_priv *priv = fe->tuner_priv;
967 mutex_lock(&priv->lock);
969 /* standby mode w/ slave tuner output
970 * & loop thru & xtal oscillator on */
971 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
973 mutex_unlock(&priv->lock);
978 static int tda18271_release(struct dvb_frontend *fe)
980 struct tda18271_priv *priv = fe->tuner_priv;
982 mutex_lock(&tda18271_list_mutex);
985 hybrid_tuner_release_state(priv);
987 mutex_unlock(&tda18271_list_mutex);
989 fe->tuner_priv = NULL;
994 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
996 struct tda18271_priv *priv = fe->tuner_priv;
997 *frequency = priv->frequency;
1001 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1003 struct tda18271_priv *priv = fe->tuner_priv;
1004 *bandwidth = priv->bandwidth;
1008 /* ------------------------------------------------------------------ */
1010 #define tda18271_update_std(std_cfg, name) do { \
1011 if (map->std_cfg.if_freq + \
1012 map->std_cfg.agc_mode + map->std_cfg.std + \
1013 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1014 tda_dbg("Using custom std config for %s\n", name); \
1015 memcpy(&std->std_cfg, &map->std_cfg, \
1016 sizeof(struct tda18271_std_map_item)); \
1019 #define tda18271_dump_std_item(std_cfg, name) do { \
1020 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1021 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1022 name, std->std_cfg.if_freq, \
1023 std->std_cfg.agc_mode, std->std_cfg.std, \
1024 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1027 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1029 struct tda18271_priv *priv = fe->tuner_priv;
1030 struct tda18271_std_map *std = &priv->std;
1032 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1033 tda18271_dump_std_item(fm_radio, " fm ");
1034 tda18271_dump_std_item(atv_b, "atv b ");
1035 tda18271_dump_std_item(atv_dk, "atv dk");
1036 tda18271_dump_std_item(atv_gh, "atv gh");
1037 tda18271_dump_std_item(atv_i, "atv i ");
1038 tda18271_dump_std_item(atv_l, "atv l ");
1039 tda18271_dump_std_item(atv_lc, "atv l'");
1040 tda18271_dump_std_item(atv_mn, "atv mn");
1041 tda18271_dump_std_item(atsc_6, "atsc 6");
1042 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1043 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1044 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1045 tda18271_dump_std_item(qam_6, "qam 6 ");
1046 tda18271_dump_std_item(qam_8, "qam 8 ");
1051 static int tda18271_update_std_map(struct dvb_frontend *fe,
1052 struct tda18271_std_map *map)
1054 struct tda18271_priv *priv = fe->tuner_priv;
1055 struct tda18271_std_map *std = &priv->std;
1060 tda18271_update_std(fm_radio, "fm");
1061 tda18271_update_std(atv_b, "atv b");
1062 tda18271_update_std(atv_dk, "atv dk");
1063 tda18271_update_std(atv_gh, "atv gh");
1064 tda18271_update_std(atv_i, "atv i");
1065 tda18271_update_std(atv_l, "atv l");
1066 tda18271_update_std(atv_lc, "atv l'");
1067 tda18271_update_std(atv_mn, "atv mn");
1068 tda18271_update_std(atsc_6, "atsc 6");
1069 tda18271_update_std(dvbt_6, "dvbt 6");
1070 tda18271_update_std(dvbt_7, "dvbt 7");
1071 tda18271_update_std(dvbt_8, "dvbt 8");
1072 tda18271_update_std(qam_6, "qam 6");
1073 tda18271_update_std(qam_8, "qam 8");
1078 static int tda18271_get_id(struct dvb_frontend *fe)
1080 struct tda18271_priv *priv = fe->tuner_priv;
1081 unsigned char *regs = priv->tda18271_regs;
1085 mutex_lock(&priv->lock);
1086 tda18271_read_regs(fe);
1087 mutex_unlock(&priv->lock);
1089 switch (regs[R_ID] & 0x7f) {
1091 name = "TDA18271HD/C1";
1092 priv->id = TDA18271HDC1;
1095 name = "TDA18271HD/C2";
1096 priv->id = TDA18271HDC2;
1099 name = "Unknown device";
1104 tda_info("%s detected @ %d-%04x%s\n", name,
1105 i2c_adapter_id(priv->i2c_props.adap),
1106 priv->i2c_props.addr,
1107 (0 == ret) ? "" : ", device not supported.");
1112 static struct dvb_tuner_ops tda18271_tuner_ops = {
1114 .name = "NXP TDA18271HD",
1115 .frequency_min = 45000000,
1116 .frequency_max = 864000000,
1117 .frequency_step = 62500
1119 .init = tda18271_init,
1120 .sleep = tda18271_sleep,
1121 .set_params = tda18271_set_params,
1122 .set_analog_params = tda18271_set_analog_params,
1123 .release = tda18271_release,
1124 .get_frequency = tda18271_get_frequency,
1125 .get_bandwidth = tda18271_get_bandwidth,
1128 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1129 struct i2c_adapter *i2c,
1130 struct tda18271_config *cfg)
1132 struct tda18271_priv *priv = NULL;
1135 mutex_lock(&tda18271_list_mutex);
1137 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1138 hybrid_tuner_instance_list,
1139 i2c, addr, "tda18271");
1145 /* new tuner instance */
1146 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1147 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1148 priv->cal_initialized = false;
1149 mutex_init(&priv->lock);
1151 fe->tuner_priv = priv;
1154 priv->small_i2c = cfg->small_i2c;
1156 if (tda_fail(tda18271_get_id(fe)))
1159 if (tda_fail(tda18271_assign_map_layout(fe)))
1162 mutex_lock(&priv->lock);
1163 tda18271_init_regs(fe);
1165 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1166 tda18271c2_rf_cal_init(fe);
1168 mutex_unlock(&priv->lock);
1171 /* existing tuner instance */
1172 fe->tuner_priv = priv;
1174 /* allow dvb driver to override i2c gate setting */
1175 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1176 priv->gate = cfg->gate;
1180 /* override default std map with values in config struct */
1181 if ((cfg) && (cfg->std_map))
1182 tda18271_update_std_map(fe, cfg->std_map);
1184 mutex_unlock(&tda18271_list_mutex);
1186 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1187 sizeof(struct dvb_tuner_ops));
1189 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1190 tda18271_dump_std_map(fe);
1194 mutex_unlock(&tda18271_list_mutex);
1196 tda18271_release(fe);
1199 EXPORT_SYMBOL_GPL(tda18271_attach);
1200 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1201 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1202 MODULE_LICENSE("GPL");
1203 MODULE_VERSION("0.3");
1206 * Overrides for Emacs so that we follow Linus's tabbing style.
1207 * ---------------------------------------------------------------------------