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