1 /* tuner-xc2028
2 *
3 * Copyright (c) 2007-2008 Mauro Carvalho Chehab (mchehab@infradead.org)
4 *
5 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * - frontend interface
7 *
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>
14 #include <linux/videodev2.h>
15 #include <linux/delay.h>
16 #include <media/tuner.h>
17 #include <linux/mutex.h>
18 #include <linux/slab.h>
19 #include <asm/unaligned.h>
20 #include "tuner-i2c.h"
21 #include "tuner-xc2028.h"
22 #include "tuner-xc2028-types.h"
23
24 #include <linux/dvb/frontend.h>
25 #include "dvb_frontend.h"
26
27 /* Registers (Write-only) */
28 #define XREG_INIT 0x00
29 #define XREG_RF_FREQ 0x02
30 #define XREG_POWER_DOWN 0x08
31
32 /* Registers (Read-only) */
33 #define XREG_FREQ_ERROR 0x01
34 #define XREG_LOCK 0x02
35 #define XREG_VERSION 0x04
36 #define XREG_PRODUCT_ID 0x08
37 #define XREG_HSYNC_FREQ 0x10
38 #define XREG_FRAME_LINES 0x20
39 #define XREG_SNR 0x40
40
41 #define XREG_ADC_ENV 0x0100
42
43 static int debug;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug, "enable verbose debug messages");
46
47 static int no_poweroff;
48 module_param(no_poweroff, int, 0644);
49 MODULE_PARM_DESC(no_poweroff, "0 (default) powers device off when not used.\n"
50 "1 keep device energized and with tuner ready all the times.\n"
51 " Faster, but consumes more power and keeps the device hotter\n");
52
53 static char audio_std[8];
54 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
55 MODULE_PARM_DESC(audio_std,
56 "Audio standard. XC3028 audio decoder explicitly "
57 "needs to know what audio\n"
58 "standard is needed for some video standards with audio A2 or NICAM.\n"
59 "The valid values are:\n"
60 "A2\n"
61 "A2/A\n"
62 "A2/B\n"
63 "NICAM\n"
64 "NICAM/A\n"
65 "NICAM/B\n");
66
67 static char firmware_name[30];
68 module_param_string(firmware_name, firmware_name, sizeof(firmware_name), 0);
69 MODULE_PARM_DESC(firmware_name, "Firmware file name. Allows overriding the "
70 "default firmware name\n");
71
72 static LIST_HEAD(hybrid_tuner_instance_list);
73 static DEFINE_MUTEX(xc2028_list_mutex);
74
75 /* struct for storing firmware table */
76 struct firmware_description {
77 unsigned int type;
78 v4l2_std_id id;
79 __u16 int_freq;
80 unsigned char *ptr;
81 unsigned int size;
82 };
83
84 struct firmware_properties {
85 unsigned int type;
86 v4l2_std_id id;
87 v4l2_std_id std_req;
88 __u16 int_freq;
89 unsigned int scode_table;
90 int scode_nr;
91 };
92
93 struct xc2028_data {
94 struct list_head hybrid_tuner_instance_list;
95 struct tuner_i2c_props i2c_props;
96 __u32 frequency;
97
98 struct firmware_description *firm;
99 int firm_size;
100 __u16 firm_version;
101
102 __u16 hwmodel;
103 __u16 hwvers;
104
105 struct xc2028_ctrl ctrl;
106
107 struct firmware_properties cur_fw;
108
109 struct mutex lock;
110 };
111
112 #define i2c_send(priv, buf, size) ({ \
113 int _rc; \
114 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
115 if (size != _rc) \
116 tuner_info("i2c output error: rc = %d (should be %d)\n",\
117 _rc, (int)size); \
118 if (priv->ctrl.msleep) \
119 msleep(priv->ctrl.msleep); \
120 _rc; \
121 })
122
123 #define i2c_rcv(priv, buf, size) ({ \
124 int _rc; \
125 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
126 if (size != _rc) \
127 tuner_err("i2c input error: rc = %d (should be %d)\n", \
128 _rc, (int)size); \
129 _rc; \
130 })
131
132 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
133 int _rc; \
134 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
135 ibuf, isize); \
136 if (isize != _rc) \
137 tuner_err("i2c input error: rc = %d (should be %d)\n", \
138 _rc, (int)isize); \
139 if (priv->ctrl.msleep) \
140 msleep(priv->ctrl.msleep); \
141 _rc; \
142 })
143
144 #define send_seq(priv, data...) ({ \
145 static u8 _val[] = data; \
146 int _rc; \
147 if (sizeof(_val) != \
148 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
149 _val, sizeof(_val)))) { \
150 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
151 } else if (priv->ctrl.msleep) \
152 msleep(priv->ctrl.msleep); \
153 _rc; \
154 })
155
xc2028_get_reg(struct xc2028_data * priv,u16 reg,u16 * val)156 static int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
157 {
158 unsigned char buf[2];
159 unsigned char ibuf[2];
160
161 tuner_dbg("%s %04x called\n", __func__, reg);
162
163 buf[0] = reg >> 8;
164 buf[1] = (unsigned char) reg;
165
166 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
167 return -EIO;
168
169 *val = (ibuf[1]) | (ibuf[0] << 8);
170 return 0;
171 }
172
173 #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0)
dump_firm_type_and_int_freq(unsigned int type,u16 int_freq)174 static void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq)
175 {
176 if (type & BASE)
177 printk("BASE ");
178 if (type & INIT1)
179 printk("INIT1 ");
180 if (type & F8MHZ)
181 printk("F8MHZ ");
182 if (type & MTS)
183 printk("MTS ");
184 if (type & D2620)
185 printk("D2620 ");
186 if (type & D2633)
187 printk("D2633 ");
188 if (type & DTV6)
189 printk("DTV6 ");
190 if (type & QAM)
191 printk("QAM ");
192 if (type & DTV7)
193 printk("DTV7 ");
194 if (type & DTV78)
195 printk("DTV78 ");
196 if (type & DTV8)
197 printk("DTV8 ");
198 if (type & FM)
199 printk("FM ");
200 if (type & INPUT1)
201 printk("INPUT1 ");
202 if (type & LCD)
203 printk("LCD ");
204 if (type & NOGD)
205 printk("NOGD ");
206 if (type & MONO)
207 printk("MONO ");
208 if (type & ATSC)
209 printk("ATSC ");
210 if (type & IF)
211 printk("IF ");
212 if (type & LG60)
213 printk("LG60 ");
214 if (type & ATI638)
215 printk("ATI638 ");
216 if (type & OREN538)
217 printk("OREN538 ");
218 if (type & OREN36)
219 printk("OREN36 ");
220 if (type & TOYOTA388)
221 printk("TOYOTA388 ");
222 if (type & TOYOTA794)
223 printk("TOYOTA794 ");
224 if (type & DIBCOM52)
225 printk("DIBCOM52 ");
226 if (type & ZARLINK456)
227 printk("ZARLINK456 ");
228 if (type & CHINA)
229 printk("CHINA ");
230 if (type & F6MHZ)
231 printk("F6MHZ ");
232 if (type & INPUT2)
233 printk("INPUT2 ");
234 if (type & SCODE)
235 printk("SCODE ");
236 if (type & HAS_IF)
237 printk("HAS_IF_%d ", int_freq);
238 }
239
parse_audio_std_option(void)240 static v4l2_std_id parse_audio_std_option(void)
241 {
242 if (strcasecmp(audio_std, "A2") == 0)
243 return V4L2_STD_A2;
244 if (strcasecmp(audio_std, "A2/A") == 0)
245 return V4L2_STD_A2_A;
246 if (strcasecmp(audio_std, "A2/B") == 0)
247 return V4L2_STD_A2_B;
248 if (strcasecmp(audio_std, "NICAM") == 0)
249 return V4L2_STD_NICAM;
250 if (strcasecmp(audio_std, "NICAM/A") == 0)
251 return V4L2_STD_NICAM_A;
252 if (strcasecmp(audio_std, "NICAM/B") == 0)
253 return V4L2_STD_NICAM_B;
254
255 return 0;
256 }
257
free_firmware(struct xc2028_data * priv)258 static void free_firmware(struct xc2028_data *priv)
259 {
260 int i;
261 tuner_dbg("%s called\n", __func__);
262
263 if (!priv->firm)
264 return;
265
266 for (i = 0; i < priv->firm_size; i++)
267 kfree(priv->firm[i].ptr);
268
269 kfree(priv->firm);
270
271 priv->firm = NULL;
272 priv->firm_size = 0;
273
274 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
275 }
276
load_all_firmwares(struct dvb_frontend * fe)277 static int load_all_firmwares(struct dvb_frontend *fe)
278 {
279 struct xc2028_data *priv = fe->tuner_priv;
280 const struct firmware *fw = NULL;
281 const unsigned char *p, *endp;
282 int rc = 0;
283 int n, n_array;
284 char name[33];
285 char *fname;
286
287 tuner_dbg("%s called\n", __func__);
288
289 if (!firmware_name[0])
290 fname = priv->ctrl.fname;
291 else
292 fname = firmware_name;
293
294 tuner_dbg("Reading firmware %s\n", fname);
295 rc = request_firmware(&fw, fname, priv->i2c_props.adap->dev.parent);
296 if (rc < 0) {
297 if (rc == -ENOENT)
298 tuner_err("Error: firmware %s not found.\n",
299 fname);
300 else
301 tuner_err("Error %d while requesting firmware %s \n",
302 rc, fname);
303
304 return rc;
305 }
306 p = fw->data;
307 endp = p + fw->size;
308
309 if (fw->size < sizeof(name) - 1 + 2 + 2) {
310 tuner_err("Error: firmware file %s has invalid size!\n",
311 fname);
312 goto corrupt;
313 }
314
315 memcpy(name, p, sizeof(name) - 1);
316 name[sizeof(name) - 1] = 0;
317 p += sizeof(name) - 1;
318
319 priv->firm_version = get_unaligned_le16(p);
320 p += 2;
321
322 n_array = get_unaligned_le16(p);
323 p += 2;
324
325 tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n",
326 n_array, fname, name,
327 priv->firm_version >> 8, priv->firm_version & 0xff);
328
329 priv->firm = kcalloc(n_array, sizeof(*priv->firm), GFP_KERNEL);
330 if (priv->firm == NULL) {
331 tuner_err("Not enough memory to load firmware file.\n");
332 rc = -ENOMEM;
333 goto err;
334 }
335 priv->firm_size = n_array;
336
337 n = -1;
338 while (p < endp) {
339 __u32 type, size;
340 v4l2_std_id id;
341 __u16 int_freq = 0;
342
343 n++;
344 if (n >= n_array) {
345 tuner_err("More firmware images in file than "
346 "were expected!\n");
347 goto corrupt;
348 }
349
350 /* Checks if there's enough bytes to read */
351 if (endp - p < sizeof(type) + sizeof(id) + sizeof(size))
352 goto header;
353
354 type = get_unaligned_le32(p);
355 p += sizeof(type);
356
357 id = get_unaligned_le64(p);
358 p += sizeof(id);
359
360 if (type & HAS_IF) {
361 int_freq = get_unaligned_le16(p);
362 p += sizeof(int_freq);
363 if (endp - p < sizeof(size))
364 goto header;
365 }
366
367 size = get_unaligned_le32(p);
368 p += sizeof(size);
369
370 if (!size || size > endp - p) {
371 tuner_err("Firmware type ");
372 dump_firm_type(type);
373 printk("(%x), id %llx is corrupted "
374 "(size=%d, expected %d)\n",
375 type, (unsigned long long)id,
376 (unsigned)(endp - p), size);
377 goto corrupt;
378 }
379
380 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
381 if (priv->firm[n].ptr == NULL) {
382 tuner_err("Not enough memory to load firmware file.\n");
383 rc = -ENOMEM;
384 goto err;
385 }
386 tuner_dbg("Reading firmware type ");
387 if (debug) {
388 dump_firm_type_and_int_freq(type, int_freq);
389 printk("(%x), id %llx, size=%d.\n",
390 type, (unsigned long long)id, size);
391 }
392
393 memcpy(priv->firm[n].ptr, p, size);
394 priv->firm[n].type = type;
395 priv->firm[n].id = id;
396 priv->firm[n].size = size;
397 priv->firm[n].int_freq = int_freq;
398
399 p += size;
400 }
401
402 if (n + 1 != priv->firm_size) {
403 tuner_err("Firmware file is incomplete!\n");
404 goto corrupt;
405 }
406
407 goto done;
408
409 header:
410 tuner_err("Firmware header is incomplete!\n");
411 corrupt:
412 rc = -EINVAL;
413 tuner_err("Error: firmware file is corrupted!\n");
414
415 err:
416 tuner_info("Releasing partially loaded firmware file.\n");
417 free_firmware(priv);
418
419 done:
420 release_firmware(fw);
421 if (rc == 0)
422 tuner_dbg("Firmware files loaded.\n");
423
424 return rc;
425 }
426
seek_firmware(struct dvb_frontend * fe,unsigned int type,v4l2_std_id * id)427 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
428 v4l2_std_id *id)
429 {
430 struct xc2028_data *priv = fe->tuner_priv;
431 int i, best_i = -1, best_nr_matches = 0;
432 unsigned int type_mask = 0;
433
434 tuner_dbg("%s called, want type=", __func__);
435 if (debug) {
436 dump_firm_type(type);
437 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
438 }
439
440 if (!priv->firm) {
441 tuner_err("Error! firmware not loaded\n");
442 return -EINVAL;
443 }
444
445 if (((type & ~SCODE) == 0) && (*id == 0))
446 *id = V4L2_STD_PAL;
447
448 if (type & BASE)
449 type_mask = BASE_TYPES;
450 else if (type & SCODE) {
451 type &= SCODE_TYPES;
452 type_mask = SCODE_TYPES & ~HAS_IF;
453 } else if (type & DTV_TYPES)
454 type_mask = DTV_TYPES;
455 else if (type & STD_SPECIFIC_TYPES)
456 type_mask = STD_SPECIFIC_TYPES;
457
458 type &= type_mask;
459
460 if (!(type & SCODE))
461 type_mask = ~0;
462
463 /* Seek for exact match */
464 for (i = 0; i < priv->firm_size; i++) {
465 if ((type == (priv->firm[i].type & type_mask)) &&
466 (*id == priv->firm[i].id))
467 goto found;
468 }
469
470 /* Seek for generic video standard match */
471 for (i = 0; i < priv->firm_size; i++) {
472 v4l2_std_id match_mask;
473 int nr_matches;
474
475 if (type != (priv->firm[i].type & type_mask))
476 continue;
477
478 match_mask = *id & priv->firm[i].id;
479 if (!match_mask)
480 continue;
481
482 if ((*id & match_mask) == *id)
483 goto found; /* Supports all the requested standards */
484
485 nr_matches = hweight64(match_mask);
486 if (nr_matches > best_nr_matches) {
487 best_nr_matches = nr_matches;
488 best_i = i;
489 }
490 }
491
492 if (best_nr_matches > 0) {
493 tuner_dbg("Selecting best matching firmware (%d bits) for "
494 "type=", best_nr_matches);
495 dump_firm_type(type);
496 printk("(%x), id %016llx:\n", type, (unsigned long long)*id);
497 i = best_i;
498 goto found;
499 }
500
501 /*FIXME: Would make sense to seek for type "hint" match ? */
502
503 i = -ENOENT;
504 goto ret;
505
506 found:
507 *id = priv->firm[i].id;
508
509 ret:
510 tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found");
511 if (debug) {
512 dump_firm_type(type);
513 printk("(%x), id %016llx.\n", type, (unsigned long long)*id);
514 }
515 return i;
516 }
517
do_tuner_callback(struct dvb_frontend * fe,int cmd,int arg)518 static inline int do_tuner_callback(struct dvb_frontend *fe, int cmd, int arg)
519 {
520 struct xc2028_data *priv = fe->tuner_priv;
521
522 /* analog side (tuner-core) uses i2c_adap->algo_data.
523 * digital side is not guaranteed to have algo_data defined.
524 *
525 * digital side will always have fe->dvb defined.
526 * analog side (tuner-core) doesn't (yet) define fe->dvb.
527 */
528
529 return (!fe->callback) ? -EINVAL :
530 fe->callback(((fe->dvb) && (fe->dvb->priv)) ?
531 fe->dvb->priv : priv->i2c_props.adap->algo_data,
532 DVB_FRONTEND_COMPONENT_TUNER, cmd, arg);
533 }
534
load_firmware(struct dvb_frontend * fe,unsigned int type,v4l2_std_id * id)535 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
536 v4l2_std_id *id)
537 {
538 struct xc2028_data *priv = fe->tuner_priv;
539 int pos, rc;
540 unsigned char *p, *endp, buf[priv->ctrl.max_len];
541
542 tuner_dbg("%s called\n", __func__);
543
544 pos = seek_firmware(fe, type, id);
545 if (pos < 0)
546 return pos;
547
548 tuner_info("Loading firmware for type=");
549 dump_firm_type(priv->firm[pos].type);
550 printk("(%x), id %016llx.\n", priv->firm[pos].type,
551 (unsigned long long)*id);
552
553 p = priv->firm[pos].ptr;
554 endp = p + priv->firm[pos].size;
555
556 while (p < endp) {
557 __u16 size;
558
559 /* Checks if there's enough bytes to read */
560 if (p + sizeof(size) > endp) {
561 tuner_err("Firmware chunk size is wrong\n");
562 return -EINVAL;
563 }
564
565 size = le16_to_cpu(*(__u16 *) p);
566 p += sizeof(size);
567
568 if (size == 0xffff)
569 return 0;
570
571 if (!size) {
572 /* Special callback command received */
573 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
574 if (rc < 0) {
575 tuner_err("Error at RESET code %d\n",
576 (*p) & 0x7f);
577 return -EINVAL;
578 }
579 continue;
580 }
581 if (size >= 0xff00) {
582 switch (size) {
583 case 0xff00:
584 rc = do_tuner_callback(fe, XC2028_RESET_CLK, 0);
585 if (rc < 0) {
586 tuner_err("Error at RESET code %d\n",
587 (*p) & 0x7f);
588 return -EINVAL;
589 }
590 break;
591 default:
592 tuner_info("Invalid RESET code %d\n",
593 size & 0x7f);
594 return -EINVAL;
595
596 }
597 continue;
598 }
599
600 /* Checks for a sleep command */
601 if (size & 0x8000) {
602 msleep(size & 0x7fff);
603 continue;
604 }
605
606 if ((size + p > endp)) {
607 tuner_err("missing bytes: need %d, have %d\n",
608 size, (int)(endp - p));
609 return -EINVAL;
610 }
611
612 buf[0] = *p;
613 p++;
614 size--;
615
616 /* Sends message chunks */
617 while (size > 0) {
618 int len = (size < priv->ctrl.max_len - 1) ?
619 size : priv->ctrl.max_len - 1;
620
621 memcpy(buf + 1, p, len);
622
623 rc = i2c_send(priv, buf, len + 1);
624 if (rc < 0) {
625 tuner_err("%d returned from send\n", rc);
626 return -EINVAL;
627 }
628
629 p += len;
630 size -= len;
631 }
632
633 /* silently fail if the frontend doesn't support I2C flush */
634 rc = do_tuner_callback(fe, XC2028_I2C_FLUSH, 0);
635 if ((rc < 0) && (rc != -EINVAL)) {
636 tuner_err("error executing flush: %d\n", rc);
637 return rc;
638 }
639 }
640 return 0;
641 }
642
load_scode(struct dvb_frontend * fe,unsigned int type,v4l2_std_id * id,__u16 int_freq,int scode)643 static int load_scode(struct dvb_frontend *fe, unsigned int type,
644 v4l2_std_id *id, __u16 int_freq, int scode)
645 {
646 struct xc2028_data *priv = fe->tuner_priv;
647 int pos, rc;
648 unsigned char *p;
649
650 tuner_dbg("%s called\n", __func__);
651
652 if (!int_freq) {
653 pos = seek_firmware(fe, type, id);
654 if (pos < 0)
655 return pos;
656 } else {
657 for (pos = 0; pos < priv->firm_size; pos++) {
658 if ((priv->firm[pos].int_freq == int_freq) &&
659 (priv->firm[pos].type & HAS_IF))
660 break;
661 }
662 if (pos == priv->firm_size)
663 return -ENOENT;
664 }
665
666 p = priv->firm[pos].ptr;
667
668 if (priv->firm[pos].type & HAS_IF) {
669 if (priv->firm[pos].size != 12 * 16 || scode >= 16)
670 return -EINVAL;
671 p += 12 * scode;
672 } else {
673 /* 16 SCODE entries per file; each SCODE entry is 12 bytes and
674 * has a 2-byte size header in the firmware format. */
675 if (priv->firm[pos].size != 14 * 16 || scode >= 16 ||
676 le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12)
677 return -EINVAL;
678 p += 14 * scode + 2;
679 }
680
681 tuner_info("Loading SCODE for type=");
682 dump_firm_type_and_int_freq(priv->firm[pos].type,
683 priv->firm[pos].int_freq);
684 printk("(%x), id %016llx.\n", priv->firm[pos].type,
685 (unsigned long long)*id);
686
687 if (priv->firm_version < 0x0202)
688 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
689 else
690 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
691 if (rc < 0)
692 return -EIO;
693
694 rc = i2c_send(priv, p, 12);
695 if (rc < 0)
696 return -EIO;
697
698 rc = send_seq(priv, {0x00, 0x8c});
699 if (rc < 0)
700 return -EIO;
701
702 return 0;
703 }
704
check_firmware(struct dvb_frontend * fe,unsigned int type,v4l2_std_id std,__u16 int_freq)705 static int check_firmware(struct dvb_frontend *fe, unsigned int type,
706 v4l2_std_id std, __u16 int_freq)
707 {
708 struct xc2028_data *priv = fe->tuner_priv;
709 struct firmware_properties new_fw;
710 int rc = 0, retry_count = 0;
711 u16 version, hwmodel;
712 v4l2_std_id std0;
713
714 tuner_dbg("%s called\n", __func__);
715
716 if (!priv->firm) {
717 if (!priv->ctrl.fname) {
718 tuner_info("xc2028/3028 firmware name not set!\n");
719 return -EINVAL;
720 }
721
722 rc = load_all_firmwares(fe);
723 if (rc < 0)
724 return rc;
725 }
726
727 if (priv->ctrl.mts && !(type & FM))
728 type |= MTS;
729
730 retry:
731 new_fw.type = type;
732 new_fw.id = std;
733 new_fw.std_req = std;
734 new_fw.scode_table = SCODE | priv->ctrl.scode_table;
735 new_fw.scode_nr = 0;
736 new_fw.int_freq = int_freq;
737
738 tuner_dbg("checking firmware, user requested type=");
739 if (debug) {
740 dump_firm_type(new_fw.type);
741 printk("(%x), id %016llx, ", new_fw.type,
742 (unsigned long long)new_fw.std_req);
743 if (!int_freq) {
744 printk("scode_tbl ");
745 dump_firm_type(priv->ctrl.scode_table);
746 printk("(%x), ", priv->ctrl.scode_table);
747 } else
748 printk("int_freq %d, ", new_fw.int_freq);
749 printk("scode_nr %d\n", new_fw.scode_nr);
750 }
751
752 /* No need to reload base firmware if it matches */
753 if (((BASE | new_fw.type) & BASE_TYPES) ==
754 (priv->cur_fw.type & BASE_TYPES)) {
755 tuner_dbg("BASE firmware not changed.\n");
756 goto skip_base;
757 }
758
759 /* Updating BASE - forget about all currently loaded firmware */
760 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
761
762 /* Reset is needed before loading firmware */
763 rc = do_tuner_callback(fe, XC2028_TUNER_RESET, 0);
764 if (rc < 0)
765 goto fail;
766
767 /* BASE firmwares are all std0 */
768 std0 = 0;
769 rc = load_firmware(fe, BASE | new_fw.type, &std0);
770 if (rc < 0) {
771 tuner_err("Error %d while loading base firmware\n",
772 rc);
773 goto fail;
774 }
775
776 /* Load INIT1, if needed */
777 tuner_dbg("Load init1 firmware, if exists\n");
778
779 rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0);
780 if (rc == -ENOENT)
781 rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ,
782 &std0);
783 if (rc < 0 && rc != -ENOENT) {
784 tuner_err("Error %d while loading init1 firmware\n",
785 rc);
786 goto fail;
787 }
788
789 skip_base:
790 /*
791 * No need to reload standard specific firmware if base firmware
792 * was not reloaded and requested video standards have not changed.
793 */
794 if (priv->cur_fw.type == (BASE | new_fw.type) &&
795 priv->cur_fw.std_req == std) {
796 tuner_dbg("Std-specific firmware already loaded.\n");
797 goto skip_std_specific;
798 }
799
800 /* Reloading std-specific firmware forces a SCODE update */
801 priv->cur_fw.scode_table = 0;
802
803 rc = load_firmware(fe, new_fw.type, &new_fw.id);
804 if (rc == -ENOENT)
805 rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id);
806
807 if (rc < 0)
808 goto fail;
809
810 skip_std_specific:
811 if (priv->cur_fw.scode_table == new_fw.scode_table &&
812 priv->cur_fw.scode_nr == new_fw.scode_nr) {
813 tuner_dbg("SCODE firmware already loaded.\n");
814 goto check_device;
815 }
816
817 if (new_fw.type & FM)
818 goto check_device;
819
820 /* Load SCODE firmware, if exists */
821 tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr);
822
823 rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id,
824 new_fw.int_freq, new_fw.scode_nr);
825
826 check_device:
827 if (xc2028_get_reg(priv, 0x0004, &version) < 0 ||
828 xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) {
829 tuner_err("Unable to read tuner registers.\n");
830 goto fail;
831 }
832
833 tuner_dbg("Device is Xceive %d version %d.%d, "
834 "firmware version %d.%d\n",
835 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
836 (version & 0xf0) >> 4, version & 0xf);
837
838
839 if (priv->ctrl.read_not_reliable)
840 goto read_not_reliable;
841
842 /* Check firmware version against what we downloaded. */
843 if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) {
844 if (!priv->ctrl.read_not_reliable) {
845 tuner_err("Incorrect readback of firmware version.\n");
846 goto fail;
847 } else {
848 tuner_err("Returned an incorrect version. However, "
849 "read is not reliable enough. Ignoring it.\n");
850 hwmodel = 3028;
851 }
852 }
853
854 /* Check that the tuner hardware model remains consistent over time. */
855 if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) {
856 priv->hwmodel = hwmodel;
857 priv->hwvers = version & 0xff00;
858 } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel ||
859 priv->hwvers != (version & 0xff00)) {
860 tuner_err("Read invalid device hardware information - tuner "
861 "hung?\n");
862 goto fail;
863 }
864
865 read_not_reliable:
866 memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw));
867
868 /*
869 * By setting BASE in cur_fw.type only after successfully loading all
870 * firmwares, we can:
871 * 1. Identify that BASE firmware with type=0 has been loaded;
872 * 2. Tell whether BASE firmware was just changed the next time through.
873 */
874 priv->cur_fw.type |= BASE;
875
876 return 0;
877
878 fail:
879 memset(&priv->cur_fw, 0, sizeof(priv->cur_fw));
880 if (retry_count < 8) {
881 msleep(50);
882 retry_count++;
883 tuner_dbg("Retrying firmware load\n");
884 goto retry;
885 }
886
887 if (rc == -ENOENT)
888 rc = -EINVAL;
889 return rc;
890 }
891
xc2028_signal(struct dvb_frontend * fe,u16 * strength)892 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
893 {
894 struct xc2028_data *priv = fe->tuner_priv;
895 u16 frq_lock, signal = 0;
896 int rc;
897
898 tuner_dbg("%s called\n", __func__);
899
900 mutex_lock(&priv->lock);
901
902 /* Sync Lock Indicator */
903 rc = xc2028_get_reg(priv, XREG_LOCK, &frq_lock);
904 if (rc < 0)
905 goto ret;
906
907 /* Frequency is locked */
908 if (frq_lock == 1)
909 signal = 1 << 11;
910
911 /* Get SNR of the video signal */
912 rc = xc2028_get_reg(priv, XREG_SNR, &signal);
913 if (rc < 0)
914 goto ret;
915
916 /* Use both frq_lock and signal to generate the result */
917 signal = signal || ((signal & 0x07) << 12);
918
919 ret:
920 mutex_unlock(&priv->lock);
921
922 *strength = signal;
923
924 tuner_dbg("signal strength is %d\n", signal);
925
926 return rc;
927 }
928
929 #define DIV 15625
930
generic_set_freq(struct dvb_frontend * fe,u32 freq,enum v4l2_tuner_type new_type,unsigned int type,v4l2_std_id std,u16 int_freq)931 static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */,
932 enum v4l2_tuner_type new_type,
933 unsigned int type,
934 v4l2_std_id std,
935 u16 int_freq)
936 {
937 struct xc2028_data *priv = fe->tuner_priv;
938 int rc = -EINVAL;
939 unsigned char buf[4];
940 u32 div, offset = 0;
941
942 tuner_dbg("%s called\n", __func__);
943
944 mutex_lock(&priv->lock);
945
946 tuner_dbg("should set frequency %d kHz\n", freq / 1000);
947
948 if (check_firmware(fe, type, std, int_freq) < 0)
949 goto ret;
950
951 /* On some cases xc2028 can disable video output, if
952 * very weak signals are received. By sending a soft
953 * reset, this is re-enabled. So, it is better to always
954 * send a soft reset before changing channels, to be sure
955 * that xc2028 will be in a safe state.
956 * Maybe this might also be needed for DTV.
957 */
958 switch (new_type) {
959 case V4L2_TUNER_ANALOG_TV:
960 rc = send_seq(priv, {0x00, 0x00});
961
962 /* Analog mode requires offset = 0 */
963 break;
964 case V4L2_TUNER_RADIO:
965 /* Radio mode requires offset = 0 */
966 break;
967 case V4L2_TUNER_DIGITAL_TV:
968 /*
969 * Digital modes require an offset to adjust to the
970 * proper frequency. The offset depends on what
971 * firmware version is used.
972 */
973
974 /*
975 * Adjust to the center frequency. This is calculated by the
976 * formula: offset = 1.25MHz - BW/2
977 * For DTV 7/8, the firmware uses BW = 8000, so it needs a
978 * further adjustment to get the frequency center on VHF
979 */
980
981 /*
982 * The firmware DTV78 used to work fine in UHF band (8 MHz
983 * bandwidth) but not at all in VHF band (7 MHz bandwidth).
984 * The real problem was connected to the formula used to
985 * calculate the center frequency offset in VHF band.
986 * In fact, removing the 500KHz adjustment fixed the problem.
987 * This is coherent to what was implemented for the DTV7
988 * firmware.
989 * In the end, now the center frequency is the same for all 3
990 * firmwares (DTV7, DTV8, DTV78) and doesn't depend on channel
991 * bandwidth.
992 */
993
994 if (priv->cur_fw.type & DTV6)
995 offset = 1750000;
996 else /* DTV7 or DTV8 or DTV78 */
997 offset = 2750000;
998
999 /*
1000 * xc3028 additional "magic"
1001 * Depending on the firmware version, it needs some adjustments
1002 * to properly centralize the frequency. This seems to be
1003 * needed to compensate the SCODE table adjustments made by
1004 * newer firmwares
1005 */
1006
1007 /*
1008 * The proper adjustment would be to do it at s-code table.
1009 * However, this didn't work, as reported by
1010 * Robert Lowery <rglowery@exemail.com.au>
1011 */
1012
1013 #if 0
1014 /*
1015 * Still need tests for XC3028L (firmware 3.2 or upper)
1016 * So, for now, let's just comment the per-firmware
1017 * version of this change. Reports with xc3028l working
1018 * with and without the lines bellow are welcome
1019 */
1020
1021 if (priv->firm_version < 0x0302) {
1022 if (priv->cur_fw.type & DTV7)
1023 offset += 500000;
1024 } else {
1025 if (priv->cur_fw.type & DTV7)
1026 offset -= 300000;
1027 else if (type != ATSC) /* DVB @6MHz, DTV 8 and DTV 7/8 */
1028 offset += 200000;
1029 }
1030 #endif
1031 }
1032
1033 div = (freq - offset + DIV / 2) / DIV;
1034
1035 /* CMD= Set frequency */
1036 if (priv->firm_version < 0x0202)
1037 rc = send_seq(priv, {0x00, XREG_RF_FREQ, 0x00, 0x00});
1038 else
1039 rc = send_seq(priv, {0x80, XREG_RF_FREQ, 0x00, 0x00});
1040 if (rc < 0)
1041 goto ret;
1042
1043 /* Return code shouldn't be checked.
1044 The reset CLK is needed only with tm6000.
1045 Driver should work fine even if this fails.
1046 */
1047 if (priv->ctrl.msleep)
1048 msleep(priv->ctrl.msleep);
1049 do_tuner_callback(fe, XC2028_RESET_CLK, 1);
1050
1051 msleep(10);
1052
1053 buf[0] = 0xff & (div >> 24);
1054 buf[1] = 0xff & (div >> 16);
1055 buf[2] = 0xff & (div >> 8);
1056 buf[3] = 0xff & (div);
1057
1058 rc = i2c_send(priv, buf, sizeof(buf));
1059 if (rc < 0)
1060 goto ret;
1061 msleep(100);
1062
1063 priv->frequency = freq;
1064
1065 tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n",
1066 buf[0], buf[1], buf[2], buf[3],
1067 freq / 1000000, (freq % 1000000) / 1000);
1068
1069 rc = 0;
1070
1071 ret:
1072 mutex_unlock(&priv->lock);
1073
1074 return rc;
1075 }
1076
xc2028_set_analog_freq(struct dvb_frontend * fe,struct analog_parameters * p)1077 static int xc2028_set_analog_freq(struct dvb_frontend *fe,
1078 struct analog_parameters *p)
1079 {
1080 struct xc2028_data *priv = fe->tuner_priv;
1081 unsigned int type=0;
1082
1083 tuner_dbg("%s called\n", __func__);
1084
1085 if (p->mode == V4L2_TUNER_RADIO) {
1086 type |= FM;
1087 if (priv->ctrl.input1)
1088 type |= INPUT1;
1089 return generic_set_freq(fe, (625l * p->frequency) / 10,
1090 V4L2_TUNER_RADIO, type, 0, 0);
1091 }
1092
1093 /* if std is not defined, choose one */
1094 if (!p->std)
1095 p->std = V4L2_STD_MN;
1096
1097 /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */
1098 if (!(p->std & V4L2_STD_MN))
1099 type |= F8MHZ;
1100
1101 /* Add audio hack to std mask */
1102 p->std |= parse_audio_std_option();
1103
1104 return generic_set_freq(fe, 62500l * p->frequency,
1105 V4L2_TUNER_ANALOG_TV, type, p->std, 0);
1106 }
1107
xc2028_set_params(struct dvb_frontend * fe)1108 static int xc2028_set_params(struct dvb_frontend *fe)
1109 {
1110 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1111 u32 delsys = c->delivery_system;
1112 u32 bw = c->bandwidth_hz;
1113 struct xc2028_data *priv = fe->tuner_priv;
1114 unsigned int type=0;
1115 u16 demod = 0;
1116
1117 tuner_dbg("%s called\n", __func__);
1118
1119 switch (delsys) {
1120 case SYS_DVBT:
1121 case SYS_DVBT2:
1122 /*
1123 * The only countries with 6MHz seem to be Taiwan/Uruguay.
1124 * Both seem to require QAM firmware for OFDM decoding
1125 * Tested in Taiwan by Terry Wu <terrywu2009@gmail.com>
1126 */
1127 if (bw <= 6000000)
1128 type |= QAM;
1129
1130 switch (priv->ctrl.type) {
1131 case XC2028_D2633:
1132 type |= D2633;
1133 break;
1134 case XC2028_D2620:
1135 type |= D2620;
1136 break;
1137 case XC2028_AUTO:
1138 default:
1139 /* Zarlink seems to need D2633 */
1140 if (priv->ctrl.demod == XC3028_FE_ZARLINK456)
1141 type |= D2633;
1142 else
1143 type |= D2620;
1144 }
1145 break;
1146 case SYS_ATSC:
1147 /* The only ATSC firmware (at least on v2.7) is D2633 */
1148 type |= ATSC | D2633;
1149 break;
1150 /* DVB-S and pure QAM (FE_QAM) are not supported */
1151 default:
1152 return -EINVAL;
1153 }
1154
1155 if (bw <= 6000000) {
1156 type |= DTV6;
1157 priv->ctrl.vhfbw7 = 0;
1158 priv->ctrl.uhfbw8 = 0;
1159 } else if (bw <= 7000000) {
1160 if (c->frequency < 470000000)
1161 priv->ctrl.vhfbw7 = 1;
1162 else
1163 priv->ctrl.uhfbw8 = 0;
1164 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV7;
1165 type |= F8MHZ;
1166 } else {
1167 if (c->frequency < 470000000)
1168 priv->ctrl.vhfbw7 = 0;
1169 else
1170 priv->ctrl.uhfbw8 = 1;
1171 type |= (priv->ctrl.vhfbw7 && priv->ctrl.uhfbw8) ? DTV78 : DTV8;
1172 type |= F8MHZ;
1173 }
1174
1175 /* All S-code tables need a 200kHz shift */
1176 if (priv->ctrl.demod) {
1177 demod = priv->ctrl.demod;
1178
1179 /*
1180 * Newer firmwares require a 200 kHz offset only for ATSC
1181 */
1182 if (type == ATSC || priv->firm_version < 0x0302)
1183 demod += 200;
1184 /*
1185 * The DTV7 S-code table needs a 700 kHz shift.
1186 *
1187 * DTV7 is only used in Australia. Germany or Italy may also
1188 * use this firmware after initialization, but a tune to a UHF
1189 * channel should then cause DTV78 to be used.
1190 *
1191 * Unfortunately, on real-field tests, the s-code offset
1192 * didn't work as expected, as reported by
1193 * Robert Lowery <rglowery@exemail.com.au>
1194 */
1195 }
1196
1197 return generic_set_freq(fe, c->frequency,
1198 V4L2_TUNER_DIGITAL_TV, type, 0, demod);
1199 }
1200
xc2028_sleep(struct dvb_frontend * fe)1201 static int xc2028_sleep(struct dvb_frontend *fe)
1202 {
1203 struct xc2028_data *priv = fe->tuner_priv;
1204 int rc = 0;
1205
1206 /* Avoid firmware reload on slow devices or if PM disabled */
1207 if (no_poweroff || priv->ctrl.disable_power_mgmt)
1208 return 0;
1209
1210 tuner_dbg("Putting xc2028/3028 into poweroff mode.\n");
1211 if (debug > 1) {
1212 tuner_dbg("Printing sleep stack trace:\n");
1213 dump_stack();
1214 }
1215
1216 mutex_lock(&priv->lock);
1217
1218 if (priv->firm_version < 0x0202)
1219 rc = send_seq(priv, {0x00, XREG_POWER_DOWN, 0x00, 0x00});
1220 else
1221 rc = send_seq(priv, {0x80, XREG_POWER_DOWN, 0x00, 0x00});
1222
1223 priv->cur_fw.type = 0; /* need firmware reload */
1224
1225 mutex_unlock(&priv->lock);
1226
1227 return rc;
1228 }
1229
xc2028_dvb_release(struct dvb_frontend * fe)1230 static int xc2028_dvb_release(struct dvb_frontend *fe)
1231 {
1232 struct xc2028_data *priv = fe->tuner_priv;
1233
1234 tuner_dbg("%s called\n", __func__);
1235
1236 mutex_lock(&xc2028_list_mutex);
1237
1238 /* only perform final cleanup if this is the last instance */
1239 if (hybrid_tuner_report_instance_count(priv) == 1) {
1240 kfree(priv->ctrl.fname);
1241 free_firmware(priv);
1242 }
1243
1244 if (priv)
1245 hybrid_tuner_release_state(priv);
1246
1247 mutex_unlock(&xc2028_list_mutex);
1248
1249 fe->tuner_priv = NULL;
1250
1251 return 0;
1252 }
1253
xc2028_get_frequency(struct dvb_frontend * fe,u32 * frequency)1254 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1255 {
1256 struct xc2028_data *priv = fe->tuner_priv;
1257
1258 tuner_dbg("%s called\n", __func__);
1259
1260 *frequency = priv->frequency;
1261
1262 return 0;
1263 }
1264
xc2028_set_config(struct dvb_frontend * fe,void * priv_cfg)1265 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
1266 {
1267 struct xc2028_data *priv = fe->tuner_priv;
1268 struct xc2028_ctrl *p = priv_cfg;
1269 int rc = 0;
1270
1271 tuner_dbg("%s called\n", __func__);
1272
1273 mutex_lock(&priv->lock);
1274
1275 memcpy(&priv->ctrl, p, sizeof(priv->ctrl));
1276 if (priv->ctrl.max_len < 9)
1277 priv->ctrl.max_len = 13;
1278
1279 if (p->fname) {
1280 if (priv->ctrl.fname && strcmp(p->fname, priv->ctrl.fname)) {
1281 kfree(priv->ctrl.fname);
1282 free_firmware(priv);
1283 }
1284
1285 priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL);
1286 if (priv->ctrl.fname == NULL)
1287 rc = -ENOMEM;
1288 }
1289
1290 mutex_unlock(&priv->lock);
1291
1292 return rc;
1293 }
1294
1295 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
1296 .info = {
1297 .name = "Xceive XC3028",
1298 .frequency_min = 42000000,
1299 .frequency_max = 864000000,
1300 .frequency_step = 50000,
1301 },
1302
1303 .set_config = xc2028_set_config,
1304 .set_analog_params = xc2028_set_analog_freq,
1305 .release = xc2028_dvb_release,
1306 .get_frequency = xc2028_get_frequency,
1307 .get_rf_strength = xc2028_signal,
1308 .set_params = xc2028_set_params,
1309 .sleep = xc2028_sleep,
1310 };
1311
xc2028_attach(struct dvb_frontend * fe,struct xc2028_config * cfg)1312 struct dvb_frontend *xc2028_attach(struct dvb_frontend *fe,
1313 struct xc2028_config *cfg)
1314 {
1315 struct xc2028_data *priv;
1316 int instance;
1317
1318 if (debug)
1319 printk(KERN_DEBUG "xc2028: Xcv2028/3028 init called!\n");
1320
1321 if (NULL == cfg)
1322 return NULL;
1323
1324 if (!fe) {
1325 printk(KERN_ERR "xc2028: No frontend!\n");
1326 return NULL;
1327 }
1328
1329 mutex_lock(&xc2028_list_mutex);
1330
1331 instance = hybrid_tuner_request_state(struct xc2028_data, priv,
1332 hybrid_tuner_instance_list,
1333 cfg->i2c_adap, cfg->i2c_addr,
1334 "xc2028");
1335 switch (instance) {
1336 case 0:
1337 /* memory allocation failure */
1338 goto fail;
1339 break;
1340 case 1:
1341 /* new tuner instance */
1342 priv->ctrl.max_len = 13;
1343
1344 mutex_init(&priv->lock);
1345
1346 fe->tuner_priv = priv;
1347 break;
1348 case 2:
1349 /* existing tuner instance */
1350 fe->tuner_priv = priv;
1351 break;
1352 }
1353
1354 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
1355 sizeof(xc2028_dvb_tuner_ops));
1356
1357 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
1358
1359 if (cfg->ctrl)
1360 xc2028_set_config(fe, cfg->ctrl);
1361
1362 mutex_unlock(&xc2028_list_mutex);
1363
1364 return fe;
1365 fail:
1366 mutex_unlock(&xc2028_list_mutex);
1367
1368 xc2028_dvb_release(fe);
1369 return NULL;
1370 }
1371
1372 EXPORT_SYMBOL(xc2028_attach);
1373
1374 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
1375 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
1376 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
1377 MODULE_LICENSE("GPL");
1378