blob: 63cc4004e211e9654044b612400460bb93832bf9 [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
931static int tda18271_set_params(struct dvb_frontend *fe,
932 struct dvb_frontend_parameters *params)
933{
934 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300935 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300936 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300937 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300938 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300939
940 priv->mode = TDA18271_DIGITAL;
941
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300942 if (fe->ops.info.type == FE_ATSC) {
943 switch (params->u.vsb.modulation) {
944 case VSB_8:
945 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300946 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300947 break;
948 case QAM_64:
949 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300950 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300951 break;
952 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300953 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300954 return -EINVAL;
955 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300956#if 0
957 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300958 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300959#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300960 bw = 6000000;
961 } else if (fe->ops.info.type == FE_OFDM) {
962 switch (params->u.ofdm.bandwidth) {
963 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300964 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300965 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300966 break;
967 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300968 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300969 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300970 break;
971 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300972 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300973 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300974 break;
975 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300976 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300977 return -EINVAL;
978 }
Antti Palosaarif9d0bc12011-04-07 15:45:38 -0300979 } else if (fe->ops.info.type == FE_QAM) {
980 /* DVB-C */
981 map = &std_map->qam_8;
982 bw = 8000000;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300983 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300984 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300985 return -EINVAL;
986 }
987
Michael Krufkyed736832008-01-19 17:41:04 -0300988 /* When tuning digital, the analog demod must be tri-stated */
989 if (fe->ops.analog_ops.standby)
990 fe->ops.analog_ops.standby(fe);
991
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300992 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300993
Michael Krufky4bd5d102008-05-04 21:32:21 -0300994 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300995 goto fail;
996
997 priv->frequency = freq;
998 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
999 params->u.ofdm.bandwidth : 0;
1000fail:
1001 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001002}
1003
1004static int tda18271_set_analog_params(struct dvb_frontend *fe,
1005 struct analog_parameters *params)
1006{
1007 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001008 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001009 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001010 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001011 int ret;
Michael Krufky4d831782009-09-27 14:05:12 -03001012 u32 freq = params->frequency * 125 *
1013 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001014
1015 priv->mode = TDA18271_ANALOG;
1016
Michael Krufkyc353f422008-01-08 10:38:10 -03001017 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001018 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -03001019 mode = "fm";
1020 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001021 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001022 mode = "MN";
1023 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001024 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001025 mode = "B";
1026 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001027 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001028 mode = "GH";
1029 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001030 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001031 mode = "I";
1032 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001033 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001034 mode = "DK";
1035 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001036 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001037 mode = "L";
1038 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001039 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -03001040 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001041 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001042 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001043 mode = "xx";
1044 }
1045
Michael Krufky182519f2007-12-25 15:10:11 -03001046 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001047
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001048 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001049
Michael Krufky4bd5d102008-05-04 21:32:21 -03001050 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001051 goto fail;
1052
1053 priv->frequency = freq;
1054 priv->bandwidth = 0;
1055fail:
1056 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001057}
1058
1059static int tda18271_release(struct dvb_frontend *fe)
1060{
Michael Krufkya4f263b2008-01-06 15:52:56 -03001061 struct tda18271_priv *priv = fe->tuner_priv;
1062
1063 mutex_lock(&tda18271_list_mutex);
1064
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001065 if (priv)
1066 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -03001067
Michael Krufkya4f263b2008-01-06 15:52:56 -03001068 mutex_unlock(&tda18271_list_mutex);
1069
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001070 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001071
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001072 return 0;
1073}
1074
1075static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1076{
1077 struct tda18271_priv *priv = fe->tuner_priv;
1078 *frequency = priv->frequency;
1079 return 0;
1080}
1081
1082static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1083{
1084 struct tda18271_priv *priv = fe->tuner_priv;
1085 *bandwidth = priv->bandwidth;
1086 return 0;
1087}
1088
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001089/* ------------------------------------------------------------------ */
1090
1091#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001092 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001093 map->std_cfg.agc_mode + map->std_cfg.std + \
1094 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001095 tda_dbg("Using custom std config for %s\n", name); \
1096 memcpy(&std->std_cfg, &map->std_cfg, \
1097 sizeof(struct tda18271_std_map_item)); \
1098 } } while (0)
1099
1100#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001101 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1102 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001103 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001104 std->std_cfg.agc_mode, std->std_cfg.std, \
1105 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001106 } while (0)
1107
1108static int tda18271_dump_std_map(struct dvb_frontend *fe)
1109{
1110 struct tda18271_priv *priv = fe->tuner_priv;
1111 struct tda18271_std_map *std = &priv->std;
1112
1113 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001114 tda18271_dump_std_item(fm_radio, " fm ");
1115 tda18271_dump_std_item(atv_b, "atv b ");
1116 tda18271_dump_std_item(atv_dk, "atv dk");
1117 tda18271_dump_std_item(atv_gh, "atv gh");
1118 tda18271_dump_std_item(atv_i, "atv i ");
1119 tda18271_dump_std_item(atv_l, "atv l ");
1120 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001121 tda18271_dump_std_item(atv_mn, "atv mn");
1122 tda18271_dump_std_item(atsc_6, "atsc 6");
1123 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1124 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1125 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001126 tda18271_dump_std_item(qam_6, "qam 6 ");
1127 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001128
1129 return 0;
1130}
1131
1132static int tda18271_update_std_map(struct dvb_frontend *fe,
1133 struct tda18271_std_map *map)
1134{
1135 struct tda18271_priv *priv = fe->tuner_priv;
1136 struct tda18271_std_map *std = &priv->std;
1137
1138 if (!map)
1139 return -EINVAL;
1140
Michael Krufkyc353f422008-01-08 10:38:10 -03001141 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001142 tda18271_update_std(atv_b, "atv b");
1143 tda18271_update_std(atv_dk, "atv dk");
1144 tda18271_update_std(atv_gh, "atv gh");
1145 tda18271_update_std(atv_i, "atv i");
1146 tda18271_update_std(atv_l, "atv l");
1147 tda18271_update_std(atv_lc, "atv l'");
1148 tda18271_update_std(atv_mn, "atv mn");
1149 tda18271_update_std(atsc_6, "atsc 6");
1150 tda18271_update_std(dvbt_6, "dvbt 6");
1151 tda18271_update_std(dvbt_7, "dvbt 7");
1152 tda18271_update_std(dvbt_8, "dvbt 8");
1153 tda18271_update_std(qam_6, "qam 6");
1154 tda18271_update_std(qam_8, "qam 8");
1155
1156 return 0;
1157}
1158
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001159static int tda18271_get_id(struct dvb_frontend *fe)
1160{
1161 struct tda18271_priv *priv = fe->tuner_priv;
1162 unsigned char *regs = priv->tda18271_regs;
1163 char *name;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001164
Michael Krufky8d316bf2008-01-06 15:31:35 -03001165 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001166 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001167 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001168
1169 switch (regs[R_ID] & 0x7f) {
1170 case 3:
1171 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001172 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001173 break;
1174 case 4:
1175 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001176 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001177 break;
1178 default:
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001179 tda_info("Unknown device (%i) detected @ %d-%04x, device not supported.\n",
1180 regs[R_ID], i2c_adapter_id(priv->i2c_props.adap),
1181 priv->i2c_props.addr);
1182 return -EINVAL;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001183 }
1184
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001185 tda_info("%s detected @ %d-%04x\n", name,
1186 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001187
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001188 return 0;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001189}
1190
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001191static int tda18271_setup_configuration(struct dvb_frontend *fe,
1192 struct tda18271_config *cfg)
Michael Krufky42f9a032009-10-23 03:20:45 -03001193{
1194 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky42f9a032009-10-23 03:20:45 -03001195
1196 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1197 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1198 priv->config = (cfg) ? cfg->config : 0;
1199 priv->small_i2c = (cfg) ?
1200 cfg->small_i2c : TDA18271_39_BYTE_CHUNK_INIT;
1201 priv->output_opt = (cfg) ?
1202 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
1203
Michael Krufky42f9a032009-10-23 03:20:45 -03001204 return 0;
1205}
1206
Michael Krufky5881ecf2009-10-24 18:37:22 -03001207static inline int tda18271_need_cal_on_startup(struct tda18271_config *cfg)
1208{
1209 /* tda18271_cal_on_startup == -1 when cal module option is unset */
1210 return ((tda18271_cal_on_startup == -1) ?
1211 /* honor configuration setting */
1212 ((cfg) && (cfg->rf_cal_on_startup)) :
1213 /* module option overrides configuration setting */
1214 (tda18271_cal_on_startup)) ? 1 : 0;
1215}
1216
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001217static int tda18271_set_config(struct dvb_frontend *fe, void *priv_cfg)
1218{
1219 struct tda18271_config *cfg = (struct tda18271_config *) priv_cfg;
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001220
1221 tda18271_setup_configuration(fe, cfg);
1222
Michael Krufky5881ecf2009-10-24 18:37:22 -03001223 if (tda18271_need_cal_on_startup(cfg))
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001224 tda18271_init(fe);
1225
Michael Krufky972aacc2009-11-01 02:52:01 -03001226 /* override default std map with values in config struct */
1227 if ((cfg) && (cfg->std_map))
1228 tda18271_update_std_map(fe, cfg->std_map);
1229
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001230 return 0;
1231}
1232
Chris Rankin0b8bd832011-08-20 16:01:26 -03001233static const struct dvb_tuner_ops tda18271_tuner_ops = {
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001234 .info = {
1235 .name = "NXP TDA18271HD",
1236 .frequency_min = 45000000,
1237 .frequency_max = 864000000,
1238 .frequency_step = 62500
1239 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001240 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001241 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001242 .set_params = tda18271_set_params,
1243 .set_analog_params = tda18271_set_analog_params,
1244 .release = tda18271_release,
Michael Krufky42f9a032009-10-23 03:20:45 -03001245 .set_config = tda18271_set_config,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001246 .get_frequency = tda18271_get_frequency,
1247 .get_bandwidth = tda18271_get_bandwidth,
1248};
1249
1250struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001251 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001252 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001253{
1254 struct tda18271_priv *priv = NULL;
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001255 int instance, ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001256
Michael Krufkya4f263b2008-01-06 15:52:56 -03001257 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001258
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001259 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1260 hybrid_tuner_instance_list,
1261 i2c, addr, "tda18271");
1262 switch (instance) {
1263 case 0:
1264 goto fail;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001265 case 1:
1266 /* new tuner instance */
Michael Krufky42f9a032009-10-23 03:20:45 -03001267 fe->tuner_priv = priv;
1268
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001269 tda18271_setup_configuration(fe, cfg);
Michael Krufky81016b42009-08-27 16:58:06 -03001270
Michael Krufkya4f263b2008-01-06 15:52:56 -03001271 priv->cal_initialized = false;
1272 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001273
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001274 ret = tda18271_get_id(fe);
1275 if (tda_fail(ret))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001276 goto fail;
1277
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001278 ret = tda18271_assign_map_layout(fe);
1279 if (tda_fail(ret))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001280 goto fail;
1281
1282 mutex_lock(&priv->lock);
1283 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001284
Michael Krufky5881ecf2009-10-24 18:37:22 -03001285 if ((tda18271_need_cal_on_startup(cfg)) &&
1286 (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001287 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001288
Michael Krufkya4f263b2008-01-06 15:52:56 -03001289 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001290 break;
1291 default:
1292 /* existing tuner instance */
1293 fe->tuner_priv = priv;
1294
Michael Krufkyd5abef62009-08-30 03:07:10 -03001295 /* allow dvb driver to override configuration settings */
1296 if (cfg) {
1297 if (cfg->gate != TDA18271_GATE_ANALOG)
1298 priv->gate = cfg->gate;
1299 if (cfg->role)
1300 priv->role = cfg->role;
1301 if (cfg->config)
1302 priv->config = cfg->config;
1303 if (cfg->small_i2c)
1304 priv->small_i2c = cfg->small_i2c;
1305 if (cfg->output_opt)
1306 priv->output_opt = cfg->output_opt;
Michael Krufky42f9a032009-10-23 03:20:45 -03001307 if (cfg->std_map)
1308 tda18271_update_std_map(fe, cfg->std_map);
Michael Krufkyd5abef62009-08-30 03:07:10 -03001309 }
Michael Krufky5881ecf2009-10-24 18:37:22 -03001310 if (tda18271_need_cal_on_startup(cfg))
Michael Krufky188ea052009-10-24 18:18:03 -03001311 tda18271_init(fe);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001312 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001313 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001314
Michael Krufky972aacc2009-11-01 02:52:01 -03001315 /* override default std map with values in config struct */
1316 if ((cfg) && (cfg->std_map))
1317 tda18271_update_std_map(fe, cfg->std_map);
1318
Michael Krufkya4f263b2008-01-06 15:52:56 -03001319 mutex_unlock(&tda18271_list_mutex);
1320
1321 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1322 sizeof(struct dvb_tuner_ops));
1323
Michael Krufkyc7353722008-03-30 19:40:20 -03001324 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001325 tda18271_dump_std_map(fe);
1326
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001327 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001328fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001329 mutex_unlock(&tda18271_list_mutex);
1330
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001331 tda18271_release(fe);
1332 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001333}
1334EXPORT_SYMBOL_GPL(tda18271_attach);
1335MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1336MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1337MODULE_LICENSE("GPL");
Michael Krufky5abaa532009-10-25 10:43:30 -03001338MODULE_VERSION("0.4");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001339
1340/*
1341 * Overrides for Emacs so that we follow Linus's tabbing style.
1342 * ---------------------------------------------------------------------------
1343 * Local variables:
1344 * c-basic-offset: 8
1345 * End:
1346 */