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