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 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;
55 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
57 case TDA18271_DIGITAL:
58 regs[R_MPD] |= 0x80; /* IF notch = 1 */
63 static int tda18271_channel_configuration(struct dvb_frontend *fe,
64 struct tda18271_std_map_item *map,
67 struct tda18271_priv *priv = fe->tuner_priv;
68 unsigned char *regs = priv->tda18271_regs;
72 /* update TV broadcast parameters */
75 regs[R_EP3] &= ~0x1f; /* clear std bits */
76 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
78 /* set rfagc to high speed mode */
81 /* set cal mode to normal */
84 /* update IF output level */
85 regs[R_EP4] &= ~0x1c; /* clear if level bits */
86 regs[R_EP4] |= (map->if_lvl << 2);
90 regs[R_EP4] |= map->fm_rfn << 7;
92 /* update rf top / if top */
94 regs[R_EB22] |= map->rfagc_top;
95 ret = tda18271_write_regs(fe, R_EB22, 1);
99 /* --------------------------------------------------------------- */
101 /* disable Power Level Indicator */
104 /* frequency dependent parameters */
106 tda18271_calc_ir_measure(fe, &freq);
108 tda18271_calc_bp_filter(fe, &freq);
110 tda18271_calc_rf_band(fe, &freq);
112 tda18271_calc_gain_taper(fe, &freq);
114 /* --------------------------------------------------------------- */
116 /* dual tuner and agc1 extra configuration */
118 switch (priv->role) {
119 case TDA18271_MASTER:
120 regs[R_EB1] |= 0x04; /* main vco */
123 regs[R_EB1] &= ~0x04; /* cal vco */
127 /* agc1 always active */
128 regs[R_EB1] &= ~0x02;
130 /* agc1 has priority on agc2 */
131 regs[R_EB1] &= ~0x01;
133 ret = tda18271_write_regs(fe, R_EB1, 1);
137 /* --------------------------------------------------------------- */
139 N = map->if_freq * 1000 + freq;
141 switch (priv->role) {
142 case TDA18271_MASTER:
143 tda18271_calc_main_pll(fe, N);
144 tda18271_set_if_notch(fe);
145 tda18271_write_regs(fe, R_MPD, 4);
148 tda18271_calc_cal_pll(fe, N);
149 tda18271_write_regs(fe, R_CPD, 4);
151 regs[R_MPD] = regs[R_CPD] & 0x7f;
152 tda18271_set_if_notch(fe);
153 tda18271_write_regs(fe, R_MPD, 1);
157 ret = tda18271_write_regs(fe, R_TM, 7);
161 /* force charge pump source */
162 charge_pump_source(fe, 1);
166 /* return pll to normal operation */
167 charge_pump_source(fe, 0);
171 /* set rfagc to normal speed mode */
173 regs[R_EP3] &= ~0x04;
176 ret = tda18271_write_regs(fe, R_EP3, 1);
181 static int tda18271_read_thermometer(struct dvb_frontend *fe)
183 struct tda18271_priv *priv = fe->tuner_priv;
184 unsigned char *regs = priv->tda18271_regs;
187 /* switch thermometer on */
189 tda18271_write_regs(fe, R_TM, 1);
191 /* read thermometer info */
192 tda18271_read_regs(fe);
194 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
195 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
197 if ((regs[R_TM] & 0x20) == 0x20)
202 tda18271_write_regs(fe, R_TM, 1);
204 msleep(10); /* temperature sensing */
206 /* read thermometer info */
207 tda18271_read_regs(fe);
210 tm = tda18271_lookup_thermometer(fe);
212 /* switch thermometer off */
214 tda18271_write_regs(fe, R_TM, 1);
216 /* set CAL mode to normal */
217 regs[R_EP4] &= ~0x03;
218 tda18271_write_regs(fe, R_EP4, 1);
223 /* ------------------------------------------------------------------ */
225 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
228 struct tda18271_priv *priv = fe->tuner_priv;
229 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
230 unsigned char *regs = priv->tda18271_regs;
231 int tm_current, rfcal_comp, approx, i, ret;
232 u8 dc_over_dt, rf_tab;
235 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
239 /* read die current temperature */
240 tm_current = tda18271_read_thermometer(fe);
242 /* frequency dependent parameters */
244 tda18271_calc_rf_cal(fe, &freq);
245 rf_tab = regs[R_EB14];
247 i = tda18271_lookup_rf_band(fe, &freq, NULL);
251 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
252 approx = map[i].rf_a1 *
253 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
255 approx = map[i].rf_a2 *
256 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
264 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
266 /* calculate temperature compensation */
267 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
269 regs[R_EB14] = approx + rfcal_comp;
270 ret = tda18271_write_regs(fe, R_EB14, 1);
275 static int tda18271_por(struct dvb_frontend *fe)
277 struct tda18271_priv *priv = fe->tuner_priv;
278 unsigned char *regs = priv->tda18271_regs;
281 /* power up detector 1 */
282 regs[R_EB12] &= ~0x20;
283 ret = tda18271_write_regs(fe, R_EB12, 1);
287 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
288 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
289 ret = tda18271_write_regs(fe, R_EB18, 1);
293 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
296 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
300 /* disable 1.5 MHz low pass filter */
301 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
302 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
303 ret = tda18271_write_regs(fe, R_EB21, 3);
308 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
310 struct tda18271_priv *priv = fe->tuner_priv;
311 unsigned char *regs = priv->tda18271_regs;
314 /* set CAL mode to normal */
315 regs[R_EP4] &= ~0x03;
316 tda18271_write_regs(fe, R_EP4, 1);
318 /* switch off agc1 */
319 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
321 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
322 tda18271_write_regs(fe, R_EB18, 1);
324 /* frequency dependent parameters */
326 tda18271_calc_bp_filter(fe, &freq);
327 tda18271_calc_gain_taper(fe, &freq);
328 tda18271_calc_rf_band(fe, &freq);
329 tda18271_calc_km(fe, &freq);
331 tda18271_write_regs(fe, R_EP1, 3);
332 tda18271_write_regs(fe, R_EB13, 1);
334 /* main pll charge pump source */
335 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
337 /* cal pll charge pump source */
338 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
340 /* force dcdc converter to 0 V */
342 tda18271_write_regs(fe, R_EB14, 1);
344 /* disable plls lock */
345 regs[R_EB20] &= ~0x20;
346 tda18271_write_regs(fe, R_EB20, 1);
348 /* set CAL mode to RF tracking filter calibration */
350 tda18271_write_regs(fe, R_EP4, 2);
352 /* --------------------------------------------------------------- */
354 /* set the internal calibration signal */
357 tda18271_calc_cal_pll(fe, N);
358 tda18271_write_regs(fe, R_CPD, 4);
360 /* downconvert internal calibration */
363 tda18271_calc_main_pll(fe, N);
364 tda18271_write_regs(fe, R_MPD, 4);
368 tda18271_write_regs(fe, R_EP2, 1);
369 tda18271_write_regs(fe, R_EP1, 1);
370 tda18271_write_regs(fe, R_EP2, 1);
371 tda18271_write_regs(fe, R_EP1, 1);
373 /* --------------------------------------------------------------- */
375 /* normal operation for the main pll */
376 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
378 /* normal operation for the cal pll */
379 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
381 msleep(10); /* plls locking */
383 /* launch the rf tracking filters calibration */
384 regs[R_EB20] |= 0x20;
385 tda18271_write_regs(fe, R_EB20, 1);
387 msleep(60); /* calibration */
389 /* --------------------------------------------------------------- */
391 /* set CAL mode to normal */
392 regs[R_EP4] &= ~0x03;
395 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
397 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
398 tda18271_write_regs(fe, R_EB18, 1);
400 tda18271_write_regs(fe, R_EP3, 2);
402 /* synchronization */
403 tda18271_write_regs(fe, R_EP1, 1);
405 /* get calibration result */
406 tda18271_read_extended(fe);
411 static int tda18271_powerscan(struct dvb_frontend *fe,
412 u32 *freq_in, u32 *freq_out)
414 struct tda18271_priv *priv = fe->tuner_priv;
415 unsigned char *regs = priv->tda18271_regs;
416 int sgn, bcal, count, wait, ret;
423 tda18271_calc_rf_band(fe, &freq);
424 tda18271_calc_rf_cal(fe, &freq);
425 tda18271_calc_gain_taper(fe, &freq);
426 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
428 tda18271_write_regs(fe, R_EP2, 1);
429 tda18271_write_regs(fe, R_EB14, 1);
431 /* downconvert frequency */
434 tda18271_calc_main_pll(fe, freq);
435 tda18271_write_regs(fe, R_MPD, 4);
437 msleep(5); /* pll locking */
440 regs[R_EP4] &= ~0x03;
442 tda18271_write_regs(fe, R_EP4, 1);
444 /* launch power detection measurement */
445 tda18271_write_regs(fe, R_EP2, 1);
447 /* read power detection info, stored in EB10 */
448 ret = tda18271_read_extended(fe);
452 /* algorithm initialization */
454 *freq_out = *freq_in;
459 while ((regs[R_EB10] & 0x3f) < cid_target) {
460 /* downconvert updated freq to 1 MHz */
461 freq = *freq_in + (sgn * count) + 1000000;
463 tda18271_calc_main_pll(fe, freq);
464 tda18271_write_regs(fe, R_MPD, 4);
467 msleep(5); /* pll locking */
470 udelay(100); /* pll locking */
472 /* launch power detection measurement */
473 tda18271_write_regs(fe, R_EP2, 1);
475 /* read power detection info, stored in EB10 */
476 ret = tda18271_read_extended(fe);
482 if (count <= count_limit)
493 if ((regs[R_EB10] & 0x3f) >= cid_target) {
495 *freq_out = freq - 1000000;
499 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
500 bcal, *freq_in, *freq_out, freq);
505 static int tda18271_powerscan_init(struct dvb_frontend *fe)
507 struct tda18271_priv *priv = fe->tuner_priv;
508 unsigned char *regs = priv->tda18271_regs;
511 /* set standard to digital */
512 regs[R_EP3] &= ~0x1f; /* clear std bits */
515 /* set cal mode to normal */
516 regs[R_EP4] &= ~0x03;
518 /* update IF output level */
519 regs[R_EP4] &= ~0x1c; /* clear if level bits */
521 ret = tda18271_write_regs(fe, R_EP3, 2);
525 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
526 ret = tda18271_write_regs(fe, R_EB18, 1);
530 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
532 /* 1.5 MHz low pass filter */
533 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
534 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
536 ret = tda18271_write_regs(fe, R_EB21, 3);
541 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
543 struct tda18271_priv *priv = fe->tuner_priv;
544 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
545 unsigned char *regs = priv->tda18271_regs;
555 i = tda18271_lookup_rf_band(fe, &freq, NULL);
560 rf_default[RF1] = 1000 * map[i].rf1_def;
561 rf_default[RF2] = 1000 * map[i].rf2_def;
562 rf_default[RF3] = 1000 * map[i].rf3_def;
564 for (rf = RF1; rf <= RF3; rf++) {
565 if (0 == rf_default[rf])
567 tda_cal("freq = %d, rf = %d\n", freq, rf);
569 /* look for optimized calibration frequency */
570 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
574 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
575 prog_tab[rf] = regs[R_EB14];
578 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
580 prog_cal[rf] = prog_tab[rf];
585 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
586 map[i].rf1 = rf_freq[RF1] / 1000;
589 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
590 prog_cal[RF1] + prog_tab[RF1]) /
591 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
592 map[i].rf2 = rf_freq[RF2] / 1000;
595 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
596 prog_cal[RF2] + prog_tab[RF2]) /
597 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
598 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
599 map[i].rf3 = rf_freq[RF3] / 1000;
609 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
611 struct tda18271_priv *priv = fe->tuner_priv;
615 tda_info("tda18271: performing RF tracking filter calibration\n");
617 /* wait for die temperature stabilization */
620 ret = tda18271_powerscan_init(fe);
624 /* rf band calibration */
625 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
627 tda18271_rf_tracking_filters_init(fe, 1000 *
628 priv->rf_cal_state[i].rfmax);
633 priv->tm_rfcal = tda18271_read_thermometer(fe);
638 /* ------------------------------------------------------------------ */
640 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
642 struct tda18271_priv *priv = fe->tuner_priv;
643 unsigned char *regs = priv->tda18271_regs;
646 /* test RF_CAL_OK to see if we need init */
647 if ((regs[R_EP1] & 0x10) == 0)
648 priv->cal_initialized = false;
650 if (priv->cal_initialized)
653 ret = tda18271_calc_rf_filter_curve(fe);
657 ret = tda18271_por(fe);
661 tda_info("tda18271: RF tracking filter calibration complete\n");
663 priv->cal_initialized = true;
666 tda_info("tda18271: RF tracking filter calibration failed!\n");
671 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
674 struct tda18271_priv *priv = fe->tuner_priv;
675 unsigned char *regs = priv->tda18271_regs;
679 /* calculate bp filter */
680 tda18271_calc_bp_filter(fe, &freq);
681 tda18271_write_regs(fe, R_EP1, 1);
685 tda18271_write_regs(fe, R_EB4, 1);
688 tda18271_write_regs(fe, R_EB7, 1);
691 tda18271_write_regs(fe, R_EB14, 1);
694 tda18271_write_regs(fe, R_EB20, 1);
696 /* set cal mode to RF tracking filter calibration */
699 /* calculate cal pll */
701 switch (priv->mode) {
702 case TDA18271_ANALOG:
705 case TDA18271_DIGITAL:
710 tda18271_calc_cal_pll(fe, N);
712 /* calculate main pll */
714 switch (priv->mode) {
715 case TDA18271_ANALOG:
718 case TDA18271_DIGITAL:
719 N = freq + bw / 2 + 1000000;
723 tda18271_calc_main_pll(fe, N);
725 ret = tda18271_write_regs(fe, R_EP3, 11);
729 msleep(5); /* RF tracking filter calibration initialization */
731 /* search for K,M,CO for RF calibration */
732 tda18271_calc_km(fe, &freq);
733 tda18271_write_regs(fe, R_EB13, 1);
735 /* search for rf band */
736 tda18271_calc_rf_band(fe, &freq);
738 /* search for gain taper */
739 tda18271_calc_gain_taper(fe, &freq);
741 tda18271_write_regs(fe, R_EP2, 1);
742 tda18271_write_regs(fe, R_EP1, 1);
743 tda18271_write_regs(fe, R_EP2, 1);
744 tda18271_write_regs(fe, R_EP1, 1);
748 tda18271_write_regs(fe, R_EB4, 1);
751 tda18271_write_regs(fe, R_EB7, 1);
752 msleep(10); /* pll locking */
755 tda18271_write_regs(fe, R_EB20, 1);
756 msleep(60); /* RF tracking filter calibration completion */
758 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
759 tda18271_write_regs(fe, R_EP4, 1);
761 tda18271_write_regs(fe, R_EP1, 1);
763 /* RF tracking filter correction for VHF_Low band */
764 if (0 == tda18271_calc_rf_cal(fe, &freq))
765 tda18271_write_regs(fe, R_EB14, 1);
770 /* ------------------------------------------------------------------ */
772 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
774 struct tda18271_priv *priv = fe->tuner_priv;
775 unsigned char *regs = priv->tda18271_regs;
778 ret = tda18271_read_regs(fe);
782 /* test IR_CAL_OK to see if we need init */
783 if ((regs[R_EP1] & 0x08) == 0)
784 ret = tda18271_init_regs(fe);
789 static int tda18271_init(struct dvb_frontend *fe)
791 struct tda18271_priv *priv = fe->tuner_priv;
794 mutex_lock(&priv->lock);
797 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
802 ret = tda18271_ir_cal_init(fe);
806 if (priv->id == TDA18271HDC2)
807 tda18271c2_rf_cal_init(fe);
809 mutex_unlock(&priv->lock);
814 static int tda18271_tune(struct dvb_frontend *fe,
815 struct tda18271_std_map_item *map, u32 freq, u32 bw)
817 struct tda18271_priv *priv = fe->tuner_priv;
820 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
821 freq, map->if_freq, bw, map->agc_mode, map->std);
823 ret = tda18271_init(fe);
827 mutex_lock(&priv->lock);
831 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
834 tda18271c2_rf_tracking_filters_correction(fe, freq);
837 ret = tda18271_channel_configuration(fe, map, freq, bw);
839 mutex_unlock(&priv->lock);
844 /* ------------------------------------------------------------------ */
846 static int tda18271_set_params(struct dvb_frontend *fe,
847 struct dvb_frontend_parameters *params)
849 struct tda18271_priv *priv = fe->tuner_priv;
850 struct tda18271_std_map *std_map = &priv->std;
851 struct tda18271_std_map_item *map;
853 u32 bw, freq = params->frequency;
855 priv->mode = TDA18271_DIGITAL;
857 if (fe->ops.info.type == FE_ATSC) {
858 switch (params->u.vsb.modulation) {
861 map = &std_map->atsc_6;
865 map = &std_map->qam_6;
868 tda_warn("modulation not set!\n");
872 /* userspace request is already center adjusted */
873 freq += 1750000; /* Adjust to center (+1.75MHZ) */
876 } else if (fe->ops.info.type == FE_OFDM) {
877 switch (params->u.ofdm.bandwidth) {
878 case BANDWIDTH_6_MHZ:
880 map = &std_map->dvbt_6;
882 case BANDWIDTH_7_MHZ:
884 map = &std_map->dvbt_7;
886 case BANDWIDTH_8_MHZ:
888 map = &std_map->dvbt_8;
891 tda_warn("bandwidth not set!\n");
895 tda_warn("modulation type not supported!\n");
899 /* When tuning digital, the analog demod must be tri-stated */
900 if (fe->ops.analog_ops.standby)
901 fe->ops.analog_ops.standby(fe);
903 ret = tda18271_tune(fe, map, freq, bw);
908 priv->frequency = freq;
909 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
910 params->u.ofdm.bandwidth : 0;
915 static int tda18271_set_analog_params(struct dvb_frontend *fe,
916 struct analog_parameters *params)
918 struct tda18271_priv *priv = fe->tuner_priv;
919 struct tda18271_std_map *std_map = &priv->std;
920 struct tda18271_std_map_item *map;
923 u32 freq = params->frequency * 62500;
925 priv->mode = TDA18271_ANALOG;
927 if (params->mode == V4L2_TUNER_RADIO) {
929 map = &std_map->fm_radio;
931 } else if (params->std & V4L2_STD_MN) {
932 map = &std_map->atv_mn;
934 } else if (params->std & V4L2_STD_B) {
935 map = &std_map->atv_b;
937 } else if (params->std & V4L2_STD_GH) {
938 map = &std_map->atv_gh;
940 } else if (params->std & V4L2_STD_PAL_I) {
941 map = &std_map->atv_i;
943 } else if (params->std & V4L2_STD_DK) {
944 map = &std_map->atv_dk;
946 } else if (params->std & V4L2_STD_SECAM_L) {
947 map = &std_map->atv_l;
949 } else if (params->std & V4L2_STD_SECAM_LC) {
950 map = &std_map->atv_lc;
953 map = &std_map->atv_i;
957 tda_dbg("setting tda18271 to system %s\n", mode);
959 ret = tda18271_tune(fe, map, freq, 0);
964 priv->frequency = freq;
970 static int tda18271_sleep(struct dvb_frontend *fe)
972 struct tda18271_priv *priv = fe->tuner_priv;
975 mutex_lock(&priv->lock);
977 /* standby mode w/ slave tuner output
978 * & loop thru & xtal oscillator on */
979 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
981 mutex_unlock(&priv->lock);
986 static int tda18271_release(struct dvb_frontend *fe)
988 struct tda18271_priv *priv = fe->tuner_priv;
990 mutex_lock(&tda18271_list_mutex);
993 hybrid_tuner_release_state(priv);
995 mutex_unlock(&tda18271_list_mutex);
997 fe->tuner_priv = NULL;
1002 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1004 struct tda18271_priv *priv = fe->tuner_priv;
1005 *frequency = priv->frequency;
1009 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1011 struct tda18271_priv *priv = fe->tuner_priv;
1012 *bandwidth = priv->bandwidth;
1016 /* ------------------------------------------------------------------ */
1018 #define tda18271_update_std(std_cfg, name) do { \
1019 if (map->std_cfg.if_freq + \
1020 map->std_cfg.agc_mode + map->std_cfg.std + \
1021 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1022 tda_dbg("Using custom std config for %s\n", name); \
1023 memcpy(&std->std_cfg, &map->std_cfg, \
1024 sizeof(struct tda18271_std_map_item)); \
1027 #define tda18271_dump_std_item(std_cfg, name) do { \
1028 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1029 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1030 name, std->std_cfg.if_freq, \
1031 std->std_cfg.agc_mode, std->std_cfg.std, \
1032 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1035 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1037 struct tda18271_priv *priv = fe->tuner_priv;
1038 struct tda18271_std_map *std = &priv->std;
1040 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1041 tda18271_dump_std_item(fm_radio, " fm ");
1042 tda18271_dump_std_item(atv_b, "atv b ");
1043 tda18271_dump_std_item(atv_dk, "atv dk");
1044 tda18271_dump_std_item(atv_gh, "atv gh");
1045 tda18271_dump_std_item(atv_i, "atv i ");
1046 tda18271_dump_std_item(atv_l, "atv l ");
1047 tda18271_dump_std_item(atv_lc, "atv l'");
1048 tda18271_dump_std_item(atv_mn, "atv mn");
1049 tda18271_dump_std_item(atsc_6, "atsc 6");
1050 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1051 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1052 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1053 tda18271_dump_std_item(qam_6, "qam 6 ");
1054 tda18271_dump_std_item(qam_8, "qam 8 ");
1059 static int tda18271_update_std_map(struct dvb_frontend *fe,
1060 struct tda18271_std_map *map)
1062 struct tda18271_priv *priv = fe->tuner_priv;
1063 struct tda18271_std_map *std = &priv->std;
1068 tda18271_update_std(fm_radio, "fm");
1069 tda18271_update_std(atv_b, "atv b");
1070 tda18271_update_std(atv_dk, "atv dk");
1071 tda18271_update_std(atv_gh, "atv gh");
1072 tda18271_update_std(atv_i, "atv i");
1073 tda18271_update_std(atv_l, "atv l");
1074 tda18271_update_std(atv_lc, "atv l'");
1075 tda18271_update_std(atv_mn, "atv mn");
1076 tda18271_update_std(atsc_6, "atsc 6");
1077 tda18271_update_std(dvbt_6, "dvbt 6");
1078 tda18271_update_std(dvbt_7, "dvbt 7");
1079 tda18271_update_std(dvbt_8, "dvbt 8");
1080 tda18271_update_std(qam_6, "qam 6");
1081 tda18271_update_std(qam_8, "qam 8");
1086 static int tda18271_get_id(struct dvb_frontend *fe)
1088 struct tda18271_priv *priv = fe->tuner_priv;
1089 unsigned char *regs = priv->tda18271_regs;
1093 mutex_lock(&priv->lock);
1094 tda18271_read_regs(fe);
1095 mutex_unlock(&priv->lock);
1097 switch (regs[R_ID] & 0x7f) {
1099 name = "TDA18271HD/C1";
1100 priv->id = TDA18271HDC1;
1103 name = "TDA18271HD/C2";
1104 priv->id = TDA18271HDC2;
1107 name = "Unknown device";
1112 tda_info("%s detected @ %d-%04x%s\n", name,
1113 i2c_adapter_id(priv->i2c_props.adap),
1114 priv->i2c_props.addr,
1115 (0 == ret) ? "" : ", device not supported.");
1120 static struct dvb_tuner_ops tda18271_tuner_ops = {
1122 .name = "NXP TDA18271HD",
1123 .frequency_min = 45000000,
1124 .frequency_max = 864000000,
1125 .frequency_step = 62500
1127 .init = tda18271_init,
1128 .sleep = tda18271_sleep,
1129 .set_params = tda18271_set_params,
1130 .set_analog_params = tda18271_set_analog_params,
1131 .release = tda18271_release,
1132 .get_frequency = tda18271_get_frequency,
1133 .get_bandwidth = tda18271_get_bandwidth,
1136 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1137 struct i2c_adapter *i2c,
1138 struct tda18271_config *cfg)
1140 struct tda18271_priv *priv = NULL;
1143 mutex_lock(&tda18271_list_mutex);
1145 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1146 hybrid_tuner_instance_list,
1147 i2c, addr, "tda18271");
1153 /* new tuner instance */
1154 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1155 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1156 priv->cal_initialized = false;
1157 mutex_init(&priv->lock);
1159 fe->tuner_priv = priv;
1162 priv->small_i2c = cfg->small_i2c;
1164 if (tda_fail(tda18271_get_id(fe)))
1167 if (tda_fail(tda18271_assign_map_layout(fe)))
1170 mutex_lock(&priv->lock);
1171 tda18271_init_regs(fe);
1173 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1174 tda18271c2_rf_cal_init(fe);
1176 mutex_unlock(&priv->lock);
1179 /* existing tuner instance */
1180 fe->tuner_priv = priv;
1182 /* allow dvb driver to override i2c gate setting */
1183 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1184 priv->gate = cfg->gate;
1188 /* override default std map with values in config struct */
1189 if ((cfg) && (cfg->std_map))
1190 tda18271_update_std_map(fe, cfg->std_map);
1192 mutex_unlock(&tda18271_list_mutex);
1194 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1195 sizeof(struct dvb_tuner_ops));
1197 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1198 tda18271_dump_std_map(fe);
1202 mutex_unlock(&tda18271_list_mutex);
1204 tda18271_release(fe);
1207 EXPORT_SYMBOL_GPL(tda18271_attach);
1208 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1209 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1210 MODULE_LICENSE("GPL");
1211 MODULE_VERSION("0.3");
1214 * Overrides for Emacs so that we follow Linus's tabbing style.
1215 * ---------------------------------------------------------------------------