Commit | Line | Data |
---|---|---|
5bea1cd3 | 1 | /* |
6ca04de3 | 2 | tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner |
5bea1cd3 | 3 | |
59067f7e | 4 | Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org> |
5bea1cd3 MK |
5 | |
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. | |
10 | ||
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. | |
15 | ||
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. | |
19 | */ | |
20 | ||
5bea1cd3 MK |
21 | #include <linux/delay.h> |
22 | #include <linux/videodev2.h> | |
6ca04de3 | 23 | #include "tda18271-priv.h" |
5bea1cd3 | 24 | |
b5f3e1e1 | 25 | int tda18271_debug; |
54465b08 | 26 | module_param_named(debug, tda18271_debug, int, 0644); |
0e1fab90 | 27 | MODULE_PARM_DESC(debug, "set debug level " |
cf04d29c | 28 | "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))"); |
5bea1cd3 | 29 | |
bc835d80 | 30 | static int tda18271_cal_on_startup; |
0f96251e MK |
31 | module_param_named(cal, tda18271_cal_on_startup, int, 0644); |
32 | MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup"); | |
33 | ||
a4f263b5 | 34 | static DEFINE_MUTEX(tda18271_list_mutex); |
f9e315a1 | 35 | static LIST_HEAD(hybrid_tuner_instance_list); |
a4f263b5 | 36 | |
5bea1cd3 MK |
37 | /*---------------------------------------------------------------------*/ |
38 | ||
868f5ccd MK |
39 | static inline int charge_pump_source(struct dvb_frontend *fe, int force) |
40 | { | |
41 | struct tda18271_priv *priv = fe->tuner_priv; | |
42 | return tda18271_charge_pump_source(fe, | |
43 | (priv->role == TDA18271_SLAVE) ? | |
44 | TDA18271_CAL_PLL : | |
45 | TDA18271_MAIN_PLL, force); | |
46 | } | |
47 | ||
44e645c2 MK |
48 | static inline void tda18271_set_if_notch(struct dvb_frontend *fe) |
49 | { | |
50 | struct tda18271_priv *priv = fe->tuner_priv; | |
51 | unsigned char *regs = priv->tda18271_regs; | |
52 | ||
53 | switch (priv->mode) { | |
54 | case TDA18271_ANALOG: | |
55 | regs[R_MPD] &= ~0x80; /* IF notch = 0 */ | |
56 | break; | |
57 | case TDA18271_DIGITAL: | |
58 | regs[R_MPD] |= 0x80; /* IF notch = 1 */ | |
59 | break; | |
60 | } | |
61 | } | |
62 | ||
255b5113 | 63 | static int tda18271_channel_configuration(struct dvb_frontend *fe, |
c293d0a7 MK |
64 | struct tda18271_std_map_item *map, |
65 | u32 freq, u32 bw) | |
255b5113 MK |
66 | { |
67 | struct tda18271_priv *priv = fe->tuner_priv; | |
68 | unsigned char *regs = priv->tda18271_regs; | |
31940e39 | 69 | int ret; |
255b5113 MK |
70 | u32 N; |
71 | ||
72 | /* update TV broadcast parameters */ | |
73 | ||
74 | /* set standard */ | |
75 | regs[R_EP3] &= ~0x1f; /* clear std bits */ | |
7f7203df | 76 | regs[R_EP3] |= (map->agc_mode << 3) | map->std; |
255b5113 | 77 | |
51858d13 MK |
78 | if (priv->id == TDA18271HDC2) { |
79 | /* set rfagc to high speed mode */ | |
80 | regs[R_EP3] &= ~0x04; | |
81 | } | |
40194b2b | 82 | |
255b5113 MK |
83 | /* set cal mode to normal */ |
84 | regs[R_EP4] &= ~0x03; | |
85 | ||
44e645c2 | 86 | /* update IF output level */ |
255b5113 | 87 | regs[R_EP4] &= ~0x1c; /* clear if level bits */ |
14c74b23 | 88 | regs[R_EP4] |= (map->if_lvl << 2); |
255b5113 | 89 | |
c293d0a7 MK |
90 | /* update FM_RFn */ |
91 | regs[R_EP4] &= ~0x80; | |
92 | regs[R_EP4] |= map->fm_rfn << 7; | |
255b5113 | 93 | |
c0dc0c11 MK |
94 | /* update rf top / if top */ |
95 | regs[R_EB22] = 0x00; | |
96 | regs[R_EB22] |= map->rfagc_top; | |
31940e39 | 97 | ret = tda18271_write_regs(fe, R_EB22, 1); |
4bd5d107 | 98 | if (tda_fail(ret)) |
31940e39 | 99 | goto fail; |
255b5113 MK |
100 | |
101 | /* --------------------------------------------------------------- */ | |
102 | ||
103 | /* disable Power Level Indicator */ | |
104 | regs[R_EP1] |= 0x40; | |
105 | ||
7ae1ac4c MK |
106 | /* make sure thermometer is off */ |
107 | regs[R_TM] &= ~0x10; | |
108 | ||
255b5113 MK |
109 | /* frequency dependent parameters */ |
110 | ||
111 | tda18271_calc_ir_measure(fe, &freq); | |
112 | ||
113 | tda18271_calc_bp_filter(fe, &freq); | |
114 | ||
115 | tda18271_calc_rf_band(fe, &freq); | |
116 | ||
117 | tda18271_calc_gain_taper(fe, &freq); | |
118 | ||
119 | /* --------------------------------------------------------------- */ | |
120 | ||
121 | /* dual tuner and agc1 extra configuration */ | |
122 | ||
868f5ccd MK |
123 | switch (priv->role) { |
124 | case TDA18271_MASTER: | |
125 | regs[R_EB1] |= 0x04; /* main vco */ | |
126 | break; | |
127 | case TDA18271_SLAVE: | |
128 | regs[R_EB1] &= ~0x04; /* cal vco */ | |
129 | break; | |
130 | } | |
255b5113 MK |
131 | |
132 | /* agc1 always active */ | |
133 | regs[R_EB1] &= ~0x02; | |
134 | ||
135 | /* agc1 has priority on agc2 */ | |
136 | regs[R_EB1] &= ~0x01; | |
137 | ||
31940e39 | 138 | ret = tda18271_write_regs(fe, R_EB1, 1); |
4bd5d107 | 139 | if (tda_fail(ret)) |
31940e39 | 140 | goto fail; |
255b5113 MK |
141 | |
142 | /* --------------------------------------------------------------- */ | |
143 | ||
c293d0a7 | 144 | N = map->if_freq * 1000 + freq; |
255b5113 | 145 | |
868f5ccd MK |
146 | switch (priv->role) { |
147 | case TDA18271_MASTER: | |
148 | tda18271_calc_main_pll(fe, N); | |
44e645c2 | 149 | tda18271_set_if_notch(fe); |
868f5ccd MK |
150 | tda18271_write_regs(fe, R_MPD, 4); |
151 | break; | |
152 | case TDA18271_SLAVE: | |
153 | tda18271_calc_cal_pll(fe, N); | |
154 | tda18271_write_regs(fe, R_CPD, 4); | |
155 | ||
156 | regs[R_MPD] = regs[R_CPD] & 0x7f; | |
44e645c2 | 157 | tda18271_set_if_notch(fe); |
868f5ccd MK |
158 | tda18271_write_regs(fe, R_MPD, 1); |
159 | break; | |
160 | } | |
255b5113 | 161 | |
31940e39 | 162 | ret = tda18271_write_regs(fe, R_TM, 7); |
4bd5d107 | 163 | if (tda_fail(ret)) |
31940e39 | 164 | goto fail; |
255b5113 | 165 | |
868f5ccd MK |
166 | /* force charge pump source */ |
167 | charge_pump_source(fe, 1); | |
255b5113 MK |
168 | |
169 | msleep(1); | |
170 | ||
868f5ccd MK |
171 | /* return pll to normal operation */ |
172 | charge_pump_source(fe, 0); | |
255b5113 | 173 | |
40194b2b MK |
174 | msleep(20); |
175 | ||
51858d13 MK |
176 | if (priv->id == TDA18271HDC2) { |
177 | /* set rfagc to normal speed mode */ | |
178 | if (map->fm_rfn) | |
179 | regs[R_EP3] &= ~0x04; | |
180 | else | |
181 | regs[R_EP3] |= 0x04; | |
182 | ret = tda18271_write_regs(fe, R_EP3, 1); | |
183 | } | |
31940e39 MK |
184 | fail: |
185 | return ret; | |
255b5113 MK |
186 | } |
187 | ||
188 | static int tda18271_read_thermometer(struct dvb_frontend *fe) | |
189 | { | |
190 | struct tda18271_priv *priv = fe->tuner_priv; | |
191 | unsigned char *regs = priv->tda18271_regs; | |
192 | int tm; | |
193 | ||
194 | /* switch thermometer on */ | |
195 | regs[R_TM] |= 0x10; | |
196 | tda18271_write_regs(fe, R_TM, 1); | |
197 | ||
198 | /* read thermometer info */ | |
199 | tda18271_read_regs(fe); | |
200 | ||
201 | if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) || | |
202 | (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) { | |
203 | ||
204 | if ((regs[R_TM] & 0x20) == 0x20) | |
205 | regs[R_TM] &= ~0x20; | |
206 | else | |
207 | regs[R_TM] |= 0x20; | |
208 | ||
209 | tda18271_write_regs(fe, R_TM, 1); | |
210 | ||
211 | msleep(10); /* temperature sensing */ | |
212 | ||
213 | /* read thermometer info */ | |
214 | tda18271_read_regs(fe); | |
215 | } | |
216 | ||
217 | tm = tda18271_lookup_thermometer(fe); | |
218 | ||
219 | /* switch thermometer off */ | |
220 | regs[R_TM] &= ~0x10; | |
221 | tda18271_write_regs(fe, R_TM, 1); | |
222 | ||
223 | /* set CAL mode to normal */ | |
224 | regs[R_EP4] &= ~0x03; | |
225 | tda18271_write_regs(fe, R_EP4, 1); | |
226 | ||
227 | return tm; | |
228 | } | |
229 | ||
12afe378 MK |
230 | /* ------------------------------------------------------------------ */ |
231 | ||
d1c53424 MK |
232 | static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe, |
233 | u32 freq) | |
255b5113 MK |
234 | { |
235 | struct tda18271_priv *priv = fe->tuner_priv; | |
236 | struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state; | |
237 | unsigned char *regs = priv->tda18271_regs; | |
20f42063 | 238 | int tm_current, rfcal_comp, approx, i, ret; |
255b5113 MK |
239 | u8 dc_over_dt, rf_tab; |
240 | ||
241 | /* power up */ | |
20f42063 | 242 | ret = tda18271_set_standby_mode(fe, 0, 0, 0); |
4bd5d107 | 243 | if (tda_fail(ret)) |
20f42063 | 244 | goto fail; |
255b5113 MK |
245 | |
246 | /* read die current temperature */ | |
247 | tm_current = tda18271_read_thermometer(fe); | |
248 | ||
249 | /* frequency dependent parameters */ | |
250 | ||
251 | tda18271_calc_rf_cal(fe, &freq); | |
252 | rf_tab = regs[R_EB14]; | |
253 | ||
254 | i = tda18271_lookup_rf_band(fe, &freq, NULL); | |
4bd5d107 MK |
255 | if (tda_fail(i)) |
256 | return i; | |
255b5113 MK |
257 | |
258 | if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) { | |
259 | approx = map[i].rf_a1 * | |
260 | (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab; | |
261 | } else { | |
262 | approx = map[i].rf_a2 * | |
263 | (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab; | |
264 | } | |
265 | ||
266 | if (approx < 0) | |
267 | approx = 0; | |
268 | if (approx > 255) | |
269 | approx = 255; | |
270 | ||
271 | tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt); | |
272 | ||
273 | /* calculate temperature compensation */ | |
09f83c4f | 274 | rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal); |
255b5113 MK |
275 | |
276 | regs[R_EB14] = approx + rfcal_comp; | |
20f42063 MK |
277 | ret = tda18271_write_regs(fe, R_EB14, 1); |
278 | fail: | |
279 | return ret; | |
255b5113 MK |
280 | } |
281 | ||
282 | static int tda18271_por(struct dvb_frontend *fe) | |
283 | { | |
284 | struct tda18271_priv *priv = fe->tuner_priv; | |
285 | unsigned char *regs = priv->tda18271_regs; | |
24124f78 | 286 | int ret; |
255b5113 MK |
287 | |
288 | /* power up detector 1 */ | |
289 | regs[R_EB12] &= ~0x20; | |
24124f78 | 290 | ret = tda18271_write_regs(fe, R_EB12, 1); |
4bd5d107 | 291 | if (tda_fail(ret)) |
24124f78 | 292 | goto fail; |
255b5113 MK |
293 | |
294 | regs[R_EB18] &= ~0x80; /* turn agc1 loop on */ | |
295 | regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */ | |
24124f78 | 296 | ret = tda18271_write_regs(fe, R_EB18, 1); |
4bd5d107 | 297 | if (tda_fail(ret)) |
24124f78 | 298 | goto fail; |
255b5113 MK |
299 | |
300 | regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */ | |
301 | ||
302 | /* POR mode */ | |
24124f78 | 303 | ret = tda18271_set_standby_mode(fe, 1, 0, 0); |
4bd5d107 | 304 | if (tda_fail(ret)) |
24124f78 | 305 | goto fail; |
255b5113 MK |
306 | |
307 | /* disable 1.5 MHz low pass filter */ | |
308 | regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */ | |
309 | regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */ | |
24124f78 MK |
310 | ret = tda18271_write_regs(fe, R_EB21, 3); |
311 | fail: | |
312 | return ret; | |
255b5113 MK |
313 | } |
314 | ||
315 | static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq) | |
316 | { | |
317 | struct tda18271_priv *priv = fe->tuner_priv; | |
318 | unsigned char *regs = priv->tda18271_regs; | |
319 | u32 N; | |
320 | ||
321 | /* set CAL mode to normal */ | |
322 | regs[R_EP4] &= ~0x03; | |
323 | tda18271_write_regs(fe, R_EP4, 1); | |
324 | ||
325 | /* switch off agc1 */ | |
326 | regs[R_EP3] |= 0x40; /* sm_lt = 1 */ | |
327 | ||
328 | regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */ | |
329 | tda18271_write_regs(fe, R_EB18, 1); | |
330 | ||
331 | /* frequency dependent parameters */ | |
332 | ||
333 | tda18271_calc_bp_filter(fe, &freq); | |
334 | tda18271_calc_gain_taper(fe, &freq); | |
335 | tda18271_calc_rf_band(fe, &freq); | |
336 | tda18271_calc_km(fe, &freq); | |
337 | ||
338 | tda18271_write_regs(fe, R_EP1, 3); | |
339 | tda18271_write_regs(fe, R_EB13, 1); | |
340 | ||
341 | /* main pll charge pump source */ | |
4efb0ca5 | 342 | tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1); |
255b5113 MK |
343 | |
344 | /* cal pll charge pump source */ | |
4efb0ca5 | 345 | tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1); |
255b5113 MK |
346 | |
347 | /* force dcdc converter to 0 V */ | |
348 | regs[R_EB14] = 0x00; | |
349 | tda18271_write_regs(fe, R_EB14, 1); | |
350 | ||
351 | /* disable plls lock */ | |
352 | regs[R_EB20] &= ~0x20; | |
353 | tda18271_write_regs(fe, R_EB20, 1); | |
354 | ||
355 | /* set CAL mode to RF tracking filter calibration */ | |
356 | regs[R_EP4] |= 0x03; | |
357 | tda18271_write_regs(fe, R_EP4, 2); | |
358 | ||
359 | /* --------------------------------------------------------------- */ | |
360 | ||
361 | /* set the internal calibration signal */ | |
362 | N = freq; | |
363 | ||
ae07d042 MK |
364 | tda18271_calc_cal_pll(fe, N); |
365 | tda18271_write_regs(fe, R_CPD, 4); | |
255b5113 MK |
366 | |
367 | /* downconvert internal calibration */ | |
368 | N += 1000000; | |
369 | ||
370 | tda18271_calc_main_pll(fe, N); | |
371 | tda18271_write_regs(fe, R_MPD, 4); | |
372 | ||
373 | msleep(5); | |
374 | ||
375 | tda18271_write_regs(fe, R_EP2, 1); | |
376 | tda18271_write_regs(fe, R_EP1, 1); | |
377 | tda18271_write_regs(fe, R_EP2, 1); | |
378 | tda18271_write_regs(fe, R_EP1, 1); | |
379 | ||
380 | /* --------------------------------------------------------------- */ | |
381 | ||
382 | /* normal operation for the main pll */ | |
4efb0ca5 | 383 | tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0); |
255b5113 MK |
384 | |
385 | /* normal operation for the cal pll */ | |
4efb0ca5 | 386 | tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0); |
255b5113 | 387 | |
ae07d042 | 388 | msleep(10); /* plls locking */ |
255b5113 MK |
389 | |
390 | /* launch the rf tracking filters calibration */ | |
391 | regs[R_EB20] |= 0x20; | |
392 | tda18271_write_regs(fe, R_EB20, 1); | |
393 | ||
394 | msleep(60); /* calibration */ | |
395 | ||
396 | /* --------------------------------------------------------------- */ | |
397 | ||
398 | /* set CAL mode to normal */ | |
399 | regs[R_EP4] &= ~0x03; | |
400 | ||
401 | /* switch on agc1 */ | |
402 | regs[R_EP3] &= ~0x40; /* sm_lt = 0 */ | |
403 | ||
404 | regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */ | |
405 | tda18271_write_regs(fe, R_EB18, 1); | |
406 | ||
407 | tda18271_write_regs(fe, R_EP3, 2); | |
408 | ||
409 | /* synchronization */ | |
410 | tda18271_write_regs(fe, R_EP1, 1); | |
411 | ||
412 | /* get calibration result */ | |
413 | tda18271_read_extended(fe); | |
414 | ||
415 | return regs[R_EB14]; | |
416 | } | |
417 | ||
418 | static int tda18271_powerscan(struct dvb_frontend *fe, | |
419 | u32 *freq_in, u32 *freq_out) | |
420 | { | |
421 | struct tda18271_priv *priv = fe->tuner_priv; | |
422 | unsigned char *regs = priv->tda18271_regs; | |
24124f78 | 423 | int sgn, bcal, count, wait, ret; |
255b5113 MK |
424 | u8 cid_target; |
425 | u16 count_limit; | |
426 | u32 freq; | |
427 | ||
428 | freq = *freq_in; | |
429 | ||
430 | tda18271_calc_rf_band(fe, &freq); | |
431 | tda18271_calc_rf_cal(fe, &freq); | |
432 | tda18271_calc_gain_taper(fe, &freq); | |
433 | tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit); | |
434 | ||
435 | tda18271_write_regs(fe, R_EP2, 1); | |
436 | tda18271_write_regs(fe, R_EB14, 1); | |
437 | ||
438 | /* downconvert frequency */ | |
439 | freq += 1000000; | |
440 | ||
441 | tda18271_calc_main_pll(fe, freq); | |
442 | tda18271_write_regs(fe, R_MPD, 4); | |
443 | ||
444 | msleep(5); /* pll locking */ | |
445 | ||
446 | /* detection mode */ | |
447 | regs[R_EP4] &= ~0x03; | |
448 | regs[R_EP4] |= 0x01; | |
449 | tda18271_write_regs(fe, R_EP4, 1); | |
450 | ||
451 | /* launch power detection measurement */ | |
452 | tda18271_write_regs(fe, R_EP2, 1); | |
453 | ||
454 | /* read power detection info, stored in EB10 */ | |
24124f78 | 455 | ret = tda18271_read_extended(fe); |
4bd5d107 | 456 | if (tda_fail(ret)) |
24124f78 | 457 | return ret; |
255b5113 MK |
458 | |
459 | /* algorithm initialization */ | |
460 | sgn = 1; | |
461 | *freq_out = *freq_in; | |
462 | bcal = 0; | |
463 | count = 0; | |
464 | wait = false; | |
465 | ||
466 | while ((regs[R_EB10] & 0x3f) < cid_target) { | |
467 | /* downconvert updated freq to 1 MHz */ | |
468 | freq = *freq_in + (sgn * count) + 1000000; | |
469 | ||
470 | tda18271_calc_main_pll(fe, freq); | |
471 | tda18271_write_regs(fe, R_MPD, 4); | |
472 | ||
473 | if (wait) { | |
474 | msleep(5); /* pll locking */ | |
475 | wait = false; | |
476 | } else | |
477 | udelay(100); /* pll locking */ | |
478 | ||
479 | /* launch power detection measurement */ | |
480 | tda18271_write_regs(fe, R_EP2, 1); | |
481 | ||
482 | /* read power detection info, stored in EB10 */ | |
24124f78 | 483 | ret = tda18271_read_extended(fe); |
4bd5d107 | 484 | if (tda_fail(ret)) |
24124f78 | 485 | return ret; |
255b5113 MK |
486 | |
487 | count += 200; | |
488 | ||
e7809a07 | 489 | if (count <= count_limit) |
255b5113 MK |
490 | continue; |
491 | ||
492 | if (sgn <= 0) | |
493 | break; | |
494 | ||
495 | sgn = -1 * sgn; | |
496 | count = 200; | |
497 | wait = true; | |
498 | } | |
499 | ||
500 | if ((regs[R_EB10] & 0x3f) >= cid_target) { | |
501 | bcal = 1; | |
502 | *freq_out = freq - 1000000; | |
503 | } else | |
504 | bcal = 0; | |
505 | ||
cf04d29c | 506 | tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n", |
255b5113 MK |
507 | bcal, *freq_in, *freq_out, freq); |
508 | ||
509 | return bcal; | |
510 | } | |
511 | ||
512 | static int tda18271_powerscan_init(struct dvb_frontend *fe) | |
513 | { | |
514 | struct tda18271_priv *priv = fe->tuner_priv; | |
515 | unsigned char *regs = priv->tda18271_regs; | |
24124f78 | 516 | int ret; |
255b5113 MK |
517 | |
518 | /* set standard to digital */ | |
519 | regs[R_EP3] &= ~0x1f; /* clear std bits */ | |
520 | regs[R_EP3] |= 0x12; | |
521 | ||
522 | /* set cal mode to normal */ | |
523 | regs[R_EP4] &= ~0x03; | |
524 | ||
44e645c2 | 525 | /* update IF output level */ |
255b5113 MK |
526 | regs[R_EP4] &= ~0x1c; /* clear if level bits */ |
527 | ||
24124f78 | 528 | ret = tda18271_write_regs(fe, R_EP3, 2); |
4bd5d107 | 529 | if (tda_fail(ret)) |
24124f78 | 530 | goto fail; |
255b5113 MK |
531 | |
532 | regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */ | |
24124f78 | 533 | ret = tda18271_write_regs(fe, R_EB18, 1); |
4bd5d107 | 534 | if (tda_fail(ret)) |
24124f78 | 535 | goto fail; |
255b5113 MK |
536 | |
537 | regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */ | |
538 | ||
539 | /* 1.5 MHz low pass filter */ | |
540 | regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */ | |
541 | regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */ | |
542 | ||
24124f78 MK |
543 | ret = tda18271_write_regs(fe, R_EB21, 3); |
544 | fail: | |
545 | return ret; | |
255b5113 MK |
546 | } |
547 | ||
548 | static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq) | |
549 | { | |
550 | struct tda18271_priv *priv = fe->tuner_priv; | |
551 | struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state; | |
552 | unsigned char *regs = priv->tda18271_regs; | |
553 | int bcal, rf, i; | |
554 | #define RF1 0 | |
555 | #define RF2 1 | |
556 | #define RF3 2 | |
557 | u32 rf_default[3]; | |
558 | u32 rf_freq[3]; | |
559 | u8 prog_cal[3]; | |
560 | u8 prog_tab[3]; | |
561 | ||
562 | i = tda18271_lookup_rf_band(fe, &freq, NULL); | |
563 | ||
4bd5d107 | 564 | if (tda_fail(i)) |
255b5113 MK |
565 | return i; |
566 | ||
567 | rf_default[RF1] = 1000 * map[i].rf1_def; | |
568 | rf_default[RF2] = 1000 * map[i].rf2_def; | |
569 | rf_default[RF3] = 1000 * map[i].rf3_def; | |
570 | ||
571 | for (rf = RF1; rf <= RF3; rf++) { | |
572 | if (0 == rf_default[rf]) | |
573 | return 0; | |
cf04d29c | 574 | tda_cal("freq = %d, rf = %d\n", freq, rf); |
255b5113 MK |
575 | |
576 | /* look for optimized calibration frequency */ | |
577 | bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]); | |
4bd5d107 | 578 | if (tda_fail(bcal)) |
24124f78 | 579 | return bcal; |
255b5113 MK |
580 | |
581 | tda18271_calc_rf_cal(fe, &rf_freq[rf]); | |
582 | prog_tab[rf] = regs[R_EB14]; | |
583 | ||
584 | if (1 == bcal) | |
585 | prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]); | |
586 | else | |
587 | prog_cal[rf] = prog_tab[rf]; | |
588 | ||
589 | switch (rf) { | |
590 | case RF1: | |
591 | map[i].rf_a1 = 0; | |
592 | map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1]; | |
593 | map[i].rf1 = rf_freq[RF1] / 1000; | |
594 | break; | |
595 | case RF2: | |
596 | map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] - | |
597 | prog_cal[RF1] + prog_tab[RF1]) / | |
598 | ((rf_freq[RF2] - rf_freq[RF1]) / 1000); | |
599 | map[i].rf2 = rf_freq[RF2] / 1000; | |
600 | break; | |
601 | case RF3: | |
602 | map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] - | |
603 | prog_cal[RF2] + prog_tab[RF2]) / | |
604 | ((rf_freq[RF3] - rf_freq[RF2]) / 1000); | |
605 | map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2]; | |
606 | map[i].rf3 = rf_freq[RF3] / 1000; | |
607 | break; | |
608 | default: | |
609 | BUG(); | |
610 | } | |
611 | } | |
612 | ||
613 | return 0; | |
614 | } | |
615 | ||
09f83c4f | 616 | static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe) |
255b5113 MK |
617 | { |
618 | struct tda18271_priv *priv = fe->tuner_priv; | |
619 | unsigned int i; | |
24124f78 | 620 | int ret; |
255b5113 MK |
621 | |
622 | tda_info("tda18271: performing RF tracking filter calibration\n"); | |
623 | ||
624 | /* wait for die temperature stabilization */ | |
625 | msleep(200); | |
626 | ||
24124f78 | 627 | ret = tda18271_powerscan_init(fe); |
4bd5d107 | 628 | if (tda_fail(ret)) |
24124f78 | 629 | goto fail; |
255b5113 MK |
630 | |
631 | /* rf band calibration */ | |
c151c32f MK |
632 | for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) { |
633 | ret = | |
255b5113 MK |
634 | tda18271_rf_tracking_filters_init(fe, 1000 * |
635 | priv->rf_cal_state[i].rfmax); | |
4bd5d107 | 636 | if (tda_fail(ret)) |
c151c32f MK |
637 | goto fail; |
638 | } | |
255b5113 | 639 | |
09f83c4f | 640 | priv->tm_rfcal = tda18271_read_thermometer(fe); |
24124f78 MK |
641 | fail: |
642 | return ret; | |
255b5113 MK |
643 | } |
644 | ||
645 | /* ------------------------------------------------------------------ */ | |
646 | ||
12afe378 | 647 | static int tda18271c2_rf_cal_init(struct dvb_frontend *fe) |
255b5113 MK |
648 | { |
649 | struct tda18271_priv *priv = fe->tuner_priv; | |
839c6c96 | 650 | unsigned char *regs = priv->tda18271_regs; |
24124f78 | 651 | int ret; |
839c6c96 MK |
652 | |
653 | /* test RF_CAL_OK to see if we need init */ | |
654 | if ((regs[R_EP1] & 0x10) == 0) | |
655 | priv->cal_initialized = false; | |
255b5113 MK |
656 | |
657 | if (priv->cal_initialized) | |
658 | return 0; | |
659 | ||
24124f78 | 660 | ret = tda18271_calc_rf_filter_curve(fe); |
4bd5d107 | 661 | if (tda_fail(ret)) |
24124f78 | 662 | goto fail; |
255b5113 | 663 | |
24124f78 | 664 | ret = tda18271_por(fe); |
4bd5d107 | 665 | if (tda_fail(ret)) |
24124f78 | 666 | goto fail; |
255b5113 | 667 | |
6bfa6657 MK |
668 | tda_info("tda18271: RF tracking filter calibration complete\n"); |
669 | ||
255b5113 | 670 | priv->cal_initialized = true; |
c151c32f | 671 | goto end; |
24124f78 | 672 | fail: |
c151c32f MK |
673 | tda_info("tda18271: RF tracking filter calibration failed!\n"); |
674 | end: | |
24124f78 | 675 | return ret; |
255b5113 MK |
676 | } |
677 | ||
4d2d42bc MK |
678 | static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe, |
679 | u32 freq, u32 bw) | |
5bea1cd3 MK |
680 | { |
681 | struct tda18271_priv *priv = fe->tuner_priv; | |
682 | unsigned char *regs = priv->tda18271_regs; | |
10ed0bf4 | 683 | int ret; |
fe0bf6d7 | 684 | u32 N = 0; |
5bea1cd3 | 685 | |
255b5113 | 686 | /* calculate bp filter */ |
b92bf0f6 | 687 | tda18271_calc_bp_filter(fe, &freq); |
5bea1cd3 MK |
688 | tda18271_write_regs(fe, R_EP1, 1); |
689 | ||
690 | regs[R_EB4] &= 0x07; | |
691 | regs[R_EB4] |= 0x60; | |
692 | tda18271_write_regs(fe, R_EB4, 1); | |
693 | ||
694 | regs[R_EB7] = 0x60; | |
695 | tda18271_write_regs(fe, R_EB7, 1); | |
696 | ||
697 | regs[R_EB14] = 0x00; | |
698 | tda18271_write_regs(fe, R_EB14, 1); | |
699 | ||
700 | regs[R_EB20] = 0xcc; | |
701 | tda18271_write_regs(fe, R_EB20, 1); | |
702 | ||
255b5113 | 703 | /* set cal mode to RF tracking filter calibration */ |
26501a70 | 704 | regs[R_EP4] |= 0x03; |
5bea1cd3 | 705 | |
255b5113 | 706 | /* calculate cal pll */ |
5bea1cd3 MK |
707 | |
708 | switch (priv->mode) { | |
709 | case TDA18271_ANALOG: | |
710 | N = freq - 1250000; | |
711 | break; | |
712 | case TDA18271_DIGITAL: | |
713 | N = freq + bw / 2; | |
714 | break; | |
715 | } | |
716 | ||
fe0bf6d7 | 717 | tda18271_calc_cal_pll(fe, N); |
5bea1cd3 | 718 | |
255b5113 | 719 | /* calculate main pll */ |
5bea1cd3 MK |
720 | |
721 | switch (priv->mode) { | |
722 | case TDA18271_ANALOG: | |
723 | N = freq - 250000; | |
724 | break; | |
725 | case TDA18271_DIGITAL: | |
726 | N = freq + bw / 2 + 1000000; | |
727 | break; | |
728 | } | |
729 | ||
fe0bf6d7 | 730 | tda18271_calc_main_pll(fe, N); |
5bea1cd3 | 731 | |
10ed0bf4 | 732 | ret = tda18271_write_regs(fe, R_EP3, 11); |
4bd5d107 | 733 | if (tda_fail(ret)) |
10ed0bf4 MK |
734 | return ret; |
735 | ||
5bea1cd3 MK |
736 | msleep(5); /* RF tracking filter calibration initialization */ |
737 | ||
255b5113 | 738 | /* search for K,M,CO for RF calibration */ |
b92bf0f6 | 739 | tda18271_calc_km(fe, &freq); |
5bea1cd3 MK |
740 | tda18271_write_regs(fe, R_EB13, 1); |
741 | ||
255b5113 | 742 | /* search for rf band */ |
b92bf0f6 | 743 | tda18271_calc_rf_band(fe, &freq); |
5bea1cd3 | 744 | |
255b5113 | 745 | /* search for gain taper */ |
b92bf0f6 | 746 | tda18271_calc_gain_taper(fe, &freq); |
5bea1cd3 MK |
747 | |
748 | tda18271_write_regs(fe, R_EP2, 1); | |
749 | tda18271_write_regs(fe, R_EP1, 1); | |
750 | tda18271_write_regs(fe, R_EP2, 1); | |
751 | tda18271_write_regs(fe, R_EP1, 1); | |
752 | ||
753 | regs[R_EB4] &= 0x07; | |
754 | regs[R_EB4] |= 0x40; | |
755 | tda18271_write_regs(fe, R_EB4, 1); | |
756 | ||
757 | regs[R_EB7] = 0x40; | |
758 | tda18271_write_regs(fe, R_EB7, 1); | |
4d2d42bc | 759 | msleep(10); /* pll locking */ |
5bea1cd3 MK |
760 | |
761 | regs[R_EB20] = 0xec; | |
762 | tda18271_write_regs(fe, R_EB20, 1); | |
763 | msleep(60); /* RF tracking filter calibration completion */ | |
764 | ||
765 | regs[R_EP4] &= ~0x03; /* set cal mode to normal */ | |
766 | tda18271_write_regs(fe, R_EP4, 1); | |
767 | ||
768 | tda18271_write_regs(fe, R_EP1, 1); | |
769 | ||
b92bf0f6 MK |
770 | /* RF tracking filter correction for VHF_Low band */ |
771 | if (0 == tda18271_calc_rf_cal(fe, &freq)) | |
5bea1cd3 | 772 | tda18271_write_regs(fe, R_EB14, 1); |
5bea1cd3 | 773 | |
4d2d42bc MK |
774 | return 0; |
775 | } | |
776 | ||
d1c53424 MK |
777 | /* ------------------------------------------------------------------ */ |
778 | ||
12afe378 MK |
779 | static int tda18271_ir_cal_init(struct dvb_frontend *fe) |
780 | { | |
781 | struct tda18271_priv *priv = fe->tuner_priv; | |
782 | unsigned char *regs = priv->tda18271_regs; | |
d35fccaf | 783 | int ret; |
12afe378 | 784 | |
d35fccaf | 785 | ret = tda18271_read_regs(fe); |
4bd5d107 | 786 | if (tda_fail(ret)) |
d35fccaf | 787 | goto fail; |
12afe378 MK |
788 | |
789 | /* test IR_CAL_OK to see if we need init */ | |
790 | if ((regs[R_EP1] & 0x08) == 0) | |
d35fccaf MK |
791 | ret = tda18271_init_regs(fe); |
792 | fail: | |
793 | return ret; | |
12afe378 MK |
794 | } |
795 | ||
796 | static int tda18271_init(struct dvb_frontend *fe) | |
797 | { | |
798 | struct tda18271_priv *priv = fe->tuner_priv; | |
d35fccaf | 799 | int ret; |
12afe378 MK |
800 | |
801 | mutex_lock(&priv->lock); | |
802 | ||
803 | /* power up */ | |
d35fccaf | 804 | ret = tda18271_set_standby_mode(fe, 0, 0, 0); |
4bd5d107 | 805 | if (tda_fail(ret)) |
d35fccaf | 806 | goto fail; |
12afe378 MK |
807 | |
808 | /* initialization */ | |
d35fccaf | 809 | ret = tda18271_ir_cal_init(fe); |
4bd5d107 | 810 | if (tda_fail(ret)) |
d35fccaf | 811 | goto fail; |
12afe378 MK |
812 | |
813 | if (priv->id == TDA18271HDC2) | |
814 | tda18271c2_rf_cal_init(fe); | |
d35fccaf | 815 | fail: |
12afe378 MK |
816 | mutex_unlock(&priv->lock); |
817 | ||
d35fccaf | 818 | return ret; |
12afe378 MK |
819 | } |
820 | ||
d1c53424 | 821 | static int tda18271_tune(struct dvb_frontend *fe, |
c293d0a7 | 822 | struct tda18271_std_map_item *map, u32 freq, u32 bw) |
4d2d42bc MK |
823 | { |
824 | struct tda18271_priv *priv = fe->tuner_priv; | |
d35fccaf | 825 | int ret; |
4d2d42bc | 826 | |
7f7203df MK |
827 | tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n", |
828 | freq, map->if_freq, bw, map->agc_mode, map->std); | |
d1c53424 | 829 | |
d35fccaf | 830 | ret = tda18271_init(fe); |
4bd5d107 | 831 | if (tda_fail(ret)) |
d35fccaf | 832 | goto fail; |
4d2d42bc MK |
833 | |
834 | mutex_lock(&priv->lock); | |
835 | ||
ccbac9bb MK |
836 | switch (priv->id) { |
837 | case TDA18271HDC1: | |
d1c53424 | 838 | tda18271c1_rf_tracking_filter_calibration(fe, freq, bw); |
ccbac9bb MK |
839 | break; |
840 | case TDA18271HDC2: | |
d1c53424 | 841 | tda18271c2_rf_tracking_filters_correction(fe, freq); |
ccbac9bb MK |
842 | break; |
843 | } | |
31940e39 | 844 | ret = tda18271_channel_configuration(fe, map, freq, bw); |
d1c53424 MK |
845 | |
846 | mutex_unlock(&priv->lock); | |
d35fccaf MK |
847 | fail: |
848 | return ret; | |
ccbac9bb MK |
849 | } |
850 | ||
5bea1cd3 MK |
851 | /* ------------------------------------------------------------------ */ |
852 | ||
853 | static int tda18271_set_params(struct dvb_frontend *fe, | |
854 | struct dvb_frontend_parameters *params) | |
855 | { | |
856 | struct tda18271_priv *priv = fe->tuner_priv; | |
f21e0d7f | 857 | struct tda18271_std_map *std_map = &priv->std; |
c293d0a7 | 858 | struct tda18271_std_map_item *map; |
ccbac9bb | 859 | int ret; |
2ba65d51 | 860 | u32 bw, freq = params->frequency; |
5bea1cd3 MK |
861 | |
862 | priv->mode = TDA18271_DIGITAL; | |
863 | ||
5bea1cd3 MK |
864 | if (fe->ops.info.type == FE_ATSC) { |
865 | switch (params->u.vsb.modulation) { | |
866 | case VSB_8: | |
867 | case VSB_16: | |
c293d0a7 | 868 | map = &std_map->atsc_6; |
5bea1cd3 MK |
869 | break; |
870 | case QAM_64: | |
871 | case QAM_256: | |
c293d0a7 | 872 | map = &std_map->qam_6; |
5bea1cd3 MK |
873 | break; |
874 | default: | |
182519f4 | 875 | tda_warn("modulation not set!\n"); |
5bea1cd3 MK |
876 | return -EINVAL; |
877 | } | |
14e3c152 MK |
878 | #if 0 |
879 | /* userspace request is already center adjusted */ | |
5bea1cd3 | 880 | freq += 1750000; /* Adjust to center (+1.75MHZ) */ |
14e3c152 | 881 | #endif |
5bea1cd3 MK |
882 | bw = 6000000; |
883 | } else if (fe->ops.info.type == FE_OFDM) { | |
884 | switch (params->u.ofdm.bandwidth) { | |
885 | case BANDWIDTH_6_MHZ: | |
5bea1cd3 | 886 | bw = 6000000; |
c293d0a7 | 887 | map = &std_map->dvbt_6; |
5bea1cd3 MK |
888 | break; |
889 | case BANDWIDTH_7_MHZ: | |
5bea1cd3 | 890 | bw = 7000000; |
c293d0a7 | 891 | map = &std_map->dvbt_7; |
5bea1cd3 MK |
892 | break; |
893 | case BANDWIDTH_8_MHZ: | |
5bea1cd3 | 894 | bw = 8000000; |
c293d0a7 | 895 | map = &std_map->dvbt_8; |
5bea1cd3 MK |
896 | break; |
897 | default: | |
182519f4 | 898 | tda_warn("bandwidth not set!\n"); |
5bea1cd3 MK |
899 | return -EINVAL; |
900 | } | |
901 | } else { | |
182519f4 | 902 | tda_warn("modulation type not supported!\n"); |
5bea1cd3 MK |
903 | return -EINVAL; |
904 | } | |
905 | ||
ed73683f MK |
906 | /* When tuning digital, the analog demod must be tri-stated */ |
907 | if (fe->ops.analog_ops.standby) | |
908 | fe->ops.analog_ops.standby(fe); | |
909 | ||
c293d0a7 | 910 | ret = tda18271_tune(fe, map, freq, bw); |
ccbac9bb | 911 | |
4bd5d107 | 912 | if (tda_fail(ret)) |
ccbac9bb MK |
913 | goto fail; |
914 | ||
915 | priv->frequency = freq; | |
916 | priv->bandwidth = (fe->ops.info.type == FE_OFDM) ? | |
917 | params->u.ofdm.bandwidth : 0; | |
918 | fail: | |
919 | return ret; | |
5bea1cd3 MK |
920 | } |
921 | ||
922 | static int tda18271_set_analog_params(struct dvb_frontend *fe, | |
923 | struct analog_parameters *params) | |
924 | { | |
925 | struct tda18271_priv *priv = fe->tuner_priv; | |
f21e0d7f | 926 | struct tda18271_std_map *std_map = &priv->std; |
c293d0a7 | 927 | struct tda18271_std_map_item *map; |
5bea1cd3 | 928 | char *mode; |
c293d0a7 | 929 | int ret; |
2ba65d51 | 930 | u32 freq = params->frequency * 62500; |
5bea1cd3 MK |
931 | |
932 | priv->mode = TDA18271_ANALOG; | |
933 | ||
c353f42f | 934 | if (params->mode == V4L2_TUNER_RADIO) { |
c353f42f | 935 | freq = freq / 1000; |
c293d0a7 | 936 | map = &std_map->fm_radio; |
c353f42f MK |
937 | mode = "fm"; |
938 | } else if (params->std & V4L2_STD_MN) { | |
c293d0a7 | 939 | map = &std_map->atv_mn; |
5bea1cd3 MK |
940 | mode = "MN"; |
941 | } else if (params->std & V4L2_STD_B) { | |
c293d0a7 | 942 | map = &std_map->atv_b; |
5bea1cd3 MK |
943 | mode = "B"; |
944 | } else if (params->std & V4L2_STD_GH) { | |
c293d0a7 | 945 | map = &std_map->atv_gh; |
5bea1cd3 MK |
946 | mode = "GH"; |
947 | } else if (params->std & V4L2_STD_PAL_I) { | |
c293d0a7 | 948 | map = &std_map->atv_i; |
5bea1cd3 MK |
949 | mode = "I"; |
950 | } else if (params->std & V4L2_STD_DK) { | |
c293d0a7 | 951 | map = &std_map->atv_dk; |
5bea1cd3 MK |
952 | mode = "DK"; |
953 | } else if (params->std & V4L2_STD_SECAM_L) { | |
c293d0a7 | 954 | map = &std_map->atv_l; |
5bea1cd3 MK |
955 | mode = "L"; |
956 | } else if (params->std & V4L2_STD_SECAM_LC) { | |
c293d0a7 | 957 | map = &std_map->atv_lc; |
95af8a26 | 958 | mode = "L'"; |
5bea1cd3 | 959 | } else { |
c293d0a7 | 960 | map = &std_map->atv_i; |
5bea1cd3 MK |
961 | mode = "xx"; |
962 | } | |
963 | ||
182519f4 | 964 | tda_dbg("setting tda18271 to system %s\n", mode); |
5bea1cd3 | 965 | |
c293d0a7 | 966 | ret = tda18271_tune(fe, map, freq, 0); |
ccbac9bb | 967 | |
4bd5d107 | 968 | if (tda_fail(ret)) |
ccbac9bb MK |
969 | goto fail; |
970 | ||
971 | priv->frequency = freq; | |
972 | priv->bandwidth = 0; | |
973 | fail: | |
974 | return ret; | |
5bea1cd3 MK |
975 | } |
976 | ||
518d8739 MK |
977 | static int tda18271_sleep(struct dvb_frontend *fe) |
978 | { | |
979 | struct tda18271_priv *priv = fe->tuner_priv; | |
d35fccaf | 980 | int ret; |
518d8739 MK |
981 | |
982 | mutex_lock(&priv->lock); | |
983 | ||
984 | /* standby mode w/ slave tuner output | |
985 | * & loop thru & xtal oscillator on */ | |
d35fccaf | 986 | ret = tda18271_set_standby_mode(fe, 1, 0, 0); |
518d8739 MK |
987 | |
988 | mutex_unlock(&priv->lock); | |
989 | ||
d35fccaf | 990 | return ret; |
518d8739 MK |
991 | } |
992 | ||
5bea1cd3 MK |
993 | static int tda18271_release(struct dvb_frontend *fe) |
994 | { | |
a4f263b5 MK |
995 | struct tda18271_priv *priv = fe->tuner_priv; |
996 | ||
997 | mutex_lock(&tda18271_list_mutex); | |
998 | ||
f9e315a1 MK |
999 | if (priv) |
1000 | hybrid_tuner_release_state(priv); | |
a4f263b5 | 1001 | |
a4f263b5 MK |
1002 | mutex_unlock(&tda18271_list_mutex); |
1003 | ||
5bea1cd3 | 1004 | fe->tuner_priv = NULL; |
a4f263b5 | 1005 | |
5bea1cd3 MK |
1006 | return 0; |
1007 | } | |
1008 | ||
1009 | static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency) | |
1010 | { | |
1011 | struct tda18271_priv *priv = fe->tuner_priv; | |
1012 | *frequency = priv->frequency; | |
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth) | |
1017 | { | |
1018 | struct tda18271_priv *priv = fe->tuner_priv; | |
1019 | *bandwidth = priv->bandwidth; | |
1020 | return 0; | |
1021 | } | |
1022 | ||
f21e0d7f MK |
1023 | /* ------------------------------------------------------------------ */ |
1024 | ||
1025 | #define tda18271_update_std(std_cfg, name) do { \ | |
7f7203df | 1026 | if (map->std_cfg.if_freq + \ |
c735372f MK |
1027 | map->std_cfg.agc_mode + map->std_cfg.std + \ |
1028 | map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \ | |
f21e0d7f MK |
1029 | tda_dbg("Using custom std config for %s\n", name); \ |
1030 | memcpy(&std->std_cfg, &map->std_cfg, \ | |
1031 | sizeof(struct tda18271_std_map_item)); \ | |
1032 | } } while (0) | |
1033 | ||
1034 | #define tda18271_dump_std_item(std_cfg, name) do { \ | |
c735372f MK |
1035 | tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \ |
1036 | "if_lvl = %d, rfagc_top = 0x%02x\n", \ | |
7f7203df | 1037 | name, std->std_cfg.if_freq, \ |
c735372f MK |
1038 | std->std_cfg.agc_mode, std->std_cfg.std, \ |
1039 | std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \ | |
f21e0d7f MK |
1040 | } while (0) |
1041 | ||
1042 | static int tda18271_dump_std_map(struct dvb_frontend *fe) | |
1043 | { | |
1044 | struct tda18271_priv *priv = fe->tuner_priv; | |
1045 | struct tda18271_std_map *std = &priv->std; | |
1046 | ||
1047 | tda_dbg("========== STANDARD MAP SETTINGS ==========\n"); | |
c735372f MK |
1048 | tda18271_dump_std_item(fm_radio, " fm "); |
1049 | tda18271_dump_std_item(atv_b, "atv b "); | |
1050 | tda18271_dump_std_item(atv_dk, "atv dk"); | |
1051 | tda18271_dump_std_item(atv_gh, "atv gh"); | |
1052 | tda18271_dump_std_item(atv_i, "atv i "); | |
1053 | tda18271_dump_std_item(atv_l, "atv l "); | |
1054 | tda18271_dump_std_item(atv_lc, "atv l'"); | |
f21e0d7f MK |
1055 | tda18271_dump_std_item(atv_mn, "atv mn"); |
1056 | tda18271_dump_std_item(atsc_6, "atsc 6"); | |
1057 | tda18271_dump_std_item(dvbt_6, "dvbt 6"); | |
1058 | tda18271_dump_std_item(dvbt_7, "dvbt 7"); | |
1059 | tda18271_dump_std_item(dvbt_8, "dvbt 8"); | |
c735372f MK |
1060 | tda18271_dump_std_item(qam_6, "qam 6 "); |
1061 | tda18271_dump_std_item(qam_8, "qam 8 "); | |
f21e0d7f MK |
1062 | |
1063 | return 0; | |
1064 | } | |
1065 | ||
1066 | static int tda18271_update_std_map(struct dvb_frontend *fe, | |
1067 | struct tda18271_std_map *map) | |
1068 | { | |
1069 | struct tda18271_priv *priv = fe->tuner_priv; | |
1070 | struct tda18271_std_map *std = &priv->std; | |
1071 | ||
1072 | if (!map) | |
1073 | return -EINVAL; | |
1074 | ||
c353f42f | 1075 | tda18271_update_std(fm_radio, "fm"); |
f21e0d7f MK |
1076 | tda18271_update_std(atv_b, "atv b"); |
1077 | tda18271_update_std(atv_dk, "atv dk"); | |
1078 | tda18271_update_std(atv_gh, "atv gh"); | |
1079 | tda18271_update_std(atv_i, "atv i"); | |
1080 | tda18271_update_std(atv_l, "atv l"); | |
1081 | tda18271_update_std(atv_lc, "atv l'"); | |
1082 | tda18271_update_std(atv_mn, "atv mn"); | |
1083 | tda18271_update_std(atsc_6, "atsc 6"); | |
1084 | tda18271_update_std(dvbt_6, "dvbt 6"); | |
1085 | tda18271_update_std(dvbt_7, "dvbt 7"); | |
1086 | tda18271_update_std(dvbt_8, "dvbt 8"); | |
1087 | tda18271_update_std(qam_6, "qam 6"); | |
1088 | tda18271_update_std(qam_8, "qam 8"); | |
1089 | ||
1090 | return 0; | |
1091 | } | |
1092 | ||
49e7aaf0 MK |
1093 | static int tda18271_get_id(struct dvb_frontend *fe) |
1094 | { | |
1095 | struct tda18271_priv *priv = fe->tuner_priv; | |
1096 | unsigned char *regs = priv->tda18271_regs; | |
1097 | char *name; | |
1098 | int ret = 0; | |
1099 | ||
8d316bf5 | 1100 | mutex_lock(&priv->lock); |
49e7aaf0 | 1101 | tda18271_read_regs(fe); |
8d316bf5 | 1102 | mutex_unlock(&priv->lock); |
49e7aaf0 MK |
1103 | |
1104 | switch (regs[R_ID] & 0x7f) { | |
1105 | case 3: | |
1106 | name = "TDA18271HD/C1"; | |
255b5113 | 1107 | priv->id = TDA18271HDC1; |
49e7aaf0 MK |
1108 | break; |
1109 | case 4: | |
1110 | name = "TDA18271HD/C2"; | |
255b5113 | 1111 | priv->id = TDA18271HDC2; |
49e7aaf0 MK |
1112 | break; |
1113 | default: | |
1114 | name = "Unknown device"; | |
1115 | ret = -EINVAL; | |
1116 | break; | |
1117 | } | |
1118 | ||
182519f4 | 1119 | tda_info("%s detected @ %d-%04x%s\n", name, |
f9e315a1 MK |
1120 | i2c_adapter_id(priv->i2c_props.adap), |
1121 | priv->i2c_props.addr, | |
49e7aaf0 MK |
1122 | (0 == ret) ? "" : ", device not supported."); |
1123 | ||
1124 | return ret; | |
1125 | } | |
1126 | ||
5bea1cd3 MK |
1127 | static struct dvb_tuner_ops tda18271_tuner_ops = { |
1128 | .info = { | |
1129 | .name = "NXP TDA18271HD", | |
1130 | .frequency_min = 45000000, | |
1131 | .frequency_max = 864000000, | |
1132 | .frequency_step = 62500 | |
1133 | }, | |
efce8410 | 1134 | .init = tda18271_init, |
518d8739 | 1135 | .sleep = tda18271_sleep, |
5bea1cd3 MK |
1136 | .set_params = tda18271_set_params, |
1137 | .set_analog_params = tda18271_set_analog_params, | |
1138 | .release = tda18271_release, | |
1139 | .get_frequency = tda18271_get_frequency, | |
1140 | .get_bandwidth = tda18271_get_bandwidth, | |
1141 | }; | |
1142 | ||
1143 | struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr, | |
e435f95c | 1144 | struct i2c_adapter *i2c, |
f21e0d7f | 1145 | struct tda18271_config *cfg) |
5bea1cd3 MK |
1146 | { |
1147 | struct tda18271_priv *priv = NULL; | |
f9e315a1 | 1148 | int instance; |
a4f263b5 MK |
1149 | |
1150 | mutex_lock(&tda18271_list_mutex); | |
1151 | ||
f9e315a1 MK |
1152 | instance = hybrid_tuner_request_state(struct tda18271_priv, priv, |
1153 | hybrid_tuner_instance_list, | |
1154 | i2c, addr, "tda18271"); | |
1155 | switch (instance) { | |
1156 | case 0: | |
1157 | goto fail; | |
f9e315a1 MK |
1158 | case 1: |
1159 | /* new tuner instance */ | |
a4f263b5 | 1160 | priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO; |
868f5ccd | 1161 | priv->role = (cfg) ? cfg->role : TDA18271_MASTER; |
a4f263b5 MK |
1162 | priv->cal_initialized = false; |
1163 | mutex_init(&priv->lock); | |
5bea1cd3 | 1164 | |
a4f263b5 | 1165 | fe->tuner_priv = priv; |
5bea1cd3 | 1166 | |
5555309c MK |
1167 | if (cfg) |
1168 | priv->small_i2c = cfg->small_i2c; | |
1169 | ||
4bd5d107 | 1170 | if (tda_fail(tda18271_get_id(fe))) |
a4f263b5 | 1171 | goto fail; |
49e7aaf0 | 1172 | |
4bd5d107 | 1173 | if (tda_fail(tda18271_assign_map_layout(fe))) |
a4f263b5 | 1174 | goto fail; |
255b5113 | 1175 | |
a4f263b5 MK |
1176 | mutex_lock(&priv->lock); |
1177 | tda18271_init_regs(fe); | |
0f96251e MK |
1178 | |
1179 | if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2)) | |
12afe378 | 1180 | tda18271c2_rf_cal_init(fe); |
0f96251e | 1181 | |
a4f263b5 | 1182 | mutex_unlock(&priv->lock); |
f9e315a1 MK |
1183 | break; |
1184 | default: | |
1185 | /* existing tuner instance */ | |
1186 | fe->tuner_priv = priv; | |
1187 | ||
1188 | /* allow dvb driver to override i2c gate setting */ | |
1189 | if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG)) | |
1190 | priv->gate = cfg->gate; | |
1191 | break; | |
a4f263b5 | 1192 | } |
5bea1cd3 | 1193 | |
f21e0d7f MK |
1194 | /* override default std map with values in config struct */ |
1195 | if ((cfg) && (cfg->std_map)) | |
1196 | tda18271_update_std_map(fe, cfg->std_map); | |
1197 | ||
a4f263b5 | 1198 | mutex_unlock(&tda18271_list_mutex); |
8d316bf5 | 1199 | |
a4f263b5 MK |
1200 | memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops, |
1201 | sizeof(struct dvb_tuner_ops)); | |
efce8410 | 1202 | |
c735372f | 1203 | if (tda18271_debug & (DBG_MAP | DBG_ADV)) |
a4f263b5 | 1204 | tda18271_dump_std_map(fe); |
8d316bf5 | 1205 | |
5bea1cd3 | 1206 | return fe; |
49e7aaf0 | 1207 | fail: |
a4f263b5 MK |
1208 | mutex_unlock(&tda18271_list_mutex); |
1209 | ||
49e7aaf0 MK |
1210 | tda18271_release(fe); |
1211 | return NULL; | |
5bea1cd3 MK |
1212 | } |
1213 | EXPORT_SYMBOL_GPL(tda18271_attach); | |
1214 | MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver"); | |
1215 | MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>"); | |
1216 | MODULE_LICENSE("GPL"); | |
5ec96b0c | 1217 | MODULE_VERSION("0.3"); |
5bea1cd3 MK |
1218 | |
1219 | /* | |
1220 | * Overrides for Emacs so that we follow Linus's tabbing style. | |
1221 | * --------------------------------------------------------------------------- | |
1222 | * Local variables: | |
1223 | * c-basic-offset: 8 | |
1224 | * End: | |
1225 | */ |