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