blob: aaaa2f8851863cb7f849da859ba93f7c9dd1095a [file] [log] [blame]
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001/*
2 tda18271.c - driver for the Philips / NXP TDA18271 silicon tuner
3
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
21#include <linux/i2c.h>
22#include <linux/delay.h>
23#include <linux/videodev2.h>
Michael Krufky7d11c532007-10-24 09:55:54 -030024#include "tuner-driver.h"
Michael Krufky5bea1cd2007-10-22 09:56:38 -030025
26#include "tda18271.h"
27
28static int debug;
29module_param(debug, int, 0644);
30MODULE_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 {\
33 if (debug >= level) \
34 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ##arg); } while (0)
Michael Krufky5bea1cd2007-10-22 09:56:38 -030035
36#define R_ID 0x00 /* ID byte */
37#define R_TM 0x01 /* Thermo byte */
38#define R_PL 0x02 /* Power level byte */
39#define R_EP1 0x03 /* Easy Prog byte 1 */
40#define R_EP2 0x04 /* Easy Prog byte 2 */
41#define R_EP3 0x05 /* Easy Prog byte 3 */
42#define R_EP4 0x06 /* Easy Prog byte 4 */
43#define R_EP5 0x07 /* Easy Prog byte 5 */
44#define R_CPD 0x08 /* Cal Post-Divider byte */
45#define R_CD1 0x09 /* Cal Divider byte 1 */
46#define R_CD2 0x0a /* Cal Divider byte 2 */
47#define R_CD3 0x0b /* Cal Divider byte 3 */
48#define R_MPD 0x0c /* Main Post-Divider byte */
49#define R_MD1 0x0d /* Main Divider byte 1 */
50#define R_MD2 0x0e /* Main Divider byte 2 */
51#define R_MD3 0x0f /* Main Divider byte 3 */
52#define R_EB1 0x10 /* Extended byte 1 */
53#define R_EB2 0x11 /* Extended byte 2 */
54#define R_EB3 0x12 /* Extended byte 3 */
55#define R_EB4 0x13 /* Extended byte 4 */
56#define R_EB5 0x14 /* Extended byte 5 */
57#define R_EB6 0x15 /* Extended byte 6 */
58#define R_EB7 0x16 /* Extended byte 7 */
59#define R_EB8 0x17 /* Extended byte 8 */
60#define R_EB9 0x18 /* Extended byte 9 */
61#define R_EB10 0x19 /* Extended byte 10 */
62#define R_EB11 0x1a /* Extended byte 11 */
63#define R_EB12 0x1b /* Extended byte 12 */
64#define R_EB13 0x1c /* Extended byte 13 */
65#define R_EB14 0x1d /* Extended byte 14 */
66#define R_EB15 0x1e /* Extended byte 15 */
67#define R_EB16 0x1f /* Extended byte 16 */
68#define R_EB17 0x20 /* Extended byte 17 */
69#define R_EB18 0x21 /* Extended byte 18 */
70#define R_EB19 0x22 /* Extended byte 19 */
71#define R_EB20 0x23 /* Extended byte 20 */
72#define R_EB21 0x24 /* Extended byte 21 */
73#define R_EB22 0x25 /* Extended byte 22 */
74#define R_EB23 0x26 /* Extended byte 23 */
75
76struct tda18271_pll_map {
77 u32 lomax;
78 u8 pd; /* post div */
79 u8 d; /* div */
80};
81
82static struct tda18271_pll_map tda18271_main_pll[] = {
83 { .lomax = 32000, .pd = 0x5f, .d = 0xf0 },
84 { .lomax = 35000, .pd = 0x5e, .d = 0xe0 },
85 { .lomax = 37000, .pd = 0x5d, .d = 0xd0 },
86 { .lomax = 41000, .pd = 0x5c, .d = 0xc0 },
87 { .lomax = 44000, .pd = 0x5b, .d = 0xb0 },
88 { .lomax = 49000, .pd = 0x5a, .d = 0xa0 },
89 { .lomax = 54000, .pd = 0x59, .d = 0x90 },
90 { .lomax = 61000, .pd = 0x58, .d = 0x80 },
91 { .lomax = 65000, .pd = 0x4f, .d = 0x78 },
92 { .lomax = 70000, .pd = 0x4e, .d = 0x70 },
93 { .lomax = 75000, .pd = 0x4d, .d = 0x68 },
94 { .lomax = 82000, .pd = 0x4c, .d = 0x60 },
95 { .lomax = 89000, .pd = 0x4b, .d = 0x58 },
96 { .lomax = 98000, .pd = 0x4a, .d = 0x50 },
97 { .lomax = 109000, .pd = 0x49, .d = 0x48 },
98 { .lomax = 123000, .pd = 0x48, .d = 0x40 },
99 { .lomax = 131000, .pd = 0x3f, .d = 0x3c },
100 { .lomax = 141000, .pd = 0x3e, .d = 0x38 },
101 { .lomax = 151000, .pd = 0x3d, .d = 0x34 },
102 { .lomax = 164000, .pd = 0x3c, .d = 0x30 },
103 { .lomax = 179000, .pd = 0x3b, .d = 0x2c },
104 { .lomax = 197000, .pd = 0x3a, .d = 0x28 },
105 { .lomax = 219000, .pd = 0x39, .d = 0x24 },
106 { .lomax = 246000, .pd = 0x38, .d = 0x20 },
107 { .lomax = 263000, .pd = 0x2f, .d = 0x1e },
108 { .lomax = 282000, .pd = 0x2e, .d = 0x1c },
109 { .lomax = 303000, .pd = 0x2d, .d = 0x1a },
110 { .lomax = 329000, .pd = 0x2c, .d = 0x18 },
111 { .lomax = 359000, .pd = 0x2b, .d = 0x16 },
112 { .lomax = 395000, .pd = 0x2a, .d = 0x14 },
113 { .lomax = 438000, .pd = 0x29, .d = 0x12 },
114 { .lomax = 493000, .pd = 0x28, .d = 0x10 },
115 { .lomax = 526000, .pd = 0x1f, .d = 0x0f },
116 { .lomax = 564000, .pd = 0x1e, .d = 0x0e },
117 { .lomax = 607000, .pd = 0x1d, .d = 0x0d },
118 { .lomax = 658000, .pd = 0x1c, .d = 0x0c },
119 { .lomax = 718000, .pd = 0x1b, .d = 0x0b },
120 { .lomax = 790000, .pd = 0x1a, .d = 0x0a },
121 { .lomax = 877000, .pd = 0x19, .d = 0x09 },
122 { .lomax = 987000, .pd = 0x18, .d = 0x08 },
123 { .lomax = 0, .pd = 0x00, .d = 0x00 }, /* end */
124};
125
126static struct tda18271_pll_map tda18271_cal_pll[] = {
127 { .lomax = 33000, .pd = 0xdd, .d = 0xd0 },
128 { .lomax = 36000, .pd = 0xdc, .d = 0xc0 },
129 { .lomax = 40000, .pd = 0xdb, .d = 0xb0 },
130 { .lomax = 44000, .pd = 0xda, .d = 0xa0 },
131 { .lomax = 49000, .pd = 0xd9, .d = 0x90 },
132 { .lomax = 55000, .pd = 0xd8, .d = 0x80 },
133 { .lomax = 63000, .pd = 0xd3, .d = 0x70 },
134 { .lomax = 67000, .pd = 0xcd, .d = 0x68 },
135 { .lomax = 73000, .pd = 0xcc, .d = 0x60 },
136 { .lomax = 80000, .pd = 0xcb, .d = 0x58 },
137 { .lomax = 88000, .pd = 0xca, .d = 0x50 },
138 { .lomax = 98000, .pd = 0xc9, .d = 0x48 },
139 { .lomax = 110000, .pd = 0xc8, .d = 0x40 },
140 { .lomax = 126000, .pd = 0xc3, .d = 0x38 },
141 { .lomax = 135000, .pd = 0xbd, .d = 0x34 },
142 { .lomax = 147000, .pd = 0xbc, .d = 0x30 },
143 { .lomax = 160000, .pd = 0xbb, .d = 0x2c },
144 { .lomax = 176000, .pd = 0xba, .d = 0x28 },
145 { .lomax = 196000, .pd = 0xb9, .d = 0x24 },
146 { .lomax = 220000, .pd = 0xb8, .d = 0x20 },
147 { .lomax = 252000, .pd = 0xb3, .d = 0x1c },
148 { .lomax = 271000, .pd = 0xad, .d = 0x1a },
149 { .lomax = 294000, .pd = 0xac, .d = 0x18 },
150 { .lomax = 321000, .pd = 0xab, .d = 0x16 },
151 { .lomax = 353000, .pd = 0xaa, .d = 0x14 },
152 { .lomax = 392000, .pd = 0xa9, .d = 0x12 },
153 { .lomax = 441000, .pd = 0xa8, .d = 0x10 },
154 { .lomax = 505000, .pd = 0xa3, .d = 0x0e },
155 { .lomax = 543000, .pd = 0x9d, .d = 0x0d },
156 { .lomax = 589000, .pd = 0x9c, .d = 0x0c },
157 { .lomax = 642000, .pd = 0x9b, .d = 0x0b },
158 { .lomax = 707000, .pd = 0x9a, .d = 0x0a },
159 { .lomax = 785000, .pd = 0x99, .d = 0x09 },
160 { .lomax = 883000, .pd = 0x98, .d = 0x08 },
161 { .lomax = 1010000, .pd = 0x93, .d = 0x07 },
162 { .lomax = 0, .pd = 0x00, .d = 0x00 }, /* end */
163};
164
165struct tda18271_map {
166 u32 rfmax;
167 u8 val;
168};
169
170static struct tda18271_map tda18271_bp_filter[] = {
171 { .rfmax = 62000, .val = 0x00 },
172 { .rfmax = 84000, .val = 0x01 },
173 { .rfmax = 100000, .val = 0x02 },
174 { .rfmax = 140000, .val = 0x03 },
175 { .rfmax = 170000, .val = 0x04 },
176 { .rfmax = 180000, .val = 0x05 },
177 { .rfmax = 865000, .val = 0x06 },
178 { .rfmax = 0, .val = 0x00 }, /* end */
179};
180
181static struct tda18271_map tda18271_km[] = {
182 { .rfmax = 61100, .val = 0x74 },
183 { .rfmax = 350000, .val = 0x40 },
184 { .rfmax = 720000, .val = 0x30 },
185 { .rfmax = 865000, .val = 0x40 },
186 { .rfmax = 0, .val = 0x00 }, /* end */
187};
188
189static struct tda18271_map tda18271_rf_band[] = {
190 { .rfmax = 47900, .val = 0x00 },
191 { .rfmax = 61100, .val = 0x01 },
192/* { .rfmax = 152600, .val = 0x02 }, */
193 { .rfmax = 121200, .val = 0x02 },
194 { .rfmax = 164700, .val = 0x03 },
195 { .rfmax = 203500, .val = 0x04 },
196 { .rfmax = 457800, .val = 0x05 },
197 { .rfmax = 865000, .val = 0x06 },
198 { .rfmax = 0, .val = 0x00 }, /* end */
199};
200
201static struct tda18271_map tda18271_gain_taper[] = {
202 { .rfmax = 45400, .val = 0x1f },
203 { .rfmax = 45800, .val = 0x1e },
204 { .rfmax = 46200, .val = 0x1d },
205 { .rfmax = 46700, .val = 0x1c },
206 { .rfmax = 47100, .val = 0x1b },
207 { .rfmax = 47500, .val = 0x1a },
208 { .rfmax = 47900, .val = 0x19 },
209 { .rfmax = 49600, .val = 0x17 },
210 { .rfmax = 51200, .val = 0x16 },
211 { .rfmax = 52900, .val = 0x15 },
212 { .rfmax = 54500, .val = 0x14 },
213 { .rfmax = 56200, .val = 0x13 },
214 { .rfmax = 57800, .val = 0x12 },
215 { .rfmax = 59500, .val = 0x11 },
216 { .rfmax = 61100, .val = 0x10 },
217 { .rfmax = 67600, .val = 0x0d },
218 { .rfmax = 74200, .val = 0x0c },
219 { .rfmax = 80700, .val = 0x0b },
220 { .rfmax = 87200, .val = 0x0a },
221 { .rfmax = 93800, .val = 0x09 },
222 { .rfmax = 100300, .val = 0x08 },
223 { .rfmax = 106900, .val = 0x07 },
224 { .rfmax = 113400, .val = 0x06 },
225 { .rfmax = 119900, .val = 0x05 },
226 { .rfmax = 126500, .val = 0x04 },
227 { .rfmax = 133000, .val = 0x03 },
228 { .rfmax = 139500, .val = 0x02 },
229 { .rfmax = 146100, .val = 0x01 },
230 { .rfmax = 152600, .val = 0x00 },
231 { .rfmax = 154300, .val = 0x1f },
232 { .rfmax = 156100, .val = 0x1e },
233 { .rfmax = 157800, .val = 0x1d },
234 { .rfmax = 159500, .val = 0x1c },
235 { .rfmax = 161200, .val = 0x1b },
236 { .rfmax = 163000, .val = 0x1a },
237 { .rfmax = 164700, .val = 0x19 },
238 { .rfmax = 170200, .val = 0x17 },
239 { .rfmax = 175800, .val = 0x16 },
240 { .rfmax = 181300, .val = 0x15 },
241 { .rfmax = 186900, .val = 0x14 },
242 { .rfmax = 192400, .val = 0x13 },
243 { .rfmax = 198000, .val = 0x12 },
244 { .rfmax = 203500, .val = 0x11 },
245 { .rfmax = 216200, .val = 0x14 },
246 { .rfmax = 228900, .val = 0x13 },
247 { .rfmax = 241600, .val = 0x12 },
248 { .rfmax = 254400, .val = 0x11 },
249 { .rfmax = 267100, .val = 0x10 },
250 { .rfmax = 279800, .val = 0x0f },
251 { .rfmax = 292500, .val = 0x0e },
252 { .rfmax = 305200, .val = 0x0d },
253 { .rfmax = 317900, .val = 0x0c },
254 { .rfmax = 330700, .val = 0x0b },
255 { .rfmax = 343400, .val = 0x0a },
256 { .rfmax = 356100, .val = 0x09 },
257 { .rfmax = 368800, .val = 0x08 },
258 { .rfmax = 381500, .val = 0x07 },
259 { .rfmax = 394200, .val = 0x06 },
260 { .rfmax = 406900, .val = 0x05 },
261 { .rfmax = 419700, .val = 0x04 },
262 { .rfmax = 432400, .val = 0x03 },
263 { .rfmax = 445100, .val = 0x02 },
264 { .rfmax = 457800, .val = 0x01 },
265 { .rfmax = 476300, .val = 0x19 },
266 { .rfmax = 494800, .val = 0x18 },
267 { .rfmax = 513300, .val = 0x17 },
268 { .rfmax = 531800, .val = 0x16 },
269 { .rfmax = 550300, .val = 0x15 },
270 { .rfmax = 568900, .val = 0x14 },
271 { .rfmax = 587400, .val = 0x13 },
272 { .rfmax = 605900, .val = 0x12 },
273 { .rfmax = 624400, .val = 0x11 },
274 { .rfmax = 642900, .val = 0x10 },
275 { .rfmax = 661400, .val = 0x0f },
276 { .rfmax = 679900, .val = 0x0e },
277 { .rfmax = 698400, .val = 0x0d },
278 { .rfmax = 716900, .val = 0x0c },
279 { .rfmax = 735400, .val = 0x0b },
280 { .rfmax = 753900, .val = 0x0a },
281 { .rfmax = 772500, .val = 0x09 },
282 { .rfmax = 791000, .val = 0x08 },
283 { .rfmax = 809500, .val = 0x07 },
284 { .rfmax = 828000, .val = 0x06 },
285 { .rfmax = 846500, .val = 0x05 },
286 { .rfmax = 865000, .val = 0x04 },
287 { .rfmax = 0, .val = 0x00 }, /* end */
288};
289
290static struct tda18271_map tda18271_rf_cal[] = {
291 { .rfmax = 41000, .val = 0x1e },
292 { .rfmax = 43000, .val = 0x30 },
293 { .rfmax = 45000, .val = 0x43 },
294 { .rfmax = 46000, .val = 0x4d },
295 { .rfmax = 47000, .val = 0x54 },
296 { .rfmax = 47900, .val = 0x64 },
297 { .rfmax = 49100, .val = 0x20 },
298 { .rfmax = 50000, .val = 0x22 },
299 { .rfmax = 51000, .val = 0x2a },
300 { .rfmax = 53000, .val = 0x32 },
301 { .rfmax = 55000, .val = 0x35 },
302 { .rfmax = 56000, .val = 0x3c },
303 { .rfmax = 57000, .val = 0x3f },
304 { .rfmax = 58000, .val = 0x48 },
305 { .rfmax = 59000, .val = 0x4d },
306 { .rfmax = 60000, .val = 0x58 },
307 { .rfmax = 61100, .val = 0x5f },
308 { .rfmax = 0, .val = 0x00 }, /* end */
309};
310
311/*---------------------------------------------------------------------*/
312
313#define TDA18271_NUM_REGS 39
314
315#define TDA18271_ANALOG 0
316#define TDA18271_DIGITAL 1
317
318struct tda18271_priv {
319 u8 i2c_addr;
320 struct i2c_adapter *i2c_adap;
321 unsigned char tda18271_regs[TDA18271_NUM_REGS];
322 int mode;
323
324 u32 frequency;
325 u32 bandwidth;
326};
327
Michael Krufky7d11c532007-10-24 09:55:54 -0300328static int tda18271_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
329{
330 struct tda18271_priv *priv = fe->tuner_priv;
331 struct analog_tuner_ops *ops = fe->ops.analog_demod_ops;
332 int ret = 0;
333
334 switch (priv->mode) {
335 case TDA18271_ANALOG:
336 if (ops && ops->i2c_gate_ctrl)
337 ret = ops->i2c_gate_ctrl(fe, enable);
338 break;
339 case TDA18271_DIGITAL:
340 if (fe->ops.i2c_gate_ctrl)
341 ret = fe->ops.i2c_gate_ctrl(fe, enable);
342 break;
343 }
344
345 return ret;
346};
347
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300348/*---------------------------------------------------------------------*/
349
350static void tda18271_dump_regs(struct dvb_frontend *fe)
351{
352 struct tda18271_priv *priv = fe->tuner_priv;
353 unsigned char *regs = priv->tda18271_regs;
354
Michael Krufky6f998742007-10-24 01:00:24 -0300355 dprintk(1, "=== TDA18271 REG DUMP ===\n");
356 dprintk(1, "ID_BYTE = 0x%x\n", 0xff & regs[R_ID]);
357 dprintk(1, "THERMO_BYTE = 0x%x\n", 0xff & regs[R_TM]);
358 dprintk(1, "POWER_LEVEL_BYTE = 0x%x\n", 0xff & regs[R_PL]);
359 dprintk(1, "EASY_PROG_BYTE_1 = 0x%x\n", 0xff & regs[R_EP1]);
360 dprintk(1, "EASY_PROG_BYTE_2 = 0x%x\n", 0xff & regs[R_EP2]);
361 dprintk(1, "EASY_PROG_BYTE_3 = 0x%x\n", 0xff & regs[R_EP3]);
362 dprintk(1, "EASY_PROG_BYTE_4 = 0x%x\n", 0xff & regs[R_EP4]);
363 dprintk(1, "EASY_PROG_BYTE_5 = 0x%x\n", 0xff & regs[R_EP5]);
364 dprintk(1, "CAL_POST_DIV_BYTE = 0x%x\n", 0xff & regs[R_CPD]);
365 dprintk(1, "CAL_DIV_BYTE_1 = 0x%x\n", 0xff & regs[R_CD1]);
366 dprintk(1, "CAL_DIV_BYTE_2 = 0x%x\n", 0xff & regs[R_CD2]);
367 dprintk(1, "CAL_DIV_BYTE_3 = 0x%x\n", 0xff & regs[R_CD3]);
368 dprintk(1, "MAIN_POST_DIV_BYTE = 0x%x\n", 0xff & regs[R_MPD]);
369 dprintk(1, "MAIN_DIV_BYTE_1 = 0x%x\n", 0xff & regs[R_MD1]);
370 dprintk(1, "MAIN_DIV_BYTE_2 = 0x%x\n", 0xff & regs[R_MD2]);
371 dprintk(1, "MAIN_DIV_BYTE_3 = 0x%x\n", 0xff & regs[R_MD3]);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300372}
373
374static void tda18271_read_regs(struct dvb_frontend *fe)
375{
376 struct tda18271_priv *priv = fe->tuner_priv;
377 unsigned char *regs = priv->tda18271_regs;
378 unsigned char buf = 0x00;
379 int ret;
380 struct i2c_msg msg[] = {
381 { .addr = priv->i2c_addr, .flags = 0,
382 .buf = &buf, .len = 1 },
383 { .addr = priv->i2c_addr, .flags = I2C_M_RD,
384 .buf = regs, .len = 16 }
385 };
386
Michael Krufky7d11c532007-10-24 09:55:54 -0300387 tda18271_i2c_gate_ctrl(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300388
389 /* read all registers */
390 ret = i2c_transfer(priv->i2c_adap, msg, 2);
391
Michael Krufky7d11c532007-10-24 09:55:54 -0300392 tda18271_i2c_gate_ctrl(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300393
394 if (ret != 2)
395 printk("ERROR: %s: i2c_transfer returned: %d\n",
396 __FUNCTION__, ret);
397
398 if (debug > 1)
399 tda18271_dump_regs(fe);
400}
401
402static void tda18271_write_regs(struct dvb_frontend *fe, int idx, int len)
403{
404 struct tda18271_priv *priv = fe->tuner_priv;
405 unsigned char *regs = priv->tda18271_regs;
406 unsigned char buf[TDA18271_NUM_REGS+1];
407 struct i2c_msg msg = { .addr = priv->i2c_addr, .flags = 0,
408 .buf = buf, .len = len+1 };
409 int i, ret;
410
411 BUG_ON((len == 0) || (idx+len > sizeof(buf)));
412
413 buf[0] = idx;
414 for (i = 1; i <= len; i++) {
415 buf[i] = regs[idx-1+i];
416 }
417
Michael Krufky7d11c532007-10-24 09:55:54 -0300418 tda18271_i2c_gate_ctrl(fe, 1);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300419
420 /* write registers */
421 ret = i2c_transfer(priv->i2c_adap, &msg, 1);
422
Michael Krufky7d11c532007-10-24 09:55:54 -0300423 tda18271_i2c_gate_ctrl(fe, 0);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300424
425 if (ret != 1)
426 printk(KERN_WARNING "ERROR: %s: i2c_transfer returned: %d\n",
427 __FUNCTION__, ret);
428}
429
430/*---------------------------------------------------------------------*/
431
432static void tda18271_init_regs(struct dvb_frontend *fe)
433{
434 struct tda18271_priv *priv = fe->tuner_priv;
435 unsigned char *regs = priv->tda18271_regs;
436
437 tda18271_read_regs(fe);
438
439 /* test IR_CAL_OK to see if we need init */
440 if ((regs[R_EP1] & 0x08) != 0)
441 return;
442
443 printk(KERN_INFO "tda18271: initializing registers\n");
444
445 /* initialize registers */
446 regs[R_ID] = 0x83;
447 regs[R_TM] = 0x08;
448 regs[R_PL] = 0x80;
449 regs[R_EP1] = 0xc6;
450 regs[R_EP2] = 0xdf;
451 regs[R_EP3] = 0x16;
452 regs[R_EP4] = 0x60;
453 regs[R_EP5] = 0x80;
454 regs[R_CPD] = 0x80;
455 regs[R_CD1] = 0x00;
456 regs[R_CD2] = 0x00;
457 regs[R_CD3] = 0x00;
458 regs[R_MPD] = 0x00;
459 regs[R_MD1] = 0x00;
460 regs[R_MD2] = 0x00;
461 regs[R_MD3] = 0x00;
462 regs[R_EB1] = 0xff;
463 regs[R_EB2] = 0x01;
464 regs[R_EB3] = 0x84;
465 regs[R_EB4] = 0x41;
466 regs[R_EB5] = 0x01;
467 regs[R_EB6] = 0x84;
468 regs[R_EB7] = 0x40;
469 regs[R_EB8] = 0x07;
470 regs[R_EB9] = 0x00;
471 regs[R_EB10] = 0x00;
472 regs[R_EB11] = 0x96;
473 regs[R_EB12] = 0x0f;
474 regs[R_EB13] = 0xc1;
475 regs[R_EB14] = 0x00;
476 regs[R_EB15] = 0x8f;
477 regs[R_EB16] = 0x00;
478 regs[R_EB17] = 0x00;
479 regs[R_EB18] = 0x00;
480 regs[R_EB19] = 0x00;
481 regs[R_EB20] = 0x20;
482 regs[R_EB21] = 0x33;
483 regs[R_EB22] = 0x48;
484 regs[R_EB23] = 0xb0;
485
486 tda18271_write_regs(fe, 0x00, TDA18271_NUM_REGS);
487 /* setup AGC1 & AGC2 */
488 regs[R_EB17] = 0x00;
489 tda18271_write_regs(fe, R_EB17, 1);
490 regs[R_EB17] = 0x03;
491 tda18271_write_regs(fe, R_EB17, 1);
492 regs[R_EB17] = 0x43;
493 tda18271_write_regs(fe, R_EB17, 1);
494 regs[R_EB17] = 0x4c;
495 tda18271_write_regs(fe, R_EB17, 1);
496
497 regs[R_EB20] = 0xa0;
498 tda18271_write_regs(fe, R_EB20, 1);
499 regs[R_EB20] = 0xa7;
500 tda18271_write_regs(fe, R_EB20, 1);
501 regs[R_EB20] = 0xe7;
502 tda18271_write_regs(fe, R_EB20, 1);
503 regs[R_EB20] = 0xec;
504 tda18271_write_regs(fe, R_EB20, 1);
505
506 /* image rejection calibration */
507
508 /* low-band */
509 regs[R_EP3] = 0x1f;
510 regs[R_EP4] = 0x66;
511 regs[R_EP5] = 0x81;
512 regs[R_CPD] = 0xcc;
513 regs[R_CD1] = 0x6c;
514 regs[R_CD2] = 0x00;
515 regs[R_CD3] = 0x00;
516 regs[R_MPD] = 0xcd;
517 regs[R_MD1] = 0x77;
518 regs[R_MD2] = 0x08;
519 regs[R_MD3] = 0x00;
520
521 tda18271_write_regs(fe, R_EP3, 11);
522 msleep(5); /* pll locking */
523
524 regs[R_EP1] = 0xc6;
525 tda18271_write_regs(fe, R_EP1, 1);
526 msleep(5); /* wanted low measurement */
527
528 regs[R_EP3] = 0x1f;
529 regs[R_EP4] = 0x66;
530 regs[R_EP5] = 0x85;
531 regs[R_CPD] = 0xcb;
532 regs[R_CD1] = 0x66;
533 regs[R_CD2] = 0x70;
534 regs[R_CD3] = 0x00;
535
536 tda18271_write_regs(fe, R_EP3, 7);
537 msleep(5); /* pll locking */
538
539 regs[R_EP2] = 0xdf;
540 tda18271_write_regs(fe, R_EP2, 1);
541 msleep(30); /* image low optimization completion */
542
543 /* mid-band */
544 regs[R_EP3] = 0x1f;
545 regs[R_EP4] = 0x66;
546 regs[R_EP5] = 0x82;
547 regs[R_CPD] = 0xa8;
548 regs[R_CD1] = 0x66;
549 regs[R_CD2] = 0x00;
550 regs[R_CD3] = 0x00;
551 regs[R_MPD] = 0xa9;
552 regs[R_MD1] = 0x73;
553 regs[R_MD2] = 0x1a;
554 regs[R_MD3] = 0x00;
555
556 tda18271_write_regs(fe, R_EP3, 11);
557 msleep(5); /* pll locking */
558
559 regs[R_EP1] = 0xc6;
560 tda18271_write_regs(fe, R_EP1, 1);
561 msleep(5); /* wanted mid measurement */
562
563 regs[R_EP3] = 0x1f;
564 regs[R_EP4] = 0x66;
565 regs[R_EP5] = 0x86;
566 regs[R_CPD] = 0xa8;
567 regs[R_CD1] = 0x66;
568 regs[R_CD2] = 0xa0;
569 regs[R_CD3] = 0x00;
570
571 tda18271_write_regs(fe, R_EP3, 7);
572 msleep(5); /* pll locking */
573
574 regs[R_EP2] = 0xdf;
575 tda18271_write_regs(fe, R_EP2, 1);
576 msleep(30); /* image mid optimization completion */
577
578 /* high-band */
579 regs[R_EP3] = 0x1f;
580 regs[R_EP4] = 0x66;
581 regs[R_EP5] = 0x83;
582 regs[R_CPD] = 0x98;
583 regs[R_CD1] = 0x65;
584 regs[R_CD2] = 0x00;
585 regs[R_CD3] = 0x00;
586 regs[R_MPD] = 0x99;
587 regs[R_MD1] = 0x71;
588 regs[R_MD2] = 0xcd;
589 regs[R_MD3] = 0x00;
590
591 tda18271_write_regs(fe, R_EP3, 11);
592 msleep(5); /* pll locking */
593
594 regs[R_EP1] = 0xc6;
595 tda18271_write_regs(fe, R_EP1, 1);
596 msleep(5); /* wanted high measurement */
597
598 regs[R_EP3] = 0x1f;
599 regs[R_EP4] = 0x66;
600 regs[R_EP5] = 0x87;
601 regs[R_CPD] = 0x98;
602 regs[R_CD1] = 0x65;
603 regs[R_CD2] = 0x50;
604 regs[R_CD3] = 0x00;
605
606 tda18271_write_regs(fe, R_EP3, 7);
607 msleep(5); /* pll locking */
608
609 regs[R_EP2] = 0xdf;
610
611 tda18271_write_regs(fe, R_EP2, 1);
612 msleep(30); /* image high optimization completion */
613
614 regs[R_EP4] = 0x64;
615 tda18271_write_regs(fe, R_EP4, 1);
616
617 regs[R_EP1] = 0xc6;
618 tda18271_write_regs(fe, R_EP1, 1);
619}
620
621static int tda18271_tune(struct dvb_frontend *fe,
622 u32 ifc, u32 freq, u32 bw, u8 std)
623{
624 struct tda18271_priv *priv = fe->tuner_priv;
625 unsigned char *regs = priv->tda18271_regs;
626 u32 div, N = 0;
627 int i;
628
629
Michael Krufky6f998742007-10-24 01:00:24 -0300630 dprintk(1, "freq = %d, ifc = %d\n", freq, ifc);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300631
632 tda18271_init_regs(fe);
633 /* RF tracking filter calibration */
634
635 /* calculate BP_Filter */
636 i = 0;
637 while ((tda18271_bp_filter[i].rfmax * 1000) < freq) {
638 if (tda18271_bp_filter[i + 1].rfmax == 0)
639 break;
640 i++;
641 }
Michael Krufky6f998742007-10-24 01:00:24 -0300642 dprintk(2, "bp filter = 0x%x, i = %d\n", tda18271_bp_filter[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300643
644 regs[R_EP1] &= ~0x07; /* clear bp filter bits */
645 regs[R_EP1] |= tda18271_bp_filter[i].val;
646 tda18271_write_regs(fe, R_EP1, 1);
647
648 regs[R_EB4] &= 0x07;
649 regs[R_EB4] |= 0x60;
650 tda18271_write_regs(fe, R_EB4, 1);
651
652 regs[R_EB7] = 0x60;
653 tda18271_write_regs(fe, R_EB7, 1);
654
655 regs[R_EB14] = 0x00;
656 tda18271_write_regs(fe, R_EB14, 1);
657
658 regs[R_EB20] = 0xcc;
659 tda18271_write_regs(fe, R_EB20, 1);
660
661 /* set CAL mode to RF tracking filter calibration */
662 regs[R_EB4] |= 0x03;
663
664 /* calculate CAL PLL */
665
666 switch (priv->mode) {
667 case TDA18271_ANALOG:
668 N = freq - 1250000;
669 break;
670 case TDA18271_DIGITAL:
671 N = freq + bw / 2;
672 break;
673 }
674
675 i = 0;
676 while ((tda18271_cal_pll[i].lomax * 1000) < N) {
677 if (tda18271_cal_pll[i + 1].lomax == 0)
678 break;
679 i++;
680 }
Michael Krufky6f998742007-10-24 01:00:24 -0300681 dprintk(2, "cal pll, pd = 0x%x, d = 0x%x, i = %d\n",
682 tda18271_cal_pll[i].pd, tda18271_cal_pll[i].d, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300683
684 regs[R_CPD] = tda18271_cal_pll[i].pd;
685
686 div = ((tda18271_cal_pll[i].d * (N / 1000)) << 7) / 125;
687 regs[R_CD1] = 0xff & (div >> 16);
688 regs[R_CD2] = 0xff & (div >> 8);
689 regs[R_CD3] = 0xff & div;
690
691 /* calculate MAIN PLL */
692
693 switch (priv->mode) {
694 case TDA18271_ANALOG:
695 N = freq - 250000;
696 break;
697 case TDA18271_DIGITAL:
698 N = freq + bw / 2 + 1000000;
699 break;
700 }
701
702 i = 0;
703 while ((tda18271_main_pll[i].lomax * 1000) < N) {
704 if (tda18271_main_pll[i + 1].lomax == 0)
705 break;
706 i++;
707 }
Michael Krufky6f998742007-10-24 01:00:24 -0300708 dprintk(2, "main pll, pd = 0x%x, d = 0x%x, i = %d\n",
709 tda18271_main_pll[i].pd, tda18271_main_pll[i].d, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300710
711 regs[R_MPD] = (0x7f & tda18271_main_pll[i].pd);
712
713 switch (priv->mode) {
714 case TDA18271_ANALOG:
715 regs[R_MPD] &= ~0x08;
716 break;
717 case TDA18271_DIGITAL:
718 regs[R_MPD] |= 0x08;
719 break;
720 }
721
722 div = ((tda18271_main_pll[i].d * (N / 1000)) << 7) / 125;
723 regs[R_MD1] = 0xff & (div >> 16);
724 regs[R_MD2] = 0xff & (div >> 8);
725 regs[R_MD3] = 0xff & div;
726
727 tda18271_write_regs(fe, R_EP3, 11);
728 msleep(5); /* RF tracking filter calibration initialization */
729
730 /* search for K,M,CO for RF Calibration */
731 i = 0;
732 while ((tda18271_km[i].rfmax * 1000) < freq) {
733 if (tda18271_km[i + 1].rfmax == 0)
734 break;
735 i++;
736 }
Michael Krufky6f998742007-10-24 01:00:24 -0300737 dprintk(2, "km = 0x%x, i = %d\n", tda18271_km[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300738
739 regs[R_EB13] &= 0x83;
740 regs[R_EB13] |= tda18271_km[i].val;
741 tda18271_write_regs(fe, R_EB13, 1);
742
743 /* search for RF_BAND */
744 i = 0;
745 while ((tda18271_rf_band[i].rfmax * 1000) < freq) {
746 if (tda18271_rf_band[i + 1].rfmax == 0)
747 break;
748 i++;
749 }
Michael Krufky6f998742007-10-24 01:00:24 -0300750 dprintk(2, "rf band = 0x%x, i = %d\n", tda18271_rf_band[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300751
752 regs[R_EP2] &= ~0xe0; /* clear rf band bits */
753 regs[R_EP2] |= (tda18271_rf_band[i].val << 5);
754
755 /* search for Gain_Taper */
756 i = 0;
757 while ((tda18271_gain_taper[i].rfmax * 1000) < freq) {
758 if (tda18271_gain_taper[i + 1].rfmax == 0)
759 break;
760 i++;
761 }
Michael Krufky6f998742007-10-24 01:00:24 -0300762 dprintk(2, "gain taper = 0x%x, i = %d\n",
763 tda18271_gain_taper[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300764
765 regs[R_EP2] &= ~0x1f; /* clear gain taper bits */
766 regs[R_EP2] |= tda18271_gain_taper[i].val;
767
768 tda18271_write_regs(fe, R_EP2, 1);
769 tda18271_write_regs(fe, R_EP1, 1);
770 tda18271_write_regs(fe, R_EP2, 1);
771 tda18271_write_regs(fe, R_EP1, 1);
772
773 regs[R_EB4] &= 0x07;
774 regs[R_EB4] |= 0x40;
775 tda18271_write_regs(fe, R_EB4, 1);
776
777 regs[R_EB7] = 0x40;
778 tda18271_write_regs(fe, R_EB7, 1);
779 msleep(10);
780
781 regs[R_EB20] = 0xec;
782 tda18271_write_regs(fe, R_EB20, 1);
783 msleep(60); /* RF tracking filter calibration completion */
784
785 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
786 tda18271_write_regs(fe, R_EP4, 1);
787
788 tda18271_write_regs(fe, R_EP1, 1);
789
790 /* RF tracking filer correction for VHF_Low band */
791 i = 0;
792 while ((tda18271_rf_cal[i].rfmax * 1000) < freq) {
793 if (tda18271_rf_cal[i].rfmax == 0)
794 break;
795 i++;
796 }
Michael Krufky6f998742007-10-24 01:00:24 -0300797 dprintk(2, "rf cal = 0x%x, i = %d\n", tda18271_rf_cal[i].val, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300798
799 /* VHF_Low band only */
800 if (tda18271_rf_cal[i].rfmax != 0) {
801 regs[R_EB14] = tda18271_rf_cal[i].val;
802 tda18271_write_regs(fe, R_EB14, 1);
803 }
804
805 /* Channel Configuration */
806
807 switch (priv->mode) {
808 case TDA18271_ANALOG:
809 regs[R_EB22] = 0x2c;
810 break;
811 case TDA18271_DIGITAL:
812 regs[R_EB22] = 0x37;
813 break;
814 }
815 tda18271_write_regs(fe, R_EB22, 1);
816
817 regs[R_EP1] |= 0x40; /* set dis power level on */
818
819 /* set standard */
820 regs[R_EP3] &= ~0x1f; /* clear std bits */
821
822 /* see table 22 */
823 regs[R_EP3] |= std;
824
825 /* TO DO: *
826 * ================ *
827 * FM radio, 0x18 *
828 * ATSC 6MHz, 0x1c *
829 * DVB-T 6MHz, 0x1c *
830 * DVB-T 7MHz, 0x1d *
831 * DVB-T 8MHz, 0x1e *
832 * QAM 6MHz, 0x1d *
833 * QAM 8MHz, 0x1f */
834
835 regs[R_EP4] &= ~0x03; /* set cal mode to normal */
836
837 regs[R_EP4] &= ~0x1c; /* clear if level bits */
838 switch (priv->mode) {
839 case TDA18271_ANALOG:
840 regs[R_MPD] &= ~0x80; /* IF notch = 0 */
841 break;
842 case TDA18271_DIGITAL:
843 regs[R_EP4] |= 0x04;
844 regs[R_MPD] |= 0x80;
845 break;
846 }
847
848 regs[R_EP4] &= ~0x80; /* turn this bit on only for fm */
849
850 /* FIXME: image rejection validity EP5[2:0] */
851
852 /* calculate MAIN PLL */
853 N = freq + ifc;
854
855 i = 0;
856 while ((tda18271_main_pll[i].lomax * 1000) < N) {
857 if (tda18271_main_pll[i + 1].lomax == 0)
858 break;
859 i++;
860 }
Michael Krufky6f998742007-10-24 01:00:24 -0300861 dprintk(2, "main pll, pd = 0x%x, d = 0x%x, i = %d\n",
862 tda18271_main_pll[i].pd, tda18271_main_pll[i].d, i);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300863
864 regs[R_MPD] = (0x7f & tda18271_main_pll[i].pd);
865 switch (priv->mode) {
866 case TDA18271_ANALOG:
867 regs[R_MPD] &= ~0x08;
868 break;
869 case TDA18271_DIGITAL:
870 regs[R_MPD] |= 0x08;
871 break;
872 }
873
874 div = ((tda18271_main_pll[i].d * (N / 1000)) << 7) / 125;
875 regs[R_MD1] = 0xff & (div >> 16);
876 regs[R_MD2] = 0xff & (div >> 8);
877 regs[R_MD3] = 0xff & div;
878
879 tda18271_write_regs(fe, R_TM, 15);
880 msleep(5);
881 return 0;
882}
883
884/* ------------------------------------------------------------------ */
885
886static int tda18271_set_params(struct dvb_frontend *fe,
887 struct dvb_frontend_parameters *params)
888{
889 struct tda18271_priv *priv = fe->tuner_priv;
890 u8 std;
891 u32 bw, sgIF = 0;
892
893 u32 freq = params->frequency;
894
895 priv->mode = TDA18271_DIGITAL;
896
897 /* see table 22 */
898 if (fe->ops.info.type == FE_ATSC) {
899 switch (params->u.vsb.modulation) {
900 case VSB_8:
901 case VSB_16:
902 std = 0x1b; /* device-specific (spec says 0x1c) */
903 sgIF = 5380000;
904 break;
905 case QAM_64:
906 case QAM_256:
907 std = 0x18; /* device-specific (spec says 0x1d) */
908 sgIF = 4000000;
909 break;
910 default:
911 printk(KERN_WARNING "%s: modulation not set!\n",
912 __FUNCTION__);
913 return -EINVAL;
914 }
915 freq += 1750000; /* Adjust to center (+1.75MHZ) */
916 bw = 6000000;
917 } else if (fe->ops.info.type == FE_OFDM) {
918 switch (params->u.ofdm.bandwidth) {
919 case BANDWIDTH_6_MHZ:
920 std = 0x1c;
921 bw = 6000000;
922 break;
923 case BANDWIDTH_7_MHZ:
924 std = 0x1d;
925 bw = 7000000;
926 break;
927 case BANDWIDTH_8_MHZ:
928 std = 0x1e;
929 bw = 8000000;
930 break;
931 default:
932 printk(KERN_WARNING "%s: bandwidth not set!\n",
933 __FUNCTION__);
934 return -EINVAL;
935 }
936 } else {
937 printk(KERN_WARNING "%s: modulation type not supported!\n",
938 __FUNCTION__);
939 return -EINVAL;
940 }
941
942 return tda18271_tune(fe, sgIF, freq, bw, std);
943}
944
945static int tda18271_set_analog_params(struct dvb_frontend *fe,
946 struct analog_parameters *params)
947{
948 struct tda18271_priv *priv = fe->tuner_priv;
949 u8 std;
950 unsigned int sgIF;
951 char *mode;
952
953 priv->mode = TDA18271_ANALOG;
954
955 /* see table 22 */
956 if (params->std & V4L2_STD_MN) {
957 std = 0x0d;
958 sgIF = 92;
959 mode = "MN";
960 } else if (params->std & V4L2_STD_B) {
961 std = 0x0e;
962 sgIF = 108;
963 mode = "B";
964 } else if (params->std & V4L2_STD_GH) {
965 std = 0x0f;
966 sgIF = 124;
967 mode = "GH";
968 } else if (params->std & V4L2_STD_PAL_I) {
969 std = 0x0f;
970 sgIF = 124;
971 mode = "I";
972 } else if (params->std & V4L2_STD_DK) {
973 std = 0x0f;
974 sgIF = 124;
975 mode = "DK";
976 } else if (params->std & V4L2_STD_SECAM_L) {
977 std = 0x0f;
978 sgIF = 124;
979 mode = "L";
980 } else if (params->std & V4L2_STD_SECAM_LC) {
981 std = 0x0f;
982 sgIF = 20;
983 mode = "LC";
984 } else {
985 std = 0x0f;
986 sgIF = 124;
987 mode = "xx";
988 }
989
990 if (params->mode == V4L2_TUNER_RADIO)
991 sgIF = 88; /* if frequency is 5.5 MHz */
992
Michael Krufky6f998742007-10-24 01:00:24 -0300993 dprintk(1, "setting tda18271 to system %s\n", mode);
Michael Krufky5bea1cd2007-10-22 09:56:38 -0300994
995 return tda18271_tune(fe, sgIF * 62500, params->frequency * 62500,
996 0, std);
997}
998
999static int tda18271_release(struct dvb_frontend *fe)
1000{
1001 kfree(fe->tuner_priv);
1002 fe->tuner_priv = NULL;
1003 return 0;
1004}
1005
1006static int tda18271_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1007{
1008 struct tda18271_priv *priv = fe->tuner_priv;
1009 *frequency = priv->frequency;
1010 return 0;
1011}
1012
1013static int tda18271_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
1014{
1015 struct tda18271_priv *priv = fe->tuner_priv;
1016 *bandwidth = priv->bandwidth;
1017 return 0;
1018}
1019
1020static struct dvb_tuner_ops tda18271_tuner_ops = {
1021 .info = {
1022 .name = "NXP TDA18271HD",
1023 .frequency_min = 45000000,
1024 .frequency_max = 864000000,
1025 .frequency_step = 62500
1026 },
1027 .set_params = tda18271_set_params,
1028 .set_analog_params = tda18271_set_analog_params,
1029 .release = tda18271_release,
1030 .get_frequency = tda18271_get_frequency,
1031 .get_bandwidth = tda18271_get_bandwidth,
1032};
1033
1034struct dvb_frontend *tda18271_attach(struct dvb_frontend *fe, u8 addr,
1035 struct i2c_adapter *i2c)
1036{
1037 struct tda18271_priv *priv = NULL;
1038
Michael Krufky6f998742007-10-24 01:00:24 -03001039 dprintk(1, "@ 0x%x\n", addr);
Michael Krufky5bea1cd2007-10-22 09:56:38 -03001040 priv = kzalloc(sizeof(struct tda18271_priv), GFP_KERNEL);
1041 if (priv == NULL)
1042 return NULL;
1043
1044 priv->i2c_addr = addr;
1045 priv->i2c_adap = i2c;
1046
1047 memcpy(&fe->ops.tuner_ops, &tda18271_tuner_ops,
1048 sizeof(struct dvb_tuner_ops));
1049
1050 fe->tuner_priv = priv;
1051
1052 return fe;
1053}
1054EXPORT_SYMBOL_GPL(tda18271_attach);
1055MODULE_DESCRIPTION("NXP TDA18271HD analog / digital tuner driver");
1056MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
1057MODULE_LICENSE("GPL");
1058
1059/*
1060 * Overrides for Emacs so that we follow Linus's tabbing style.
1061 * ---------------------------------------------------------------------------
1062 * Local variables:
1063 * c-basic-offset: 8
1064 * End:
1065 */