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); | |
59 | struct device *dev; | |
60 | void *video_dev; | |
61 | int count; | |
de3fe21b MCC |
62 | __u32 frequency; |
63 | ||
64 | struct firmware_description *firm; | |
65 | int firm_size; | |
66 | ||
67 | __u16 version; | |
68 | ||
69 | struct xc2028_ctrl ctrl; | |
215b95ba | 70 | |
701672eb ML |
71 | v4l2_std_id firm_type; /* video stds supported |
72 | by current firmware */ | |
73 | fe_bandwidth_t bandwidth; /* Firmware bandwidth: | |
74 | 6M, 7M or 8M */ | |
75 | int need_load_generic; /* The generic firmware | |
76 | were loaded? */ | |
de3fe21b MCC |
77 | |
78 | int max_len; /* Max firmware chunk */ | |
79 | ||
701672eb ML |
80 | enum tuner_mode mode; |
81 | struct i2c_client *i2c_client; | |
215b95ba MCC |
82 | |
83 | struct mutex lock; | |
6cb45879 MCC |
84 | }; |
85 | ||
ab0b9fc6 MCC |
86 | #define i2c_send(rc, priv, buf, size) do { \ |
87 | rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \ | |
88 | if (size != rc) \ | |
83fb340b | 89 | tuner_err("i2c output error: rc = %d (should be %d)\n", \ |
ab0b9fc6 MCC |
90 | rc, (int)size); \ |
91 | } while (0) | |
92 | ||
93 | #define i2c_rcv(rc, priv, buf, size) do { \ | |
94 | rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \ | |
95 | if (size != rc) \ | |
83fb340b | 96 | tuner_err("i2c input error: rc = %d (should be %d)\n", \ |
ab0b9fc6 MCC |
97 | rc, (int)size); \ |
98 | } while (0) | |
99 | ||
100 | #define send_seq(priv, data...) do { \ | |
101 | int rc; \ | |
215b95ba | 102 | static u8 _val[] = data; \ |
6cb45879 | 103 | if (sizeof(_val) != \ |
ab0b9fc6 | 104 | (rc = tuner_i2c_xfer_send(&priv->i2c_props, \ |
215b95ba | 105 | _val, sizeof(_val)))) { \ |
83fb340b | 106 | tuner_err("Error on line %d: %d\n", __LINE__, rc); \ |
ab0b9fc6 | 107 | return -EINVAL; \ |
6cb45879 | 108 | } \ |
ab0b9fc6 MCC |
109 | msleep(10); \ |
110 | } while (0) | |
6cb45879 | 111 | |
80b52208 | 112 | static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg) |
6cb45879 MCC |
113 | { |
114 | int rc; | |
b873e1a3 | 115 | unsigned char buf[2]; |
215b95ba | 116 | |
83fb340b | 117 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 118 | |
80b52208 MCC |
119 | buf[0] = reg>>8; |
120 | buf[1] = (unsigned char) reg; | |
6cb45879 | 121 | |
80b52208 | 122 | i2c_send(rc, priv, buf, 2); |
ab0b9fc6 | 123 | if (rc < 0) |
6cb45879 MCC |
124 | return rc; |
125 | ||
215b95ba | 126 | i2c_rcv(rc, priv, buf, 2); |
ab0b9fc6 | 127 | if (rc < 0) |
6cb45879 MCC |
128 | return rc; |
129 | ||
ab0b9fc6 | 130 | return (buf[1]) | (buf[0] << 8); |
6cb45879 MCC |
131 | } |
132 | ||
43efe702 MCC |
133 | void dump_firm_type(unsigned int type) |
134 | { | |
135 | if (type & BASE) | |
136 | printk("BASE "); | |
f380e1d2 MCC |
137 | if (type & INIT1) |
138 | printk("INIT1 "); | |
43efe702 MCC |
139 | if (type & F8MHZ) |
140 | printk("F8MHZ "); | |
141 | if (type & MTS) | |
142 | printk("MTS "); | |
143 | if (type & D2620) | |
144 | printk("D2620 "); | |
145 | if (type & D2633) | |
146 | printk("D2633 "); | |
147 | if (type & DTV6) | |
148 | printk("DTV6 "); | |
149 | if (type & QAM) | |
150 | printk("QAM "); | |
151 | if (type & DTV7) | |
152 | printk("DTV7 "); | |
153 | if (type & DTV78) | |
154 | printk("DTV78 "); | |
155 | if (type & DTV8) | |
156 | printk("DTV8 "); | |
157 | if (type & FM) | |
158 | printk("FM "); | |
159 | if (type & INPUT1) | |
160 | printk("INPUT1 "); | |
161 | if (type & LCD) | |
162 | printk("LCD "); | |
163 | if (type & NOGD) | |
164 | printk("NOGD "); | |
165 | if (type & MONO) | |
166 | printk("MONO "); | |
167 | if (type & ATSC) | |
168 | printk("ATSC "); | |
169 | if (type & IF) | |
170 | printk("IF "); | |
171 | if (type & LG60) | |
172 | printk("LG60 "); | |
173 | if (type & ATI638) | |
174 | printk("ATI638 "); | |
175 | if (type & OREN538) | |
176 | printk("OREN538 "); | |
177 | if (type & OREN36) | |
178 | printk("OREN36 "); | |
179 | if (type & TOYOTA388) | |
180 | printk("TOYOTA388 "); | |
181 | if (type & TOYOTA794) | |
182 | printk("TOYOTA794 "); | |
183 | if (type & DIBCOM52) | |
184 | printk("DIBCOM52 "); | |
185 | if (type & ZARLINK456) | |
186 | printk("ZARLINK456 "); | |
187 | if (type & CHINA) | |
188 | printk("CHINA "); | |
189 | if (type & F6MHZ) | |
190 | printk("F6MHZ "); | |
191 | if (type & INPUT2) | |
192 | printk("INPUT2 "); | |
193 | if (type & SCODE) | |
194 | printk("SCODE "); | |
195 | } | |
196 | ||
a82200fb MCC |
197 | static v4l2_std_id parse_audio_std_option(void) |
198 | { | |
199 | if (strcasecmp(audio_std, "A2")) | |
200 | return V4L2_STD_A2; | |
201 | if (strcasecmp(audio_std, "A2/A")) | |
202 | return V4L2_STD_A2_A; | |
203 | if (strcasecmp(audio_std, "A2/B")) | |
204 | return V4L2_STD_A2_B; | |
205 | if (strcasecmp(audio_std, "NICAM")) | |
206 | return V4L2_STD_NICAM; | |
207 | if (strcasecmp(audio_std, "NICAM/A")) | |
208 | return V4L2_STD_NICAM_A; | |
209 | if (strcasecmp(audio_std, "NICAM/B")) | |
210 | return V4L2_STD_NICAM_B; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
ab0b9fc6 | 215 | static void free_firmware(struct xc2028_data *priv) |
6cb45879 | 216 | { |
de3fe21b MCC |
217 | int i; |
218 | ||
219 | if (!priv->firm) | |
220 | return; | |
221 | ||
ab0b9fc6 MCC |
222 | for (i = 0; i < priv->firm_size; i++) |
223 | kfree(priv->firm[i].ptr); | |
224 | ||
de3fe21b MCC |
225 | kfree(priv->firm); |
226 | ||
ab0b9fc6 | 227 | priv->firm = NULL; |
de3fe21b MCC |
228 | priv->need_load_generic = 1; |
229 | } | |
230 | ||
ab0b9fc6 | 231 | static int load_all_firmwares(struct dvb_frontend *fe) |
de3fe21b MCC |
232 | { |
233 | struct xc2028_data *priv = fe->tuner_priv; | |
ab0b9fc6 | 234 | const struct firmware *fw = NULL; |
6cb45879 | 235 | unsigned char *p, *endp; |
ab0b9fc6 MCC |
236 | int rc = 0; |
237 | int n, n_array; | |
de3fe21b | 238 | char name[33]; |
6cb45879 | 239 | |
83fb340b | 240 | tuner_dbg("%s called\n", __FUNCTION__); |
215b95ba | 241 | |
f380e1d2 | 242 | tuner_info("Reading firmware %s\n", priv->ctrl.fname); |
de3fe21b | 243 | rc = request_firmware(&fw, priv->ctrl.fname, priv->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 | ||
ab0b9fc6 | 314 | size = le32_to_cpu(*(v4l2_std_id *) 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 MCC |
548 | if (!priv->firm) { |
549 | if (!priv->ctrl.fname) | |
550 | return -EINVAL; | |
551 | ||
ab0b9fc6 MCC |
552 | rc = load_all_firmwares(fe); |
553 | if (rc < 0) | |
de3fe21b MCC |
554 | return rc; |
555 | } | |
556 | ||
83fb340b | 557 | tuner_dbg("I am in mode %u and I should switch to mode %i\n", |
ab0b9fc6 | 558 | priv->mode, new_mode); |
701672eb ML |
559 | |
560 | /* first of all, determine whether we have switched the mode */ | |
ab0b9fc6 | 561 | if (new_mode != priv->mode) { |
215b95ba MCC |
562 | priv->mode = new_mode; |
563 | priv->need_load_generic = 1; | |
701672eb ML |
564 | } |
565 | ||
215b95ba | 566 | change_digital_bandwidth = (priv->mode == T_DIGITAL_TV |
ab0b9fc6 | 567 | && bandwidth != priv->bandwidth) ? 1 : 0; |
83fb340b | 568 | tuner_dbg("old bandwidth %u, new bandwidth %u\n", priv->bandwidth, |
ab0b9fc6 | 569 | bandwidth); |
701672eb | 570 | |
215b95ba | 571 | if (priv->need_load_generic) { |
6cb45879 | 572 | /* Reset is needed before loading firmware */ |
215b95ba MCC |
573 | rc = priv->tuner_callback(priv->video_dev, |
574 | XC2028_TUNER_RESET, 0); | |
ab0b9fc6 | 575 | if (rc < 0) |
6cb45879 MCC |
576 | return rc; |
577 | ||
ab0b9fc6 | 578 | type0 = BASE; |
de3fe21b MCC |
579 | |
580 | if (priv->ctrl.type == XC2028_FIRM_MTS) | |
581 | type0 |= MTS; | |
582 | ||
ab0b9fc6 | 583 | if (priv->bandwidth == 8) |
de3fe21b MCC |
584 | type0 |= F8MHZ; |
585 | ||
586 | /* FIXME: How to load FM and FM|INPUT1 firmwares? */ | |
587 | ||
588 | rc = load_firmware(fe, type0, &std0); | |
ab0b9fc6 | 589 | if (rc < 0) { |
83fb340b MCC |
590 | tuner_err("Error %d while loading generic firmware\n", |
591 | rc); | |
6cb45879 | 592 | return rc; |
de3fe21b | 593 | } |
6cb45879 | 594 | |
ab0b9fc6 MCC |
595 | priv->need_load_generic = 0; |
596 | priv->firm_type = 0; | |
597 | if (priv->mode == T_DIGITAL_TV) | |
598 | change_digital_bandwidth = 1; | |
701672eb ML |
599 | } |
600 | ||
83fb340b | 601 | tuner_dbg("I should change bandwidth %u\n", change_digital_bandwidth); |
701672eb ML |
602 | |
603 | if (change_digital_bandwidth) { | |
de3fe21b MCC |
604 | |
605 | /*FIXME: Should allow selecting between D2620 and D2633 */ | |
606 | type |= D2620; | |
607 | ||
608 | /* FIXME: When should select a DTV78 firmware? | |
609 | */ | |
ab0b9fc6 | 610 | switch (bandwidth) { |
de3fe21b MCC |
611 | case BANDWIDTH_8_MHZ: |
612 | type |= DTV8; | |
701672eb | 613 | break; |
de3fe21b MCC |
614 | case BANDWIDTH_7_MHZ: |
615 | type |= DTV7; | |
701672eb | 616 | break; |
de3fe21b MCC |
617 | case BANDWIDTH_6_MHZ: |
618 | /* FIXME: Should allow select also ATSC */ | |
43efe702 | 619 | type |= DTV6 | QAM; |
701672eb ML |
620 | break; |
621 | ||
de3fe21b | 622 | default: |
83fb340b | 623 | tuner_err("error: bandwidth not supported.\n"); |
701672eb | 624 | }; |
215b95ba | 625 | priv->bandwidth = bandwidth; |
6cb45879 MCC |
626 | } |
627 | ||
de3fe21b | 628 | /* Load INIT1, if needed */ |
83fb340b | 629 | tuner_dbg("Load init1 firmware, if exists\n"); |
f380e1d2 | 630 | type0 = BASE | INIT1; |
de3fe21b MCC |
631 | if (priv->ctrl.type == XC2028_FIRM_MTS) |
632 | type0 |= MTS; | |
633 | ||
634 | /* FIXME: Should handle errors - if INIT1 found */ | |
635 | rc = load_firmware(fe, type0, &std0); | |
636 | ||
637 | /* FIXME: Should add support for FM radio | |
638 | */ | |
639 | ||
640 | if (priv->ctrl.type == XC2028_FIRM_MTS) | |
641 | type |= MTS; | |
642 | ||
215b95ba | 643 | if (priv->firm_type & std) { |
83fb340b | 644 | tuner_dbg("Std-specific firmware already loaded.\n"); |
6cb45879 | 645 | return 0; |
2e4160ca | 646 | } |
6cb45879 | 647 | |
a82200fb MCC |
648 | /* Add audio hack to std mask */ |
649 | std |= parse_audio_std_option(); | |
650 | ||
de3fe21b | 651 | rc = load_firmware(fe, type, &std); |
ab0b9fc6 | 652 | if (rc < 0) |
6cb45879 MCC |
653 | return rc; |
654 | ||
f380e1d2 | 655 | /* Load SCODE firmware, if exists */ |
83fb340b | 656 | tuner_dbg("Trying to load scode 0\n"); |
f380e1d2 MCC |
657 | type |= SCODE; |
658 | ||
659 | rc = load_scode(fe, type, &std, 0); | |
43efe702 | 660 | |
80b52208 MCC |
661 | version = xc2028_get_reg(priv, 0x0004); |
662 | hwmodel = xc2028_get_reg(priv, 0x0008); | |
663 | ||
664 | tuner_info("Device is Xceive %d version %d.%d, " | |
665 | "firmware version %d.%d\n", | |
666 | hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8, | |
667 | (version & 0xf0) >> 4, version & 0xf); | |
6cb45879 | 668 | |
ab0b9fc6 | 669 | priv->firm_type = std; |
6cb45879 MCC |
670 | |
671 | return 0; | |
672 | } | |
673 | ||
215b95ba | 674 | static int xc2028_signal(struct dvb_frontend *fe, u16 *strength) |
6cb45879 | 675 | { |
215b95ba | 676 | struct xc2028_data *priv = fe->tuner_priv; |
ab0b9fc6 | 677 | int frq_lock, signal = 0; |
3b20532c | 678 | |
83fb340b | 679 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 680 | |
215b95ba | 681 | mutex_lock(&priv->lock); |
6cb45879 | 682 | |
215b95ba MCC |
683 | *strength = 0; |
684 | ||
80b52208 MCC |
685 | /* Sync Lock Indicator */ |
686 | frq_lock = xc2028_get_reg(priv, 0x0002); | |
ab0b9fc6 | 687 | if (frq_lock <= 0) |
3b20532c | 688 | goto ret; |
6cb45879 MCC |
689 | |
690 | /* Frequency is locked. Return signal quality */ | |
691 | ||
80b52208 MCC |
692 | /* Get SNR of the video signal */ |
693 | signal = xc2028_get_reg(priv, 0x0040); | |
6cb45879 | 694 | |
ab0b9fc6 MCC |
695 | if (signal <= 0) |
696 | signal = frq_lock; | |
3b20532c MCC |
697 | |
698 | ret: | |
215b95ba MCC |
699 | mutex_unlock(&priv->lock); |
700 | ||
701 | *strength = signal; | |
6cb45879 | 702 | |
215b95ba | 703 | return 0; |
6cb45879 MCC |
704 | } |
705 | ||
706 | #define DIV 15625 | |
707 | ||
ab0b9fc6 MCC |
708 | static int generic_set_tv_freq(struct dvb_frontend *fe, u32 freq /* in Hz */ , |
709 | enum tuner_mode new_mode, | |
710 | v4l2_std_id std, fe_bandwidth_t bandwidth) | |
6cb45879 | 711 | { |
215b95ba | 712 | struct xc2028_data *priv = fe->tuner_priv; |
ab0b9fc6 MCC |
713 | int rc = -EINVAL; |
714 | unsigned char buf[5]; | |
715 | u32 div, offset = 0; | |
6cb45879 | 716 | |
83fb340b | 717 | tuner_dbg("%s called\n", __FUNCTION__); |
215b95ba | 718 | |
de3fe21b MCC |
719 | mutex_lock(&priv->lock); |
720 | ||
d4e76681 MCC |
721 | /* HACK: It seems that specific firmware need to be reloaded |
722 | when freq is changed */ | |
701672eb | 723 | |
ab0b9fc6 | 724 | priv->firm_type = 0; |
701672eb | 725 | |
6cb45879 | 726 | /* Reset GPIO 1 */ |
215b95ba | 727 | rc = priv->tuner_callback(priv->video_dev, XC2028_TUNER_RESET, 0); |
ab0b9fc6 | 728 | if (rc < 0) |
215b95ba MCC |
729 | goto ret; |
730 | ||
6cb45879 | 731 | msleep(10); |
83fb340b | 732 | tuner_dbg("should set frequency %d kHz)\n", freq / 1000); |
6cb45879 | 733 | |
ab0b9fc6 | 734 | if (check_firmware(fe, new_mode, std, bandwidth) < 0) |
3b20532c | 735 | goto ret; |
2e4160ca | 736 | |
ab0b9fc6 | 737 | if (new_mode == T_DIGITAL_TV) |
d4e76681 | 738 | offset = 2750000; |
2e4160ca | 739 | |
ab0b9fc6 | 740 | div = (freq - offset + DIV / 2) / DIV; |
2e4160ca | 741 | |
6cb45879 | 742 | /* CMD= Set frequency */ |
de3fe21b | 743 | |
ab0b9fc6 | 744 | if (priv->version < 0x0202) { |
de3fe21b MCC |
745 | send_seq(priv, {0x00, 0x02, 0x00, 0x00}); |
746 | } else { | |
747 | send_seq(priv, {0x80, 0x02, 0x00, 0x00}); | |
748 | } | |
749 | ||
215b95ba | 750 | rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1); |
ab0b9fc6 | 751 | if (rc < 0) |
215b95ba | 752 | goto ret; |
6cb45879 MCC |
753 | |
754 | msleep(10); | |
701672eb | 755 | |
ab0b9fc6 MCC |
756 | buf[0] = 0xff & (div >> 24); |
757 | buf[1] = 0xff & (div >> 16); | |
758 | buf[2] = 0xff & (div >> 8); | |
759 | buf[3] = 0xff & (div); | |
760 | buf[4] = 0; | |
6cb45879 | 761 | |
215b95ba | 762 | i2c_send(rc, priv, buf, sizeof(buf)); |
ab0b9fc6 | 763 | if (rc < 0) |
3b20532c | 764 | goto ret; |
6cb45879 MCC |
765 | msleep(100); |
766 | ||
ab0b9fc6 | 767 | priv->frequency = freq; |
215b95ba | 768 | |
83fb340b | 769 | tuner_dbg("divider= %02x %02x %02x %02x (freq=%d.%02d)\n", |
ab0b9fc6 MCC |
770 | buf[1], buf[2], buf[3], buf[4], |
771 | freq / 1000000, (freq % 1000000) / 10000); | |
3b20532c | 772 | |
ab0b9fc6 | 773 | rc = 0; |
6cb45879 | 774 | |
215b95ba MCC |
775 | ret: |
776 | mutex_unlock(&priv->lock); | |
6cb45879 | 777 | |
215b95ba | 778 | return rc; |
701672eb ML |
779 | } |
780 | ||
215b95ba | 781 | static int xc2028_set_tv_freq(struct dvb_frontend *fe, |
ab0b9fc6 | 782 | struct analog_parameters *p) |
6cb45879 | 783 | { |
215b95ba | 784 | struct xc2028_data *priv = fe->tuner_priv; |
6cb45879 | 785 | |
83fb340b | 786 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 787 | |
ab0b9fc6 MCC |
788 | return generic_set_tv_freq(fe, 62500l * p->frequency, T_ANALOG_TV, |
789 | p->std, BANDWIDTH_8_MHZ /* NOT USED */); | |
215b95ba | 790 | } |
6cb45879 | 791 | |
215b95ba MCC |
792 | static int xc2028_set_params(struct dvb_frontend *fe, |
793 | struct dvb_frontend_parameters *p) | |
6cb45879 | 794 | { |
215b95ba | 795 | struct xc2028_data *priv = fe->tuner_priv; |
6cb45879 | 796 | |
83fb340b | 797 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 798 | |
215b95ba MCC |
799 | /* FIXME: Only OFDM implemented */ |
800 | if (fe->ops.info.type != FE_OFDM) { | |
83fb340b | 801 | tuner_err("DTV type not implemented.\n"); |
215b95ba | 802 | return -EINVAL; |
6cb45879 | 803 | } |
6cb45879 | 804 | |
215b95ba | 805 | return generic_set_tv_freq(fe, p->frequency, T_DIGITAL_TV, |
ab0b9fc6 MCC |
806 | 0 /* NOT USED */, |
807 | p->u.ofdm.bandwidth); | |
6cb45879 | 808 | |
6cb45879 | 809 | } |
701672eb | 810 | |
215b95ba | 811 | static int xc2028_dvb_release(struct dvb_frontend *fe) |
701672eb | 812 | { |
215b95ba MCC |
813 | struct xc2028_data *priv = fe->tuner_priv; |
814 | ||
83fb340b | 815 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 816 | |
215b95ba | 817 | priv->count--; |
701672eb | 818 | |
de3fe21b | 819 | if (!priv->count) { |
1808a698 MCC |
820 | list_del(&priv->xc2028_list); |
821 | ||
ab0b9fc6 | 822 | kfree(priv->ctrl.fname); |
de3fe21b MCC |
823 | |
824 | free_firmware(priv); | |
ab0b9fc6 | 825 | kfree(priv); |
de3fe21b | 826 | } |
701672eb ML |
827 | |
828 | return 0; | |
829 | } | |
830 | ||
215b95ba | 831 | static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
701672eb | 832 | { |
215b95ba | 833 | struct xc2028_data *priv = fe->tuner_priv; |
701672eb | 834 | |
83fb340b | 835 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 836 | |
215b95ba | 837 | *frequency = priv->frequency; |
701672eb ML |
838 | |
839 | return 0; | |
840 | } | |
841 | ||
ab0b9fc6 | 842 | static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg) |
de3fe21b MCC |
843 | { |
844 | struct xc2028_data *priv = fe->tuner_priv; | |
845 | struct xc2028_ctrl *p = priv_cfg; | |
846 | ||
83fb340b | 847 | tuner_dbg("%s called\n", __FUNCTION__); |
de3fe21b MCC |
848 | |
849 | priv->ctrl.type = p->type; | |
850 | ||
851 | if (p->fname) { | |
ab0b9fc6 | 852 | kfree(priv->ctrl.fname); |
de3fe21b | 853 | |
ab0b9fc6 | 854 | priv->ctrl.fname = kmalloc(strlen(p->fname) + 1, GFP_KERNEL); |
de3fe21b MCC |
855 | if (!priv->ctrl.fname) |
856 | return -ENOMEM; | |
857 | ||
858 | free_firmware(priv); | |
859 | strcpy(priv->ctrl.fname, p->fname); | |
860 | } | |
861 | ||
ab0b9fc6 | 862 | if (p->max_len > 0) |
352fae1d MCC |
863 | priv->max_len = p->max_len; |
864 | ||
de3fe21b MCC |
865 | return 0; |
866 | } | |
867 | ||
215b95ba | 868 | static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = { |
701672eb | 869 | .info = { |
ab0b9fc6 MCC |
870 | .name = "Xceive XC3028", |
871 | .frequency_min = 42000000, | |
872 | .frequency_max = 864000000, | |
873 | .frequency_step = 50000, | |
874 | }, | |
701672eb | 875 | |
de3fe21b | 876 | .set_config = xc2028_set_config, |
215b95ba MCC |
877 | .set_analog_params = xc2028_set_tv_freq, |
878 | .release = xc2028_dvb_release, | |
879 | .get_frequency = xc2028_get_frequency, | |
880 | .get_rf_strength = xc2028_signal, | |
881 | .set_params = xc2028_set_params, | |
701672eb | 882 | |
701672eb ML |
883 | }; |
884 | ||
ab0b9fc6 | 885 | int xc2028_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c_adap, |
215b95ba | 886 | u8 i2c_addr, struct device *dev, void *video_dev, |
ab0b9fc6 | 887 | int (*tuner_callback) (void *dev, int command, int arg)) |
701672eb | 888 | { |
215b95ba | 889 | struct xc2028_data *priv; |
701672eb | 890 | |
83fb340b MCC |
891 | if (debug) |
892 | printk(KERN_DEBUG PREFIX "Xcv2028/3028 init called!\n"); | |
701672eb | 893 | |
215b95ba MCC |
894 | if (NULL == dev) |
895 | return -ENODEV; | |
896 | ||
897 | if (NULL == video_dev) | |
898 | return -ENODEV; | |
899 | ||
900 | if (!tuner_callback) { | |
ab0b9fc6 | 901 | printk(KERN_ERR PREFIX "No tuner callback!\n"); |
215b95ba MCC |
902 | return -EINVAL; |
903 | } | |
904 | ||
905 | list_for_each_entry(priv, &xc2028_list, xc2028_list) { | |
ab0b9fc6 | 906 | if (priv->dev == dev) |
215b95ba | 907 | dev = NULL; |
215b95ba MCC |
908 | } |
909 | ||
910 | if (dev) { | |
911 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
912 | if (priv == NULL) | |
913 | return -ENOMEM; | |
701672eb | 914 | |
215b95ba | 915 | fe->tuner_priv = priv; |
3b20532c | 916 | |
ab0b9fc6 MCC |
917 | priv->bandwidth = BANDWIDTH_6_MHZ; |
918 | priv->need_load_generic = 1; | |
215b95ba MCC |
919 | priv->mode = T_UNINITIALIZED; |
920 | priv->i2c_props.addr = i2c_addr; | |
921 | priv->i2c_props.adap = i2c_adap; | |
922 | priv->dev = dev; | |
923 | priv->video_dev = video_dev; | |
924 | priv->tuner_callback = tuner_callback; | |
de3fe21b MCC |
925 | priv->max_len = 13; |
926 | ||
215b95ba MCC |
927 | |
928 | mutex_init(&priv->lock); | |
929 | ||
ab0b9fc6 | 930 | list_add_tail(&priv->xc2028_list, &xc2028_list); |
215b95ba | 931 | } |
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 | ||
939 | return 0; | |
940 | } | |
701672eb ML |
941 | EXPORT_SYMBOL(xc2028_attach); |
942 | ||
215b95ba | 943 | MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver"); |
983d214e | 944 | MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>"); |
215b95ba MCC |
945 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
946 | MODULE_LICENSE("GPL"); |