1 // SPDX-License-Identifier: GPL-2.0-or-later
3 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
9 #include "tda18271-priv.h"
12 #include <linux/delay.h>
13 #include <linux/videodev2.h>
16 module_param_named(debug, tda18271_debug, int, 0644);
17 MODULE_PARM_DESC(debug, "set debug level (info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
19 static int tda18271_cal_on_startup = -1;
20 module_param_named(cal, tda18271_cal_on_startup, int, 0644);
21 MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");
23 static DEFINE_MUTEX(tda18271_list_mutex);
24 static LIST_HEAD(hybrid_tuner_instance_list);
26 /*---------------------------------------------------------------------*/
28 static int tda18271_toggle_output(struct dvb_frontend *fe, int standby)
30 struct tda18271_priv *priv = fe->tuner_priv;
32 int ret = tda18271_set_standby_mode(fe, standby ? 1 : 0,
33 priv->output_opt & TDA18271_OUTPUT_LT_OFF ? 1 : 0,
34 priv->output_opt & TDA18271_OUTPUT_XT_OFF ? 1 : 0);
39 tda_dbg("%s mode: xtal oscillator %s, slave tuner loop through %s\n",
40 standby ? "standby" : "active",
41 priv->output_opt & TDA18271_OUTPUT_XT_OFF ? "off" : "on",
42 priv->output_opt & TDA18271_OUTPUT_LT_OFF ? "off" : "on");
47 /*---------------------------------------------------------------------*/
49 static inline int charge_pump_source(struct dvb_frontend *fe, int force)
51 struct tda18271_priv *priv = fe->tuner_priv;
52 return tda18271_charge_pump_source(fe,
53 (priv->role == TDA18271_SLAVE) ?
55 TDA18271_MAIN_PLL, force);
58 static inline void tda18271_set_if_notch(struct dvb_frontend *fe)
60 struct tda18271_priv *priv = fe->tuner_priv;
61 unsigned char *regs = priv->tda18271_regs;
65 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
67 case TDA18271_DIGITAL:
68 regs[R_MPD] |= 0x80; /* IF notch = 1 */
73 static int tda18271_channel_configuration(struct dvb_frontend *fe,
74 struct tda18271_std_map_item *map,
77 struct tda18271_priv *priv = fe->tuner_priv;
78 unsigned char *regs = priv->tda18271_regs;
82 /* update TV broadcast parameters */
85 regs[R_EP3] &= ~0x1f; /* clear std bits */
86 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
88 if (priv->id == TDA18271HDC2) {
89 /* set rfagc to high speed mode */
93 /* set cal mode to normal */
96 /* update IF output level */
97 regs[R_EP4] &= ~0x1c; /* clear if level bits */
98 regs[R_EP4] |= (map->if_lvl << 2);
101 regs[R_EP4] &= ~0x80;
102 regs[R_EP4] |= map->fm_rfn << 7;
104 /* update rf top / if top */
106 regs[R_EB22] |= map->rfagc_top;
107 ret = tda18271_write_regs(fe, R_EB22, 1);
111 /* --------------------------------------------------------------- */
113 /* disable Power Level Indicator */
116 /* make sure thermometer is off */
119 /* frequency dependent parameters */
121 tda18271_calc_ir_measure(fe, &freq);
123 tda18271_calc_bp_filter(fe, &freq);
125 tda18271_calc_rf_band(fe, &freq);
127 tda18271_calc_gain_taper(fe, &freq);
129 /* --------------------------------------------------------------- */
131 /* dual tuner and agc1 extra configuration */
133 switch (priv->role) {
134 case TDA18271_MASTER:
135 regs[R_EB1] |= 0x04; /* main vco */
138 regs[R_EB1] &= ~0x04; /* cal vco */
142 /* agc1 always active */
143 regs[R_EB1] &= ~0x02;
145 /* agc1 has priority on agc2 */
146 regs[R_EB1] &= ~0x01;
148 ret = tda18271_write_regs(fe, R_EB1, 1);
152 /* --------------------------------------------------------------- */
154 N = map->if_freq * 1000 + freq;
156 switch (priv->role) {
157 case TDA18271_MASTER:
158 tda18271_calc_main_pll(fe, N);
159 tda18271_set_if_notch(fe);
160 tda18271_write_regs(fe, R_MPD, 4);
163 tda18271_calc_cal_pll(fe, N);
164 tda18271_write_regs(fe, R_CPD, 4);
166 regs[R_MPD] = regs[R_CPD] & 0x7f;
167 tda18271_set_if_notch(fe);
168 tda18271_write_regs(fe, R_MPD, 1);
172 ret = tda18271_write_regs(fe, R_TM, 7);
176 /* force charge pump source */
177 charge_pump_source(fe, 1);
181 /* return pll to normal operation */
182 charge_pump_source(fe, 0);
186 if (priv->id == TDA18271HDC2) {
187 /* set rfagc to normal speed mode */
189 regs[R_EP3] &= ~0x04;
192 ret = tda18271_write_regs(fe, R_EP3, 1);
198 static int tda18271_read_thermometer(struct dvb_frontend *fe)
200 struct tda18271_priv *priv = fe->tuner_priv;
201 unsigned char *regs = priv->tda18271_regs;
204 /* switch thermometer on */
206 tda18271_write_regs(fe, R_TM, 1);
208 /* read thermometer info */
209 tda18271_read_regs(fe);
211 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
212 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
214 if ((regs[R_TM] & 0x20) == 0x20)
219 tda18271_write_regs(fe, R_TM, 1);
221 msleep(10); /* temperature sensing */
223 /* read thermometer info */
224 tda18271_read_regs(fe);
227 tm = tda18271_lookup_thermometer(fe);
229 /* switch thermometer off */
231 tda18271_write_regs(fe, R_TM, 1);
233 /* set CAL mode to normal */
234 regs[R_EP4] &= ~0x03;
235 tda18271_write_regs(fe, R_EP4, 1);
240 /* ------------------------------------------------------------------ */
242 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
245 struct tda18271_priv *priv = fe->tuner_priv;
246 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
247 unsigned char *regs = priv->tda18271_regs;
249 u8 tm_current, dc_over_dt, rf_tab;
250 s32 rfcal_comp, approx;
253 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
257 /* read die current temperature */
258 tm_current = tda18271_read_thermometer(fe);
260 /* frequency dependent parameters */
262 tda18271_calc_rf_cal(fe, &freq);
263 rf_tab = regs[R_EB14];
265 i = tda18271_lookup_rf_band(fe, &freq, NULL);
269 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
270 approx = map[i].rf_a1 * (s32)(freq / 1000 - map[i].rf1) +
271 map[i].rf_b1 + rf_tab;
273 approx = map[i].rf_a2 * (s32)(freq / 1000 - map[i].rf2) +
274 map[i].rf_b2 + rf_tab;
282 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
284 /* calculate temperature compensation */
285 rfcal_comp = dc_over_dt * (s32)(tm_current - priv->tm_rfcal) / 1000;
287 regs[R_EB14] = (unsigned char)(approx + rfcal_comp);
288 ret = tda18271_write_regs(fe, R_EB14, 1);
293 static int tda18271_por(struct dvb_frontend *fe)
295 struct tda18271_priv *priv = fe->tuner_priv;
296 unsigned char *regs = priv->tda18271_regs;
299 /* power up detector 1 */
300 regs[R_EB12] &= ~0x20;
301 ret = tda18271_write_regs(fe, R_EB12, 1);
305 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
306 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
307 ret = tda18271_write_regs(fe, R_EB18, 1);
311 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
314 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
318 /* disable 1.5 MHz low pass filter */
319 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
320 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
321 ret = tda18271_write_regs(fe, R_EB21, 3);
326 static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
328 struct tda18271_priv *priv = fe->tuner_priv;
329 unsigned char *regs = priv->tda18271_regs;
332 /* set CAL mode to normal */
333 regs[R_EP4] &= ~0x03;
334 tda18271_write_regs(fe, R_EP4, 1);
336 /* switch off agc1 */
337 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
339 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
340 tda18271_write_regs(fe, R_EB18, 1);
342 /* frequency dependent parameters */
344 tda18271_calc_bp_filter(fe, &freq);
345 tda18271_calc_gain_taper(fe, &freq);
346 tda18271_calc_rf_band(fe, &freq);
347 tda18271_calc_km(fe, &freq);
349 tda18271_write_regs(fe, R_EP1, 3);
350 tda18271_write_regs(fe, R_EB13, 1);
352 /* main pll charge pump source */
353 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
355 /* cal pll charge pump source */
356 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
358 /* force dcdc converter to 0 V */
360 tda18271_write_regs(fe, R_EB14, 1);
362 /* disable plls lock */
363 regs[R_EB20] &= ~0x20;
364 tda18271_write_regs(fe, R_EB20, 1);
366 /* set CAL mode to RF tracking filter calibration */
368 tda18271_write_regs(fe, R_EP4, 2);
370 /* --------------------------------------------------------------- */
372 /* set the internal calibration signal */
375 tda18271_calc_cal_pll(fe, N);
376 tda18271_write_regs(fe, R_CPD, 4);
378 /* downconvert internal calibration */
381 tda18271_calc_main_pll(fe, N);
382 tda18271_write_regs(fe, R_MPD, 4);
386 tda18271_write_regs(fe, R_EP2, 1);
387 tda18271_write_regs(fe, R_EP1, 1);
388 tda18271_write_regs(fe, R_EP2, 1);
389 tda18271_write_regs(fe, R_EP1, 1);
391 /* --------------------------------------------------------------- */
393 /* normal operation for the main pll */
394 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
396 /* normal operation for the cal pll */
397 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
399 msleep(10); /* plls locking */
401 /* launch the rf tracking filters calibration */
402 regs[R_EB20] |= 0x20;
403 tda18271_write_regs(fe, R_EB20, 1);
405 msleep(60); /* calibration */
407 /* --------------------------------------------------------------- */
409 /* set CAL mode to normal */
410 regs[R_EP4] &= ~0x03;
413 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
415 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
416 tda18271_write_regs(fe, R_EB18, 1);
418 tda18271_write_regs(fe, R_EP3, 2);
420 /* synchronization */
421 tda18271_write_regs(fe, R_EP1, 1);
423 /* get calibration result */
424 tda18271_read_extended(fe);
429 static int tda18271_powerscan(struct dvb_frontend *fe,
430 u32 *freq_in, u32 *freq_out)
432 struct tda18271_priv *priv = fe->tuner_priv;
433 unsigned char *regs = priv->tda18271_regs;
434 int sgn, bcal, count, wait, ret;
441 tda18271_calc_rf_band(fe, &freq);
442 tda18271_calc_rf_cal(fe, &freq);
443 tda18271_calc_gain_taper(fe, &freq);
444 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
446 tda18271_write_regs(fe, R_EP2, 1);
447 tda18271_write_regs(fe, R_EB14, 1);
449 /* downconvert frequency */
452 tda18271_calc_main_pll(fe, freq);
453 tda18271_write_regs(fe, R_MPD, 4);
455 msleep(5); /* pll locking */
458 regs[R_EP4] &= ~0x03;
460 tda18271_write_regs(fe, R_EP4, 1);
462 /* launch power detection measurement */
463 tda18271_write_regs(fe, R_EP2, 1);
465 /* read power detection info, stored in EB10 */
466 ret = tda18271_read_extended(fe);
470 /* algorithm initialization */
472 *freq_out = *freq_in;
477 while ((regs[R_EB10] & 0x3f) < cid_target) {
478 /* downconvert updated freq to 1 MHz */
479 freq = *freq_in + (sgn * count) + 1000000;
481 tda18271_calc_main_pll(fe, freq);
482 tda18271_write_regs(fe, R_MPD, 4);
485 msleep(5); /* pll locking */
488 udelay(100); /* pll locking */
490 /* launch power detection measurement */
491 tda18271_write_regs(fe, R_EP2, 1);
493 /* read power detection info, stored in EB10 */
494 ret = tda18271_read_extended(fe);
500 if (count <= count_limit)
511 if ((regs[R_EB10] & 0x3f) >= cid_target) {
513 *freq_out = freq - 1000000;
517 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
518 bcal, *freq_in, *freq_out, freq);
523 static int tda18271_powerscan_init(struct dvb_frontend *fe)
525 struct tda18271_priv *priv = fe->tuner_priv;
526 unsigned char *regs = priv->tda18271_regs;
529 /* set standard to digital */
530 regs[R_EP3] &= ~0x1f; /* clear std bits */
533 /* set cal mode to normal */
534 regs[R_EP4] &= ~0x03;
536 /* update IF output level */
537 regs[R_EP4] &= ~0x1c; /* clear if level bits */
539 ret = tda18271_write_regs(fe, R_EP3, 2);
543 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
544 ret = tda18271_write_regs(fe, R_EB18, 1);
548 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
550 /* 1.5 MHz low pass filter */
551 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
552 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
554 ret = tda18271_write_regs(fe, R_EB21, 3);
559 static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
561 struct tda18271_priv *priv = fe->tuner_priv;
562 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
563 unsigned char *regs = priv->tda18271_regs;
565 s32 divisor, dividend;
574 i = tda18271_lookup_rf_band(fe, &freq, NULL);
579 rf_default[RF1] = 1000 * map[i].rf1_def;
580 rf_default[RF2] = 1000 * map[i].rf2_def;
581 rf_default[RF3] = 1000 * map[i].rf3_def;
583 for (rf = RF1; rf <= RF3; rf++) {
584 if (0 == rf_default[rf])
586 tda_cal("freq = %d, rf = %d\n", freq, rf);
588 /* look for optimized calibration frequency */
589 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
593 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
594 prog_tab[rf] = (s32)regs[R_EB14];
598 (s32)tda18271_calibrate_rf(fe, rf_freq[rf]);
600 prog_cal[rf] = prog_tab[rf];
605 map[i].rf_b1 = (prog_cal[RF1] - prog_tab[RF1]);
606 map[i].rf1 = rf_freq[RF1] / 1000;
609 dividend = (prog_cal[RF2] - prog_tab[RF2] -
610 prog_cal[RF1] + prog_tab[RF1]);
611 divisor = (s32)(rf_freq[RF2] - rf_freq[RF1]) / 1000;
612 map[i].rf_a1 = (dividend / divisor);
613 map[i].rf2 = rf_freq[RF2] / 1000;
616 dividend = (prog_cal[RF3] - prog_tab[RF3] -
617 prog_cal[RF2] + prog_tab[RF2]);
618 divisor = (s32)(rf_freq[RF3] - rf_freq[RF2]) / 1000;
619 map[i].rf_a2 = (dividend / divisor);
620 map[i].rf_b2 = (prog_cal[RF2] - prog_tab[RF2]);
621 map[i].rf3 = rf_freq[RF3] / 1000;
631 static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
633 struct tda18271_priv *priv = fe->tuner_priv;
637 tda_info("performing RF tracking filter calibration\n");
639 /* wait for die temperature stabilization */
642 ret = tda18271_powerscan_init(fe);
646 /* rf band calibration */
647 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
649 tda18271_rf_tracking_filters_init(fe, 1000 *
650 priv->rf_cal_state[i].rfmax);
655 priv->tm_rfcal = tda18271_read_thermometer(fe);
660 /* ------------------------------------------------------------------ */
662 static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
664 struct tda18271_priv *priv = fe->tuner_priv;
665 unsigned char *regs = priv->tda18271_regs;
668 /* test RF_CAL_OK to see if we need init */
669 if ((regs[R_EP1] & 0x10) == 0)
670 priv->cal_initialized = false;
672 if (priv->cal_initialized)
675 ret = tda18271_calc_rf_filter_curve(fe);
679 ret = tda18271_por(fe);
683 tda_info("RF tracking filter calibration complete\n");
685 priv->cal_initialized = true;
688 tda_info("RF tracking filter calibration failed!\n");
693 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
696 struct tda18271_priv *priv = fe->tuner_priv;
697 unsigned char *regs = priv->tda18271_regs;
701 /* calculate bp filter */
702 tda18271_calc_bp_filter(fe, &freq);
703 tda18271_write_regs(fe, R_EP1, 1);
707 tda18271_write_regs(fe, R_EB4, 1);
710 tda18271_write_regs(fe, R_EB7, 1);
713 tda18271_write_regs(fe, R_EB14, 1);
716 tda18271_write_regs(fe, R_EB20, 1);
718 /* set cal mode to RF tracking filter calibration */
721 /* calculate cal pll */
723 switch (priv->mode) {
724 case TDA18271_ANALOG:
727 case TDA18271_DIGITAL:
732 tda18271_calc_cal_pll(fe, N);
734 /* calculate main pll */
736 switch (priv->mode) {
737 case TDA18271_ANALOG:
740 case TDA18271_DIGITAL:
741 N = freq + bw / 2 + 1000000;
745 tda18271_calc_main_pll(fe, N);
747 ret = tda18271_write_regs(fe, R_EP3, 11);
751 msleep(5); /* RF tracking filter calibration initialization */
753 /* search for K,M,CO for RF calibration */
754 tda18271_calc_km(fe, &freq);
755 tda18271_write_regs(fe, R_EB13, 1);
757 /* search for rf band */
758 tda18271_calc_rf_band(fe, &freq);
760 /* search for gain taper */
761 tda18271_calc_gain_taper(fe, &freq);
763 tda18271_write_regs(fe, R_EP2, 1);
764 tda18271_write_regs(fe, R_EP1, 1);
765 tda18271_write_regs(fe, R_EP2, 1);
766 tda18271_write_regs(fe, R_EP1, 1);
770 tda18271_write_regs(fe, R_EB4, 1);
773 tda18271_write_regs(fe, R_EB7, 1);
774 msleep(10); /* pll locking */
777 tda18271_write_regs(fe, R_EB20, 1);
778 msleep(60); /* RF tracking filter calibration completion */
780 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
781 tda18271_write_regs(fe, R_EP4, 1);
783 tda18271_write_regs(fe, R_EP1, 1);
785 /* RF tracking filter correction for VHF_Low band */
786 if (0 == tda18271_calc_rf_cal(fe, &freq))
787 tda18271_write_regs(fe, R_EB14, 1);
792 /* ------------------------------------------------------------------ */
794 static int tda18271_ir_cal_init(struct dvb_frontend *fe)
796 struct tda18271_priv *priv = fe->tuner_priv;
797 unsigned char *regs = priv->tda18271_regs;
800 ret = tda18271_read_regs(fe);
804 /* test IR_CAL_OK to see if we need init */
805 if ((regs[R_EP1] & 0x08) == 0)
806 ret = tda18271_init_regs(fe);
811 static int tda18271_init(struct dvb_frontend *fe)
813 struct tda18271_priv *priv = fe->tuner_priv;
816 mutex_lock(&priv->lock);
819 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
824 ret = tda18271_ir_cal_init(fe);
828 if (priv->id == TDA18271HDC2)
829 tda18271c2_rf_cal_init(fe);
831 mutex_unlock(&priv->lock);
836 static int tda18271_sleep(struct dvb_frontend *fe)
838 struct tda18271_priv *priv = fe->tuner_priv;
841 mutex_lock(&priv->lock);
843 /* enter standby mode, with required output features enabled */
844 ret = tda18271_toggle_output(fe, 1);
846 mutex_unlock(&priv->lock);
851 /* ------------------------------------------------------------------ */
853 static int tda18271_agc(struct dvb_frontend *fe)
855 struct tda18271_priv *priv = fe->tuner_priv;
858 switch (priv->config) {
859 case TDA8290_LNA_OFF:
860 /* no external agc configuration required */
861 if (tda18271_debug & DBG_ADV)
862 tda_dbg("no agc configuration provided\n");
864 case TDA8290_LNA_ON_BRIDGE:
865 /* switch with GPIO of saa713x */
866 tda_dbg("invoking callback\n");
868 ret = fe->callback(priv->i2c_props.adap->algo_data,
869 DVB_FRONTEND_COMPONENT_TUNER,
870 TDA18271_CALLBACK_CMD_AGC_ENABLE,
873 case TDA8290_LNA_GP0_HIGH_ON:
874 case TDA8290_LNA_GP0_HIGH_OFF:
876 /* n/a - currently not supported */
877 tda_err("unsupported configuration: %d\n", priv->config);
884 static int tda18271_tune(struct dvb_frontend *fe,
885 struct tda18271_std_map_item *map, u32 freq, u32 bw)
887 struct tda18271_priv *priv = fe->tuner_priv;
890 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
891 freq, map->if_freq, bw, map->agc_mode, map->std);
893 ret = tda18271_agc(fe);
895 tda_warn("failed to configure agc\n");
897 ret = tda18271_init(fe);
901 mutex_lock(&priv->lock);
905 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
908 tda18271c2_rf_tracking_filters_correction(fe, freq);
911 ret = tda18271_channel_configuration(fe, map, freq, bw);
913 mutex_unlock(&priv->lock);
918 /* ------------------------------------------------------------------ */
920 static int tda18271_set_params(struct dvb_frontend *fe)
922 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
923 u32 delsys = c->delivery_system;
924 u32 bw = c->bandwidth_hz;
925 u32 freq = c->frequency;
926 struct tda18271_priv *priv = fe->tuner_priv;
927 struct tda18271_std_map *std_map = &priv->std;
928 struct tda18271_std_map_item *map;
931 priv->mode = TDA18271_DIGITAL;
935 map = &std_map->atsc_6;
942 map = &std_map->dvbt_6;
943 } else if (bw <= 7000000) {
944 map = &std_map->dvbt_7;
946 map = &std_map->dvbt_8;
949 case SYS_DVBC_ANNEX_B:
952 case SYS_DVBC_ANNEX_A:
953 case SYS_DVBC_ANNEX_C:
955 map = &std_map->qam_6;
956 } else if (bw <= 7000000) {
957 map = &std_map->qam_7;
959 map = &std_map->qam_8;
963 tda_warn("modulation type not supported!\n");
967 /* When tuning digital, the analog demod must be tri-stated */
968 if (fe->ops.analog_ops.standby)
969 fe->ops.analog_ops.standby(fe);
971 ret = tda18271_tune(fe, map, freq, bw);
976 priv->if_freq = map->if_freq;
977 priv->frequency = freq;
978 priv->bandwidth = bw;
983 static int tda18271_set_analog_params(struct dvb_frontend *fe,
984 struct analog_parameters *params)
986 struct tda18271_priv *priv = fe->tuner_priv;
987 struct tda18271_std_map *std_map = &priv->std;
988 struct tda18271_std_map_item *map;
991 u32 freq = params->frequency * 125 *
992 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
994 priv->mode = TDA18271_ANALOG;
996 if (params->mode == V4L2_TUNER_RADIO) {
997 map = &std_map->fm_radio;
999 } else if (params->std & V4L2_STD_MN) {
1000 map = &std_map->atv_mn;
1002 } else if (params->std & V4L2_STD_B) {
1003 map = &std_map->atv_b;
1005 } else if (params->std & V4L2_STD_GH) {
1006 map = &std_map->atv_gh;
1008 } else if (params->std & V4L2_STD_PAL_I) {
1009 map = &std_map->atv_i;
1011 } else if (params->std & V4L2_STD_DK) {
1012 map = &std_map->atv_dk;
1014 } else if (params->std & V4L2_STD_SECAM_L) {
1015 map = &std_map->atv_l;
1017 } else if (params->std & V4L2_STD_SECAM_LC) {
1018 map = &std_map->atv_lc;
1021 map = &std_map->atv_i;
1025 tda_dbg("setting tda18271 to system %s\n", mode);
1027 ret = tda18271_tune(fe, map, freq, 0);
1032 priv->if_freq = map->if_freq;
1033 priv->frequency = freq;
1034 priv->bandwidth = 0;
1039 static void tda18271_release(struct dvb_frontend *fe)
1041 struct tda18271_priv *priv = fe->tuner_priv;
1043 mutex_lock(&tda18271_list_mutex);
1046 hybrid_tuner_release_state(priv);
1048 mutex_unlock(&tda18271_list_mutex);
1050 fe->tuner_priv = NULL;
1053 static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1055 struct tda18271_priv *priv = fe->tuner_priv;
1056 *frequency = priv->frequency;
1060 static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1062 struct tda18271_priv *priv = fe->tuner_priv;
1063 *bandwidth = priv->bandwidth;
1067 static int tda18271_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
1069 struct tda18271_priv *priv = fe->tuner_priv;
1070 *frequency = (u32)priv->if_freq * 1000;
1074 /* ------------------------------------------------------------------ */
1076 #define tda18271_update_std(std_cfg, name) do { \
1077 if (map->std_cfg.if_freq + \
1078 map->std_cfg.agc_mode + map->std_cfg.std + \
1079 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1080 tda_dbg("Using custom std config for %s\n", name); \
1081 memcpy(&std->std_cfg, &map->std_cfg, \
1082 sizeof(struct tda18271_std_map_item)); \
1085 #define tda18271_dump_std_item(std_cfg, name) do { \
1086 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1087 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1088 name, std->std_cfg.if_freq, \
1089 std->std_cfg.agc_mode, std->std_cfg.std, \
1090 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1093 static int tda18271_dump_std_map(struct dvb_frontend *fe)
1095 struct tda18271_priv *priv = fe->tuner_priv;
1096 struct tda18271_std_map *std = &priv->std;
1098 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1099 tda18271_dump_std_item(fm_radio, " fm ");
1100 tda18271_dump_std_item(atv_b, "atv b ");
1101 tda18271_dump_std_item(atv_dk, "atv dk");
1102 tda18271_dump_std_item(atv_gh, "atv gh");
1103 tda18271_dump_std_item(atv_i, "atv i ");
1104 tda18271_dump_std_item(atv_l, "atv l ");
1105 tda18271_dump_std_item(atv_lc, "atv l'");
1106 tda18271_dump_std_item(atv_mn, "atv mn");
1107 tda18271_dump_std_item(atsc_6, "atsc 6");
1108 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1109 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1110 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1111 tda18271_dump_std_item(qam_6, "qam 6 ");
1112 tda18271_dump_std_item(qam_7, "qam 7 ");
1113 tda18271_dump_std_item(qam_8, "qam 8 ");
1118 static int tda18271_update_std_map(struct dvb_frontend *fe,
1119 struct tda18271_std_map *map)
1121 struct tda18271_priv *priv = fe->tuner_priv;
1122 struct tda18271_std_map *std = &priv->std;
1127 tda18271_update_std(fm_radio, "fm");
1128 tda18271_update_std(atv_b, "atv b");
1129 tda18271_update_std(atv_dk, "atv dk");
1130 tda18271_update_std(atv_gh, "atv gh");
1131 tda18271_update_std(atv_i, "atv i");
1132 tda18271_update_std(atv_l, "atv l");
1133 tda18271_update_std(atv_lc, "atv l'");
1134 tda18271_update_std(atv_mn, "atv mn");
1135 tda18271_update_std(atsc_6, "atsc 6");
1136 tda18271_update_std(dvbt_6, "dvbt 6");
1137 tda18271_update_std(dvbt_7, "dvbt 7");
1138 tda18271_update_std(dvbt_8, "dvbt 8");
1139 tda18271_update_std(qam_6, "qam 6");
1140 tda18271_update_std(qam_7, "qam 7");
1141 tda18271_update_std(qam_8, "qam 8");
1146 static int tda18271_get_id(struct dvb_frontend *fe)
1148 struct tda18271_priv *priv = fe->tuner_priv;
1149 unsigned char *regs = priv->tda18271_regs;
1153 mutex_lock(&priv->lock);
1154 ret = tda18271_read_regs(fe);
1155 mutex_unlock(&priv->lock);
1158 tda_info("Error reading device ID @ %d-%04x, bailing out.\n",
1159 i2c_adapter_id(priv->i2c_props.adap),
1160 priv->i2c_props.addr);
1164 switch (regs[R_ID] & 0x7f) {
1166 name = "TDA18271HD/C1";
1167 priv->id = TDA18271HDC1;
1170 name = "TDA18271HD/C2";
1171 priv->id = TDA18271HDC2;
1174 tda_info("Unknown device (%i) detected @ %d-%04x, device not supported.\n",
1175 regs[R_ID], i2c_adapter_id(priv->i2c_props.adap),
1176 priv->i2c_props.addr);
1180 tda_info("%s detected @ %d-%04x\n", name,
1181 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr);
1186 static int tda18271_setup_configuration(struct dvb_frontend *fe,
1187 struct tda18271_config *cfg)
1189 struct tda18271_priv *priv = fe->tuner_priv;
1191 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1192 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1193 priv->config = (cfg) ? cfg->config : 0;
1194 priv->small_i2c = (cfg) ?
1195 cfg->small_i2c : TDA18271_39_BYTE_CHUNK_INIT;
1196 priv->output_opt = (cfg) ?
1197 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
1202 static inline int tda18271_need_cal_on_startup(struct tda18271_config *cfg)
1204 /* tda18271_cal_on_startup == -1 when cal module option is unset */
1205 return ((tda18271_cal_on_startup == -1) ?
1206 /* honor configuration setting */
1207 ((cfg) && (cfg->rf_cal_on_startup)) :
1208 /* module option overrides configuration setting */
1209 (tda18271_cal_on_startup)) ? 1 : 0;
1212 static int tda18271_set_config(struct dvb_frontend *fe, void *priv_cfg)
1214 struct tda18271_config *cfg = (struct tda18271_config *) priv_cfg;
1216 tda18271_setup_configuration(fe, cfg);
1218 if (tda18271_need_cal_on_startup(cfg))
1221 /* override default std map with values in config struct */
1222 if ((cfg) && (cfg->std_map))
1223 tda18271_update_std_map(fe, cfg->std_map);
1228 static const struct dvb_tuner_ops tda18271_tuner_ops = {
1230 .name = "NXP TDA18271HD",
1231 .frequency_min_hz = 45 * MHz,
1232 .frequency_max_hz = 864 * MHz,
1233 .frequency_step_hz = 62500
1235 .init = tda18271_init,
1236 .sleep = tda18271_sleep,
1237 .set_params = tda18271_set_params,
1238 .set_analog_params = tda18271_set_analog_params,
1239 .release = tda18271_release,
1240 .set_config = tda18271_set_config,
1241 .get_frequency = tda18271_get_frequency,
1242 .get_bandwidth = tda18271_get_bandwidth,
1243 .get_if_frequency = tda18271_get_if_frequency,
1246 struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1247 struct i2c_adapter *i2c,
1248 struct tda18271_config *cfg)
1250 struct tda18271_priv *priv = NULL;
1253 mutex_lock(&tda18271_list_mutex);
1255 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1256 hybrid_tuner_instance_list,
1257 i2c, addr, "tda18271");
1262 /* new tuner instance */
1263 fe->tuner_priv = priv;
1265 tda18271_setup_configuration(fe, cfg);
1267 priv->cal_initialized = false;
1268 mutex_init(&priv->lock);
1270 ret = tda18271_get_id(fe);
1274 ret = tda18271_assign_map_layout(fe);
1278 /* if delay_cal is set, delay IR & RF calibration until init()
1279 * module option 'cal' overrides this delay */
1280 if ((cfg->delay_cal) && (!tda18271_need_cal_on_startup(cfg)))
1283 mutex_lock(&priv->lock);
1284 tda18271_init_regs(fe);
1286 if ((tda18271_need_cal_on_startup(cfg)) &&
1287 (priv->id == TDA18271HDC2))
1288 tda18271c2_rf_cal_init(fe);
1290 /* enter standby mode, with required output features enabled */
1291 ret = tda18271_toggle_output(fe, 1);
1294 mutex_unlock(&priv->lock);
1297 /* existing tuner instance */
1298 fe->tuner_priv = priv;
1300 /* allow dvb driver to override configuration settings */
1302 if (cfg->gate != TDA18271_GATE_ANALOG)
1303 priv->gate = cfg->gate;
1305 priv->role = cfg->role;
1307 priv->config = cfg->config;
1309 priv->small_i2c = cfg->small_i2c;
1310 if (cfg->output_opt)
1311 priv->output_opt = cfg->output_opt;
1313 tda18271_update_std_map(fe, cfg->std_map);
1315 if (tda18271_need_cal_on_startup(cfg))
1320 /* override default std map with values in config struct */
1321 if ((cfg) && (cfg->std_map))
1322 tda18271_update_std_map(fe, cfg->std_map);
1324 mutex_unlock(&tda18271_list_mutex);
1326 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1327 sizeof(struct dvb_tuner_ops));
1329 if (tda18271_debug & (DBG_MAP | DBG_ADV))
1330 tda18271_dump_std_map(fe);
1334 mutex_unlock(&tda18271_list_mutex);
1336 tda18271_release(fe);
1339 EXPORT_SYMBOL_GPL(tda18271_attach);
1340 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1342 MODULE_LICENSE("GPL");
1343 MODULE_VERSION("0.4");