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