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