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; | |
66c2d53d | 53 | __u16 int_freq; |
de3fe21b MCC |
54 | unsigned char *ptr; |
55 | unsigned int size; | |
56 | }; | |
6cb45879 | 57 | |
e0f0b37a CP |
58 | struct firmware_properties { |
59 | unsigned int type; | |
60 | v4l2_std_id id; | |
61 | v4l2_std_id std_req; | |
66c2d53d | 62 | __u16 int_freq; |
e0f0b37a CP |
63 | unsigned int scode_table; |
64 | int scode_nr; | |
65 | }; | |
66 | ||
6cb45879 | 67 | struct xc2028_data { |
215b95ba MCC |
68 | struct list_head xc2028_list; |
69 | struct tuner_i2c_props i2c_props; | |
70 | int (*tuner_callback) (void *dev, | |
71 | int command, int arg); | |
215b95ba MCC |
72 | void *video_dev; |
73 | int count; | |
de3fe21b MCC |
74 | __u32 frequency; |
75 | ||
76 | struct firmware_description *firm; | |
77 | int firm_size; | |
06fd82dc | 78 | __u16 firm_version; |
de3fe21b | 79 | |
8bf799a6 CP |
80 | __u16 hwmodel; |
81 | __u16 hwvers; | |
82 | ||
de3fe21b | 83 | struct xc2028_ctrl ctrl; |
215b95ba | 84 | |
e0f0b37a | 85 | struct firmware_properties cur_fw; |
215b95ba MCC |
86 | |
87 | struct mutex lock; | |
6cb45879 MCC |
88 | }; |
89 | ||
47cc5b78 CP |
90 | #define i2c_send(priv, buf, size) ({ \ |
91 | int _rc; \ | |
92 | _rc = tuner_i2c_xfer_send(&priv->i2c_props, buf, size); \ | |
93 | if (size != _rc) \ | |
94 | tuner_info("i2c output error: rc = %d (should be %d)\n",\ | |
95 | _rc, (int)size); \ | |
96 | _rc; \ | |
97 | }) | |
98 | ||
99 | #define i2c_rcv(priv, buf, size) ({ \ | |
100 | int _rc; \ | |
101 | _rc = tuner_i2c_xfer_recv(&priv->i2c_props, buf, size); \ | |
102 | if (size != _rc) \ | |
83fb340b | 103 | tuner_err("i2c input error: rc = %d (should be %d)\n", \ |
47cc5b78 CP |
104 | _rc, (int)size); \ |
105 | _rc; \ | |
106 | }) | |
ab0b9fc6 | 107 | |
7d58d111 CP |
108 | #define i2c_send_recv(priv, obuf, osize, ibuf, isize) ({ \ |
109 | int _rc; \ | |
110 | _rc = tuner_i2c_xfer_send_recv(&priv->i2c_props, obuf, osize, \ | |
111 | ibuf, isize); \ | |
112 | if (isize != _rc) \ | |
113 | tuner_err("i2c input error: rc = %d (should be %d)\n", \ | |
114 | _rc, (int)isize); \ | |
115 | _rc; \ | |
116 | }) | |
117 | ||
47cc5b78 | 118 | #define send_seq(priv, data...) ({ \ |
215b95ba | 119 | static u8 _val[] = data; \ |
47cc5b78 | 120 | int _rc; \ |
6cb45879 | 121 | if (sizeof(_val) != \ |
47cc5b78 | 122 | (_rc = tuner_i2c_xfer_send(&priv->i2c_props, \ |
215b95ba | 123 | _val, sizeof(_val)))) { \ |
47cc5b78 CP |
124 | tuner_err("Error on line %d: %d\n", __LINE__, _rc); \ |
125 | } else \ | |
126 | msleep(10); \ | |
127 | _rc; \ | |
128 | }) | |
6cb45879 | 129 | |
7d58d111 | 130 | static unsigned int xc2028_get_reg(struct xc2028_data *priv, u16 reg, u16 *val) |
6cb45879 | 131 | { |
b873e1a3 | 132 | unsigned char buf[2]; |
7d58d111 | 133 | unsigned char ibuf[2]; |
215b95ba | 134 | |
7d58d111 | 135 | tuner_dbg("%s %04x called\n", __FUNCTION__, reg); |
6cb45879 | 136 | |
7d58d111 | 137 | buf[0] = reg >> 8; |
80b52208 | 138 | buf[1] = (unsigned char) reg; |
6cb45879 | 139 | |
7d58d111 CP |
140 | if (i2c_send_recv(priv, buf, 2, ibuf, 2) != 2) |
141 | return -EIO; | |
6cb45879 | 142 | |
7d58d111 CP |
143 | *val = (ibuf[1]) | (ibuf[0] << 8); |
144 | return 0; | |
6cb45879 MCC |
145 | } |
146 | ||
e0262688 CP |
147 | #define dump_firm_type(t) dump_firm_type_and_int_freq(t, 0) |
148 | void dump_firm_type_and_int_freq(unsigned int type, u16 int_freq) | |
43efe702 MCC |
149 | { |
150 | if (type & BASE) | |
151 | printk("BASE "); | |
f380e1d2 MCC |
152 | if (type & INIT1) |
153 | printk("INIT1 "); | |
43efe702 MCC |
154 | if (type & F8MHZ) |
155 | printk("F8MHZ "); | |
156 | if (type & MTS) | |
157 | printk("MTS "); | |
158 | if (type & D2620) | |
159 | printk("D2620 "); | |
160 | if (type & D2633) | |
161 | printk("D2633 "); | |
162 | if (type & DTV6) | |
163 | printk("DTV6 "); | |
164 | if (type & QAM) | |
165 | printk("QAM "); | |
166 | if (type & DTV7) | |
167 | printk("DTV7 "); | |
168 | if (type & DTV78) | |
169 | printk("DTV78 "); | |
170 | if (type & DTV8) | |
171 | printk("DTV8 "); | |
172 | if (type & FM) | |
173 | printk("FM "); | |
174 | if (type & INPUT1) | |
175 | printk("INPUT1 "); | |
176 | if (type & LCD) | |
177 | printk("LCD "); | |
178 | if (type & NOGD) | |
179 | printk("NOGD "); | |
180 | if (type & MONO) | |
181 | printk("MONO "); | |
182 | if (type & ATSC) | |
183 | printk("ATSC "); | |
184 | if (type & IF) | |
185 | printk("IF "); | |
186 | if (type & LG60) | |
187 | printk("LG60 "); | |
188 | if (type & ATI638) | |
189 | printk("ATI638 "); | |
190 | if (type & OREN538) | |
191 | printk("OREN538 "); | |
192 | if (type & OREN36) | |
193 | printk("OREN36 "); | |
194 | if (type & TOYOTA388) | |
195 | printk("TOYOTA388 "); | |
196 | if (type & TOYOTA794) | |
197 | printk("TOYOTA794 "); | |
198 | if (type & DIBCOM52) | |
199 | printk("DIBCOM52 "); | |
200 | if (type & ZARLINK456) | |
201 | printk("ZARLINK456 "); | |
202 | if (type & CHINA) | |
203 | printk("CHINA "); | |
204 | if (type & F6MHZ) | |
205 | printk("F6MHZ "); | |
206 | if (type & INPUT2) | |
207 | printk("INPUT2 "); | |
208 | if (type & SCODE) | |
209 | printk("SCODE "); | |
e0262688 CP |
210 | if (type & HAS_IF) |
211 | printk("HAS_IF_%d ", int_freq); | |
43efe702 MCC |
212 | } |
213 | ||
ef8c1888 | 214 | static v4l2_std_id parse_audio_std_option(void) |
a82200fb | 215 | { |
e155d908 | 216 | if (strcasecmp(audio_std, "A2") == 0) |
a82200fb | 217 | return V4L2_STD_A2; |
e155d908 | 218 | if (strcasecmp(audio_std, "A2/A") == 0) |
a82200fb | 219 | return V4L2_STD_A2_A; |
e155d908 | 220 | if (strcasecmp(audio_std, "A2/B") == 0) |
a82200fb | 221 | return V4L2_STD_A2_B; |
e155d908 | 222 | if (strcasecmp(audio_std, "NICAM") == 0) |
a82200fb | 223 | return V4L2_STD_NICAM; |
e155d908 | 224 | if (strcasecmp(audio_std, "NICAM/A") == 0) |
a82200fb | 225 | return V4L2_STD_NICAM_A; |
e155d908 | 226 | if (strcasecmp(audio_std, "NICAM/B") == 0) |
a82200fb MCC |
227 | return V4L2_STD_NICAM_B; |
228 | ||
229 | return 0; | |
230 | } | |
231 | ||
ab0b9fc6 | 232 | static void free_firmware(struct xc2028_data *priv) |
6cb45879 | 233 | { |
de3fe21b MCC |
234 | int i; |
235 | ||
236 | if (!priv->firm) | |
237 | return; | |
238 | ||
ab0b9fc6 MCC |
239 | for (i = 0; i < priv->firm_size; i++) |
240 | kfree(priv->firm[i].ptr); | |
241 | ||
de3fe21b MCC |
242 | kfree(priv->firm); |
243 | ||
ab0b9fc6 | 244 | priv->firm = NULL; |
06fd82dc | 245 | priv->firm_size = 0; |
e0f0b37a CP |
246 | |
247 | memset(&priv->cur_fw, 0, sizeof(priv->cur_fw)); | |
de3fe21b MCC |
248 | } |
249 | ||
ab0b9fc6 | 250 | static int load_all_firmwares(struct dvb_frontend *fe) |
de3fe21b MCC |
251 | { |
252 | struct xc2028_data *priv = fe->tuner_priv; | |
ab0b9fc6 | 253 | const struct firmware *fw = NULL; |
6cb45879 | 254 | unsigned char *p, *endp; |
ab0b9fc6 MCC |
255 | int rc = 0; |
256 | int n, n_array; | |
de3fe21b | 257 | char name[33]; |
6cb45879 | 258 | |
83fb340b | 259 | tuner_dbg("%s called\n", __FUNCTION__); |
215b95ba | 260 | |
06fd82dc | 261 | tuner_dbg("Reading firmware %s\n", priv->ctrl.fname); |
a37b4c9b ML |
262 | rc = request_firmware(&fw, priv->ctrl.fname, |
263 | &priv->i2c_props.adap->dev); | |
6cb45879 | 264 | if (rc < 0) { |
ab0b9fc6 | 265 | if (rc == -ENOENT) |
83fb340b | 266 | tuner_err("Error: firmware %s not found.\n", |
de3fe21b | 267 | priv->ctrl.fname); |
2e4160ca | 268 | else |
83fb340b | 269 | tuner_err("Error %d while requesting firmware %s \n", |
de3fe21b | 270 | rc, priv->ctrl.fname); |
2e4160ca | 271 | |
6cb45879 MCC |
272 | return rc; |
273 | } | |
ab0b9fc6 MCC |
274 | p = fw->data; |
275 | endp = p + fw->size; | |
6cb45879 | 276 | |
06fd82dc CP |
277 | if (fw->size < sizeof(name) - 1 + 2 + 2) { |
278 | tuner_err("Error: firmware file %s has invalid size!\n", | |
279 | priv->ctrl.fname); | |
280 | goto corrupt; | |
6cb45879 | 281 | } |
de3fe21b | 282 | |
ab0b9fc6 MCC |
283 | memcpy(name, p, sizeof(name) - 1); |
284 | name[sizeof(name) - 1] = 0; | |
285 | p += sizeof(name) - 1; | |
de3fe21b | 286 | |
06fd82dc | 287 | priv->firm_version = le16_to_cpu(*(__u16 *) p); |
de3fe21b MCC |
288 | p += 2; |
289 | ||
ab0b9fc6 | 290 | n_array = le16_to_cpu(*(__u16 *) p); |
de3fe21b MCC |
291 | p += 2; |
292 | ||
06fd82dc CP |
293 | tuner_info("Loading %d firmware images from %s, type: %s, ver %d.%d\n", |
294 | n_array, priv->ctrl.fname, name, | |
295 | priv->firm_version >> 8, priv->firm_version & 0xff); | |
de3fe21b | 296 | |
ab0b9fc6 | 297 | priv->firm = kzalloc(sizeof(*priv->firm) * n_array, GFP_KERNEL); |
06fd82dc CP |
298 | if (priv->firm == NULL) { |
299 | tuner_err("Not enough memory to load firmware file.\n"); | |
ab0b9fc6 | 300 | rc = -ENOMEM; |
06fd82dc | 301 | goto err; |
6cb45879 | 302 | } |
de3fe21b | 303 | priv->firm_size = n_array; |
06fd82dc | 304 | |
ab0b9fc6 MCC |
305 | n = -1; |
306 | while (p < endp) { | |
de3fe21b MCC |
307 | __u32 type, size; |
308 | v4l2_std_id id; | |
66c2d53d | 309 | __u16 int_freq = 0; |
de3fe21b MCC |
310 | |
311 | n++; | |
312 | if (n >= n_array) { | |
06fd82dc CP |
313 | tuner_err("More firmware images in file than " |
314 | "were expected!\n"); | |
de3fe21b MCC |
315 | goto corrupt; |
316 | } | |
317 | ||
318 | /* Checks if there's enough bytes to read */ | |
ab0b9fc6 | 319 | if (p + sizeof(type) + sizeof(id) + sizeof(size) > endp) { |
83fb340b | 320 | tuner_err("Firmware header is incomplete!\n"); |
de3fe21b MCC |
321 | goto corrupt; |
322 | } | |
323 | ||
ab0b9fc6 | 324 | type = le32_to_cpu(*(__u32 *) p); |
de3fe21b MCC |
325 | p += sizeof(type); |
326 | ||
ab0b9fc6 | 327 | id = le64_to_cpu(*(v4l2_std_id *) p); |
de3fe21b MCC |
328 | p += sizeof(id); |
329 | ||
66c2d53d MCC |
330 | if (type & HAS_IF) { |
331 | int_freq = le16_to_cpu(*(__u16 *) p); | |
332 | p += sizeof(int_freq); | |
333 | } | |
334 | ||
2fc580ff | 335 | size = le32_to_cpu(*(__u32 *) p); |
de3fe21b MCC |
336 | p += sizeof(size); |
337 | ||
ab0b9fc6 | 338 | if ((!size) || (size + p > endp)) { |
83fb340b | 339 | tuner_err("Firmware type "); |
43efe702 | 340 | dump_firm_type(type); |
ef8c1888 MCC |
341 | printk("(%x), id %llx is corrupted " |
342 | "(size=%d, expected %d)\n", | |
91240dd9 | 343 | type, (unsigned long long)id, |
ef8c1888 | 344 | (unsigned)(endp - p), size); |
de3fe21b MCC |
345 | goto corrupt; |
346 | } | |
347 | ||
ab0b9fc6 | 348 | priv->firm[n].ptr = kzalloc(size, GFP_KERNEL); |
06fd82dc CP |
349 | if (priv->firm[n].ptr == NULL) { |
350 | tuner_err("Not enough memory to load firmware file.\n"); | |
ab0b9fc6 | 351 | rc = -ENOMEM; |
de3fe21b MCC |
352 | goto err; |
353 | } | |
06fd82dc CP |
354 | tuner_dbg("Reading firmware type "); |
355 | if (debug) { | |
e0262688 | 356 | dump_firm_type_and_int_freq(type, int_freq); |
06fd82dc | 357 | printk("(%x), id %llx, size=%d.\n", |
e0262688 | 358 | type, (unsigned long long)id, size); |
06fd82dc | 359 | } |
de3fe21b MCC |
360 | |
361 | memcpy(priv->firm[n].ptr, p, size); | |
362 | priv->firm[n].type = type; | |
363 | priv->firm[n].id = id; | |
364 | priv->firm[n].size = size; | |
66c2d53d | 365 | priv->firm[n].int_freq = int_freq; |
de3fe21b MCC |
366 | |
367 | p += size; | |
368 | } | |
369 | ||
ab0b9fc6 | 370 | if (n + 1 != priv->firm_size) { |
83fb340b | 371 | tuner_err("Firmware file is incomplete!\n"); |
de3fe21b MCC |
372 | goto corrupt; |
373 | } | |
374 | ||
375 | goto done; | |
376 | ||
377 | corrupt: | |
ab0b9fc6 | 378 | rc = -EINVAL; |
83fb340b | 379 | tuner_err("Error: firmware file is corrupted!\n"); |
de3fe21b MCC |
380 | |
381 | err: | |
06fd82dc | 382 | tuner_info("Releasing partially loaded firmware file.\n"); |
de3fe21b MCC |
383 | free_firmware(priv); |
384 | ||
385 | done: | |
386 | release_firmware(fw); | |
06fd82dc CP |
387 | if (rc == 0) |
388 | tuner_dbg("Firmware files loaded.\n"); | |
de3fe21b MCC |
389 | |
390 | return rc; | |
391 | } | |
392 | ||
f380e1d2 MCC |
393 | static int seek_firmware(struct dvb_frontend *fe, unsigned int type, |
394 | v4l2_std_id *id) | |
de3fe21b MCC |
395 | { |
396 | struct xc2028_data *priv = fe->tuner_priv; | |
b1535293 | 397 | int i, best_i = -1, best_nr_matches = 0; |
de3fe21b | 398 | |
b1535293 CP |
399 | tuner_dbg("%s called, want type=", __FUNCTION__); |
400 | if (debug) { | |
401 | dump_firm_type(type); | |
402 | printk("(%x), id %016llx.\n", type, (unsigned long long)*id); | |
403 | } | |
de3fe21b MCC |
404 | |
405 | if (!priv->firm) { | |
83fb340b | 406 | tuner_err("Error! firmware not loaded\n"); |
de3fe21b MCC |
407 | return -EINVAL; |
408 | } | |
409 | ||
f380e1d2 | 410 | if (((type & ~SCODE) == 0) && (*id == 0)) |
ab0b9fc6 | 411 | *id = V4L2_STD_PAL; |
de3fe21b | 412 | |
e0f0b37a CP |
413 | if (type & BASE) |
414 | type &= BASE_TYPES; | |
415 | else if (type & SCODE) | |
416 | type &= SCODE_TYPES; | |
417 | else if (type & DTV_TYPES) | |
11a9eff9 CP |
418 | type &= DTV_TYPES; |
419 | else if (type & STD_SPECIFIC_TYPES) | |
420 | type &= STD_SPECIFIC_TYPES; | |
e0f0b37a | 421 | |
de3fe21b | 422 | /* Seek for exact match */ |
ab0b9fc6 MCC |
423 | for (i = 0; i < priv->firm_size; i++) { |
424 | if ((type == priv->firm[i].type) && (*id == priv->firm[i].id)) | |
de3fe21b MCC |
425 | goto found; |
426 | } | |
427 | ||
428 | /* Seek for generic video standard match */ | |
ab0b9fc6 | 429 | for (i = 0; i < priv->firm_size; i++) { |
b1535293 CP |
430 | v4l2_std_id match_mask; |
431 | int nr_matches; | |
432 | ||
433 | if (type != priv->firm[i].type) | |
434 | continue; | |
435 | ||
436 | match_mask = *id & priv->firm[i].id; | |
437 | if (!match_mask) | |
438 | continue; | |
439 | ||
440 | if ((*id & match_mask) == *id) | |
441 | goto found; /* Supports all the requested standards */ | |
442 | ||
443 | nr_matches = hweight64(match_mask); | |
444 | if (nr_matches > best_nr_matches) { | |
445 | best_nr_matches = nr_matches; | |
446 | best_i = i; | |
447 | } | |
448 | } | |
449 | ||
450 | if (best_nr_matches > 0) { | |
451 | tuner_dbg("Selecting best matching firmware (%d bits) for " | |
452 | "type=", best_nr_matches); | |
453 | dump_firm_type(type); | |
454 | printk("(%x), id %016llx:\n", type, (unsigned long long)*id); | |
455 | i = best_i; | |
456 | goto found; | |
de3fe21b MCC |
457 | } |
458 | ||
459 | /*FIXME: Would make sense to seek for type "hint" match ? */ | |
460 | ||
b1535293 | 461 | i = -ENOENT; |
f380e1d2 | 462 | goto ret; |
de3fe21b MCC |
463 | |
464 | found: | |
465 | *id = priv->firm[i].id; | |
de3fe21b | 466 | |
f380e1d2 | 467 | ret: |
b1535293 | 468 | tuner_dbg("%s firmware for type=", (i < 0) ? "Can't find" : "Found"); |
83fb340b MCC |
469 | if (debug) { |
470 | dump_firm_type(type); | |
91240dd9 | 471 | printk("(%x), id %016llx.\n", type, (unsigned long long)*id); |
83fb340b | 472 | } |
f380e1d2 MCC |
473 | return i; |
474 | } | |
475 | ||
476 | static int load_firmware(struct dvb_frontend *fe, unsigned int type, | |
477 | v4l2_std_id *id) | |
478 | { | |
479 | struct xc2028_data *priv = fe->tuner_priv; | |
480 | int pos, rc; | |
0a196b6f | 481 | unsigned char *p, *endp, buf[priv->ctrl.max_len]; |
f380e1d2 | 482 | |
83fb340b | 483 | tuner_dbg("%s called\n", __FUNCTION__); |
f380e1d2 MCC |
484 | |
485 | pos = seek_firmware(fe, type, id); | |
486 | if (pos < 0) | |
487 | return pos; | |
488 | ||
83fb340b | 489 | tuner_info("Loading firmware for type="); |
b1535293 CP |
490 | dump_firm_type(priv->firm[pos].type); |
491 | printk("(%x), id %016llx.\n", priv->firm[pos].type, | |
492 | (unsigned long long)*id); | |
83fb340b | 493 | |
f380e1d2 | 494 | p = priv->firm[pos].ptr; |
f380e1d2 | 495 | endp = p + priv->firm[pos].size; |
6cb45879 | 496 | |
ab0b9fc6 | 497 | while (p < endp) { |
de3fe21b MCC |
498 | __u16 size; |
499 | ||
500 | /* Checks if there's enough bytes to read */ | |
ab0b9fc6 | 501 | if (p + sizeof(size) > endp) { |
83fb340b | 502 | tuner_err("Firmware chunk size is wrong\n"); |
de3fe21b MCC |
503 | return -EINVAL; |
504 | } | |
505 | ||
ab0b9fc6 | 506 | size = le16_to_cpu(*(__u16 *) p); |
de3fe21b MCC |
507 | p += sizeof(size); |
508 | ||
509 | if (size == 0xffff) | |
510 | return 0; | |
511 | ||
512 | if (!size) { | |
6cb45879 | 513 | /* Special callback command received */ |
215b95ba | 514 | rc = priv->tuner_callback(priv->video_dev, |
ab0b9fc6 MCC |
515 | XC2028_TUNER_RESET, 0); |
516 | if (rc < 0) { | |
83fb340b | 517 | tuner_err("Error at RESET code %d\n", |
ab0b9fc6 | 518 | (*p) & 0x7f); |
de3fe21b | 519 | return -EINVAL; |
6cb45879 | 520 | } |
6cb45879 MCC |
521 | continue; |
522 | } | |
5403bbae ML |
523 | if (size >= 0xff00) { |
524 | switch (size) { | |
525 | case 0xff00: | |
526 | rc = priv->tuner_callback(priv->video_dev, | |
527 | XC2028_RESET_CLK, 0); | |
528 | if (rc < 0) { | |
529 | tuner_err("Error at RESET code %d\n", | |
530 | (*p) & 0x7f); | |
531 | return -EINVAL; | |
532 | } | |
b32f9fb9 | 533 | break; |
5403bbae ML |
534 | default: |
535 | tuner_info("Invalid RESET code %d\n", | |
536 | size & 0x7f); | |
537 | return -EINVAL; | |
538 | ||
539 | } | |
2d4c0ac6 | 540 | continue; |
5403bbae | 541 | } |
de3fe21b MCC |
542 | |
543 | /* Checks for a sleep command */ | |
544 | if (size & 0x8000) { | |
ab0b9fc6 | 545 | msleep(size & 0x7fff); |
de3fe21b | 546 | continue; |
6cb45879 MCC |
547 | } |
548 | ||
de3fe21b | 549 | if ((size + p > endp)) { |
83fb340b | 550 | tuner_err("missing bytes: need %d, have %d\n", |
ab0b9fc6 | 551 | size, (int)(endp - p)); |
de3fe21b MCC |
552 | return -EINVAL; |
553 | } | |
6cb45879 | 554 | |
de3fe21b | 555 | buf[0] = *p; |
6cb45879 | 556 | p++; |
de3fe21b | 557 | size--; |
6cb45879 | 558 | |
de3fe21b | 559 | /* Sends message chunks */ |
ab0b9fc6 | 560 | while (size > 0) { |
0a196b6f CP |
561 | int len = (size < priv->ctrl.max_len - 1) ? |
562 | size : priv->ctrl.max_len - 1; | |
6cb45879 | 563 | |
ab0b9fc6 | 564 | memcpy(buf + 1, p, len); |
6cb45879 | 565 | |
47cc5b78 | 566 | rc = i2c_send(priv, buf, len + 1); |
ab0b9fc6 | 567 | if (rc < 0) { |
83fb340b | 568 | tuner_err("%d returned from send\n", rc); |
de3fe21b MCC |
569 | return -EINVAL; |
570 | } | |
571 | ||
572 | p += len; | |
573 | size -= len; | |
574 | } | |
575 | } | |
43efe702 | 576 | return 0; |
6cb45879 MCC |
577 | } |
578 | ||
f380e1d2 | 579 | static int load_scode(struct dvb_frontend *fe, unsigned int type, |
66c2d53d | 580 | v4l2_std_id *id, __u16 int_freq, int scode) |
f380e1d2 MCC |
581 | { |
582 | struct xc2028_data *priv = fe->tuner_priv; | |
583 | int pos, rc; | |
584 | unsigned char *p; | |
585 | ||
83fb340b | 586 | tuner_dbg("%s called\n", __FUNCTION__); |
f380e1d2 | 587 | |
66c2d53d MCC |
588 | if (!int_freq) { |
589 | pos = seek_firmware(fe, type, id); | |
590 | if (pos < 0) | |
591 | return pos; | |
592 | } else { | |
593 | for (pos = 0; pos < priv->firm_size; pos++) { | |
594 | if ((priv->firm[pos].int_freq == int_freq) && | |
9ca01e78 | 595 | (priv->firm[pos].type & HAS_IF)) |
66c2d53d MCC |
596 | break; |
597 | } | |
598 | if (pos == priv->firm_size) | |
599 | return -ENOENT; | |
600 | } | |
f380e1d2 MCC |
601 | |
602 | p = priv->firm[pos].ptr; | |
603 | ||
9ca01e78 | 604 | if (priv->firm[pos].type & HAS_IF) { |
66c2d53d MCC |
605 | if (priv->firm[pos].size != 12 * 16 || scode >= 16) |
606 | return -EINVAL; | |
607 | p += 12 * scode; | |
608 | } else { | |
609 | /* 16 SCODE entries per file; each SCODE entry is 12 bytes and | |
610 | * has a 2-byte size header in the firmware format. */ | |
611 | if (priv->firm[pos].size != 14 * 16 || scode >= 16 || | |
612 | le16_to_cpu(*(__u16 *)(p + 14 * scode)) != 12) | |
613 | return -EINVAL; | |
614 | p += 14 * scode + 2; | |
615 | } | |
f380e1d2 | 616 | |
d7b22c5c | 617 | tuner_info("Loading SCODE for type="); |
e0262688 CP |
618 | dump_firm_type_and_int_freq(priv->firm[pos].type, |
619 | priv->firm[pos].int_freq); | |
d7b22c5c CP |
620 | printk("(%x), id %016llx.\n", priv->firm[pos].type, |
621 | (unsigned long long)*id); | |
622 | ||
06fd82dc | 623 | if (priv->firm_version < 0x0202) |
47cc5b78 CP |
624 | rc = send_seq(priv, {0x20, 0x00, 0x00, 0x00}); |
625 | else | |
626 | rc = send_seq(priv, {0xa0, 0x00, 0x00, 0x00}); | |
627 | if (rc < 0) | |
628 | return -EIO; | |
f380e1d2 | 629 | |
66c2d53d | 630 | rc = i2c_send(priv, p, 12); |
47cc5b78 CP |
631 | if (rc < 0) |
632 | return -EIO; | |
f380e1d2 | 633 | |
47cc5b78 CP |
634 | rc = send_seq(priv, {0x00, 0x8c}); |
635 | if (rc < 0) | |
636 | return -EIO; | |
f380e1d2 MCC |
637 | |
638 | return 0; | |
639 | } | |
640 | ||
00deff1a | 641 | static int check_firmware(struct dvb_frontend *fe, unsigned int type, |
66c2d53d | 642 | v4l2_std_id std, __u16 int_freq) |
6cb45879 | 643 | { |
00deff1a | 644 | struct xc2028_data *priv = fe->tuner_priv; |
e0f0b37a | 645 | struct firmware_properties new_fw; |
00deff1a MCC |
646 | int rc = 0, is_retry = 0; |
647 | u16 version, hwmodel; | |
648 | v4l2_std_id std0; | |
6cb45879 | 649 | |
83fb340b | 650 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 651 | |
de3fe21b | 652 | if (!priv->firm) { |
a37b4c9b ML |
653 | if (!priv->ctrl.fname) { |
654 | tuner_info("xc2028/3028 firmware name not set!\n"); | |
de3fe21b | 655 | return -EINVAL; |
a37b4c9b | 656 | } |
de3fe21b | 657 | |
ab0b9fc6 MCC |
658 | rc = load_all_firmwares(fe); |
659 | if (rc < 0) | |
de3fe21b MCC |
660 | return rc; |
661 | } | |
662 | ||
5add9a6f | 663 | if (priv->ctrl.mts) |
e0f0b37a | 664 | type |= MTS; |
6cb45879 | 665 | |
8bf799a6 | 666 | retry: |
e0f0b37a CP |
667 | new_fw.type = type; |
668 | new_fw.id = std; | |
669 | new_fw.std_req = std; | |
670 | new_fw.scode_table = SCODE | priv->ctrl.scode_table; | |
671 | new_fw.scode_nr = 0; | |
66c2d53d | 672 | new_fw.int_freq = int_freq; |
e0f0b37a CP |
673 | |
674 | tuner_dbg("checking firmware, user requested type="); | |
675 | if (debug) { | |
676 | dump_firm_type(new_fw.type); | |
e0262688 | 677 | printk("(%x), id %016llx, ", new_fw.type, |
e0f0b37a | 678 | (unsigned long long)new_fw.std_req); |
e0262688 CP |
679 | if (!int_freq) { |
680 | printk("scode_tbl "); | |
681 | dump_firm_type(priv->ctrl.scode_table); | |
682 | printk("(%x), ", priv->ctrl.scode_table); | |
683 | } else | |
684 | printk("int_freq %d, ", new_fw.int_freq); | |
685 | printk("scode_nr %d\n", new_fw.scode_nr); | |
e0f0b37a CP |
686 | } |
687 | ||
688 | /* No need to reload base firmware if it matches */ | |
689 | if (((BASE | new_fw.type) & BASE_TYPES) == | |
690 | (priv->cur_fw.type & BASE_TYPES)) { | |
691 | tuner_dbg("BASE firmware not changed.\n"); | |
692 | goto skip_base; | |
693 | } | |
694 | ||
695 | /* Updating BASE - forget about all currently loaded firmware */ | |
696 | memset(&priv->cur_fw, 0, sizeof(priv->cur_fw)); | |
697 | ||
698 | /* Reset is needed before loading firmware */ | |
699 | rc = priv->tuner_callback(priv->video_dev, | |
700 | XC2028_TUNER_RESET, 0); | |
701 | if (rc < 0) | |
702 | goto fail; | |
703 | ||
47bd5bc6 CP |
704 | /* BASE firmwares are all std0 */ |
705 | std0 = 0; | |
706 | rc = load_firmware(fe, BASE | new_fw.type, &std0); | |
e0f0b37a CP |
707 | if (rc < 0) { |
708 | tuner_err("Error %d while loading base firmware\n", | |
709 | rc); | |
710 | goto fail; | |
711 | } | |
5403bbae | 712 | |
de3fe21b | 713 | /* Load INIT1, if needed */ |
83fb340b | 714 | tuner_dbg("Load init1 firmware, if exists\n"); |
de3fe21b | 715 | |
47bd5bc6 | 716 | rc = load_firmware(fe, BASE | INIT1 | new_fw.type, &std0); |
1ad0b796 CP |
717 | if (rc == -ENOENT) |
718 | rc = load_firmware(fe, (BASE | INIT1 | new_fw.type) & ~F8MHZ, | |
719 | &std0); | |
e0f0b37a CP |
720 | if (rc < 0 && rc != -ENOENT) { |
721 | tuner_err("Error %d while loading init1 firmware\n", | |
722 | rc); | |
723 | goto fail; | |
724 | } | |
de3fe21b | 725 | |
e0f0b37a CP |
726 | skip_base: |
727 | /* | |
728 | * No need to reload standard specific firmware if base firmware | |
729 | * was not reloaded and requested video standards have not changed. | |
de3fe21b | 730 | */ |
e0f0b37a CP |
731 | if (priv->cur_fw.type == (BASE | new_fw.type) && |
732 | priv->cur_fw.std_req == std) { | |
83fb340b | 733 | tuner_dbg("Std-specific firmware already loaded.\n"); |
e0f0b37a | 734 | goto skip_std_specific; |
2e4160ca | 735 | } |
6cb45879 | 736 | |
e0f0b37a CP |
737 | /* Reloading std-specific firmware forces a SCODE update */ |
738 | priv->cur_fw.scode_table = 0; | |
739 | ||
e0f0b37a | 740 | rc = load_firmware(fe, new_fw.type, &new_fw.id); |
cca83798 MCC |
741 | if (rc == -ENOENT) |
742 | rc = load_firmware(fe, new_fw.type & ~F8MHZ, &new_fw.id); | |
743 | ||
ab0b9fc6 | 744 | if (rc < 0) |
e0f0b37a CP |
745 | goto fail; |
746 | ||
747 | skip_std_specific: | |
748 | if (priv->cur_fw.scode_table == new_fw.scode_table && | |
749 | priv->cur_fw.scode_nr == new_fw.scode_nr) { | |
750 | tuner_dbg("SCODE firmware already loaded.\n"); | |
751 | goto check_device; | |
752 | } | |
6cb45879 | 753 | |
f380e1d2 | 754 | /* Load SCODE firmware, if exists */ |
e0f0b37a | 755 | tuner_dbg("Trying to load scode %d\n", new_fw.scode_nr); |
f380e1d2 | 756 | |
66c2d53d MCC |
757 | rc = load_scode(fe, new_fw.type | new_fw.scode_table, &new_fw.id, |
758 | new_fw.int_freq, new_fw.scode_nr); | |
43efe702 | 759 | |
e0f0b37a | 760 | check_device: |
8bf799a6 CP |
761 | if (xc2028_get_reg(priv, 0x0004, &version) < 0 || |
762 | xc2028_get_reg(priv, 0x0008, &hwmodel) < 0) { | |
763 | tuner_err("Unable to read tuner registers.\n"); | |
764 | goto fail; | |
765 | } | |
80b52208 MCC |
766 | |
767 | tuner_info("Device is Xceive %d version %d.%d, " | |
768 | "firmware version %d.%d\n", | |
769 | hwmodel, (version & 0xf000) >> 12, (version & 0xf00) >> 8, | |
770 | (version & 0xf0) >> 4, version & 0xf); | |
6cb45879 | 771 | |
8bf799a6 CP |
772 | /* Check firmware version against what we downloaded. */ |
773 | if (priv->firm_version != ((version & 0xf0) << 4 | (version & 0x0f))) { | |
774 | tuner_err("Incorrect readback of firmware version.\n"); | |
775 | goto fail; | |
776 | } | |
777 | ||
778 | /* Check that the tuner hardware model remains consistent over time. */ | |
779 | if (priv->hwmodel == 0 && (hwmodel == 2028 || hwmodel == 3028)) { | |
780 | priv->hwmodel = hwmodel; | |
781 | priv->hwvers = version & 0xff00; | |
782 | } else if (priv->hwmodel == 0 || priv->hwmodel != hwmodel || | |
783 | priv->hwvers != (version & 0xff00)) { | |
784 | tuner_err("Read invalid device hardware information - tuner " | |
785 | "hung?\n"); | |
786 | goto fail; | |
787 | } | |
788 | ||
e0f0b37a CP |
789 | memcpy(&priv->cur_fw, &new_fw, sizeof(priv->cur_fw)); |
790 | ||
791 | /* | |
792 | * By setting BASE in cur_fw.type only after successfully loading all | |
793 | * firmwares, we can: | |
794 | * 1. Identify that BASE firmware with type=0 has been loaded; | |
795 | * 2. Tell whether BASE firmware was just changed the next time through. | |
796 | */ | |
797 | priv->cur_fw.type |= BASE; | |
6cb45879 MCC |
798 | |
799 | return 0; | |
e0f0b37a CP |
800 | |
801 | fail: | |
802 | memset(&priv->cur_fw, 0, sizeof(priv->cur_fw)); | |
8bf799a6 CP |
803 | if (!is_retry) { |
804 | msleep(50); | |
805 | is_retry = 1; | |
806 | tuner_dbg("Retrying firmware load\n"); | |
807 | goto retry; | |
808 | } | |
809 | ||
e0f0b37a CP |
810 | if (rc == -ENOENT) |
811 | rc = -EINVAL; | |
812 | return rc; | |
6cb45879 MCC |
813 | } |
814 | ||
215b95ba | 815 | static int xc2028_signal(struct dvb_frontend *fe, u16 *strength) |
6cb45879 | 816 | { |
215b95ba | 817 | struct xc2028_data *priv = fe->tuner_priv; |
7d58d111 CP |
818 | u16 frq_lock, signal = 0; |
819 | int rc; | |
3b20532c | 820 | |
83fb340b | 821 | tuner_dbg("%s called\n", __FUNCTION__); |
6cb45879 | 822 | |
215b95ba | 823 | mutex_lock(&priv->lock); |
6cb45879 | 824 | |
80b52208 | 825 | /* Sync Lock Indicator */ |
7d58d111 CP |
826 | rc = xc2028_get_reg(priv, 0x0002, &frq_lock); |
827 | if (rc < 0 || frq_lock == 0) | |
3b20532c | 828 | goto ret; |
6cb45879 MCC |
829 | |
830 | /* Frequency is locked. Return signal quality */ | |
831 | ||
80b52208 | 832 | /* Get SNR of the video signal */ |
7d58d111 CP |
833 | rc = xc2028_get_reg(priv, 0x0040, &signal); |
834 | if (rc < 0) | |
835 | signal = -frq_lock; | |
3b20532c MCC |
836 | |
837 | ret: | |
215b95ba MCC |
838 | mutex_unlock(&priv->lock); |
839 | ||
840 | *strength = signal; | |
6cb45879 | 841 | |
7d58d111 | 842 | return rc; |
6cb45879 MCC |
843 | } |
844 | ||
845 | #define DIV 15625 | |
846 | ||
00deff1a | 847 | static int generic_set_freq(struct dvb_frontend *fe, u32 freq /* in HZ */, |
66c2d53d MCC |
848 | enum tuner_mode new_mode, |
849 | unsigned int type, | |
850 | v4l2_std_id std, | |
851 | u16 int_freq) | |
6cb45879 | 852 | { |
215b95ba | 853 | struct xc2028_data *priv = fe->tuner_priv; |
ab0b9fc6 | 854 | int rc = -EINVAL; |
2ce4b3aa | 855 | unsigned char buf[4]; |
ab0b9fc6 | 856 | u32 div, offset = 0; |
6cb45879 | 857 | |
83fb340b | 858 | tuner_dbg("%s called\n", __FUNCTION__); |
215b95ba | 859 | |
de3fe21b MCC |
860 | mutex_lock(&priv->lock); |
861 | ||
2ce4b3aa | 862 | tuner_dbg("should set frequency %d kHz\n", freq / 1000); |
6cb45879 | 863 | |
66c2d53d | 864 | if (check_firmware(fe, type, std, int_freq) < 0) |
3b20532c | 865 | goto ret; |
2e4160ca | 866 | |
2800ae9c MCC |
867 | /* On some cases xc2028 can disable video output, if |
868 | * very weak signals are received. By sending a soft | |
869 | * reset, this is re-enabled. So, it is better to always | |
870 | * send a soft reset before changing channels, to be sure | |
871 | * that xc2028 will be in a safe state. | |
872 | * Maybe this might also be needed for DTV. | |
873 | */ | |
00deff1a | 874 | if (new_mode == T_ANALOG_TV) { |
2800ae9c | 875 | rc = send_seq(priv, {0x00, 0x00}); |
00deff1a | 876 | } else { |
d4e76681 | 877 | offset = 2750000; |
e0f0b37a | 878 | if (priv->cur_fw.type & DTV7) |
a44f1c43 CP |
879 | offset -= 500000; |
880 | } | |
2e4160ca | 881 | |
ab0b9fc6 | 882 | div = (freq - offset + DIV / 2) / DIV; |
2e4160ca | 883 | |
6cb45879 | 884 | /* CMD= Set frequency */ |
06fd82dc | 885 | if (priv->firm_version < 0x0202) |
47cc5b78 CP |
886 | rc = send_seq(priv, {0x00, 0x02, 0x00, 0x00}); |
887 | else | |
888 | rc = send_seq(priv, {0x80, 0x02, 0x00, 0x00}); | |
889 | if (rc < 0) | |
890 | goto ret; | |
de3fe21b | 891 | |
215b95ba | 892 | rc = priv->tuner_callback(priv->video_dev, XC2028_RESET_CLK, 1); |
ab0b9fc6 | 893 | if (rc < 0) |
215b95ba | 894 | goto ret; |
6cb45879 MCC |
895 | |
896 | msleep(10); | |
701672eb | 897 | |
ab0b9fc6 MCC |
898 | buf[0] = 0xff & (div >> 24); |
899 | buf[1] = 0xff & (div >> 16); | |
900 | buf[2] = 0xff & (div >> 8); | |
901 | buf[3] = 0xff & (div); | |
6cb45879 | 902 | |
47cc5b78 | 903 | rc = i2c_send(priv, buf, sizeof(buf)); |
ab0b9fc6 | 904 | if (rc < 0) |
3b20532c | 905 | goto ret; |
6cb45879 MCC |
906 | msleep(100); |
907 | ||
ab0b9fc6 | 908 | priv->frequency = freq; |
215b95ba | 909 | |
2ce4b3aa CP |
910 | tuner_dbg("divisor= %02x %02x %02x %02x (freq=%d.%03d)\n", |
911 | buf[0], buf[1], buf[2], buf[3], | |
912 | freq / 1000000, (freq % 1000000) / 1000); | |
3b20532c | 913 | |
ab0b9fc6 | 914 | rc = 0; |
6cb45879 | 915 | |
215b95ba MCC |
916 | ret: |
917 | mutex_unlock(&priv->lock); | |
6cb45879 | 918 | |
215b95ba | 919 | return rc; |
701672eb ML |
920 | } |
921 | ||
00deff1a | 922 | static int xc2028_set_analog_freq(struct dvb_frontend *fe, |
ab0b9fc6 | 923 | struct analog_parameters *p) |
6cb45879 | 924 | { |
215b95ba | 925 | struct xc2028_data *priv = fe->tuner_priv; |
00deff1a MCC |
926 | unsigned int type=0; |
927 | ||
928 | tuner_dbg("%s called\n", __FUNCTION__); | |
c71d4bc5 | 929 | |
d74cb25e MCC |
930 | if (p->mode == V4L2_TUNER_RADIO) { |
931 | type |= FM; | |
932 | if (priv->ctrl.input1) | |
933 | type |= INPUT1; | |
934 | return generic_set_freq(fe, (625l * p->frequency) / 10, | |
66c2d53d | 935 | T_ANALOG_TV, type, 0, 0); |
d74cb25e MCC |
936 | } |
937 | ||
a5e9fe14 MCC |
938 | /* if std is not defined, choose one */ |
939 | if (!p->std) | |
940 | p->std = V4L2_STD_MN; | |
941 | ||
942 | /* PAL/M, PAL/N, PAL/Nc and NTSC variants should use 6MHz firmware */ | |
00deff1a MCC |
943 | if (!(p->std & V4L2_STD_MN)) |
944 | type |= F8MHZ; | |
6cb45879 | 945 | |
00deff1a MCC |
946 | /* Add audio hack to std mask */ |
947 | p->std |= parse_audio_std_option(); | |
6cb45879 | 948 | |
00deff1a | 949 | return generic_set_freq(fe, 62500l * p->frequency, |
66c2d53d | 950 | T_ANALOG_TV, type, p->std, 0); |
215b95ba | 951 | } |
6cb45879 | 952 | |
215b95ba MCC |
953 | static int xc2028_set_params(struct dvb_frontend *fe, |
954 | struct dvb_frontend_parameters *p) | |
6cb45879 | 955 | { |
215b95ba | 956 | struct xc2028_data *priv = fe->tuner_priv; |
00deff1a | 957 | unsigned int type=0; |
d04aa54a | 958 | fe_bandwidth_t bw = BANDWIDTH_8_MHZ; |
ad35ce9e | 959 | u16 demod = 0; |
6cb45879 | 960 | |
83fb340b | 961 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 962 | |
00deff1a MCC |
963 | if (priv->ctrl.d2633) |
964 | type |= D2633; | |
965 | else | |
966 | type |= D2620; | |
967 | ||
d04aa54a | 968 | switch(fe->ops.info.type) { |
d04aa54a MCC |
969 | case FE_OFDM: |
970 | bw = p->u.ofdm.bandwidth; | |
971 | break; | |
972 | case FE_QAM: | |
5c15648a MCC |
973 | tuner_info("WARN: There are some reports that " |
974 | "QAM 6 MHz doesn't work.\n" | |
975 | "If this works for you, please report by " | |
976 | "e-mail to: v4l-dvb-maintainer@linuxtv.org\n"); | |
d04aa54a MCC |
977 | bw = BANDWIDTH_6_MHZ; |
978 | type |= QAM; | |
979 | break; | |
980 | case FE_ATSC: | |
981 | bw = BANDWIDTH_6_MHZ; | |
4bfae52b MCC |
982 | /* The only ATSC firmware (at least on v2.7) is D2633, |
983 | so overrides ctrl->d2633 */ | |
d04aa54a | 984 | type |= ATSC| D2633; |
4bfae52b | 985 | type &= ~D2620; |
d04aa54a | 986 | break; |
5c15648a MCC |
987 | /* DVB-S is not supported */ |
988 | default: | |
989 | return -EINVAL; | |
d04aa54a MCC |
990 | } |
991 | ||
d04aa54a MCC |
992 | /* FIXME: |
993 | There are two Scodes that will never be selected: | |
994 | DTV78 ZARLINK456, DTV78 DIBCOM52 | |
995 | When it should opt for DTV78 instead of DTV7 or DTV8? | |
996 | */ | |
00deff1a MCC |
997 | switch (bw) { |
998 | case BANDWIDTH_8_MHZ: | |
d04aa54a | 999 | type |= DTV8 | F8MHZ; |
00deff1a MCC |
1000 | break; |
1001 | case BANDWIDTH_7_MHZ: | |
d04aa54a | 1002 | type |= DTV7 | F8MHZ; |
00deff1a MCC |
1003 | break; |
1004 | case BANDWIDTH_6_MHZ: | |
d04aa54a | 1005 | type |= DTV6 ; |
00deff1a MCC |
1006 | break; |
1007 | default: | |
1008 | tuner_err("error: bandwidth not supported.\n"); | |
1009 | }; | |
1010 | ||
66c2d53d MCC |
1011 | /* All S-code tables need a 200kHz shift */ |
1012 | if (priv->ctrl.demod) | |
ad35ce9e | 1013 | demod = priv->ctrl.demod + 200; |
b542dfdc | 1014 | |
00deff1a | 1015 | return generic_set_freq(fe, p->frequency, |
ad35ce9e | 1016 | T_DIGITAL_TV, type, 0, demod); |
6cb45879 | 1017 | } |
701672eb | 1018 | |
45819c38 CP |
1019 | static int xc2028_sleep(struct dvb_frontend *fe) |
1020 | { | |
1021 | struct xc2028_data *priv = fe->tuner_priv; | |
1022 | int rc = 0; | |
1023 | ||
1024 | tuner_dbg("%s called\n", __FUNCTION__); | |
1025 | ||
1026 | mutex_lock(&priv->lock); | |
1027 | ||
1028 | if (priv->firm_version < 0x0202) | |
1029 | rc = send_seq(priv, {0x00, 0x08, 0x00, 0x00}); | |
1030 | else | |
1031 | rc = send_seq(priv, {0x80, 0x08, 0x00, 0x00}); | |
1032 | ||
1033 | priv->cur_fw.type = 0; /* need firmware reload */ | |
1034 | ||
1035 | mutex_unlock(&priv->lock); | |
1036 | ||
1037 | return rc; | |
1038 | } | |
1039 | ||
1040 | ||
215b95ba | 1041 | static int xc2028_dvb_release(struct dvb_frontend *fe) |
701672eb | 1042 | { |
215b95ba MCC |
1043 | struct xc2028_data *priv = fe->tuner_priv; |
1044 | ||
83fb340b | 1045 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 1046 | |
aa501be9 CP |
1047 | mutex_lock(&xc2028_list_mutex); |
1048 | ||
215b95ba | 1049 | priv->count--; |
701672eb | 1050 | |
de3fe21b | 1051 | if (!priv->count) { |
1808a698 MCC |
1052 | list_del(&priv->xc2028_list); |
1053 | ||
ab0b9fc6 | 1054 | kfree(priv->ctrl.fname); |
de3fe21b MCC |
1055 | |
1056 | free_firmware(priv); | |
ab0b9fc6 | 1057 | kfree(priv); |
06fd82dc | 1058 | fe->tuner_priv = NULL; |
de3fe21b | 1059 | } |
701672eb | 1060 | |
aa501be9 CP |
1061 | mutex_unlock(&xc2028_list_mutex); |
1062 | ||
701672eb ML |
1063 | return 0; |
1064 | } | |
1065 | ||
215b95ba | 1066 | static int xc2028_get_frequency(struct dvb_frontend *fe, u32 *frequency) |
701672eb | 1067 | { |
215b95ba | 1068 | struct xc2028_data *priv = fe->tuner_priv; |
701672eb | 1069 | |
83fb340b | 1070 | tuner_dbg("%s called\n", __FUNCTION__); |
701672eb | 1071 | |
215b95ba | 1072 | *frequency = priv->frequency; |
701672eb ML |
1073 | |
1074 | return 0; | |
1075 | } | |
1076 | ||
ab0b9fc6 | 1077 | static int xc2028_set_config(struct dvb_frontend *fe, void *priv_cfg) |
de3fe21b MCC |
1078 | { |
1079 | struct xc2028_data *priv = fe->tuner_priv; | |
1080 | struct xc2028_ctrl *p = priv_cfg; | |
0a196b6f | 1081 | int rc = 0; |
de3fe21b | 1082 | |
83fb340b | 1083 | tuner_dbg("%s called\n", __FUNCTION__); |
de3fe21b | 1084 | |
06fd82dc CP |
1085 | mutex_lock(&priv->lock); |
1086 | ||
0a196b6f CP |
1087 | kfree(priv->ctrl.fname); |
1088 | free_firmware(priv); | |
de3fe21b | 1089 | |
0a196b6f CP |
1090 | memcpy(&priv->ctrl, p, sizeof(priv->ctrl)); |
1091 | priv->ctrl.fname = NULL; | |
de3fe21b | 1092 | |
0a196b6f CP |
1093 | if (p->fname) { |
1094 | priv->ctrl.fname = kstrdup(p->fname, GFP_KERNEL); | |
1095 | if (priv->ctrl.fname == NULL) | |
1096 | rc = -ENOMEM; | |
de3fe21b MCC |
1097 | } |
1098 | ||
0a196b6f CP |
1099 | if (priv->ctrl.max_len < 9) |
1100 | priv->ctrl.max_len = 13; | |
352fae1d | 1101 | |
06fd82dc CP |
1102 | mutex_unlock(&priv->lock); |
1103 | ||
0a196b6f | 1104 | return rc; |
de3fe21b MCC |
1105 | } |
1106 | ||
215b95ba | 1107 | static const struct dvb_tuner_ops xc2028_dvb_tuner_ops = { |
701672eb | 1108 | .info = { |
ab0b9fc6 MCC |
1109 | .name = "Xceive XC3028", |
1110 | .frequency_min = 42000000, | |
1111 | .frequency_max = 864000000, | |
1112 | .frequency_step = 50000, | |
1113 | }, | |
701672eb | 1114 | |
de3fe21b | 1115 | .set_config = xc2028_set_config, |
00deff1a | 1116 | .set_analog_params = xc2028_set_analog_freq, |
215b95ba MCC |
1117 | .release = xc2028_dvb_release, |
1118 | .get_frequency = xc2028_get_frequency, | |
1119 | .get_rf_strength = xc2028_signal, | |
1120 | .set_params = xc2028_set_params, | |
45819c38 | 1121 | .sleep = xc2028_sleep, |
701672eb | 1122 | |
701672eb ML |
1123 | }; |
1124 | ||
a37b4c9b | 1125 | void *xc2028_attach(struct dvb_frontend *fe, struct xc2028_config *cfg) |
701672eb | 1126 | { |
215b95ba | 1127 | struct xc2028_data *priv; |
a37b4c9b | 1128 | void *video_dev; |
701672eb | 1129 | |
83fb340b | 1130 | if (debug) |
3157ecef | 1131 | printk(KERN_DEBUG PREFIX ": Xcv2028/3028 init called!\n"); |
701672eb | 1132 | |
71a2ee37 | 1133 | if (NULL == cfg || NULL == cfg->video_dev) |
a37b4c9b | 1134 | return NULL; |
215b95ba | 1135 | |
a37b4c9b | 1136 | if (!fe) { |
3157ecef | 1137 | printk(KERN_ERR PREFIX ": No frontend!\n"); |
a37b4c9b | 1138 | return NULL; |
215b95ba MCC |
1139 | } |
1140 | ||
a37b4c9b ML |
1141 | video_dev = cfg->video_dev; |
1142 | ||
aa501be9 CP |
1143 | mutex_lock(&xc2028_list_mutex); |
1144 | ||
215b95ba | 1145 | list_for_each_entry(priv, &xc2028_list, xc2028_list) { |
a37b4c9b ML |
1146 | if (priv->video_dev == cfg->video_dev) { |
1147 | video_dev = NULL; | |
1148 | break; | |
1149 | } | |
215b95ba MCC |
1150 | } |
1151 | ||
a37b4c9b | 1152 | if (video_dev) { |
215b95ba | 1153 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
aa501be9 CP |
1154 | if (priv == NULL) { |
1155 | mutex_unlock(&xc2028_list_mutex); | |
a37b4c9b | 1156 | return NULL; |
aa501be9 | 1157 | } |
3b20532c | 1158 | |
a37b4c9b ML |
1159 | priv->i2c_props.addr = cfg->i2c_addr; |
1160 | priv->i2c_props.adap = cfg->i2c_adap; | |
215b95ba | 1161 | priv->video_dev = video_dev; |
a37b4c9b | 1162 | priv->tuner_callback = cfg->callback; |
0a196b6f | 1163 | priv->ctrl.max_len = 13; |
de3fe21b | 1164 | |
215b95ba MCC |
1165 | mutex_init(&priv->lock); |
1166 | ||
ab0b9fc6 | 1167 | list_add_tail(&priv->xc2028_list, &xc2028_list); |
215b95ba | 1168 | } |
a37b4c9b ML |
1169 | |
1170 | fe->tuner_priv = priv; | |
1808a698 | 1171 | priv->count++; |
215b95ba MCC |
1172 | |
1173 | memcpy(&fe->ops.tuner_ops, &xc2028_dvb_tuner_ops, | |
ab0b9fc6 | 1174 | sizeof(xc2028_dvb_tuner_ops)); |
215b95ba MCC |
1175 | |
1176 | tuner_info("type set to %s\n", "XCeive xc2028/xc3028 tuner"); | |
1177 | ||
71a2ee37 MCC |
1178 | if (cfg->ctrl) |
1179 | xc2028_set_config(fe, cfg->ctrl); | |
1180 | ||
aa501be9 CP |
1181 | mutex_unlock(&xc2028_list_mutex); |
1182 | ||
a37b4c9b | 1183 | return fe; |
215b95ba | 1184 | } |
a37b4c9b | 1185 | |
701672eb ML |
1186 | EXPORT_SYMBOL(xc2028_attach); |
1187 | ||
215b95ba | 1188 | MODULE_DESCRIPTION("Xceive xc2028/xc3028 tuner driver"); |
983d214e | 1189 | MODULE_AUTHOR("Michel Ludwig <michel.ludwig@gmail.com>"); |
215b95ba MCC |
1190 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>"); |
1191 | MODULE_LICENSE("GPL"); |