blob: bb0a65310b71ff06fe95a6a403d13a7ebff327af [file] [log] [blame]
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001/*
Michael Krufky6ca04de2007-11-23 16:52:15 -03002 tda18271-fe.c - driver for the Philips / NXP TDA18271 silicon tuner
Michael Krufky5bea1cd2007-10-22 09:56:38 -03003
Michael Krufky59067f72008-01-02 01:58:26 -03004 Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
Michael Krufky5bea1cd2007-10-22 09:56:38 -03005
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
Michael Krufky5bea1cd2007-10-22 09:56:38 -030021#include <linux/delay.h>
22#include <linux/videodev2.h>
Michael Krufky6ca04de2007-11-23 16:52:15 -030023#include "tda18271-priv.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030024
Michael Krufkyb5f3e1e2007-12-02 16:36:05 -030025int tda18271_debug;
Michael Krufky54465b02007-11-23 18:14:53 -030026module_param_named(debug, tda18271_debug, int, 0644);
Michael Krufky0e1fab92008-01-03 01:40:47 -030027MODULE_PARM_DESC(debug, "set debug level "
Michael Krufkycf04d292008-01-09 00:34:30 -030028 "(info=1, map=2, reg=4, adv=8, cal=16 (or-able))");
Michael Krufky5bea1cd2007-10-22 09:56:38 -030029
Michael Krufkybc835d82008-01-14 11:10:54 -030030static int tda18271_cal_on_startup;
Michael Krufky0f962512008-01-13 22:01:07 -030031module_param_named(cal, tda18271_cal_on_startup, int, 0644);
32MODULE_PARM_DESC(cal, "perform RF tracking filter calibration on startup");
33
Michael Krufkya4f263b2008-01-06 15:52:56 -030034static DEFINE_MUTEX(tda18271_list_mutex);
Michael Krufkyf9e315a2008-04-22 14:41:54 -030035static LIST_HEAD(hybrid_tuner_instance_list);
Michael Krufkya4f263b2008-01-06 15:52:56 -030036
Michael Krufky5bea1cd2007-10-22 09:56:38 -030037/*---------------------------------------------------------------------*/
38
Michael Krufky255b5112008-01-01 22:52:09 -030039static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc353f422008-01-08 10:38:10 -030040 u32 ifc, u32 freq, u32 bw, u8 std,
41 int radio)
Michael Krufky255b5112008-01-01 22:52:09 -030042{
43 struct tda18271_priv *priv = fe->tuner_priv;
44 unsigned char *regs = priv->tda18271_regs;
45 u32 N;
46
47 /* update TV broadcast parameters */
48
49 /* set standard */
50 regs[R_EP3] &= ~0x1f; /* clear std bits */
51 regs[R_EP3] |= std;
52
53 /* set cal mode to normal */
54 regs[R_EP4] &= ~0x03;
55
56 /* update IF output level & IF notch frequency */
57 regs[R_EP4] &= ~0x1c; /* clear if level bits */
58
59 switch (priv->mode) {
60 case TDA18271_ANALOG:
61 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
62 break;
63 case TDA18271_DIGITAL:
64 regs[R_EP4] |= 0x04; /* IF level = 1 */
65 regs[R_MPD] |= 0x80; /* IF notch = 1 */
66 break;
67 }
Michael Krufkyc353f422008-01-08 10:38:10 -030068
69 if (radio)
70 regs[R_EP4] |= 0x80;
71 else
72 regs[R_EP4] &= ~0x80;
Michael Krufky255b5112008-01-01 22:52:09 -030073
74 /* update RF_TOP / IF_TOP */
75 switch (priv->mode) {
76 case TDA18271_ANALOG:
77 regs[R_EB22] = 0x2c;
78 break;
79 case TDA18271_DIGITAL:
80 regs[R_EB22] = 0x37;
81 break;
82 }
83 tda18271_write_regs(fe, R_EB22, 1);
84
85 /* --------------------------------------------------------------- */
86
87 /* disable Power Level Indicator */
88 regs[R_EP1] |= 0x40;
89
90 /* frequency dependent parameters */
91
92 tda18271_calc_ir_measure(fe, &freq);
93
94 tda18271_calc_bp_filter(fe, &freq);
95
96 tda18271_calc_rf_band(fe, &freq);
97
98 tda18271_calc_gain_taper(fe, &freq);
99
100 /* --------------------------------------------------------------- */
101
102 /* dual tuner and agc1 extra configuration */
103
104 /* main vco when Master, cal vco when slave */
105 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
106
107 /* agc1 always active */
108 regs[R_EB1] &= ~0x02;
109
110 /* agc1 has priority on agc2 */
111 regs[R_EB1] &= ~0x01;
112
113 tda18271_write_regs(fe, R_EB1, 1);
114
115 /* --------------------------------------------------------------- */
116
117 N = freq + ifc;
118
119 /* FIXME: assumes master */
120 tda18271_calc_main_pll(fe, N);
121 tda18271_write_regs(fe, R_MPD, 4);
122
123 tda18271_write_regs(fe, R_TM, 7);
124
125 /* main pll charge pump source */
126 regs[R_EB4] |= 0x20;
127 tda18271_write_regs(fe, R_EB4, 1);
128
129 msleep(1);
130
131 /* normal operation for the main pll */
132 regs[R_EB4] &= ~0x20;
133 tda18271_write_regs(fe, R_EB4, 1);
134
135 msleep(5);
136
137 return 0;
138}
139
140static int tda18271_read_thermometer(struct dvb_frontend *fe)
141{
142 struct tda18271_priv *priv = fe->tuner_priv;
143 unsigned char *regs = priv->tda18271_regs;
144 int tm;
145
146 /* switch thermometer on */
147 regs[R_TM] |= 0x10;
148 tda18271_write_regs(fe, R_TM, 1);
149
150 /* read thermometer info */
151 tda18271_read_regs(fe);
152
153 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
154 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
155
156 if ((regs[R_TM] & 0x20) == 0x20)
157 regs[R_TM] &= ~0x20;
158 else
159 regs[R_TM] |= 0x20;
160
161 tda18271_write_regs(fe, R_TM, 1);
162
163 msleep(10); /* temperature sensing */
164
165 /* read thermometer info */
166 tda18271_read_regs(fe);
167 }
168
169 tm = tda18271_lookup_thermometer(fe);
170
171 /* switch thermometer off */
172 regs[R_TM] &= ~0x10;
173 tda18271_write_regs(fe, R_TM, 1);
174
175 /* set CAL mode to normal */
176 regs[R_EP4] &= ~0x03;
177 tda18271_write_regs(fe, R_EP4, 1);
178
179 return tm;
180}
181
Michael Krufky12afe372008-04-22 14:42:07 -0300182/* ------------------------------------------------------------------ */
183
Michael Krufkyd1c53422008-04-22 14:42:07 -0300184static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
185 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300186{
187 struct tda18271_priv *priv = fe->tuner_priv;
188 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
189 unsigned char *regs = priv->tda18271_regs;
190 int tm_current, rfcal_comp, approx, i;
191 u8 dc_over_dt, rf_tab;
192
193 /* power up */
Michael Krufky518d8732008-01-13 17:01:01 -0300194 tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300195
196 /* read die current temperature */
197 tm_current = tda18271_read_thermometer(fe);
198
199 /* frequency dependent parameters */
200
201 tda18271_calc_rf_cal(fe, &freq);
202 rf_tab = regs[R_EB14];
203
204 i = tda18271_lookup_rf_band(fe, &freq, NULL);
205 if (i < 0)
206 return -EINVAL;
207
208 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
209 approx = map[i].rf_a1 *
210 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
211 } else {
212 approx = map[i].rf_a2 *
213 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
214 }
215
216 if (approx < 0)
217 approx = 0;
218 if (approx > 255)
219 approx = 255;
220
221 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
222
223 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300224 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300225
226 regs[R_EB14] = approx + rfcal_comp;
227 tda18271_write_regs(fe, R_EB14, 1);
228
229 return 0;
230}
231
232static int tda18271_por(struct dvb_frontend *fe)
233{
234 struct tda18271_priv *priv = fe->tuner_priv;
235 unsigned char *regs = priv->tda18271_regs;
236
237 /* power up detector 1 */
238 regs[R_EB12] &= ~0x20;
239 tda18271_write_regs(fe, R_EB12, 1);
240
241 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
242 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
243 tda18271_write_regs(fe, R_EB18, 1);
244
245 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
246
247 /* POR mode */
Michael Krufky518d8732008-01-13 17:01:01 -0300248 tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300249
250 /* disable 1.5 MHz low pass filter */
251 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
252 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
253 tda18271_write_regs(fe, R_EB21, 3);
254
255 return 0;
256}
257
258static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
259{
260 struct tda18271_priv *priv = fe->tuner_priv;
261 unsigned char *regs = priv->tda18271_regs;
262 u32 N;
263
264 /* set CAL mode to normal */
265 regs[R_EP4] &= ~0x03;
266 tda18271_write_regs(fe, R_EP4, 1);
267
268 /* switch off agc1 */
269 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
270
271 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
272 tda18271_write_regs(fe, R_EB18, 1);
273
274 /* frequency dependent parameters */
275
276 tda18271_calc_bp_filter(fe, &freq);
277 tda18271_calc_gain_taper(fe, &freq);
278 tda18271_calc_rf_band(fe, &freq);
279 tda18271_calc_km(fe, &freq);
280
281 tda18271_write_regs(fe, R_EP1, 3);
282 tda18271_write_regs(fe, R_EB13, 1);
283
284 /* main pll charge pump source */
285 regs[R_EB4] |= 0x20;
286 tda18271_write_regs(fe, R_EB4, 1);
287
288 /* cal pll charge pump source */
289 regs[R_EB7] |= 0x20;
290 tda18271_write_regs(fe, R_EB7, 1);
291
292 /* force dcdc converter to 0 V */
293 regs[R_EB14] = 0x00;
294 tda18271_write_regs(fe, R_EB14, 1);
295
296 /* disable plls lock */
297 regs[R_EB20] &= ~0x20;
298 tda18271_write_regs(fe, R_EB20, 1);
299
300 /* set CAL mode to RF tracking filter calibration */
301 regs[R_EP4] |= 0x03;
302 tda18271_write_regs(fe, R_EP4, 2);
303
304 /* --------------------------------------------------------------- */
305
306 /* set the internal calibration signal */
307 N = freq;
308
309 tda18271_calc_main_pll(fe, N);
310 tda18271_write_regs(fe, R_MPD, 4);
311
312 /* downconvert internal calibration */
313 N += 1000000;
314
315 tda18271_calc_main_pll(fe, N);
316 tda18271_write_regs(fe, R_MPD, 4);
317
318 msleep(5);
319
320 tda18271_write_regs(fe, R_EP2, 1);
321 tda18271_write_regs(fe, R_EP1, 1);
322 tda18271_write_regs(fe, R_EP2, 1);
323 tda18271_write_regs(fe, R_EP1, 1);
324
325 /* --------------------------------------------------------------- */
326
327 /* normal operation for the main pll */
328 regs[R_EB4] &= ~0x20;
329 tda18271_write_regs(fe, R_EB4, 1);
330
331 /* normal operation for the cal pll */
332 regs[R_EB7] &= ~0x20;
333 tda18271_write_regs(fe, R_EB7, 1);
334
335 msleep(5); /* plls locking */
336
337 /* launch the rf tracking filters calibration */
338 regs[R_EB20] |= 0x20;
339 tda18271_write_regs(fe, R_EB20, 1);
340
341 msleep(60); /* calibration */
342
343 /* --------------------------------------------------------------- */
344
345 /* set CAL mode to normal */
346 regs[R_EP4] &= ~0x03;
347
348 /* switch on agc1 */
349 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
350
351 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
352 tda18271_write_regs(fe, R_EB18, 1);
353
354 tda18271_write_regs(fe, R_EP3, 2);
355
356 /* synchronization */
357 tda18271_write_regs(fe, R_EP1, 1);
358
359 /* get calibration result */
360 tda18271_read_extended(fe);
361
362 return regs[R_EB14];
363}
364
365static int tda18271_powerscan(struct dvb_frontend *fe,
366 u32 *freq_in, u32 *freq_out)
367{
368 struct tda18271_priv *priv = fe->tuner_priv;
369 unsigned char *regs = priv->tda18271_regs;
370 int sgn, bcal, count, wait;
371 u8 cid_target;
372 u16 count_limit;
373 u32 freq;
374
375 freq = *freq_in;
376
377 tda18271_calc_rf_band(fe, &freq);
378 tda18271_calc_rf_cal(fe, &freq);
379 tda18271_calc_gain_taper(fe, &freq);
380 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
381
382 tda18271_write_regs(fe, R_EP2, 1);
383 tda18271_write_regs(fe, R_EB14, 1);
384
385 /* downconvert frequency */
386 freq += 1000000;
387
388 tda18271_calc_main_pll(fe, freq);
389 tda18271_write_regs(fe, R_MPD, 4);
390
391 msleep(5); /* pll locking */
392
393 /* detection mode */
394 regs[R_EP4] &= ~0x03;
395 regs[R_EP4] |= 0x01;
396 tda18271_write_regs(fe, R_EP4, 1);
397
398 /* launch power detection measurement */
399 tda18271_write_regs(fe, R_EP2, 1);
400
401 /* read power detection info, stored in EB10 */
402 tda18271_read_extended(fe);
403
404 /* algorithm initialization */
405 sgn = 1;
406 *freq_out = *freq_in;
407 bcal = 0;
408 count = 0;
409 wait = false;
410
411 while ((regs[R_EB10] & 0x3f) < cid_target) {
412 /* downconvert updated freq to 1 MHz */
413 freq = *freq_in + (sgn * count) + 1000000;
414
415 tda18271_calc_main_pll(fe, freq);
416 tda18271_write_regs(fe, R_MPD, 4);
417
418 if (wait) {
419 msleep(5); /* pll locking */
420 wait = false;
421 } else
422 udelay(100); /* pll locking */
423
424 /* launch power detection measurement */
425 tda18271_write_regs(fe, R_EP2, 1);
426
427 /* read power detection info, stored in EB10 */
428 tda18271_read_extended(fe);
429
430 count += 200;
431
432 if (count < count_limit)
433 continue;
434
435 if (sgn <= 0)
436 break;
437
438 sgn = -1 * sgn;
439 count = 200;
440 wait = true;
441 }
442
443 if ((regs[R_EB10] & 0x3f) >= cid_target) {
444 bcal = 1;
445 *freq_out = freq - 1000000;
446 } else
447 bcal = 0;
448
Michael Krufkycf04d292008-01-09 00:34:30 -0300449 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300450 bcal, *freq_in, *freq_out, freq);
451
452 return bcal;
453}
454
455static int tda18271_powerscan_init(struct dvb_frontend *fe)
456{
457 struct tda18271_priv *priv = fe->tuner_priv;
458 unsigned char *regs = priv->tda18271_regs;
459
460 /* set standard to digital */
461 regs[R_EP3] &= ~0x1f; /* clear std bits */
462 regs[R_EP3] |= 0x12;
463
464 /* set cal mode to normal */
465 regs[R_EP4] &= ~0x03;
466
467 /* update IF output level & IF notch frequency */
468 regs[R_EP4] &= ~0x1c; /* clear if level bits */
469
470 tda18271_write_regs(fe, R_EP3, 2);
471
472 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
473 tda18271_write_regs(fe, R_EB18, 1);
474
475 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
476
477 /* 1.5 MHz low pass filter */
478 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
479 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
480
481 tda18271_write_regs(fe, R_EB21, 3);
482
483 return 0;
484}
485
486static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
487{
488 struct tda18271_priv *priv = fe->tuner_priv;
489 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
490 unsigned char *regs = priv->tda18271_regs;
491 int bcal, rf, i;
492#define RF1 0
493#define RF2 1
494#define RF3 2
495 u32 rf_default[3];
496 u32 rf_freq[3];
497 u8 prog_cal[3];
498 u8 prog_tab[3];
499
500 i = tda18271_lookup_rf_band(fe, &freq, NULL);
501
502 if (i < 0)
503 return i;
504
505 rf_default[RF1] = 1000 * map[i].rf1_def;
506 rf_default[RF2] = 1000 * map[i].rf2_def;
507 rf_default[RF3] = 1000 * map[i].rf3_def;
508
509 for (rf = RF1; rf <= RF3; rf++) {
510 if (0 == rf_default[rf])
511 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300512 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300513
514 /* look for optimized calibration frequency */
515 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
516
517 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
518 prog_tab[rf] = regs[R_EB14];
519
520 if (1 == bcal)
521 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
522 else
523 prog_cal[rf] = prog_tab[rf];
524
525 switch (rf) {
526 case RF1:
527 map[i].rf_a1 = 0;
528 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
529 map[i].rf1 = rf_freq[RF1] / 1000;
530 break;
531 case RF2:
532 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
533 prog_cal[RF1] + prog_tab[RF1]) /
534 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
535 map[i].rf2 = rf_freq[RF2] / 1000;
536 break;
537 case RF3:
538 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
539 prog_cal[RF2] + prog_tab[RF2]) /
540 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
541 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
542 map[i].rf3 = rf_freq[RF3] / 1000;
543 break;
544 default:
545 BUG();
546 }
547 }
548
549 return 0;
550}
551
Michael Krufky09f83c42008-01-05 20:00:09 -0300552static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300553{
554 struct tda18271_priv *priv = fe->tuner_priv;
555 unsigned int i;
556
557 tda_info("tda18271: performing RF tracking filter calibration\n");
558
559 /* wait for die temperature stabilization */
560 msleep(200);
561
562 tda18271_powerscan_init(fe);
563
564 /* rf band calibration */
565 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
566 tda18271_rf_tracking_filters_init(fe, 1000 *
567 priv->rf_cal_state[i].rfmax);
568
Michael Krufky09f83c42008-01-05 20:00:09 -0300569 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300570
571 return 0;
572}
573
574/* ------------------------------------------------------------------ */
575
Michael Krufky12afe372008-04-22 14:42:07 -0300576static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300577{
578 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300579 unsigned char *regs = priv->tda18271_regs;
580
581 /* test RF_CAL_OK to see if we need init */
582 if ((regs[R_EP1] & 0x10) == 0)
583 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300584
585 if (priv->cal_initialized)
586 return 0;
587
Michael Krufky09f83c42008-01-05 20:00:09 -0300588 tda18271_calc_rf_filter_curve(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300589
590 tda18271_por(fe);
591
Michael Krufky6bfa6652008-01-07 00:51:48 -0300592 tda_info("tda18271: RF tracking filter calibration complete\n");
593
Michael Krufky255b5112008-01-01 22:52:09 -0300594 priv->cal_initialized = true;
595
596 return 0;
597}
598
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300599static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
600 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300601{
602 struct tda18271_priv *priv = fe->tuner_priv;
603 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300604 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300605
Michael Krufky255b5112008-01-01 22:52:09 -0300606 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300607 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300608 tda18271_write_regs(fe, R_EP1, 1);
609
610 regs[R_EB4] &= 0x07;
611 regs[R_EB4] |= 0x60;
612 tda18271_write_regs(fe, R_EB4, 1);
613
614 regs[R_EB7] = 0x60;
615 tda18271_write_regs(fe, R_EB7, 1);
616
617 regs[R_EB14] = 0x00;
618 tda18271_write_regs(fe, R_EB14, 1);
619
620 regs[R_EB20] = 0xcc;
621 tda18271_write_regs(fe, R_EB20, 1);
622
Michael Krufky255b5112008-01-01 22:52:09 -0300623 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300624 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300625
Michael Krufky255b5112008-01-01 22:52:09 -0300626 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300627
628 switch (priv->mode) {
629 case TDA18271_ANALOG:
630 N = freq - 1250000;
631 break;
632 case TDA18271_DIGITAL:
633 N = freq + bw / 2;
634 break;
635 }
636
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300637 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300638
Michael Krufky255b5112008-01-01 22:52:09 -0300639 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300640
641 switch (priv->mode) {
642 case TDA18271_ANALOG:
643 N = freq - 250000;
644 break;
645 case TDA18271_DIGITAL:
646 N = freq + bw / 2 + 1000000;
647 break;
648 }
649
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300650 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300651
652 tda18271_write_regs(fe, R_EP3, 11);
653 msleep(5); /* RF tracking filter calibration initialization */
654
Michael Krufky255b5112008-01-01 22:52:09 -0300655 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300656 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300657 tda18271_write_regs(fe, R_EB13, 1);
658
Michael Krufky255b5112008-01-01 22:52:09 -0300659 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300660 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300661
Michael Krufky255b5112008-01-01 22:52:09 -0300662 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300663 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300664
665 tda18271_write_regs(fe, R_EP2, 1);
666 tda18271_write_regs(fe, R_EP1, 1);
667 tda18271_write_regs(fe, R_EP2, 1);
668 tda18271_write_regs(fe, R_EP1, 1);
669
670 regs[R_EB4] &= 0x07;
671 regs[R_EB4] |= 0x40;
672 tda18271_write_regs(fe, R_EB4, 1);
673
674 regs[R_EB7] = 0x40;
675 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300676 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300677
678 regs[R_EB20] = 0xec;
679 tda18271_write_regs(fe, R_EB20, 1);
680 msleep(60); /* RF tracking filter calibration completion */
681
682 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
683 tda18271_write_regs(fe, R_EP4, 1);
684
685 tda18271_write_regs(fe, R_EP1, 1);
686
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300687 /* RF tracking filter correction for VHF_Low band */
688 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300689 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300690
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300691 return 0;
692}
693
Michael Krufkyd1c53422008-04-22 14:42:07 -0300694/* ------------------------------------------------------------------ */
695
Michael Krufky12afe372008-04-22 14:42:07 -0300696static int tda18271_ir_cal_init(struct dvb_frontend *fe)
697{
698 struct tda18271_priv *priv = fe->tuner_priv;
699 unsigned char *regs = priv->tda18271_regs;
700
701 tda18271_read_regs(fe);
702
703 /* test IR_CAL_OK to see if we need init */
704 if ((regs[R_EP1] & 0x08) == 0)
705 tda18271_init_regs(fe);
706
707 return 0;
708}
709
710static int tda18271_init(struct dvb_frontend *fe)
711{
712 struct tda18271_priv *priv = fe->tuner_priv;
713
714 mutex_lock(&priv->lock);
715
716 /* power up */
717 tda18271_set_standby_mode(fe, 0, 0, 0);
718
719 /* initialization */
720 tda18271_ir_cal_init(fe);
721
722 if (priv->id == TDA18271HDC2)
723 tda18271c2_rf_cal_init(fe);
724
725 mutex_unlock(&priv->lock);
726
727 return 0;
728}
729
Michael Krufkyd1c53422008-04-22 14:42:07 -0300730static int tda18271_tune(struct dvb_frontend *fe,
731 u32 ifc, u32 freq, u32 bw, u8 std, int radio)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300732{
733 struct tda18271_priv *priv = fe->tuner_priv;
734
Michael Krufkyd1c53422008-04-22 14:42:07 -0300735 tda_dbg("freq = %d, ifc = %d, bw = %d, std = 0x%02x\n",
736 freq, ifc, bw, std);
737
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300738 tda18271_init(fe);
739
740 mutex_lock(&priv->lock);
741
Michael Krufkyd1c53422008-04-22 14:42:07 -0300742 switch (priv->id) {
743 case TDA18271HDC1:
744 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
745 break;
746 case TDA18271HDC2:
747 tda18271c2_rf_tracking_filters_correction(fe, freq);
748 break;
749 }
Michael Krufkya60b8662008-04-22 14:42:06 -0300750 tda18271_channel_configuration(fe, ifc, freq, bw, std, radio);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300751
Michael Krufky8d316bf2008-01-06 15:31:35 -0300752 mutex_unlock(&priv->lock);
Michael Krufky6ca04de2007-11-23 16:52:15 -0300753
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300754 return 0;
755}
756
757/* ------------------------------------------------------------------ */
758
759static int tda18271_set_params(struct dvb_frontend *fe,
760 struct dvb_frontend_parameters *params)
761{
762 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300763 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300764 int ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300765 u8 std;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300766 u16 sgIF;
767 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300768
769 priv->mode = TDA18271_DIGITAL;
770
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300771 if (fe->ops.info.type == FE_ATSC) {
772 switch (params->u.vsb.modulation) {
773 case VSB_8:
774 case VSB_16:
Michael Krufky255b5112008-01-01 22:52:09 -0300775 std = std_map->atsc_6.std_bits;
776 sgIF = std_map->atsc_6.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300777 break;
778 case QAM_64:
779 case QAM_256:
Michael Krufky255b5112008-01-01 22:52:09 -0300780 std = std_map->qam_6.std_bits;
781 sgIF = std_map->qam_6.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300782 break;
783 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300784 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300785 return -EINVAL;
786 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300787#if 0
788 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300789 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300790#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300791 bw = 6000000;
792 } else if (fe->ops.info.type == FE_OFDM) {
793 switch (params->u.ofdm.bandwidth) {
794 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300795 bw = 6000000;
Michael Krufky255b5112008-01-01 22:52:09 -0300796 std = std_map->dvbt_6.std_bits;
797 sgIF = std_map->dvbt_6.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300798 break;
799 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300800 bw = 7000000;
Michael Krufky255b5112008-01-01 22:52:09 -0300801 std = std_map->dvbt_7.std_bits;
802 sgIF = std_map->dvbt_7.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300803 break;
804 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300805 bw = 8000000;
Michael Krufky255b5112008-01-01 22:52:09 -0300806 std = std_map->dvbt_8.std_bits;
807 sgIF = std_map->dvbt_8.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300808 break;
809 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300810 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300811 return -EINVAL;
812 }
813 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300814 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300815 return -EINVAL;
816 }
817
Michael Krufkyed736832008-01-19 17:41:04 -0300818 /* When tuning digital, the analog demod must be tri-stated */
819 if (fe->ops.analog_ops.standby)
820 fe->ops.analog_ops.standby(fe);
821
Michael Krufkyc353f422008-01-08 10:38:10 -0300822 ret = tda18271_tune(fe, sgIF * 1000, freq, bw, std, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300823
824 if (ret < 0)
825 goto fail;
826
827 priv->frequency = freq;
828 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
829 params->u.ofdm.bandwidth : 0;
830fail:
831 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300832}
833
834static int tda18271_set_analog_params(struct dvb_frontend *fe,
835 struct analog_parameters *params)
836{
837 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300838 struct tda18271_std_map *std_map = &priv->std;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300839 char *mode;
Michael Krufkyc353f422008-01-08 10:38:10 -0300840 int ret, radio = 0;
Michael Krufky95af8a22008-01-01 18:31:34 -0300841 u8 std;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300842 u16 sgIF;
843 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300844
845 priv->mode = TDA18271_ANALOG;
846
Michael Krufkyc353f422008-01-08 10:38:10 -0300847 if (params->mode == V4L2_TUNER_RADIO) {
848 radio = 1;
849 freq = freq / 1000;
850 std = std_map->fm_radio.std_bits;
851 sgIF = std_map->fm_radio.if_freq;
852 mode = "fm";
853 } else if (params->std & V4L2_STD_MN) {
Michael Krufky255b5112008-01-01 22:52:09 -0300854 std = std_map->atv_mn.std_bits;
855 sgIF = std_map->atv_mn.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300856 mode = "MN";
857 } else if (params->std & V4L2_STD_B) {
Michael Krufky255b5112008-01-01 22:52:09 -0300858 std = std_map->atv_b.std_bits;
859 sgIF = std_map->atv_b.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300860 mode = "B";
861 } else if (params->std & V4L2_STD_GH) {
Michael Krufky255b5112008-01-01 22:52:09 -0300862 std = std_map->atv_gh.std_bits;
863 sgIF = std_map->atv_gh.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300864 mode = "GH";
865 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufky255b5112008-01-01 22:52:09 -0300866 std = std_map->atv_i.std_bits;
867 sgIF = std_map->atv_i.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300868 mode = "I";
869 } else if (params->std & V4L2_STD_DK) {
Michael Krufky255b5112008-01-01 22:52:09 -0300870 std = std_map->atv_dk.std_bits;
871 sgIF = std_map->atv_dk.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300872 mode = "DK";
873 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufky255b5112008-01-01 22:52:09 -0300874 std = std_map->atv_l.std_bits;
875 sgIF = std_map->atv_l.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300876 mode = "L";
877 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufky255b5112008-01-01 22:52:09 -0300878 std = std_map->atv_lc.std_bits;
879 sgIF = std_map->atv_lc.if_freq;
Michael Krufky95af8a22008-01-01 18:31:34 -0300880 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300881 } else {
Michael Krufky255b5112008-01-01 22:52:09 -0300882 std = std_map->atv_i.std_bits;
883 sgIF = std_map->atv_i.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300884 mode = "xx";
885 }
886
Michael Krufky182519f2007-12-25 15:10:11 -0300887 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300888
Michael Krufkyc353f422008-01-08 10:38:10 -0300889 ret = tda18271_tune(fe, sgIF * 1000, freq, 0, std, radio);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300890
891 if (ret < 0)
892 goto fail;
893
894 priv->frequency = freq;
895 priv->bandwidth = 0;
896fail:
897 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300898}
899
Michael Krufky518d8732008-01-13 17:01:01 -0300900static int tda18271_sleep(struct dvb_frontend *fe)
901{
902 struct tda18271_priv *priv = fe->tuner_priv;
903
904 mutex_lock(&priv->lock);
905
906 /* standby mode w/ slave tuner output
907 * & loop thru & xtal oscillator on */
908 tda18271_set_standby_mode(fe, 1, 0, 0);
909
910 mutex_unlock(&priv->lock);
911
912 return 0;
913}
914
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300915static int tda18271_release(struct dvb_frontend *fe)
916{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300917 struct tda18271_priv *priv = fe->tuner_priv;
918
919 mutex_lock(&tda18271_list_mutex);
920
Michael Krufkyf9e315a2008-04-22 14:41:54 -0300921 if (priv)
922 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -0300923
Michael Krufkya4f263b2008-01-06 15:52:56 -0300924 mutex_unlock(&tda18271_list_mutex);
925
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300926 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300927
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300928 return 0;
929}
930
931static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
932{
933 struct tda18271_priv *priv = fe->tuner_priv;
934 *frequency = priv->frequency;
935 return 0;
936}
937
938static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
939{
940 struct tda18271_priv *priv = fe->tuner_priv;
941 *bandwidth = priv->bandwidth;
942 return 0;
943}
944
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300945/* ------------------------------------------------------------------ */
946
947#define tda18271_update_std(std_cfg, name) do { \
948 if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) { \
949 tda_dbg("Using custom std config for %s\n", name); \
950 memcpy(&std->std_cfg, &map->std_cfg, \
951 sizeof(struct tda18271_std_map_item)); \
952 } } while (0)
953
954#define tda18271_dump_std_item(std_cfg, name) do { \
955 tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \
956 name, std->std_cfg.if_freq, std->std_cfg.std_bits); \
957 } while (0)
958
959static int tda18271_dump_std_map(struct dvb_frontend *fe)
960{
961 struct tda18271_priv *priv = fe->tuner_priv;
962 struct tda18271_std_map *std = &priv->std;
963
964 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc353f422008-01-08 10:38:10 -0300965 tda18271_dump_std_item(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300966 tda18271_dump_std_item(atv_b, "pal b");
967 tda18271_dump_std_item(atv_dk, "pal dk");
968 tda18271_dump_std_item(atv_gh, "pal gh");
969 tda18271_dump_std_item(atv_i, "pal i");
970 tda18271_dump_std_item(atv_l, "pal l");
971 tda18271_dump_std_item(atv_lc, "pal l'");
972 tda18271_dump_std_item(atv_mn, "atv mn");
973 tda18271_dump_std_item(atsc_6, "atsc 6");
974 tda18271_dump_std_item(dvbt_6, "dvbt 6");
975 tda18271_dump_std_item(dvbt_7, "dvbt 7");
976 tda18271_dump_std_item(dvbt_8, "dvbt 8");
977 tda18271_dump_std_item(qam_6, "qam 6");
978 tda18271_dump_std_item(qam_8, "qam 8");
979
980 return 0;
981}
982
983static int tda18271_update_std_map(struct dvb_frontend *fe,
984 struct tda18271_std_map *map)
985{
986 struct tda18271_priv *priv = fe->tuner_priv;
987 struct tda18271_std_map *std = &priv->std;
988
989 if (!map)
990 return -EINVAL;
991
Michael Krufkyc353f422008-01-08 10:38:10 -0300992 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300993 tda18271_update_std(atv_b, "atv b");
994 tda18271_update_std(atv_dk, "atv dk");
995 tda18271_update_std(atv_gh, "atv gh");
996 tda18271_update_std(atv_i, "atv i");
997 tda18271_update_std(atv_l, "atv l");
998 tda18271_update_std(atv_lc, "atv l'");
999 tda18271_update_std(atv_mn, "atv mn");
1000 tda18271_update_std(atsc_6, "atsc 6");
1001 tda18271_update_std(dvbt_6, "dvbt 6");
1002 tda18271_update_std(dvbt_7, "dvbt 7");
1003 tda18271_update_std(dvbt_8, "dvbt 8");
1004 tda18271_update_std(qam_6, "qam 6");
1005 tda18271_update_std(qam_8, "qam 8");
1006
1007 return 0;
1008}
1009
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001010static int tda18271_get_id(struct dvb_frontend *fe)
1011{
1012 struct tda18271_priv *priv = fe->tuner_priv;
1013 unsigned char *regs = priv->tda18271_regs;
1014 char *name;
1015 int ret = 0;
1016
Michael Krufky8d316bf2008-01-06 15:31:35 -03001017 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001018 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001019 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001020
1021 switch (regs[R_ID] & 0x7f) {
1022 case 3:
1023 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001024 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001025 break;
1026 case 4:
1027 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001028 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001029 break;
1030 default:
1031 name = "Unknown device";
1032 ret = -EINVAL;
1033 break;
1034 }
1035
Michael Krufky182519f2007-12-25 15:10:11 -03001036 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001037 i2c_adapter_id(priv->i2c_props.adap),
1038 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001039 (0 == ret) ? "" : ", device not supported.");
1040
1041 return ret;
1042}
1043
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001044static struct dvb_tuner_ops tda18271_tuner_ops = {
1045 .info = {
1046 .name = "NXP TDA18271HD",
1047 .frequency_min = 45000000,
1048 .frequency_max = 864000000,
1049 .frequency_step = 62500
1050 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001051 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001052 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001053 .set_params = tda18271_set_params,
1054 .set_analog_params = tda18271_set_analog_params,
1055 .release = tda18271_release,
1056 .get_frequency = tda18271_get_frequency,
1057 .get_bandwidth = tda18271_get_bandwidth,
1058};
1059
1060struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001061 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001062 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001063{
1064 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001065 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001066
Michael Krufkya4f263b2008-01-06 15:52:56 -03001067 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001068
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001069 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1070 hybrid_tuner_instance_list,
1071 i2c, addr, "tda18271");
1072 switch (instance) {
1073 case 0:
1074 goto fail;
1075 break;
1076 case 1:
1077 /* new tuner instance */
Michael Krufkya4f263b2008-01-06 15:52:56 -03001078 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1079 priv->cal_initialized = false;
1080 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001081
Michael Krufkya4f263b2008-01-06 15:52:56 -03001082 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001083
Michael Krufky55553092008-04-22 14:46:06 -03001084 if (cfg)
1085 priv->small_i2c = cfg->small_i2c;
1086
Michael Krufkya4f263b2008-01-06 15:52:56 -03001087 if (tda18271_get_id(fe) < 0)
1088 goto fail;
1089
1090 if (tda18271_assign_map_layout(fe) < 0)
1091 goto fail;
1092
1093 mutex_lock(&priv->lock);
1094 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001095
1096 if ((tda18271_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001097 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001098
Michael Krufkya4f263b2008-01-06 15:52:56 -03001099 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001100 break;
1101 default:
1102 /* existing tuner instance */
1103 fe->tuner_priv = priv;
1104
1105 /* allow dvb driver to override i2c gate setting */
1106 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1107 priv->gate = cfg->gate;
1108 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001109 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001110
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001111 /* override default std map with values in config struct */
1112 if ((cfg) && (cfg->std_map))
1113 tda18271_update_std_map(fe, cfg->std_map);
1114
Michael Krufkya4f263b2008-01-06 15:52:56 -03001115 mutex_unlock(&tda18271_list_mutex);
1116
1117 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1118 sizeof(struct dvb_tuner_ops));
1119
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001120 if (tda18271_debug & DBG_MAP)
1121 tda18271_dump_std_map(fe);
1122
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001123 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001124fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001125 mutex_unlock(&tda18271_list_mutex);
1126
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001127 tda18271_release(fe);
1128 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001129}
1130EXPORT_SYMBOL_GPL(tda18271_attach);
1131MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1132MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1133MODULE_LICENSE("GPL");
Michael Krufky255b5112008-01-01 22:52:09 -03001134MODULE_VERSION("0.2");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001135
1136/*
1137 * Overrides for Emacs so that we follow Linus's tabbing style.
1138 * ---------------------------------------------------------------------------
1139 * Local variables:
1140 * c-basic-offset: 8
1141 * End:
1142 */