blob: 3a50ce96fcb9a6b0224254c2db369a3a7e53d28b [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 Krufky20f42062008-05-04 19:57:06 -0300259 int tm_current, rfcal_comp, approx, i, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300260 u8 dc_over_dt, rf_tab;
261
262 /* power up */
Michael Krufky20f42062008-05-04 19:57:06 -0300263 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300264 if (tda_fail(ret))
Michael Krufky20f42062008-05-04 19:57:06 -0300265 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300266
267 /* read die current temperature */
268 tm_current = tda18271_read_thermometer(fe);
269
270 /* frequency dependent parameters */
271
272 tda18271_calc_rf_cal(fe, &freq);
273 rf_tab = regs[R_EB14];
274
275 i = tda18271_lookup_rf_band(fe, &freq, NULL);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300276 if (tda_fail(i))
277 return i;
Michael Krufky255b5112008-01-01 22:52:09 -0300278
279 if ((0 == map[i].rf3) || (freq / 1000 < map[i].rf2)) {
280 approx = map[i].rf_a1 *
281 (freq / 1000 - map[i].rf1) + map[i].rf_b1 + rf_tab;
282 } else {
283 approx = map[i].rf_a2 *
284 (freq / 1000 - map[i].rf2) + map[i].rf_b2 + rf_tab;
285 }
286
287 if (approx < 0)
288 approx = 0;
289 if (approx > 255)
290 approx = 255;
291
292 tda18271_lookup_map(fe, RF_CAL_DC_OVER_DT, &freq, &dc_over_dt);
293
294 /* calculate temperature compensation */
Henk Vergonetb5d18972009-09-15 02:09:17 -0300295 rfcal_comp = dc_over_dt * (tm_current - priv->tm_rfcal) / 1000;
Michael Krufky255b5112008-01-01 22:52:09 -0300296
297 regs[R_EB14] = approx + rfcal_comp;
Michael Krufky20f42062008-05-04 19:57:06 -0300298 ret = tda18271_write_regs(fe, R_EB14, 1);
299fail:
300 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300301}
302
303static int tda18271_por(struct dvb_frontend *fe)
304{
305 struct tda18271_priv *priv = fe->tuner_priv;
306 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300307 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300308
309 /* power up detector 1 */
310 regs[R_EB12] &= ~0x20;
Michael Krufky24124f72008-05-03 19:28:00 -0300311 ret = tda18271_write_regs(fe, R_EB12, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300312 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300313 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300314
315 regs[R_EB18] &= ~0x80; /* turn agc1 loop on */
316 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300317 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300318 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300319 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300320
321 regs[R_EB21] |= 0x03; /* set agc2_gain to -6 dB */
322
323 /* POR mode */
Michael Krufky24124f72008-05-03 19:28:00 -0300324 ret = tda18271_set_standby_mode(fe, 1, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300325 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300326 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300327
328 /* disable 1.5 MHz low pass filter */
329 regs[R_EB23] &= ~0x04; /* forcelp_fc2_en = 0 */
330 regs[R_EB23] &= ~0x02; /* XXX: lp_fc[2] = 0 */
Michael Krufky24124f72008-05-03 19:28:00 -0300331 ret = tda18271_write_regs(fe, R_EB21, 3);
332fail:
333 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300334}
335
336static int tda18271_calibrate_rf(struct dvb_frontend *fe, u32 freq)
337{
338 struct tda18271_priv *priv = fe->tuner_priv;
339 unsigned char *regs = priv->tda18271_regs;
340 u32 N;
341
342 /* set CAL mode to normal */
343 regs[R_EP4] &= ~0x03;
344 tda18271_write_regs(fe, R_EP4, 1);
345
346 /* switch off agc1 */
347 regs[R_EP3] |= 0x40; /* sm_lt = 1 */
348
349 regs[R_EB18] |= 0x03; /* set agc1_gain to 15 dB */
350 tda18271_write_regs(fe, R_EB18, 1);
351
352 /* frequency dependent parameters */
353
354 tda18271_calc_bp_filter(fe, &freq);
355 tda18271_calc_gain_taper(fe, &freq);
356 tda18271_calc_rf_band(fe, &freq);
357 tda18271_calc_km(fe, &freq);
358
359 tda18271_write_regs(fe, R_EP1, 3);
360 tda18271_write_regs(fe, R_EB13, 1);
361
362 /* main pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300363 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300364
365 /* cal pll charge pump source */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300366 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 1);
Michael Krufky255b5112008-01-01 22:52:09 -0300367
368 /* force dcdc converter to 0 V */
369 regs[R_EB14] = 0x00;
370 tda18271_write_regs(fe, R_EB14, 1);
371
372 /* disable plls lock */
373 regs[R_EB20] &= ~0x20;
374 tda18271_write_regs(fe, R_EB20, 1);
375
376 /* set CAL mode to RF tracking filter calibration */
377 regs[R_EP4] |= 0x03;
378 tda18271_write_regs(fe, R_EP4, 2);
379
380 /* --------------------------------------------------------------- */
381
382 /* set the internal calibration signal */
383 N = freq;
384
Michael Krufkyae07d042008-04-22 14:46:21 -0300385 tda18271_calc_cal_pll(fe, N);
386 tda18271_write_regs(fe, R_CPD, 4);
Michael Krufky255b5112008-01-01 22:52:09 -0300387
388 /* downconvert internal calibration */
389 N += 1000000;
390
391 tda18271_calc_main_pll(fe, N);
392 tda18271_write_regs(fe, R_MPD, 4);
393
394 msleep(5);
395
396 tda18271_write_regs(fe, R_EP2, 1);
397 tda18271_write_regs(fe, R_EP1, 1);
398 tda18271_write_regs(fe, R_EP2, 1);
399 tda18271_write_regs(fe, R_EP1, 1);
400
401 /* --------------------------------------------------------------- */
402
403 /* normal operation for the main pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300404 tda18271_charge_pump_source(fe, TDA18271_MAIN_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300405
406 /* normal operation for the cal pll */
Michael Krufky4efb0ca2008-04-22 14:46:23 -0300407 tda18271_charge_pump_source(fe, TDA18271_CAL_PLL, 0);
Michael Krufky255b5112008-01-01 22:52:09 -0300408
Michael Krufkyae07d042008-04-22 14:46:21 -0300409 msleep(10); /* plls locking */
Michael Krufky255b5112008-01-01 22:52:09 -0300410
411 /* launch the rf tracking filters calibration */
412 regs[R_EB20] |= 0x20;
413 tda18271_write_regs(fe, R_EB20, 1);
414
415 msleep(60); /* calibration */
416
417 /* --------------------------------------------------------------- */
418
419 /* set CAL mode to normal */
420 regs[R_EP4] &= ~0x03;
421
422 /* switch on agc1 */
423 regs[R_EP3] &= ~0x40; /* sm_lt = 0 */
424
425 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
426 tda18271_write_regs(fe, R_EB18, 1);
427
428 tda18271_write_regs(fe, R_EP3, 2);
429
430 /* synchronization */
431 tda18271_write_regs(fe, R_EP1, 1);
432
433 /* get calibration result */
434 tda18271_read_extended(fe);
435
436 return regs[R_EB14];
437}
438
439static int tda18271_powerscan(struct dvb_frontend *fe,
440 u32 *freq_in, u32 *freq_out)
441{
442 struct tda18271_priv *priv = fe->tuner_priv;
443 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300444 int sgn, bcal, count, wait, ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300445 u8 cid_target;
446 u16 count_limit;
447 u32 freq;
448
449 freq = *freq_in;
450
451 tda18271_calc_rf_band(fe, &freq);
452 tda18271_calc_rf_cal(fe, &freq);
453 tda18271_calc_gain_taper(fe, &freq);
454 tda18271_lookup_cid_target(fe, &freq, &cid_target, &count_limit);
455
456 tda18271_write_regs(fe, R_EP2, 1);
457 tda18271_write_regs(fe, R_EB14, 1);
458
459 /* downconvert frequency */
460 freq += 1000000;
461
462 tda18271_calc_main_pll(fe, freq);
463 tda18271_write_regs(fe, R_MPD, 4);
464
465 msleep(5); /* pll locking */
466
467 /* detection mode */
468 regs[R_EP4] &= ~0x03;
469 regs[R_EP4] |= 0x01;
470 tda18271_write_regs(fe, R_EP4, 1);
471
472 /* launch power detection measurement */
473 tda18271_write_regs(fe, R_EP2, 1);
474
475 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300476 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300477 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300478 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300479
480 /* algorithm initialization */
481 sgn = 1;
482 *freq_out = *freq_in;
483 bcal = 0;
484 count = 0;
485 wait = false;
486
487 while ((regs[R_EB10] & 0x3f) < cid_target) {
488 /* downconvert updated freq to 1 MHz */
489 freq = *freq_in + (sgn * count) + 1000000;
490
491 tda18271_calc_main_pll(fe, freq);
492 tda18271_write_regs(fe, R_MPD, 4);
493
494 if (wait) {
495 msleep(5); /* pll locking */
496 wait = false;
497 } else
498 udelay(100); /* pll locking */
499
500 /* launch power detection measurement */
501 tda18271_write_regs(fe, R_EP2, 1);
502
503 /* read power detection info, stored in EB10 */
Michael Krufky24124f72008-05-03 19:28:00 -0300504 ret = tda18271_read_extended(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300505 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300506 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300507
508 count += 200;
509
Michael Krufkye7809a02008-04-22 14:46:22 -0300510 if (count <= count_limit)
Michael Krufky255b5112008-01-01 22:52:09 -0300511 continue;
512
513 if (sgn <= 0)
514 break;
515
516 sgn = -1 * sgn;
517 count = 200;
518 wait = true;
519 }
520
521 if ((regs[R_EB10] & 0x3f) >= cid_target) {
522 bcal = 1;
523 *freq_out = freq - 1000000;
524 } else
525 bcal = 0;
526
Michael Krufkycf04d292008-01-09 00:34:30 -0300527 tda_cal("bcal = %d, freq_in = %d, freq_out = %d (freq = %d)\n",
Michael Krufky255b5112008-01-01 22:52:09 -0300528 bcal, *freq_in, *freq_out, freq);
529
530 return bcal;
531}
532
533static int tda18271_powerscan_init(struct dvb_frontend *fe)
534{
535 struct tda18271_priv *priv = fe->tuner_priv;
536 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300537 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300538
539 /* set standard to digital */
540 regs[R_EP3] &= ~0x1f; /* clear std bits */
541 regs[R_EP3] |= 0x12;
542
543 /* set cal mode to normal */
544 regs[R_EP4] &= ~0x03;
545
Michael Krufky44e645c2008-06-08 20:10:29 -0300546 /* update IF output level */
Michael Krufky255b5112008-01-01 22:52:09 -0300547 regs[R_EP4] &= ~0x1c; /* clear if level bits */
548
Michael Krufky24124f72008-05-03 19:28:00 -0300549 ret = tda18271_write_regs(fe, R_EP3, 2);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300550 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300551 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300552
553 regs[R_EB18] &= ~0x03; /* set agc1_gain to 6 dB */
Michael Krufky24124f72008-05-03 19:28:00 -0300554 ret = tda18271_write_regs(fe, R_EB18, 1);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300555 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300556 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300557
558 regs[R_EB21] &= ~0x03; /* set agc2_gain to -15 dB */
559
560 /* 1.5 MHz low pass filter */
561 regs[R_EB23] |= 0x04; /* forcelp_fc2_en = 1 */
562 regs[R_EB23] |= 0x02; /* lp_fc[2] = 1 */
563
Michael Krufky24124f72008-05-03 19:28:00 -0300564 ret = tda18271_write_regs(fe, R_EB21, 3);
565fail:
566 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300567}
568
569static int tda18271_rf_tracking_filters_init(struct dvb_frontend *fe, u32 freq)
570{
571 struct tda18271_priv *priv = fe->tuner_priv;
572 struct tda18271_rf_tracking_filter_cal *map = priv->rf_cal_state;
573 unsigned char *regs = priv->tda18271_regs;
574 int bcal, rf, i;
575#define RF1 0
576#define RF2 1
577#define RF3 2
578 u32 rf_default[3];
579 u32 rf_freq[3];
580 u8 prog_cal[3];
581 u8 prog_tab[3];
582
583 i = tda18271_lookup_rf_band(fe, &freq, NULL);
584
Michael Krufky4bd5d102008-05-04 21:32:21 -0300585 if (tda_fail(i))
Michael Krufky255b5112008-01-01 22:52:09 -0300586 return i;
587
588 rf_default[RF1] = 1000 * map[i].rf1_def;
589 rf_default[RF2] = 1000 * map[i].rf2_def;
590 rf_default[RF3] = 1000 * map[i].rf3_def;
591
592 for (rf = RF1; rf <= RF3; rf++) {
593 if (0 == rf_default[rf])
594 return 0;
Michael Krufkycf04d292008-01-09 00:34:30 -0300595 tda_cal("freq = %d, rf = %d\n", freq, rf);
Michael Krufky255b5112008-01-01 22:52:09 -0300596
597 /* look for optimized calibration frequency */
598 bcal = tda18271_powerscan(fe, &rf_default[rf], &rf_freq[rf]);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300599 if (tda_fail(bcal))
Michael Krufky24124f72008-05-03 19:28:00 -0300600 return bcal;
Michael Krufky255b5112008-01-01 22:52:09 -0300601
602 tda18271_calc_rf_cal(fe, &rf_freq[rf]);
603 prog_tab[rf] = regs[R_EB14];
604
605 if (1 == bcal)
606 prog_cal[rf] = tda18271_calibrate_rf(fe, rf_freq[rf]);
607 else
608 prog_cal[rf] = prog_tab[rf];
609
610 switch (rf) {
611 case RF1:
612 map[i].rf_a1 = 0;
613 map[i].rf_b1 = prog_cal[RF1] - prog_tab[RF1];
614 map[i].rf1 = rf_freq[RF1] / 1000;
615 break;
616 case RF2:
617 map[i].rf_a1 = (prog_cal[RF2] - prog_tab[RF2] -
618 prog_cal[RF1] + prog_tab[RF1]) /
Seth Barrya57c1dc2009-09-27 16:42:29 -0300619 (s32)((rf_freq[RF2] - rf_freq[RF1]) / 1000);
Michael Krufky255b5112008-01-01 22:52:09 -0300620 map[i].rf2 = rf_freq[RF2] / 1000;
621 break;
622 case RF3:
623 map[i].rf_a2 = (prog_cal[RF3] - prog_tab[RF3] -
624 prog_cal[RF2] + prog_tab[RF2]) /
Seth Barrya57c1dc2009-09-27 16:42:29 -0300625 (s32)((rf_freq[RF3] - rf_freq[RF2]) / 1000);
Michael Krufky255b5112008-01-01 22:52:09 -0300626 map[i].rf_b2 = prog_cal[RF2] - prog_tab[RF2];
627 map[i].rf3 = rf_freq[RF3] / 1000;
628 break;
629 default:
630 BUG();
631 }
632 }
633
634 return 0;
635}
636
Michael Krufky09f83c42008-01-05 20:00:09 -0300637static int tda18271_calc_rf_filter_curve(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300638{
639 struct tda18271_priv *priv = fe->tuner_priv;
640 unsigned int i;
Michael Krufky24124f72008-05-03 19:28:00 -0300641 int ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300642
643 tda_info("tda18271: performing RF tracking filter calibration\n");
644
645 /* wait for die temperature stabilization */
646 msleep(200);
647
Michael Krufky24124f72008-05-03 19:28:00 -0300648 ret = tda18271_powerscan_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300649 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300650 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300651
652 /* rf band calibration */
Michael Krufkyc151c322008-05-04 17:54:23 -0300653 for (i = 0; priv->rf_cal_state[i].rfmax != 0; i++) {
654 ret =
Michael Krufky255b5112008-01-01 22:52:09 -0300655 tda18271_rf_tracking_filters_init(fe, 1000 *
656 priv->rf_cal_state[i].rfmax);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300657 if (tda_fail(ret))
Michael Krufkyc151c322008-05-04 17:54:23 -0300658 goto fail;
659 }
Michael Krufky255b5112008-01-01 22:52:09 -0300660
Michael Krufky09f83c42008-01-05 20:00:09 -0300661 priv->tm_rfcal = tda18271_read_thermometer(fe);
Michael Krufky24124f72008-05-03 19:28:00 -0300662fail:
663 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300664}
665
666/* ------------------------------------------------------------------ */
667
Michael Krufky12afe372008-04-22 14:42:07 -0300668static int tda18271c2_rf_cal_init(struct dvb_frontend *fe)
Michael Krufky255b5112008-01-01 22:52:09 -0300669{
670 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufky839c6c92008-01-13 18:29:44 -0300671 unsigned char *regs = priv->tda18271_regs;
Michael Krufky24124f72008-05-03 19:28:00 -0300672 int ret;
Michael Krufky839c6c92008-01-13 18:29:44 -0300673
674 /* test RF_CAL_OK to see if we need init */
675 if ((regs[R_EP1] & 0x10) == 0)
676 priv->cal_initialized = false;
Michael Krufky255b5112008-01-01 22:52:09 -0300677
678 if (priv->cal_initialized)
679 return 0;
680
Michael Krufky24124f72008-05-03 19:28:00 -0300681 ret = tda18271_calc_rf_filter_curve(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300682 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300683 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300684
Michael Krufky24124f72008-05-03 19:28:00 -0300685 ret = tda18271_por(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300686 if (tda_fail(ret))
Michael Krufky24124f72008-05-03 19:28:00 -0300687 goto fail;
Michael Krufky255b5112008-01-01 22:52:09 -0300688
Michael Krufky6bfa6652008-01-07 00:51:48 -0300689 tda_info("tda18271: RF tracking filter calibration complete\n");
690
Michael Krufky255b5112008-01-01 22:52:09 -0300691 priv->cal_initialized = true;
Michael Krufkyc151c322008-05-04 17:54:23 -0300692 goto end;
Michael Krufky24124f72008-05-03 19:28:00 -0300693fail:
Michael Krufkyc151c322008-05-04 17:54:23 -0300694 tda_info("tda18271: RF tracking filter calibration failed!\n");
695end:
Michael Krufky24124f72008-05-03 19:28:00 -0300696 return ret;
Michael Krufky255b5112008-01-01 22:52:09 -0300697}
698
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300699static int tda18271c1_rf_tracking_filter_calibration(struct dvb_frontend *fe,
700 u32 freq, u32 bw)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300701{
702 struct tda18271_priv *priv = fe->tuner_priv;
703 unsigned char *regs = priv->tda18271_regs;
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300704 int ret;
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300705 u32 N = 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300706
Michael Krufky255b5112008-01-01 22:52:09 -0300707 /* calculate bp filter */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300708 tda18271_calc_bp_filter(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300709 tda18271_write_regs(fe, R_EP1, 1);
710
711 regs[R_EB4] &= 0x07;
712 regs[R_EB4] |= 0x60;
713 tda18271_write_regs(fe, R_EB4, 1);
714
715 regs[R_EB7] = 0x60;
716 tda18271_write_regs(fe, R_EB7, 1);
717
718 regs[R_EB14] = 0x00;
719 tda18271_write_regs(fe, R_EB14, 1);
720
721 regs[R_EB20] = 0xcc;
722 tda18271_write_regs(fe, R_EB20, 1);
723
Michael Krufky255b5112008-01-01 22:52:09 -0300724 /* set cal mode to RF tracking filter calibration */
Michael Krufky26501a72007-12-21 14:28:46 -0300725 regs[R_EP4] |= 0x03;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300726
Michael Krufky255b5112008-01-01 22:52:09 -0300727 /* calculate cal pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300728
729 switch (priv->mode) {
730 case TDA18271_ANALOG:
731 N = freq - 1250000;
732 break;
733 case TDA18271_DIGITAL:
734 N = freq + bw / 2;
735 break;
736 }
737
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300738 tda18271_calc_cal_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300739
Michael Krufky255b5112008-01-01 22:52:09 -0300740 /* calculate main pll */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300741
742 switch (priv->mode) {
743 case TDA18271_ANALOG:
744 N = freq - 250000;
745 break;
746 case TDA18271_DIGITAL:
747 N = freq + bw / 2 + 1000000;
748 break;
749 }
750
Michael Krufkyfe0bf6d2007-12-24 05:05:05 -0300751 tda18271_calc_main_pll(fe, N);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300752
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300753 ret = tda18271_write_regs(fe, R_EP3, 11);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300754 if (tda_fail(ret))
Michael Krufky10ed0bf2008-05-04 20:26:47 -0300755 return ret;
756
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300757 msleep(5); /* RF tracking filter calibration initialization */
758
Michael Krufky255b5112008-01-01 22:52:09 -0300759 /* search for K,M,CO for RF calibration */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300760 tda18271_calc_km(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300761 tda18271_write_regs(fe, R_EB13, 1);
762
Michael Krufky255b5112008-01-01 22:52:09 -0300763 /* search for rf band */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300764 tda18271_calc_rf_band(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300765
Michael Krufky255b5112008-01-01 22:52:09 -0300766 /* search for gain taper */
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300767 tda18271_calc_gain_taper(fe, &freq);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300768
769 tda18271_write_regs(fe, R_EP2, 1);
770 tda18271_write_regs(fe, R_EP1, 1);
771 tda18271_write_regs(fe, R_EP2, 1);
772 tda18271_write_regs(fe, R_EP1, 1);
773
774 regs[R_EB4] &= 0x07;
775 regs[R_EB4] |= 0x40;
776 tda18271_write_regs(fe, R_EB4, 1);
777
778 regs[R_EB7] = 0x40;
779 tda18271_write_regs(fe, R_EB7, 1);
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300780 msleep(10); /* pll locking */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300781
782 regs[R_EB20] = 0xec;
783 tda18271_write_regs(fe, R_EB20, 1);
784 msleep(60); /* RF tracking filter calibration completion */
785
786 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
787 tda18271_write_regs(fe, R_EP4, 1);
788
789 tda18271_write_regs(fe, R_EP1, 1);
790
Michael Krufkyb92bf0f2007-12-25 18:54:22 -0300791 /* RF tracking filter correction for VHF_Low band */
792 if (0 == tda18271_calc_rf_cal(fe, &freq))
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300793 tda18271_write_regs(fe, R_EB14, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300794
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300795 return 0;
796}
797
Michael Krufkyd1c53422008-04-22 14:42:07 -0300798/* ------------------------------------------------------------------ */
799
Michael Krufky12afe372008-04-22 14:42:07 -0300800static int tda18271_ir_cal_init(struct dvb_frontend *fe)
801{
802 struct tda18271_priv *priv = fe->tuner_priv;
803 unsigned char *regs = priv->tda18271_regs;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300804 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300805
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300806 ret = tda18271_read_regs(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300807 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300808 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300809
810 /* test IR_CAL_OK to see if we need init */
811 if ((regs[R_EP1] & 0x08) == 0)
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300812 ret = tda18271_init_regs(fe);
813fail:
814 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300815}
816
817static int tda18271_init(struct dvb_frontend *fe)
818{
819 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300820 int ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300821
822 mutex_lock(&priv->lock);
823
Michael Krufky4240b462009-08-29 16:25:37 -0300824 /* full power up */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300825 ret = tda18271_set_standby_mode(fe, 0, 0, 0);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300826 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300827 goto fail;
Michael Krufky12afe372008-04-22 14:42:07 -0300828
829 /* initialization */
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300830 ret = tda18271_ir_cal_init(fe);
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 if (priv->id == TDA18271HDC2)
835 tda18271c2_rf_cal_init(fe);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300836fail:
Michael Krufky12afe372008-04-22 14:42:07 -0300837 mutex_unlock(&priv->lock);
838
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300839 return ret;
Michael Krufky12afe372008-04-22 14:42:07 -0300840}
841
Michael Krufkycc7e26d2009-08-29 16:27:21 -0300842static int tda18271_sleep(struct dvb_frontend *fe)
843{
844 struct tda18271_priv *priv = fe->tuner_priv;
845 int ret;
846
847 mutex_lock(&priv->lock);
848
849 /* enter standby mode, with required output features enabled */
850 ret = tda18271_toggle_output(fe, 1);
851
852 mutex_unlock(&priv->lock);
853
854 return ret;
855}
856
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300857/* ------------------------------------------------------------------ */
858
859static int tda18271_agc(struct dvb_frontend *fe)
860{
861 struct tda18271_priv *priv = fe->tuner_priv;
862 int ret = 0;
863
864 switch (priv->config) {
865 case 0:
Michael Krufkyecda4272009-09-06 14:38:48 -0300866 /* no external agc configuration required */
867 if (tda18271_debug & DBG_ADV)
868 tda_dbg("no agc configuration provided\n");
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300869 break;
870 case 3:
871 /* switch with GPIO of saa713x */
872 tda_dbg("invoking callback\n");
873 if (fe->callback)
874 ret = fe->callback(priv->i2c_props.adap->algo_data,
875 DVB_FRONTEND_COMPONENT_TUNER,
876 TDA18271_CALLBACK_CMD_AGC_ENABLE,
877 priv->mode);
878 break;
879 case 1:
880 case 2:
881 default:
882 /* n/a - currently not supported */
883 tda_err("unsupported configuration: %d\n", priv->config);
884 ret = -EINVAL;
885 break;
886 }
887 return ret;
888}
889
Michael Krufkyd1c53422008-04-22 14:42:07 -0300890static int tda18271_tune(struct dvb_frontend *fe,
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300891 struct tda18271_std_map_item *map, u32 freq, u32 bw)
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300892{
893 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300894 int ret;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300895
Michael Krufky7f7203d2008-04-22 14:46:06 -0300896 tda_dbg("freq = %d, ifc = %d, bw = %d, agc_mode = %d, std = %d\n",
897 freq, map->if_freq, bw, map->agc_mode, map->std);
Michael Krufkyd1c53422008-04-22 14:42:07 -0300898
Michael Krufkyadcc4b32009-03-04 19:42:06 -0300899 ret = tda18271_agc(fe);
900 if (tda_fail(ret))
901 tda_warn("failed to configure agc\n");
902
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300903 ret = tda18271_init(fe);
Michael Krufky4bd5d102008-05-04 21:32:21 -0300904 if (tda_fail(ret))
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300905 goto fail;
Michael Krufky4d2d42b2008-04-22 14:42:07 -0300906
907 mutex_lock(&priv->lock);
908
Michael Krufkyd1c53422008-04-22 14:42:07 -0300909 switch (priv->id) {
910 case TDA18271HDC1:
911 tda18271c1_rf_tracking_filter_calibration(fe, freq, bw);
912 break;
913 case TDA18271HDC2:
914 tda18271c2_rf_tracking_filters_correction(fe, freq);
915 break;
916 }
Michael Krufky31940e32008-05-04 19:37:27 -0300917 ret = tda18271_channel_configuration(fe, map, freq, bw);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300918
Michael Krufky8d316bf2008-01-06 15:31:35 -0300919 mutex_unlock(&priv->lock);
Michael Krufkyd35fcca2008-05-03 18:20:21 -0300920fail:
921 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300922}
923
924/* ------------------------------------------------------------------ */
925
926static int tda18271_set_params(struct dvb_frontend *fe,
927 struct dvb_frontend_parameters *params)
928{
929 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300930 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300931 struct tda18271_std_map_item *map;
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300932 int ret;
Michael Krufky2ba65d52008-01-03 01:17:45 -0300933 u32 bw, freq = params->frequency;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300934
935 priv->mode = TDA18271_DIGITAL;
936
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300937 if (fe->ops.info.type == FE_ATSC) {
938 switch (params->u.vsb.modulation) {
939 case VSB_8:
940 case VSB_16:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300941 map = &std_map->atsc_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300942 break;
943 case QAM_64:
944 case QAM_256:
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300945 map = &std_map->qam_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300946 break;
947 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300948 tda_warn("modulation not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300949 return -EINVAL;
950 }
Michael Krufky14e3c152007-12-07 00:33:08 -0300951#if 0
952 /* userspace request is already center adjusted */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300953 freq += 1750000; /* Adjust to center (+1.75MHZ) */
Michael Krufky14e3c152007-12-07 00:33:08 -0300954#endif
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300955 bw = 6000000;
956 } else if (fe->ops.info.type == FE_OFDM) {
957 switch (params->u.ofdm.bandwidth) {
958 case BANDWIDTH_6_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300959 bw = 6000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300960 map = &std_map->dvbt_6;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300961 break;
962 case BANDWIDTH_7_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300963 bw = 7000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300964 map = &std_map->dvbt_7;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300965 break;
966 case BANDWIDTH_8_MHZ:
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300967 bw = 8000000;
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300968 map = &std_map->dvbt_8;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300969 break;
970 default:
Michael Krufky182519f2007-12-25 15:10:11 -0300971 tda_warn("bandwidth not set!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300972 return -EINVAL;
973 }
974 } else {
Michael Krufky182519f2007-12-25 15:10:11 -0300975 tda_warn("modulation type not supported!\n");
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300976 return -EINVAL;
977 }
978
Michael Krufkyed736832008-01-19 17:41:04 -0300979 /* When tuning digital, the analog demod must be tri-stated */
980 if (fe->ops.analog_ops.standby)
981 fe->ops.analog_ops.standby(fe);
982
Michael Krufkyc293d0a2008-04-22 14:46:06 -0300983 ret = tda18271_tune(fe, map, freq, bw);
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300984
Michael Krufky4bd5d102008-05-04 21:32:21 -0300985 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -0300986 goto fail;
987
988 priv->frequency = freq;
989 priv->bandwidth = (fe->ops.info.type == FE_OFDM) ?
990 params->u.ofdm.bandwidth : 0;
991fail:
992 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300993}
994
995static int tda18271_set_analog_params(struct dvb_frontend *fe,
996 struct analog_parameters *params)
997{
998 struct tda18271_priv *priv = fe->tuner_priv;
Michael Krufkyf21e0d72008-01-02 03:01:54 -0300999 struct tda18271_std_map *std_map = &priv->std;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001000 struct tda18271_std_map_item *map;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001001 char *mode;
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001002 int ret;
Michael Krufky4d831782009-09-27 14:05:12 -03001003 u32 freq = params->frequency * 125 *
1004 ((params->mode == V4L2_TUNER_RADIO) ? 1 : 1000) / 2;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001005
1006 priv->mode = TDA18271_ANALOG;
1007
Michael Krufkyc353f422008-01-08 10:38:10 -03001008 if (params->mode == V4L2_TUNER_RADIO) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001009 map = &std_map->fm_radio;
Michael Krufkyc353f422008-01-08 10:38:10 -03001010 mode = "fm";
1011 } else if (params->std & V4L2_STD_MN) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001012 map = &std_map->atv_mn;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001013 mode = "MN";
1014 } else if (params->std & V4L2_STD_B) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001015 map = &std_map->atv_b;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001016 mode = "B";
1017 } else if (params->std & V4L2_STD_GH) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001018 map = &std_map->atv_gh;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001019 mode = "GH";
1020 } else if (params->std & V4L2_STD_PAL_I) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001021 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001022 mode = "I";
1023 } else if (params->std & V4L2_STD_DK) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001024 map = &std_map->atv_dk;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001025 mode = "DK";
1026 } else if (params->std & V4L2_STD_SECAM_L) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001027 map = &std_map->atv_l;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001028 mode = "L";
1029 } else if (params->std & V4L2_STD_SECAM_LC) {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001030 map = &std_map->atv_lc;
Michael Krufky95af8a22008-01-01 18:31:34 -03001031 mode = "L'";
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001032 } else {
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001033 map = &std_map->atv_i;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001034 mode = "xx";
1035 }
1036
Michael Krufky182519f2007-12-25 15:10:11 -03001037 tda_dbg("setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001038
Michael Krufkyc293d0a2008-04-22 14:46:06 -03001039 ret = tda18271_tune(fe, map, freq, 0);
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001040
Michael Krufky4bd5d102008-05-04 21:32:21 -03001041 if (tda_fail(ret))
Michael Krufkyccbac9b2008-01-06 00:55:21 -03001042 goto fail;
1043
1044 priv->frequency = freq;
1045 priv->bandwidth = 0;
1046fail:
1047 return ret;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001048}
1049
1050static int tda18271_release(struct dvb_frontend *fe)
1051{
Michael Krufkya4f263b2008-01-06 15:52:56 -03001052 struct tda18271_priv *priv = fe->tuner_priv;
1053
1054 mutex_lock(&tda18271_list_mutex);
1055
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001056 if (priv)
1057 hybrid_tuner_release_state(priv);
Michael Krufkya4f263b2008-01-06 15:52:56 -03001058
Michael Krufkya4f263b2008-01-06 15:52:56 -03001059 mutex_unlock(&tda18271_list_mutex);
1060
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001061 fe->tuner_priv = NULL;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001062
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001063 return 0;
1064}
1065
1066static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1067{
1068 struct tda18271_priv *priv = fe->tuner_priv;
1069 *frequency = priv->frequency;
1070 return 0;
1071}
1072
1073static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1074{
1075 struct tda18271_priv *priv = fe->tuner_priv;
1076 *bandwidth = priv->bandwidth;
1077 return 0;
1078}
1079
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001080/* ------------------------------------------------------------------ */
1081
1082#define tda18271_update_std(std_cfg, name) do { \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001083 if (map->std_cfg.if_freq + \
Michael Krufkyc7353722008-03-30 19:40:20 -03001084 map->std_cfg.agc_mode + map->std_cfg.std + \
1085 map->std_cfg.if_lvl + map->std_cfg.rfagc_top > 0) { \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001086 tda_dbg("Using custom std config for %s\n", name); \
1087 memcpy(&std->std_cfg, &map->std_cfg, \
1088 sizeof(struct tda18271_std_map_item)); \
1089 } } while (0)
1090
1091#define tda18271_dump_std_item(std_cfg, name) do { \
Michael Krufkyc7353722008-03-30 19:40:20 -03001092 tda_dbg("(%s) if_freq = %d, agc_mode = %d, std = %d, " \
1093 "if_lvl = %d, rfagc_top = 0x%02x\n", \
Michael Krufky7f7203d2008-04-22 14:46:06 -03001094 name, std->std_cfg.if_freq, \
Michael Krufkyc7353722008-03-30 19:40:20 -03001095 std->std_cfg.agc_mode, std->std_cfg.std, \
1096 std->std_cfg.if_lvl, std->std_cfg.rfagc_top); \
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001097 } while (0)
1098
1099static int tda18271_dump_std_map(struct dvb_frontend *fe)
1100{
1101 struct tda18271_priv *priv = fe->tuner_priv;
1102 struct tda18271_std_map *std = &priv->std;
1103
1104 tda_dbg("========== STANDARD MAP SETTINGS ==========\n");
Michael Krufkyc7353722008-03-30 19:40:20 -03001105 tda18271_dump_std_item(fm_radio, " fm ");
1106 tda18271_dump_std_item(atv_b, "atv b ");
1107 tda18271_dump_std_item(atv_dk, "atv dk");
1108 tda18271_dump_std_item(atv_gh, "atv gh");
1109 tda18271_dump_std_item(atv_i, "atv i ");
1110 tda18271_dump_std_item(atv_l, "atv l ");
1111 tda18271_dump_std_item(atv_lc, "atv l'");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001112 tda18271_dump_std_item(atv_mn, "atv mn");
1113 tda18271_dump_std_item(atsc_6, "atsc 6");
1114 tda18271_dump_std_item(dvbt_6, "dvbt 6");
1115 tda18271_dump_std_item(dvbt_7, "dvbt 7");
1116 tda18271_dump_std_item(dvbt_8, "dvbt 8");
Michael Krufkyc7353722008-03-30 19:40:20 -03001117 tda18271_dump_std_item(qam_6, "qam 6 ");
1118 tda18271_dump_std_item(qam_8, "qam 8 ");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001119
1120 return 0;
1121}
1122
1123static int tda18271_update_std_map(struct dvb_frontend *fe,
1124 struct tda18271_std_map *map)
1125{
1126 struct tda18271_priv *priv = fe->tuner_priv;
1127 struct tda18271_std_map *std = &priv->std;
1128
1129 if (!map)
1130 return -EINVAL;
1131
Michael Krufkyc353f422008-01-08 10:38:10 -03001132 tda18271_update_std(fm_radio, "fm");
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001133 tda18271_update_std(atv_b, "atv b");
1134 tda18271_update_std(atv_dk, "atv dk");
1135 tda18271_update_std(atv_gh, "atv gh");
1136 tda18271_update_std(atv_i, "atv i");
1137 tda18271_update_std(atv_l, "atv l");
1138 tda18271_update_std(atv_lc, "atv l'");
1139 tda18271_update_std(atv_mn, "atv mn");
1140 tda18271_update_std(atsc_6, "atsc 6");
1141 tda18271_update_std(dvbt_6, "dvbt 6");
1142 tda18271_update_std(dvbt_7, "dvbt 7");
1143 tda18271_update_std(dvbt_8, "dvbt 8");
1144 tda18271_update_std(qam_6, "qam 6");
1145 tda18271_update_std(qam_8, "qam 8");
1146
1147 return 0;
1148}
1149
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001150static int tda18271_get_id(struct dvb_frontend *fe)
1151{
1152 struct tda18271_priv *priv = fe->tuner_priv;
1153 unsigned char *regs = priv->tda18271_regs;
1154 char *name;
1155 int ret = 0;
1156
Michael Krufky8d316bf2008-01-06 15:31:35 -03001157 mutex_lock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001158 tda18271_read_regs(fe);
Michael Krufky8d316bf2008-01-06 15:31:35 -03001159 mutex_unlock(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001160
1161 switch (regs[R_ID] & 0x7f) {
1162 case 3:
1163 name = "TDA18271HD/C1";
Michael Krufky255b5112008-01-01 22:52:09 -03001164 priv->id = TDA18271HDC1;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001165 break;
1166 case 4:
1167 name = "TDA18271HD/C2";
Michael Krufky255b5112008-01-01 22:52:09 -03001168 priv->id = TDA18271HDC2;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001169 break;
1170 default:
1171 name = "Unknown device";
1172 ret = -EINVAL;
1173 break;
1174 }
1175
Michael Krufky182519f2007-12-25 15:10:11 -03001176 tda_info("%s detected @ %d-%04x%s\n", name,
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001177 i2c_adapter_id(priv->i2c_props.adap),
1178 priv->i2c_props.addr,
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001179 (0 == ret) ? "" : ", device not supported.");
1180
1181 return ret;
1182}
1183
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001184static struct dvb_tuner_ops tda18271_tuner_ops = {
1185 .info = {
1186 .name = "NXP TDA18271HD",
1187 .frequency_min = 45000000,
1188 .frequency_max = 864000000,
1189 .frequency_step = 62500
1190 },
Michael Krufkyefce8412007-12-01 17:40:16 -03001191 .init = tda18271_init,
Michael Krufky518d8732008-01-13 17:01:01 -03001192 .sleep = tda18271_sleep,
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001193 .set_params = tda18271_set_params,
1194 .set_analog_params = tda18271_set_analog_params,
1195 .release = tda18271_release,
1196 .get_frequency = tda18271_get_frequency,
1197 .get_bandwidth = tda18271_get_bandwidth,
1198};
1199
1200struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
Michael Krufkye435f952007-12-09 22:23:30 -03001201 struct i2c_adapter *i2c,
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001202 struct tda18271_config *cfg)
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001203{
1204 struct tda18271_priv *priv = NULL;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001205 int instance;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001206
Michael Krufkya4f263b2008-01-06 15:52:56 -03001207 mutex_lock(&tda18271_list_mutex);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001208
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001209 instance = hybrid_tuner_request_state(struct tda18271_priv, priv,
1210 hybrid_tuner_instance_list,
1211 i2c, addr, "tda18271");
1212 switch (instance) {
1213 case 0:
1214 goto fail;
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001215 case 1:
Michael Krufky81016b42009-08-27 16:58:06 -03001216 {
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001217 /* new tuner instance */
Michael Krufky81016b42009-08-27 16:58:06 -03001218 int rf_cal_on_startup;
1219
Michael Krufkya4f263b2008-01-06 15:52:56 -03001220 priv->gate = (cfg) ? cfg->gate : TDA18271_GATE_AUTO;
Michael Krufky868f5cc2008-04-22 14:46:23 -03001221 priv->role = (cfg) ? cfg->role : TDA18271_MASTER;
Michael Krufkyadcc4b32009-03-04 19:42:06 -03001222 priv->config = (cfg) ? cfg->config : 0;
Michael Krufky12165312009-08-30 02:32:23 -03001223 priv->small_i2c = (cfg) ? cfg->small_i2c : 0;
Michael Krufky4240b462009-08-29 16:25:37 -03001224 priv->output_opt = (cfg) ?
1225 cfg->output_opt : TDA18271_OUTPUT_LT_XT_ON;
Michael Krufky81016b42009-08-27 16:58:06 -03001226
1227 /* tda18271_cal_on_startup == -1 when cal
1228 * module option is unset */
1229 if (tda18271_cal_on_startup == -1) {
1230 /* honor attach-time configuration */
1231 rf_cal_on_startup =
1232 ((cfg) && (cfg->rf_cal_on_startup)) ? 1 : 0;
1233 } else {
1234 /* module option overrides attach configuration */
1235 rf_cal_on_startup = tda18271_cal_on_startup;
1236 }
1237
Michael Krufkya4f263b2008-01-06 15:52:56 -03001238 priv->cal_initialized = false;
1239 mutex_init(&priv->lock);
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001240
Michael Krufkya4f263b2008-01-06 15:52:56 -03001241 fe->tuner_priv = priv;
Michael Krufky255b5112008-01-01 22:52:09 -03001242
Michael Krufky4bd5d102008-05-04 21:32:21 -03001243 if (tda_fail(tda18271_get_id(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001244 goto fail;
1245
Michael Krufky4bd5d102008-05-04 21:32:21 -03001246 if (tda_fail(tda18271_assign_map_layout(fe)))
Michael Krufkya4f263b2008-01-06 15:52:56 -03001247 goto fail;
1248
1249 mutex_lock(&priv->lock);
1250 tda18271_init_regs(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001251
Michael Krufky81016b42009-08-27 16:58:06 -03001252 if ((rf_cal_on_startup) && (priv->id == TDA18271HDC2))
Michael Krufky12afe372008-04-22 14:42:07 -03001253 tda18271c2_rf_cal_init(fe);
Michael Krufky0f962512008-01-13 22:01:07 -03001254
Michael Krufkya4f263b2008-01-06 15:52:56 -03001255 mutex_unlock(&priv->lock);
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001256 break;
Michael Krufky81016b42009-08-27 16:58:06 -03001257 }
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001258 default:
1259 /* existing tuner instance */
1260 fe->tuner_priv = priv;
1261
Michael Krufkyd5abef62009-08-30 03:07:10 -03001262 /* allow dvb driver to override configuration settings */
1263 if (cfg) {
1264 if (cfg->gate != TDA18271_GATE_ANALOG)
1265 priv->gate = cfg->gate;
1266 if (cfg->role)
1267 priv->role = cfg->role;
1268 if (cfg->config)
1269 priv->config = cfg->config;
1270 if (cfg->small_i2c)
1271 priv->small_i2c = cfg->small_i2c;
1272 if (cfg->output_opt)
1273 priv->output_opt = cfg->output_opt;
1274 }
Michael Krufkyf9e315a2008-04-22 14:41:54 -03001275 break;
Michael Krufkya4f263b2008-01-06 15:52:56 -03001276 }
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001277
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001278 /* override default std map with values in config struct */
1279 if ((cfg) && (cfg->std_map))
1280 tda18271_update_std_map(fe, cfg->std_map);
1281
Michael Krufkya4f263b2008-01-06 15:52:56 -03001282 mutex_unlock(&tda18271_list_mutex);
1283
1284 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1285 sizeof(struct dvb_tuner_ops));
1286
Michael Krufkyc7353722008-03-30 19:40:20 -03001287 if (tda18271_debug & (DBG_MAP | DBG_ADV))
Michael Krufkyf21e0d72008-01-02 03:01:54 -03001288 tda18271_dump_std_map(fe);
1289
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001290 return fe;
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001291fail:
Michael Krufkya4f263b2008-01-06 15:52:56 -03001292 mutex_unlock(&tda18271_list_mutex);
1293
Michael Krufky49e7aaf2007-12-24 04:15:20 -03001294 tda18271_release(fe);
1295 return NULL;
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001296}
1297EXPORT_SYMBOL_GPL(tda18271_attach);
1298MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1299MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1300MODULE_LICENSE("GPL");
Michael Krufky5ec96b02008-04-22 14:46:23 -03001301MODULE_VERSION("0.3");
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001302
1303/*
1304 * Overrides for Emacs so that we follow Linus's tabbing style.
1305 * ---------------------------------------------------------------------------
1306 * Local variables:
1307 * c-basic-offset: 8
1308 * End:
1309 */