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 "tda18271-priv.h"
24 #include <linux/delay.h>
25 #include <linux/videodev2.h>
28 module_param_named(debug
, tda18271_debug
, int, 0644);
29 MODULE_PARM_DESC(debug
, "set debug level "
30 "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
32 static int tda18271_cal_on_startup
= -1;
33 module_param_named(cal
, tda18271_cal_on_startup
, int, 0644);
34 MODULE_PARM_DESC(cal
, "perform RF tracking filter calibration on startup");
36 static DEFINE_MUTEX(tda18271_list_mutex
);
37 static LIST_HEAD(hybrid_tuner_instance_list
);
39 /*---------------------------------------------------------------------*/
41 static int tda18271_toggle_output(struct dvb_frontend
*fe
, int standby
)
43 struct tda18271_priv
*priv
= fe
->tuner_priv
;
45 int ret
= tda18271_set_standby_mode(fe
, standby
? 1 : 0,
46 priv
->output_opt
& TDA18271_OUTPUT_LT_OFF
? 1 : 0,
47 priv
->output_opt
& TDA18271_OUTPUT_XT_OFF
? 1 : 0);
52 tda_dbg("%s mode: xtal oscillator %s, slave tuner loop thru %s\n",
53 standby
? "standby" : "active",
54 priv
->output_opt
& TDA18271_OUTPUT_XT_OFF
? "off" : "on",
55 priv
->output_opt
& TDA18271_OUTPUT_LT_OFF
? "off" : "on");
60 /*---------------------------------------------------------------------*/
62 static inline int charge_pump_source(struct dvb_frontend
*fe
, int force
)
64 struct tda18271_priv
*priv
= fe
->tuner_priv
;
65 return tda18271_charge_pump_source(fe
,
66 (priv
->role
== TDA18271_SLAVE
) ?
68 TDA18271_MAIN_PLL
, force
);
71 static inline void tda18271_set_if_notch(struct dvb_frontend
*fe
)
73 struct tda18271_priv
*priv
= fe
->tuner_priv
;
74 unsigned char *regs
= priv
->tda18271_regs
;
78 regs
[R_MPD
] &= ~0x80; /* IF notch = 0 */
80 case TDA18271_DIGITAL
:
81 regs
[R_MPD
] |= 0x80; /* IF notch = 1 */
86 static int tda18271_channel_configuration(struct dvb_frontend
*fe
,
87 struct tda18271_std_map_item
*map
,
90 struct tda18271_priv
*priv
= fe
->tuner_priv
;
91 unsigned char *regs
= priv
->tda18271_regs
;
95 /* update TV broadcast parameters */
98 regs
[R_EP3
] &= ~0x1f; /* clear std bits */
99 regs
[R_EP3
] |= (map
->agc_mode
<< 3) | map
->std
;
101 if (priv
->id
== TDA18271HDC2
) {
102 /* set rfagc to high speed mode */
103 regs
[R_EP3
] &= ~0x04;
106 /* set cal mode to normal */
107 regs
[R_EP4
] &= ~0x03;
109 /* update IF output level */
110 regs
[R_EP4
] &= ~0x1c; /* clear if level bits */
111 regs
[R_EP4
] |= (map
->if_lvl
<< 2);
114 regs
[R_EP4
] &= ~0x80;
115 regs
[R_EP4
] |= map
->fm_rfn
<< 7;
117 /* update rf top / if top */
119 regs
[R_EB22
] |= map
->rfagc_top
;
120 ret
= tda18271_write_regs(fe
, R_EB22
, 1);
124 /* --------------------------------------------------------------- */
126 /* disable Power Level Indicator */
129 /* make sure thermometer is off */
132 /* frequency dependent parameters */
134 tda18271_calc_ir_measure(fe
, &freq
);
136 tda18271_calc_bp_filter(fe
, &freq
);
138 tda18271_calc_rf_band(fe
, &freq
);
140 tda18271_calc_gain_taper(fe
, &freq
);
142 /* --------------------------------------------------------------- */
144 /* dual tuner and agc1 extra configuration */
146 switch (priv
->role
) {
147 case TDA18271_MASTER
:
148 regs
[R_EB1
] |= 0x04; /* main vco */
151 regs
[R_EB1
] &= ~0x04; /* cal vco */
155 /* agc1 always active */
156 regs
[R_EB1
] &= ~0x02;
158 /* agc1 has priority on agc2 */
159 regs
[R_EB1
] &= ~0x01;
161 ret
= tda18271_write_regs(fe
, R_EB1
, 1);
165 /* --------------------------------------------------------------- */
167 N
= map
->if_freq
* 1000 + freq
;
169 switch (priv
->role
) {
170 case TDA18271_MASTER
:
171 tda18271_calc_main_pll(fe
, N
);
172 tda18271_set_if_notch(fe
);
173 tda18271_write_regs(fe
, R_MPD
, 4);
176 tda18271_calc_cal_pll(fe
, N
);
177 tda18271_write_regs(fe
, R_CPD
, 4);
179 regs
[R_MPD
] = regs
[R_CPD
] & 0x7f;
180 tda18271_set_if_notch(fe
);
181 tda18271_write_regs(fe
, R_MPD
, 1);
185 ret
= tda18271_write_regs(fe
, R_TM
, 7);
189 /* force charge pump source */
190 charge_pump_source(fe
, 1);
194 /* return pll to normal operation */
195 charge_pump_source(fe
, 0);
199 if (priv
->id
== TDA18271HDC2
) {
200 /* set rfagc to normal speed mode */
202 regs
[R_EP3
] &= ~0x04;
205 ret
= tda18271_write_regs(fe
, R_EP3
, 1);
211 static int tda18271_read_thermometer(struct dvb_frontend
*fe
)
213 struct tda18271_priv
*priv
= fe
->tuner_priv
;
214 unsigned char *regs
= priv
->tda18271_regs
;
217 /* switch thermometer on */
219 tda18271_write_regs(fe
, R_TM
, 1);
221 /* read thermometer info */
222 tda18271_read_regs(fe
);
224 if ((((regs
[R_TM
] & 0x0f) == 0x00) && ((regs
[R_TM
] & 0x20) == 0x20)) ||
225 (((regs
[R_TM
] & 0x0f) == 0x08) && ((regs
[R_TM
] & 0x20) == 0x00))) {
227 if ((regs
[R_TM
] & 0x20) == 0x20)
232 tda18271_write_regs(fe
, R_TM
, 1);
234 msleep(10); /* temperature sensing */
236 /* read thermometer info */
237 tda18271_read_regs(fe
);
240 tm
= tda18271_lookup_thermometer(fe
);
242 /* switch thermometer off */
244 tda18271_write_regs(fe
, R_TM
, 1);
246 /* set CAL mode to normal */
247 regs
[R_EP4
] &= ~0x03;
248 tda18271_write_regs(fe
, R_EP4
, 1);
253 /* ------------------------------------------------------------------ */
255 static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend
*fe
,
258 struct tda18271_priv
*priv
= fe
->tuner_priv
;
259 struct tda18271_rf_tracking_filter_cal
*map
= priv
->rf_cal_state
;
260 unsigned char *regs
= priv
->tda18271_regs
;
262 u8 tm_current
, dc_over_dt
, rf_tab
;
263 s32 rfcal_comp
, approx
;
266 ret
= tda18271_set_standby_mode(fe
, 0, 0, 0);
270 /* read die current temperature */
271 tm_current
= tda18271_read_thermometer(fe
);
273 /* frequency dependent parameters */
275 tda18271_calc_rf_cal(fe
, &freq
);
276 rf_tab
= regs
[R_EB14
];
278 i
= tda18271_lookup_rf_band(fe
, &freq
, NULL
);
282 if ((0 == map
[i
].rf3
) || (freq
/ 1000 < map
[i
].rf2
)) {
283 approx
= map
[i
].rf_a1
* (s32
)(freq
/ 1000 - map
[i
].rf1
) +
284 map
[i
].rf_b1
+ rf_tab
;
286 approx
= map
[i
].rf_a2
* (s32
)(freq
/ 1000 - map
[i
].rf2
) +
287 map
[i
].rf_b2
+ rf_tab
;
295 tda18271_lookup_map(fe
, RF_CAL_DC_OVER_DT
, &freq
, &dc_over_dt
);
297 /* calculate temperature compensation */
298 rfcal_comp
= dc_over_dt
* (s32
)(tm_current
- priv
->tm_rfcal
) / 1000;
300 regs
[R_EB14
] = (unsigned char)(approx
+ rfcal_comp
);
301 ret
= tda18271_write_regs(fe
, R_EB14
, 1);
306 static int tda18271_por(struct dvb_frontend
*fe
)
308 struct tda18271_priv
*priv
= fe
->tuner_priv
;
309 unsigned char *regs
= priv
->tda18271_regs
;
312 /* power up detector 1 */
313 regs
[R_EB12
] &= ~0x20;
314 ret
= tda18271_write_regs(fe
, R_EB12
, 1);
318 regs
[R_EB18
] &= ~0x80; /* turn agc1 loop on */
319 regs
[R_EB18
] &= ~0x03; /* set agc1_gain to 6 dB */
320 ret
= tda18271_write_regs(fe
, R_EB18
, 1);
324 regs
[R_EB21
] |= 0x03; /* set agc2_gain to -6 dB */
327 ret
= tda18271_set_standby_mode(fe
, 1, 0, 0);
331 /* disable 1.5 MHz low pass filter */
332 regs
[R_EB23
] &= ~0x04; /* forcelp_fc2_en = 0 */
333 regs
[R_EB23
] &= ~0x02; /* XXX: lp_fc[2] = 0 */
334 ret
= tda18271_write_regs(fe
, R_EB21
, 3);
339 static int tda18271_calibrate_rf(struct dvb_frontend
*fe
, u32 freq
)
341 struct tda18271_priv
*priv
= fe
->tuner_priv
;
342 unsigned char *regs
= priv
->tda18271_regs
;
345 /* set CAL mode to normal */
346 regs
[R_EP4
] &= ~0x03;
347 tda18271_write_regs(fe
, R_EP4
, 1);
349 /* switch off agc1 */
350 regs
[R_EP3
] |= 0x40; /* sm_lt = 1 */
352 regs
[R_EB18
] |= 0x03; /* set agc1_gain to 15 dB */
353 tda18271_write_regs(fe
, R_EB18
, 1);
355 /* frequency dependent parameters */
357 tda18271_calc_bp_filter(fe
, &freq
);
358 tda18271_calc_gain_taper(fe
, &freq
);
359 tda18271_calc_rf_band(fe
, &freq
);
360 tda18271_calc_km(fe
, &freq
);
362 tda18271_write_regs(fe
, R_EP1
, 3);
363 tda18271_write_regs(fe
, R_EB13
, 1);
365 /* main pll charge pump source */
366 tda18271_charge_pump_source(fe
, TDA18271_MAIN_PLL
, 1);
368 /* cal pll charge pump source */
369 tda18271_charge_pump_source(fe
, TDA18271_CAL_PLL
, 1);
371 /* force dcdc converter to 0 V */
373 tda18271_write_regs(fe
, R_EB14
, 1);
375 /* disable plls lock */
376 regs
[R_EB20
] &= ~0x20;
377 tda18271_write_regs(fe
, R_EB20
, 1);
379 /* set CAL mode to RF tracking filter calibration */
381 tda18271_write_regs(fe
, R_EP4
, 2);
383 /* --------------------------------------------------------------- */
385 /* set the internal calibration signal */
388 tda18271_calc_cal_pll(fe
, N
);
389 tda18271_write_regs(fe
, R_CPD
, 4);
391 /* downconvert internal calibration */
394 tda18271_calc_main_pll(fe
, N
);
395 tda18271_write_regs(fe
, R_MPD
, 4);
399 tda18271_write_regs(fe
, R_EP2
, 1);
400 tda18271_write_regs(fe
, R_EP1
, 1);
401 tda18271_write_regs(fe
, R_EP2
, 1);
402 tda18271_write_regs(fe
, R_EP1
, 1);
404 /* --------------------------------------------------------------- */
406 /* normal operation for the main pll */
407 tda18271_charge_pump_source(fe
, TDA18271_MAIN_PLL
, 0);
409 /* normal operation for the cal pll */
410 tda18271_charge_pump_source(fe
, TDA18271_CAL_PLL
, 0);
412 msleep(10); /* plls locking */
414 /* launch the rf tracking filters calibration */
415 regs
[R_EB20
] |= 0x20;
416 tda18271_write_regs(fe
, R_EB20
, 1);
418 msleep(60); /* calibration */
420 /* --------------------------------------------------------------- */
422 /* set CAL mode to normal */
423 regs
[R_EP4
] &= ~0x03;
426 regs
[R_EP3
] &= ~0x40; /* sm_lt = 0 */
428 regs
[R_EB18
] &= ~0x03; /* set agc1_gain to 6 dB */
429 tda18271_write_regs(fe
, R_EB18
, 1);
431 tda18271_write_regs(fe
, R_EP3
, 2);
433 /* synchronization */
434 tda18271_write_regs(fe
, R_EP1
, 1);
436 /* get calibration result */
437 tda18271_read_extended(fe
);
442 static int tda18271_powerscan(struct dvb_frontend
*fe
,
443 u32
*freq_in
, u32
*freq_out
)
445 struct tda18271_priv
*priv
= fe
->tuner_priv
;
446 unsigned char *regs
= priv
->tda18271_regs
;
447 int sgn
, bcal
, count
, wait
, ret
;
454 tda18271_calc_rf_band(fe
, &freq
);
455 tda18271_calc_rf_cal(fe
, &freq
);
456 tda18271_calc_gain_taper(fe
, &freq
);
457 tda18271_lookup_cid_target(fe
, &freq
, &cid_target
, &count_limit
);
459 tda18271_write_regs(fe
, R_EP2
, 1);
460 tda18271_write_regs(fe
, R_EB14
, 1);
462 /* downconvert frequency */
465 tda18271_calc_main_pll(fe
, freq
);
466 tda18271_write_regs(fe
, R_MPD
, 4);
468 msleep(5); /* pll locking */
471 regs
[R_EP4
] &= ~0x03;
473 tda18271_write_regs(fe
, R_EP4
, 1);
475 /* launch power detection measurement */
476 tda18271_write_regs(fe
, R_EP2
, 1);
478 /* read power detection info, stored in EB10 */
479 ret
= tda18271_read_extended(fe
);
483 /* algorithm initialization */
485 *freq_out
= *freq_in
;
490 while ((regs
[R_EB10
] & 0x3f) < cid_target
) {
491 /* downconvert updated freq to 1 MHz */
492 freq
= *freq_in
+ (sgn
* count
) + 1000000;
494 tda18271_calc_main_pll(fe
, freq
);
495 tda18271_write_regs(fe
, R_MPD
, 4);
498 msleep(5); /* pll locking */
501 udelay(100); /* pll locking */
503 /* launch power detection measurement */
504 tda18271_write_regs(fe
, R_EP2
, 1);
506 /* read power detection info, stored in EB10 */
507 ret
= tda18271_read_extended(fe
);
513 if (count
<= count_limit
)
524 if ((regs
[R_EB10
] & 0x3f) >= cid_target
) {
526 *freq_out
= freq
- 1000000;
530 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
531 bcal
, *freq_in
, *freq_out
, freq
);
536 static int tda18271_powerscan_init(struct dvb_frontend
*fe
)
538 struct tda18271_priv
*priv
= fe
->tuner_priv
;
539 unsigned char *regs
= priv
->tda18271_regs
;
542 /* set standard to digital */
543 regs
[R_EP3
] &= ~0x1f; /* clear std bits */
546 /* set cal mode to normal */
547 regs
[R_EP4
] &= ~0x03;
549 /* update IF output level */
550 regs
[R_EP4
] &= ~0x1c; /* clear if level bits */
552 ret
= tda18271_write_regs(fe
, R_EP3
, 2);
556 regs
[R_EB18
] &= ~0x03; /* set agc1_gain to 6 dB */
557 ret
= tda18271_write_regs(fe
, R_EB18
, 1);
561 regs
[R_EB21
] &= ~0x03; /* set agc2_gain to -15 dB */
563 /* 1.5 MHz low pass filter */
564 regs
[R_EB23
] |= 0x04; /* forcelp_fc2_en = 1 */
565 regs
[R_EB23
] |= 0x02; /* lp_fc[2] = 1 */
567 ret
= tda18271_write_regs(fe
, R_EB21
, 3);
572 static int tda18271_rf_tracking_filters_init(struct dvb_frontend
*fe
, u32 freq
)
574 struct tda18271_priv
*priv
= fe
->tuner_priv
;
575 struct tda18271_rf_tracking_filter_cal
*map
= priv
->rf_cal_state
;
576 unsigned char *regs
= priv
->tda18271_regs
;
578 s32 divisor
, dividend
;
587 i
= tda18271_lookup_rf_band(fe
, &freq
, NULL
);
592 rf_default
[RF1
] = 1000 * map
[i
].rf1_def
;
593 rf_default
[RF2
] = 1000 * map
[i
].rf2_def
;
594 rf_default
[RF3
] = 1000 * map
[i
].rf3_def
;
596 for (rf
= RF1
; rf
<= RF3
; rf
++) {
597 if (0 == rf_default
[rf
])
599 tda_cal("freq = %d, rf = %d\n", freq
, rf
);
601 /* look for optimized calibration frequency */
602 bcal
= tda18271_powerscan(fe
, &rf_default
[rf
], &rf_freq
[rf
]);
606 tda18271_calc_rf_cal(fe
, &rf_freq
[rf
]);
607 prog_tab
[rf
] = (s32
)regs
[R_EB14
];
611 (s32
)tda18271_calibrate_rf(fe
, rf_freq
[rf
]);
613 prog_cal
[rf
] = prog_tab
[rf
];
618 map
[i
].rf_b1
= (prog_cal
[RF1
] - prog_tab
[RF1
]);
619 map
[i
].rf1
= rf_freq
[RF1
] / 1000;
622 dividend
= (prog_cal
[RF2
] - prog_tab
[RF2
] -
623 prog_cal
[RF1
] + prog_tab
[RF1
]);
624 divisor
= (s32
)(rf_freq
[RF2
] - rf_freq
[RF1
]) / 1000;
625 map
[i
].rf_a1
= (dividend
/ divisor
);
626 map
[i
].rf2
= rf_freq
[RF2
] / 1000;
629 dividend
= (prog_cal
[RF3
] - prog_tab
[RF3
] -
630 prog_cal
[RF2
] + prog_tab
[RF2
]);
631 divisor
= (s32
)(rf_freq
[RF3
] - rf_freq
[RF2
]) / 1000;
632 map
[i
].rf_a2
= (dividend
/ divisor
);
633 map
[i
].rf_b2
= (prog_cal
[RF2
] - prog_tab
[RF2
]);
634 map
[i
].rf3
= rf_freq
[RF3
] / 1000;
644 static int tda18271_calc_rf_filter_curve(struct dvb_frontend
*fe
)
646 struct tda18271_priv
*priv
= fe
->tuner_priv
;
650 tda_info("performing RF tracking filter calibration\n");
652 /* wait for die temperature stabilization */
655 ret
= tda18271_powerscan_init(fe
);
659 /* rf band calibration */
660 for (i
= 0; priv
->rf_cal_state
[i
].rfmax
!= 0; i
++) {
662 tda18271_rf_tracking_filters_init(fe
, 1000 *
663 priv
->rf_cal_state
[i
].rfmax
);
668 priv
->tm_rfcal
= tda18271_read_thermometer(fe
);
673 /* ------------------------------------------------------------------ */
675 static int tda18271c2_rf_cal_init(struct dvb_frontend
*fe
)
677 struct tda18271_priv
*priv
= fe
->tuner_priv
;
678 unsigned char *regs
= priv
->tda18271_regs
;
681 /* test RF_CAL_OK to see if we need init */
682 if ((regs
[R_EP1
] & 0x10) == 0)
683 priv
->cal_initialized
= false;
685 if (priv
->cal_initialized
)
688 ret
= tda18271_calc_rf_filter_curve(fe
);
692 ret
= tda18271_por(fe
);
696 tda_info("RF tracking filter calibration complete\n");
698 priv
->cal_initialized
= true;
701 tda_info("RF tracking filter calibration failed!\n");
706 static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend
*fe
,
709 struct tda18271_priv
*priv
= fe
->tuner_priv
;
710 unsigned char *regs
= priv
->tda18271_regs
;
714 /* calculate bp filter */
715 tda18271_calc_bp_filter(fe
, &freq
);
716 tda18271_write_regs(fe
, R_EP1
, 1);
720 tda18271_write_regs(fe
, R_EB4
, 1);
723 tda18271_write_regs(fe
, R_EB7
, 1);
726 tda18271_write_regs(fe
, R_EB14
, 1);
729 tda18271_write_regs(fe
, R_EB20
, 1);
731 /* set cal mode to RF tracking filter calibration */
734 /* calculate cal pll */
736 switch (priv
->mode
) {
737 case TDA18271_ANALOG
:
740 case TDA18271_DIGITAL
:
745 tda18271_calc_cal_pll(fe
, N
);
747 /* calculate main pll */
749 switch (priv
->mode
) {
750 case TDA18271_ANALOG
:
753 case TDA18271_DIGITAL
:
754 N
= freq
+ bw
/ 2 + 1000000;
758 tda18271_calc_main_pll(fe
, N
);
760 ret
= tda18271_write_regs(fe
, R_EP3
, 11);
764 msleep(5); /* RF tracking filter calibration initialization */
766 /* search for K,M,CO for RF calibration */
767 tda18271_calc_km(fe
, &freq
);
768 tda18271_write_regs(fe
, R_EB13
, 1);
770 /* search for rf band */
771 tda18271_calc_rf_band(fe
, &freq
);
773 /* search for gain taper */
774 tda18271_calc_gain_taper(fe
, &freq
);
776 tda18271_write_regs(fe
, R_EP2
, 1);
777 tda18271_write_regs(fe
, R_EP1
, 1);
778 tda18271_write_regs(fe
, R_EP2
, 1);
779 tda18271_write_regs(fe
, R_EP1
, 1);
783 tda18271_write_regs(fe
, R_EB4
, 1);
786 tda18271_write_regs(fe
, R_EB7
, 1);
787 msleep(10); /* pll locking */
790 tda18271_write_regs(fe
, R_EB20
, 1);
791 msleep(60); /* RF tracking filter calibration completion */
793 regs
[R_EP4
] &= ~0x03; /* set cal mode to normal */
794 tda18271_write_regs(fe
, R_EP4
, 1);
796 tda18271_write_regs(fe
, R_EP1
, 1);
798 /* RF tracking filter correction for VHF_Low band */
799 if (0 == tda18271_calc_rf_cal(fe
, &freq
))
800 tda18271_write_regs(fe
, R_EB14
, 1);
805 /* ------------------------------------------------------------------ */
807 static int tda18271_ir_cal_init(struct dvb_frontend
*fe
)
809 struct tda18271_priv
*priv
= fe
->tuner_priv
;
810 unsigned char *regs
= priv
->tda18271_regs
;
813 ret
= tda18271_read_regs(fe
);
817 /* test IR_CAL_OK to see if we need init */
818 if ((regs
[R_EP1
] & 0x08) == 0)
819 ret
= tda18271_init_regs(fe
);
824 static int tda18271_init(struct dvb_frontend
*fe
)
826 struct tda18271_priv
*priv
= fe
->tuner_priv
;
829 mutex_lock(&priv
->lock
);
832 ret
= tda18271_set_standby_mode(fe
, 0, 0, 0);
837 ret
= tda18271_ir_cal_init(fe
);
841 if (priv
->id
== TDA18271HDC2
)
842 tda18271c2_rf_cal_init(fe
);
844 mutex_unlock(&priv
->lock
);
849 static int tda18271_sleep(struct dvb_frontend
*fe
)
851 struct tda18271_priv
*priv
= fe
->tuner_priv
;
854 mutex_lock(&priv
->lock
);
856 /* enter standby mode, with required output features enabled */
857 ret
= tda18271_toggle_output(fe
, 1);
859 mutex_unlock(&priv
->lock
);
864 /* ------------------------------------------------------------------ */
866 static int tda18271_agc(struct dvb_frontend
*fe
)
868 struct tda18271_priv
*priv
= fe
->tuner_priv
;
871 switch (priv
->config
) {
872 case TDA8290_LNA_OFF
:
873 /* no external agc configuration required */
874 if (tda18271_debug
& DBG_ADV
)
875 tda_dbg("no agc configuration provided\n");
877 case TDA8290_LNA_ON_BRIDGE
:
878 /* switch with GPIO of saa713x */
879 tda_dbg("invoking callback\n");
881 ret
= fe
->callback(priv
->i2c_props
.adap
->algo_data
,
882 DVB_FRONTEND_COMPONENT_TUNER
,
883 TDA18271_CALLBACK_CMD_AGC_ENABLE
,
886 case TDA8290_LNA_GP0_HIGH_ON
:
887 case TDA8290_LNA_GP0_HIGH_OFF
:
889 /* n/a - currently not supported */
890 tda_err("unsupported configuration: %d\n", priv
->config
);
897 static int tda18271_tune(struct dvb_frontend
*fe
,
898 struct tda18271_std_map_item
*map
, u32 freq
, u32 bw
)
900 struct tda18271_priv
*priv
= fe
->tuner_priv
;
903 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
904 freq
, map
->if_freq
, bw
, map
->agc_mode
, map
->std
);
906 ret
= tda18271_agc(fe
);
908 tda_warn("failed to configure agc\n");
910 ret
= tda18271_init(fe
);
914 mutex_lock(&priv
->lock
);
918 tda18271c1_rf_tracking_filter_calibration(fe
, freq
, bw
);
921 tda18271c2_rf_tracking_filters_correction(fe
, freq
);
924 ret
= tda18271_channel_configuration(fe
, map
, freq
, bw
);
926 mutex_unlock(&priv
->lock
);
931 /* ------------------------------------------------------------------ */
933 static int tda18271_set_params(struct dvb_frontend
*fe
)
935 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
936 u32 delsys
= c
->delivery_system
;
937 u32 bw
= c
->bandwidth_hz
;
938 u32 freq
= c
->frequency
;
939 struct tda18271_priv
*priv
= fe
->tuner_priv
;
940 struct tda18271_std_map
*std_map
= &priv
->std
;
941 struct tda18271_std_map_item
*map
;
944 priv
->mode
= TDA18271_DIGITAL
;
948 map
= &std_map
->atsc_6
;
955 map
= &std_map
->dvbt_6
;
956 } else if (bw
<= 7000000) {
957 map
= &std_map
->dvbt_7
;
959 map
= &std_map
->dvbt_8
;
962 case SYS_DVBC_ANNEX_B
:
965 case SYS_DVBC_ANNEX_A
:
966 case SYS_DVBC_ANNEX_C
:
968 map
= &std_map
->qam_6
;
969 } else if (bw
<= 7000000) {
970 map
= &std_map
->qam_7
;
972 map
= &std_map
->qam_8
;
976 tda_warn("modulation type not supported!\n");
980 /* When tuning digital, the analog demod must be tri-stated */
981 if (fe
->ops
.analog_ops
.standby
)
982 fe
->ops
.analog_ops
.standby(fe
);
984 ret
= tda18271_tune(fe
, map
, freq
, bw
);
989 priv
->if_freq
= map
->if_freq
;
990 priv
->frequency
= freq
;
991 priv
->bandwidth
= bw
;
996 static int tda18271_set_analog_params(struct dvb_frontend
*fe
,
997 struct analog_parameters
*params
)
999 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1000 struct tda18271_std_map
*std_map
= &priv
->std
;
1001 struct tda18271_std_map_item
*map
;
1004 u32 freq
= params
->frequency
* 125 *
1005 ((params
->mode
== V4L2_TUNER_RADIO
) ? 1 : 1000) / 2;
1007 priv
->mode
= TDA18271_ANALOG
;
1009 if (params
->mode
== V4L2_TUNER_RADIO
) {
1010 map
= &std_map
->fm_radio
;
1012 } else if (params
->std
& V4L2_STD_MN
) {
1013 map
= &std_map
->atv_mn
;
1015 } else if (params
->std
& V4L2_STD_B
) {
1016 map
= &std_map
->atv_b
;
1018 } else if (params
->std
& V4L2_STD_GH
) {
1019 map
= &std_map
->atv_gh
;
1021 } else if (params
->std
& V4L2_STD_PAL_I
) {
1022 map
= &std_map
->atv_i
;
1024 } else if (params
->std
& V4L2_STD_DK
) {
1025 map
= &std_map
->atv_dk
;
1027 } else if (params
->std
& V4L2_STD_SECAM_L
) {
1028 map
= &std_map
->atv_l
;
1030 } else if (params
->std
& V4L2_STD_SECAM_LC
) {
1031 map
= &std_map
->atv_lc
;
1034 map
= &std_map
->atv_i
;
1038 tda_dbg("setting tda18271 to system %s\n", mode
);
1040 ret
= tda18271_tune(fe
, map
, freq
, 0);
1045 priv
->if_freq
= map
->if_freq
;
1046 priv
->frequency
= freq
;
1047 priv
->bandwidth
= 0;
1052 static int tda18271_release(struct dvb_frontend
*fe
)
1054 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1056 mutex_lock(&tda18271_list_mutex
);
1059 hybrid_tuner_release_state(priv
);
1061 mutex_unlock(&tda18271_list_mutex
);
1063 fe
->tuner_priv
= NULL
;
1068 static int tda18271_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1070 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1071 *frequency
= priv
->frequency
;
1075 static int tda18271_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
1077 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1078 *bandwidth
= priv
->bandwidth
;
1082 static int tda18271_get_if_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
1084 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1085 *frequency
= (u32
)priv
->if_freq
* 1000;
1089 /* ------------------------------------------------------------------ */
1091 #define tda18271_update_std(std_cfg, name) do { \
1092 if (map->std_cfg.if_freq + \
1093 map->std_cfg.agc_mode + map->std_cfg.std + \
1094 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
1095 tda_dbg("Using custom std config for %s\n", name); \
1096 memcpy(&std->std_cfg, &map->std_cfg, \
1097 sizeof(struct tda18271_std_map_item)); \
1100 #define tda18271_dump_std_item(std_cfg, name) do { \
1101 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1102 "if_lvl = %d, rfagc_top = 0x%02x\n", \
1103 name, std->std_cfg.if_freq, \
1104 std->std_cfg.agc_mode, std->std_cfg.std, \
1105 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
1108 static int tda18271_dump_std_map(struct dvb_frontend
*fe
)
1110 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1111 struct tda18271_std_map
*std
= &priv
->std
;
1113 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
1114 tda18271_dump_std_item(fm_radio
, " fm ");
1115 tda18271_dump_std_item(atv_b
, "atv b ");
1116 tda18271_dump_std_item(atv_dk
, "atv dk");
1117 tda18271_dump_std_item(atv_gh
, "atv gh");
1118 tda18271_dump_std_item(atv_i
, "atv i ");
1119 tda18271_dump_std_item(atv_l
, "atv l ");
1120 tda18271_dump_std_item(atv_lc
, "atv l'");
1121 tda18271_dump_std_item(atv_mn
, "atv mn");
1122 tda18271_dump_std_item(atsc_6
, "atsc 6");
1123 tda18271_dump_std_item(dvbt_6
, "dvbt 6");
1124 tda18271_dump_std_item(dvbt_7
, "dvbt 7");
1125 tda18271_dump_std_item(dvbt_8
, "dvbt 8");
1126 tda18271_dump_std_item(qam_6
, "qam 6 ");
1127 tda18271_dump_std_item(qam_7
, "qam 7 ");
1128 tda18271_dump_std_item(qam_8
, "qam 8 ");
1133 static int tda18271_update_std_map(struct dvb_frontend
*fe
,
1134 struct tda18271_std_map
*map
)
1136 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1137 struct tda18271_std_map
*std
= &priv
->std
;
1142 tda18271_update_std(fm_radio
, "fm");
1143 tda18271_update_std(atv_b
, "atv b");
1144 tda18271_update_std(atv_dk
, "atv dk");
1145 tda18271_update_std(atv_gh
, "atv gh");
1146 tda18271_update_std(atv_i
, "atv i");
1147 tda18271_update_std(atv_l
, "atv l");
1148 tda18271_update_std(atv_lc
, "atv l'");
1149 tda18271_update_std(atv_mn
, "atv mn");
1150 tda18271_update_std(atsc_6
, "atsc 6");
1151 tda18271_update_std(dvbt_6
, "dvbt 6");
1152 tda18271_update_std(dvbt_7
, "dvbt 7");
1153 tda18271_update_std(dvbt_8
, "dvbt 8");
1154 tda18271_update_std(qam_6
, "qam 6");
1155 tda18271_update_std(qam_7
, "qam 7");
1156 tda18271_update_std(qam_8
, "qam 8");
1161 static int tda18271_get_id(struct dvb_frontend
*fe
)
1163 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1164 unsigned char *regs
= priv
->tda18271_regs
;
1168 mutex_lock(&priv
->lock
);
1169 ret
= tda18271_read_regs(fe
);
1170 mutex_unlock(&priv
->lock
);
1173 tda_info("Error reading device ID @ %d-%04x, bailing out.\n",
1174 i2c_adapter_id(priv
->i2c_props
.adap
),
1175 priv
->i2c_props
.addr
);
1179 switch (regs
[R_ID
] & 0x7f) {
1181 name
= "TDA18271HD/C1";
1182 priv
->id
= TDA18271HDC1
;
1185 name
= "TDA18271HD/C2";
1186 priv
->id
= TDA18271HDC2
;
1189 tda_info("Unknown device (%i) detected @ %d-%04x, device not supported.\n",
1190 regs
[R_ID
], i2c_adapter_id(priv
->i2c_props
.adap
),
1191 priv
->i2c_props
.addr
);
1195 tda_info("%s detected @ %d-%04x\n", name
,
1196 i2c_adapter_id(priv
->i2c_props
.adap
), priv
->i2c_props
.addr
);
1201 static int tda18271_setup_configuration(struct dvb_frontend
*fe
,
1202 struct tda18271_config
*cfg
)
1204 struct tda18271_priv
*priv
= fe
->tuner_priv
;
1206 priv
->gate
= (cfg
) ? cfg
->gate
: TDA18271_GATE_AUTO
;
1207 priv
->role
= (cfg
) ? cfg
->role
: TDA18271_MASTER
;
1208 priv
->config
= (cfg
) ? cfg
->config
: 0;
1209 priv
->small_i2c
= (cfg
) ?
1210 cfg
->small_i2c
: TDA18271_39_BYTE_CHUNK_INIT
;
1211 priv
->output_opt
= (cfg
) ?
1212 cfg
->output_opt
: TDA18271_OUTPUT_LT_XT_ON
;
1217 static inline int tda18271_need_cal_on_startup(struct tda18271_config
*cfg
)
1219 /* tda18271_cal_on_startup == -1 when cal module option is unset */
1220 return ((tda18271_cal_on_startup
== -1) ?
1221 /* honor configuration setting */
1222 ((cfg
) && (cfg
->rf_cal_on_startup
)) :
1223 /* module option overrides configuration setting */
1224 (tda18271_cal_on_startup
)) ? 1 : 0;
1227 static int tda18271_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
1229 struct tda18271_config
*cfg
= (struct tda18271_config
*) priv_cfg
;
1231 tda18271_setup_configuration(fe
, cfg
);
1233 if (tda18271_need_cal_on_startup(cfg
))
1236 /* override default std map with values in config struct */
1237 if ((cfg
) && (cfg
->std_map
))
1238 tda18271_update_std_map(fe
, cfg
->std_map
);
1243 static const struct dvb_tuner_ops tda18271_tuner_ops
= {
1245 .name
= "NXP TDA18271HD",
1246 .frequency_min
= 45000000,
1247 .frequency_max
= 864000000,
1248 .frequency_step
= 62500
1250 .init
= tda18271_init
,
1251 .sleep
= tda18271_sleep
,
1252 .set_params
= tda18271_set_params
,
1253 .set_analog_params
= tda18271_set_analog_params
,
1254 .release
= tda18271_release
,
1255 .set_config
= tda18271_set_config
,
1256 .get_frequency
= tda18271_get_frequency
,
1257 .get_bandwidth
= tda18271_get_bandwidth
,
1258 .get_if_frequency
= tda18271_get_if_frequency
,
1261 struct dvb_frontend
*tda18271_attach(struct dvb_frontend
*fe
, u8 addr
,
1262 struct i2c_adapter
*i2c
,
1263 struct tda18271_config
*cfg
)
1265 struct tda18271_priv
*priv
= NULL
;
1268 mutex_lock(&tda18271_list_mutex
);
1270 instance
= hybrid_tuner_request_state(struct tda18271_priv
, priv
,
1271 hybrid_tuner_instance_list
,
1272 i2c
, addr
, "tda18271");
1277 /* new tuner instance */
1278 fe
->tuner_priv
= priv
;
1280 tda18271_setup_configuration(fe
, cfg
);
1282 priv
->cal_initialized
= false;
1283 mutex_init(&priv
->lock
);
1285 ret
= tda18271_get_id(fe
);
1289 ret
= tda18271_assign_map_layout(fe
);
1293 /* if delay_cal is set, delay IR & RF calibration until init()
1294 * module option 'cal' overrides this delay */
1295 if ((cfg
->delay_cal
) && (!tda18271_need_cal_on_startup(cfg
)))
1298 mutex_lock(&priv
->lock
);
1299 tda18271_init_regs(fe
);
1301 if ((tda18271_need_cal_on_startup(cfg
)) &&
1302 (priv
->id
== TDA18271HDC2
))
1303 tda18271c2_rf_cal_init(fe
);
1305 /* enter standby mode, with required output features enabled */
1306 ret
= tda18271_toggle_output(fe
, 1);
1309 mutex_unlock(&priv
->lock
);
1312 /* existing tuner instance */
1313 fe
->tuner_priv
= priv
;
1315 /* allow dvb driver to override configuration settings */
1317 if (cfg
->gate
!= TDA18271_GATE_ANALOG
)
1318 priv
->gate
= cfg
->gate
;
1320 priv
->role
= cfg
->role
;
1322 priv
->config
= cfg
->config
;
1324 priv
->small_i2c
= cfg
->small_i2c
;
1325 if (cfg
->output_opt
)
1326 priv
->output_opt
= cfg
->output_opt
;
1328 tda18271_update_std_map(fe
, cfg
->std_map
);
1330 if (tda18271_need_cal_on_startup(cfg
))
1335 /* override default std map with values in config struct */
1336 if ((cfg
) && (cfg
->std_map
))
1337 tda18271_update_std_map(fe
, cfg
->std_map
);
1339 mutex_unlock(&tda18271_list_mutex
);
1341 memcpy(&fe
->ops
.tuner_ops
, &tda18271_tuner_ops
,
1342 sizeof(struct dvb_tuner_ops
));
1344 if (tda18271_debug
& (DBG_MAP
| DBG_ADV
))
1345 tda18271_dump_std_map(fe
);
1349 mutex_unlock(&tda18271_list_mutex
);
1351 tda18271_release(fe
);
1354 EXPORT_SYMBOL_GPL(tda18271_attach
);
1355 MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1356 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1357 MODULE_LICENSE("GPL");
1358 MODULE_VERSION("0.4");