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 | } | |
de3fe21b MCC |
464 | |
465 | /* Checks for a sleep command */ | |
466 | if (size & 0x8000) { | |
ab0b9fc6 | 467 | msleep(size & 0x7fff); |
de3fe21b | 468 | continue; |
6cb45879 MCC |
469 | } |
470 | ||
de3fe21b | 471 | if ((size + p > endp)) { |
83fb340b | 472 | tuner_err("missing bytes: need %d, have %d\n", |
ab0b9fc6 | 473 | size, (int)(endp - p)); |
de3fe21b MCC |
474 | return -EINVAL; |
475 | } | |
6cb45879 | 476 | |
de3fe21b | 477 | buf[0] = *p; |
6cb45879 | 478 | p++; |
de3fe21b | 479 | size--; |
6cb45879 | 480 | |
de3fe21b | 481 | /* Sends message chunks */ |
ab0b9fc6 MCC |
482 | while (size > 0) { |
483 | int len = (size < priv->max_len - 1) ? | |
484 | size : priv->max_len - 1; | |
6cb45879 | 485 | |
ab0b9fc6 | 486 | memcpy(buf + 1, p, len); |
6cb45879 | 487 | |
ab0b9fc6 MCC |
488 | i2c_send(rc, priv, buf, len + 1); |
489 | if (rc < 0) { | |
83fb340b | 490 | tuner_err("%d returned from send\n", rc); |
de3fe21b MCC |
491 | return -EINVAL; |
492 | } | |
493 | ||
494 | p += len; | |
495 | size -= len; | |
496 | } | |
497 | } | |
43efe702 | 498 | return 0; |
6cb45879 MCC |
499 | } |
500 | ||
f380e1d2 MCC |
501 | static int load_scode(struct dvb_frontend *fe, unsigned int type, |
502 | v4l2_std_id *id, int scode) | |
503 | { | |
504 | struct xc2028_data *priv = fe->tuner_priv; | |
505 | int pos, rc; | |
506 | unsigned char *p; | |
507 | ||
83fb340b | 508 | tuner_dbg("%s called\n", __FUNCTION__); |
f380e1d2 MCC |
509 | |
510 | pos = seek_firmware(fe, type, id); | |
511 | if (pos < 0) | |
512 | return pos; | |
513 | ||
514 | p = priv->firm[pos].ptr; | |
515 | ||
516 | if (!p) { | |
83fb340b | 517 | tuner_err("Firmware pointer were freed!"); |
f380e1d2 MCC |
518 | return -EINVAL; |
519 | } | |
520 | ||
521 | if ((priv->firm[pos].size != 12 * 16) || (scode >= 16)) | |
522 | return -EINVAL; | |
523 | ||
524 | if (priv->version < 0x0202) { | |
525 | send_seq(priv, {0x20, 0x00, 0x00, 0x00}); | |
526 | } else { | |
527 | send_seq(priv, {0xa0, 0x00, 0x00, 0x00}); | |
528 | } | |
529 | ||
530 | i2c_send(rc, priv, p + 12 * scode, 12); | |
531 | ||
532 | send_seq(priv, {0x00, 0x8c}); | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
215b95ba | 537 | static int check_firmware(struct dvb_frontend *fe, enum tuner_mode new_mode, |
ab0b9fc6 | 538 | v4l2_std_id std, fe_bandwidth_t bandwidth) |
6cb45879 | 539 | { |
215b95ba | 540 | struct xc2028_data *priv = fe->tuner_priv; |
80b52208 | 541 | int rc, version, hwmodel; |
ab0b9fc6 MCC |
542 | v4l2_std_id std0 = 0; |
543 | unsigned int type0 = 0, type = 0; | |
de3fe21b | 544 | int change_digital_bandwidth; |
6cb45879 | 545 | |
83fb340b | 546 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 547 | |
de3fe21b | 548 | if (!priv->firm) { |
a37b4c9b ML |
549 | if (!priv->ctrl.fname) { |
550 | tuner_info("xc2028/3028 firmware name not set!\n"); | |
de3fe21b | 551 | return -EINVAL; |
a37b4c9b | 552 | } |
de3fe21b | 553 | |
ab0b9fc6 MCC |
554 | rc = load_all_firmwares(fe); |
555 | if (rc < 0) | |
de3fe21b MCC |
556 | return rc; |
557 | } | |
558 | ||
83fb340b | 559 | tuner_dbg("I am in mode %u and I should switch to mode %i\n", |
ab0b9fc6 | 560 | priv->mode, new_mode); |
701672eb ML |
561 | |
562 | /* first of all, determine whether we have switched the mode */ | |
ab0b9fc6 | 563 | if (new_mode != priv->mode) { |
215b95ba MCC |
564 | priv->mode = new_mode; |
565 | priv->need_load_generic = 1; | |
701672eb ML |
566 | } |
567 | ||
215b95ba | 568 | change_digital_bandwidth = (priv->mode == T_DIGITAL_TV |
ab0b9fc6 | 569 | && bandwidth != priv->bandwidth) ? 1 : 0; |
83fb340b | 570 | tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth, |
ab0b9fc6 | 571 | bandwidth); |
701672eb | 572 | |
215b95ba | 573 | if (priv->need_load_generic) { |
6cb45879 | 574 | /* Reset is needed before loading firmware */ |
215b95ba MCC |
575 | rc = priv->tuner_callback(priv->video_dev, |
576 | XC2028_TUNER_RESET, 0); | |
ab0b9fc6 | 577 | if (rc < 0) |
6cb45879 MCC |
578 | return rc; |
579 | ||
ab0b9fc6 | 580 | type0 = BASE; |
de3fe21b MCC |
581 | |
582 | if (priv->ctrl.type == XC2028_FIRM_MTS) | |
583 | type0 |= MTS; | |
584 | ||
ab0b9fc6 | 585 | if (priv->bandwidth == 8) |
de3fe21b MCC |
586 | type0 |= F8MHZ; |
587 | ||
588 | /* FIXME: How to load FM and FM|INPUT1 firmwares? */ | |
589 | ||
590 | rc = load_firmware(fe, type0, &std0); | |
ab0b9fc6 | 591 | if (rc < 0) { |
83fb340b MCC |
592 | tuner_err("Error %d while loading generic firmware\n", |
593 | rc); | |
6cb45879 | 594 | return rc; |
de3fe21b | 595 | } |
6cb45879 | 596 | |
ab0b9fc6 MCC |
597 | priv->need_load_generic = 0; |
598 | priv->firm_type = 0; | |
599 | if (priv->mode == T_DIGITAL_TV) | |
600 | change_digital_bandwidth = 1; | |
701672eb ML |
601 | } |
602 | ||
83fb340b | 603 | tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth); |
701672eb ML |
604 | |
605 | if (change_digital_bandwidth) { | |
de3fe21b MCC |
606 | |
607 | /*FIXME: Should allow selecting between D2620 and D2633 */ | |
608 | type |= D2620; | |
609 | ||
610 | /* FIXME: When should select a DTV78 firmware? | |
611 | */ | |
ab0b9fc6 | 612 | switch (bandwidth) { |
de3fe21b MCC |
613 | case BANDWIDTH_8_MHZ: |
614 | type |= DTV8; | |
701672eb | 615 | break; |
de3fe21b MCC |
616 | case BANDWIDTH_7_MHZ: |
617 | type |= DTV7; | |
701672eb | 618 | break; |
de3fe21b MCC |
619 | case BANDWIDTH_6_MHZ: |
620 | /* FIXME: Should allow select also ATSC */ | |
43efe702 | 621 | type |= DTV6 | QAM; |
701672eb ML |
622 | break; |
623 | ||
de3fe21b | 624 | default: |
83fb340b | 625 | tuner_err("error: bandwidth not supported.\n"); |
701672eb | 626 | }; |
215b95ba | 627 | priv->bandwidth = bandwidth; |
6cb45879 MCC |
628 | } |
629 | ||
de3fe21b | 630 | /* Load INIT1, if needed */ |
83fb340b | 631 | tuner_dbg("Load init1 firmware, if exists\n"); |
f380e1d2 | 632 | type0 = BASE | INIT1; |
de3fe21b MCC |
633 | if (priv->ctrl.type == XC2028_FIRM_MTS) |
634 | type0 |= MTS; | |
635 | ||
636 | /* FIXME: Should handle errors - if INIT1 found */ | |
637 | rc = load_firmware(fe, type0, &std0); | |
638 | ||
639 | /* FIXME: Should add support for FM radio | |
640 | */ | |
641 | ||
642 | if (priv->ctrl.type == XC2028_FIRM_MTS) | |
643 | type |= MTS; | |
644 | ||
215b95ba | 645 | if (priv->firm_type & std) { |
83fb340b | 646 | tuner_dbg("Std-specific firmware already loaded.\n"); |
6cb45879 | 647 | return 0; |
2e4160ca | 648 | } |
6cb45879 | 649 | |
a82200fb MCC |
650 | /* Add audio hack to std mask */ |
651 | std |= parse_audio_std_option(); | |
652 | ||
de3fe21b | 653 | rc = load_firmware(fe, type, &std); |
ab0b9fc6 | 654 | if (rc < 0) |
6cb45879 MCC |
655 | return rc; |
656 | ||
f380e1d2 | 657 | /* Load SCODE firmware, if exists */ |
83fb340b | 658 | tuner_dbg("Trying to load scode 0\n"); |
f380e1d2 MCC |
659 | type |= SCODE; |
660 | ||
661 | rc = load_scode(fe, type, &std, 0); | |
43efe702 | 662 | |
80b52208 MCC |
663 | version = xc2028_get_reg(priv, 0x0004); |
664 | hwmodel = xc2028_get_reg(priv, 0x0008); | |
665 | ||
666 | tuner_info("Device is Xceive %d version %d.%d, " | |
667 | "firmware version %d.%d\n", | |
668 | hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8, | |
669 | (version & 0xf0) >> 4, version & 0xf); | |
6cb45879 | 670 | |
ab0b9fc6 | 671 | priv->firm_type = std; |
6cb45879 MCC |
672 | |
673 | return 0; | |
674 | } | |
675 | ||
215b95ba | 676 | static int xc2028_signal(struct dvb_frontend *fe, u16 *strength) |
6cb45879 | 677 | { |
215b95ba | 678 | struct xc2028_data *priv = fe->tuner_priv; |
ab0b9fc6 | 679 | int frq_lock, signal = 0; |
3b20532c | 680 | |
83fb340b | 681 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 682 | |
215b95ba | 683 | mutex_lock(&priv->lock); |
6cb45879 | 684 | |
215b95ba MCC |
685 | *strength = 0; |
686 | ||
80b52208 MCC |
687 | /* Sync Lock Indicator */ |
688 | frq_lock = xc2028_get_reg(priv, 0x0002); | |
ab0b9fc6 | 689 | if (frq_lock <= 0) |
3b20532c | 690 | goto ret; |
6cb45879 MCC |
691 | |
692 | /* Frequency is locked. Return signal quality */ | |
693 | ||
80b52208 MCC |
694 | /* Get SNR of the video signal */ |
695 | signal = xc2028_get_reg(priv, 0x0040); | |
6cb45879 | 696 | |
ab0b9fc6 MCC |
697 | if (signal <= 0) |
698 | signal = frq_lock; | |
3b20532c MCC |
699 | |
700 | ret: | |
215b95ba MCC |
701 | mutex_unlock(&priv->lock); |
702 | ||
703 | *strength = signal; | |
6cb45879 | 704 | |
215b95ba | 705 | return 0; |
6cb45879 MCC |
706 | } |
707 | ||
708 | #define DIV 15625 | |
709 | ||
ab0b9fc6 MCC |
710 | static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ , |
711 | enum tuner_mode new_mode, | |
712 | v4l2_std_id std, fe_bandwidth_t bandwidth) | |
6cb45879 | 713 | { |
215b95ba | 714 | struct xc2028_data *priv = fe->tuner_priv; |
ab0b9fc6 MCC |
715 | int rc = -EINVAL; |
716 | unsigned char buf[5]; | |
717 | u32 div, offset = 0; | |
6cb45879 | 718 | |
83fb340b | 719 | tuner_dbg("%s called\n", __FUNCTION__); |
215b95ba | 720 | |
de3fe21b MCC |
721 | mutex_lock(&priv->lock); |
722 | ||
d4e76681 MCC |
723 | /* HACK: It seems that specific firmware need to be reloaded |
724 | when freq is changed */ | |
701672eb | 725 | |
ab0b9fc6 | 726 | priv->firm_type = 0; |
701672eb | 727 | |
6cb45879 | 728 | /* Reset GPIO 1 */ |
215b95ba | 729 | rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0); |
ab0b9fc6 | 730 | if (rc < 0) |
215b95ba MCC |
731 | goto ret; |
732 | ||
6cb45879 | 733 | msleep(10); |
83fb340b | 734 | tuner_dbg("should set frequency %d kHz)\n", freq / 1000); |
6cb45879 | 735 | |
ab0b9fc6 | 736 | if (check_firmware(fe, new_mode, std, bandwidth) < 0) |
3b20532c | 737 | goto ret; |
2e4160ca | 738 | |
ab0b9fc6 | 739 | if (new_mode == T_DIGITAL_TV) |
d4e76681 | 740 | offset = 2750000; |
2e4160ca | 741 | |
ab0b9fc6 | 742 | div = (freq - offset + DIV / 2) / DIV; |
2e4160ca | 743 | |
6cb45879 | 744 | /* CMD= Set frequency */ |
de3fe21b | 745 | |
ab0b9fc6 | 746 | if (priv->version < 0x0202) { |
de3fe21b MCC |
747 | send_seq(priv, {0x00, 0x02, 0x00, 0x00}); |
748 | } else { | |
749 | send_seq(priv, {0x80, 0x02, 0x00, 0x00}); | |
750 | } | |
751 | ||
215b95ba | 752 | rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1); |
ab0b9fc6 | 753 | if (rc < 0) |
215b95ba | 754 | goto ret; |
6cb45879 MCC |
755 | |
756 | msleep(10); | |
701672eb | 757 | |
ab0b9fc6 MCC |
758 | buf[0] = 0xff & (div >> 24); |
759 | buf[1] = 0xff & (div >> 16); | |
760 | buf[2] = 0xff & (div >> 8); | |
761 | buf[3] = 0xff & (div); | |
762 | buf[4] = 0; | |
6cb45879 | 763 | |
215b95ba | 764 | i2c_send(rc, priv, buf, sizeof(buf)); |
ab0b9fc6 | 765 | if (rc < 0) |
3b20532c | 766 | goto ret; |
6cb45879 MCC |
767 | msleep(100); |
768 | ||
ab0b9fc6 | 769 | priv->frequency = freq; |
215b95ba | 770 | |
83fb340b | 771 | tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n", |
ab0b9fc6 MCC |
772 | buf[1], buf[2], buf[3], buf[4], |
773 | freq / 1000000, (freq % 1000000) / 10000); | |
3b20532c | 774 | |
ab0b9fc6 | 775 | rc = 0; |
6cb45879 | 776 | |
215b95ba MCC |
777 | ret: |
778 | mutex_unlock(&priv->lock); | |
6cb45879 | 779 | |
215b95ba | 780 | return rc; |
701672eb ML |
781 | } |
782 | ||
215b95ba | 783 | static int xc2028_set_tv_freq(struct dvb_frontend *fe, |
ab0b9fc6 | 784 | struct analog_parameters *p) |
6cb45879 | 785 | { |
215b95ba | 786 | struct xc2028_data *priv = fe->tuner_priv; |
6cb45879 | 787 | |
83fb340b | 788 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 789 | |
ab0b9fc6 MCC |
790 | return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV, |
791 | p->std, BANDWIDTH_8_MHZ /* NOT USED */); | |
215b95ba | 792 | } |
6cb45879 | 793 | |
215b95ba MCC |
794 | static int xc2028_set_params(struct dvb_frontend *fe, |
795 | struct dvb_frontend_parameters *p) | |
6cb45879 | 796 | { |
215b95ba | 797 | struct xc2028_data *priv = fe->tuner_priv; |
6cb45879 | 798 | |
83fb340b | 799 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 800 | |
215b95ba MCC |
801 | /* FIXME: Only OFDM implemented */ |
802 | if (fe->ops.info.type != FE_OFDM) { | |
83fb340b | 803 | tuner_err("DTV type not implemented.\n"); |
215b95ba | 804 | return -EINVAL; |
6cb45879 | 805 | } |
6cb45879 | 806 | |
215b95ba | 807 | return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV, |
ab0b9fc6 MCC |
808 | 0 /* NOT USED */, |
809 | p->u.ofdm.bandwidth); | |
6cb45879 | 810 | |
6cb45879 | 811 | } |
701672eb | 812 | |
215b95ba | 813 | static int xc2028_dvb_release(struct dvb_frontend *fe) |
701672eb | 814 | { |
215b95ba MCC |
815 | struct xc2028_data *priv = fe->tuner_priv; |
816 | ||
83fb340b | 817 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 818 | |
215b95ba | 819 | priv->count--; |
701672eb | 820 | |
de3fe21b | 821 | if (!priv->count) { |
1808a698 MCC |
822 | list_del(&priv->xc2028_list); |
823 | ||
ab0b9fc6 | 824 | kfree(priv->ctrl.fname); |
de3fe21b MCC |
825 | |
826 | free_firmware(priv); | |
ab0b9fc6 | 827 | kfree(priv); |
de3fe21b | 828 | } |
701672eb ML |
829 | |
830 | return 0; | |
831 | } | |
832 | ||
215b95ba | 833 | static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
701672eb | 834 | { |
215b95ba | 835 | struct xc2028_data *priv = fe->tuner_priv; |
701672eb | 836 | |
83fb340b | 837 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 838 | |
215b95ba | 839 | *frequency = priv->frequency; |
701672eb ML |
840 | |
841 | return 0; | |
842 | } | |
843 | ||
ab0b9fc6 | 844 | static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg) |
de3fe21b MCC |
845 | { |
846 | struct xc2028_data *priv = fe->tuner_priv; | |
847 | struct xc2028_ctrl *p = priv_cfg; | |
848 | ||
83fb340b | 849 | tuner_dbg("%s called\n", __FUNCTION__); |
de3fe21b MCC |
850 | |
851 | priv->ctrl.type = p->type; | |
852 | ||
853 | if (p->fname) { | |
ab0b9fc6 | 854 | kfree(priv->ctrl.fname); |
de3fe21b | 855 | |
ab0b9fc6 | 856 | priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL); |
de3fe21b MCC |
857 | if (!priv->ctrl.fname) |
858 | return -ENOMEM; | |
859 | ||
860 | free_firmware(priv); | |
861 | strcpy(priv->ctrl.fname, p->fname); | |
862 | } | |
863 | ||
ab0b9fc6 | 864 | if (p->max_len > 0) |
352fae1d MCC |
865 | priv->max_len = p->max_len; |
866 | ||
de3fe21b MCC |
867 | return 0; |
868 | } | |
869 | ||
215b95ba | 870 | static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = { |
701672eb | 871 | .info = { |
ab0b9fc6 MCC |
872 | .name = "Xceive XC3028", |
873 | .frequency_min = 42000000, | |
874 | .frequency_max = 864000000, | |
875 | .frequency_step = 50000, | |
876 | }, | |
701672eb | 877 | |
de3fe21b | 878 | .set_config = xc2028_set_config, |
215b95ba MCC |
879 | .set_analog_params = xc2028_set_tv_freq, |
880 | .release = xc2028_dvb_release, | |
881 | .get_frequency = xc2028_get_frequency, | |
882 | .get_rf_strength = xc2028_signal, | |
883 | .set_params = xc2028_set_params, | |
701672eb | 884 | |
701672eb ML |
885 | }; |
886 | ||
a37b4c9b | 887 | void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg) |
701672eb | 888 | { |
215b95ba | 889 | struct xc2028_data *priv; |
a37b4c9b | 890 | void *video_dev; |
701672eb | 891 | |
83fb340b MCC |
892 | if (debug) |
893 | printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n"); | |
701672eb | 894 | |
a37b4c9b ML |
895 | if (NULL == cfg->video_dev) |
896 | return NULL; | |
215b95ba | 897 | |
a37b4c9b ML |
898 | if (!fe) { |
899 | printk(KERN_ERR PREFIX "No frontend!\n"); | |
900 | return NULL; | |
215b95ba MCC |
901 | } |
902 | ||
a37b4c9b ML |
903 | video_dev = cfg->video_dev; |
904 | ||
215b95ba | 905 | list_for_each_entry(priv, &xc2028_list, xc2028_list) { |
a37b4c9b ML |
906 | if (priv->video_dev == cfg->video_dev) { |
907 | video_dev = NULL; | |
908 | break; | |
909 | } | |
215b95ba MCC |
910 | } |
911 | ||
a37b4c9b | 912 | if (video_dev) { |
215b95ba MCC |
913 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
914 | if (priv == NULL) | |
a37b4c9b | 915 | return NULL; |
3b20532c | 916 | |
ab0b9fc6 MCC |
917 | priv->bandwidth = BANDWIDTH_6_MHZ; |
918 | priv->need_load_generic = 1; | |
215b95ba | 919 | priv->mode = T_UNINITIALIZED; |
a37b4c9b ML |
920 | priv->i2c_props.addr = cfg->i2c_addr; |
921 | priv->i2c_props.adap = cfg->i2c_adap; | |
215b95ba | 922 | priv->video_dev = video_dev; |
a37b4c9b | 923 | priv->tuner_callback = cfg->callback; |
de3fe21b MCC |
924 | priv->max_len = 13; |
925 | ||
215b95ba MCC |
926 | mutex_init(&priv->lock); |
927 | ||
ab0b9fc6 | 928 | list_add_tail(&priv->xc2028_list, &xc2028_list); |
215b95ba | 929 | } |
a37b4c9b ML |
930 | |
931 | fe->tuner_priv = priv; | |
1808a698 | 932 | priv->count++; |
215b95ba MCC |
933 | |
934 | memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops, | |
ab0b9fc6 | 935 | sizeof(xc2028_dvb_tuner_ops)); |
215b95ba MCC |
936 | |
937 | tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner"); | |
938 | ||
a37b4c9b | 939 | return fe; |
215b95ba | 940 | } |
a37b4c9b | 941 | |
701672eb ML |
942 | EXPORT_SYMBOL(xc2028_attach); |
943 | ||
215b95ba | 944 | MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver"); |
983d214e | 945 | MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>"); |
215b95ba MCC |
946 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
947 | MODULE_LICENSE("GPL"); |