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 LIST_HEAD(tda18271_list);
31 static DEFINE_MUTEX(tda18271_list_mutex);
33 /*---------------------------------------------------------------------*/
35 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
37 struct tda18271_priv *priv = fe->tuner_priv;
38 unsigned char *regs = priv->tda18271_regs;
40 tda18271_read_regs(fe);
42 /* test IR_CAL_OK to see if we need init */
43 if ((regs[R_EP1] & 0x08) == 0)
44 tda18271_init_regs(fe);
49 /* ------------------------------------------------------------------ */
51 static int tda18271_channel_configuration(struct dvb_frontend *fe,
52 u32 ifc, u32 freq, u32 bw, u8 std,
55 struct tda18271_priv *priv = fe->tuner_priv;
56 unsigned char *regs = priv->tda18271_regs;
59 /* update TV broadcast parameters */
62 regs[R_EP3] &= ~0x1f; /* clear std bits */
65 /* set cal mode to normal */
68 /* update IF output level & IF notch frequency */
69 regs[R_EP4] &= ~0x1c; /* clear if level bits */
73 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
75 case TDA18271_DIGITAL:
76 regs[R_EP4] |= 0x04; /* IF level = 1 */
77 regs[R_MPD] |= 0x80; /* IF notch = 1 */
86 /* update RF_TOP / IF_TOP */
91 case TDA18271_DIGITAL:
95 tda18271_write_regs(fe, R_EB22, 1);
97 /* --------------------------------------------------------------- */
99 /* disable Power Level Indicator */
102 /* frequency dependent parameters */
104 tda18271_calc_ir_measure(fe, &freq);
106 tda18271_calc_bp_filter(fe, &freq);
108 tda18271_calc_rf_band(fe, &freq);
110 tda18271_calc_gain_taper(fe, &freq);
112 /* --------------------------------------------------------------- */
114 /* dual tuner and agc1 extra configuration */
116 /* main vco when Master, cal vco when slave */
117 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
119 /* agc1 always active */
120 regs[R_EB1] &= ~0x02;
122 /* agc1 has priority on agc2 */
123 regs[R_EB1] &= ~0x01;
125 tda18271_write_regs(fe, R_EB1, 1);
127 /* --------------------------------------------------------------- */
131 /* FIXME: assumes master */
132 tda18271_calc_main_pll(fe, N);
133 tda18271_write_regs(fe, R_MPD, 4);
135 tda18271_write_regs(fe, R_TM, 7);
137 /* main pll charge pump source */
139 tda18271_write_regs(fe, R_EB4, 1);
143 /* normal operation for the main pll */
144 regs[R_EB4] &= ~0x20;
145 tda18271_write_regs(fe, R_EB4, 1);
152 static int tda18271_read_thermometer(struct dvb_frontend *fe)
154 struct tda18271_priv *priv = fe->tuner_priv;
155 unsigned char *regs = priv->tda18271_regs;
158 /* switch thermometer on */
160 tda18271_write_regs(fe, R_TM, 1);
162 /* read thermometer info */
163 tda18271_read_regs(fe);
165 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
166 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
168 if ((regs[R_TM] & 0x20) == 0x20)
173 tda18271_write_regs(fe, R_TM, 1);
175 msleep(10); /* temperature sensing */
177 /* read thermometer info */
178 tda18271_read_regs(fe);
181 tm = tda18271_lookup_thermometer(fe);
183 /* switch thermometer off */
185 tda18271_write_regs(fe, R_TM, 1);
187 /* set CAL mode to normal */
188 regs[R_EP4] &= ~0x03;
189 tda18271_write_regs(fe, R_EP4, 1);
194 static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe,
197 struct tda18271_priv *priv = fe->tuner_priv;
198 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
199 unsigned char *regs = priv->tda18271_regs;
200 int tm_current, rfcal_comp, approx, i;
201 u8 dc_over_dt, rf_tab;
204 tda18271_set_standby_mode(fe, 0, 0, 0);
206 /* read die current temperature */
207 tm_current = tda18271_read_thermometer(fe);
209 /* frequency dependent parameters */
211 tda18271_calc_rf_cal(fe, &freq);
212 rf_tab = regs[R_EB14];
214 i = tda18271_lookup_rf_band(fe, &freq, NULL);
218 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
219 approx = map[i].rf_a1 *
220 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
222 approx = map[i].rf_a2 *
223 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
231 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
233 /* calculate temperature compensation */
234 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
236 regs[R_EB14] = approx + rfcal_comp;
237 tda18271_write_regs(fe, R_EB14, 1);
242 static int tda18271_por(struct dvb_frontend *fe)
244 struct tda18271_priv *priv = fe->tuner_priv;
245 unsigned char *regs = priv->tda18271_regs;
247 /* power up detector 1 */
248 regs[R_EB12] &= ~0x20;
249 tda18271_write_regs(fe, R_EB12, 1);
251 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
252 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
253 tda18271_write_regs(fe, R_EB18, 1);
255 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
258 tda18271_set_standby_mode(fe, 1, 0, 0);
260 /* disable 1.5 MHz low pass filter */
261 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
262 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
263 tda18271_write_regs(fe, R_EB21, 3);
268 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
270 struct tda18271_priv *priv = fe->tuner_priv;
271 unsigned char *regs = priv->tda18271_regs;
274 /* set CAL mode to normal */
275 regs[R_EP4] &= ~0x03;
276 tda18271_write_regs(fe, R_EP4, 1);
278 /* switch off agc1 */
279 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
281 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
282 tda18271_write_regs(fe, R_EB18, 1);
284 /* frequency dependent parameters */
286 tda18271_calc_bp_filter(fe, &freq);
287 tda18271_calc_gain_taper(fe, &freq);
288 tda18271_calc_rf_band(fe, &freq);
289 tda18271_calc_km(fe, &freq);
291 tda18271_write_regs(fe, R_EP1, 3);
292 tda18271_write_regs(fe, R_EB13, 1);
294 /* main pll charge pump source */
296 tda18271_write_regs(fe, R_EB4, 1);
298 /* cal pll charge pump source */
300 tda18271_write_regs(fe, R_EB7, 1);
302 /* force dcdc converter to 0 V */
304 tda18271_write_regs(fe, R_EB14, 1);
306 /* disable plls lock */
307 regs[R_EB20] &= ~0x20;
308 tda18271_write_regs(fe, R_EB20, 1);
310 /* set CAL mode to RF tracking filter calibration */
312 tda18271_write_regs(fe, R_EP4, 2);
314 /* --------------------------------------------------------------- */
316 /* set the internal calibration signal */
319 tda18271_calc_main_pll(fe, N);
320 tda18271_write_regs(fe, R_MPD, 4);
322 /* downconvert internal calibration */
325 tda18271_calc_main_pll(fe, N);
326 tda18271_write_regs(fe, R_MPD, 4);
330 tda18271_write_regs(fe, R_EP2, 1);
331 tda18271_write_regs(fe, R_EP1, 1);
332 tda18271_write_regs(fe, R_EP2, 1);
333 tda18271_write_regs(fe, R_EP1, 1);
335 /* --------------------------------------------------------------- */
337 /* normal operation for the main pll */
338 regs[R_EB4] &= ~0x20;
339 tda18271_write_regs(fe, R_EB4, 1);
341 /* normal operation for the cal pll */
342 regs[R_EB7] &= ~0x20;
343 tda18271_write_regs(fe, R_EB7, 1);
345 msleep(5); /* plls locking */
347 /* launch the rf tracking filters calibration */
348 regs[R_EB20] |= 0x20;
349 tda18271_write_regs(fe, R_EB20, 1);
351 msleep(60); /* calibration */
353 /* --------------------------------------------------------------- */
355 /* set CAL mode to normal */
356 regs[R_EP4] &= ~0x03;
359 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
361 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
362 tda18271_write_regs(fe, R_EB18, 1);
364 tda18271_write_regs(fe, R_EP3, 2);
366 /* synchronization */
367 tda18271_write_regs(fe, R_EP1, 1);
369 /* get calibration result */
370 tda18271_read_extended(fe);
375 static int tda18271_powerscan(struct dvb_frontend *fe,
376 u32 *freq_in, u32 *freq_out)
378 struct tda18271_priv *priv = fe->tuner_priv;
379 unsigned char *regs = priv->tda18271_regs;
380 int sgn, bcal, count, wait;
387 tda18271_calc_rf_band(fe, &freq);
388 tda18271_calc_rf_cal(fe, &freq);
389 tda18271_calc_gain_taper(fe, &freq);
390 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
392 tda18271_write_regs(fe, R_EP2, 1);
393 tda18271_write_regs(fe, R_EB14, 1);
395 /* downconvert frequency */
398 tda18271_calc_main_pll(fe, freq);
399 tda18271_write_regs(fe, R_MPD, 4);
401 msleep(5); /* pll locking */
404 regs[R_EP4] &= ~0x03;
406 tda18271_write_regs(fe, R_EP4, 1);
408 /* launch power detection measurement */
409 tda18271_write_regs(fe, R_EP2, 1);
411 /* read power detection info, stored in EB10 */
412 tda18271_read_extended(fe);
414 /* algorithm initialization */
416 *freq_out = *freq_in;
421 while ((regs[R_EB10] & 0x3f) < cid_target) {
422 /* downconvert updated freq to 1 MHz */
423 freq = *freq_in + (sgn * count) + 1000000;
425 tda18271_calc_main_pll(fe, freq);
426 tda18271_write_regs(fe, R_MPD, 4);
429 msleep(5); /* pll locking */
432 udelay(100); /* pll locking */
434 /* launch power detection measurement */
435 tda18271_write_regs(fe, R_EP2, 1);
437 /* read power detection info, stored in EB10 */
438 tda18271_read_extended(fe);
442 if (count < count_limit)
453 if ((regs[R_EB10] & 0x3f) >= cid_target) {
455 *freq_out = freq - 1000000;
459 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
460 bcal, *freq_in, *freq_out, freq);
465 static int tda18271_powerscan_init(struct dvb_frontend *fe)
467 struct tda18271_priv *priv = fe->tuner_priv;
468 unsigned char *regs = priv->tda18271_regs;
470 /* set standard to digital */
471 regs[R_EP3] &= ~0x1f; /* clear std bits */
474 /* set cal mode to normal */
475 regs[R_EP4] &= ~0x03;
477 /* update IF output level & IF notch frequency */
478 regs[R_EP4] &= ~0x1c; /* clear if level bits */
480 tda18271_write_regs(fe, R_EP3, 2);
482 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
483 tda18271_write_regs(fe, R_EB18, 1);
485 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
487 /* 1.5 MHz low pass filter */
488 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
489 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
491 tda18271_write_regs(fe, R_EB21, 3);
496 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
498 struct tda18271_priv *priv = fe->tuner_priv;
499 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
500 unsigned char *regs = priv->tda18271_regs;
510 i = tda18271_lookup_rf_band(fe, &freq, NULL);
515 rf_default[RF1] = 1000 * map[i].rf1_def;
516 rf_default[RF2] = 1000 * map[i].rf2_def;
517 rf_default[RF3] = 1000 * map[i].rf3_def;
519 for (rf = RF1; rf <= RF3; rf++) {
520 if (0 == rf_default[rf])
522 tda_cal("freq = %d, rf = %d\n", freq, rf);
524 /* look for optimized calibration frequency */
525 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
527 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
528 prog_tab[rf] = regs[R_EB14];
531 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
533 prog_cal[rf] = prog_tab[rf];
538 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
539 map[i].rf1 = rf_freq[RF1] / 1000;
542 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
543 prog_cal[RF1] + prog_tab[RF1]) /
544 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
545 map[i].rf2 = rf_freq[RF2] / 1000;
548 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
549 prog_cal[RF2] + prog_tab[RF2]) /
550 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
551 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
552 map[i].rf3 = rf_freq[RF3] / 1000;
562 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
564 struct tda18271_priv *priv = fe->tuner_priv;
567 tda_info("tda18271: performing RF tracking filter calibration\n");
569 /* wait for die temperature stabilization */
572 tda18271_powerscan_init(fe);
574 /* rf band calibration */
575 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
576 tda18271_rf_tracking_filters_init(fe, 1000 *
577 priv->rf_cal_state[i].rfmax);
579 priv->tm_rfcal = tda18271_read_thermometer(fe);
584 /* ------------------------------------------------------------------ */
586 static int tda18271_rf_cal_init(struct dvb_frontend *fe)
588 struct tda18271_priv *priv = fe->tuner_priv;
590 if (priv->cal_initialized)
593 tda18271_calc_rf_filter_curve(fe);
597 tda_info("tda18271: RF tracking filter calibration complete\n");
599 priv->cal_initialized = true;
604 static int tda18271_init(struct dvb_frontend *fe)
606 struct tda18271_priv *priv = fe->tuner_priv;
608 mutex_lock(&priv->lock);
611 tda18271_set_standby_mode(fe, 0, 0, 0);
614 tda18271_ir_cal_init(fe);
616 if (priv->id == TDA18271HDC2)
617 tda18271_rf_cal_init(fe);
619 mutex_unlock(&priv->lock);
624 static int tda18271c2_tune(struct dvb_frontend *fe,
625 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
627 struct tda18271_priv *priv = fe->tuner_priv;
629 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
633 mutex_lock(&priv->lock);
635 tda18271_rf_tracking_filters_correction(fe, freq);
637 tda18271_channel_configuration(fe, ifc, freq, bw, std, radio);
639 mutex_unlock(&priv->lock);
644 /* ------------------------------------------------------------------ */
646 static int tda18271c1_tune(struct dvb_frontend *fe,
647 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
649 struct tda18271_priv *priv = fe->tuner_priv;
650 unsigned char *regs = priv->tda18271_regs;
655 mutex_lock(&priv->lock);
657 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
659 /* RF tracking filter calibration */
661 /* calculate bp filter */
662 tda18271_calc_bp_filter(fe, &freq);
663 tda18271_write_regs(fe, R_EP1, 1);
667 tda18271_write_regs(fe, R_EB4, 1);
670 tda18271_write_regs(fe, R_EB7, 1);
673 tda18271_write_regs(fe, R_EB14, 1);
676 tda18271_write_regs(fe, R_EB20, 1);
678 /* set cal mode to RF tracking filter calibration */
681 /* calculate cal pll */
683 switch (priv->mode) {
684 case TDA18271_ANALOG:
687 case TDA18271_DIGITAL:
692 tda18271_calc_cal_pll(fe, N);
694 /* calculate main pll */
696 switch (priv->mode) {
697 case TDA18271_ANALOG:
700 case TDA18271_DIGITAL:
701 N = freq + bw / 2 + 1000000;
705 tda18271_calc_main_pll(fe, N);
707 tda18271_write_regs(fe, R_EP3, 11);
708 msleep(5); /* RF tracking filter calibration initialization */
710 /* search for K,M,CO for RF calibration */
711 tda18271_calc_km(fe, &freq);
712 tda18271_write_regs(fe, R_EB13, 1);
714 /* search for rf band */
715 tda18271_calc_rf_band(fe, &freq);
717 /* search for gain taper */
718 tda18271_calc_gain_taper(fe, &freq);
720 tda18271_write_regs(fe, R_EP2, 1);
721 tda18271_write_regs(fe, R_EP1, 1);
722 tda18271_write_regs(fe, R_EP2, 1);
723 tda18271_write_regs(fe, R_EP1, 1);
727 tda18271_write_regs(fe, R_EB4, 1);
730 tda18271_write_regs(fe, R_EB7, 1);
734 tda18271_write_regs(fe, R_EB20, 1);
735 msleep(60); /* RF tracking filter calibration completion */
737 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
738 tda18271_write_regs(fe, R_EP4, 1);
740 tda18271_write_regs(fe, R_EP1, 1);
742 /* RF tracking filter correction for VHF_Low band */
743 if (0 == tda18271_calc_rf_cal(fe, &freq))
744 tda18271_write_regs(fe, R_EB14, 1);
746 /* Channel Configuration */
748 switch (priv->mode) {
749 case TDA18271_ANALOG:
752 case TDA18271_DIGITAL:
756 tda18271_write_regs(fe, R_EB22, 1);
758 regs[R_EP1] |= 0x40; /* set dis power level on */
761 regs[R_EP3] &= ~0x1f; /* clear std bits */
766 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
768 regs[R_EP4] &= ~0x1c; /* clear if level bits */
769 switch (priv->mode) {
770 case TDA18271_ANALOG:
771 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
773 case TDA18271_DIGITAL:
782 regs[R_EP4] &= ~0x80;
784 /* image rejection validity */
785 tda18271_calc_ir_measure(fe, &freq);
787 /* calculate MAIN PLL */
790 tda18271_calc_main_pll(fe, N);
792 tda18271_write_regs(fe, R_TM, 15);
794 mutex_unlock(&priv->lock);
799 static inline int tda18271_tune(struct dvb_frontend *fe,
800 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
802 struct tda18271_priv *priv = fe->tuner_priv;
807 ret = tda18271c1_tune(fe, ifc, freq, bw, std, radio);
810 ret = tda18271c2_tune(fe, ifc, freq, bw, std, radio);
816 /* ------------------------------------------------------------------ */
818 static int tda18271_set_params(struct dvb_frontend *fe,
819 struct dvb_frontend_parameters *params)
821 struct tda18271_priv *priv = fe->tuner_priv;
822 struct tda18271_std_map *std_map = &priv->std;
826 u32 bw, freq = params->frequency;
828 priv->mode = TDA18271_DIGITAL;
831 if (fe->ops.info.type == FE_ATSC) {
832 switch (params->u.vsb.modulation) {
835 std = std_map->atsc_6.std_bits;
836 sgIF = std_map->atsc_6.if_freq;
840 std = std_map->qam_6.std_bits;
841 sgIF = std_map->qam_6.if_freq;
844 tda_warn("modulation not set!\n");
848 /* userspace request is already center adjusted */
849 freq += 1750000; /* Adjust to center (+1.75MHZ) */
852 } else if (fe->ops.info.type == FE_OFDM) {
853 switch (params->u.ofdm.bandwidth) {
854 case BANDWIDTH_6_MHZ:
856 std = std_map->dvbt_6.std_bits;
857 sgIF = std_map->dvbt_6.if_freq;
859 case BANDWIDTH_7_MHZ:
861 std = std_map->dvbt_7.std_bits;
862 sgIF = std_map->dvbt_7.if_freq;
864 case BANDWIDTH_8_MHZ:
866 std = std_map->dvbt_8.std_bits;
867 sgIF = std_map->dvbt_8.if_freq;
870 tda_warn("bandwidth not set!\n");
874 tda_warn("modulation type not supported!\n");
878 ret = tda18271_tune(fe, sgIF * 1000, freq, bw, std, 0);
883 priv->frequency = freq;
884 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
885 params->u.ofdm.bandwidth : 0;
890 static int tda18271_set_analog_params(struct dvb_frontend *fe,
891 struct analog_parameters *params)
893 struct tda18271_priv *priv = fe->tuner_priv;
894 struct tda18271_std_map *std_map = &priv->std;
899 u32 freq = params->frequency * 62500;
901 priv->mode = TDA18271_ANALOG;
903 if (params->mode == V4L2_TUNER_RADIO) {
906 std = std_map->fm_radio.std_bits;
907 sgIF = std_map->fm_radio.if_freq;
909 } else if (params->std & V4L2_STD_MN) {
910 std = std_map->atv_mn.std_bits;
911 sgIF = std_map->atv_mn.if_freq;
913 } else if (params->std & V4L2_STD_B) {
914 std = std_map->atv_b.std_bits;
915 sgIF = std_map->atv_b.if_freq;
917 } else if (params->std & V4L2_STD_GH) {
918 std = std_map->atv_gh.std_bits;
919 sgIF = std_map->atv_gh.if_freq;
921 } else if (params->std & V4L2_STD_PAL_I) {
922 std = std_map->atv_i.std_bits;
923 sgIF = std_map->atv_i.if_freq;
925 } else if (params->std & V4L2_STD_DK) {
926 std = std_map->atv_dk.std_bits;
927 sgIF = std_map->atv_dk.if_freq;
929 } else if (params->std & V4L2_STD_SECAM_L) {
930 std = std_map->atv_l.std_bits;
931 sgIF = std_map->atv_l.if_freq;
933 } else if (params->std & V4L2_STD_SECAM_LC) {
934 std = std_map->atv_lc.std_bits;
935 sgIF = std_map->atv_lc.if_freq;
938 std = std_map->atv_i.std_bits;
939 sgIF = std_map->atv_i.if_freq;
943 tda_dbg("setting tda18271 to system %s\n", mode);
945 ret = tda18271_tune(fe, sgIF * 1000, freq, 0, std, radio);
950 priv->frequency = freq;
956 static int tda18271_sleep(struct dvb_frontend *fe)
958 struct tda18271_priv *priv = fe->tuner_priv;
960 mutex_lock(&priv->lock);
962 /* standby mode w/ slave tuner output
963 * & loop thru & xtal oscillator on */
964 tda18271_set_standby_mode(fe, 1, 0, 0);
966 mutex_unlock(&priv->lock);
971 static int tda18271_release(struct dvb_frontend *fe)
973 struct tda18271_priv *priv = fe->tuner_priv;
975 mutex_lock(&tda18271_list_mutex);
980 tda_dbg("destroying instance @ %d-%04x\n",
981 i2c_adapter_id(priv->i2c_adap),
983 list_del(&priv->tda18271_list);
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 + map->std_cfg.std_bits > 0) { \
1012 tda_dbg("Using custom std config for %s\n", name); \
1013 memcpy(&std->std_cfg, &map->std_cfg, \
1014 sizeof(struct tda18271_std_map_item)); \
1017 #define tda18271_dump_std_item(std_cfg, name) do { \
1018 tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \
1019 name, std->std_cfg.if_freq, std->std_cfg.std_bits); \
1022 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1024 struct tda18271_priv *priv = fe->tuner_priv;
1025 struct tda18271_std_map *std = &priv->std;
1027 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1028 tda18271_dump_std_item(fm_radio, "fm");
1029 tda18271_dump_std_item(atv_b, "pal b");
1030 tda18271_dump_std_item(atv_dk, "pal dk");
1031 tda18271_dump_std_item(atv_gh, "pal gh");
1032 tda18271_dump_std_item(atv_i, "pal i");
1033 tda18271_dump_std_item(atv_l, "pal l");
1034 tda18271_dump_std_item(atv_lc, "pal l'");
1035 tda18271_dump_std_item(atv_mn, "atv mn");
1036 tda18271_dump_std_item(atsc_6, "atsc 6");
1037 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1038 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1039 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1040 tda18271_dump_std_item(qam_6, "qam 6");
1041 tda18271_dump_std_item(qam_8, "qam 8");
1046 static int tda18271_update_std_map(struct dvb_frontend *fe,
1047 struct tda18271_std_map *map)
1049 struct tda18271_priv *priv = fe->tuner_priv;
1050 struct tda18271_std_map *std = &priv->std;
1055 tda18271_update_std(fm_radio, "fm");
1056 tda18271_update_std(atv_b, "atv b");
1057 tda18271_update_std(atv_dk, "atv dk");
1058 tda18271_update_std(atv_gh, "atv gh");
1059 tda18271_update_std(atv_i, "atv i");
1060 tda18271_update_std(atv_l, "atv l");
1061 tda18271_update_std(atv_lc, "atv l'");
1062 tda18271_update_std(atv_mn, "atv mn");
1063 tda18271_update_std(atsc_6, "atsc 6");
1064 tda18271_update_std(dvbt_6, "dvbt 6");
1065 tda18271_update_std(dvbt_7, "dvbt 7");
1066 tda18271_update_std(dvbt_8, "dvbt 8");
1067 tda18271_update_std(qam_6, "qam 6");
1068 tda18271_update_std(qam_8, "qam 8");
1073 static int tda18271_get_id(struct dvb_frontend *fe)
1075 struct tda18271_priv *priv = fe->tuner_priv;
1076 unsigned char *regs = priv->tda18271_regs;
1080 mutex_lock(&priv->lock);
1081 tda18271_read_regs(fe);
1082 mutex_unlock(&priv->lock);
1084 switch (regs[R_ID] & 0x7f) {
1086 name = "TDA18271HD/C1";
1087 priv->id = TDA18271HDC1;
1090 name = "TDA18271HD/C2";
1091 priv->id = TDA18271HDC2;
1094 name = "Unknown device";
1099 tda_info("%s detected @ %d-%04x%s\n", name,
1100 i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
1101 (0 == ret) ? "" : ", device not supported.");
1106 static struct dvb_tuner_ops tda18271_tuner_ops = {
1108 .name = "NXP TDA18271HD",
1109 .frequency_min = 45000000,
1110 .frequency_max = 864000000,
1111 .frequency_step = 62500
1113 .init = tda18271_init,
1114 .sleep = tda18271_sleep,
1115 .set_params = tda18271_set_params,
1116 .set_analog_params = tda18271_set_analog_params,
1117 .release = tda18271_release,
1118 .get_frequency = tda18271_get_frequency,
1119 .get_bandwidth = tda18271_get_bandwidth,
1122 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1123 struct i2c_adapter *i2c,
1124 struct tda18271_config *cfg)
1126 struct tda18271_priv *priv = NULL;
1127 int state_found = 0;
1129 mutex_lock(&tda18271_list_mutex);
1131 list_for_each_entry(priv, &tda18271_list, tda18271_list) {
1132 if ((i2c_adapter_id(priv->i2c_adap) == i2c_adapter_id(i2c)) &&
1133 (priv->i2c_addr == addr)) {
1134 tda_dbg("attaching existing tuner @ %d-%04x\n",
1135 i2c_adapter_id(priv->i2c_adap),
1138 fe->tuner_priv = priv;
1140 /* allow dvb driver to override i2c gate setting */
1141 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1142 priv->gate = cfg->gate;
1146 if (state_found == 0) {
1147 tda_dbg("creating new tuner instance @ %d-%04x\n",
1148 i2c_adapter_id(i2c), addr);
1150 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
1152 mutex_unlock(&tda18271_list_mutex);
1156 priv->i2c_addr = addr;
1157 priv->i2c_adap = i2c;
1158 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1159 priv->cal_initialized = false;
1160 mutex_init(&priv->lock);
1163 fe->tuner_priv = priv;
1165 list_add_tail(&priv->tda18271_list, &tda18271_list);
1167 if (tda18271_get_id(fe) < 0)
1170 if (tda18271_assign_map_layout(fe) < 0)
1173 mutex_lock(&priv->lock);
1174 tda18271_init_regs(fe);
1175 mutex_unlock(&priv->lock);
1178 /* override default std map with values in config struct */
1179 if ((cfg) && (cfg->std_map))
1180 tda18271_update_std_map(fe, cfg->std_map);
1182 mutex_unlock(&tda18271_list_mutex);
1184 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1185 sizeof(struct dvb_tuner_ops));
1187 if (tda18271_debug & DBG_MAP)
1188 tda18271_dump_std_map(fe);
1192 mutex_unlock(&tda18271_list_mutex);
1194 tda18271_release(fe);
1197 EXPORT_SYMBOL_GPL(tda18271_attach);
1198 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1199 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1200 MODULE_LICENSE("GPL");
1201 MODULE_VERSION("0.2");
1204 * Overrides for Emacs so that we follow Linus's tabbing style.
1205 * ---------------------------------------------------------------------------