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