blob: 1944177da873ee5c3dc0a37a719bc304d00ed7c8 [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
4 Copyright (C) 2007 Michael Krufky (mkrufky@linuxtv.org)
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
Michael Krufky5bea1cd2007-10-22 09:56:38 -030021#include <linux/delay.h>
22#include <linux/videodev2.h>
Michael Krufky7d11c532007-10-24 09:55:54 -030023#include "tuner-driver.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030024
25#include "tda18271.h"
Michael Krufky6ca04de2007-11-23 16:52:15 -030026#include "tda18271-priv.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030027
Michael Krufky54465b02007-11-23 18:14:53 -030028static int tda18271_debug;
29module_param_named(debug, tda18271_debug, int, 0644);
Michael Krufky5bea1cd2007-10-22 09:56:38 -030030MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
31
Michael Krufky6f998742007-10-24 01:00:24 -030032#define dprintk(level, fmt, arg...) do {\
Michael Krufky54465b02007-11-23 18:14:53 -030033 if (tda18271_debug >= level) \
Michael Krufky6f998742007-10-24 01:00:24 -030034 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ##arg); } while (0)
Michael Krufky5bea1cd2007-10-22 09:56:38 -030035
Michael Krufky5bea1cd2007-10-22 09:56:38 -030036/*---------------------------------------------------------------------*/
37
Michael Krufky5bea1cd2007-10-22 09:56:38 -030038#define TDA18271_ANALOG 0
39#define TDA18271_DIGITAL 1
40
41struct tda18271_priv {
42 u8 i2c_addr;
43 struct i2c_adapter *i2c_adap;
44 unsigned char tda18271_regs[TDA18271_NUM_REGS];
45 int mode;
46
47 u32 frequency;
48 u32 bandwidth;
49};
50
Michael Krufky7d11c532007-10-24 09:55:54 -030051static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
52{
53 struct tda18271_priv *priv = fe->tuner_priv;
54 struct analog_tuner_ops *ops = fe->ops.analog_demod_ops;
55 int ret = 0;
56
57 switch (priv->mode) {
58 case TDA18271_ANALOG:
59 if (ops && ops->i2c_gate_ctrl)
60 ret = ops->i2c_gate_ctrl(fe, enable);
61 break;
62 case TDA18271_DIGITAL:
63 if (fe->ops.i2c_gate_ctrl)
64 ret = fe->ops.i2c_gate_ctrl(fe, enable);
65 break;
66 }
67
68 return ret;
69};
70
Michael Krufky5bea1cd2007-10-22 09:56:38 -030071/*---------------------------------------------------------------------*/
72
73static void tda18271_dump_regs(struct dvb_frontend *fe)
74{
75 struct tda18271_priv *priv = fe->tuner_priv;
76 unsigned char *regs = priv->tda18271_regs;
77
Michael Krufky6f998742007-10-24 01:00:24 -030078 dprintk(1, "=== TDA18271 REG DUMP ===\n");
79 dprintk(1, "ID_BYTE = 0x%x\n", 0xff & regs[R_ID]);
80 dprintk(1, "THERMO_BYTE = 0x%x\n", 0xff & regs[R_TM]);
81 dprintk(1, "POWER_LEVEL_BYTE = 0x%x\n", 0xff & regs[R_PL]);
82 dprintk(1, "EASY_PROG_BYTE_1 = 0x%x\n", 0xff & regs[R_EP1]);
83 dprintk(1, "EASY_PROG_BYTE_2 = 0x%x\n", 0xff & regs[R_EP2]);
84 dprintk(1, "EASY_PROG_BYTE_3 = 0x%x\n", 0xff & regs[R_EP3]);
85 dprintk(1, "EASY_PROG_BYTE_4 = 0x%x\n", 0xff & regs[R_EP4]);
86 dprintk(1, "EASY_PROG_BYTE_5 = 0x%x\n", 0xff & regs[R_EP5]);
87 dprintk(1, "CAL_POST_DIV_BYTE = 0x%x\n", 0xff & regs[R_CPD]);
88 dprintk(1, "CAL_DIV_BYTE_1 = 0x%x\n", 0xff & regs[R_CD1]);
89 dprintk(1, "CAL_DIV_BYTE_2 = 0x%x\n", 0xff & regs[R_CD2]);
90 dprintk(1, "CAL_DIV_BYTE_3 = 0x%x\n", 0xff & regs[R_CD3]);
91 dprintk(1, "MAIN_POST_DIV_BYTE = 0x%x\n", 0xff & regs[R_MPD]);
92 dprintk(1, "MAIN_DIV_BYTE_1 = 0x%x\n", 0xff & regs[R_MD1]);
93 dprintk(1, "MAIN_DIV_BYTE_2 = 0x%x\n", 0xff & regs[R_MD2]);
94 dprintk(1, "MAIN_DIV_BYTE_3 = 0x%x\n", 0xff & regs[R_MD3]);
Michael Krufky5bea1cd2007-10-22 09:56:38 -030095}
96
97static void tda18271_read_regs(struct dvb_frontend *fe)
98{
99 struct tda18271_priv *priv = fe->tuner_priv;
100 unsigned char *regs = priv->tda18271_regs;
101 unsigned char buf = 0x00;
102 int ret;
103 struct i2c_msg msg[] = {
104 { .addr = priv->i2c_addr, .flags = 0,
105 .buf = &buf, .len = 1 },
106 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
107 .buf = regs, .len = 16 }
108 };
109
Michael Krufky7d11c532007-10-24 09:55:54 -0300110 tda18271_i2c_gate_ctrl(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300111
112 /* read all registers */
113 ret = i2c_transfer(priv->i2c_adap, msg, 2);
114
Michael Krufky7d11c532007-10-24 09:55:54 -0300115 tda18271_i2c_gate_ctrl(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300116
117 if (ret != 2)
118 printk("ERROR: %s: i2c_transfer returned: %d\n",
119 __FUNCTION__, ret);
120
Michael Krufky54465b02007-11-23 18:14:53 -0300121 if (tda18271_debug > 2)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300122 tda18271_dump_regs(fe);
123}
124
125static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
126{
127 struct tda18271_priv *priv = fe->tuner_priv;
128 unsigned char *regs = priv->tda18271_regs;
129 unsigned char buf[TDA18271_NUM_REGS+1];
130 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
131 .buf = buf, .len = len+1 };
132 int i, ret;
133
134 BUG_ON((len == 0) || (idx+len > sizeof(buf)));
135
136 buf[0] = idx;
137 for (i = 1; i <= len; i++) {
138 buf[i] = regs[idx-1+i];
139 }
140
Michael Krufky7d11c532007-10-24 09:55:54 -0300141 tda18271_i2c_gate_ctrl(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300142
143 /* write registers */
144 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
145
Michael Krufky7d11c532007-10-24 09:55:54 -0300146 tda18271_i2c_gate_ctrl(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300147
148 if (ret != 1)
149 printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n",
150 __FUNCTION__, ret);
151}
152
153/*---------------------------------------------------------------------*/
154
Michael Krufky22ee1252007-11-22 17:13:00 -0300155static int tda18271_init_regs(struct dvb_frontend *fe)
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300156{
157 struct tda18271_priv *priv = fe->tuner_priv;
158 unsigned char *regs = priv->tda18271_regs;
159
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300160 printk(KERN_INFO "tda18271: initializing registers\n");
161
162 /* initialize registers */
163 regs[R_ID] = 0x83;
164 regs[R_TM] = 0x08;
165 regs[R_PL] = 0x80;
166 regs[R_EP1] = 0xc6;
167 regs[R_EP2] = 0xdf;
168 regs[R_EP3] = 0x16;
169 regs[R_EP4] = 0x60;
170 regs[R_EP5] = 0x80;
171 regs[R_CPD] = 0x80;
172 regs[R_CD1] = 0x00;
173 regs[R_CD2] = 0x00;
174 regs[R_CD3] = 0x00;
175 regs[R_MPD] = 0x00;
176 regs[R_MD1] = 0x00;
177 regs[R_MD2] = 0x00;
178 regs[R_MD3] = 0x00;
179 regs[R_EB1] = 0xff;
180 regs[R_EB2] = 0x01;
181 regs[R_EB3] = 0x84;
182 regs[R_EB4] = 0x41;
183 regs[R_EB5] = 0x01;
184 regs[R_EB6] = 0x84;
185 regs[R_EB7] = 0x40;
186 regs[R_EB8] = 0x07;
187 regs[R_EB9] = 0x00;
188 regs[R_EB10] = 0x00;
189 regs[R_EB11] = 0x96;
190 regs[R_EB12] = 0x0f;
191 regs[R_EB13] = 0xc1;
192 regs[R_EB14] = 0x00;
193 regs[R_EB15] = 0x8f;
194 regs[R_EB16] = 0x00;
195 regs[R_EB17] = 0x00;
196 regs[R_EB18] = 0x00;
197 regs[R_EB19] = 0x00;
198 regs[R_EB20] = 0x20;
199 regs[R_EB21] = 0x33;
200 regs[R_EB22] = 0x48;
201 regs[R_EB23] = 0xb0;
202
203 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
204 /* setup AGC1 & AGC2 */
205 regs[R_EB17] = 0x00;
206 tda18271_write_regs(fe, R_EB17, 1);
207 regs[R_EB17] = 0x03;
208 tda18271_write_regs(fe, R_EB17, 1);
209 regs[R_EB17] = 0x43;
210 tda18271_write_regs(fe, R_EB17, 1);
211 regs[R_EB17] = 0x4c;
212 tda18271_write_regs(fe, R_EB17, 1);
213
214 regs[R_EB20] = 0xa0;
215 tda18271_write_regs(fe, R_EB20, 1);
216 regs[R_EB20] = 0xa7;
217 tda18271_write_regs(fe, R_EB20, 1);
218 regs[R_EB20] = 0xe7;
219 tda18271_write_regs(fe, R_EB20, 1);
220 regs[R_EB20] = 0xec;
221 tda18271_write_regs(fe, R_EB20, 1);
222
223 /* image rejection calibration */
224
225 /* low-band */
226 regs[R_EP3] = 0x1f;
227 regs[R_EP4] = 0x66;
228 regs[R_EP5] = 0x81;
229 regs[R_CPD] = 0xcc;
230 regs[R_CD1] = 0x6c;
231 regs[R_CD2] = 0x00;
232 regs[R_CD3] = 0x00;
233 regs[R_MPD] = 0xcd;
234 regs[R_MD1] = 0x77;
235 regs[R_MD2] = 0x08;
236 regs[R_MD3] = 0x00;
237
238 tda18271_write_regs(fe, R_EP3, 11);
239 msleep(5); /* pll locking */
240
241 regs[R_EP1] = 0xc6;
242 tda18271_write_regs(fe, R_EP1, 1);
243 msleep(5); /* wanted low measurement */
244
245 regs[R_EP3] = 0x1f;
246 regs[R_EP4] = 0x66;
247 regs[R_EP5] = 0x85;
248 regs[R_CPD] = 0xcb;
249 regs[R_CD1] = 0x66;
250 regs[R_CD2] = 0x70;
251 regs[R_CD3] = 0x00;
252
253 tda18271_write_regs(fe, R_EP3, 7);
254 msleep(5); /* pll locking */
255
256 regs[R_EP2] = 0xdf;
257 tda18271_write_regs(fe, R_EP2, 1);
258 msleep(30); /* image low optimization completion */
259
260 /* mid-band */
261 regs[R_EP3] = 0x1f;
262 regs[R_EP4] = 0x66;
263 regs[R_EP5] = 0x82;
264 regs[R_CPD] = 0xa8;
265 regs[R_CD1] = 0x66;
266 regs[R_CD2] = 0x00;
267 regs[R_CD3] = 0x00;
268 regs[R_MPD] = 0xa9;
269 regs[R_MD1] = 0x73;
270 regs[R_MD2] = 0x1a;
271 regs[R_MD3] = 0x00;
272
273 tda18271_write_regs(fe, R_EP3, 11);
274 msleep(5); /* pll locking */
275
276 regs[R_EP1] = 0xc6;
277 tda18271_write_regs(fe, R_EP1, 1);
278 msleep(5); /* wanted mid measurement */
279
280 regs[R_EP3] = 0x1f;
281 regs[R_EP4] = 0x66;
282 regs[R_EP5] = 0x86;
283 regs[R_CPD] = 0xa8;
284 regs[R_CD1] = 0x66;
285 regs[R_CD2] = 0xa0;
286 regs[R_CD3] = 0x00;
287
288 tda18271_write_regs(fe, R_EP3, 7);
289 msleep(5); /* pll locking */
290
291 regs[R_EP2] = 0xdf;
292 tda18271_write_regs(fe, R_EP2, 1);
293 msleep(30); /* image mid optimization completion */
294
295 /* high-band */
296 regs[R_EP3] = 0x1f;
297 regs[R_EP4] = 0x66;
298 regs[R_EP5] = 0x83;
299 regs[R_CPD] = 0x98;
300 regs[R_CD1] = 0x65;
301 regs[R_CD2] = 0x00;
302 regs[R_CD3] = 0x00;
303 regs[R_MPD] = 0x99;
304 regs[R_MD1] = 0x71;
305 regs[R_MD2] = 0xcd;
306 regs[R_MD3] = 0x00;
307
308 tda18271_write_regs(fe, R_EP3, 11);
309 msleep(5); /* pll locking */
310
311 regs[R_EP1] = 0xc6;
312 tda18271_write_regs(fe, R_EP1, 1);
313 msleep(5); /* wanted high measurement */
314
315 regs[R_EP3] = 0x1f;
316 regs[R_EP4] = 0x66;
317 regs[R_EP5] = 0x87;
318 regs[R_CPD] = 0x98;
319 regs[R_CD1] = 0x65;
320 regs[R_CD2] = 0x50;
321 regs[R_CD3] = 0x00;
322
323 tda18271_write_regs(fe, R_EP3, 7);
324 msleep(5); /* pll locking */
325
326 regs[R_EP2] = 0xdf;
327
328 tda18271_write_regs(fe, R_EP2, 1);
329 msleep(30); /* image high optimization completion */
330
331 regs[R_EP4] = 0x64;
332 tda18271_write_regs(fe, R_EP4, 1);
333
334 regs[R_EP1] = 0xc6;
335 tda18271_write_regs(fe, R_EP1, 1);
Michael Krufky22ee1252007-11-22 17:13:00 -0300336
337 return 0;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300338}
339
Michael Krufkyefce8412007-12-01 17:40:16 -0300340static int tda18271_init(struct dvb_frontend *fe)
341{
342 struct tda18271_priv *priv = fe->tuner_priv;
343 unsigned char *regs = priv->tda18271_regs;
344
345 tda18271_read_regs(fe);
346
347 /* test IR_CAL_OK to see if we need init */
348 if ((regs[R_EP1] & 0x08) == 0)
349 tda18271_init_regs(fe);
350
351 return 0;
352}
353
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300354static int tda18271_tune(struct dvb_frontend *fe,
355 u32 ifc, u32 freq, u32 bw, u8 std)
356{
357 struct tda18271_priv *priv = fe->tuner_priv;
358 unsigned char *regs = priv->tda18271_regs;
359 u32 div, N = 0;
360 int i;
361
Michael Krufky14572632007-12-02 02:32:49 -0300362 tda18271_init(fe);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300363
Michael Krufky6f998742007-10-24 01:00:24 -0300364 dprintk(1, "freq = %d, ifc = %d\n", freq, ifc);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300365
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300366 /* RF tracking filter calibration */
367
368 /* calculate BP_Filter */
369 i = 0;
370 while ((tda18271_bp_filter[i].rfmax * 1000) < freq) {
371 if (tda18271_bp_filter[i + 1].rfmax == 0)
372 break;
373 i++;
374 }
Michael Krufky6f998742007-10-24 01:00:24 -0300375 dprintk(2, "bp filter = 0x%x, i = %d\n", tda18271_bp_filter[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300376
377 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
378 regs[R_EP1] |= tda18271_bp_filter[i].val;
379 tda18271_write_regs(fe, R_EP1, 1);
380
381 regs[R_EB4] &= 0x07;
382 regs[R_EB4] |= 0x60;
383 tda18271_write_regs(fe, R_EB4, 1);
384
385 regs[R_EB7] = 0x60;
386 tda18271_write_regs(fe, R_EB7, 1);
387
388 regs[R_EB14] = 0x00;
389 tda18271_write_regs(fe, R_EB14, 1);
390
391 regs[R_EB20] = 0xcc;
392 tda18271_write_regs(fe, R_EB20, 1);
393
394 /* set CAL mode to RF tracking filter calibration */
395 regs[R_EB4] |= 0x03;
396
397 /* calculate CAL PLL */
398
399 switch (priv->mode) {
400 case TDA18271_ANALOG:
401 N = freq - 1250000;
402 break;
403 case TDA18271_DIGITAL:
404 N = freq + bw / 2;
405 break;
406 }
407
408 i = 0;
409 while ((tda18271_cal_pll[i].lomax * 1000) < N) {
410 if (tda18271_cal_pll[i + 1].lomax == 0)
411 break;
412 i++;
413 }
Michael Krufky6f998742007-10-24 01:00:24 -0300414 dprintk(2, "cal pll, pd = 0x%x, d = 0x%x, i = %d\n",
415 tda18271_cal_pll[i].pd, tda18271_cal_pll[i].d, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300416
417 regs[R_CPD] = tda18271_cal_pll[i].pd;
418
419 div = ((tda18271_cal_pll[i].d * (N / 1000)) << 7) / 125;
420 regs[R_CD1] = 0xff & (div >> 16);
421 regs[R_CD2] = 0xff & (div >> 8);
422 regs[R_CD3] = 0xff & div;
423
424 /* calculate MAIN PLL */
425
426 switch (priv->mode) {
427 case TDA18271_ANALOG:
428 N = freq - 250000;
429 break;
430 case TDA18271_DIGITAL:
431 N = freq + bw / 2 + 1000000;
432 break;
433 }
434
435 i = 0;
436 while ((tda18271_main_pll[i].lomax * 1000) < N) {
437 if (tda18271_main_pll[i + 1].lomax == 0)
438 break;
439 i++;
440 }
Michael Krufky6f998742007-10-24 01:00:24 -0300441 dprintk(2, "main pll, pd = 0x%x, d = 0x%x, i = %d\n",
442 tda18271_main_pll[i].pd, tda18271_main_pll[i].d, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300443
444 regs[R_MPD] = (0x7f & tda18271_main_pll[i].pd);
445
446 switch (priv->mode) {
447 case TDA18271_ANALOG:
448 regs[R_MPD] &= ~0x08;
449 break;
450 case TDA18271_DIGITAL:
451 regs[R_MPD] |= 0x08;
452 break;
453 }
454
455 div = ((tda18271_main_pll[i].d * (N / 1000)) << 7) / 125;
456 regs[R_MD1] = 0xff & (div >> 16);
457 regs[R_MD2] = 0xff & (div >> 8);
458 regs[R_MD3] = 0xff & div;
459
460 tda18271_write_regs(fe, R_EP3, 11);
461 msleep(5); /* RF tracking filter calibration initialization */
462
463 /* search for K,M,CO for RF Calibration */
464 i = 0;
465 while ((tda18271_km[i].rfmax * 1000) < freq) {
466 if (tda18271_km[i + 1].rfmax == 0)
467 break;
468 i++;
469 }
Michael Krufky6f998742007-10-24 01:00:24 -0300470 dprintk(2, "km = 0x%x, i = %d\n", tda18271_km[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300471
472 regs[R_EB13] &= 0x83;
473 regs[R_EB13] |= tda18271_km[i].val;
474 tda18271_write_regs(fe, R_EB13, 1);
475
476 /* search for RF_BAND */
477 i = 0;
478 while ((tda18271_rf_band[i].rfmax * 1000) < freq) {
479 if (tda18271_rf_band[i + 1].rfmax == 0)
480 break;
481 i++;
482 }
Michael Krufky6f998742007-10-24 01:00:24 -0300483 dprintk(2, "rf band = 0x%x, i = %d\n", tda18271_rf_band[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300484
485 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
486 regs[R_EP2] |= (tda18271_rf_band[i].val << 5);
487
488 /* search for Gain_Taper */
489 i = 0;
490 while ((tda18271_gain_taper[i].rfmax * 1000) < freq) {
491 if (tda18271_gain_taper[i + 1].rfmax == 0)
492 break;
493 i++;
494 }
Michael Krufky6f998742007-10-24 01:00:24 -0300495 dprintk(2, "gain taper = 0x%x, i = %d\n",
496 tda18271_gain_taper[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300497
498 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
499 regs[R_EP2] |= tda18271_gain_taper[i].val;
500
501 tda18271_write_regs(fe, R_EP2, 1);
502 tda18271_write_regs(fe, R_EP1, 1);
503 tda18271_write_regs(fe, R_EP2, 1);
504 tda18271_write_regs(fe, R_EP1, 1);
505
506 regs[R_EB4] &= 0x07;
507 regs[R_EB4] |= 0x40;
508 tda18271_write_regs(fe, R_EB4, 1);
509
510 regs[R_EB7] = 0x40;
511 tda18271_write_regs(fe, R_EB7, 1);
512 msleep(10);
513
514 regs[R_EB20] = 0xec;
515 tda18271_write_regs(fe, R_EB20, 1);
516 msleep(60); /* RF tracking filter calibration completion */
517
518 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
519 tda18271_write_regs(fe, R_EP4, 1);
520
521 tda18271_write_regs(fe, R_EP1, 1);
522
523 /* RF tracking filer correction for VHF_Low band */
524 i = 0;
525 while ((tda18271_rf_cal[i].rfmax * 1000) < freq) {
526 if (tda18271_rf_cal[i].rfmax == 0)
527 break;
528 i++;
529 }
Michael Krufky6f998742007-10-24 01:00:24 -0300530 dprintk(2, "rf cal = 0x%x, i = %d\n", tda18271_rf_cal[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300531
532 /* VHF_Low band only */
533 if (tda18271_rf_cal[i].rfmax != 0) {
534 regs[R_EB14] = tda18271_rf_cal[i].val;
535 tda18271_write_regs(fe, R_EB14, 1);
536 }
537
538 /* Channel Configuration */
539
540 switch (priv->mode) {
541 case TDA18271_ANALOG:
542 regs[R_EB22] = 0x2c;
543 break;
544 case TDA18271_DIGITAL:
545 regs[R_EB22] = 0x37;
546 break;
547 }
548 tda18271_write_regs(fe, R_EB22, 1);
549
550 regs[R_EP1] |= 0x40; /* set dis power level on */
551
552 /* set standard */
553 regs[R_EP3] &= ~0x1f; /* clear std bits */
554
555 /* see table 22 */
556 regs[R_EP3] |= std;
557
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300558 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
559
560 regs[R_EP4] &= ~0x1c; /* clear if level bits */
561 switch (priv->mode) {
562 case TDA18271_ANALOG:
563 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
564 break;
565 case TDA18271_DIGITAL:
566 regs[R_EP4] |= 0x04;
567 regs[R_MPD] |= 0x80;
568 break;
569 }
570
571 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
572
573 /* FIXME: image rejection validity EP5[2:0] */
574
575 /* calculate MAIN PLL */
576 N = freq + ifc;
577
578 i = 0;
579 while ((tda18271_main_pll[i].lomax * 1000) < N) {
580 if (tda18271_main_pll[i + 1].lomax == 0)
581 break;
582 i++;
583 }
Michael Krufky6f998742007-10-24 01:00:24 -0300584 dprintk(2, "main pll, pd = 0x%x, d = 0x%x, i = %d\n",
585 tda18271_main_pll[i].pd, tda18271_main_pll[i].d, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300586
587 regs[R_MPD] = (0x7f & tda18271_main_pll[i].pd);
588 switch (priv->mode) {
589 case TDA18271_ANALOG:
590 regs[R_MPD] &= ~0x08;
591 break;
592 case TDA18271_DIGITAL:
593 regs[R_MPD] |= 0x08;
594 break;
595 }
596
597 div = ((tda18271_main_pll[i].d * (N / 1000)) << 7) / 125;
598 regs[R_MD1] = 0xff & (div >> 16);
599 regs[R_MD2] = 0xff & (div >> 8);
600 regs[R_MD3] = 0xff & div;
601
602 tda18271_write_regs(fe, R_TM, 15);
603 msleep(5);
Michael Krufky6ca04de2007-11-23 16:52:15 -0300604
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300605 return 0;
606}
607
608/* ------------------------------------------------------------------ */
609
610static int tda18271_set_params(struct dvb_frontend *fe,
611 struct dvb_frontend_parameters *params)
612{
613 struct tda18271_priv *priv = fe->tuner_priv;
614 u8 std;
615 u32 bw, sgIF = 0;
616
617 u32 freq = params->frequency;
618
619 priv->mode = TDA18271_DIGITAL;
620
621 /* see table 22 */
622 if (fe->ops.info.type == FE_ATSC) {
623 switch (params->u.vsb.modulation) {
624 case VSB_8:
625 case VSB_16:
626 std = 0x1b; /* device-specific (spec says 0x1c) */
627 sgIF = 5380000;
628 break;
629 case QAM_64:
630 case QAM_256:
631 std = 0x18; /* device-specific (spec says 0x1d) */
632 sgIF = 4000000;
633 break;
634 default:
635 printk(KERN_WARNING "%s: modulation not set!\n",
636 __FUNCTION__);
637 return -EINVAL;
638 }
639 freq += 1750000; /* Adjust to center (+1.75MHZ) */
640 bw = 6000000;
641 } else if (fe->ops.info.type == FE_OFDM) {
642 switch (params->u.ofdm.bandwidth) {
643 case BANDWIDTH_6_MHZ:
Michael Krufky6ca04de2007-11-23 16:52:15 -0300644 std = 0x1b; /* device-specific (spec says 0x1c) */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300645 bw = 6000000;
Michael Krufky6ca04de2007-11-23 16:52:15 -0300646 sgIF = 3300000;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300647 break;
648 case BANDWIDTH_7_MHZ:
Michael Krufky6ca04de2007-11-23 16:52:15 -0300649 std = 0x19; /* device-specific (spec says 0x1d) */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300650 bw = 7000000;
Michael Krufky6ca04de2007-11-23 16:52:15 -0300651 sgIF = 3800000;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300652 break;
653 case BANDWIDTH_8_MHZ:
Michael Krufky6ca04de2007-11-23 16:52:15 -0300654 std = 0x1a; /* device-specific (spec says 0x1e) */
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300655 bw = 8000000;
Michael Krufky6ca04de2007-11-23 16:52:15 -0300656 sgIF = 4300000;
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300657 break;
658 default:
659 printk(KERN_WARNING "%s: bandwidth not set!\n",
660 __FUNCTION__);
661 return -EINVAL;
662 }
663 } else {
664 printk(KERN_WARNING "%s: modulation type not supported!\n",
665 __FUNCTION__);
666 return -EINVAL;
667 }
668
669 return tda18271_tune(fe, sgIF, freq, bw, std);
670}
671
672static int tda18271_set_analog_params(struct dvb_frontend *fe,
673 struct analog_parameters *params)
674{
675 struct tda18271_priv *priv = fe->tuner_priv;
676 u8 std;
677 unsigned int sgIF;
678 char *mode;
679
680 priv->mode = TDA18271_ANALOG;
681
682 /* see table 22 */
683 if (params->std & V4L2_STD_MN) {
684 std = 0x0d;
685 sgIF = 92;
686 mode = "MN";
687 } else if (params->std & V4L2_STD_B) {
688 std = 0x0e;
689 sgIF = 108;
690 mode = "B";
691 } else if (params->std & V4L2_STD_GH) {
692 std = 0x0f;
693 sgIF = 124;
694 mode = "GH";
695 } else if (params->std & V4L2_STD_PAL_I) {
696 std = 0x0f;
697 sgIF = 124;
698 mode = "I";
699 } else if (params->std & V4L2_STD_DK) {
700 std = 0x0f;
701 sgIF = 124;
702 mode = "DK";
703 } else if (params->std & V4L2_STD_SECAM_L) {
704 std = 0x0f;
705 sgIF = 124;
706 mode = "L";
707 } else if (params->std & V4L2_STD_SECAM_LC) {
708 std = 0x0f;
709 sgIF = 20;
710 mode = "LC";
711 } else {
712 std = 0x0f;
713 sgIF = 124;
714 mode = "xx";
715 }
716
717 if (params->mode == V4L2_TUNER_RADIO)
718 sgIF = 88; /* if frequency is 5.5 MHz */
719
Michael Krufky6f998742007-10-24 01:00:24 -0300720 dprintk(1, "setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300721
722 return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
723 0, std);
724}
725
726static int tda18271_release(struct dvb_frontend *fe)
727{
728 kfree(fe->tuner_priv);
729 fe->tuner_priv = NULL;
730 return 0;
731}
732
733static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
734{
735 struct tda18271_priv *priv = fe->tuner_priv;
736 *frequency = priv->frequency;
737 return 0;
738}
739
740static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
741{
742 struct tda18271_priv *priv = fe->tuner_priv;
743 *bandwidth = priv->bandwidth;
744 return 0;
745}
746
747static struct dvb_tuner_ops tda18271_tuner_ops = {
748 .info = {
749 .name = "NXP TDA18271HD",
750 .frequency_min = 45000000,
751 .frequency_max = 864000000,
752 .frequency_step = 62500
753 },
Michael Krufkyefce8412007-12-01 17:40:16 -0300754 .init = tda18271_init,
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300755 .set_params = tda18271_set_params,
756 .set_analog_params = tda18271_set_analog_params,
757 .release = tda18271_release,
758 .get_frequency = tda18271_get_frequency,
759 .get_bandwidth = tda18271_get_bandwidth,
760};
761
762struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
763 struct i2c_adapter *i2c)
764{
765 struct tda18271_priv *priv = NULL;
766
Michael Krufky6ca04de2007-11-23 16:52:15 -0300767 dprintk(1, "@ %d-%04x\n", i2c_adapter_id(i2c), addr);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300768 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
769 if (priv == NULL)
770 return NULL;
771
772 priv->i2c_addr = addr;
773 priv->i2c_adap = i2c;
774
775 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
776 sizeof(struct dvb_tuner_ops));
777
778 fe->tuner_priv = priv;
779
Michael Krufkyefce8412007-12-01 17:40:16 -0300780 tda18271_init_regs(fe);
781
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300782 return fe;
783}
784EXPORT_SYMBOL_GPL(tda18271_attach);
785MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
786MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
787MODULE_LICENSE("GPL");
788
789/*
790 * Overrides for Emacs so that we follow Linus's tabbing style.
791 * ---------------------------------------------------------------------------
792 * Local variables:
793 * c-basic-offset: 8
794 * End:
795 */