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