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;
56 /* update TV broadcast parameters */
59 regs[R_EP3] &= ~0x1f; /* clear std bits */
60 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
62 /* set rfagc to high speed mode */
65 /* set cal mode to normal */
68 /* update IF output level & IF notch frequency */
69 regs[R_EP4] &= ~0x1c; /* clear if level bits */
70 regs[R_EP4] |= (map->if_lvl << 2);
74 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
76 case TDA18271_DIGITAL:
77 regs[R_MPD] |= 0x80; /* IF notch = 1 */
83 regs[R_EP4] |= map->fm_rfn << 7;
85 /* update rf top / if top */
87 regs[R_EB22] |= map->rfagc_top;
88 tda18271_write_regs(fe, R_EB22, 1);
90 /* --------------------------------------------------------------- */
92 /* disable Power Level Indicator */
95 /* frequency dependent parameters */
97 tda18271_calc_ir_measure(fe, &freq);
99 tda18271_calc_bp_filter(fe, &freq);
101 tda18271_calc_rf_band(fe, &freq);
103 tda18271_calc_gain_taper(fe, &freq);
105 /* --------------------------------------------------------------- */
107 /* dual tuner and agc1 extra configuration */
109 switch (priv->role) {
110 case TDA18271_MASTER:
111 regs[R_EB1] |= 0x04; /* main vco */
114 regs[R_EB1] &= ~0x04; /* cal vco */
118 /* agc1 always active */
119 regs[R_EB1] &= ~0x02;
121 /* agc1 has priority on agc2 */
122 regs[R_EB1] &= ~0x01;
124 tda18271_write_regs(fe, R_EB1, 1);
126 /* --------------------------------------------------------------- */
128 N = map->if_freq * 1000 + freq;
130 switch (priv->role) {
131 case TDA18271_MASTER:
132 tda18271_calc_main_pll(fe, N);
133 tda18271_write_regs(fe, R_MPD, 4);
136 tda18271_calc_cal_pll(fe, N);
137 tda18271_write_regs(fe, R_CPD, 4);
139 regs[R_MPD] = regs[R_CPD] & 0x7f;
140 tda18271_write_regs(fe, R_MPD, 1);
144 tda18271_write_regs(fe, R_TM, 7);
146 /* force charge pump source */
147 charge_pump_source(fe, 1);
151 /* return pll to normal operation */
152 charge_pump_source(fe, 0);
156 /* set rfagc to normal speed mode */
158 regs[R_EP3] &= ~0x04;
161 tda18271_write_regs(fe, R_EP3, 1);
166 static int tda18271_read_thermometer(struct dvb_frontend *fe)
168 struct tda18271_priv *priv = fe->tuner_priv;
169 unsigned char *regs = priv->tda18271_regs;
172 /* switch thermometer on */
174 tda18271_write_regs(fe, R_TM, 1);
176 /* read thermometer info */
177 tda18271_read_regs(fe);
179 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
180 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
182 if ((regs[R_TM] & 0x20) == 0x20)
187 tda18271_write_regs(fe, R_TM, 1);
189 msleep(10); /* temperature sensing */
191 /* read thermometer info */
192 tda18271_read_regs(fe);
195 tm = tda18271_lookup_thermometer(fe);
197 /* switch thermometer off */
199 tda18271_write_regs(fe, R_TM, 1);
201 /* set CAL mode to normal */
202 regs[R_EP4] &= ~0x03;
203 tda18271_write_regs(fe, R_EP4, 1);
208 /* ------------------------------------------------------------------ */
210 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
213 struct tda18271_priv *priv = fe->tuner_priv;
214 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
215 unsigned char *regs = priv->tda18271_regs;
216 int tm_current, rfcal_comp, approx, i;
217 u8 dc_over_dt, rf_tab;
220 tda18271_set_standby_mode(fe, 0, 0, 0);
222 /* read die current temperature */
223 tm_current = tda18271_read_thermometer(fe);
225 /* frequency dependent parameters */
227 tda18271_calc_rf_cal(fe, &freq);
228 rf_tab = regs[R_EB14];
230 i = tda18271_lookup_rf_band(fe, &freq, NULL);
234 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
235 approx = map[i].rf_a1 *
236 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
238 approx = map[i].rf_a2 *
239 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
247 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
249 /* calculate temperature compensation */
250 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
252 regs[R_EB14] = approx + rfcal_comp;
253 tda18271_write_regs(fe, R_EB14, 1);
258 static int tda18271_por(struct dvb_frontend *fe)
260 struct tda18271_priv *priv = fe->tuner_priv;
261 unsigned char *regs = priv->tda18271_regs;
263 /* power up detector 1 */
264 regs[R_EB12] &= ~0x20;
265 tda18271_write_regs(fe, R_EB12, 1);
267 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
268 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
269 tda18271_write_regs(fe, R_EB18, 1);
271 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
274 tda18271_set_standby_mode(fe, 1, 0, 0);
276 /* disable 1.5 MHz low pass filter */
277 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
278 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
279 tda18271_write_regs(fe, R_EB21, 3);
284 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
286 struct tda18271_priv *priv = fe->tuner_priv;
287 unsigned char *regs = priv->tda18271_regs;
290 /* set CAL mode to normal */
291 regs[R_EP4] &= ~0x03;
292 tda18271_write_regs(fe, R_EP4, 1);
294 /* switch off agc1 */
295 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
297 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
298 tda18271_write_regs(fe, R_EB18, 1);
300 /* frequency dependent parameters */
302 tda18271_calc_bp_filter(fe, &freq);
303 tda18271_calc_gain_taper(fe, &freq);
304 tda18271_calc_rf_band(fe, &freq);
305 tda18271_calc_km(fe, &freq);
307 tda18271_write_regs(fe, R_EP1, 3);
308 tda18271_write_regs(fe, R_EB13, 1);
310 /* main pll charge pump source */
311 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
313 /* cal pll charge pump source */
314 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
316 /* force dcdc converter to 0 V */
318 tda18271_write_regs(fe, R_EB14, 1);
320 /* disable plls lock */
321 regs[R_EB20] &= ~0x20;
322 tda18271_write_regs(fe, R_EB20, 1);
324 /* set CAL mode to RF tracking filter calibration */
326 tda18271_write_regs(fe, R_EP4, 2);
328 /* --------------------------------------------------------------- */
330 /* set the internal calibration signal */
333 tda18271_calc_cal_pll(fe, N);
334 tda18271_write_regs(fe, R_CPD, 4);
336 /* downconvert internal calibration */
339 tda18271_calc_main_pll(fe, N);
340 tda18271_write_regs(fe, R_MPD, 4);
344 tda18271_write_regs(fe, R_EP2, 1);
345 tda18271_write_regs(fe, R_EP1, 1);
346 tda18271_write_regs(fe, R_EP2, 1);
347 tda18271_write_regs(fe, R_EP1, 1);
349 /* --------------------------------------------------------------- */
351 /* normal operation for the main pll */
352 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
354 /* normal operation for the cal pll */
355 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
357 msleep(10); /* plls locking */
359 /* launch the rf tracking filters calibration */
360 regs[R_EB20] |= 0x20;
361 tda18271_write_regs(fe, R_EB20, 1);
363 msleep(60); /* calibration */
365 /* --------------------------------------------------------------- */
367 /* set CAL mode to normal */
368 regs[R_EP4] &= ~0x03;
371 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
373 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
374 tda18271_write_regs(fe, R_EB18, 1);
376 tda18271_write_regs(fe, R_EP3, 2);
378 /* synchronization */
379 tda18271_write_regs(fe, R_EP1, 1);
381 /* get calibration result */
382 tda18271_read_extended(fe);
387 static int tda18271_powerscan(struct dvb_frontend *fe,
388 u32 *freq_in, u32 *freq_out)
390 struct tda18271_priv *priv = fe->tuner_priv;
391 unsigned char *regs = priv->tda18271_regs;
392 int sgn, bcal, count, wait;
399 tda18271_calc_rf_band(fe, &freq);
400 tda18271_calc_rf_cal(fe, &freq);
401 tda18271_calc_gain_taper(fe, &freq);
402 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
404 tda18271_write_regs(fe, R_EP2, 1);
405 tda18271_write_regs(fe, R_EB14, 1);
407 /* downconvert frequency */
410 tda18271_calc_main_pll(fe, freq);
411 tda18271_write_regs(fe, R_MPD, 4);
413 msleep(5); /* pll locking */
416 regs[R_EP4] &= ~0x03;
418 tda18271_write_regs(fe, R_EP4, 1);
420 /* launch power detection measurement */
421 tda18271_write_regs(fe, R_EP2, 1);
423 /* read power detection info, stored in EB10 */
424 tda18271_read_extended(fe);
426 /* algorithm initialization */
428 *freq_out = *freq_in;
433 while ((regs[R_EB10] & 0x3f) < cid_target) {
434 /* downconvert updated freq to 1 MHz */
435 freq = *freq_in + (sgn * count) + 1000000;
437 tda18271_calc_main_pll(fe, freq);
438 tda18271_write_regs(fe, R_MPD, 4);
441 msleep(5); /* pll locking */
444 udelay(100); /* pll locking */
446 /* launch power detection measurement */
447 tda18271_write_regs(fe, R_EP2, 1);
449 /* read power detection info, stored in EB10 */
450 tda18271_read_extended(fe);
454 if (count <= count_limit)
465 if ((regs[R_EB10] & 0x3f) >= cid_target) {
467 *freq_out = freq - 1000000;
471 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
472 bcal, *freq_in, *freq_out, freq);
477 static int tda18271_powerscan_init(struct dvb_frontend *fe)
479 struct tda18271_priv *priv = fe->tuner_priv;
480 unsigned char *regs = priv->tda18271_regs;
482 /* set standard to digital */
483 regs[R_EP3] &= ~0x1f; /* clear std bits */
486 /* set cal mode to normal */
487 regs[R_EP4] &= ~0x03;
489 /* update IF output level & IF notch frequency */
490 regs[R_EP4] &= ~0x1c; /* clear if level bits */
492 tda18271_write_regs(fe, R_EP3, 2);
494 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
495 tda18271_write_regs(fe, R_EB18, 1);
497 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
499 /* 1.5 MHz low pass filter */
500 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
501 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
503 tda18271_write_regs(fe, R_EB21, 3);
508 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
510 struct tda18271_priv *priv = fe->tuner_priv;
511 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
512 unsigned char *regs = priv->tda18271_regs;
522 i = tda18271_lookup_rf_band(fe, &freq, NULL);
527 rf_default[RF1] = 1000 * map[i].rf1_def;
528 rf_default[RF2] = 1000 * map[i].rf2_def;
529 rf_default[RF3] = 1000 * map[i].rf3_def;
531 for (rf = RF1; rf <= RF3; rf++) {
532 if (0 == rf_default[rf])
534 tda_cal("freq = %d, rf = %d\n", freq, rf);
536 /* look for optimized calibration frequency */
537 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
539 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
540 prog_tab[rf] = regs[R_EB14];
543 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
545 prog_cal[rf] = prog_tab[rf];
550 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
551 map[i].rf1 = rf_freq[RF1] / 1000;
554 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
555 prog_cal[RF1] + prog_tab[RF1]) /
556 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
557 map[i].rf2 = rf_freq[RF2] / 1000;
560 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
561 prog_cal[RF2] + prog_tab[RF2]) /
562 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
563 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
564 map[i].rf3 = rf_freq[RF3] / 1000;
574 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
576 struct tda18271_priv *priv = fe->tuner_priv;
579 tda_info("tda18271: performing RF tracking filter calibration\n");
581 /* wait for die temperature stabilization */
584 tda18271_powerscan_init(fe);
586 /* rf band calibration */
587 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
588 tda18271_rf_tracking_filters_init(fe, 1000 *
589 priv->rf_cal_state[i].rfmax);
591 priv->tm_rfcal = tda18271_read_thermometer(fe);
596 /* ------------------------------------------------------------------ */
598 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
600 struct tda18271_priv *priv = fe->tuner_priv;
601 unsigned char *regs = priv->tda18271_regs;
603 /* test RF_CAL_OK to see if we need init */
604 if ((regs[R_EP1] & 0x10) == 0)
605 priv->cal_initialized = false;
607 if (priv->cal_initialized)
610 tda18271_calc_rf_filter_curve(fe);
614 tda_info("tda18271: RF tracking filter calibration complete\n");
616 priv->cal_initialized = true;
621 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
624 struct tda18271_priv *priv = fe->tuner_priv;
625 unsigned char *regs = priv->tda18271_regs;
628 /* calculate bp filter */
629 tda18271_calc_bp_filter(fe, &freq);
630 tda18271_write_regs(fe, R_EP1, 1);
634 tda18271_write_regs(fe, R_EB4, 1);
637 tda18271_write_regs(fe, R_EB7, 1);
640 tda18271_write_regs(fe, R_EB14, 1);
643 tda18271_write_regs(fe, R_EB20, 1);
645 /* set cal mode to RF tracking filter calibration */
648 /* calculate cal pll */
650 switch (priv->mode) {
651 case TDA18271_ANALOG:
654 case TDA18271_DIGITAL:
659 tda18271_calc_cal_pll(fe, N);
661 /* calculate main pll */
663 switch (priv->mode) {
664 case TDA18271_ANALOG:
667 case TDA18271_DIGITAL:
668 N = freq + bw / 2 + 1000000;
672 tda18271_calc_main_pll(fe, N);
674 tda18271_write_regs(fe, R_EP3, 11);
675 msleep(5); /* RF tracking filter calibration initialization */
677 /* search for K,M,CO for RF calibration */
678 tda18271_calc_km(fe, &freq);
679 tda18271_write_regs(fe, R_EB13, 1);
681 /* search for rf band */
682 tda18271_calc_rf_band(fe, &freq);
684 /* search for gain taper */
685 tda18271_calc_gain_taper(fe, &freq);
687 tda18271_write_regs(fe, R_EP2, 1);
688 tda18271_write_regs(fe, R_EP1, 1);
689 tda18271_write_regs(fe, R_EP2, 1);
690 tda18271_write_regs(fe, R_EP1, 1);
694 tda18271_write_regs(fe, R_EB4, 1);
697 tda18271_write_regs(fe, R_EB7, 1);
698 msleep(10); /* pll locking */
701 tda18271_write_regs(fe, R_EB20, 1);
702 msleep(60); /* RF tracking filter calibration completion */
704 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
705 tda18271_write_regs(fe, R_EP4, 1);
707 tda18271_write_regs(fe, R_EP1, 1);
709 /* RF tracking filter correction for VHF_Low band */
710 if (0 == tda18271_calc_rf_cal(fe, &freq))
711 tda18271_write_regs(fe, R_EB14, 1);
716 /* ------------------------------------------------------------------ */
718 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
720 struct tda18271_priv *priv = fe->tuner_priv;
721 unsigned char *regs = priv->tda18271_regs;
724 ret = tda18271_read_regs(fe);
728 /* test IR_CAL_OK to see if we need init */
729 if ((regs[R_EP1] & 0x08) == 0)
730 ret = tda18271_init_regs(fe);
735 static int tda18271_init(struct dvb_frontend *fe)
737 struct tda18271_priv *priv = fe->tuner_priv;
740 mutex_lock(&priv->lock);
743 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
748 ret = tda18271_ir_cal_init(fe);
752 if (priv->id == TDA18271HDC2)
753 tda18271c2_rf_cal_init(fe);
755 mutex_unlock(&priv->lock);
760 static int tda18271_tune(struct dvb_frontend *fe,
761 struct tda18271_std_map_item *map, u32 freq, u32 bw)
763 struct tda18271_priv *priv = fe->tuner_priv;
766 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
767 freq, map->if_freq, bw, map->agc_mode, map->std);
769 ret = tda18271_init(fe);
773 mutex_lock(&priv->lock);
777 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
780 tda18271c2_rf_tracking_filters_correction(fe, freq);
783 tda18271_channel_configuration(fe, map, freq, bw);
785 mutex_unlock(&priv->lock);
790 /* ------------------------------------------------------------------ */
792 static int tda18271_set_params(struct dvb_frontend *fe,
793 struct dvb_frontend_parameters *params)
795 struct tda18271_priv *priv = fe->tuner_priv;
796 struct tda18271_std_map *std_map = &priv->std;
797 struct tda18271_std_map_item *map;
799 u32 bw, freq = params->frequency;
801 priv->mode = TDA18271_DIGITAL;
803 if (fe->ops.info.type == FE_ATSC) {
804 switch (params->u.vsb.modulation) {
807 map = &std_map->atsc_6;
811 map = &std_map->qam_6;
814 tda_warn("modulation not set!\n");
818 /* userspace request is already center adjusted */
819 freq += 1750000; /* Adjust to center (+1.75MHZ) */
822 } else if (fe->ops.info.type == FE_OFDM) {
823 switch (params->u.ofdm.bandwidth) {
824 case BANDWIDTH_6_MHZ:
826 map = &std_map->dvbt_6;
828 case BANDWIDTH_7_MHZ:
830 map = &std_map->dvbt_7;
832 case BANDWIDTH_8_MHZ:
834 map = &std_map->dvbt_8;
837 tda_warn("bandwidth not set!\n");
841 tda_warn("modulation type not supported!\n");
845 /* When tuning digital, the analog demod must be tri-stated */
846 if (fe->ops.analog_ops.standby)
847 fe->ops.analog_ops.standby(fe);
849 ret = tda18271_tune(fe, map, freq, bw);
854 priv->frequency = freq;
855 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
856 params->u.ofdm.bandwidth : 0;
861 static int tda18271_set_analog_params(struct dvb_frontend *fe,
862 struct analog_parameters *params)
864 struct tda18271_priv *priv = fe->tuner_priv;
865 struct tda18271_std_map *std_map = &priv->std;
866 struct tda18271_std_map_item *map;
869 u32 freq = params->frequency * 62500;
871 priv->mode = TDA18271_ANALOG;
873 if (params->mode == V4L2_TUNER_RADIO) {
875 map = &std_map->fm_radio;
877 } else if (params->std & V4L2_STD_MN) {
878 map = &std_map->atv_mn;
880 } else if (params->std & V4L2_STD_B) {
881 map = &std_map->atv_b;
883 } else if (params->std & V4L2_STD_GH) {
884 map = &std_map->atv_gh;
886 } else if (params->std & V4L2_STD_PAL_I) {
887 map = &std_map->atv_i;
889 } else if (params->std & V4L2_STD_DK) {
890 map = &std_map->atv_dk;
892 } else if (params->std & V4L2_STD_SECAM_L) {
893 map = &std_map->atv_l;
895 } else if (params->std & V4L2_STD_SECAM_LC) {
896 map = &std_map->atv_lc;
899 map = &std_map->atv_i;
903 tda_dbg("setting tda18271 to system %s\n", mode);
905 ret = tda18271_tune(fe, map, freq, 0);
910 priv->frequency = freq;
916 static int tda18271_sleep(struct dvb_frontend *fe)
918 struct tda18271_priv *priv = fe->tuner_priv;
921 mutex_lock(&priv->lock);
923 /* standby mode w/ slave tuner output
924 * & loop thru & xtal oscillator on */
925 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
927 mutex_unlock(&priv->lock);
932 static int tda18271_release(struct dvb_frontend *fe)
934 struct tda18271_priv *priv = fe->tuner_priv;
936 mutex_lock(&tda18271_list_mutex);
939 hybrid_tuner_release_state(priv);
941 mutex_unlock(&tda18271_list_mutex);
943 fe->tuner_priv = NULL;
948 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
950 struct tda18271_priv *priv = fe->tuner_priv;
951 *frequency = priv->frequency;
955 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
957 struct tda18271_priv *priv = fe->tuner_priv;
958 *bandwidth = priv->bandwidth;
962 /* ------------------------------------------------------------------ */
964 #define tda18271_update_std(std_cfg, name) do { \
965 if (map->std_cfg.if_freq + \
966 map->std_cfg.agc_mode + map->std_cfg.std + \
967 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
968 tda_dbg("Using custom std config for %s\n", name); \
969 memcpy(&std->std_cfg, &map->std_cfg, \
970 sizeof(struct tda18271_std_map_item)); \
973 #define tda18271_dump_std_item(std_cfg, name) do { \
974 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
975 "if_lvl = %d, rfagc_top = 0x%02x\n", \
976 name, std->std_cfg.if_freq, \
977 std->std_cfg.agc_mode, std->std_cfg.std, \
978 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
981 static int tda18271_dump_std_map(struct dvb_frontend *fe)
983 struct tda18271_priv *priv = fe->tuner_priv;
984 struct tda18271_std_map *std = &priv->std;
986 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
987 tda18271_dump_std_item(fm_radio, " fm ");
988 tda18271_dump_std_item(atv_b, "atv b ");
989 tda18271_dump_std_item(atv_dk, "atv dk");
990 tda18271_dump_std_item(atv_gh, "atv gh");
991 tda18271_dump_std_item(atv_i, "atv i ");
992 tda18271_dump_std_item(atv_l, "atv l ");
993 tda18271_dump_std_item(atv_lc, "atv l'");
994 tda18271_dump_std_item(atv_mn, "atv mn");
995 tda18271_dump_std_item(atsc_6, "atsc 6");
996 tda18271_dump_std_item(dvbt_6, "dvbt 6");
997 tda18271_dump_std_item(dvbt_7, "dvbt 7");
998 tda18271_dump_std_item(dvbt_8, "dvbt 8");
999 tda18271_dump_std_item(qam_6, "qam 6 ");
1000 tda18271_dump_std_item(qam_8, "qam 8 ");
1005 static int tda18271_update_std_map(struct dvb_frontend *fe,
1006 struct tda18271_std_map *map)
1008 struct tda18271_priv *priv = fe->tuner_priv;
1009 struct tda18271_std_map *std = &priv->std;
1014 tda18271_update_std(fm_radio, "fm");
1015 tda18271_update_std(atv_b, "atv b");
1016 tda18271_update_std(atv_dk, "atv dk");
1017 tda18271_update_std(atv_gh, "atv gh");
1018 tda18271_update_std(atv_i, "atv i");
1019 tda18271_update_std(atv_l, "atv l");
1020 tda18271_update_std(atv_lc, "atv l'");
1021 tda18271_update_std(atv_mn, "atv mn");
1022 tda18271_update_std(atsc_6, "atsc 6");
1023 tda18271_update_std(dvbt_6, "dvbt 6");
1024 tda18271_update_std(dvbt_7, "dvbt 7");
1025 tda18271_update_std(dvbt_8, "dvbt 8");
1026 tda18271_update_std(qam_6, "qam 6");
1027 tda18271_update_std(qam_8, "qam 8");
1032 static int tda18271_get_id(struct dvb_frontend *fe)
1034 struct tda18271_priv *priv = fe->tuner_priv;
1035 unsigned char *regs = priv->tda18271_regs;
1039 mutex_lock(&priv->lock);
1040 tda18271_read_regs(fe);
1041 mutex_unlock(&priv->lock);
1043 switch (regs[R_ID] & 0x7f) {
1045 name = "TDA18271HD/C1";
1046 priv->id = TDA18271HDC1;
1049 name = "TDA18271HD/C2";
1050 priv->id = TDA18271HDC2;
1053 name = "Unknown device";
1058 tda_info("%s detected @ %d-%04x%s\n", name,
1059 i2c_adapter_id(priv->i2c_props.adap),
1060 priv->i2c_props.addr,
1061 (0 == ret) ? "" : ", device not supported.");
1066 static struct dvb_tuner_ops tda18271_tuner_ops = {
1068 .name = "NXP TDA18271HD",
1069 .frequency_min = 45000000,
1070 .frequency_max = 864000000,
1071 .frequency_step = 62500
1073 .init = tda18271_init,
1074 .sleep = tda18271_sleep,
1075 .set_params = tda18271_set_params,
1076 .set_analog_params = tda18271_set_analog_params,
1077 .release = tda18271_release,
1078 .get_frequency = tda18271_get_frequency,
1079 .get_bandwidth = tda18271_get_bandwidth,
1082 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1083 struct i2c_adapter *i2c,
1084 struct tda18271_config *cfg)
1086 struct tda18271_priv *priv = NULL;
1089 mutex_lock(&tda18271_list_mutex);
1091 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1092 hybrid_tuner_instance_list,
1093 i2c, addr, "tda18271");
1099 /* new tuner instance */
1100 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1101 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1102 priv->cal_initialized = false;
1103 mutex_init(&priv->lock);
1105 fe->tuner_priv = priv;
1108 priv->small_i2c = cfg->small_i2c;
1110 if (tda18271_get_id(fe) < 0)
1113 if (tda18271_assign_map_layout(fe) < 0)
1116 mutex_lock(&priv->lock);
1117 tda18271_init_regs(fe);
1119 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
1120 tda18271c2_rf_cal_init(fe);
1122 mutex_unlock(&priv->lock);
1125 /* existing tuner instance */
1126 fe->tuner_priv = priv;
1128 /* allow dvb driver to override i2c gate setting */
1129 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1130 priv->gate = cfg->gate;
1134 /* override default std map with values in config struct */
1135 if ((cfg) && (cfg->std_map))
1136 tda18271_update_std_map(fe, cfg->std_map);
1138 mutex_unlock(&tda18271_list_mutex);
1140 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1141 sizeof(struct dvb_tuner_ops));
1143 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1144 tda18271_dump_std_map(fe);
1148 mutex_unlock(&tda18271_list_mutex);
1150 tda18271_release(fe);
1153 EXPORT_SYMBOL_GPL(tda18271_attach);
1154 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1155 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1156 MODULE_LICENSE("GPL");
1157 MODULE_VERSION("0.3");
1160 * Overrides for Emacs so that we follow Linus's tabbing style.
1161 * ---------------------------------------------------------------------------