blob: 53299b0a857c11ba8ead9553033e5d81e0afdcdd [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 Krufky3a6b49f2009-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 Krufky3a6b49f2009-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 Krufky3a6b49f2009-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 Krufky3a6b49f2009-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 Krufky3a6b49f2009-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;
937 u32 band = BANDWIDTH_6_MHZ;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300938 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300939 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300940 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300941 int ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300942
943 priv->mode = TDA18271_DIGITAL;
944
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300945 switch (delsys) {
946 case SYS_ATSC:
947 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300948 bw = 6000000;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300949 break;
950 case SYS_DVBT:
951 case SYS_DVBT2:
952 if (bw <= 6000000) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300953 map = &std_map->dvbt_6;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300954 } else if (bw <= 7000000) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300955 map = &std_map->dvbt_7;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300956 band = BANDWIDTH_7_MHZ;
957 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300958 map = &std_map->dvbt_8;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300959 band = BANDWIDTH_8_MHZ;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300960 }
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300961 break;
962 case SYS_DVBC_ANNEX_B:
963 bw = 6000000;
964 /* falltrough */
965 case SYS_DVBC_ANNEX_A:
966 case SYS_DVBC_ANNEX_C:
967 if (bw <= 6000000) {
968 map = &std_map->qam_6;
969 } else if (bw <= 7000000) {
970 map = &std_map->qam_7;
971 band = BANDWIDTH_7_MHZ;
972 } else {
973 map = &std_map->qam_8;
974 band = BANDWIDTH_8_MHZ;
975 }
976 break;
977 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300978 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300979 return -EINVAL;
980 }
981
Michael Krufkyed736832008-01-19 17:41:04 -0300982 /* When tuning digital, the analog demod must be tri-stated */
983 if (fe->ops.analog_ops.standby)
984 fe->ops.analog_ops.standby(fe);
985
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300986 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300987
Michael Krufky4bd5d102008-05-04 21:32:21 -0300988 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300989 goto fail;
990
Michael Krufky8c8ee112011-11-03 09:59:42 -0300991 priv->if_freq = map->if_freq;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300992 priv->frequency = freq;
Mauro Carvalho Chehab5ca1c942011-12-21 08:15:29 -0300993 priv->bandwidth = band;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300994fail:
995 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300996}
997
998static int tda18271_set_analog_params(struct dvb_frontend *fe,
999 struct analog_parameters *params)
1000{
1001 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001002 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001003 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001004 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001005 int ret;
Michael Krufky4d831782009-09-27 14:05:12 -03001006 u32 freq = params->frequency * 125 *
1007 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001008
1009 priv->mode = TDA18271_ANALOG;
1010
Michael Krufkyc353f422008-01-08 10:38:10 -03001011 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001012 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -03001013 mode = "fm";
1014 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001015 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001016 mode = "MN";
1017 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001018 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001019 mode = "B";
1020 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001021 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001022 mode = "GH";
1023 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001024 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001025 mode = "I";
1026 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001027 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001028 mode = "DK";
1029 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001030 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001031 mode = "L";
1032 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001033 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -03001034 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001035 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001036 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001037 mode = "xx";
1038 }
1039
Michael Krufky182519f2007-12-25 15:10:11 -03001040 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001041
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001042 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001043
Michael Krufky4bd5d102008-05-04 21:32:21 -03001044 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001045 goto fail;
1046
Michael Krufky8c8ee112011-11-03 09:59:42 -03001047 priv->if_freq = map->if_freq;
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001048 priv->frequency = freq;
1049 priv->bandwidth = 0;
1050fail:
1051 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001052}
1053
1054static int tda18271_release(struct dvb_frontend *fe)
1055{
Michael Krufkya4f263b2008-01-06 15:52:56 -03001056 struct tda18271_priv *priv = fe->tuner_priv;
1057
1058 mutex_lock(&tda18271_list_mutex);
1059
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001060 if (priv)
1061 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -03001062
Michael Krufkya4f263b2008-01-06 15:52:56 -03001063 mutex_unlock(&tda18271_list_mutex);
1064
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001065 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001066
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001067 return 0;
1068}
1069
1070static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1071{
1072 struct tda18271_priv *priv = fe->tuner_priv;
1073 *frequency = priv->frequency;
1074 return 0;
1075}
1076
1077static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1078{
1079 struct tda18271_priv *priv = fe->tuner_priv;
1080 *bandwidth = priv->bandwidth;
1081 return 0;
1082}
1083
Michael Krufky8c8ee112011-11-03 09:59:42 -03001084static int tda18271_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
1085{
1086 struct tda18271_priv *priv = fe->tuner_priv;
1087 *frequency = (u32)priv->if_freq * 1000;
1088 return 0;
1089}
1090
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001091/* ------------------------------------------------------------------ */
1092
1093#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001094 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001095 map->std_cfg.agc_mode + map->std_cfg.std + \
1096 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001097 tda_dbg("Using custom std config for %s\n", name); \
1098 memcpy(&std->std_cfg, &map->std_cfg, \
1099 sizeof(struct tda18271_std_map_item)); \
1100 } } while (0)
1101
1102#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001103 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1104 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001105 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001106 std->std_cfg.agc_mode, std->std_cfg.std, \
1107 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001108 } while (0)
1109
1110static int tda18271_dump_std_map(struct dvb_frontend *fe)
1111{
1112 struct tda18271_priv *priv = fe->tuner_priv;
1113 struct tda18271_std_map *std = &priv->std;
1114
1115 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001116 tda18271_dump_std_item(fm_radio, " fm ");
1117 tda18271_dump_std_item(atv_b, "atv b ");
1118 tda18271_dump_std_item(atv_dk, "atv dk");
1119 tda18271_dump_std_item(atv_gh, "atv gh");
1120 tda18271_dump_std_item(atv_i, "atv i ");
1121 tda18271_dump_std_item(atv_l, "atv l ");
1122 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001123 tda18271_dump_std_item(atv_mn, "atv mn");
1124 tda18271_dump_std_item(atsc_6, "atsc 6");
1125 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1126 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1127 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001128 tda18271_dump_std_item(qam_6, "qam 6 ");
1129 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001130
1131 return 0;
1132}
1133
1134static int tda18271_update_std_map(struct dvb_frontend *fe,
1135 struct tda18271_std_map *map)
1136{
1137 struct tda18271_priv *priv = fe->tuner_priv;
1138 struct tda18271_std_map *std = &priv->std;
1139
1140 if (!map)
1141 return -EINVAL;
1142
Michael Krufkyc353f422008-01-08 10:38:10 -03001143 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001144 tda18271_update_std(atv_b, "atv b");
1145 tda18271_update_std(atv_dk, "atv dk");
1146 tda18271_update_std(atv_gh, "atv gh");
1147 tda18271_update_std(atv_i, "atv i");
1148 tda18271_update_std(atv_l, "atv l");
1149 tda18271_update_std(atv_lc, "atv l'");
1150 tda18271_update_std(atv_mn, "atv mn");
1151 tda18271_update_std(atsc_6, "atsc 6");
1152 tda18271_update_std(dvbt_6, "dvbt 6");
1153 tda18271_update_std(dvbt_7, "dvbt 7");
1154 tda18271_update_std(dvbt_8, "dvbt 8");
1155 tda18271_update_std(qam_6, "qam 6");
1156 tda18271_update_std(qam_8, "qam 8");
1157
1158 return 0;
1159}
1160
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001161static int tda18271_get_id(struct dvb_frontend *fe)
1162{
1163 struct tda18271_priv *priv = fe->tuner_priv;
1164 unsigned char *regs = priv->tda18271_regs;
1165 char *name;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001166
Michael Krufky8d316bf2008-01-06 15:31:35 -03001167 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001168 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001169 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001170
1171 switch (regs[R_ID] & 0x7f) {
1172 case 3:
1173 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001174 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001175 break;
1176 case 4:
1177 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001178 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001179 break;
1180 default:
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001181 tda_info("Unknown device (%i) detected @ %d-%04x, device not supported.\n",
1182 regs[R_ID], i2c_adapter_id(priv->i2c_props.adap),
1183 priv->i2c_props.addr);
1184 return -EINVAL;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001185 }
1186
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001187 tda_info("%s detected @ %d-%04x\n", name,
1188 i2c_adapter_id(priv->i2c_props.adap), priv->i2c_props.addr);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001189
Mauro Carvalho Chehabbfc3d572010-09-26 00:39:26 -03001190 return 0;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001191}
1192
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001193static int tda18271_setup_configuration(struct dvb_frontend *fe,
1194 struct tda18271_config *cfg)
Michael Krufky42f9a032009-10-23 03:20:45 -03001195{
1196 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky42f9a032009-10-23 03:20:45 -03001197
1198 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
1199 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
1200 priv->config = (cfg) ? cfg->config : 0;
1201 priv->small_i2c = (cfg) ?
1202 cfg->small_i2c : TDA18271_39_BYTE_CHUNK_INIT;
1203 priv->output_opt = (cfg) ?
1204 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
1205
Michael Krufky42f9a032009-10-23 03:20:45 -03001206 return 0;
1207}
1208
Michael Krufky5881ecf2009-10-24 18:37:22 -03001209static inline int tda18271_need_cal_on_startup(struct tda18271_config *cfg)
1210{
1211 /* tda18271_cal_on_startup == -1 when cal module option is unset */
1212 return ((tda18271_cal_on_startup == -1) ?
1213 /* honor configuration setting */
1214 ((cfg) && (cfg->rf_cal_on_startup)) :
1215 /* module option overrides configuration setting */
1216 (tda18271_cal_on_startup)) ? 1 : 0;
1217}
1218
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001219static int tda18271_set_config(struct dvb_frontend *fe, void *priv_cfg)
1220{
1221 struct tda18271_config *cfg = (struct tda18271_config *) priv_cfg;
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001222
1223 tda18271_setup_configuration(fe, cfg);
1224
Michael Krufky5881ecf2009-10-24 18:37:22 -03001225 if (tda18271_need_cal_on_startup(cfg))
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001226 tda18271_init(fe);
1227
Michael Krufky972aacc2009-11-01 02:52:01 -03001228 /* override default std map with values in config struct */
1229 if ((cfg) && (cfg->std_map))
1230 tda18271_update_std_map(fe, cfg->std_map);
1231
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001232 return 0;
1233}
1234
Chris Rankin0b8bd832011-08-20 16:01:26 -03001235static const struct dvb_tuner_ops tda18271_tuner_ops = {
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001236 .info = {
1237 .name = "NXP TDA18271HD",
1238 .frequency_min = 45000000,
1239 .frequency_max = 864000000,
1240 .frequency_step = 62500
1241 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001242 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001243 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001244 .set_params = tda18271_set_params,
1245 .set_analog_params = tda18271_set_analog_params,
1246 .release = tda18271_release,
Michael Krufky42f9a032009-10-23 03:20:45 -03001247 .set_config = tda18271_set_config,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001248 .get_frequency = tda18271_get_frequency,
1249 .get_bandwidth = tda18271_get_bandwidth,
Michael Krufky8c8ee112011-11-03 09:59:42 -03001250 .get_if_frequency = tda18271_get_if_frequency,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001251};
1252
1253struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001254 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001255 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001256{
1257 struct tda18271_priv *priv = NULL;
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001258 int instance, ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001259
Michael Krufkya4f263b2008-01-06 15:52:56 -03001260 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001261
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001262 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1263 hybrid_tuner_instance_list,
1264 i2c, addr, "tda18271");
1265 switch (instance) {
1266 case 0:
1267 goto fail;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001268 case 1:
1269 /* new tuner instance */
Michael Krufky42f9a032009-10-23 03:20:45 -03001270 fe->tuner_priv = priv;
1271
Michael Krufkyc54e1dd2009-10-24 17:47:49 -03001272 tda18271_setup_configuration(fe, cfg);
Michael Krufky81016b42009-08-27 16:58:06 -03001273
Michael Krufkya4f263b2008-01-06 15:52:56 -03001274 priv->cal_initialized = false;
1275 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001276
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001277 ret = tda18271_get_id(fe);
1278 if (tda_fail(ret))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001279 goto fail;
1280
Michael Krufkyfab9bfb2010-05-03 02:10:15 -03001281 ret = tda18271_assign_map_layout(fe);
1282 if (tda_fail(ret))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001283 goto fail;
1284
1285 mutex_lock(&priv->lock);
1286 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001287
Michael Krufky5881ecf2009-10-24 18:37:22 -03001288 if ((tda18271_need_cal_on_startup(cfg)) &&
1289 (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001290 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001291
Michael Krufkya4f263b2008-01-06 15:52:56 -03001292 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001293 break;
1294 default:
1295 /* existing tuner instance */
1296 fe->tuner_priv = priv;
1297
Michael Krufkyd5abef62009-08-30 03:07:10 -03001298 /* allow dvb driver to override configuration settings */
1299 if (cfg) {
1300 if (cfg->gate != TDA18271_GATE_ANALOG)
1301 priv->gate = cfg->gate;
1302 if (cfg->role)
1303 priv->role = cfg->role;
1304 if (cfg->config)
1305 priv->config = cfg->config;
1306 if (cfg->small_i2c)
1307 priv->small_i2c = cfg->small_i2c;
1308 if (cfg->output_opt)
1309 priv->output_opt = cfg->output_opt;
Michael Krufky42f9a032009-10-23 03:20:45 -03001310 if (cfg->std_map)
1311 tda18271_update_std_map(fe, cfg->std_map);
Michael Krufkyd5abef62009-08-30 03:07:10 -03001312 }
Michael Krufky5881ecf2009-10-24 18:37:22 -03001313 if (tda18271_need_cal_on_startup(cfg))
Michael Krufky188ea052009-10-24 18:18:03 -03001314 tda18271_init(fe);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001315 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001316 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001317
Michael Krufky972aacc2009-11-01 02:52:01 -03001318 /* override default std map with values in config struct */
1319 if ((cfg) && (cfg->std_map))
1320 tda18271_update_std_map(fe, cfg->std_map);
1321
Michael Krufkya4f263b2008-01-06 15:52:56 -03001322 mutex_unlock(&tda18271_list_mutex);
1323
1324 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1325 sizeof(struct dvb_tuner_ops));
1326
Michael Krufkyc7353722008-03-30 19:40:20 -03001327 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001328 tda18271_dump_std_map(fe);
1329
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001330 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001331fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001332 mutex_unlock(&tda18271_list_mutex);
1333
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001334 tda18271_release(fe);
1335 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001336}
1337EXPORT_SYMBOL_GPL(tda18271_attach);
1338MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1339MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1340MODULE_LICENSE("GPL");
Michael Krufky5abaa532009-10-25 10:43:30 -03001341MODULE_VERSION("0.4");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001342
1343/*
1344 * Overrides for Emacs so that we follow Linus's tabbing style.
1345 * ---------------------------------------------------------------------------
1346 * Local variables:
1347 * c-basic-offset: 8
1348 * End:
1349 */