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