V4L/DVB (em28xx): Add support for Pinnacle Dazzle DVC 100
[linux-2.6-block.git] / drivers / media / video / tuner-xc2028.c
CommitLineData
6cb45879
MCC
1/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
983d214e 4 *
701672eb
ML
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
983d214e 7 *
6cb45879
MCC
8 * This code is placed under the terms of the GNU General Public License v2
9 */
10
11#include <linux/i2c.h>
12#include <asm/div64.h>
13#include <linux/firmware.h>
ab0b9fc6 14#include <linux/videodev2.h>
6cb45879 15#include <linux/delay.h>
701672eb 16#include <media/tuner.h>
3b20532c 17#include <linux/mutex.h>
215b95ba 18#include "tuner-i2c.h"
6cb45879 19#include "tuner-xc2028.h"
de3fe21b 20#include "tuner-xc2028-types.h"
6cb45879 21
701672eb
ML
22#include <linux/dvb/frontend.h>
23#include "dvb_frontend.h"
24
ef8c1888 25
9dd659de 26#define PREFIX "xc2028"
215b95ba 27
83fb340b
MCC
28static int debug;
29module_param(debug, int, 0644);
30MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
a82200fb
MCC
32static char audio_std[8];
33module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
34MODULE_PARM_DESC(audio_std,
35 "Audio standard. XC3028 audio decoder explicitly "
36 "needs to know what audio\n"
37 "standard is needed for some video standards with audio A2 or NICAM.\n"
38 "The valid values are:\n"
39 "A2\n"
40 "A2/A\n"
41 "A2/B\n"
42 "NICAM\n"
43 "NICAM/A\n"
44 "NICAM/B\n");
45
215b95ba 46static LIST_HEAD(xc2028_list);
aa501be9
CP
47static DEFINE_MUTEX(xc2028_list_mutex);
48
de3fe21b
MCC
49/* struct for storing firmware table */
50struct firmware_description {
51 unsigned int type;
52 v4l2_std_id id;
66c2d53d 53 __u16 int_freq;
de3fe21b
MCC
54 unsigned char *ptr;
55 unsigned int size;
56};
6cb45879 57
e0f0b37a
CP
58struct firmware_properties {
59 unsigned int type;
60 v4l2_std_id id;
61 v4l2_std_id std_req;
66c2d53d 62 __u16 int_freq;
e0f0b37a
CP
63 unsigned int scode_table;
64 int scode_nr;
65};
66
6cb45879 67struct xc2028_data {
215b95ba
MCC
68 struct list_head xc2028_list;
69 struct tuner_i2c_props i2c_props;
70 int (*tuner_callback) (void *dev,
71 int command, int arg);
215b95ba
MCC
72 void *video_dev;
73 int count;
de3fe21b
MCC
74 __u32 frequency;
75
76 struct firmware_description *firm;
77 int firm_size;
06fd82dc 78 __u16 firm_version;
de3fe21b 79
8bf799a6
CP
80 __u16 hwmodel;
81 __u16 hwvers;
82
de3fe21b 83 struct xc2028_ctrl ctrl;
215b95ba 84
e0f0b37a 85 struct firmware_properties cur_fw;
215b95ba
MCC
86
87 struct mutex lock;
6cb45879
MCC
88};
89
47cc5b78
CP
90#define i2c_send(priv, buf, size) ({ \
91 int _rc; \
92 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
93 if (size != _rc) \
94 tuner_info("i2c output error: rc = %d (should be %d)\n",\
95 _rc, (int)size); \
96 _rc; \
97})
98
99#define i2c_rcv(priv, buf, size) ({ \
100 int _rc; \
101 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
102 if (size != _rc) \
83fb340b 103 tuner_err("i2c input error: rc = %d (should be %d)\n", \
47cc5b78
CP
104 _rc, (int)size); \
105 _rc; \
106})
ab0b9fc6 107
7d58d111
CP
108#define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
109 int _rc; \
110 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
111 ibuf, isize); \
112 if (isize != _rc) \
113 tuner_err("i2c input error: rc = %d (should be %d)\n", \
114 _rc, (int)isize); \
115 _rc; \
116})
117
47cc5b78 118#define send_seq(priv, data...) ({ \
215b95ba 119 static u8 _val[] = data; \
47cc5b78 120 int _rc; \
6cb45879 121 if (sizeof(_val) != \
47cc5b78 122 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
215b95ba 123 _val, sizeof(_val)))) { \
47cc5b78
CP
124 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
125 } else \
126 msleep(10); \
127 _rc; \
128})
6cb45879 129
7d58d111 130static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
6cb45879 131{
b873e1a3 132 unsigned char buf[2];
7d58d111 133 unsigned char ibuf[2];
215b95ba 134
7d58d111 135 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
6cb45879 136
7d58d111 137 buf[0] = reg >> 8;
80b52208 138 buf[1] = (unsigned char) reg;
6cb45879 139
7d58d111
CP
140 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
141 return -EIO;
6cb45879 142
7d58d111
CP
143 *val = (ibuf[1]) | (ibuf[0] << 8);
144 return 0;
6cb45879
MCC
145}
146
43efe702
MCC
147void dump_firm_type(unsigned int type)
148{
149 if (type & BASE)
150 printk("BASE ");
f380e1d2
MCC
151 if (type & INIT1)
152 printk("INIT1 ");
43efe702
MCC
153 if (type & F8MHZ)
154 printk("F8MHZ ");
155 if (type & MTS)
156 printk("MTS ");
157 if (type & D2620)
158 printk("D2620 ");
159 if (type & D2633)
160 printk("D2633 ");
161 if (type & DTV6)
162 printk("DTV6 ");
163 if (type & QAM)
164 printk("QAM ");
165 if (type & DTV7)
166 printk("DTV7 ");
167 if (type & DTV78)
168 printk("DTV78 ");
169 if (type & DTV8)
170 printk("DTV8 ");
171 if (type & FM)
172 printk("FM ");
173 if (type & INPUT1)
174 printk("INPUT1 ");
175 if (type & LCD)
176 printk("LCD ");
177 if (type & NOGD)
178 printk("NOGD ");
179 if (type & MONO)
180 printk("MONO ");
181 if (type & ATSC)
182 printk("ATSC ");
183 if (type & IF)
184 printk("IF ");
185 if (type & LG60)
186 printk("LG60 ");
187 if (type & ATI638)
188 printk("ATI638 ");
189 if (type & OREN538)
190 printk("OREN538 ");
191 if (type & OREN36)
192 printk("OREN36 ");
193 if (type & TOYOTA388)
194 printk("TOYOTA388 ");
195 if (type & TOYOTA794)
196 printk("TOYOTA794 ");
197 if (type & DIBCOM52)
198 printk("DIBCOM52 ");
199 if (type & ZARLINK456)
200 printk("ZARLINK456 ");
201 if (type & CHINA)
202 printk("CHINA ");
203 if (type & F6MHZ)
204 printk("F6MHZ ");
205 if (type & INPUT2)
206 printk("INPUT2 ");
207 if (type & SCODE)
208 printk("SCODE ");
209}
210
ef8c1888 211static v4l2_std_id parse_audio_std_option(void)
a82200fb 212{
e155d908 213 if (strcasecmp(audio_std, "A2") == 0)
a82200fb 214 return V4L2_STD_A2;
e155d908 215 if (strcasecmp(audio_std, "A2/A") == 0)
a82200fb 216 return V4L2_STD_A2_A;
e155d908 217 if (strcasecmp(audio_std, "A2/B") == 0)
a82200fb 218 return V4L2_STD_A2_B;
e155d908 219 if (strcasecmp(audio_std, "NICAM") == 0)
a82200fb 220 return V4L2_STD_NICAM;
e155d908 221 if (strcasecmp(audio_std, "NICAM/A") == 0)
a82200fb 222 return V4L2_STD_NICAM_A;
e155d908 223 if (strcasecmp(audio_std, "NICAM/B") == 0)
a82200fb
MCC
224 return V4L2_STD_NICAM_B;
225
226 return 0;
227}
228
ab0b9fc6 229static void free_firmware(struct xc2028_data *priv)
6cb45879 230{
de3fe21b
MCC
231 int i;
232
233 if (!priv->firm)
234 return;
235
ab0b9fc6
MCC
236 for (i = 0; i < priv->firm_size; i++)
237 kfree(priv->firm[i].ptr);
238
de3fe21b
MCC
239 kfree(priv->firm);
240
ab0b9fc6 241 priv->firm = NULL;
06fd82dc 242 priv->firm_size = 0;
e0f0b37a
CP
243
244 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
de3fe21b
MCC
245}
246
ab0b9fc6 247static int load_all_firmwares(struct dvb_frontend *fe)
de3fe21b
MCC
248{
249 struct xc2028_data *priv = fe->tuner_priv;
ab0b9fc6 250 const struct firmware *fw = NULL;
6cb45879 251 unsigned char *p, *endp;
ab0b9fc6
MCC
252 int rc = 0;
253 int n, n_array;
de3fe21b 254 char name[33];
6cb45879 255
83fb340b 256 tuner_dbg("%s called\n", __FUNCTION__);
215b95ba 257
06fd82dc 258 tuner_dbg("Reading firmware %s\n", priv->ctrl.fname);
a37b4c9b
ML
259 rc = request_firmware(&fw, priv->ctrl.fname,
260 &priv->i2c_props.adap->dev);
6cb45879 261 if (rc < 0) {
ab0b9fc6 262 if (rc == -ENOENT)
83fb340b 263 tuner_err("Error: firmware %s not found.\n",
de3fe21b 264 priv->ctrl.fname);
2e4160ca 265 else
83fb340b 266 tuner_err("Error %d while requesting firmware %s \n",
de3fe21b 267 rc, priv->ctrl.fname);
2e4160ca 268
6cb45879
MCC
269 return rc;
270 }
ab0b9fc6
MCC
271 p = fw->data;
272 endp = p + fw->size;
6cb45879 273
06fd82dc
CP
274 if (fw->size < sizeof(name) - 1 + 2 + 2) {
275 tuner_err("Error: firmware file %s has invalid size!\n",
276 priv->ctrl.fname);
277 goto corrupt;
6cb45879 278 }
de3fe21b 279
ab0b9fc6
MCC
280 memcpy(name, p, sizeof(name) - 1);
281 name[sizeof(name) - 1] = 0;
282 p += sizeof(name) - 1;
de3fe21b 283
06fd82dc 284 priv->firm_version = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
285 p += 2;
286
ab0b9fc6 287 n_array = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
288 p += 2;
289
06fd82dc
CP
290 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
291 n_array, priv->ctrl.fname, name,
292 priv->firm_version >> 8, priv->firm_version & 0xff);
de3fe21b 293
ab0b9fc6 294 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
06fd82dc
CP
295 if (priv->firm == NULL) {
296 tuner_err("Not enough memory to load firmware file.\n");
ab0b9fc6 297 rc = -ENOMEM;
06fd82dc 298 goto err;
6cb45879 299 }
de3fe21b 300 priv->firm_size = n_array;
06fd82dc 301
ab0b9fc6
MCC
302 n = -1;
303 while (p < endp) {
de3fe21b
MCC
304 __u32 type, size;
305 v4l2_std_id id;
66c2d53d 306 __u16 int_freq = 0;
de3fe21b
MCC
307
308 n++;
309 if (n >= n_array) {
06fd82dc
CP
310 tuner_err("More firmware images in file than "
311 "were expected!\n");
de3fe21b
MCC
312 goto corrupt;
313 }
314
315 /* Checks if there's enough bytes to read */
ab0b9fc6 316 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
83fb340b 317 tuner_err("Firmware header is incomplete!\n");
de3fe21b
MCC
318 goto corrupt;
319 }
320
ab0b9fc6 321 type = le32_to_cpu(*(__u32 *) p);
de3fe21b
MCC
322 p += sizeof(type);
323
ab0b9fc6 324 id = le64_to_cpu(*(v4l2_std_id *) p);
de3fe21b
MCC
325 p += sizeof(id);
326
66c2d53d
MCC
327 if (type & HAS_IF) {
328 int_freq = le16_to_cpu(*(__u16 *) p);
329 p += sizeof(int_freq);
330 }
331
2fc580ff 332 size = le32_to_cpu(*(__u32 *) p);
de3fe21b
MCC
333 p += sizeof(size);
334
ab0b9fc6 335 if ((!size) || (size + p > endp)) {
83fb340b 336 tuner_err("Firmware type ");
43efe702 337 dump_firm_type(type);
ef8c1888
MCC
338 printk("(%x), id %llx is corrupted "
339 "(size=%d, expected %d)\n",
91240dd9 340 type, (unsigned long long)id,
ef8c1888 341 (unsigned)(endp - p), size);
de3fe21b
MCC
342 goto corrupt;
343 }
344
ab0b9fc6 345 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
06fd82dc
CP
346 if (priv->firm[n].ptr == NULL) {
347 tuner_err("Not enough memory to load firmware file.\n");
ab0b9fc6 348 rc = -ENOMEM;
de3fe21b
MCC
349 goto err;
350 }
06fd82dc
CP
351 tuner_dbg("Reading firmware type ");
352 if (debug) {
353 dump_firm_type(type);
354 printk("(%x), id %llx, size=%d.\n",
355 type, (unsigned long long)id, size);
356 }
de3fe21b
MCC
357
358 memcpy(priv->firm[n].ptr, p, size);
359 priv->firm[n].type = type;
360 priv->firm[n].id = id;
361 priv->firm[n].size = size;
66c2d53d 362 priv->firm[n].int_freq = int_freq;
de3fe21b
MCC
363
364 p += size;
365 }
366
ab0b9fc6 367 if (n + 1 != priv->firm_size) {
83fb340b 368 tuner_err("Firmware file is incomplete!\n");
de3fe21b
MCC
369 goto corrupt;
370 }
371
372 goto done;
373
374corrupt:
ab0b9fc6 375 rc = -EINVAL;
83fb340b 376 tuner_err("Error: firmware file is corrupted!\n");
de3fe21b
MCC
377
378err:
06fd82dc 379 tuner_info("Releasing partially loaded firmware file.\n");
de3fe21b
MCC
380 free_firmware(priv);
381
382done:
383 release_firmware(fw);
06fd82dc
CP
384 if (rc == 0)
385 tuner_dbg("Firmware files loaded.\n");
de3fe21b
MCC
386
387 return rc;
388}
389
f380e1d2
MCC
390static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
391 v4l2_std_id *id)
de3fe21b
MCC
392{
393 struct xc2028_data *priv = fe->tuner_priv;
b1535293 394 int i, best_i = -1, best_nr_matches = 0;
de3fe21b 395
b1535293
CP
396 tuner_dbg("%s called, want type=", __FUNCTION__);
397 if (debug) {
398 dump_firm_type(type);
399 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
400 }
de3fe21b
MCC
401
402 if (!priv->firm) {
83fb340b 403 tuner_err("Error! firmware not loaded\n");
de3fe21b
MCC
404 return -EINVAL;
405 }
406
f380e1d2 407 if (((type & ~SCODE) == 0) && (*id == 0))
ab0b9fc6 408 *id = V4L2_STD_PAL;
de3fe21b 409
e0f0b37a
CP
410 if (type & BASE)
411 type &= BASE_TYPES;
412 else if (type & SCODE)
413 type &= SCODE_TYPES;
414 else if (type & DTV_TYPES)
11a9eff9
CP
415 type &= DTV_TYPES;
416 else if (type & STD_SPECIFIC_TYPES)
417 type &= STD_SPECIFIC_TYPES;
e0f0b37a 418
de3fe21b 419 /* Seek for exact match */
ab0b9fc6
MCC
420 for (i = 0; i < priv->firm_size; i++) {
421 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
de3fe21b
MCC
422 goto found;
423 }
424
425 /* Seek for generic video standard match */
ab0b9fc6 426 for (i = 0; i < priv->firm_size; i++) {
b1535293
CP
427 v4l2_std_id match_mask;
428 int nr_matches;
429
430 if (type != priv->firm[i].type)
431 continue;
432
433 match_mask = *id & priv->firm[i].id;
434 if (!match_mask)
435 continue;
436
437 if ((*id & match_mask) == *id)
438 goto found; /* Supports all the requested standards */
439
440 nr_matches = hweight64(match_mask);
441 if (nr_matches > best_nr_matches) {
442 best_nr_matches = nr_matches;
443 best_i = i;
444 }
445 }
446
447 if (best_nr_matches > 0) {
448 tuner_dbg("Selecting best matching firmware (%d bits) for "
449 "type=", best_nr_matches);
450 dump_firm_type(type);
451 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
452 i = best_i;
453 goto found;
de3fe21b
MCC
454 }
455
456 /*FIXME: Would make sense to seek for type "hint" match ? */
457
b1535293 458 i = -ENOENT;
f380e1d2 459 goto ret;
de3fe21b
MCC
460
461found:
462 *id = priv->firm[i].id;
de3fe21b 463
f380e1d2 464ret:
b1535293 465 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
83fb340b
MCC
466 if (debug) {
467 dump_firm_type(type);
91240dd9 468 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
83fb340b 469 }
f380e1d2
MCC
470 return i;
471}
472
473static int load_firmware(struct dvb_frontend *fe, unsigned int type,
474 v4l2_std_id *id)
475{
476 struct xc2028_data *priv = fe->tuner_priv;
477 int pos, rc;
0a196b6f 478 unsigned char *p, *endp, buf[priv->ctrl.max_len];
f380e1d2 479
83fb340b 480 tuner_dbg("%s called\n", __FUNCTION__);
f380e1d2
MCC
481
482 pos = seek_firmware(fe, type, id);
483 if (pos < 0)
484 return pos;
485
83fb340b 486 tuner_info("Loading firmware for type=");
b1535293
CP
487 dump_firm_type(priv->firm[pos].type);
488 printk("(%x), id %016llx.\n", priv->firm[pos].type,
489 (unsigned long long)*id);
83fb340b 490
f380e1d2 491 p = priv->firm[pos].ptr;
f380e1d2 492 endp = p + priv->firm[pos].size;
6cb45879 493
ab0b9fc6 494 while (p < endp) {
de3fe21b
MCC
495 __u16 size;
496
497 /* Checks if there's enough bytes to read */
ab0b9fc6 498 if (p + sizeof(size) > endp) {
83fb340b 499 tuner_err("Firmware chunk size is wrong\n");
de3fe21b
MCC
500 return -EINVAL;
501 }
502
ab0b9fc6 503 size = le16_to_cpu(*(__u16 *) p);
de3fe21b
MCC
504 p += sizeof(size);
505
506 if (size == 0xffff)
507 return 0;
508
509 if (!size) {
6cb45879 510 /* Special callback command received */
215b95ba 511 rc = priv->tuner_callback(priv->video_dev,
ab0b9fc6
MCC
512 XC2028_TUNER_RESET, 0);
513 if (rc < 0) {
83fb340b 514 tuner_err("Error at RESET code %d\n",
ab0b9fc6 515 (*p) & 0x7f);
de3fe21b 516 return -EINVAL;
6cb45879 517 }
6cb45879
MCC
518 continue;
519 }
5403bbae
ML
520 if (size >= 0xff00) {
521 switch (size) {
522 case 0xff00:
523 rc = priv->tuner_callback(priv->video_dev,
524 XC2028_RESET_CLK, 0);
525 if (rc < 0) {
526 tuner_err("Error at RESET code %d\n",
527 (*p) & 0x7f);
528 return -EINVAL;
529 }
b32f9fb9 530 break;
5403bbae
ML
531 default:
532 tuner_info("Invalid RESET code %d\n",
533 size & 0x7f);
534 return -EINVAL;
535
536 }
2d4c0ac6 537 continue;
5403bbae 538 }
de3fe21b
MCC
539
540 /* Checks for a sleep command */
541 if (size & 0x8000) {
ab0b9fc6 542 msleep(size & 0x7fff);
de3fe21b 543 continue;
6cb45879
MCC
544 }
545
de3fe21b 546 if ((size + p > endp)) {
83fb340b 547 tuner_err("missing bytes: need %d, have %d\n",
ab0b9fc6 548 size, (int)(endp - p));
de3fe21b
MCC
549 return -EINVAL;
550 }
6cb45879 551
de3fe21b 552 buf[0] = *p;
6cb45879 553 p++;
de3fe21b 554 size--;
6cb45879 555
de3fe21b 556 /* Sends message chunks */
ab0b9fc6 557 while (size > 0) {
0a196b6f
CP
558 int len = (size < priv->ctrl.max_len - 1) ?
559 size : priv->ctrl.max_len - 1;
6cb45879 560
ab0b9fc6 561 memcpy(buf + 1, p, len);
6cb45879 562
47cc5b78 563 rc = i2c_send(priv, buf, len + 1);
ab0b9fc6 564 if (rc < 0) {
83fb340b 565 tuner_err("%d returned from send\n", rc);
de3fe21b
MCC
566 return -EINVAL;
567 }
568
569 p += len;
570 size -= len;
571 }
572 }
43efe702 573 return 0;
6cb45879
MCC
574}
575
f380e1d2 576static int load_scode(struct dvb_frontend *fe, unsigned int type,
66c2d53d 577 v4l2_std_id *id, __u16 int_freq, int scode)
f380e1d2
MCC
578{
579 struct xc2028_data *priv = fe->tuner_priv;
580 int pos, rc;
581 unsigned char *p;
582
83fb340b 583 tuner_dbg("%s called\n", __FUNCTION__);
f380e1d2 584
66c2d53d
MCC
585 if (!int_freq) {
586 pos = seek_firmware(fe, type, id);
587 if (pos < 0)
588 return pos;
589 } else {
590 for (pos = 0; pos < priv->firm_size; pos++) {
591 if ((priv->firm[pos].int_freq == int_freq) &&
592 (type & HAS_IF))
593 break;
594 }
595 if (pos == priv->firm_size)
596 return -ENOENT;
597 }
f380e1d2
MCC
598
599 p = priv->firm[pos].ptr;
600
66c2d53d
MCC
601 if (type & HAS_IF) {
602 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
603 return -EINVAL;
604 p += 12 * scode;
605 } else {
606 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
607 * has a 2-byte size header in the firmware format. */
608 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
609 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
610 return -EINVAL;
611 p += 14 * scode + 2;
612 }
f380e1d2 613
d7b22c5c
CP
614 tuner_info("Loading SCODE for type=");
615 dump_firm_type(priv->firm[pos].type);
616 printk("(%x), id %016llx.\n", priv->firm[pos].type,
617 (unsigned long long)*id);
618
06fd82dc 619 if (priv->firm_version < 0x0202)
47cc5b78
CP
620 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
621 else
622 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
623 if (rc < 0)
624 return -EIO;
f380e1d2 625
66c2d53d 626 rc = i2c_send(priv, p, 12);
47cc5b78
CP
627 if (rc < 0)
628 return -EIO;
f380e1d2 629
47cc5b78
CP
630 rc = send_seq(priv, {0x00, 0x8c});
631 if (rc < 0)
632 return -EIO;
f380e1d2
MCC
633
634 return 0;
635}
636
00deff1a 637static int check_firmware(struct dvb_frontend *fe, unsigned int type,
66c2d53d 638 v4l2_std_id std, __u16 int_freq)
6cb45879 639{
00deff1a 640 struct xc2028_data *priv = fe->tuner_priv;
e0f0b37a 641 struct firmware_properties new_fw;
00deff1a
MCC
642 int rc = 0, is_retry = 0;
643 u16 version, hwmodel;
644 v4l2_std_id std0;
6cb45879 645
83fb340b 646 tuner_dbg("%s called\n", __FUNCTION__);
6cb45879 647
de3fe21b 648 if (!priv->firm) {
a37b4c9b
ML
649 if (!priv->ctrl.fname) {
650 tuner_info("xc2028/3028 firmware name not set!\n");
de3fe21b 651 return -EINVAL;
a37b4c9b 652 }
de3fe21b 653
ab0b9fc6
MCC
654 rc = load_all_firmwares(fe);
655 if (rc < 0)
de3fe21b
MCC
656 return rc;
657 }
658
5add9a6f 659 if (priv->ctrl.mts)
e0f0b37a 660 type |= MTS;
6cb45879 661
8bf799a6 662retry:
e0f0b37a
CP
663 new_fw.type = type;
664 new_fw.id = std;
665 new_fw.std_req = std;
666 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
667 new_fw.scode_nr = 0;
66c2d53d 668 new_fw.int_freq = int_freq;
e0f0b37a
CP
669
670 tuner_dbg("checking firmware, user requested type=");
671 if (debug) {
672 dump_firm_type(new_fw.type);
673 printk("(%x), id %016llx, scode_tbl ", new_fw.type,
674 (unsigned long long)new_fw.std_req);
675 dump_firm_type(priv->ctrl.scode_table);
676 printk("(%x), scode_nr %d\n", priv->ctrl.scode_table,
677 new_fw.scode_nr);
678 }
679
680 /* No need to reload base firmware if it matches */
681 if (((BASE | new_fw.type) & BASE_TYPES) ==
682 (priv->cur_fw.type & BASE_TYPES)) {
683 tuner_dbg("BASE firmware not changed.\n");
684 goto skip_base;
685 }
686
687 /* Updating BASE - forget about all currently loaded firmware */
688 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
689
690 /* Reset is needed before loading firmware */
691 rc = priv->tuner_callback(priv->video_dev,
692 XC2028_TUNER_RESET, 0);
693 if (rc < 0)
694 goto fail;
695
47bd5bc6
CP
696 /* BASE firmwares are all std0 */
697 std0 = 0;
698 rc = load_firmware(fe, BASE | new_fw.type, &std0);
e0f0b37a
CP
699 if (rc < 0) {
700 tuner_err("Error %d while loading base firmware\n",
701 rc);
702 goto fail;
703 }
5403bbae 704
de3fe21b 705 /* Load INIT1, if needed */
83fb340b 706 tuner_dbg("Load init1 firmware, if exists\n");
de3fe21b 707
47bd5bc6 708 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
1ad0b796
CP
709 if (rc == -ENOENT)
710 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
711 &std0);
e0f0b37a
CP
712 if (rc < 0 && rc != -ENOENT) {
713 tuner_err("Error %d while loading init1 firmware\n",
714 rc);
715 goto fail;
716 }
de3fe21b 717
e0f0b37a
CP
718skip_base:
719 /*
720 * No need to reload standard specific firmware if base firmware
721 * was not reloaded and requested video standards have not changed.
de3fe21b 722 */
e0f0b37a
CP
723 if (priv->cur_fw.type == (BASE | new_fw.type) &&
724 priv->cur_fw.std_req == std) {
83fb340b 725 tuner_dbg("Std-specific firmware already loaded.\n");
e0f0b37a 726 goto skip_std_specific;
2e4160ca 727 }
6cb45879 728
e0f0b37a
CP
729 /* Reloading std-specific firmware forces a SCODE update */
730 priv->cur_fw.scode_table = 0;
731
e0f0b37a 732 rc = load_firmware(fe, new_fw.type, &new_fw.id);
cca83798
MCC
733 if (rc == -ENOENT)
734 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
735
ab0b9fc6 736 if (rc < 0)
e0f0b37a
CP
737 goto fail;
738
739skip_std_specific:
740 if (priv->cur_fw.scode_table == new_fw.scode_table &&
741 priv->cur_fw.scode_nr == new_fw.scode_nr) {
742 tuner_dbg("SCODE firmware already loaded.\n");
743 goto check_device;
744 }
6cb45879 745
f380e1d2 746 /* Load SCODE firmware, if exists */
e0f0b37a 747 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
f380e1d2 748
66c2d53d
MCC
749 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
750 new_fw.int_freq, new_fw.scode_nr);
43efe702 751
e0f0b37a 752check_device:
8bf799a6
CP
753 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
754 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
755 tuner_err("Unable to read tuner registers.\n");
756 goto fail;
757 }
80b52208
MCC
758
759 tuner_info("Device is Xceive %d version %d.%d, "
760 "firmware version %d.%d\n",
761 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
762 (version & 0xf0) >> 4, version & 0xf);
6cb45879 763
8bf799a6
CP
764 /* Check firmware version against what we downloaded. */
765 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
766 tuner_err("Incorrect readback of firmware version.\n");
767 goto fail;
768 }
769
770 /* Check that the tuner hardware model remains consistent over time. */
771 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
772 priv->hwmodel = hwmodel;
773 priv->hwvers = version & 0xff00;
774 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
775 priv->hwvers != (version & 0xff00)) {
776 tuner_err("Read invalid device hardware information - tuner "
777 "hung?\n");
778 goto fail;
779 }
780
e0f0b37a
CP
781 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
782
783 /*
784 * By setting BASE in cur_fw.type only after successfully loading all
785 * firmwares, we can:
786 * 1. Identify that BASE firmware with type=0 has been loaded;
787 * 2. Tell whether BASE firmware was just changed the next time through.
788 */
789 priv->cur_fw.type |= BASE;
6cb45879
MCC
790
791 return 0;
e0f0b37a
CP
792
793fail:
794 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
8bf799a6
CP
795 if (!is_retry) {
796 msleep(50);
797 is_retry = 1;
798 tuner_dbg("Retrying firmware load\n");
799 goto retry;
800 }
801
e0f0b37a
CP
802 if (rc == -ENOENT)
803 rc = -EINVAL;
804 return rc;
6cb45879
MCC
805}
806
215b95ba 807static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
6cb45879 808{
215b95ba 809 struct xc2028_data *priv = fe->tuner_priv;
7d58d111
CP
810 u16 frq_lock, signal = 0;
811 int rc;
3b20532c 812
83fb340b 813 tuner_dbg("%s called\n", __FUNCTION__);
6cb45879 814
215b95ba 815 mutex_lock(&priv->lock);
6cb45879 816
80b52208 817 /* Sync Lock Indicator */
7d58d111
CP
818 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
819 if (rc < 0 || frq_lock == 0)
3b20532c 820 goto ret;
6cb45879
MCC
821
822 /* Frequency is locked. Return signal quality */
823
80b52208 824 /* Get SNR of the video signal */
7d58d111
CP
825 rc = xc2028_get_reg(priv, 0x0040, &signal);
826 if (rc < 0)
827 signal = -frq_lock;
3b20532c
MCC
828
829ret:
215b95ba
MCC
830 mutex_unlock(&priv->lock);
831
832 *strength = signal;
6cb45879 833
7d58d111 834 return rc;
6cb45879
MCC
835}
836
837#define DIV 15625
838
00deff1a 839static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
66c2d53d
MCC
840 enum tuner_mode new_mode,
841 unsigned int type,
842 v4l2_std_id std,
843 u16 int_freq)
6cb45879 844{
215b95ba 845 struct xc2028_data *priv = fe->tuner_priv;
ab0b9fc6 846 int rc = -EINVAL;
2ce4b3aa 847 unsigned char buf[4];
ab0b9fc6 848 u32 div, offset = 0;
6cb45879 849
83fb340b 850 tuner_dbg("%s called\n", __FUNCTION__);
215b95ba 851
de3fe21b
MCC
852 mutex_lock(&priv->lock);
853
2ce4b3aa 854 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
6cb45879 855
66c2d53d 856 if (check_firmware(fe, type, std, int_freq) < 0)
3b20532c 857 goto ret;
2e4160ca 858
2800ae9c
MCC
859 /* On some cases xc2028 can disable video output, if
860 * very weak signals are received. By sending a soft
861 * reset, this is re-enabled. So, it is better to always
862 * send a soft reset before changing channels, to be sure
863 * that xc2028 will be in a safe state.
864 * Maybe this might also be needed for DTV.
865 */
00deff1a 866 if (new_mode == T_ANALOG_TV) {
2800ae9c 867 rc = send_seq(priv, {0x00, 0x00});
00deff1a 868 } else {
d4e76681 869 offset = 2750000;
e0f0b37a 870 if (priv->cur_fw.type & DTV7)
a44f1c43
CP
871 offset -= 500000;
872 }
2e4160ca 873
ab0b9fc6 874 div = (freq - offset + DIV / 2) / DIV;
2e4160ca 875
6cb45879 876 /* CMD= Set frequency */
06fd82dc 877 if (priv->firm_version < 0x0202)
47cc5b78
CP
878 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
879 else
880 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
881 if (rc < 0)
882 goto ret;
de3fe21b 883
215b95ba 884 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
ab0b9fc6 885 if (rc < 0)
215b95ba 886 goto ret;
6cb45879
MCC
887
888 msleep(10);
701672eb 889
ab0b9fc6
MCC
890 buf[0] = 0xff & (div >> 24);
891 buf[1] = 0xff & (div >> 16);
892 buf[2] = 0xff & (div >> 8);
893 buf[3] = 0xff & (div);
6cb45879 894
47cc5b78 895 rc = i2c_send(priv, buf, sizeof(buf));
ab0b9fc6 896 if (rc < 0)
3b20532c 897 goto ret;
6cb45879
MCC
898 msleep(100);
899
ab0b9fc6 900 priv->frequency = freq;
215b95ba 901
2ce4b3aa
CP
902 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
903 buf[0], buf[1], buf[2], buf[3],
904 freq / 1000000, (freq % 1000000) / 1000);
3b20532c 905
ab0b9fc6 906 rc = 0;
6cb45879 907
215b95ba
MCC
908ret:
909 mutex_unlock(&priv->lock);
6cb45879 910
215b95ba 911 return rc;
701672eb
ML
912}
913
00deff1a 914static int xc2028_set_analog_freq(struct dvb_frontend *fe,
ab0b9fc6 915 struct analog_parameters *p)
6cb45879 916{
215b95ba 917 struct xc2028_data *priv = fe->tuner_priv;
00deff1a
MCC
918 unsigned int type=0;
919
920 tuner_dbg("%s called\n", __FUNCTION__);
c71d4bc5 921
d74cb25e
MCC
922 if (p->mode == V4L2_TUNER_RADIO) {
923 type |= FM;
924 if (priv->ctrl.input1)
925 type |= INPUT1;
926 return generic_set_freq(fe, (625l * p->frequency) / 10,
66c2d53d 927 T_ANALOG_TV, type, 0, 0);
d74cb25e
MCC
928 }
929
a5e9fe14
MCC
930 /* if std is not defined, choose one */
931 if (!p->std)
932 p->std = V4L2_STD_MN;
933
934 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
00deff1a
MCC
935 if (!(p->std & V4L2_STD_MN))
936 type |= F8MHZ;
6cb45879 937
00deff1a
MCC
938 /* Add audio hack to std mask */
939 p->std |= parse_audio_std_option();
6cb45879 940
00deff1a 941 return generic_set_freq(fe, 62500l * p->frequency,
66c2d53d 942 T_ANALOG_TV, type, p->std, 0);
215b95ba 943}
6cb45879 944
215b95ba
MCC
945static int xc2028_set_params(struct dvb_frontend *fe,
946 struct dvb_frontend_parameters *p)
6cb45879 947{
215b95ba 948 struct xc2028_data *priv = fe->tuner_priv;
00deff1a 949 unsigned int type=0;
d04aa54a 950 fe_bandwidth_t bw = BANDWIDTH_8_MHZ;
6cb45879 951
83fb340b 952 tuner_dbg("%s called\n", __FUNCTION__);
701672eb 953
00deff1a
MCC
954 if (priv->ctrl.d2633)
955 type |= D2633;
956 else
957 type |= D2620;
958
d04aa54a 959 switch(fe->ops.info.type) {
d04aa54a
MCC
960 case FE_OFDM:
961 bw = p->u.ofdm.bandwidth;
962 break;
963 case FE_QAM:
5c15648a
MCC
964 tuner_info("WARN: There are some reports that "
965 "QAM 6 MHz doesn't work.\n"
966 "If this works for you, please report by "
967 "e-mail to: v4l-dvb-maintainer@linuxtv.org\n");
d04aa54a
MCC
968 bw = BANDWIDTH_6_MHZ;
969 type |= QAM;
970 break;
971 case FE_ATSC:
972 bw = BANDWIDTH_6_MHZ;
4bfae52b
MCC
973 /* The only ATSC firmware (at least on v2.7) is D2633,
974 so overrides ctrl->d2633 */
d04aa54a 975 type |= ATSC| D2633;
4bfae52b 976 type &= ~D2620;
d04aa54a 977 break;
5c15648a
MCC
978 /* DVB-S is not supported */
979 default:
980 return -EINVAL;
d04aa54a
MCC
981 }
982
d04aa54a
MCC
983 /* FIXME:
984 There are two Scodes that will never be selected:
985 DTV78 ZARLINK456, DTV78 DIBCOM52
986 When it should opt for DTV78 instead of DTV7 or DTV8?
987 */
00deff1a
MCC
988 switch (bw) {
989 case BANDWIDTH_8_MHZ:
d04aa54a 990 type |= DTV8 | F8MHZ;
00deff1a
MCC
991 break;
992 case BANDWIDTH_7_MHZ:
d04aa54a 993 type |= DTV7 | F8MHZ;
00deff1a
MCC
994 break;
995 case BANDWIDTH_6_MHZ:
d04aa54a 996 type |= DTV6 ;
00deff1a
MCC
997 break;
998 default:
999 tuner_err("error: bandwidth not supported.\n");
1000 };
1001
66c2d53d
MCC
1002 /* All S-code tables need a 200kHz shift */
1003 if (priv->ctrl.demod)
1004 priv->ctrl.demod += 200;
b542dfdc 1005
00deff1a 1006 return generic_set_freq(fe, p->frequency,
66c2d53d 1007 T_DIGITAL_TV, type, 0, priv->ctrl.demod);
6cb45879 1008}
701672eb 1009
45819c38
CP
1010static int xc2028_sleep(struct dvb_frontend *fe)
1011{
1012 struct xc2028_data *priv = fe->tuner_priv;
1013 int rc = 0;
1014
1015 tuner_dbg("%s called\n", __FUNCTION__);
1016
1017 mutex_lock(&priv->lock);
1018
1019 if (priv->firm_version < 0x0202)
1020 rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00});
1021 else
1022 rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00});
1023
1024 priv->cur_fw.type = 0; /* need firmware reload */
1025
1026 mutex_unlock(&priv->lock);
1027
1028 return rc;
1029}
1030
1031
215b95ba 1032static int xc2028_dvb_release(struct dvb_frontend *fe)
701672eb 1033{
215b95ba
MCC
1034 struct xc2028_data *priv = fe->tuner_priv;
1035
83fb340b 1036 tuner_dbg("%s called\n", __FUNCTION__);
701672eb 1037
aa501be9
CP
1038 mutex_lock(&xc2028_list_mutex);
1039
215b95ba 1040 priv->count--;
701672eb 1041
de3fe21b 1042 if (!priv->count) {
1808a698
MCC
1043 list_del(&priv->xc2028_list);
1044
ab0b9fc6 1045 kfree(priv->ctrl.fname);
de3fe21b
MCC
1046
1047 free_firmware(priv);
ab0b9fc6 1048 kfree(priv);
06fd82dc 1049 fe->tuner_priv = NULL;
de3fe21b 1050 }
701672eb 1051
aa501be9
CP
1052 mutex_unlock(&xc2028_list_mutex);
1053
701672eb
ML
1054 return 0;
1055}
1056
215b95ba 1057static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
701672eb 1058{
215b95ba 1059 struct xc2028_data *priv = fe->tuner_priv;
701672eb 1060
83fb340b 1061 tuner_dbg("%s called\n", __FUNCTION__);
701672eb 1062
215b95ba 1063 *frequency = priv->frequency;
701672eb
ML
1064
1065 return 0;
1066}
1067
ab0b9fc6 1068static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
de3fe21b
MCC
1069{
1070 struct xc2028_data *priv = fe->tuner_priv;
1071 struct xc2028_ctrl *p = priv_cfg;
0a196b6f 1072 int rc = 0;
de3fe21b 1073
83fb340b 1074 tuner_dbg("%s called\n", __FUNCTION__);
de3fe21b 1075
06fd82dc
CP
1076 mutex_lock(&priv->lock);
1077
0a196b6f
CP
1078 kfree(priv->ctrl.fname);
1079 free_firmware(priv);
de3fe21b 1080
0a196b6f
CP
1081 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1082 priv->ctrl.fname = NULL;
de3fe21b 1083
0a196b6f
CP
1084 if (p->fname) {
1085 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1086 if (priv->ctrl.fname == NULL)
1087 rc = -ENOMEM;
de3fe21b
MCC
1088 }
1089
0a196b6f
CP
1090 if (priv->ctrl.max_len < 9)
1091 priv->ctrl.max_len = 13;
352fae1d 1092
06fd82dc
CP
1093 mutex_unlock(&priv->lock);
1094
0a196b6f 1095 return rc;
de3fe21b
MCC
1096}
1097
215b95ba 1098static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
701672eb 1099 .info = {
ab0b9fc6
MCC
1100 .name = "Xceive XC3028",
1101 .frequency_min = 42000000,
1102 .frequency_max = 864000000,
1103 .frequency_step = 50000,
1104 },
701672eb 1105
de3fe21b 1106 .set_config = xc2028_set_config,
00deff1a 1107 .set_analog_params = xc2028_set_analog_freq,
215b95ba
MCC
1108 .release = xc2028_dvb_release,
1109 .get_frequency = xc2028_get_frequency,
1110 .get_rf_strength = xc2028_signal,
1111 .set_params = xc2028_set_params,
45819c38 1112 .sleep = xc2028_sleep,
701672eb 1113
701672eb
ML
1114};
1115
a37b4c9b 1116void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
701672eb 1117{
215b95ba 1118 struct xc2028_data *priv;
a37b4c9b 1119 void *video_dev;
701672eb 1120
83fb340b 1121 if (debug)
3157ecef 1122 printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n");
701672eb 1123
71a2ee37 1124 if (NULL == cfg || NULL == cfg->video_dev)
a37b4c9b 1125 return NULL;
215b95ba 1126
a37b4c9b 1127 if (!fe) {
3157ecef 1128 printk(KERN_ERR PREFIX ": No frontend!\n");
a37b4c9b 1129 return NULL;
215b95ba
MCC
1130 }
1131
a37b4c9b
ML
1132 video_dev = cfg->video_dev;
1133
aa501be9
CP
1134 mutex_lock(&xc2028_list_mutex);
1135
215b95ba 1136 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
a37b4c9b
ML
1137 if (priv->video_dev == cfg->video_dev) {
1138 video_dev = NULL;
1139 break;
1140 }
215b95ba
MCC
1141 }
1142
a37b4c9b 1143 if (video_dev) {
215b95ba 1144 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
aa501be9
CP
1145 if (priv == NULL) {
1146 mutex_unlock(&xc2028_list_mutex);
a37b4c9b 1147 return NULL;
aa501be9 1148 }
3b20532c 1149
a37b4c9b
ML
1150 priv->i2c_props.addr = cfg->i2c_addr;
1151 priv->i2c_props.adap = cfg->i2c_adap;
215b95ba 1152 priv->video_dev = video_dev;
a37b4c9b 1153 priv->tuner_callback = cfg->callback;
0a196b6f 1154 priv->ctrl.max_len = 13;
de3fe21b 1155
215b95ba
MCC
1156 mutex_init(&priv->lock);
1157
ab0b9fc6 1158 list_add_tail(&priv->xc2028_list, &xc2028_list);
215b95ba 1159 }
a37b4c9b
ML
1160
1161 fe->tuner_priv = priv;
1808a698 1162 priv->count++;
215b95ba
MCC
1163
1164 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
ab0b9fc6 1165 sizeof(xc2028_dvb_tuner_ops));
215b95ba
MCC
1166
1167 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1168
71a2ee37
MCC
1169 if (cfg->ctrl)
1170 xc2028_set_config(fe, cfg->ctrl);
1171
aa501be9
CP
1172 mutex_unlock(&xc2028_list_mutex);
1173
a37b4c9b 1174 return fe;
215b95ba 1175}
a37b4c9b 1176
701672eb
ML
1177EXPORT_SYMBOL(xc2028_attach);
1178
215b95ba 1179MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
983d214e 1180MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
215b95ba
MCC
1181MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1182MODULE_LICENSE("GPL");