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