]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/media/video/tuner-xc2028.c
V4L/DVB (6633): xc2028: make register reads atomic
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / video / tuner-xc2028.c
1 /* tuner-xc2028
2 *
3 * Copyright (c) 2007 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 "tuner-i2c.h"
19 #include "tuner-xc2028.h"
20 #include "tuner-xc2028-types.h"
21
22 #include <linux/dvb/frontend.h>
23 #include "dvb_frontend.h"
24
25
26 #define PREFIX "xc2028"
27
28 static int debug;
29 module_param(debug, int, 0644);
30 MODULE_PARM_DESC(debug, "enable verbose debug messages");
31
32 static char audio_std[8];
33 module_param_string(audio_std, audio_std, sizeof(audio_std), 0);
34 MODULE_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
46 static LIST_HEAD(xc2028_list);
47 /* struct for storing firmware table */
48 struct firmware_description {
49 unsigned int type;
50 v4l2_std_id id;
51 unsigned char *ptr;
52 unsigned int size;
53 };
54
55 struct xc2028_data {
56 struct list_head xc2028_list;
57 struct tuner_i2c_props i2c_props;
58 int (*tuner_callback) (void *dev,
59 int command, int arg);
60 void *video_dev;
61 int count;
62 __u32 frequency;
63
64 struct firmware_description *firm;
65 int firm_size;
66
67 __u16 version;
68
69 struct xc2028_ctrl ctrl;
70
71 v4l2_std_id firm_type; /* video stds supported
72 by current firmware */
73 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
74 6M, 7M or 8M */
75 int need_load_generic; /* The generic firmware
76 were loaded? */
77
78 int max_len; /* Max firmware chunk */
79
80 enum tuner_mode mode;
81 struct i2c_client *i2c_client;
82
83 struct mutex lock;
84 };
85
86 #define i2c_send(priv, buf, size) ({ \
87 int _rc; \
88 _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \
89 if (size != _rc) \
90 tuner_info("i2c output error: rc = %d (should be %d)\n",\
91 _rc, (int)size); \
92 _rc; \
93 })
94
95 #define i2c_rcv(priv, buf, size) ({ \
96 int _rc; \
97 _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \
98 if (size != _rc) \
99 tuner_err("i2c input error: rc = %d (should be %d)\n", \
100 _rc, (int)size); \
101 _rc; \
102 })
103
104 #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \
105 int _rc; \
106 _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \
107 ibuf, isize); \
108 if (isize != _rc) \
109 tuner_err("i2c input error: rc = %d (should be %d)\n", \
110 _rc, (int)isize); \
111 _rc; \
112 })
113
114 #define send_seq(priv, data...) ({ \
115 static u8 _val[] = data; \
116 int _rc; \
117 if (sizeof(_val) != \
118 (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \
119 _val, sizeof(_val)))) { \
120 tuner_err("Error on line %d: %d\n", __LINE__, _rc); \
121 } else \
122 msleep(10); \
123 _rc; \
124 })
125
126 static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val)
127 {
128 unsigned char buf[2];
129 unsigned char ibuf[2];
130
131 tuner_dbg("%s %04x called\n", __FUNCTION__, reg);
132
133 buf[0] = reg >> 8;
134 buf[1] = (unsigned char) reg;
135
136 if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2)
137 return -EIO;
138
139 *val = (ibuf[1]) | (ibuf[0] << 8);
140 return 0;
141 }
142
143 void dump_firm_type(unsigned int type)
144 {
145 if (type & BASE)
146 printk("BASE ");
147 if (type & INIT1)
148 printk("INIT1 ");
149 if (type & F8MHZ)
150 printk("F8MHZ ");
151 if (type & MTS)
152 printk("MTS ");
153 if (type & D2620)
154 printk("D2620 ");
155 if (type & D2633)
156 printk("D2633 ");
157 if (type & DTV6)
158 printk("DTV6 ");
159 if (type & QAM)
160 printk("QAM ");
161 if (type & DTV7)
162 printk("DTV7 ");
163 if (type & DTV78)
164 printk("DTV78 ");
165 if (type & DTV8)
166 printk("DTV8 ");
167 if (type & FM)
168 printk("FM ");
169 if (type & INPUT1)
170 printk("INPUT1 ");
171 if (type & LCD)
172 printk("LCD ");
173 if (type & NOGD)
174 printk("NOGD ");
175 if (type & MONO)
176 printk("MONO ");
177 if (type & ATSC)
178 printk("ATSC ");
179 if (type & IF)
180 printk("IF ");
181 if (type & LG60)
182 printk("LG60 ");
183 if (type & ATI638)
184 printk("ATI638 ");
185 if (type & OREN538)
186 printk("OREN538 ");
187 if (type & OREN36)
188 printk("OREN36 ");
189 if (type & TOYOTA388)
190 printk("TOYOTA388 ");
191 if (type & TOYOTA794)
192 printk("TOYOTA794 ");
193 if (type & DIBCOM52)
194 printk("DIBCOM52 ");
195 if (type & ZARLINK456)
196 printk("ZARLINK456 ");
197 if (type & CHINA)
198 printk("CHINA ");
199 if (type & F6MHZ)
200 printk("F6MHZ ");
201 if (type & INPUT2)
202 printk("INPUT2 ");
203 if (type & SCODE)
204 printk("SCODE ");
205 }
206
207 static v4l2_std_id parse_audio_std_option(void)
208 {
209 if (strcasecmp(audio_std, "A2") == 0)
210 return V4L2_STD_A2;
211 if (strcasecmp(audio_std, "A2/A") == 0)
212 return V4L2_STD_A2_A;
213 if (strcasecmp(audio_std, "A2/B") == 0)
214 return V4L2_STD_A2_B;
215 if (strcasecmp(audio_std, "NICAM") == 0)
216 return V4L2_STD_NICAM;
217 if (strcasecmp(audio_std, "NICAM/A") == 0)
218 return V4L2_STD_NICAM_A;
219 if (strcasecmp(audio_std, "NICAM/B") == 0)
220 return V4L2_STD_NICAM_B;
221
222 return 0;
223 }
224
225 static void free_firmware(struct xc2028_data *priv)
226 {
227 int i;
228
229 if (!priv->firm)
230 return;
231
232 for (i = 0; i < priv->firm_size; i++)
233 kfree(priv->firm[i].ptr);
234
235 kfree(priv->firm);
236
237 priv->firm = NULL;
238 priv->need_load_generic = 1;
239 }
240
241 static int load_all_firmwares(struct dvb_frontend *fe)
242 {
243 struct xc2028_data *priv = fe->tuner_priv;
244 const struct firmware *fw = NULL;
245 unsigned char *p, *endp;
246 int rc = 0;
247 int n, n_array;
248 char name[33];
249
250 tuner_dbg("%s called\n", __FUNCTION__);
251
252 tuner_info("Reading firmware %s\n", priv->ctrl.fname);
253 rc = request_firmware(&fw, priv->ctrl.fname,
254 &priv->i2c_props.adap->dev);
255 if (rc < 0) {
256 if (rc == -ENOENT)
257 tuner_err("Error: firmware %s not found.\n",
258 priv->ctrl.fname);
259 else
260 tuner_err("Error %d while requesting firmware %s \n",
261 rc, priv->ctrl.fname);
262
263 return rc;
264 }
265 p = fw->data;
266 endp = p + fw->size;
267
268 if (fw->size < sizeof(name) - 1 + 2) {
269 tuner_err("Error: firmware size is zero!\n");
270 rc = -EINVAL;
271 goto done;
272 }
273
274 memcpy(name, p, sizeof(name) - 1);
275 name[sizeof(name) - 1] = 0;
276 p += sizeof(name) - 1;
277
278 priv->version = le16_to_cpu(*(__u16 *) p);
279 p += 2;
280
281 tuner_info("Firmware: %s, ver %d.%d\n", name,
282 priv->version >> 8, priv->version & 0xff);
283
284 if (p + 2 > endp)
285 goto corrupt;
286
287 n_array = le16_to_cpu(*(__u16 *) p);
288 p += 2;
289
290 tuner_info("There are %d firmwares at %s\n",
291 n_array, priv->ctrl.fname);
292
293 priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL);
294
295 if (!fw) {
296 tuner_err("Not enough memory for reading firmware.\n");
297 rc = -ENOMEM;
298 goto done;
299 }
300
301 priv->firm_size = n_array;
302 n = -1;
303 while (p < endp) {
304 __u32 type, size;
305 v4l2_std_id id;
306
307 n++;
308 if (n >= n_array) {
309 tuner_err("Too much firmwares at the file\n");
310 goto corrupt;
311 }
312
313 /* Checks if there's enough bytes to read */
314 if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) {
315 tuner_err("Firmware header is incomplete!\n");
316 goto corrupt;
317 }
318
319 type = le32_to_cpu(*(__u32 *) p);
320 p += sizeof(type);
321
322 id = le64_to_cpu(*(v4l2_std_id *) p);
323 p += sizeof(id);
324
325 size = le32_to_cpu(*(__u32 *) p);
326 p += sizeof(size);
327
328 if ((!size) || (size + p > endp)) {
329 tuner_err("Firmware type ");
330 dump_firm_type(type);
331 printk("(%x), id %llx is corrupted "
332 "(size=%d, expected %d)\n",
333 type, id,
334 (unsigned)(endp - p), size);
335 goto corrupt;
336 }
337
338 priv->firm[n].ptr = kzalloc(size, GFP_KERNEL);
339 if (!priv->firm[n].ptr) {
340 tuner_err("Not enough memory.\n");
341 rc = -ENOMEM;
342 goto err;
343 }
344 tuner_info("Reading firmware type ");
345 dump_firm_type(type);
346 printk("(%x), id %lx, size=%d.\n",
347 type, (unsigned long)id, size);
348
349 memcpy(priv->firm[n].ptr, p, size);
350 priv->firm[n].type = type;
351 priv->firm[n].id = id;
352 priv->firm[n].size = size;
353
354 p += size;
355 }
356
357 if (n + 1 != priv->firm_size) {
358 tuner_err("Firmware file is incomplete!\n");
359 goto corrupt;
360 }
361
362 goto done;
363
364 corrupt:
365 rc = -EINVAL;
366 tuner_err("Error: firmware file is corrupted!\n");
367
368 err:
369 tuner_info("Releasing loaded firmware file.\n");
370
371 free_firmware(priv);
372
373 done:
374 release_firmware(fw);
375 tuner_dbg("Firmware files loaded.\n");
376
377 return rc;
378 }
379
380 static int seek_firmware(struct dvb_frontend *fe, unsigned int type,
381 v4l2_std_id *id)
382 {
383 struct xc2028_data *priv = fe->tuner_priv;
384 int i;
385
386 tuner_dbg("%s called\n", __FUNCTION__);
387
388 if (!priv->firm) {
389 tuner_err("Error! firmware not loaded\n");
390 return -EINVAL;
391 }
392
393 if (((type & ~SCODE) == 0) && (*id == 0))
394 *id = V4L2_STD_PAL;
395
396 /* Seek for exact match */
397 for (i = 0; i < priv->firm_size; i++) {
398 if ((type == priv->firm[i].type) && (*id == priv->firm[i].id))
399 goto found;
400 }
401
402 /* Seek for generic video standard match */
403 for (i = 0; i < priv->firm_size; i++) {
404 if ((type == priv->firm[i].type) && (*id & priv->firm[i].id))
405 goto found;
406 }
407
408 /*FIXME: Would make sense to seek for type "hint" match ? */
409
410 i = -EINVAL;
411 goto ret;
412
413 found:
414 *id = priv->firm[i].id;
415
416 ret:
417 tuner_dbg("%s firmware for type=", (i < 0)? "Can't find": "Found");
418 if (debug) {
419 dump_firm_type(type);
420 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
421 }
422 return i;
423 }
424
425 static int load_firmware(struct dvb_frontend *fe, unsigned int type,
426 v4l2_std_id *id)
427 {
428 struct xc2028_data *priv = fe->tuner_priv;
429 int pos, rc;
430 unsigned char *p, *endp, buf[priv->max_len];
431
432 tuner_dbg("%s called\n", __FUNCTION__);
433
434 pos = seek_firmware(fe, type, id);
435 if (pos < 0)
436 return pos;
437
438 tuner_info("Loading firmware for type=");
439 dump_firm_type(type);
440 printk("(%x), id %08lx.\n", type, (unsigned long)*id);
441
442 p = priv->firm[pos].ptr;
443
444 if (!p) {
445 tuner_err("Firmware pointer were freed!");
446 return -EINVAL;
447 }
448 endp = p + priv->firm[pos].size;
449
450 while (p < endp) {
451 __u16 size;
452
453 /* Checks if there's enough bytes to read */
454 if (p + sizeof(size) > endp) {
455 tuner_err("Firmware chunk size is wrong\n");
456 return -EINVAL;
457 }
458
459 size = le16_to_cpu(*(__u16 *) p);
460 p += sizeof(size);
461
462 if (size == 0xffff)
463 return 0;
464
465 if (!size) {
466 /* Special callback command received */
467 rc = priv->tuner_callback(priv->video_dev,
468 XC2028_TUNER_RESET, 0);
469 if (rc < 0) {
470 tuner_err("Error at RESET code %d\n",
471 (*p) & 0x7f);
472 return -EINVAL;
473 }
474 continue;
475 }
476 if (size >= 0xff00) {
477 switch (size) {
478 case 0xff00:
479 rc = priv->tuner_callback(priv->video_dev,
480 XC2028_RESET_CLK, 0);
481 if (rc < 0) {
482 tuner_err("Error at RESET code %d\n",
483 (*p) & 0x7f);
484 return -EINVAL;
485 }
486 default:
487 tuner_info("Invalid RESET code %d\n",
488 size & 0x7f);
489 return -EINVAL;
490
491 }
492 continue;
493 }
494
495 /* Checks for a sleep command */
496 if (size & 0x8000) {
497 msleep(size & 0x7fff);
498 continue;
499 }
500
501 if ((size + p > endp)) {
502 tuner_err("missing bytes: need %d, have %d\n",
503 size, (int)(endp - p));
504 return -EINVAL;
505 }
506
507 buf[0] = *p;
508 p++;
509 size--;
510
511 /* Sends message chunks */
512 while (size > 0) {
513 int len = (size < priv->max_len - 1) ?
514 size : priv->max_len - 1;
515
516 memcpy(buf + 1, p, len);
517
518 rc = i2c_send(priv, buf, len + 1);
519 if (rc < 0) {
520 tuner_err("%d returned from send\n", rc);
521 return -EINVAL;
522 }
523
524 p += len;
525 size -= len;
526 }
527 }
528 return 0;
529 }
530
531 static int load_scode(struct dvb_frontend *fe, unsigned int type,
532 v4l2_std_id *id, int scode)
533 {
534 struct xc2028_data *priv = fe->tuner_priv;
535 int pos, rc;
536 unsigned char *p;
537
538 tuner_dbg("%s called\n", __FUNCTION__);
539
540 pos = seek_firmware(fe, type, id);
541 if (pos < 0)
542 return pos;
543
544 p = priv->firm[pos].ptr;
545
546 if (!p) {
547 tuner_err("Firmware pointer were freed!");
548 return -EINVAL;
549 }
550
551 if ((priv->firm[pos].size != 12 * 16) || (scode >= 16))
552 return -EINVAL;
553
554 if (priv->version < 0x0202)
555 rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00});
556 else
557 rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00});
558 if (rc < 0)
559 return -EIO;
560
561 rc = i2c_send(priv, p + 12 * scode, 12);
562 if (rc < 0)
563 return -EIO;
564
565 rc = send_seq(priv, {0x00, 0x8c});
566 if (rc < 0)
567 return -EIO;
568
569 return 0;
570 }
571
572 static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode,
573 v4l2_std_id std, fe_bandwidth_t bandwidth)
574 {
575 struct xc2028_data *priv = fe->tuner_priv;
576 int rc;
577 u16 version, hwmodel;
578 v4l2_std_id std0 = 0;
579 unsigned int type0 = 0, type = 0;
580 int change_digital_bandwidth;
581
582 tuner_dbg("%s called\n", __FUNCTION__);
583
584 if (!priv->firm) {
585 if (!priv->ctrl.fname) {
586 tuner_info("xc2028/3028 firmware name not set!\n");
587 return -EINVAL;
588 }
589
590 rc = load_all_firmwares(fe);
591 if (rc < 0)
592 return rc;
593 }
594
595 tuner_dbg("I am in mode %u and I should switch to mode %i\n",
596 priv->mode, new_mode);
597
598 /* first of all, determine whether we have switched the mode */
599 if (new_mode != priv->mode) {
600 priv->mode = new_mode;
601 priv->need_load_generic = 1;
602 }
603
604 change_digital_bandwidth = (priv->mode == T_DIGITAL_TV
605 && bandwidth != priv->bandwidth) ? 1 : 0;
606 tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth,
607 bandwidth);
608
609 if (priv->need_load_generic) {
610 /* Reset is needed before loading firmware */
611 rc = priv->tuner_callback(priv->video_dev,
612 XC2028_TUNER_RESET, 0);
613 if (rc < 0)
614 return rc;
615
616 type0 = BASE;
617
618 if (priv->ctrl.type == XC2028_FIRM_MTS)
619 type0 |= MTS;
620
621 if (priv->bandwidth == 8)
622 type0 |= F8MHZ;
623
624 /* FIXME: How to load FM and FM|INPUT1 firmwares? */
625
626 rc = load_firmware(fe, type0, &std0);
627 if (rc < 0) {
628 tuner_err("Error %d while loading generic firmware\n",
629 rc);
630 return rc;
631 }
632
633 priv->need_load_generic = 0;
634 priv->firm_type = 0;
635 if (priv->mode == T_DIGITAL_TV)
636 change_digital_bandwidth = 1;
637 }
638
639 tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth);
640
641 if (change_digital_bandwidth) {
642
643 /*FIXME: Should allow selecting between D2620 and D2633 */
644 type |= D2620;
645
646 /* FIXME: When should select a DTV78 firmware?
647 */
648 switch (bandwidth) {
649 case BANDWIDTH_8_MHZ:
650 type |= DTV8;
651 break;
652 case BANDWIDTH_7_MHZ:
653 type |= DTV7;
654 break;
655 case BANDWIDTH_6_MHZ:
656 /* FIXME: Should allow select also ATSC */
657 type |= DTV6 | QAM;
658 break;
659
660 default:
661 tuner_err("error: bandwidth not supported.\n");
662 };
663 priv->bandwidth = bandwidth;
664 }
665
666 if (!change_digital_bandwidth && priv->mode == T_DIGITAL_TV)
667 return 0;
668
669 /* Load INIT1, if needed */
670 tuner_dbg("Load init1 firmware, if exists\n");
671 type0 = BASE | INIT1;
672 if (priv->ctrl.type == XC2028_FIRM_MTS)
673 type0 |= MTS;
674
675 /* FIXME: Should handle errors - if INIT1 found */
676 rc = load_firmware(fe, type0, &std0);
677
678 /* FIXME: Should add support for FM radio
679 */
680
681 if (priv->ctrl.type == XC2028_FIRM_MTS)
682 type |= MTS;
683
684 if (priv->firm_type & std) {
685 tuner_dbg("Std-specific firmware already loaded.\n");
686 return 0;
687 }
688
689 /* Add audio hack to std mask */
690 std |= parse_audio_std_option();
691
692 rc = load_firmware(fe, type, &std);
693 if (rc < 0)
694 return rc;
695
696 /* Load SCODE firmware, if exists */
697 tuner_dbg("Trying to load scode 0\n");
698 type |= SCODE;
699
700 rc = load_scode(fe, type, &std, 0);
701
702 xc2028_get_reg(priv, 0x0004, &version);
703 xc2028_get_reg(priv, 0x0008, &hwmodel);
704
705 tuner_info("Device is Xceive %d version %d.%d, "
706 "firmware version %d.%d\n",
707 hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8,
708 (version & 0xf0) >> 4, version & 0xf);
709
710 priv->firm_type = std;
711
712 return 0;
713 }
714
715 static int xc2028_signal(struct dvb_frontend *fe, u16 *strength)
716 {
717 struct xc2028_data *priv = fe->tuner_priv;
718 u16 frq_lock, signal = 0;
719 int rc;
720
721 tuner_dbg("%s called\n", __FUNCTION__);
722
723 mutex_lock(&priv->lock);
724
725 /* Sync Lock Indicator */
726 rc = xc2028_get_reg(priv, 0x0002, &frq_lock);
727 if (rc < 0 || frq_lock == 0)
728 goto ret;
729
730 /* Frequency is locked. Return signal quality */
731
732 /* Get SNR of the video signal */
733 rc = xc2028_get_reg(priv, 0x0040, &signal);
734 if (rc < 0)
735 signal = -frq_lock;
736
737 ret:
738 mutex_unlock(&priv->lock);
739
740 *strength = signal;
741
742 return rc;
743 }
744
745 #define DIV 15625
746
747 static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ ,
748 enum tuner_mode new_mode,
749 v4l2_std_id std, fe_bandwidth_t bandwidth)
750 {
751 struct xc2028_data *priv = fe->tuner_priv;
752 int rc = -EINVAL;
753 unsigned char buf[5];
754 u32 div, offset = 0;
755
756 tuner_dbg("%s called\n", __FUNCTION__);
757
758 mutex_lock(&priv->lock);
759
760 /* HACK: It seems that specific firmware need to be reloaded
761 when freq is changed */
762
763 priv->firm_type = 0;
764
765 /* Reset GPIO 1 */
766 rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0);
767 if (rc < 0)
768 goto ret;
769
770 msleep(10);
771 tuner_dbg("should set frequency %d kHz)\n", freq / 1000);
772
773 if (check_firmware(fe, new_mode, std, bandwidth) < 0)
774 goto ret;
775
776 if (new_mode == T_DIGITAL_TV)
777 offset = 2750000;
778
779 div = (freq - offset + DIV / 2) / DIV;
780
781 /* CMD= Set frequency */
782
783 if (priv->version < 0x0202)
784 rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00});
785 else
786 rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00});
787 if (rc < 0)
788 goto ret;
789
790 rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1);
791 if (rc < 0)
792 goto ret;
793
794 msleep(10);
795
796 buf[0] = 0xff & (div >> 24);
797 buf[1] = 0xff & (div >> 16);
798 buf[2] = 0xff & (div >> 8);
799 buf[3] = 0xff & (div);
800 buf[4] = 0;
801
802 rc = i2c_send(priv, buf, sizeof(buf));
803 if (rc < 0)
804 goto ret;
805 msleep(100);
806
807 priv->frequency = freq;
808
809 tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
810 buf[1], buf[2], buf[3], buf[4],
811 freq / 1000000, (freq % 1000000) / 10000);
812
813 rc = 0;
814
815 ret:
816 mutex_unlock(&priv->lock);
817
818 return rc;
819 }
820
821 static int xc2028_set_tv_freq(struct dvb_frontend *fe,
822 struct analog_parameters *p)
823 {
824 struct xc2028_data *priv = fe->tuner_priv;
825
826 tuner_dbg("%s called\n", __FUNCTION__);
827
828 return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV,
829 p->std, BANDWIDTH_8_MHZ /* NOT USED */);
830 }
831
832 static int xc2028_set_params(struct dvb_frontend *fe,
833 struct dvb_frontend_parameters *p)
834 {
835 struct xc2028_data *priv = fe->tuner_priv;
836
837 tuner_dbg("%s called\n", __FUNCTION__);
838
839 /* FIXME: Only OFDM implemented */
840 if (fe->ops.info.type != FE_OFDM) {
841 tuner_err("DTV type not implemented.\n");
842 return -EINVAL;
843 }
844
845 return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV,
846 0 /* NOT USED */,
847 p->u.ofdm.bandwidth);
848
849 }
850
851 static int xc2028_dvb_release(struct dvb_frontend *fe)
852 {
853 struct xc2028_data *priv = fe->tuner_priv;
854
855 tuner_dbg("%s called\n", __FUNCTION__);
856
857 priv->count--;
858
859 if (!priv->count) {
860 list_del(&priv->xc2028_list);
861
862 kfree(priv->ctrl.fname);
863
864 free_firmware(priv);
865 kfree(priv);
866 }
867
868 return 0;
869 }
870
871 static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency)
872 {
873 struct xc2028_data *priv = fe->tuner_priv;
874
875 tuner_dbg("%s called\n", __FUNCTION__);
876
877 *frequency = priv->frequency;
878
879 return 0;
880 }
881
882 static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg)
883 {
884 struct xc2028_data *priv = fe->tuner_priv;
885 struct xc2028_ctrl *p = priv_cfg;
886
887 tuner_dbg("%s called\n", __FUNCTION__);
888
889 priv->ctrl.type = p->type;
890
891 if (p->fname) {
892 kfree(priv->ctrl.fname);
893
894 priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL);
895 if (!priv->ctrl.fname)
896 return -ENOMEM;
897
898 free_firmware(priv);
899 strcpy(priv->ctrl.fname, p->fname);
900 }
901
902 if (p->max_len > 0)
903 priv->max_len = p->max_len;
904
905 return 0;
906 }
907
908 static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = {
909 .info = {
910 .name = "Xceive XC3028",
911 .frequency_min = 42000000,
912 .frequency_max = 864000000,
913 .frequency_step = 50000,
914 },
915
916 .set_config = xc2028_set_config,
917 .set_analog_params = xc2028_set_tv_freq,
918 .release = xc2028_dvb_release,
919 .get_frequency = xc2028_get_frequency,
920 .get_rf_strength = xc2028_signal,
921 .set_params = xc2028_set_params,
922
923 };
924
925 void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg)
926 {
927 struct xc2028_data *priv;
928 void *video_dev;
929
930 if (debug)
931 printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n");
932
933 if (NULL == cfg->video_dev)
934 return NULL;
935
936 if (!fe) {
937 printk(KERN_ERR PREFIX "No frontend!\n");
938 return NULL;
939 }
940
941 video_dev = cfg->video_dev;
942
943 list_for_each_entry(priv, &xc2028_list, xc2028_list) {
944 if (priv->video_dev == cfg->video_dev) {
945 video_dev = NULL;
946 break;
947 }
948 }
949
950 if (video_dev) {
951 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
952 if (priv == NULL)
953 return NULL;
954
955 priv->bandwidth = BANDWIDTH_6_MHZ;
956 priv->need_load_generic = 1;
957 priv->mode = T_UNINITIALIZED;
958 priv->i2c_props.addr = cfg->i2c_addr;
959 priv->i2c_props.adap = cfg->i2c_adap;
960 priv->video_dev = video_dev;
961 priv->tuner_callback = cfg->callback;
962 priv->max_len = 13;
963
964 mutex_init(&priv->lock);
965
966 list_add_tail(&priv->xc2028_list, &xc2028_list);
967 }
968
969 fe->tuner_priv = priv;
970 priv->count++;
971
972 memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops,
973 sizeof(xc2028_dvb_tuner_ops));
974
975 tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner");
976
977 return fe;
978 }
979
980 EXPORT_SYMBOL(xc2028_attach);
981
982 MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver");
983 MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>");
984 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
985 MODULE_LICENSE("GPL");