blob: d3d91ea9f1ac0e12f37c03bfe5c4a8b8be1f6ac8 [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 Krufky81016b42009-08-27 16:58:06 -030030static int tda18271_cal_on_startup = -1;
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 Krufky4240b462009-08-29 16:25:37 -030039static 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
Michael Krufky868f5cc2008-04-22 14:46:23 -030060static 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
Michael Krufky44e645c2008-06-08 20:10:29 -030069static 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
Michael Krufky255b5112008-01-01 22:52:09 -030084static int tda18271_channel_configuration(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -030085 struct tda18271_std_map_item *map,
86 u32 freq, u32 bw)
Michael Krufky255b5112008-01-01 22:52:09 -030087{
88 struct tda18271_priv *priv = fe->tuner_priv;
89 unsigned char *regs = priv->tda18271_regs;
Michael Krufky31940e32008-05-04 19:37:27 -030090 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -030091 u32 N;
92
93 /* update TV broadcast parameters */
94
95 /* set standard */
96 regs[R_EP3] &= ~0x1f; /* clear std bits */
Michael Krufky7f7203d2008-04-22 14:46:06 -030097 regs[R_EP3] |= (map->agc_mode << 3) | map->std;
Michael Krufky255b5112008-01-01 22:52:09 -030098
Michael Krufky51858d12008-06-09 02:03:31 -030099 if (priv->id == TDA18271HDC2) {
100 /* set rfagc to high speed mode */
101 regs[R_EP3] &= ~0x04;
102 }
Michael Krufky40194b22008-04-22 14:46:22 -0300103
Michael Krufky255b5112008-01-01 22:52:09 -0300104 /* set cal mode to normal */
105 regs[R_EP4] &= ~0x03;
106
Michael Krufky44e645c2008-06-08 20:10:29 -0300107 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300108 regs[R_EP4] &= ~0x1c; /* clear if level bits */
Michael Krufky14c74b22008-04-22 14:46:21 -0300109 regs[R_EP4] |= (map->if_lvl << 2);
Michael Krufky255b5112008-01-01 22:52:09 -0300110
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300111 /* update FM_RFn */
112 regs[R_EP4] &= ~0x80;
113 regs[R_EP4] |= map->fm_rfn << 7;
Michael Krufky255b5112008-01-01 22:52:09 -0300114
Michael Krufkyc0dc0c12008-04-22 14:46:22 -0300115 /* update rf top / if top */
116 regs[R_EB22] = 0x00;
117 regs[R_EB22] |= map->rfagc_top;
Michael Krufky31940e32008-05-04 19:37:27 -0300118 ret = tda18271_write_regs(fe, R_EB22, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300119 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300120 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300121
122 /* --------------------------------------------------------------- */
123
124 /* disable Power Level Indicator */
125 regs[R_EP1] |= 0x40;
126
Michael Krufky7ae1ac42008-06-11 13:52:49 -0300127 /* make sure thermometer is off */
128 regs[R_TM] &= ~0x10;
129
Michael Krufky255b5112008-01-01 22:52:09 -0300130 /* 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
Michael Krufky868f5cc2008-04-22 14:46:23 -0300144 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 }
Michael Krufky255b5112008-01-01 22:52:09 -0300152
153 /* agc1 always active */
154 regs[R_EB1] &= ~0x02;
155
156 /* agc1 has priority on agc2 */
157 regs[R_EB1] &= ~0x01;
158
Michael Krufky31940e32008-05-04 19:37:27 -0300159 ret = tda18271_write_regs(fe, R_EB1, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300160 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300161 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300162
163 /* --------------------------------------------------------------- */
164
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300165 N = map->if_freq * 1000 + freq;
Michael Krufky255b5112008-01-01 22:52:09 -0300166
Michael Krufky868f5cc2008-04-22 14:46:23 -0300167 switch (priv->role) {
168 case TDA18271_MASTER:
169 tda18271_calc_main_pll(fe, N);
Michael Krufky44e645c2008-06-08 20:10:29 -0300170 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300171 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;
Michael Krufky44e645c2008-06-08 20:10:29 -0300178 tda18271_set_if_notch(fe);
Michael Krufky868f5cc2008-04-22 14:46:23 -0300179 tda18271_write_regs(fe, R_MPD, 1);
180 break;
181 }
Michael Krufky255b5112008-01-01 22:52:09 -0300182
Michael Krufky31940e32008-05-04 19:37:27 -0300183 ret = tda18271_write_regs(fe, R_TM, 7);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300184 if (tda_fail(ret))
Michael Krufky31940e32008-05-04 19:37:27 -0300185 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300186
Michael Krufky868f5cc2008-04-22 14:46:23 -0300187 /* force charge pump source */
188 charge_pump_source(fe, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300189
190 msleep(1);
191
Michael Krufky868f5cc2008-04-22 14:46:23 -0300192 /* return pll to normal operation */
193 charge_pump_source(fe, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300194
Michael Krufky40194b22008-04-22 14:46:22 -0300195 msleep(20);
196
Michael Krufky51858d12008-06-09 02:03:31 -0300197 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 }
Michael Krufky31940e32008-05-04 19:37:27 -0300205fail:
206 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300207}
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
Michael Krufky12afe372008-04-22 14:42:07 -0300251/* ------------------------------------------------------------------ */
252
Michael Krufkyd1c53422008-04-22 14:42:07 -0300253static int tda18271c2_rf_tracking_filters_correction(struct dvb_frontend *fe,
254 u32 freq)
Michael Krufky255b5112008-01-01 22:52:09 -0300255{
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;
Michael Krufky3a6b49fe2009-09-27 23:10:20 -0300259 int i, ret;
260 u8 tm_current, dc_over_dt, rf_tab;
261 s32 rfcal_comp, approx;
Michael Krufky255b5112008-01-01 22:52:09 -0300262
263 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300264 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300265 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300266 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300267
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);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300277 if (tda_fail(i))
278 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300279
280 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
Michael Krufky3a6b49fe2009-09-27 23:10:20 -0300281 approx = map[i].rf_a1 * (s32)(freq / 1000 - map[i].rf1) +
282 map[i].rf_b1 + rf_tab;
Michael Krufky255b5112008-01-01 22:52:09 -0300283 } else {
Michael Krufky3a6b49fe2009-09-27 23:10:20 -0300284 approx = map[i].rf_a2 * (s32)(freq / 1000 - map[i].rf2) +
285 map[i].rf_b2 + rf_tab;
Michael Krufky255b5112008-01-01 22:52:09 -0300286 }
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 */
Michael Krufky3a6b49fe2009-09-27 23:10:20 -0300296 rfcal_comp = dc_over_dt * (s32)(tm_current - priv->tm_rfcal) / 1000;
Michael Krufky255b5112008-01-01 22:52:09 -0300297
Michael Krufky3a6b49fe2009-09-27 23:10:20 -0300298 regs[R_EB14] = (unsigned char)(approx + rfcal_comp);
Michael Krufky20f42062008-05-04 19:57:06 -0300299 ret = tda18271_write_regs(fe, R_EB14, 1);
300fail:
301 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300302}
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;
Michael Krufky24124f72008-05-03 19:28:00 -0300308 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300309
310 /* power up detector 1 */
311 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300312 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300313 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300314 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300315
316 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
317 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300318 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300319 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300320 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300321
322 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
323
324 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300325 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300326 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300327 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300328
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 */
Michael Krufky24124f72008-05-03 19:28:00 -0300332 ret = tda18271_write_regs(fe, R_EB21, 3);
333fail:
334 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300335}
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 */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300364 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300365
366 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300367 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300368
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
Michael Krufkyae07d042008-04-22 14:46:21 -0300386 tda18271_calc_cal_pll(fe, N);
387 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300388
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 */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300405 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300406
407 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300408 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300409
Michael Krufkyae07d042008-04-22 14:46:21 -0300410 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300411
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;
Michael Krufky24124f72008-05-03 19:28:00 -0300445 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300446 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 */
Michael Krufky24124f72008-05-03 19:28:00 -0300477 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300478 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300479 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300480
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 */
Michael Krufky24124f72008-05-03 19:28:00 -0300505 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300506 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300507 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300508
509 count += 200;
510
Michael Krufkye7809a02008-04-22 14:46:22 -0300511 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300512 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
Michael Krufkycf04d292008-01-09 00:34:30 -0300528 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300529 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;
Michael Krufky24124f72008-05-03 19:28:00 -0300538 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300539
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
Michael Krufky44e645c2008-06-08 20:10:29 -0300547 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300548 regs[R_EP4] &= ~0x1c; /* clear if level bits */
549
Michael Krufky24124f72008-05-03 19:28:00 -0300550 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300551 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300552 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300553
554 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300555 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300556 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300557 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300558
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
Michael Krufky24124f72008-05-03 19:28:00 -0300565 ret = tda18271_write_regs(fe, R_EB21, 3);
566fail:
567 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300568}
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;
Michael Krufky3986bd12009-09-27 17:00:13 -0300576 s32 divisor, dividend;
Michael Krufky255b5112008-01-01 22:52:09 -0300577#define RF1 0
578#define RF2 1
579#define RF3 2
580 u32 rf_default[3];
581 u32 rf_freq[3];
Michael Krufky381ad0e2011-04-16 13:22:10 -0300582 s32 prog_cal[3];
583 s32 prog_tab[3];
Michael Krufky255b5112008-01-01 22:52:09 -0300584
585 i = tda18271_lookup_rf_band(fe, &freq, NULL);
586
Michael Krufky4bd5d102008-05-04 21:32:21 -0300587 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300588 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;
Michael Krufkycf04d292008-01-09 00:34:30 -0300597 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300598
599 /* look for optimized calibration frequency */
600 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300601 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300602 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300603
604 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
Michael Krufky381ad0e2011-04-16 13:22:10 -0300605 prog_tab[rf] = (s32)regs[R_EB14];
Michael Krufky255b5112008-01-01 22:52:09 -0300606
607 if (1 == bcal)
Michael Krufky381ad0e2011-04-16 13:22:10 -0300608 prog_cal[rf] =
609 (s32)tda18271_calibrate_rf(fe, rf_freq[rf]);
Michael Krufky255b5112008-01-01 22:52:09 -0300610 else
611 prog_cal[rf] = prog_tab[rf];
612
613 switch (rf) {
614 case RF1:
615 map[i].rf_a1 = 0;
Michael Krufky381ad0e2011-04-16 13:22:10 -0300616 map[i].rf_b1 = (prog_cal[RF1] - prog_tab[RF1]);
Michael Krufky255b5112008-01-01 22:52:09 -0300617 map[i].rf1 = rf_freq[RF1] / 1000;
618 break;
619 case RF2:
Michael Krufky381ad0e2011-04-16 13:22:10 -0300620 dividend = (prog_cal[RF2] - prog_tab[RF2] -
621 prog_cal[RF1] + prog_tab[RF1]);
Michael Krufky3986bd12009-09-27 17:00:13 -0300622 divisor = (s32)(rf_freq[RF2] - rf_freq[RF1]) / 1000;
623 map[i].rf_a1 = (dividend / divisor);
Michael Krufky255b5112008-01-01 22:52:09 -0300624 map[i].rf2 = rf_freq[RF2] / 1000;
625 break;
626 case RF3:
Michael Krufky381ad0e2011-04-16 13:22:10 -0300627 dividend = (prog_cal[RF3] - prog_tab[RF3] -
628 prog_cal[RF2] + prog_tab[RF2]);
Michael Krufky3986bd12009-09-27 17:00:13 -0300629 divisor = (s32)(rf_freq[RF3] - rf_freq[RF2]) / 1000;
630 map[i].rf_a2 = (dividend / divisor);
Michael Krufky381ad0e2011-04-16 13:22:10 -0300631 map[i].rf_b2 = (prog_cal[RF2] - prog_tab[RF2]);
Michael Krufky255b5112008-01-01 22:52:09 -0300632 map[i].rf3 = rf_freq[RF3] / 1000;
633 break;
634 default:
635 BUG();
636 }
637 }
638
639 return 0;
640}
641
Michael Krufky09f83c42008-01-05 20:00:09 -0300642static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300643{
644 struct tda18271_priv *priv = fe->tuner_priv;
645 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300646 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300647
648 tda_info("tda18271: performing RF tracking filter calibration\n");
649
650 /* wait for die temperature stabilization */
651 msleep(200);
652
Michael Krufky24124f72008-05-03 19:28:00 -0300653 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300654 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300655 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300656
657 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300658 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
659 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300660 tda18271_rf_tracking_filters_init(fe, 1000 *
661 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300662 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300663 goto fail;
664 }
Michael Krufky255b5112008-01-01 22:52:09 -0300665
Michael Krufky09f83c42008-01-05 20:00:09 -0300666 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300667fail:
668 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300669}
670
671/* ------------------------------------------------------------------ */
672
Michael Krufky12afe372008-04-22 14:42:07 -0300673static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300674{
675 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300676 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300677 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300678
679 /* test RF_CAL_OK to see if we need init */
680 if ((regs[R_EP1] & 0x10) == 0)
681 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300682
683 if (priv->cal_initialized)
684 return 0;
685
Michael Krufky24124f72008-05-03 19:28:00 -0300686 ret = tda18271_calc_rf_filter_curve(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300687 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300688 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300689
Michael Krufky24124f72008-05-03 19:28:00 -0300690 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300691 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300692 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300693
Michael Krufky6bfa6652008-01-07 00:51:48 -0300694 tda_info("tda18271: RF tracking filter calibration complete\n");
695
Michael Krufky255b5112008-01-01 22:52:09 -0300696 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300697 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300698fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300699 tda_info("tda18271: RF tracking filter calibration failed!\n");
700end:
Michael Krufky24124f72008-05-03 19:28:00 -0300701 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300702}
703
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300704static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
705 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300706{
707 struct tda18271_priv *priv = fe->tuner_priv;
708 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300709 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300710 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300711
Michael Krufky255b5112008-01-01 22:52:09 -0300712 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300713 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300714 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
Michael Krufky255b5112008-01-01 22:52:09 -0300729 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300730 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300731
Michael Krufky255b5112008-01-01 22:52:09 -0300732 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300733
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
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300743 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300744
Michael Krufky255b5112008-01-01 22:52:09 -0300745 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300746
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
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300756 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300757
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300758 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300759 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300760 return ret;
761
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300762 msleep(5); /* RF tracking filter calibration initialization */
763
Michael Krufky255b5112008-01-01 22:52:09 -0300764 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300765 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300766 tda18271_write_regs(fe, R_EB13, 1);
767
Michael Krufky255b5112008-01-01 22:52:09 -0300768 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300769 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300770
Michael Krufky255b5112008-01-01 22:52:09 -0300771 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300772 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300773
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);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300785 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300786
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
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300796 /* RF tracking filter correction for VHF_Low band */
797 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300798 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300799
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300800 return 0;
801}
802
Michael Krufkyd1c53422008-04-22 14:42:07 -0300803/* ------------------------------------------------------------------ */
804
Michael Krufky12afe372008-04-22 14:42:07 -0300805static 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;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300809 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300810
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300811 ret = tda18271_read_regs(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300812 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300813 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300814
815 /* test IR_CAL_OK to see if we need init */
816 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300817 ret = tda18271_init_regs(fe);
818fail:
819 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300820}
821
822static int tda18271_init(struct dvb_frontend *fe)
823{
824 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300825 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300826
827 mutex_lock(&priv->lock);
828
Michael Krufky4240b462009-08-29 16:25:37 -0300829 /* full power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300830 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300831 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300832 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300833
834 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300835 ret = tda18271_ir_cal_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300836 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300837 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300838
839 if (priv->id == TDA18271HDC2)
840 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300841fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300842 mutex_unlock(&priv->lock);
843
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300844 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300845}
846
Michael Krufkycc7e26d2009-08-29 16:27:21 -0300847static 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
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300862/* ------------------------------------------------------------------ */
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:
Michael Krufkyecda4272009-09-06 14:38:48 -0300871 /* no external agc configuration required */
872 if (tda18271_debug & DBG_ADV)
873 tda_dbg("no agc configuration provided\n");
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300874 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
Michael Krufkyd1c53422008-04-22 14:42:07 -0300895static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300896 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300897{
898 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300899 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300900
Michael Krufky7f7203d2008-04-22 14:46:06 -0300901 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);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300903
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300904 ret = tda18271_agc(fe);
905 if (tda_fail(ret))
906 tda_warn("failed to configure agc\n");
907
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300908 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300909 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300910 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300911
912 mutex_lock(&priv->lock);
913
Michael Krufkyd1c53422008-04-22 14:42:07 -0300914 switch (priv->id) {
915 case TDA18271HDC1:
916 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
917 break;
918 case TDA18271HDC2:
919 tda18271c2_rf_tracking_filters_correction(fe, freq);
920 break;
921 }
Michael Krufky31940e32008-05-04 19:37:27 -0300922 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300923
Michael Krufky8d316bf2008-01-06 15:31:35 -0300924 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300925fail:
926 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300927}
928
929/* ------------------------------------------------------------------ */
930
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300931static int tda18271_set_params(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300932{
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300933 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;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300937 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300938 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300939 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300940 int ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300941
942 priv->mode = TDA18271_DIGITAL;
943
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300944 switch (delsys) {
945 case SYS_ATSC:
946 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300947 bw = 6000000;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300948 break;
949 case SYS_DVBT:
950 case SYS_DVBT2:
951 if (bw <= 6000000) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300952 map = &std_map->dvbt_6;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300953 } else if (bw <= 7000000) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300954 map = &std_map->dvbt_7;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300955 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300956 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300957 }
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300958 break;
959 case SYS_DVBC_ANNEX_B:
960 bw = 6000000;
961 /* falltrough */
962 case SYS_DVBC_ANNEX_A:
963 case SYS_DVBC_ANNEX_C:
964 if (bw <= 6000000) {
965 map = &std_map->qam_6;
966 } else if (bw <= 7000000) {
967 map = &std_map->qam_7;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300968 } else {
969 map = &std_map->qam_8;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300970 }
971 break;
972 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300973 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300974 return -EINVAL;
975 }
976
Michael Krufkyed736832008-01-19 17:41:04 -0300977 /* When tuning digital, the analog demod must be tri-stated */
978 if (fe->ops.analog_ops.standby)
979 fe->ops.analog_ops.standby(fe);
980
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300981 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300982
Michael Krufky4bd5d102008-05-04 21:32:21 -0300983 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300984 goto fail;
985
Michael Krufky8c8ee112011-11-03 09:59:42 -0300986 priv->if_freq = map->if_freq;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300987 priv->frequency = freq;
Mauro Carvalho Chehabc6f56e72011-12-26 20:02:28 -0300988 priv->bandwidth = bw;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300989fail:
990 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300991}
992
993static int tda18271_set_analog_params(struct dvb_frontend *fe,
994 struct analog_parameters *params)
995{
996 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300997 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300998 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300999 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001000 int ret;
Michael Krufky4d831782009-09-27 14:05:12 -03001001 u32 freq = params->frequency * 125 *
1002 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001003
1004 priv->mode = TDA18271_ANALOG;
1005
Michael Krufkyc353f422008-01-08 10:38:10 -03001006 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001007 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -03001008 mode = "fm";
1009 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001010 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001011 mode = "MN";
1012 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001013 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001014 mode = "B";
1015 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001016 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001017 mode = "GH";
1018 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001019 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001020 mode = "I";
1021 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001022 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001023 mode = "DK";
1024 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001025 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001026 mode = "L";
1027 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001028 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -03001029 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001030 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001031 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001032 mode = "xx";
1033 }
1034
Michael Krufky182519f2007-12-25 15:10:11 -03001035 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001036
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001037 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001038
Michael Krufky4bd5d102008-05-04 21:32:21 -03001039 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001040 goto fail;
1041
Michael Krufky8c8ee112011-11-03 09:59:42 -03001042 priv->if_freq = map->if_freq;
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001043 priv->frequency = freq;
1044 priv->bandwidth = 0;
1045fail:
1046 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001047}
1048
1049static int tda18271_release(struct dvb_frontend *fe)
1050{
Michael Krufkya4f263b2008-01-06 15:52:56 -03001051 struct tda18271_priv *priv = fe->tuner_priv;
1052
1053 mutex_lock(&tda18271_list_mutex);
1054
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001055 if (priv)
1056 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -03001057
Michael Krufkya4f263b2008-01-06 15:52:56 -03001058 mutex_unlock(&tda18271_list_mutex);
1059
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001060 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001061
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001062 return 0;
1063}
1064
1065static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1066{
1067 struct tda18271_priv *priv = fe->tuner_priv;
1068 *frequency = priv->frequency;
1069 return 0;
1070}
1071
1072static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1073{
1074 struct tda18271_priv *priv = fe->tuner_priv;
1075 *bandwidth = priv->bandwidth;
1076 return 0;
1077}
1078
Michael Krufky8c8ee112011-11-03 09:59:42 -03001079static int tda18271_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
1080{
1081 struct tda18271_priv *priv = fe->tuner_priv;
1082 *frequency = (u32)priv->if_freq * 1000;
1083 return 0;
1084}
1085
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001086/* ------------------------------------------------------------------ */
1087
1088#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001089 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001090 map->std_cfg.agc_mode + map->std_cfg.std + \
1091 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001092 tda_dbg("Using custom std config for %s\n", name); \
1093 memcpy(&std->std_cfg, &map->std_cfg, \
1094 sizeof(struct tda18271_std_map_item)); \
1095 } } while (0)
1096
1097#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001098 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1099 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001100 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001101 std->std_cfg.agc_mode, std->std_cfg.std, \
1102 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001103 } while (0)
1104
1105static int tda18271_dump_std_map(struct dvb_frontend *fe)
1106{
1107 struct tda18271_priv *priv = fe->tuner_priv;
1108 struct tda18271_std_map *std = &priv->std;
1109
1110 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001111 tda18271_dump_std_item(fm_radio, " fm ");
1112 tda18271_dump_std_item(atv_b, "atv b ");
1113 tda18271_dump_std_item(atv_dk, "atv dk");
1114 tda18271_dump_std_item(atv_gh, "atv gh");
1115 tda18271_dump_std_item(atv_i, "atv i ");
1116 tda18271_dump_std_item(atv_l, "atv l ");
1117 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001118 tda18271_dump_std_item(atv_mn, "atv mn");
1119 tda18271_dump_std_item(atsc_6, "atsc 6");
1120 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1121 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1122 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001123 tda18271_dump_std_item(qam_6, "qam 6 ");
1124 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001125
1126 return 0;
1127}
1128
1129static int tda18271_update_std_map(struct dvb_frontend *fe,
1130 struct tda18271_std_map *map)
1131{
1132 struct tda18271_priv *priv = fe->tuner_priv;
1133 struct tda18271_std_map *std = &priv->std;
1134
1135 if (!map)
1136 return -EINVAL;
1137
Michael Krufkyc353f422008-01-08 10:38:10 -03001138 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001139 tda18271_update_std(atv_b, "atv b");
1140 tda18271_update_std(atv_dk, "atv dk");
1141 tda18271_update_std(atv_gh, "atv gh");
1142 tda18271_update_std(atv_i, "atv i");
1143 tda18271_update_std(atv_l, "atv l");
1144 tda18271_update_std(atv_lc, "atv l'");
1145 tda18271_update_std(atv_mn, "atv mn");
1146 tda18271_update_std(atsc_6, "atsc 6");
1147 tda18271_update_std(dvbt_6, "dvbt 6");
1148 tda18271_update_std(dvbt_7, "dvbt 7");
1149 tda18271_update_std(dvbt_8, "dvbt 8");
1150 tda18271_update_std(qam_6, "qam 6");
1151 tda18271_update_std(qam_8, "qam 8");
1152
1153 return 0;
1154}
1155
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001156static int tda18271_get_id(struct dvb_frontend *fe)
1157{
1158 struct tda18271_priv *priv = fe->tuner_priv;
1159 unsigned char *regs = priv->tda18271_regs;
1160 char *name;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001161
Michael Krufky8d316bf2008-01-06 15:31:35 -03001162 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001163 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001164 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001165
1166 switch (regs[R_ID] & 0x7f) {
1167 case 3:
1168 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001169 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001170 break;
1171 case 4:
1172 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001173 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001174 break;
1175 default:
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001176 tda_info("Unknown device (%i) detected @ %d-%04x, device not supported.\n",
1177 regs[R_ID], i2c_adapter_id(priv->i2c_props.adap),
1178 priv->i2c_props.addr);
1179 return -EINVAL;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001180 }
1181
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001182 tda_info("%s detected @ %d-%04x\n", name,
1183 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001184
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001185 return 0;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001186}
1187
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001188static int tda18271_setup_configuration(struct dvb_frontend *fe,
1189 struct tda18271_config *cfg)
Michael Krufky42f9a032009-10-23 03:20:45 -03001190{
1191 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky42f9a032009-10-23 03:20:45 -03001192
1193 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1194 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1195 priv->config = (cfg) ? cfg->config : 0;
1196 priv->small_i2c = (cfg) ?
1197 cfg->small_i2c : TDA18271_39_BYTE_CHUNK_INIT;
1198 priv->output_opt = (cfg) ?
1199 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
1200
Michael Krufky42f9a032009-10-23 03:20:45 -03001201 return 0;
1202}
1203
Michael Krufky5881ecf2009-10-24 18:37:22 -03001204static inline int tda18271_need_cal_on_startup(struct tda18271_config *cfg)
1205{
1206 /* tda18271_cal_on_startup == -1 when cal module option is unset */
1207 return ((tda18271_cal_on_startup == -1) ?
1208 /* honor configuration setting */
1209 ((cfg) && (cfg->rf_cal_on_startup)) :
1210 /* module option overrides configuration setting */
1211 (tda18271_cal_on_startup)) ? 1 : 0;
1212}
1213
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001214static int tda18271_set_config(struct dvb_frontend *fe, void *priv_cfg)
1215{
1216 struct tda18271_config *cfg = (struct tda18271_config *) priv_cfg;
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001217
1218 tda18271_setup_configuration(fe, cfg);
1219
Michael Krufky5881ecf2009-10-24 18:37:22 -03001220 if (tda18271_need_cal_on_startup(cfg))
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001221 tda18271_init(fe);
1222
Michael Krufky972aacc2009-11-01 02:52:01 -03001223 /* override default std map with values in config struct */
1224 if ((cfg) && (cfg->std_map))
1225 tda18271_update_std_map(fe, cfg->std_map);
1226
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001227 return 0;
1228}
1229
Chris Rankin0b8bd832011-08-20 16:01:26 -03001230static const struct dvb_tuner_ops tda18271_tuner_ops = {
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001231 .info = {
1232 .name = "NXP TDA18271HD",
1233 .frequency_min = 45000000,
1234 .frequency_max = 864000000,
1235 .frequency_step = 62500
1236 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001237 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001238 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001239 .set_params = tda18271_set_params,
1240 .set_analog_params = tda18271_set_analog_params,
1241 .release = tda18271_release,
Michael Krufky42f9a032009-10-23 03:20:45 -03001242 .set_config = tda18271_set_config,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001243 .get_frequency = tda18271_get_frequency,
1244 .get_bandwidth = tda18271_get_bandwidth,
Michael Krufky8c8ee112011-11-03 09:59:42 -03001245 .get_if_frequency = tda18271_get_if_frequency,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001246};
1247
1248struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001249 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001250 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001251{
1252 struct tda18271_priv *priv = NULL;
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001253 int instance, ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001254
Michael Krufkya4f263b2008-01-06 15:52:56 -03001255 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001256
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001257 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1258 hybrid_tuner_instance_list,
1259 i2c, addr, "tda18271");
1260 switch (instance) {
1261 case 0:
1262 goto fail;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001263 case 1:
1264 /* new tuner instance */
Michael Krufky42f9a032009-10-23 03:20:45 -03001265 fe->tuner_priv = priv;
1266
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001267 tda18271_setup_configuration(fe, cfg);
Michael Krufky81016b42009-08-27 16:58:06 -03001268
Michael Krufkya4f263b2008-01-06 15:52:56 -03001269 priv->cal_initialized = false;
1270 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001271
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001272 ret = tda18271_get_id(fe);
1273 if (tda_fail(ret))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001274 goto fail;
1275
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001276 ret = tda18271_assign_map_layout(fe);
1277 if (tda_fail(ret))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001278 goto fail;
1279
1280 mutex_lock(&priv->lock);
1281 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001282
Michael Krufky5881ecf2009-10-24 18:37:22 -03001283 if ((tda18271_need_cal_on_startup(cfg)) &&
1284 (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001285 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001286
Michael Krufkya4f263b2008-01-06 15:52:56 -03001287 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001288 break;
1289 default:
1290 /* existing tuner instance */
1291 fe->tuner_priv = priv;
1292
Michael Krufkyd5abef62009-08-30 03:07:10 -03001293 /* allow dvb driver to override configuration settings */
1294 if (cfg) {
1295 if (cfg->gate != TDA18271_GATE_ANALOG)
1296 priv->gate = cfg->gate;
1297 if (cfg->role)
1298 priv->role = cfg->role;
1299 if (cfg->config)
1300 priv->config = cfg->config;
1301 if (cfg->small_i2c)
1302 priv->small_i2c = cfg->small_i2c;
1303 if (cfg->output_opt)
1304 priv->output_opt = cfg->output_opt;
Michael Krufky42f9a032009-10-23 03:20:45 -03001305 if (cfg->std_map)
1306 tda18271_update_std_map(fe, cfg->std_map);
Michael Krufkyd5abef62009-08-30 03:07:10 -03001307 }
Michael Krufky5881ecf2009-10-24 18:37:22 -03001308 if (tda18271_need_cal_on_startup(cfg))
Michael Krufky188ea052009-10-24 18:18:03 -03001309 tda18271_init(fe);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001310 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001311 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001312
Michael Krufky972aacc2009-11-01 02:52:01 -03001313 /* override default std map with values in config struct */
1314 if ((cfg) && (cfg->std_map))
1315 tda18271_update_std_map(fe, cfg->std_map);
1316
Michael Krufkya4f263b2008-01-06 15:52:56 -03001317 mutex_unlock(&tda18271_list_mutex);
1318
1319 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1320 sizeof(struct dvb_tuner_ops));
1321
Michael Krufkyc7353722008-03-30 19:40:20 -03001322 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001323 tda18271_dump_std_map(fe);
1324
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001325 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001326fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001327 mutex_unlock(&tda18271_list_mutex);
1328
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001329 tda18271_release(fe);
1330 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001331}
1332EXPORT_SYMBOL_GPL(tda18271_attach);
1333MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1334MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1335MODULE_LICENSE("GPL");
Michael Krufky5abaa532009-10-25 10:43:30 -03001336MODULE_VERSION("0.4");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001337
1338/*
1339 * Overrides for Emacs so that we follow Linus's tabbing style.
1340 * ---------------------------------------------------------------------------
1341 * Local variables:
1342 * c-basic-offset: 8
1343 * End:
1344 */