blob: aa93e155062793be0981661cc1464d91af947279 [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 "
28 "(info=1, map=2, reg=4, adv=8 (or-able))");
Michael Krufky5bea1cd2007-10-22 09:56:38 -030029
Michael Krufkya4f263b2008-01-06 15:52:56 -030030static LIST_HEAD(tda18271_list);
31static DEFINE_MUTEX(tda18271_list_mutex);
32
Michael Krufky5bea1cd2007-10-22 09:56:38 -030033/*---------------------------------------------------------------------*/
34
Michael Krufky09f83c42008-01-05 20:00:09 -030035static int tda18271_ir_cal_init(struct dvb_frontend *fe)
Michael Krufkyefce8412007-12-01 17:40:16 -030036{
37 struct tda18271_priv *priv = fe->tuner_priv;
38 unsigned char *regs = priv->tda18271_regs;
39
40 tda18271_read_regs(fe);
41
42 /* test IR_CAL_OK to see if we need init */
43 if ((regs[R_EP1] & 0x08) == 0)
44 tda18271_init_regs(fe);
45
46 return 0;
47}
48
Michael Krufky255b5112008-01-01 22:52:09 -030049/* ------------------------------------------------------------------ */
50
51static int tda18271_channel_configuration(struct dvb_frontend *fe,
52 u32 ifc, u32 freq, u32 bw, u8 std)
53{
54 struct tda18271_priv *priv = fe->tuner_priv;
55 unsigned char *regs = priv->tda18271_regs;
56 u32 N;
57
58 /* update TV broadcast parameters */
59
60 /* set standard */
61 regs[R_EP3] &= ~0x1f; /* clear std bits */
62 regs[R_EP3] |= std;
63
64 /* set cal mode to normal */
65 regs[R_EP4] &= ~0x03;
66
67 /* update IF output level & IF notch frequency */
68 regs[R_EP4] &= ~0x1c; /* clear if level bits */
69
70 switch (priv->mode) {
71 case TDA18271_ANALOG:
72 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
73 break;
74 case TDA18271_DIGITAL:
75 regs[R_EP4] |= 0x04; /* IF level = 1 */
76 regs[R_MPD] |= 0x80; /* IF notch = 1 */
77 break;
78 }
79 regs[R_EP4] &= ~0x80; /* FM_RFn: turn this bit on only for fm radio */
80
81 /* update RF_TOP / IF_TOP */
82 switch (priv->mode) {
83 case TDA18271_ANALOG:
84 regs[R_EB22] = 0x2c;
85 break;
86 case TDA18271_DIGITAL:
87 regs[R_EB22] = 0x37;
88 break;
89 }
90 tda18271_write_regs(fe, R_EB22, 1);
91
92 /* --------------------------------------------------------------- */
93
94 /* disable Power Level Indicator */
95 regs[R_EP1] |= 0x40;
96
97 /* frequency dependent parameters */
98
99 tda18271_calc_ir_measure(fe, &freq);
100
101 tda18271_calc_bp_filter(fe, &freq);
102
103 tda18271_calc_rf_band(fe, &freq);
104
105 tda18271_calc_gain_taper(fe, &freq);
106
107 /* --------------------------------------------------------------- */
108
109 /* dual tuner and agc1 extra configuration */
110
111 /* main vco when Master, cal vco when slave */
112 regs[R_EB1] |= 0x04; /* FIXME: assumes master */
113
114 /* agc1 always active */
115 regs[R_EB1] &= ~0x02;
116
117 /* agc1 has priority on agc2 */
118 regs[R_EB1] &= ~0x01;
119
120 tda18271_write_regs(fe, R_EB1, 1);
121
122 /* --------------------------------------------------------------- */
123
124 N = freq + ifc;
125
126 /* FIXME: assumes master */
127 tda18271_calc_main_pll(fe, N);
128 tda18271_write_regs(fe, R_MPD, 4);
129
130 tda18271_write_regs(fe, R_TM, 7);
131
132 /* main pll charge pump source */
133 regs[R_EB4] |= 0x20;
134 tda18271_write_regs(fe, R_EB4, 1);
135
136 msleep(1);
137
138 /* normal operation for the main pll */
139 regs[R_EB4] &= ~0x20;
140 tda18271_write_regs(fe, R_EB4, 1);
141
142 msleep(5);
143
144 return 0;
145}
146
147static int tda18271_read_thermometer(struct dvb_frontend *fe)
148{
149 struct tda18271_priv *priv = fe->tuner_priv;
150 unsigned char *regs = priv->tda18271_regs;
151 int tm;
152
153 /* switch thermometer on */
154 regs[R_TM] |= 0x10;
155 tda18271_write_regs(fe, R_TM, 1);
156
157 /* read thermometer info */
158 tda18271_read_regs(fe);
159
160 if ((((regs[R_TM] & 0x0f) == 0x00) && ((regs[R_TM] & 0x20) == 0x20)) ||
161 (((regs[R_TM] & 0x0f) == 0x08) && ((regs[R_TM] & 0x20) == 0x00))) {
162
163 if ((regs[R_TM] & 0x20) == 0x20)
164 regs[R_TM] &= ~0x20;
165 else
166 regs[R_TM] |= 0x20;
167
168 tda18271_write_regs(fe, R_TM, 1);
169
170 msleep(10); /* temperature sensing */
171
172 /* read thermometer info */
173 tda18271_read_regs(fe);
174 }
175
176 tm = tda18271_lookup_thermometer(fe);
177
178 /* switch thermometer off */
179 regs[R_TM] &= ~0x10;
180 tda18271_write_regs(fe, R_TM, 1);
181
182 /* set CAL mode to normal */
183 regs[R_EP4] &= ~0x03;
184 tda18271_write_regs(fe, R_EP4, 1);
185
186 return tm;
187}
188
189static int tda18271_rf_tracking_filters_correction(struct dvb_frontend *fe,
Michael Krufky09f83c42008-01-05 20:00:09 -0300190 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300191{
192 struct tda18271_priv *priv = fe->tuner_priv;
193 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
194 unsigned char *regs = priv->tda18271_regs;
195 int tm_current, rfcal_comp, approx, i;
196 u8 dc_over_dt, rf_tab;
197
198 /* power up */
199 regs[R_EP3] &= ~0xe0; /* sm = 0, sm_lt = 0, sm_xt = 0 */
200 tda18271_write_regs(fe, R_EP3, 1);
201
202 /* read die current temperature */
203 tm_current = tda18271_read_thermometer(fe);
204
205 /* frequency dependent parameters */
206
207 tda18271_calc_rf_cal(fe, &freq);
208 rf_tab = regs[R_EB14];
209
210 i = tda18271_lookup_rf_band(fe, &freq, NULL);
211 if (i < 0)
212 return -EINVAL;
213
214 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
215 approx = map[i].rf_a1 *
216 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
217 } else {
218 approx = map[i].rf_a2 *
219 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
220 }
221
222 if (approx < 0)
223 approx = 0;
224 if (approx > 255)
225 approx = 255;
226
227 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
228
229 /* calculate temperature compensation */
Michael Krufky09f83c42008-01-05 20:00:09 -0300230 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal);
Michael Krufky255b5112008-01-01 22:52:09 -0300231
232 regs[R_EB14] = approx + rfcal_comp;
233 tda18271_write_regs(fe, R_EB14, 1);
234
235 return 0;
236}
237
238static int tda18271_por(struct dvb_frontend *fe)
239{
240 struct tda18271_priv *priv = fe->tuner_priv;
241 unsigned char *regs = priv->tda18271_regs;
242
243 /* power up detector 1 */
244 regs[R_EB12] &= ~0x20;
245 tda18271_write_regs(fe, R_EB12, 1);
246
247 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
248 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
249 tda18271_write_regs(fe, R_EB18, 1);
250
251 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
252
253 /* POR mode */
254 regs[R_EP3] &= ~0xe0; /* clear sm, sm_lt, sm_xt */
255 regs[R_EP3] |= 0x80; /* sm = 1, sm_lt = 0, sm_xt = 0 */
256 tda18271_write_regs(fe, R_EP3, 1);
257
258 /* disable 1.5 MHz low pass filter */
259 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
260 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
261 tda18271_write_regs(fe, R_EB21, 3);
262
263 return 0;
264}
265
266static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
267{
268 struct tda18271_priv *priv = fe->tuner_priv;
269 unsigned char *regs = priv->tda18271_regs;
270 u32 N;
271
272 /* set CAL mode to normal */
273 regs[R_EP4] &= ~0x03;
274 tda18271_write_regs(fe, R_EP4, 1);
275
276 /* switch off agc1 */
277 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
278
279 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
280 tda18271_write_regs(fe, R_EB18, 1);
281
282 /* frequency dependent parameters */
283
284 tda18271_calc_bp_filter(fe, &freq);
285 tda18271_calc_gain_taper(fe, &freq);
286 tda18271_calc_rf_band(fe, &freq);
287 tda18271_calc_km(fe, &freq);
288
289 tda18271_write_regs(fe, R_EP1, 3);
290 tda18271_write_regs(fe, R_EB13, 1);
291
292 /* main pll charge pump source */
293 regs[R_EB4] |= 0x20;
294 tda18271_write_regs(fe, R_EB4, 1);
295
296 /* cal pll charge pump source */
297 regs[R_EB7] |= 0x20;
298 tda18271_write_regs(fe, R_EB7, 1);
299
300 /* force dcdc converter to 0 V */
301 regs[R_EB14] = 0x00;
302 tda18271_write_regs(fe, R_EB14, 1);
303
304 /* disable plls lock */
305 regs[R_EB20] &= ~0x20;
306 tda18271_write_regs(fe, R_EB20, 1);
307
308 /* set CAL mode to RF tracking filter calibration */
309 regs[R_EP4] |= 0x03;
310 tda18271_write_regs(fe, R_EP4, 2);
311
312 /* --------------------------------------------------------------- */
313
314 /* set the internal calibration signal */
315 N = freq;
316
317 tda18271_calc_main_pll(fe, N);
318 tda18271_write_regs(fe, R_MPD, 4);
319
320 /* downconvert internal calibration */
321 N += 1000000;
322
323 tda18271_calc_main_pll(fe, N);
324 tda18271_write_regs(fe, R_MPD, 4);
325
326 msleep(5);
327
328 tda18271_write_regs(fe, R_EP2, 1);
329 tda18271_write_regs(fe, R_EP1, 1);
330 tda18271_write_regs(fe, R_EP2, 1);
331 tda18271_write_regs(fe, R_EP1, 1);
332
333 /* --------------------------------------------------------------- */
334
335 /* normal operation for the main pll */
336 regs[R_EB4] &= ~0x20;
337 tda18271_write_regs(fe, R_EB4, 1);
338
339 /* normal operation for the cal pll */
340 regs[R_EB7] &= ~0x20;
341 tda18271_write_regs(fe, R_EB7, 1);
342
343 msleep(5); /* plls locking */
344
345 /* launch the rf tracking filters calibration */
346 regs[R_EB20] |= 0x20;
347 tda18271_write_regs(fe, R_EB20, 1);
348
349 msleep(60); /* calibration */
350
351 /* --------------------------------------------------------------- */
352
353 /* set CAL mode to normal */
354 regs[R_EP4] &= ~0x03;
355
356 /* switch on agc1 */
357 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
358
359 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
360 tda18271_write_regs(fe, R_EB18, 1);
361
362 tda18271_write_regs(fe, R_EP3, 2);
363
364 /* synchronization */
365 tda18271_write_regs(fe, R_EP1, 1);
366
367 /* get calibration result */
368 tda18271_read_extended(fe);
369
370 return regs[R_EB14];
371}
372
373static int tda18271_powerscan(struct dvb_frontend *fe,
374 u32 *freq_in, u32 *freq_out)
375{
376 struct tda18271_priv *priv = fe->tuner_priv;
377 unsigned char *regs = priv->tda18271_regs;
378 int sgn, bcal, count, wait;
379 u8 cid_target;
380 u16 count_limit;
381 u32 freq;
382
383 freq = *freq_in;
384
385 tda18271_calc_rf_band(fe, &freq);
386 tda18271_calc_rf_cal(fe, &freq);
387 tda18271_calc_gain_taper(fe, &freq);
388 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
389
390 tda18271_write_regs(fe, R_EP2, 1);
391 tda18271_write_regs(fe, R_EB14, 1);
392
393 /* downconvert frequency */
394 freq += 1000000;
395
396 tda18271_calc_main_pll(fe, freq);
397 tda18271_write_regs(fe, R_MPD, 4);
398
399 msleep(5); /* pll locking */
400
401 /* detection mode */
402 regs[R_EP4] &= ~0x03;
403 regs[R_EP4] |= 0x01;
404 tda18271_write_regs(fe, R_EP4, 1);
405
406 /* launch power detection measurement */
407 tda18271_write_regs(fe, R_EP2, 1);
408
409 /* read power detection info, stored in EB10 */
410 tda18271_read_extended(fe);
411
412 /* algorithm initialization */
413 sgn = 1;
414 *freq_out = *freq_in;
415 bcal = 0;
416 count = 0;
417 wait = false;
418
419 while ((regs[R_EB10] & 0x3f) < cid_target) {
420 /* downconvert updated freq to 1 MHz */
421 freq = *freq_in + (sgn * count) + 1000000;
422
423 tda18271_calc_main_pll(fe, freq);
424 tda18271_write_regs(fe, R_MPD, 4);
425
426 if (wait) {
427 msleep(5); /* pll locking */
428 wait = false;
429 } else
430 udelay(100); /* pll locking */
431
432 /* launch power detection measurement */
433 tda18271_write_regs(fe, R_EP2, 1);
434
435 /* read power detection info, stored in EB10 */
436 tda18271_read_extended(fe);
437
438 count += 200;
439
440 if (count < count_limit)
441 continue;
442
443 if (sgn <= 0)
444 break;
445
446 sgn = -1 * sgn;
447 count = 200;
448 wait = true;
449 }
450
451 if ((regs[R_EB10] & 0x3f) >= cid_target) {
452 bcal = 1;
453 *freq_out = freq - 1000000;
454 } else
455 bcal = 0;
456
457 tda_dbg("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
458 bcal, *freq_in, *freq_out, freq);
459
460 return bcal;
461}
462
463static int tda18271_powerscan_init(struct dvb_frontend *fe)
464{
465 struct tda18271_priv *priv = fe->tuner_priv;
466 unsigned char *regs = priv->tda18271_regs;
467
468 /* set standard to digital */
469 regs[R_EP3] &= ~0x1f; /* clear std bits */
470 regs[R_EP3] |= 0x12;
471
472 /* set cal mode to normal */
473 regs[R_EP4] &= ~0x03;
474
475 /* update IF output level & IF notch frequency */
476 regs[R_EP4] &= ~0x1c; /* clear if level bits */
477
478 tda18271_write_regs(fe, R_EP3, 2);
479
480 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
481 tda18271_write_regs(fe, R_EB18, 1);
482
483 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
484
485 /* 1.5 MHz low pass filter */
486 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
487 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
488
489 tda18271_write_regs(fe, R_EB21, 3);
490
491 return 0;
492}
493
494static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
495{
496 struct tda18271_priv *priv = fe->tuner_priv;
497 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
498 unsigned char *regs = priv->tda18271_regs;
499 int bcal, rf, i;
500#define RF1 0
501#define RF2 1
502#define RF3 2
503 u32 rf_default[3];
504 u32 rf_freq[3];
505 u8 prog_cal[3];
506 u8 prog_tab[3];
507
508 i = tda18271_lookup_rf_band(fe, &freq, NULL);
509
510 if (i < 0)
511 return i;
512
513 rf_default[RF1] = 1000 * map[i].rf1_def;
514 rf_default[RF2] = 1000 * map[i].rf2_def;
515 rf_default[RF3] = 1000 * map[i].rf3_def;
516
517 for (rf = RF1; rf <= RF3; rf++) {
518 if (0 == rf_default[rf])
519 return 0;
520 tda_dbg("freq = %d, rf = %d\n", freq, rf);
521
522 /* look for optimized calibration frequency */
523 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
524
525 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
526 prog_tab[rf] = regs[R_EB14];
527
528 if (1 == bcal)
529 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
530 else
531 prog_cal[rf] = prog_tab[rf];
532
533 switch (rf) {
534 case RF1:
535 map[i].rf_a1 = 0;
536 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
537 map[i].rf1 = rf_freq[RF1] / 1000;
538 break;
539 case RF2:
540 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
541 prog_cal[RF1] + prog_tab[RF1]) /
542 ((rf_freq[RF2] - rf_freq[RF1]) / 1000);
543 map[i].rf2 = rf_freq[RF2] / 1000;
544 break;
545 case RF3:
546 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
547 prog_cal[RF2] + prog_tab[RF2]) /
548 ((rf_freq[RF3] - rf_freq[RF2]) / 1000);
549 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
550 map[i].rf3 = rf_freq[RF3] / 1000;
551 break;
552 default:
553 BUG();
554 }
555 }
556
557 return 0;
558}
559
Michael Krufky09f83c42008-01-05 20:00:09 -0300560static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300561{
562 struct tda18271_priv *priv = fe->tuner_priv;
563 unsigned int i;
564
565 tda_info("tda18271: performing RF tracking filter calibration\n");
566
567 /* wait for die temperature stabilization */
568 msleep(200);
569
570 tda18271_powerscan_init(fe);
571
572 /* rf band calibration */
573 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++)
574 tda18271_rf_tracking_filters_init(fe, 1000 *
575 priv->rf_cal_state[i].rfmax);
576
Michael Krufky09f83c42008-01-05 20:00:09 -0300577 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300578
579 return 0;
580}
581
582/* ------------------------------------------------------------------ */
583
Michael Krufky09f83c42008-01-05 20:00:09 -0300584static int tda18271_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300585{
586 struct tda18271_priv *priv = fe->tuner_priv;
587
588 if (priv->cal_initialized)
589 return 0;
590
Michael Krufky09f83c42008-01-05 20:00:09 -0300591 tda18271_calc_rf_filter_curve(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300592
593 tda18271_por(fe);
594
595 priv->cal_initialized = true;
596
597 return 0;
598}
599
Michael Krufky09f83c42008-01-05 20:00:09 -0300600static int tda18271_init(struct dvb_frontend *fe)
601{
602 struct tda18271_priv *priv = fe->tuner_priv;
603
Michael Krufky8d316bf2008-01-06 15:31:35 -0300604 mutex_lock(&priv->lock);
605
Michael Krufky09f83c42008-01-05 20:00:09 -0300606 /* initialization */
607 tda18271_ir_cal_init(fe);
608
609 if (priv->id == TDA18271HDC2)
610 tda18271_rf_cal_init(fe);
611
Michael Krufky8d316bf2008-01-06 15:31:35 -0300612 mutex_unlock(&priv->lock);
613
Michael Krufky09f83c42008-01-05 20:00:09 -0300614 return 0;
615}
616
Michael Krufky255b5112008-01-01 22:52:09 -0300617static int tda18271c2_tune(struct dvb_frontend *fe,
618 u32 ifc, u32 freq, u32 bw, u8 std)
619{
Michael Krufky8d316bf2008-01-06 15:31:35 -0300620 struct tda18271_priv *priv = fe->tuner_priv;
621
Michael Krufky255b5112008-01-01 22:52:09 -0300622 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
623
Michael Krufky09f83c42008-01-05 20:00:09 -0300624 tda18271_init(fe);
Michael Krufky255b5112008-01-01 22:52:09 -0300625
Michael Krufky8d316bf2008-01-06 15:31:35 -0300626 mutex_lock(&priv->lock);
627
Michael Krufky09f83c42008-01-05 20:00:09 -0300628 tda18271_rf_tracking_filters_correction(fe, freq);
Michael Krufky255b5112008-01-01 22:52:09 -0300629
630 tda18271_channel_configuration(fe, ifc, freq, bw, std);
631
Michael Krufky8d316bf2008-01-06 15:31:35 -0300632 mutex_unlock(&priv->lock);
633
Michael Krufky255b5112008-01-01 22:52:09 -0300634 return 0;
635}
636
637/* ------------------------------------------------------------------ */
638
639static int tda18271c1_tune(struct dvb_frontend *fe,
640 u32 ifc, u32 freq, u32 bw, u8 std)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300641{
642 struct tda18271_priv *priv = fe->tuner_priv;
643 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300644 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300645
Michael Krufky14572632007-12-02 02:32:49 -0300646 tda18271_init(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300647
Michael Krufky8d316bf2008-01-06 15:31:35 -0300648 mutex_lock(&priv->lock);
649
Michael Krufky182519f2007-12-25 15:10:11 -0300650 tda_dbg("freq = %d, ifc = %d\n", freq, ifc);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300651
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300652 /* RF tracking filter calibration */
653
Michael Krufky255b5112008-01-01 22:52:09 -0300654 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300655 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300656 tda18271_write_regs(fe, R_EP1, 1);
657
658 regs[R_EB4] &= 0x07;
659 regs[R_EB4] |= 0x60;
660 tda18271_write_regs(fe, R_EB4, 1);
661
662 regs[R_EB7] = 0x60;
663 tda18271_write_regs(fe, R_EB7, 1);
664
665 regs[R_EB14] = 0x00;
666 tda18271_write_regs(fe, R_EB14, 1);
667
668 regs[R_EB20] = 0xcc;
669 tda18271_write_regs(fe, R_EB20, 1);
670
Michael Krufky255b5112008-01-01 22:52:09 -0300671 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300672 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300673
Michael Krufky255b5112008-01-01 22:52:09 -0300674 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300675
676 switch (priv->mode) {
677 case TDA18271_ANALOG:
678 N = freq - 1250000;
679 break;
680 case TDA18271_DIGITAL:
681 N = freq + bw / 2;
682 break;
683 }
684
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300685 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300686
Michael Krufky255b5112008-01-01 22:52:09 -0300687 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300688
689 switch (priv->mode) {
690 case TDA18271_ANALOG:
691 N = freq - 250000;
692 break;
693 case TDA18271_DIGITAL:
694 N = freq + bw / 2 + 1000000;
695 break;
696 }
697
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300698 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300699
700 tda18271_write_regs(fe, R_EP3, 11);
701 msleep(5); /* RF tracking filter calibration initialization */
702
Michael Krufky255b5112008-01-01 22:52:09 -0300703 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300704 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300705 tda18271_write_regs(fe, R_EB13, 1);
706
Michael Krufky255b5112008-01-01 22:52:09 -0300707 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300708 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300709
Michael Krufky255b5112008-01-01 22:52:09 -0300710 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300711 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300712
713 tda18271_write_regs(fe, R_EP2, 1);
714 tda18271_write_regs(fe, R_EP1, 1);
715 tda18271_write_regs(fe, R_EP2, 1);
716 tda18271_write_regs(fe, R_EP1, 1);
717
718 regs[R_EB4] &= 0x07;
719 regs[R_EB4] |= 0x40;
720 tda18271_write_regs(fe, R_EB4, 1);
721
722 regs[R_EB7] = 0x40;
723 tda18271_write_regs(fe, R_EB7, 1);
724 msleep(10);
725
726 regs[R_EB20] = 0xec;
727 tda18271_write_regs(fe, R_EB20, 1);
728 msleep(60); /* RF tracking filter calibration completion */
729
730 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
731 tda18271_write_regs(fe, R_EP4, 1);
732
733 tda18271_write_regs(fe, R_EP1, 1);
734
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300735 /* RF tracking filter correction for VHF_Low band */
736 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300737 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300738
739 /* Channel Configuration */
740
741 switch (priv->mode) {
742 case TDA18271_ANALOG:
743 regs[R_EB22] = 0x2c;
744 break;
745 case TDA18271_DIGITAL:
746 regs[R_EB22] = 0x37;
747 break;
748 }
749 tda18271_write_regs(fe, R_EB22, 1);
750
751 regs[R_EP1] |= 0x40; /* set dis power level on */
752
753 /* set standard */
754 regs[R_EP3] &= ~0x1f; /* clear std bits */
755
756 /* see table 22 */
757 regs[R_EP3] |= std;
758
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300759 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
760
761 regs[R_EP4] &= ~0x1c; /* clear if level bits */
762 switch (priv->mode) {
763 case TDA18271_ANALOG:
764 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
765 break;
766 case TDA18271_DIGITAL:
767 regs[R_EP4] |= 0x04;
768 regs[R_MPD] |= 0x80;
769 break;
770 }
771
772 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
773
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300774 /* image rejection validity */
775 tda18271_calc_ir_measure(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300776
777 /* calculate MAIN PLL */
778 N = freq + ifc;
779
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300780 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300781
782 tda18271_write_regs(fe, R_TM, 15);
783 msleep(5);
Michael Krufky8d316bf2008-01-06 15:31:35 -0300784 mutex_unlock(&priv->lock);
Michael Krufky6ca04de2007-11-23 16:52:15 -0300785
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300786 return 0;
787}
788
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300789static inline int tda18271_tune(struct dvb_frontend *fe,
790 u32 ifc, u32 freq, u32 bw, u8 std)
791{
792 struct tda18271_priv *priv = fe->tuner_priv;
793 int ret = -EINVAL;
794
795 switch (priv->id) {
796 case TDA18271HDC1:
797 ret = tda18271c1_tune(fe, ifc, freq, bw, std);
798 break;
799 case TDA18271HDC2:
800 ret = tda18271c2_tune(fe, ifc, freq, bw, std);
801 break;
802 }
803 return ret;
804}
805
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300806/* ------------------------------------------------------------------ */
807
808static int tda18271_set_params(struct dvb_frontend *fe,
809 struct dvb_frontend_parameters *params)
810{
811 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300812 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300813 int ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300814 u8 std;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300815 u16 sgIF;
816 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300817
818 priv->mode = TDA18271_DIGITAL;
819
820 /* see table 22 */
821 if (fe->ops.info.type == FE_ATSC) {
822 switch (params->u.vsb.modulation) {
823 case VSB_8:
824 case VSB_16:
Michael Krufky255b5112008-01-01 22:52:09 -0300825 std = std_map->atsc_6.std_bits;
826 sgIF = std_map->atsc_6.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300827 break;
828 case QAM_64:
829 case QAM_256:
Michael Krufky255b5112008-01-01 22:52:09 -0300830 std = std_map->qam_6.std_bits;
831 sgIF = std_map->qam_6.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300832 break;
833 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300834 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300835 return -EINVAL;
836 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300837#if 0
838 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300839 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300840#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300841 bw = 6000000;
842 } else if (fe->ops.info.type == FE_OFDM) {
843 switch (params->u.ofdm.bandwidth) {
844 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300845 bw = 6000000;
Michael Krufky255b5112008-01-01 22:52:09 -0300846 std = std_map->dvbt_6.std_bits;
847 sgIF = std_map->dvbt_6.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300848 break;
849 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300850 bw = 7000000;
Michael Krufky255b5112008-01-01 22:52:09 -0300851 std = std_map->dvbt_7.std_bits;
852 sgIF = std_map->dvbt_7.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300853 break;
854 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300855 bw = 8000000;
Michael Krufky255b5112008-01-01 22:52:09 -0300856 std = std_map->dvbt_8.std_bits;
857 sgIF = std_map->dvbt_8.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300858 break;
859 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300860 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300861 return -EINVAL;
862 }
863 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300864 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300865 return -EINVAL;
866 }
867
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300868 ret = tda18271_tune(fe, sgIF * 1000, freq, bw, std);
869
870 if (ret < 0)
871 goto fail;
872
873 priv->frequency = freq;
874 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
875 params->u.ofdm.bandwidth : 0;
876fail:
877 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300878}
879
880static int tda18271_set_analog_params(struct dvb_frontend *fe,
881 struct analog_parameters *params)
882{
883 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300884 struct tda18271_std_map *std_map = &priv->std;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300885 char *mode;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300886 int ret;
Michael Krufky95af8a22008-01-01 18:31:34 -0300887 u8 std;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300888 u16 sgIF;
889 u32 freq = params->frequency * 62500;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300890
891 priv->mode = TDA18271_ANALOG;
892
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300893 if (params->std & V4L2_STD_MN) {
Michael Krufky255b5112008-01-01 22:52:09 -0300894 std = std_map->atv_mn.std_bits;
895 sgIF = std_map->atv_mn.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300896 mode = "MN";
897 } else if (params->std & V4L2_STD_B) {
Michael Krufky255b5112008-01-01 22:52:09 -0300898 std = std_map->atv_b.std_bits;
899 sgIF = std_map->atv_b.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300900 mode = "B";
901 } else if (params->std & V4L2_STD_GH) {
Michael Krufky255b5112008-01-01 22:52:09 -0300902 std = std_map->atv_gh.std_bits;
903 sgIF = std_map->atv_gh.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300904 mode = "GH";
905 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufky255b5112008-01-01 22:52:09 -0300906 std = std_map->atv_i.std_bits;
907 sgIF = std_map->atv_i.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300908 mode = "I";
909 } else if (params->std & V4L2_STD_DK) {
Michael Krufky255b5112008-01-01 22:52:09 -0300910 std = std_map->atv_dk.std_bits;
911 sgIF = std_map->atv_dk.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300912 mode = "DK";
913 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufky255b5112008-01-01 22:52:09 -0300914 std = std_map->atv_l.std_bits;
915 sgIF = std_map->atv_l.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300916 mode = "L";
917 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufky255b5112008-01-01 22:52:09 -0300918 std = std_map->atv_lc.std_bits;
919 sgIF = std_map->atv_lc.if_freq;
Michael Krufky95af8a22008-01-01 18:31:34 -0300920 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300921 } else {
Michael Krufky255b5112008-01-01 22:52:09 -0300922 std = std_map->atv_i.std_bits;
923 sgIF = std_map->atv_i.if_freq;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300924 mode = "xx";
925 }
926
Michael Krufky182519f2007-12-25 15:10:11 -0300927 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300928
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300929 ret = tda18271_tune(fe, sgIF * 1000, freq, 0, std);
930
931 if (ret < 0)
932 goto fail;
933
934 priv->frequency = freq;
935 priv->bandwidth = 0;
936fail:
937 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300938}
939
940static int tda18271_release(struct dvb_frontend *fe)
941{
Michael Krufkya4f263b2008-01-06 15:52:56 -0300942 struct tda18271_priv *priv = fe->tuner_priv;
943
944 mutex_lock(&tda18271_list_mutex);
945
946 priv->count--;
947
948 if (!priv->count) {
949 tda_dbg("destroying instance @ %d-%04x\n",
950 i2c_adapter_id(priv->i2c_adap),
951 priv->i2c_addr);
952 list_del(&priv->tda18271_list);
953
954 kfree(priv);
955 }
956 mutex_unlock(&tda18271_list_mutex);
957
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300958 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -0300959
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300960 return 0;
961}
962
963static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
964{
965 struct tda18271_priv *priv = fe->tuner_priv;
966 *frequency = priv->frequency;
967 return 0;
968}
969
970static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
971{
972 struct tda18271_priv *priv = fe->tuner_priv;
973 *bandwidth = priv->bandwidth;
974 return 0;
975}
976
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300977/* ------------------------------------------------------------------ */
978
979#define tda18271_update_std(std_cfg, name) do { \
980 if (map->std_cfg.if_freq + map->std_cfg.std_bits > 0) { \
981 tda_dbg("Using custom std config for %s\n", name); \
982 memcpy(&std->std_cfg, &map->std_cfg, \
983 sizeof(struct tda18271_std_map_item)); \
984 } } while (0)
985
986#define tda18271_dump_std_item(std_cfg, name) do { \
987 tda_dbg("(%s) if freq = %d, std bits = 0x%02x\n", \
988 name, std->std_cfg.if_freq, std->std_cfg.std_bits); \
989 } while (0)
990
991static int tda18271_dump_std_map(struct dvb_frontend *fe)
992{
993 struct tda18271_priv *priv = fe->tuner_priv;
994 struct tda18271_std_map *std = &priv->std;
995
996 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
997 tda18271_dump_std_item(atv_b, "pal b");
998 tda18271_dump_std_item(atv_dk, "pal dk");
999 tda18271_dump_std_item(atv_gh, "pal gh");
1000 tda18271_dump_std_item(atv_i, "pal i");
1001 tda18271_dump_std_item(atv_l, "pal l");
1002 tda18271_dump_std_item(atv_lc, "pal l'");
1003 tda18271_dump_std_item(atv_mn, "atv mn");
1004 tda18271_dump_std_item(atsc_6, "atsc 6");
1005 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1006 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1007 tda18271_dump_std_item(dvbt_8, "dvbt 8");
1008 tda18271_dump_std_item(qam_6, "qam 6");
1009 tda18271_dump_std_item(qam_8, "qam 8");
1010
1011 return 0;
1012}
1013
1014static int tda18271_update_std_map(struct dvb_frontend *fe,
1015 struct tda18271_std_map *map)
1016{
1017 struct tda18271_priv *priv = fe->tuner_priv;
1018 struct tda18271_std_map *std = &priv->std;
1019
1020 if (!map)
1021 return -EINVAL;
1022
1023 tda18271_update_std(atv_b, "atv b");
1024 tda18271_update_std(atv_dk, "atv dk");
1025 tda18271_update_std(atv_gh, "atv gh");
1026 tda18271_update_std(atv_i, "atv i");
1027 tda18271_update_std(atv_l, "atv l");
1028 tda18271_update_std(atv_lc, "atv l'");
1029 tda18271_update_std(atv_mn, "atv mn");
1030 tda18271_update_std(atsc_6, "atsc 6");
1031 tda18271_update_std(dvbt_6, "dvbt 6");
1032 tda18271_update_std(dvbt_7, "dvbt 7");
1033 tda18271_update_std(dvbt_8, "dvbt 8");
1034 tda18271_update_std(qam_6, "qam 6");
1035 tda18271_update_std(qam_8, "qam 8");
1036
1037 return 0;
1038}
1039
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001040static int tda18271_get_id(struct dvb_frontend *fe)
1041{
1042 struct tda18271_priv *priv = fe->tuner_priv;
1043 unsigned char *regs = priv->tda18271_regs;
1044 char *name;
1045 int ret = 0;
1046
Michael Krufky8d316bf2008-01-06 15:31:35 -03001047 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001048 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001049 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001050
1051 switch (regs[R_ID] & 0x7f) {
1052 case 3:
1053 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001054 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001055 break;
1056 case 4:
1057 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001058 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001059 break;
1060 default:
1061 name = "Unknown device";
1062 ret = -EINVAL;
1063 break;
1064 }
1065
Michael Krufky182519f2007-12-25 15:10:11 -03001066 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001067 i2c_adapter_id(priv->i2c_adap), priv->i2c_addr,
1068 (0 == ret) ? "" : ", device not supported.");
1069
1070 return ret;
1071}
1072
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001073static struct dvb_tuner_ops tda18271_tuner_ops = {
1074 .info = {
1075 .name = "NXP TDA18271HD",
1076 .frequency_min = 45000000,
1077 .frequency_max = 864000000,
1078 .frequency_step = 62500
1079 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001080 .init = tda18271_init,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001081 .set_params = tda18271_set_params,
1082 .set_analog_params = tda18271_set_analog_params,
1083 .release = tda18271_release,
1084 .get_frequency = tda18271_get_frequency,
1085 .get_bandwidth = tda18271_get_bandwidth,
1086};
1087
1088struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001089 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001090 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001091{
1092 struct tda18271_priv *priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001093 int state_found = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001094
Michael Krufkya4f263b2008-01-06 15:52:56 -03001095 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001096
Michael Krufkya4f263b2008-01-06 15:52:56 -03001097 list_for_each_entry(priv, &tda18271_list, tda18271_list) {
1098 if ((i2c_adapter_id(priv->i2c_adap) == i2c_adapter_id(i2c)) &&
1099 (priv->i2c_addr == addr)) {
1100 tda_dbg("attaching existing tuner @ %d-%04x\n",
1101 i2c_adapter_id(priv->i2c_adap),
1102 priv->i2c_addr);
1103 priv->count++;
1104 fe->tuner_priv = priv;
1105 state_found = 1;
1106 /* allow dvb driver to override i2c gate setting */
1107 if ((cfg) && (cfg->gate != TDA18271_GATE_ANALOG))
1108 priv->gate = cfg->gate;
1109 break;
1110 }
1111 }
1112 if (state_found == 0) {
1113 tda_dbg("creating new tuner instance @ %d-%04x\n",
1114 i2c_adapter_id(i2c), addr);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001115
Michael Krufkya4f263b2008-01-06 15:52:56 -03001116 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
1117 if (priv == NULL) {
1118 mutex_unlock(&tda18271_list_mutex);
1119 return NULL;
1120 }
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001121
Michael Krufkya4f263b2008-01-06 15:52:56 -03001122 priv->i2c_addr = addr;
1123 priv->i2c_adap = i2c;
1124 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1125 priv->cal_initialized = false;
1126 mutex_init(&priv->lock);
1127 priv->count++;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001128
Michael Krufkya4f263b2008-01-06 15:52:56 -03001129 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001130
Michael Krufkya4f263b2008-01-06 15:52:56 -03001131 list_add_tail(&priv->tda18271_list, &tda18271_list);
1132
1133 if (tda18271_get_id(fe) < 0)
1134 goto fail;
1135
1136 if (tda18271_assign_map_layout(fe) < 0)
1137 goto fail;
1138
1139 mutex_lock(&priv->lock);
1140 tda18271_init_regs(fe);
1141 mutex_unlock(&priv->lock);
1142 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001143
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001144 /* override default std map with values in config struct */
1145 if ((cfg) && (cfg->std_map))
1146 tda18271_update_std_map(fe, cfg->std_map);
1147
Michael Krufkya4f263b2008-01-06 15:52:56 -03001148 mutex_unlock(&tda18271_list_mutex);
1149
1150 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1151 sizeof(struct dvb_tuner_ops));
1152
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001153 if (tda18271_debug & DBG_MAP)
1154 tda18271_dump_std_map(fe);
1155
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001156 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001157fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001158 mutex_unlock(&tda18271_list_mutex);
1159
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001160 tda18271_release(fe);
1161 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001162}
1163EXPORT_SYMBOL_GPL(tda18271_attach);
1164MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1165MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1166MODULE_LICENSE("GPL");
Michael Krufky255b5112008-01-01 22:52:09 -03001167MODULE_VERSION("0.2");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001168
1169/*
1170 * Overrides for Emacs so that we follow Linus's tabbing style.
1171 * ---------------------------------------------------------------------------
1172 * Local variables:
1173 * c-basic-offset: 8
1174 * End:
1175 */