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