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