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